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