]> git.pond.sub.org Git - empserver/blob - src/lib/subs/retreat.c
Remove some unused junk.
[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 vec[I_MAX + 1];
150     int time_to_stop;
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     getvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
182     if (vec[I_MILIT] == 0 && vec[I_CIVIL] == 0) {
183         wu(0, sp->shp_own,
184            "%s %s,\nbut had no crew, and couldn't retreat!\n", prship(sp),
185            conditions[findcondition(code)].desc[orig]);
186         if (!orig)
187             putship(sp->shp_uid, sp);
188         return 0;
189     }
190
191     getsect(sp->shp_x, sp->shp_y, &sect);
192     switch (check_nav(&sect)) {
193     case CN_CONSTRUCTION:
194         wu(0, sp->shp_own,
195            "%s %s,\nbut was caught in a construction zone, and couldn't retreat!\n",
196            prship(sp), conditions[findcondition(code)].desc[orig]);
197         if (!orig)
198             putship(sp->shp_uid, sp);
199         return 0;
200     case CN_LANDLOCKED:
201         wu(0, sp->shp_own,
202            "%s %s,\nbut was landlocked, 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     case CN_NAVIGABLE:
209         break;
210     case CN_ERROR:
211     default:
212         wu(0, sp->shp_own,
213            "%s %s,\nbut was subject to an empire error, and couldn't retreat!\n",
214            prship(sp), conditions[findcondition(code)].desc[orig]);
215         if (!orig)
216             putship(sp->shp_uid, sp);
217         return 0;
218         /*NOTREACHED*/
219     }
220
221     if (sp->shp_mobil <= 0.0) {
222         wu(0, sp->shp_own,
223            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
224            prship(sp), conditions[findcondition(code)].desc[orig]);
225         if (!orig)
226             putship(sp->shp_uid, sp);
227         return 0;
228     }
229
230     n = (-1 * MAX_RETREAT);
231     stopping = 0;
232     time_to_stop = 0;
233     while ((!stopping) && n) {
234         dx = dy = 0;
235         if (sp->shp_rpath[0] == 0 || sp->shp_rpath[0] == 0) {
236             stopping = 1;
237             continue;
238         }
239         if (sp->shp_mobil <= 0.0) {
240             wu(0, sp->shp_own,
241                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
242                prship(sp), conditions[findcondition(code)].desc[orig]);
243             if (!orig)
244                 putship(sp->shp_uid, sp);
245             return 0;
246         }
247         dir = chkdir(sp->shp_rpath[0], DIR_STOP, DIR_VIEW);
248         memmove(sp->shp_rpath, sp->shp_rpath+1, sizeof(sp->shp_rpath) - 1);
249         if (dir == -1)
250             continue;
251         if (dir == DIR_STOP)
252             stopping++;
253         else {
254             dx = diroff[dir][0];
255             dy = diroff[dir][1];
256         }
257         n++;
258
259         mcp = &mchr[(int)sp->shp_type];
260         newx = xnorm(sp->shp_x + dx);
261         newy = ynorm(sp->shp_y + dy);
262         mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
263         mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
264
265         getsect(newx, newy, &sect);
266         if (check_nav(&sect) != CN_NAVIGABLE ||
267             (sect.sct_own && !player->owner &&
268              getrel(getnatp(sect.sct_own), sp->shp_own) < FRIENDLY)) {
269             wu(0, sp->shp_own, "%s %s,\nbut could not retreat to %s!\n",
270                prship(sp), conditions[findcondition(code)].desc[orig],
271                xyas(newx, newy, sp->shp_own));
272             if (!orig)
273                 putship(sp->shp_uid, sp);
274             return 0;
275         }
276         sp->shp_x = newx;
277         sp->shp_y = newy;
278         sp->shp_mobil -= mobcost;
279         if (stopping)
280             continue;
281
282         mines = getvar(V_MINE, (s_char *)&sect, EF_SECTOR);
283         if ((mcp->m_flags & M_SWEEP) && mines > 0 && !player->owner) {
284             max = vl_find(V_SHELL, mcp->m_vtype,
285                           mcp->m_vamt, (int)mcp->m_nv);
286             shells = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
287             for (m = 0; mines > 0 && m < 5; m++) {
288                 if (chance(0.66)) {
289                     mines--;
290                     shells = min(max, shells + 1);
291                 }
292             }
293             putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
294             putvar(V_SHELL, shells, (s_char *)sp, EF_SHIP);
295             putsect(&sect);
296         }
297         if (mines > 0 && !player->owner && chance(DMINE_HITCHANCE(mines))) {
298             wu(0, sp->shp_own,
299                "%s %s,\nand hit a mine in %s while retreating!\n",
300                prship(sp), conditions[findcondition(code)].desc[orig],
301                xyas(newx, newy, sp->shp_own));
302             nreport(sp->shp_own, N_HIT_MINE, 0, 1);
303             m = MINE_DAMAGE();
304             shipdamage(sp, m);
305             mines--;
306             putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
307             putsect(&sect);
308             if (sp->shp_effic < SHIP_MINEFF)
309                 time_to_stop = 1;
310             if (!orig)
311                 putship(sp->shp_uid, sp);
312             return 0;
313         }
314         if (time_to_stop)
315             stopping = 1;
316     }
317
318     if (orig) {
319         wu(0, sp->shp_own, "%s %s, and retreated to %s\n", prship(sp),
320            conditions[findcondition(code)].desc[orig],
321            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
322     } else {
323         wu(0, sp->shp_own, "%s %s, and ended up at %s\n",
324            prship(sp),
325            conditions[findcondition(code)].desc[orig],
326            xyas(sp->shp_x, sp->shp_y, sp->shp_own));
327     }
328     if (!orig)
329         putship(sp->shp_uid, sp);
330     return 1;
331 }
332
333 #if 0
334 static int
335 check_nav(sect)
336 struct sctstr *sect;
337 {
338     switch (dchr[sect->sct_type].d_flg & 03) {
339     case NAVOK:
340         break;
341
342     case NAV_02:
343         if (sect->sct_effic < 2)
344             return CN_CONSTRUCTION;
345         break;
346     case NAV_60:
347         if (sect->sct_effic < 60)
348             return CN_CONSTRUCTION;
349         break;
350     default:
351         return CN_LANDLOCKED;
352     }
353     return CN_NAVIGABLE;
354 }
355 #endif
356
357 static int
358 findcondition(s_char code)
359 {
360     int x;
361
362     x = 0;
363     while (conditions[x].code) {
364         if (conditions[x].code == code)
365             return (x);
366         x++;
367     }
368
369     return (x);
370 }
371
372 int
373 check_retreat_and_do_landdamage(struct lndstr *lp, int dam)
374 {
375     if (dam <= 0)
376         return 0;
377
378     landdamage(lp, dam);
379     if (lp->lnd_rflags & RET_INJURED)
380         retreat_land(lp, 'i');
381
382     return 1;
383 }
384
385 void
386 retreat_land(struct lndstr *lp, s_char code)
387 {
388     struct nstr_item ni;
389     struct lndstr land;
390     s_char buf[2];
391
392     if (lp->lnd_rflags & RET_GROUP) {
393         buf[0] = lp->lnd_army;
394         buf[1] = 0;
395         snxtitem(&ni, EF_SHIP, buf);
396         while (nxtitem(&ni, (s_char *)&land))
397             if ((land.lnd_army == buf[0]) && (land.lnd_own == lp->lnd_own)) {
398                 if (land.lnd_uid == lp->lnd_uid) {
399                     retreat_land1(lp, code, 1);
400                     if (lp->lnd_rpath[0] == 0)
401                         lp->lnd_rflags = 0;
402                 } else {
403                     retreat_land1(&land, code, 0);
404                     getland(land.lnd_uid, &land);
405                     if (land.lnd_rpath[0] == 0) {
406                         land.lnd_rflags = 0;
407                         putland(land.lnd_uid, &land);
408                     }
409                 }
410             }
411     } else {
412         retreat_land1(lp, code, 1);
413         if (lp->lnd_rpath[0] == 0)
414             lp->lnd_rflags = 0;
415     }
416 }
417
418 static int
419 retreat_land1(struct lndstr *lp, s_char code, int orig)
420
421
422                         /* Is this the originally scared unit, or a follower */
423 {
424     struct sctstr sect;
425     register int n;
426     register int m;
427     int max;
428     int dir;
429     coord newx;
430     coord newy;
431     coord dx;
432     coord dy;
433     int stopping;
434     int mines;
435     int shells;
436     double mobcost;
437     struct lchrstr *lcp;
438     int time_to_stop;
439
440     lp->lnd_mission = 0;
441     if (lp->lnd_own == 0)
442         return 0;
443
444     if (isupper(code))
445         code = tolower(code);
446
447     n = 0;
448     if (lp->lnd_effic < LAND_MINEFF) {
449         wu(0, lp->lnd_own,
450            "%s %s,\nbut it died in the attack, and so couldn't retreat!\n",
451            prland(lp), conditions[findcondition(code)].desc[orig]);
452         if (!orig)
453             putland(lp->lnd_uid, lp);
454         return 0;
455     }
456
457     getsect(lp->lnd_x, lp->lnd_y, &sect);
458
459     if (lp->lnd_mobil <= 0.0) {
460         wu(0, lp->lnd_own,
461            "%s %s,\nbut had no mobility, and couldn't retreat!\n",
462            prland(lp), conditions[findcondition(code)].desc[orig]);
463         if (!orig)
464             putland(lp->lnd_uid, lp);
465         return 0;
466     }
467
468     n = (-1 * MAX_RETREAT);
469     stopping = 0;
470     time_to_stop = 0;
471     while ((!stopping) && n) {
472         dx = dy = 0;
473         if (lp->lnd_rpath[0] == 0 || lp->lnd_rpath[0] == 0) {
474             stopping = 1;
475             continue;
476         }
477         if (lp->lnd_mobil <= 0.0) {
478             wu(0, lp->lnd_own,
479                "%s %s,\nbut ran out of mobility, and couldn't retreat fully!\n",
480                prland(lp), conditions[findcondition(code)].desc[orig]);
481             if (!orig)
482                 putland(lp->lnd_uid, lp);
483             return 0;
484         }
485         dir = chkdir(lp->lnd_rpath[0], DIR_STOP, DIR_VIEW);
486         memmove(lp->lnd_rpath, lp->lnd_rpath+1, sizeof(lp->lnd_rpath) - 1);
487         if (dir == -1)
488             continue;
489         if (dir == DIR_STOP)
490             stopping++;
491         else {
492             dx = diroff[dir][0];
493             dy = diroff[dir][1];
494         }
495         n++;
496
497         lcp = &lchr[(int)lp->lnd_type];
498         newx = xnorm(lp->lnd_x + dx);
499         newy = ynorm(lp->lnd_y + dy);
500
501         getsect(newx, newy, &sect);
502         if ((sect.sct_type == SCT_WATER) ||
503             (sect.sct_type == SCT_MOUNT) ||
504             (sect.sct_type == SCT_SANCT) ||
505             (sect.sct_type == SCT_WASTE) ||
506             (sect.sct_own != lp->lnd_own)) {
507             wu(0, lp->lnd_own, "%s %s,\nbut could not retreat to %s!\n",
508                prland(lp),
509                conditions[findcondition(code)].desc[orig],
510                xyas(newx, newy, lp->lnd_own));
511             if (!orig)
512                 putland(lp->lnd_uid, lp);
513             return 0;
514         }
515         mobcost = lnd_mobcost(lp, &sect, MOB_ROAD);
516         lp->lnd_x = newx;
517         lp->lnd_y = newy;
518         lp->lnd_mobil -= mobcost;
519         if (stopping)
520             continue;
521
522         mines = getvar(V_MINE, (s_char *)&sect, EF_SECTOR);
523         if ((lcp->l_flags & L_ENGINEER) && mines > 0 &&
524             (sect.sct_oldown != lp->lnd_own)) {
525             max = vl_find(V_SHELL, lcp->l_vtype,
526                           lcp->l_vamt, (int)lcp->l_nv);
527             shells = getvar(V_SHELL, (s_char *)lp, EF_LAND);
528             for (m = 0; mines > 0 && m < 5; m++) {
529                 if (chance(0.66)) {
530                     mines--;
531                     shells = min(max, shells + 1);
532                 }
533             }
534             putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
535             putvar(V_SHELL, shells, (s_char *)lp, EF_LAND);
536             putsect(&sect);
537         }
538         if (mines > 0 && (sect.sct_oldown != lp->lnd_own) &&
539             chance(DMINE_HITCHANCE(mines))) {
540             wu(0, lp->lnd_own,
541                "%s %s,\nand hit a mine while retreating!\n", prland(lp),
542                conditions[findcondition(code)].desc[orig], xyas(newx, newy,
543                                                                 lp->
544                                                                 lnd_own));
545             nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
546             m = MINE_LDAMAGE();
547             landdamage(lp, m);
548             mines--;
549             putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
550             putsect(&sect);
551             if (lp->lnd_effic < LAND_MINEFF)
552                 time_to_stop = 1;
553             if (!orig)
554                 putland(lp->lnd_uid, lp);
555             return 0;
556         }
557         if (time_to_stop)
558             stopping = 1;
559     }
560
561     if (orig) {
562         wu(0, lp->lnd_own, "%s %s, and retreated to %s\n",
563            prland(lp),
564            conditions[findcondition(code)].desc[orig],
565            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
566     } else {
567         wu(0, lp->lnd_own, "%s %s, and ended up at %s\n",
568            prland(lp),
569            conditions[findcondition(code)].desc[orig],
570            xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
571     }
572     if (!orig)
573         putland(lp->lnd_uid, lp);
574     return 1;
575 }