2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, 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, §)) {
152 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
153 prship(sp), conditions[findcondition(code)].desc[orig]);
158 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
159 prship(sp), conditions[findcondition(code)].desc[orig]);
166 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
167 prship(sp), conditions[findcondition(code)].desc[orig]);
171 if (sp->shp_mobil <= 0.0) {
173 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
174 prship(sp), conditions[findcondition(code)].desc[orig]);
178 for (i = 0; i < MAX_RETREAT && sp->shp_rpath[0]; i++) {
179 if (sp->shp_mobil <= 0.0) {
181 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
182 prship(sp), conditions[findcondition(code)].desc[orig]);
185 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
186 if (dir == DIR_STOP || CANT_HAPPEN(dir < 0)) {
187 memmove(sp->shp_rpath, sp->shp_rpath + 1,
188 sizeof(sp->shp_rpath) - 1);
189 if (sp->shp_rpath[0] == 0)
196 mcp = &mchr[(int)sp->shp_type];
197 newx = xnorm(sp->shp_x + dx);
198 newy = ynorm(sp->shp_y + dy);
199 mobcost = shp_mobcost(sp);
201 getsect(newx, newy, §);
202 if (shp_check_nav(sp, §) != NAVOK ||
204 && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
205 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
206 prship(sp), conditions[findcondition(code)].desc[orig],
207 xyas(newx, newy, sp->shp_own));
212 sp->shp_mobil -= mobcost;
214 memmove(sp->shp_rpath, sp->shp_rpath + 1,
215 sizeof(sp->shp_rpath) - 1);
216 if (sp->shp_rpath[0] == 0)
219 mines = sect.sct_mines;
221 if (sect.sct_type != SCT_WATER || mines <= 0)
223 if (mcp->m_flags & M_SWEEP) {
224 max = mcp->m_item[I_SHELL];
225 shells = sp->shp_item[I_SHELL];
226 for (m = 0; mines > 0 && m < 5; m++) {
229 shells = MIN(max, shells + 1);
230 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
234 if (sect.sct_mines != mines) {
236 "%s cleared %d mine%s in %s while retreating\n",
237 prship(sp), sect.sct_mines - mines,
238 splur(sect.sct_mines - mines),
239 xyas(newx, newy, sp->shp_own));
240 sect.sct_mines = mines;
241 sp->shp_item[I_SHELL] = shells;
245 writemap(sp->shp_own);
247 if (chance(DMINE_HITCHANCE(mines))) {
249 "%s %s,\nand hit a mine in %s while retreating!\n",
250 prship(sp), conditions[findcondition(code)].desc[orig],
251 xyas(newx, newy, sp->shp_own));
252 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
256 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
257 writemap(sp->shp_own);
258 sect.sct_mines = mines;
265 wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
266 prship(sp), conditions[findcondition(code)].desc[orig],
267 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
269 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
271 conditions[findcondition(code)].desc[orig],
272 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
279 findcondition(char code)
283 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
284 CANT_HAPPEN(!conditions[i].code);
289 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
295 if (lp->lnd_rflags & RET_INJURED)
296 retreat_land(lp, 'i');
302 retreat_land(struct lndstr *lp, char code)
307 if (lp->lnd_effic < LAND_MINEFF || CANT_HAPPEN(!lp->lnd_own))
309 if (lp->lnd_own == player->cnum)
312 retreat_land1(lp, code, 1);
314 if (lp->lnd_rflags & RET_GROUP) {
315 snxtitem_group(&ni, EF_LAND, lp->lnd_army);
316 while (nxtitem(&ni, &land)) {
317 if (land.lnd_own != lp->lnd_own || land.lnd_uid == lp->lnd_uid)
319 if (retreat_land1(&land, code, 0))
320 putland(land.lnd_uid, &land);
326 retreat_land1(struct lndstr *lp, char code, int orig)
329 /* Is this the originally scared unit, or a follower */
345 getsect(lp->lnd_x, lp->lnd_y, §);
347 if (lp->lnd_mobil <= 0.0) {
349 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
350 prland(lp), conditions[findcondition(code)].desc[orig]);
354 for (i = 0; i < MAX_RETREAT && lp->lnd_rpath[0]; i++) {
355 if (lp->lnd_mobil <= 0.0) {
357 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
358 prland(lp), conditions[findcondition(code)].desc[orig]);
361 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
362 if (dir == DIR_STOP || CANT_HAPPEN(dir < 0)) {
363 memmove(lp->lnd_rpath, lp->lnd_rpath + 1,
364 sizeof(lp->lnd_rpath) - 1);
365 if (lp->lnd_rpath[0] == 0)
372 lcp = &lchr[(int)lp->lnd_type];
373 newx = xnorm(lp->lnd_x + dx);
374 newy = ynorm(lp->lnd_y + dy);
376 getsect(newx, newy, §);
377 mobcost = lnd_mobcost(lp, §);
379 || sect.sct_type == SCT_MOUNT
380 || sect.sct_own != lp->lnd_own) {
381 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
383 conditions[findcondition(code)].desc[orig],
384 xyas(newx, newy, lp->lnd_own));
389 lp->lnd_mobil -= mobcost;
391 memmove(lp->lnd_rpath, lp->lnd_rpath + 1,
392 sizeof(lp->lnd_rpath) - 1);
393 if (lp->lnd_rpath[0] == 0)
396 mines = SCT_LANDMINES(§);
397 if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
399 if (lcp->l_flags & L_ENGINEER) {
400 max = lcp->l_item[I_SHELL];
401 shells = lp->lnd_item[I_SHELL];
402 for (m = 0; mines > 0 && m < 5; m++) {
405 shells = MIN(max, shells + 1);
408 sect.sct_mines = mines;
409 lp->lnd_item[I_SHELL] = shells;
412 if (chance(DMINE_LHITCHANCE(mines))) {
414 "%s %s,\nand hit a mine in %s while retreating!\n",
416 conditions[findcondition(code)].desc[orig],
417 xyas(newx, newy, lp->lnd_own));
418 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
420 if (lcp->l_flags & L_ENGINEER)
424 sect.sct_mines = mines;
431 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
433 conditions[findcondition(code)].desc[orig],
434 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
436 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
438 conditions[findcondition(code)].desc[orig],
439 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));