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
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.
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.
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
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.
28 * cede.c: Give a sector to a neighbor
30 * Known contributors to this file:
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);
55 static int sct_or_shp[] = { EF_SECTOR, EF_SHIP, EF_BAD };
58 int is_sector = 0, is_ship = 0;
65 if ((p = getstarg(player->argp[1], "Cede what? ", buf)) == 0)
69 if (snxtitem(&ni, EF_SHIP, p))
71 if (!is_sector && !is_ship)
73 if ((n = natarg(player->argp[2], "to which country? ")) < 0)
75 if (is_sector && is_ship) {
79 getstarg(player->argp[3], "Cede sectors or ships (se, sh)? ",
84 type = ef_byname_from(p, sct_or_shp);
86 pr("Please type 'se' or 'sh'!\n");
92 if (n == player->cnum) {
93 pr("Giving something to yourself?\n");
97 if (natp->nat_stat != STAT_ACTIVE) {
98 pr("You can only give to normal countries...\n");
101 if (getrel(natp, player->cnum) < FRIENDLY) {
102 pr("You can only cede to a country that is friendly towards you...\n");
108 return cede_sect(&ns, to);
110 return cede_ship(&ni, to);
114 cede_sect(struct nstr_sect *ns, natid to)
116 struct sctstr sect, osect;
125 while (nxtsct(ns, §)) {
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));
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];
139 if (!getsect(off_x, off_y, &osect))
141 if ((osect.sct_own == to) && (osect.sct_mobil))
143 if ((osect.sct_own == to) &&
144 has_units_with_mob(osect.sct_x, osect.sct_y, to))
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)))
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));
162 grab_sect(§, to);
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));
169 pr("%d sector%s\n", nsect, splur(nsect));
174 has_units_with_mob(coord x, coord y, natid cn)
179 snxtitem_xy(&ni, EF_LAND, x, y);
180 while (nxtitem(&ni, &land)) {
181 if (land.lnd_own != cn)
183 if (land.lnd_mobil > 0)
201 grab_sect(struct sctstr *sp, natid to)
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;
218 snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
219 while (nxtitem(&ni, pp)) {
220 if (pp->pln_own == 0)
222 if (pp->pln_ship >= 0)
224 if (pp->pln_own != player->cnum)
226 if (pp->pln_flags & PLN_LAUNCHED)
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);
233 makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid,
234 pp->pln_x, pp->pln_y);
237 putplane(pp->pln_uid, pp);
241 snxtitem_xy(&ni, EF_NUKE, sp->sct_x, sp->sct_y);
242 while (nxtitem(&ni, np)) {
243 if (np->nuk_own == 0)
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);
250 makenotlost(EF_NUKE, np->nuk_own, np->nuk_uid,
251 np->nuk_x, np->nuk_y);
256 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
257 while (nxtitem(&ni, lp)) {
258 if (lp->lnd_own == 0)
260 if (lp->lnd_ship == 0)
262 if (lp->lnd_own != player->cnum)
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);
277 if (sp->sct_oldown == to) {
278 sp->sct_che = 0; /* FIXME where do these guys go? */
279 sp->sct_che_target = 0;
283 if (sp->sct_oldown == to)
285 else /* people don't like being given away */
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)
299 cede_ship(struct nstr_item *ni, natid to)
304 struct nstr_item tni;
308 while (nxtitem(ni, &ship)) {
310 if (!player->owner || ship.shp_own == 0)
314 snxtitem_xy(&tni, EF_SHIP, ship.shp_x, ship.shp_y);
315 while (nxtitem(&tni, &tship) && bad)
316 if (tship.shp_own == to)
319 getsect(ship.shp_x, ship.shp_y, §);
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));
325 grab_ship(&ship, to);
326 putship(ship.shp_uid, &ship);
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));
332 pr(" %d ship%s\n", nships, splur(nships));
338 grab_ship(struct shpstr *sp, natid to)
347 snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
348 while (nxtitem(&ni, pp)) {
349 if (pp->pln_own == 0)
351 if (pp->pln_flags & PLN_LAUNCHED)
353 if (pp->pln_ship != sp->shp_uid)
355 if (pp->pln_own != player->cnum)
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);
362 makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid,
363 pp->pln_x, pp->pln_y);
366 putplane(pp->pln_uid, pp);
370 snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
371 while (nxtitem(&ni, lp)) {
372 if (lp->lnd_own == 0)
374 if (lp->lnd_ship != sp->shp_uid)
376 if (lp->lnd_own != player->cnum)
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);
389 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
391 makenotlost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);