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);
66 struct nstr_item nbst;
72 if (!(sav = getstarg(player->argp[1], "From ship(s)? ", buf)))
74 if (!snxtitem(&nbst, EF_SHIP, sav))
76 while (nxtitem(&nbst, &sub)) {
77 if (sub.shp_own != player->cnum)
79 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
81 if (sub.shp_item[I_GUN] == 0
82 || sub.shp_item[I_SHELL] < SHP_TORP_SHELLS)
84 if (sub.shp_item[I_MILIT] < 1)
86 if (sub.shp_effic < 60)
88 if (sub.shp_mobil <= 0)
92 pr("%d ships are eligible to torp\n", ntorping);
93 snxtitem(&nbst, EF_SHIP, sav);
94 while (nxtitem(&nbst, &sub)) {
97 if (sub.shp_own != player->cnum) {
100 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0) {
101 pr("Ship # %d: A %s can't fire torpedoes!\n",
102 sub.shp_uid, mchr[(int)sub.shp_type].m_name);
105 if (sub.shp_item[I_GUN] == 0
106 || sub.shp_item[I_SHELL] < SHP_TORP_SHELLS) {
107 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
110 if (sub.shp_item[I_MILIT] < 1) {
111 pr("Ship #%d has insufficient crew\n", sub.shp_uid);
114 if (sub.shp_effic < 60) {
115 pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
118 if (sub.shp_mobil <= 0) {
119 pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
123 sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
124 if ((ptr = getstarg(player->argp[2], prompt, buf)) == 0)
126 if (!check_ship_ok(&sub))
128 if ((victno = atoi(ptr)) < 0)
130 if (!getship(victno, &vship))
134 vshipown = vship.shp_own;
135 if (victno == subno) {
136 pr("Shooting yourself, eh? How strange...\n");
139 if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
140 if (!(mchr[(int)sub.shp_type].m_flags & M_SUBT)) {
141 pr("You can't torpedo a submarine!\n");
145 dam = shp_torp(&sub, 1);
147 putship(sub.shp_uid, &sub);
148 if (CANT_HAPPEN(dam < 0)) {
149 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
153 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
154 anti_torp(sub.shp_uid, ntorping, vshipown);
155 getship(sub.shp_uid, &sub);
156 if (sub.shp_own == 0)
159 erange = roundrange(torprange(&sub));
160 pr("Effective torpedo range is %d.0\n", erange);
162 getship(victno, &vship);
163 vshipown = vship.shp_own;
164 range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
165 hitchance = DTORP_HITCHANCE(range, sub.shp_visib);
166 if (range <= erange) {
167 pr("Hitchance = %d%%\n", (int)(hitchance * 100));
169 /* Now, can the torpedo even get there? */
170 if (!line_of_sight(NULL, sub.shp_x, sub.shp_y,
171 vship.shp_x, vship.shp_y)) {
172 pr("BOOM!... Torpedo slams into land before reaching target.\n");
173 /* We only tell the victim if we were within range. */
174 if (range <= erange) {
176 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
177 xyas(sub.shp_x, sub.shp_y, vshipown),
180 } else if (range > erange) {
181 pr("Out of range\n");
182 } else if (chance(hitchance)) {
185 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n",
186 prsub(&sub), xyas(sub.shp_x, sub.shp_y, vshipown),
187 prship(&vship), dam);
188 if (vship.shp_rflags & RET_TORPED) {
189 retreat_ship(&vship, 't');
190 shipdamage(&vship, dam);
192 shipdamage(&vship, dam);
193 pr("Torpedo hit %s for %d damage.\n", prship(&vship), dam);
195 if (vship.shp_effic < SHIP_MINEFF)
196 pr("%s sunk!\n", prship(&vship));
197 putship(vship.shp_uid, &vship);
198 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
199 nreport(vshipown, N_TORP_SHIP, 0, 1);
201 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
205 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
206 xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
209 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
210 anti_torp(sub.shp_uid, ntorping, vshipown);
216 anti_torp(int f, int ntorping, int vshipown)
224 if (sub.shp_own == vshipown)
227 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
228 pr("Starting our attack run...\n");
231 while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
234 if (dd.shp_own != vshipown)
236 if (!canshoot(&dd, &sub))
239 if (cantorp(&dd, &sub)) {
240 /* Try torping.. if we can, maybe we can fire */
241 if (!fire_torp(&dd, &sub, ntorping))
242 if (candchrg(&dd, &sub))
243 fire_dchrg(&dd, &sub, ntorping);
245 fire_dchrg(&dd, &sub, ntorping);
249 /* Can ship A shoot at ship B? */
251 canshoot(struct shpstr *a, struct shpstr *b)
253 /* Anyone can shoot a normal ship */
254 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
257 /* You can depth-charge a sub */
258 if (mchr[(int)a->shp_type].m_flags & M_DCH)
261 /* If you have SUBT flag, you can torp a sub */
262 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
268 /* Can ship A torp ship B? */
270 cantorp(struct shpstr *a, struct shpstr *b)
272 if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
275 /* Anyone with TORP flag can torp a normal ship */
276 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
279 /* Ship b is a sub, so we need to have the SUBT flag */
280 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
286 /* Can ship A depth-charge (or fire guns at) ship B? */
288 candchrg(struct shpstr *a, struct shpstr *b)
290 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0) {
291 if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
297 if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
304 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
306 int range, erange, dam;
308 erange = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
309 range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
313 if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
315 putship(sp->shp_uid, sp);
322 pr("Kaboom!!! Incoming shells!\n");
323 if (sp->shp_own != 0)
325 "%s fired at %s\n", prship(sp), prship(targ));
327 pr("BLAM! %d damage!\n", dam);
328 shipdamage(targ, dam);
329 putship(targ->shp_uid, targ);
332 putship(sp->shp_uid, sp);
338 pr("\nCAPTAIN! !!Depth charges!!...\n");
339 if (sp->shp_own != 0)
341 "%s depth charged %s\n", prship(sp), prsub(targ));
342 pr("click...WHAM! %d damage!\n", dam);
343 shipdamage(targ, dam);
344 putship(targ->shp_uid, targ);
349 fire_torp(struct shpstr *sp, struct shpstr *targ, int ntargets)
351 int range, erange, dam;
353 erange = roundrange(torprange(sp));
354 range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
358 if (!line_of_sight(NULL, sp->shp_x, sp->shp_y,
359 targ->shp_x, targ->shp_y))
361 dam = shp_torp(sp, 1);
362 putship(sp->shp_uid, sp);
366 pr("Captain! Torpedoes sighted!\n");
368 if (chance(DTORP_HITCHANCE(range, sp->shp_visib))) {
370 if (sp->shp_own != 0)
371 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
373 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
377 shipdamage(targ, dam);
378 putship(targ->shp_uid, targ);
380 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
381 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
383 nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
386 if (sp->shp_own != 0)
388 "%s missed %s with a torp at %s\n",
389 prship(sp), prsub(targ),
390 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
397 prsub(struct shpstr *sp)
399 if (mchr[(int)sp->shp_type].m_flags & M_SUB)