]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
e57a8d87f85e91e74b160582be94b59e41932cb1
[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         wu(0, sp->shp_own,
128            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
129            prship(sp), conditions[findcondition(code)].desc[orig]);
130         if (!orig)
131             putship(sp->shp_uid, sp);
132         return 0;
133     }
134
135     /* check crew - uws don't count */
136     if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
137         wu(0, sp->shp_own,
138            "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
139            conditions[findcondition(code)].desc[orig]);
140         return 0;
141     }
142
143     getsect(sp->shp_x, sp->shp_y, &sect);
144     switch (shp_check_nav(sp, &sect)) {
145     case SHP_STUCK_NOT:
146         break;
147     case SHP_STUCK_CONSTRUCTION:
148         wu(0, sp->shp_own,
149            "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
150            prship(sp), conditions[findcondition(code)].desc[orig]);
151         return 0;
152     default:
153         CANT_REACH();
154         /* fall through */
155     case SHP_STUCK_CANAL:
156     case SHP_STUCK_IMPASSABLE:
157         wu(0, sp->shp_own,
158            "%s %s,\nbut was landlocked, and couldn't retreat!\n",
159            prship(sp), conditions[findcondition(code)].desc[orig]);
160         return 0;
161     }
162
163     if (sp->shp_mobil <= 0.0) {
164         wu(0, sp->shp_own,
165            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
166            prship(sp), conditions[findcondition(code)].desc[orig]);
167         return 0;
168     }
169
170     for (i = 0; i < MAX_RETREAT && sp->shp_rpath[0]; i++) {
171         if (sp->shp_mobil <= 0.0) {
172             wu(0, sp->shp_own,
173                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
174                prship(sp), conditions[findcondition(code)].desc[orig]);
175             return 1;
176         }
177         dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
178         if (dir == DIR_STOP || CANT_HAPPEN(dir < 0)) {
179             memmove(sp->shp_rpath, sp->shp_rpath + 1,
180                     sizeof(sp->shp_rpath) - 1);
181             if (sp->shp_rpath[0] == 0)
182                 sp->shp_rflags = 0;
183             break;
184         }
185         dx = diroff[dir][0];
186         dy = diroff[dir][1];
187
188         mcp = &mchr[(int)sp->shp_type];
189         newx = xnorm(sp->shp_x + dx);
190         newy = ynorm(sp->shp_y + dy);
191         mobcost = shp_mobcost(sp);
192
193         getsect(newx, newy, &sect);
194         if (shp_check_nav(sp, &sect) != SHP_STUCK_NOT ||
195             (sect.sct_own
196              && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
197             wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
198                prship(sp), conditions[findcondition(code)].desc[orig],
199                xyas(newx, newy, sp->shp_own));
200             return 1;
201         }
202         sp->shp_x = newx;
203         sp->shp_y = newy;
204         sp->shp_mobil -= mobcost;
205         sp->shp_mission = 0;
206         memmove(sp->shp_rpath, sp->shp_rpath + 1,
207                 sizeof(sp->shp_rpath) - 1);
208         if (sp->shp_rpath[0] == 0)
209             sp->shp_rflags = 0;
210
211         mines = sect.sct_mines;
212         changed = 0;
213         if (sect.sct_type != SCT_WATER || mines <= 0)
214             continue;
215         if (mcp->m_flags & M_SWEEP) {
216             max = mcp->m_item[I_SHELL];
217             shells = sp->shp_item[I_SHELL];
218             for (m = 0; mines > 0 && m < 5; m++) {
219                 if (chance(0.66)) {
220                     mines--;
221                     shells = MIN(max, shells + 1);
222                     changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
223                                        'X', 0);
224                 }
225             }
226             if (sect.sct_mines != mines) {
227                 wu(0, sp->shp_own,
228                    "%s cleared %d mine%s in %s while retreating\n",
229                    prship(sp), sect.sct_mines - mines,
230                    splur(sect.sct_mines - mines),
231                    xyas(newx, newy, sp->shp_own));
232                 sect.sct_mines = mines;
233                 sp->shp_item[I_SHELL] = shells;
234                 putsect(&sect);
235             }
236             if (changed)
237                 writemap(sp->shp_own);
238         }
239         if (chance(DMINE_HITCHANCE(mines))) {
240             wu(0, sp->shp_own,
241                "%s %s,\nand hit a mine in %s while retreating!\n",
242                prship(sp), conditions[findcondition(code)].desc[orig],
243                xyas(newx, newy, sp->shp_own));
244             nreport(sp->shp_own, N_HIT_MINE, 0, 1);
245             m = MINE_DAMAGE();
246             shipdamage(sp, m);
247             mines--;
248             if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
249                 writemap(sp->shp_own);
250             sect.sct_mines = mines;
251             putsect(&sect);
252             return 1;
253         }
254     }
255
256     if (orig) {
257         wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
258            prship(sp), conditions[findcondition(code)].desc[orig],
259            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
260     } else {
261         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
262            prship(sp),
263            conditions[findcondition(code)].desc[orig],
264            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
265     }
266
267     return 1;
268 }
269
270 static int
271 findcondition(char code)
272 {
273     int i;
274
275     for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
276     CANT_HAPPEN(!conditions[i].code);
277     return i;
278 }
279
280 void
281 retreat_land(struct lndstr *lp, char code)
282 {
283     struct nstr_item ni;
284     struct lndstr land;
285
286     if (CANT_HAPPEN(!lp->lnd_own))
287         return;
288     if (lp->lnd_own == player->cnum)
289         return;
290
291     retreat_land1(lp, code, 1);
292
293     if (lp->lnd_rflags & RET_GROUP) {
294         snxtitem_group(&ni, EF_LAND, lp->lnd_army);
295         while (nxtitem(&ni, &land)) {
296             if (land.lnd_own != lp->lnd_own || land.lnd_uid == lp->lnd_uid)
297                 continue;
298             if (retreat_land1(&land, code, 0))
299                 putland(land.lnd_uid, &land);
300         }
301     }
302 }
303
304 static int
305 retreat_land1(struct lndstr *lp, char code, int orig)
306
307
308                         /* Is this the originally scared unit, or a follower */
309 {
310     struct sctstr sect;
311     int i;
312     int m;
313     int max;
314     int dir;
315     coord newx;
316     coord newy;
317     coord dx;
318     coord dy;
319     int mines;
320     int shells;
321     double mobcost;
322     struct lchrstr *lcp;
323
324     if (lp->lnd_effic < LAND_MINEFF) {
325         wu(0, lp->lnd_own,
326            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
327            prland(lp), conditions[findcondition(code)].desc[orig]);
328         if (!orig)
329             putland(lp->lnd_uid, lp);
330         return 0;
331     }
332
333     getsect(lp->lnd_x, lp->lnd_y, &sect);
334
335     if (lp->lnd_mobil <= 0.0) {
336         wu(0, lp->lnd_own,
337            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
338            prland(lp), conditions[findcondition(code)].desc[orig]);
339         return 0;
340     }
341
342     for (i = 0; i < MAX_RETREAT && lp->lnd_rpath[0]; i++) {
343         if (lp->lnd_mobil <= 0.0) {
344             wu(0, lp->lnd_own,
345                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
346                prland(lp), conditions[findcondition(code)].desc[orig]);
347             return 1;
348         }
349         dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
350         if (dir == DIR_STOP || CANT_HAPPEN(dir < 0)) {
351             memmove(lp->lnd_rpath, lp->lnd_rpath + 1,
352                     sizeof(lp->lnd_rpath) - 1);
353             if (lp->lnd_rpath[0] == 0)
354                 lp->lnd_rflags = 0;
355             break;
356         }
357         dx = diroff[dir][0];
358         dy = diroff[dir][1];
359
360         lcp = &lchr[(int)lp->lnd_type];
361         newx = xnorm(lp->lnd_x + dx);
362         newy = ynorm(lp->lnd_y + dy);
363
364         getsect(newx, newy, &sect);
365         mobcost = lnd_mobcost(lp, &sect);
366         if (mobcost < 0
367             || sect.sct_type == SCT_MOUNT
368             || sect.sct_own != lp->lnd_own) {
369             wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
370                prland(lp),
371                conditions[findcondition(code)].desc[orig],
372                xyas(newx, newy, lp->lnd_own));
373             return 1;
374         }
375         lp->lnd_x = newx;
376         lp->lnd_y = newy;
377         lp->lnd_mobil -= mobcost;
378         lp->lnd_mission = 0;
379         memmove(lp->lnd_rpath, lp->lnd_rpath + 1,
380                 sizeof(lp->lnd_rpath) - 1);
381         if (lp->lnd_rpath[0] == 0)
382             lp->lnd_rflags = 0;
383
384         mines = SCT_LANDMINES(&sect);
385         if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
386             continue;
387         if (lcp->l_flags & L_ENGINEER) {
388             max = lcp->l_item[I_SHELL];
389             shells = lp->lnd_item[I_SHELL];
390             for (m = 0; mines > 0 && m < 5; m++) {
391                 if (chance(0.66)) {
392                     mines--;
393                     shells = MIN(max, shells + 1);
394                 }
395             }
396             sect.sct_mines = mines;
397             lp->lnd_item[I_SHELL] = shells;
398             putsect(&sect);
399         }
400         if (chance(DMINE_LHITCHANCE(mines))) {
401             wu(0, lp->lnd_own,
402                "%s %s,\nand hit a mine in %s while retreating!\n",
403                prland(lp),
404                conditions[findcondition(code)].desc[orig],
405                xyas(newx, newy, lp->lnd_own));
406             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
407             m = MINE_LDAMAGE();
408             if (lcp->l_flags & L_ENGINEER)
409                 m /= 2;
410             landdamage(lp, m);
411             mines--;
412             sect.sct_mines = mines;
413             putsect(&sect);
414             return 1;
415         }
416     }
417
418     if (orig) {
419         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
420            prland(lp),
421            conditions[findcondition(code)].desc[orig],
422            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
423     } else {
424         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
425            prland(lp),
426            conditions[findcondition(code)].desc[orig],
427            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
428     }
429
430     return 1;
431 }