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 * fortdef.c: Fort defends an area.
30 * Known contributors to this file:
34 * The base routines can also be used for general purposes.
35 * Noisy tells whther to send teles, print things, etc.
36 * Defending tells whether they are being defensive, or offensive.
51 #include "prototypes.h"
56 extern double tfactfire(natid, double);
59 * See if any nearby ships will open up on the attacker
60 * Return damage done to attacker, if any.
61 * Subtracts shells used for firing. Responding ships
62 * require military, shells, and guns.
65 shipdef(natid att, natid own, coord x, coord y)
67 return sd(att, own, x, y, NOISY, 1, 1);
71 sd(natid att, natid own, coord x, coord y, int noisy, int defending,
87 snxtitem_dist(&ni, EF_SHIP, x, y, 8);
88 while (nxtitem(&ni, (caddr_t)&ship) && eff > 0.30) {
89 if (ship.shp_own == att)
91 if (ship.shp_own == 0)
94 rel = getrel(getnatp(ship.shp_own), own);
95 rel2 = getrel(getnatp(ship.shp_own), att);
96 if ((ship.shp_own != own) && ((rel != ALLIED) || (rel2 != AT_WAR)))
98 if (ship.shp_effic < 60)
100 if ((mchr[(int)ship.shp_type].m_flags & M_SUB) && (!usesubs))
102 range = techfact(ship.shp_tech,
103 ship.shp_frnge * ship.shp_effic / 200.0);
104 range = (double)roundrange(range);
105 if (range < ni.curdist)
107 /* must have gun, shell, and milit to fire */
108 if (getvec(VT_ITEM, vec, (caddr_t)&ship, EF_SHIP) < 3)
110 if (vec[I_SHELL] < ship.shp_glim)
111 vec[I_SHELL] += supply_commod(ship.shp_own, ship.shp_x,
113 vec[I_SHELL] - ship.shp_glim);
114 nshot = min(min(vec[I_GUN], vec[I_SHELL]), vec[I_MILIT]);
115 nshot = min(nshot, ship.shp_glim);
118 (void)putvar(V_SHELL, vec[I_SHELL] - nshot, (caddr_t)&ship,
120 putship(ship.shp_uid, &ship);
122 nreport(ship.shp_own, N_FIRE_BACK, att, 1);
124 nreport(ship.shp_own, N_FIRE_S_ATTACK, att, 1);
125 dam = seagun(ship.shp_effic, nshot);
126 eff *= (1.0 - (0.01 * dam));
129 pr("Incoming shell%s %d damage!\n",
130 nshot == 1 ? " does" : "s do", dam);
132 if (noisy || (ship.shp_own != own)) {
133 if (ship.shp_own == own)
134 wu(0, own, "%s fired on %s at %s doing %d damage.\n",
135 prship(&ship), cname(att), xyas(x, y, own), dam);
139 "%s fired on %s at %s in defense of %s, doing %d damage.\n",
140 prship(&ship), cname(att), xyas(x, y, ship.shp_own),
144 "%s supported %s attacks against %s at %s, doing %d damage.\n",
145 prship(&ship), cname(own), cname(att), xyas(x, y,
152 return (int)100 - (eff * 100);
156 * Determine if any nearby gun-equipped sectors are within
157 * range and able to fire at an attacker. Firing sectors
158 * need to have guns, shells, and military. Sector being
159 * attacked is x,y -- attacker is at ax,ay.
162 /* defdef isn't called anywhere, and uses wrong
163 * number of arguments for dd */
165 defdef(att, def_own, defval, ax, ay)
172 return dd(att, def_own, defval, ax, ay, NOISY, 1);
177 dd(natid att, natid def_own, coord ax, coord ay, int noisy, int defending)
182 struct sctstr firing;
186 if (opt_NO_FORT_FIRE) /* Forts can't fire! */
192 tech = tfactfire(def_own, 1.0);
194 snxtsct_dist(&ns, ax, ay, 8);
195 while (nxtsct(&ns, &firing) && dam < 80) {
196 if (firing.sct_own == att)
198 if (firing.sct_own == 0)
200 if (firing.sct_effic < (u_char)FORTEFF)
202 rel = getrel(getnatp(firing.sct_own), def_own);
203 rel2 = getrel(getnatp(firing.sct_own), att);
204 if ((firing.sct_own != def_own) &&
205 ((rel != ALLIED) || (rel2 != AT_WAR)))
207 if (getvec(VT_ITEM, vec, (caddr_t)&firing, EF_SECTOR) < 0)
210 range = tfactfire(def_own, 7.0);
211 if (firing.sct_effic > 59)
213 /* Here we round down the range, and then add 1 to it
214 to determine if we could possibly hit the sector. If
215 we do, we call sb where the range is re-calculated and
216 the percentages are checked. */
217 range = (double)((int)(range) + 1);
218 if (range < ns.curdist)
220 /* XXX defdef damage is additive, but ship or land unit damage isn't */
221 dam += sb(att, def_own, &firing, ax, ay, noisy, defending);
228 * See if the sector being fired at will defend itself.
231 sb(natid att, natid def, struct sctstr *sp, coord tx, coord ty, int noisy,
240 if (sp->sct_type != SCT_FORTR) {
241 /* XXX I don't like this restriction */
245 if (sp->sct_effic < (u_char)FORTEFF)
253 range = tfactfire(own, 7.0);
254 if (sp->sct_effic > 59)
256 range = (double)roundrange(range);
257 range2 = mapdist((int)sp->sct_x, (int)sp->sct_y, tx, ty);
260 gun = getvar(V_GUN, (caddr_t)sp, EF_SECTOR);
263 shell = getvar(V_SHELL, (caddr_t)sp, EF_SECTOR);
265 shell += supply_commod(sp->sct_own, sp->sct_x, sp->sct_y, I_SHELL,
269 putvar(V_SHELL, shell - 1, (caddr_t)sp, EF_SECTOR);
271 damage = landgun((int)sp->sct_effic, gun);
272 if (sp->sct_own != def)
274 "%s fired on %s in %s in defense of %s, doing %d damage!\n",
275 xyas(sp->sct_x, sp->sct_y, sp->sct_own), cname(att), xyas(tx,
281 nreport(sp->sct_own, N_FIRE_BACK, att, 1);
283 nreport(sp->sct_own, N_FIRE_F_ATTACK, att, 1);
285 pr("Incoming shell! %d damage done.\007\n", damage);