]> git.pond.sub.org Git - empserver/blob - src/lib/commands/torp.c
Factor out ship firing range calculation into shp_fire_range()
[empserver] / src / lib / commands / torp.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  *  torp.c: Fire torpedoes at enemy ships
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare
32  *     Thomas Ruschak, 1992
33  *     Ken Stevens, 1995
34  *     Steve McClure, 2000
35  */
36
37 #include <config.h>
38
39 #include "commands.h"
40 #include "damage.h"
41 #include "news.h"
42 #include "optlist.h"
43 #include "retreat.h"
44 #include "ship.h"
45
46 static void anti_torp(int f, int ntorping, int vshipown);
47 static void fire_dchrg(struct shpstr *, struct shpstr *, int);
48 static int fire_torp(struct shpstr *, struct shpstr *, int);
49
50 int
51 torp(void)
52 {
53     natid vshipown;
54     int range;
55     int dam;
56     int subno;
57     int victno;
58     int erange;
59     double hitchance;
60     struct shpstr vship;
61     struct shpstr sub;
62     char *ptr;
63     struct nstr_item nbst;
64     char buf[1024];
65     char *sav;
66     int ntorping = 0;
67     char prompt[128];
68
69     if (!(sav = getstarg(player->argp[1], "From ship(s)? ", buf)))
70         return RET_SYN;
71     if (!snxtitem(&nbst, EF_SHIP, sav))
72         return RET_SYN;
73     while (nxtitem(&nbst, &sub)) {
74         if (sub.shp_own != player->cnum)
75             continue;
76         if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
77             continue;
78         if (sub.shp_item[I_GUN] == 0
79             || sub.shp_item[I_SHELL] < SHP_TORP_SHELLS)
80             continue;
81         if (sub.shp_item[I_MILIT] < 1)
82             continue;
83         if (sub.shp_effic < 60)
84             continue;
85         if (sub.shp_mobil <= 0)
86             continue;
87         ntorping++;
88     }
89     pr("%d ships are eligible to torp\n", ntorping);
90     snxtitem(&nbst, EF_SHIP, sav);
91     while (nxtitem(&nbst, &sub)) {
92         if (!sub.shp_own)
93             continue;
94         if (sub.shp_own != player->cnum) {
95             continue;
96         }
97         if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0) {
98             pr("Ship # %d: A %s can't fire torpedoes!\n",
99                sub.shp_uid, mchr[(int)sub.shp_type].m_name);
100             continue;
101         }
102         if (sub.shp_item[I_GUN] == 0
103             || sub.shp_item[I_SHELL] < SHP_TORP_SHELLS) {
104             pr("Ship #%d has insufficient armament\n", sub.shp_uid);
105             continue;
106         }
107         if (sub.shp_item[I_MILIT] < 1) {
108             pr("Ship #%d has insufficient crew\n", sub.shp_uid);
109             continue;
110         }
111         if (sub.shp_effic < 60) {
112             pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
113             continue;
114         }
115         if (sub.shp_mobil <= 0) {
116             pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
117             continue;
118         }
119         subno = sub.shp_uid;
120         sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
121         if ((ptr = getstarg(player->argp[2], prompt, buf)) == 0)
122             return RET_SYN;
123         if (!check_ship_ok(&sub))
124             return RET_FAIL;
125         if ((victno = atoi(ptr)) < 0)
126             return RET_SYN;
127         if (!getship(victno, &vship))
128             return RET_FAIL;
129         if (!vship.shp_own)
130             return RET_FAIL;
131         vshipown = vship.shp_own;
132         if (victno == subno) {
133             pr("Shooting yourself, eh?  How strange...\n");
134             continue;
135         }
136         if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
137             if (!(mchr[(int)sub.shp_type].m_flags & M_SUBT)) {
138                 pr("You can't torpedo a submarine!\n");
139                 continue;
140             }
141         }
142         dam = shp_torp(&sub, 1);
143         sub.shp_mission = 0;
144         putship(sub.shp_uid, &sub);
145         if (CANT_HAPPEN(dam < 0)) {
146             pr("Ship #%d has insufficient armament\n", sub.shp_uid);
147             continue;
148         }
149
150         if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
151             anti_torp(sub.shp_uid, ntorping, vshipown);
152         getship(sub.shp_uid, &sub);
153         if (sub.shp_own == 0)
154             continue;
155
156         erange = roundrange(torprange(&sub));
157         pr("Effective torpedo range is %d.0\n", erange);
158         pr("Whooosh... ");
159         getship(victno, &vship);
160         vshipown = vship.shp_own;
161         range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
162         hitchance = DTORP_HITCHANCE(range, sub.shp_visib);
163         if (range <= erange) {
164             pr("Hitchance = %d%%\n", (int)(hitchance * 100));
165         }
166         /* Now, can the torpedo even get there? */
167         if (!line_of_sight(NULL, sub.shp_x, sub.shp_y,
168                            vship.shp_x, vship.shp_y)) {
169             pr("BOOM!... Torpedo slams into land before reaching target.\n");
170             /* We only tell the victim if we were within range. */
171             if (range <= erange) {
172                 if (vshipown != 0)
173                     wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
174                        xyas(sub.shp_x, sub.shp_y, vshipown),
175                        prship(&vship));
176             }
177         } else if (range > erange) {
178             pr("Out of range\n");
179         } else if (chance(hitchance)) {
180             pr("BOOM!...\n");
181             if (vshipown != 0)
182                 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n",
183                    prsub(&sub), xyas(sub.shp_x, sub.shp_y, vshipown),
184                    prship(&vship), dam);
185             if (vship.shp_rflags & RET_TORPED) {
186                 retreat_ship(&vship, 't');
187                 shipdamage(&vship, dam);
188             } else
189                 shipdamage(&vship, dam);
190             pr("Torpedo hit %s for %d damage.\n", prship(&vship), dam);
191
192             if (vship.shp_effic < SHIP_MINEFF)
193                 pr("%s sunk!\n", prship(&vship));
194             putship(vship.shp_uid, &vship);
195             if (mchr[(int)sub.shp_type].m_flags & M_SUB)
196                 nreport(vshipown, N_TORP_SHIP, 0, 1);
197             else
198                 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
199         } else {
200             pr("Missed\n");
201             if (vshipown != 0)
202                 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
203                    xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
204         }
205
206         if (mchr[(int)sub.shp_type].m_flags & M_SUB)
207             anti_torp(sub.shp_uid, ntorping, vshipown);
208     }
209     return RET_OK;
210 }
211
212 static void
213 anti_torp(int f, int ntorping, int vshipown)
214 {
215     struct shpstr sub;
216     struct shpstr dd;
217     int x;
218
219     getship(f, &sub);
220
221     if (sub.shp_own == vshipown)
222         return;
223
224     if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
225         pr("Starting our attack run...\n");
226
227     x = 0;
228     while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
229         if (dd.shp_own == 0)
230             continue;
231         if (dd.shp_own != vshipown)
232             continue;
233
234         if (!fire_torp(&dd, &sub, ntorping))
235             fire_dchrg(&dd, &sub, ntorping);
236     }
237 }
238
239 static void
240 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
241 {
242     int range, erange, dam;
243
244     erange = roundrange(shp_fire_range(sp));
245     range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
246     if (range > erange)
247         return;
248
249     if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
250         /* Return fire to a torpedo boat */
251         if (mchr[sp->shp_type].m_flags & M_SUB)
252             return;             /* sub deck gun can't return fire */
253         dam = shp_fire(sp);
254         putship(sp->shp_uid, sp);
255         if (dam < 0)
256             return;
257         if (ntargets > 2)
258             dam /= ntargets / 2;
259
260         pr_beep();
261         pr("Kaboom!!! Incoming shells!\n");
262         if (sp->shp_own != 0)
263             wu(0, sp->shp_own,
264                "%s fired at %s\n", prship(sp), prship(targ));
265         pr_beep();
266         pr("BLAM! %d damage!\n", dam);
267     } else {
268         /* Return fire to a submarine */
269         dam = shp_dchrg(sp);
270         putship(sp->shp_uid, sp);
271         if (dam < 0)
272             return;
273         if (ntargets > 2)
274             dam /= ntargets / 2;
275
276         pr("\nCAPTAIN!  !!Depth charges!!...\n");
277         if (sp->shp_own != 0)
278             wu(0, sp->shp_own,
279                "%s depth charged %s\n", prship(sp), prsub(targ));
280         pr("click...WHAM!  %d damage!\n", dam);
281     }
282     shipdamage(targ, dam);
283     putship(targ->shp_uid, targ);
284 }
285
286 static int
287 fire_torp(struct shpstr *sp, struct shpstr *targ, int ntargets)
288 {
289     int range, erange, dam;
290
291     if ((mchr[targ->shp_type].m_flags & M_SUB)
292         && (mchr[sp->shp_type].m_flags & M_SUBT) == 0)
293         return 0;               /* need sub-torp to torpedo a sub */
294
295     erange = roundrange(torprange(sp));
296     range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
297     if (range > erange)
298         return 0;
299
300     if (!line_of_sight(NULL, sp->shp_x, sp->shp_y,
301                        targ->shp_x, targ->shp_y))
302         return 0;
303     dam = shp_torp(sp, 1);
304     putship(sp->shp_uid, sp);
305     if (dam < 0)
306         return 0;
307
308     pr("Captain! Torpedoes sighted!\n");
309
310     if (chance(DTORP_HITCHANCE(range, sp->shp_visib))) {
311         pr("BOOM!...\n");
312         if (sp->shp_own != 0)
313             wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
314                prship(sp),
315                xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
316         if (ntargets > 2)
317             dam /= ntargets / 2;
318
319         shipdamage(targ, dam);
320         putship(targ->shp_uid, targ);
321
322         if (mchr[(int)sp->shp_type].m_flags & M_SUB)
323             nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
324         else
325             nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
326     } else {
327         pr("Missed!\n");
328         if (sp->shp_own != 0)
329             wu(0, sp->shp_own,
330                "%s missed %s with a torp at %s\n",
331                prship(sp), prsub(targ),
332                xyas(sp->shp_x, sp->shp_y, sp->shp_own));
333     }
334
335     return 1;
336 }
337
338 char *
339 prsub(struct shpstr *sp)
340 {
341     if (mchr[(int)sp->shp_type].m_flags & M_SUB)
342         return "sub";
343     else
344         return prship(sp);
345 }