]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
Break inclusion cycle: prototypes.h and commands.h included each
[empserver] / src / lib / subs / retreat.c
1 /*
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
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 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.
25  *
26  *  ---
27  *
28  *  retreat.c: Retreat subroutines
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  */
33
34 #include <config.h>
35
36 #include <stdlib.h>
37 #include "damage.h"
38 #include "file.h"
39 #include "land.h"
40 #include "map.h"
41 #include "misc.h"
42 #include "nat.h"
43 #include "news.h"
44 #include "nsc.h"
45 #include "optlist.h"
46 #include "path.h"
47 #include "player.h"
48 #include "prototypes.h"
49 #include "retreat.h"
50 #include "sect.h"
51 #include "ship.h"
52 #include "xy.h"
53
54 static int findcondition(char);
55 static int retreat_land1(struct lndstr *, char, int);
56 static int retreat_ship1(struct shpstr *, char, int);
57
58 struct ccode {
59     char code;
60     char *desc[2];
61 };
62
63 static struct ccode conditions[] = {
64     { 'i', { "retreated with a damaged friend",
65              "was damaged" } },
66     { 't', { "retreated with a torpedoed ship",
67              "was hit by a torpedo" } },
68     { 's', { "retreated with a ship scared by sonar",
69              "detected a sonar ping" } },
70     { 'h', { "retreated with a helpless ship",
71              "was fired upon with no one able to defend it" } },
72     { 'b', { "retreated with a bombed friend",
73              "was bombed" } },
74     { 'd', { "retreated with a depth-charged ship",
75              "was depth-charged" } },
76     { 'u', { "retreated with a boarded ship", "was boarded" } },
77     { 0,   { "panicked", "panicked"} }
78 };
79
80 int
81 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
82 {
83     if (dam <= 0)
84         return 0;
85
86     shipdamage(sp, dam);
87     if (sp->shp_rflags & RET_INJURED)
88         retreat_ship(sp, 'i');
89
90     return 1;
91 }
92
93 void
94 retreat_ship(struct shpstr *sp, char code)
95 {
96     struct nstr_item ni;
97     struct shpstr ship;
98
99     if (sp->shp_rflags & RET_GROUP) {
100         snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
101         while (nxtitem(&ni, &ship))
102             if (ship.shp_own == sp->shp_own) {
103                 if (ship.shp_uid == sp->shp_uid) {
104                     retreat_ship1(sp, code, 1);
105                     if (sp->shp_rpath[0] == 0)
106                         sp->shp_rflags = 0;
107                 } else {
108                     retreat_ship1(&ship, code, 0);
109                     getship(ship.shp_uid, &ship);
110                     if (ship.shp_rpath[0] == 0) {
111                         ship.shp_rflags = 0;
112                         putship(ship.shp_uid, &ship);
113                     }
114                 }
115             }
116     } else {
117         retreat_ship1(sp, code, 1);
118         if (sp->shp_rpath[0] == 0)
119             sp->shp_rflags = 0;
120     }
121 }
122
123 static int
124 retreat_ship1(struct shpstr *sp, char code, int orig)
125
126
127                         /* Is this the originally scared ship, or a follower */
128 {
129     struct sctstr sect;
130     int n;
131     int m;
132     int max;
133     int dir;
134     coord newx;
135     coord newy;
136     coord dx;
137     coord dy;
138     int stopping;
139     int mines;
140     int shells;
141     double mobcost;
142     struct mchrstr *mcp;
143     int changed;
144
145     sp->shp_mission = 0;
146     if (sp->shp_own == 0)
147         return 0;
148
149     n = 0;
150     if (sp->shp_effic < SHIP_MINEFF) {
151         wu(0, sp->shp_own,
152            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
153            prship(sp), conditions[findcondition(code)].desc[orig]);
154         if (!orig)
155             putship(sp->shp_uid, sp);
156         return 0;
157     }
158
159     if (opt_SAIL) {
160         /* can't retreat a ship that's sailin, bad things happend */
161         if (*sp->shp_path) {
162             wu(0, sp->shp_own,
163                "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
164                prship(sp), conditions[findcondition(code)].desc[orig]);
165             if (!orig)
166                 putship(sp->shp_uid, sp);
167             return 0;
168         }
169     }
170     /* check crew - uws don't count */
171     if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
172         wu(0, sp->shp_own,
173            "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
174            conditions[findcondition(code)].desc[orig]);
175         if (!orig)
176             putship(sp->shp_uid, sp);
177         return 0;
178     }
179
180     getsect(sp->shp_x, sp->shp_y, &sect);
181     switch (shp_check_nav(&sect, sp)) {
182     case CN_CONSTRUCTION:
183         wu(0, sp->shp_own,
184            "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
185            prship(sp), conditions[findcondition(code)].desc[orig]);
186         if (!orig)
187             putship(sp->shp_uid, sp);
188         return 0;
189     case CN_LANDLOCKED:
190         wu(0, sp->shp_own,
191            "%s %s,\nbut was landlocked, and couldn't retreat!\n",
192            prship(sp), conditions[findcondition(code)].desc[orig]);
193         if (!orig)
194             putship(sp->shp_uid, sp);
195         return 0;
196         /*NOTREACHED*/
197     case CN_NAVIGABLE:
198         break;
199     case CN_ERROR:
200     default:
201         wu(0, sp->shp_own,
202            "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
203            prship(sp), conditions[findcondition(code)].desc[orig]);
204         if (!orig)
205             putship(sp->shp_uid, sp);
206         return 0;
207         /*NOTREACHED*/
208     }
209
210     if (sp->shp_mobil <= 0.0) {
211         wu(0, sp->shp_own,
212            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
213            prship(sp), conditions[findcondition(code)].desc[orig]);
214         if (!orig)
215             putship(sp->shp_uid, sp);
216         return 0;
217     }
218
219     n = (-1 * MAX_RETREAT);
220     stopping = 0;
221     while (!stopping && n) {
222         dx = dy = 0;
223         if (sp->shp_rpath[0] == 0) {
224             stopping = 1;
225             continue;
226         }
227         if (sp->shp_mobil <= 0.0) {
228             wu(0, sp->shp_own,
229                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
230                prship(sp), conditions[findcondition(code)].desc[orig]);
231             if (!orig)
232                 putship(sp->shp_uid, sp);
233             return 0;
234         }
235         dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
236         memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
237         if (dir < 0)
238             continue;
239         if (dir == DIR_STOP)
240             stopping++;
241         else {
242             dx = diroff[dir][0];
243             dy = diroff[dir][1];
244         }
245         n++;
246
247         mcp = &mchr[(int)sp->shp_type];
248         newx = xnorm(sp->shp_x + dx);
249         newy = ynorm(sp->shp_y + dy);
250         mobcost = shp_mobcost(sp);
251
252         getsect(newx, newy, &sect);
253         if (shp_check_nav(&sect, sp) != CN_NAVIGABLE ||
254             (sect.sct_own && sect.sct_own != sp->shp_own &&
255              getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
256             wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
257                prship(sp), conditions[findcondition(code)].desc[orig],
258                xyas(newx, newy, sp->shp_own));
259             if (!orig)
260                 putship(sp->shp_uid, sp);
261             return 0;
262         }
263         sp->shp_x = newx;
264         sp->shp_y = newy;
265         sp->shp_mobil -= mobcost;
266         if (stopping)
267             continue;
268
269         mines = sect.sct_mines;
270         changed = 0;
271         if ((mcp->m_flags & M_SWEEP) && sect.sct_type == SCT_WATER) {
272             max = mcp->m_item[I_SHELL];
273             shells = sp->shp_item[I_SHELL];
274             for (m = 0; mines > 0 && m < 5; m++) {
275                 if (chance(0.66)) {
276                     mines--;
277                     shells = MIN(max, shells + 1);
278                     changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
279                                        'X', 0);
280                 }
281             }
282             if (sect.sct_mines != mines) {
283                 wu(0, sp->shp_own,
284                    "%s cleared %d mine%s in %s while retreating\n",
285                    prship(sp), sect.sct_mines-mines, splur(sect.sct_mines-mines),
286                    xyas(newx, newy, sp->shp_own));
287                 sect.sct_mines = mines;
288                 sp->shp_item[I_SHELL] = shells;
289                 putsect(&sect);
290             }
291             if (changed)
292                 writemap(sp->shp_own);
293         }
294         if (sect.sct_type == SCT_WATER && mines > 0
295             && chance(DMINE_HITCHANCE(mines))) {
296             wu(0, sp->shp_own,
297                "%s %s,\nand hit a mine in %s while retreating!\n",
298                prship(sp), conditions[findcondition(code)].desc[orig],
299                xyas(newx, newy, sp->shp_own));
300             nreport(sp->shp_own, N_HIT_MINE, 0, 1);
301             m = MINE_DAMAGE();
302             shipdamage(sp, m);
303             mines--;
304             if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
305                 writemap(sp->shp_own);
306             sect.sct_mines = mines;
307             putsect(&sect);
308             if (!orig)
309                 putship(sp->shp_uid, sp);
310             return 0;
311         }
312     }
313
314     if (orig) {
315         wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
316            prship(sp), conditions[findcondition(code)].desc[orig],
317            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
318     } else {
319         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
320            prship(sp),
321            conditions[findcondition(code)].desc[orig],
322            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
323     }
324     if (!orig)
325         putship(sp->shp_uid, sp);
326     return 1;
327 }
328
329 static int
330 findcondition(char code)
331 {
332     int i;
333
334     for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
335     CANT_HAPPEN(!conditions[i].code);
336     return i;
337 }
338
339 int
340 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
341 {
342     if (dam <= 0)
343         return 0;
344
345     landdamage(lp, dam);
346     if (lp->lnd_rflags & RET_INJURED)
347         retreat_land(lp, 'i');
348
349     return 1;
350 }
351
352 void
353 retreat_land(struct lndstr *lp, char code)
354 {
355     struct nstr_item ni;
356     struct lndstr land;
357
358     if (lp->lnd_rflags & RET_GROUP) {
359         snxtitem_group(&ni, EF_SHIP, lp->lnd_army);
360         while (nxtitem(&ni, &land))
361             if (land.lnd_own == lp->lnd_own) {
362                 if (land.lnd_uid == lp->lnd_uid) {
363                     retreat_land1(lp, code, 1);
364                     if (lp->lnd_rpath[0] == 0)
365                         lp->lnd_rflags = 0;
366                 } else {
367                     retreat_land1(&land, code, 0);
368                     getland(land.lnd_uid, &land);
369                     if (land.lnd_rpath[0] == 0) {
370                         land.lnd_rflags = 0;
371                         putland(land.lnd_uid, &land);
372                     }
373                 }
374             }
375     } else {
376         retreat_land1(lp, code, 1);
377         if (lp->lnd_rpath[0] == 0)
378             lp->lnd_rflags = 0;
379     }
380 }
381
382 static int
383 retreat_land1(struct lndstr *lp, char code, int orig)
384
385
386                         /* Is this the originally scared unit, or a follower */
387 {
388     struct sctstr sect;
389     int n;
390     int m;
391     int max;
392     int dir;
393     coord newx;
394     coord newy;
395     coord dx;
396     coord dy;
397     int stopping;
398     int mines;
399     int shells;
400     double mobcost;
401     struct lchrstr *lcp;
402
403     lp->lnd_mission = 0;
404     if (lp->lnd_own == 0)
405         return 0;
406
407     n = 0;
408     if (lp->lnd_effic < LAND_MINEFF) {
409         wu(0, lp->lnd_own,
410            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
411            prland(lp), conditions[findcondition(code)].desc[orig]);
412         if (!orig)
413             putland(lp->lnd_uid, lp);
414         return 0;
415     }
416
417     getsect(lp->lnd_x, lp->lnd_y, &sect);
418
419     if (lp->lnd_mobil <= 0.0) {
420         wu(0, lp->lnd_own,
421            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
422            prland(lp), conditions[findcondition(code)].desc[orig]);
423         if (!orig)
424             putland(lp->lnd_uid, lp);
425         return 0;
426     }
427
428     n = (-1 * MAX_RETREAT);
429     stopping = 0;
430     while (!stopping && n) {
431         dx = dy = 0;
432         if (lp->lnd_rpath[0] == 0) {
433             stopping = 1;
434             continue;
435         }
436         if (lp->lnd_mobil <= 0.0) {
437             wu(0, lp->lnd_own,
438                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
439                prland(lp), conditions[findcondition(code)].desc[orig]);
440             if (!orig)
441                 putland(lp->lnd_uid, lp);
442             return 0;
443         }
444         dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
445         memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
446         if (dir < 0)
447             continue;
448         if (dir == DIR_STOP)
449             stopping++;
450         else {
451             dx = diroff[dir][0];
452             dy = diroff[dir][1];
453         }
454         n++;
455
456         lcp = &lchr[(int)lp->lnd_type];
457         newx = xnorm(lp->lnd_x + dx);
458         newy = ynorm(lp->lnd_y + dy);
459
460         getsect(newx, newy, &sect);
461         mobcost = lnd_mobcost(lp, &sect);
462         if (mobcost < 0
463             || sect.sct_type == SCT_MOUNT
464             || sect.sct_own != lp->lnd_own) {
465             wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
466                prland(lp),
467                conditions[findcondition(code)].desc[orig],
468                xyas(newx, newy, lp->lnd_own));
469             if (!orig)
470                 putland(lp->lnd_uid, lp);
471             return 0;
472         }
473         lp->lnd_x = newx;
474         lp->lnd_y = newy;
475         lp->lnd_mobil -= mobcost;
476         if (stopping)
477             continue;
478
479         mines = sect.sct_mines;
480         if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
481             (sect.sct_oldown != lp->lnd_own)) {
482             max = lcp->l_item[I_SHELL];
483             shells = lp->lnd_item[I_SHELL];
484             for (m = 0; mines > 0 && m < 5; m++) {
485                 if (chance(0.66)) {
486                     mines--;
487                     shells = MIN(max, shells + 1);
488                 }
489             }
490             sect.sct_mines = mines;
491             lp->lnd_item[I_SHELL] = shells;
492             putsect(&sect);
493         }
494         if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
495             chance(DMINE_LHITCHANCE(mines))) {
496             wu(0, lp->lnd_own,
497                "%s %s,\nand hit a mine in %s while retreating!\n",
498                prland(lp),
499                conditions[findcondition(code)].desc[orig],
500                xyas(newx, newy, lp->lnd_own));
501             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
502             m = MINE_LDAMAGE();
503             if (lcp->l_flags & L_ENGINEER)
504                 m /= 2;
505             landdamage(lp, m);
506             mines--;
507             sect.sct_mines = mines;
508             putsect(&sect);
509             if (!orig)
510                 putland(lp->lnd_uid, lp);
511             return 0;
512         }
513     }
514
515     if (orig) {
516         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
517            prland(lp),
518            conditions[findcondition(code)].desc[orig],
519            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
520     } else {
521         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
522            prland(lp),
523            conditions[findcondition(code)].desc[orig],
524            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
525     }
526     if (!orig)
527         putland(lp->lnd_uid, lp);
528     return 1;
529 }