]> git.pond.sub.org Git - empserver/blob - src/lib/subs/shpsub.c
Clean up checks for zero value of pln_damage()
[empserver] / src / lib / subs / shpsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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 *);
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);
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     /* ship might have changed (launched interceptors, missile defense) */
358     getship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
359     shipdamage(&mlp->unit.ship, dam);
360     putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
361     if (!mlp->unit.ship.shp_own) {
362         emp_remque((struct emp_qelem *)mlp);
363         free(mlp);
364     }
365 }
366
367 static int
368 shp_damage(struct emp_qelem *list, int totdam, int wantflags,
369            int nowantflags, int x, int y)
370 {
371     struct emp_qelem *qp;
372     struct emp_qelem *next;
373     struct ulist *mlp;
374     int dam;
375     int count;
376
377     if (!totdam
378         || !(count = shp_count(list, wantflags, nowantflags, x, y)))
379         return 0;
380     dam = ldround((double)totdam / count, 1);
381     for (qp = list->q_back; qp != list; qp = next) {
382         next = qp->q_back;
383         mlp = (struct ulist *)qp;
384         if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
385             continue;
386         if (wantflags &&
387             (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
388             continue;
389         if (nowantflags &&
390             ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
391             continue;
392         shp_damage_one(mlp, dam);
393     }
394     return dam;
395 }
396
397 static int
398 shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
399              int nowantflags)
400 {
401     struct emp_qelem *qp;
402     struct emp_qelem *next;
403     struct ulist *mlp;
404
405     for (qp = list->q_back; qp != list; qp = next) {
406         next = qp->q_back;
407         mlp = (struct ulist *)qp;
408 /* If the ship isn't in the requested sector, then continue */
409         if (newx != mlp->unit.ship.shp_x || newy != mlp->unit.ship.shp_y)
410             continue;
411         if (wantflags &&
412             (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
413             continue;
414         if (nowantflags &&
415             ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
416             continue;
417         return 1;
418     }
419     return 0;
420 }
421
422 static struct ulist *
423 most_valuable_ship(struct emp_qelem *list, coord x, coord y)
424 {
425     struct emp_qelem *qp;
426     struct emp_qelem *next;
427     struct ulist *mlp;
428     struct ulist *mvs = NULL;
429
430     for (qp = list->q_back; qp != list; qp = next) {
431         next = qp->q_back;
432         mlp = (struct ulist *)qp;
433         if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
434             continue;
435         if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
436             continue;
437         if (!((struct mchrstr *)mlp->chrp)->m_nxlight &&
438             !((struct mchrstr *)mlp->chrp)->m_nchoppers &&
439             ((struct mchrstr *)mlp->chrp)->m_cost < 1000 &&
440             !((struct mchrstr *)mlp->chrp)->m_nplanes &&
441             !((struct mchrstr *)mlp->chrp)->m_nland)
442             continue;
443         if (!mvs) {
444             mvs = mlp;
445             continue;
446         }
447         if (((struct mchrstr *)mlp->chrp)->m_cost * mlp->unit.ship.shp_effic >
448             ((struct mchrstr *)mlp->chrp)->m_cost * mvs->unit.ship.shp_effic)
449             mvs = mlp;
450     }
451     return mvs;
452 }
453
454 static int
455 shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
456 {
457     struct emp_qelem *qp;
458     struct emp_qelem *next;
459     struct ulist *mlp;
460     int hard;
461     int easiest = 9876;         /* things start great for victim */
462     int count = 0;
463
464     for (qp = list->q_back; qp != list; qp = next) {
465         next = qp->q_back;
466         mlp = (struct ulist *)qp;
467         if (wantflags &&
468             (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
469             continue;
470         if (nowantflags &&
471             ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
472             continue;
473         hard = shp_hardtarget(&mlp->unit.ship);
474         if (hard < easiest)
475             easiest = hard;     /* things get worse for victim */
476         ++count;
477     }
478     return easiest - count;
479 }
480
481 static int
482 shp_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
483                          natid victim)
484 {
485     int dam, sublaunch;
486     int stopping = 0;
487     struct emp_qelem msl_list, *qp, *newqp;
488     struct plist *plp;
489     struct ulist *mvs;
490
491     mvs = most_valuable_ship(list, newx, newy);
492     if (!mvs)
493         return 0;
494
495     msl_sel(&msl_list, newx, newy, victim, P_T | P_MAR, 0, MI_INTERDICT);
496
497     for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
498         newqp = qp->q_back;
499         plp = (struct plist *)qp;
500
501         if (mvs && mission_pln_equip(plp, NULL, 'p') >= 0) {
502             if (msl_launch(&plp->plane, EF_SHIP, prship(&mvs->unit.ship),
503                            newx, newy, victim, &sublaunch) < 0)
504                 goto use_up_msl;
505             stopping = 1;
506             if (msl_hit(&plp->plane,
507                         shp_hardtarget(&mvs->unit.ship), EF_SHIP,
508                         N_SHP_MISS, N_SHP_SMISS, sublaunch, victim)) {
509                 dam = pln_damage(&plp->plane, 'p', 1);
510                 mpr(victim,
511                     "missile interdiction mission does %d damage to %s!\n",
512                     dam, prship(&mvs->unit.ship));
513                 shp_damage_one(mvs, dam);
514             } else {
515                 dam = pln_damage(&plp->plane, 'p', 0);
516                 collateral_damage(newx, newy, dam);
517             }
518             mvs = most_valuable_ship(list, newx, newy);
519         use_up_msl:
520             plp->plane.pln_effic = 0;
521             putplane(plp->plane.pln_uid, &plp->plane);
522         }
523         emp_remque(qp);
524         free(qp);
525     }
526
527     return stopping;
528 }
529
530 /* Note that this function has a side effect - it uses coastwatch
531  * ranges to see if it should fire upon a ship.  So, this function
532  * is expected to return positive if a ship is in range, and 0 if a
533  * ship is not in range. */
534 static int
535 notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
536 {
537     struct emp_qelem *qp;
538     struct emp_qelem *next;
539     struct ulist *mlp;
540     struct natstr *natp;
541     int vrange;
542
543     natp = getnatp(sectp->sct_own);
544
545     vrange = sectp->sct_type == SCT_RADAR ? 14 : 4;
546     vrange *= tfact(sectp->sct_own, 1.0) * sectp->sct_effic / 100.0;
547
548     if (vrange < 1)
549         vrange = 1;
550
551     if (vrange < trange)
552         return 0;
553
554     for (qp = list->q_back; qp != list; qp = next) {
555         next = qp->q_back;
556         mlp = (struct ulist *)qp;
557         if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
558             continue;
559         if (natp->nat_flags & NF_COASTWATCH)
560             wu(0, sectp->sct_own,
561                "%s %s sighted at %s\n",
562                cname(mlp->unit.ship.shp_own),
563                prship(&mlp->unit.ship),
564                xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
565                     sectp->sct_own));
566         if (opt_HIDDEN)
567             setcont(sectp->sct_own, mlp->unit.ship.shp_own, FOUND_COAST);
568     }
569
570     return 1;
571 }
572
573 static int
574 shp_fort_interdiction(struct emp_qelem *list, coord newx, coord newy,
575                       natid victim)
576 {
577     struct nstr_sect ns;
578     struct sctstr fsect;
579     int trange, range;
580     int dam;
581     int stopping = 0;
582     int totdam = 0;
583     signed char notified[MAXNOC];
584     int i;
585
586     /* Inform neutral and worse */
587     for (i = 0; i < MAXNOC; ++i) {
588         if (getrel(getnatp(i), victim) <= NEUTRAL)
589             notified[i] = 0;
590         else
591             notified[i] = 1;
592     }
593
594     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
595     while (nxtsct(&ns, &fsect)) {
596         if (!fsect.sct_own)
597             continue;
598         if (fsect.sct_own == victim)
599             continue;
600         if (notified[fsect.sct_own])
601             continue;
602         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
603         if (notify_coastguard(list, trange, &fsect))
604             notified[fsect.sct_own] = 1;
605     }
606     if (opt_NO_FORT_FIRE)
607         return 0;               /* Only coastwatch notify in nofortfire */
608     /* Only fire at Hostile ships */
609     for (i = 0; i < MAXNOC; ++i) {
610         if (getrel(getnatp(i), victim) >= NEUTRAL)
611             notified[i] = 0;
612     }
613     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
614     while (nxtsct(&ns, &fsect)) {
615         if (!notified[fsect.sct_own])
616             continue;
617         range = roundrange(fortrange(&fsect));
618         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
619         if (trange > range)
620             continue;
621         dam = fort_fire(&fsect);
622         putsect(&fsect);
623         if (dam < 0)
624             continue;
625         stopping = 1;
626         totdam += dam;
627         mpr(victim, "Incoming fire does %d damage!\n", dam);
628 #if 0
629         mpr(victim, "%s fires at you for %d!\n",
630             xyas(fsect.sct_x,fsect.sct_y,victim),
631             dam);
632 #endif
633         wu(0, fsect.sct_own,
634            "%s fires at %s ships in %s for %d!\n",
635            xyas(fsect.sct_x, fsect.sct_y,
636                 fsect.sct_own),
637            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
638         nreport(fsect.sct_own, N_SHP_SHELL, victim, 1);
639     }
640     if (totdam > 0)
641         shp_damage(list, totdam, 0, M_SUB, newx, newy);
642     return stopping;
643 }
644
645 static int
646 shp_mission_interdiction(struct emp_qelem *list, coord x, coord y,
647                          natid victim, int subs)
648 {
649     char *what = subs ? "subs" : "ships";
650     int wantflags = subs ? M_SUB : 0;
651     int nowantflags = subs ? 0 : M_SUB;
652     int mission = subs ? MI_SINTERDICT : MI_INTERDICT;
653     int dam;
654
655     dam = unit_interdict(x, y, victim, what,
656                          shp_easiest_target(list, wantflags, nowantflags),
657                          mission);
658     if (dam >= 0)
659         shp_damage(list, dam, wantflags, nowantflags, x, y);
660     return dam >= 0;
661 }
662
663 static int
664 shp_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
665 {
666     int stopping = 0;
667
668     if (shp_contains(list, newx, newy, 0, M_SUB)) {
669         stopping |= shp_fort_interdiction(list, newx, newy, victim);
670
671         if (shp_contains(list, newx, newy, 0, M_SUB)) {
672             stopping |= shp_mission_interdiction(list, newx, newy, victim, 0);
673             stopping |= shp_missile_interdiction(list, newx, newy, victim);
674         }
675     }
676     if (shp_contains(list, newx, newy, M_SUB, 0))
677         stopping |= shp_mission_interdiction(list, newx, newy, victim, 1);
678     return stopping;
679 }
680
681 /* high value of hardtarget is harder to hit */
682 int
683 shp_hardtarget(struct shpstr *sp)
684 {
685     struct sctstr sect;
686     int vis, onsea;
687     struct mchrstr *mcp = mchr + sp->shp_type;
688
689     vis = shp_visib(sp);
690     getsect(sp->shp_x, sp->shp_y, &sect);
691     onsea = sect.sct_type == SCT_WATER;
692     if (mcp->m_flags & M_SUB)
693         vis *= 4;
694     return (int)((sp->shp_effic / 100.0) *
695                  (20 + shp_speed(sp) * onsea / 2.0 - vis));
696 }
697
698 static int
699 shp_hit_mine(struct shpstr *sp)
700 {
701     double m;
702
703     mpr(sp->shp_own, "Kawhomp! Mine detected in %s!\n",
704         xyas(sp->shp_x, sp->shp_y, sp->shp_own));
705
706     nreport(sp->shp_own, N_HIT_MINE, 0, 1);
707
708     m = MINE_DAMAGE();
709     if (mchr[sp->shp_type].m_flags & M_SWEEP)
710         m /= 2.0;
711
712     shipdamage(sp, ldround(m, 1));
713
714     return (int)m;
715 }
716
717 int
718 shp_nav_one_sector(struct emp_qelem *list, int dir, natid actor,
719                    int together)
720 {
721     struct sctstr sect;
722     struct emp_qelem *qp;
723     struct emp_qelem *next;
724     struct ulist *mlp;
725     struct emp_qelem done;
726     coord dx;
727     coord dy;
728     coord newx;
729     coord newy;
730     int stopping = 0;
731     double mobcost;
732     double tech;                /* for mapping */
733     double tf;                  /* for mapping */
734     char dp[80];
735     int navigate;
736
737     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
738         unit_put(list, actor);
739         return 1;
740     }
741     dx = diroff[dir][0];
742     dy = diroff[dir][1];
743     for (qp = list->q_back; qp != list; qp = next) {
744         next = qp->q_back;
745         mlp = (struct ulist *)qp;
746         newx = xnorm(mlp->unit.ship.shp_x + dx);
747         newy = ynorm(mlp->unit.ship.shp_y + dy);
748         getsect(newx, newy, &sect);
749         navigate = shp_check_nav(&sect, &mlp->unit.ship);
750         if (navigate != CN_NAVIGABLE ||
751             (sect.sct_own && actor != sect.sct_own &&
752              getrel(getnatp(sect.sct_own), actor) < FRIENDLY)) {
753             if (dchr[sect.sct_type].d_nav == NAV_CANAL &&
754                 !(((struct mchrstr *)mlp->chrp)->m_flags & M_CANAL) &&
755                 navigate == CN_LANDLOCKED)
756                 sprintf(dp,
757                         "is too large to fit into the canal system at %s",
758                         xyas(newx, newy, actor));
759             else
760                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
761             if (together) {
762                 mpr(actor, "%s\n", dp);
763                 return 2;
764             } else {
765                 shp_mess(dp, mlp);
766                 continue;
767             }
768         }
769
770         if (mlp->mobil <= 0.0) {
771             shp_mess("is out of mobility", mlp);
772             continue;
773         }
774         mobcost = shp_mobcost(&mlp->unit.ship);
775         mlp->unit.ship.shp_x = newx;
776         mlp->unit.ship.shp_y = newy;
777         if (mlp->mobil - mobcost < -127) {
778             mlp->mobil = -127;
779         } else {
780             mlp->mobil -= mobcost;
781         }
782         mlp->unit.ship.shp_mobil = (int)mlp->mobil;
783         putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
784
785         /* Now update the map for this ship */
786         tech = techfact(mlp->unit.ship.shp_tech,
787                         ((struct mchrstr *)mlp->chrp)->m_vrnge);
788         if (((struct mchrstr *)mlp->chrp)->m_flags & M_SONAR)
789             tf = techfact(mlp->unit.ship.shp_tech, 1.0);
790         else
791             tf = 0.0;
792         radmapupd(mlp->unit.ship.shp_own,
793                   mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
794                   (int)mlp->unit.ship.shp_effic, (int)tech, tf);
795     }
796     if (QEMPTY(list))
797         return stopping;
798     stopping |= shp_sweep(list, 0, 0, actor);
799     if (QEMPTY(list))
800         return stopping;
801     stopping |= shp_check_mines(list);
802     if (QEMPTY(list))
803         return stopping;
804
805     /* interdict ships sector by sector */
806     emp_initque(&done);
807     while (!QEMPTY(list)) {
808         mlp = (struct ulist *)list->q_back;
809         newx = mlp->unit.ship.shp_x;
810         newy = mlp->unit.ship.shp_y;
811         stopping |= shp_interdict(list, newx, newy, actor);
812         /* move survivors in this sector to done */
813         for (qp = list->q_back; qp != list; qp = next) {
814             next = qp->q_back;
815             mlp = (struct ulist *)qp;
816             if (mlp->unit.ship.shp_x == newx &&
817                 mlp->unit.ship.shp_y == newy) {
818                 emp_remque(qp);
819                 emp_insque(qp, &done);
820             }
821         }
822     }
823     /* assign surviving ships back to list */
824     emp_insque(list, &done);
825     emp_remque(&done);
826
827     return stopping;
828 }
829
830 /*
831  * shp_miss_defence
832  * Check for incoming missiles with a P_MAR flag.
833  * Return True=1 if the missile was shotdown.
834  * Or False=0
835  *
836  * Chad Zabel, July 95
837  */
838
839 int
840 shp_missile_defense(coord dx, coord dy, natid bombown, int hardtarget)
841 {
842     struct nstr_item ni;
843     struct shpstr ship;
844     int hitchance;
845     double gun, eff, teff;
846
847     snxtitem_dist(&ni, EF_SHIP, dx, dy, 1);
848
849     while (nxtitem(&ni, &ship)) {
850         if (!ship.shp_own)
851             continue;
852
853         if (!(mchr[(int)ship.shp_type].m_flags & M_ANTIMISSILE))
854             continue;
855
856         if (getrel(getnatp(ship.shp_own), bombown) >= NEUTRAL)
857             continue;
858
859         if (ship.shp_effic < 60)
860             continue;
861
862         if (ship.shp_item[I_MILIT] < 1) /* do we have mil? */
863             continue;
864         if (ship.shp_item[I_GUN] < 1)   /* we need at least 1 gun */
865             continue;
866         if (!shp_supply(&ship, I_SHELL, 2))
867             continue;
868         ship.shp_item[I_SHELL] -= 2;
869         putship(ship.shp_uid, &ship);
870
871         /* now calculate the odds */
872         gun = shp_usable_guns(&ship);
873         eff = ship.shp_effic / 100.0;
874         teff = ship.shp_tech / (ship.shp_tech + 200.0);
875         /* raise 4.5 for better interception -KHS */
876         hitchance = (int)(gun * eff * teff * 4.5) - hardtarget;
877         if (hitchance < 0)
878             hitchance = 0;
879         if (hitchance > 100)
880             hitchance = 100;
881
882         mpr(bombown, "%s anti-missile system activated...",
883             cname(ship.shp_own));
884         mpr(ship.shp_own, "Ship #%i anti-missile system activated!\n",
885             ship.shp_uid);
886         mpr(ship.shp_own, "%d%% hitchance...", hitchance);
887
888         if (roll(100) <= hitchance) {
889             mpr(bombown, "KABOOOM!! Missile destroyed\n\n");
890             mpr(ship.shp_own,
891                 "KABOOOM!!  Incoming missile destroyed!\n\n");
892             return 1;
893         } else {
894             mpr(bombown, "SWOOSH!!  anti-missile system failed!!\n");
895             mpr(ship.shp_own,
896                 "SWOOSH!!  Missile evades anti-missile systems\n\n");
897         }
898     }
899     return 0;                   /* all attempts failed */
900 }
901
902
903 /* Fire missiles at a ship which has fired shells */
904 void
905 shp_missdef(struct shpstr *sp, natid victim)
906 {
907     struct emp_qelem list;
908     struct ulist *mlp;
909     int eff;
910     char buf[512];
911
912     emp_initque(&list);
913
914     mlp = malloc(sizeof(struct ulist));
915     mlp->chrp = (struct empobj_chr *)&mchr[(int)sp->shp_type];
916     mlp->unit.ship = *sp;
917     mlp->mobil = sp->shp_mobil;
918     emp_insque(&mlp->queue, &list);
919     sprintf(buf, "%s", prship(&mlp->unit.ship));
920
921     eff = sp->shp_effic;
922     shp_missile_interdiction(&list, sp->shp_x, sp->shp_y, sp->shp_own);
923     getship(sp->shp_uid, sp);
924
925     if (!sp->shp_own) {
926         wu(0, victim,
927            "missiles launched in defense did 100%% damage to %s\n",
928            buf);
929         wu(0, victim, "%s sunk!\n", buf);
930     } else if (eff > 0 && sp->shp_effic < eff) {
931         wu(0, victim,
932            "missiles launched in defense did %d%% damage to %s\n",
933            100 * (eff - sp->shp_effic) / eff, buf);
934     }
935     if (!QEMPTY(&list))
936         free(mlp);
937 }
938
939 double
940 shp_mobcost(struct shpstr *sp)
941 {
942     return speed_factor(sp->shp_effic * 0.01 * shp_speed(sp),
943                         sp->shp_tech);
944 }
945
946 /*
947  * Set SP's tech to TLEV along with everything else that depends on it.
948  */
949 void
950 shp_set_tech(struct shpstr *sp, int tlev)
951 {
952     struct mchrstr *mcp = mchr + sp->shp_type;
953
954     if (CANT_HAPPEN(tlev < mcp->m_tech))
955         tlev = mcp->m_tech;
956
957     sp->shp_tech = tlev;
958 }