]> git.pond.sub.org Git - empserver/blob - src/lib/subs/landgun.c
ef27134613b1edd82f9d1ecf4b388c85c932db48
[empserver] / src / lib / subs / landgun.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  landgun.c: Fire weapons
29  *
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2006-2008
32  */
33
34 #include <config.h>
35
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 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 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 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  * Return number of guns ship SP can fire.
207  */
208 int
209 shp_usable_guns(struct shpstr *sp)
210 {
211     return MIN(shp_glim(sp), sp->shp_item[I_GUN]);
212 }
213
214 /*
215  * Return effective firing range for range factor RNG at tech TLEV.
216  */
217 static double
218 effrange(int rng, double tlev)
219 {
220     /* FIXME don't truncate TLEV */
221     return techfact((int)tlev, rng / 2.0);
222 }
223
224 /*
225  * Return firing range for sector SP.
226  */
227 double
228 fortrange(struct sctstr *sp)
229 {
230     struct natstr *np = getnatp(sp->sct_own);
231     double rng;
232
233     if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
234         return -1.0;
235
236     rng = effrange(14.0 * fire_range_factor, np->nat_level[NAT_TLEV]);
237     if (sp->sct_effic > 59)
238         rng++;
239     return rng;
240 }
241
242 /*
243  * Return firing range for ship SP.
244  */
245 double
246 shp_fire_range(struct shpstr *sp)
247 {
248     return effrange(shp_frnge(sp), sp->shp_tech);
249 }
250
251 /*
252  * Return torpedo range for ship SP.
253  */
254 double
255 torprange(struct shpstr *sp)
256 {
257     return effrange(shp_frnge(sp) * 2, sp->shp_tech)
258         * sp->shp_effic / 100.0;
259 }
260
261 /*
262  * Return hit chance for torpedo from ship SP at range RANGE.
263  */
264 double
265 shp_torp_hitchance(struct shpstr *sp, int range)
266 {
267     return DTORP_HITCHANCE(range, shp_visib(sp));
268 }
269
270 /*
271  * Return firing range for land unit SP.
272  */
273 double
274 lnd_fire_range(struct lndstr *lp)
275 {
276     return effrange(lnd_frg(lp), lp->lnd_tech);
277 }
278
279 int
280 roundrange(double r)
281 {
282     return roundavg(r);
283 }