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