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, (s_char *)&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 */
150 if (sp->shp_own == 0)
154 code = tolower(code);
157 if (sp->shp_effic < SHIP_MINEFF) {
159 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
160 prship(sp), conditions[findcondition(code)].desc[orig]);
162 putship(sp->shp_uid, sp);
167 /* can't retreat a ship that's sailin, bad things happend */
170 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
171 prship(sp), conditions[findcondition(code)].desc[orig]);
173 putship(sp->shp_uid, sp);
177 /* check crew - uws don't count */
178 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
180 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
181 conditions[findcondition(code)].desc[orig]);
183 putship(sp->shp_uid, sp);
187 getsect(sp->shp_x, sp->shp_y, §);
188 switch (check_nav(§)) {
189 case CN_CONSTRUCTION:
191 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
192 prship(sp), conditions[findcondition(code)].desc[orig]);
194 putship(sp->shp_uid, sp);
198 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
199 prship(sp), conditions[findcondition(code)].desc[orig]);
201 putship(sp->shp_uid, sp);
209 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
210 prship(sp), conditions[findcondition(code)].desc[orig]);
212 putship(sp->shp_uid, sp);
217 if (sp->shp_mobil <= 0.0) {
219 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
220 prship(sp), conditions[findcondition(code)].desc[orig]);
222 putship(sp->shp_uid, sp);
226 n = (-1 * MAX_RETREAT);
229 while ((!stopping) && n) {
231 if (sp->shp_rpath[0] == 0 || sp->shp_rpath[0] == 0) {
235 if (sp->shp_mobil <= 0.0) {
237 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
238 prship(sp), conditions[findcondition(code)].desc[orig]);
240 putship(sp->shp_uid, sp);
243 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_VIEW);
244 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
255 mcp = &mchr[(int)sp->shp_type];
256 newx = xnorm(sp->shp_x + dx);
257 newy = ynorm(sp->shp_y + dy);
258 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
259 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
261 getsect(newx, newy, §);
262 if (check_nav(§) != CN_NAVIGABLE ||
263 (sect.sct_own && !player->owner &&
264 getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
265 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
266 prship(sp), conditions[findcondition(code)].desc[orig],
267 xyas(newx, newy, sp->shp_own));
269 putship(sp->shp_uid, sp);
274 sp->shp_mobil -= mobcost;
278 mines = sect.sct_mines;
279 if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
280 max = mcp->m_item[I_SHELL];
281 shells = sp->shp_item[I_SHELL];
282 for (m = 0; mines > 0 && m < 5; m++) {
285 shells = min(max, shells + 1);
288 sect.sct_mines = mines;
289 sect.sct_item[I_SHELL] = shells;
292 if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
294 "%s %s,\nand hit a mine in %s while retreating!\n",
295 prship(sp), conditions[findcondition(code)].desc[orig],
296 xyas(newx, newy, sp->shp_own));
297 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
301 sect.sct_mines = mines;
303 if (sp->shp_effic < SHIP_MINEFF)
306 putship(sp->shp_uid, sp);
314 wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
315 conditions[findcondition(code)].desc[orig],
316 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
318 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
320 conditions[findcondition(code)].desc[orig],
321 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
324 putship(sp->shp_uid, sp);
333 switch (dchr[sect->sct_type].d_flg & 03) {
338 if (sect->sct_effic < 2)
339 return CN_CONSTRUCTION;
342 if (sect->sct_effic < 60)
343 return CN_CONSTRUCTION;
346 return CN_LANDLOCKED;
353 findcondition(s_char code)
358 while (conditions[x].code) {
359 if (conditions[x].code == code)
368 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
374 if (lp->lnd_rflags & RET_INJURED)
375 retreat_land(lp, 'i');
381 retreat_land(struct lndstr *lp, s_char code)
387 if (lp->lnd_rflags & RET_GROUP) {
388 buf[0] = lp->lnd_army;
390 snxtitem(&ni, EF_SHIP, buf);
391 while (nxtitem(&ni, (s_char *)&land))
392 if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
393 if (land.lnd_uid == lp->lnd_uid) {
394 retreat_land1(lp, code, 1);
395 if (lp->lnd_rpath[0] == 0)
398 retreat_land1(&land, code, 0);
399 getland(land.lnd_uid, &land);
400 if (land.lnd_rpath[0] == 0) {
402 putland(land.lnd_uid, &land);
407 retreat_land1(lp, code, 1);
408 if (lp->lnd_rpath[0] == 0)
414 retreat_land1(struct lndstr *lp, s_char code, int orig)
417 /* Is this the originally scared unit, or a follower */
436 if (lp->lnd_own == 0)
440 code = tolower(code);
443 if (lp->lnd_effic < LAND_MINEFF) {
445 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
446 prland(lp), conditions[findcondition(code)].desc[orig]);
448 putland(lp->lnd_uid, lp);
452 getsect(lp->lnd_x, lp->lnd_y, §);
454 if (lp->lnd_mobil <= 0.0) {
456 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
457 prland(lp), conditions[findcondition(code)].desc[orig]);
459 putland(lp->lnd_uid, lp);
463 n = (-1 * MAX_RETREAT);
466 while ((!stopping) && n) {
468 if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
472 if (lp->lnd_mobil <= 0.0) {
474 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
475 prland(lp), conditions[findcondition(code)].desc[orig]);
477 putland(lp->lnd_uid, lp);
480 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
481 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
492 lcp = &lchr[(int)lp->lnd_type];
493 newx = xnorm(lp->lnd_x + dx);
494 newy = ynorm(lp->lnd_y + dy);
496 getsect(newx, newy, §);
497 if ((sect.sct_type == SCT_WATER) ||
498 (sect.sct_type == SCT_MOUNT) ||
499 (sect.sct_type == SCT_SANCT) ||
500 (sect.sct_type == SCT_WASTE) ||
501 (sect.sct_own != lp->lnd_own)) {
502 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
504 conditions[findcondition(code)].desc[orig],
505 xyas(newx, newy, lp->lnd_own));
507 putland(lp->lnd_uid, lp);
510 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
513 lp->lnd_mobil -= mobcost;
517 mines = sect.sct_mines;
518 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
519 (sect.sct_oldown != lp->lnd_own)) {
520 max = lcp->l_item[I_SHELL];
521 shells = lp->lnd_item[I_SHELL];
522 for (m = 0; mines > 0 && m < 5; m++) {
525 shells = min(max, shells + 1);
528 sect.sct_mines = mines;
529 lp->lnd_item[I_SHELL] = shells;
532 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
533 chance(DMINE_HITCHANCE(mines))) {
535 "%s %s,\nand hit a mine in %s while retreating!\n",
537 conditions[findcondition(code)].desc[orig],
538 xyas(newx, newy, lp->lnd_own));
539 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
543 sect.sct_mines = mines;
545 if (lp->lnd_effic < LAND_MINEFF)
548 putland(lp->lnd_uid, lp);
556 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
558 conditions[findcondition(code)].desc[orig],
559 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
561 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
563 conditions[findcondition(code)].desc[orig],
564 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
567 putland(lp->lnd_uid, lp);