]> git.pond.sub.org Git - empserver/blob - src/lib/subs/shpsub.c
Declare all configuration variables in optlist.h. Include that
[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 static int shp_check_nav(struct sctstr *);
60 static int shp_check_one_mines(struct mlist *);
61 static int shp_hit_mine(struct shpstr *, struct mchrstr *);
62 static void shp_mess(s_char *, struct mlist *);
63
64 void
65 shp_sel(struct nstr_item *ni, struct emp_qelem *list)
66
67
68     /*  int     wantflags;
69        int      nowantflags;
70      */
71 {
72     struct shpstr ship;
73     struct mchrstr *mcp;
74     struct mlist *mlp;
75
76     emp_initque(list);
77     while (nxtitem(ni, (s_char *)&ship)) {
78         if (!player->owner)
79             continue;
80         mcp = &mchr[(int)ship.shp_type];
81         /* if (wantflags && (mcp->m_flags & wantflags) != wantflags)
82            continue;
83            if (nowantflags && mcp->m_flags & nowantflags)
84            continue;
85          */
86         if (opt_MARKET) {
87             if (ontradingblock(EF_SHIP, (int *)&ship)) {
88                 pr("ship #%d inelligible - it's for sale.\n",
89                    ship.shp_uid);
90                 continue;
91             }
92         }
93         /* This abuse is better fixed by building a ship with the normal negative
94            mobility that everything else is built with */
95 /*
96         if (opt_MOB_ACCESS) {
97           if (ship.shp_effic < 21 &&
98             ship.shp_mobil < etu_per_update) {
99             pr("%s needs at least %d mob to navigate.\n",
100                prship(&ship), etu_per_update);
101             continue;
102           }
103         }
104 */
105         ship.shp_mission = 0;
106         ship.shp_rflags = 0;
107         memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
108         putship(ship.shp_uid, &ship);
109         mlp = (struct mlist *)malloc(sizeof(struct mlist));
110         mlp->mcp = mcp;
111         mlp->ship = ship;
112         mlp->mobil = (double)ship.shp_mobil;
113         emp_insque(&mlp->queue, list);
114     }
115 }
116
117 /* This function assumes that the list was created by shp_sel */
118 void
119 shp_nav(struct emp_qelem *list, double *minmobp, double *maxmobp,
120         int *togetherp, natid actor)
121 {
122     extern int update_pending;
123     struct emp_qelem *qp;
124     struct emp_qelem *next;
125     struct mlist *mlp;
126     struct sctstr sect;
127     struct shpstr ship;
128     int vec[I_MAX + 1];
129     coord allx;
130     coord ally;
131     int first = 1;
132
133     *minmobp = 9876.0;
134     *maxmobp = -9876.0;
135     *togetherp = 1;
136     for (qp = list->q_back; qp != list; qp = next) {
137         next = qp->q_back;
138         mlp = (struct mlist *)qp;
139         getship(mlp->ship.shp_uid, &ship);
140         if (ship.shp_own != actor) {
141             mpr(actor, "%s was sunk at %s\n",
142                 prship(&ship), xyas(ship.shp_x, ship.shp_y, actor));
143             emp_remque((struct emp_qelem *)mlp);
144             free((s_char *)mlp);
145             continue;
146         }
147         if (opt_SAIL) {
148             if (*ship.shp_path && !update_pending) {
149                 shp_mess("has a sail path", mlp);
150                 mpr(actor, "Use `sail <#> -' to reset\n");
151                 continue;
152             }
153         }
154         /* check crew - uws don't count */
155         getvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
156         if (vec[I_MILIT] == 0 && vec[I_CIVIL] == 0) {
157             shp_mess("is crewless", mlp);
158             continue;
159         }
160         if (!getsect(ship.shp_x, ship.shp_y, &sect)) {
161             shp_mess("was sucked into the sky by a strange looking spaceship", mlp);    /* heh -KHS */
162             continue;
163         }
164         switch (shp_check_nav(&sect)) {
165         case CN_CONSTRUCTION:
166             shp_mess("is caught in a construction zone", mlp);
167             continue;
168         case CN_LANDLOCKED:
169             shp_mess("is landlocked", mlp);
170             break;
171         case CN_NAVIGABLE:
172             break;
173         case CN_ERROR:
174         default:
175             shp_mess("was just swallowed by a big green worm", mlp);
176             continue;
177         }
178         if (first) {
179             allx = ship.shp_x;
180             ally = ship.shp_y;
181             first = 0;
182         }
183         if (ship.shp_x != allx || ship.shp_y != ally)
184             *togetherp = 0;
185         if (ship.shp_mobil + 1 < (int)mlp->mobil) {
186             mlp->mobil = (double)ship.shp_mobil;
187         }
188         if (mlp->mobil < *minmobp)
189             *minmobp = mlp->mobil;
190         if (mlp->mobil > *maxmobp)
191             *maxmobp = mlp->mobil;
192         mlp->ship = ship;
193     }
194 }
195
196 void
197 shp_put(struct emp_qelem *list, natid actor)
198 {
199     register struct emp_qelem *qp;
200     register struct emp_qelem *newqp;
201     struct mlist *mlp;
202
203     qp = list->q_back;
204     while (qp != list) {
205         mlp = (struct mlist *)qp;
206         mpr(actor, "%s stopped at %s\n", prship(&mlp->ship),
207             xyas(mlp->ship.shp_x, mlp->ship.shp_y, mlp->ship.shp_own));
208         mlp->ship.shp_mobil = (int)mlp->mobil;
209         putship(mlp->ship.shp_uid, &mlp->ship);
210         newqp = qp->q_back;
211         emp_remque(qp);
212         free((s_char *)qp);
213         qp = newqp;
214     }
215 }
216
217 int
218 shp_sweep(struct emp_qelem *ship_list, int verbose, natid actor)
219 {
220     struct emp_qelem *qp;
221     struct emp_qelem *next;
222     struct mlist *mlp;
223     struct sctstr sect;
224     int mines, m, max, shells;
225     int changed = 0;
226     int stopping = 0;
227     double mobcost;
228
229     for (qp = ship_list->q_back; qp != ship_list; qp = next) {
230         next = qp->q_back;
231         mlp = (struct mlist *)qp;
232         if (!(mlp->mcp->m_flags & M_SWEEP)) {
233             if (verbose)
234                 mpr(actor, "%s doesn't have minesweeping capability!\n",
235                     prship(&mlp->ship));
236             continue;
237         }
238         if (mlp->mobil <= 0.0) {
239             if (verbose)
240                 mpr(actor, "%s is out of mobility!\n", prship(&mlp->ship));
241             continue;
242         }
243         getsect(mlp->ship.shp_x, mlp->ship.shp_y, &sect);
244         if (sect.sct_type != SCT_WATER) {
245             if (verbose)
246                 mpr(actor, "%s is not at sea.  No mines there!\n",
247                     prship(&mlp->ship));
248             continue;
249         }
250         mobcost = mlp->ship.shp_effic * 0.01 * mlp->ship.shp_speed;
251         mobcost = 480.0 / (mobcost +
252                            techfact(mlp->ship.shp_tech, mobcost));
253         mlp->mobil -= mobcost;
254         mlp->ship.shp_mobil = (int)mlp->mobil;
255         putship(mlp->ship.shp_uid, &mlp->ship);
256         if (!(mines = getvar(V_MINE, (s_char *)&sect, EF_SECTOR)))
257             continue;
258         max = vl_find(V_SHELL, mlp->mcp->m_vtype,
259                       mlp->mcp->m_vamt, (int)mlp->mcp->m_nv);
260         shells = getvar(V_SHELL, (s_char *)&mlp->ship, EF_SHIP);
261         for (m = 0; mines > 0 && m < 5; m++) {
262             if (chance(0.66)) {
263                 mpr(actor, "Sweep...\n");
264                 mines--;
265                 shells = min(max, shells + 1);
266                 changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
267             }
268         }
269         putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
270         putvar(V_SHELL, shells, (s_char *)&mlp->ship, EF_SHIP);
271         if (shp_check_one_mines(mlp)) {
272             stopping = 1;
273             emp_remque(qp);
274             free((s_char *)qp);
275         }
276         putship(mlp->ship.shp_uid, &mlp->ship);
277         putsect(&sect);
278     }
279     if (changed)
280         writemap(actor);
281     return stopping;
282 }
283
284 static int
285 shp_check_one_mines(struct mlist *mlp)
286 {
287     struct sctstr sect;
288     int mines;
289     int changed = 0;
290     int actor;
291
292     getsect(mlp->ship.shp_x, mlp->ship.shp_y, &sect);
293     if (sect.sct_type != SCT_WATER)
294         return 0;
295     if (!(mines = getvar(V_MINE, (s_char *)&sect, EF_SECTOR)))
296         return 0;
297     if (chance(DMINE_HITCHANCE(mines))) {
298         actor = mlp->ship.shp_own;
299         shp_hit_mine(&mlp->ship, mlp->mcp);
300         mines--;
301         changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
302         if (changed)
303             writemap(actor);
304         putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
305         putsect(&sect);
306         putship(mlp->ship.shp_uid, (s_char *)&mlp->ship);
307         if (!mlp->ship.shp_own)
308             return 1;
309     }
310     return 0;
311 }
312
313 static int
314 shp_check_mines(struct emp_qelem *ship_list)
315 {
316     struct emp_qelem *qp;
317     struct emp_qelem *next;
318     struct mlist *mlp;
319     int stopping = 0;
320
321     for (qp = ship_list->q_back; qp != ship_list; qp = next) {
322         next = qp->q_back;
323         mlp = (struct mlist *)qp;
324         if (shp_check_one_mines(mlp)) {
325             stopping = 1;
326             emp_remque(qp);
327             free((s_char *)qp);
328         }
329     }
330     return stopping;
331 }
332
333 void
334 shp_list(struct emp_qelem *ship_list)
335 {
336     struct emp_qelem *qp;
337     struct emp_qelem *next;
338     struct mlist *mlp;
339     struct shpstr *shp;
340     int vec[I_MAX + 1];
341
342     pr("shp#     ship type       x,y   fl  eff mil  sh gun pn he xl ln mob tech\n");
343
344     for (qp = ship_list->q_back; qp != ship_list; qp = next) {
345         next = qp->q_back;
346         mlp = (struct mlist *)qp;
347         shp = &mlp->ship;
348         pr("%4d ", shp->shp_uid);
349         pr("%-16.16s ", mlp->mcp->m_name);
350         prxy("%4d,%-4d ", shp->shp_x, shp->shp_y, mlp->ship.shp_own);
351         pr("%1c", shp->shp_fleet);
352         pr("%4d%%", shp->shp_effic);
353         getvec(VT_ITEM, vec, (s_char *)shp, EF_SHIP);
354         pr("%4d", vec[I_MILIT]);
355         pr("%4d", vec[I_SHELL]);
356         pr("%4d", vec[I_GUN]);
357         count_planes(shp);
358         pr("%3d", shp->shp_nplane);
359         pr("%3d", shp->shp_nchoppers);
360         pr("%3d", shp->shp_nxlight);
361         count_units(shp);
362         pr("%3d", shp->shp_nland);
363         pr("%4d", shp->shp_mobil);
364         pr("%4d\n", shp->shp_tech);
365     }
366 }
367
368 static void
369 shp_mess(s_char *str, struct mlist *mlp)
370 {
371     mpr(mlp->ship.shp_own, "%s %s & stays in %s\n",
372         prship(&mlp->ship),
373         str, xyas(mlp->ship.shp_x, mlp->ship.shp_y, mlp->ship.shp_own));
374     mlp->ship.shp_mobil = (int)mlp->mobil;
375     putship(mlp->ship.shp_uid, &mlp->ship);
376     emp_remque((struct emp_qelem *)mlp);
377     free((s_char *)mlp);
378 }
379
380 static int
381 shp_check_nav(struct sctstr *sect)
382 {
383     switch (dchr[sect->sct_type].d_flg & 03) {
384     case NAVOK:
385         break;
386
387     case NAV_02:
388         if (sect->sct_effic < 2)
389             return CN_CONSTRUCTION;
390         break;
391     case NAV_60:
392         if (sect->sct_effic < 60)
393             return CN_CONSTRUCTION;
394         break;
395     default:
396         return CN_LANDLOCKED;
397     }
398     return CN_NAVIGABLE;
399 }
400
401 static int
402 shp_count(struct emp_qelem *list, int wantflags, int nowantflags, int x,
403           int y)
404 {
405     struct emp_qelem *qp;
406     struct emp_qelem *next;
407     struct mlist *mlp;
408     int count = 0;
409
410     for (qp = list->q_back; qp != list; qp = next) {
411         next = qp->q_back;
412         mlp = (struct mlist *)qp;
413         if (mlp->ship.shp_x != x || mlp->ship.shp_y != y)
414             continue;
415         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
416             continue;
417         if (nowantflags && mlp->mcp->m_flags & nowantflags)
418             continue;
419         ++count;
420     }
421     return count;
422 }
423
424 static void
425 shp_damage_one(struct mlist *mlp, int dam)
426 {
427     shipdamage(&mlp->ship, dam);
428     putship(mlp->ship.shp_uid, &mlp->ship);
429     if (!mlp->ship.shp_own) {
430         emp_remque((struct emp_qelem *)mlp);
431         free((s_char *)mlp);
432     }
433 }
434
435 static int
436 shp_damage(struct emp_qelem *list, int totdam, int wantflags,
437            int nowantflags, int x, int y)
438 {
439     struct emp_qelem *qp;
440     struct emp_qelem *next;
441     struct mlist *mlp;
442     int dam;
443     int count;
444
445     if (!totdam
446         || !(count = shp_count(list, wantflags, nowantflags, x, y)))
447         return 0;
448     dam = ldround(((double)totdam / (double)count), 1);
449     for (qp = list->q_back; qp != list; qp = next) {
450         next = qp->q_back;
451         mlp = (struct mlist *)qp;
452         if (mlp->ship.shp_x != x || mlp->ship.shp_y != y)
453             continue;
454         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
455             continue;
456         if (nowantflags && mlp->mcp->m_flags & nowantflags)
457             continue;
458         shp_damage_one(mlp, dam);
459     }
460     return dam;
461 }
462
463 static int
464 shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
465              int nowantflags)
466 {
467     struct emp_qelem *qp;
468     struct emp_qelem *next;
469     struct mlist *mlp;
470
471     for (qp = list->q_back; qp != list; qp = next) {
472         next = qp->q_back;
473         mlp = (struct mlist *)qp;
474 /* If the ship isn't in the requested sector, then continue */
475         if (newx != mlp->ship.shp_x || newy != mlp->ship.shp_y)
476             continue;
477         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
478             continue;
479         if (nowantflags && mlp->mcp->m_flags & nowantflags)
480             continue;
481         return 1;
482     }
483     return 0;
484 }
485
486 static struct mlist *
487 most_valuable_ship(struct emp_qelem *list)
488 {
489     struct emp_qelem *qp;
490     struct emp_qelem *next;
491     struct mlist *mlp;
492     struct mlist *mvs = 0;
493
494     for (qp = list->q_back; qp != list; qp = next) {
495         next = qp->q_back;
496         mlp = (struct mlist *)qp;
497         if (mlp->mcp->m_flags & M_SUB)
498             continue;
499         if (!mlp->mcp->m_nxlight &&
500             !mlp->mcp->m_nchoppers &&
501             mlp->mcp->m_cost < 1000 &&
502             !mlp->mcp->m_nplanes && !mlp->mcp->m_nland)
503             continue;
504         if (!mvs) {
505             mvs = mlp;
506             continue;
507         }
508         if (mlp->mcp->m_cost * mlp->ship.shp_effic >
509             mvs->mcp->m_cost * mvs->ship.shp_effic)
510             mvs = mlp;
511     }
512     return mvs;
513 }
514
515 static int
516 shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
517 {
518     struct emp_qelem *qp;
519     struct emp_qelem *next;
520     struct mlist *mlp;
521     int hard;
522     int easiest = 9876;         /* things start great for victim */
523     int count = 0;
524
525     for (qp = list->q_back; qp != list; qp = next) {
526         next = qp->q_back;
527         mlp = (struct mlist *)qp;
528         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
529             continue;
530         if (nowantflags && mlp->mcp->m_flags & nowantflags)
531             continue;
532         hard = shp_hardtarget(&mlp->ship);
533         if (hard < easiest)
534             easiest = hard;     /* things get worse for victim */
535         ++count;
536     }
537     return easiest - count;
538 }
539
540 static int
541 shp_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
542                          natid victim)
543 {
544     int dam;
545     int twotries;
546     int stopping = 0;
547     struct emp_qelem msl_list, *qp, *newqp;
548     struct mlist *mvs;
549     s_char what[512];
550
551     msl_sel(&msl_list, newx, newy, victim, P_T | P_MAR, 0, MI_INTERDICT);
552
553     twotries = 0;
554     while (!QEMPTY(&msl_list) && (mvs = most_valuable_ship(list))) {
555         sprintf(what, "%s", prship(&mvs->ship));
556         dam = msl_launch_mindam(&msl_list, newx, newy,
557                                 shp_hardtarget(&mvs->ship),
558                                 EF_SHIP, 1, what, victim, MI_INTERDICT);
559         if (dam) {
560             mpr(victim,
561                 "missile interdiction mission does %d damage to %s!\n",
562                 dam, what);
563             shp_damage_one(mvs, dam);
564             twotries = 0;
565             stopping |= 1;
566         } else if (++twotries >= 2) {
567             break;
568         }
569     }
570     qp = msl_list.q_forw;
571     while (qp != msl_list.q_forw) {
572         newqp = qp->q_forw;
573         emp_remque(qp);
574         free(qp);
575         qp = newqp;
576     }
577
578     return stopping;
579 }
580
581 /* Note that this function has a side effect - it uses coastwatch
582  * ranges to see if it should fire upon a ship.  So, this function
583  * is expected to return positive if a ship is in range, and 0 if a
584  * ship is not in range. */
585 static int
586 notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
587 {
588     struct emp_qelem *qp;
589     struct emp_qelem *next;
590     struct mlist *mlp;
591     struct natstr *natp;
592     int vrange;
593
594     natp = getnatp(sectp->sct_own);
595
596     vrange = sectp->sct_type == SCT_RADAR ? 14 : 4;
597     vrange *= tfact(sectp->sct_own, 1.0) * sectp->sct_effic / 100.0;
598
599     if (vrange < 1)
600         vrange = 1;
601
602     if (vrange < trange)
603         return 0;
604
605     /* We got here, so we could theoretically see the ship.  Now,
606      * do we want to see it in our telebox? If not, return positive
607      * since we could see the ship and want forts to fire. */
608     if (!(natp->nat_flags & NF_COASTWATCH))
609         return 1;
610
611     for (qp = list->q_back; qp != list; qp = next) {
612         next = qp->q_back;
613         mlp = (struct mlist *)qp;
614         if (mlp->mcp->m_flags & M_SUB)
615             continue;
616         wu(0, sectp->sct_own,
617            "%s %s sighted at %s\n",
618            cname(mlp->ship.shp_own),
619            prship(&mlp->ship),
620            xyas(mlp->ship.shp_x, mlp->ship.shp_y, sectp->sct_own));
621         if (opt_HIDDEN) {
622             setcont(sectp->sct_own, mlp->ship.shp_own, FOUND_LOOK);
623         }
624     }
625
626     return 1;
627 }
628
629 static int
630 shp_fort_interdiction(struct emp_qelem *list, coord newx, coord newy,
631                       natid victim)
632 {
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 static 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 static 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     mlp->ship = *sp;
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 }