]> git.pond.sub.org Git - empserver/blob - src/lib/commands/torp.c
Factor out common ship gun fire code into shp_fire()
[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, 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     int range, erange;
225     struct shpstr sub;
226     struct shpstr dd;
227     int x;
228
229     getship(f, &sub);
230
231     if (sub.shp_own == vshipown)
232         return;
233
234     if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
235         pr("Starting our attack run...\n");
236
237     x = 0;
238     while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
239         if (dd.shp_own == 0)
240             continue;
241         if (dd.shp_own != vshipown)
242             continue;
243         if (!canshoot(&dd, &sub))
244             continue;
245
246         erange = roundrange(effrange(dd.shp_frnge, dd.shp_tech));
247         range = mapdist(sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y);
248         if (range > erange)
249             continue;
250
251         if (!line_of_sight(NULL, sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y))
252             continue;
253
254         if (cantorp(&dd, &sub)) {
255             /* Try torping.. if we can, maybe we can fire */
256             if (!fire_torp(&dd, &sub, range, ntorping))
257                 if (candchrg(&dd, &sub))
258                     fire_dchrg(&dd, &sub, ntorping);
259         } else
260             fire_dchrg(&dd, &sub, ntorping);
261     }
262 }
263
264 /* Can ship A shoot at ship B? */
265 static int
266 canshoot(struct shpstr *a, struct shpstr *b)
267 {
268     /* Anyone can shoot a normal ship */
269     if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
270         return 1;
271
272     /* You can depth-charge a sub */
273     if (mchr[(int)a->shp_type].m_flags & M_DCH)
274         return 1;
275
276     /* If you have SUBT flag, you can torp a sub */
277     if (mchr[(int)a->shp_type].m_flags & M_SUBT)
278         return 1;
279
280     return 0;
281 }
282
283 /* Can ship A torp ship B? */
284 static int
285 cantorp(struct shpstr *a, struct shpstr *b)
286 {
287     if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
288         return 0;
289
290     /* Anyone with TORP flag can torp a normal ship */
291     if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
292         return 1;
293
294     /* Ship b is a sub, so we need to have the SUBT flag */
295     if (mchr[(int)a->shp_type].m_flags & M_SUBT)
296         return 1;
297
298     return 0;
299 }
300
301 /* Can ship A depth-charge (or fire guns at) ship B? */
302 static int
303 candchrg(struct shpstr *a, struct shpstr *b)
304 {
305     if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0) {
306         if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
307             return 1;
308
309         return 0;
310     }
311
312     if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
313         return 0;
314
315     return 1;
316 }
317
318 static void
319 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
320 {
321     int dam;
322
323     if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
324         dam = shp_fire(sp);
325         putship(sp->shp_uid, sp);
326         if (dam < 0)
327             return;
328         if (ntargets > 2)
329             dam /= ntargets / 2;
330
331         pr_beep();
332         pr("Kaboom!!! Incoming shells!\n");
333         if (sp->shp_own != 0)
334             wu(0, sp->shp_own,
335                "%s fired at %s\n", prship(sp), prship(targ));
336         pr_beep();
337         pr("BLAM! %d damage!\n", dam);
338         shipdamage(targ, dam);
339         putship(targ->shp_uid, targ);
340     } else {
341         dam = shp_dchrg(sp);
342         putship(sp->shp_uid, sp);
343         if (dam < 0)
344             return;
345         if (ntargets > 2)
346             dam /= ntargets / 2;
347
348         pr("\nCAPTAIN!  !!Depth charges!!...\n");
349         if (sp->shp_own != 0)
350             wu(0, sp->shp_own,
351                "%s depth charged %s\n", prship(sp), prsub(targ));
352         pr("click...WHAM!  %d damage!\n", dam);
353         shipdamage(targ, dam);
354         putship(targ->shp_uid, targ);
355     }
356 }
357
358 static int
359 fire_torp(struct shpstr *sp, struct shpstr *targ, int range, int ntargets)
360 {
361     int dam;
362     int shells;
363     double hitchance;
364
365     shells = sp->shp_item[I_SHELL];
366
367     if (shells < SHP_TORP_SHELLS)
368         shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
369                                 I_SHELL, SHP_TORP_SHELLS - shells);
370
371     if (sp->shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
372         return 0;
373
374     if (sp->shp_item[I_MILIT] < 1)
375         return 0;
376
377     if (sp->shp_effic < 60)
378         return 0;
379
380     if (sp->shp_mobil <= 0)
381         return 0;
382
383     /* All set.. fire! */
384     shells -= SHP_TORP_SHELLS;
385     sp->shp_item[I_SHELL] = shells;
386     putship(sp->shp_uid, sp);
387
388     /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
389     sp->shp_mobil -= shp_mobcost(sp) / 2.0;
390
391     hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
392
393     pr("Captain! Torpedoes sighted!\n");
394
395     if (chance(hitchance)) {
396         pr("BOOM!...\n");
397         if (sp->shp_own != 0)
398             wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
399                prship(sp),
400                xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
401         dam = TORP_DAMAGE();
402         if (ntargets > 2)
403             dam /= ntargets / 2;
404
405         shipdamage(targ, dam);
406         putship(targ->shp_uid, targ);
407
408         if (mchr[(int)sp->shp_type].m_flags & M_SUB)
409             nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
410         else
411             nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
412     } else {
413         pr("Missed!\n");
414         if (sp->shp_own != 0)
415             wu(0, sp->shp_own,
416                "%s missed %s with a torp at %s\n",
417                prship(sp), prsub(targ),
418                xyas(sp->shp_x, sp->shp_y, sp->shp_own));
419     }
420
421     return 1;
422 }
423
424 char *
425 prsub(struct shpstr *sp)
426 {
427     if (mchr[(int)sp->shp_type].m_flags & M_SUB)
428         return "sub";
429     else
430         return prship(sp);
431 }