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 retreat_ship1(sp, code, 1);
100 if (sp->shp_rpath[0] == 0)
103 if (sp->shp_rflags & RET_GROUP) {
104 snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
105 while (nxtitem(&ni, &ship))
106 if (ship.shp_own == sp->shp_own) {
107 if (ship.shp_uid != sp->shp_uid) {
108 retreat_ship1(&ship, code, 0);
109 getship(ship.shp_uid, &ship);
110 if (ship.shp_rpath[0] == 0) {
112 putship(ship.shp_uid, &ship);
120 retreat_ship1(struct shpstr *sp, char code, int orig)
123 /* Is this the originally scared ship, or a follower */
141 if (sp->shp_own == 0)
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)) {
176 case CN_CONSTRUCTION:
178 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
179 prship(sp), conditions[findcondition(code)].desc[orig]);
181 putship(sp->shp_uid, sp);
185 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
186 prship(sp), conditions[findcondition(code)].desc[orig]);
188 putship(sp->shp_uid, sp);
195 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
196 prship(sp), conditions[findcondition(code)].desc[orig]);
198 putship(sp->shp_uid, sp);
202 if (sp->shp_mobil <= 0.0) {
204 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
205 prship(sp), conditions[findcondition(code)].desc[orig]);
207 putship(sp->shp_uid, sp);
213 while (!stopping && n) {
215 if (sp->shp_rpath[0] == 0) {
219 if (sp->shp_mobil <= 0.0) {
221 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
222 prship(sp), conditions[findcondition(code)].desc[orig]);
224 putship(sp->shp_uid, sp);
227 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
228 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
239 mcp = &mchr[(int)sp->shp_type];
240 newx = xnorm(sp->shp_x + dx);
241 newy = ynorm(sp->shp_y + dy);
242 mobcost = shp_mobcost(sp);
244 getsect(newx, newy, §);
245 if (shp_check_nav(§, sp) != CN_NAVIGABLE ||
247 && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
248 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
249 prship(sp), conditions[findcondition(code)].desc[orig],
250 xyas(newx, newy, sp->shp_own));
252 putship(sp->shp_uid, sp);
257 sp->shp_mobil -= mobcost;
262 mines = sect.sct_mines;
264 if (sect.sct_type != SCT_WATER || mines <= 0)
266 if (mcp->m_flags & M_SWEEP) {
267 max = mcp->m_item[I_SHELL];
268 shells = sp->shp_item[I_SHELL];
269 for (m = 0; mines > 0 && m < 5; m++) {
272 shells = MIN(max, shells + 1);
273 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
277 if (sect.sct_mines != mines) {
279 "%s cleared %d mine%s in %s while retreating\n",
280 prship(sp), sect.sct_mines - mines,
281 splur(sect.sct_mines - mines),
282 xyas(newx, newy, sp->shp_own));
283 sect.sct_mines = mines;
284 sp->shp_item[I_SHELL] = shells;
288 writemap(sp->shp_own);
290 if (chance(DMINE_HITCHANCE(mines))) {
292 "%s %s,\nand hit a mine in %s while retreating!\n",
293 prship(sp), conditions[findcondition(code)].desc[orig],
294 xyas(newx, newy, sp->shp_own));
295 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
299 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
300 writemap(sp->shp_own);
301 sect.sct_mines = mines;
304 putship(sp->shp_uid, sp);
310 wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
311 prship(sp), conditions[findcondition(code)].desc[orig],
312 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
314 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
316 conditions[findcondition(code)].desc[orig],
317 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
320 putship(sp->shp_uid, sp);
325 findcondition(char code)
329 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
330 CANT_HAPPEN(!conditions[i].code);
335 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
341 if (lp->lnd_rflags & RET_INJURED)
342 retreat_land(lp, 'i');
348 retreat_land(struct lndstr *lp, char code)
353 retreat_land1(lp, code, 1);
354 if (lp->lnd_rpath[0] == 0)
357 if (lp->lnd_rflags & RET_GROUP) {
358 snxtitem_group(&ni, EF_LAND, lp->lnd_army);
359 while (nxtitem(&ni, &land))
360 if (land.lnd_own == lp->lnd_own) {
361 if (land.lnd_uid != lp->lnd_uid) {
362 retreat_land1(&land, code, 0);
363 getland(land.lnd_uid, &land);
364 if (land.lnd_rpath[0] == 0) {
366 putland(land.lnd_uid, &land);
374 retreat_land1(struct lndstr *lp, char code, int orig)
377 /* Is this the originally scared unit, or a follower */
394 if (lp->lnd_own == 0)
397 if (lp->lnd_effic < LAND_MINEFF) {
399 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
400 prland(lp), conditions[findcondition(code)].desc[orig]);
402 putland(lp->lnd_uid, lp);
406 getsect(lp->lnd_x, lp->lnd_y, §);
408 if (lp->lnd_mobil <= 0.0) {
410 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
411 prland(lp), conditions[findcondition(code)].desc[orig]);
413 putland(lp->lnd_uid, lp);
419 while (!stopping && n) {
421 if (lp->lnd_rpath[0] == 0) {
425 if (lp->lnd_mobil <= 0.0) {
427 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
428 prland(lp), conditions[findcondition(code)].desc[orig]);
430 putland(lp->lnd_uid, lp);
433 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
434 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
445 lcp = &lchr[(int)lp->lnd_type];
446 newx = xnorm(lp->lnd_x + dx);
447 newy = ynorm(lp->lnd_y + dy);
449 getsect(newx, newy, §);
450 mobcost = lnd_mobcost(lp, §);
452 || sect.sct_type == SCT_MOUNT
453 || sect.sct_own != lp->lnd_own) {
454 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
456 conditions[findcondition(code)].desc[orig],
457 xyas(newx, newy, lp->lnd_own));
459 putland(lp->lnd_uid, lp);
464 lp->lnd_mobil -= mobcost;
469 mines = SCT_LANDMINES(§);
470 if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
472 if (lcp->l_flags & L_ENGINEER) {
473 max = lcp->l_item[I_SHELL];
474 shells = lp->lnd_item[I_SHELL];
475 for (m = 0; mines > 0 && m < 5; m++) {
478 shells = MIN(max, shells + 1);
481 sect.sct_mines = mines;
482 lp->lnd_item[I_SHELL] = shells;
485 if (chance(DMINE_LHITCHANCE(mines))) {
487 "%s %s,\nand hit a mine in %s while retreating!\n",
489 conditions[findcondition(code)].desc[orig],
490 xyas(newx, newy, lp->lnd_own));
491 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
493 if (lcp->l_flags & L_ENGINEER)
497 sect.sct_mines = mines;
500 putland(lp->lnd_uid, lp);
506 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
508 conditions[findcondition(code)].desc[orig],
509 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
511 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
513 conditions[findcondition(code)].desc[orig],
514 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
517 putland(lp->lnd_uid, lp);