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