2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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 * landgun.c: Fire weapons
29 * Known contributors to this file:
30 * Markus Armbruster, 2006-2013
41 #include "prototypes.h"
46 fortgun(int effic, int guns)
49 double g = MIN(guns, 7);
51 d = (roll(30) + 19.0) * (g / 7.0);
57 seagun(int effic, int guns)
69 landunitgun(int effic, int guns)
81 * Fire from fortress @sp.
82 * Use ammo, resupply if necessary.
83 * Return damage if the fortress fires, else -1.
86 fort_fire(struct sctstr *sp)
88 int guns = sp->sct_item[I_GUN];
90 if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
92 if (sp->sct_item[I_MILIT] < 5 || guns == 0)
94 if (!sct_supply(sp, I_SHELL, 1))
96 sp->sct_item[I_SHELL]--;
97 return (int)fortgun(sp->sct_effic, guns);
101 * Fire from ship @sp.
102 * Use ammo, resupply if necessary.
103 * Return damage if the ship fires, else -1.
106 shp_fire(struct shpstr *sp)
110 if (sp->shp_effic < 60)
112 guns = shp_usable_guns(sp);
113 guns = MIN(guns, (sp->shp_item[I_MILIT] + 1) / 2);
116 shp_supply(sp, I_SHELL, (guns + 1) / 2);
117 guns = MIN(guns, sp->shp_item[I_SHELL] * 2);
120 sp->shp_item[I_SHELL] -= (guns + 1) / 2;
121 return (int)seagun(sp->shp_effic, guns);
125 * Drop depth-charges from ship @sp.
126 * Use ammo, resupply if necessary.
127 * Return damage if the ship drops depth-charges, else -1.
130 shp_dchrg(struct shpstr *sp)
134 if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_DCH) == 0)
136 if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 0)
138 shp_supply(sp, I_SHELL, 2);
139 dchrgs = MIN(2, sp->shp_item[I_SHELL]);
142 sp->shp_item[I_SHELL] -= dchrgs;
143 return (int)seagun(sp->shp_effic, 2 * dchrgs - 1);
147 * Fire torpedo from ship @sp.
148 * Use ammo, resupply if necessary.
149 * Use mobility if @usemob is non-zero.
150 * Return damage if the ship fires, else -1.
153 shp_torp(struct shpstr *sp, int usemob)
155 if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_TORP) == 0)
157 if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 0)
159 if (usemob && sp->shp_mobil <= 0)
161 if (!shp_supply(sp, I_SHELL, SHP_TORP_SHELLS))
163 sp->shp_item[I_SHELL] -= SHP_TORP_SHELLS;
165 sp->shp_mobil -= (int)shp_mobcost(sp) / 2.0;
166 return TORP_DAMAGE();
170 * Fire from land unit @lp.
171 * Use ammo, resupply if necessary.
172 * Return damage if the land unit fires, else -1.
175 lnd_fire(struct lndstr *lp)
177 int guns, ammo, shells;
180 if (lp->lnd_effic < LAND_MINFIREEFF)
182 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
184 if (lp->lnd_item[I_MILIT] == 0)
187 guns = MIN(guns, lp->lnd_item[I_GUN]);
190 ammo = lchr[lp->lnd_type].l_ammo;
191 if (CANT_HAPPEN(ammo == 0))
193 lnd_supply(lp, I_SHELL, ammo);
194 shells = lp->lnd_item[I_SHELL];
197 d = landunitgun(lp->lnd_effic, guns);
199 d *= (double)shells / (double)ammo;
202 lp->lnd_item[I_SHELL] -= ammo;
207 * Sabotage with land unit @lp, target has @item[] commodities.
209 * Return damage if the land unit sabotages, else -1.
212 lnd_sabo(struct lndstr *lp, short item[])
216 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
218 if (!(lchr[lp->lnd_type].l_flags & L_SPY))
220 if (!lp->lnd_item[I_SHELL])
222 lp->lnd_item[I_SHELL]--;
223 dam = fortgun(3 * lp->lnd_effic, 7);
224 if (item[I_SHELL] > 20)
225 dam += seagun(lp->lnd_effic, roll0(item[I_SHELL] / 10));
226 if (item[I_PETROL] > 100)
227 dam += seagun(lp->lnd_effic, roll0(item[I_PETROL] / 50));
232 * Return number of guns ship @sp can fire.
235 shp_usable_guns(struct shpstr *sp)
237 return MIN(shp_glim(sp), sp->shp_item[I_GUN]);
241 * Return effective firing range for range factor @rng at tech @tlev.
244 effrange(int rng, double tlev)
246 /* FIXME don't truncate TLEV */
247 return techfact((int)tlev, rng / 2.0);
251 * Return firing range for sector @sp.
254 fortrange(struct sctstr *sp)
256 struct natstr *np = getnatp(sp->sct_own);
259 if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
262 rng = effrange(14.0 * fire_range_factor, np->nat_level[NAT_TLEV]);
263 if (sp->sct_effic >= 60)
269 * Return firing range for ship @sp.
272 shp_fire_range(struct shpstr *sp)
274 return effrange(shp_frnge(sp), sp->shp_tech);
278 * Return torpedo range for ship @sp.
281 torprange(struct shpstr *sp)
283 return effrange(shp_frnge(sp) * 2, sp->shp_tech)
284 * sp->shp_effic / 100.0;
288 * Return hit chance for torpedo from ship @sp at range @range.
291 shp_torp_hitchance(struct shpstr *sp, int range)
293 return DTORP_HITCHANCE(range, shp_visib(sp));
297 * Return firing range for land unit @lp.
300 lnd_fire_range(struct lndstr *lp)
302 return effrange(lnd_frg(lp), lp->lnd_tech);