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