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