]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
Delete trivial instances of /*NOTREACHED*/
[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     retreat_ship1(sp, code, 1);
100     if (sp->shp_rpath[0] == 0)
101         sp->shp_rflags = 0;
102
103     if (sp->shp_rflags & RET_GROUP) {
104         snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
105         while (nxtitem(&ni, &ship))
106             if (ship.shp_own == sp->shp_own) {
107                 if (ship.shp_uid != sp->shp_uid) {
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     }
117 }
118
119 static int
120 retreat_ship1(struct shpstr *sp, char code, int orig)
121
122
123                         /* Is this the originally scared ship, or a follower */
124 {
125     struct sctstr sect;
126     int n;
127     int m;
128     int max;
129     int dir;
130     coord newx;
131     coord newy;
132     coord dx;
133     coord dy;
134     int stopping;
135     int mines;
136     int shells;
137     double mobcost;
138     struct mchrstr *mcp;
139     int changed;
140
141     if (sp->shp_own == 0)
142         return 0;
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(&sect, sp)) {
176     case CN_CONSTRUCTION:
177         wu(0, sp->shp_own,
178            "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
179            prship(sp), conditions[findcondition(code)].desc[orig]);
180         if (!orig)
181             putship(sp->shp_uid, sp);
182         return 0;
183     case CN_LANDLOCKED:
184         wu(0, sp->shp_own,
185            "%s %s,\nbut was landlocked, and couldn't retreat!\n",
186            prship(sp), conditions[findcondition(code)].desc[orig]);
187         if (!orig)
188             putship(sp->shp_uid, sp);
189         return 0;
190     case CN_NAVIGABLE:
191         break;
192     case CN_ERROR:
193     default:
194         wu(0, sp->shp_own,
195            "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
196            prship(sp), conditions[findcondition(code)].desc[orig]);
197         if (!orig)
198             putship(sp->shp_uid, sp);
199         return 0;
200     }
201
202     if (sp->shp_mobil <= 0.0) {
203         wu(0, sp->shp_own,
204            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
205            prship(sp), conditions[findcondition(code)].desc[orig]);
206         if (!orig)
207             putship(sp->shp_uid, sp);
208         return 0;
209     }
210
211     n = -MAX_RETREAT;
212     stopping = 0;
213     while (!stopping && n) {
214         dx = dy = 0;
215         if (sp->shp_rpath[0] == 0) {
216             stopping = 1;
217             continue;
218         }
219         if (sp->shp_mobil <= 0.0) {
220             wu(0, sp->shp_own,
221                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
222                prship(sp), conditions[findcondition(code)].desc[orig]);
223             if (!orig)
224                 putship(sp->shp_uid, sp);
225             return 0;
226         }
227         dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
228         memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
229         if (dir < 0)
230             continue;
231         if (dir == DIR_STOP)
232             stopping++;
233         else {
234             dx = diroff[dir][0];
235             dy = diroff[dir][1];
236         }
237         n++;
238
239         mcp = &mchr[(int)sp->shp_type];
240         newx = xnorm(sp->shp_x + dx);
241         newy = ynorm(sp->shp_y + dy);
242         mobcost = shp_mobcost(sp);
243
244         getsect(newx, newy, &sect);
245         if (shp_check_nav(&sect, sp) != CN_NAVIGABLE ||
246             (sect.sct_own
247              && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
248             wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
249                prship(sp), conditions[findcondition(code)].desc[orig],
250                xyas(newx, newy, sp->shp_own));
251             if (!orig)
252                 putship(sp->shp_uid, sp);
253             return 0;
254         }
255         sp->shp_x = newx;
256         sp->shp_y = newy;
257         sp->shp_mobil -= mobcost;
258         sp->shp_mission = 0;
259         if (stopping)
260             continue;
261
262         mines = sect.sct_mines;
263         changed = 0;
264         if (sect.sct_type != SCT_WATER || mines <= 0)
265             continue;
266         if (mcp->m_flags & M_SWEEP) {
267             max = mcp->m_item[I_SHELL];
268             shells = sp->shp_item[I_SHELL];
269             for (m = 0; mines > 0 && m < 5; m++) {
270                 if (chance(0.66)) {
271                     mines--;
272                     shells = MIN(max, shells + 1);
273                     changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
274                                        'X', 0);
275                 }
276             }
277             if (sect.sct_mines != mines) {
278                 wu(0, sp->shp_own,
279                    "%s cleared %d mine%s in %s while retreating\n",
280                    prship(sp), sect.sct_mines - mines,
281                    splur(sect.sct_mines - mines),
282                    xyas(newx, newy, sp->shp_own));
283                 sect.sct_mines = mines;
284                 sp->shp_item[I_SHELL] = shells;
285                 putsect(&sect);
286             }
287             if (changed)
288                 writemap(sp->shp_own);
289         }
290         if (chance(DMINE_HITCHANCE(mines))) {
291             wu(0, sp->shp_own,
292                "%s %s,\nand hit a mine in %s while retreating!\n",
293                prship(sp), conditions[findcondition(code)].desc[orig],
294                xyas(newx, newy, sp->shp_own));
295             nreport(sp->shp_own, N_HIT_MINE, 0, 1);
296             m = MINE_DAMAGE();
297             shipdamage(sp, m);
298             mines--;
299             if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
300                 writemap(sp->shp_own);
301             sect.sct_mines = mines;
302             putsect(&sect);
303             if (!orig)
304                 putship(sp->shp_uid, sp);
305             return 0;
306         }
307     }
308
309     if (orig) {
310         wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
311            prship(sp), conditions[findcondition(code)].desc[orig],
312            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
313     } else {
314         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
315            prship(sp),
316            conditions[findcondition(code)].desc[orig],
317            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
318     }
319     if (!orig)
320         putship(sp->shp_uid, sp);
321     return 1;
322 }
323
324 static int
325 findcondition(char code)
326 {
327     int i;
328
329     for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
330     CANT_HAPPEN(!conditions[i].code);
331     return i;
332 }
333
334 int
335 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
336 {
337     if (dam <= 0)
338         return 0;
339
340     landdamage(lp, dam);
341     if (lp->lnd_rflags & RET_INJURED)
342         retreat_land(lp, 'i');
343
344     return 1;
345 }
346
347 void
348 retreat_land(struct lndstr *lp, char code)
349 {
350     struct nstr_item ni;
351     struct lndstr land;
352
353     retreat_land1(lp, code, 1);
354     if (lp->lnd_rpath[0] == 0)
355         lp->lnd_rflags = 0;
356
357     if (lp->lnd_rflags & RET_GROUP) {
358         snxtitem_group(&ni, EF_LAND, lp->lnd_army);
359         while (nxtitem(&ni, &land))
360             if (land.lnd_own == lp->lnd_own) {
361                 if (land.lnd_uid != lp->lnd_uid) {
362                     retreat_land1(&land, code, 0);
363                     getland(land.lnd_uid, &land);
364                     if (land.lnd_rpath[0] == 0) {
365                         land.lnd_rflags = 0;
366                         putland(land.lnd_uid, &land);
367                     }
368                 }
369             }
370     }
371 }
372
373 static int
374 retreat_land1(struct lndstr *lp, char code, int orig)
375
376
377                         /* Is this the originally scared unit, or a follower */
378 {
379     struct sctstr sect;
380     int n;
381     int m;
382     int max;
383     int dir;
384     coord newx;
385     coord newy;
386     coord dx;
387     coord dy;
388     int stopping;
389     int mines;
390     int shells;
391     double mobcost;
392     struct lchrstr *lcp;
393
394     if (lp->lnd_own == 0)
395         return 0;
396
397     if (lp->lnd_effic < LAND_MINEFF) {
398         wu(0, lp->lnd_own,
399            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
400            prland(lp), conditions[findcondition(code)].desc[orig]);
401         if (!orig)
402             putland(lp->lnd_uid, lp);
403         return 0;
404     }
405
406     getsect(lp->lnd_x, lp->lnd_y, &sect);
407
408     if (lp->lnd_mobil <= 0.0) {
409         wu(0, lp->lnd_own,
410            "%s %s,\nbut had no mobility, and 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     n = -MAX_RETREAT;
418     stopping = 0;
419     while (!stopping && n) {
420         dx = dy = 0;
421         if (lp->lnd_rpath[0] == 0) {
422             stopping = 1;
423             continue;
424         }
425         if (lp->lnd_mobil <= 0.0) {
426             wu(0, lp->lnd_own,
427                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
428                prland(lp), conditions[findcondition(code)].desc[orig]);
429             if (!orig)
430                 putland(lp->lnd_uid, lp);
431             return 0;
432         }
433         dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
434         memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
435         if (dir < 0)
436             continue;
437         if (dir == DIR_STOP)
438             stopping++;
439         else {
440             dx = diroff[dir][0];
441             dy = diroff[dir][1];
442         }
443         n++;
444
445         lcp = &lchr[(int)lp->lnd_type];
446         newx = xnorm(lp->lnd_x + dx);
447         newy = ynorm(lp->lnd_y + dy);
448
449         getsect(newx, newy, &sect);
450         mobcost = lnd_mobcost(lp, &sect);
451         if (mobcost < 0
452             || sect.sct_type == SCT_MOUNT
453             || sect.sct_own != lp->lnd_own) {
454             wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
455                prland(lp),
456                conditions[findcondition(code)].desc[orig],
457                xyas(newx, newy, lp->lnd_own));
458             if (!orig)
459                 putland(lp->lnd_uid, lp);
460             return 0;
461         }
462         lp->lnd_x = newx;
463         lp->lnd_y = newy;
464         lp->lnd_mobil -= mobcost;
465         lp->lnd_mission = 0;
466         if (stopping)
467             continue;
468
469         mines = SCT_LANDMINES(&sect);
470         if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
471             continue;
472         if (lcp->l_flags & L_ENGINEER) {
473             max = lcp->l_item[I_SHELL];
474             shells = lp->lnd_item[I_SHELL];
475             for (m = 0; mines > 0 && m < 5; m++) {
476                 if (chance(0.66)) {
477                     mines--;
478                     shells = MIN(max, shells + 1);
479                 }
480             }
481             sect.sct_mines = mines;
482             lp->lnd_item[I_SHELL] = shells;
483             putsect(&sect);
484         }
485         if (chance(DMINE_LHITCHANCE(mines))) {
486             wu(0, lp->lnd_own,
487                "%s %s,\nand hit a mine in %s while retreating!\n",
488                prland(lp),
489                conditions[findcondition(code)].desc[orig],
490                xyas(newx, newy, lp->lnd_own));
491             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
492             m = MINE_LDAMAGE();
493             if (lcp->l_flags & L_ENGINEER)
494                 m /= 2;
495             landdamage(lp, m);
496             mines--;
497             sect.sct_mines = mines;
498             putsect(&sect);
499             if (!orig)
500                 putland(lp->lnd_uid, lp);
501             return 0;
502         }
503     }
504
505     if (orig) {
506         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
507            prland(lp),
508            conditions[findcondition(code)].desc[orig],
509            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
510     } else {
511         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
512            prland(lp),
513            conditions[findcondition(code)].desc[orig],
514            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
515     }
516     if (!orig)
517         putland(lp->lnd_uid, lp);
518     return 1;
519 }