]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
75c6d984c4e8aeab146c1381d35e2c81e92fe93c
[empserver] / src / lib / subs / retreat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  retreat.c: Retreat subroutines
29  *
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  */
33
34 #include <config.h>
35
36 #include "damage.h"
37 #include "file.h"
38 #include "land.h"
39 #include "map.h"
40 #include "misc.h"
41 #include "nat.h"
42 #include "news.h"
43 #include "nsc.h"
44 #include "optlist.h"
45 #include "path.h"
46 #include "player.h"
47 #include "prototypes.h"
48 #include "retreat.h"
49 #include "sect.h"
50 #include "ship.h"
51 #include "xy.h"
52
53 static int findcondition(char);
54 static int retreat_land1(struct lndstr *, char, int);
55 static int retreat_ship1(struct shpstr *, char, int);
56
57 struct ccode {
58     char code;
59     char *desc[2];
60 };
61
62 static struct ccode conditions[] = {
63     { 'i', { "retreated with a damaged friend",
64              "was damaged" } },
65     { 't', { "retreated with a torpedoed ship",
66              "was hit by a torpedo" } },
67     { 's', { "retreated with a ship scared by sonar",
68              "detected a sonar ping" } },
69     { 'h', { "retreated with a helpless ship",
70              "was fired upon with no one able to defend it" } },
71     { 'b', { "retreated with a bombed friend",
72              "was bombed" } },
73     { 'd', { "retreated with a depth-charged ship",
74              "was depth-charged" } },
75     { 'u', { "retreated with a boarded ship", "was boarded" } },
76     { 0,   { "panicked", "panicked"} }
77 };
78
79 int
80 check_retreat_and_do_shipdamage(struct shpstr *sp, int dam)
81 {
82     if (dam <= 0)
83         return 0;
84
85     shipdamage(sp, dam);
86     if (sp->shp_rflags & RET_INJURED)
87         retreat_ship(sp, 'i');
88
89     return 1;
90 }
91
92 void
93 retreat_ship(struct shpstr *sp, char code)
94 {
95     struct nstr_item ni;
96     struct shpstr ship;
97
98     if (sp->shp_rflags & RET_GROUP) {
99         snxtitem_group(&ni, EF_SHIP, sp->shp_fleet);
100         while (nxtitem(&ni, &ship))
101             if (ship.shp_own == sp->shp_own) {
102                 if (ship.shp_uid == sp->shp_uid) {
103                     retreat_ship1(sp, code, 1);
104                     if (sp->shp_rpath[0] == 0)
105                         sp->shp_rflags = 0;
106                 } else {
107                     retreat_ship1(&ship, code, 0);
108                     getship(ship.shp_uid, &ship);
109                     if (ship.shp_rpath[0] == 0) {
110                         ship.shp_rflags = 0;
111                         putship(ship.shp_uid, &ship);
112                     }
113                 }
114             }
115     } else {
116         retreat_ship1(sp, code, 1);
117         if (sp->shp_rpath[0] == 0)
118             sp->shp_rflags = 0;
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     sp->shp_mission = 0;
145     if (sp->shp_own == 0)
146         return 0;
147
148     if (sp->shp_effic < SHIP_MINEFF) {
149         wu(0, sp->shp_own,
150            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
151            prship(sp), conditions[findcondition(code)].desc[orig]);
152         if (!orig)
153             putship(sp->shp_uid, sp);
154         return 0;
155     }
156
157     if (opt_SAIL) {
158         /* can't retreat a ship that's sailin, bad things happend */
159         if (*sp->shp_path) {
160             wu(0, sp->shp_own,
161                "%s %s,\nbut had sailing orders, and couldn't retreat!\n",
162                prship(sp), conditions[findcondition(code)].desc[orig]);
163             if (!orig)
164                 putship(sp->shp_uid, sp);
165             return 0;
166         }
167     }
168     /* check crew - uws don't count */
169     if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
170         wu(0, sp->shp_own,
171            "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
172            conditions[findcondition(code)].desc[orig]);
173         if (!orig)
174             putship(sp->shp_uid, sp);
175         return 0;
176     }
177
178     getsect(sp->shp_x, sp->shp_y, &sect);
179     switch (shp_check_nav(&sect, sp)) {
180     case CN_CONSTRUCTION:
181         wu(0, sp->shp_own,
182            "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
183            prship(sp), conditions[findcondition(code)].desc[orig]);
184         if (!orig)
185             putship(sp->shp_uid, sp);
186         return 0;
187     case CN_LANDLOCKED:
188         wu(0, sp->shp_own,
189            "%s %s,\nbut was landlocked, and couldn't retreat!\n",
190            prship(sp), conditions[findcondition(code)].desc[orig]);
191         if (!orig)
192             putship(sp->shp_uid, sp);
193         return 0;
194         /*NOTREACHED*/
195     case CN_NAVIGABLE:
196         break;
197     case CN_ERROR:
198     default:
199         wu(0, sp->shp_own,
200            "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
201            prship(sp), conditions[findcondition(code)].desc[orig]);
202         if (!orig)
203             putship(sp->shp_uid, sp);
204         return 0;
205         /*NOTREACHED*/
206     }
207
208     if (sp->shp_mobil <= 0.0) {
209         wu(0, sp->shp_own,
210            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
211            prship(sp), conditions[findcondition(code)].desc[orig]);
212         if (!orig)
213             putship(sp->shp_uid, sp);
214         return 0;
215     }
216
217     n = -MAX_RETREAT;
218     stopping = 0;
219     while (!stopping && n) {
220         dx = dy = 0;
221         if (sp->shp_rpath[0] == 0) {
222             stopping = 1;
223             continue;
224         }
225         if (sp->shp_mobil <= 0.0) {
226             wu(0, sp->shp_own,
227                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
228                prship(sp), conditions[findcondition(code)].desc[orig]);
229             if (!orig)
230                 putship(sp->shp_uid, sp);
231             return 0;
232         }
233         dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_LAST);
234         memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
235         if (dir < 0)
236             continue;
237         if (dir == DIR_STOP)
238             stopping++;
239         else {
240             dx = diroff[dir][0];
241             dy = diroff[dir][1];
242         }
243         n++;
244
245         mcp = &mchr[(int)sp->shp_type];
246         newx = xnorm(sp->shp_x + dx);
247         newy = ynorm(sp->shp_y + dy);
248         mobcost = shp_mobcost(sp);
249
250         getsect(newx, newy, &sect);
251         if (shp_check_nav(&sect, sp) != CN_NAVIGABLE ||
252             (sect.sct_own && sect.sct_own != sp->shp_own &&
253              getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
254             wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
255                prship(sp), conditions[findcondition(code)].desc[orig],
256                xyas(newx, newy, sp->shp_own));
257             if (!orig)
258                 putship(sp->shp_uid, sp);
259             return 0;
260         }
261         sp->shp_x = newx;
262         sp->shp_y = newy;
263         sp->shp_mobil -= mobcost;
264         if (stopping)
265             continue;
266
267         mines = sect.sct_mines;
268         changed = 0;
269         if (sect.sct_type != SCT_WATER || mines <= 0)
270             continue;
271         if (mcp->m_flags & M_SWEEP) {
272             max = mcp->m_item[I_SHELL];
273             shells = sp->shp_item[I_SHELL];
274             for (m = 0; mines > 0 && m < 5; m++) {
275                 if (chance(0.66)) {
276                     mines--;
277                     shells = MIN(max, shells + 1);
278                     changed |= map_set(sp->shp_own, sp->shp_x, sp->shp_y,
279                                        'X', 0);
280                 }
281             }
282             if (sect.sct_mines != mines) {
283                 wu(0, sp->shp_own,
284                    "%s cleared %d mine%s in %s while retreating\n",
285                    prship(sp), sect.sct_mines - mines,
286                    splur(sect.sct_mines - mines),
287                    xyas(newx, newy, sp->shp_own));
288                 sect.sct_mines = mines;
289                 sp->shp_item[I_SHELL] = shells;
290                 putsect(&sect);
291             }
292             if (changed)
293                 writemap(sp->shp_own);
294         }
295         if (chance(DMINE_HITCHANCE(mines))) {
296             wu(0, sp->shp_own,
297                "%s %s,\nand hit a mine in %s while retreating!\n",
298                prship(sp), conditions[findcondition(code)].desc[orig],
299                xyas(newx, newy, sp->shp_own));
300             nreport(sp->shp_own, N_HIT_MINE, 0, 1);
301             m = MINE_DAMAGE();
302             shipdamage(sp, m);
303             mines--;
304             if (map_set(sp->shp_own, sp->shp_x, sp->shp_y, 'X', 0))
305                 writemap(sp->shp_own);
306             sect.sct_mines = mines;
307             putsect(&sect);
308             if (!orig)
309                 putship(sp->shp_uid, sp);
310             return 0;
311         }
312     }
313
314     if (orig) {
315         wu(0, sp->shp_own, "%s %s, and retreated to %s\n",
316            prship(sp), conditions[findcondition(code)].desc[orig],
317            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
318     } else {
319         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
320            prship(sp),
321            conditions[findcondition(code)].desc[orig],
322            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
323     }
324     if (!orig)
325         putship(sp->shp_uid, sp);
326     return 1;
327 }
328
329 static int
330 findcondition(char code)
331 {
332     int i;
333
334     for (i = 0; conditions[i].code && conditions[i].code != code; i++) ;
335     CANT_HAPPEN(!conditions[i].code);
336     return i;
337 }
338
339 int
340 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
341 {
342     if (dam <= 0)
343         return 0;
344
345     landdamage(lp, dam);
346     if (lp->lnd_rflags & RET_INJURED)
347         retreat_land(lp, 'i');
348
349     return 1;
350 }
351
352 void
353 retreat_land(struct lndstr *lp, char code)
354 {
355     struct nstr_item ni;
356     struct lndstr land;
357
358     if (lp->lnd_rflags & RET_GROUP) {
359         snxtitem_group(&ni, EF_SHIP, lp->lnd_army);
360         while (nxtitem(&ni, &land))
361             if (land.lnd_own == lp->lnd_own) {
362                 if (land.lnd_uid == lp->lnd_uid) {
363                     retreat_land1(lp, code, 1);
364                     if (lp->lnd_rpath[0] == 0)
365                         lp->lnd_rflags = 0;
366                 } else {
367                     retreat_land1(&land, code, 0);
368                     getland(land.lnd_uid, &land);
369                     if (land.lnd_rpath[0] == 0) {
370                         land.lnd_rflags = 0;
371                         putland(land.lnd_uid, &land);
372                     }
373                 }
374             }
375     } else {
376         retreat_land1(lp, code, 1);
377         if (lp->lnd_rpath[0] == 0)
378             lp->lnd_rflags = 0;
379     }
380 }
381
382 static int
383 retreat_land1(struct lndstr *lp, char code, int orig)
384
385
386                         /* Is this the originally scared unit, or a follower */
387 {
388     struct sctstr sect;
389     int n;
390     int m;
391     int max;
392     int dir;
393     coord newx;
394     coord newy;
395     coord dx;
396     coord dy;
397     int stopping;
398     int mines;
399     int shells;
400     double mobcost;
401     struct lchrstr *lcp;
402
403     lp->lnd_mission = 0;
404     if (lp->lnd_own == 0)
405         return 0;
406
407     if (lp->lnd_effic < LAND_MINEFF) {
408         wu(0, lp->lnd_own,
409            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
410            prland(lp), conditions[findcondition(code)].desc[orig]);
411         if (!orig)
412             putland(lp->lnd_uid, lp);
413         return 0;
414     }
415
416     getsect(lp->lnd_x, lp->lnd_y, &sect);
417
418     if (lp->lnd_mobil <= 0.0) {
419         wu(0, lp->lnd_own,
420            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
421            prland(lp), conditions[findcondition(code)].desc[orig]);
422         if (!orig)
423             putland(lp->lnd_uid, lp);
424         return 0;
425     }
426
427     n = -MAX_RETREAT;
428     stopping = 0;
429     while (!stopping && n) {
430         dx = dy = 0;
431         if (lp->lnd_rpath[0] == 0) {
432             stopping = 1;
433             continue;
434         }
435         if (lp->lnd_mobil <= 0.0) {
436             wu(0, lp->lnd_own,
437                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
438                prland(lp), conditions[findcondition(code)].desc[orig]);
439             if (!orig)
440                 putland(lp->lnd_uid, lp);
441             return 0;
442         }
443         dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_LAST);
444         memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
445         if (dir < 0)
446             continue;
447         if (dir == DIR_STOP)
448             stopping++;
449         else {
450             dx = diroff[dir][0];
451             dy = diroff[dir][1];
452         }
453         n++;
454
455         lcp = &lchr[(int)lp->lnd_type];
456         newx = xnorm(lp->lnd_x + dx);
457         newy = ynorm(lp->lnd_y + dy);
458
459         getsect(newx, newy, &sect);
460         mobcost = lnd_mobcost(lp, &sect);
461         if (mobcost < 0
462             || sect.sct_type == SCT_MOUNT
463             || sect.sct_own != lp->lnd_own) {
464             wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
465                prland(lp),
466                conditions[findcondition(code)].desc[orig],
467                xyas(newx, newy, lp->lnd_own));
468             if (!orig)
469                 putland(lp->lnd_uid, lp);
470             return 0;
471         }
472         lp->lnd_x = newx;
473         lp->lnd_y = newy;
474         lp->lnd_mobil -= mobcost;
475         if (stopping)
476             continue;
477
478         mines = SCT_LANDMINES(&sect);
479         if (mines <= 0 || sect.sct_oldown == lp->lnd_own)
480             continue;
481         if (lcp->l_flags & L_ENGINEER) {
482             max = lcp->l_item[I_SHELL];
483             shells = lp->lnd_item[I_SHELL];
484             for (m = 0; mines > 0 && m < 5; m++) {
485                 if (chance(0.66)) {
486                     mines--;
487                     shells = MIN(max, shells + 1);
488                 }
489             }
490             sect.sct_mines = mines;
491             lp->lnd_item[I_SHELL] = shells;
492             putsect(&sect);
493         }
494         if (chance(DMINE_LHITCHANCE(mines))) {
495             wu(0, lp->lnd_own,
496                "%s %s,\nand hit a mine in %s while retreating!\n",
497                prland(lp),
498                conditions[findcondition(code)].desc[orig],
499                xyas(newx, newy, lp->lnd_own));
500             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
501             m = MINE_LDAMAGE();
502             if (lcp->l_flags & L_ENGINEER)
503                 m /= 2;
504             landdamage(lp, m);
505             mines--;
506             sect.sct_mines = mines;
507             putsect(&sect);
508             if (!orig)
509                 putland(lp->lnd_uid, lp);
510             return 0;
511         }
512     }
513
514     if (orig) {
515         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
516            prland(lp),
517            conditions[findcondition(code)].desc[orig],
518            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
519     } else {
520         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
521            prland(lp),
522            conditions[findcondition(code)].desc[orig],
523            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
524     }
525     if (!orig)
526         putland(lp->lnd_uid, lp);
527     return 1;
528 }