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
48 #include "prototypes.h"
54 static int findcondition(char);
55 static int retreat_land1(struct lndstr *, char, int);
56 static int retreat_ship1(struct shpstr *, char, int);
63 static struct ccode conditions[] = {
64 { 'i', { "retreated with a damaged friend",
66 { 't', { "retreated with a torpedoed ship",
67 "was hit by a torpedo" } },
68 { 's', { "retreated with a ship scared by sonar",
69 "detected a sonar ping" } },
70 { 'h', { "retreated with a helpless ship",
71 "was fired upon with no one able to defend it" } },
72 { 'b', { "retreated with a bombed friend",
74 { 'd', { "retreated with a depth-charged ship",
75 "was depth-charged" } },
76 { 'u', { "retreated with a boarded ship", "was boarded" } },
77 { 0, { "panicked", "panicked"} }
81 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
87 if (sp->shp_rflags & RET_INJURED)
88 retreat_ship(sp, 'i');
94 retreat_ship(struct shpstr *sp, char code)
99 if (sp->shp_effic < SHIP_MINEFF || CANT_HAPPEN(!sp->shp_own))
102 retreat_ship1(sp, code, 1);
103 if (sp->shp_rpath[0] == 0)
106 if (sp->shp_rflags & RET_GROUP) {
107 snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
108 while (nxtitem(&ni, &ship))
109 if (ship.shp_own == sp->shp_own) {
110 if (ship.shp_uid != sp->shp_uid) {
111 retreat_ship1(&ship, code, 0);
112 getship(ship.shp_uid, &ship);
113 if (ship.shp_rpath[0] == 0) {
115 putship(ship.shp_uid, &ship);
123 retreat_ship1(struct shpstr *sp, char code, int orig)
126 /* Is this the originally scared ship, or a follower */
145 /* can't retreat a ship that's sailin, bad things happend */
148 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
149 prship(sp), conditions[findcondition(code)].desc[orig]);
151 putship(sp->shp_uid, sp);
155 /* check crew - uws don't count */
156 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
158 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
159 conditions[findcondition(code)].desc[orig]);
161 putship(sp->shp_uid, sp);
165 getsect(sp->shp_x, sp->shp_y, §);
166 switch (shp_check_nav(sp, §)) {
170 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
171 prship(sp), conditions[findcondition(code)].desc[orig]);
173 putship(sp->shp_uid, sp);
178 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
179 prship(sp), conditions[findcondition(code)].desc[orig]);
181 putship(sp->shp_uid, sp);
188 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
189 prship(sp), conditions[findcondition(code)].desc[orig]);
191 putship(sp->shp_uid, sp);
195 if (sp->shp_mobil <= 0.0) {
197 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
198 prship(sp), conditions[findcondition(code)].desc[orig]);
200 putship(sp->shp_uid, sp);
206 while (!stopping && n) {
208 if (sp->shp_rpath[0] == 0) {
212 if (sp->shp_mobil <= 0.0) {
214 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
215 prship(sp), conditions[findcondition(code)].desc[orig]);
217 putship(sp->shp_uid, sp);
220 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
221 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
232 mcp = &mchr[(int)sp->shp_type];
233 newx = xnorm(sp->shp_x + dx);
234 newy = ynorm(sp->shp_y + dy);
235 mobcost = shp_mobcost(sp);
237 getsect(newx, newy, §);
238 if (shp_check_nav(sp, §) != NAVOK ||
240 && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
241 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
242 prship(sp), conditions[findcondition(code)].desc[orig],
243 xyas(newx, newy, sp->shp_own));
245 putship(sp->shp_uid, sp);
250 sp->shp_mobil -= mobcost;
255 mines = sect.sct_mines;
257 if (sect.sct_type != SCT_WATER || mines <= 0)
259 if (mcp->m_flags & M_SWEEP) {
260 max = mcp->m_item[I_SHELL];
261 shells = sp->shp_item[I_SHELL];
262 for (m = 0; mines > 0 && m < 5; m++) {
265 shells = MIN(max, shells + 1);
266 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
270 if (sect.sct_mines != mines) {
272 "%s cleared %d mine%s in %s while retreating\n",
273 prship(sp), sect.sct_mines - mines,
274 splur(sect.sct_mines - mines),
275 xyas(newx, newy, sp->shp_own));
276 sect.sct_mines = mines;
277 sp->shp_item[I_SHELL] = shells;
281 writemap(sp->shp_own);
283 if (chance(DMINE_HITCHANCE(mines))) {
285 "%s %s,\nand hit a mine in %s while retreating!\n",
286 prship(sp), conditions[findcondition(code)].desc[orig],
287 xyas(newx, newy, sp->shp_own));
288 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
292 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
293 writemap(sp->shp_own);
294 sect.sct_mines = mines;
297 putship(sp->shp_uid, sp);
303 wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
304 prship(sp), conditions[findcondition(code)].desc[orig],
305 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
307 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
309 conditions[findcondition(code)].desc[orig],
310 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
313 putship(sp->shp_uid, sp);
318 findcondition(char code)
322 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
323 CANT_HAPPEN(!conditions[i].code);
328 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
334 if (lp->lnd_rflags & RET_INJURED)
335 retreat_land(lp, 'i');
341 retreat_land(struct lndstr *lp, char code)
346 if (lp->lnd_effic < LAND_MINEFF || CANT_HAPPEN(!lp->lnd_own))
349 retreat_land1(lp, code, 1);
350 if (lp->lnd_rpath[0] == 0)
353 if (lp->lnd_rflags & RET_GROUP) {
354 snxtitem_group(&ni, EF_LAND, lp->lnd_army);
355 while (nxtitem(&ni, &land))
356 if (land.lnd_own == lp->lnd_own) {
357 if (land.lnd_uid != lp->lnd_uid) {
358 retreat_land1(&land, code, 0);
359 getland(land.lnd_uid, &land);
360 if (land.lnd_rpath[0] == 0) {
362 putland(land.lnd_uid, &land);
370 retreat_land1(struct lndstr *lp, char code, int orig)
373 /* Is this the originally scared unit, or a follower */
390 getsect(lp->lnd_x, lp->lnd_y, §);
392 if (lp->lnd_mobil <= 0.0) {
394 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
395 prland(lp), conditions[findcondition(code)].desc[orig]);
397 putland(lp->lnd_uid, lp);
403 while (!stopping && n) {
405 if (lp->lnd_rpath[0] == 0) {
409 if (lp->lnd_mobil <= 0.0) {
411 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
412 prland(lp), conditions[findcondition(code)].desc[orig]);
414 putland(lp->lnd_uid, lp);
417 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
418 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
429 lcp = &lchr[(int)lp->lnd_type];
430 newx = xnorm(lp->lnd_x + dx);
431 newy = ynorm(lp->lnd_y + dy);
433 getsect(newx, newy, §);
434 mobcost = lnd_mobcost(lp, §);
436 || sect.sct_type == SCT_MOUNT
437 || sect.sct_own != lp->lnd_own) {
438 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
440 conditions[findcondition(code)].desc[orig],
441 xyas(newx, newy, lp->lnd_own));
443 putland(lp->lnd_uid, lp);
448 lp->lnd_mobil -= mobcost;
453 mines = SCT_LANDMINES(§);
454 if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
456 if (lcp->l_flags & L_ENGINEER) {
457 max = lcp->l_item[I_SHELL];
458 shells = lp->lnd_item[I_SHELL];
459 for (m = 0; mines > 0 && m < 5; m++) {
462 shells = MIN(max, shells + 1);
465 sect.sct_mines = mines;
466 lp->lnd_item[I_SHELL] = shells;
469 if (chance(DMINE_LHITCHANCE(mines))) {
471 "%s %s,\nand hit a mine in %s while retreating!\n",
473 conditions[findcondition(code)].desc[orig],
474 xyas(newx, newy, lp->lnd_own));
475 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
477 if (lcp->l_flags & L_ENGINEER)
481 sect.sct_mines = mines;
484 putland(lp->lnd_uid, lp);
490 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
492 conditions[findcondition(code)].desc[orig],
493 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
495 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
497 conditions[findcondition(code)].desc[orig],
498 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
501 putland(lp->lnd_uid, lp);