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