]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
Update copyright notice.
[empserver] / src / lib / subs / retreat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 <string.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "nat.h"
39 #include "var.h"
40 #include "retreat.h"
41 #include "ship.h"
42 #include "land.h"
43 #include "sect.h"
44 #include "news.h"
45 #include "xy.h"
46 #include "nsc.h"
47 #include "path.h"
48 #include "file.h"
49 #include "options.h"
50 #include "damage.h"
51 #include "prototypes.h"
52 #include "optlist.h"
53
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);
57
58 struct ccode {
59     s_char code;
60     s_char *desc[2];
61 } conditions[] = {
62     {
63         'i', {
64     "retreated with a damaged friend", "was damaged",},}, {
65         't', {
66     "retreated with a torpedoed ship", "was hit by a torpedo",},}, {
67         's', {
68     "retreated with a ship scared by sonar",
69                 "detected a sonar ping",},}, {
70         'h', {
71     "retreated with a helpless ship",
72                 "was fired upon with no one able to defend it",},}, {
73         'b', {
74     "retreated with a bombed friend", "was bombed",},}, {
75         'd', {
76     "retreated with a depth-charged ship", "was depth-charged",},}, {
77         'u', {
78     "retreated with a boared ship", "was boarded",},}, {
79         0, {
80 "", ""}},};
81
82 int
83 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
84 {
85     if (dam <= 0)
86         return 0;
87
88     shipdamage(sp, dam);
89     if (sp->shp_rflags & RET_INJURED)
90         retreat_ship(sp, 'i');
91
92     return 1;
93 }
94
95 void
96 retreat_ship(struct shpstr *sp, s_char code)
97 {
98     struct nstr_item ni;
99     struct shpstr ship;
100     s_char buf[2];
101
102     if (sp->shp_rflags & RET_GROUP) {
103         buf[0] = sp->shp_fleet;
104         buf[1] = 0;
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)
112                         sp->shp_rflags = 0;
113                 } else {
114                     retreat_ship1(&ship, code, 0);
115                     getship(ship.shp_uid, &ship);
116                     if (ship.shp_rpath[0] == 0) {
117                         ship.shp_rflags = 0;
118                         putship(ship.shp_uid, &ship);
119                     }
120                 }
121             }
122     } else {
123         retreat_ship1(sp, code, 1);
124         if (sp->shp_rpath[0] == 0)
125             sp->shp_rflags = 0;
126     }
127 }
128
129 static int
130 retreat_ship1(struct shpstr *sp, s_char code, int orig)
131
132
133                         /* Is this the originally scared ship, or a follower */
134 {
135     struct sctstr sect;
136     register int n;
137     register int m;
138     int max;
139     int dir;
140     coord newx;
141     coord newy;
142     coord dx;
143     coord dy;
144     int stopping;
145     int mines;
146     int shells;
147     double mobcost;
148     struct mchrstr *mcp;
149     int time_to_stop;
150
151     sp->shp_mission = 0;
152     if (sp->shp_own == 0)
153         return 0;
154
155     if (isupper(code))
156         code = tolower(code);
157
158     n = 0;
159     if (sp->shp_effic < SHIP_MINEFF) {
160         wu(0, sp->shp_own,
161            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
162            prship(sp), conditions[findcondition(code)].desc[orig]);
163         if (!orig)
164             putship(sp->shp_uid, sp);
165         return 0;
166     }
167
168     if (opt_SAIL) {
169         /* can't retreat a ship that's sailin, bad things happend */
170         if (*sp->shp_path) {
171             wu(0, sp->shp_own,
172                "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
173                prship(sp), conditions[findcondition(code)].desc[orig]);
174             if (!orig)
175                 putship(sp->shp_uid, sp);
176             return 0;
177         }
178     }
179     /* check crew - uws don't count */
180     if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[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         memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
247         if (dir == -1)
248             continue;
249         if (dir == DIR_STOP)
250             stopping++;
251         else {
252             dx = diroff[dir][0];
253             dy = diroff[dir][1];
254         }
255         n++;
256
257         mcp = &mchr[(int)sp->shp_type];
258         newx = xnorm(sp->shp_x + dx);
259         newy = ynorm(sp->shp_y + dy);
260         mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
261         mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
262
263         getsect(newx, newy, &sect);
264         if (check_nav(&sect) != CN_NAVIGABLE ||
265             (sect.sct_own && !player->owner &&
266              getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
267             wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
268                prship(sp), conditions[findcondition(code)].desc[orig],
269                xyas(newx, newy, sp->shp_own));
270             if (!orig)
271                 putship(sp->shp_uid, sp);
272             return 0;
273         }
274         sp->shp_x = newx;
275         sp->shp_y = newy;
276         sp->shp_mobil -= mobcost;
277         if (stopping)
278             continue;
279
280         mines = sect.sct_mines;
281         if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
282             max = mcp->m_item[I_SHELL];
283             shells = sp->shp_item[I_SHELL];
284             for (m = 0; mines > 0 && m < 5; m++) {
285                 if (chance(0.66)) {
286                     mines--;
287                     shells = min(max, shells + 1);
288                 }
289             }
290             sect.sct_mines = mines;
291             sect.sct_item[I_SHELL] = shells;
292             putsect(&sect);
293         }
294         if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
295             wu(0, sp->shp_own,
296                "%s %s,\nand hit a mine in %s while retreating!\n",
297                prship(sp), conditions[findcondition(code)].desc[orig],
298                xyas(newx, newy, sp->shp_own));
299             nreport(sp->shp_own, N_HIT_MINE, 0, 1);
300             m = MINE_DAMAGE();
301             shipdamage(sp, m);
302             mines--;
303             sect.sct_mines = mines;
304             putsect(&sect);
305             if (sp->shp_effic < SHIP_MINEFF)
306                 time_to_stop = 1;
307             if (!orig)
308                 putship(sp->shp_uid, sp);
309             return 0;
310         }
311         if (time_to_stop)
312             stopping = 1;
313     }
314
315     if (orig) {
316         wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
317            conditions[findcondition(code)].desc[orig],
318            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
319     } else {
320         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
321            prship(sp),
322            conditions[findcondition(code)].desc[orig],
323            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
324     }
325     if (!orig)
326         putship(sp->shp_uid, sp);
327     return 1;
328 }
329
330 #if 0
331 static int
332 check_nav(sect)
333 struct sctstr *sect;
334 {
335     switch (dchr[sect->sct_type].d_flg & 03) {
336     case NAVOK:
337         break;
338
339     case NAV_02:
340         if (sect->sct_effic < 2)
341             return CN_CONSTRUCTION;
342         break;
343     case NAV_60:
344         if (sect->sct_effic < 60)
345             return CN_CONSTRUCTION;
346         break;
347     default:
348         return CN_LANDLOCKED;
349     }
350     return CN_NAVIGABLE;
351 }
352 #endif
353
354 static int
355 findcondition(s_char code)
356 {
357     int x;
358
359     x = 0;
360     while (conditions[x].code) {
361         if (conditions[x].code == code)
362             return (x);
363         x++;
364     }
365
366     return (x);
367 }
368
369 int
370 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
371 {
372     if (dam <= 0)
373         return 0;
374
375     landdamage(lp, dam);
376     if (lp->lnd_rflags & RET_INJURED)
377         retreat_land(lp, 'i');
378
379     return 1;
380 }
381
382 void
383 retreat_land(struct lndstr *lp, s_char code)
384 {
385     struct nstr_item ni;
386     struct lndstr land;
387     s_char buf[2];
388
389     if (lp->lnd_rflags & RET_GROUP) {
390         buf[0] = lp->lnd_army;
391         buf[1] = 0;
392         snxtitem(&ni, EF_SHIP, buf);
393         while (nxtitem(&ni, (s_char *)&land))
394             if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
395                 if (land.lnd_uid == lp->lnd_uid) {
396                     retreat_land1(lp, code, 1);
397                     if (lp->lnd_rpath[0] == 0)
398                         lp->lnd_rflags = 0;
399                 } else {
400                     retreat_land1(&land, code, 0);
401                     getland(land.lnd_uid, &land);
402                     if (land.lnd_rpath[0] == 0) {
403                         land.lnd_rflags = 0;
404                         putland(land.lnd_uid, &land);
405                     }
406                 }
407             }
408     } else {
409         retreat_land1(lp, code, 1);
410         if (lp->lnd_rpath[0] == 0)
411             lp->lnd_rflags = 0;
412     }
413 }
414
415 static int
416 retreat_land1(struct lndstr *lp, s_char code, int orig)
417
418
419                         /* Is this the originally scared unit, or a follower */
420 {
421     struct sctstr sect;
422     register int n;
423     register int m;
424     int max;
425     int dir;
426     coord newx;
427     coord newy;
428     coord dx;
429     coord dy;
430     int stopping;
431     int mines;
432     int shells;
433     double mobcost;
434     struct lchrstr *lcp;
435     int time_to_stop;
436
437     lp->lnd_mission = 0;
438     if (lp->lnd_own == 0)
439         return 0;
440
441     if (isupper(code))
442         code = tolower(code);
443
444     n = 0;
445     if (lp->lnd_effic < LAND_MINEFF) {
446         wu(0, lp->lnd_own,
447            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
448            prland(lp), conditions[findcondition(code)].desc[orig]);
449         if (!orig)
450             putland(lp->lnd_uid, lp);
451         return 0;
452     }
453
454     getsect(lp->lnd_x, lp->lnd_y, &sect);
455
456     if (lp->lnd_mobil <= 0.0) {
457         wu(0, lp->lnd_own,
458            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
459            prland(lp), conditions[findcondition(code)].desc[orig]);
460         if (!orig)
461             putland(lp->lnd_uid, lp);
462         return 0;
463     }
464
465     n = (-1 * MAX_RETREAT);
466     stopping = 0;
467     time_to_stop = 0;
468     while ((!stopping) && n) {
469         dx = dy = 0;
470         if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
471             stopping = 1;
472             continue;
473         }
474         if (lp->lnd_mobil <= 0.0) {
475             wu(0, lp->lnd_own,
476                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
477                prland(lp), conditions[findcondition(code)].desc[orig]);
478             if (!orig)
479                 putland(lp->lnd_uid, lp);
480             return 0;
481         }
482         dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
483         memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
484         if (dir == -1)
485             continue;
486         if (dir == DIR_STOP)
487             stopping++;
488         else {
489             dx = diroff[dir][0];
490             dy = diroff[dir][1];
491         }
492         n++;
493
494         lcp = &lchr[(int)lp->lnd_type];
495         newx = xnorm(lp->lnd_x + dx);
496         newy = ynorm(lp->lnd_y + dy);
497
498         getsect(newx, newy, &sect);
499         if ((sect.sct_type == SCT_WATER) ||
500             (sect.sct_type == SCT_MOUNT) ||
501             (sect.sct_type == SCT_SANCT) ||
502             (sect.sct_type == SCT_WASTE) ||
503             (sect.sct_own != lp->lnd_own)) {
504             wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
505                prland(lp),
506                conditions[findcondition(code)].desc[orig],
507                xyas(newx, newy, lp->lnd_own));
508             if (!orig)
509                 putland(lp->lnd_uid, lp);
510             return 0;
511         }
512         mobcost = lnd_mobcost(lp, &sect, MOB_ROAD);
513         lp->lnd_x = newx;
514         lp->lnd_y = newy;
515         lp->lnd_mobil -= mobcost;
516         if (stopping)
517             continue;
518
519         mines = sect.sct_mines;
520         if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
521             (sect.sct_oldown != lp->lnd_own)) {
522             max = lcp->l_item[I_SHELL];
523             shells = lp->lnd_item[I_SHELL];
524             for (m = 0; mines > 0 && m < 5; m++) {
525                 if (chance(0.66)) {
526                     mines--;
527                     shells = min(max, shells + 1);
528                 }
529             }
530             sect.sct_mines = mines;
531             lp->lnd_item[I_SHELL] = shells;
532             putsect(&sect);
533         }
534         if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
535             chance(DMINE_HITCHANCE(mines))) {
536             wu(0, lp->lnd_own,
537                "%s %s,\nand hit a mine in %s while retreating!\n",
538                prland(lp),
539                conditions[findcondition(code)].desc[orig],
540                xyas(newx, newy, lp->lnd_own));
541             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
542             m = MINE_LDAMAGE();
543             landdamage(lp, m);
544             mines--;
545             sect.sct_mines = mines;
546             putsect(&sect);
547             if (lp->lnd_effic < LAND_MINEFF)
548                 time_to_stop = 1;
549             if (!orig)
550                 putland(lp->lnd_uid, lp);
551             return 0;
552         }
553         if (time_to_stop)
554             stopping = 1;
555     }
556
557     if (orig) {
558         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
559            prland(lp),
560            conditions[findcondition(code)].desc[orig],
561            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
562     } else {
563         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
564            prland(lp),
565            conditions[findcondition(code)].desc[orig],
566            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
567     }
568     if (!orig)
569         putland(lp->lnd_uid, lp);
570     return 1;
571 }