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