2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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 * retreat.c: Retreat subroutines
29 * Known contributors to this file:
45 #include "prototypes.h"
51 static int findcondition(char);
52 static int retreat_land1(struct lndstr *, char, int);
53 static int retreat_ship1(struct shpstr *, char, int);
60 static struct ccode conditions[] = {
61 { 'i', { "retreated with a damaged friend",
63 { 't', { "retreated with a torpedoed ship",
64 "was hit by a torpedo" } },
65 { 's', { "retreated with a ship scared by sonar",
66 "detected a sonar ping" } },
67 { 'h', { "retreated with a helpless ship",
68 "was fired upon with no one able to defend it" } },
69 { 'b', { "retreated with a bombed friend",
71 { 'd', { "retreated with a depth-charged ship",
72 "was depth-charged" } },
73 { 'u', { "retreated with a boarded ship", "was boarded" } },
74 { 0, { "panicked", "panicked"} }
78 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
84 if (sp->shp_rflags & RET_INJURED)
85 retreat_ship(sp, 'i');
91 retreat_ship(struct shpstr *sp, char code)
96 if (sp->shp_rflags & RET_GROUP) {
97 snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
98 while (nxtitem(&ni, &ship))
99 if (ship.shp_own == sp->shp_own) {
100 if (ship.shp_uid == sp->shp_uid) {
101 retreat_ship1(sp, code, 1);
102 if (sp->shp_rpath[0] == 0)
105 retreat_ship1(&ship, code, 0);
106 getship(ship.shp_uid, &ship);
107 if (ship.shp_rpath[0] == 0) {
109 putship(ship.shp_uid, &ship);
114 retreat_ship1(sp, code, 1);
115 if (sp->shp_rpath[0] == 0)
121 retreat_ship1(struct shpstr *sp, char code, int orig)
124 /* Is this the originally scared ship, or a follower */
143 if (sp->shp_own == 0)
146 if (sp->shp_effic < SHIP_MINEFF) {
148 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
149 prship(sp), conditions[findcondition(code)].desc[orig]);
151 putship(sp->shp_uid, sp);
156 /* can't retreat a ship that's sailin, bad things happend */
159 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
160 prship(sp), conditions[findcondition(code)].desc[orig]);
162 putship(sp->shp_uid, sp);
166 /* check crew - uws don't count */
167 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
169 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
170 conditions[findcondition(code)].desc[orig]);
172 putship(sp->shp_uid, sp);
176 getsect(sp->shp_x, sp->shp_y, §);
177 switch (shp_check_nav(§, sp)) {
178 case CN_CONSTRUCTION:
180 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
181 prship(sp), conditions[findcondition(code)].desc[orig]);
183 putship(sp->shp_uid, sp);
187 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
188 prship(sp), conditions[findcondition(code)].desc[orig]);
190 putship(sp->shp_uid, sp);
198 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
199 prship(sp), conditions[findcondition(code)].desc[orig]);
201 putship(sp->shp_uid, sp);
206 if (sp->shp_mobil <= 0.0) {
208 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
209 prship(sp), conditions[findcondition(code)].desc[orig]);
211 putship(sp->shp_uid, sp);
217 while (!stopping && n) {
219 if (sp->shp_rpath[0] == 0) {
223 if (sp->shp_mobil <= 0.0) {
225 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
226 prship(sp), conditions[findcondition(code)].desc[orig]);
228 putship(sp->shp_uid, sp);
231 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
232 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
243 mcp = &mchr[(int)sp->shp_type];
244 newx = xnorm(sp->shp_x + dx);
245 newy = ynorm(sp->shp_y + dy);
246 mobcost = shp_mobcost(sp);
248 getsect(newx, newy, §);
249 if (shp_check_nav(§, sp) != CN_NAVIGABLE ||
251 && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
252 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
253 prship(sp), conditions[findcondition(code)].desc[orig],
254 xyas(newx, newy, sp->shp_own));
256 putship(sp->shp_uid, sp);
261 sp->shp_mobil -= mobcost;
265 mines = sect.sct_mines;
267 if (sect.sct_type != SCT_WATER || mines <= 0)
269 if (mcp->m_flags & M_SWEEP) {
270 max = mcp->m_item[I_SHELL];
271 shells = sp->shp_item[I_SHELL];
272 for (m = 0; mines > 0 && m < 5; m++) {
275 shells = MIN(max, shells + 1);
276 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
280 if (sect.sct_mines != mines) {
282 "%s cleared %d mine%s in %s while retreating\n",
283 prship(sp), sect.sct_mines - mines,
284 splur(sect.sct_mines - mines),
285 xyas(newx, newy, sp->shp_own));
286 sect.sct_mines = mines;
287 sp->shp_item[I_SHELL] = shells;
291 writemap(sp->shp_own);
293 if (chance(DMINE_HITCHANCE(mines))) {
295 "%s %s,\nand hit a mine in %s while retreating!\n",
296 prship(sp), conditions[findcondition(code)].desc[orig],
297 xyas(newx, newy, sp->shp_own));
298 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
302 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
303 writemap(sp->shp_own);
304 sect.sct_mines = mines;
307 putship(sp->shp_uid, sp);
313 wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
314 prship(sp), conditions[findcondition(code)].desc[orig],
315 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
317 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
319 conditions[findcondition(code)].desc[orig],
320 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
323 putship(sp->shp_uid, sp);
328 findcondition(char code)
332 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
333 CANT_HAPPEN(!conditions[i].code);
338 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
344 if (lp->lnd_rflags & RET_INJURED)
345 retreat_land(lp, 'i');
351 retreat_land(struct lndstr *lp, char code)
356 if (lp->lnd_rflags & RET_GROUP) {
357 snxtitem_group(&ni, EF_SHIP, lp->lnd_army);
358 while (nxtitem(&ni, &land))
359 if (land.lnd_own == lp->lnd_own) {
360 if (land.lnd_uid == lp->lnd_uid) {
361 retreat_land1(lp, code, 1);
362 if (lp->lnd_rpath[0] == 0)
365 retreat_land1(&land, code, 0);
366 getland(land.lnd_uid, &land);
367 if (land.lnd_rpath[0] == 0) {
369 putland(land.lnd_uid, &land);
374 retreat_land1(lp, code, 1);
375 if (lp->lnd_rpath[0] == 0)
381 retreat_land1(struct lndstr *lp, char code, int orig)
384 /* Is this the originally scared unit, or a follower */
402 if (lp->lnd_own == 0)
405 if (lp->lnd_effic < LAND_MINEFF) {
407 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
408 prland(lp), conditions[findcondition(code)].desc[orig]);
410 putland(lp->lnd_uid, lp);
414 getsect(lp->lnd_x, lp->lnd_y, §);
416 if (lp->lnd_mobil <= 0.0) {
418 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
419 prland(lp), conditions[findcondition(code)].desc[orig]);
421 putland(lp->lnd_uid, lp);
427 while (!stopping && n) {
429 if (lp->lnd_rpath[0] == 0) {
433 if (lp->lnd_mobil <= 0.0) {
435 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
436 prland(lp), conditions[findcondition(code)].desc[orig]);
438 putland(lp->lnd_uid, lp);
441 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
442 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
453 lcp = &lchr[(int)lp->lnd_type];
454 newx = xnorm(lp->lnd_x + dx);
455 newy = ynorm(lp->lnd_y + dy);
457 getsect(newx, newy, §);
458 mobcost = lnd_mobcost(lp, §);
460 || sect.sct_type == SCT_MOUNT
461 || sect.sct_own != lp->lnd_own) {
462 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
464 conditions[findcondition(code)].desc[orig],
465 xyas(newx, newy, lp->lnd_own));
467 putland(lp->lnd_uid, lp);
472 lp->lnd_mobil -= mobcost;
476 mines = SCT_LANDMINES(§);
477 if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
479 if (lcp->l_flags & L_ENGINEER) {
480 max = lcp->l_item[I_SHELL];
481 shells = lp->lnd_item[I_SHELL];
482 for (m = 0; mines > 0 && m < 5; m++) {
485 shells = MIN(max, shells + 1);
488 sect.sct_mines = mines;
489 lp->lnd_item[I_SHELL] = shells;
492 if (chance(DMINE_LHITCHANCE(mines))) {
494 "%s %s,\nand hit a mine in %s while retreating!\n",
496 conditions[findcondition(code)].desc[orig],
497 xyas(newx, newy, lp->lnd_own));
498 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
500 if (lcp->l_flags & L_ENGINEER)
504 sect.sct_mines = mines;
507 putland(lp->lnd_uid, lp);
513 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
515 conditions[findcondition(code)].desc[orig],
516 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
518 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
520 conditions[findcondition(code)].desc[orig],
521 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
524 putland(lp->lnd_uid, lp);