]> git.pond.sub.org Git - empserver/blob - src/lib/subs/shpsub.c
subs: Make shp_check_nav() more like lnd_check_mar()
[empserver] / src / lib / subs / shpsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  shpsub.c: Ship subroutine stuff
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Steve McClure, 1996-2000
32  *     Markus Armbruster, 2006-2014
33  */
34
35 #include <config.h>
36
37 #include <stdlib.h>
38 #include "chance.h"
39 #include "damage.h"
40 #include "empobj.h"
41 #include "file.h"
42 #include "map.h"
43 #include "misc.h"
44 #include "mission.h"
45 #include "news.h"
46 #include "nsc.h"
47 #include "optlist.h"
48 #include "path.h"
49 #include "player.h"
50 #include "prototypes.h"
51 #include "queue.h"
52 #include "server.h"
53 #include "unit.h"
54 #include "xy.h"
55
56 static void shp_nav_put_one(struct ulist *);
57 static int shp_check_one_mines(struct ulist *);
58 static int shp_hit_mine(struct shpstr *);
59 static void shp_stays(natid, char *, struct ulist *);
60
61 void
62 shp_sel(struct nstr_item *ni, struct emp_qelem *list)
63 {
64     struct shpstr ship;
65
66     emp_initque(list);
67     while (nxtitem(ni, &ship)) {
68         /*
69          * It would be nice to let deities navigate foreign ships, but
70          * much of the code assumes that only the ship's owner can
71          * navigate it.
72          */
73         if (!ship.shp_own || ship.shp_own != player->cnum)
74             continue;
75         if (opt_MARKET) {
76             if (ontradingblock(EF_SHIP, &ship)) {
77                 pr("ship #%d inelligible - it's for sale.\n",
78                    ship.shp_uid);
79                 continue;
80             }
81         }
82         ship.shp_mission = 0;
83         ship.shp_rflags = 0;
84         memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
85         putship(ship.shp_uid, &ship);
86         shp_insque(&ship, list);
87     }
88 }
89
90 /*
91  * Append SP to LIST.
92  * Return the new list link.
93  */
94 struct ulist *
95 shp_insque(struct shpstr *sp, struct emp_qelem *list)
96 {
97     struct ulist *mlp = malloc(sizeof(struct ulist));
98
99     mlp->chrp = (struct empobj_chr *)&mchr[sp->shp_type];
100     mlp->unit.ship = *sp;
101     mlp->mobil = sp->shp_mobil;
102     emp_insque(&mlp->queue, list);
103     return mlp;
104 }
105
106 /* This function assumes that the list was created by shp_sel */
107 void
108 shp_nav(struct emp_qelem *list, double *minmobp, double *maxmobp,
109         int *togetherp, natid actor)
110 {
111     struct emp_qelem *qp;
112     struct emp_qelem *next;
113     struct ulist *mlp;
114     struct shpstr *sp;
115     struct sctstr sect;
116     coord allx;
117     coord ally;
118     int first = 1;
119
120     *minmobp = 9876.0;
121     *maxmobp = -9876.0;
122     *togetherp = 1;
123     for (qp = list->q_back; qp != list; qp = next) {
124         next = qp->q_back;
125         mlp = (struct ulist *)qp;
126         sp = &mlp->unit.ship;
127         getship(sp->shp_uid, sp);
128         if (sp->shp_own != actor) {
129             mpr(actor, "%s was sunk at %s\n",
130                 prship(sp), xyas(sp->shp_x, sp->shp_y, actor));
131             emp_remque(&mlp->queue);
132             free(mlp);
133             continue;
134         }
135         /* check crew - uws don't count */
136         if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
137             shp_stays(actor, "is crewless", mlp);
138             continue;
139         }
140         if (!getsect(sp->shp_x, sp->shp_y, &sect)) {
141             shp_stays(actor, "was sucked into the sky by a strange looking spaceship", mlp);    /* heh -KHS */
142             continue;
143         }
144         switch (shp_check_nav(sp, &sect)) {
145         case SHP_STUCK_NOT:
146             break;
147         case SHP_STUCK_CONSTRUCTION:
148             shp_stays(actor, "is caught in a construction zone", mlp);
149             continue;
150         default:
151             CANT_REACH();
152             /* fall through */
153         case SHP_STUCK_CANAL:
154         case SHP_STUCK_IMPASSABLE:
155             shp_stays(actor, "is landlocked", mlp);
156             continue;
157         }
158         if (first) {
159             allx = sp->shp_x;
160             ally = sp->shp_y;
161             first = 0;
162         }
163         if (sp->shp_x != allx || sp->shp_y != ally)
164             *togetherp = 0;
165         if (sp->shp_mobil + 1 < (int)mlp->mobil) {
166             mlp->mobil = sp->shp_mobil;
167         }
168         if (mlp->mobil < *minmobp)
169             *minmobp = mlp->mobil;
170         if (mlp->mobil > *maxmobp)
171             *maxmobp = mlp->mobil;
172     }
173 }
174
175 static void
176 shp_nav_put(struct emp_qelem *list, natid actor)
177 {
178     struct emp_qelem *qp, *next;
179     struct ulist *mlp;
180     struct shpstr *sp;
181
182     for (qp = list->q_back; qp != list; qp = next) {
183         next = qp->q_back;
184         mlp = (struct ulist *)qp;
185         sp = &mlp->unit.ship;
186         mpr(actor, "%s stopped at %s\n",
187             prship(sp), xyas(sp->shp_x, sp->shp_y, actor));
188         shp_nav_put_one(mlp);
189     }
190 }
191
192 static void
193 shp_nav_put_one(struct ulist *mlp)
194 {
195     mlp->unit.ship.shp_mobil = (int)mlp->mobil;
196     putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
197     emp_remque(&mlp->queue);
198     free(mlp);
199 }
200
201 int
202 shp_sweep(struct emp_qelem *ship_list, int verbose, int takemob, natid actor)
203 {
204     struct emp_qelem *qp;
205     struct emp_qelem *next;
206     struct ulist *mlp;
207     struct sctstr sect;
208     int mines, m, max, shells;
209     int changed = 0;
210     int stopping = 0;
211
212     for (qp = ship_list->q_back; qp != ship_list; qp = next) {
213         next = qp->q_back;
214         mlp = (struct ulist *)qp;
215         if (!(((struct mchrstr *)mlp->chrp)->m_flags & M_SWEEP)) {
216             if (verbose)
217                 mpr(actor, "%s doesn't have minesweeping capability!\n",
218                     prship(&mlp->unit.ship));
219             continue;
220         }
221         if (takemob && mlp->mobil <= 0.0) {
222             if (verbose)
223                 mpr(actor, "%s is out of mobility!\n",
224                     prship(&mlp->unit.ship));
225             continue;
226         }
227         getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, &sect);
228         if (sect.sct_type != SCT_WATER) {
229             if (verbose)
230                 mpr(actor, "%s is not at sea.  No mines there!\n",
231                     prship(&mlp->unit.ship));
232             continue;
233         }
234         if (takemob) {
235             mlp->mobil -= shp_mobcost(&mlp->unit.ship);
236             mlp->unit.ship.shp_mobil = (int)mlp->mobil;
237         }
238         putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
239         if (!(mines = sect.sct_mines))
240             continue;
241         max = ((struct mchrstr *)mlp->chrp)->m_item[I_SHELL];
242         shells = mlp->unit.ship.shp_item[I_SHELL];
243         for (m = 0; mines > 0 && m < 5; m++) {
244             if (chance(0.66)) {
245                 mpr(actor, "Sweep...\n");
246                 mines--;
247                 shells = MIN(max, shells + 1);
248                 changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
249             }
250         }
251         sect.sct_mines = mines;
252         mlp->unit.ship.shp_item[I_SHELL] = shells;
253         putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
254         putsect(&sect);
255         if (shp_check_one_mines(mlp)) {
256             stopping = 1;
257             emp_remque(qp);
258             free(qp);
259         }
260     }
261     if (changed)
262         writemap(actor);
263     return stopping;
264 }
265
266 static int
267 shp_check_one_mines(struct ulist *mlp)
268 {
269     struct sctstr sect;
270     int actor;
271
272     getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, &sect);
273     if (sect.sct_type != SCT_WATER)
274         return 0;
275     if (!sect.sct_mines)
276         return 0;
277     if (chance(DMINE_HITCHANCE(sect.sct_mines))) {
278         actor = mlp->unit.ship.shp_own;
279         shp_hit_mine(&mlp->unit.ship);
280         sect.sct_mines--;
281         if (map_set(actor, sect.sct_x, sect.sct_y, 'X', 0))
282             writemap(actor);
283         putsect(&sect);
284         putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
285         if (!mlp->unit.ship.shp_own)
286             return 1;
287     }
288     return 0;
289 }
290
291 static int
292 shp_check_mines(struct emp_qelem *ship_list)
293 {
294     struct emp_qelem *qp;
295     struct emp_qelem *next;
296     struct ulist *mlp;
297     int stopping = 0;
298
299     for (qp = ship_list->q_back; qp != ship_list; qp = next) {
300         next = qp->q_back;
301         mlp = (struct ulist *)qp;
302         if (shp_check_one_mines(mlp)) {
303             stopping = 1;
304             emp_remque(qp);
305             free(qp);
306         }
307     }
308     return stopping;
309 }
310
311
312 static void
313 shp_stays(natid actor, char *str, struct ulist *mlp)
314 {
315     mpr(actor, "%s %s & stays in %s\n",
316         prship(&mlp->unit.ship), str,
317         xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, actor));
318     shp_nav_put_one(mlp);
319 }
320
321 /*
322  * Return whether and why SP would be stuck in SECTP.
323  */
324 enum shp_stuck
325 shp_check_nav(struct shpstr *sp, struct sctstr *sectp)
326 {
327     switch (dchr[sectp->sct_type].d_nav) {
328     case NAVOK:
329         break;
330     case NAV_CANAL:
331         if (!(mchr[sp->shp_type].m_flags & M_CANAL)) {
332             return SHP_STUCK_CANAL;
333         }
334         /* fall through */
335     case NAV_02:
336         if (sectp->sct_effic < 2)
337             return SHP_STUCK_CONSTRUCTION;
338         break;
339     case NAV_60:
340         if (sectp->sct_effic < 60)
341             return SHP_STUCK_CONSTRUCTION;
342         break;
343     default:
344         CANT_REACH();
345         /* fall through */
346     case NAV_NONE:
347         return SHP_STUCK_IMPASSABLE;
348     }
349     return SHP_STUCK_NOT;
350 }
351
352 int
353 sect_has_dock(struct sctstr *sect)
354 {
355     switch (dchr[sect->sct_type].d_nav) {
356     case NAV_02:
357     case NAV_CANAL:
358         return 1;
359     default:
360         return 0;
361     }
362 }
363
364 static int
365 shp_count(struct emp_qelem *list, int wantflags, int nowantflags,
366           int x, int y)
367 {
368     struct emp_qelem *qp;
369     struct emp_qelem *next;
370     struct ulist *mlp;
371     int count = 0;
372
373     for (qp = list->q_back; qp != list; qp = next) {
374         next = qp->q_back;
375         mlp = (struct ulist *)qp;
376         if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
377             continue;
378         if (wantflags &&
379             (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
380             continue;
381         if (nowantflags &&
382             ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
383             continue;
384         ++count;
385     }
386     return count;
387 }
388
389 static void
390 shp_damage_one(struct ulist *mlp, int dam)
391 {
392     /* ship might have changed (launched interceptors, missile defense) */
393     getship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
394     shipdamage(&mlp->unit.ship, dam);
395     putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
396     if (!mlp->unit.ship.shp_own) {
397         emp_remque(&mlp->queue);
398         free(mlp);
399     }
400 }
401
402 static int
403 shp_damage(struct emp_qelem *list, int totdam, int wantflags,
404            int nowantflags, int x, int y)
405 {
406     struct emp_qelem *qp;
407     struct emp_qelem *next;
408     struct ulist *mlp;
409     int dam;
410     int count;
411
412     if (!totdam
413         || !(count = shp_count(list, wantflags, nowantflags, x, y)))
414         return 0;
415     dam = ldround((double)totdam / count, 1);
416     for (qp = list->q_back; qp != list; qp = next) {
417         next = qp->q_back;
418         mlp = (struct ulist *)qp;
419         if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
420             continue;
421         if (wantflags &&
422             (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
423             continue;
424         if (nowantflags &&
425             ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
426             continue;
427         shp_damage_one(mlp, dam);
428     }
429     return dam;
430 }
431
432 static int
433 shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
434              int nowantflags)
435 {
436     struct emp_qelem *qp;
437     struct emp_qelem *next;
438     struct ulist *mlp;
439
440     for (qp = list->q_back; qp != list; qp = next) {
441         next = qp->q_back;
442         mlp = (struct ulist *)qp;
443 /* If the ship isn't in the requested sector, then continue */
444         if (newx != mlp->unit.ship.shp_x || newy != mlp->unit.ship.shp_y)
445             continue;
446         if (wantflags &&
447             (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
448             continue;
449         if (nowantflags &&
450             ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
451             continue;
452         return 1;
453     }
454     return 0;
455 }
456
457 static struct ulist *
458 most_valuable_ship(struct emp_qelem *list, coord x, coord y)
459 {
460     struct emp_qelem *qp;
461     struct emp_qelem *next;
462     struct ulist *mlp;
463     struct ulist *mvs = NULL;
464
465     for (qp = list->q_back; qp != list; qp = next) {
466         next = qp->q_back;
467         mlp = (struct ulist *)qp;
468         if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
469             continue;
470         if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
471             continue;
472         if (!((struct mchrstr *)mlp->chrp)->m_nxlight &&
473             !((struct mchrstr *)mlp->chrp)->m_nchoppers &&
474             ((struct mchrstr *)mlp->chrp)->m_cost < 1000 &&
475             !((struct mchrstr *)mlp->chrp)->m_nplanes &&
476             !((struct mchrstr *)mlp->chrp)->m_nland)
477             continue;
478         if (!mvs) {
479             mvs = mlp;
480             continue;
481         }
482         if (((struct mchrstr *)mlp->chrp)->m_cost * mlp->unit.ship.shp_effic >
483             ((struct mchrstr *)mvs->chrp)->m_cost * mvs->unit.ship.shp_effic)
484             mvs = mlp;
485     }
486     return mvs;
487 }
488
489 static int
490 shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
491 {
492     struct emp_qelem *qp;
493     struct emp_qelem *next;
494     struct ulist *mlp;
495     int hard;
496     int easiest = 9876;         /* things start great for victim */
497     int count = 0;
498
499     for (qp = list->q_back; qp != list; qp = next) {
500         next = qp->q_back;
501         mlp = (struct ulist *)qp;
502         if (wantflags &&
503             (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
504             continue;
505         if (nowantflags &&
506             ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
507             continue;
508         hard = shp_hardtarget(&mlp->unit.ship);
509         if (hard < easiest)
510             easiest = hard;     /* things get worse for victim */
511         ++count;
512     }
513     return easiest - count;
514 }
515
516 static int
517 shp_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
518                          natid victim)
519 {
520     int dam, sublaunch;
521     int stopping = 0;
522     struct emp_qelem msl_list, *qp, *newqp;
523     struct plist *plp;
524     struct ulist *mvs;
525
526     mvs = most_valuable_ship(list, newx, newy);
527     if (!mvs)
528         return 0;
529
530     msl_sel(&msl_list, newx, newy, victim, P_T | P_MAR, 0, MI_INTERDICT);
531
532     for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
533         newqp = qp->q_back;
534         plp = (struct plist *)qp;
535
536         if (mvs && mission_pln_equip(plp, NULL, 'p') >= 0) {
537             if (msl_launch(&plp->plane, EF_SHIP, prship(&mvs->unit.ship),
538                            newx, newy, victim, &sublaunch) < 0)
539                 goto use_up_msl;
540             stopping = 1;
541             if (msl_hit(&plp->plane,
542                         shp_hardtarget(&mvs->unit.ship), EF_SHIP,
543                         N_SHP_MISS, N_SHP_SMISS, sublaunch, victim)) {
544                 dam = pln_damage(&plp->plane, 'p', 1);
545                 mpr(victim,
546                     "missile interdiction mission does %d damage to %s!\n",
547                     dam, prship(&mvs->unit.ship));
548                 shp_damage_one(mvs, dam);
549             } else {
550                 dam = pln_damage(&plp->plane, 'p', 0);
551                 collateral_damage(newx, newy, dam);
552             }
553             mvs = most_valuable_ship(list, newx, newy);
554         use_up_msl:
555             plp->plane.pln_effic = 0;
556             putplane(plp->plane.pln_uid, &plp->plane);
557         }
558         emp_remque(qp);
559         free(qp);
560     }
561
562     return stopping;
563 }
564
565 /* Note that this function has a side effect - it uses coastwatch
566  * ranges to see if it should fire upon a ship.  So, this function
567  * is expected to return positive if a ship is in range, and 0 if a
568  * ship is not in range. */
569 static int
570 notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
571 {
572     struct emp_qelem *qp;
573     struct emp_qelem *next;
574     struct ulist *mlp;
575     struct natstr *natp;
576     int vrange;
577
578     natp = getnatp(sectp->sct_own);
579
580     vrange = sectp->sct_type == SCT_RADAR ? 14 : 4;
581     vrange *= tfact(sectp->sct_own, 1.0) * sectp->sct_effic / 100.0;
582
583     if (vrange < 1)
584         vrange = 1;
585
586     if (vrange < trange)
587         return 0;
588
589     for (qp = list->q_back; qp != list; qp = next) {
590         next = qp->q_back;
591         mlp = (struct ulist *)qp;
592         if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
593             continue;
594         if (natp->nat_flags & NF_COASTWATCH)
595             wu(0, sectp->sct_own,
596                "%s %s sighted at %s\n",
597                cname(mlp->unit.ship.shp_own),
598                prship(&mlp->unit.ship),
599                xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
600                     sectp->sct_own));
601         if (opt_HIDDEN)
602             setcont(sectp->sct_own, mlp->unit.ship.shp_own, FOUND_COAST);
603     }
604
605     return 1;
606 }
607
608 static int
609 shp_fort_interdiction(struct emp_qelem *list, coord newx, coord newy,
610                       natid victim)
611 {
612     struct nstr_sect ns;
613     struct sctstr fsect;
614     int trange, range;
615     int dam;
616     int stopping = 0;
617     int totdam = 0;
618     signed char notified[MAXNOC];
619     int i;
620
621     /* Inform neutral and worse */
622     for (i = 0; i < MAXNOC; ++i) {
623         if (relations_with(i, victim) <= NEUTRAL)
624             notified[i] = 0;
625         else
626             notified[i] = 1;
627     }
628
629     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
630     while (nxtsct(&ns, &fsect)) {
631         if (!fsect.sct_own)
632             continue;
633         if (fsect.sct_own == victim)
634             continue;
635         if (notified[fsect.sct_own])
636             continue;
637         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
638         if (notify_coastguard(list, trange, &fsect))
639             notified[fsect.sct_own] = 1;
640     }
641     if (opt_NO_FORT_FIRE)
642         return 0;               /* Only coastwatch notify in nofortfire */
643     /* Only fire at Hostile ships */
644     for (i = 0; i < MAXNOC; ++i) {
645         if (relations_with(i, victim) >= NEUTRAL)
646             notified[i] = 0;
647     }
648     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
649     while (nxtsct(&ns, &fsect)) {
650         if (!notified[fsect.sct_own])
651             continue;
652         range = roundrange(fortrange(&fsect));
653         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
654         if (trange > range)
655             continue;
656         dam = fort_fire(&fsect);
657         putsect(&fsect);
658         if (dam < 0)
659             continue;
660         stopping = 1;
661         totdam += dam;
662         mpr(victim, "Incoming fire does %d damage!\n", dam);
663 #if 0
664         mpr(victim, "%s fires at you for %d!\n",
665             xyas(fsect.sct_x, fsect.sct_y, victim), dam);
666 #endif
667         wu(0, fsect.sct_own,
668            "%s fires at %s ships in %s for %d!\n",
669            xyas(fsect.sct_x, fsect.sct_y,
670                 fsect.sct_own),
671            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
672         nreport(fsect.sct_own, N_SHP_SHELL, victim, 1);
673     }
674     if (totdam > 0)
675         shp_damage(list, totdam, 0, M_SUB, newx, newy);
676     return stopping;
677 }
678
679 static int
680 shp_mission_interdiction(struct emp_qelem *list, coord x, coord y,
681                          natid victim, int subs)
682 {
683     char *what = subs ? "subs" : "ships";
684     int wantflags = subs ? M_SUB : 0;
685     int nowantflags = subs ? 0 : M_SUB;
686     int mission = subs ? MI_SINTERDICT : MI_INTERDICT;
687     int dam;
688
689     dam = unit_interdict(x, y, victim, what,
690                          shp_easiest_target(list, wantflags, nowantflags),
691                          mission);
692     if (dam >= 0)
693         shp_damage(list, dam, wantflags, nowantflags, x, y);
694     return dam >= 0;
695 }
696
697 static int
698 shp_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
699 {
700     int stopping = 0;
701
702     if (shp_contains(list, newx, newy, 0, M_SUB)) {
703         stopping |= shp_fort_interdiction(list, newx, newy, victim);
704
705         if (shp_contains(list, newx, newy, 0, M_SUB)) {
706             stopping |= shp_mission_interdiction(list, newx, newy, victim, 0);
707             stopping |= shp_missile_interdiction(list, newx, newy, victim);
708         }
709     }
710     if (shp_contains(list, newx, newy, M_SUB, 0))
711         stopping |= shp_mission_interdiction(list, newx, newy, victim, 1);
712     return stopping;
713 }
714
715 /* high value of hardtarget is harder to hit */
716 int
717 shp_hardtarget(struct shpstr *sp)
718 {
719     struct sctstr sect;
720     int vis, onsea;
721     struct mchrstr *mcp = mchr + sp->shp_type;
722
723     vis = shp_visib(sp);
724     getsect(sp->shp_x, sp->shp_y, &sect);
725     onsea = sect.sct_type == SCT_WATER;
726     if (mcp->m_flags & M_SUB)
727         vis *= 4;
728     return (int)((sp->shp_effic / 100.0) *
729                  (20 + shp_speed(sp) * onsea / 2.0 - vis));
730 }
731
732 static int
733 shp_hit_mine(struct shpstr *sp)
734 {
735     double m;
736
737     mpr(sp->shp_own, "Kawhomp! Mine detected in %s!\n",
738         xyas(sp->shp_x, sp->shp_y, sp->shp_own));
739
740     nreport(sp->shp_own, N_HIT_MINE, 0, 1);
741
742     m = MINE_DAMAGE();
743     if (mchr[sp->shp_type].m_flags & M_SWEEP)
744         m /= 2.0;
745
746     shipdamage(sp, ldround(m, 1));
747
748     return (int)m;
749 }
750
751 int
752 shp_nav_one_sector(struct emp_qelem *list, int dir, natid actor,
753                    int together)
754 {
755     struct sctstr sect;
756     struct emp_qelem *qp;
757     struct emp_qelem *next;
758     struct ulist *mlp;
759     struct emp_qelem done;
760     coord dx;
761     coord dy;
762     coord newx;
763     coord newy;
764     int move;
765     enum shp_stuck stuck;
766     int stopping = 0;
767     double mobcost;
768     char dp[80];
769
770     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
771         shp_nav_put(list, actor);
772         return 1;
773     }
774     dx = diroff[dir][0];
775     dy = diroff[dir][1];
776
777     move = 0;
778     for (qp = list->q_back; qp != list; qp = next) {
779         next = qp->q_back;
780         mlp = (struct ulist *)qp;
781         newx = xnorm(mlp->unit.ship.shp_x + dx);
782         newy = ynorm(mlp->unit.ship.shp_y + dy);
783         getsect(newx, newy, &sect);
784         stuck = shp_check_nav(&mlp->unit.ship, &sect);
785         if (stuck == SHP_STUCK_NOT &&
786             (!sect.sct_own
787              || relations_with(sect.sct_own, actor) >= FRIENDLY))
788             move = 1;
789     }
790
791     for (qp = list->q_back; qp != list; qp = next) {
792         next = qp->q_back;
793         mlp = (struct ulist *)qp;
794         newx = xnorm(mlp->unit.ship.shp_x + dx);
795         newy = ynorm(mlp->unit.ship.shp_y + dy);
796         getsect(newx, newy, &sect);
797         stuck = shp_check_nav(&mlp->unit.ship, &sect);
798         if (stuck != SHP_STUCK_NOT ||
799             (sect.sct_own
800              && relations_with(sect.sct_own, actor) < FRIENDLY)) {
801             if (stuck == SHP_STUCK_CANAL)
802                 sprintf(dp,
803                         "is too large to fit into the canal system at %s",
804                         xyas(newx, newy, actor));
805             else
806                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
807             if (together && !move) {
808                 mpr(actor, "%s\n", dp);
809                 return 1;
810             } else {
811                 shp_stays(actor, dp, mlp);
812                 continue;
813             }
814         }
815
816         if (mlp->mobil <= 0.0) {
817             shp_stays(actor, "is out of mobility", mlp);
818             continue;
819         }
820         mobcost = shp_mobcost(&mlp->unit.ship);
821         mlp->unit.ship.shp_x = newx;
822         mlp->unit.ship.shp_y = newy;
823         if (mlp->mobil - mobcost < -127) {
824             mlp->mobil = -127;
825         } else {
826             mlp->mobil -= mobcost;
827         }
828         mlp->unit.ship.shp_mobil = (int)mlp->mobil;
829         putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
830
831         /* Now update the map for this ship */
832         rad_map_set(mlp->unit.ship.shp_own,
833                     mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
834                     mlp->unit.ship.shp_effic, mlp->unit.ship.shp_tech,
835                     ((struct mchrstr *)mlp->chrp)->m_vrnge);
836     }
837     if (QEMPTY(list))
838         return stopping;
839     stopping |= shp_sweep(list, 0, 0, actor);
840     if (QEMPTY(list))
841         return stopping;
842     stopping |= shp_check_mines(list);
843     if (QEMPTY(list))
844         return stopping;
845
846     /* interdict ships sector by sector */
847     emp_initque(&done);
848     while (!QEMPTY(list)) {
849         mlp = (struct ulist *)list->q_back;
850         newx = mlp->unit.ship.shp_x;
851         newy = mlp->unit.ship.shp_y;
852         stopping |= shp_interdict(list, newx, newy, actor);
853         /* move survivors in this sector to done */
854         for (qp = list->q_back; qp != list; qp = next) {
855             next = qp->q_back;
856             mlp = (struct ulist *)qp;
857             if (mlp->unit.ship.shp_x == newx &&
858                 mlp->unit.ship.shp_y == newy) {
859                 emp_remque(qp);
860                 emp_insque(qp, &done);
861             }
862         }
863     }
864     /* assign surviving ships back to list */
865     emp_insque(list, &done);
866     emp_remque(&done);
867
868     return stopping;
869 }
870
871 /*
872  * shp_miss_defence
873  * Check for incoming missiles with a P_MAR flag.
874  * Return True=1 if the missile was shotdown.
875  * Or False=0
876  *
877  * Chad Zabel, July 95
878  */
879
880 int
881 shp_missile_defense(coord dx, coord dy, natid bombown, int hardtarget)
882 {
883     struct nstr_item ni;
884     struct shpstr ship;
885     int hitchance, hit;
886     double gun, eff, teff;
887
888     snxtitem_dist(&ni, EF_SHIP, dx, dy, 1);
889
890     while (nxtitem(&ni, &ship)) {
891         if (!ship.shp_own)
892             continue;
893
894         if (!(mchr[(int)ship.shp_type].m_flags & M_ANTIMISSILE))
895             continue;
896
897         if (relations_with(ship.shp_own, bombown) >= NEUTRAL)
898             continue;
899
900         if (ship.shp_effic < 60)
901             continue;
902
903         if (ship.shp_item[I_MILIT] < 1) /* do we have mil? */
904             continue;
905         if (ship.shp_item[I_GUN] < 1)   /* we need at least 1 gun */
906             continue;
907         if (!shp_supply(&ship, I_SHELL, 2))
908             continue;
909         ship.shp_item[I_SHELL] -= 2;
910         putship(ship.shp_uid, &ship);
911
912         /* now calculate the odds */
913         gun = shp_usable_guns(&ship);
914         eff = ship.shp_effic / 100.0;
915         teff = ship.shp_tech / (ship.shp_tech + 200.0);
916         /* raise 4.5 for better interception -KHS */
917         hitchance = (int)(gun * eff * teff * 4.5) - hardtarget;
918         hitchance = LIMIT_TO(hitchance, 0, 100);
919         hit = pct_chance(hitchance);
920
921         mpr(bombown, "%s anti-missile system activated...%s\n",
922             cname(ship.shp_own),
923             hit ? "KABOOOM!! Missile destroyed\n"
924             : "SWOOSH!!  anti-missile system failed!!");
925         mpr(ship.shp_own, "Ship #%i anti-missile system activated!\n",
926             ship.shp_uid);
927         mpr(ship.shp_own, "%d%% hitchance...%s\n", hitchance,
928             hit ? "KABOOOM!!  Incoming missile destroyed!\n"
929             : "SWOOSH!!  Missile evades anti-missile systems\n");
930
931         if (hit)
932             return 1;
933     }
934     return 0;                   /* all attempts failed */
935 }
936
937
938 /* Fire missiles at a ship which has fired shells */
939 void
940 shp_missdef(struct shpstr *sp, natid victim)
941 {
942     struct emp_qelem list;
943     struct ulist *mlp;
944     int eff;
945     char buf[512];
946
947     emp_initque(&list);
948     mlp = shp_insque(sp, &list);
949     sprintf(buf, "%s", prship(&mlp->unit.ship));
950
951     eff = sp->shp_effic;
952     shp_missile_interdiction(&list, sp->shp_x, sp->shp_y, sp->shp_own);
953     getship(sp->shp_uid, sp);
954
955     if (!sp->shp_own) {
956         wu(0, victim,
957            "missiles launched in defense did 100%% damage to %s\n",
958            buf);
959         wu(0, victim, "%s sunk!\n", buf);
960     } else if (eff > 0 && sp->shp_effic < eff) {
961         wu(0, victim,
962            "missiles launched in defense did %d%% damage to %s\n",
963            100 * (eff - sp->shp_effic) / eff, buf);
964     }
965     if (!QEMPTY(&list))
966         free(mlp);
967 }
968
969 double
970 shp_mobcost(struct shpstr *sp)
971 {
972     return speed_factor(sp->shp_effic * 0.01 * shp_speed(sp),
973                         sp->shp_tech);
974 }
975
976 /*
977  * Set SP's tech to TLEV along with everything else that depends on it.
978  */
979 void
980 shp_set_tech(struct shpstr *sp, int tlev)
981 {
982     struct mchrstr *mcp = mchr + sp->shp_type;
983
984     if (CANT_HAPPEN(tlev < mcp->m_tech))
985         tlev = mcp->m_tech;
986
987     sp->shp_tech = tlev;
988 }