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"
60 "retreated with a damaged friend", "was damaged",},}, {
62 "retreated with a torpedoed ship", "was hit by a torpedo",},}, {
64 "retreated with a ship scared by sonar",
65 "detected a sonar ping",},}, {
67 "retreated with a helpless ship",
68 "was fired upon with no one able to defend it",},}, {
70 "retreated with a bombed friend", "was bombed",},}, {
72 "retreated with a depth-charged ship", "was depth-charged",},}, {
74 "retreated with a boared ship", "was boarded",},}, {
79 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
85 if (sp->shp_rflags & RET_INJURED)
86 retreat_ship(sp, 'i');
92 retreat_ship(struct shpstr *sp, s_char code)
98 if (sp->shp_rflags & RET_GROUP) {
100 buf[0] = sp->shp_fleet;
101 snxtitem(&ni, EF_SHIP, buf);
102 while (nxtitem(&ni, (s_char *)&ship))
103 if ((ship.shp_fleet == buf[0]) &&
104 (ship.shp_own == sp->shp_own)) {
105 if (ship.shp_uid == sp->shp_uid) {
106 retreat_ship1(sp, code, 1);
107 if (sp->shp_rpath[0] == 0)
110 retreat_ship1(&ship, code, 0);
111 getship(ship.shp_uid, &ship);
112 if (ship.shp_rpath[0] == 0) {
114 putship(ship.shp_uid, &ship);
119 retreat_ship1(sp, code, 1);
120 if (sp->shp_rpath[0] == 0)
126 retreat_ship1(struct shpstr *sp, s_char code, int orig)
129 /* Is this the originally scared ship, or a follower */
131 extern double techfact(int, double);
148 s_char buf[RET_LEN - 1];
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 getvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
180 if (vec[I_MILIT] == 0 && vec[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 bzero(buf, RET_LEN - 1);
247 bcopy(&sp->shp_rpath[1], buf, RET_LEN - 1);
248 bzero(sp->shp_rpath, RET_LEN);
249 bcopy(buf, sp->shp_rpath, RET_LEN - 1);
260 mcp = &mchr[(int)sp->shp_type];
261 newx = xnorm(sp->shp_x + dx);
262 newy = ynorm(sp->shp_y + dy);
263 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
264 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
266 getsect(newx, newy, §);
267 if (check_nav(§) != CN_NAVIGABLE ||
268 (sect.sct_own && !player->owner &&
269 getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
270 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
271 prship(sp), conditions[findcondition(code)].desc[orig],
272 xyas(newx, newy, sp->shp_own));
274 putship(sp->shp_uid, sp);
279 sp->shp_mobil -= mobcost;
283 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
284 if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
285 max = vl_find(V_SHELL, mcp->m_vtype,
286 mcp->m_vamt, (int)mcp->m_nv);
287 shells = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
288 for (m = 0; mines > 0 && m < 5; m++) {
291 shells = min(max, shells + 1);
294 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
295 putvar(V_SHELL, shells, (s_char *)sp, EF_SHIP);
298 if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
300 "%s %s,\nand hit a mine in %s while retreating!\n",
301 prship(sp), conditions[findcondition(code)].desc[orig],
302 xyas(newx, newy, sp->shp_own));
303 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
307 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
309 if (sp->shp_effic < SHIP_MINEFF)
312 putship(sp->shp_uid, sp);
320 wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
321 conditions[findcondition(code)].desc[orig],
322 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
324 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
326 conditions[findcondition(code)].desc[orig],
327 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
330 putship(sp->shp_uid, sp);
339 extern struct dchrstr dchr[];
341 switch (dchr[sect->sct_type].d_flg & 03) {
346 if (sect->sct_effic < 2)
347 return CN_CONSTRUCTION;
350 if (sect->sct_effic < 60)
351 return CN_CONSTRUCTION;
354 return CN_LANDLOCKED;
360 findcondition(s_char code)
365 while (conditions[x].code) {
366 if (conditions[x].code == code)
375 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
381 if (lp->lnd_rflags & RET_INJURED)
382 retreat_land(lp, 'i');
388 retreat_land(struct lndstr *lp, s_char code)
394 if (lp->lnd_rflags & RET_GROUP) {
396 buf[0] = lp->lnd_army;
397 snxtitem(&ni, EF_SHIP, buf);
398 while (nxtitem(&ni, (s_char *)&land))
399 if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
400 if (land.lnd_uid == lp->lnd_uid) {
401 retreat_land1(lp, code, 1);
402 if (lp->lnd_rpath[0] == 0)
405 retreat_land1(&land, code, 0);
406 getland(land.lnd_uid, &land);
407 if (land.lnd_rpath[0] == 0) {
409 putland(land.lnd_uid, &land);
414 retreat_land1(lp, code, 1);
415 if (lp->lnd_rpath[0] == 0)
421 retreat_land1(struct lndstr *lp, s_char code, int orig)
424 /* Is this the originally scared unit, or a follower */
426 extern double techfact(int, double);
442 s_char buf[RET_LEN - 1];
445 if (lp->lnd_own == 0)
449 code = tolower(code);
452 if (lp->lnd_effic < LAND_MINEFF) {
454 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
455 prland(lp), conditions[findcondition(code)].desc[orig]);
457 putland(lp->lnd_uid, lp);
461 getsect(lp->lnd_x, lp->lnd_y, §);
463 if (lp->lnd_mobil <= 0.0) {
465 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
466 prland(lp), conditions[findcondition(code)].desc[orig]);
468 putland(lp->lnd_uid, lp);
472 n = (-1 * MAX_RETREAT);
475 while ((!stopping) && n) {
477 if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
481 if (lp->lnd_mobil <= 0.0) {
483 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
484 prland(lp), conditions[findcondition(code)].desc[orig]);
486 putland(lp->lnd_uid, lp);
489 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
490 bzero(buf, RET_LEN - 1);
491 bcopy(&lp->lnd_rpath[1], buf, RET_LEN - 1);
492 bzero(lp->lnd_rpath, RET_LEN);
493 bcopy(buf, lp->lnd_rpath, RET_LEN - 1);
504 lcp = &lchr[(int)lp->lnd_type];
505 newx = xnorm(lp->lnd_x + dx);
506 newy = ynorm(lp->lnd_y + dy);
508 getsect(newx, newy, §);
509 if ((sect.sct_type == SCT_WATER) ||
510 (sect.sct_type == SCT_MOUNT) ||
511 (sect.sct_type == SCT_SANCT) ||
512 (sect.sct_type == SCT_WASTE) ||
513 (sect.sct_own != lp->lnd_own)) {
514 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
516 conditions[findcondition(code)].desc[orig],
517 xyas(newx, newy, lp->lnd_own));
519 putland(lp->lnd_uid, lp);
522 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
525 lp->lnd_mobil -= mobcost;
529 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
530 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
531 (sect.sct_oldown != lp->lnd_own)) {
532 max = vl_find(V_SHELL, lcp->l_vtype,
533 lcp->l_vamt, (int)lcp->l_nv);
534 shells = getvar(V_SHELL, (s_char *)lp, EF_LAND);
535 for (m = 0; mines > 0 && m < 5; m++) {
538 shells = min(max, shells + 1);
541 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
542 putvar(V_SHELL, shells, (s_char *)lp, EF_LAND);
545 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
546 chance(DMINE_HITCHANCE(mines))) {
548 "%s %s,\nand hit a mine while retreating!\n", prland(lp),
549 conditions[findcondition(code)].desc[orig], xyas(newx, newy,
552 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
556 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
558 if (lp->lnd_effic < LAND_MINEFF)
561 putland(lp->lnd_uid, lp);
569 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
571 conditions[findcondition(code)].desc[orig],
572 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
574 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
576 conditions[findcondition(code)].desc[orig],
577 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
580 putland(lp->lnd_uid, lp);