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