2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
53 static void anti_torp(int f, int ntorping, int vshipown);
54 static int candchrg(struct shpstr *, struct shpstr *);
55 static int canshoot(struct shpstr *, struct shpstr *);
56 static int cantorp(struct shpstr *, struct shpstr *);
57 static void fire_dchrg(struct shpstr *, struct shpstr *, int);
58 static int fire_torp(struct shpstr *, struct shpstr *, int, int);
76 struct nstr_item nbst;
82 if (!(sav = getstarg(player->argp[1], "From ship(s)? ", buf)))
84 if (!snxtitem(&nbst, EF_SHIP, sav))
86 while (nxtitem(&nbst, &sub)) {
87 if (sub.shp_own != player->cnum)
89 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
91 shells = sub.shp_item[I_SHELL];
92 if (shells < SHP_TORP_SHELLS)
93 shells += supply_commod(sub.shp_own, sub.shp_x, sub.shp_y,
94 I_SHELL, SHP_TORP_SHELLS - shells);
95 if (sub.shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
97 if (sub.shp_item[I_MILIT] < 1)
99 if (sub.shp_effic < 60)
101 if (sub.shp_mobil <= 0)
105 pr("%d ships are eligible to torp\n", ntorping);
106 snxtitem(&nbst, EF_SHIP, sav);
107 while (nxtitem(&nbst, &sub)) {
110 if (sub.shp_own != player->cnum) {
113 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0) {
114 pr("Ship # %d: A %s can't fire torpedoes!\n", sub.shp_uid,
115 mchr[(int)sub.shp_type].m_name);
118 shells = sub.shp_item[I_SHELL];
119 if (shells < SHP_TORP_SHELLS)
120 shells += supply_commod(sub.shp_own, sub.shp_x, sub.shp_y,
121 I_SHELL, SHP_TORP_SHELLS - shells);
122 if (sub.shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS) {
123 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
126 if (sub.shp_item[I_MILIT] < 1) {
127 pr("Ship #%d has insufficient crew\n", sub.shp_uid);
130 if (sub.shp_effic < 60) {
131 pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
134 if (sub.shp_mobil <= 0) {
135 pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
139 sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
140 if ((ptr = getstarg(player->argp[2], prompt, buf)) == 0)
142 if (!check_ship_ok(&sub))
144 if ((victno = atoi(ptr)) < 0)
146 if (!getship(victno, &vship))
150 vshipown = vship.shp_own;
151 if (victno == subno) {
152 pr("Shooting yourself, eh? How strange...\n");
155 if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
156 if (!(mchr[(int)sub.shp_type].m_flags & M_SUBT)) {
157 pr("You can't torpedo a submarine!\n");
161 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
162 anti_torp(sub.shp_uid, ntorping, vshipown);
163 getship(sub.shp_uid, &sub);
164 if (sub.shp_own == 0) {
167 erange = roundrange(torprange(&sub));
168 pr("Effective torpedo range is %d.0\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(NULL, 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)
245 if (sub.shp_own == vshipown)
248 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
249 pr("Starting our attack run...\n");
252 while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
255 if (dd.shp_own != vshipown)
257 if (dd.shp_effic < 60)
260 if (!canshoot(&dd, &sub))
263 erange = roundrange(effrange(dd.shp_frnge, dd.shp_tech));
264 range = mapdist(sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y);
268 if (!line_of_sight(NULL, sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y))
271 if (cantorp(&dd, &sub)) {
272 /* Try torping.. if we can, maybe we can fire */
273 if (!fire_torp(&dd, &sub, range, ntorping))
274 if (candchrg(&dd, &sub))
275 fire_dchrg(&dd, &sub, ntorping);
277 fire_dchrg(&dd, &sub, ntorping);
281 /* Can ship A shoot at ship B? */
283 canshoot(struct shpstr *a, struct shpstr *b)
285 /* Anyone can shoot a normal ship */
286 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
289 /* You can depth-charge a sub */
290 if (mchr[(int)a->shp_type].m_flags & M_DCH)
293 /* If you have SUBT flag, you can torp a sub */
294 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
300 /* Can ship A torp ship B? */
302 cantorp(struct shpstr *a, struct shpstr *b)
304 if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
307 /* Anyone with TORP flag can torp a normal ship */
308 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
311 /* Ship b is a sub, so we need to have the SUBT flag */
312 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
318 /* Can ship A depth-charge (or fire guns at) ship B? */
320 candchrg(struct shpstr *a, struct shpstr *b)
322 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0) {
323 if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
329 if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
336 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
343 shells = sp->shp_item[I_SHELL];
344 gun = sp->shp_item[I_GUN];
345 gun = MIN(gun, sp->shp_glim);
346 gun = MIN(gun, sp->shp_item[I_MILIT] / 2);
348 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
349 I_SHELL, (gun + 1) / 2 - shells);
351 gun = MIN(gun, shells * 2);
355 /* ok, all set.. now, we shoot */
356 shells -= ldround(((double)gun) / 2.0, 1);
357 sp->shp_item[I_SHELL] = shells;
358 putship(sp->shp_uid, sp);
360 guneff = seagun(sp->shp_effic, gun);
365 if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
367 pr("Kaboom!!! Incoming shells!\n");
368 if (sp->shp_own != 0)
370 "%s fired at %s\n", prship(sp), prship(targ));
372 pr("BLAM! %d damage!\n", dam);
373 shipdamage(targ, dam);
374 putship(targ->shp_uid, targ);
376 pr("\nCAPTAIN! !!Depth charges!!...\n");
377 if (sp->shp_own != 0)
379 "%s depth charged %s\n", prship(sp), prsub(targ));
380 pr("click...WHAM! %d damage!\n", dam);
381 shipdamage(targ, dam);
382 putship(targ->shp_uid, targ);
387 fire_torp(struct shpstr *sp, struct shpstr *targ, int range, int ntargets)
395 shells = sp->shp_item[I_SHELL];
397 if (shells < SHP_TORP_SHELLS)
398 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
399 I_SHELL, SHP_TORP_SHELLS - shells);
401 if (sp->shp_item[I_GUN] == 0 || shells < SHP_TORP_SHELLS)
404 if (sp->shp_item[I_MILIT] < 1)
407 if (sp->shp_effic < 60)
410 if (sp->shp_mobil <= 0)
413 /* All set.. fire! */
414 shells -= SHP_TORP_SHELLS;
415 sp->shp_item[I_SHELL] = shells;
416 putship(sp->shp_uid, sp);
418 mcp = &mchr[(int)sp->shp_type];
419 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
420 mobcost = (480.0 / (mobcost + techfact(sp->shp_tech, mobcost)));
422 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
424 sp->shp_mobil -= mobcost;
426 hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
428 pr("Captain! Torpedoes sighted!\n");
430 if (chance(hitchance)) {
432 if (sp->shp_own != 0)
433 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
435 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
440 shipdamage(targ, dam);
441 putship(targ->shp_uid, targ);
443 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
444 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
446 nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
449 if (sp->shp_own != 0)
451 "%s missed %s with a torp at %s\n",
452 prship(sp), prsub(targ),
453 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
460 prsub(struct shpstr *sp)
462 if (mchr[(int)sp->shp_type].m_flags & M_SUB)