2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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:
49 #include "prototypes.h"
52 static int findcondition(s_char);
53 static int retreat_land1(struct lndstr *, s_char, int);
54 static int retreat_ship1(struct shpstr *, s_char, int);
62 "retreated with a damaged friend", "was damaged",},}, {
64 "retreated with a torpedoed ship", "was hit by a torpedo",},}, {
66 "retreated with a ship scared by sonar",
67 "detected a sonar ping",},}, {
69 "retreated with a helpless ship",
70 "was fired upon with no one able to defend it",},}, {
72 "retreated with a bombed friend", "was bombed",},}, {
74 "retreated with a depth-charged ship", "was depth-charged",},}, {
76 "retreated with a boarded ship", "was boarded",},}, {
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, s_char code)
100 if (sp->shp_rflags & RET_GROUP) {
101 buf[0] = sp->shp_fleet;
103 snxtitem(&ni, EF_SHIP, buf);
104 while (nxtitem(&ni, &ship))
105 if ((ship.shp_fleet == buf[0]) &&
106 (ship.shp_own == sp->shp_own)) {
107 if (ship.shp_uid == sp->shp_uid) {
108 retreat_ship1(sp, code, 1);
109 if (sp->shp_rpath[0] == 0)
112 retreat_ship1(&ship, code, 0);
113 getship(ship.shp_uid, &ship);
114 if (ship.shp_rpath[0] == 0) {
116 putship(ship.shp_uid, &ship);
121 retreat_ship1(sp, code, 1);
122 if (sp->shp_rpath[0] == 0)
128 retreat_ship1(struct shpstr *sp, s_char code, int orig)
131 /* Is this the originally scared ship, or a follower */
151 if (sp->shp_own == 0)
155 code = tolower(code);
158 if (sp->shp_effic < SHIP_MINEFF) {
160 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
161 prship(sp), conditions[findcondition(code)].desc[orig]);
163 putship(sp->shp_uid, sp);
168 /* can't retreat a ship that's sailin, bad things happend */
171 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
172 prship(sp), conditions[findcondition(code)].desc[orig]);
174 putship(sp->shp_uid, sp);
178 /* check crew - uws don't count */
179 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
181 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
182 conditions[findcondition(code)].desc[orig]);
184 putship(sp->shp_uid, sp);
188 getsect(sp->shp_x, sp->shp_y, §);
189 switch (check_nav(§)) {
190 case CN_CONSTRUCTION:
192 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
193 prship(sp), conditions[findcondition(code)].desc[orig]);
195 putship(sp->shp_uid, sp);
199 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
200 prship(sp), conditions[findcondition(code)].desc[orig]);
202 putship(sp->shp_uid, sp);
210 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
211 prship(sp), conditions[findcondition(code)].desc[orig]);
213 putship(sp->shp_uid, sp);
218 if (sp->shp_mobil <= 0.0) {
220 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
221 prship(sp), conditions[findcondition(code)].desc[orig]);
223 putship(sp->shp_uid, sp);
227 n = (-1 * MAX_RETREAT);
230 while ((!stopping) && n) {
232 if (sp->shp_rpath[0] == 0 || sp->shp_rpath[0] == 0) {
236 if (sp->shp_mobil <= 0.0) {
238 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
239 prship(sp), conditions[findcondition(code)].desc[orig]);
241 putship(sp->shp_uid, sp);
244 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_VIEW);
245 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
256 mcp = &mchr[(int)sp->shp_type];
257 newx = xnorm(sp->shp_x + dx);
258 newy = ynorm(sp->shp_y + dy);
259 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
260 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
262 getsect(newx, newy, §);
263 if (check_nav(§) != CN_NAVIGABLE ||
264 (sect.sct_own && !player->owner &&
265 getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
266 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
267 prship(sp), conditions[findcondition(code)].desc[orig],
268 xyas(newx, newy, sp->shp_own));
270 putship(sp->shp_uid, sp);
275 sp->shp_mobil -= mobcost;
279 mines = sect.sct_mines;
281 if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
282 max = mcp->m_item[I_SHELL];
283 shells = sp->shp_item[I_SHELL];
284 for (m = 0; mines > 0 && m < 5; m++) {
287 shells = min(max, shells + 1);
288 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
292 if (sect.sct_mines != mines) {
294 "%s cleared %d mine%s in %s while retreating\n",
295 prship(sp), sect.sct_mines-mines, splur(sect.sct_mines-mines),
296 xyas(newx, newy, sp->shp_own));
298 sect.sct_mines = mines;
299 sect.sct_item[I_SHELL] = shells;
302 writemap(sp->shp_own);
304 if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
306 "%s %s,\nand hit a mine in %s while retreating!\n",
307 prship(sp), conditions[findcondition(code)].desc[orig],
308 xyas(newx, newy, sp->shp_own));
309 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
313 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
314 writemap(sp->shp_own);
315 sect.sct_mines = mines;
317 if (sp->shp_effic < SHIP_MINEFF)
320 putship(sp->shp_uid, sp);
328 wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
329 conditions[findcondition(code)].desc[orig],
330 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
332 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
334 conditions[findcondition(code)].desc[orig],
335 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
338 putship(sp->shp_uid, sp);
343 findcondition(s_char code)
348 while (conditions[x].code) {
349 if (conditions[x].code == code)
358 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
364 if (lp->lnd_rflags & RET_INJURED)
365 retreat_land(lp, 'i');
371 retreat_land(struct lndstr *lp, s_char code)
377 if (lp->lnd_rflags & RET_GROUP) {
378 buf[0] = lp->lnd_army;
380 snxtitem(&ni, EF_SHIP, buf);
381 while (nxtitem(&ni, &land))
382 if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
383 if (land.lnd_uid == lp->lnd_uid) {
384 retreat_land1(lp, code, 1);
385 if (lp->lnd_rpath[0] == 0)
388 retreat_land1(&land, code, 0);
389 getland(land.lnd_uid, &land);
390 if (land.lnd_rpath[0] == 0) {
392 putland(land.lnd_uid, &land);
397 retreat_land1(lp, code, 1);
398 if (lp->lnd_rpath[0] == 0)
404 retreat_land1(struct lndstr *lp, s_char code, int orig)
407 /* Is this the originally scared unit, or a follower */
426 if (lp->lnd_own == 0)
430 code = tolower(code);
433 if (lp->lnd_effic < LAND_MINEFF) {
435 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
436 prland(lp), conditions[findcondition(code)].desc[orig]);
438 putland(lp->lnd_uid, lp);
442 getsect(lp->lnd_x, lp->lnd_y, §);
444 if (lp->lnd_mobil <= 0.0) {
446 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
447 prland(lp), conditions[findcondition(code)].desc[orig]);
449 putland(lp->lnd_uid, lp);
453 n = (-1 * MAX_RETREAT);
456 while ((!stopping) && n) {
458 if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
462 if (lp->lnd_mobil <= 0.0) {
464 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
465 prland(lp), conditions[findcondition(code)].desc[orig]);
467 putland(lp->lnd_uid, lp);
470 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
471 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
482 lcp = &lchr[(int)lp->lnd_type];
483 newx = xnorm(lp->lnd_x + dx);
484 newy = ynorm(lp->lnd_y + dy);
486 getsect(newx, newy, §);
487 if ((sect.sct_type == SCT_WATER) ||
488 (sect.sct_type == SCT_MOUNT) ||
489 (sect.sct_type == SCT_SANCT) ||
490 (sect.sct_type == SCT_WASTE) ||
491 (sect.sct_own != lp->lnd_own)) {
492 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
494 conditions[findcondition(code)].desc[orig],
495 xyas(newx, newy, lp->lnd_own));
497 putland(lp->lnd_uid, lp);
500 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
503 lp->lnd_mobil -= mobcost;
507 mines = sect.sct_mines;
508 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
509 (sect.sct_oldown != lp->lnd_own)) {
510 max = lcp->l_item[I_SHELL];
511 shells = lp->lnd_item[I_SHELL];
512 for (m = 0; mines > 0 && m < 5; m++) {
515 shells = min(max, shells + 1);
518 sect.sct_mines = mines;
519 lp->lnd_item[I_SHELL] = shells;
522 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
523 chance(DMINE_HITCHANCE(mines))) {
525 "%s %s,\nand hit a mine in %s while retreating!\n",
527 conditions[findcondition(code)].desc[orig],
528 xyas(newx, newy, lp->lnd_own));
529 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
533 sect.sct_mines = mines;
535 if (lp->lnd_effic < LAND_MINEFF)
538 putland(lp->lnd_uid, lp);
546 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
548 conditions[findcondition(code)].desc[orig],
549 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
551 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
553 conditions[findcondition(code)].desc[orig],
554 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
557 putland(lp->lnd_uid, lp);