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