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 retreat_ship(struct shpstr *sp, char code)
87 if (CANT_HAPPEN(!sp->shp_own))
89 if (sp->shp_own == player->cnum)
92 retreat_ship1(sp, code, 1);
94 if (sp->shp_rflags & RET_GROUP) {
95 snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
96 while (nxtitem(&ni, &ship)) {
97 if (ship.shp_own != sp->shp_own || ship.shp_uid == sp->shp_uid)
99 if (retreat_ship1(&ship, code, 0))
100 putship(ship.shp_uid, &ship);
106 retreat_ship1(struct shpstr *sp, char code, int orig)
109 /* Is this the originally scared ship, or a follower */
126 if (sp->shp_effic < SHIP_MINEFF) {
128 putship(sp->shp_uid, sp);
132 /* check crew - uws don't count */
133 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
135 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
136 conditions[findcondition(code)].desc[orig]);
140 getsect(sp->shp_x, sp->shp_y, §);
141 switch (shp_check_nav(sp, §)) {
144 case SHP_STUCK_CONSTRUCTION:
146 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
147 prship(sp), conditions[findcondition(code)].desc[orig]);
152 case SHP_STUCK_CANAL:
153 case SHP_STUCK_IMPASSABLE:
155 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
156 prship(sp), conditions[findcondition(code)].desc[orig]);
160 if (sp->shp_mobil <= 0.0) {
162 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
163 prship(sp), conditions[findcondition(code)].desc[orig]);
167 for (i = 0; i < MAX_RETREAT && sp->shp_rpath[0]; i++) {
168 if (sp->shp_mobil <= 0.0) {
170 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
171 prship(sp), conditions[findcondition(code)].desc[orig]);
174 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
175 if (dir == DIR_STOP || CANT_HAPPEN(dir < 0)) {
176 memmove(sp->shp_rpath, sp->shp_rpath + 1,
177 sizeof(sp->shp_rpath) - 1);
178 if (sp->shp_rpath[0] == 0)
185 mcp = &mchr[(int)sp->shp_type];
186 newx = xnorm(sp->shp_x + dx);
187 newy = ynorm(sp->shp_y + dy);
188 mobcost = shp_mobcost(sp);
190 getsect(newx, newy, §);
191 if (shp_check_nav(sp, §) != SHP_STUCK_NOT ||
193 && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
194 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
195 prship(sp), conditions[findcondition(code)].desc[orig],
196 xyas(newx, newy, sp->shp_own));
201 sp->shp_mobil -= mobcost;
203 memmove(sp->shp_rpath, sp->shp_rpath + 1,
204 sizeof(sp->shp_rpath) - 1);
205 if (sp->shp_rpath[0] == 0)
208 mines = sect.sct_mines;
210 if (sect.sct_type != SCT_WATER || mines <= 0)
212 if (mcp->m_flags & M_SWEEP) {
213 max = mcp->m_item[I_SHELL];
214 shells = sp->shp_item[I_SHELL];
215 for (m = 0; mines > 0 && m < 5; m++) {
218 shells = MIN(max, shells + 1);
219 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
223 if (sect.sct_mines != mines) {
225 "%s cleared %d mine%s in %s while retreating\n",
226 prship(sp), sect.sct_mines - mines,
227 splur(sect.sct_mines - mines),
228 xyas(newx, newy, sp->shp_own));
229 sect.sct_mines = mines;
230 sp->shp_item[I_SHELL] = shells;
234 writemap(sp->shp_own);
236 if (chance(DMINE_HITCHANCE(mines))) {
238 "%s %s,\nand hit a mine in %s while retreating!\n",
239 prship(sp), conditions[findcondition(code)].desc[orig],
240 xyas(newx, newy, sp->shp_own));
241 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
245 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
246 writemap(sp->shp_own);
247 sect.sct_mines = mines;
254 wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
255 prship(sp), conditions[findcondition(code)].desc[orig],
256 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
258 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
260 conditions[findcondition(code)].desc[orig],
261 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
268 findcondition(char code)
272 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
273 CANT_HAPPEN(!conditions[i].code);
278 retreat_land(struct lndstr *lp, char code)
283 if (CANT_HAPPEN(!lp->lnd_own))
285 if (lp->lnd_own == player->cnum)
288 retreat_land1(lp, code, 1);
290 if (lp->lnd_rflags & RET_GROUP) {
291 snxtitem_group(&ni, EF_LAND, lp->lnd_army);
292 while (nxtitem(&ni, &land)) {
293 if (land.lnd_own != lp->lnd_own || land.lnd_uid == lp->lnd_uid)
295 if (retreat_land1(&land, code, 0))
296 putland(land.lnd_uid, &land);
302 retreat_land1(struct lndstr *lp, char code, int orig)
305 /* Is this the originally scared unit, or a follower */
321 if (lp->lnd_effic < LAND_MINEFF) {
323 putland(lp->lnd_uid, lp);
327 getsect(lp->lnd_x, lp->lnd_y, §);
329 if (lp->lnd_mobil <= 0.0) {
331 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
332 prland(lp), conditions[findcondition(code)].desc[orig]);
336 for (i = 0; i < MAX_RETREAT && lp->lnd_rpath[0]; i++) {
337 if (lp->lnd_mobil <= 0.0) {
339 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
340 prland(lp), conditions[findcondition(code)].desc[orig]);
343 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
344 if (dir == DIR_STOP || CANT_HAPPEN(dir < 0)) {
345 memmove(lp->lnd_rpath, lp->lnd_rpath + 1,
346 sizeof(lp->lnd_rpath) - 1);
347 if (lp->lnd_rpath[0] == 0)
354 lcp = &lchr[(int)lp->lnd_type];
355 newx = xnorm(lp->lnd_x + dx);
356 newy = ynorm(lp->lnd_y + dy);
358 getsect(newx, newy, §);
359 mobcost = lnd_mobcost(lp, §);
361 || sect.sct_type == SCT_MOUNT
362 || sect.sct_own != lp->lnd_own) {
363 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
365 conditions[findcondition(code)].desc[orig],
366 xyas(newx, newy, lp->lnd_own));
371 lp->lnd_mobil -= mobcost;
373 memmove(lp->lnd_rpath, lp->lnd_rpath + 1,
374 sizeof(lp->lnd_rpath) - 1);
375 if (lp->lnd_rpath[0] == 0)
378 mines = SCT_LANDMINES(§);
379 if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
381 if (lcp->l_flags & L_ENGINEER) {
382 max = lcp->l_item[I_SHELL];
383 shells = lp->lnd_item[I_SHELL];
384 for (m = 0; mines > 0 && m < 5; m++) {
387 shells = MIN(max, shells + 1);
390 sect.sct_mines = mines;
391 lp->lnd_item[I_SHELL] = shells;
394 if (chance(DMINE_LHITCHANCE(mines))) {
396 "%s %s,\nand hit a mine in %s while retreating!\n",
398 conditions[findcondition(code)].desc[orig],
399 xyas(newx, newy, lp->lnd_own));
400 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
402 if (lcp->l_flags & L_ENGINEER)
406 sect.sct_mines = mines;
413 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
415 conditions[findcondition(code)].desc[orig],
416 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
418 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
420 conditions[findcondition(code)].desc[orig],
421 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));