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