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