]> git.pond.sub.org Git - empserver/blob - src/lib/commands/cede.c
Get rid of src/lib/common/land.c
[empserver] / src / lib / commands / cede.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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     if ((p = getstarg(player->argp[1], "Cede what? ", buf)) == 0)
66         return RET_SYN;
67     if (snxtsct(&ns, p))
68         is_sector = 1;
69     if (snxtitem(&ni, EF_SHIP, p))
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         if ((p =
79              getstarg(player->argp[3], "Cede sectors or ships (se, sh)? ",
80                       buf)) == 0)
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 (getrel(natp, 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, (natid)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         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
232         pp->pln_own = to;
233         makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid,
234                     pp->pln_x, pp->pln_y);
235         pp->pln_mobil = 0;
236         pp->pln_mission = 0;
237         putplane(pp->pln_uid, pp);
238     }
239
240     np = &nuk;
241     snxtitem_xy(&ni, EF_NUKE, sp->sct_x, sp->sct_y);
242     while (nxtitem(&ni, np)) {
243         if (np->nuk_own == 0)
244             continue;
245
246         wu(0, to, "\t%s ceded to you by %s\n",
247            prnuke(np), cname(player->cnum));
248         makelost(EF_NUKE, np->nuk_own, np->nuk_uid, np->nuk_x, np->nuk_y);
249         np->nuk_own = to;
250         makenotlost(EF_NUKE, np->nuk_own, np->nuk_uid,
251                     np->nuk_x, np->nuk_y);
252         putnuke(ni.cur, np);
253     }
254
255     lp = &l;
256     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
257     while (nxtitem(&ni, lp)) {
258         if (lp->lnd_own == 0)
259             continue;
260         if (lp->lnd_ship == 0)
261             continue;
262         if (lp->lnd_own != player->cnum)
263             continue;
264
265         wu(0, to, "\t%s ceded to you by %s\n",
266            prland(lp), cname(player->cnum));
267         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
268         makenotlost(EF_LAND, to, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
269         lp->lnd_own = to;
270         lp->lnd_mobil = 0;
271         lp->lnd_mission = 0;
272         putland(ni.cur, lp);
273     }
274
275     sp->sct_avail = 0;
276
277     if (sp->sct_oldown == to) {
278         sp->sct_che = 0;        /* FIXME where do these guys go? */
279         sp->sct_che_target = 0;
280         sp->sct_loyal = 0;
281     }
282
283     if (sp->sct_oldown == to)
284         sp->sct_loyal = 0;
285     else                        /* people don't like being given away */
286         sp->sct_loyal = 50;
287
288     sp->sct_dist_x = sp->sct_x;
289     sp->sct_dist_y = sp->sct_y;
290     makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
291     makenotlost(EF_SECTOR, to, 0, sp->sct_x, sp->sct_y);
292     if (sp->sct_oldown == sp->sct_own)
293         sp->sct_oldown = to;
294     sp->sct_own = to;
295     sp->sct_mobil = 0;
296 }
297
298 static int
299 cede_ship(struct nstr_item *ni, natid to)
300 {
301     struct shpstr ship;
302     struct shpstr tship;
303     struct sctstr sect;
304     struct nstr_item tni;
305     int nships = 0;
306     int bad = 0;
307
308     while (nxtitem(ni, &ship)) {
309
310         if (!player->owner || ship.shp_own == 0)
311             continue;
312
313         bad = 1;
314         snxtitem_xy(&tni, EF_SHIP, ship.shp_x, ship.shp_y);
315         while (nxtitem(&tni, &tship) && bad)
316             if (tship.shp_own == to)
317                 bad = 0;
318
319         getsect(ship.shp_x, ship.shp_y, &sect);
320         if (bad && (sect.sct_own != to)) {
321             pr("%s isn't in a %s sector, and is not with a %s ship!\n",
322                prship(&ship), cname(to), cname(to));
323             continue;
324         }
325         grab_ship(&ship, to);
326         putship(ship.shp_uid, &ship);
327         nships++;
328         wu(0, to, "%s ceded to you by %s\n",
329            prship(&ship), cname(player->cnum));
330         pr("%s ceded to %s\n", prship(&ship), cname(to));
331     }
332     pr("    %d ship%s\n", nships, splur(nships));
333
334     return RET_OK;
335 }
336
337 static void
338 grab_ship(struct shpstr *sp, natid to)
339 {
340     struct plnstr *pp;
341     struct lndstr *lp;
342     struct nstr_item ni;
343     struct plnstr p;
344     struct lndstr l;
345
346     pp = &p;
347     snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
348     while (nxtitem(&ni, pp)) {
349         if (pp->pln_own == 0)
350             continue;
351         if (pp->pln_flags & PLN_LAUNCHED)
352             continue;
353         if (pp->pln_ship != sp->shp_uid)
354             continue;
355         if (pp->pln_own != player->cnum)
356             continue;
357
358         wu(0, to, "\t%s ceded to you by %s\n",
359            prplane(pp), cname(player->cnum));
360         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
361         pp->pln_own = to;
362         makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid,
363                     pp->pln_x, pp->pln_y);
364         pp->pln_mobil = 0;
365         pp->pln_mission = 0;
366         putplane(pp->pln_uid, pp);
367     }
368
369     lp = &l;
370     snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
371     while (nxtitem(&ni, lp)) {
372         if (lp->lnd_own == 0)
373             continue;
374         if (lp->lnd_ship != sp->shp_uid)
375             continue;
376         if (lp->lnd_own != player->cnum)
377             continue;
378
379         wu(0, to, "\t%s ceded to you by %s\n",
380            prland(lp), cname(player->cnum));
381         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
382         makenotlost(EF_LAND, to, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
383         lp->lnd_own = to;
384         lp->lnd_mobil = 0;
385         lp->lnd_mission = 0;
386         putland(ni.cur, lp);
387     }
388
389     makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
390     sp->shp_own = to;
391     makenotlost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
392 }