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