2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * shpsub.c: Ship subroutine stuff
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
33 * Markus Armbruster, 2006-2009
48 #include "prototypes.h"
55 static int shp_check_one_mines(struct ulist *);
56 static int shp_hit_mine(struct shpstr *, struct mchrstr *);
57 static void shp_mess(char *, struct ulist *);
60 shp_sel(struct nstr_item *ni, struct emp_qelem *list)
67 while (nxtitem(ni, &ship)) {
70 mcp = &mchr[(int)ship.shp_type];
72 if (ontradingblock(EF_SHIP, &ship)) {
73 pr("ship #%d inelligible - it's for sale.\n",
80 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
81 putship(ship.shp_uid, &ship);
82 mlp = malloc(sizeof(struct ulist));
83 mlp->chrp = (struct empobj_chr *)mcp;
84 mlp->unit.ship = ship;
85 mlp->mobil = ship.shp_mobil;
86 emp_insque(&mlp->queue, list);
90 /* This function assumes that the list was created by shp_sel */
92 shp_nav(struct emp_qelem *list, double *minmobp, double *maxmobp,
93 int *togetherp, natid actor)
96 struct emp_qelem *next;
107 for (qp = list->q_back; qp != list; qp = next) {
109 mlp = (struct ulist *)qp;
110 getship(mlp->unit.ship.shp_uid, &ship);
111 if (ship.shp_own != actor) {
112 mpr(actor, "%s was sunk at %s\n",
113 prship(&ship), xyas(ship.shp_x, ship.shp_y, actor));
114 emp_remque((struct emp_qelem *)mlp);
119 if (*ship.shp_path && !update_running) {
120 shp_mess("has a sail path", mlp);
121 mpr(actor, "Use `sail <#> -' to reset\n");
125 /* check crew - uws don't count */
126 if (ship.shp_item[I_MILIT] == 0 && ship.shp_item[I_CIVIL] == 0) {
127 shp_mess("is crewless", mlp);
130 if (!getsect(ship.shp_x, ship.shp_y, §)) {
131 shp_mess("was sucked into the sky by a strange looking spaceship", mlp); /* heh -KHS */
134 switch (shp_check_nav(§, &ship)) {
135 case CN_CONSTRUCTION:
136 shp_mess("is caught in a construction zone", mlp);
139 shp_mess("is landlocked", mlp);
145 shp_mess("was just swallowed by a big green worm", mlp);
153 if (ship.shp_x != allx || ship.shp_y != ally)
155 if (ship.shp_mobil + 1 < (int)mlp->mobil) {
156 mlp->mobil = ship.shp_mobil;
158 if (mlp->mobil < *minmobp)
159 *minmobp = mlp->mobil;
160 if (mlp->mobil > *maxmobp)
161 *maxmobp = mlp->mobil;
162 mlp->unit.ship = ship;
167 shp_sweep(struct emp_qelem *ship_list, int verbose, int takemob, natid actor)
169 struct emp_qelem *qp;
170 struct emp_qelem *next;
173 int mines, m, max, shells;
177 for (qp = ship_list->q_back; qp != ship_list; qp = next) {
179 mlp = (struct ulist *)qp;
180 if (!(((struct mchrstr *)mlp->chrp)->m_flags & M_SWEEP)) {
182 mpr(actor, "%s doesn't have minesweeping capability!\n",
183 prship(&mlp->unit.ship));
186 if (takemob && mlp->mobil <= 0.0) {
188 mpr(actor, "%s is out of mobility!\n",
189 prship(&mlp->unit.ship));
192 getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, §);
193 if (sect.sct_type != SCT_WATER) {
195 mpr(actor, "%s is not at sea. No mines there!\n",
196 prship(&mlp->unit.ship));
200 mlp->mobil -= shp_mobcost(&mlp->unit.ship);
201 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
203 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
204 if (!(mines = sect.sct_mines))
206 max = ((struct mchrstr *)mlp->chrp)->m_item[I_SHELL];
207 shells = mlp->unit.ship.shp_item[I_SHELL];
208 for (m = 0; mines > 0 && m < 5; m++) {
210 mpr(actor, "Sweep...\n");
212 shells = MIN(max, shells + 1);
213 changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
216 sect.sct_mines = mines;
217 mlp->unit.ship.shp_item[I_SHELL] = shells;
218 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
220 if (shp_check_one_mines(mlp)) {
232 shp_check_one_mines(struct ulist *mlp)
237 getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, §);
238 if (sect.sct_type != SCT_WATER)
242 if (chance(DMINE_HITCHANCE(sect.sct_mines))) {
243 actor = mlp->unit.ship.shp_own;
244 shp_hit_mine(&mlp->unit.ship, ((struct mchrstr *)mlp->chrp));
246 if (map_set(actor, sect.sct_x, sect.sct_y, 'X', 0))
249 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
250 if (!mlp->unit.ship.shp_own)
257 shp_check_mines(struct emp_qelem *ship_list)
259 struct emp_qelem *qp;
260 struct emp_qelem *next;
264 for (qp = ship_list->q_back; qp != ship_list; qp = next) {
266 mlp = (struct ulist *)qp;
267 if (shp_check_one_mines(mlp)) {
278 shp_mess(char *str, struct ulist *mlp)
280 mpr(mlp->unit.ship.shp_own, "%s %s & stays in %s\n",
281 prship(&mlp->unit.ship),
282 str, xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
283 mlp->unit.ship.shp_own));
284 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
285 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
286 emp_remque((struct emp_qelem *)mlp);
291 shp_check_nav(struct sctstr *sect, struct shpstr *shp)
293 switch (dchr[sect->sct_type].d_nav) {
297 if (mchr[(int)shp->shp_type].m_flags & M_CANAL) {
298 if (sect->sct_effic < 2)
299 return CN_CONSTRUCTION;
301 return CN_LANDLOCKED;
304 if (sect->sct_effic < 2)
305 return CN_CONSTRUCTION;
308 if (sect->sct_effic < 60)
309 return CN_CONSTRUCTION;
312 return CN_LANDLOCKED;
318 sect_has_dock(struct sctstr *sect)
320 switch (dchr[sect->sct_type].d_nav) {
330 shp_count(struct emp_qelem *list, int wantflags, int nowantflags,
333 struct emp_qelem *qp;
334 struct emp_qelem *next;
338 for (qp = list->q_back; qp != list; qp = next) {
340 mlp = (struct ulist *)qp;
341 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
344 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
347 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
355 shp_damage_one(struct ulist *mlp, int dam)
357 shipdamage(&mlp->unit.ship, dam);
358 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
359 if (!mlp->unit.ship.shp_own) {
360 emp_remque((struct emp_qelem *)mlp);
366 shp_damage(struct emp_qelem *list, int totdam, int wantflags,
367 int nowantflags, int x, int y)
369 struct emp_qelem *qp;
370 struct emp_qelem *next;
376 || !(count = shp_count(list, wantflags, nowantflags, x, y)))
378 dam = ldround((double)totdam / count, 1);
379 for (qp = list->q_back; qp != list; qp = next) {
381 mlp = (struct ulist *)qp;
382 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
385 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
388 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
390 shp_damage_one(mlp, dam);
396 shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
399 struct emp_qelem *qp;
400 struct emp_qelem *next;
403 for (qp = list->q_back; qp != list; qp = next) {
405 mlp = (struct ulist *)qp;
406 /* If the ship isn't in the requested sector, then continue */
407 if (newx != mlp->unit.ship.shp_x || newy != mlp->unit.ship.shp_y)
410 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
413 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
420 static struct ulist *
421 most_valuable_ship(struct emp_qelem *list, coord x, coord y)
423 struct emp_qelem *qp;
424 struct emp_qelem *next;
426 struct ulist *mvs = NULL;
428 for (qp = list->q_back; qp != list; qp = next) {
430 mlp = (struct ulist *)qp;
431 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
433 if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
435 if (!((struct mchrstr *)mlp->chrp)->m_nxlight &&
436 !((struct mchrstr *)mlp->chrp)->m_nchoppers &&
437 ((struct mchrstr *)mlp->chrp)->m_cost < 1000 &&
438 !((struct mchrstr *)mlp->chrp)->m_nplanes &&
439 !((struct mchrstr *)mlp->chrp)->m_nland)
445 if (((struct mchrstr *)mlp->chrp)->m_cost * mlp->unit.ship.shp_effic >
446 ((struct mchrstr *)mlp->chrp)->m_cost * mvs->unit.ship.shp_effic)
453 shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
455 struct emp_qelem *qp;
456 struct emp_qelem *next;
459 int easiest = 9876; /* things start great for victim */
462 for (qp = list->q_back; qp != list; qp = next) {
464 mlp = (struct ulist *)qp;
466 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
469 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
471 hard = shp_hardtarget(&mlp->unit.ship);
473 easiest = hard; /* things get worse for victim */
476 return easiest - count;
480 shp_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
485 struct emp_qelem msl_list, *qp, *newqp;
489 msl_sel(&msl_list, newx, newy, victim, P_T | P_MAR, 0, MI_INTERDICT);
491 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
493 plp = (struct plist *)qp;
495 mvs = most_valuable_ship(list, newx, newy);
496 if (mvs && mission_pln_equip(plp, NULL, 'p') >= 0) {
497 if (msl_launch(&plp->plane, EF_SHIP, prship(&mvs->unit.ship),
498 newx, newy, victim, &sublaunch) < 0)
500 if (msl_hit(&plp->plane,
501 shp_hardtarget(&mvs->unit.ship), EF_SHIP,
502 N_SHP_MISS, N_SHP_SMISS, sublaunch, victim)) {
503 dam = pln_damage(&plp->plane, 'p', 1);
506 "missile interdiction mission does %d damage to %s!\n",
507 dam, prship(&mvs->unit.ship));
508 shp_damage_one(mvs, dam);
512 dam = pln_damage(&plp->plane, 'p', 0);
513 collateral_damage(newx, newy, dam);
516 plp->plane.pln_effic = 0;
517 putplane(plp->plane.pln_uid, &plp->plane);
526 /* Note that this function has a side effect - it uses coastwatch
527 * ranges to see if it should fire upon a ship. So, this function
528 * is expected to return positive if a ship is in range, and 0 if a
529 * ship is not in range. */
531 notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
533 struct emp_qelem *qp;
534 struct emp_qelem *next;
539 natp = getnatp(sectp->sct_own);
541 vrange = sectp->sct_type == SCT_RADAR ? 14 : 4;
542 vrange *= tfact(sectp->sct_own, 1.0) * sectp->sct_effic / 100.0;
550 for (qp = list->q_back; qp != list; qp = next) {
552 mlp = (struct ulist *)qp;
553 if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
555 if (natp->nat_flags & NF_COASTWATCH)
556 wu(0, sectp->sct_own,
557 "%s %s sighted at %s\n",
558 cname(mlp->unit.ship.shp_own),
559 prship(&mlp->unit.ship),
560 xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
563 setcont(sectp->sct_own, mlp->unit.ship.shp_own, FOUND_COAST);
570 shp_fort_interdiction(struct emp_qelem *list, coord newx, coord newy,
578 signed char notified[MAXNOC];
581 /* Inform neutral and worse */
582 for (i = 0; i < MAXNOC; ++i) {
583 if (getrel(getnatp(i), victim) <= NEUTRAL)
589 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
590 while (nxtsct(&ns, &fsect)) {
593 if (fsect.sct_own == victim)
595 if (notified[fsect.sct_own])
597 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
598 if (notify_coastguard(list, trange, &fsect))
599 notified[fsect.sct_own] = 1;
601 if (opt_NO_FORT_FIRE)
602 return 0; /* Only coastwatch notify in nofortfire */
603 /* Only fire at Hostile ships */
604 for (i = 0; i < MAXNOC; ++i) {
605 if (getrel(getnatp(i), victim) >= NEUTRAL)
608 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
609 while (nxtsct(&ns, &fsect)) {
610 if (!notified[fsect.sct_own])
612 range = roundrange(fortrange(&fsect));
613 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
616 dam = fort_fire(&fsect);
621 mpr(victim, "Incoming fire does %d damage!\n", dam);
623 mpr(victim, "%s fires at you for %d!\n",
624 xyas(fsect.sct_x,fsect.sct_y,victim),
628 "%s fires at %s ships in %s for %d!\n",
629 xyas(fsect.sct_x, fsect.sct_y,
631 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
632 nreport(fsect.sct_own, N_SHP_SHELL, victim, 1);
635 return shp_damage(list, totdam, 0, M_SUB, newx, newy);
640 shp_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
644 if (shp_contains(list, newx, newy, 0, M_SUB)) {
645 stopping |= shp_fort_interdiction(list, newx, newy, victim);
647 if (shp_contains(list, newx, newy, 0, M_SUB)) {
650 unit_interdict(newx, newy, victim, "ships",
651 shp_easiest_target(list, 0, M_SUB),
653 0, M_SUB, newx, newy);
654 if (most_valuable_ship(list, newx, newy)) {
656 shp_missile_interdiction(list, newx, newy, victim);
660 if (shp_contains(list, newx, newy, M_SUB, 0)) {
663 unit_interdict(newx, newy, victim, "subs",
664 shp_easiest_target(list, M_SUB, 0),
666 M_SUB, 0, newx, newy);
671 /* high value of hardtarget is harder to hit */
673 shp_hardtarget(struct shpstr *sp)
677 struct mchrstr *mcp = mchr + sp->shp_type;
680 getsect(sp->shp_x, sp->shp_y, §);
681 onsea = sect.sct_type == SCT_WATER;
682 if (mcp->m_flags & M_SUB)
684 return (int)((sp->shp_effic / 100.0) *
685 (20 + shp_speed(sp) * onsea / 2.0 - vis));
689 shp_hit_mine(struct shpstr *sp, struct mchrstr *mcp)
693 mpr(sp->shp_own, "Kawhomp! Mine detected in %s!\n",
694 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
696 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
699 if (mcp->m_flags & M_SWEEP)
702 shipdamage(sp, ldround(m, 1));
708 shp_nav_one_sector(struct emp_qelem *list, int dir, natid actor,
712 struct emp_qelem *qp;
713 struct emp_qelem *next;
715 struct emp_qelem done;
722 double tech; /* for mapping */
723 double tf; /* for mapping */
727 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
728 unit_put(list, actor);
733 for (qp = list->q_back; qp != list; qp = next) {
735 mlp = (struct ulist *)qp;
736 newx = xnorm(mlp->unit.ship.shp_x + dx);
737 newy = ynorm(mlp->unit.ship.shp_y + dy);
738 getsect(newx, newy, §);
739 navigate = shp_check_nav(§, &mlp->unit.ship);
740 if (navigate != CN_NAVIGABLE ||
741 (sect.sct_own && actor != sect.sct_own &&
742 getrel(getnatp(sect.sct_own), actor) < FRIENDLY)) {
743 if (dchr[sect.sct_type].d_nav == NAV_CANAL &&
744 !(((struct mchrstr *)mlp->chrp)->m_flags & M_CANAL) &&
745 navigate == CN_LANDLOCKED)
747 "is too large to fit into the canal system at %s",
748 xyas(newx, newy, actor));
750 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
752 mpr(actor, "%s\n", dp);
760 if (mlp->mobil <= 0.0) {
761 shp_mess("is out of mobility", mlp);
764 mobcost = shp_mobcost(&mlp->unit.ship);
765 mlp->unit.ship.shp_x = newx;
766 mlp->unit.ship.shp_y = newy;
767 if (mlp->mobil - mobcost < -127) {
770 mlp->mobil -= mobcost;
772 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
773 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
775 /* Now update the map for this ship */
776 tech = techfact(mlp->unit.ship.shp_tech,
777 ((struct mchrstr *)mlp->chrp)->m_vrnge);
778 if (((struct mchrstr *)mlp->chrp)->m_flags & M_SONAR)
779 tf = techfact(mlp->unit.ship.shp_tech, 1.0);
782 radmapupd(mlp->unit.ship.shp_own,
783 mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
784 (int)mlp->unit.ship.shp_effic, (int)tech, tf);
788 stopping |= shp_sweep(list, 0, 0, actor);
791 stopping |= shp_check_mines(list);
795 /* interdict ships sector by sector */
797 while (!QEMPTY(list)) {
798 mlp = (struct ulist *)list->q_back;
799 newx = mlp->unit.ship.shp_x;
800 newy = mlp->unit.ship.shp_y;
801 stopping |= shp_interdict(list, newx, newy, actor);
802 /* move survivors in this sector to done */
803 for (qp = list->q_back; qp != list; qp = next) {
805 mlp = (struct ulist *)qp;
806 if (mlp->unit.ship.shp_x == newx &&
807 mlp->unit.ship.shp_y == newy) {
809 emp_insque(qp, &done);
813 /* assign surviving ships back to list */
814 emp_insque(list, &done);
822 * Check for incoming missiles with a P_MAR flag.
823 * Return True=1 if the missile was shotdown.
826 * Chad Zabel, July 95
830 shp_missile_defense(coord dx, coord dy, natid bombown, int hardtarget)
835 double gun, eff, teff;
837 snxtitem_dist(&ni, EF_SHIP, dx, dy, 1);
839 while (nxtitem(&ni, &ship)) {
843 if (!(mchr[(int)ship.shp_type].m_flags & M_ANTIMISSILE))
846 if (getrel(getnatp(ship.shp_own), bombown) >= NEUTRAL)
849 if (ship.shp_effic < 60)
852 if (ship.shp_item[I_MILIT] < 1) /* do we have mil? */
854 if (ship.shp_item[I_GUN] < 1) /* we need at least 1 gun */
856 if (!shp_supply(&ship, I_SHELL, 2))
858 ship.shp_item[I_SHELL] -= 2;
859 putship(ship.shp_uid, &ship);
861 /* now calculate the odds */
862 gun = shp_usable_guns(&ship);
863 eff = ship.shp_effic / 100.0;
864 teff = ship.shp_tech / (ship.shp_tech + 200.0);
865 /* raise 4.5 for better interception -KHS */
866 hitchance = (int)(gun * eff * teff * 4.5) - hardtarget;
872 mpr(bombown, "%s anti-missile system activated...",
873 cname(ship.shp_own));
874 mpr(ship.shp_own, "Ship #%i anti-missile system activated!\n",
876 mpr(ship.shp_own, "%d%% hitchance...", hitchance);
878 if (roll(100) <= hitchance) {
879 mpr(bombown, "KABOOOM!! Missile destroyed\n\n");
881 "KABOOOM!! Incoming missile destroyed!\n\n");
884 mpr(bombown, "SWOOSH!! anti-missile system failed!!\n");
886 "SWOOSH!! Missile evades anti-missile systems\n\n");
889 return 0; /* all attempts failed */
893 /* Fire missiles at a ship which has fired shells */
895 shp_missdef(struct shpstr *sp, natid victim)
897 struct emp_qelem list;
904 mlp = malloc(sizeof(struct ulist));
905 mlp->chrp = (struct empobj_chr *)&mchr[(int)sp->shp_type];
906 mlp->unit.ship = *sp;
907 mlp->mobil = sp->shp_mobil;
908 emp_insque(&mlp->queue, &list);
909 sprintf(buf, "%s", prship(&mlp->unit.ship));
912 if (most_valuable_ship(&list, sp->shp_x, sp->shp_y)) {
913 shp_missile_interdiction(&list, sp->shp_x, sp->shp_y, sp->shp_own);
914 getship(sp->shp_uid, sp);
918 "missiles launched in defense did 100%% damage to %s\n",
920 wu(0, victim, "%s sunk!\n", buf);
921 } else if (eff > 0 && sp->shp_effic < eff) {
923 "missiles launched in defense did %d%% damage to %s\n",
924 100 * (eff - sp->shp_effic) / eff, buf);
932 shp_mobcost(struct shpstr *sp)
934 return speed_factor(sp->shp_effic * 0.01 * shp_speed(sp),
939 * Set SP's tech to TLEV along with everything else that depends on it.
942 shp_set_tech(struct shpstr *sp, int tlev)
944 struct mchrstr *mcp = mchr + sp->shp_type;
946 if (CANT_HAPPEN(tlev < mcp->m_tech))