2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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 p = getstarg(player->argp[1], "Cede what? ", buf);
70 if (snxtitem(&ni, EF_SHIP, p, NULL))
72 if (!is_sector && !is_ship)
74 if ((n = natarg(player->argp[2], "to which country? ")) < 0)
76 if (is_sector && is_ship) {
79 p = getstarg(player->argp[3], "Cede sectors or ships (se, sh)? ",
85 type = ef_byname_from(p, sct_or_shp);
87 pr("Please type 'se' or 'sh'!\n");
93 if (n == player->cnum) {
94 pr("Giving something to yourself?\n");
98 if (natp->nat_stat != STAT_ACTIVE) {
99 pr("You can only give to normal countries...\n");
102 if (relations_with(n, player->cnum) < FRIENDLY) {
103 pr("You can only cede to a country that is friendly towards you...\n");
109 return cede_sect(&ns, to);
111 return cede_ship(&ni, to);
115 cede_sect(struct nstr_sect *ns, natid to)
117 struct sctstr sect, osect;
126 while (nxtsct(ns, §)) {
129 if (sect.sct_mobil == 0) {
130 pr("%s has no mobility and cannot be ceded\n",
131 xyas(sect.sct_x, sect.sct_y, player->cnum));
136 for (n = 1; n <= 6; n++) { /* Directions */
137 off_x = sect.sct_x + diroff[n][0];
138 off_y = sect.sct_y + diroff[n][1];
140 if (!getsect(off_x, off_y, &osect))
142 if ((osect.sct_own == to) && (osect.sct_mobil))
144 if ((osect.sct_own == to) &&
145 has_units_with_mob(osect.sct_x, osect.sct_y, to))
148 snxtitem_all(&ni, EF_SHIP);
149 while (nxtitem(&ni, &ship)) {
150 if ((ship.shp_own == to) &&
151 ((ship.shp_x == sect.sct_x) && (ship.shp_y == sect.sct_y)))
155 pr("%s has no sector with mobility adjacent to or ship in %s!\n",
156 cname(to), xyas(sect.sct_x, sect.sct_y, player->cnum));
163 grab_sect(§, to);
165 pr(" %s %d%% ceded\n", xyas(sect.sct_x, sect.sct_y, player->cnum),
166 (int)sect.sct_effic);
167 wu(0, to, "%s ceded to you by %s\n",
168 xyas(sect.sct_x, sect.sct_y, to), cname(player->cnum));
170 pr("%d sector%s\n", nsect, splur(nsect));
175 has_units_with_mob(coord x, coord y, natid cn)
180 snxtitem_xy(&ni, EF_LAND, x, y);
181 while (nxtitem(&ni, &land)) {
182 if (land.lnd_own != cn)
184 if (land.lnd_mobil > 0)
202 grab_sect(struct sctstr *sp, natid to)
212 /* Wipe all the distribution info */
213 memset(sp->sct_dist, 0, sizeof(sp->sct_dist));
214 memset(sp->sct_del, 0, sizeof(sp->sct_del));
215 sp->sct_dist_x = sp->sct_x;
216 sp->sct_dist_y = sp->sct_y;
219 snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
220 while (nxtitem(&ni, pp)) {
221 if (pp->pln_own == 0)
223 if (pp->pln_ship >= 0)
225 if (pp->pln_own != player->cnum)
227 if (pp->pln_flags & PLN_LAUNCHED)
230 wu(0, to, "\t%s ceded to you by %s\n",
231 prplane(pp), cname(player->cnum));
235 putplane(pp->pln_uid, pp);
239 snxtitem_xy(&ni, EF_NUKE, sp->sct_x, sp->sct_y);
240 while (nxtitem(&ni, np)) {
241 if (np->nuk_own == 0)
244 wu(0, to, "\t%s ceded to you by %s\n",
245 prnuke(np), cname(player->cnum));
251 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
252 while (nxtitem(&ni, lp)) {
253 if (lp->lnd_own == 0)
255 if (lp->lnd_ship == 0)
257 if (lp->lnd_own != player->cnum)
260 wu(0, to, "\t%s ceded to you by %s\n",
261 prland(lp), cname(player->cnum));
270 if (sp->sct_oldown == to) {
271 sp->sct_che = 0; /* FIXME where do these guys go? */
272 sp->sct_che_target = 0;
276 if (sp->sct_oldown == to)
278 else /* people don't like being given away */
281 sp->sct_dist_x = sp->sct_x;
282 sp->sct_dist_y = sp->sct_y;
283 if (sp->sct_oldown == sp->sct_own)
290 cede_ship(struct nstr_item *ni, natid to)
295 struct nstr_item tni;
299 while (nxtitem(ni, &ship)) {
301 if (!player->owner || ship.shp_own == 0)
305 snxtitem_xy(&tni, EF_SHIP, ship.shp_x, ship.shp_y);
306 while (nxtitem(&tni, &tship) && bad)
307 if (tship.shp_own == to)
310 getsect(ship.shp_x, ship.shp_y, §);
311 if (bad && (sect.sct_own != to)) {
312 pr("%s isn't in a %s sector, and is not with a %s ship!\n",
313 prship(&ship), cname(to), cname(to));
316 grab_ship(&ship, to);
317 putship(ship.shp_uid, &ship);
319 wu(0, to, "%s ceded to you by %s\n",
320 prship(&ship), cname(player->cnum));
321 pr("%s ceded to %s\n", prship(&ship), cname(to));
323 pr(" %d ship%s\n", nships, splur(nships));
329 grab_ship(struct shpstr *sp, natid to)
338 snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
339 while (nxtitem(&ni, pp)) {
340 if (pp->pln_own == 0)
342 if (pp->pln_flags & PLN_LAUNCHED)
344 if (pp->pln_ship != sp->shp_uid)
346 if (pp->pln_own != player->cnum)
349 wu(0, to, "\t%s ceded to you by %s\n",
350 prplane(pp), cname(player->cnum));
354 putplane(pp->pln_uid, pp);
358 snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
359 while (nxtitem(&ni, lp)) {
360 if (lp->lnd_own == 0)
362 if (lp->lnd_ship != sp->shp_uid)
364 if (lp->lnd_own != player->cnum)
367 wu(0, to, "\t%s ceded to you by %s\n",
368 prland(lp), cname(player->cnum));