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