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