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 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, s_char code)
99 if (sp->shp_rflags & RET_GROUP) {
100 buf[0] = sp->shp_fleet;
102 snxtitem(&ni, EF_SHIP, buf);
103 while (nxtitem(&ni, &ship))
104 if ((ship.shp_fleet == buf[0]) &&
105 (ship.shp_own == sp->shp_own)) {
106 if (ship.shp_uid == sp->shp_uid) {
107 retreat_ship1(sp, code, 1);
108 if (sp->shp_rpath[0] == 0)
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);
120 retreat_ship1(sp, code, 1);
121 if (sp->shp_rpath[0] == 0)
127 retreat_ship1(struct shpstr *sp, s_char code, int orig)
130 /* Is this the originally scared ship, or a follower */
150 if (sp->shp_own == 0)
154 if (sp->shp_effic < SHIP_MINEFF) {
156 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
157 prship(sp), conditions[findcondition(code)].desc[orig]);
159 putship(sp->shp_uid, sp);
164 /* can't retreat a ship that's sailin, bad things happend */
167 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
168 prship(sp), conditions[findcondition(code)].desc[orig]);
170 putship(sp->shp_uid, sp);
174 /* check crew - uws don't count */
175 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
177 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
178 conditions[findcondition(code)].desc[orig]);
180 putship(sp->shp_uid, sp);
184 getsect(sp->shp_x, sp->shp_y, §);
185 switch (shp_check_nav(§, sp)) {
186 case CN_CONSTRUCTION:
188 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
189 prship(sp), conditions[findcondition(code)].desc[orig]);
191 putship(sp->shp_uid, sp);
195 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
196 prship(sp), conditions[findcondition(code)].desc[orig]);
198 putship(sp->shp_uid, sp);
206 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
207 prship(sp), conditions[findcondition(code)].desc[orig]);
209 putship(sp->shp_uid, sp);
214 if (sp->shp_mobil <= 0.0) {
216 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
217 prship(sp), conditions[findcondition(code)].desc[orig]);
219 putship(sp->shp_uid, sp);
223 n = (-1 * MAX_RETREAT);
226 while (!stopping && n) {
228 if (sp->shp_rpath[0] == 0) {
232 if (sp->shp_mobil <= 0.0) {
234 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
235 prship(sp), conditions[findcondition(code)].desc[orig]);
237 putship(sp->shp_uid, sp);
240 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
241 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
252 mcp = &mchr[(int)sp->shp_type];
253 newx = xnorm(sp->shp_x + dx);
254 newy = ynorm(sp->shp_y + dy);
255 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
256 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
258 getsect(newx, newy, §);
259 if (shp_check_nav(§, sp) != CN_NAVIGABLE ||
260 (sect.sct_own && !player->owner &&
261 getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
262 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
263 prship(sp), conditions[findcondition(code)].desc[orig],
264 xyas(newx, newy, sp->shp_own));
266 putship(sp->shp_uid, sp);
271 sp->shp_mobil -= mobcost;
275 mines = sect.sct_mines;
277 if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
278 max = mcp->m_item[I_SHELL];
279 shells = sp->shp_item[I_SHELL];
280 for (m = 0; mines > 0 && m < 5; m++) {
283 shells = MIN(max, shells + 1);
284 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
288 if (sect.sct_mines != mines) {
290 "%s cleared %d mine%s in %s while retreating\n",
291 prship(sp), sect.sct_mines-mines, splur(sect.sct_mines-mines),
292 xyas(newx, newy, sp->shp_own));
294 sect.sct_mines = mines;
295 sect.sct_item[I_SHELL] = shells;
298 writemap(sp->shp_own);
300 if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
302 "%s %s,\nand hit a mine in %s while retreating!\n",
303 prship(sp), conditions[findcondition(code)].desc[orig],
304 xyas(newx, newy, sp->shp_own));
305 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
309 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
310 writemap(sp->shp_own);
311 sect.sct_mines = mines;
313 if (sp->shp_effic < SHIP_MINEFF)
316 putship(sp->shp_uid, sp);
324 wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
325 conditions[findcondition(code)].desc[orig],
326 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
328 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
330 conditions[findcondition(code)].desc[orig],
331 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
334 putship(sp->shp_uid, sp);
339 findcondition(char code)
343 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
344 CANT_HAPPEN(!conditions[i].code);
349 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
355 if (lp->lnd_rflags & RET_INJURED)
356 retreat_land(lp, 'i');
362 retreat_land(struct lndstr *lp, s_char code)
368 if (lp->lnd_rflags & RET_GROUP) {
369 buf[0] = lp->lnd_army;
371 snxtitem(&ni, EF_SHIP, buf);
372 while (nxtitem(&ni, &land))
373 if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
374 if (land.lnd_uid == lp->lnd_uid) {
375 retreat_land1(lp, code, 1);
376 if (lp->lnd_rpath[0] == 0)
379 retreat_land1(&land, code, 0);
380 getland(land.lnd_uid, &land);
381 if (land.lnd_rpath[0] == 0) {
383 putland(land.lnd_uid, &land);
388 retreat_land1(lp, code, 1);
389 if (lp->lnd_rpath[0] == 0)
395 retreat_land1(struct lndstr *lp, s_char code, int orig)
398 /* Is this the originally scared unit, or a follower */
417 if (lp->lnd_own == 0)
421 if (lp->lnd_effic < LAND_MINEFF) {
423 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
424 prland(lp), conditions[findcondition(code)].desc[orig]);
426 putland(lp->lnd_uid, lp);
430 getsect(lp->lnd_x, lp->lnd_y, §);
432 if (lp->lnd_mobil <= 0.0) {
434 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
435 prland(lp), conditions[findcondition(code)].desc[orig]);
437 putland(lp->lnd_uid, lp);
441 n = (-1 * MAX_RETREAT);
444 while (!stopping && n) {
446 if (lp->lnd_rpath[0] == 0) {
450 if (lp->lnd_mobil <= 0.0) {
452 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
453 prland(lp), conditions[findcondition(code)].desc[orig]);
455 putland(lp->lnd_uid, lp);
458 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
459 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
470 lcp = &lchr[(int)lp->lnd_type];
471 newx = xnorm(lp->lnd_x + dx);
472 newy = ynorm(lp->lnd_y + dy);
474 getsect(newx, newy, §);
475 if ((sect.sct_type == SCT_WATER) ||
476 (sect.sct_type == SCT_MOUNT) ||
477 (sect.sct_type == SCT_SANCT) ||
478 (sect.sct_type == SCT_WASTE) ||
479 (sect.sct_own != lp->lnd_own)) {
480 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
482 conditions[findcondition(code)].desc[orig],
483 xyas(newx, newy, lp->lnd_own));
485 putland(lp->lnd_uid, lp);
488 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
491 lp->lnd_mobil -= mobcost;
495 mines = sect.sct_mines;
496 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
497 (sect.sct_oldown != lp->lnd_own)) {
498 max = lcp->l_item[I_SHELL];
499 shells = lp->lnd_item[I_SHELL];
500 for (m = 0; mines > 0 && m < 5; m++) {
503 shells = MIN(max, shells + 1);
506 sect.sct_mines = mines;
507 lp->lnd_item[I_SHELL] = shells;
510 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
511 chance(DMINE_HITCHANCE(mines))) {
513 "%s %s,\nand hit a mine in %s while retreating!\n",
515 conditions[findcondition(code)].desc[orig],
516 xyas(newx, newy, lp->lnd_own));
517 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
521 sect.sct_mines = mines;
523 if (lp->lnd_effic < LAND_MINEFF)
526 putland(lp->lnd_uid, lp);
534 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
536 conditions[findcondition(code)].desc[orig],
537 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
539 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
541 conditions[findcondition(code)].desc[orig],
542 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
545 putland(lp->lnd_uid, lp);