]> git.pond.sub.org Git - empserver/blob - src/lib/commands/torp.c
Fix torpedo return fire line of sight requirement and 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 int candchrg(struct shpstr *, struct shpstr *);
48 static int canshoot(struct shpstr *, struct shpstr *);
49 static int cantorp(struct shpstr *, struct shpstr *);
50 static void fire_dchrg(struct shpstr *, struct shpstr *, int);
51 static int fire_torp(struct shpstr *, struct shpstr *, int);
52
53 int
54 torp(void)
55 {
56     natid vshipown;
57     int range;
58     int dam;
59     int shells;
60     int subno;
61     int victno;
62     int erange;
63     double hitchance;
64     struct shpstr vship;
65     struct shpstr sub;
66     char *ptr;
67     struct nstr_item nbst;
68     char buf[1024];
69     char *sav;
70     int ntorping = 0;
71     char prompt[128];
72
73     if (!(sav = getstarg(player->argp[1], "From ship(s)? ", buf)))
74         return RET_SYN;
75     if (!snxtitem(&nbst, EF_SHIP, sav))
76         return RET_SYN;
77     while (nxtitem(&nbst, &sub)) {
78         if (sub.shp_own != player->cnum)
79             continue;
80         if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
81             continue;
82         shells = sub.shp_item[I_SHELL];
83         if (shells < SHP_TORP_SHELLS)
84             shells += supply_commod(sub.shp_own, sub.shp_x, sub.shp_y,
85                                     I_SHELL, SHP_TORP_SHELLS - shells);
86         if (sub.shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
87             continue;
88         if (sub.shp_item[I_MILIT] < 1)
89             continue;
90         if (sub.shp_effic < 60)
91             continue;
92         if (sub.shp_mobil <= 0)
93             continue;
94         ntorping++;
95     }
96     pr("%d ships are eligible to torp\n", ntorping);
97     snxtitem(&nbst, EF_SHIP, sav);
98     while (nxtitem(&nbst, &sub)) {
99         if (!sub.shp_own)
100             continue;
101         if (sub.shp_own != player->cnum) {
102             continue;
103         }
104         if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0) {
105             pr("Ship # %d: A %s can't fire torpedoes!\n",
106                sub.shp_uid, mchr[(int)sub.shp_type].m_name);
107             continue;
108         }
109         shells = sub.shp_item[I_SHELL];
110         if (shells < SHP_TORP_SHELLS)
111             shells += supply_commod(sub.shp_own, sub.shp_x, sub.shp_y,
112                                     I_SHELL, SHP_TORP_SHELLS - shells);
113         if (sub.shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS) {
114             pr("Ship #%d has insufficient armament\n", sub.shp_uid);
115             continue;
116         }
117         if (sub.shp_item[I_MILIT] < 1) {
118             pr("Ship #%d has insufficient crew\n", sub.shp_uid);
119             continue;
120         }
121         if (sub.shp_effic < 60) {
122             pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
123             continue;
124         }
125         if (sub.shp_mobil <= 0) {
126             pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
127             continue;
128         }
129         subno = sub.shp_uid;
130         sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
131         if ((ptr = getstarg(player->argp[2], prompt, buf)) == 0)
132             return RET_SYN;
133         if (!check_ship_ok(&sub))
134             return RET_FAIL;
135         if ((victno = atoi(ptr)) < 0)
136             return RET_SYN;
137         if (!getship(victno, &vship))
138             return RET_FAIL;
139         if (!vship.shp_own)
140             return RET_FAIL;
141         vshipown = vship.shp_own;
142         if (victno == subno) {
143             pr("Shooting yourself, eh?  How strange...\n");
144             continue;
145         }
146         if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
147             if (!(mchr[(int)sub.shp_type].m_flags & M_SUBT)) {
148                 pr("You can't torpedo a submarine!\n");
149                 continue;
150             }
151         }
152         if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
153             anti_torp(sub.shp_uid, ntorping, vshipown);
154         getship(sub.shp_uid, &sub);
155         if (sub.shp_own == 0) {
156             continue;
157         }
158         erange = roundrange(torprange(&sub));
159         pr("Effective torpedo range is %d.0\n", erange);
160         shells -= SHP_TORP_SHELLS;
161         sub.shp_item[I_SHELL] = shells;
162         putship(sub.shp_uid, &sub);
163         /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
164         sub.shp_mobil -= shp_mobcost(&sub) / 2.0;
165         pr("Whooosh... ");
166         getship(victno, &vship);
167         vshipown = vship.shp_own;
168         range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
169         hitchance = DTORP_HITCHANCE(range, sub.shp_visib);
170         if (range <= erange) {
171             pr("Hitchance = %d%%\n", (int)(hitchance * 100));
172         }
173         /* Now, can the torpedo even get there? */
174         if (!line_of_sight(NULL, sub.shp_x, sub.shp_y,
175                            vship.shp_x, vship.shp_y)) {
176             pr("BOOM!... Torpedo slams into land before reaching target.\n");
177             /* We only tell the victim if we were within range. */
178             if (range <= erange) {
179                 if (vshipown != 0)
180                     wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
181                        xyas(sub.shp_x, sub.shp_y, vshipown),
182                        prship(&vship));
183             }
184         } else if (range > erange) {
185             pr("Out of range\n");
186         } else if (hitchance >= 1.0 || chance(hitchance)) {
187             pr("BOOM!...\n");
188             dam = TORP_DAMAGE();
189             if (vshipown != 0)
190                 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n",
191                    prsub(&sub), xyas(sub.shp_x, sub.shp_y, vshipown),
192                    prship(&vship), dam);
193             if (vship.shp_rflags & RET_TORPED) {
194                 retreat_ship(&vship, 't');
195                 shipdamage(&vship, dam);
196             } else
197                 shipdamage(&vship, dam);
198             pr("Torpedo hit %s for %d damage.\n", prship(&vship), dam);
199
200             if (vship.shp_effic < SHIP_MINEFF)
201                 pr("%s sunk!\n", prship(&vship));
202             putship(vship.shp_uid, &vship);
203             if (mchr[(int)sub.shp_type].m_flags & M_SUB)
204                 nreport(vshipown, N_TORP_SHIP, 0, 1);
205             else
206                 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
207         } else {
208             pr("Missed\n");
209             if (vshipown != 0)
210                 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
211                    xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
212         }
213         sub.shp_mission = 0;
214         putship(sub.shp_uid, &sub);
215         if (mchr[(int)sub.shp_type].m_flags & M_SUB)
216             anti_torp(sub.shp_uid, ntorping, vshipown);
217     }
218     return RET_OK;
219 }
220
221 static void
222 anti_torp(int f, int ntorping, int vshipown)
223 {
224     struct shpstr sub;
225     struct shpstr dd;
226     int x;
227
228     getship(f, &sub);
229
230     if (sub.shp_own == vshipown)
231         return;
232
233     if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
234         pr("Starting our attack run...\n");
235
236     x = 0;
237     while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
238         if (dd.shp_own == 0)
239             continue;
240         if (dd.shp_own != vshipown)
241             continue;
242         if (!canshoot(&dd, &sub))
243             continue;
244
245         if (cantorp(&dd, &sub)) {
246             /* Try torping.. if we can, maybe we can fire */
247             if (!fire_torp(&dd, &sub, ntorping))
248                 if (candchrg(&dd, &sub))
249                     fire_dchrg(&dd, &sub, ntorping);
250         } else
251             fire_dchrg(&dd, &sub, ntorping);
252     }
253 }
254
255 /* Can ship A shoot at ship B? */
256 static int
257 canshoot(struct shpstr *a, struct shpstr *b)
258 {
259     /* Anyone can shoot a normal ship */
260     if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
261         return 1;
262
263     /* You can depth-charge a sub */
264     if (mchr[(int)a->shp_type].m_flags & M_DCH)
265         return 1;
266
267     /* If you have SUBT flag, you can torp a sub */
268     if (mchr[(int)a->shp_type].m_flags & M_SUBT)
269         return 1;
270
271     return 0;
272 }
273
274 /* Can ship A torp ship B? */
275 static int
276 cantorp(struct shpstr *a, struct shpstr *b)
277 {
278     if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
279         return 0;
280
281     /* Anyone with TORP flag can torp a normal ship */
282     if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
283         return 1;
284
285     /* Ship b is a sub, so we need to have the SUBT flag */
286     if (mchr[(int)a->shp_type].m_flags & M_SUBT)
287         return 1;
288
289     return 0;
290 }
291
292 /* Can ship A depth-charge (or fire guns at) ship B? */
293 static int
294 candchrg(struct shpstr *a, struct shpstr *b)
295 {
296     if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0) {
297         if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
298             return 1;
299
300         return 0;
301     }
302
303     if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
304         return 0;
305
306     return 1;
307 }
308
309 static void
310 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
311 {
312     int range, erange, dam;
313
314     erange = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
315     range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
316     if (range > erange)
317         return;
318
319     if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
320         dam = shp_fire(sp);
321         putship(sp->shp_uid, sp);
322         if (dam < 0)
323             return;
324         if (ntargets > 2)
325             dam /= ntargets / 2;
326
327         pr_beep();
328         pr("Kaboom!!! Incoming shells!\n");
329         if (sp->shp_own != 0)
330             wu(0, sp->shp_own,
331                "%s fired at %s\n", prship(sp), prship(targ));
332         pr_beep();
333         pr("BLAM! %d damage!\n", dam);
334         shipdamage(targ, dam);
335         putship(targ->shp_uid, targ);
336     } else {
337         dam = shp_dchrg(sp);
338         putship(sp->shp_uid, sp);
339         if (dam < 0)
340             return;
341         if (ntargets > 2)
342             dam /= ntargets / 2;
343
344         pr("\nCAPTAIN!  !!Depth charges!!...\n");
345         if (sp->shp_own != 0)
346             wu(0, sp->shp_own,
347                "%s depth charged %s\n", prship(sp), prsub(targ));
348         pr("click...WHAM!  %d damage!\n", dam);
349         shipdamage(targ, dam);
350         putship(targ->shp_uid, targ);
351     }
352 }
353
354 static int
355 fire_torp(struct shpstr *sp, struct shpstr *targ, int ntargets)
356 {
357     int range, erange, dam;
358     int shells;
359     double hitchance;
360
361     erange = roundrange(torprange(sp));
362     range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
363     if (range > erange)
364         return 0;
365
366     shells = sp->shp_item[I_SHELL];
367
368     if (shells < SHP_TORP_SHELLS)
369         shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
370                                 I_SHELL, SHP_TORP_SHELLS - shells);
371
372     if (sp->shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
373         return 0;
374
375     if (sp->shp_item[I_MILIT] < 1)
376         return 0;
377
378     if (sp->shp_effic < 60)
379         return 0;
380
381     if (sp->shp_mobil <= 0)
382         return 0;
383
384     if (!line_of_sight(NULL, sp->shp_x, sp->shp_y,
385                        targ->shp_x, targ->shp_y))
386         return 0;
387
388     /* All set.. fire! */
389     shells -= SHP_TORP_SHELLS;
390     sp->shp_item[I_SHELL] = shells;
391     putship(sp->shp_uid, sp);
392
393     /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
394     sp->shp_mobil -= shp_mobcost(sp) / 2.0;
395
396     hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
397
398     pr("Captain! Torpedoes sighted!\n");
399
400     if (chance(hitchance)) {
401         pr("BOOM!...\n");
402         if (sp->shp_own != 0)
403             wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
404                prship(sp),
405                xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
406         dam = TORP_DAMAGE();
407         if (ntargets > 2)
408             dam /= ntargets / 2;
409
410         shipdamage(targ, dam);
411         putship(targ->shp_uid, targ);
412
413         if (mchr[(int)sp->shp_type].m_flags & M_SUB)
414             nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
415         else
416             nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
417     } else {
418         pr("Missed!\n");
419         if (sp->shp_own != 0)
420             wu(0, sp->shp_own,
421                "%s missed %s with a torp at %s\n",
422                prship(sp), prsub(targ),
423                xyas(sp->shp_x, sp->shp_y, sp->shp_own));
424     }
425
426     return 1;
427 }
428
429 char *
430 prsub(struct shpstr *sp)
431 {
432     if (mchr[(int)sp->shp_type].m_flags & M_SUB)
433         return "sub";
434     else
435         return prship(sp);
436 }