]> git.pond.sub.org Git - empserver/blob - src/lib/subs/shpsub.c
(ontradingblock, trdswitchown): Second parameter is a generic pointer,
[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(s_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 changed = 0;
283     int actor;
284
285     getsect(mlp->ship.shp_x, mlp->ship.shp_y, &sect);
286     if (sect.sct_type != SCT_WATER)
287         return 0;
288     if (!sect.sct_mines)
289         return 0;
290     if (chance(DMINE_HITCHANCE(sect.sct_mines))) {
291         actor = mlp->ship.shp_own;
292         shp_hit_mine(&mlp->ship, mlp->mcp);
293         sect.sct_mines--;
294         changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
295         if (changed)
296             writemap(actor);
297         putsect(&sect);
298         putship(mlp->ship.shp_uid, &mlp->ship);
299         if (!mlp->ship.shp_own)
300             return 1;
301     }
302     return 0;
303 }
304
305 static int
306 shp_check_mines(struct emp_qelem *ship_list)
307 {
308     struct emp_qelem *qp;
309     struct emp_qelem *next;
310     struct mlist *mlp;
311     int stopping = 0;
312
313     for (qp = ship_list->q_back; qp != ship_list; qp = next) {
314         next = qp->q_back;
315         mlp = (struct mlist *)qp;
316         if (shp_check_one_mines(mlp)) {
317             stopping = 1;
318             emp_remque(qp);
319             free(qp);
320         }
321     }
322     return stopping;
323 }
324
325 void
326 shp_list(struct emp_qelem *ship_list)
327 {
328     struct emp_qelem *qp;
329     struct emp_qelem *next;
330     struct mlist *mlp;
331     struct shpstr *shp;
332
333     pr("shp#     ship type       x,y   fl  eff mil  sh gun pn he xl ln mob tech\n");
334
335     for (qp = ship_list->q_back; qp != ship_list; qp = next) {
336         next = qp->q_back;
337         mlp = (struct mlist *)qp;
338         shp = &mlp->ship;
339         pr("%4d ", shp->shp_uid);
340         pr("%-16.16s ", mlp->mcp->m_name);
341         prxy("%4d,%-4d ", shp->shp_x, shp->shp_y, mlp->ship.shp_own);
342         pr("%c", shp->shp_fleet);
343         pr("%4d%%", shp->shp_effic);
344         pr("%4d", shp->shp_item[I_MILIT]);
345         pr("%4d", shp->shp_item[I_SHELL]);
346         pr("%4d", shp->shp_item[I_GUN]);
347         count_planes(shp);
348         pr("%3d", shp->shp_nplane);
349         pr("%3d", shp->shp_nchoppers);
350         pr("%3d", shp->shp_nxlight);
351         count_units(shp);
352         pr("%3d", shp->shp_nland);
353         pr("%4d", shp->shp_mobil);
354         pr("%4d\n", shp->shp_tech);
355     }
356 }
357
358 static void
359 shp_mess(s_char *str, struct mlist *mlp)
360 {
361     mpr(mlp->ship.shp_own, "%s %s & stays in %s\n",
362         prship(&mlp->ship),
363         str, xyas(mlp->ship.shp_x, mlp->ship.shp_y, mlp->ship.shp_own));
364     mlp->ship.shp_mobil = (int)mlp->mobil;
365     putship(mlp->ship.shp_uid, &mlp->ship);
366     emp_remque((struct emp_qelem *)mlp);
367     free(mlp);
368 }
369
370 int
371 shp_check_nav(struct sctstr *sect, struct shpstr *shp)
372 {
373     switch (dchr[sect->sct_type].d_nav) {
374     case NAVOK:
375         break;
376     case NAV_CANAL:
377         if (mchr[(int)shp->shp_type].m_flags & M_CANAL) {
378             if (sect->sct_effic < 2)
379                 return CN_CONSTRUCTION;
380         } else
381             return CN_LANDLOCKED;
382         break;
383     case NAV_02:
384         if (sect->sct_effic < 2)
385             return CN_CONSTRUCTION;
386         break;
387     case NAV_60:
388         if (sect->sct_effic < 60)
389             return CN_CONSTRUCTION;
390         break;
391     default:
392         return CN_LANDLOCKED;
393     }
394     return CN_NAVIGABLE;
395 }
396
397 int
398 sect_has_dock(struct sctstr *sect)
399 {
400     switch (dchr[sect->sct_type].d_nav) {
401     case NAV_02:
402         return 1;
403     default:
404         return 0;
405     }
406 }
407
408 static int
409 shp_count(struct emp_qelem *list, int wantflags, int nowantflags, int x,
410           int y)
411 {
412     struct emp_qelem *qp;
413     struct emp_qelem *next;
414     struct mlist *mlp;
415     int count = 0;
416
417     for (qp = list->q_back; qp != list; qp = next) {
418         next = qp->q_back;
419         mlp = (struct mlist *)qp;
420         if (mlp->ship.shp_x != x || mlp->ship.shp_y != y)
421             continue;
422         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
423             continue;
424         if (nowantflags && mlp->mcp->m_flags & nowantflags)
425             continue;
426         ++count;
427     }
428     return count;
429 }
430
431 static void
432 shp_damage_one(struct mlist *mlp, int dam)
433 {
434     shipdamage(&mlp->ship, dam);
435     putship(mlp->ship.shp_uid, &mlp->ship);
436     if (!mlp->ship.shp_own) {
437         emp_remque((struct emp_qelem *)mlp);
438         free(mlp);
439     }
440 }
441
442 static int
443 shp_damage(struct emp_qelem *list, int totdam, int wantflags,
444            int nowantflags, int x, int y)
445 {
446     struct emp_qelem *qp;
447     struct emp_qelem *next;
448     struct mlist *mlp;
449     int dam;
450     int count;
451
452     if (!totdam
453         || !(count = shp_count(list, wantflags, nowantflags, x, y)))
454         return 0;
455     dam = ldround(((double)totdam / (double)count), 1);
456     for (qp = list->q_back; qp != list; qp = next) {
457         next = qp->q_back;
458         mlp = (struct mlist *)qp;
459         if (mlp->ship.shp_x != x || mlp->ship.shp_y != y)
460             continue;
461         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
462             continue;
463         if (nowantflags && mlp->mcp->m_flags & nowantflags)
464             continue;
465         shp_damage_one(mlp, dam);
466     }
467     return dam;
468 }
469
470 static int
471 shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
472              int nowantflags)
473 {
474     struct emp_qelem *qp;
475     struct emp_qelem *next;
476     struct mlist *mlp;
477
478     for (qp = list->q_back; qp != list; qp = next) {
479         next = qp->q_back;
480         mlp = (struct mlist *)qp;
481 /* If the ship isn't in the requested sector, then continue */
482         if (newx != mlp->ship.shp_x || newy != mlp->ship.shp_y)
483             continue;
484         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
485             continue;
486         if (nowantflags && mlp->mcp->m_flags & nowantflags)
487             continue;
488         return 1;
489     }
490     return 0;
491 }
492
493 static struct mlist *
494 most_valuable_ship(struct emp_qelem *list)
495 {
496     struct emp_qelem *qp;
497     struct emp_qelem *next;
498     struct mlist *mlp;
499     struct mlist *mvs = 0;
500
501     for (qp = list->q_back; qp != list; qp = next) {
502         next = qp->q_back;
503         mlp = (struct mlist *)qp;
504         if (mlp->mcp->m_flags & M_SUB)
505             continue;
506         if (!mlp->mcp->m_nxlight &&
507             !mlp->mcp->m_nchoppers &&
508             mlp->mcp->m_cost < 1000 &&
509             !mlp->mcp->m_nplanes && !mlp->mcp->m_nland)
510             continue;
511         if (!mvs) {
512             mvs = mlp;
513             continue;
514         }
515         if (mlp->mcp->m_cost * mlp->ship.shp_effic >
516             mvs->mcp->m_cost * mvs->ship.shp_effic)
517             mvs = mlp;
518     }
519     return mvs;
520 }
521
522 static int
523 shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
524 {
525     struct emp_qelem *qp;
526     struct emp_qelem *next;
527     struct mlist *mlp;
528     int hard;
529     int easiest = 9876;         /* things start great for victim */
530     int count = 0;
531
532     for (qp = list->q_back; qp != list; qp = next) {
533         next = qp->q_back;
534         mlp = (struct mlist *)qp;
535         if (wantflags && (mlp->mcp->m_flags & wantflags) != wantflags)
536             continue;
537         if (nowantflags && mlp->mcp->m_flags & nowantflags)
538             continue;
539         hard = shp_hardtarget(&mlp->ship);
540         if (hard < easiest)
541             easiest = hard;     /* things get worse for victim */
542         ++count;
543     }
544     return easiest - count;
545 }
546
547 static int
548 shp_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
549                          natid victim)
550 {
551     int dam;
552     int twotries;
553     int stopping = 0;
554     struct emp_qelem msl_list, *qp, *newqp;
555     struct mlist *mvs;
556     s_char what[512];
557
558     msl_sel(&msl_list, newx, newy, victim, P_T | P_MAR, 0, MI_INTERDICT);
559
560     twotries = 0;
561     while (!QEMPTY(&msl_list) && (mvs = most_valuable_ship(list))) {
562         sprintf(what, "%s", prship(&mvs->ship));
563         dam = msl_launch_mindam(&msl_list, newx, newy,
564                                 shp_hardtarget(&mvs->ship),
565                                 EF_SHIP, 1, what, victim, MI_INTERDICT);
566         if (dam) {
567             mpr(victim,
568                 "missile interdiction mission does %d damage to %s!\n",
569                 dam, what);
570             shp_damage_one(mvs, dam);
571             twotries = 0;
572             stopping |= 1;
573         } else if (++twotries >= 2) {
574             break;
575         }
576     }
577     qp = msl_list.q_forw;
578     while (qp != msl_list.q_forw) {
579         newqp = qp->q_forw;
580         emp_remque(qp);
581         free(qp);
582         qp = newqp;
583     }
584
585     return stopping;
586 }
587
588 /* Note that this function has a side effect - it uses coastwatch
589  * ranges to see if it should fire upon a ship.  So, this function
590  * is expected to return positive if a ship is in range, and 0 if a
591  * ship is not in range. */
592 static int
593 notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
594 {
595     struct emp_qelem *qp;
596     struct emp_qelem *next;
597     struct mlist *mlp;
598     struct natstr *natp;
599     int vrange;
600
601     natp = getnatp(sectp->sct_own);
602
603     vrange = sectp->sct_type == SCT_RADAR ? 14 : 4;
604     vrange *= tfact(sectp->sct_own, 1.0) * sectp->sct_effic / 100.0;
605
606     if (vrange < 1)
607         vrange = 1;
608
609     if (vrange < trange)
610         return 0;
611
612     for (qp = list->q_back; qp != list; qp = next) {
613         next = qp->q_back;
614         mlp = (struct mlist *)qp;
615         if (mlp->mcp->m_flags & M_SUB)
616             continue;
617         if (natp->nat_flags & NF_COASTWATCH)
618             wu(0, sectp->sct_own,
619                "%s %s sighted at %s\n",
620                cname(mlp->ship.shp_own),
621                prship(&mlp->ship),
622                xyas(mlp->ship.shp_x, mlp->ship.shp_y, sectp->sct_own));
623         if (opt_HIDDEN)
624             setcont(sectp->sct_own, mlp->ship.shp_own, FOUND_COAST);
625     }
626
627     return 1;
628 }
629
630 static int
631 shp_fort_interdiction(struct emp_qelem *list, coord newx, coord newy,
632                       natid victim)
633 {
634     struct nstr_sect ns;
635     struct sctstr fsect;
636     int trange;
637     double range, range2, guneff;
638     int shell, gun;
639     int dam;
640     int totdam = 0;
641     s_char notified[MAXNOC];
642     int i;
643
644     /* Inform neutral and worse */
645     for (i = 0; i < MAXNOC; ++i) {
646         if (getrel(getnatp(i), victim) <= NEUTRAL)
647             notified[i] = 0;
648         else
649             notified[i] = 1;
650     }
651
652     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
653     while (nxtsct(&ns, &fsect)) {
654         if (!fsect.sct_own)
655             continue;
656         if (fsect.sct_own == victim)
657             continue;
658         if (notified[fsect.sct_own])
659             continue;
660         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
661         if (notify_coastguard(list, trange, &fsect))
662             notified[fsect.sct_own] = 1;
663     }
664     if (opt_NO_FORT_FIRE)
665         return 0;               /* Only coastwatch notify in nofortfire */
666     /* Only fire at Hostile ships */
667     for (i = 0; i < MAXNOC; ++i) {
668         if (getrel(getnatp(i), victim) >= NEUTRAL)
669             notified[i] = 0;
670     }
671     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
672     while (nxtsct(&ns, &fsect)) {
673         if (!notified[fsect.sct_own])
674             continue;
675         if (fsect.sct_type != SCT_FORTR)
676             continue;
677         gun = fsect.sct_item[I_GUN];
678         if (gun < 1)
679             continue;
680         range = tfactfire(fsect.sct_own, (double)MIN(gun, 7));
681         if (fsect.sct_effic > 59)
682             range++;
683         range2 = roundrange(range);
684         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
685         if (trange > range2)
686             continue;
687         if (fsect.sct_item[I_MILIT] < 5)
688             continue;
689         shell = fsect.sct_item[I_SHELL];
690         if (shell < 1)
691             shell += supply_commod(fsect.sct_own,
692                                    fsect.sct_x, fsect.sct_y, I_SHELL, 1);
693         if (shell < 1)
694             continue;
695         shell--;
696         fsect.sct_item[I_SHELL] = shell;
697         putsect(&fsect);
698         if (gun > 7)
699             gun = 7;
700         guneff = landgun((int)fsect.sct_effic, gun);
701         dam = (int)guneff;
702         totdam += dam;
703         mpr(victim, "Incoming fire does %d damage!\n", dam);
704 /*
705   mpr(victim, "%s fires at you for %d!\n",
706   xyas(fsect.sct_x,fsect.sct_y,victim),
707   dam);
708 */
709         wu(0, fsect.sct_own,
710            "%s fires at %s ships in %s for %d!\n",
711            xyas(fsect.sct_x, fsect.sct_y,
712                 fsect.sct_own),
713            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
714         nreport(fsect.sct_own, N_SHP_SHELL, victim, 1);
715     }
716     if (totdam > 0)
717         return shp_damage(list, totdam, 0, M_SUB, newx, newy);
718     return 0;
719 }
720
721 static int
722 shp_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
723 {
724     int stopping = 0;
725
726     if (shp_contains(list, newx, newy, 0, M_SUB)) {
727         stopping |= shp_fort_interdiction(list, newx, newy, victim);
728
729         if (shp_contains(list, newx, newy, 0, M_SUB)) {
730             stopping |=
731                 shp_damage(list,
732                            unit_interdict(newx, newy, victim, "ships",
733                                           shp_easiest_target(list, 0, M_SUB),
734                                           MI_INTERDICT),
735                            0, M_SUB, newx, 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),
748                        M_SUB, 0, newx, 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 mlist *mlp;
820     struct emp_qelem done;
821     coord dx;
822     coord dy;
823     coord newx;
824     coord newy;
825     int stopping = 0;
826     double mobcost;
827     double tech;                /* for mapping */
828     double tf;                  /* for mapping */
829     s_char dp[80];
830     int navigate;
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         navigate = shp_check_nav(&sect, &mlp->ship);
845         if (navigate != CN_NAVIGABLE ||
846             (sect.sct_own && actor != sect.sct_own &&
847              getrel(getnatp(sect.sct_own), actor) < FRIENDLY)) {
848             if (dchr[sect.sct_type].d_nav == NAV_CANAL &&
849                 !(mlp->mcp->m_flags & M_CANAL) &&
850                 navigate == CN_LANDLOCKED)
851                 sprintf(dp,
852                         "is too large to fit into the canal system at %s",
853                         xyas(newx, newy, actor));
854             else
855                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
856             if (together) {
857                 mpr(actor, "%s\n", dp);
858                 return 2;
859             } else {
860                 shp_mess(dp, mlp);
861                 continue;
862             }
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
882         /* Now update the map for this ship */
883         tech = techfact(mlp->ship.shp_tech, (double)mlp->mcp->m_vrnge);
884         if (mlp->mcp->m_flags & M_SONAR)
885             tf = techfact(mlp->ship.shp_tech, 1.0);
886         else
887             tf = 0.0;
888         radmapupd(mlp->ship.shp_own, mlp->ship.shp_x, mlp->ship.shp_y,
889                   (int)mlp->ship.shp_effic, (int)tech, tf);
890     }
891     if (QEMPTY(list))
892         return stopping;
893     stopping |= shp_sweep(list, 0, actor);
894     if (QEMPTY(list))
895         return stopping;
896     stopping |= shp_check_mines(list);
897     if (QEMPTY(list))
898         return stopping;
899
900     /* interdict ships sector by sector */
901     emp_initque(&done);
902     while (!QEMPTY(list)) {
903         mlp = (struct mlist *)list->q_back;
904         newx = mlp->ship.shp_x;
905         newy = mlp->ship.shp_y;
906         stopping |= shp_interdict(list, newx, newy, actor);
907         /* move survivors in this sector to done */
908         for (qp = list->q_back; qp != list; qp = next) {
909             next = qp->q_back;
910             mlp = (struct mlist *)qp;
911             if (mlp->ship.shp_x == newx && mlp->ship.shp_y == newy) {
912                 emp_remque(qp);
913                 emp_insque(qp, &done);
914             }
915         }
916     }
917     /* assign surviving ships back to list */
918     emp_insque(list, &done);
919     emp_remque(&done);
920
921     return stopping;
922 }
923
924 /*
925  * shp_miss_defence 
926  * Check for incoming missiles with a P_MAR flag. 
927  * Return True=1 if the missile was shotdown.
928  * Or False=0
929  * 
930  * Chad Zabel, July 95
931  */
932
933 int
934 shp_missile_defense(coord dx, coord dy, natid bombown, int hardtarget)
935 {
936     struct nstr_item ni;
937     struct shpstr ship;
938     int hitchance;
939     int shell;
940     double gun, eff, teff;
941
942     snxtitem_dist(&ni, EF_SHIP, dx, dy, 1);
943
944     while (nxtitem(&ni, &ship)) {
945         if (!ship.shp_own)
946             continue;
947
948         if (!(mchr[(int)ship.shp_type].m_flags & M_ANTIMISSILE))
949             continue;
950
951         if (getrel(getnatp(ship.shp_own), bombown) >= NEUTRAL)
952             continue;
953
954         if (ship.shp_effic < 60)
955             continue;
956
957         shell = ship.shp_item[I_SHELL];
958         if (ship.shp_item[I_MILIT] < 1) /* do we have mil? */
959             continue;
960         if (shell < 2) {        /* do we need shells */
961             shell += supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
962                                    I_SHELL, 2);
963             if (shell < 2)
964                 continue;
965         }
966         if (ship.shp_item[I_GUN] < 1)   /* we need at least 1 gun */
967             continue;
968
969         /* now calculate the odds */
970         gun = MIN(ship.shp_item[I_GUN], ship.shp_glim);
971         eff = (double)ship.shp_effic / 100.0;
972         teff =
973             (((double)ship.shp_tech) / (((double)ship.shp_tech) + 200.0));
974         /* raise 4.5 for better interception -KHS */
975         hitchance = (int)(gun * eff * teff * 4.5) - hardtarget;
976         if (hitchance < 0)
977             hitchance = 0;
978         if (hitchance > 100)
979             hitchance = 100;
980
981         mpr(bombown, "%s anti-missile system activated...",
982             cname(ship.shp_own));
983         mpr(ship.shp_own, "Ship #%i anti-missile system activated!\n",
984             ship.shp_uid);
985         mpr(ship.shp_own, "%d%% hitchance...", hitchance);
986         /* use ammo */
987         ship.shp_item[I_SHELL] = shell - 2;
988         putship(ship.shp_uid, &ship);
989
990         if (roll(100) <= hitchance) {
991             mpr(bombown, "KABOOOM!! Missile destroyed\n\n");
992             mpr(ship.shp_own,
993                 "KABOOOM!!  Incoming missile destroyed!\n\n");
994             return 1;
995         } else {
996             mpr(bombown, "SWOOSH!!  anti-missile system failed!!\n");
997             mpr(ship.shp_own,
998                 "SWOOSH!!  Missile evades anti-missile systems\n\n");
999         }
1000     }
1001     return 0;                   /* all attempts failed */
1002 }
1003
1004 s_char *
1005 shp_path(int together, struct shpstr *shp, s_char *buf)
1006 {
1007     coord destx;
1008     coord desty;
1009     struct sctstr d_sect;
1010     s_char *cp;
1011
1012     if (!sarg_xy(buf, &destx, &desty))
1013         return 0;
1014     if (!together) {
1015         mpr(shp->shp_own,
1016             "Cannot go to a destination sector if not all starting in the same sector\n");
1017         return 0;
1018     }
1019     if (!getsect(destx, desty, &d_sect)) {
1020         mpr(shp->shp_own, "%d,%d is not a sector\n", destx, desty);
1021         return 0;
1022     }
1023
1024     cp = BestShipPath(buf, shp->shp_x, shp->shp_y,
1025                       d_sect.sct_x, d_sect.sct_y, player->cnum);
1026     if (!cp || shp->shp_mobil <= 0) {
1027         mpr(shp->shp_own, "Can't get to '%s' right now.\n",
1028             xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
1029         return 0;
1030     }
1031     return cp;
1032 }
1033
1034 /* Fire missiles at a ship which has fired shells */
1035 void
1036 shp_missdef(struct shpstr *sp, natid victim)
1037 {
1038     struct emp_qelem list;
1039     struct mlist *mlp;
1040     int eff;
1041     s_char buf[512];
1042
1043     emp_initque(&list);
1044
1045     mlp = malloc(sizeof(struct mlist));
1046     mlp->mcp = &mchr[(int)sp->shp_type];
1047     mlp->ship = *sp;
1048     mlp->mobil = (double)sp->shp_mobil;
1049     emp_insque(&mlp->queue, &list);
1050     sprintf(buf, "%s", prship(&mlp->ship));
1051
1052     eff = sp->shp_effic;
1053     if (most_valuable_ship(&list)) {
1054         shp_missile_interdiction(&list, sp->shp_x, sp->shp_y, sp->shp_own);
1055         getship(sp->shp_uid, sp);
1056
1057         if (!sp->shp_own) {
1058             wu(0, victim,
1059                "missiles launched in defense did 100%% damage to %s\n",
1060                buf);
1061             wu(0, victim, "%s sunk!\n", buf);
1062         } else if (eff > 0 && sp->shp_effic < eff) {
1063             wu(0, victim,
1064                "missiles launched in defense did %d%% damage to %s\n",
1065                100 * (eff - sp->shp_effic) / eff, buf);
1066         }
1067     }
1068     if (!QEMPTY(&list))
1069         free(mlp);
1070 }
1071
1072 /*
1073  * Set SP's tech to TLEV along with everything else that depends on it.
1074  */
1075 void
1076 shp_set_tech(struct shpstr *sp, int tlev)
1077 {
1078     struct mchrstr *mcp = mchr + sp->shp_type;
1079     int tech_diff = tlev - mcp->m_tech;
1080
1081     if (CANT_HAPPEN(tech_diff < 0)) {
1082       tlev -= tech_diff;
1083       tech_diff = 0;
1084     }
1085
1086     sp->shp_tech = tlev;
1087     sp->shp_armor = (short)SHP_DEF(mcp->m_armor, tech_diff);
1088     sp->shp_speed = (short)SHP_SPD(mcp->m_speed, tech_diff);
1089     sp->shp_visib = (short)SHP_VIS(mcp->m_visib, tech_diff);
1090     sp->shp_frnge = (short)SHP_RNG(mcp->m_frnge, tech_diff);
1091     sp->shp_glim  = (short)SHP_FIR(mcp->m_glim, tech_diff);
1092 }