2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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
51 static void anti_torp(int f, int ntorping, int vshipown);
52 static int candchrg(struct shpstr *, struct shpstr *);
53 static int canshoot(struct shpstr *, struct shpstr *);
54 static int cantorp(struct shpstr *, struct shpstr *);
55 static void fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets);
56 static int fire_torp(struct shpstr *, struct shpstr *, int, int);
74 struct nstr_item nbst;
80 if (!(sav = getstarg(player->argp[1], "From ship(s)? ", buf)))
82 if (!snxtitem(&nbst, EF_SHIP, sav))
84 while (nxtitem(&nbst, &sub)) {
85 if (sub.shp_own != player->cnum)
87 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
89 shells = sub.shp_item[I_SHELL];
90 if (shells < SHP_TORP_SHELLS)
91 shells += supply_commod(sub.shp_own, sub.shp_x, sub.shp_y,
92 I_SHELL, SHP_TORP_SHELLS - shells);
93 if (sub.shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
95 if (sub.shp_item[I_MILIT] < 1)
97 if (sub.shp_effic < 60)
99 if (sub.shp_mobil <= 0)
103 pr("%d ships are eligible to torp\n", ntorping);
104 snxtitem(&nbst, EF_SHIP, sav);
105 while (nxtitem(&nbst, &sub)) {
108 if (sub.shp_own != player->cnum) {
111 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0) {
112 pr("Ship # %d: A %s can't fire torpedoes!\n", sub.shp_uid,
113 mchr[(int)sub.shp_type].m_name);
116 shells = sub.shp_item[I_SHELL];
117 if (shells < SHP_TORP_SHELLS)
118 shells += supply_commod(sub.shp_own, sub.shp_x, sub.shp_y,
119 I_SHELL, SHP_TORP_SHELLS - shells);
120 if (sub.shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS) {
121 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
124 if (sub.shp_item[I_MILIT] < 1) {
125 pr("Ship #%d has insufficient crew\n", sub.shp_uid);
128 if (sub.shp_effic < 60) {
129 pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
132 if (sub.shp_mobil <= 0) {
133 pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
137 sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
138 if ((ptr = getstarg(player->argp[2], prompt, buf)) == 0)
140 if (!check_ship_ok(&sub))
142 if ((victno = atoi(ptr)) < 0)
144 if (!getship(victno, &vship))
148 vshipown = vship.shp_own;
149 if (victno == subno) {
150 pr("Shooting yourself, eh? How strange...\n");
153 if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
154 if (!(mchr[(int)sub.shp_type].m_flags & M_SUBT)) {
155 pr("You can't torpedo a submarine!\n");
159 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
160 anti_torp(sub.shp_uid, ntorping, vshipown);
161 getship(sub.shp_uid, &sub);
162 if (sub.shp_own == 0) {
165 erange = ((double)sub.shp_effic / 100.0) *
166 techfact(sub.shp_tech, ((double)sub.shp_frnge));
167 erange = (double)roundrange(erange);
168 pr("Effective torpedo range is %.1f\n", erange);
169 shells -= SHP_TORP_SHELLS;
170 sub.shp_item[I_SHELL] = shells;
171 putship(sub.shp_uid, &sub);
172 mcp = &mchr[(int)sub.shp_type];
173 mobcost = sub.shp_effic * 0.01 * sub.shp_speed;
174 mobcost = (480.0 / (mobcost + techfact(sub.shp_tech, mobcost)));
176 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
178 sub.shp_mobil -= mobcost;
180 getship(victno, &vship);
181 vshipown = vship.shp_own;
182 range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
183 hitchance = DTORP_HITCHANCE(range, sub.shp_visib);
184 if (range <= erange) {
185 pr("Hitchance = %d%%\n", (int)(hitchance * 100));
187 /* Now, can the torpedo even get there? */
188 if (!line_of_sight((s_char **)0, sub.shp_x, sub.shp_y,
189 vship.shp_x, vship.shp_y)) {
190 pr("BOOM!... Torpedo slams into land before reaching target.\n");
191 /* We only tell the victim if we were within range. */
192 if (range <= erange) {
194 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
195 xyas(sub.shp_x, sub.shp_y, vshipown),
198 } else if (range > erange) {
199 pr("Out of range\n");
200 } else if (hitchance >= 1.0 || chance(hitchance)) {
204 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n",
205 prsub(&sub), xyas(sub.shp_x, sub.shp_y, vshipown),
206 prship(&vship), dam);
207 if (vship.shp_rflags & RET_TORPED) {
208 retreat_ship(&vship, 't');
209 shipdamage(&vship, dam);
211 shipdamage(&vship, dam);
212 pr("Torpedo hit %s for %d damage.\n", prship(&vship), dam);
214 if (vship.shp_effic < SHIP_MINEFF)
215 pr("%s sunk!\n", prship(&vship));
216 putship(vship.shp_uid, &vship);
217 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
218 nreport(vshipown, N_TORP_SHIP, 0, 1);
220 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
224 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
225 xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
228 putship(sub.shp_uid, &sub);
229 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
230 anti_torp(sub.shp_uid, ntorping, vshipown);
236 anti_torp(int f, int ntorping, int vshipown)
246 if (sub.shp_own == vshipown)
249 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
250 pr("Starting our attack run...\n");
253 while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
256 if (dd.shp_own != vshipown)
258 if (dd.shp_effic < 60)
261 if (!canshoot(&dd, &sub))
264 erange = techfact(dd.shp_tech, ((double)dd.shp_frnge)) / 2.0;
266 erange = (double)roundrange(erange);
268 range = mapdist(sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y);
273 if (!line_of_sight((s_char **)0, sub.shp_x, sub.shp_y,
277 if (cantorp(&dd, &sub)) {
278 /* Try torping.. if we can, maybe we can fire */
279 if (!fire_torp(&dd, &sub, range, ntorping))
280 if (candchrg(&dd, &sub))
281 fire_dchrg(&dd, &sub, ntorping);
283 fire_dchrg(&dd, &sub, ntorping);
287 /* Can ship A shoot at ship B? */
289 canshoot(struct shpstr *a, struct shpstr *b)
291 /* Anyone can shoot a normal ship */
292 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
295 /* You can depth-charge a sub */
296 if (mchr[(int)a->shp_type].m_flags & M_DCH)
299 /* If you have SUBT flag, you can torp a sub */
300 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
306 /* Can ship A torp ship B? */
308 cantorp(struct shpstr *a, struct shpstr *b)
310 if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
313 /* Anyone with TORP flag can torp a normal ship */
314 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
317 /* Ship b is a sub, so we need to have the SUBT flag */
318 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
324 /* Can ship A depth-charge (or fire guns at) ship B? */
326 candchrg(struct shpstr *a, struct shpstr *b)
328 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0) {
329 if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
335 if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
342 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
349 shells = sp->shp_item[I_SHELL];
350 gun = sp->shp_item[I_GUN];
351 gun = min(gun, sp->shp_glim);
352 gun = min(gun, sp->shp_item[I_MILIT] / 2);
355 supply_commod(sp->shp_own, sp->shp_x, sp->shp_y, I_SHELL,
356 (gun + 1) / 2 - shells);
358 gun = min(gun, shells * 2);
362 /* ok, all set.. now, we shoot */
363 shells -= ldround(((double)gun) / 2.0, 1);
364 sp->shp_item[I_SHELL] = shells;
365 putship(sp->shp_uid, sp);
367 guneff = seagun(sp->shp_effic, gun);
370 if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
372 pr("Kaboom!!! Incoming shells!\n");
373 if (sp->shp_own != 0)
375 "%s fired at %s\n", prship(sp), prship(targ));
378 dam /= ((float)ntargets / 2.0);
380 pr("BLAM! %d damage!\n", dam);
381 shipdamage(targ, dam);
382 putship(targ->shp_uid, targ);
384 pr("\nCAPTAIN! !!Depth charges!!...\n");
385 if (sp->shp_own != 0)
387 "%s depth charged %s\n", prship(sp), prsub(targ));
390 dam /= ((float)ntargets / 2.0);
392 pr("click...WHAM! %d damage!\n", dam);
393 shipdamage(targ, dam);
394 putship(targ->shp_uid, targ);
399 fire_torp(struct shpstr *sp, struct shpstr *targ, int range, int ntargets)
407 shells = sp->shp_item[I_SHELL];
409 if (shells < SHP_TORP_SHELLS)
410 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y, I_SHELL,
411 SHP_TORP_SHELLS - shells);
413 if (sp->shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
416 if (sp->shp_item[I_MILIT] < 1)
419 if (sp->shp_effic < 60)
422 if (sp->shp_mobil <= 0)
425 /* All set.. fire! */
426 shells -= SHP_TORP_SHELLS;
427 sp->shp_item[I_SHELL] = shells;
428 putship(sp->shp_uid, sp);
430 mcp = &mchr[(int)sp->shp_type];
431 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
432 mobcost = (480.0 / (mobcost + techfact(sp->shp_tech, mobcost)));
434 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
436 sp->shp_mobil -= mobcost;
438 hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
440 pr("Captain! Torpedoes sighted!\n");
442 if (chance(hitchance)) {
444 if (sp->shp_own != 0)
445 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
447 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
451 dam /= ((float)ntargets / 2.0);
453 shipdamage(targ, dam);
454 putship(targ->shp_uid, targ);
456 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
457 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
459 nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
462 if (sp->shp_own != 0)
464 "%s missed %s with a torp at %s\n",
465 prship(sp), prsub(targ),
466 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
473 prsub(struct shpstr *sp)
475 if (mchr[(int)sp->shp_type].m_flags & M_SUB)