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