]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
retreat: Oops on retreating ghosts
[empserver] / src / lib / subs / retreat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  retreat.c: Retreat subroutines
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 2000
31  *     Ron Koenderink, 2005-2006
32  *     Markus Armbruster, 2006-2014
33  */
34
35 #include <config.h>
36
37 #include "chance.h"
38 #include "damage.h"
39 #include "file.h"
40 #include "land.h"
41 #include "map.h"
42 #include "misc.h"
43 #include "nat.h"
44 #include "news.h"
45 #include "nsc.h"
46 #include "optlist.h"
47 #include "path.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 (CANT_HAPPEN(!sp->shp_own))
100         return;
101
102     retreat_ship1(sp, code, 1);
103     if (sp->shp_rpath[0] == 0)
104         sp->shp_rflags = 0;
105
106     if (sp->shp_rflags & RET_GROUP) {
107         snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
108         while (nxtitem(&ni, &ship))
109             if (ship.shp_own == sp->shp_own) {
110                 if (ship.shp_uid != sp->shp_uid) {
111                     retreat_ship1(&ship, code, 0);
112                     getship(ship.shp_uid, &ship);
113                     if (ship.shp_rpath[0] == 0) {
114                         ship.shp_rflags = 0;
115                         putship(ship.shp_uid, &ship);
116                     }
117                 }
118             }
119     }
120 }
121
122 static int
123 retreat_ship1(struct shpstr *sp, char code, int orig)
124
125
126                         /* Is this the originally scared ship, or a follower */
127 {
128     struct sctstr sect;
129     int n;
130     int m;
131     int max;
132     int dir;
133     coord newx;
134     coord newy;
135     coord dx;
136     coord dy;
137     int stopping;
138     int mines;
139     int shells;
140     double mobcost;
141     struct mchrstr *mcp;
142     int changed;
143
144     if (sp->shp_effic < SHIP_MINEFF) {
145         wu(0, sp->shp_own,
146            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
147            prship(sp), conditions[findcondition(code)].desc[orig]);
148         if (!orig)
149             putship(sp->shp_uid, sp);
150         return 0;
151     }
152
153     if (opt_SAIL) {
154         /* can't retreat a ship that's sailin, bad things happend */
155         if (*sp->shp_path) {
156             wu(0, sp->shp_own,
157                "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
158                prship(sp), conditions[findcondition(code)].desc[orig]);
159             if (!orig)
160                 putship(sp->shp_uid, sp);
161             return 0;
162         }
163     }
164     /* check crew - uws don't count */
165     if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
166         wu(0, sp->shp_own,
167            "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
168            conditions[findcondition(code)].desc[orig]);
169         if (!orig)
170             putship(sp->shp_uid, sp);
171         return 0;
172     }
173
174     getsect(sp->shp_x, sp->shp_y, &sect);
175     switch (shp_check_nav(sp, &sect)) {
176     case NAV_02:
177     case NAV_60:
178         wu(0, sp->shp_own,
179            "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
180            prship(sp), conditions[findcondition(code)].desc[orig]);
181         if (!orig)
182             putship(sp->shp_uid, sp);
183         return 0;
184     case NAV_NONE:
185     case NAV_CANAL:
186         wu(0, sp->shp_own,
187            "%s %s,\nbut was landlocked, and couldn't retreat!\n",
188            prship(sp), conditions[findcondition(code)].desc[orig]);
189         if (!orig)
190             putship(sp->shp_uid, sp);
191         return 0;
192     case NAVOK:
193         break;
194     default:
195         CANT_REACH();
196         wu(0, sp->shp_own,
197            "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
198            prship(sp), conditions[findcondition(code)].desc[orig]);
199         if (!orig)
200             putship(sp->shp_uid, sp);
201         return 0;
202     }
203
204     if (sp->shp_mobil <= 0.0) {
205         wu(0, sp->shp_own,
206            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
207            prship(sp), conditions[findcondition(code)].desc[orig]);
208         if (!orig)
209             putship(sp->shp_uid, sp);
210         return 0;
211     }
212
213     n = -MAX_RETREAT;
214     stopping = 0;
215     while (!stopping && n) {
216         dx = dy = 0;
217         if (sp->shp_rpath[0] == 0) {
218             stopping = 1;
219             continue;
220         }
221         if (sp->shp_mobil <= 0.0) {
222             wu(0, sp->shp_own,
223                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
224                prship(sp), conditions[findcondition(code)].desc[orig]);
225             if (!orig)
226                 putship(sp->shp_uid, sp);
227             return 0;
228         }
229         dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
230         memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
231         if (dir < 0)
232             continue;
233         if (dir == DIR_STOP)
234             stopping++;
235         else {
236             dx = diroff[dir][0];
237             dy = diroff[dir][1];
238         }
239         n++;
240
241         mcp = &mchr[(int)sp->shp_type];
242         newx = xnorm(sp->shp_x + dx);
243         newy = ynorm(sp->shp_y + dy);
244         mobcost = shp_mobcost(sp);
245
246         getsect(newx, newy, &sect);
247         if (shp_check_nav(sp, &sect) != NAVOK ||
248             (sect.sct_own
249              && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
250             wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
251                prship(sp), conditions[findcondition(code)].desc[orig],
252                xyas(newx, newy, sp->shp_own));
253             if (!orig)
254                 putship(sp->shp_uid, sp);
255             return 0;
256         }
257         sp->shp_x = newx;
258         sp->shp_y = newy;
259         sp->shp_mobil -= mobcost;
260         sp->shp_mission = 0;
261         if (stopping)
262             continue;
263
264         mines = sect.sct_mines;
265         changed = 0;
266         if (sect.sct_type != SCT_WATER || mines <= 0)
267             continue;
268         if (mcp->m_flags & M_SWEEP) {
269             max = mcp->m_item[I_SHELL];
270             shells = sp->shp_item[I_SHELL];
271             for (m = 0; mines > 0 && m < 5; m++) {
272                 if (chance(0.66)) {
273                     mines--;
274                     shells = MIN(max, shells + 1);
275                     changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
276                                        'X', 0);
277                 }
278             }
279             if (sect.sct_mines != mines) {
280                 wu(0, sp->shp_own,
281                    "%s cleared %d mine%s in %s while retreating\n",
282                    prship(sp), sect.sct_mines - mines,
283                    splur(sect.sct_mines - mines),
284                    xyas(newx, newy, sp->shp_own));
285                 sect.sct_mines = mines;
286                 sp->shp_item[I_SHELL] = shells;
287                 putsect(&sect);
288             }
289             if (changed)
290                 writemap(sp->shp_own);
291         }
292         if (chance(DMINE_HITCHANCE(mines))) {
293             wu(0, sp->shp_own,
294                "%s %s,\nand hit a mine in %s while retreating!\n",
295                prship(sp), conditions[findcondition(code)].desc[orig],
296                xyas(newx, newy, sp->shp_own));
297             nreport(sp->shp_own, N_HIT_MINE, 0, 1);
298             m = MINE_DAMAGE();
299             shipdamage(sp, m);
300             mines--;
301             if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
302                 writemap(sp->shp_own);
303             sect.sct_mines = mines;
304             putsect(&sect);
305             if (!orig)
306                 putship(sp->shp_uid, sp);
307             return 0;
308         }
309     }
310
311     if (orig) {
312         wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
313            prship(sp), conditions[findcondition(code)].desc[orig],
314            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
315     } else {
316         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
317            prship(sp),
318            conditions[findcondition(code)].desc[orig],
319            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
320     }
321     if (!orig)
322         putship(sp->shp_uid, sp);
323     return 1;
324 }
325
326 static int
327 findcondition(char code)
328 {
329     int i;
330
331     for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
332     CANT_HAPPEN(!conditions[i].code);
333     return i;
334 }
335
336 int
337 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
338 {
339     if (dam <= 0)
340         return 0;
341
342     landdamage(lp, dam);
343     if (lp->lnd_rflags & RET_INJURED)
344         retreat_land(lp, 'i');
345
346     return 1;
347 }
348
349 void
350 retreat_land(struct lndstr *lp, char code)
351 {
352     struct nstr_item ni;
353     struct lndstr land;
354
355     if (CANT_HAPPEN(!lp->lnd_own))
356         return;
357
358     retreat_land1(lp, code, 1);
359     if (lp->lnd_rpath[0] == 0)
360         lp->lnd_rflags = 0;
361
362     if (lp->lnd_rflags & RET_GROUP) {
363         snxtitem_group(&ni, EF_LAND, lp->lnd_army);
364         while (nxtitem(&ni, &land))
365             if (land.lnd_own == lp->lnd_own) {
366                 if (land.lnd_uid != lp->lnd_uid) {
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     }
376 }
377
378 static int
379 retreat_land1(struct lndstr *lp, char code, int orig)
380
381
382                         /* Is this the originally scared unit, or a follower */
383 {
384     struct sctstr sect;
385     int n;
386     int m;
387     int max;
388     int dir;
389     coord newx;
390     coord newy;
391     coord dx;
392     coord dy;
393     int stopping;
394     int mines;
395     int shells;
396     double mobcost;
397     struct lchrstr *lcp;
398
399     if (lp->lnd_effic < LAND_MINEFF) {
400         wu(0, lp->lnd_own,
401            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
402            prland(lp), conditions[findcondition(code)].desc[orig]);
403         if (!orig)
404             putland(lp->lnd_uid, lp);
405         return 0;
406     }
407
408     getsect(lp->lnd_x, lp->lnd_y, &sect);
409
410     if (lp->lnd_mobil <= 0.0) {
411         wu(0, lp->lnd_own,
412            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
413            prland(lp), conditions[findcondition(code)].desc[orig]);
414         if (!orig)
415             putland(lp->lnd_uid, lp);
416         return 0;
417     }
418
419     n = -MAX_RETREAT;
420     stopping = 0;
421     while (!stopping && n) {
422         dx = dy = 0;
423         if (lp->lnd_rpath[0] == 0) {
424             stopping = 1;
425             continue;
426         }
427         if (lp->lnd_mobil <= 0.0) {
428             wu(0, lp->lnd_own,
429                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
430                prland(lp), conditions[findcondition(code)].desc[orig]);
431             if (!orig)
432                 putland(lp->lnd_uid, lp);
433             return 0;
434         }
435         dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
436         memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
437         if (dir < 0)
438             continue;
439         if (dir == DIR_STOP)
440             stopping++;
441         else {
442             dx = diroff[dir][0];
443             dy = diroff[dir][1];
444         }
445         n++;
446
447         lcp = &lchr[(int)lp->lnd_type];
448         newx = xnorm(lp->lnd_x + dx);
449         newy = ynorm(lp->lnd_y + dy);
450
451         getsect(newx, newy, &sect);
452         mobcost = lnd_mobcost(lp, &sect);
453         if (mobcost < 0
454             || sect.sct_type == SCT_MOUNT
455             || sect.sct_own != lp->lnd_own) {
456             wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
457                prland(lp),
458                conditions[findcondition(code)].desc[orig],
459                xyas(newx, newy, lp->lnd_own));
460             if (!orig)
461                 putland(lp->lnd_uid, lp);
462             return 0;
463         }
464         lp->lnd_x = newx;
465         lp->lnd_y = newy;
466         lp->lnd_mobil -= mobcost;
467         lp->lnd_mission = 0;
468         if (stopping)
469             continue;
470
471         mines = SCT_LANDMINES(&sect);
472         if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
473             continue;
474         if (lcp->l_flags & L_ENGINEER) {
475             max = lcp->l_item[I_SHELL];
476             shells = lp->lnd_item[I_SHELL];
477             for (m = 0; mines > 0 && m < 5; m++) {
478                 if (chance(0.66)) {
479                     mines--;
480                     shells = MIN(max, shells + 1);
481                 }
482             }
483             sect.sct_mines = mines;
484             lp->lnd_item[I_SHELL] = shells;
485             putsect(&sect);
486         }
487         if (chance(DMINE_LHITCHANCE(mines))) {
488             wu(0, lp->lnd_own,
489                "%s %s,\nand hit a mine in %s while retreating!\n",
490                prland(lp),
491                conditions[findcondition(code)].desc[orig],
492                xyas(newx, newy, lp->lnd_own));
493             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
494             m = MINE_LDAMAGE();
495             if (lcp->l_flags & L_ENGINEER)
496                 m /= 2;
497             landdamage(lp, m);
498             mines--;
499             sect.sct_mines = mines;
500             putsect(&sect);
501             if (!orig)
502                 putland(lp->lnd_uid, lp);
503             return 0;
504         }
505     }
506
507     if (orig) {
508         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
509            prland(lp),
510            conditions[findcondition(code)].desc[orig],
511            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
512     } else {
513         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
514            prland(lp),
515            conditions[findcondition(code)].desc[orig],
516            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
517     }
518     if (!orig)
519         putland(lp->lnd_uid, lp);
520     return 1;
521 }