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