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