2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * cede.c: Give a sector to a neighbor
29 * Known contributors to this file:
44 static int has_units_with_mob(coord, coord, natid);
45 static void cede_hdr(void);
46 static int cede_sect(struct nstr_sect *, natid);
47 static int cede_ship(struct nstr_item *, natid);
48 static void grab_sect(struct sctstr *, natid);
49 static void grab_ship(struct shpstr *, natid);
54 static int sct_or_shp[] = { EF_SECTOR, EF_SHIP, EF_BAD };
57 int is_sector = 0, is_ship = 0;
64 p = getstarg(player->argp[1], "Cede what? ", buf);
69 if (snxtitem(&ni, EF_SHIP, p, NULL))
71 if (!is_sector && !is_ship)
73 if ((n = natarg(player->argp[2], "to which country? ")) < 0)
75 if (is_sector && is_ship) {
78 p = 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 (relations_with(n, 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, 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));
234 putplane(pp->pln_uid, pp);
238 snxtitem_xy(&ni, EF_NUKE, sp->sct_x, sp->sct_y);
239 while (nxtitem(&ni, np)) {
240 if (np->nuk_own == 0)
243 wu(0, to, "\t%s ceded to you by %s\n",
244 prnuke(np), cname(player->cnum));
250 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
251 while (nxtitem(&ni, lp)) {
252 if (lp->lnd_own == 0)
254 if (lp->lnd_ship == 0)
256 if (lp->lnd_own != player->cnum)
259 wu(0, to, "\t%s ceded to you by %s\n",
260 prland(lp), cname(player->cnum));
269 if (sp->sct_oldown == to) {
270 sp->sct_che = 0; /* FIXME where do these guys go? */
271 sp->sct_che_target = 0;
275 if (sp->sct_oldown == to)
277 else /* people don't like being given away */
280 sp->sct_dist_x = sp->sct_x;
281 sp->sct_dist_y = sp->sct_y;
282 if (sp->sct_oldown == sp->sct_own)
289 cede_ship(struct nstr_item *ni, natid to)
294 struct nstr_item tni;
298 while (nxtitem(ni, &ship)) {
300 if (!player->owner || ship.shp_own == 0)
304 snxtitem_xy(&tni, EF_SHIP, ship.shp_x, ship.shp_y);
305 while (nxtitem(&tni, &tship) && bad)
306 if (tship.shp_own == to)
309 getsect(ship.shp_x, ship.shp_y, §);
310 if (bad && (sect.sct_own != to)) {
311 pr("%s isn't in a %s sector, and is not with a %s ship!\n",
312 prship(&ship), cname(to), cname(to));
315 grab_ship(&ship, to);
316 putship(ship.shp_uid, &ship);
318 wu(0, to, "%s ceded to you by %s\n",
319 prship(&ship), cname(player->cnum));
320 pr("%s ceded to %s\n", prship(&ship), cname(to));
322 pr(" %d ship%s\n", nships, splur(nships));
328 grab_ship(struct shpstr *sp, natid to)
337 snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
338 while (nxtitem(&ni, pp)) {
339 if (pp->pln_own == 0)
341 if (pp->pln_flags & PLN_LAUNCHED)
343 if (pp->pln_ship != sp->shp_uid)
345 if (pp->pln_own != player->cnum)
348 wu(0, to, "\t%s ceded to you by %s\n",
349 prplane(pp), cname(player->cnum));
353 putplane(pp->pln_uid, pp);
357 snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
358 while (nxtitem(&ni, lp)) {
359 if (lp->lnd_own == 0)
361 if (lp->lnd_ship != sp->shp_uid)
363 if (lp->lnd_own != player->cnum)
366 wu(0, to, "\t%s ceded to you by %s\n",
367 prland(lp), cname(player->cnum));