2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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 * landgun.c: Return values for land and ship gun firing damages
30 * Known contributors to this file:
31 * Markus Armbruster, 2006
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 shots, int guns, int ammo, int shells)
72 shots = MIN(shots, guns);
74 d += 5.0 + random() % 6;
76 if (shells < ammo && ammo != 0)
77 d *= (double)shells / (double)ammo;
82 * Fire from fortress SP.
83 * Use ammo, resupply if necessary.
84 * Return damage if the fortress fires, else -1.
87 fort_fire(struct sctstr *sp)
89 int guns = sp->sct_item[I_GUN];
92 if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
94 if (sp->sct_item[I_MILIT] < 5 || guns == 0)
96 shells = sp->sct_item[I_SHELL];
97 shells += supply_commod(sp->sct_own, sp->sct_x, sp->sct_y,
101 sp->sct_item[I_SHELL] = shells - 1;
102 return (int)fortgun(sp->sct_effic, guns);
107 * Use ammo, resupply if necessary.
108 * Return damage if the ship fires, else -1.
111 shp_fire(struct shpstr *sp)
115 if (sp->shp_effic < 60)
118 guns = MIN(guns, sp->shp_item[I_GUN]);
119 guns = MIN(guns, (sp->shp_item[I_MILIT] + 1) / 2);
122 shells = sp->shp_item[I_SHELL];
123 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
124 I_SHELL, (guns + 1) / 2 - shells);
125 guns = MIN(guns, shells * 2);
128 sp->shp_item[I_SHELL] = shells - (guns + 1) / 2;
129 return (int)seagun(sp->shp_effic, guns);
133 * Drop depth-charges from ship SP.
134 * Use ammo, resupply if necessary.
135 * Return damage if the ship drops depth-charges, else -1.
138 shp_dchrg(struct shpstr *sp)
142 if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_DCH) == 0)
144 if (sp->shp_item[I_MILIT] == 0)
146 shells = sp->shp_item[I_SHELL];
147 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
148 I_SHELL, 2 - shells);
151 sp->shp_item[I_SHELL] = shells - 2;
152 return (int)seagun(sp->shp_effic, 3);
156 * Fire torpedo from ship SP.
157 * Use ammo and mobility, resupply if necessary.
158 * Return damage if the ship fires, else -1.
161 shp_torp(struct shpstr *sp, int usemob)
165 if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_TORP) == 0)
167 if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 0)
169 if (usemob && sp->shp_mobil <= 0)
171 shells = sp->shp_item[I_SHELL];
172 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
173 I_SHELL, SHP_TORP_SHELLS - shells);
174 if (shells < SHP_TORP_SHELLS)
176 sp->shp_item[I_SHELL] = shells - SHP_TORP_SHELLS;
178 sp->shp_mobil -= (int)shp_mobcost(sp) / 2.0;
179 return TORP_DAMAGE();
183 * Return effective firing range for range factor RNG at tech TLEV.
186 effrange(int rng, double tlev)
188 /* FIXME don't truncate TLEV */
189 return techfact((int)tlev, rng / 2.0);
193 * Return torpedo range for ship SP.
196 torprange(struct shpstr *sp)
198 return effrange(sp->shp_frnge * 2, sp->shp_tech)
199 * sp->shp_effic / 100.0;
203 * Return firing range for sector SP.
206 fortrange(struct sctstr *sp)
208 struct natstr *np = getnatp(sp->sct_own);
211 if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
214 rng = effrange(14.0 * fire_range_factor, np->nat_level[NAT_TLEV]);
215 if (sp->sct_effic > 59)