2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, 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:
31 * Ron Koenderink, 2005-2006
32 * Markus Armbruster, 2006-2014
49 #include "prototypes.h"
55 static int findcondition(char);
56 static int retreat_land1(struct lndstr *, char, int);
57 static int retreat_ship1(struct shpstr *, char, int);
64 static struct ccode conditions[] = {
65 { 'i', { "retreated with a damaged friend",
67 { 't', { "retreated with a torpedoed ship",
68 "was hit by a torpedo" } },
69 { 's', { "retreated with a ship scared by sonar",
70 "detected a sonar ping" } },
71 { 'h', { "retreated with a helpless ship",
72 "was fired upon with no one able to defend it" } },
73 { 'b', { "retreated with a bombed friend",
75 { 'd', { "retreated with a depth-charged ship",
76 "was depth-charged" } },
77 { 'u', { "retreated with a boarded ship", "was boarded" } },
78 { 0, { "panicked", "panicked"} }
82 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
88 if (sp->shp_rflags & RET_INJURED)
89 retreat_ship(sp, 'i');
95 retreat_ship(struct shpstr *sp, char code)
100 if (sp->shp_effic < SHIP_MINEFF || CANT_HAPPEN(!sp->shp_own))
102 if (sp->shp_own == player->cnum)
105 retreat_ship1(sp, code, 1);
107 if (sp->shp_rflags & RET_GROUP) {
108 snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
109 while (nxtitem(&ni, &ship)) {
110 if (ship.shp_own != sp->shp_own || ship.shp_uid == sp->shp_uid)
112 if (retreat_ship1(&ship, code, 0))
113 putship(ship.shp_uid, &ship);
119 retreat_ship1(struct shpstr *sp, char code, int orig)
122 /* Is this the originally scared ship, or a follower */
139 /* check crew - uws don't count */
140 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
142 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
143 conditions[findcondition(code)].desc[orig]);
147 getsect(sp->shp_x, sp->shp_y, §);
148 switch (shp_check_nav(sp, §)) {
151 case SHP_STUCK_CONSTRUCTION:
153 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
154 prship(sp), conditions[findcondition(code)].desc[orig]);
159 case SHP_STUCK_CANAL:
160 case SHP_STUCK_IMPASSABLE:
162 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
163 prship(sp), conditions[findcondition(code)].desc[orig]);
167 if (sp->shp_mobil <= 0.0) {
169 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
170 prship(sp), conditions[findcondition(code)].desc[orig]);
174 for (i = 0; i < MAX_RETREAT && sp->shp_rpath[0]; i++) {
175 if (sp->shp_mobil <= 0.0) {
177 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
178 prship(sp), conditions[findcondition(code)].desc[orig]);
181 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
182 if (dir == DIR_STOP || CANT_HAPPEN(dir < 0)) {
183 memmove(sp->shp_rpath, sp->shp_rpath + 1,
184 sizeof(sp->shp_rpath) - 1);
185 if (sp->shp_rpath[0] == 0)
192 mcp = &mchr[(int)sp->shp_type];
193 newx = xnorm(sp->shp_x + dx);
194 newy = ynorm(sp->shp_y + dy);
195 mobcost = shp_mobcost(sp);
197 getsect(newx, newy, §);
198 if (shp_check_nav(sp, §) != SHP_STUCK_NOT ||
200 && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
201 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
202 prship(sp), conditions[findcondition(code)].desc[orig],
203 xyas(newx, newy, sp->shp_own));
208 sp->shp_mobil -= mobcost;
210 memmove(sp->shp_rpath, sp->shp_rpath + 1,
211 sizeof(sp->shp_rpath) - 1);
212 if (sp->shp_rpath[0] == 0)
215 mines = sect.sct_mines;
217 if (sect.sct_type != SCT_WATER || mines <= 0)
219 if (mcp->m_flags & M_SWEEP) {
220 max = mcp->m_item[I_SHELL];
221 shells = sp->shp_item[I_SHELL];
222 for (m = 0; mines > 0 && m < 5; m++) {
225 shells = MIN(max, shells + 1);
226 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
230 if (sect.sct_mines != mines) {
232 "%s cleared %d mine%s in %s while retreating\n",
233 prship(sp), sect.sct_mines - mines,
234 splur(sect.sct_mines - mines),
235 xyas(newx, newy, sp->shp_own));
236 sect.sct_mines = mines;
237 sp->shp_item[I_SHELL] = shells;
241 writemap(sp->shp_own);
243 if (chance(DMINE_HITCHANCE(mines))) {
245 "%s %s,\nand hit a mine in %s while retreating!\n",
246 prship(sp), conditions[findcondition(code)].desc[orig],
247 xyas(newx, newy, sp->shp_own));
248 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
252 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
253 writemap(sp->shp_own);
254 sect.sct_mines = mines;
261 wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
262 prship(sp), conditions[findcondition(code)].desc[orig],
263 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
265 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
267 conditions[findcondition(code)].desc[orig],
268 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
275 findcondition(char code)
279 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
280 CANT_HAPPEN(!conditions[i].code);
285 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
291 if (lp->lnd_rflags & RET_INJURED)
292 retreat_land(lp, 'i');
298 retreat_land(struct lndstr *lp, char code)
303 if (lp->lnd_effic < LAND_MINEFF || CANT_HAPPEN(!lp->lnd_own))
305 if (lp->lnd_own == player->cnum)
308 retreat_land1(lp, code, 1);
310 if (lp->lnd_rflags & RET_GROUP) {
311 snxtitem_group(&ni, EF_LAND, lp->lnd_army);
312 while (nxtitem(&ni, &land)) {
313 if (land.lnd_own != lp->lnd_own || land.lnd_uid == lp->lnd_uid)
315 if (retreat_land1(&land, code, 0))
316 putland(land.lnd_uid, &land);
322 retreat_land1(struct lndstr *lp, char code, int orig)
325 /* Is this the originally scared unit, or a follower */
341 getsect(lp->lnd_x, lp->lnd_y, §);
343 if (lp->lnd_mobil <= 0.0) {
345 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
346 prland(lp), conditions[findcondition(code)].desc[orig]);
350 for (i = 0; i < MAX_RETREAT && lp->lnd_rpath[0]; i++) {
351 if (lp->lnd_mobil <= 0.0) {
353 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
354 prland(lp), conditions[findcondition(code)].desc[orig]);
357 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
358 if (dir == DIR_STOP || CANT_HAPPEN(dir < 0)) {
359 memmove(lp->lnd_rpath, lp->lnd_rpath + 1,
360 sizeof(lp->lnd_rpath) - 1);
361 if (lp->lnd_rpath[0] == 0)
368 lcp = &lchr[(int)lp->lnd_type];
369 newx = xnorm(lp->lnd_x + dx);
370 newy = ynorm(lp->lnd_y + dy);
372 getsect(newx, newy, §);
373 mobcost = lnd_mobcost(lp, §);
375 || sect.sct_type == SCT_MOUNT
376 || sect.sct_own != lp->lnd_own) {
377 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
379 conditions[findcondition(code)].desc[orig],
380 xyas(newx, newy, lp->lnd_own));
385 lp->lnd_mobil -= mobcost;
387 memmove(lp->lnd_rpath, lp->lnd_rpath + 1,
388 sizeof(lp->lnd_rpath) - 1);
389 if (lp->lnd_rpath[0] == 0)
392 mines = SCT_LANDMINES(§);
393 if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
395 if (lcp->l_flags & L_ENGINEER) {
396 max = lcp->l_item[I_SHELL];
397 shells = lp->lnd_item[I_SHELL];
398 for (m = 0; mines > 0 && m < 5; m++) {
401 shells = MIN(max, shells + 1);
404 sect.sct_mines = mines;
405 lp->lnd_item[I_SHELL] = shells;
408 if (chance(DMINE_LHITCHANCE(mines))) {
410 "%s %s,\nand hit a mine in %s while retreating!\n",
412 conditions[findcondition(code)].desc[orig],
413 xyas(newx, newy, lp->lnd_own));
414 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
416 if (lcp->l_flags & L_ENGINEER)
420 sect.sct_mines = mines;
427 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
429 conditions[findcondition(code)].desc[orig],
430 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
432 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
434 conditions[findcondition(code)].desc[orig],
435 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));