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);
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)
230 if (sub.shp_own == vshipown)
233 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
234 pr("Starting our attack run...\n");
237 while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
240 if (dd.shp_own != vshipown)
242 if (!canshoot(&dd, &sub))
245 if (cantorp(&dd, &sub)) {
246 /* Try torping.. if we can, maybe we can fire */
247 if (!fire_torp(&dd, &sub, ntorping))
248 if (candchrg(&dd, &sub))
249 fire_dchrg(&dd, &sub, ntorping);
251 fire_dchrg(&dd, &sub, ntorping);
255 /* Can ship A shoot at ship B? */
257 canshoot(struct shpstr *a, struct shpstr *b)
259 /* Anyone can shoot a normal ship */
260 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
263 /* You can depth-charge a sub */
264 if (mchr[(int)a->shp_type].m_flags & M_DCH)
267 /* If you have SUBT flag, you can torp a sub */
268 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
274 /* Can ship A torp ship B? */
276 cantorp(struct shpstr *a, struct shpstr *b)
278 if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
281 /* Anyone with TORP flag can torp a normal ship */
282 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
285 /* Ship b is a sub, so we need to have the SUBT flag */
286 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
292 /* Can ship A depth-charge (or fire guns at) ship B? */
294 candchrg(struct shpstr *a, struct shpstr *b)
296 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0) {
297 if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
303 if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
310 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
312 int range, erange, dam;
314 erange = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
315 range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
319 if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
321 putship(sp->shp_uid, sp);
328 pr("Kaboom!!! Incoming shells!\n");
329 if (sp->shp_own != 0)
331 "%s fired at %s\n", prship(sp), prship(targ));
333 pr("BLAM! %d damage!\n", dam);
334 shipdamage(targ, dam);
335 putship(targ->shp_uid, targ);
338 putship(sp->shp_uid, sp);
344 pr("\nCAPTAIN! !!Depth charges!!...\n");
345 if (sp->shp_own != 0)
347 "%s depth charged %s\n", prship(sp), prsub(targ));
348 pr("click...WHAM! %d damage!\n", dam);
349 shipdamage(targ, dam);
350 putship(targ->shp_uid, targ);
355 fire_torp(struct shpstr *sp, struct shpstr *targ, int ntargets)
357 int range, erange, dam;
361 erange = roundrange(torprange(sp));
362 range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
366 shells = sp->shp_item[I_SHELL];
368 if (shells < SHP_TORP_SHELLS)
369 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
370 I_SHELL, SHP_TORP_SHELLS - shells);
372 if (sp->shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
375 if (sp->shp_item[I_MILIT] < 1)
378 if (sp->shp_effic < 60)
381 if (sp->shp_mobil <= 0)
384 if (!line_of_sight(NULL, sp->shp_x, sp->shp_y,
385 targ->shp_x, targ->shp_y))
388 /* All set.. fire! */
389 shells -= SHP_TORP_SHELLS;
390 sp->shp_item[I_SHELL] = shells;
391 putship(sp->shp_uid, sp);
393 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
394 sp->shp_mobil -= shp_mobcost(sp) / 2.0;
396 hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
398 pr("Captain! Torpedoes sighted!\n");
400 if (chance(hitchance)) {
402 if (sp->shp_own != 0)
403 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
405 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
410 shipdamage(targ, dam);
411 putship(targ->shp_uid, targ);
413 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
414 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
416 nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
419 if (sp->shp_own != 0)
421 "%s missed %s with a torp at %s\n",
422 prship(sp), prsub(targ),
423 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
430 prsub(struct shpstr *sp)
432 if (mchr[(int)sp->shp_type].m_flags & M_SUB)