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