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