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
51 void anti_torp(int f, int ntorping, int vshipown);
52 void fire_dchrg(struct shpstr *sp, struct shpstr *targ, int range,
55 s_char *prsub(struct shpstr *sp);
60 extern int torpedo_damage;
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, (s_char *)&sub)) {
85 if (sub.shp_own != player->cnum)
87 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
89 shells = getvar(V_SHELL, (s_char *)&sub, EF_SHIP);
92 supply_commod(sub.shp_own, sub.shp_x, sub.shp_y, I_SHELL,
94 if (getvar(V_GUN, (s_char *)&sub, EF_SHIP) == 0 || shells < 3)
96 if (getvar(V_MILIT, (s_char *)&sub, EF_SHIP) < 1)
98 if (sub.shp_effic < 60)
100 if (sub.shp_mobil <= 0)
104 pr("%d ships are eligible to torp\n", ntorping);
105 snxtitem(&nbst, EF_SHIP, sav);
106 while (nxtitem(&nbst, (s_char *)&sub)) {
109 if (sub.shp_own != player->cnum) {
112 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0) {
113 pr("Ship # %d: A %s can't fire torpedoes!\n", sub.shp_uid,
114 mchr[(int)sub.shp_type].m_name);
117 shells = getvar(V_SHELL, (s_char *)&sub, EF_SHIP);
120 supply_commod(sub.shp_own, sub.shp_x, sub.shp_y, I_SHELL,
122 if (getvar(V_GUN, (s_char *)&sub, EF_SHIP) == 0 || shells < 3) {
123 pr("Ship #%d has insufficient armament\n", sub.shp_uid);
126 if (getvar(V_MILIT, (s_char *)&sub, EF_SHIP) < 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);
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 putvar(V_SHELL, shells, (s_char *)&sub, EF_SHIP);
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, range, ntorping);
286 fire_dchrg(&dd, &sub, range, 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 range, int ntargets)
352 shells = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
353 gun = getvar(V_GUN, (s_char *)sp, EF_SHIP);
354 gun = min(gun, sp->shp_glim);
355 gun = min(gun, getvar(V_MILIT, (s_char *)sp, EF_SHIP) / 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 putvar(V_SHELL, shells, (s_char *)sp, EF_SHIP);
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)
404 extern int torpedo_damage;
411 shells = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
414 shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y, I_SHELL,
417 if (getvar(V_GUN, (s_char *)sp, EF_SHIP) == 0 || shells < 3)
420 if (getvar(V_MILIT, (s_char *)sp, EF_SHIP) < 1)
423 if (sp->shp_effic < 60)
426 if (sp->shp_mobil <= 0)
429 /* All set.. fire! */
431 putvar(V_SHELL, shells, (s_char *)sp, EF_SHIP);
432 putship(sp->shp_uid, sp);
434 mcp = &mchr[(int)sp->shp_type];
435 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
436 mobcost = (480.0 / (mobcost + techfact(sp->shp_tech, mobcost)));
438 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
440 sp->shp_mobil -= mobcost;
442 hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
444 pr("Captain! Torpedoes sighted!\n");
446 if (chance(hitchance)) {
448 if (sp->shp_own != 0)
449 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
451 xyas(sp->shp_x, sp->shp_y, sp->shp_own), prsub(targ));
455 dam /= ((float)ntargets / 2.0);
457 shipdamage(targ, dam);
458 putship(targ->shp_uid, targ);
460 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
461 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
463 nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
466 if (sp->shp_own != 0)
468 "%s missed %s with a torp at %s\n",
469 prship(sp), prsub(targ),
470 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
477 prsub(struct shpstr *sp)
479 if (mchr[(int)sp->shp_type].m_flags & M_SUB)