]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / subs / retreat.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  retreat.c: Retreat subroutines
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  *     
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "nat.h"
38 #include "var.h"
39 #include "retreat.h"
40 #include "ship.h"
41 #include "land.h"
42 #include "sect.h"
43 #include "news.h"
44 #include "xy.h"
45 #include "nsc.h"
46 #include "path.h"
47 #include "deity.h"
48 #include "file.h"
49 #include "options.h"
50 #include "damage.h"
51 #include "prototypes.h"
52 #include "optlist.h"
53
54 struct ccode {
55     s_char code;
56     s_char *desc[2];
57 } conditions[] = {
58     {
59         'i', {
60     "retreated with a damaged friend", "was damaged",},}, {
61         't', {
62     "retreated with a torpedoed ship", "was hit by a torpedo",},}, {
63         's', {
64     "retreated with a ship scared by sonar",
65                 "detected a sonar ping",},}, {
66         'h', {
67     "retreated with a helpless ship",
68                 "was fired upon with no one able to defend it",},}, {
69         'b', {
70     "retreated with a bombed friend", "was bombed",},}, {
71         'd', {
72     "retreated with a depth-charged ship", "was depth-charged",},}, {
73         'u', {
74     "retreated with a boared ship", "was boarded",},}, {
75         0, {
76 "", ""}},};
77
78 int
79 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
80 {
81     if (dam <= 0)
82         return 0;
83
84     shipdamage(sp, dam);
85     if (sp->shp_rflags & RET_INJURED)
86         retreat_ship(sp, 'i');
87
88     return 1;
89 }
90
91 void
92 retreat_ship(struct shpstr *sp, s_char code)
93 {
94     struct nstr_item ni;
95     struct shpstr ship;
96     s_char buf[2];
97
98     if (sp->shp_rflags & RET_GROUP) {
99         bzero(buf, 2);
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)
108                         sp->shp_rflags = 0;
109                 } else {
110                     retreat_ship1(&ship, code, 0);
111                     getship(ship.shp_uid, &ship);
112                     if (ship.shp_rpath[0] == 0) {
113                         ship.shp_rflags = 0;
114                         putship(ship.shp_uid, &ship);
115                     }
116                 }
117             }
118     } else {
119         retreat_ship1(sp, code, 1);
120         if (sp->shp_rpath[0] == 0)
121             sp->shp_rflags = 0;
122     }
123 }
124
125 int
126 retreat_ship1(struct shpstr *sp, s_char code, int orig)
127
128
129                         /* Is this the originally scared ship, or a follower */
130 {
131     extern double techfact(int, double);
132     struct sctstr sect;
133     register int n;
134     register int m;
135     int max;
136     int dir;
137     coord newx;
138     coord newy;
139     coord dx;
140     coord dy;
141     int stopping;
142     int mines;
143     int shells;
144     double mobcost;
145     struct mchrstr *mcp;
146     int vec[I_MAX + 1];
147     int time_to_stop;
148     s_char buf[RET_LEN - 1];
149
150     sp->shp_mission = 0;
151     if (sp->shp_own == 0)
152         return 0;
153
154     if (isupper(code))
155         code = tolower(code);
156
157     n = 0;
158     if (sp->shp_effic < SHIP_MINEFF) {
159         wu(0, sp->shp_own,
160            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
161            prship(sp), conditions[findcondition(code)].desc[orig]);
162         if (!orig)
163             putship(sp->shp_uid, sp);
164         return 0;
165     }
166
167     if (opt_SAIL) {
168         /* can't retreat a ship that's sailin, bad things happend */
169         if (*sp->shp_path) {
170             wu(0, sp->shp_own,
171                "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
172                prship(sp), conditions[findcondition(code)].desc[orig]);
173             if (!orig)
174                 putship(sp->shp_uid, sp);
175             return 0;
176         }
177     }
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) {
181         wu(0, sp->shp_own,
182            "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
183            conditions[findcondition(code)].desc[orig]);
184         if (!orig)
185             putship(sp->shp_uid, sp);
186         return 0;
187     }
188
189     getsect(sp->shp_x, sp->shp_y, &sect);
190     switch (check_nav(&sect)) {
191     case CN_CONSTRUCTION:
192         wu(0, sp->shp_own,
193            "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
194            prship(sp), conditions[findcondition(code)].desc[orig]);
195         if (!orig)
196             putship(sp->shp_uid, sp);
197         return 0;
198     case CN_LANDLOCKED:
199         wu(0, sp->shp_own,
200            "%s %s,\nbut was landlocked, and couldn't retreat!\n",
201            prship(sp), conditions[findcondition(code)].desc[orig]);
202         if (!orig)
203             putship(sp->shp_uid, sp);
204         return 0;
205         /*NOTREACHED*/
206     case CN_NAVIGABLE:
207         break;
208     case CN_ERROR:
209     default:
210         wu(0, sp->shp_own,
211            "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
212            prship(sp), conditions[findcondition(code)].desc[orig]);
213         if (!orig)
214             putship(sp->shp_uid, sp);
215         return 0;
216         /*NOTREACHED*/
217     }
218
219     if (sp->shp_mobil <= 0.0) {
220         wu(0, sp->shp_own,
221            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
222            prship(sp), conditions[findcondition(code)].desc[orig]);
223         if (!orig)
224             putship(sp->shp_uid, sp);
225         return 0;
226     }
227
228     n = (-1 * MAX_RETREAT);
229     stopping = 0;
230     time_to_stop = 0;
231     while ((!stopping) && n) {
232         dx = dy = 0;
233         if (sp->shp_rpath[0] == 0 || sp->shp_rpath[0] == 0) {
234             stopping = 1;
235             continue;
236         }
237         if (sp->shp_mobil <= 0.0) {
238             wu(0, sp->shp_own,
239                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
240                prship(sp), conditions[findcondition(code)].desc[orig]);
241             if (!orig)
242                 putship(sp->shp_uid, sp);
243             return 0;
244         }
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);
250         if (dir == -1)
251             continue;
252         if (dir == DIR_STOP)
253             stopping++;
254         else {
255             dx = diroff[dir][0];
256             dy = diroff[dir][1];
257         }
258         n++;
259
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));
265
266         getsect(newx, newy, &sect);
267         if (check_nav(&sect) != 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));
273             if (!orig)
274                 putship(sp->shp_uid, sp);
275             return 0;
276         }
277         sp->shp_x = newx;
278         sp->shp_y = newy;
279         sp->shp_mobil -= mobcost;
280         if (stopping)
281             continue;
282
283         mines = getvar(V_MINE, (s_char *)&sect, 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++) {
289                 if (chance(0.66)) {
290                     mines--;
291                     shells = min(max, shells + 1);
292                 }
293             }
294             putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
295             putvar(V_SHELL, shells, (s_char *)sp, EF_SHIP);
296             putsect(&sect);
297         }
298         if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
299             wu(0, sp->shp_own,
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);
304             m = MINE_DAMAGE();
305             shipdamage(sp, m);
306             mines--;
307             putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
308             putsect(&sect);
309             if (sp->shp_effic < SHIP_MINEFF)
310                 time_to_stop = 1;
311             if (!orig)
312                 putship(sp->shp_uid, sp);
313             return 0;
314         }
315         if (time_to_stop)
316             stopping = 1;
317     }
318
319     if (orig) {
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));
323     } else {
324         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
325            prship(sp),
326            conditions[findcondition(code)].desc[orig],
327            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
328     }
329     if (!orig)
330         putship(sp->shp_uid, sp);
331     return 1;
332 }
333
334 #if 0
335 static int
336 check_nav(sect)
337 struct sctstr *sect;
338 {
339     extern struct dchrstr dchr[];
340
341     switch (dchr[sect->sct_type].d_flg & 03) {
342     case NAVOK:
343         break;
344
345     case NAV_02:
346         if (sect->sct_effic < 2)
347             return CN_CONSTRUCTION;
348         break;
349     case NAV_60:
350         if (sect->sct_effic < 60)
351             return CN_CONSTRUCTION;
352         break;
353     default:
354         return CN_LANDLOCKED;
355     }
356     return CN_NAVIGABLE;
357 }
358 #endif
359 int
360 findcondition(s_char code)
361 {
362     int x;
363
364     x = 0;
365     while (conditions[x].code) {
366         if (conditions[x].code == code)
367             return (x);
368         x++;
369     }
370
371     return (x);
372 }
373
374 int
375 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
376 {
377     if (dam <= 0)
378         return 0;
379
380     landdamage(lp, dam);
381     if (lp->lnd_rflags & RET_INJURED)
382         retreat_land(lp, 'i');
383
384     return 1;
385 }
386
387 void
388 retreat_land(struct lndstr *lp, s_char code)
389 {
390     struct nstr_item ni;
391     struct lndstr land;
392     s_char buf[2];
393
394     if (lp->lnd_rflags & RET_GROUP) {
395         bzero(buf, 2);
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)
403                         lp->lnd_rflags = 0;
404                 } else {
405                     retreat_land1(&land, code, 0);
406                     getland(land.lnd_uid, &land);
407                     if (land.lnd_rpath[0] == 0) {
408                         land.lnd_rflags = 0;
409                         putland(land.lnd_uid, &land);
410                     }
411                 }
412             }
413     } else {
414         retreat_land1(lp, code, 1);
415         if (lp->lnd_rpath[0] == 0)
416             lp->lnd_rflags = 0;
417     }
418 }
419
420 int
421 retreat_land1(struct lndstr *lp, s_char code, int orig)
422
423
424                         /* Is this the originally scared unit, or a follower */
425 {
426     extern double techfact(int, double);
427     struct sctstr sect;
428     register int n;
429     register int m;
430     int max;
431     int dir;
432     coord newx;
433     coord newy;
434     coord dx;
435     coord dy;
436     int stopping;
437     int mines;
438     int shells;
439     double mobcost;
440     struct lchrstr *lcp;
441     int time_to_stop;
442     s_char buf[RET_LEN - 1];
443
444     lp->lnd_mission = 0;
445     if (lp->lnd_own == 0)
446         return 0;
447
448     if (isupper(code))
449         code = tolower(code);
450
451     n = 0;
452     if (lp->lnd_effic < LAND_MINEFF) {
453         wu(0, lp->lnd_own,
454            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
455            prland(lp), conditions[findcondition(code)].desc[orig]);
456         if (!orig)
457             putland(lp->lnd_uid, lp);
458         return 0;
459     }
460
461     getsect(lp->lnd_x, lp->lnd_y, &sect);
462
463     if (lp->lnd_mobil <= 0.0) {
464         wu(0, lp->lnd_own,
465            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
466            prland(lp), conditions[findcondition(code)].desc[orig]);
467         if (!orig)
468             putland(lp->lnd_uid, lp);
469         return 0;
470     }
471
472     n = (-1 * MAX_RETREAT);
473     stopping = 0;
474     time_to_stop = 0;
475     while ((!stopping) && n) {
476         dx = dy = 0;
477         if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
478             stopping = 1;
479             continue;
480         }
481         if (lp->lnd_mobil <= 0.0) {
482             wu(0, lp->lnd_own,
483                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
484                prland(lp), conditions[findcondition(code)].desc[orig]);
485             if (!orig)
486                 putland(lp->lnd_uid, lp);
487             return 0;
488         }
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);
494         if (dir == -1)
495             continue;
496         if (dir == DIR_STOP)
497             stopping++;
498         else {
499             dx = diroff[dir][0];
500             dy = diroff[dir][1];
501         }
502         n++;
503
504         lcp = &lchr[(int)lp->lnd_type];
505         newx = xnorm(lp->lnd_x + dx);
506         newy = ynorm(lp->lnd_y + dy);
507
508         getsect(newx, newy, &sect);
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",
515                prland(lp),
516                conditions[findcondition(code)].desc[orig],
517                xyas(newx, newy, lp->lnd_own));
518             if (!orig)
519                 putland(lp->lnd_uid, lp);
520             return 0;
521         }
522         mobcost = lnd_mobcost(lp, &sect, MOB_ROAD);
523         lp->lnd_x = newx;
524         lp->lnd_y = newy;
525         lp->lnd_mobil -= mobcost;
526         if (stopping)
527             continue;
528
529         mines = getvar(V_MINE, (s_char *)&sect, 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++) {
536                 if (chance(0.66)) {
537                     mines--;
538                     shells = min(max, shells + 1);
539                 }
540             }
541             putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
542             putvar(V_SHELL, shells, (s_char *)lp, EF_LAND);
543             putsect(&sect);
544         }
545         if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
546             chance(DMINE_HITCHANCE(mines))) {
547             wu(0, lp->lnd_own,
548                "%s %s,\nand hit a mine while retreating!\n", prland(lp),
549                conditions[findcondition(code)].desc[orig], xyas(newx, newy,
550                                                                 lp->
551                                                                 lnd_own));
552             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
553             m = MINE_LDAMAGE();
554             landdamage(lp, m);
555             mines--;
556             putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
557             putsect(&sect);
558             if (lp->lnd_effic < LAND_MINEFF)
559                 time_to_stop = 1;
560             if (!orig)
561                 putland(lp->lnd_uid, lp);
562             return 0;
563         }
564         if (time_to_stop)
565             stopping = 1;
566     }
567
568     if (orig) {
569         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
570            prland(lp),
571            conditions[findcondition(code)].desc[orig],
572            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
573     } else {
574         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
575            prland(lp),
576            conditions[findcondition(code)].desc[orig],
577            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
578     }
579     if (!orig)
580         putland(lp->lnd_uid, lp);
581     return 1;
582 }