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