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