]> git.pond.sub.org Git - empserver/blob - src/lib/commands/cede.c
861cc1aae9d4864ee735da660c1166183add78d4
[empserver] / src / lib / commands / cede.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  cede.c: Give a sector to a neighbor
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare
32  *     Thomas Ruschak
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "xy.h"
38 #include "sect.h"
39 #include "nsc.h"
40 #include "nat.h"
41 #include "path.h"
42 #include "file.h"
43 #include "plane.h"
44 #include "land.h"
45 #include "ship.h"
46 #include "nuke.h"
47 #include "commands.h"
48
49 static void cede_hdr(void);
50 static int cede_sect(struct nstr_sect *, natid);
51 static int cede_ship(struct nstr_item *, natid);
52 static void grab_sect(struct sctstr *, natid);
53 static void grab_ship(struct shpstr *, natid);
54
55 int
56 cede(void)
57 {
58     natid to;
59     int n;
60     int is_sector = 0, is_ship = 0;
61     s_char *p;
62     struct nstr_sect ns;
63     struct nstr_item ni;
64     struct natstr *natp;
65     s_char buf[1024];
66
67     if ((p = getstarg(player->argp[1], "Cede what? ", buf)) == 0)
68         return RET_SYN;
69     if (snxtsct(&ns, p))
70         is_sector = 1;
71     if (snxtitem(&ni, EF_SHIP, p))
72         is_ship = 1;
73     if (!is_sector && !is_ship)
74         return RET_SYN;
75     if ((n = natarg(player->argp[2], "to which country? ")) < 0)
76         return RET_SYN;
77     if (is_sector && is_ship) {
78         int type;
79
80         if ((p =
81              getstarg(player->argp[3], "Cede sectors or ships (se, sh)? ",
82                       buf)) == 0)
83             return RET_FAIL;
84         if (strlen(p) > 4)
85             p[2] = 0;
86         type = ef_byname(p);
87
88         if (type == EF_SECTOR)
89             is_ship = 0;
90         else if (type == EF_SHIP)
91             is_sector = 0;
92         else {
93             pr("Please type 'se' or 'sh'!\n");
94             return RET_FAIL;
95         }
96     }
97
98
99     if (n == player->cnum) {
100         pr("Giving something to yourself?\n");
101         return RET_FAIL;
102     }
103     natp = getnatp(n);
104     if (natp->nat_stat & STAT_GOD) {
105         pr("You can only give to normal countries...\n");
106         return RET_FAIL;
107     }
108     if (getrel(natp, player->cnum) < FRIENDLY) {
109         pr("You can only cede to a country that is friendly towards you...\n");
110         return RET_FAIL;
111     }
112     to = n;
113
114     if (is_sector)
115         return cede_sect(&ns, to);
116     else
117         return cede_ship(&ni, to);
118 }
119
120 static int
121 cede_sect(struct nstr_sect *ns, natid to)
122 {
123     struct sctstr sect, osect;
124     int nsect;
125     int n, bad;
126     s_char dirstr[20];
127     int off_x, off_y;
128     struct nstr_item ni;
129     struct shpstr ship;
130
131     prdate();
132     for (n = 1; n <= 6; n++)
133         dirstr[n] = dirch[n];
134     dirstr[0] = '.';
135     dirstr[7] = '$';
136     dirstr[8] = '\0';
137     nsect = 0;
138     while (nxtsct(ns, &sect)) {
139         if (!player->owner)
140             continue;
141         if (sect.sct_mobil == 0) {
142             pr("%s has no mobility and cannot be ceded\n",
143                xyas(sect.sct_x, sect.sct_y, player->cnum));
144             continue;
145         }
146
147         bad = 1;
148         for (n = 1; n <= 6; n++) {      /* Directions */
149             off_x = sect.sct_x + diroff[n][0];
150             off_y = sect.sct_y + diroff[n][1];
151
152             if (!getsect(off_x, off_y, &osect))
153                 continue;
154             if ((osect.sct_own == to) && (osect.sct_mobil))
155                 bad = 0;
156             if ((osect.sct_own == to) &&
157                 has_units_with_mob(osect.sct_x, osect.sct_y, to))
158                 bad = 0;
159         }
160         snxtitem_all(&ni, EF_SHIP);
161         while (nxtitem(&ni, (s_char *)&ship)) {
162             if ((ship.shp_own == to) &&
163                 ((ship.shp_x == sect.sct_x) && (ship.shp_y == sect.sct_y)))
164                 bad = 0;
165         }
166         if (bad) {
167             pr("%s has no sector with mobility adjacent to or ship in %s!\n", cname(to), xyas(sect.sct_x, sect.sct_y, player->cnum));
168             continue;
169         }
170
171         if (nsect++ == 0)
172             cede_hdr();
173
174         grab_sect(&sect, to);
175         putsect(&sect);
176         pr("  %s %d%% ceded\n", xyas(sect.sct_x, sect.sct_y, player->cnum),
177            (int)sect.sct_effic);
178         wu(0, (natid)to, "%s ceded to you by %s\n",
179            xyas(sect.sct_x, sect.sct_y, to), cname(player->cnum));
180     }
181     pr("%d sector%s\n", nsect, splur(nsect));
182     return RET_OK;
183 }
184
185 static void
186 cede_hdr(void)
187 {
188     if (player->god)
189         pr("own ");
190     pr("  sect eff\n");
191 }
192
193
194
195 static void
196 grab_sect(struct sctstr *sp, natid to)
197 {
198     struct plnstr *pp;
199     struct lndstr *lp;
200     struct nukstr *np;
201     struct nstr_item ni;
202     struct plnstr p;
203     struct lndstr l;
204     struct nukstr nuk;
205
206     /* Wipe all the distribution info */
207     memset(sp->sct_dist, 0, sizeof(sp->sct_dist));
208     memset(sp->sct_del, 0, sizeof(sp->sct_del));
209     sp->sct_dist_x = sp->sct_x;
210     sp->sct_dist_y = sp->sct_y;
211
212     pp = &p;
213     snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
214     while (nxtitem(&ni, pp)) {
215         if (pp->pln_own == 0)
216             continue;
217         if (pp->pln_ship >= 0)
218             continue;
219         if (pp->pln_own != player->cnum)
220             continue;
221         if (pp->pln_flags & PLN_LAUNCHED)
222             continue;
223
224         wu(0, to, "\t%s ceded to you by %s\n",
225            prplane(pp), cname(player->cnum));
226         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
227         pp->pln_own = to;
228         makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
229                     pp->pln_y);
230         pp->pln_mobil = 0;
231         pp->pln_mission = 0;
232         putplane(pp->pln_uid, pp);
233     }
234
235     np = &nuk;
236     snxtitem_xy(&ni, EF_NUKE, sp->sct_x, sp->sct_y);
237     while (nxtitem(&ni, np)) {
238         if (np->nuk_own == 0)
239             continue;
240
241         wu(0, to, "\tnuclear stockpile #%d ceded to you by %s\n",
242            np->nuk_uid, cname(player->cnum));
243         makelost(EF_NUKE, np->nuk_own, np->nuk_uid, np->nuk_x, np->nuk_y);
244         np->nuk_own = to;
245         makenotlost(EF_NUKE, np->nuk_own, np->nuk_uid, np->nuk_x,
246                     np->nuk_y);
247         putnuke(ni.cur, np);
248     }
249
250     lp = &l;
251     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
252     while (nxtitem(&ni, lp)) {
253         if (lp->lnd_own == 0)
254             continue;
255         if (lp->lnd_ship == 0)
256             continue;
257         if (lp->lnd_own != player->cnum)
258             continue;
259
260         wu(0, to, "\t%s ceded to you by %s\n", prland(lp),
261            cname(player->cnum));
262         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
263         makenotlost(EF_LAND, to, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
264         lp->lnd_own = to;
265         lp->lnd_mobil = 0;
266         lp->lnd_mission = 0;
267         putland(ni.cur, lp);
268     }
269
270     sp->sct_avail = 0;
271
272     if (sp->sct_oldown == to) {
273         sp->sct_che = 0;        /* FIXME where do these guys go? */
274         sp->sct_che_target = 0;
275         sp->sct_loyal = 0;
276     }
277
278     if (sp->sct_oldown == to)
279         sp->sct_loyal = 0;
280     else                        /* people don't like being given away */
281         sp->sct_loyal = 50;
282
283     sp->sct_dist_x = sp->sct_x;
284     sp->sct_dist_y = sp->sct_y;
285     makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
286     makenotlost(EF_SECTOR, to, 0, sp->sct_x, sp->sct_y);
287     if (sp->sct_oldown == sp->sct_own)
288         sp->sct_oldown = to;
289     sp->sct_own = to;
290     sp->sct_mobil = 0;
291 }
292
293 static int
294 cede_ship(struct nstr_item *ni, natid to)
295 {
296     struct shpstr ship;
297     struct shpstr tship;
298     struct sctstr sect;
299     struct nstr_item tni;
300     int nships = 0;
301     int bad = 0;
302
303     while (nxtitem(ni, (s_char *)&ship)) {
304
305         if (!player->owner || ship.shp_own == 0)
306             continue;
307
308         bad = 1;
309         snxtitem_xy(&tni, EF_SHIP, ship.shp_x, ship.shp_y);
310         while (nxtitem(&tni, (s_char *)&tship) && bad)
311             if (tship.shp_own == to)
312                 bad = 0;
313
314         getsect(ship.shp_x, ship.shp_y, &sect);
315         if (bad && (sect.sct_own != to)) {
316             pr("%s isn't in a %s sector, and is not with a %s ship!\n",
317                prship(&ship), cname(to), cname(to));
318             continue;
319         }
320         grab_ship(&ship, to);
321         putship(ship.shp_uid, &ship);
322         nships++;
323         wu(0, to, "%s ceded to you by %s\n",
324            prship(&ship), cname(player->cnum));
325         pr("%s ceded to %s\n", prship(&ship), cname(to));
326     }
327     pr("    %d ship%s\n", nships, splur(nships));
328
329     return RET_OK;
330 }
331
332 static void
333 grab_ship(struct shpstr *sp, natid to)
334 {
335     struct plnstr *pp;
336     struct lndstr *lp;
337     struct nstr_item ni;
338     struct plnstr p;
339     struct lndstr l;
340
341     pp = &p;
342     snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
343     while (nxtitem(&ni, pp)) {
344         if (pp->pln_own == 0)
345             continue;
346         if (pp->pln_flags & PLN_LAUNCHED)
347             continue;
348         if (pp->pln_ship != sp->shp_uid)
349             continue;
350         if (pp->pln_own != player->cnum)
351             continue;
352
353         wu(0, to, "\t%s ceded to you by %s\n",
354            prplane(pp), cname(player->cnum));
355         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
356         pp->pln_own = to;
357         makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
358                     pp->pln_y);
359         pp->pln_mobil = 0;
360         pp->pln_mission = 0;
361         putplane(pp->pln_uid, pp);
362     }
363
364     lp = &l;
365     snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
366     while (nxtitem(&ni, lp)) {
367         if (lp->lnd_own == 0)
368             continue;
369         if (lp->lnd_ship != sp->shp_uid)
370             continue;
371         if (lp->lnd_own != player->cnum)
372             continue;
373
374         wu(0, to, "\t%s ceded to you by %s\n",
375            prland(lp), cname(player->cnum));
376         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
377         makenotlost(EF_LAND, to, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
378         lp->lnd_own = to;
379         lp->lnd_mobil = 0;
380         lp->lnd_mission = 0;
381         putland(ni.cur, lp);
382     }
383
384     makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
385     sp->shp_own = to;
386     makenotlost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
387 }