2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2017, 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
40 #include "prototypes.h"
45 fortgun(int effic, int guns)
48 double g = MIN(guns, 7);
50 d = (roll(30) + 19.0) * (g / 7.0);
56 seagun(int effic, int guns)
68 landunitgun(int effic, int guns)
80 * Fire from fortress @sp.
81 * Use ammo, resupply if necessary.
82 * Return damage if the fortress fires, else -1.
85 fort_fire(struct sctstr *sp)
87 int guns = sp->sct_item[I_GUN];
89 if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
91 if (sp->sct_item[I_MILIT] < 5 || guns == 0)
93 if (!sct_supply(sp, I_SHELL, 1))
95 sp->sct_item[I_SHELL]--;
96 return (int)fortgun(sp->sct_effic, guns);
100 * Fire from ship @sp.
101 * Use ammo, resupply if necessary.
102 * Return damage if the ship fires, else -1.
105 shp_fire(struct shpstr *sp)
109 if (sp->shp_effic < 60)
111 guns = shp_usable_guns(sp);
112 guns = MIN(guns, (sp->shp_item[I_MILIT] + 1) / 2);
115 shp_supply(sp, I_SHELL, (guns + 1) / 2);
116 guns = MIN(guns, sp->shp_item[I_SHELL] * 2);
119 sp->shp_item[I_SHELL] -= (guns + 1) / 2;
120 return (int)seagun(sp->shp_effic, guns);
124 * Drop depth-charges from ship @sp.
125 * Use ammo, resupply if necessary.
126 * Return damage if the ship drops depth-charges, else -1.
129 shp_dchrg(struct shpstr *sp)
133 if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_DCH) == 0)
135 if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 0)
137 shp_supply(sp, I_SHELL, 2);
138 dchrgs = MIN(2, sp->shp_item[I_SHELL]);
141 sp->shp_item[I_SHELL] -= dchrgs;
142 return (int)seagun(sp->shp_effic, 2 * dchrgs - 1);
146 * Fire torpedo from ship @sp.
147 * Use ammo, resupply if necessary.
148 * Use mobility if @usemob is non-zero.
149 * Return damage if the ship fires, else -1.
152 shp_torp(struct shpstr *sp, int usemob)
154 if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_TORP) == 0)
156 if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 0)
158 if (usemob && sp->shp_mobil <= 0)
160 if (!shp_supply(sp, I_SHELL, SHP_TORP_SHELLS))
162 sp->shp_item[I_SHELL] -= SHP_TORP_SHELLS;
164 sp->shp_mobil -= (int)shp_mobcost(sp) / 2.0;
165 return TORP_DAMAGE();
169 * Fire from land unit @lp.
170 * Use ammo, resupply if necessary.
171 * Return damage if the land unit fires, else -1.
174 lnd_fire(struct lndstr *lp)
176 int guns, ammo, shells;
179 if (lp->lnd_effic < LAND_MINFIREEFF)
181 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
183 if (lp->lnd_item[I_MILIT] == 0)
186 guns = MIN(guns, lp->lnd_item[I_GUN]);
189 ammo = lchr[lp->lnd_type].l_ammo;
190 if (CANT_HAPPEN(ammo == 0))
192 lnd_supply(lp, I_SHELL, ammo);
193 shells = lp->lnd_item[I_SHELL];
196 d = landunitgun(lp->lnd_effic, guns);
198 d *= (double)shells / (double)ammo;
201 lp->lnd_item[I_SHELL] -= ammo;
206 * Sabotage with land unit @lp, target has @item[] commodities.
208 * Return damage if the land unit sabotages, else -1.
211 lnd_sabo(struct lndstr *lp, short item[])
215 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
217 if (!(lchr[lp->lnd_type].l_flags & L_SPY))
219 if (!lp->lnd_item[I_SHELL])
221 lp->lnd_item[I_SHELL]--;
222 dam = fortgun(3 * lp->lnd_effic, 7);
223 if (item[I_SHELL] > 20)
224 dam += seagun(lp->lnd_effic, roll0(item[I_SHELL] / 10));
225 if (item[I_PETROL] > 100)
226 dam += seagun(lp->lnd_effic, roll0(item[I_PETROL] / 50));
231 * Return number of guns ship @sp can fire.
234 shp_usable_guns(struct shpstr *sp)
236 return MIN(shp_glim(sp), sp->shp_item[I_GUN]);
240 * Return effective firing range for range factor @rng at tech @tlev.
243 effrange(int rng, double tlev)
245 /* FIXME don't truncate TLEV */
246 return techfact((int)tlev, rng / 2.0);
250 * Return firing range for sector @sp.
253 fortrange(struct sctstr *sp)
255 struct natstr *np = getnatp(sp->sct_own);
258 if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
261 rng = effrange(14.0 * fire_range_factor, np->nat_level[NAT_TLEV]);
262 if (sp->sct_effic >= 60)
268 * Return firing range for ship @sp.
271 shp_fire_range(struct shpstr *sp)
273 return effrange(shp_frnge(sp), sp->shp_tech);
277 * Return torpedo range for ship @sp.
280 torprange(struct shpstr *sp)
282 return effrange(shp_frnge(sp) * 2, sp->shp_tech)
283 * sp->shp_effic / 100.0;
287 * Return hit chance for torpedo from ship @sp at range @range.
290 shp_torp_hitchance(struct shpstr *sp, int range)
292 return DTORP_HITCHANCE(range, shp_visib(sp));
296 * Return firing range for land unit @lp.
299 lnd_fire_range(struct lndstr *lp)
301 return effrange(lnd_frg(lp), lp->lnd_tech);