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
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.
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.
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
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.
28 * torp.c: Fire torpedoes at enemy ships
30 * Known contributors to this file:
32 * Thomas Ruschak, 1992
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 range,
57 static int fire_torp(struct shpstr *, struct shpstr *, int, int);
59 s_char *prsub(struct shpstr *sp);
64 extern int torpedo_damage;
78 struct nstr_item nbst;
84 if (!(sav = getstarg(player->argp[1], "From ship(s)? ", buf)))
86 if (!snxtitem(&nbst, EF_SHIP, sav))
88 while (nxtitem(&nbst, (s_char *)&sub)) {
89 if (sub.shp_own != player->cnum)
91 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
93 shells = getvar(V_SHELL, (s_char *)&sub, EF_SHIP);
96 supply_commod(sub.shp_own, sub.shp_x, sub.shp_y, I_SHELL,
98 if (getvar(V_GUN, (s_char *)&sub, EF_SHIP) == 0 || shells < 3)
100 if (getvar(V_MILIT, (s_char *)&sub, EF_SHIP) < 1)
102 if (sub.shp_effic < 60)
104 if (sub.shp_mobil <= 0)
108 pr("%d ships are eligible to torp\n", ntorping);
109 snxtitem(&nbst, EF_SHIP, sav);
110 while (nxtitem(&nbst, (s_char *)&sub)) {
113 if (sub.shp_own != player->cnum) {
116 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0) {
117 pr("Ship # %d: A %s can't fire torpedoes!\n", sub.shp_uid,
118 mchr[(int)sub.shp_type].m_name);
121 shells = getvar(V_SHELL, (s_char *)&sub, EF_SHIP);
124 supply_commod(sub.shp_own, sub.shp_x, sub.shp_y, I_SHELL,
126 if (getvar(V_GUN, (s_char *)&sub, EF_SHIP) == 0 || shells < 3) {
127 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
130 if (getvar(V_MILIT, (s_char *)&sub, EF_SHIP) < 1) {
131 pr("Ship #%d has insufficient crew\n", sub.shp_uid);
134 if (sub.shp_effic < 60) {
135 pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
138 if (sub.shp_mobil <= 0) {
139 pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
143 sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
144 if ((ptr = getstarg(player->argp[2], prompt, buf)) == 0)
146 if (!check_ship_ok(&sub))
148 if ((victno = atoi(ptr)) < 0)
150 if (!getship(victno, &vship))
154 vshipown = vship.shp_own;
155 if (victno == subno) {
156 pr("Shooting yourself, eh? How strange...\n");
159 if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
160 if (!(mchr[(int)sub.shp_type].m_flags & M_SUBT)) {
161 pr("You can't torpedo a submarine!\n");
165 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
166 anti_torp(sub.shp_uid, ntorping, vshipown);
167 getship(sub.shp_uid, &sub);
168 if (sub.shp_own == 0) {
171 erange = ((double)sub.shp_effic / 100.0) *
172 techfact(sub.shp_tech, ((double)sub.shp_frnge));
173 erange = (double)roundrange(erange);
174 pr("Effective torpedo range is %.1f\n", erange);
176 putvar(V_SHELL, shells, (s_char *)&sub, EF_SHIP);
177 putship(sub.shp_uid, &sub);
178 mcp = &mchr[(int)sub.shp_type];
179 mobcost = sub.shp_effic * 0.01 * sub.shp_speed;
180 mobcost = (480.0 / (mobcost + techfact(sub.shp_tech, mobcost)));
182 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
184 sub.shp_mobil -= mobcost;
186 getship(victno, &vship);
187 vshipown = vship.shp_own;
188 range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
189 hitchance = DTORP_HITCHANCE(range, sub.shp_visib);
190 if (range <= erange) {
191 pr("Hitchance = %d%%\n", (int)(hitchance * 100));
193 /* Now, can the torpedo even get there? */
194 if (!line_of_sight((s_char **)0, sub.shp_x, sub.shp_y,
195 vship.shp_x, vship.shp_y)) {
196 pr("BOOM!... Torpedo slams into land before reaching target.\n");
197 /* We only tell the victim if we were within range. */
198 if (range <= erange) {
200 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
201 xyas(sub.shp_x, sub.shp_y, vshipown),
204 } else if (range > erange) {
205 pr("Out of range\n");
206 } else if (hitchance >= 1.0 || chance(hitchance)) {
210 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n",
211 prsub(&sub), xyas(sub.shp_x, sub.shp_y, vshipown),
212 prship(&vship), dam);
213 if (vship.shp_rflags & RET_TORPED) {
214 retreat_ship(&vship, 't');
215 shipdamage(&vship, dam);
217 shipdamage(&vship, dam);
218 pr("Torpedo hit %s for %d damage.\n", prship(&vship), dam);
220 if (vship.shp_effic < SHIP_MINEFF)
221 pr("%s sunk!\n", prship(&vship));
222 putship(vship.shp_uid, &vship);
223 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
224 nreport(vshipown, N_TORP_SHIP, 0, 1);
226 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
230 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
231 xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
234 putship(sub.shp_uid, &sub);
235 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
236 anti_torp(sub.shp_uid, ntorping, vshipown);
242 anti_torp(int f, int ntorping, int vshipown)
252 if (sub.shp_own == vshipown)
255 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
256 pr("Starting our attack run...\n");
259 while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
262 if (dd.shp_own != vshipown)
264 if (dd.shp_effic < 60)
267 if (!canshoot(&dd, &sub))
270 erange = techfact(dd.shp_tech, ((double)dd.shp_frnge)) / 2.0;
272 erange = (double)roundrange(erange);
274 range = mapdist(sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y);
279 if (!line_of_sight((s_char **)0, sub.shp_x, sub.shp_y,
283 if (cantorp(&dd, &sub)) {
284 /* Try torping.. if we can, maybe we can fire */
285 if (!fire_torp(&dd, &sub, range, ntorping))
286 if (candchrg(&dd, &sub))
287 fire_dchrg(&dd, &sub, range, ntorping);
289 fire_dchrg(&dd, &sub, range, ntorping);
293 /* Can ship A shoot at ship B? */
295 canshoot(struct shpstr *a, struct shpstr *b)
297 /* Anyone can shoot a normal ship */
298 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
301 /* You can depth-charge a sub */
302 if (mchr[(int)a->shp_type].m_flags & M_DCH)
305 /* If you have SUBT flag, you can torp a sub */
306 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
312 /* Can ship A torp ship B? */
314 cantorp(struct shpstr *a, struct shpstr *b)
316 if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
319 /* Anyone with TORP flag can torp a normal ship */
320 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
323 /* Ship b is a sub, so we need to have the SUBT flag */
324 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
330 /* Can ship A depth-charge (or fire guns at) ship B? */
332 candchrg(struct shpstr *a, struct shpstr *b)
334 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0) {
335 if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
341 if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
348 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int range, int ntargets)
355 shells = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
356 gun = getvar(V_GUN, (s_char *)sp, EF_SHIP);
357 gun = min(gun, sp->shp_glim);
358 gun = min(gun, getvar(V_MILIT, (s_char *)sp, EF_SHIP) / 2);
361 supply_commod(sp->shp_own, sp->shp_x, sp->shp_y, I_SHELL,
362 (gun + 1) / 2 - shells);
364 gun = min(gun, shells * 2);
368 /* ok, all set.. now, we shoot */
369 shells -= ldround(((double)gun) / 2.0, 1);
370 putvar(V_SHELL, shells, (s_char *)sp, EF_SHIP);
371 putship(sp->shp_uid, sp);
373 guneff = seagun(sp->shp_effic, gun);
376 if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
378 pr("Kaboom!!! Incoming shells!\n");
379 if (sp->shp_own != 0)
381 "%s fired at %s\n", prship(sp), prship(targ));
384 dam /= ((float)ntargets / 2.0);
386 pr("BLAM! %d damage!\n", dam);
387 shipdamage(targ, dam);
388 putship(targ->shp_uid, targ);
390 pr("\nCAPTAIN! !!Depth charges!!...\n");
391 if (sp->shp_own != 0)
393 "%s depth charged %s\n", prship(sp), prsub(targ));
396 dam /= ((float)ntargets / 2.0);
398 pr("click...WHAM! %d damage!\n", dam);
399 shipdamage(targ, dam);
400 putship(targ->shp_uid, targ);
405 fire_torp(struct shpstr *sp, struct shpstr *targ, int range, int ntargets)
407 extern int torpedo_damage;
414 shells = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
417 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y, I_SHELL,
420 if (getvar(V_GUN, (s_char *)sp, EF_SHIP) == 0 || shells < 3)
423 if (getvar(V_MILIT, (s_char *)sp, EF_SHIP) < 1)
426 if (sp->shp_effic < 60)
429 if (sp->shp_mobil <= 0)
432 /* All set.. fire! */
434 putvar(V_SHELL, shells, (s_char *)sp, EF_SHIP);
435 putship(sp->shp_uid, sp);
437 mcp = &mchr[(int)sp->shp_type];
438 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
439 mobcost = (480.0 / (mobcost + techfact(sp->shp_tech, mobcost)));
441 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
443 sp->shp_mobil -= mobcost;
445 hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
447 pr("Captain! Torpedoes sighted!\n");
449 if (chance(hitchance)) {
451 if (sp->shp_own != 0)
452 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
454 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
458 dam /= ((float)ntargets / 2.0);
460 shipdamage(targ, dam);
461 putship(targ->shp_uid, targ);
463 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
464 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
466 nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
469 if (sp->shp_own != 0)
471 "%s missed %s with a torp at %s\n",
472 prship(sp), prsub(targ),
473 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
480 prsub(struct shpstr *sp)
482 if (mchr[(int)sp->shp_type].m_flags & M_SUB)