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