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);
106 if (sp->shp_rpath[0] == 0)
109 if (sp->shp_rflags & RET_GROUP) {
110 snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
111 while (nxtitem(&ni, &ship))
112 if (ship.shp_own == sp->shp_own) {
113 if (ship.shp_uid != sp->shp_uid) {
114 retreat_ship1(&ship, code, 0);
115 getship(ship.shp_uid, &ship);
116 if (ship.shp_rpath[0] == 0) {
118 putship(ship.shp_uid, &ship);
126 retreat_ship1(struct shpstr *sp, char code, int orig)
129 /* Is this the originally scared ship, or a follower */
148 /* can't retreat a ship that's sailin, bad things happend */
151 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
152 prship(sp), conditions[findcondition(code)].desc[orig]);
154 putship(sp->shp_uid, sp);
158 /* check crew - uws don't count */
159 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
161 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
162 conditions[findcondition(code)].desc[orig]);
164 putship(sp->shp_uid, sp);
168 getsect(sp->shp_x, sp->shp_y, §);
169 switch (shp_check_nav(sp, §)) {
173 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
174 prship(sp), conditions[findcondition(code)].desc[orig]);
176 putship(sp->shp_uid, sp);
181 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
182 prship(sp), conditions[findcondition(code)].desc[orig]);
184 putship(sp->shp_uid, sp);
191 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
192 prship(sp), conditions[findcondition(code)].desc[orig]);
194 putship(sp->shp_uid, sp);
198 if (sp->shp_mobil <= 0.0) {
200 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
201 prship(sp), conditions[findcondition(code)].desc[orig]);
203 putship(sp->shp_uid, sp);
209 while (!stopping && n) {
211 if (sp->shp_rpath[0] == 0) {
215 if (sp->shp_mobil <= 0.0) {
217 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
218 prship(sp), conditions[findcondition(code)].desc[orig]);
220 putship(sp->shp_uid, sp);
223 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
224 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
235 mcp = &mchr[(int)sp->shp_type];
236 newx = xnorm(sp->shp_x + dx);
237 newy = ynorm(sp->shp_y + dy);
238 mobcost = shp_mobcost(sp);
240 getsect(newx, newy, §);
241 if (shp_check_nav(sp, §) != NAVOK ||
243 && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
244 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
245 prship(sp), conditions[findcondition(code)].desc[orig],
246 xyas(newx, newy, sp->shp_own));
248 putship(sp->shp_uid, sp);
253 sp->shp_mobil -= mobcost;
258 mines = sect.sct_mines;
260 if (sect.sct_type != SCT_WATER || mines <= 0)
262 if (mcp->m_flags & M_SWEEP) {
263 max = mcp->m_item[I_SHELL];
264 shells = sp->shp_item[I_SHELL];
265 for (m = 0; mines > 0 && m < 5; m++) {
268 shells = MIN(max, shells + 1);
269 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
273 if (sect.sct_mines != mines) {
275 "%s cleared %d mine%s in %s while retreating\n",
276 prship(sp), sect.sct_mines - mines,
277 splur(sect.sct_mines - mines),
278 xyas(newx, newy, sp->shp_own));
279 sect.sct_mines = mines;
280 sp->shp_item[I_SHELL] = shells;
284 writemap(sp->shp_own);
286 if (chance(DMINE_HITCHANCE(mines))) {
288 "%s %s,\nand hit a mine in %s while retreating!\n",
289 prship(sp), conditions[findcondition(code)].desc[orig],
290 xyas(newx, newy, sp->shp_own));
291 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
295 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
296 writemap(sp->shp_own);
297 sect.sct_mines = mines;
300 putship(sp->shp_uid, sp);
306 wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
307 prship(sp), conditions[findcondition(code)].desc[orig],
308 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
310 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
312 conditions[findcondition(code)].desc[orig],
313 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
316 putship(sp->shp_uid, sp);
321 findcondition(char code)
325 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
326 CANT_HAPPEN(!conditions[i].code);
331 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
337 if (lp->lnd_rflags & RET_INJURED)
338 retreat_land(lp, 'i');
344 retreat_land(struct lndstr *lp, char code)
349 if (lp->lnd_effic < LAND_MINEFF || CANT_HAPPEN(!lp->lnd_own))
351 if (lp->lnd_own == player->cnum)
354 retreat_land1(lp, code, 1);
355 if (lp->lnd_rpath[0] == 0)
358 if (lp->lnd_rflags & RET_GROUP) {
359 snxtitem_group(&ni, EF_LAND, lp->lnd_army);
360 while (nxtitem(&ni, &land))
361 if (land.lnd_own == lp->lnd_own) {
362 if (land.lnd_uid != lp->lnd_uid) {
363 retreat_land1(&land, code, 0);
364 getland(land.lnd_uid, &land);
365 if (land.lnd_rpath[0] == 0) {
367 putland(land.lnd_uid, &land);
375 retreat_land1(struct lndstr *lp, char code, int orig)
378 /* Is this the originally scared unit, or a follower */
395 getsect(lp->lnd_x, lp->lnd_y, §);
397 if (lp->lnd_mobil <= 0.0) {
399 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
400 prland(lp), conditions[findcondition(code)].desc[orig]);
402 putland(lp->lnd_uid, lp);
408 while (!stopping && n) {
410 if (lp->lnd_rpath[0] == 0) {
414 if (lp->lnd_mobil <= 0.0) {
416 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
417 prland(lp), conditions[findcondition(code)].desc[orig]);
419 putland(lp->lnd_uid, lp);
422 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
423 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
434 lcp = &lchr[(int)lp->lnd_type];
435 newx = xnorm(lp->lnd_x + dx);
436 newy = ynorm(lp->lnd_y + dy);
438 getsect(newx, newy, §);
439 mobcost = lnd_mobcost(lp, §);
441 || sect.sct_type == SCT_MOUNT
442 || sect.sct_own != lp->lnd_own) {
443 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
445 conditions[findcondition(code)].desc[orig],
446 xyas(newx, newy, lp->lnd_own));
448 putland(lp->lnd_uid, lp);
453 lp->lnd_mobil -= mobcost;
458 mines = SCT_LANDMINES(§);
459 if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
461 if (lcp->l_flags & L_ENGINEER) {
462 max = lcp->l_item[I_SHELL];
463 shells = lp->lnd_item[I_SHELL];
464 for (m = 0; mines > 0 && m < 5; m++) {
467 shells = MIN(max, shells + 1);
470 sect.sct_mines = mines;
471 lp->lnd_item[I_SHELL] = shells;
474 if (chance(DMINE_LHITCHANCE(mines))) {
476 "%s %s,\nand hit a mine in %s while retreating!\n",
478 conditions[findcondition(code)].desc[orig],
479 xyas(newx, newy, lp->lnd_own));
480 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
482 if (lcp->l_flags & L_ENGINEER)
486 sect.sct_mines = mines;
489 putland(lp->lnd_uid, lp);
495 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
497 conditions[findcondition(code)].desc[orig],
498 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
500 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
502 conditions[findcondition(code)].desc[orig],
503 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
506 putland(lp->lnd_uid, lp);