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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * retreat.c: Retreat subroutines
30 * Known contributors to this file:
51 #include "prototypes.h"
54 static int findcondition(s_char);
55 static int retreat_land1(struct lndstr *, s_char, int);
56 static int retreat_ship1(struct shpstr *, s_char, int);
64 "retreated with a damaged friend", "was damaged",},}, {
66 "retreated with a torpedoed ship", "was hit by a torpedo",},}, {
68 "retreated with a ship scared by sonar",
69 "detected a sonar ping",},}, {
71 "retreated with a helpless ship",
72 "was fired upon with no one able to defend it",},}, {
74 "retreated with a bombed friend", "was bombed",},}, {
76 "retreated with a depth-charged ship", "was depth-charged",},}, {
78 "retreated with a boarded ship", "was boarded",},}, {
83 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
89 if (sp->shp_rflags & RET_INJURED)
90 retreat_ship(sp, 'i');
96 retreat_ship(struct shpstr *sp, s_char code)
102 if (sp->shp_rflags & RET_GROUP) {
103 buf[0] = sp->shp_fleet;
105 snxtitem(&ni, EF_SHIP, buf);
106 while (nxtitem(&ni, &ship))
107 if ((ship.shp_fleet == buf[0]) &&
108 (ship.shp_own == sp->shp_own)) {
109 if (ship.shp_uid == sp->shp_uid) {
110 retreat_ship1(sp, code, 1);
111 if (sp->shp_rpath[0] == 0)
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);
123 retreat_ship1(sp, code, 1);
124 if (sp->shp_rpath[0] == 0)
130 retreat_ship1(struct shpstr *sp, s_char code, int orig)
133 /* Is this the originally scared ship, or a follower */
153 if (sp->shp_own == 0)
157 code = tolower(code);
160 if (sp->shp_effic < SHIP_MINEFF) {
162 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
163 prship(sp), conditions[findcondition(code)].desc[orig]);
165 putship(sp->shp_uid, sp);
170 /* can't retreat a ship that's sailin, bad things happend */
173 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
174 prship(sp), conditions[findcondition(code)].desc[orig]);
176 putship(sp->shp_uid, sp);
180 /* check crew - uws don't count */
181 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
183 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
184 conditions[findcondition(code)].desc[orig]);
186 putship(sp->shp_uid, sp);
190 getsect(sp->shp_x, sp->shp_y, §);
191 switch (check_nav(§)) {
192 case CN_CONSTRUCTION:
194 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
195 prship(sp), conditions[findcondition(code)].desc[orig]);
197 putship(sp->shp_uid, sp);
201 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
202 prship(sp), conditions[findcondition(code)].desc[orig]);
204 putship(sp->shp_uid, sp);
212 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
213 prship(sp), conditions[findcondition(code)].desc[orig]);
215 putship(sp->shp_uid, sp);
220 if (sp->shp_mobil <= 0.0) {
222 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
223 prship(sp), conditions[findcondition(code)].desc[orig]);
225 putship(sp->shp_uid, sp);
229 n = (-1 * MAX_RETREAT);
232 while (!stopping && n) {
234 if (sp->shp_rpath[0] == 0) {
238 if (sp->shp_mobil <= 0.0) {
240 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
241 prship(sp), conditions[findcondition(code)].desc[orig]);
243 putship(sp->shp_uid, sp);
246 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
247 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
258 mcp = &mchr[(int)sp->shp_type];
259 newx = xnorm(sp->shp_x + dx);
260 newy = ynorm(sp->shp_y + dy);
261 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
262 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
264 getsect(newx, newy, §);
265 if (check_nav(§) != CN_NAVIGABLE ||
266 (sect.sct_own && !player->owner &&
267 getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
268 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
269 prship(sp), conditions[findcondition(code)].desc[orig],
270 xyas(newx, newy, sp->shp_own));
272 putship(sp->shp_uid, sp);
277 sp->shp_mobil -= mobcost;
281 mines = sect.sct_mines;
283 if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
284 max = mcp->m_item[I_SHELL];
285 shells = sp->shp_item[I_SHELL];
286 for (m = 0; mines > 0 && m < 5; m++) {
289 shells = MIN(max, shells + 1);
290 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
294 if (sect.sct_mines != mines) {
296 "%s cleared %d mine%s in %s while retreating\n",
297 prship(sp), sect.sct_mines-mines, splur(sect.sct_mines-mines),
298 xyas(newx, newy, sp->shp_own));
300 sect.sct_mines = mines;
301 sect.sct_item[I_SHELL] = shells;
304 writemap(sp->shp_own);
306 if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
308 "%s %s,\nand hit a mine in %s while retreating!\n",
309 prship(sp), conditions[findcondition(code)].desc[orig],
310 xyas(newx, newy, sp->shp_own));
311 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
315 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
316 writemap(sp->shp_own);
317 sect.sct_mines = mines;
319 if (sp->shp_effic < SHIP_MINEFF)
322 putship(sp->shp_uid, sp);
330 wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
331 conditions[findcondition(code)].desc[orig],
332 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
334 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
336 conditions[findcondition(code)].desc[orig],
337 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
340 putship(sp->shp_uid, sp);
345 findcondition(s_char code)
350 while (conditions[x].code) {
351 if (conditions[x].code == code)
360 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
366 if (lp->lnd_rflags & RET_INJURED)
367 retreat_land(lp, 'i');
373 retreat_land(struct lndstr *lp, s_char code)
379 if (lp->lnd_rflags & RET_GROUP) {
380 buf[0] = lp->lnd_army;
382 snxtitem(&ni, EF_SHIP, buf);
383 while (nxtitem(&ni, &land))
384 if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
385 if (land.lnd_uid == lp->lnd_uid) {
386 retreat_land1(lp, code, 1);
387 if (lp->lnd_rpath[0] == 0)
390 retreat_land1(&land, code, 0);
391 getland(land.lnd_uid, &land);
392 if (land.lnd_rpath[0] == 0) {
394 putland(land.lnd_uid, &land);
399 retreat_land1(lp, code, 1);
400 if (lp->lnd_rpath[0] == 0)
406 retreat_land1(struct lndstr *lp, s_char code, int orig)
409 /* Is this the originally scared unit, or a follower */
428 if (lp->lnd_own == 0)
432 code = tolower(code);
435 if (lp->lnd_effic < LAND_MINEFF) {
437 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
438 prland(lp), conditions[findcondition(code)].desc[orig]);
440 putland(lp->lnd_uid, lp);
444 getsect(lp->lnd_x, lp->lnd_y, §);
446 if (lp->lnd_mobil <= 0.0) {
448 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
449 prland(lp), conditions[findcondition(code)].desc[orig]);
451 putland(lp->lnd_uid, lp);
455 n = (-1 * MAX_RETREAT);
458 while (!stopping && n) {
460 if (lp->lnd_rpath[0] == 0) {
464 if (lp->lnd_mobil <= 0.0) {
466 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
467 prland(lp), conditions[findcondition(code)].desc[orig]);
469 putland(lp->lnd_uid, lp);
472 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
473 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
484 lcp = &lchr[(int)lp->lnd_type];
485 newx = xnorm(lp->lnd_x + dx);
486 newy = ynorm(lp->lnd_y + dy);
488 getsect(newx, newy, §);
489 if ((sect.sct_type == SCT_WATER) ||
490 (sect.sct_type == SCT_MOUNT) ||
491 (sect.sct_type == SCT_SANCT) ||
492 (sect.sct_type == SCT_WASTE) ||
493 (sect.sct_own != lp->lnd_own)) {
494 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
496 conditions[findcondition(code)].desc[orig],
497 xyas(newx, newy, lp->lnd_own));
499 putland(lp->lnd_uid, lp);
502 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
505 lp->lnd_mobil -= mobcost;
509 mines = sect.sct_mines;
510 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
511 (sect.sct_oldown != lp->lnd_own)) {
512 max = lcp->l_item[I_SHELL];
513 shells = lp->lnd_item[I_SHELL];
514 for (m = 0; mines > 0 && m < 5; m++) {
517 shells = MIN(max, shells + 1);
520 sect.sct_mines = mines;
521 lp->lnd_item[I_SHELL] = shells;
524 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
525 chance(DMINE_HITCHANCE(mines))) {
527 "%s %s,\nand hit a mine in %s while retreating!\n",
529 conditions[findcondition(code)].desc[orig],
530 xyas(newx, newy, lp->lnd_own));
531 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
535 sect.sct_mines = mines;
537 if (lp->lnd_effic < LAND_MINEFF)
540 putland(lp->lnd_uid, lp);
548 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
550 conditions[findcondition(code)].desc[orig],
551 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
553 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
555 conditions[findcondition(code)].desc[orig],
556 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
559 putland(lp->lnd_uid, lp);