]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
retreat: Don't report a destroyed ship/land unit couldn't retreat
[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 (sp->shp_effic < SHIP_MINEFF || 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 (opt_SAIL) {
145         /* can't retreat a ship that's sailin, bad things happend */
146         if (*sp->shp_path) {
147             wu(0, sp->shp_own,
148                "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
149                prship(sp), conditions[findcondition(code)].desc[orig]);
150             if (!orig)
151                 putship(sp->shp_uid, sp);
152             return 0;
153         }
154     }
155     /* check crew - uws don't count */
156     if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
157         wu(0, sp->shp_own,
158            "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
159            conditions[findcondition(code)].desc[orig]);
160         if (!orig)
161             putship(sp->shp_uid, sp);
162         return 0;
163     }
164
165     getsect(sp->shp_x, sp->shp_y, &sect);
166     switch (shp_check_nav(sp, &sect)) {
167     case NAV_02:
168     case NAV_60:
169         wu(0, sp->shp_own,
170            "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
171            prship(sp), conditions[findcondition(code)].desc[orig]);
172         if (!orig)
173             putship(sp->shp_uid, sp);
174         return 0;
175     case NAV_NONE:
176     case NAV_CANAL:
177         wu(0, sp->shp_own,
178            "%s %s,\nbut was landlocked, 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 NAVOK:
184         break;
185     default:
186         CANT_REACH();
187         wu(0, sp->shp_own,
188            "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
189            prship(sp), conditions[findcondition(code)].desc[orig]);
190         if (!orig)
191             putship(sp->shp_uid, sp);
192         return 0;
193     }
194
195     if (sp->shp_mobil <= 0.0) {
196         wu(0, sp->shp_own,
197            "%s %s,\nbut had no mobility, 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     n = -MAX_RETREAT;
205     stopping = 0;
206     while (!stopping && n) {
207         dx = dy = 0;
208         if (sp->shp_rpath[0] == 0) {
209             stopping = 1;
210             continue;
211         }
212         if (sp->shp_mobil <= 0.0) {
213             wu(0, sp->shp_own,
214                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
215                prship(sp), conditions[findcondition(code)].desc[orig]);
216             if (!orig)
217                 putship(sp->shp_uid, sp);
218             return 0;
219         }
220         dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
221         memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
222         if (dir < 0)
223             continue;
224         if (dir == DIR_STOP)
225             stopping++;
226         else {
227             dx = diroff[dir][0];
228             dy = diroff[dir][1];
229         }
230         n++;
231
232         mcp = &mchr[(int)sp->shp_type];
233         newx = xnorm(sp->shp_x + dx);
234         newy = ynorm(sp->shp_y + dy);
235         mobcost = shp_mobcost(sp);
236
237         getsect(newx, newy, &sect);
238         if (shp_check_nav(sp, &sect) != NAVOK ||
239             (sect.sct_own
240              && relations_with(sect.sct_own, sp->shp_own) < FRIENDLY)) {
241             wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
242                prship(sp), conditions[findcondition(code)].desc[orig],
243                xyas(newx, newy, sp->shp_own));
244             if (!orig)
245                 putship(sp->shp_uid, sp);
246             return 0;
247         }
248         sp->shp_x = newx;
249         sp->shp_y = newy;
250         sp->shp_mobil -= mobcost;
251         sp->shp_mission = 0;
252         if (stopping)
253             continue;
254
255         mines = sect.sct_mines;
256         changed = 0;
257         if (sect.sct_type != SCT_WATER || mines <= 0)
258             continue;
259         if (mcp->m_flags & M_SWEEP) {
260             max = mcp->m_item[I_SHELL];
261             shells = sp->shp_item[I_SHELL];
262             for (m = 0; mines > 0 && m < 5; m++) {
263                 if (chance(0.66)) {
264                     mines--;
265                     shells = MIN(max, shells + 1);
266                     changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
267                                        'X', 0);
268                 }
269             }
270             if (sect.sct_mines != mines) {
271                 wu(0, sp->shp_own,
272                    "%s cleared %d mine%s in %s while retreating\n",
273                    prship(sp), sect.sct_mines - mines,
274                    splur(sect.sct_mines - mines),
275                    xyas(newx, newy, sp->shp_own));
276                 sect.sct_mines = mines;
277                 sp->shp_item[I_SHELL] = shells;
278                 putsect(&sect);
279             }
280             if (changed)
281                 writemap(sp->shp_own);
282         }
283         if (chance(DMINE_HITCHANCE(mines))) {
284             wu(0, sp->shp_own,
285                "%s %s,\nand hit a mine in %s while retreating!\n",
286                prship(sp), conditions[findcondition(code)].desc[orig],
287                xyas(newx, newy, sp->shp_own));
288             nreport(sp->shp_own, N_HIT_MINE, 0, 1);
289             m = MINE_DAMAGE();
290             shipdamage(sp, m);
291             mines--;
292             if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
293                 writemap(sp->shp_own);
294             sect.sct_mines = mines;
295             putsect(&sect);
296             if (!orig)
297                 putship(sp->shp_uid, sp);
298             return 0;
299         }
300     }
301
302     if (orig) {
303         wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
304            prship(sp), conditions[findcondition(code)].desc[orig],
305            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
306     } else {
307         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
308            prship(sp),
309            conditions[findcondition(code)].desc[orig],
310            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
311     }
312     if (!orig)
313         putship(sp->shp_uid, sp);
314     return 1;
315 }
316
317 static int
318 findcondition(char code)
319 {
320     int i;
321
322     for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
323     CANT_HAPPEN(!conditions[i].code);
324     return i;
325 }
326
327 int
328 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
329 {
330     if (dam <= 0)
331         return 0;
332
333     landdamage(lp, dam);
334     if (lp->lnd_rflags & RET_INJURED)
335         retreat_land(lp, 'i');
336
337     return 1;
338 }
339
340 void
341 retreat_land(struct lndstr *lp, char code)
342 {
343     struct nstr_item ni;
344     struct lndstr land;
345
346     if (lp->lnd_effic < LAND_MINEFF || CANT_HAPPEN(!lp->lnd_own))
347         return;
348
349     retreat_land1(lp, code, 1);
350     if (lp->lnd_rpath[0] == 0)
351         lp->lnd_rflags = 0;
352
353     if (lp->lnd_rflags & RET_GROUP) {
354         snxtitem_group(&ni, EF_LAND, lp->lnd_army);
355         while (nxtitem(&ni, &land))
356             if (land.lnd_own == lp->lnd_own) {
357                 if (land.lnd_uid != lp->lnd_uid) {
358                     retreat_land1(&land, code, 0);
359                     getland(land.lnd_uid, &land);
360                     if (land.lnd_rpath[0] == 0) {
361                         land.lnd_rflags = 0;
362                         putland(land.lnd_uid, &land);
363                     }
364                 }
365             }
366     }
367 }
368
369 static int
370 retreat_land1(struct lndstr *lp, char code, int orig)
371
372
373                         /* Is this the originally scared unit, or a follower */
374 {
375     struct sctstr sect;
376     int n;
377     int m;
378     int max;
379     int dir;
380     coord newx;
381     coord newy;
382     coord dx;
383     coord dy;
384     int stopping;
385     int mines;
386     int shells;
387     double mobcost;
388     struct lchrstr *lcp;
389
390     getsect(lp->lnd_x, lp->lnd_y, &sect);
391
392     if (lp->lnd_mobil <= 0.0) {
393         wu(0, lp->lnd_own,
394            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
395            prland(lp), conditions[findcondition(code)].desc[orig]);
396         if (!orig)
397             putland(lp->lnd_uid, lp);
398         return 0;
399     }
400
401     n = -MAX_RETREAT;
402     stopping = 0;
403     while (!stopping && n) {
404         dx = dy = 0;
405         if (lp->lnd_rpath[0] == 0) {
406             stopping = 1;
407             continue;
408         }
409         if (lp->lnd_mobil <= 0.0) {
410             wu(0, lp->lnd_own,
411                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
412                prland(lp), conditions[findcondition(code)].desc[orig]);
413             if (!orig)
414                 putland(lp->lnd_uid, lp);
415             return 0;
416         }
417         dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
418         memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
419         if (dir < 0)
420             continue;
421         if (dir == DIR_STOP)
422             stopping++;
423         else {
424             dx = diroff[dir][0];
425             dy = diroff[dir][1];
426         }
427         n++;
428
429         lcp = &lchr[(int)lp->lnd_type];
430         newx = xnorm(lp->lnd_x + dx);
431         newy = ynorm(lp->lnd_y + dy);
432
433         getsect(newx, newy, &sect);
434         mobcost = lnd_mobcost(lp, &sect);
435         if (mobcost < 0
436             || sect.sct_type == SCT_MOUNT
437             || sect.sct_own != lp->lnd_own) {
438             wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
439                prland(lp),
440                conditions[findcondition(code)].desc[orig],
441                xyas(newx, newy, lp->lnd_own));
442             if (!orig)
443                 putland(lp->lnd_uid, lp);
444             return 0;
445         }
446         lp->lnd_x = newx;
447         lp->lnd_y = newy;
448         lp->lnd_mobil -= mobcost;
449         lp->lnd_mission = 0;
450         if (stopping)
451             continue;
452
453         mines = SCT_LANDMINES(&sect);
454         if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
455             continue;
456         if (lcp->l_flags & L_ENGINEER) {
457             max = lcp->l_item[I_SHELL];
458             shells = lp->lnd_item[I_SHELL];
459             for (m = 0; mines > 0 && m < 5; m++) {
460                 if (chance(0.66)) {
461                     mines--;
462                     shells = MIN(max, shells + 1);
463                 }
464             }
465             sect.sct_mines = mines;
466             lp->lnd_item[I_SHELL] = shells;
467             putsect(&sect);
468         }
469         if (chance(DMINE_LHITCHANCE(mines))) {
470             wu(0, lp->lnd_own,
471                "%s %s,\nand hit a mine in %s while retreating!\n",
472                prland(lp),
473                conditions[findcondition(code)].desc[orig],
474                xyas(newx, newy, lp->lnd_own));
475             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
476             m = MINE_LDAMAGE();
477             if (lcp->l_flags & L_ENGINEER)
478                 m /= 2;
479             landdamage(lp, m);
480             mines--;
481             sect.sct_mines = mines;
482             putsect(&sect);
483             if (!orig)
484                 putland(lp->lnd_uid, lp);
485             return 0;
486         }
487     }
488
489     if (orig) {
490         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
491            prland(lp),
492            conditions[findcondition(code)].desc[orig],
493            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
494     } else {
495         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
496            prland(lp),
497            conditions[findcondition(code)].desc[orig],
498            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
499     }
500     if (!orig)
501         putland(lp->lnd_uid, lp);
502     return 1;
503 }