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