]> git.pond.sub.org Git - empserver/blob - src/lib/commands/cede.c
Import of Empire 4.2.12
[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 "deity.h"
43 #include "path.h"
44 #include "file.h"
45 #include "plane.h"
46 #include "land.h"
47 #include "ship.h"
48 #include "nuke.h"
49 #include "item.h"
50 #include "commands.h"
51
52 static  void cede_hdr(void);
53
54 int
55 cede(void)
56 {
57         natid   to;
58         int     n;
59         int     is_sector=0,is_ship=0;
60         s_char  *p;
61         struct  nstr_sect ns;
62         struct  nstr_item ni;
63         struct  natstr *natp;
64         s_char  buf[1024];
65
66         if ((p = getstarg(player->argp[1], "Cede what? ", buf)) == 0)
67                 return RET_SYN;
68         if (snxtsct(&ns, p))
69                 is_sector = 1;
70         if (snxtitem(&ni, EF_SHIP, p))
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                 if ((p = getstarg(player->argp[3], "Cede sectors or ships (se, sh)? ", buf)) == 0)
80                         return RET_FAIL;
81                 if (strlen(p) > 4)
82                         p[2] = 0;
83                 type = ef_byname(p);
84
85                 if (type == EF_SECTOR)
86                         is_ship = 0;
87                 else if (type == EF_SHIP)
88                         is_sector = 0;
89                 else{
90                         pr("Please type 'se' or 'sh'!\n");
91                         return RET_FAIL;
92                 }
93         }
94
95
96         if (n == player->cnum){
97                 pr("Giving something to yourself?\n");
98                 return RET_FAIL;
99         }
100         natp = getnatp(n);
101         if (natp->nat_stat & STAT_GOD){
102                 pr("You can only give to normal countries...\n");
103                 return RET_FAIL;
104         }
105         if (getrel(natp, player->cnum) < FRIENDLY) {
106                 pr("You can only cede to a country that is friendly towards you...\n");
107                 return RET_FAIL;
108         }
109         to = n;
110
111         if (is_sector)
112                 return cede_sect(&ns,to);
113         else
114                 return cede_ship(&ni,to);
115 }
116
117 int
118 cede_sect(struct nstr_sect *ns, natid to)
119 {
120         struct  sctstr sect, osect;
121         int     nsect;
122         int     n, bad;
123         s_char  dirstr[20];
124         int     off_x, off_y;
125         struct  nstr_item ni;
126         struct  shpstr ship;
127
128         prdate();
129         for (n = 1; n <= 6; n++)
130                 dirstr[n] = dirch[n];
131         dirstr[0] = '.';
132         dirstr[7] = '$';
133         dirstr[8] = '\0';
134         nsect = 0;
135         while (nxtsct(ns, &sect)) {
136                 if (!player->owner)
137                         continue;
138                 if (sect.sct_mobil == 0){
139                         pr("%s has no mobility and cannot be ceded\n",
140                                 xyas(sect.sct_x,sect.sct_y,player->cnum));
141                         continue;
142                 }
143
144                 bad = 1;
145                 for (n = 1; n <= 6; n++) {      /* Directions */
146                         off_x = sect.sct_x + diroff[n][0];
147                         off_y = sect.sct_y + diroff[n][1];
148
149                         if (!getsect(off_x, off_y, &osect))
150                                 continue;
151                         if ((osect.sct_own == to) && (osect.sct_mobil))
152                                 bad=0;
153                         if ((osect.sct_own == to) &&
154                                 has_units_with_mob(osect.sct_x, osect.sct_y,to))
155                                 bad=0;
156                 }
157                 snxtitem_all(&ni,EF_SHIP);
158                 while (nxtitem(&ni, (s_char *)&ship)){
159                         if ((ship.shp_own == to) &&
160                                 ((ship.shp_x == sect.sct_x) &&
161                                 (ship.shp_y == sect.sct_y)))
162                                 bad = 0;
163                 }
164                 if (bad){
165                         pr("%s has no sector with mobility adjacent to or ship in %s!\n",
166                                 cname(to), xyas(sect.sct_x,sect.sct_y,player->cnum));
167                         continue;
168                 }
169
170                 if (nsect++ == 0)
171                         cede_hdr();
172
173                 grab_sect(&sect,to);
174                 putsect(&sect);
175                 pr("  %s %d%% ceded\n",xyas(sect.sct_x,sect.sct_y,player->cnum),
176                         (int)sect.sct_effic);
177                 wu(0,(natid)to,"%s ceded to you by %s\n",
178                         xyas(sect.sct_x,sect.sct_y,to), cname(player->cnum));
179         }
180         pr("%d sector%s\n", nsect, splur(nsect));
181         return RET_OK;
182 }
183
184 static void
185 cede_hdr(void)
186 {
187         if (player->god)
188                 pr("own ");
189         pr("  sect eff\n");
190 }
191
192
193
194 void
195 grab_sect(register struct sctstr *sp, natid to)
196 {
197         struct  plnstr *pp;
198         struct  lndstr *lp;
199         struct  nukstr *np;
200         int     oldche;
201         struct  nstr_item ni;
202         struct  plnstr p;
203         struct  lndstr l;
204         struct  nukstr nuk;
205         int     vec[I_MAX+1];
206
207         /* Wipe all the distribution info */
208         bzero((s_char *)vec, sizeof(vec));
209         putvec(VT_DIST, vec, (s_char *)sp, EF_SECTOR);
210         putvec(VT_DEL, vec, (s_char *)sp, EF_SECTOR);
211         sp->sct_dist_x = sp->sct_x;
212         sp->sct_dist_y = sp->sct_y;
213
214         pp = &p;
215         snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
216         while (nxtitem(&ni, (caddr_t)pp)) {
217                 if (pp->pln_own == 0)
218                         continue;
219                 if (pp->pln_ship >= 0)
220                         continue;
221                 if (pp->pln_own != player->cnum)
222                         continue;
223                 if (pp->pln_flags & PLN_LAUNCHED)
224                         continue;
225
226                 wu(0,to,"\t%s ceded to you by %s\n",
227                    prplane(pp),
228                    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, pp->pln_y);
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, (caddr_t)np)) {
240                 if (np->nuk_own == 0)
241                         continue;
242
243                 wu(0,to,"\tnuclear stockpile #%d ceded to you by %s\n",
244                         np->nuk_uid, cname(player->cnum));
245                 makelost(EF_NUKE, np->nuk_own, np->nuk_uid, np->nuk_x, np->nuk_y);
246                 np->nuk_own = to;
247                 makenotlost(EF_NUKE, np->nuk_own, np->nuk_uid, np->nuk_x, np->nuk_y);
248                 putnuke(ni.cur, np);
249         }
250
251         lp = &l;
252         snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
253         while (nxtitem(&ni, (caddr_t)lp)) {
254                 if (lp->lnd_own == 0)
255                         continue;
256                 if (lp->lnd_ship == 0)
257                         continue;
258                 if (lp->lnd_own != player->cnum)
259                         continue;
260
261                 wu(0,to,"\t%s ceded to you by %s\n", prland(lp),
262                    cname(player->cnum));
263                 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
264                 makenotlost(EF_LAND, to, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
265                 lp->lnd_own = to;
266                 lp->lnd_mobil = 0;
267                 lp->lnd_mission = 0;
268                 putland(ni.cur, lp);
269         }
270
271         sp->sct_avail = 0;
272
273         if (sp->sct_oldown == to) {
274                 oldche = get_che_value(getvar(V_CHE, (s_char *)sp, EF_SECTOR));
275                 set_che_value(oldche, 0);
276                 set_che_cnum(oldche, 0);
277                 (void) putvar(V_CHE, oldche, (s_char *)sp, EF_SECTOR);
278                 sp->sct_loyal = 0;
279         }
280
281         if (sp->sct_oldown == to)
282                 sp->sct_loyal = 0;
283         else /* people don't like being given away */
284                 sp->sct_loyal = 50;
285
286         sp->sct_dist_x = sp->sct_x;
287         sp->sct_dist_y = sp->sct_y;
288         makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
289         makenotlost(EF_SECTOR, to, 0, sp->sct_x, sp->sct_y);
290         if (sp->sct_oldown == sp->sct_own)
291                 sp->sct_oldown = to;
292         sp->sct_own = to;
293         sp->sct_mobil = 0;
294 }
295
296 int
297 cede_ship(struct nstr_item *ni, natid to)
298 {
299         struct  shpstr ship;
300         struct  shpstr tship;
301         struct  sctstr sect;
302         struct  nstr_item tni;
303         int     nships=0;
304         int     bad=0;
305         
306         while (nxtitem(ni, (s_char *)&ship)) {
307
308                 if (!player->owner || ship.shp_own == 0)
309                         continue;
310
311                 bad = 1;
312                 snxtitem_xy(&tni,EF_SHIP,ship.shp_x,ship.shp_y);
313                 while (nxtitem(&tni, (s_char *)&tship) && bad)
314                         if (tship.shp_own == to)
315                                 bad = 0;
316
317                 getsect(ship.shp_x,ship.shp_y,&sect);
318                 if (bad && (sect.sct_own != to)){
319                         pr("%s isn't in a %s sector, and is not with a %s ship!\n", prship(&ship),
320                                 cname(to), cname(to));
321                         continue;
322                 }
323                 grab_ship(&ship,to);
324                 putship(ship.shp_uid,&ship);
325                 nships++;
326                 wu(0,to,"%s ceded to you by %s\n",
327                    prship(&ship),
328                    cname(player->cnum));
329                 pr("%s ceded to %s\n",
330                    prship(&ship),
331                    cname(to));
332         }
333         pr("    %d ship%s\n", nships, splur(nships));
334
335         return RET_OK;
336 }
337
338 void
339 grab_ship(register struct shpstr *sp, natid to)
340 {
341         register struct plnstr *pp;
342         register struct lndstr *lp;
343         struct  nstr_item ni;
344         struct  plnstr p;
345         struct  lndstr l;
346
347         pp = &p;
348         snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
349         while (nxtitem(&ni, (caddr_t)pp)) {
350                 if (pp->pln_own == 0)
351                         continue;
352                 if (pp->pln_flags & PLN_LAUNCHED)
353                         continue;
354                 if (pp->pln_ship != sp->shp_uid)
355                         continue;
356                 if (pp->pln_own != player->cnum)
357                         continue;
358
359                 wu(0,to,"\t%s ceded to you by %s\n",
360                    prplane(pp),
361                    cname(player->cnum));
362                 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
363                 pp->pln_own = to;
364                 makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
365                 pp->pln_mobil = 0;
366                 pp->pln_mission = 0;
367                 putplane(pp->pln_uid, pp);
368         }
369
370         lp = &l;
371         snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
372         while (nxtitem(&ni, (caddr_t)lp)) {
373                 if (lp->lnd_own == 0)
374                         continue;
375                 if (lp->lnd_ship != sp->shp_uid)
376                         continue;
377                 if (lp->lnd_own != player->cnum)
378                         continue;
379
380                 wu(0,to,"\t%s ceded to you by %s\n",
381                    prland(lp), cname(player->cnum));
382                 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
383                 makenotlost(EF_LAND, to, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
384                 lp->lnd_own = to;
385                 lp->lnd_mobil = 0;
386                 lp->lnd_mission = 0;
387                 putland(ni.cur, lp);
388         }
389
390         makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
391         sp->shp_own = to;
392         makenotlost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
393 }