]> git.pond.sub.org Git - empserver/blob - src/lib/subs/shpsub.c
(sd, shp_missile_defense): Replace getvec() by direct, read-only item
[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 "misc.h"
36 #include "queue.h"
37 #include "player.h"
38 #include "var.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     register struct emp_qelem *qp;
194     register 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 = vl_find(V_SHELL, mlp->mcp->m_vtype,
253                       mlp->mcp->m_vamt, (int)mlp->mcp->m_nv);
254         shells = mlp->ship.shp_item[I_SHELL];
255         for (m = 0; mines > 0 && m < 5; m++) {
256             if (chance(0.66)) {
257                 mpr(actor, "Sweep...\n");
258                 mines--;
259                 shells = min(max, shells + 1);
260                 changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
261             }
262         }
263         sect.sct_mines = mines;
264         mlp->ship.shp_item[I_SHELL] = shells;
265         if (shp_check_one_mines(mlp)) {
266             stopping = 1;
267             emp_remque(qp);
268             free((s_char *)qp);
269         }
270         putship(mlp->ship.shp_uid, &mlp->ship);
271         putsect(&sect);
272     }
273     if (changed)
274         writemap(actor);
275     return stopping;
276 }
277
278 static int
279 shp_check_one_mines(struct mlist *mlp)
280 {
281     struct sctstr sect;
282     int changed = 0;
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         changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
295         if (changed)
296             writemap(actor);
297         putsect(&sect);
298         putship(mlp->ship.shp_uid, (s_char *)&mlp->ship);
299         if (!mlp->ship.shp_own)
300             return 1;
301     }
302     return 0;
303 }
304
305 static int
306 shp_check_mines(struct emp_qelem *ship_list)
307 {
308     struct emp_qelem *qp;
309     struct emp_qelem *next;
310     struct mlist *mlp;
311     int stopping = 0;
312
313     for (qp = ship_list->q_back; qp != ship_list; qp = next) {
314         next = qp->q_back;
315         mlp = (struct mlist *)qp;
316         if (shp_check_one_mines(mlp)) {
317             stopping = 1;
318             emp_remque(qp);
319             free((s_char *)qp);
320         }
321     }
322     return stopping;
323 }
324
325 void
326 shp_list(struct emp_qelem *ship_list)
327 {
328     struct emp_qelem *qp;
329     struct emp_qelem *next;
330     struct mlist *mlp;
331     struct shpstr *shp;
332
333     pr("shp#     ship type       x,y   fl  eff mil  sh gun pn he xl ln mob tech\n");
334
335     for (qp = ship_list->q_back; qp != ship_list; qp = next) {
336         next = qp->q_back;
337         mlp = (struct mlist *)qp;
338         shp = &mlp->ship;
339         pr("%4d ", shp->shp_uid);
340         pr("%-16.16s ", mlp->mcp->m_name);
341         prxy("%4d,%-4d ", shp->shp_x, shp->shp_y, mlp->ship.shp_own);
342         pr("%1c", shp->shp_fleet);
343         pr("%4d%%", shp->shp_effic);
344         pr("%4d", shp->shp_item[I_MILIT]);
345         pr("%4d", shp->shp_item[I_SHELL]);
346         pr("%4d", shp->shp_item[I_GUN]);
347         count_planes(shp);
348         pr("%3d", shp->shp_nplane);
349         pr("%3d", shp->shp_nchoppers);
350         pr("%3d", shp->shp_nxlight);
351         count_units(shp);
352         pr("%3d", shp->shp_nland);
353         pr("%4d", shp->shp_mobil);
354         pr("%4d\n", shp->shp_tech);
355     }
356 }
357
358 static void
359 shp_mess(s_char *str, struct mlist *mlp)
360 {
361     mpr(mlp->ship.shp_own, "%s %s & stays in %s\n",
362         prship(&mlp->ship),
363         str, xyas(mlp->ship.shp_x, mlp->ship.shp_y, mlp->ship.shp_own));
364     mlp->ship.shp_mobil = (int)mlp->mobil;
365     putship(mlp->ship.shp_uid, &mlp->ship);
366     emp_remque((struct emp_qelem *)mlp);
367     free((s_char *)mlp);
368 }
369
370 static int
371 shp_check_nav(struct sctstr *sect)
372 {
373     switch (dchr[sect->sct_type].d_flg & 03) {
374     case NAVOK:
375         break;
376
377     case NAV_02:
378         if (sect->sct_effic < 2)
379             return CN_CONSTRUCTION;
380         break;
381     case NAV_60:
382         if (sect->sct_effic < 60)
383             return CN_CONSTRUCTION;
384         break;
385     default:
386         return CN_LANDLOCKED;
387     }
388     return CN_NAVIGABLE;
389 }
390
391 static int
392 shp_count(struct emp_qelem *list, int wantflags, int nowantflags, int x,
393           int y)
394 {
395     struct emp_qelem *qp;
396     struct emp_qelem *next;
397     struct mlist *mlp;
398     int count = 0;
399
400     for (qp = list->q_back; qp != list; qp = next) {
401         next = qp->q_back;
402         mlp = (struct mlist *)qp;
403         if (mlp->ship.shp_x != x || mlp->ship.shp_y != y)
404             continue;
405         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
406             continue;
407         if (nowantflags && mlp->mcp->m_flags & nowantflags)
408             continue;
409         ++count;
410     }
411     return count;
412 }
413
414 static void
415 shp_damage_one(struct mlist *mlp, int dam)
416 {
417     shipdamage(&mlp->ship, dam);
418     putship(mlp->ship.shp_uid, &mlp->ship);
419     if (!mlp->ship.shp_own) {
420         emp_remque((struct emp_qelem *)mlp);
421         free((s_char *)mlp);
422     }
423 }
424
425 static int
426 shp_damage(struct emp_qelem *list, int totdam, int wantflags,
427            int nowantflags, int x, int y)
428 {
429     struct emp_qelem *qp;
430     struct emp_qelem *next;
431     struct mlist *mlp;
432     int dam;
433     int count;
434
435     if (!totdam
436         || !(count = shp_count(list, wantflags, nowantflags, x, y)))
437         return 0;
438     dam = ldround(((double)totdam / (double)count), 1);
439     for (qp = list->q_back; qp != list; qp = next) {
440         next = qp->q_back;
441         mlp = (struct mlist *)qp;
442         if (mlp->ship.shp_x != x || mlp->ship.shp_y != y)
443             continue;
444         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
445             continue;
446         if (nowantflags && mlp->mcp->m_flags & nowantflags)
447             continue;
448         shp_damage_one(mlp, dam);
449     }
450     return dam;
451 }
452
453 static int
454 shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
455              int nowantflags)
456 {
457     struct emp_qelem *qp;
458     struct emp_qelem *next;
459     struct mlist *mlp;
460
461     for (qp = list->q_back; qp != list; qp = next) {
462         next = qp->q_back;
463         mlp = (struct mlist *)qp;
464 /* If the ship isn't in the requested sector, then continue */
465         if (newx != mlp->ship.shp_x || newy != mlp->ship.shp_y)
466             continue;
467         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
468             continue;
469         if (nowantflags && mlp->mcp->m_flags & nowantflags)
470             continue;
471         return 1;
472     }
473     return 0;
474 }
475
476 static struct mlist *
477 most_valuable_ship(struct emp_qelem *list)
478 {
479     struct emp_qelem *qp;
480     struct emp_qelem *next;
481     struct mlist *mlp;
482     struct mlist *mvs = 0;
483
484     for (qp = list->q_back; qp != list; qp = next) {
485         next = qp->q_back;
486         mlp = (struct mlist *)qp;
487         if (mlp->mcp->m_flags & M_SUB)
488             continue;
489         if (!mlp->mcp->m_nxlight &&
490             !mlp->mcp->m_nchoppers &&
491             mlp->mcp->m_cost < 1000 &&
492             !mlp->mcp->m_nplanes && !mlp->mcp->m_nland)
493             continue;
494         if (!mvs) {
495             mvs = mlp;
496             continue;
497         }
498         if (mlp->mcp->m_cost * mlp->ship.shp_effic >
499             mvs->mcp->m_cost * mvs->ship.shp_effic)
500             mvs = mlp;
501     }
502     return mvs;
503 }
504
505 static int
506 shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
507 {
508     struct emp_qelem *qp;
509     struct emp_qelem *next;
510     struct mlist *mlp;
511     int hard;
512     int easiest = 9876;         /* things start great for victim */
513     int count = 0;
514
515     for (qp = list->q_back; qp != list; qp = next) {
516         next = qp->q_back;
517         mlp = (struct mlist *)qp;
518         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
519             continue;
520         if (nowantflags && mlp->mcp->m_flags & nowantflags)
521             continue;
522         hard = shp_hardtarget(&mlp->ship);
523         if (hard < easiest)
524             easiest = hard;     /* things get worse for victim */
525         ++count;
526     }
527     return easiest - count;
528 }
529
530 static int
531 shp_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
532                          natid victim)
533 {
534     int dam;
535     int twotries;
536     int stopping = 0;
537     struct emp_qelem msl_list, *qp, *newqp;
538     struct mlist *mvs;
539     s_char what[512];
540
541     msl_sel(&msl_list, newx, newy, victim, P_T | P_MAR, 0, MI_INTERDICT);
542
543     twotries = 0;
544     while (!QEMPTY(&msl_list) && (mvs = most_valuable_ship(list))) {
545         sprintf(what, "%s", prship(&mvs->ship));
546         dam = msl_launch_mindam(&msl_list, newx, newy,
547                                 shp_hardtarget(&mvs->ship),
548                                 EF_SHIP, 1, what, victim, MI_INTERDICT);
549         if (dam) {
550             mpr(victim,
551                 "missile interdiction mission does %d damage to %s!\n",
552                 dam, what);
553             shp_damage_one(mvs, dam);
554             twotries = 0;
555             stopping |= 1;
556         } else if (++twotries >= 2) {
557             break;
558         }
559     }
560     qp = msl_list.q_forw;
561     while (qp != msl_list.q_forw) {
562         newqp = qp->q_forw;
563         emp_remque(qp);
564         free(qp);
565         qp = newqp;
566     }
567
568     return stopping;
569 }
570
571 /* Note that this function has a side effect - it uses coastwatch
572  * ranges to see if it should fire upon a ship.  So, this function
573  * is expected to return positive if a ship is in range, and 0 if a
574  * ship is not in range. */
575 static int
576 notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
577 {
578     struct emp_qelem *qp;
579     struct emp_qelem *next;
580     struct mlist *mlp;
581     struct natstr *natp;
582     int vrange;
583
584     natp = getnatp(sectp->sct_own);
585
586     vrange = sectp->sct_type == SCT_RADAR ? 14 : 4;
587     vrange *= tfact(sectp->sct_own, 1.0) * sectp->sct_effic / 100.0;
588
589     if (vrange < 1)
590         vrange = 1;
591
592     if (vrange < trange)
593         return 0;
594
595     /* We got here, so we could theoretically see the ship.  Now,
596      * do we want to see it in our telebox? If not, return positive
597      * since we could see the ship and want forts to fire. */
598     if (!(natp->nat_flags & NF_COASTWATCH))
599         return 1;
600
601     for (qp = list->q_back; qp != list; qp = next) {
602         next = qp->q_back;
603         mlp = (struct mlist *)qp;
604         if (mlp->mcp->m_flags & M_SUB)
605             continue;
606         wu(0, sectp->sct_own,
607            "%s %s sighted at %s\n",
608            cname(mlp->ship.shp_own),
609            prship(&mlp->ship),
610            xyas(mlp->ship.shp_x, mlp->ship.shp_y, sectp->sct_own));
611         if (opt_HIDDEN) {
612             setcont(sectp->sct_own, mlp->ship.shp_own, FOUND_LOOK);
613         }
614     }
615
616     return 1;
617 }
618
619 static int
620 shp_fort_interdiction(struct emp_qelem *list, coord newx, coord newy,
621                       natid victim)
622 {
623     struct nstr_sect ns;
624     struct sctstr fsect;
625     int trange;
626     double range, range2, guneff;
627     int shell, gun;
628     int dam;
629     int totdam = 0;
630     s_char notified[MAXNOC];
631     int i;
632
633     /* Inform neutral and worse */
634     for (i = 0; i < MAXNOC; ++i) {
635         if (getrel(getnatp(i), victim) <= NEUTRAL)
636             notified[i] = 0;
637         else
638             notified[i] = 1;
639     }
640
641     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
642     while (nxtsct(&ns, &fsect)) {
643         if (!fsect.sct_own)
644             continue;
645         if (fsect.sct_own == victim)
646             continue;
647         if (notified[fsect.sct_own])
648             continue;
649         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
650         if (notify_coastguard(list, trange, &fsect))
651             notified[fsect.sct_own] = 1;
652     }
653     if (opt_NO_FORT_FIRE)
654         return 0;               /* Only coastwatch notify in nofortfire */
655     /* Only fire at Hostile ships */
656     for (i = 0; i < MAXNOC; ++i) {
657         if (getrel(getnatp(i), victim) >= NEUTRAL)
658             notified[i] = 0;
659     }
660     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
661     while (nxtsct(&ns, &fsect)) {
662         if (!notified[fsect.sct_own])
663             continue;
664         if (fsect.sct_type != SCT_FORTR)
665             continue;
666         gun = fsect.sct_item[I_GUN];
667         if (gun < 1)
668             continue;
669         range = tfactfire(fsect.sct_own, (double)min(gun, 7));
670         if (fsect.sct_effic > 59)
671             range++;
672         range2 = roundrange(range);
673         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
674         if (trange > range2)
675             continue;
676         if (fsect.sct_item[I_MILIT] < 5)
677             continue;
678         shell = fsect.sct_item[I_SHELL];
679         if (shell < 1)
680             shell += supply_commod(fsect.sct_own,
681                                    fsect.sct_x, fsect.sct_y, I_SHELL, 1);
682         if (shell < 1)
683             continue;
684         shell--;
685         fsect.sct_item[I_SHELL] = shell;
686         putsect(&fsect);
687         if (gun > 7)
688             gun = 7;
689         guneff = landgun((int)fsect.sct_effic, gun);
690         dam = (int)guneff;
691         totdam += dam;
692         mpr(victim, "Incoming fire does %d damage!\n", dam);
693 /*
694   mpr(victim, "%s fires at you for %d!\n",
695   xyas(fsect.sct_x,fsect.sct_y,victim),
696   dam);
697 */
698         wu(0, fsect.sct_own,
699            "%s fires at %s ships in %s for %d!\n",
700            xyas(fsect.sct_x, fsect.sct_y,
701                 fsect.sct_own),
702            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
703         nreport(fsect.sct_own, N_SHP_SHELL, victim, 1);
704     }
705     if (totdam > 0)
706         return shp_damage(list, totdam, 0, M_SUB, newx, newy);
707     return 0;
708 }
709
710 static int
711 shp_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
712 {
713     int stopping = 0;
714
715     if (shp_contains(list, newx, newy, 0, M_SUB)) {
716         stopping |= shp_fort_interdiction(list, newx, newy, victim);
717
718         if (shp_contains(list, newx, newy, 0, M_SUB)) {
719             stopping |=
720                 shp_damage(list,
721                            unit_interdict(newx, newy, victim, "ships",
722                                           shp_easiest_target(list, 0,
723                                                              M_SUB),
724                                           MI_INTERDICT), 0, M_SUB, newx,
725                            newy);
726             if (most_valuable_ship(list)) {
727                 stopping |=
728                     shp_missile_interdiction(list, newx, newy, victim);
729             }
730         }
731     }
732     if (shp_contains(list, newx, newy, M_SUB, 0)) {
733         stopping |=
734             shp_damage(list,
735                        unit_interdict(newx, newy, victim, "subs",
736                                       shp_easiest_target(list, M_SUB, 0),
737                                       MI_SINTERDICT), M_SUB, 0, newx,
738                        newy);
739     }
740     return stopping;
741 }
742
743 /* high value of hardtarget is harder to hit */
744 int
745 shp_hardtarget(struct shpstr *sp)
746 {
747     struct sctstr sect;
748     int vis, onsea;
749     struct mchrstr *mcp = mchr + sp->shp_type;
750
751     vis = sp->shp_visib;
752     getsect(sp->shp_x, sp->shp_y, &sect);
753     onsea = (sect.sct_type == SCT_WATER);
754     if (mcp->m_flags & M_SUB)
755         vis *= 4;
756     return (int)(((double)sp->shp_effic / 100.0) *
757                  (20 + (double)sp->shp_speed * onsea / 2.0 - vis));
758 }
759
760 static int
761 shp_hit_mine(struct shpstr *sp, struct mchrstr *mcp)
762 {
763     double m;
764
765     mpr(sp->shp_own, "Kawhomp! Mine detected in %s!\n",
766         xyas(sp->shp_x, sp->shp_y, sp->shp_own));
767
768     nreport(sp->shp_own, N_HIT_MINE, 0, 1);
769
770     m = 22.0 + (double)(random() % 21);
771     if (mcp->m_flags & M_SWEEP)
772         m /= 2.0;
773
774     shipdamage(sp, ldround(m, 1));
775
776     return (int)m;
777 }
778
779 void
780 shp_view(struct emp_qelem *list)
781 {
782     struct sctstr sect;
783     struct emp_qelem *qp;
784     struct emp_qelem *next;
785     struct mlist *mlp;
786
787     for (qp = list->q_back; qp != list; qp = next) {
788         next = qp->q_back;
789         mlp = (struct mlist *)qp;
790         getsect(mlp->ship.shp_x, mlp->ship.shp_y, &sect);
791         if (mlp->mcp->m_flags & M_FOOD)
792             mpr(mlp->ship.shp_own, "[fert:%d] ", sect.sct_fertil);
793         if (mlp->mcp->m_flags & M_OIL)
794             mpr(mlp->ship.shp_own, "[oil:%d] ", sect.sct_oil);
795         mpr(mlp->ship.shp_own, "%s @ %s %d%% %s\n",
796             prship(&mlp->ship),
797             xyas(mlp->ship.shp_x, mlp->ship.shp_y, player->cnum),
798             sect.sct_effic, dchr[sect.sct_type].d_name);
799     }
800 }
801
802 int
803 shp_nav_one_sector(struct emp_qelem *list, int dir, natid actor,
804                    int together)
805 {
806     struct sctstr sect;
807     struct emp_qelem *qp;
808     struct emp_qelem *next;
809     struct emp_qelem *nqp;
810     struct emp_qelem *nnext;
811     struct mlist *mlp;
812     coord dx;
813     coord dy;
814     coord newx;
815     coord newy;
816     int stopping = 0;
817     double mobcost;
818     double tech;                /* for mapping */
819     double tf;                  /* for mapping */
820     s_char dp[80];
821
822     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
823         shp_put(list, actor);
824         return 1;
825     }
826     dx = diroff[dir][0];
827     dy = diroff[dir][1];
828     for (qp = list->q_back; qp != list; qp = next) {
829         next = qp->q_back;
830         mlp = (struct mlist *)qp;
831         newx = xnorm(mlp->ship.shp_x + dx);
832         newy = ynorm(mlp->ship.shp_y + dy);
833         getsect(newx, newy, &sect);
834         if (shp_check_nav(&sect) != CN_NAVIGABLE ||
835             (sect.sct_own && actor != sect.sct_own &&
836              getrel(getnatp(sect.sct_own), actor) < FRIENDLY)) {
837             if (together) {
838                 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
839                 return 2;
840             } else {
841                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
842                 shp_mess(dp, mlp);
843                 continue;
844             }
845         }
846         if (opt_BIG_CITY && sect.sct_type == SCT_CAPIT) {
847             if (mlp->mcp->m_lcm + 2 * mlp->mcp->m_hcm >= 60) {
848                 sprintf(dp,
849                         "is too large to fit into the canal system at %s",
850                         xyas(newx, newy, actor));
851                 shp_mess(dp, mlp);
852                 continue;
853             }
854         }
855         if (mlp->mobil <= 0.0) {
856             shp_mess("is out of mobility", mlp);
857             continue;
858         }
859         mobcost = mlp->ship.shp_effic * 0.01 * mlp->ship.shp_speed;
860         mobcost = 480.0 /
861             (mobcost + techfact(mlp->ship.shp_tech, mobcost));
862         mlp->ship.shp_x = newx;
863         mlp->ship.shp_y = newy;
864         if (mlp->mobil - mobcost < -127) {
865             mlp->mobil = -127;
866         } else {
867             mlp->mobil -= mobcost;
868         }
869         mlp->ship.shp_mobil = (int)mlp->mobil;
870         putship(mlp->ship.shp_uid, &mlp->ship);
871         mlp->done = 0;          /* We haven't interdicted this ship yet */
872
873         /* Now update the map for this ship */
874         tech = techfact(mlp->ship.shp_tech, (double)mlp->mcp->m_vrnge);
875         if (mlp->mcp->m_flags & M_SONAR)
876             tf = techfact(mlp->ship.shp_tech, 1.0);
877         else
878             tf = 0.0;
879         radmapupd(mlp->ship.shp_own, mlp->ship.shp_x, mlp->ship.shp_y,
880                   (int)mlp->ship.shp_effic, (int)tech, tf);
881     }
882     if (QEMPTY(list))
883         return stopping;
884     stopping |= shp_sweep(list, 0, actor);
885     if (QEMPTY(list))
886         return stopping;
887     stopping |= shp_check_mines(list);
888     if (QEMPTY(list))
889         return stopping;
890
891 /* Ok, run through each ship and interdict each coordinate */
892     for (qp = list->q_back; qp != list; qp = next) {
893         next = qp->q_back;
894         mlp = (struct mlist *)qp;
895 /* Has this ship been interdicted yet? */
896         if (mlp->done)
897             continue;
898         newx = mlp->ship.shp_x;
899         newy = mlp->ship.shp_y;
900         stopping |= shp_interdict(list, newx, newy, actor);
901         if (QEMPTY(list))
902             return stopping;
903 /* Now, set all ships in this coordinate to done */
904         for (nqp = list->q_back; nqp != list; nqp = nnext) {
905             nnext = nqp->q_back;
906             mlp = (struct mlist *)nqp;
907             if (mlp->ship.shp_x == newx && mlp->ship.shp_y == newy)
908                 mlp->done = 1;
909         }
910     }
911
912     return stopping;
913 }
914
915 /*
916  * shp_miss_defence 
917  * Check for incoming missiles with a P_MAR flag. 
918  * Return True=1 if the missile was shotdown.
919  * Or False=0
920  * 
921  * Chad Zabel, July 95
922  */
923
924 int
925 shp_missile_defense(coord dx, coord dy, natid bombown, int hardtarget)
926 {
927     struct nstr_item ni;
928     struct shpstr ship;
929     int hitchance;
930     int shell;
931     double gun, eff, teff;
932
933     snxtitem_dist(&ni, EF_SHIP, dx, dy, 1);
934
935     while (nxtitem(&ni, (caddr_t)&ship)) {
936         if (!ship.shp_own)
937             continue;
938
939         if (!(mchr[(int)ship.shp_type].m_flags & M_ANTIMISSILE))
940             continue;
941
942         if (getrel(getnatp(ship.shp_own), bombown) >= NEUTRAL)
943             continue;
944
945         if (ship.shp_effic < 60)
946             continue;
947
948         shell = ship.shp_item[I_SHELL];
949         if (ship.shp_item[I_MILIT] < 1) /* do we have mil? */
950             continue;
951         if (shell < 2) {        /* do we need shells */
952             shell += supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
953                                    I_SHELL, 2);
954             if (shell < 2)
955                 continue;
956         }
957         if (ship.shp_item[I_GUN] < 1)   /* we need at least 1 gun */
958             continue;
959
960         /* now calculate the odds */
961         gun = min(ship.shp_item[I_GUN], ship.shp_glim);
962         eff = (double)ship.shp_effic / 100.0;
963         teff =
964             (((double)ship.shp_tech) / (((double)ship.shp_tech) + 200.0));
965         /* raise 4.5 for better interception -KHS */
966         hitchance = (int)(gun * eff * teff * 4.5) - hardtarget;
967         if (hitchance < 0)
968             hitchance = 0;
969         if (hitchance > 100)
970             hitchance = 100;
971
972         mpr(bombown, "%s anti-missile system activated...",
973             cname(ship.shp_own));
974         mpr(ship.shp_own, "Ship #%i anti-missile system activated!\n",
975             ship.shp_uid);
976         mpr(ship.shp_own, "%d%% hitchance...", hitchance);
977         /* use ammo */
978         ship.shp_item[I_SHELL] = shell - 2;
979         putship(ship.shp_uid, &ship);
980
981         if (roll(100) <= hitchance) {
982             mpr(bombown, "KABOOOM!! Missile destroyed\n\n");
983             mpr(ship.shp_own,
984                 "KABOOOM!!  Incoming missile destroyed!\n\n");
985             return 1;
986         } else {
987             mpr(bombown, "SWOOSH!!  anti-missile system failed!!\n");
988             mpr(ship.shp_own,
989                 "SWOOSH!!  Missile evades anti-missile systems\n\n");
990         }
991     }
992     return 0;                   /* all attempts failed */
993 }
994
995 s_char *
996 shp_path(int together, struct shpstr *shp, s_char *buf)
997 {
998     coord destx;
999     coord desty;
1000     struct sctstr d_sect;
1001     s_char *cp;
1002
1003     if (!sarg_xy(buf, &destx, &desty))
1004         return 0;
1005     if (!together) {
1006         mpr(shp->shp_own,
1007             "Cannot go to a destination sector if not all starting in the same sector\n");
1008         return 0;
1009     }
1010     if (!getsect(destx, desty, &d_sect)) {
1011         mpr(shp->shp_own, "%d,%d is not a sector\n", destx, desty);
1012         return 0;
1013     }
1014
1015     cp = (s_char *)BestShipPath(buf, shp->shp_x, shp->shp_y,
1016                                 d_sect.sct_x, d_sect.sct_y, player->cnum);
1017     if (!cp || shp->shp_mobil <= 0) {
1018         mpr(shp->shp_own, "Can't get to '%s' right now.\n",
1019             xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
1020         return 0;
1021     }
1022     return cp;
1023 }
1024
1025 /* Fire missiles at a ship which has fired shells */
1026 void
1027 shp_missdef(struct shpstr *sp, natid victim)
1028 {
1029     struct emp_qelem list;
1030     struct mlist *mlp;
1031     int eff;
1032     s_char buf[512];
1033
1034     emp_initque(&list);
1035
1036     mlp = (struct mlist *)malloc(sizeof(struct mlist));
1037     mlp->mcp = &mchr[(int)sp->shp_type];
1038     mlp->ship = *sp;
1039     mlp->mobil = (double)sp->shp_mobil;
1040     emp_insque(&mlp->queue, &list);
1041     sprintf(buf, "%s", prship(&mlp->ship));
1042
1043     eff = sp->shp_effic;
1044     if (most_valuable_ship(&list)) {
1045         shp_missile_interdiction(&list, sp->shp_x, sp->shp_y, sp->shp_own);
1046         getship(sp->shp_uid, sp);
1047
1048         if (!sp->shp_own) {
1049             wu(0, victim,
1050                "missiles launched in defense did 100%% damage to %s\n",
1051                buf);
1052             wu(0, victim, "%s sunk!\n", buf);
1053         } else if (eff > 0 && sp->shp_effic < eff) {
1054             wu(0, victim,
1055                "missiles launched in defense did %d%% damage to %s\n",
1056                100 * (eff - sp->shp_effic) / eff, buf);
1057         }
1058     }
1059     if (!QEMPTY(&list))
1060         free(mlp);
1061 }