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
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
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);
76 struct nstr_item nbst;
82 if (!(sav = getstarg(player->argp[1], "From ship(s)? ", buf)))
84 if (!snxtitem(&nbst, EF_SHIP, sav))
86 while (nxtitem(&nbst, &sub)) {
87 if (sub.shp_own != player->cnum)
89 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
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)
97 if (sub.shp_item[I_MILIT] < 1)
99 if (sub.shp_effic < 60)
101 if (sub.shp_mobil <= 0)
105 pr("%d ships are eligible to torp\n", ntorping);
106 snxtitem(&nbst, EF_SHIP, sav);
107 while (nxtitem(&nbst, &sub)) {
110 if (sub.shp_own != player->cnum) {
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);
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);
126 if (sub.shp_item[I_MILIT] < 1) {
127 pr("Ship #%d has insufficient crew\n", sub.shp_uid);
130 if (sub.shp_effic < 60) {
131 pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
134 if (sub.shp_mobil <= 0) {
135 pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
139 sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
140 if ((ptr = getstarg(player->argp[2], prompt, buf)) == 0)
142 if (!check_ship_ok(&sub))
144 if ((victno = atoi(ptr)) < 0)
146 if (!getship(victno, &vship))
150 vshipown = vship.shp_own;
151 if (victno == subno) {
152 pr("Shooting yourself, eh? How strange...\n");
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");
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) {
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)));
178 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
180 sub.shp_mobil -= mobcost;
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));
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) {
196 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
197 xyas(sub.shp_x, sub.shp_y, vshipown),
200 } else if (range > erange) {
201 pr("Out of range\n");
202 } else if (hitchance >= 1.0 || chance(hitchance)) {
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);
213 shipdamage(&vship, dam);
214 pr("Torpedo hit %s for %d damage.\n", prship(&vship), dam);
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);
222 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
226 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
227 xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
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);
238 anti_torp(int f, int ntorping, int vshipown)
248 if (sub.shp_own == vshipown)
251 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
252 pr("Starting our attack run...\n");
255 while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
258 if (dd.shp_own != vshipown)
260 if (dd.shp_effic < 60)
263 if (!canshoot(&dd, &sub))
266 erange = techfact(dd.shp_tech, ((double)dd.shp_frnge)) / 2.0;
268 erange = (double)roundrange(erange);
270 range = mapdist(sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y);
275 if (!line_of_sight(NULL, sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y))
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);
284 fire_dchrg(&dd, &sub, ntorping);
288 /* Can ship A shoot at ship B? */
290 canshoot(struct shpstr *a, struct shpstr *b)
292 /* Anyone can shoot a normal ship */
293 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
296 /* You can depth-charge a sub */
297 if (mchr[(int)a->shp_type].m_flags & M_DCH)
300 /* If you have SUBT flag, you can torp a sub */
301 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
307 /* Can ship A torp ship B? */
309 cantorp(struct shpstr *a, struct shpstr *b)
311 if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
314 /* Anyone with TORP flag can torp a normal ship */
315 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
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)
325 /* Can ship A depth-charge (or fire guns at) ship B? */
327 candchrg(struct shpstr *a, struct shpstr *b)
329 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0) {
330 if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
336 if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
343 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
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);
356 supply_commod(sp->shp_own, sp->shp_x, sp->shp_y, I_SHELL,
357 (gun + 1) / 2 - shells);
359 gun = MIN(gun, shells * 2);
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);
368 guneff = seagun(sp->shp_effic, gun);
371 if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
373 pr("Kaboom!!! Incoming shells!\n");
374 if (sp->shp_own != 0)
376 "%s fired at %s\n", prship(sp), prship(targ));
379 dam /= ((float)ntargets / 2.0);
381 pr("BLAM! %d damage!\n", dam);
382 shipdamage(targ, dam);
383 putship(targ->shp_uid, targ);
385 pr("\nCAPTAIN! !!Depth charges!!...\n");
386 if (sp->shp_own != 0)
388 "%s depth charged %s\n", prship(sp), prsub(targ));
391 dam /= ((float)ntargets / 2.0);
393 pr("click...WHAM! %d damage!\n", dam);
394 shipdamage(targ, dam);
395 putship(targ->shp_uid, targ);
400 fire_torp(struct shpstr *sp, struct shpstr *targ, int range, int ntargets)
408 shells = sp->shp_item[I_SHELL];
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);
414 if (sp->shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
417 if (sp->shp_item[I_MILIT] < 1)
420 if (sp->shp_effic < 60)
423 if (sp->shp_mobil <= 0)
426 /* All set.. fire! */
427 shells -= SHP_TORP_SHELLS;
428 sp->shp_item[I_SHELL] = shells;
429 putship(sp->shp_uid, sp);
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)));
435 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
437 sp->shp_mobil -= mobcost;
439 hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
441 pr("Captain! Torpedoes sighted!\n");
443 if (chance(hitchance)) {
445 if (sp->shp_own != 0)
446 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
448 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
452 dam /= ((float)ntargets / 2.0);
454 shipdamage(targ, dam);
455 putship(targ->shp_uid, targ);
457 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
458 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
460 nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
463 if (sp->shp_own != 0)
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));
474 prsub(struct shpstr *sp)
476 if (mchr[(int)sp->shp_type].m_flags & M_SUB)