]> git.pond.sub.org Git - empserver/blob - src/lib/subs/landgun.c
Move declarations for chance.c to new chance.h
[empserver] / src / lib / subs / landgun.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  landgun.c: Fire weapons
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2006-2009
31  */
32
33 #include <config.h>
34
35 #include "chance.h"
36 #include "damage.h"
37 #include "file.h"
38 #include "land.h"
39 #include "nat.h"
40 #include "optlist.h"
41 #include "prototypes.h"
42 #include "sect.h"
43 #include "ship.h"
44
45 static double
46 fortgun(int effic, int guns)
47 {
48     double d;
49     double g = MIN(guns, 7);
50
51     d = (random() % 30 + 20.0) * (g / 7.0);
52     d *= effic / 100.0;
53     return d;
54 }
55
56 static double
57 seagun(int effic, int guns)
58 {
59     double d;
60
61     d = 0.0;
62     while (guns--)
63         d += 10.0 + random() % 6;
64     d *= effic * 0.01;
65     return d;
66 }
67
68 static double
69 landunitgun(int effic, int guns)
70 {
71     double d;
72
73     d = 0.0;
74     while (guns--)
75         d += 5.0 + random() % 6;
76     d *= effic * 0.01;
77     return d;
78 }
79
80 /*
81  * Fire from fortress SP.
82  * Use ammo, resupply if necessary.
83  * Return damage if the fortress fires, else -1.
84  */
85 int
86 fort_fire(struct sctstr *sp)
87 {
88     int guns = sp->sct_item[I_GUN];
89
90     if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
91         return -1;
92     if (sp->sct_item[I_MILIT] < 5 || guns == 0)
93         return -1;
94     if (!sct_supply(sp, I_SHELL, 1))
95         return -1;
96     sp->sct_item[I_SHELL]--;
97     return (int)fortgun(sp->sct_effic, guns);
98 }
99
100 /*
101  * Fire from ship SP.
102  * Use ammo, resupply if necessary.
103  * Return damage if the ship fires, else -1.
104  */
105 int
106 shp_fire(struct shpstr *sp)
107 {
108     int guns;
109
110     if (sp->shp_effic < 60)
111         return -1;
112     guns = shp_usable_guns(sp);
113     guns = MIN(guns, (sp->shp_item[I_MILIT] + 1) / 2);
114     if (guns == 0)
115         return -1;
116     shp_supply(sp, I_SHELL, (guns + 1) / 2);
117     guns = MIN(guns, sp->shp_item[I_SHELL] * 2);
118     if (guns == 0)
119        return -1;
120     sp->shp_item[I_SHELL] -= (guns + 1) / 2;
121     return (int)seagun(sp->shp_effic, guns);
122 }
123
124 /*
125  * Drop depth-charges from ship SP.
126  * Use ammo, resupply if necessary.
127  * Return damage if the ship drops depth-charges, else -1.
128  */
129 int
130 shp_dchrg(struct shpstr *sp)
131 {
132     int dchrgs;
133
134     if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_DCH) == 0)
135         return -1;
136     if (sp->shp_item[I_MILIT] == 0)
137         return -1;
138     shp_supply(sp, I_SHELL, 2);
139     dchrgs = MIN(2, sp->shp_item[I_SHELL]);
140     if (dchrgs == 0)
141         return -1;
142     sp->shp_item[I_SHELL] -= dchrgs;
143     return (int)seagun(sp->shp_effic, 2 * dchrgs - 1);
144 }
145
146 /*
147  * Fire torpedo from ship SP.
148  * Use ammo and mobility, resupply if necessary.
149  * Return damage if the ship fires, else -1.
150  */
151 int
152 shp_torp(struct shpstr *sp, int usemob)
153 {
154     if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_TORP) == 0)
155         return -1;
156     if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 0)
157         return -1;
158     if (usemob && sp->shp_mobil <= 0)
159         return -1;
160     if (!shp_supply(sp, I_SHELL, SHP_TORP_SHELLS))
161         return -1;
162     sp->shp_item[I_SHELL] -= SHP_TORP_SHELLS;
163     if (usemob)
164         sp->shp_mobil -= (int)shp_mobcost(sp) / 2.0;
165     return TORP_DAMAGE();
166 }
167
168 /*
169  * Fire from land unit LP.
170  * Use ammo, resupply if necessary.
171  * Return damage if the land unit fires, else -1.
172  */
173 int
174 lnd_fire(struct lndstr *lp)
175 {
176     int guns, ammo, shells;
177     double d;
178
179     if (lp->lnd_effic < LAND_MINFIREEFF)
180         return -1;
181     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
182         return -1;
183     if (lp->lnd_item[I_MILIT] == 0)
184         return -1;
185     guns = lnd_dam(lp);
186     guns = MIN(guns, lp->lnd_item[I_GUN]);
187     if (guns == 0)
188         return -1;
189     ammo = lchr[lp->lnd_type].l_ammo;
190     if (CANT_HAPPEN(ammo == 0))
191         ammo = 1;
192     lnd_supply(lp, I_SHELL, ammo);
193     shells = lp->lnd_item[I_SHELL];
194     if (shells == 0)
195         return -1;
196     d = landunitgun(lp->lnd_effic, guns);
197     if (shells < ammo) {
198         d *= (double)shells / (double)ammo;
199         ammo = shells;
200     }
201     lp->lnd_item[I_SHELL] -= ammo;
202     return d;
203 }
204
205 /*
206  * Sabotage with land unit LP.
207  * Use ammo.
208  * Return damage if the land unit sabotages, else -1.
209  */
210 int
211 lnd_sabo(struct lndstr *lp, short item[])
212 {
213     int dam;
214
215     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
216         return -1;
217     if (!(lchr[lp->lnd_type].l_flags & L_SPY))
218         return -1;
219     if (!lp->lnd_item[I_SHELL])
220         return -1;
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, random() % (item[I_SHELL] / 10));
225     if (item[I_PETROL] > 100)
226         dam += seagun(lp->lnd_effic, random() % (item[I_PETROL] / 50));
227     return dam;
228 }
229
230 /*
231  * Return number of guns ship SP can fire.
232  */
233 int
234 shp_usable_guns(struct shpstr *sp)
235 {
236     return MIN(shp_glim(sp), sp->shp_item[I_GUN]);
237 }
238
239 /*
240  * Return effective firing range for range factor RNG at tech TLEV.
241  */
242 static double
243 effrange(int rng, double tlev)
244 {
245     /* FIXME don't truncate TLEV */
246     return techfact((int)tlev, rng / 2.0);
247 }
248
249 /*
250  * Return firing range for sector SP.
251  */
252 double
253 fortrange(struct sctstr *sp)
254 {
255     struct natstr *np = getnatp(sp->sct_own);
256     double rng;
257
258     if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
259         return -1.0;
260
261     rng = effrange(14.0 * fire_range_factor, np->nat_level[NAT_TLEV]);
262     if (sp->sct_effic >= 60)
263         rng++;
264     return rng;
265 }
266
267 /*
268  * Return firing range for ship SP.
269  */
270 double
271 shp_fire_range(struct shpstr *sp)
272 {
273     return effrange(shp_frnge(sp), sp->shp_tech);
274 }
275
276 /*
277  * Return torpedo range for ship SP.
278  */
279 double
280 torprange(struct shpstr *sp)
281 {
282     return effrange(shp_frnge(sp) * 2, sp->shp_tech)
283         * sp->shp_effic / 100.0;
284 }
285
286 /*
287  * Return hit chance for torpedo from ship SP at range RANGE.
288  */
289 double
290 shp_torp_hitchance(struct shpstr *sp, int range)
291 {
292     return DTORP_HITCHANCE(range, shp_visib(sp));
293 }
294
295 /*
296  * Return firing range for land unit SP.
297  */
298 double
299 lnd_fire_range(struct lndstr *lp)
300 {
301     return effrange(lnd_frg(lp), lp->lnd_tech);
302 }
303
304 int
305 roundrange(double r)
306 {
307     return roundavg(r);
308 }