]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
5ecd294c99251a3fc893fef30361d899858d3fed
[empserver] / src / lib / subs / retreat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, 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 "player.h"
49 #include "prototypes.h"
50 #include "retreat.h"
51 #include "sect.h"
52 #include "ship.h"
53 #include "xy.h"
54
55 static int findcondition(char);
56 static int retreat_land1(struct lndstr *, char, int);
57 static int retreat_ship1(struct shpstr *, char, int);
58
59 struct ccode {
60     char code;
61     char *desc[2];
62 };
63
64 static struct ccode conditions[] = {
65     { 'i', { "retreated with a damaged friend",
66              "was damaged" } },
67     { 't', { "retreated with a torpedoed ship",
68              "was hit by a torpedo" } },
69     { 's', { "retreated with a ship scared by sonar",
70              "detected a sonar ping" } },
71     { 'h', { "retreated with a helpless ship",
72              "was fired upon with no one able to defend it" } },
73     { 'b', { "retreated with a bombed friend",
74              "was bombed" } },
75     { 'd', { "retreated with a depth-charged ship",
76              "was depth-charged" } },
77     { 'u', { "retreated with a boarded ship", "was boarded" } },
78     { 0,   { "panicked", "panicked"} }
79 };
80
81 void
82 retreat_ship(struct shpstr *sp, char code)
83 {
84     struct nstr_item ni;
85     struct shpstr ship;
86
87     if (CANT_HAPPEN(!sp->shp_own))
88         return;
89     if (sp->shp_own == player->cnum)
90         return;
91
92     retreat_ship1(sp, code, 1);
93
94     if (sp->shp_rflags & RET_GROUP) {
95         snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
96         while (nxtitem(&ni, &ship)) {
97             if (ship.shp_own != sp->shp_own || ship.shp_uid == sp->shp_uid)
98                 continue;
99             if (retreat_ship1(&ship, code, 0))
100                 putship(ship.shp_uid, &ship);
101         }
102     }
103 }
104
105 static int
106 retreat_ship1(struct shpstr *sp, char code, int orig)
107
108
109                         /* Is this the originally scared ship, or a follower */
110 {
111     struct sctstr sect;
112     int i;
113     int m;
114     int max;
115     int dir;
116     coord newx;
117     coord newy;
118     coord dx;
119     coord dy;
120     int mines;
121     int shells;
122     double mobcost;
123     struct mchrstr *mcp;
124     int changed;
125
126     if (sp->shp_effic < SHIP_MINEFF) {
127         if (!orig)
128             putship(sp->shp_uid, sp);
129         return 0;
130     }
131
132     /* check crew - uws don't count */
133     if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
134         wu(0, sp->shp_own,
135            "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
136            conditions[findcondition(code)].desc[orig]);
137         return 0;
138     }
139
140     getsect(sp->shp_x, sp->shp_y, &sect);
141     switch (shp_check_nav(sp, &sect)) {
142     case SHP_STUCK_NOT:
143         break;
144     case SHP_STUCK_CONSTRUCTION:
145         wu(0, sp->shp_own,
146            "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
147            prship(sp), conditions[findcondition(code)].desc[orig]);
148         return 0;
149     default:
150         CANT_REACH();
151         /* fall through */
152     case SHP_STUCK_CANAL:
153     case SHP_STUCK_IMPASSABLE:
154         wu(0, sp->shp_own,
155            "%s %s,\nbut was landlocked, and couldn't retreat!\n",
156            prship(sp), conditions[findcondition(code)].desc[orig]);
157         return 0;
158     }
159
160     if (sp->shp_mobil <= 0.0) {
161         wu(0, sp->shp_own,
162            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
163            prship(sp), conditions[findcondition(code)].desc[orig]);
164         return 0;
165     }
166
167     for (i = 0; i < MAX_RETREAT && sp->shp_rpath[0]; i++) {
168         if (sp->shp_mobil <= 0.0) {
169             wu(0, sp->shp_own,
170                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
171                prship(sp), conditions[findcondition(code)].desc[orig]);
172             return 1;
173         }
174         dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
175         if (dir == DIR_STOP || CANT_HAPPEN(dir < 0)) {
176             memmove(sp->shp_rpath, sp->shp_rpath + 1,
177                     sizeof(sp->shp_rpath) - 1);
178             if (sp->shp_rpath[0] == 0)
179                 sp->shp_rflags = 0;
180             break;
181         }
182         dx = diroff[dir][0];
183         dy = diroff[dir][1];
184
185         mcp = &mchr[(int)sp->shp_type];
186         newx = xnorm(sp->shp_x + dx);
187         newy = ynorm(sp->shp_y + dy);
188         mobcost = shp_mobcost(sp);
189
190         getsect(newx, newy, &sect);
191         if (shp_check_nav(sp, &sect) != SHP_STUCK_NOT ||
192             (sect.sct_own
193              && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
194             wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
195                prship(sp), conditions[findcondition(code)].desc[orig],
196                xyas(newx, newy, sp->shp_own));
197             return 1;
198         }
199         sp->shp_x = newx;
200         sp->shp_y = newy;
201         sp->shp_mobil -= mobcost;
202         sp->shp_mission = 0;
203         memmove(sp->shp_rpath, sp->shp_rpath + 1,
204                 sizeof(sp->shp_rpath) - 1);
205         if (sp->shp_rpath[0] == 0)
206             sp->shp_rflags = 0;
207
208         mines = sect.sct_mines;
209         changed = 0;
210         if (sect.sct_type != SCT_WATER || mines <= 0)
211             continue;
212         if (mcp->m_flags & M_SWEEP) {
213             max = mcp->m_item[I_SHELL];
214             shells = sp->shp_item[I_SHELL];
215             for (m = 0; mines > 0 && m < 5; m++) {
216                 if (chance(0.66)) {
217                     mines--;
218                     shells = MIN(max, shells + 1);
219                     changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
220                                        'X', 0);
221                 }
222             }
223             if (sect.sct_mines != mines) {
224                 wu(0, sp->shp_own,
225                    "%s cleared %d mine%s in %s while retreating\n",
226                    prship(sp), sect.sct_mines - mines,
227                    splur(sect.sct_mines - mines),
228                    xyas(newx, newy, sp->shp_own));
229                 sect.sct_mines = mines;
230                 sp->shp_item[I_SHELL] = shells;
231                 putsect(&sect);
232             }
233             if (changed)
234                 writemap(sp->shp_own);
235         }
236         if (chance(DMINE_HITCHANCE(mines))) {
237             wu(0, sp->shp_own,
238                "%s %s,\nand hit a mine in %s while retreating!\n",
239                prship(sp), conditions[findcondition(code)].desc[orig],
240                xyas(newx, newy, sp->shp_own));
241             nreport(sp->shp_own, N_HIT_MINE, 0, 1);
242             m = MINE_DAMAGE();
243             shipdamage(sp, m);
244             mines--;
245             if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
246                 writemap(sp->shp_own);
247             sect.sct_mines = mines;
248             putsect(&sect);
249             return 1;
250         }
251     }
252
253     if (orig) {
254         wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
255            prship(sp), conditions[findcondition(code)].desc[orig],
256            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
257     } else {
258         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
259            prship(sp),
260            conditions[findcondition(code)].desc[orig],
261            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
262     }
263
264     return 1;
265 }
266
267 static int
268 findcondition(char code)
269 {
270     int i;
271
272     for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
273     CANT_HAPPEN(!conditions[i].code);
274     return i;
275 }
276
277 void
278 retreat_land(struct lndstr *lp, char code)
279 {
280     struct nstr_item ni;
281     struct lndstr land;
282
283     if (CANT_HAPPEN(!lp->lnd_own))
284         return;
285     if (lp->lnd_own == player->cnum)
286         return;
287
288     retreat_land1(lp, code, 1);
289
290     if (lp->lnd_rflags & RET_GROUP) {
291         snxtitem_group(&ni, EF_LAND, lp->lnd_army);
292         while (nxtitem(&ni, &land)) {
293             if (land.lnd_own != lp->lnd_own || land.lnd_uid == lp->lnd_uid)
294                 continue;
295             if (retreat_land1(&land, code, 0))
296                 putland(land.lnd_uid, &land);
297         }
298     }
299 }
300
301 static int
302 retreat_land1(struct lndstr *lp, char code, int orig)
303
304
305                         /* Is this the originally scared unit, or a follower */
306 {
307     struct sctstr sect;
308     int i;
309     int m;
310     int max;
311     int dir;
312     coord newx;
313     coord newy;
314     coord dx;
315     coord dy;
316     int mines;
317     int shells;
318     double mobcost;
319     struct lchrstr *lcp;
320
321     if (lp->lnd_effic < LAND_MINEFF) {
322         if (!orig)
323             putland(lp->lnd_uid, lp);
324         return 0;
325     }
326
327     getsect(lp->lnd_x, lp->lnd_y, &sect);
328
329     if (lp->lnd_mobil <= 0.0) {
330         wu(0, lp->lnd_own,
331            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
332            prland(lp), conditions[findcondition(code)].desc[orig]);
333         return 0;
334     }
335
336     for (i = 0; i < MAX_RETREAT && lp->lnd_rpath[0]; i++) {
337         if (lp->lnd_mobil <= 0.0) {
338             wu(0, lp->lnd_own,
339                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
340                prland(lp), conditions[findcondition(code)].desc[orig]);
341             return 1;
342         }
343         dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
344         if (dir == DIR_STOP || CANT_HAPPEN(dir < 0)) {
345             memmove(lp->lnd_rpath, lp->lnd_rpath + 1,
346                     sizeof(lp->lnd_rpath) - 1);
347             if (lp->lnd_rpath[0] == 0)
348                 lp->lnd_rflags = 0;
349             break;
350         }
351         dx = diroff[dir][0];
352         dy = diroff[dir][1];
353
354         lcp = &lchr[(int)lp->lnd_type];
355         newx = xnorm(lp->lnd_x + dx);
356         newy = ynorm(lp->lnd_y + dy);
357
358         getsect(newx, newy, &sect);
359         mobcost = lnd_mobcost(lp, &sect);
360         if (mobcost < 0
361             || sect.sct_type == SCT_MOUNT
362             || sect.sct_own != lp->lnd_own) {
363             wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
364                prland(lp),
365                conditions[findcondition(code)].desc[orig],
366                xyas(newx, newy, lp->lnd_own));
367             return 1;
368         }
369         lp->lnd_x = newx;
370         lp->lnd_y = newy;
371         lp->lnd_mobil -= mobcost;
372         lp->lnd_mission = 0;
373         memmove(lp->lnd_rpath, lp->lnd_rpath + 1,
374                 sizeof(lp->lnd_rpath) - 1);
375         if (lp->lnd_rpath[0] == 0)
376             lp->lnd_rflags = 0;
377
378         mines = SCT_LANDMINES(&sect);
379         if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
380             continue;
381         if (lcp->l_flags & L_ENGINEER) {
382             max = lcp->l_item[I_SHELL];
383             shells = lp->lnd_item[I_SHELL];
384             for (m = 0; mines > 0 && m < 5; m++) {
385                 if (chance(0.66)) {
386                     mines--;
387                     shells = MIN(max, shells + 1);
388                 }
389             }
390             sect.sct_mines = mines;
391             lp->lnd_item[I_SHELL] = shells;
392             putsect(&sect);
393         }
394         if (chance(DMINE_LHITCHANCE(mines))) {
395             wu(0, lp->lnd_own,
396                "%s %s,\nand hit a mine in %s while retreating!\n",
397                prland(lp),
398                conditions[findcondition(code)].desc[orig],
399                xyas(newx, newy, lp->lnd_own));
400             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
401             m = MINE_LDAMAGE();
402             if (lcp->l_flags & L_ENGINEER)
403                 m /= 2;
404             landdamage(lp, m);
405             mines--;
406             sect.sct_mines = mines;
407             putsect(&sect);
408             return 1;
409         }
410     }
411
412     if (orig) {
413         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
414            prland(lp),
415            conditions[findcondition(code)].desc[orig],
416            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
417     } else {
418         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
419            prland(lp),
420            conditions[findcondition(code)].desc[orig],
421            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
422     }
423
424     return 1;
425 }