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, int ntargets);
54 s_char *prsub(struct shpstr *sp);
59 extern int torpedo_damage;
73 struct nstr_item nbst;
79 if (!(sav = getstarg(player->argp[1], "From ship(s)? ", buf)))
81 if (!snxtitem(&nbst, EF_SHIP, sav))
83 while (nxtitem(&nbst, (s_char *)&sub)){
84 if (sub.shp_own != player->cnum)
86 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0)
88 shells = getvar(V_SHELL, (s_char *)&sub, EF_SHIP);
90 shells += supply_commod(sub.shp_own,sub.shp_x,sub.shp_y,I_SHELL,
92 if (getvar(V_GUN, (s_char *)&sub, EF_SHIP) == 0 || shells < 3)
94 if (getvar(V_MILIT, (s_char *)&sub, EF_SHIP) < 1)
96 if (sub.shp_effic < 60)
98 if (sub.shp_mobil <= 0)
102 pr("%d ships are eligible to torp\n",ntorping);
103 snxtitem(&nbst, EF_SHIP, sav);
104 while (nxtitem(&nbst, (s_char *)&sub)) {
107 if (sub.shp_own != player->cnum) {
110 if ((mchr[(int)sub.shp_type].m_flags & M_TORP) == 0) {
111 pr("Ship # %d: A %s can't fire torpedoes!\n", sub.shp_uid,
112 mchr[(int)sub.shp_type].m_name);
115 shells = getvar(V_SHELL, (s_char *)&sub, EF_SHIP);
117 shells += supply_commod(sub.shp_own,sub.shp_x,sub.shp_y
120 if (getvar(V_GUN, (s_char *)&sub, EF_SHIP) == 0 || shells < 3) {
121 pr("Ship #%d has insufficient armament\n",sub.shp_uid);
124 if (getvar(V_MILIT, (s_char *)&sub, EF_SHIP) < 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);
138 sprintf(prompt, "Ship %d, target? ",sub.shp_uid);
139 if ((ptr = getstarg(player->argp[2],prompt, buf)) == 0)
141 if (!check_ship_ok(&sub))
143 if ((victno = atoi(ptr)) < 0)
145 if (!getship(victno, &vship))
149 vshipown=vship.shp_own;
150 if (victno == subno) {
151 pr("Shooting yourself, eh? How strange...\n");
154 if (mchr[(int)vship.shp_type].m_flags & M_SUB) {
155 if (!(mchr[(int)sub.shp_type].m_flags & M_SUBT)){
156 pr("You can't torpedo a submarine!\n");
160 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
161 anti_torp(sub.shp_uid,ntorping,vshipown);
162 getship(sub.shp_uid,&sub);
163 if (sub.shp_own == 0){
166 erange = ((double)sub.shp_effic/100.0) *
167 techfact(sub.shp_tech, ((double)sub.shp_frnge));
168 erange = (double)roundrange(erange);
169 pr("Effective torpedo range is %.1f\n", erange);
171 putvar(V_SHELL, shells, (s_char *)&sub, EF_SHIP);
172 putship(sub.shp_uid,&sub);
173 mcp = &mchr[(int)sub.shp_type];
174 mobcost = sub.shp_effic * 0.01 * sub.shp_speed;
175 mobcost = (480.0 / (mobcost + techfact(sub.shp_tech, mobcost)));
177 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
179 sub.shp_mobil -= mobcost;
181 getship(victno, &vship);
182 vshipown = vship.shp_own;
183 range = mapdist(sub.shp_x, sub.shp_y, vship.shp_x, vship.shp_y);
184 hitchance = DTORP_HITCHANCE(range, sub.shp_visib);
185 if (range <= erange) {
186 pr("Hitchance = %d%%\n",(int)(hitchance*100));
188 /* Now, can the torpedo even get there? */
189 if (!line_of_sight((s_char **)0, sub.shp_x, sub.shp_y,
190 vship.shp_x, vship.shp_y)) {
191 pr("BOOM!... Torpedo slams into land before reaching target.\n");
192 /* We only tell the victim if we were within range. */
193 if (range <= erange) {
195 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
196 xyas(sub.shp_x, sub.shp_y, vshipown),
199 } else if (range > erange) {
200 pr("Out of range\n");
201 } else if (hitchance >= 1.0 || chance(hitchance)) {
205 wu(0, vshipown, "%s in %s torpedoed %s for %d damage.\n", prsub(&sub),
206 xyas(sub.shp_x, sub.shp_y, vshipown),
207 prship(&vship), dam);
208 if (vship.shp_rflags & RET_TORPED){
209 retreat_ship(&vship, 't');
210 shipdamage(&vship, dam);
212 shipdamage(&vship, dam);
213 pr("Torpedo hit %s for %d damage.\n",
217 if (vship.shp_effic < SHIP_MINEFF)
220 putship(vship.shp_uid, &vship);
221 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
222 nreport(vshipown, N_TORP_SHIP, 0, 1);
224 nreport(vshipown, N_SHIP_TORP, player->cnum, 1);
228 wu(0, vshipown, "Torpedo sighted @ %s by %s\n",
229 xyas(sub.shp_x, sub.shp_y, vshipown),
233 putship(sub.shp_uid, &sub);
234 if (mchr[(int)sub.shp_type].m_flags & M_SUB)
235 anti_torp(sub.shp_uid,ntorping,vshipown);
241 anti_torp(int f, int ntorping, int vshipown)
251 if (sub.shp_own == vshipown)
254 if ((mchr[(int)sub.shp_type].m_flags & M_SUB) == 0)
255 pr("Starting our attack run...\n");
258 while (getship(x++,&dd) && sub.shp_effic >= SHIP_MINEFF){
261 if (dd.shp_own != vshipown)
263 if (dd.shp_effic < 60)
266 if (!canshoot(&dd,&sub))
269 erange = techfact(dd.shp_tech,
270 ((double)dd.shp_frnge))/ 2.0;
272 erange = (double)roundrange(erange);
274 range = mapdist(sub.shp_x, sub.shp_y, dd.shp_x, dd.shp_y);
279 if (!line_of_sight((s_char **)0, sub.shp_x, sub.shp_y,
283 if (cantorp(&dd,&sub)){
284 /* Try torping.. if we can, maybe we can fire */
285 if (!fire_torp(&dd,&sub,range,ntorping))
286 if (candchrg(&dd,&sub))
287 fire_dchrg(&dd,&sub,range,ntorping);
289 fire_dchrg(&dd,&sub,range,ntorping);
293 /* Can ship A shoot at ship B? */
295 canshoot(struct shpstr *a, struct shpstr *b)
297 /* Anyone can shoot a normal ship */
298 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
301 /* You can depth-charge a sub */
302 if (mchr[(int)a->shp_type].m_flags & M_DCH)
305 /* If you have SUBT flag, you can torp a sub */
306 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
312 /* Can ship A torp ship B? */
314 cantorp(struct shpstr *a, struct shpstr *b)
316 if ((mchr[(int)a->shp_type].m_flags & M_TORP) == 0)
319 /* Anyone with TORP flag can torp a normal ship */
320 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0)
323 /* Ship b is a sub, so we need to have the SUBT flag */
324 if (mchr[(int)a->shp_type].m_flags & M_SUBT)
330 /* Can ship A depth-charge (or fire guns at) ship B? */
332 candchrg(struct shpstr *a, struct shpstr *b)
334 if ((mchr[(int)b->shp_type].m_flags & M_SUB) == 0){
335 if ((mchr[(int)a->shp_type].m_flags & M_SUB) == 0)
341 if ((mchr[(int)a->shp_type].m_flags & M_DCH) == 0)
348 fire_dchrg(struct shpstr *sp, struct shpstr *targ, int range, int ntargets)
355 shells = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
356 gun = getvar(V_GUN, (s_char *)sp, EF_SHIP);
357 gun = min(gun, sp->shp_glim);
358 gun = min(gun, getvar(V_MILIT, (s_char *)sp, EF_SHIP)/2);
360 shells+=supply_commod(sp->shp_own,sp->shp_x,sp->shp_y,I_SHELL,(gun+1)/2-shells);
362 gun = min(gun, shells*2);
363 if (gun == 0) return;
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");
383 dam /= ((float)ntargets/2.0);
385 pr("BLAM! %d damage!\n", dam);
386 shipdamage(targ, dam);
387 putship(targ->shp_uid,targ);
389 pr("\nCAPTAIN! !!Depth charges!!...\n");
392 "%s depth charged %s\n",
393 prship(sp), prsub(targ));
396 dam /= ((float)ntargets/2.0);
398 pr("click...WHAM! %d damage!\n", dam);
399 shipdamage(targ, dam);
400 putship(targ->shp_uid,targ);
405 fire_torp(struct shpstr *sp, struct shpstr *targ, int range, int ntargets)
407 extern int torpedo_damage;
414 shells = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
417 shells += supply_commod(sp->shp_own,sp->shp_x,sp->shp_y,I_SHELL,
420 if (getvar(V_GUN, (s_char *)sp, EF_SHIP) == 0 || shells < 3)
423 if (getvar(V_MILIT, (s_char *)sp, EF_SHIP) < 1)
426 if (sp->shp_effic < 60)
429 if (sp->shp_mobil <= 0)
432 /* All set.. fire! */
434 putvar(V_SHELL, shells, (s_char *)sp, EF_SHIP);
435 putship(sp->shp_uid,sp);
437 mcp = &mchr[(int)sp->shp_type];
438 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
439 mobcost = (480.0 / (mobcost + techfact(sp->shp_tech, mobcost)));
441 /* Mob cost for a torp is equal to the cost of 1/2 sector of movement */
443 sp->shp_mobil -= mobcost;
445 hitchance = DTORP_HITCHANCE(range, sp->shp_visib);
447 pr("Captain! Torpedoes sighted!\n");
449 if (chance(hitchance)) {
452 wu(0, sp->shp_own, "%s @ %s torpedoed %s\n",
454 xyas(sp->shp_x, sp->shp_y, sp->shp_own),
459 dam /= ((float)ntargets/2.0);
461 shipdamage(targ, dam);
462 putship(targ->shp_uid, targ);
464 if (mchr[(int)sp->shp_type].m_flags & M_SUB)
465 nreport(targ->shp_own, N_TORP_SHIP, 0, 1);
467 nreport(targ->shp_own, N_SHIP_TORP, player->cnum, 1);
472 "%s missed %s with a torp at %s\n",
473 prship(sp), prsub(targ),
474 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
481 prsub(struct shpstr *sp)
483 if (mchr[(int)sp->shp_type].m_flags & M_SUB)