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