]> git.pond.sub.org Git - empserver/blob - src/lib/subs/landgun.c
Update copyright notice
[empserver] / src / lib / subs / landgun.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, 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-2013
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 = (roll(30) + 19.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 += 9.0 + roll(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 += 4.0 + roll(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 || sp->shp_item[I_GUN] == 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, resupply if necessary.
149  * Use mobility if @usemob is non-zero.
150  * Return damage if the ship fires, else -1.
151  */
152 int
153 shp_torp(struct shpstr *sp, int usemob)
154 {
155     if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_TORP) == 0)
156         return -1;
157     if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 0)
158         return -1;
159     if (usemob && sp->shp_mobil <= 0)
160         return -1;
161     if (!shp_supply(sp, I_SHELL, SHP_TORP_SHELLS))
162         return -1;
163     sp->shp_item[I_SHELL] -= SHP_TORP_SHELLS;
164     if (usemob)
165         sp->shp_mobil -= (int)shp_mobcost(sp) / 2.0;
166     return TORP_DAMAGE();
167 }
168
169 /*
170  * Fire from land unit @lp.
171  * Use ammo, resupply if necessary.
172  * Return damage if the land unit fires, else -1.
173  */
174 int
175 lnd_fire(struct lndstr *lp)
176 {
177     int guns, ammo, shells;
178     double d;
179
180     if (lp->lnd_effic < LAND_MINFIREEFF)
181         return -1;
182     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
183         return -1;
184     if (lp->lnd_item[I_MILIT] == 0)
185         return -1;
186     guns = lnd_dam(lp);
187     guns = MIN(guns, lp->lnd_item[I_GUN]);
188     if (guns == 0)
189         return -1;
190     ammo = lchr[lp->lnd_type].l_ammo;
191     if (CANT_HAPPEN(ammo == 0))
192         ammo = 1;
193     lnd_supply(lp, I_SHELL, ammo);
194     shells = lp->lnd_item[I_SHELL];
195     if (shells == 0)
196         return -1;
197     d = landunitgun(lp->lnd_effic, guns);
198     if (shells < ammo) {
199         d *= (double)shells / (double)ammo;
200         ammo = shells;
201     }
202     lp->lnd_item[I_SHELL] -= ammo;
203     return d;
204 }
205
206 /*
207  * Sabotage with land unit @lp, target has @item[] commodities.
208  * Use ammo.
209  * Return damage if the land unit sabotages, else -1.
210  */
211 int
212 lnd_sabo(struct lndstr *lp, short item[])
213 {
214     int dam;
215
216     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
217         return -1;
218     if (!(lchr[lp->lnd_type].l_flags & L_SPY))
219         return -1;
220     if (!lp->lnd_item[I_SHELL])
221         return -1;
222     lp->lnd_item[I_SHELL]--;
223     dam = fortgun(3 * lp->lnd_effic, 7);
224     if (item[I_SHELL] > 20)
225         dam += seagun(lp->lnd_effic, roll0(item[I_SHELL] / 10));
226     if (item[I_PETROL] > 100)
227         dam += seagun(lp->lnd_effic, roll0(item[I_PETROL] / 50));
228     return dam;
229 }
230
231 /*
232  * Return number of guns ship @sp can fire.
233  */
234 int
235 shp_usable_guns(struct shpstr *sp)
236 {
237     return MIN(shp_glim(sp), sp->shp_item[I_GUN]);
238 }
239
240 /*
241  * Return effective firing range for range factor @rng at tech @tlev.
242  */
243 static double
244 effrange(int rng, double tlev)
245 {
246     /* FIXME don't truncate TLEV */
247     return techfact((int)tlev, rng / 2.0);
248 }
249
250 /*
251  * Return firing range for sector @sp.
252  */
253 double
254 fortrange(struct sctstr *sp)
255 {
256     struct natstr *np = getnatp(sp->sct_own);
257     double rng;
258
259     if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
260         return -1.0;
261
262     rng = effrange(14.0 * fire_range_factor, np->nat_level[NAT_TLEV]);
263     if (sp->sct_effic >= 60)
264         rng++;
265     return rng;
266 }
267
268 /*
269  * Return firing range for ship @sp.
270  */
271 double
272 shp_fire_range(struct shpstr *sp)
273 {
274     return effrange(shp_frnge(sp), sp->shp_tech);
275 }
276
277 /*
278  * Return torpedo range for ship @sp.
279  */
280 double
281 torprange(struct shpstr *sp)
282 {
283     return effrange(shp_frnge(sp) * 2, sp->shp_tech)
284         * sp->shp_effic / 100.0;
285 }
286
287 /*
288  * Return hit chance for torpedo from ship @sp at range @range.
289  */
290 double
291 shp_torp_hitchance(struct shpstr *sp, int range)
292 {
293     return DTORP_HITCHANCE(range, shp_visib(sp));
294 }
295
296 /*
297  * Return firing range for land unit @lp.
298  */
299 double
300 lnd_fire_range(struct lndstr *lp)
301 {
302     return effrange(lnd_frg(lp), lp->lnd_tech);
303 }
304
305 int
306 roundrange(double r)
307 {
308     return roundavg(r);
309 }