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