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