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