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