2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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 *);
57 static void shp_mess(char *, struct ulist *);
60 shp_sel(struct nstr_item *ni, struct emp_qelem *list)
67 while (nxtitem(ni, &ship)) {
69 * It would be nice to let deities navigate foreign ships, but
70 * much of the code assumes that only the ship's owner can
73 if (ship.shp_own != player->cnum)
75 mcp = &mchr[(int)ship.shp_type];
77 if (ontradingblock(EF_SHIP, &ship)) {
78 pr("ship #%d inelligible - it's for sale.\n",
85 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
86 putship(ship.shp_uid, &ship);
87 mlp = malloc(sizeof(struct ulist));
88 mlp->chrp = (struct empobj_chr *)mcp;
89 mlp->unit.ship = ship;
90 mlp->mobil = ship.shp_mobil;
91 emp_insque(&mlp->queue, list);
95 /* This function assumes that the list was created by shp_sel */
97 shp_nav(struct emp_qelem *list, double *minmobp, double *maxmobp,
98 int *togetherp, natid actor)
100 struct emp_qelem *qp;
101 struct emp_qelem *next;
112 for (qp = list->q_back; qp != list; qp = next) {
114 mlp = (struct ulist *)qp;
115 getship(mlp->unit.ship.shp_uid, &ship);
116 if (ship.shp_own != actor) {
117 mpr(actor, "%s was sunk at %s\n",
118 prship(&ship), xyas(ship.shp_x, ship.shp_y, actor));
119 emp_remque((struct emp_qelem *)mlp);
124 if (*ship.shp_path && !update_running) {
125 shp_mess("has a sail path", mlp);
126 mpr(actor, "Use `sail <#> -' to reset\n");
130 /* check crew - uws don't count */
131 if (ship.shp_item[I_MILIT] == 0 && ship.shp_item[I_CIVIL] == 0) {
132 shp_mess("is crewless", mlp);
135 if (!getsect(ship.shp_x, ship.shp_y, §)) {
136 shp_mess("was sucked into the sky by a strange looking spaceship", mlp); /* heh -KHS */
139 switch (shp_check_nav(§, &ship)) {
140 case CN_CONSTRUCTION:
141 shp_mess("is caught in a construction zone", mlp);
144 shp_mess("is landlocked", mlp);
150 shp_mess("was just swallowed by a big green worm", mlp);
158 if (ship.shp_x != allx || ship.shp_y != ally)
160 if (ship.shp_mobil + 1 < (int)mlp->mobil) {
161 mlp->mobil = ship.shp_mobil;
163 if (mlp->mobil < *minmobp)
164 *minmobp = mlp->mobil;
165 if (mlp->mobil > *maxmobp)
166 *maxmobp = mlp->mobil;
167 mlp->unit.ship = ship;
172 shp_sweep(struct emp_qelem *ship_list, int verbose, int takemob, natid actor)
174 struct emp_qelem *qp;
175 struct emp_qelem *next;
178 int mines, m, max, shells;
182 for (qp = ship_list->q_back; qp != ship_list; qp = next) {
184 mlp = (struct ulist *)qp;
185 if (!(((struct mchrstr *)mlp->chrp)->m_flags & M_SWEEP)) {
187 mpr(actor, "%s doesn't have minesweeping capability!\n",
188 prship(&mlp->unit.ship));
191 if (takemob && mlp->mobil <= 0.0) {
193 mpr(actor, "%s is out of mobility!\n",
194 prship(&mlp->unit.ship));
197 getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, §);
198 if (sect.sct_type != SCT_WATER) {
200 mpr(actor, "%s is not at sea. No mines there!\n",
201 prship(&mlp->unit.ship));
205 mlp->mobil -= shp_mobcost(&mlp->unit.ship);
206 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
208 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
209 if (!(mines = sect.sct_mines))
211 max = ((struct mchrstr *)mlp->chrp)->m_item[I_SHELL];
212 shells = mlp->unit.ship.shp_item[I_SHELL];
213 for (m = 0; mines > 0 && m < 5; m++) {
215 mpr(actor, "Sweep...\n");
217 shells = MIN(max, shells + 1);
218 changed |= map_set(actor, sect.sct_x, sect.sct_y, 'X', 0);
221 sect.sct_mines = mines;
222 mlp->unit.ship.shp_item[I_SHELL] = shells;
223 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
225 if (shp_check_one_mines(mlp)) {
237 shp_check_one_mines(struct ulist *mlp)
242 getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, §);
243 if (sect.sct_type != SCT_WATER)
247 if (chance(DMINE_HITCHANCE(sect.sct_mines))) {
248 actor = mlp->unit.ship.shp_own;
249 shp_hit_mine(&mlp->unit.ship);
251 if (map_set(actor, sect.sct_x, sect.sct_y, 'X', 0))
254 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
255 if (!mlp->unit.ship.shp_own)
262 shp_check_mines(struct emp_qelem *ship_list)
264 struct emp_qelem *qp;
265 struct emp_qelem *next;
269 for (qp = ship_list->q_back; qp != ship_list; qp = next) {
271 mlp = (struct ulist *)qp;
272 if (shp_check_one_mines(mlp)) {
283 shp_mess(char *str, struct ulist *mlp)
285 mpr(mlp->unit.ship.shp_own, "%s %s & stays in %s\n",
286 prship(&mlp->unit.ship),
287 str, xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
288 mlp->unit.ship.shp_own));
289 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
290 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
291 emp_remque((struct emp_qelem *)mlp);
296 shp_check_nav(struct sctstr *sect, struct shpstr *shp)
298 switch (dchr[sect->sct_type].d_nav) {
302 if (mchr[(int)shp->shp_type].m_flags & M_CANAL) {
303 if (sect->sct_effic < 2)
304 return CN_CONSTRUCTION;
306 return CN_LANDLOCKED;
309 if (sect->sct_effic < 2)
310 return CN_CONSTRUCTION;
313 if (sect->sct_effic < 60)
314 return CN_CONSTRUCTION;
317 return CN_LANDLOCKED;
323 sect_has_dock(struct sctstr *sect)
325 switch (dchr[sect->sct_type].d_nav) {
335 shp_count(struct emp_qelem *list, int wantflags, int nowantflags,
338 struct emp_qelem *qp;
339 struct emp_qelem *next;
343 for (qp = list->q_back; qp != list; qp = next) {
345 mlp = (struct ulist *)qp;
346 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
349 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
352 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
360 shp_damage_one(struct ulist *mlp, int dam)
362 /* ship might have changed (launched interceptors, missile defense) */
363 getship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
364 shipdamage(&mlp->unit.ship, dam);
365 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
366 if (!mlp->unit.ship.shp_own) {
367 emp_remque((struct emp_qelem *)mlp);
373 shp_damage(struct emp_qelem *list, int totdam, int wantflags,
374 int nowantflags, int x, int y)
376 struct emp_qelem *qp;
377 struct emp_qelem *next;
383 || !(count = shp_count(list, wantflags, nowantflags, x, y)))
385 dam = ldround((double)totdam / count, 1);
386 for (qp = list->q_back; qp != list; qp = next) {
388 mlp = (struct ulist *)qp;
389 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
392 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
395 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
397 shp_damage_one(mlp, dam);
403 shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
406 struct emp_qelem *qp;
407 struct emp_qelem *next;
410 for (qp = list->q_back; qp != list; qp = next) {
412 mlp = (struct ulist *)qp;
413 /* If the ship isn't in the requested sector, then continue */
414 if (newx != mlp->unit.ship.shp_x || newy != mlp->unit.ship.shp_y)
417 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
420 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
427 static struct ulist *
428 most_valuable_ship(struct emp_qelem *list, coord x, coord y)
430 struct emp_qelem *qp;
431 struct emp_qelem *next;
433 struct ulist *mvs = NULL;
435 for (qp = list->q_back; qp != list; qp = next) {
437 mlp = (struct ulist *)qp;
438 if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
440 if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
442 if (!((struct mchrstr *)mlp->chrp)->m_nxlight &&
443 !((struct mchrstr *)mlp->chrp)->m_nchoppers &&
444 ((struct mchrstr *)mlp->chrp)->m_cost < 1000 &&
445 !((struct mchrstr *)mlp->chrp)->m_nplanes &&
446 !((struct mchrstr *)mlp->chrp)->m_nland)
452 if (((struct mchrstr *)mlp->chrp)->m_cost * mlp->unit.ship.shp_effic >
453 ((struct mchrstr *)mlp->chrp)->m_cost * mvs->unit.ship.shp_effic)
460 shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
462 struct emp_qelem *qp;
463 struct emp_qelem *next;
466 int easiest = 9876; /* things start great for victim */
469 for (qp = list->q_back; qp != list; qp = next) {
471 mlp = (struct ulist *)qp;
473 (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
476 ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
478 hard = shp_hardtarget(&mlp->unit.ship);
480 easiest = hard; /* things get worse for victim */
483 return easiest - count;
487 shp_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
492 struct emp_qelem msl_list, *qp, *newqp;
496 mvs = most_valuable_ship(list, newx, newy);
500 msl_sel(&msl_list, newx, newy, victim, P_T | P_MAR, 0, MI_INTERDICT);
502 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
504 plp = (struct plist *)qp;
506 if (mvs && mission_pln_equip(plp, NULL, 'p') >= 0) {
507 if (msl_launch(&plp->plane, EF_SHIP, prship(&mvs->unit.ship),
508 newx, newy, victim, &sublaunch) < 0)
511 if (msl_hit(&plp->plane,
512 shp_hardtarget(&mvs->unit.ship), EF_SHIP,
513 N_SHP_MISS, N_SHP_SMISS, sublaunch, victim)) {
514 dam = pln_damage(&plp->plane, 'p', 1);
516 "missile interdiction mission does %d damage to %s!\n",
517 dam, prship(&mvs->unit.ship));
518 shp_damage_one(mvs, dam);
520 dam = pln_damage(&plp->plane, 'p', 0);
521 collateral_damage(newx, newy, dam);
523 mvs = most_valuable_ship(list, newx, newy);
525 plp->plane.pln_effic = 0;
526 putplane(plp->plane.pln_uid, &plp->plane);
535 /* Note that this function has a side effect - it uses coastwatch
536 * ranges to see if it should fire upon a ship. So, this function
537 * is expected to return positive if a ship is in range, and 0 if a
538 * ship is not in range. */
540 notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
542 struct emp_qelem *qp;
543 struct emp_qelem *next;
548 natp = getnatp(sectp->sct_own);
550 vrange = sectp->sct_type == SCT_RADAR ? 14 : 4;
551 vrange *= tfact(sectp->sct_own, 1.0) * sectp->sct_effic / 100.0;
559 for (qp = list->q_back; qp != list; qp = next) {
561 mlp = (struct ulist *)qp;
562 if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
564 if (natp->nat_flags & NF_COASTWATCH)
565 wu(0, sectp->sct_own,
566 "%s %s sighted at %s\n",
567 cname(mlp->unit.ship.shp_own),
568 prship(&mlp->unit.ship),
569 xyas(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
572 setcont(sectp->sct_own, mlp->unit.ship.shp_own, FOUND_COAST);
579 shp_fort_interdiction(struct emp_qelem *list, coord newx, coord newy,
588 signed char notified[MAXNOC];
591 /* Inform neutral and worse */
592 for (i = 0; i < MAXNOC; ++i) {
593 if (getrel(getnatp(i), victim) <= NEUTRAL)
599 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
600 while (nxtsct(&ns, &fsect)) {
603 if (fsect.sct_own == victim)
605 if (notified[fsect.sct_own])
607 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
608 if (notify_coastguard(list, trange, &fsect))
609 notified[fsect.sct_own] = 1;
611 if (opt_NO_FORT_FIRE)
612 return 0; /* Only coastwatch notify in nofortfire */
613 /* Only fire at Hostile ships */
614 for (i = 0; i < MAXNOC; ++i) {
615 if (getrel(getnatp(i), victim) >= NEUTRAL)
618 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
619 while (nxtsct(&ns, &fsect)) {
620 if (!notified[fsect.sct_own])
622 range = roundrange(fortrange(&fsect));
623 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
626 dam = fort_fire(&fsect);
632 mpr(victim, "Incoming fire does %d damage!\n", dam);
634 mpr(victim, "%s fires at you for %d!\n",
635 xyas(fsect.sct_x, fsect.sct_y, victim), dam);
638 "%s fires at %s ships in %s for %d!\n",
639 xyas(fsect.sct_x, fsect.sct_y,
641 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
642 nreport(fsect.sct_own, N_SHP_SHELL, victim, 1);
645 shp_damage(list, totdam, 0, M_SUB, newx, newy);
650 shp_mission_interdiction(struct emp_qelem *list, coord x, coord y,
651 natid victim, int subs)
653 char *what = subs ? "subs" : "ships";
654 int wantflags = subs ? M_SUB : 0;
655 int nowantflags = subs ? 0 : M_SUB;
656 int mission = subs ? MI_SINTERDICT : MI_INTERDICT;
659 dam = unit_interdict(x, y, victim, what,
660 shp_easiest_target(list, wantflags, nowantflags),
663 shp_damage(list, dam, wantflags, nowantflags, x, y);
668 shp_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
672 if (shp_contains(list, newx, newy, 0, M_SUB)) {
673 stopping |= shp_fort_interdiction(list, newx, newy, victim);
675 if (shp_contains(list, newx, newy, 0, M_SUB)) {
676 stopping |= shp_mission_interdiction(list, newx, newy, victim, 0);
677 stopping |= shp_missile_interdiction(list, newx, newy, victim);
680 if (shp_contains(list, newx, newy, M_SUB, 0))
681 stopping |= shp_mission_interdiction(list, newx, newy, victim, 1);
685 /* high value of hardtarget is harder to hit */
687 shp_hardtarget(struct shpstr *sp)
691 struct mchrstr *mcp = mchr + sp->shp_type;
694 getsect(sp->shp_x, sp->shp_y, §);
695 onsea = sect.sct_type == SCT_WATER;
696 if (mcp->m_flags & M_SUB)
698 return (int)((sp->shp_effic / 100.0) *
699 (20 + shp_speed(sp) * onsea / 2.0 - vis));
703 shp_hit_mine(struct shpstr *sp)
707 mpr(sp->shp_own, "Kawhomp! Mine detected in %s!\n",
708 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
710 nreport(sp->shp_own, N_HIT_MINE, 0, 1);
713 if (mchr[sp->shp_type].m_flags & M_SWEEP)
716 shipdamage(sp, ldround(m, 1));
722 shp_nav_one_sector(struct emp_qelem *list, int dir, natid actor,
726 struct emp_qelem *qp;
727 struct emp_qelem *next;
729 struct emp_qelem done;
736 double tech; /* for mapping */
737 double tf; /* for mapping */
741 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
742 unit_put(list, actor);
747 for (qp = list->q_back; qp != list; qp = next) {
749 mlp = (struct ulist *)qp;
750 newx = xnorm(mlp->unit.ship.shp_x + dx);
751 newy = ynorm(mlp->unit.ship.shp_y + dy);
752 getsect(newx, newy, §);
753 navigate = shp_check_nav(§, &mlp->unit.ship);
754 if (navigate != CN_NAVIGABLE ||
755 (sect.sct_own && actor != sect.sct_own &&
756 getrel(getnatp(sect.sct_own), actor) < FRIENDLY)) {
757 if (dchr[sect.sct_type].d_nav == NAV_CANAL &&
758 !(((struct mchrstr *)mlp->chrp)->m_flags & M_CANAL) &&
759 navigate == CN_LANDLOCKED)
761 "is too large to fit into the canal system at %s",
762 xyas(newx, newy, actor));
764 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
766 mpr(actor, "%s\n", dp);
774 if (mlp->mobil <= 0.0) {
775 shp_mess("is out of mobility", mlp);
778 mobcost = shp_mobcost(&mlp->unit.ship);
779 mlp->unit.ship.shp_x = newx;
780 mlp->unit.ship.shp_y = newy;
781 if (mlp->mobil - mobcost < -127) {
784 mlp->mobil -= mobcost;
786 mlp->unit.ship.shp_mobil = (int)mlp->mobil;
787 putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
789 /* Now update the map for this ship */
790 tech = techfact(mlp->unit.ship.shp_tech,
791 ((struct mchrstr *)mlp->chrp)->m_vrnge);
792 if (((struct mchrstr *)mlp->chrp)->m_flags & M_SONAR)
793 tf = techfact(mlp->unit.ship.shp_tech, 1.0);
796 radmapupd(mlp->unit.ship.shp_own,
797 mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
798 (int)mlp->unit.ship.shp_effic, (int)tech, tf);
802 stopping |= shp_sweep(list, 0, 0, actor);
805 stopping |= shp_check_mines(list);
809 /* interdict ships sector by sector */
811 while (!QEMPTY(list)) {
812 mlp = (struct ulist *)list->q_back;
813 newx = mlp->unit.ship.shp_x;
814 newy = mlp->unit.ship.shp_y;
815 stopping |= shp_interdict(list, newx, newy, actor);
816 /* move survivors in this sector to done */
817 for (qp = list->q_back; qp != list; qp = next) {
819 mlp = (struct ulist *)qp;
820 if (mlp->unit.ship.shp_x == newx &&
821 mlp->unit.ship.shp_y == newy) {
823 emp_insque(qp, &done);
827 /* assign surviving ships back to list */
828 emp_insque(list, &done);
836 * Check for incoming missiles with a P_MAR flag.
837 * Return True=1 if the missile was shotdown.
840 * Chad Zabel, July 95
844 shp_missile_defense(coord dx, coord dy, natid bombown, int hardtarget)
849 double gun, eff, teff;
851 snxtitem_dist(&ni, EF_SHIP, dx, dy, 1);
853 while (nxtitem(&ni, &ship)) {
857 if (!(mchr[(int)ship.shp_type].m_flags & M_ANTIMISSILE))
860 if (getrel(getnatp(ship.shp_own), bombown) >= NEUTRAL)
863 if (ship.shp_effic < 60)
866 if (ship.shp_item[I_MILIT] < 1) /* do we have mil? */
868 if (ship.shp_item[I_GUN] < 1) /* we need at least 1 gun */
870 if (!shp_supply(&ship, I_SHELL, 2))
872 ship.shp_item[I_SHELL] -= 2;
873 putship(ship.shp_uid, &ship);
875 /* now calculate the odds */
876 gun = shp_usable_guns(&ship);
877 eff = ship.shp_effic / 100.0;
878 teff = ship.shp_tech / (ship.shp_tech + 200.0);
879 /* raise 4.5 for better interception -KHS */
880 hitchance = (int)(gun * eff * teff * 4.5) - hardtarget;
885 hit = roll(100) <= hitchance;
887 mpr(bombown, "%s anti-missile system activated...%s\n",
889 hit ? "KABOOOM!! Missile destroyed\n"
890 : "SWOOSH!! anti-missile system failed!!");
891 mpr(ship.shp_own, "Ship #%i anti-missile system activated!\n",
893 mpr(ship.shp_own, "%d%% hitchance...%s\n", hitchance,
894 hit ? "KABOOOM!! Incoming missile destroyed!\n"
895 : "SWOOSH!! Missile evades anti-missile systems\n");
900 return 0; /* all attempts failed */
904 /* Fire missiles at a ship which has fired shells */
906 shp_missdef(struct shpstr *sp, natid victim)
908 struct emp_qelem list;
915 mlp = malloc(sizeof(struct ulist));
916 mlp->chrp = (struct empobj_chr *)&mchr[(int)sp->shp_type];
917 mlp->unit.ship = *sp;
918 mlp->mobil = sp->shp_mobil;
919 emp_insque(&mlp->queue, &list);
920 sprintf(buf, "%s", prship(&mlp->unit.ship));
923 shp_missile_interdiction(&list, sp->shp_x, sp->shp_y, sp->shp_own);
924 getship(sp->shp_uid, sp);
928 "missiles launched in defense did 100%% damage to %s\n",
930 wu(0, victim, "%s sunk!\n", buf);
931 } else if (eff > 0 && sp->shp_effic < eff) {
933 "missiles launched in defense did %d%% damage to %s\n",
934 100 * (eff - sp->shp_effic) / eff, buf);
941 shp_mobcost(struct shpstr *sp)
943 return speed_factor(sp->shp_effic * 0.01 * shp_speed(sp),
948 * Set SP's tech to TLEV along with everything else that depends on it.
951 shp_set_tech(struct shpstr *sp, int tlev)
953 struct mchrstr *mcp = mchr + sp->shp_type;
955 if (CANT_HAPPEN(tlev < mcp->m_tech))