2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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-2010
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);
62 struct nstr_item nbst;
67 if (!snxtitem(&nbst, EF_SHIP, player->argp[1], "From ship(s)? "))
69 while (nxtitem(&nbst, &sub)) {
70 if (sub.shp_own != player->cnum)
72 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
74 if (sub.shp_item[I_GUN] == 0
75 || sub.shp_item[I_SHELL] < SHP_TORP_SHELLS)
77 if (sub.shp_item[I_MILIT] < 1)
79 if (sub.shp_effic < 60)
81 if (sub.shp_mobil <= 0)
85 pr("%d ships are eligible to torp\n", ntorping);
86 snxtitem_rewind(&nbst);
87 while (nxtitem(&nbst, &sub)) {
90 if (sub.shp_own != player->cnum) {
93 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0) {
94 pr("Ship # %d: A %s can't fire torpedoes!\n",
95 sub.shp_uid, mchr[(int)sub.shp_type].m_name);
98 if (sub.shp_item[I_GUN] == 0
99 || sub.shp_item[I_SHELL] < SHP_TORP_SHELLS) {
100 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
103 if (sub.shp_item[I_MILIT] < 1) {
104 pr("Ship #%d has insufficient crew\n", sub.shp_uid);
107 if (sub.shp_effic < 60) {
108 pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
111 if (sub.shp_mobil <= 0) {
112 pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
116 sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
117 if (!(ptr = getstarg(player->argp[2], prompt, buf)))
119 if (!check_ship_ok(&sub))
121 if ((victno = atoi(ptr)) < 0)
123 if (!getship(victno, &vship))
127 vshipown = vship.shp_own;
128 if (victno == subno) {
129 pr("Shooting yourself, eh? How strange...\n");
132 if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
133 if (!(mchr[(int)sub.shp_type].m_flags & M_SUBT)) {
134 pr("You can't torpedo a submarine!\n");
138 dam = shp_torp(&sub, 1);
140 putship(sub.shp_uid, &sub);
141 if (CANT_HAPPEN(dam < 0)) {
142 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
146 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
147 anti_torp(sub.shp_uid, ntorping, vshipown);
148 getship(sub.shp_uid, &sub);
149 if (sub.shp_own == 0)
152 erange = roundrange(torprange(&sub));
153 pr("Effective torpedo range is %d.0\n", erange);
155 getship(victno, &vship);
156 vshipown = vship.shp_own;
157 range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
158 hitchance = shp_torp_hitchance(&sub, range);
159 if (range <= erange) {
160 pr("Hitchance = %d%%\n", (int)(hitchance * 100));
162 /* Now, can the torpedo even get there? */
163 if (!line_of_sight(NULL, sub.shp_x, sub.shp_y,
164 vship.shp_x, vship.shp_y)) {
165 pr("BOOM!... Torpedo slams into land before reaching target.\n");
166 /* We only tell the victim if we were within range. */
167 if (range <= erange) {
169 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
170 xyas(sub.shp_x, sub.shp_y, vshipown),
173 } else if (range > erange) {
174 pr("Out of range\n");
175 } else if (chance(hitchance)) {
178 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n",
179 prsub(&sub), xyas(sub.shp_x, sub.shp_y, vshipown),
180 prship(&vship), dam);
181 if (vship.shp_rflags & RET_TORPED) {
182 retreat_ship(&vship, 't');
183 shipdamage(&vship, dam);
185 shipdamage(&vship, dam);
186 pr("Torpedo hit %s for %d damage.\n", prship(&vship), dam);
188 if (vship.shp_effic < SHIP_MINEFF)
189 pr("%s sunk!\n", prship(&vship));
190 putship(vship.shp_uid, &vship);
191 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
192 nreport(vshipown, N_TORP_SHIP, 0, 1);
194 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
198 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
199 xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
202 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
203 anti_torp(sub.shp_uid, ntorping, vshipown);
209 anti_torp(int f, int ntorping, int vshipown)
217 if (sub.shp_own == vshipown)
220 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
221 pr("Starting our attack run...\n");
224 while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
227 if (dd.shp_own != vshipown)
230 if (!fire_torp(&dd, &sub, ntorping))
231 fire_dchrg(&dd, &sub, ntorping);
236 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
238 int range, erange, dam;
240 erange = roundrange(shp_fire_range(sp));
241 range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
245 if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
246 /* Return fire to a torpedo boat */
247 if (mchr[sp->shp_type].m_flags & M_SUB)
248 return; /* sub deck gun can't return fire */
250 putship(sp->shp_uid, sp);
257 pr("Kaboom!!! Incoming shells!\n");
258 if (sp->shp_own != 0)
260 "%s fired at %s\n", prship(sp), prship(targ));
262 pr("BLAM! %d damage!\n", dam);
264 /* Return fire to a submarine */
266 putship(sp->shp_uid, sp);
272 pr("\nCAPTAIN! !!Depth charges!!...\n");
273 if (sp->shp_own != 0)
275 "%s depth charged %s\n", prship(sp), prsub(targ));
276 pr("click...WHAM! %d damage!\n", dam);
278 shipdamage(targ, dam);
279 putship(targ->shp_uid, targ);
283 fire_torp(struct shpstr *sp, struct shpstr *targ, int ntargets)
285 int range, erange, dam;
287 if ((mchr[targ->shp_type].m_flags & M_SUB)
288 && (mchr[sp->shp_type].m_flags & M_SUBT) == 0)
289 return 0; /* need sub-torp to torpedo a sub */
291 erange = roundrange(torprange(sp));
292 range = mapdist(sp->shp_x, sp->shp_y, targ->shp_x, targ->shp_y);
296 if (!line_of_sight(NULL, sp->shp_x, sp->shp_y,
297 targ->shp_x, targ->shp_y))
299 dam = shp_torp(sp, 1);
300 putship(sp->shp_uid, sp);
304 pr("Captain! Torpedoes sighted!\n");
306 if (chance(shp_torp_hitchance(sp, range))) {
308 if (sp->shp_own != 0)
309 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
311 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
315 shipdamage(targ, dam);
316 putship(targ->shp_uid, targ);
318 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
319 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
321 nreport(targ->shp_own, N_SHIP_TORP, sp->shp_own, 1);
324 if (sp->shp_own != 0)
326 "%s missed %s with a torp 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)