]> git.pond.sub.org Git - empserver/blob - src/lib/commands/torp.c
COPYING duplicates information from README. Remove. Move GPL from
[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 *sp, struct shpstr *targ, int ntargets);
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     double erange;
70     double hitchance;
71     struct shpstr vship;
72     struct shpstr sub;
73     s_char *ptr;
74     double mobcost;
75     struct mchrstr *mcp;
76     struct nstr_item nbst;
77     s_char buf[1024];
78     s_char *sav;
79     int ntorping = 0;
80     s_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 = ((double)sub.shp_effic / 100.0) *
168             techfact(sub.shp_tech, ((double)sub.shp_frnge));
169         erange = (double)roundrange(erange);
170         pr("Effective torpedo range is %.1f\n", erange);
171         shells -= SHP_TORP_SHELLS;
172         sub.shp_item[I_SHELL] = shells;
173         putship(sub.shp_uid, &sub);
174         mcp = &mchr[(int)sub.shp_type];
175         mobcost = sub.shp_effic * 0.01 * sub.shp_speed;
176         mobcost = (480.0 / (mobcost + techfact(sub.shp_tech, mobcost)));
177
178         /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
179         mobcost /= 2.0;
180         sub.shp_mobil -= mobcost;
181         pr("Whooosh... ");
182         getship(victno, &vship);
183         vshipown = vship.shp_own;
184         range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
185         hitchance = DTORP_HITCHANCE(range, sub.shp_visib);
186         if (range <= erange) {
187             pr("Hitchance = %d%%\n", (int)(hitchance * 100));
188         }
189         /* Now, can the torpedo even get there? */
190         if (!line_of_sight(NULL, sub.shp_x, sub.shp_y,
191                            vship.shp_x, vship.shp_y)) {
192             pr("BOOM!... Torpedo slams into land before reaching target.\n");
193             /* We only tell the victim if we were within range. */
194             if (range <= erange) {
195                 if (vshipown != 0)
196                     wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
197                        xyas(sub.shp_x, sub.shp_y, vshipown),
198                        prship(&vship));
199             }
200         } else if (range > erange) {
201             pr("Out of range\n");
202         } else if (hitchance >= 1.0 || chance(hitchance)) {
203             pr("BOOM!...\n");
204             dam = TORP_DAMAGE();
205             if (vshipown != 0)
206                 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n",
207                    prsub(&sub), xyas(sub.shp_x, sub.shp_y, vshipown),
208                    prship(&vship), dam);
209             if (vship.shp_rflags & RET_TORPED) {
210                 retreat_ship(&vship, 't');
211                 shipdamage(&vship, dam);
212             } else
213                 shipdamage(&vship, dam);
214             pr("Torpedo hit %s for %d damage.\n", prship(&vship), dam);
215
216             if (vship.shp_effic < SHIP_MINEFF)
217                 pr("%s sunk!\n", prship(&vship));
218             putship(vship.shp_uid, &vship);
219             if (mchr[(int)sub.shp_type].m_flags & M_SUB)
220                 nreport(vshipown, N_TORP_SHIP, 0, 1);
221             else
222                 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
223         } else {
224             pr("Missed\n");
225             if (vshipown != 0)
226                 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
227                    xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
228         }
229         sub.shp_mission = 0;
230         putship(sub.shp_uid, &sub);
231         if (mchr[(int)sub.shp_type].m_flags & M_SUB)
232             anti_torp(sub.shp_uid, ntorping, vshipown);
233     }
234     return RET_OK;
235 }
236
237 static void
238 anti_torp(int f, int ntorping, int vshipown)
239 {
240     int range;
241     double erange;
242     struct shpstr sub;
243     struct shpstr dd;
244     int x;
245
246     getship(f, &sub);
247
248     if (sub.shp_own == vshipown)
249         return;
250
251     if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
252         pr("Starting our attack run...\n");
253
254     x = 0;
255     while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
256         if (dd.shp_own == 0)
257             continue;
258         if (dd.shp_own != vshipown)
259             continue;
260         if (dd.shp_effic < 60)
261             continue;
262
263         if (!canshoot(&dd, &sub))
264             continue;
265
266         erange = techfact(dd.shp_tech, ((double)dd.shp_frnge)) / 2.0;
267
268         erange = (double)roundrange(erange);
269
270         range = mapdist(sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y);
271
272         if (range > erange)
273             continue;
274
275         if (!line_of_sight(NULL, sub.shp_x, sub.shp_y, 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 }