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"
59 "retreated with a damaged friend",
63 "retreated with a torpedoed ship",
64 "was hit by a torpedo",
67 "retreated with a ship scared by sonar",
68 "detected a sonar ping",
71 "retreated with a helpless ship",
72 "was fired upon with no one able to defend it",
75 "retreated with a bombed friend",
79 "retreated with a depth-charged ship",
83 "retreated with a boared ship",
90 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
96 if (sp->shp_rflags & RET_INJURED)
97 retreat_ship(sp, 'i');
103 retreat_ship(struct shpstr *sp, s_char code)
109 if (sp->shp_rflags & RET_GROUP){
111 buf[0] = sp->shp_fleet;
112 snxtitem(&ni, EF_SHIP, buf);
113 while(nxtitem(&ni,(s_char *)&ship))
114 if ((ship.shp_fleet == buf[0]) &&
115 (ship.shp_own == sp->shp_own)) {
116 if (ship.shp_uid == sp->shp_uid){
117 retreat_ship1(sp,code,1);
118 if (sp->shp_rpath[0] == 0)
121 retreat_ship1(&ship,code,0);
122 getship(ship.shp_uid,&ship);
123 if (ship.shp_rpath[0] == 0){
125 putship(ship.shp_uid,&ship);
130 retreat_ship1(sp,code,1);
131 if (sp->shp_rpath[0] == 0)
137 retreat_ship1(struct shpstr *sp, s_char code, int orig)
140 /* Is this the originally scared ship, or a follower */
142 extern double techfact(int, double);
159 s_char buf[RET_LEN-1];
162 if (sp->shp_own == 0)
166 code = tolower(code);
169 if (sp->shp_effic < SHIP_MINEFF){
170 wu(0, sp->shp_own, "%s %s,\nbut it died in the attack, and so couldn't retreat!\n", prship(sp),
171 conditions[findcondition(code)].desc[orig]);
172 if (!orig) putship(sp->shp_uid,sp);
177 /* can't retreat a ship that's sailin, bad things happend */
179 wu(0, sp->shp_own, "%s %s,\nbut had sailing orders, and couldn't retreat!\n", prship(sp),
180 conditions[findcondition(code)].desc[orig]);
181 if (!orig) putship(sp->shp_uid,sp);
185 /* check crew - uws don't count */
186 getvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
187 if (vec[I_MILIT] == 0 && vec[I_CIVIL] == 0){
188 wu(0, sp->shp_own, "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
189 conditions[findcondition(code)].desc[orig]);
190 if (!orig) putship(sp->shp_uid,sp);
194 getsect(sp->shp_x,sp->shp_y,§);
195 switch (check_nav(§)) {
196 case CN_CONSTRUCTION:
197 wu(0, sp->shp_own, "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n", prship(sp),
198 conditions[findcondition(code)].desc[orig]);
199 if (!orig) putship(sp->shp_uid,sp);
202 wu(0, sp->shp_own, "%s %s,\nbut was landlocked, and couldn't retreat!\n", prship(sp),
203 conditions[findcondition(code)].desc[orig]);
204 if (!orig) putship(sp->shp_uid,sp);
211 wu(0, sp->shp_own, "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n", prship(sp),
212 conditions[findcondition(code)].desc[orig]);
213 if (!orig) putship(sp->shp_uid,sp);
218 if (sp->shp_mobil <= 0.0) {
219 wu(0, sp->shp_own, "%s %s,\nbut had no mobility, and couldn't retreat!\n", prship(sp),
220 conditions[findcondition(code)].desc[orig]);
221 if (!orig) putship(sp->shp_uid,sp);
228 while ((!stopping) && n){
230 if (sp->shp_rpath[0] == 0 || sp->shp_rpath[0] == 0){
234 if (sp->shp_mobil <= 0.0) {
235 wu(0, sp->shp_own, "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n", prship(sp),
236 conditions[findcondition(code)].desc[orig]);
237 if (!orig) putship(sp->shp_uid,sp);
240 dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_VIEW);
241 bzero(buf,RET_LEN-1);
242 bcopy(&sp->shp_rpath[1],buf,RET_LEN-1);
243 bzero(sp->shp_rpath,RET_LEN);
244 bcopy(buf,sp->shp_rpath,RET_LEN-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", prship(sp),
266 conditions[findcondition(code)].desc[orig],
267 xyas(newx, newy, sp->shp_own));
268 if (!orig) putship(sp->shp_uid,sp);
273 sp->shp_mobil -= mobcost;
277 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
278 if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
279 max = vl_find(V_SHELL, mcp->m_vtype,
280 mcp->m_vamt, (int) mcp->m_nv);
281 shells = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
282 for (m=0; mines > 0 && m < 5; m++) {
285 shells = min(max, shells + 1);
288 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
289 putvar(V_SHELL, shells, (s_char *)sp, EF_SHIP);
292 if (mines > 0 && !player->owner &&
293 chance(DMINE_HITCHANCE(mines))) {
294 wu(0, sp->shp_own, "%s %s,\nand hit a mine in %s while retreating!\n", prship(sp),
295 conditions[findcondition(code)].desc[orig],
296 xyas(newx, newy, sp->shp_own));
297 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
301 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
303 if (sp->shp_effic < SHIP_MINEFF)
305 if (!orig) putship(sp->shp_uid,sp);
313 wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
314 conditions[findcondition(code)].desc[orig],
315 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
317 wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
319 conditions[findcondition(code)].desc[orig],
320 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
322 if (!orig) putship(sp->shp_uid,sp);
331 extern struct dchrstr dchr[];
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;
352 findcondition(s_char code)
357 while (conditions[x].code){
358 if (conditions[x].code == code)
367 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
373 if (lp->lnd_rflags & RET_INJURED)
374 retreat_land(lp, 'i');
380 retreat_land(struct lndstr *lp, s_char code)
386 if (lp->lnd_rflags & RET_GROUP){
388 buf[0] = lp->lnd_army;
389 snxtitem(&ni, EF_SHIP, buf);
390 while(nxtitem(&ni,(s_char *)&land))
391 if ((land.lnd_army == buf[0]) &&
392 (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 */
419 extern double techfact(int, double);
435 s_char buf[RET_LEN-1];
438 if (lp->lnd_own == 0)
442 code = tolower(code);
445 if (lp->lnd_effic < LAND_MINEFF){
446 wu(0, lp->lnd_own, "%s %s,\nbut it died in the attack, and so couldn't retreat!\n", prland(lp),
447 conditions[findcondition(code)].desc[orig]);
448 if (!orig) putland(lp->lnd_uid,lp);
452 getsect(lp->lnd_x,lp->lnd_y,§);
454 if (lp->lnd_mobil <= 0.0) {
455 wu(0, lp->lnd_own, "%s %s,\nbut had no mobility, and couldn't retreat!\n",
457 conditions[findcondition(code)].desc[orig]);
458 if (!orig) putland(lp->lnd_uid,lp);
465 while ((!stopping) && n){
467 if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0){
471 if (lp->lnd_mobil <= 0.0) {
472 wu(0, lp->lnd_own, "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
474 conditions[findcondition(code)].desc[orig]);
475 if (!orig) putland(lp->lnd_uid,lp);
478 dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
479 bzero(buf,RET_LEN-1);
480 bcopy(&lp->lnd_rpath[1],buf,RET_LEN-1);
481 bzero(lp->lnd_rpath,RET_LEN);
482 bcopy(buf,lp->lnd_rpath,RET_LEN-1);
493 lcp = &lchr[(int)lp->lnd_type];
494 newx = xnorm(lp->lnd_x + dx);
495 newy = ynorm(lp->lnd_y + dy);
497 getsect(newx, newy, §);
498 if ((sect.sct_type == SCT_WATER) ||
499 (sect.sct_type == SCT_MOUNT) ||
500 (sect.sct_type == SCT_SANCT) ||
501 (sect.sct_type == SCT_WASTE) ||
502 (sect.sct_own != lp->lnd_own)){
503 wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
505 conditions[findcondition(code)].desc[orig],
506 xyas(newx, newy, lp->lnd_own));
507 if (!orig) putland(lp->lnd_uid,lp);
510 mobcost = lnd_mobcost(lp, §, MOB_ROAD);
513 lp->lnd_mobil -= mobcost;
517 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
518 if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
519 (sect.sct_oldown != lp->lnd_own)){
520 max = vl_find(V_SHELL, lcp->l_vtype,
521 lcp->l_vamt, (int) lcp->l_nv);
522 shells = getvar(V_SHELL, (s_char *)lp, EF_LAND);
523 for (m=0; mines > 0 && m < 5; m++) {
526 shells = min(max, shells + 1);
529 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
530 putvar(V_SHELL, shells, (s_char *)lp, EF_LAND);
533 if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
534 chance(DMINE_HITCHANCE(mines))) {
535 wu(0, lp->lnd_own, "%s %s,\nand hit a mine 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 putvar(V_MINE, mines, (s_char *)§, EF_SECTOR);
545 if (lp->lnd_effic < LAND_MINEFF)
547 if (!orig) putland(lp->lnd_uid,lp);
555 wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
557 conditions[findcondition(code)].desc[orig],
558 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
560 wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
562 conditions[findcondition(code)].desc[orig],
563 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
565 if (!orig) putland(lp->lnd_uid,lp);