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