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(char);
54 static int retreat_land1(struct lndstr *, char, int);
55 static int retreat_ship1(struct shpstr *, char, int);
62 struct ccode conditions[] = {
63 { 'i', { "retreated with a damaged friend",
65 { 't', { "retreated with a torpedoed ship",
66 "was hit by a torpedo" } },
67 { 's', { "retreated with a ship scared by sonar",
68 "detected a sonar ping" } },
69 { 'h', { "retreated with a helpless ship",
70 "was fired upon with no one able to defend it" } },
71 { 'b', { "retreated with a bombed friend",
73 { 'd', { "retreated with a depth-charged ship",
74 "was depth-charged" } },
75 { 'u', { "retreated with a boarded ship", "was boarded" } },
76 { 0, { "panicked", "panicked"} }
80 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
86 if (sp->shp_rflags & RET_INJURED)
87 retreat_ship(sp, 'i');
93 retreat_ship(struct shpstr *sp, char code)
98 if (sp->shp_rflags & RET_GROUP) {
99 snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
100 while (nxtitem(&ni, &ship))
101 if (ship.shp_own == sp->shp_own) {
102 if (ship.shp_uid == sp->shp_uid) {
103 retreat_ship1(sp, code, 1);
104 if (sp->shp_rpath[0] == 0)
107 retreat_ship1(&ship, code, 0);
108 getship(ship.shp_uid, &ship);
109 if (ship.shp_rpath[0] == 0) {
111 putship(ship.shp_uid, &ship);
116 retreat_ship1(sp, code, 1);
117 if (sp->shp_rpath[0] == 0)
123 retreat_ship1(struct shpstr *sp, char code, int orig)
126 /* Is this the originally scared ship, or a follower */
146 if (sp->shp_own == 0)
150 if (sp->shp_effic < SHIP_MINEFF) {
152 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
153 prship(sp), conditions[findcondition(code)].desc[orig]);
155 putship(sp->shp_uid, sp);
160 /* can't retreat a ship that's sailin, bad things happend */
163 "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
164 prship(sp), conditions[findcondition(code)].desc[orig]);
166 putship(sp->shp_uid, sp);
170 /* check crew - uws don't count */
171 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
173 "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
174 conditions[findcondition(code)].desc[orig]);
176 putship(sp->shp_uid, sp);
180 getsect(sp->shp_x, sp->shp_y, §);
181 switch (shp_check_nav(§, sp)) {
182 case CN_CONSTRUCTION:
184 "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
185 prship(sp), conditions[findcondition(code)].desc[orig]);
187 putship(sp->shp_uid, sp);
191 "%s %s,\nbut was landlocked, and couldn't retreat!\n",
192 prship(sp), conditions[findcondition(code)].desc[orig]);
194 putship(sp->shp_uid, sp);
202 "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
203 prship(sp), conditions[findcondition(code)].desc[orig]);
205 putship(sp->shp_uid, sp);
210 if (sp->shp_mobil <= 0.0) {
212 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
213 prship(sp), conditions[findcondition(code)].desc[orig]);
215 putship(sp->shp_uid, sp);
219 n = (-1 * MAX_RETREAT);
222 while (!stopping && n) {
224 if (sp->shp_rpath[0] == 0) {
228 if (sp->shp_mobil <= 0.0) {
230 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
231 prship(sp), conditions[findcondition(code)].desc[orig]);
233 putship(sp->shp_uid, sp);
236 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
237 memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
248 mcp = &mchr[(int)sp->shp_type];
249 newx = xnorm(sp->shp_x + dx);
250 newy = ynorm(sp->shp_y + dy);
251 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
252 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
254 getsect(newx, newy, §);
255 if (shp_check_nav(§, sp) != CN_NAVIGABLE ||
256 (sect.sct_own && sect.sct_own != sp->shp_own &&
257 getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
258 wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
259 prship(sp), conditions[findcondition(code)].desc[orig],
260 xyas(newx, newy, sp->shp_own));
262 putship(sp->shp_uid, sp);
267 sp->shp_mobil -= mobcost;
271 mines = sect.sct_mines;
273 if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
274 max = mcp->m_item[I_SHELL];
275 shells = sp->shp_item[I_SHELL];
276 for (m = 0; mines > 0 && m < 5; m++) {
279 shells = MIN(max, shells + 1);
280 changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
284 if (sect.sct_mines != mines) {
286 "%s cleared %d mine%s in %s while retreating\n",
287 prship(sp), sect.sct_mines-mines, splur(sect.sct_mines-mines),
288 xyas(newx, newy, sp->shp_own));
290 sect.sct_mines = mines;
291 sect.sct_item[I_SHELL] = shells;
294 writemap(sp->shp_own);
296 if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
298 "%s %s,\nand hit a mine in %s while retreating!\n",
299 prship(sp), conditions[findcondition(code)].desc[orig],
300 xyas(newx, newy, sp->shp_own));
301 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
305 if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
306 writemap(sp->shp_own);
307 sect.sct_mines = mines;
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);
335 findcondition(char code)
339 for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
340 CANT_HAPPEN(!conditions[i].code);
345 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
351 if (lp->lnd_rflags & RET_INJURED)
352 retreat_land(lp, 'i');
358 retreat_land(struct lndstr *lp, char code)
363 if (lp->lnd_rflags & RET_GROUP) {
364 snxtitem_group(&ni, EF_SHIP, lp->lnd_army);
365 while (nxtitem(&ni, &land))
366 if (land.lnd_own == lp->lnd_own) {
367 if (land.lnd_uid == lp->lnd_uid) {
368 retreat_land1(lp, code, 1);
369 if (lp->lnd_rpath[0] == 0)
372 retreat_land1(&land, code, 0);
373 getland(land.lnd_uid, &land);
374 if (land.lnd_rpath[0] == 0) {
376 putland(land.lnd_uid, &land);
381 retreat_land1(lp, code, 1);
382 if (lp->lnd_rpath[0] == 0)
388 retreat_land1(struct lndstr *lp, char code, int orig)
391 /* Is this the originally scared unit, or a follower */
410 if (lp->lnd_own == 0)
414 if (lp->lnd_effic < LAND_MINEFF) {
416 "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
417 prland(lp), conditions[findcondition(code)].desc[orig]);
419 putland(lp->lnd_uid, lp);
423 getsect(lp->lnd_x, lp->lnd_y, §);
425 if (lp->lnd_mobil <= 0.0) {
427 "%s %s,\nbut had no mobility, and couldn't retreat!\n",
428 prland(lp), conditions[findcondition(code)].desc[orig]);
430 putland(lp->lnd_uid, lp);
434 n = (-1 * MAX_RETREAT);
437 while (!stopping && n) {
439 if (lp->lnd_rpath[0] == 0) {
443 if (lp->lnd_mobil <= 0.0) {
445 "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
446 prland(lp), conditions[findcondition(code)].desc[orig]);
448 putland(lp->lnd_uid, lp);
451 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
452 memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
463 lcp = &lchr[(int)lp->lnd_type];
464 newx = xnorm(lp->lnd_x + dx);
465 newy = ynorm(lp->lnd_y + dy);
467 getsect(newx, newy, §);
468 if ((sect.sct_type == SCT_WATER) ||
469 (sect.sct_type == SCT_MOUNT) ||
470 (sect.sct_type == SCT_SANCT) ||
471 (sect.sct_type == SCT_WASTE) ||
472 (sect.sct_own != lp->lnd_own)) {
473 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
475 conditions[findcondition(code)].desc[orig],
476 xyas(newx, newy, lp->lnd_own));
478 putland(lp->lnd_uid, lp);
481 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
484 lp->lnd_mobil -= mobcost;
488 mines = sect.sct_mines;
489 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
490 (sect.sct_oldown != lp->lnd_own)) {
491 max = lcp->l_item[I_SHELL];
492 shells = lp->lnd_item[I_SHELL];
493 for (m = 0; mines > 0 && m < 5; m++) {
496 shells = MIN(max, shells + 1);
499 sect.sct_mines = mines;
500 lp->lnd_item[I_SHELL] = shells;
503 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
504 chance(DMINE_LHITCHANCE(mines))) {
506 "%s %s,\nand hit a mine in %s while retreating!\n",
508 conditions[findcondition(code)].desc[orig],
509 xyas(newx, newy, lp->lnd_own));
510 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
514 sect.sct_mines = mines;
516 if (lp->lnd_effic < LAND_MINEFF)
519 putland(lp->lnd_uid, lp);
527 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
529 conditions[findcondition(code)].desc[orig],
530 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
532 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
534 conditions[findcondition(code)].desc[orig],
535 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
538 putland(lp->lnd_uid, lp);