2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * shpsub.c: Ship subroutine stuff
29 * Known contributors to this file:
31 * Steve McClure, 1996-2000
32 * Markus Armbruster, 2006-2013
50 #include "prototypes.h"
56 static int shp_check_one_mines(struct ulist *);
57 static int shp_hit_mine(struct shpstr *);
58 static void shp_stays(natid, char *, struct ulist *);
61 shp_sel(struct nstr_item *ni, struct emp_qelem *list)
68 while (nxtitem(ni, &ship)) {
70 * It would be nice to let deities navigate foreign ships, but
71 * much of the code assumes that only the ship's owner can
74 if (!ship.shp_own || ship.shp_own != player->cnum)
76 mcp = &mchr[(int)ship.shp_type];
78 if (ontradingblock(EF_SHIP, &ship)) {
79 pr("ship #%d inelligible - it's for sale.\n",
86 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
87 putship(ship.shp_uid, &ship);
88 mlp = malloc(sizeof(struct ulist));
89 mlp->chrp = (struct empobj_chr *)mcp;
90 mlp->unit.ship = ship;
91 mlp->mobil = ship.shp_mobil;
92 emp_insque(&mlp->queue, list);
96 /* This function assumes that the list was created by shp_sel */
98 shp_nav(struct emp_qelem *list, double *minmobp, double *maxmobp,
99 int *togetherp, natid actor)
101 struct emp_qelem *qp;
102 struct emp_qelem *next;
113 for (qp = list->q_back; qp != list; qp = next) {
115 mlp = (struct ulist *)qp;
116 sp = &mlp->unit.ship;
117 getship(sp->shp_uid, sp);
118 if (sp->shp_own != actor) {
119 mpr(actor, "%s was sunk at %s\n",
120 prship(sp), xyas(sp->shp_x, sp->shp_y, actor));
121 emp_remque(&mlp->queue);
126 if (*sp->shp_path && !update_running) {
127 shp_stays(actor, "has a sail path", mlp);
128 mpr(actor, "Use `sail <#> -' to reset\n");
132 /* check crew - uws don't count */
133 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
134 shp_stays(actor, "is crewless", mlp);
137 if (!getsect(sp->shp_x, sp->shp_y, §)) {
138 shp_stays(actor, "was sucked into the sky by a strange looking spaceship", mlp); /* heh -KHS */
141 switch (shp_check_nav(sp, §)) {
144 shp_stays(actor, "is caught in a construction zone", mlp);
148 shp_stays(actor, "is landlocked", mlp);
154 shp_stays(actor, "was just swallowed by a big green worm", mlp);
162 if (sp->shp_x != allx || sp->shp_y != ally)
164 if (sp->shp_mobil + 1 < (int)mlp->mobil) {
165 mlp->mobil = sp->shp_mobil;
167 if (mlp->mobil < *minmobp)
168 *minmobp = mlp->mobil;
169 if (mlp->mobil > *maxmobp)
170 *maxmobp = mlp->mobil;
175 shp_sweep(struct emp_qelem *ship_list, int verbose, int takemob, natid actor)
177 struct emp_qelem *qp;
178 struct emp_qelem *next;
181 int mines, m, max, shells;
185 for (qp = ship_list->q_back; qp != ship_list; qp = next) {
187 mlp = (struct ulist *)qp;
188 if (!(((struct mchrstr *)mlp->chrp)->m_flags & M_SWEEP)) {
190 mpr(actor, "%s doesn't have minesweeping capability!\n",
191 prship(&mlp->unit.ship));
194 if (takemob && mlp->mobil <= 0.0) {
196 mpr(actor, "%s is out of mobility!\n",
197 prship(&mlp->unit.ship));
200 getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, §);
201 if (sect.sct_type != SCT_WATER) {
203 mpr(actor, "%s is not at sea. No mines there!\n",
204 prship(&mlp->unit.ship));
208 mlp->mobil -= shp_mobcost(&mlp->unit.ship);
209 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
211 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
212 if (!(mines = sect.sct_mines))
214 max = ((struct mchrstr *)mlp->chrp)->m_item[I_SHELL];
215 shells = mlp->unit.ship.shp_item[I_SHELL];
216 for (m = 0; mines > 0 && m < 5; m++) {
218 mpr(actor, "Sweep...\n");
220 shells = MIN(max, shells + 1);
221 changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
224 sect.sct_mines = mines;
225 mlp->unit.ship.shp_item[I_SHELL] = shells;
226 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
228 if (shp_check_one_mines(mlp)) {
240 shp_check_one_mines(struct ulist *mlp)
245 getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, §);
246 if (sect.sct_type != SCT_WATER)
250 if (chance(DMINE_HITCHANCE(sect.sct_mines))) {
251 actor = mlp->unit.ship.shp_own;
252 shp_hit_mine(&mlp->unit.ship);
254 if (map_set(actor, sect.sct_x, sect.sct_y, 'X', 0))
257 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
258 if (!mlp->unit.ship.shp_own)
265 shp_check_mines(struct emp_qelem *ship_list)
267 struct emp_qelem *qp;
268 struct emp_qelem *next;
272 for (qp = ship_list->q_back; qp != ship_list; qp = next) {
274 mlp = (struct ulist *)qp;
275 if (shp_check_one_mines(mlp)) {
286 shp_stays(natid actor, char *str, struct ulist *mlp)
288 mpr(actor, "%s %s & stays in %s\n",
289 prship(&mlp->unit.ship), str,
290 xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, actor));
291 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
292 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
293 emp_remque(&mlp->queue);
298 * Can SP navigate in SECTP?
299 * Sector ownership is *not* considered!
300 * Return NAVOK when yes.
301 * Return NAV_02 when it could if the sector was at least 2% efficient.
302 * Return NAV_60 when it could if the sector was at least 60% efficient.
303 * Return NAV_CANAL when it lacks capability M_CANAL.
304 * Return NAV_NONE when this sector type isn't navigable at all.
307 shp_check_nav(struct shpstr *sp, struct sctstr *sectp)
309 switch (dchr[sectp->sct_type].d_nav) {
313 if (mchr[sp->shp_type].m_flags & M_CANAL) {
314 if (sectp->sct_effic < 2)
320 if (sectp->sct_effic < 2)
324 if (sectp->sct_effic < 60)
336 sect_has_dock(struct sctstr *sect)
338 switch (dchr[sect->sct_type].d_nav) {
348 shp_count(struct emp_qelem *list, int wantflags, int nowantflags,
351 struct emp_qelem *qp;
352 struct emp_qelem *next;
356 for (qp = list->q_back; qp != list; qp = next) {
358 mlp = (struct ulist *)qp;
359 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
362 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
365 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
373 shp_damage_one(struct ulist *mlp, int dam)
375 /* ship might have changed (launched interceptors, missile defense) */
376 getship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
377 shipdamage(&mlp->unit.ship, dam);
378 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
379 if (!mlp->unit.ship.shp_own) {
380 emp_remque(&mlp->queue);
386 shp_damage(struct emp_qelem *list, int totdam, int wantflags,
387 int nowantflags, int x, int y)
389 struct emp_qelem *qp;
390 struct emp_qelem *next;
396 || !(count = shp_count(list, wantflags, nowantflags, x, y)))
398 dam = ldround((double)totdam / count, 1);
399 for (qp = list->q_back; qp != list; qp = next) {
401 mlp = (struct ulist *)qp;
402 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
405 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
408 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
410 shp_damage_one(mlp, dam);
416 shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
419 struct emp_qelem *qp;
420 struct emp_qelem *next;
423 for (qp = list->q_back; qp != list; qp = next) {
425 mlp = (struct ulist *)qp;
426 /* If the ship isn't in the requested sector, then continue */
427 if (newx != mlp->unit.ship.shp_x || newy != mlp->unit.ship.shp_y)
430 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
433 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
440 static struct ulist *
441 most_valuable_ship(struct emp_qelem *list, coord x, coord y)
443 struct emp_qelem *qp;
444 struct emp_qelem *next;
446 struct ulist *mvs = NULL;
448 for (qp = list->q_back; qp != list; qp = next) {
450 mlp = (struct ulist *)qp;
451 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
453 if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
455 if (!((struct mchrstr *)mlp->chrp)->m_nxlight &&
456 !((struct mchrstr *)mlp->chrp)->m_nchoppers &&
457 ((struct mchrstr *)mlp->chrp)->m_cost < 1000 &&
458 !((struct mchrstr *)mlp->chrp)->m_nplanes &&
459 !((struct mchrstr *)mlp->chrp)->m_nland)
465 if (((struct mchrstr *)mlp->chrp)->m_cost * mlp->unit.ship.shp_effic >
466 ((struct mchrstr *)mvs->chrp)->m_cost * mvs->unit.ship.shp_effic)
473 shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
475 struct emp_qelem *qp;
476 struct emp_qelem *next;
479 int easiest = 9876; /* things start great for victim */
482 for (qp = list->q_back; qp != list; qp = next) {
484 mlp = (struct ulist *)qp;
486 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
489 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
491 hard = shp_hardtarget(&mlp->unit.ship);
493 easiest = hard; /* things get worse for victim */
496 return easiest - count;
500 shp_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
505 struct emp_qelem msl_list, *qp, *newqp;
509 mvs = most_valuable_ship(list, newx, newy);
513 msl_sel(&msl_list, newx, newy, victim, P_T | P_MAR, 0, MI_INTERDICT);
515 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
517 plp = (struct plist *)qp;
519 if (mvs && mission_pln_equip(plp, NULL, 'p') >= 0) {
520 if (msl_launch(&plp->plane, EF_SHIP, prship(&mvs->unit.ship),
521 newx, newy, victim, &sublaunch) < 0)
524 if (msl_hit(&plp->plane,
525 shp_hardtarget(&mvs->unit.ship), EF_SHIP,
526 N_SHP_MISS, N_SHP_SMISS, sublaunch, victim)) {
527 dam = pln_damage(&plp->plane, 'p', 1);
529 "missile interdiction mission does %d damage to %s!\n",
530 dam, prship(&mvs->unit.ship));
531 shp_damage_one(mvs, dam);
533 dam = pln_damage(&plp->plane, 'p', 0);
534 collateral_damage(newx, newy, dam);
536 mvs = most_valuable_ship(list, newx, newy);
538 plp->plane.pln_effic = 0;
539 putplane(plp->plane.pln_uid, &plp->plane);
548 /* Note that this function has a side effect - it uses coastwatch
549 * ranges to see if it should fire upon a ship. So, this function
550 * is expected to return positive if a ship is in range, and 0 if a
551 * ship is not in range. */
553 notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
555 struct emp_qelem *qp;
556 struct emp_qelem *next;
561 natp = getnatp(sectp->sct_own);
563 vrange = sectp->sct_type == SCT_RADAR ? 14 : 4;
564 vrange *= tfact(sectp->sct_own, 1.0) * sectp->sct_effic / 100.0;
572 for (qp = list->q_back; qp != list; qp = next) {
574 mlp = (struct ulist *)qp;
575 if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
577 if (natp->nat_flags & NF_COASTWATCH)
578 wu(0, sectp->sct_own,
579 "%s %s sighted at %s\n",
580 cname(mlp->unit.ship.shp_own),
581 prship(&mlp->unit.ship),
582 xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
585 setcont(sectp->sct_own, mlp->unit.ship.shp_own, FOUND_COAST);
592 shp_fort_interdiction(struct emp_qelem *list, coord newx, coord newy,
601 signed char notified[MAXNOC];
604 /* Inform neutral and worse */
605 for (i = 0; i < MAXNOC; ++i) {
606 if (relations_with(i, victim) <= NEUTRAL)
612 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
613 while (nxtsct(&ns, &fsect)) {
616 if (fsect.sct_own == victim)
618 if (notified[fsect.sct_own])
620 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
621 if (notify_coastguard(list, trange, &fsect))
622 notified[fsect.sct_own] = 1;
624 if (opt_NO_FORT_FIRE)
625 return 0; /* Only coastwatch notify in nofortfire */
626 /* Only fire at Hostile ships */
627 for (i = 0; i < MAXNOC; ++i) {
628 if (relations_with(i, victim) >= NEUTRAL)
631 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
632 while (nxtsct(&ns, &fsect)) {
633 if (!notified[fsect.sct_own])
635 range = roundrange(fortrange(&fsect));
636 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
639 dam = fort_fire(&fsect);
645 mpr(victim, "Incoming fire does %d damage!\n", dam);
647 mpr(victim, "%s fires at you for %d!\n",
648 xyas(fsect.sct_x, fsect.sct_y, victim), dam);
651 "%s fires at %s ships in %s for %d!\n",
652 xyas(fsect.sct_x, fsect.sct_y,
654 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
655 nreport(fsect.sct_own, N_SHP_SHELL, victim, 1);
658 shp_damage(list, totdam, 0, M_SUB, newx, newy);
663 shp_mission_interdiction(struct emp_qelem *list, coord x, coord y,
664 natid victim, int subs)
666 char *what = subs ? "subs" : "ships";
667 int wantflags = subs ? M_SUB : 0;
668 int nowantflags = subs ? 0 : M_SUB;
669 int mission = subs ? MI_SINTERDICT : MI_INTERDICT;
672 dam = unit_interdict(x, y, victim, what,
673 shp_easiest_target(list, wantflags, nowantflags),
676 shp_damage(list, dam, wantflags, nowantflags, x, y);
681 shp_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
685 if (shp_contains(list, newx, newy, 0, M_SUB)) {
686 stopping |= shp_fort_interdiction(list, newx, newy, victim);
688 if (shp_contains(list, newx, newy, 0, M_SUB)) {
689 stopping |= shp_mission_interdiction(list, newx, newy, victim, 0);
690 stopping |= shp_missile_interdiction(list, newx, newy, victim);
693 if (shp_contains(list, newx, newy, M_SUB, 0))
694 stopping |= shp_mission_interdiction(list, newx, newy, victim, 1);
698 /* high value of hardtarget is harder to hit */
700 shp_hardtarget(struct shpstr *sp)
704 struct mchrstr *mcp = mchr + sp->shp_type;
707 getsect(sp->shp_x, sp->shp_y, §);
708 onsea = sect.sct_type == SCT_WATER;
709 if (mcp->m_flags & M_SUB)
711 return (int)((sp->shp_effic / 100.0) *
712 (20 + shp_speed(sp) * onsea / 2.0 - vis));
716 shp_hit_mine(struct shpstr *sp)
720 mpr(sp->shp_own, "Kawhomp! Mine detected in %s!\n",
721 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
723 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
726 if (mchr[sp->shp_type].m_flags & M_SWEEP)
729 shipdamage(sp, ldround(m, 1));
735 shp_nav_one_sector(struct emp_qelem *list, int dir, natid actor,
739 struct emp_qelem *qp;
740 struct emp_qelem *next;
742 struct emp_qelem done;
752 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
753 unit_put(list, actor);
758 for (qp = list->q_back; qp != list; qp = next) {
760 mlp = (struct ulist *)qp;
761 newx = xnorm(mlp->unit.ship.shp_x + dx);
762 newy = ynorm(mlp->unit.ship.shp_y + dy);
763 getsect(newx, newy, §);
764 navigate = shp_check_nav(&mlp->unit.ship, §);
765 if (navigate != NAVOK ||
767 && relations_with(sect.sct_own, actor) < FRIENDLY)) {
768 if (navigate == NAV_CANAL)
770 "is too large to fit into the canal system at %s",
771 xyas(newx, newy, actor));
773 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
775 mpr(actor, "%s\n", dp);
778 shp_stays(actor, dp, mlp);
783 if (mlp->mobil <= 0.0) {
784 shp_stays(actor, "is out of mobility", mlp);
787 mobcost = shp_mobcost(&mlp->unit.ship);
788 mlp->unit.ship.shp_x = newx;
789 mlp->unit.ship.shp_y = newy;
790 if (mlp->mobil - mobcost < -127) {
793 mlp->mobil -= mobcost;
795 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
796 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
798 /* Now update the map for this ship */
799 rad_map_set(mlp->unit.ship.shp_own,
800 mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
801 mlp->unit.ship.shp_effic, mlp->unit.ship.shp_tech,
802 ((struct mchrstr *)mlp->chrp)->m_vrnge);
806 stopping |= shp_sweep(list, 0, 0, actor);
809 stopping |= shp_check_mines(list);
813 /* interdict ships sector by sector */
815 while (!QEMPTY(list)) {
816 mlp = (struct ulist *)list->q_back;
817 newx = mlp->unit.ship.shp_x;
818 newy = mlp->unit.ship.shp_y;
819 stopping |= shp_interdict(list, newx, newy, actor);
820 /* move survivors in this sector to done */
821 for (qp = list->q_back; qp != list; qp = next) {
823 mlp = (struct ulist *)qp;
824 if (mlp->unit.ship.shp_x == newx &&
825 mlp->unit.ship.shp_y == newy) {
827 emp_insque(qp, &done);
831 /* assign surviving ships back to list */
832 emp_insque(list, &done);
840 * Check for incoming missiles with a P_MAR flag.
841 * Return True=1 if the missile was shotdown.
844 * Chad Zabel, July 95
848 shp_missile_defense(coord dx, coord dy, natid bombown, int hardtarget)
853 double gun, eff, teff;
855 snxtitem_dist(&ni, EF_SHIP, dx, dy, 1);
857 while (nxtitem(&ni, &ship)) {
861 if (!(mchr[(int)ship.shp_type].m_flags & M_ANTIMISSILE))
864 if (relations_with(ship.shp_own, bombown) >= NEUTRAL)
867 if (ship.shp_effic < 60)
870 if (ship.shp_item[I_MILIT] < 1) /* do we have mil? */
872 if (ship.shp_item[I_GUN] < 1) /* we need at least 1 gun */
874 if (!shp_supply(&ship, I_SHELL, 2))
876 ship.shp_item[I_SHELL] -= 2;
877 putship(ship.shp_uid, &ship);
879 /* now calculate the odds */
880 gun = shp_usable_guns(&ship);
881 eff = ship.shp_effic / 100.0;
882 teff = ship.shp_tech / (ship.shp_tech + 200.0);
883 /* raise 4.5 for better interception -KHS */
884 hitchance = (int)(gun * eff * teff * 4.5) - hardtarget;
885 hitchance = LIMIT_TO(hitchance, 0, 100);
886 hit = pct_chance(hitchance);
888 mpr(bombown, "%s anti-missile system activated...%s\n",
890 hit ? "KABOOOM!! Missile destroyed\n"
891 : "SWOOSH!! anti-missile system failed!!");
892 mpr(ship.shp_own, "Ship #%i anti-missile system activated!\n",
894 mpr(ship.shp_own, "%d%% hitchance...%s\n", hitchance,
895 hit ? "KABOOOM!! Incoming missile destroyed!\n"
896 : "SWOOSH!! Missile evades anti-missile systems\n");
901 return 0; /* all attempts failed */
905 /* Fire missiles at a ship which has fired shells */
907 shp_missdef(struct shpstr *sp, natid victim)
909 struct emp_qelem list;
916 mlp = malloc(sizeof(struct ulist));
917 mlp->chrp = (struct empobj_chr *)&mchr[(int)sp->shp_type];
918 mlp->unit.ship = *sp;
919 mlp->mobil = sp->shp_mobil;
920 emp_insque(&mlp->queue, &list);
921 sprintf(buf, "%s", prship(&mlp->unit.ship));
924 shp_missile_interdiction(&list, sp->shp_x, sp->shp_y, sp->shp_own);
925 getship(sp->shp_uid, sp);
929 "missiles launched in defense did 100%% damage to %s\n",
931 wu(0, victim, "%s sunk!\n", buf);
932 } else if (eff > 0 && sp->shp_effic < eff) {
934 "missiles launched in defense did %d%% damage to %s\n",
935 100 * (eff - sp->shp_effic) / eff, buf);
942 shp_mobcost(struct shpstr *sp)
944 return speed_factor(sp->shp_effic * 0.01 * shp_speed(sp),
949 * Set SP's tech to TLEV along with everything else that depends on it.
952 shp_set_tech(struct shpstr *sp, int tlev)
954 struct mchrstr *mcp = mchr + sp->shp_type;
956 if (CANT_HAPPEN(tlev < mcp->m_tech))