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