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