]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
Replace getvec() by direct, read-only item access in some cases where
[empserver] / src / lib / subs / retreat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 <string.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "nat.h"
39 #include "var.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 "options.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 boared 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, (s_char *)&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     register int n;
137     register 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
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 (check_nav(&sect)) {
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 || 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_VIEW);
246         memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
247         if (dir == -1)
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 (check_nav(&sect) != 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         if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
282             max = vl_find(V_SHELL, mcp->m_vtype,
283                           mcp->m_vamt, (int)mcp->m_nv);
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                 }
290             }
291             sect.sct_mines = mines;
292             sect.sct_item[I_SHELL] = shells;
293             putsect(&sect);
294         }
295         if (mines > 0 && !player->owner && 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             sect.sct_mines = mines;
305             putsect(&sect);
306             if (sp->shp_effic < SHIP_MINEFF)
307                 time_to_stop = 1;
308             if (!orig)
309                 putship(sp->shp_uid, sp);
310             return 0;
311         }
312         if (time_to_stop)
313             stopping = 1;
314     }
315
316     if (orig) {
317         wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
318            conditions[findcondition(code)].desc[orig],
319            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
320     } else {
321         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
322            prship(sp),
323            conditions[findcondition(code)].desc[orig],
324            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
325     }
326     if (!orig)
327         putship(sp->shp_uid, sp);
328     return 1;
329 }
330
331 #if 0
332 static int
333 check_nav(sect)
334 struct sctstr *sect;
335 {
336     switch (dchr[sect->sct_type].d_flg & 03) {
337     case NAVOK:
338         break;
339
340     case NAV_02:
341         if (sect->sct_effic < 2)
342             return CN_CONSTRUCTION;
343         break;
344     case NAV_60:
345         if (sect->sct_effic < 60)
346             return CN_CONSTRUCTION;
347         break;
348     default:
349         return CN_LANDLOCKED;
350     }
351     return CN_NAVIGABLE;
352 }
353 #endif
354
355 static int
356 findcondition(s_char code)
357 {
358     int x;
359
360     x = 0;
361     while (conditions[x].code) {
362         if (conditions[x].code == code)
363             return (x);
364         x++;
365     }
366
367     return (x);
368 }
369
370 int
371 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
372 {
373     if (dam <= 0)
374         return 0;
375
376     landdamage(lp, dam);
377     if (lp->lnd_rflags & RET_INJURED)
378         retreat_land(lp, 'i');
379
380     return 1;
381 }
382
383 void
384 retreat_land(struct lndstr *lp, s_char code)
385 {
386     struct nstr_item ni;
387     struct lndstr land;
388     s_char buf[2];
389
390     if (lp->lnd_rflags & RET_GROUP) {
391         buf[0] = lp->lnd_army;
392         buf[1] = 0;
393         snxtitem(&ni, EF_SHIP, buf);
394         while (nxtitem(&ni, (s_char *)&land))
395             if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
396                 if (land.lnd_uid == lp->lnd_uid) {
397                     retreat_land1(lp, code, 1);
398                     if (lp->lnd_rpath[0] == 0)
399                         lp->lnd_rflags = 0;
400                 } else {
401                     retreat_land1(&land, code, 0);
402                     getland(land.lnd_uid, &land);
403                     if (land.lnd_rpath[0] == 0) {
404                         land.lnd_rflags = 0;
405                         putland(land.lnd_uid, &land);
406                     }
407                 }
408             }
409     } else {
410         retreat_land1(lp, code, 1);
411         if (lp->lnd_rpath[0] == 0)
412             lp->lnd_rflags = 0;
413     }
414 }
415
416 static int
417 retreat_land1(struct lndstr *lp, s_char code, int orig)
418
419
420                         /* Is this the originally scared unit, or a follower */
421 {
422     struct sctstr sect;
423     register int n;
424     register int m;
425     int max;
426     int dir;
427     coord newx;
428     coord newy;
429     coord dx;
430     coord dy;
431     int stopping;
432     int mines;
433     int shells;
434     double mobcost;
435     struct lchrstr *lcp;
436     int time_to_stop;
437
438     lp->lnd_mission = 0;
439     if (lp->lnd_own == 0)
440         return 0;
441
442     if (isupper(code))
443         code = tolower(code);
444
445     n = 0;
446     if (lp->lnd_effic < LAND_MINEFF) {
447         wu(0, lp->lnd_own,
448            "%s %s,\nbut it died in the attack, and so 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     getsect(lp->lnd_x, lp->lnd_y, &sect);
456
457     if (lp->lnd_mobil <= 0.0) {
458         wu(0, lp->lnd_own,
459            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
460            prland(lp), conditions[findcondition(code)].desc[orig]);
461         if (!orig)
462             putland(lp->lnd_uid, lp);
463         return 0;
464     }
465
466     n = (-1 * MAX_RETREAT);
467     stopping = 0;
468     time_to_stop = 0;
469     while ((!stopping) && n) {
470         dx = dy = 0;
471         if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
472             stopping = 1;
473             continue;
474         }
475         if (lp->lnd_mobil <= 0.0) {
476             wu(0, lp->lnd_own,
477                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
478                prland(lp), conditions[findcondition(code)].desc[orig]);
479             if (!orig)
480                 putland(lp->lnd_uid, lp);
481             return 0;
482         }
483         dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
484         memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
485         if (dir == -1)
486             continue;
487         if (dir == DIR_STOP)
488             stopping++;
489         else {
490             dx = diroff[dir][0];
491             dy = diroff[dir][1];
492         }
493         n++;
494
495         lcp = &lchr[(int)lp->lnd_type];
496         newx = xnorm(lp->lnd_x + dx);
497         newy = ynorm(lp->lnd_y + dy);
498
499         getsect(newx, newy, &sect);
500         if ((sect.sct_type == SCT_WATER) ||
501             (sect.sct_type == SCT_MOUNT) ||
502             (sect.sct_type == SCT_SANCT) ||
503             (sect.sct_type == SCT_WASTE) ||
504             (sect.sct_own != lp->lnd_own)) {
505             wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
506                prland(lp),
507                conditions[findcondition(code)].desc[orig],
508                xyas(newx, newy, lp->lnd_own));
509             if (!orig)
510                 putland(lp->lnd_uid, lp);
511             return 0;
512         }
513         mobcost = lnd_mobcost(lp, &sect, MOB_ROAD);
514         lp->lnd_x = newx;
515         lp->lnd_y = newy;
516         lp->lnd_mobil -= mobcost;
517         if (stopping)
518             continue;
519
520         mines = sect.sct_mines;
521         if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
522             (sect.sct_oldown != lp->lnd_own)) {
523             max = vl_find(V_SHELL, lcp->l_vtype,
524                           lcp->l_vamt, (int)lcp->l_nv);
525             shells = lp->lnd_item[I_SHELL];
526             for (m = 0; mines > 0 && m < 5; m++) {
527                 if (chance(0.66)) {
528                     mines--;
529                     shells = min(max, shells + 1);
530                 }
531             }
532             sect.sct_mines = mines;
533             lp->lnd_item[I_SHELL] = shells;
534             putsect(&sect);
535         }
536         if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
537             chance(DMINE_HITCHANCE(mines))) {
538             wu(0, lp->lnd_own,
539                "%s %s,\nand hit a mine while retreating!\n", prland(lp),
540                conditions[findcondition(code)].desc[orig], xyas(newx, newy,
541                                                                 lp->
542                                                                 lnd_own));
543             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
544             m = MINE_LDAMAGE();
545             landdamage(lp, m);
546             mines--;
547             sect.sct_mines = mines;
548             putsect(&sect);
549             if (lp->lnd_effic < LAND_MINEFF)
550                 time_to_stop = 1;
551             if (!orig)
552                 putland(lp->lnd_uid, lp);
553             return 0;
554         }
555         if (time_to_stop)
556             stopping = 1;
557     }
558
559     if (orig) {
560         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
561            prland(lp),
562            conditions[findcondition(code)].desc[orig],
563            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
564     } else {
565         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
566            prland(lp),
567            conditions[findcondition(code)].desc[orig],
568            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
569     }
570     if (!orig)
571         putland(lp->lnd_uid, lp);
572     return 1;
573 }