2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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 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.
28 * torp.c: Fire torpedoes at enemy ships
30 * Known contributors to this file:
32 * Thomas Ruschak, 1992
46 static void anti_torp(int f, int ntorping, int vshipown);
47 static int candchrg(struct shpstr *, struct shpstr *);
48 static int canshoot(struct shpstr *, struct shpstr *);
49 static int cantorp(struct shpstr *, struct shpstr *);
50 static void fire_dchrg(struct shpstr *, struct shpstr *, int);
51 static int fire_torp(struct shpstr *, struct shpstr *, int, int);
67 struct nstr_item nbst;
73 if (!(sav = getstarg(player->argp[1], "From ship(s)? ", buf)))
75 if (!snxtitem(&nbst, EF_SHIP, sav))
77 while (nxtitem(&nbst, &sub)) {
78 if (sub.shp_own != player->cnum)
80 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
82 shells = sub.shp_item[I_SHELL];
83 if (shells < SHP_TORP_SHELLS)
84 shells += supply_commod(sub.shp_own, sub.shp_x, sub.shp_y,
85 I_SHELL, SHP_TORP_SHELLS - shells);
86 if (sub.shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
88 if (sub.shp_item[I_MILIT] < 1)
90 if (sub.shp_effic < 60)
92 if (sub.shp_mobil <= 0)
96 pr("%d ships are eligible to torp\n", ntorping);
97 snxtitem(&nbst, EF_SHIP, sav);
98 while (nxtitem(&nbst, &sub)) {
101 if (sub.shp_own != player->cnum) {
104 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0) {
105 pr("Ship # %d: A %s can't fire torpedoes!\n",
106 sub.shp_uid, mchr[(int)sub.shp_type].m_name);
109 shells = sub.shp_item[I_SHELL];
110 if (shells < SHP_TORP_SHELLS)
111 shells += supply_commod(sub.shp_own, sub.shp_x, sub.shp_y,
112 I_SHELL, SHP_TORP_SHELLS - shells);
113 if (sub.shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS) {
114 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
117 if (sub.shp_item[I_MILIT] < 1) {
118 pr("Ship #%d has insufficient crew\n", sub.shp_uid);
121 if (sub.shp_effic < 60) {
122 pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
125 if (sub.shp_mobil <= 0) {
126 pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
130 sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
131 if ((ptr = getstarg(player->argp[2], prompt, buf)) == 0)
133 if (!check_ship_ok(&sub))
135 if ((victno = atoi(ptr)) < 0)
137 if (!getship(victno, &vship))
141 vshipown = vship.shp_own;
142 if (victno == subno) {
143 pr("Shooting yourself, eh? How strange...\n");
146 if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
147 if (!(mchr[(int)sub.shp_type].m_flags & M_SUBT)) {
148 pr("You can't torpedo a submarine!\n");
152 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
153 anti_torp(sub.shp_uid, ntorping, vshipown);
154 getship(sub.shp_uid, &sub);
155 if (sub.shp_own == 0) {
158 erange = roundrange(torprange(&sub));
159 pr("Effective torpedo range is %d.0\n", erange);
160 shells -= SHP_TORP_SHELLS;
161 sub.shp_item[I_SHELL] = shells;
162 putship(sub.shp_uid, &sub);
163 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
164 sub.shp_mobil -= shp_mobcost(&sub) / 2.0;
166 getship(victno, &vship);
167 vshipown = vship.shp_own;
168 range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
169 hitchance = DTORP_HITCHANCE(range, sub.shp_visib);
170 if (range <= erange) {
171 pr("Hitchance = %d%%\n", (int)(hitchance * 100));
173 /* Now, can the torpedo even get there? */
174 if (!line_of_sight(NULL, sub.shp_x, sub.shp_y,
175 vship.shp_x, vship.shp_y)) {
176 pr("BOOM!... Torpedo slams into land before reaching target.\n");
177 /* We only tell the victim if we were within range. */
178 if (range <= erange) {
180 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
181 xyas(sub.shp_x, sub.shp_y, vshipown),
184 } else if (range > erange) {
185 pr("Out of range\n");
186 } else if (hitchance >= 1.0 || chance(hitchance)) {
190 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n",
191 prsub(&sub), xyas(sub.shp_x, sub.shp_y, vshipown),
192 prship(&vship), dam);
193 if (vship.shp_rflags & RET_TORPED) {
194 retreat_ship(&vship, 't');
195 shipdamage(&vship, dam);
197 shipdamage(&vship, dam);
198 pr("Torpedo hit %s for %d damage.\n", prship(&vship), dam);
200 if (vship.shp_effic < SHIP_MINEFF)
201 pr("%s sunk!\n", prship(&vship));
202 putship(vship.shp_uid, &vship);
203 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
204 nreport(vshipown, N_TORP_SHIP, 0, 1);
206 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
210 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
211 xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
214 putship(sub.shp_uid, &sub);
215 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
216 anti_torp(sub.shp_uid, ntorping, vshipown);
222 anti_torp(int f, int ntorping, int vshipown)
231 if (sub.shp_own == vshipown)
234 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
235 pr("Starting our attack run...\n");
238 while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
241 if (dd.shp_own != vshipown)
243 if (!canshoot(&dd, &sub))
246 erange = roundrange(effrange(dd.shp_frnge, dd.shp_tech));
247 range = mapdist(sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y);
251 if (!line_of_sight(NULL, sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y))
254 if (cantorp(&dd, &sub)) {
255 /* Try torping.. if we can, maybe we can fire */
256 if (!fire_torp(&dd, &sub, range, ntorping))
257 if (candchrg(&dd, &sub))
258 fire_dchrg(&dd, &sub, ntorping);
260 fire_dchrg(&dd, &sub, ntorping);
264 /* Can ship A shoot at ship B? */
266 canshoot(struct shpstr *a, struct shpstr *b)
268 /* Anyone can shoot a normal ship */
269 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
272 /* You can depth-charge a sub */
273 if (mchr[(int)a->shp_type].m_flags & M_DCH)
276 /* If you have SUBT flag, you can torp a sub */
277 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
283 /* Can ship A torp ship B? */
285 cantorp(struct shpstr *a, struct shpstr *b)
287 if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
290 /* Anyone with TORP flag can torp a normal ship */
291 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
294 /* Ship b is a sub, so we need to have the SUBT flag */
295 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
301 /* Can ship A depth-charge (or fire guns at) ship B? */
303 candchrg(struct shpstr *a, struct shpstr *b)
305 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0) {
306 if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
312 if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
319 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
323 if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
325 putship(sp->shp_uid, sp);
332 pr("Kaboom!!! Incoming shells!\n");
333 if (sp->shp_own != 0)
335 "%s fired at %s\n", prship(sp), prship(targ));
337 pr("BLAM! %d damage!\n", dam);
338 shipdamage(targ, dam);
339 putship(targ->shp_uid, targ);
342 putship(sp->shp_uid, sp);
348 pr("\nCAPTAIN! !!Depth charges!!...\n");
349 if (sp->shp_own != 0)
351 "%s depth charged %s\n", prship(sp), prsub(targ));
352 pr("click...WHAM! %d damage!\n", dam);
353 shipdamage(targ, dam);
354 putship(targ->shp_uid, targ);
359 fire_torp(struct shpstr *sp, struct shpstr *targ, int range, int ntargets)
365 shells = sp->shp_item[I_SHELL];
367 if (shells < SHP_TORP_SHELLS)
368 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
369 I_SHELL, SHP_TORP_SHELLS - shells);
371 if (sp->shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
374 if (sp->shp_item[I_MILIT] < 1)
377 if (sp->shp_effic < 60)
380 if (sp->shp_mobil <= 0)
383 /* All set.. fire! */
384 shells -= SHP_TORP_SHELLS;
385 sp->shp_item[I_SHELL] = shells;
386 putship(sp->shp_uid, sp);
388 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
389 sp->shp_mobil -= shp_mobcost(sp) / 2.0;
391 hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
393 pr("Captain! Torpedoes sighted!\n");
395 if (chance(hitchance)) {
397 if (sp->shp_own != 0)
398 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
400 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
405 shipdamage(targ, dam);
406 putship(targ->shp_uid, targ);
408 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
409 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
411 nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
414 if (sp->shp_own != 0)
416 "%s missed %s with a torp at %s\n",
417 prship(sp), prsub(targ),
418 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
425 prsub(struct shpstr *sp)
427 if (mchr[(int)sp->shp_type].m_flags & M_SUB)