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