2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * torp.c: Fire torpedoes at enemy ships
29 * Known contributors to this file:
31 * Thomas Ruschak, 1992
34 * Markus Armbruster, 2004-2015
45 static void anti_torp(int f, int ntorping, int vshipown);
46 static void fire_dchrg(struct shpstr *, struct shpstr *, int);
47 static int fire_torp(struct shpstr *, struct shpstr *, int);
61 struct mchrstr *sub_mcp;
63 struct nstr_item nbst;
68 if (!snxtitem(&nbst, EF_SHIP, player->argp[1], "From ship(s)? "))
70 while (nxtitem(&nbst, &sub)) {
71 if (sub.shp_own != player->cnum)
73 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
75 if (sub.shp_item[I_GUN] == 0
76 || sub.shp_item[I_SHELL] < SHP_TORP_SHELLS)
78 if (sub.shp_item[I_MILIT] < 1)
80 if (sub.shp_effic < 60)
82 if (sub.shp_mobil <= 0)
86 pr("%d ships are eligible to torpedo\n", ntorping);
87 snxtitem_rewind(&nbst);
88 while (nxtitem(&nbst, &sub)) {
91 if (sub.shp_own != player->cnum) {
94 sub_mcp = &mchr[sub.shp_type];
95 if (!(sub_mcp->m_flags & M_TORP)) {
96 pr("Ship # %d: A %s can't fire torpedoes!\n",
97 sub.shp_uid, sub_mcp->m_name);
100 if (sub.shp_item[I_GUN] == 0
101 || sub.shp_item[I_SHELL] < SHP_TORP_SHELLS) {
102 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
105 if (sub.shp_item[I_MILIT] < 1) {
106 pr("Ship #%d has insufficient crew\n", sub.shp_uid);
109 if (sub.shp_effic < 60) {
110 pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
113 if (sub.shp_mobil <= 0) {
114 pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
118 sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
119 if (!(ptr = getstarg(player->argp[2], prompt, buf)))
121 if (!check_ship_ok(&sub))
123 if ((victno = atoi(ptr)) < 0)
125 if (!getship(victno, &vship))
129 vshipown = vship.shp_own;
130 if (victno == subno) {
131 pr("Shooting yourself, eh? How strange...\n");
134 if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
135 if (!(sub_mcp->m_flags & M_SUBT)) {
136 pr("You can't torpedo a submarine!\n");
140 dam = shp_torp(&sub, 1);
142 putship(sub.shp_uid, &sub);
143 if (CANT_HAPPEN(dam < 0)) {
144 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
148 if (!(sub_mcp->m_flags & M_SUB)) {
149 pr("Starting our attack run...\n");
150 anti_torp(sub.shp_uid, ntorping, vshipown);
152 getship(sub.shp_uid, &sub);
153 if (sub.shp_own == 0)
156 erange = roundrange(torprange(&sub));
157 pr("Effective torpedo range is %d.0\n", erange);
159 getship(victno, &vship);
160 vshipown = vship.shp_own;
161 range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
162 hitchance = shp_torp_hitchance(&sub, range);
163 if (range <= erange) {
164 pr("Hit chance = %.0f%%\n", hitchance * 100);
167 pr("Out of range\n");
168 else if (!line_of_sight(NULL, sub.shp_x, sub.shp_y,
169 vship.shp_x, vship.shp_y)) {
170 pr("BOOM!... Torpedo slams into land before reaching target.\n");
171 /* We only tell the victim if we were within range. */
172 if (vshipown != 0 && vshipown != player->cnum)
173 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
174 xyas(sub.shp_x, sub.shp_y, vshipown),
176 } else if (chance(hitchance)) {
178 if (vshipown != 0 && vshipown != player->cnum)
179 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n",
180 sub_mcp->m_flags & M_SUB ? "sub" : prship(&sub),
181 xyas(sub.shp_x, sub.shp_y, vshipown),
182 prship(&vship), dam);
183 pr("Torpedo hit %s for %d damage.\n", prsub(&vship), dam);
184 if (!(mchr[vship.shp_type].m_flags & M_SUB)) {
185 if (mchr[sub.shp_type].m_flags & M_SUB)
186 nreport(vshipown, N_TORP_SHIP, 0, 1);
188 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
190 shipdamage(&vship, dam);
191 if (vship.shp_effic < SHIP_MINEFF)
192 pr("%s sunk!\n", prsub(&vship));
193 if (vship.shp_rflags & RET_TORPED)
194 retreat_ship(&vship, vshipown, 't');
195 putship(vship.shp_uid, &vship);
198 if (vshipown != 0 && vshipown != player->cnum)
199 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
200 xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
203 if (sub_mcp->m_flags & M_SUB)
204 anti_torp(sub.shp_uid, ntorping, vshipown);
210 anti_torp(int f, int ntorping, int vshipown)
218 if (sub.shp_own == vshipown)
222 while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
225 if (dd.shp_own != vshipown)
228 if (!fire_torp(&dd, &sub, ntorping))
229 fire_dchrg(&dd, &sub, ntorping);
234 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
236 int range, erange, dam;
238 erange = roundrange(shp_fire_range(sp));
239 range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
243 if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
244 /* Return fire to a torpedo boat */
245 if (mchr[sp->shp_type].m_flags & M_SUB)
246 return; /* sub deck gun can't return fire */
248 putship(sp->shp_uid, sp);
255 pr("Kaboom!!! Incoming shells!\n");
256 if (sp->shp_own != 0)
258 "%s fired at %s\n", prship(sp), prship(targ));
260 pr("BLAM! %d damage!\n", dam);
262 /* Return fire to a submarine */
264 putship(sp->shp_uid, sp);
270 pr("\nCAPTAIN! !!Depth charges!!...\n");
271 if (sp->shp_own != 0)
273 "%s depth charged %s\n", prship(sp), prsub(targ));
274 pr("click...WHAM! %d damage!\n", dam);
276 shipdamage(targ, dam);
277 putship(targ->shp_uid, targ);
281 fire_torp(struct shpstr *sp, struct shpstr *targ, int ntargets)
283 int range, erange, dam;
285 if ((mchr[targ->shp_type].m_flags & M_SUB)
286 && (mchr[sp->shp_type].m_flags & M_SUBT) == 0)
287 return 0; /* need sub-torp to torpedo a sub */
289 erange = roundrange(torprange(sp));
290 range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
294 if (!line_of_sight(NULL, sp->shp_x, sp->shp_y,
295 targ->shp_x, targ->shp_y))
297 dam = shp_torp(sp, 1);
298 putship(sp->shp_uid, sp);
302 pr("Captain! Torpedoes sighted!\n");
304 if (chance(shp_torp_hitchance(sp, range))) {
306 if (!(mchr[targ->shp_type].m_flags & M_SUB)) {
307 if (mchr[sp->shp_type].m_flags & M_SUB)
308 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
310 nreport(targ->shp_own, N_SHIP_TORP, sp->shp_own, 1);
312 if (sp->shp_own != 0)
313 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
315 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
319 shipdamage(targ, dam);
320 putship(targ->shp_uid, targ);
324 if (sp->shp_own != 0)
326 "%s missed %s with a torpedo at %s\n",
327 prship(sp), prsub(targ),
328 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
335 prsub(struct shpstr *sp)
337 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
338 return prbuf("sub #%d", sp->shp_uid);