]> 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-2018, 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 "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 = (roll(30) + 19.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 += 9.0 + roll(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 += 4.0 + roll(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 || sp->shp_item[I_GUN] == 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, resupply if necessary.
148  * Use mobility if @usemob is non-zero.
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, target has @item[] commodities.
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, roll0(item[I_SHELL] / 10));
225     if (item[I_PETROL] > 100)
226         dam += seagun(lp->lnd_effic, roll0(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 @lp.
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 }