2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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
52 static void anti_torp(int f, int ntorping, int vshipown);
53 static int candchrg(struct shpstr *, struct shpstr *);
54 static int canshoot(struct shpstr *, struct shpstr *);
55 static int cantorp(struct shpstr *, struct shpstr *);
56 static void fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets);
57 static int fire_torp(struct shpstr *, struct shpstr *, int, int);
75 struct nstr_item nbst;
81 if (!(sav = getstarg(player->argp[1], "From ship(s)? ", buf)))
83 if (!snxtitem(&nbst, EF_SHIP, sav))
85 while (nxtitem(&nbst, (s_char *)&sub)) {
86 if (sub.shp_own != player->cnum)
88 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
90 shells = sub.shp_item[I_SHELL];
93 supply_commod(sub.shp_own, sub.shp_x, sub.shp_y, I_SHELL,
95 if (sub.shp_item[I_GUN] == 0 || shells < 3)
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, (s_char *)&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];
121 supply_commod(sub.shp_own, sub.shp_x, sub.shp_y, I_SHELL,
123 if (sub.shp_item[I_GUN] == 0 || shells < 3) {
124 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
127 if (sub.shp_item[I_MILIT] < 1) {
128 pr("Ship #%d has insufficient crew\n", sub.shp_uid);
131 if (sub.shp_effic < 60) {
132 pr("Ship #%d torpedo tubes inoperative.\n", sub.shp_uid);
135 if (sub.shp_mobil <= 0) {
136 pr("Ship #%d has insufficient mobility\n", sub.shp_uid);
140 sprintf(prompt, "Ship %d, target? ", sub.shp_uid);
141 if ((ptr = getstarg(player->argp[2], prompt, buf)) == 0)
143 if (!check_ship_ok(&sub))
145 if ((victno = atoi(ptr)) < 0)
147 if (!getship(victno, &vship))
151 vshipown = vship.shp_own;
152 if (victno == subno) {
153 pr("Shooting yourself, eh? How strange...\n");
156 if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
157 if (!(mchr[(int)sub.shp_type].m_flags & M_SUBT)) {
158 pr("You can't torpedo a submarine!\n");
162 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
163 anti_torp(sub.shp_uid, ntorping, vshipown);
164 getship(sub.shp_uid, &sub);
165 if (sub.shp_own == 0) {
168 erange = ((double)sub.shp_effic / 100.0) *
169 techfact(sub.shp_tech, ((double)sub.shp_frnge));
170 erange = (double)roundrange(erange);
171 pr("Effective torpedo range is %.1f\n", erange);
173 sub.shp_item[I_SHELL] = shells;
174 putship(sub.shp_uid, &sub);
175 mcp = &mchr[(int)sub.shp_type];
176 mobcost = sub.shp_effic * 0.01 * sub.shp_speed;
177 mobcost = (480.0 / (mobcost + techfact(sub.shp_tech, mobcost)));
179 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
181 sub.shp_mobil -= mobcost;
183 getship(victno, &vship);
184 vshipown = vship.shp_own;
185 range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
186 hitchance = DTORP_HITCHANCE(range, sub.shp_visib);
187 if (range <= erange) {
188 pr("Hitchance = %d%%\n", (int)(hitchance * 100));
190 /* Now, can the torpedo even get there? */
191 if (!line_of_sight((s_char **)0, sub.shp_x, sub.shp_y,
192 vship.shp_x, vship.shp_y)) {
193 pr("BOOM!... Torpedo slams into land before reaching target.\n");
194 /* We only tell the victim if we were within range. */
195 if (range <= erange) {
197 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
198 xyas(sub.shp_x, sub.shp_y, vshipown),
201 } else if (range > erange) {
202 pr("Out of range\n");
203 } else if (hitchance >= 1.0 || chance(hitchance)) {
207 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n",
208 prsub(&sub), xyas(sub.shp_x, sub.shp_y, vshipown),
209 prship(&vship), dam);
210 if (vship.shp_rflags & RET_TORPED) {
211 retreat_ship(&vship, 't');
212 shipdamage(&vship, dam);
214 shipdamage(&vship, dam);
215 pr("Torpedo hit %s for %d damage.\n", prship(&vship), dam);
217 if (vship.shp_effic < SHIP_MINEFF)
218 pr("%s sunk!\n", prship(&vship));
219 putship(vship.shp_uid, &vship);
220 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
221 nreport(vshipown, N_TORP_SHIP, 0, 1);
223 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
227 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
228 xyas(sub.shp_x, sub.shp_y, vshipown), prship(&vship));
231 putship(sub.shp_uid, &sub);
232 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
233 anti_torp(sub.shp_uid, ntorping, vshipown);
239 anti_torp(int f, int ntorping, int vshipown)
249 if (sub.shp_own == vshipown)
252 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
253 pr("Starting our attack run...\n");
256 while (getship(x++, &dd) && sub.shp_effic >= SHIP_MINEFF) {
259 if (dd.shp_own != vshipown)
261 if (dd.shp_effic < 60)
264 if (!canshoot(&dd, &sub))
267 erange = techfact(dd.shp_tech, ((double)dd.shp_frnge)) / 2.0;
269 erange = (double)roundrange(erange);
271 range = mapdist(sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y);
276 if (!line_of_sight((s_char **)0, sub.shp_x, sub.shp_y,
280 if (cantorp(&dd, &sub)) {
281 /* Try torping.. if we can, maybe we can fire */
282 if (!fire_torp(&dd, &sub, range, ntorping))
283 if (candchrg(&dd, &sub))
284 fire_dchrg(&dd, &sub, ntorping);
286 fire_dchrg(&dd, &sub, ntorping);
290 /* Can ship A shoot at ship B? */
292 canshoot(struct shpstr *a, struct shpstr *b)
294 /* Anyone can shoot a normal ship */
295 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
298 /* You can depth-charge a sub */
299 if (mchr[(int)a->shp_type].m_flags & M_DCH)
302 /* If you have SUBT flag, you can torp a sub */
303 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
309 /* Can ship A torp ship B? */
311 cantorp(struct shpstr *a, struct shpstr *b)
313 if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
316 /* Anyone with TORP flag can torp a normal ship */
317 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
320 /* Ship b is a sub, so we need to have the SUBT flag */
321 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
327 /* Can ship A depth-charge (or fire guns at) ship B? */
329 candchrg(struct shpstr *a, struct shpstr *b)
331 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0) {
332 if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
338 if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
345 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int ntargets)
352 shells = sp->shp_item[I_SHELL];
353 gun = sp->shp_item[I_GUN];
354 gun = min(gun, sp->shp_glim);
355 gun = min(gun, sp->shp_item[I_MILIT] / 2);
358 supply_commod(sp->shp_own, sp->shp_x, sp->shp_y, I_SHELL,
359 (gun + 1) / 2 - shells);
361 gun = min(gun, shells * 2);
365 /* ok, all set.. now, we shoot */
366 shells -= ldround(((double)gun) / 2.0, 1);
367 sp->shp_item[I_SHELL] = shells;
368 putship(sp->shp_uid, sp);
370 guneff = seagun(sp->shp_effic, gun);
373 if ((mchr[(int)targ->shp_type].m_flags & M_SUB) == 0) {
375 pr("Kaboom!!! Incoming shells!\n");
376 if (sp->shp_own != 0)
378 "%s fired at %s\n", prship(sp), prship(targ));
381 dam /= ((float)ntargets / 2.0);
383 pr("BLAM! %d damage!\n", dam);
384 shipdamage(targ, dam);
385 putship(targ->shp_uid, targ);
387 pr("\nCAPTAIN! !!Depth charges!!...\n");
388 if (sp->shp_own != 0)
390 "%s depth charged %s\n", prship(sp), prsub(targ));
393 dam /= ((float)ntargets / 2.0);
395 pr("click...WHAM! %d damage!\n", dam);
396 shipdamage(targ, dam);
397 putship(targ->shp_uid, targ);
402 fire_torp(struct shpstr *sp, struct shpstr *targ, int range, int ntargets)
410 shells = sp->shp_item[I_SHELL];
413 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y, I_SHELL,
416 if (sp->shp_item[I_GUN] == 0 || shells < 3)
419 if (sp->shp_item[I_MILIT] < 1)
422 if (sp->shp_effic < 60)
425 if (sp->shp_mobil <= 0)
428 /* All set.. fire! */
430 sp->shp_item[I_SHELL] = shells;
431 putship(sp->shp_uid, sp);
433 mcp = &mchr[(int)sp->shp_type];
434 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
435 mobcost = (480.0 / (mobcost + techfact(sp->shp_tech, mobcost)));
437 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
439 sp->shp_mobil -= mobcost;
441 hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
443 pr("Captain! Torpedoes sighted!\n");
445 if (chance(hitchance)) {
447 if (sp->shp_own != 0)
448 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
450 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
454 dam /= ((float)ntargets / 2.0);
456 shipdamage(targ, dam);
457 putship(targ->shp_uid, targ);
459 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
460 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
462 nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
465 if (sp->shp_own != 0)
467 "%s missed %s with a torp at %s\n",
468 prship(sp), prsub(targ),
469 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
476 prsub(struct shpstr *sp)
478 if (mchr[(int)sp->shp_type].m_flags & M_SUB)