]> git.pond.sub.org Git - empserver/blob - src/lib/commands/cede.c
50a7d74f92e50d7c9955667f1b57dc5517263950
[empserver] / src / lib / commands / cede.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "var.h"
38 #include "xy.h"
39 #include "sect.h"
40 #include "nsc.h"
41 #include "nat.h"
42 #include "path.h"
43 #include "file.h"
44 #include "plane.h"
45 #include "land.h"
46 #include "ship.h"
47 #include "nuke.h"
48 #include "item.h"
49 #include "commands.h"
50
51 static void cede_hdr(void);
52 static int cede_sect(struct nstr_sect *, natid);
53 static int cede_ship(struct nstr_item *, natid);
54 static void grab_sect(register struct sctstr *, natid);
55 static void grab_ship(register struct shpstr *, natid);
56
57 int
58 cede(void)
59 {
60     natid to;
61     int n;
62     int is_sector = 0, is_ship = 0;
63     s_char *p;
64     struct nstr_sect ns;
65     struct nstr_item ni;
66     struct natstr *natp;
67     s_char buf[1024];
68
69     if ((p = getstarg(player->argp[1], "Cede what? ", buf)) == 0)
70         return RET_SYN;
71     if (snxtsct(&ns, p))
72         is_sector = 1;
73     if (snxtitem(&ni, EF_SHIP, p))
74         is_ship = 1;
75     if (!is_sector && !is_ship)
76         return RET_SYN;
77     if ((n = natarg(player->argp[2], "to which country? ")) < 0)
78         return RET_SYN;
79     if (is_sector && is_ship) {
80         int type;
81
82         if ((p =
83              getstarg(player->argp[3], "Cede sectors or ships (se, sh)? ",
84                       buf)) == 0)
85             return RET_FAIL;
86         if (strlen(p) > 4)
87             p[2] = 0;
88         type = ef_byname(p);
89
90         if (type == EF_SECTOR)
91             is_ship = 0;
92         else if (type == EF_SHIP)
93             is_sector = 0;
94         else {
95             pr("Please type 'se' or 'sh'!\n");
96             return RET_FAIL;
97         }
98     }
99
100
101     if (n == player->cnum) {
102         pr("Giving something to yourself?\n");
103         return RET_FAIL;
104     }
105     natp = getnatp(n);
106     if (natp->nat_stat & STAT_GOD) {
107         pr("You can only give to normal countries...\n");
108         return RET_FAIL;
109     }
110     if (getrel(natp, player->cnum) < FRIENDLY) {
111         pr("You can only cede to a country that is friendly towards you...\n");
112         return RET_FAIL;
113     }
114     to = n;
115
116     if (is_sector)
117         return cede_sect(&ns, to);
118     else
119         return cede_ship(&ni, to);
120 }
121
122 static int
123 cede_sect(struct nstr_sect *ns, natid to)
124 {
125     struct sctstr sect, osect;
126     int nsect;
127     int n, bad;
128     s_char dirstr[20];
129     int off_x, off_y;
130     struct nstr_item ni;
131     struct shpstr ship;
132
133     prdate();
134     for (n = 1; n <= 6; n++)
135         dirstr[n] = dirch[n];
136     dirstr[0] = '.';
137     dirstr[7] = '$';
138     dirstr[8] = '\0';
139     nsect = 0;
140     while (nxtsct(ns, &sect)) {
141         if (!player->owner)
142             continue;
143         if (sect.sct_mobil == 0) {
144             pr("%s has no mobility and cannot be ceded\n",
145                xyas(sect.sct_x, sect.sct_y, player->cnum));
146             continue;
147         }
148
149         bad = 1;
150         for (n = 1; n <= 6; n++) {      /* Directions */
151             off_x = sect.sct_x + diroff[n][0];
152             off_y = sect.sct_y + diroff[n][1];
153
154             if (!getsect(off_x, off_y, &osect))
155                 continue;
156             if ((osect.sct_own == to) && (osect.sct_mobil))
157                 bad = 0;
158             if ((osect.sct_own == to) &&
159                 has_units_with_mob(osect.sct_x, osect.sct_y, to))
160                 bad = 0;
161         }
162         snxtitem_all(&ni, EF_SHIP);
163         while (nxtitem(&ni, (s_char *)&ship)) {
164             if ((ship.shp_own == to) &&
165                 ((ship.shp_x == sect.sct_x) && (ship.shp_y == sect.sct_y)))
166                 bad = 0;
167         }
168         if (bad) {
169             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));
170             continue;
171         }
172
173         if (nsect++ == 0)
174             cede_hdr();
175
176         grab_sect(&sect, to);
177         putsect(&sect);
178         pr("  %s %d%% ceded\n", xyas(sect.sct_x, sect.sct_y, player->cnum),
179            (int)sect.sct_effic);
180         wu(0, (natid)to, "%s ceded to you by %s\n",
181            xyas(sect.sct_x, sect.sct_y, to), cname(player->cnum));
182     }
183     pr("%d sector%s\n", nsect, splur(nsect));
184     return RET_OK;
185 }
186
187 static void
188 cede_hdr(void)
189 {
190     if (player->god)
191         pr("own ");
192     pr("  sect eff\n");
193 }
194
195
196
197 static void
198 grab_sect(register struct sctstr *sp, natid to)
199 {
200     struct plnstr *pp;
201     struct lndstr *lp;
202     struct nukstr *np;
203     int oldche;
204     struct nstr_item ni;
205     struct plnstr p;
206     struct lndstr l;
207     struct nukstr nuk;
208
209     /* Wipe all the distribution info */
210     memset(sp->sct_dist, 0, sizeof(sp->sct_dist));
211     memset(sp->sct_del, 0, sizeof(sp->sct_del));
212     sp->sct_dist_x = sp->sct_x;
213     sp->sct_dist_y = sp->sct_y;
214
215     pp = &p;
216     snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
217     while (nxtitem(&ni, (caddr_t)pp)) {
218         if (pp->pln_own == 0)
219             continue;
220         if (pp->pln_ship >= 0)
221             continue;
222         if (pp->pln_own != player->cnum)
223             continue;
224         if (pp->pln_flags & PLN_LAUNCHED)
225             continue;
226
227         wu(0, to, "\t%s ceded to you by %s\n",
228            prplane(pp), cname(player->cnum));
229         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
230         pp->pln_own = to;
231         makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
232                     pp->pln_y);
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, (caddr_t)np)) {
241         if (np->nuk_own == 0)
242             continue;
243
244         wu(0, to, "\tnuclear stockpile #%d ceded to you by %s\n",
245            np->nuk_uid, cname(player->cnum));
246         makelost(EF_NUKE, np->nuk_own, np->nuk_uid, np->nuk_x, np->nuk_y);
247         np->nuk_own = to;
248         makenotlost(EF_NUKE, np->nuk_own, np->nuk_uid, np->nuk_x,
249                     np->nuk_y);
250         putnuke(ni.cur, np);
251     }
252
253     lp = &l;
254     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
255     while (nxtitem(&ni, (caddr_t)lp)) {
256         if (lp->lnd_own == 0)
257             continue;
258         if (lp->lnd_ship == 0)
259             continue;
260         if (lp->lnd_own != player->cnum)
261             continue;
262
263         wu(0, to, "\t%s ceded to you by %s\n", prland(lp),
264            cname(player->cnum));
265         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
266         makenotlost(EF_LAND, to, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
267         lp->lnd_own = to;
268         lp->lnd_mobil = 0;
269         lp->lnd_mission = 0;
270         putland(ni.cur, lp);
271     }
272
273     sp->sct_avail = 0;
274
275     if (sp->sct_oldown == to) {
276         oldche = get_che_value(sp->sct_che);
277         set_che_value(oldche, 0);
278         set_che_cnum(oldche, 0);
279         sp->sct_che = oldche;
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, (s_char *)&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, (s_char *)&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(register struct shpstr *sp, natid to)
339 {
340     register struct plnstr *pp;
341     register 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, (caddr_t)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, pp->pln_x,
363                     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, (caddr_t)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 }