2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, 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-2009
47 #include "prototypes.h"
54 static int shp_check_one_mines(struct ulist *);
55 static int shp_hit_mine(struct shpstr *);
56 static void shp_stays(natid, char *, struct ulist *);
59 shp_sel(struct nstr_item *ni, struct emp_qelem *list)
66 while (nxtitem(ni, &ship)) {
68 * It would be nice to let deities navigate foreign ships, but
69 * much of the code assumes that only the ship's owner can
72 if (ship.shp_own != player->cnum)
74 mcp = &mchr[(int)ship.shp_type];
76 if (ontradingblock(EF_SHIP, &ship)) {
77 pr("ship #%d inelligible - it's for sale.\n",
84 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
85 putship(ship.shp_uid, &ship);
86 mlp = malloc(sizeof(struct ulist));
87 mlp->chrp = (struct empobj_chr *)mcp;
88 mlp->unit.ship = ship;
89 mlp->mobil = ship.shp_mobil;
90 emp_insque(&mlp->queue, list);
94 /* This function assumes that the list was created by shp_sel */
96 shp_nav(struct emp_qelem *list, double *minmobp, double *maxmobp,
97 int *togetherp, natid actor)
100 struct emp_qelem *next;
111 for (qp = list->q_back; qp != list; qp = next) {
113 mlp = (struct ulist *)qp;
114 getship(mlp->unit.ship.shp_uid, &ship);
115 if (ship.shp_own != actor) {
116 mpr(actor, "%s was sunk at %s\n",
117 prship(&ship), xyas(ship.shp_x, ship.shp_y, actor));
118 emp_remque((struct emp_qelem *)mlp);
123 if (*ship.shp_path && !update_running) {
124 shp_stays(actor, "has a sail path", mlp);
125 mpr(actor, "Use `sail <#> -' to reset\n");
129 /* check crew - uws don't count */
130 if (ship.shp_item[I_MILIT] == 0 && ship.shp_item[I_CIVIL] == 0) {
131 shp_stays(actor, "is crewless", mlp);
134 if (!getsect(ship.shp_x, ship.shp_y, §)) {
135 shp_stays(actor, "was sucked into the sky by a strange looking spaceship", mlp); /* heh -KHS */
138 switch (shp_check_nav(§, &ship)) {
139 case CN_CONSTRUCTION:
140 shp_stays(actor, "is caught in a construction zone", mlp);
143 shp_stays(actor, "is landlocked", mlp);
149 shp_stays(actor, "was just swallowed by a big green worm", mlp);
157 if (ship.shp_x != allx || ship.shp_y != ally)
159 if (ship.shp_mobil + 1 < (int)mlp->mobil) {
160 mlp->mobil = ship.shp_mobil;
162 if (mlp->mobil < *minmobp)
163 *minmobp = mlp->mobil;
164 if (mlp->mobil > *maxmobp)
165 *maxmobp = mlp->mobil;
166 mlp->unit.ship = ship;
171 shp_sweep(struct emp_qelem *ship_list, int verbose, int takemob, natid actor)
173 struct emp_qelem *qp;
174 struct emp_qelem *next;
177 int mines, m, max, shells;
181 for (qp = ship_list->q_back; qp != ship_list; qp = next) {
183 mlp = (struct ulist *)qp;
184 if (!(((struct mchrstr *)mlp->chrp)->m_flags & M_SWEEP)) {
186 mpr(actor, "%s doesn't have minesweeping capability!\n",
187 prship(&mlp->unit.ship));
190 if (takemob && mlp->mobil <= 0.0) {
192 mpr(actor, "%s is out of mobility!\n",
193 prship(&mlp->unit.ship));
196 getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, §);
197 if (sect.sct_type != SCT_WATER) {
199 mpr(actor, "%s is not at sea. No mines there!\n",
200 prship(&mlp->unit.ship));
204 mlp->mobil -= shp_mobcost(&mlp->unit.ship);
205 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
207 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
208 if (!(mines = sect.sct_mines))
210 max = ((struct mchrstr *)mlp->chrp)->m_item[I_SHELL];
211 shells = mlp->unit.ship.shp_item[I_SHELL];
212 for (m = 0; mines > 0 && m < 5; m++) {
214 mpr(actor, "Sweep...\n");
216 shells = MIN(max, shells + 1);
217 changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
220 sect.sct_mines = mines;
221 mlp->unit.ship.shp_item[I_SHELL] = shells;
222 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
224 if (shp_check_one_mines(mlp)) {
236 shp_check_one_mines(struct ulist *mlp)
241 getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, §);
242 if (sect.sct_type != SCT_WATER)
246 if (chance(DMINE_HITCHANCE(sect.sct_mines))) {
247 actor = mlp->unit.ship.shp_own;
248 shp_hit_mine(&mlp->unit.ship);
250 if (map_set(actor, sect.sct_x, sect.sct_y, 'X', 0))
253 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
254 if (!mlp->unit.ship.shp_own)
261 shp_check_mines(struct emp_qelem *ship_list)
263 struct emp_qelem *qp;
264 struct emp_qelem *next;
268 for (qp = ship_list->q_back; qp != ship_list; qp = next) {
270 mlp = (struct ulist *)qp;
271 if (shp_check_one_mines(mlp)) {
282 shp_stays(natid actor, char *str, struct ulist *mlp)
284 mpr(actor, "%s %s & stays in %s\n",
285 prship(&mlp->unit.ship), str,
286 xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, actor));
287 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
288 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
289 emp_remque((struct emp_qelem *)mlp);
294 shp_check_nav(struct sctstr *sect, struct shpstr *shp)
296 switch (dchr[sect->sct_type].d_nav) {
300 if (mchr[(int)shp->shp_type].m_flags & M_CANAL) {
301 if (sect->sct_effic < 2)
302 return CN_CONSTRUCTION;
304 return CN_LANDLOCKED;
307 if (sect->sct_effic < 2)
308 return CN_CONSTRUCTION;
311 if (sect->sct_effic < 60)
312 return CN_CONSTRUCTION;
315 return CN_LANDLOCKED;
321 sect_has_dock(struct sctstr *sect)
323 switch (dchr[sect->sct_type].d_nav) {
333 shp_count(struct emp_qelem *list, int wantflags, int nowantflags,
336 struct emp_qelem *qp;
337 struct emp_qelem *next;
341 for (qp = list->q_back; qp != list; qp = next) {
343 mlp = (struct ulist *)qp;
344 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
347 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
350 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
358 shp_damage_one(struct ulist *mlp, int dam)
360 /* ship might have changed (launched interceptors, missile defense) */
361 getship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
362 shipdamage(&mlp->unit.ship, dam);
363 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
364 if (!mlp->unit.ship.shp_own) {
365 emp_remque((struct emp_qelem *)mlp);
371 shp_damage(struct emp_qelem *list, int totdam, int wantflags,
372 int nowantflags, int x, int y)
374 struct emp_qelem *qp;
375 struct emp_qelem *next;
381 || !(count = shp_count(list, wantflags, nowantflags, x, y)))
383 dam = ldround((double)totdam / count, 1);
384 for (qp = list->q_back; qp != list; qp = next) {
386 mlp = (struct ulist *)qp;
387 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
390 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
393 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
395 shp_damage_one(mlp, dam);
401 shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
404 struct emp_qelem *qp;
405 struct emp_qelem *next;
408 for (qp = list->q_back; qp != list; qp = next) {
410 mlp = (struct ulist *)qp;
411 /* If the ship isn't in the requested sector, then continue */
412 if (newx != mlp->unit.ship.shp_x || newy != mlp->unit.ship.shp_y)
415 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
418 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
425 static struct ulist *
426 most_valuable_ship(struct emp_qelem *list, coord x, coord y)
428 struct emp_qelem *qp;
429 struct emp_qelem *next;
431 struct ulist *mvs = NULL;
433 for (qp = list->q_back; qp != list; qp = next) {
435 mlp = (struct ulist *)qp;
436 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
438 if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
440 if (!((struct mchrstr *)mlp->chrp)->m_nxlight &&
441 !((struct mchrstr *)mlp->chrp)->m_nchoppers &&
442 ((struct mchrstr *)mlp->chrp)->m_cost < 1000 &&
443 !((struct mchrstr *)mlp->chrp)->m_nplanes &&
444 !((struct mchrstr *)mlp->chrp)->m_nland)
450 if (((struct mchrstr *)mlp->chrp)->m_cost * mlp->unit.ship.shp_effic >
451 ((struct mchrstr *)mlp->chrp)->m_cost * mvs->unit.ship.shp_effic)
458 shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
460 struct emp_qelem *qp;
461 struct emp_qelem *next;
464 int easiest = 9876; /* things start great for victim */
467 for (qp = list->q_back; qp != list; qp = next) {
469 mlp = (struct ulist *)qp;
471 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
474 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
476 hard = shp_hardtarget(&mlp->unit.ship);
478 easiest = hard; /* things get worse for victim */
481 return easiest - count;
485 shp_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
490 struct emp_qelem msl_list, *qp, *newqp;
494 mvs = most_valuable_ship(list, newx, newy);
498 msl_sel(&msl_list, newx, newy, victim, P_T | P_MAR, 0, MI_INTERDICT);
500 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
502 plp = (struct plist *)qp;
504 if (mvs && mission_pln_equip(plp, NULL, 'p') >= 0) {
505 if (msl_launch(&plp->plane, EF_SHIP, prship(&mvs->unit.ship),
506 newx, newy, victim, &sublaunch) < 0)
509 if (msl_hit(&plp->plane,
510 shp_hardtarget(&mvs->unit.ship), EF_SHIP,
511 N_SHP_MISS, N_SHP_SMISS, sublaunch, victim)) {
512 dam = pln_damage(&plp->plane, 'p', 1);
514 "missile interdiction mission does %d damage to %s!\n",
515 dam, prship(&mvs->unit.ship));
516 shp_damage_one(mvs, dam);
518 dam = pln_damage(&plp->plane, 'p', 0);
519 collateral_damage(newx, newy, dam);
521 mvs = most_valuable_ship(list, newx, newy);
523 plp->plane.pln_effic = 0;
524 putplane(plp->plane.pln_uid, &plp->plane);
533 /* Note that this function has a side effect - it uses coastwatch
534 * ranges to see if it should fire upon a ship. So, this function
535 * is expected to return positive if a ship is in range, and 0 if a
536 * ship is not in range. */
538 notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
540 struct emp_qelem *qp;
541 struct emp_qelem *next;
546 natp = getnatp(sectp->sct_own);
548 vrange = sectp->sct_type == SCT_RADAR ? 14 : 4;
549 vrange *= tfact(sectp->sct_own, 1.0) * sectp->sct_effic / 100.0;
557 for (qp = list->q_back; qp != list; qp = next) {
559 mlp = (struct ulist *)qp;
560 if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
562 if (natp->nat_flags & NF_COASTWATCH)
563 wu(0, sectp->sct_own,
564 "%s %s sighted at %s\n",
565 cname(mlp->unit.ship.shp_own),
566 prship(&mlp->unit.ship),
567 xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
570 setcont(sectp->sct_own, mlp->unit.ship.shp_own, FOUND_COAST);
577 shp_fort_interdiction(struct emp_qelem *list, coord newx, coord newy,
586 signed char notified[MAXNOC];
589 /* Inform neutral and worse */
590 for (i = 0; i < MAXNOC; ++i) {
591 if (relations_with(i, victim) <= NEUTRAL)
597 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
598 while (nxtsct(&ns, &fsect)) {
601 if (fsect.sct_own == victim)
603 if (notified[fsect.sct_own])
605 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
606 if (notify_coastguard(list, trange, &fsect))
607 notified[fsect.sct_own] = 1;
609 if (opt_NO_FORT_FIRE)
610 return 0; /* Only coastwatch notify in nofortfire */
611 /* Only fire at Hostile ships */
612 for (i = 0; i < MAXNOC; ++i) {
613 if (relations_with(i, victim) >= NEUTRAL)
616 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
617 while (nxtsct(&ns, &fsect)) {
618 if (!notified[fsect.sct_own])
620 range = roundrange(fortrange(&fsect));
621 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
624 dam = fort_fire(&fsect);
630 mpr(victim, "Incoming fire does %d damage!\n", dam);
632 mpr(victim, "%s fires at you for %d!\n",
633 xyas(fsect.sct_x, fsect.sct_y, victim), dam);
636 "%s fires at %s ships in %s for %d!\n",
637 xyas(fsect.sct_x, fsect.sct_y,
639 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
640 nreport(fsect.sct_own, N_SHP_SHELL, victim, 1);
643 shp_damage(list, totdam, 0, M_SUB, newx, newy);
648 shp_mission_interdiction(struct emp_qelem *list, coord x, coord y,
649 natid victim, int subs)
651 char *what = subs ? "subs" : "ships";
652 int wantflags = subs ? M_SUB : 0;
653 int nowantflags = subs ? 0 : M_SUB;
654 int mission = subs ? MI_SINTERDICT : MI_INTERDICT;
657 dam = unit_interdict(x, y, victim, what,
658 shp_easiest_target(list, wantflags, nowantflags),
661 shp_damage(list, dam, wantflags, nowantflags, x, y);
666 shp_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
670 if (shp_contains(list, newx, newy, 0, M_SUB)) {
671 stopping |= shp_fort_interdiction(list, newx, newy, victim);
673 if (shp_contains(list, newx, newy, 0, M_SUB)) {
674 stopping |= shp_mission_interdiction(list, newx, newy, victim, 0);
675 stopping |= shp_missile_interdiction(list, newx, newy, victim);
678 if (shp_contains(list, newx, newy, M_SUB, 0))
679 stopping |= shp_mission_interdiction(list, newx, newy, victim, 1);
683 /* high value of hardtarget is harder to hit */
685 shp_hardtarget(struct shpstr *sp)
689 struct mchrstr *mcp = mchr + sp->shp_type;
692 getsect(sp->shp_x, sp->shp_y, §);
693 onsea = sect.sct_type == SCT_WATER;
694 if (mcp->m_flags & M_SUB)
696 return (int)((sp->shp_effic / 100.0) *
697 (20 + shp_speed(sp) * onsea / 2.0 - vis));
701 shp_hit_mine(struct shpstr *sp)
705 mpr(sp->shp_own, "Kawhomp! Mine detected in %s!\n",
706 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
708 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
711 if (mchr[sp->shp_type].m_flags & M_SWEEP)
714 shipdamage(sp, ldround(m, 1));
720 shp_nav_one_sector(struct emp_qelem *list, int dir, natid actor,
724 struct emp_qelem *qp;
725 struct emp_qelem *next;
727 struct emp_qelem done;
737 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
738 unit_put(list, actor);
743 for (qp = list->q_back; qp != list; qp = next) {
745 mlp = (struct ulist *)qp;
746 newx = xnorm(mlp->unit.ship.shp_x + dx);
747 newy = ynorm(mlp->unit.ship.shp_y + dy);
748 getsect(newx, newy, §);
749 navigate = shp_check_nav(§, &mlp->unit.ship);
750 if (navigate != CN_NAVIGABLE ||
752 && relations_with(sect.sct_own, actor) < FRIENDLY)) {
753 if (dchr[sect.sct_type].d_nav == NAV_CANAL &&
754 !(((struct mchrstr *)mlp->chrp)->m_flags & M_CANAL) &&
755 navigate == CN_LANDLOCKED)
757 "is too large to fit into the canal system at %s",
758 xyas(newx, newy, actor));
760 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
762 mpr(actor, "%s\n", dp);
765 shp_stays(actor, dp, mlp);
770 if (mlp->mobil <= 0.0) {
771 shp_stays(actor, "is out of mobility", mlp);
774 mobcost = shp_mobcost(&mlp->unit.ship);
775 mlp->unit.ship.shp_x = newx;
776 mlp->unit.ship.shp_y = newy;
777 if (mlp->mobil - mobcost < -127) {
780 mlp->mobil -= mobcost;
782 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
783 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
785 /* Now update the map for this ship */
786 rad_map_set(mlp->unit.ship.shp_own,
787 mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
788 mlp->unit.ship.shp_effic, mlp->unit.ship.shp_tech,
789 ((struct mchrstr *)mlp->chrp)->m_vrnge);
793 stopping |= shp_sweep(list, 0, 0, actor);
796 stopping |= shp_check_mines(list);
800 /* interdict ships sector by sector */
802 while (!QEMPTY(list)) {
803 mlp = (struct ulist *)list->q_back;
804 newx = mlp->unit.ship.shp_x;
805 newy = mlp->unit.ship.shp_y;
806 stopping |= shp_interdict(list, newx, newy, actor);
807 /* move survivors in this sector to done */
808 for (qp = list->q_back; qp != list; qp = next) {
810 mlp = (struct ulist *)qp;
811 if (mlp->unit.ship.shp_x == newx &&
812 mlp->unit.ship.shp_y == newy) {
814 emp_insque(qp, &done);
818 /* assign surviving ships back to list */
819 emp_insque(list, &done);
827 * Check for incoming missiles with a P_MAR flag.
828 * Return True=1 if the missile was shotdown.
831 * Chad Zabel, July 95
835 shp_missile_defense(coord dx, coord dy, natid bombown, int hardtarget)
840 double gun, eff, teff;
842 snxtitem_dist(&ni, EF_SHIP, dx, dy, 1);
844 while (nxtitem(&ni, &ship)) {
848 if (!(mchr[(int)ship.shp_type].m_flags & M_ANTIMISSILE))
851 if (relations_with(ship.shp_own, bombown) >= NEUTRAL)
854 if (ship.shp_effic < 60)
857 if (ship.shp_item[I_MILIT] < 1) /* do we have mil? */
859 if (ship.shp_item[I_GUN] < 1) /* we need at least 1 gun */
861 if (!shp_supply(&ship, I_SHELL, 2))
863 ship.shp_item[I_SHELL] -= 2;
864 putship(ship.shp_uid, &ship);
866 /* now calculate the odds */
867 gun = shp_usable_guns(&ship);
868 eff = ship.shp_effic / 100.0;
869 teff = ship.shp_tech / (ship.shp_tech + 200.0);
870 /* raise 4.5 for better interception -KHS */
871 hitchance = (int)(gun * eff * teff * 4.5) - hardtarget;
876 hit = roll(100) <= hitchance;
878 mpr(bombown, "%s anti-missile system activated...%s\n",
880 hit ? "KABOOOM!! Missile destroyed\n"
881 : "SWOOSH!! anti-missile system failed!!");
882 mpr(ship.shp_own, "Ship #%i anti-missile system activated!\n",
884 mpr(ship.shp_own, "%d%% hitchance...%s\n", hitchance,
885 hit ? "KABOOOM!! Incoming missile destroyed!\n"
886 : "SWOOSH!! Missile evades anti-missile systems\n");
891 return 0; /* all attempts failed */
895 /* Fire missiles at a ship which has fired shells */
897 shp_missdef(struct shpstr *sp, natid victim)
899 struct emp_qelem list;
906 mlp = malloc(sizeof(struct ulist));
907 mlp->chrp = (struct empobj_chr *)&mchr[(int)sp->shp_type];
908 mlp->unit.ship = *sp;
909 mlp->mobil = sp->shp_mobil;
910 emp_insque(&mlp->queue, &list);
911 sprintf(buf, "%s", prship(&mlp->unit.ship));
914 shp_missile_interdiction(&list, sp->shp_x, sp->shp_y, sp->shp_own);
915 getship(sp->shp_uid, sp);
919 "missiles launched in defense did 100%% damage to %s\n",
921 wu(0, victim, "%s sunk!\n", buf);
922 } else if (eff > 0 && sp->shp_effic < eff) {
924 "missiles launched in defense did %d%% damage to %s\n",
925 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))