2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 */
153 if (sp->shp_own == 0)
157 code = tolower(code);
160 if (sp->shp_effic < SHIP_MINEFF) {
162 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
163 prship(sp), conditions[findcondition(code)].desc[orig]);
165 putship(sp->shp_uid, sp);
170 /* can't retreat a ship that's sailin, bad things happend */
173 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
174 prship(sp), conditions[findcondition(code)].desc[orig]);
176 putship(sp->shp_uid, sp);
180 /* check crew - uws don't count */
181 getvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
182 if (vec[I_MILIT] == 0 && vec[I_CIVIL] == 0) {
184 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
185 conditions[findcondition(code)].desc[orig]);
187 putship(sp->shp_uid, sp);
191 getsect(sp->shp_x, sp->shp_y, §);
192 switch (check_nav(§)) {
193 case CN_CONSTRUCTION:
195 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
196 prship(sp), conditions[findcondition(code)].desc[orig]);
198 putship(sp->shp_uid, sp);
202 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
203 prship(sp), conditions[findcondition(code)].desc[orig]);
205 putship(sp->shp_uid, sp);
213 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
214 prship(sp), conditions[findcondition(code)].desc[orig]);
216 putship(sp->shp_uid, sp);
221 if (sp->shp_mobil <= 0.0) {
223 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
224 prship(sp), conditions[findcondition(code)].desc[orig]);
226 putship(sp->shp_uid, sp);
230 n = (-1 * MAX_RETREAT);
233 while ((!stopping) && n) {
235 if (sp->shp_rpath[0] == 0 || sp->shp_rpath[0] == 0) {
239 if (sp->shp_mobil <= 0.0) {
241 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
242 prship(sp), conditions[findcondition(code)].desc[orig]);
244 putship(sp->shp_uid, sp);
247 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_VIEW);
248 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
259 mcp = &mchr[(int)sp->shp_type];
260 newx = xnorm(sp->shp_x + dx);
261 newy = ynorm(sp->shp_y + dy);
262 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
263 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
265 getsect(newx, newy, §);
266 if (check_nav(§) != CN_NAVIGABLE ||
267 (sect.sct_own && !player->owner &&
268 getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
269 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
270 prship(sp), conditions[findcondition(code)].desc[orig],
271 xyas(newx, newy, sp->shp_own));
273 putship(sp->shp_uid, sp);
278 sp->shp_mobil -= mobcost;
282 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
283 if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
284 max = vl_find(V_SHELL, mcp->m_vtype,
285 mcp->m_vamt, (int)mcp->m_nv);
286 shells = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
287 for (m = 0; mines > 0 && m < 5; m++) {
290 shells = min(max, shells + 1);
293 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
294 putvar(V_SHELL, shells, (s_char *)sp, EF_SHIP);
297 if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
299 "%s %s,\nand hit a mine in %s while retreating!\n",
300 prship(sp), conditions[findcondition(code)].desc[orig],
301 xyas(newx, newy, sp->shp_own));
302 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
306 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
308 if (sp->shp_effic < SHIP_MINEFF)
311 putship(sp->shp_uid, sp);
319 wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
320 conditions[findcondition(code)].desc[orig],
321 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
323 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
325 conditions[findcondition(code)].desc[orig],
326 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
329 putship(sp->shp_uid, sp);
338 switch (dchr[sect->sct_type].d_flg & 03) {
343 if (sect->sct_effic < 2)
344 return CN_CONSTRUCTION;
347 if (sect->sct_effic < 60)
348 return CN_CONSTRUCTION;
351 return CN_LANDLOCKED;
358 findcondition(s_char code)
363 while (conditions[x].code) {
364 if (conditions[x].code == code)
373 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
379 if (lp->lnd_rflags & RET_INJURED)
380 retreat_land(lp, 'i');
386 retreat_land(struct lndstr *lp, s_char code)
392 if (lp->lnd_rflags & RET_GROUP) {
393 buf[0] = lp->lnd_army;
395 snxtitem(&ni, EF_SHIP, buf);
396 while (nxtitem(&ni, (s_char *)&land))
397 if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
398 if (land.lnd_uid == lp->lnd_uid) {
399 retreat_land1(lp, code, 1);
400 if (lp->lnd_rpath[0] == 0)
403 retreat_land1(&land, code, 0);
404 getland(land.lnd_uid, &land);
405 if (land.lnd_rpath[0] == 0) {
407 putland(land.lnd_uid, &land);
412 retreat_land1(lp, code, 1);
413 if (lp->lnd_rpath[0] == 0)
419 retreat_land1(struct lndstr *lp, s_char code, int orig)
422 /* Is this the originally scared unit, or a follower */
439 s_char buf[RET_LEN - 1];
442 if (lp->lnd_own == 0)
446 code = tolower(code);
449 if (lp->lnd_effic < LAND_MINEFF) {
451 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
452 prland(lp), conditions[findcondition(code)].desc[orig]);
454 putland(lp->lnd_uid, lp);
458 getsect(lp->lnd_x, lp->lnd_y, §);
460 if (lp->lnd_mobil <= 0.0) {
462 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
463 prland(lp), conditions[findcondition(code)].desc[orig]);
465 putland(lp->lnd_uid, lp);
469 n = (-1 * MAX_RETREAT);
472 while ((!stopping) && n) {
474 if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
478 if (lp->lnd_mobil <= 0.0) {
480 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
481 prland(lp), conditions[findcondition(code)].desc[orig]);
483 putland(lp->lnd_uid, lp);
486 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
487 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
498 lcp = &lchr[(int)lp->lnd_type];
499 newx = xnorm(lp->lnd_x + dx);
500 newy = ynorm(lp->lnd_y + dy);
502 getsect(newx, newy, §);
503 if ((sect.sct_type == SCT_WATER) ||
504 (sect.sct_type == SCT_MOUNT) ||
505 (sect.sct_type == SCT_SANCT) ||
506 (sect.sct_type == SCT_WASTE) ||
507 (sect.sct_own != lp->lnd_own)) {
508 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
510 conditions[findcondition(code)].desc[orig],
511 xyas(newx, newy, lp->lnd_own));
513 putland(lp->lnd_uid, lp);
516 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
519 lp->lnd_mobil -= mobcost;
523 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
524 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
525 (sect.sct_oldown != lp->lnd_own)) {
526 max = vl_find(V_SHELL, lcp->l_vtype,
527 lcp->l_vamt, (int)lcp->l_nv);
528 shells = getvar(V_SHELL, (s_char *)lp, EF_LAND);
529 for (m = 0; mines > 0 && m < 5; m++) {
532 shells = min(max, shells + 1);
535 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
536 putvar(V_SHELL, shells, (s_char *)lp, EF_LAND);
539 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
540 chance(DMINE_HITCHANCE(mines))) {
542 "%s %s,\nand hit a mine while retreating!\n", prland(lp),
543 conditions[findcondition(code)].desc[orig], xyas(newx, newy,
546 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
550 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
552 if (lp->lnd_effic < LAND_MINEFF)
555 putland(lp->lnd_uid, lp);
563 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
565 conditions[findcondition(code)].desc[orig],
566 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
568 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
570 conditions[findcondition(code)].desc[orig],
571 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
574 putland(lp->lnd_uid, lp);