2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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-2009
40 #include "prototypes.h"
45 fortgun(int effic, int guns)
48 double g = MIN(guns, 7);
50 d = (random() % 30 + 20.0) * (g / 7.0);
56 seagun(int effic, int guns)
62 d += 10.0 + random() % 6;
68 landunitgun(int effic, int guns)
74 d += 5.0 + random() % 6;
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);
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)
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 and mobility, resupply if necessary.
148 * Return damage if the ship fires, else -1.
151 shp_torp(struct shpstr *sp, int usemob)
153 if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_TORP) == 0)
155 if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 0)
157 if (usemob && sp->shp_mobil <= 0)
159 if (!shp_supply(sp, I_SHELL, SHP_TORP_SHELLS))
161 sp->shp_item[I_SHELL] -= SHP_TORP_SHELLS;
163 sp->shp_mobil -= (int)shp_mobcost(sp) / 2.0;
164 return TORP_DAMAGE();
168 * Fire from land unit LP.
169 * Use ammo, resupply if necessary.
170 * Return damage if the land unit fires, else -1.
173 lnd_fire(struct lndstr *lp)
175 int guns, ammo, shells;
178 if (lp->lnd_effic < LAND_MINFIREEFF)
180 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
182 if (lp->lnd_item[I_MILIT] == 0)
185 guns = MIN(guns, lp->lnd_item[I_GUN]);
188 ammo = lchr[lp->lnd_type].l_ammo;
189 if (CANT_HAPPEN(ammo == 0))
191 lnd_supply(lp, I_SHELL, ammo);
192 shells = lp->lnd_item[I_SHELL];
195 d = landunitgun(lp->lnd_effic, guns);
197 d *= (double)shells / (double)ammo;
200 lp->lnd_item[I_SHELL] -= ammo;
205 * Sabotage with land unit LP.
207 * Return damage if the land unit sabotages, else -1.
210 lnd_sabo(struct lndstr *lp, short item[])
214 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
216 if (!(lchr[lp->lnd_type].l_flags & L_SPY))
218 if (!lp->lnd_item[I_SHELL])
220 lp->lnd_item[I_SHELL]--;
221 dam = fortgun(3 * lp->lnd_effic, 7);
222 if (item[I_SHELL] > 20)
223 dam += seagun(lp->lnd_effic, random() % (item[I_SHELL] / 10));
224 if (item[I_PETROL] > 100)
225 dam += seagun(lp->lnd_effic, random() % (item[I_PETROL] / 50));
230 * Return number of guns ship SP can fire.
233 shp_usable_guns(struct shpstr *sp)
235 return MIN(shp_glim(sp), sp->shp_item[I_GUN]);
239 * Return effective firing range for range factor RNG at tech TLEV.
242 effrange(int rng, double tlev)
244 /* FIXME don't truncate TLEV */
245 return techfact((int)tlev, rng / 2.0);
249 * Return firing range for sector SP.
252 fortrange(struct sctstr *sp)
254 struct natstr *np = getnatp(sp->sct_own);
257 if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
260 rng = effrange(14.0 * fire_range_factor, np->nat_level[NAT_TLEV]);
261 if (sp->sct_effic >= 60)
267 * Return firing range for ship SP.
270 shp_fire_range(struct shpstr *sp)
272 return effrange(shp_frnge(sp), sp->shp_tech);
276 * Return torpedo range for ship SP.
279 torprange(struct shpstr *sp)
281 return effrange(shp_frnge(sp) * 2, sp->shp_tech)
282 * sp->shp_effic / 100.0;
286 * Return hit chance for torpedo from ship SP at range RANGE.
289 shp_torp_hitchance(struct shpstr *sp, int range)
291 return DTORP_HITCHANCE(range, shp_visib(sp));
295 * Return firing range for land unit SP.
298 lnd_fire_range(struct lndstr *lp)
300 return effrange(lnd_frg(lp), lp->lnd_tech);