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