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