2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 boared 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, (s_char *)&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 */
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 (check_nav(§)) {
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 || 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_VIEW);
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 (check_nav(§) != 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;
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);
290 sect.sct_mines = mines;
291 sect.sct_item[I_SHELL] = shells;
294 if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
296 "%s %s,\nand hit a mine in %s while retreating!\n",
297 prship(sp), conditions[findcondition(code)].desc[orig],
298 xyas(newx, newy, sp->shp_own));
299 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
303 sect.sct_mines = mines;
305 if (sp->shp_effic < SHIP_MINEFF)
308 putship(sp->shp_uid, sp);
316 wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
317 conditions[findcondition(code)].desc[orig],
318 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
320 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
322 conditions[findcondition(code)].desc[orig],
323 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
326 putship(sp->shp_uid, sp);
335 switch (dchr[sect->sct_type].d_flg & 03) {
340 if (sect->sct_effic < 2)
341 return CN_CONSTRUCTION;
344 if (sect->sct_effic < 60)
345 return CN_CONSTRUCTION;
348 return CN_LANDLOCKED;
355 findcondition(s_char code)
360 while (conditions[x].code) {
361 if (conditions[x].code == code)
370 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
376 if (lp->lnd_rflags & RET_INJURED)
377 retreat_land(lp, 'i');
383 retreat_land(struct lndstr *lp, s_char code)
389 if (lp->lnd_rflags & RET_GROUP) {
390 buf[0] = lp->lnd_army;
392 snxtitem(&ni, EF_SHIP, buf);
393 while (nxtitem(&ni, (s_char *)&land))
394 if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
395 if (land.lnd_uid == lp->lnd_uid) {
396 retreat_land1(lp, code, 1);
397 if (lp->lnd_rpath[0] == 0)
400 retreat_land1(&land, code, 0);
401 getland(land.lnd_uid, &land);
402 if (land.lnd_rpath[0] == 0) {
404 putland(land.lnd_uid, &land);
409 retreat_land1(lp, code, 1);
410 if (lp->lnd_rpath[0] == 0)
416 retreat_land1(struct lndstr *lp, s_char code, int orig)
419 /* Is this the originally scared unit, or a follower */
438 if (lp->lnd_own == 0)
442 code = tolower(code);
445 if (lp->lnd_effic < LAND_MINEFF) {
447 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
448 prland(lp), conditions[findcondition(code)].desc[orig]);
450 putland(lp->lnd_uid, lp);
454 getsect(lp->lnd_x, lp->lnd_y, §);
456 if (lp->lnd_mobil <= 0.0) {
458 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
459 prland(lp), conditions[findcondition(code)].desc[orig]);
461 putland(lp->lnd_uid, lp);
465 n = (-1 * MAX_RETREAT);
468 while ((!stopping) && n) {
470 if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
474 if (lp->lnd_mobil <= 0.0) {
476 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
477 prland(lp), conditions[findcondition(code)].desc[orig]);
479 putland(lp->lnd_uid, lp);
482 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
483 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
494 lcp = &lchr[(int)lp->lnd_type];
495 newx = xnorm(lp->lnd_x + dx);
496 newy = ynorm(lp->lnd_y + dy);
498 getsect(newx, newy, §);
499 if ((sect.sct_type == SCT_WATER) ||
500 (sect.sct_type == SCT_MOUNT) ||
501 (sect.sct_type == SCT_SANCT) ||
502 (sect.sct_type == SCT_WASTE) ||
503 (sect.sct_own != lp->lnd_own)) {
504 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
506 conditions[findcondition(code)].desc[orig],
507 xyas(newx, newy, lp->lnd_own));
509 putland(lp->lnd_uid, lp);
512 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
515 lp->lnd_mobil -= mobcost;
519 mines = sect.sct_mines;
520 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
521 (sect.sct_oldown != lp->lnd_own)) {
522 max = lcp->l_item[I_SHELL];
523 shells = lp->lnd_item[I_SHELL];
524 for (m = 0; mines > 0 && m < 5; m++) {
527 shells = min(max, shells + 1);
530 sect.sct_mines = mines;
531 lp->lnd_item[I_SHELL] = shells;
534 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
535 chance(DMINE_HITCHANCE(mines))) {
537 "%s %s,\nand hit a mine in %s while retreating!\n",
539 conditions[findcondition(code)].desc[orig],
540 xyas(newx, newy, lp->lnd_own));
541 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
545 sect.sct_mines = mines;
547 if (lp->lnd_effic < LAND_MINEFF)
550 putland(lp->lnd_uid, lp);
558 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
560 conditions[findcondition(code)].desc[orig],
561 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
563 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
565 conditions[findcondition(code)].desc[orig],
566 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
569 putland(lp->lnd_uid, lp);