2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * retreat.c: Retreat subroutines
30 * Known contributors to this file:
50 #include "prototypes.h"
53 static int findcondition(char);
54 static int retreat_land1(struct lndstr *, char, int);
55 static int retreat_ship1(struct shpstr *, char, int);
62 static struct ccode conditions[] = {
63 { 'i', { "retreated with a damaged friend",
65 { 't', { "retreated with a torpedoed ship",
66 "was hit by a torpedo" } },
67 { 's', { "retreated with a ship scared by sonar",
68 "detected a sonar ping" } },
69 { 'h', { "retreated with a helpless ship",
70 "was fired upon with no one able to defend it" } },
71 { 'b', { "retreated with a bombed friend",
73 { 'd', { "retreated with a depth-charged ship",
74 "was depth-charged" } },
75 { 'u', { "retreated with a boarded ship", "was boarded" } },
76 { 0, { "panicked", "panicked"} }
80 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
86 if (sp->shp_rflags & RET_INJURED)
87 retreat_ship(sp, 'i');
93 retreat_ship(struct shpstr *sp, char code)
98 if (sp->shp_rflags & RET_GROUP) {
99 snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
100 while (nxtitem(&ni, &ship))
101 if (ship.shp_own == sp->shp_own) {
102 if (ship.shp_uid == sp->shp_uid) {
103 retreat_ship1(sp, code, 1);
104 if (sp->shp_rpath[0] == 0)
107 retreat_ship1(&ship, code, 0);
108 getship(ship.shp_uid, &ship);
109 if (ship.shp_rpath[0] == 0) {
111 putship(ship.shp_uid, &ship);
116 retreat_ship1(sp, code, 1);
117 if (sp->shp_rpath[0] == 0)
123 retreat_ship1(struct shpstr *sp, char code, int orig)
126 /* Is this the originally scared ship, or a follower */
145 if (sp->shp_own == 0)
149 if (sp->shp_effic < SHIP_MINEFF) {
151 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
152 prship(sp), conditions[findcondition(code)].desc[orig]);
154 putship(sp->shp_uid, sp);
159 /* can't retreat a ship that's sailin, bad things happend */
162 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
163 prship(sp), conditions[findcondition(code)].desc[orig]);
165 putship(sp->shp_uid, sp);
169 /* check crew - uws don't count */
170 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
172 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
173 conditions[findcondition(code)].desc[orig]);
175 putship(sp->shp_uid, sp);
179 getsect(sp->shp_x, sp->shp_y, §);
180 switch (shp_check_nav(§, sp)) {
181 case CN_CONSTRUCTION:
183 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
184 prship(sp), conditions[findcondition(code)].desc[orig]);
186 putship(sp->shp_uid, sp);
190 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
191 prship(sp), conditions[findcondition(code)].desc[orig]);
193 putship(sp->shp_uid, sp);
201 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
202 prship(sp), conditions[findcondition(code)].desc[orig]);
204 putship(sp->shp_uid, sp);
209 if (sp->shp_mobil <= 0.0) {
211 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
212 prship(sp), conditions[findcondition(code)].desc[orig]);
214 putship(sp->shp_uid, sp);
218 n = (-1 * MAX_RETREAT);
220 while (!stopping && n) {
222 if (sp->shp_rpath[0] == 0) {
226 if (sp->shp_mobil <= 0.0) {
228 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
229 prship(sp), conditions[findcondition(code)].desc[orig]);
231 putship(sp->shp_uid, sp);
234 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
235 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
246 mcp = &mchr[(int)sp->shp_type];
247 newx = xnorm(sp->shp_x + dx);
248 newy = ynorm(sp->shp_y + dy);
249 mobcost = shp_mobcost(sp);
251 getsect(newx, newy, §);
252 if (shp_check_nav(§, sp) != CN_NAVIGABLE ||
253 (sect.sct_own && sect.sct_own != sp->shp_own &&
254 getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
255 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
256 prship(sp), conditions[findcondition(code)].desc[orig],
257 xyas(newx, newy, sp->shp_own));
259 putship(sp->shp_uid, sp);
264 sp->shp_mobil -= mobcost;
268 mines = sect.sct_mines;
270 if ((mcp->m_flags & M_SWEEP) && sect.sct_type == SCT_WATER) {
271 sp->shp_mobil -= mobcost;
272 max = mcp->m_item[I_SHELL];
273 shells = sp->shp_item[I_SHELL];
274 for (m = 0; mines > 0 && m < 5; m++) {
277 shells = MIN(max, shells + 1);
278 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
282 if (sect.sct_mines != mines) {
284 "%s cleared %d mine%s in %s while retreating\n",
285 prship(sp), sect.sct_mines-mines, splur(sect.sct_mines-mines),
286 xyas(newx, newy, sp->shp_own));
287 sect.sct_mines = mines;
288 sp->shp_item[I_SHELL] = shells;
292 writemap(sp->shp_own);
294 if (sect.sct_type == SCT_WATER && mines > 0
295 && chance(DMINE_HITCHANCE(mines))) {
297 "%s %s,\nand hit a mine in %s while retreating!\n",
298 prship(sp), conditions[findcondition(code)].desc[orig],
299 xyas(newx, newy, sp->shp_own));
300 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
304 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
305 writemap(sp->shp_own);
306 sect.sct_mines = mines;
309 putship(sp->shp_uid, sp);
315 wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
316 prship(sp), conditions[findcondition(code)].desc[orig],
317 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
319 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
321 conditions[findcondition(code)].desc[orig],
322 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
325 putship(sp->shp_uid, sp);
330 findcondition(char code)
334 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
335 CANT_HAPPEN(!conditions[i].code);
340 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
346 if (lp->lnd_rflags & RET_INJURED)
347 retreat_land(lp, 'i');
353 retreat_land(struct lndstr *lp, char code)
358 if (lp->lnd_rflags & RET_GROUP) {
359 snxtitem_group(&ni, EF_SHIP, 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(lp, code, 1);
364 if (lp->lnd_rpath[0] == 0)
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);
376 retreat_land1(lp, code, 1);
377 if (lp->lnd_rpath[0] == 0)
383 retreat_land1(struct lndstr *lp, char code, int orig)
386 /* Is this the originally scared unit, or a follower */
404 if (lp->lnd_own == 0)
408 if (lp->lnd_effic < LAND_MINEFF) {
410 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
411 prland(lp), conditions[findcondition(code)].desc[orig]);
413 putland(lp->lnd_uid, lp);
417 getsect(lp->lnd_x, lp->lnd_y, §);
419 if (lp->lnd_mobil <= 0.0) {
421 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
422 prland(lp), conditions[findcondition(code)].desc[orig]);
424 putland(lp->lnd_uid, lp);
428 n = (-1 * MAX_RETREAT);
430 while (!stopping && n) {
432 if (lp->lnd_rpath[0] == 0) {
436 if (lp->lnd_mobil <= 0.0) {
438 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
439 prland(lp), conditions[findcondition(code)].desc[orig]);
441 putland(lp->lnd_uid, lp);
444 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
445 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
456 lcp = &lchr[(int)lp->lnd_type];
457 newx = xnorm(lp->lnd_x + dx);
458 newy = ynorm(lp->lnd_y + dy);
460 getsect(newx, newy, §);
461 mobcost = lnd_mobcost(lp, §);
463 || sect.sct_type == SCT_MOUNT
464 || sect.sct_own != lp->lnd_own) {
465 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
467 conditions[findcondition(code)].desc[orig],
468 xyas(newx, newy, lp->lnd_own));
470 putland(lp->lnd_uid, lp);
475 lp->lnd_mobil -= mobcost;
479 mines = sect.sct_mines;
480 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
481 (sect.sct_oldown != lp->lnd_own)) {
482 max = lcp->l_item[I_SHELL];
483 shells = lp->lnd_item[I_SHELL];
484 for (m = 0; mines > 0 && m < 5; m++) {
487 shells = MIN(max, shells + 1);
490 sect.sct_mines = mines;
491 lp->lnd_item[I_SHELL] = shells;
494 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
495 chance(DMINE_LHITCHANCE(mines))) {
497 "%s %s,\nand hit a mine in %s while retreating!\n",
499 conditions[findcondition(code)].desc[orig],
500 xyas(newx, newy, lp->lnd_own));
501 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
505 sect.sct_mines = mines;
508 putland(lp->lnd_uid, lp);
514 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
516 conditions[findcondition(code)].desc[orig],
517 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
519 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
521 conditions[findcondition(code)].desc[orig],
522 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
525 putland(lp->lnd_uid, lp);