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 (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 */
144 if (sp->shp_effic < SHIP_MINEFF) {
146 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
147 prship(sp), conditions[findcondition(code)].desc[orig]);
149 putship(sp->shp_uid, sp);
154 /* can't retreat a ship that's sailin, bad things happend */
157 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
158 prship(sp), conditions[findcondition(code)].desc[orig]);
160 putship(sp->shp_uid, sp);
164 /* check crew - uws don't count */
165 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
167 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
168 conditions[findcondition(code)].desc[orig]);
170 putship(sp->shp_uid, sp);
174 getsect(sp->shp_x, sp->shp_y, §);
175 switch (shp_check_nav(sp, §)) {
179 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
180 prship(sp), conditions[findcondition(code)].desc[orig]);
182 putship(sp->shp_uid, sp);
187 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
188 prship(sp), conditions[findcondition(code)].desc[orig]);
190 putship(sp->shp_uid, sp);
197 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
198 prship(sp), conditions[findcondition(code)].desc[orig]);
200 putship(sp->shp_uid, sp);
204 if (sp->shp_mobil <= 0.0) {
206 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
207 prship(sp), conditions[findcondition(code)].desc[orig]);
209 putship(sp->shp_uid, sp);
215 while (!stopping && n) {
217 if (sp->shp_rpath[0] == 0) {
221 if (sp->shp_mobil <= 0.0) {
223 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
224 prship(sp), conditions[findcondition(code)].desc[orig]);
226 putship(sp->shp_uid, sp);
229 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
230 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
241 mcp = &mchr[(int)sp->shp_type];
242 newx = xnorm(sp->shp_x + dx);
243 newy = ynorm(sp->shp_y + dy);
244 mobcost = shp_mobcost(sp);
246 getsect(newx, newy, §);
247 if (shp_check_nav(sp, §) != NAVOK ||
249 && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
250 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
251 prship(sp), conditions[findcondition(code)].desc[orig],
252 xyas(newx, newy, sp->shp_own));
254 putship(sp->shp_uid, sp);
259 sp->shp_mobil -= mobcost;
264 mines = sect.sct_mines;
266 if (sect.sct_type != SCT_WATER || mines <= 0)
268 if (mcp->m_flags & M_SWEEP) {
269 max = mcp->m_item[I_SHELL];
270 shells = sp->shp_item[I_SHELL];
271 for (m = 0; mines > 0 && m < 5; m++) {
274 shells = MIN(max, shells + 1);
275 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
279 if (sect.sct_mines != mines) {
281 "%s cleared %d mine%s in %s while retreating\n",
282 prship(sp), sect.sct_mines - mines,
283 splur(sect.sct_mines - mines),
284 xyas(newx, newy, sp->shp_own));
285 sect.sct_mines = mines;
286 sp->shp_item[I_SHELL] = shells;
290 writemap(sp->shp_own);
292 if (chance(DMINE_HITCHANCE(mines))) {
294 "%s %s,\nand hit a mine in %s while retreating!\n",
295 prship(sp), conditions[findcondition(code)].desc[orig],
296 xyas(newx, newy, sp->shp_own));
297 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
301 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
302 writemap(sp->shp_own);
303 sect.sct_mines = mines;
306 putship(sp->shp_uid, sp);
312 wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
313 prship(sp), conditions[findcondition(code)].desc[orig],
314 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
316 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
318 conditions[findcondition(code)].desc[orig],
319 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
322 putship(sp->shp_uid, sp);
327 findcondition(char code)
331 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
332 CANT_HAPPEN(!conditions[i].code);
337 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
343 if (lp->lnd_rflags & RET_INJURED)
344 retreat_land(lp, 'i');
350 retreat_land(struct lndstr *lp, char code)
355 if (CANT_HAPPEN(!lp->lnd_own))
358 retreat_land1(lp, code, 1);
359 if (lp->lnd_rpath[0] == 0)
362 if (lp->lnd_rflags & RET_GROUP) {
363 snxtitem_group(&ni, EF_LAND, lp->lnd_army);
364 while (nxtitem(&ni, &land))
365 if (land.lnd_own == lp->lnd_own) {
366 if (land.lnd_uid != lp->lnd_uid) {
367 retreat_land1(&land, code, 0);
368 getland(land.lnd_uid, &land);
369 if (land.lnd_rpath[0] == 0) {
371 putland(land.lnd_uid, &land);
379 retreat_land1(struct lndstr *lp, char code, int orig)
382 /* Is this the originally scared unit, or a follower */
399 if (lp->lnd_effic < LAND_MINEFF) {
401 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
402 prland(lp), conditions[findcondition(code)].desc[orig]);
404 putland(lp->lnd_uid, lp);
408 getsect(lp->lnd_x, lp->lnd_y, §);
410 if (lp->lnd_mobil <= 0.0) {
412 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
413 prland(lp), conditions[findcondition(code)].desc[orig]);
415 putland(lp->lnd_uid, lp);
421 while (!stopping && n) {
423 if (lp->lnd_rpath[0] == 0) {
427 if (lp->lnd_mobil <= 0.0) {
429 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
430 prland(lp), conditions[findcondition(code)].desc[orig]);
432 putland(lp->lnd_uid, lp);
435 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
436 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
447 lcp = &lchr[(int)lp->lnd_type];
448 newx = xnorm(lp->lnd_x + dx);
449 newy = ynorm(lp->lnd_y + dy);
451 getsect(newx, newy, §);
452 mobcost = lnd_mobcost(lp, §);
454 || sect.sct_type == SCT_MOUNT
455 || sect.sct_own != lp->lnd_own) {
456 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
458 conditions[findcondition(code)].desc[orig],
459 xyas(newx, newy, lp->lnd_own));
461 putland(lp->lnd_uid, lp);
466 lp->lnd_mobil -= mobcost;
471 mines = SCT_LANDMINES(§);
472 if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
474 if (lcp->l_flags & L_ENGINEER) {
475 max = lcp->l_item[I_SHELL];
476 shells = lp->lnd_item[I_SHELL];
477 for (m = 0; mines > 0 && m < 5; m++) {
480 shells = MIN(max, shells + 1);
483 sect.sct_mines = mines;
484 lp->lnd_item[I_SHELL] = shells;
487 if (chance(DMINE_LHITCHANCE(mines))) {
489 "%s %s,\nand hit a mine in %s while retreating!\n",
491 conditions[findcondition(code)].desc[orig],
492 xyas(newx, newy, lp->lnd_own));
493 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
495 if (lcp->l_flags & L_ENGINEER)
499 sect.sct_mines = mines;
502 putland(lp->lnd_uid, lp);
508 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
510 conditions[findcondition(code)].desc[orig],
511 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
513 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
515 conditions[findcondition(code)].desc[orig],
516 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
519 putland(lp->lnd_uid, lp);