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);
347 switch (dchr[sect->sct_type].d_flg & 03) {
352 if (sect->sct_effic < 2)
353 return CN_CONSTRUCTION;
356 if (sect->sct_effic < 60)
357 return CN_CONSTRUCTION;
360 return CN_LANDLOCKED;
367 findcondition(s_char code)
372 while (conditions[x].code) {
373 if (conditions[x].code == code)
382 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
388 if (lp->lnd_rflags & RET_INJURED)
389 retreat_land(lp, 'i');
395 retreat_land(struct lndstr *lp, s_char code)
401 if (lp->lnd_rflags & RET_GROUP) {
402 buf[0] = lp->lnd_army;
404 snxtitem(&ni, EF_SHIP, buf);
405 while (nxtitem(&ni, &land))
406 if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
407 if (land.lnd_uid == lp->lnd_uid) {
408 retreat_land1(lp, code, 1);
409 if (lp->lnd_rpath[0] == 0)
412 retreat_land1(&land, code, 0);
413 getland(land.lnd_uid, &land);
414 if (land.lnd_rpath[0] == 0) {
416 putland(land.lnd_uid, &land);
421 retreat_land1(lp, code, 1);
422 if (lp->lnd_rpath[0] == 0)
428 retreat_land1(struct lndstr *lp, s_char code, int orig)
431 /* Is this the originally scared unit, or a follower */
450 if (lp->lnd_own == 0)
454 code = tolower(code);
457 if (lp->lnd_effic < LAND_MINEFF) {
459 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
460 prland(lp), conditions[findcondition(code)].desc[orig]);
462 putland(lp->lnd_uid, lp);
466 getsect(lp->lnd_x, lp->lnd_y, §);
468 if (lp->lnd_mobil <= 0.0) {
470 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
471 prland(lp), conditions[findcondition(code)].desc[orig]);
473 putland(lp->lnd_uid, lp);
477 n = (-1 * MAX_RETREAT);
480 while ((!stopping) && n) {
482 if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
486 if (lp->lnd_mobil <= 0.0) {
488 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
489 prland(lp), conditions[findcondition(code)].desc[orig]);
491 putland(lp->lnd_uid, lp);
494 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
495 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
506 lcp = &lchr[(int)lp->lnd_type];
507 newx = xnorm(lp->lnd_x + dx);
508 newy = ynorm(lp->lnd_y + dy);
510 getsect(newx, newy, §);
511 if ((sect.sct_type == SCT_WATER) ||
512 (sect.sct_type == SCT_MOUNT) ||
513 (sect.sct_type == SCT_SANCT) ||
514 (sect.sct_type == SCT_WASTE) ||
515 (sect.sct_own != lp->lnd_own)) {
516 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
518 conditions[findcondition(code)].desc[orig],
519 xyas(newx, newy, lp->lnd_own));
521 putland(lp->lnd_uid, lp);
524 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
527 lp->lnd_mobil -= mobcost;
531 mines = sect.sct_mines;
532 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
533 (sect.sct_oldown != lp->lnd_own)) {
534 max = lcp->l_item[I_SHELL];
535 shells = lp->lnd_item[I_SHELL];
536 for (m = 0; mines > 0 && m < 5; m++) {
539 shells = min(max, shells + 1);
542 sect.sct_mines = mines;
543 lp->lnd_item[I_SHELL] = shells;
546 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
547 chance(DMINE_HITCHANCE(mines))) {
549 "%s %s,\nand hit a mine in %s while retreating!\n",
551 conditions[findcondition(code)].desc[orig],
552 xyas(newx, newy, lp->lnd_own));
553 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
557 sect.sct_mines = mines;
559 if (lp->lnd_effic < LAND_MINEFF)
562 putland(lp->lnd_uid, lp);
570 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
572 conditions[findcondition(code)].desc[orig],
573 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
575 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
577 conditions[findcondition(code)].desc[orig],
578 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
581 putland(lp->lnd_uid, lp);