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