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(s_char);
54 static int retreat_land1(struct lndstr *, s_char, int);
55 static int retreat_ship1(struct shpstr *, s_char, int);
63 "retreated with a damaged friend", "was damaged",},}, {
65 "retreated with a torpedoed ship", "was hit by a torpedo",},}, {
67 "retreated with a ship scared by sonar",
68 "detected a sonar ping",},}, {
70 "retreated with a helpless ship",
71 "was fired upon with no one able to defend it",},}, {
73 "retreated with a bombed friend", "was bombed",},}, {
75 "retreated with a depth-charged ship", "was depth-charged",},}, {
77 "retreated with a boarded ship", "was boarded",},}, {
82 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
88 if (sp->shp_rflags & RET_INJURED)
89 retreat_ship(sp, 'i');
95 retreat_ship(struct shpstr *sp, s_char code)
101 if (sp->shp_rflags & RET_GROUP) {
102 buf[0] = sp->shp_fleet;
104 snxtitem(&ni, EF_SHIP, buf);
105 while (nxtitem(&ni, &ship))
106 if ((ship.shp_fleet == buf[0]) &&
107 (ship.shp_own == sp->shp_own)) {
108 if (ship.shp_uid == sp->shp_uid) {
109 retreat_ship1(sp, code, 1);
110 if (sp->shp_rpath[0] == 0)
113 retreat_ship1(&ship, code, 0);
114 getship(ship.shp_uid, &ship);
115 if (ship.shp_rpath[0] == 0) {
117 putship(ship.shp_uid, &ship);
122 retreat_ship1(sp, code, 1);
123 if (sp->shp_rpath[0] == 0)
129 retreat_ship1(struct shpstr *sp, s_char code, int orig)
132 /* Is this the originally scared ship, or a follower */
152 if (sp->shp_own == 0)
156 code = tolower(code);
159 if (sp->shp_effic < SHIP_MINEFF) {
161 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
162 prship(sp), conditions[findcondition(code)].desc[orig]);
164 putship(sp->shp_uid, sp);
169 /* can't retreat a ship that's sailin, bad things happend */
172 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
173 prship(sp), conditions[findcondition(code)].desc[orig]);
175 putship(sp->shp_uid, sp);
179 /* check crew - uws don't count */
180 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
182 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
183 conditions[findcondition(code)].desc[orig]);
185 putship(sp->shp_uid, sp);
189 getsect(sp->shp_x, sp->shp_y, §);
190 switch (shp_check_nav(§, sp)) {
191 case CN_CONSTRUCTION:
193 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
194 prship(sp), conditions[findcondition(code)].desc[orig]);
196 putship(sp->shp_uid, sp);
200 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
201 prship(sp), conditions[findcondition(code)].desc[orig]);
203 putship(sp->shp_uid, sp);
211 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
212 prship(sp), conditions[findcondition(code)].desc[orig]);
214 putship(sp->shp_uid, sp);
219 if (sp->shp_mobil <= 0.0) {
221 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
222 prship(sp), conditions[findcondition(code)].desc[orig]);
224 putship(sp->shp_uid, sp);
228 n = (-1 * MAX_RETREAT);
231 while (!stopping && n) {
233 if (sp->shp_rpath[0] == 0) {
237 if (sp->shp_mobil <= 0.0) {
239 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
240 prship(sp), conditions[findcondition(code)].desc[orig]);
242 putship(sp->shp_uid, sp);
245 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
246 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
257 mcp = &mchr[(int)sp->shp_type];
258 newx = xnorm(sp->shp_x + dx);
259 newy = ynorm(sp->shp_y + dy);
260 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
261 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
263 getsect(newx, newy, §);
264 if (shp_check_nav(§, sp) != CN_NAVIGABLE ||
265 (sect.sct_own && !player->owner &&
266 getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
267 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
268 prship(sp), conditions[findcondition(code)].desc[orig],
269 xyas(newx, newy, sp->shp_own));
271 putship(sp->shp_uid, sp);
276 sp->shp_mobil -= mobcost;
280 mines = sect.sct_mines;
282 if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
283 max = mcp->m_item[I_SHELL];
284 shells = sp->shp_item[I_SHELL];
285 for (m = 0; mines > 0 && m < 5; m++) {
288 shells = MIN(max, shells + 1);
289 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
293 if (sect.sct_mines != mines) {
295 "%s cleared %d mine%s in %s while retreating\n",
296 prship(sp), sect.sct_mines-mines, splur(sect.sct_mines-mines),
297 xyas(newx, newy, sp->shp_own));
299 sect.sct_mines = mines;
300 sect.sct_item[I_SHELL] = shells;
303 writemap(sp->shp_own);
305 if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
307 "%s %s,\nand hit a mine in %s while retreating!\n",
308 prship(sp), conditions[findcondition(code)].desc[orig],
309 xyas(newx, newy, sp->shp_own));
310 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
314 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
315 writemap(sp->shp_own);
316 sect.sct_mines = mines;
318 if (sp->shp_effic < SHIP_MINEFF)
321 putship(sp->shp_uid, sp);
329 wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
330 conditions[findcondition(code)].desc[orig],
331 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
333 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
335 conditions[findcondition(code)].desc[orig],
336 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
339 putship(sp->shp_uid, sp);
344 findcondition(s_char code)
349 while (conditions[x].code) {
350 if (conditions[x].code == code)
359 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
365 if (lp->lnd_rflags & RET_INJURED)
366 retreat_land(lp, 'i');
372 retreat_land(struct lndstr *lp, s_char code)
378 if (lp->lnd_rflags & RET_GROUP) {
379 buf[0] = lp->lnd_army;
381 snxtitem(&ni, EF_SHIP, buf);
382 while (nxtitem(&ni, &land))
383 if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
384 if (land.lnd_uid == lp->lnd_uid) {
385 retreat_land1(lp, code, 1);
386 if (lp->lnd_rpath[0] == 0)
389 retreat_land1(&land, code, 0);
390 getland(land.lnd_uid, &land);
391 if (land.lnd_rpath[0] == 0) {
393 putland(land.lnd_uid, &land);
398 retreat_land1(lp, code, 1);
399 if (lp->lnd_rpath[0] == 0)
405 retreat_land1(struct lndstr *lp, s_char code, int orig)
408 /* Is this the originally scared unit, or a follower */
427 if (lp->lnd_own == 0)
431 code = tolower(code);
434 if (lp->lnd_effic < LAND_MINEFF) {
436 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
437 prland(lp), conditions[findcondition(code)].desc[orig]);
439 putland(lp->lnd_uid, lp);
443 getsect(lp->lnd_x, lp->lnd_y, §);
445 if (lp->lnd_mobil <= 0.0) {
447 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
448 prland(lp), conditions[findcondition(code)].desc[orig]);
450 putland(lp->lnd_uid, lp);
454 n = (-1 * MAX_RETREAT);
457 while (!stopping && n) {
459 if (lp->lnd_rpath[0] == 0) {
463 if (lp->lnd_mobil <= 0.0) {
465 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
466 prland(lp), conditions[findcondition(code)].desc[orig]);
468 putland(lp->lnd_uid, lp);
471 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
472 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
483 lcp = &lchr[(int)lp->lnd_type];
484 newx = xnorm(lp->lnd_x + dx);
485 newy = ynorm(lp->lnd_y + dy);
487 getsect(newx, newy, §);
488 if ((sect.sct_type == SCT_WATER) ||
489 (sect.sct_type == SCT_MOUNT) ||
490 (sect.sct_type == SCT_SANCT) ||
491 (sect.sct_type == SCT_WASTE) ||
492 (sect.sct_own != lp->lnd_own)) {
493 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
495 conditions[findcondition(code)].desc[orig],
496 xyas(newx, newy, lp->lnd_own));
498 putland(lp->lnd_uid, lp);
501 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
504 lp->lnd_mobil -= mobcost;
508 mines = sect.sct_mines;
509 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
510 (sect.sct_oldown != lp->lnd_own)) {
511 max = lcp->l_item[I_SHELL];
512 shells = lp->lnd_item[I_SHELL];
513 for (m = 0; mines > 0 && m < 5; m++) {
516 shells = MIN(max, shells + 1);
519 sect.sct_mines = mines;
520 lp->lnd_item[I_SHELL] = shells;
523 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
524 chance(DMINE_HITCHANCE(mines))) {
526 "%s %s,\nand hit a mine in %s while retreating!\n",
528 conditions[findcondition(code)].desc[orig],
529 xyas(newx, newy, lp->lnd_own));
530 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
534 sect.sct_mines = mines;
536 if (lp->lnd_effic < LAND_MINEFF)
539 putland(lp->lnd_uid, lp);
547 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
549 conditions[findcondition(code)].desc[orig],
550 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
552 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
554 conditions[findcondition(code)].desc[orig],
555 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
558 putland(lp->lnd_uid, lp);