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