]> git.pond.sub.org Git - empserver/blob - src/lib/subs/shpsub.c
Do not include var.h where no longer needed. Clean up register keywords in these...
[empserver] / src / lib / subs / shpsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-1999, 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 <math.h>
36 #include "misc.h"
37 #include "queue.h"
38 #include "player.h"
39 #include "sect.h"
40 #include "ship.h"
41 #include "plane.h"
42 #include "land.h"
43 #include "news.h"
44 #include "item.h"
45 #include "xy.h"
46 #include "nsc.h"
47 #include "file.h"
48 #include "nat.h"
49 #include "path.h"
50 #include "mission.h"
51 #include "optlist.h"
52 #include "damage.h"
53 #include "server.h"
54 #include "prototypes.h"
55
56 static int shp_check_nav(struct sctstr *);
57 static int shp_check_one_mines(struct mlist *);
58 static int shp_hit_mine(struct shpstr *, struct mchrstr *);
59 static void shp_mess(s_char *, struct mlist *);
60
61 void
62 shp_sel(struct nstr_item *ni, struct emp_qelem *list)
63
64
65     /*  int     wantflags;
66        int      nowantflags;
67      */
68 {
69     struct shpstr ship;
70     struct mchrstr *mcp;
71     struct mlist *mlp;
72
73     emp_initque(list);
74     while (nxtitem(ni, (s_char *)&ship)) {
75         if (!player->owner)
76             continue;
77         mcp = &mchr[(int)ship.shp_type];
78         /* if (wantflags && (mcp->m_flags & wantflags) != wantflags)
79            continue;
80            if (nowantflags && mcp->m_flags & nowantflags)
81            continue;
82          */
83         if (opt_MARKET) {
84             if (ontradingblock(EF_SHIP, (int *)&ship)) {
85                 pr("ship #%d inelligible - it's for sale.\n",
86                    ship.shp_uid);
87                 continue;
88             }
89         }
90         /* This abuse is better fixed by building a ship with the normal negative
91            mobility that everything else is built with */
92 /*
93         if (opt_MOB_ACCESS) {
94           if (ship.shp_effic < 21 &&
95             ship.shp_mobil < etu_per_update) {
96             pr("%s needs at least %d mob to navigate.\n",
97                prship(&ship), etu_per_update);
98             continue;
99           }
100         }
101 */
102         ship.shp_mission = 0;
103         ship.shp_rflags = 0;
104         memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
105         putship(ship.shp_uid, &ship);
106         mlp = (struct mlist *)malloc(sizeof(struct mlist));
107         mlp->mcp = mcp;
108         mlp->ship = ship;
109         mlp->mobil = (double)ship.shp_mobil;
110         emp_insque(&mlp->queue, list);
111     }
112 }
113
114 /* This function assumes that the list was created by shp_sel */
115 void
116 shp_nav(struct emp_qelem *list, double *minmobp, double *maxmobp,
117         int *togetherp, natid actor)
118 {
119     struct emp_qelem *qp;
120     struct emp_qelem *next;
121     struct mlist *mlp;
122     struct sctstr sect;
123     struct shpstr ship;
124     coord allx;
125     coord ally;
126     int first = 1;
127
128     *minmobp = 9876.0;
129     *maxmobp = -9876.0;
130     *togetherp = 1;
131     for (qp = list->q_back; qp != list; qp = next) {
132         next = qp->q_back;
133         mlp = (struct mlist *)qp;
134         getship(mlp->ship.shp_uid, &ship);
135         if (ship.shp_own != actor) {
136             mpr(actor, "%s was sunk at %s\n",
137                 prship(&ship), xyas(ship.shp_x, ship.shp_y, actor));
138             emp_remque((struct emp_qelem *)mlp);
139             free((s_char *)mlp);
140             continue;
141         }
142         if (opt_SAIL) {
143             if (*ship.shp_path && !update_pending) {
144                 shp_mess("has a sail path", mlp);
145                 mpr(actor, "Use `sail <#> -' to reset\n");
146                 continue;
147             }
148         }
149         /* check crew - uws don't count */
150         if (ship.shp_item[I_MILIT] == 0 && ship.shp_item[I_CIVIL] == 0) {
151             shp_mess("is crewless", mlp);
152             continue;
153         }
154         if (!getsect(ship.shp_x, ship.shp_y, &sect)) {
155             shp_mess("was sucked into the sky by a strange looking spaceship", mlp);    /* heh -KHS */
156             continue;
157         }
158         switch (shp_check_nav(&sect)) {
159         case CN_CONSTRUCTION:
160             shp_mess("is caught in a construction zone", mlp);
161             continue;
162         case CN_LANDLOCKED:
163             shp_mess("is landlocked", mlp);
164             break;
165         case CN_NAVIGABLE:
166             break;
167         case CN_ERROR:
168         default:
169             shp_mess("was just swallowed by a big green worm", mlp);
170             continue;
171         }
172         if (first) {
173             allx = ship.shp_x;
174             ally = ship.shp_y;
175             first = 0;
176         }
177         if (ship.shp_x != allx || ship.shp_y != ally)
178             *togetherp = 0;
179         if (ship.shp_mobil + 1 < (int)mlp->mobil) {
180             mlp->mobil = (double)ship.shp_mobil;
181         }
182         if (mlp->mobil < *minmobp)
183             *minmobp = mlp->mobil;
184         if (mlp->mobil > *maxmobp)
185             *maxmobp = mlp->mobil;
186         mlp->ship = ship;
187     }
188 }
189
190 void
191 shp_put(struct emp_qelem *list, natid actor)
192 {
193     struct emp_qelem *qp;
194     struct emp_qelem *newqp;
195     struct mlist *mlp;
196
197     qp = list->q_back;
198     while (qp != list) {
199         mlp = (struct mlist *)qp;
200         mpr(actor, "%s stopped at %s\n", prship(&mlp->ship),
201             xyas(mlp->ship.shp_x, mlp->ship.shp_y, mlp->ship.shp_own));
202         mlp->ship.shp_mobil = (int)mlp->mobil;
203         putship(mlp->ship.shp_uid, &mlp->ship);
204         newqp = qp->q_back;
205         emp_remque(qp);
206         free((s_char *)qp);
207         qp = newqp;
208     }
209 }
210
211 int
212 shp_sweep(struct emp_qelem *ship_list, int verbose, natid actor)
213 {
214     struct emp_qelem *qp;
215     struct emp_qelem *next;
216     struct mlist *mlp;
217     struct sctstr sect;
218     int mines, m, max, shells;
219     int changed = 0;
220     int stopping = 0;
221     double mobcost;
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         mobcost = mlp->ship.shp_effic * 0.01 * mlp->ship.shp_speed;
245         mobcost = 480.0 / (mobcost +
246                            techfact(mlp->ship.shp_tech, mobcost));
247         mlp->mobil -= mobcost;
248         mlp->ship.shp_mobil = (int)mlp->mobil;
249         putship(mlp->ship.shp_uid, &mlp->ship);
250         if (!(mines = sect.sct_mines))
251             continue;
252         max = mlp->mcp->m_item[I_SHELL];
253         shells = mlp->ship.shp_item[I_SHELL];
254         for (m = 0; mines > 0 && m < 5; m++) {
255             if (chance(0.66)) {
256                 mpr(actor, "Sweep...\n");
257                 mines--;
258                 shells = min(max, shells + 1);
259                 changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
260             }
261         }
262         sect.sct_mines = mines;
263         mlp->ship.shp_item[I_SHELL] = shells;
264         if (shp_check_one_mines(mlp)) {
265             stopping = 1;
266             emp_remque(qp);
267             free((s_char *)qp);
268         }
269         putship(mlp->ship.shp_uid, &mlp->ship);
270         putsect(&sect);
271     }
272     if (changed)
273         writemap(actor);
274     return stopping;
275 }
276
277 static int
278 shp_check_one_mines(struct mlist *mlp)
279 {
280     struct sctstr sect;
281     int changed = 0;
282     int actor;
283
284     getsect(mlp->ship.shp_x, mlp->ship.shp_y, &sect);
285     if (sect.sct_type != SCT_WATER)
286         return 0;
287     if (!sect.sct_mines)
288         return 0;
289     if (chance(DMINE_HITCHANCE(sect.sct_mines))) {
290         actor = mlp->ship.shp_own;
291         shp_hit_mine(&mlp->ship, mlp->mcp);
292         sect.sct_mines--;
293         changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
294         if (changed)
295             writemap(actor);
296         putsect(&sect);
297         putship(mlp->ship.shp_uid, (s_char *)&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((s_char *)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("%1c", 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(s_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((s_char *)mlp);
367 }
368
369 static int
370 shp_check_nav(struct sctstr *sect)
371 {
372     switch (dchr[sect->sct_type].d_flg & 03) {
373     case NAVOK:
374         break;
375
376     case NAV_02:
377         if (sect->sct_effic < 2)
378             return CN_CONSTRUCTION;
379         break;
380     case NAV_60:
381         if (sect->sct_effic < 60)
382             return CN_CONSTRUCTION;
383         break;
384     default:
385         return CN_LANDLOCKED;
386     }
387     return CN_NAVIGABLE;
388 }
389
390 static int
391 shp_count(struct emp_qelem *list, int wantflags, int nowantflags, int x,
392           int y)
393 {
394     struct emp_qelem *qp;
395     struct emp_qelem *next;
396     struct mlist *mlp;
397     int count = 0;
398
399     for (qp = list->q_back; qp != list; qp = next) {
400         next = qp->q_back;
401         mlp = (struct mlist *)qp;
402         if (mlp->ship.shp_x != x || mlp->ship.shp_y != y)
403             continue;
404         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
405             continue;
406         if (nowantflags && mlp->mcp->m_flags & nowantflags)
407             continue;
408         ++count;
409     }
410     return count;
411 }
412
413 static void
414 shp_damage_one(struct mlist *mlp, int dam)
415 {
416     shipdamage(&mlp->ship, dam);
417     putship(mlp->ship.shp_uid, &mlp->ship);
418     if (!mlp->ship.shp_own) {
419         emp_remque((struct emp_qelem *)mlp);
420         free((s_char *)mlp);
421     }
422 }
423
424 static int
425 shp_damage(struct emp_qelem *list, int totdam, int wantflags,
426            int nowantflags, int x, int y)
427 {
428     struct emp_qelem *qp;
429     struct emp_qelem *next;
430     struct mlist *mlp;
431     int dam;
432     int count;
433
434     if (!totdam
435         || !(count = shp_count(list, wantflags, nowantflags, x, y)))
436         return 0;
437     dam = ldround(((double)totdam / (double)count), 1);
438     for (qp = list->q_back; qp != list; qp = next) {
439         next = qp->q_back;
440         mlp = (struct mlist *)qp;
441         if (mlp->ship.shp_x != x || mlp->ship.shp_y != y)
442             continue;
443         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
444             continue;
445         if (nowantflags && mlp->mcp->m_flags & nowantflags)
446             continue;
447         shp_damage_one(mlp, dam);
448     }
449     return dam;
450 }
451
452 static int
453 shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
454              int nowantflags)
455 {
456     struct emp_qelem *qp;
457     struct emp_qelem *next;
458     struct mlist *mlp;
459
460     for (qp = list->q_back; qp != list; qp = next) {
461         next = qp->q_back;
462         mlp = (struct mlist *)qp;
463 /* If the ship isn't in the requested sector, then continue */
464         if (newx != mlp->ship.shp_x || newy != mlp->ship.shp_y)
465             continue;
466         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
467             continue;
468         if (nowantflags && mlp->mcp->m_flags & nowantflags)
469             continue;
470         return 1;
471     }
472     return 0;
473 }
474
475 static struct mlist *
476 most_valuable_ship(struct emp_qelem *list)
477 {
478     struct emp_qelem *qp;
479     struct emp_qelem *next;
480     struct mlist *mlp;
481     struct mlist *mvs = 0;
482
483     for (qp = list->q_back; qp != list; qp = next) {
484         next = qp->q_back;
485         mlp = (struct mlist *)qp;
486         if (mlp->mcp->m_flags & M_SUB)
487             continue;
488         if (!mlp->mcp->m_nxlight &&
489             !mlp->mcp->m_nchoppers &&
490             mlp->mcp->m_cost < 1000 &&
491             !mlp->mcp->m_nplanes && !mlp->mcp->m_nland)
492             continue;
493         if (!mvs) {
494             mvs = mlp;
495             continue;
496         }
497         if (mlp->mcp->m_cost * mlp->ship.shp_effic >
498             mvs->mcp->m_cost * mvs->ship.shp_effic)
499             mvs = mlp;
500     }
501     return mvs;
502 }
503
504 static int
505 shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
506 {
507     struct emp_qelem *qp;
508     struct emp_qelem *next;
509     struct mlist *mlp;
510     int hard;
511     int easiest = 9876;         /* things start great for victim */
512     int count = 0;
513
514     for (qp = list->q_back; qp != list; qp = next) {
515         next = qp->q_back;
516         mlp = (struct mlist *)qp;
517         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
518             continue;
519         if (nowantflags && mlp->mcp->m_flags & nowantflags)
520             continue;
521         hard = shp_hardtarget(&mlp->ship);
522         if (hard < easiest)
523             easiest = hard;     /* things get worse for victim */
524         ++count;
525     }
526     return easiest - count;
527 }
528
529 static int
530 shp_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
531                          natid victim)
532 {
533     int dam;
534     int twotries;
535     int stopping = 0;
536     struct emp_qelem msl_list, *qp, *newqp;
537     struct mlist *mvs;
538     s_char what[512];
539
540     msl_sel(&msl_list, newx, newy, victim, P_T | P_MAR, 0, MI_INTERDICT);
541
542     twotries = 0;
543     while (!QEMPTY(&msl_list) && (mvs = most_valuable_ship(list))) {
544         sprintf(what, "%s", prship(&mvs->ship));
545         dam = msl_launch_mindam(&msl_list, newx, newy,
546                                 shp_hardtarget(&mvs->ship),
547                                 EF_SHIP, 1, what, victim, MI_INTERDICT);
548         if (dam) {
549             mpr(victim,
550                 "missile interdiction mission does %d damage to %s!\n",
551                 dam, what);
552             shp_damage_one(mvs, dam);
553             twotries = 0;
554             stopping |= 1;
555         } else if (++twotries >= 2) {
556             break;
557         }
558     }
559     qp = msl_list.q_forw;
560     while (qp != msl_list.q_forw) {
561         newqp = qp->q_forw;
562         emp_remque(qp);
563         free(qp);
564         qp = newqp;
565     }
566
567     return stopping;
568 }
569
570 /* Note that this function has a side effect - it uses coastwatch
571  * ranges to see if it should fire upon a ship.  So, this function
572  * is expected to return positive if a ship is in range, and 0 if a
573  * ship is not in range. */
574 static int
575 notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
576 {
577     struct emp_qelem *qp;
578     struct emp_qelem *next;
579     struct mlist *mlp;
580     struct natstr *natp;
581     int vrange;
582
583     natp = getnatp(sectp->sct_own);
584
585     vrange = sectp->sct_type == SCT_RADAR ? 14 : 4;
586     vrange *= tfact(sectp->sct_own, 1.0) * sectp->sct_effic / 100.0;
587
588     if (vrange < 1)
589         vrange = 1;
590
591     if (vrange < trange)
592         return 0;
593
594     /* We got here, so we could theoretically see the ship.  Now,
595      * do we want to see it in our telebox? If not, return positive
596      * since we could see the ship and want forts to fire. */
597     if (!(natp->nat_flags & NF_COASTWATCH))
598         return 1;
599
600     for (qp = list->q_back; qp != list; qp = next) {
601         next = qp->q_back;
602         mlp = (struct mlist *)qp;
603         if (mlp->mcp->m_flags & M_SUB)
604             continue;
605         wu(0, sectp->sct_own,
606            "%s %s sighted at %s\n",
607            cname(mlp->ship.shp_own),
608            prship(&mlp->ship),
609            xyas(mlp->ship.shp_x, mlp->ship.shp_y, sectp->sct_own));
610         if (opt_HIDDEN) {
611             setcont(sectp->sct_own, mlp->ship.shp_own, FOUND_LOOK);
612         }
613     }
614
615     return 1;
616 }
617
618 static int
619 shp_fort_interdiction(struct emp_qelem *list, coord newx, coord newy,
620                       natid victim)
621 {
622     struct nstr_sect ns;
623     struct sctstr fsect;
624     int trange;
625     double range, range2, guneff;
626     int shell, gun;
627     int dam;
628     int totdam = 0;
629     s_char notified[MAXNOC];
630     int i;
631
632     /* Inform neutral and worse */
633     for (i = 0; i < MAXNOC; ++i) {
634         if (getrel(getnatp(i), victim) <= NEUTRAL)
635             notified[i] = 0;
636         else
637             notified[i] = 1;
638     }
639
640     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
641     while (nxtsct(&ns, &fsect)) {
642         if (!fsect.sct_own)
643             continue;
644         if (fsect.sct_own == victim)
645             continue;
646         if (notified[fsect.sct_own])
647             continue;
648         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
649         if (notify_coastguard(list, trange, &fsect))
650             notified[fsect.sct_own] = 1;
651     }
652     if (opt_NO_FORT_FIRE)
653         return 0;               /* Only coastwatch notify in nofortfire */
654     /* Only fire at Hostile ships */
655     for (i = 0; i < MAXNOC; ++i) {
656         if (getrel(getnatp(i), victim) >= NEUTRAL)
657             notified[i] = 0;
658     }
659     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
660     while (nxtsct(&ns, &fsect)) {
661         if (!notified[fsect.sct_own])
662             continue;
663         if (fsect.sct_type != SCT_FORTR)
664             continue;
665         gun = fsect.sct_item[I_GUN];
666         if (gun < 1)
667             continue;
668         range = tfactfire(fsect.sct_own, (double)min(gun, 7));
669         if (fsect.sct_effic > 59)
670             range++;
671         range2 = roundrange(range);
672         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
673         if (trange > range2)
674             continue;
675         if (fsect.sct_item[I_MILIT] < 5)
676             continue;
677         shell = fsect.sct_item[I_SHELL];
678         if (shell < 1)
679             shell += supply_commod(fsect.sct_own,
680                                    fsect.sct_x, fsect.sct_y, I_SHELL, 1);
681         if (shell < 1)
682             continue;
683         shell--;
684         fsect.sct_item[I_SHELL] = shell;
685         putsect(&fsect);
686         if (gun > 7)
687             gun = 7;
688         guneff = landgun((int)fsect.sct_effic, gun);
689         dam = (int)guneff;
690         totdam += dam;
691         mpr(victim, "Incoming fire does %d damage!\n", dam);
692 /*
693   mpr(victim, "%s fires at you for %d!\n",
694   xyas(fsect.sct_x,fsect.sct_y,victim),
695   dam);
696 */
697         wu(0, fsect.sct_own,
698            "%s fires at %s ships in %s for %d!\n",
699            xyas(fsect.sct_x, fsect.sct_y,
700                 fsect.sct_own),
701            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
702         nreport(fsect.sct_own, N_SHP_SHELL, victim, 1);
703     }
704     if (totdam > 0)
705         return shp_damage(list, totdam, 0, M_SUB, newx, newy);
706     return 0;
707 }
708
709 static int
710 shp_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
711 {
712     int stopping = 0;
713
714     if (shp_contains(list, newx, newy, 0, M_SUB)) {
715         stopping |= shp_fort_interdiction(list, newx, newy, victim);
716
717         if (shp_contains(list, newx, newy, 0, M_SUB)) {
718             stopping |=
719                 shp_damage(list,
720                            unit_interdict(newx, newy, victim, "ships",
721                                           shp_easiest_target(list, 0,
722                                                              M_SUB),
723                                           MI_INTERDICT), 0, M_SUB, newx,
724                            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), M_SUB, 0, newx,
737                        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 = 22.0 + (double)(random() % 21);
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 emp_qelem *nqp;
809     struct emp_qelem *nnext;
810     struct mlist *mlp;
811     coord dx;
812     coord dy;
813     coord newx;
814     coord newy;
815     int stopping = 0;
816     double mobcost;
817     double tech;                /* for mapping */
818     double tf;                  /* for mapping */
819     s_char dp[80];
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         if (shp_check_nav(&sect) != CN_NAVIGABLE ||
834             (sect.sct_own && actor != sect.sct_own &&
835              getrel(getnatp(sect.sct_own), actor) < FRIENDLY)) {
836             if (together) {
837                 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
838                 return 2;
839             } else {
840                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
841                 shp_mess(dp, mlp);
842                 continue;
843             }
844         }
845         if (opt_BIG_CITY && sect.sct_type == SCT_CAPIT) {
846             if (mlp->mcp->m_lcm + 2 * mlp->mcp->m_hcm >= 60) {
847                 sprintf(dp,
848                         "is too large to fit into the canal system at %s",
849                         xyas(newx, newy, actor));
850                 shp_mess(dp, mlp);
851                 continue;
852             }
853         }
854         if (mlp->mobil <= 0.0) {
855             shp_mess("is out of mobility", mlp);
856             continue;
857         }
858         mobcost = mlp->ship.shp_effic * 0.01 * mlp->ship.shp_speed;
859         mobcost = 480.0 /
860             (mobcost + techfact(mlp->ship.shp_tech, mobcost));
861         mlp->ship.shp_x = newx;
862         mlp->ship.shp_y = newy;
863         if (mlp->mobil - mobcost < -127) {
864             mlp->mobil = -127;
865         } else {
866             mlp->mobil -= mobcost;
867         }
868         mlp->ship.shp_mobil = (int)mlp->mobil;
869         putship(mlp->ship.shp_uid, &mlp->ship);
870         mlp->done = 0;          /* We haven't interdicted this ship yet */
871
872         /* Now update the map for this ship */
873         tech = techfact(mlp->ship.shp_tech, (double)mlp->mcp->m_vrnge);
874         if (mlp->mcp->m_flags & M_SONAR)
875             tf = techfact(mlp->ship.shp_tech, 1.0);
876         else
877             tf = 0.0;
878         radmapupd(mlp->ship.shp_own, mlp->ship.shp_x, mlp->ship.shp_y,
879                   (int)mlp->ship.shp_effic, (int)tech, tf);
880     }
881     if (QEMPTY(list))
882         return stopping;
883     stopping |= shp_sweep(list, 0, actor);
884     if (QEMPTY(list))
885         return stopping;
886     stopping |= shp_check_mines(list);
887     if (QEMPTY(list))
888         return stopping;
889
890 /* Ok, run through each ship and interdict each coordinate */
891     for (qp = list->q_back; qp != list; qp = next) {
892         next = qp->q_back;
893         mlp = (struct mlist *)qp;
894 /* Has this ship been interdicted yet? */
895         if (mlp->done)
896             continue;
897         newx = mlp->ship.shp_x;
898         newy = mlp->ship.shp_y;
899         stopping |= shp_interdict(list, newx, newy, actor);
900         if (QEMPTY(list))
901             return stopping;
902 /* Now, set all ships in this coordinate to done */
903         for (nqp = list->q_back; nqp != list; nqp = nnext) {
904             nnext = nqp->q_back;
905             mlp = (struct mlist *)nqp;
906             if (mlp->ship.shp_x == newx && mlp->ship.shp_y == newy)
907                 mlp->done = 1;
908         }
909     }
910
911     return stopping;
912 }
913
914 /*
915  * shp_miss_defence 
916  * Check for incoming missiles with a P_MAR flag. 
917  * Return True=1 if the missile was shotdown.
918  * Or False=0
919  * 
920  * Chad Zabel, July 95
921  */
922
923 int
924 shp_missile_defense(coord dx, coord dy, natid bombown, int hardtarget)
925 {
926     struct nstr_item ni;
927     struct shpstr ship;
928     int hitchance;
929     int shell;
930     double gun, eff, teff;
931
932     snxtitem_dist(&ni, EF_SHIP, dx, dy, 1);
933
934     while (nxtitem(&ni, &ship)) {
935         if (!ship.shp_own)
936             continue;
937
938         if (!(mchr[(int)ship.shp_type].m_flags & M_ANTIMISSILE))
939             continue;
940
941         if (getrel(getnatp(ship.shp_own), bombown) >= NEUTRAL)
942             continue;
943
944         if (ship.shp_effic < 60)
945             continue;
946
947         shell = ship.shp_item[I_SHELL];
948         if (ship.shp_item[I_MILIT] < 1) /* do we have mil? */
949             continue;
950         if (shell < 2) {        /* do we need shells */
951             shell += supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
952                                    I_SHELL, 2);
953             if (shell < 2)
954                 continue;
955         }
956         if (ship.shp_item[I_GUN] < 1)   /* we need at least 1 gun */
957             continue;
958
959         /* now calculate the odds */
960         gun = min(ship.shp_item[I_GUN], ship.shp_glim);
961         eff = (double)ship.shp_effic / 100.0;
962         teff =
963             (((double)ship.shp_tech) / (((double)ship.shp_tech) + 200.0));
964         /* raise 4.5 for better interception -KHS */
965         hitchance = (int)(gun * eff * teff * 4.5) - hardtarget;
966         if (hitchance < 0)
967             hitchance = 0;
968         if (hitchance > 100)
969             hitchance = 100;
970
971         mpr(bombown, "%s anti-missile system activated...",
972             cname(ship.shp_own));
973         mpr(ship.shp_own, "Ship #%i anti-missile system activated!\n",
974             ship.shp_uid);
975         mpr(ship.shp_own, "%d%% hitchance...", hitchance);
976         /* use ammo */
977         ship.shp_item[I_SHELL] = shell - 2;
978         putship(ship.shp_uid, &ship);
979
980         if (roll(100) <= hitchance) {
981             mpr(bombown, "KABOOOM!! Missile destroyed\n\n");
982             mpr(ship.shp_own,
983                 "KABOOOM!!  Incoming missile destroyed!\n\n");
984             return 1;
985         } else {
986             mpr(bombown, "SWOOSH!!  anti-missile system failed!!\n");
987             mpr(ship.shp_own,
988                 "SWOOSH!!  Missile evades anti-missile systems\n\n");
989         }
990     }
991     return 0;                   /* all attempts failed */
992 }
993
994 s_char *
995 shp_path(int together, struct shpstr *shp, s_char *buf)
996 {
997     coord destx;
998     coord desty;
999     struct sctstr d_sect;
1000     s_char *cp;
1001
1002     if (!sarg_xy(buf, &destx, &desty))
1003         return 0;
1004     if (!together) {
1005         mpr(shp->shp_own,
1006             "Cannot go to a destination sector if not all starting in the same sector\n");
1007         return 0;
1008     }
1009     if (!getsect(destx, desty, &d_sect)) {
1010         mpr(shp->shp_own, "%d,%d is not a sector\n", destx, desty);
1011         return 0;
1012     }
1013
1014     cp = (s_char *)BestShipPath(buf, shp->shp_x, shp->shp_y,
1015                                 d_sect.sct_x, d_sect.sct_y, player->cnum);
1016     if (!cp || shp->shp_mobil <= 0) {
1017         mpr(shp->shp_own, "Can't get to '%s' right now.\n",
1018             xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
1019         return 0;
1020     }
1021     return cp;
1022 }
1023
1024 /* Fire missiles at a ship which has fired shells */
1025 void
1026 shp_missdef(struct shpstr *sp, natid victim)
1027 {
1028     struct emp_qelem list;
1029     struct mlist *mlp;
1030     int eff;
1031     s_char buf[512];
1032
1033     emp_initque(&list);
1034
1035     mlp = (struct mlist *)malloc(sizeof(struct mlist));
1036     mlp->mcp = &mchr[(int)sp->shp_type];
1037     mlp->ship = *sp;
1038     mlp->mobil = (double)sp->shp_mobil;
1039     emp_insque(&mlp->queue, &list);
1040     sprintf(buf, "%s", prship(&mlp->ship));
1041
1042     eff = sp->shp_effic;
1043     if (most_valuable_ship(&list)) {
1044         shp_missile_interdiction(&list, sp->shp_x, sp->shp_y, sp->shp_own);
1045         getship(sp->shp_uid, sp);
1046
1047         if (!sp->shp_own) {
1048             wu(0, victim,
1049                "missiles launched in defense did 100%% damage to %s\n",
1050                buf);
1051             wu(0, victim, "%s sunk!\n", buf);
1052         } else if (eff > 0 && sp->shp_effic < eff) {
1053             wu(0, victim,
1054                "missiles launched in defense did %d%% damage to %s\n",
1055                100 * (eff - sp->shp_effic) / eff, buf);
1056         }
1057     }
1058     if (!QEMPTY(&list))
1059         free(mlp);
1060 }
1061
1062 /*
1063  * Set SP's tech to TLEV along with everything else that depends on it.
1064  */
1065 void
1066 shp_set_tech(struct shpstr *sp, int tlev)
1067 {
1068     struct mchrstr *mcp = mchr + sp->shp_type;
1069     int tech_diff = tlev - mcp->m_tech;
1070
1071     if (CANT_HAPPEN(tech_diff < 0)) {
1072       tlev -= tech_diff;
1073       tech_diff = 0;
1074     }
1075
1076     sp->shp_tech = tlev;
1077     sp->shp_armor = (short)SHP_DEF(mcp->m_armor, tech_diff);
1078     sp->shp_speed = (short)SHP_SPD(mcp->m_speed, tech_diff);
1079     sp->shp_visib = (short)SHP_VIS(mcp->m_visib, tech_diff);
1080     sp->shp_frnge = (short)SHP_RNG(mcp->m_frnge, tech_diff);
1081     sp->shp_glim  = (short)SHP_FIR(mcp->m_glim, tech_diff);
1082 }