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