2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * fortdef.c: Fort defends an area.
29 * Known contributors to this file:
30 * Markus Armbruster, 2006-2011
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.
44 #include "prototypes.h"
50 static int sb(natid, natid, struct sctstr *, coord, coord, int, int);
53 * See if any nearby ships will open up on the attacker
54 * Return damage done to attacker, if any.
55 * Subtracts shells used for firing. Responding ships
56 * require military, shells, and guns.
59 shipdef(natid att, natid own, coord x, coord y)
61 return sd(att, own, x, y, NOISY, 1, 1);
65 sd(natid att, natid own, coord x, coord y, int noisy, int defending,
79 snxtitem_dist(&ni, EF_SHIP, x, y, 8);
80 while (nxtitem(&ni, &ship) && eff > 0.30) {
81 if (!feels_like_helping(ship.shp_own, own, att))
84 if ((mchr[(int)ship.shp_type].m_flags & M_SUB) && !usesubs)
86 range = roundrange(shp_fire_range(&ship));
87 if (range < ni.curdist)
89 dam = shp_fire(&ship);
90 putship(ship.shp_uid, &ship);
94 nreport(ship.shp_own, N_FIRE_BACK, att, 1);
96 nreport(ship.shp_own, N_FIRE_S_ATTACK, att, 1);
97 eff *= (1.0 - (0.01 * dam));
100 pr("Incoming shells do %d damage!\n", dam);
102 if (noisy || (ship.shp_own != own)) {
103 if (ship.shp_own == own)
104 wu(0, own, "%s fired on %s at %s doing %d damage.\n",
105 prship(&ship), cname(att), xyas(x, y, own), dam);
109 "%s fired on %s at %s in defense of %s, doing %d damage.\n",
110 prship(&ship), cname(att), xyas(x, y, ship.shp_own),
114 "%s supported %s attacks against %s at %s, doing %d damage.\n",
115 prship(&ship), cname(own), cname(att),
116 xyas(x, y, ship.shp_own), dam);
120 return (int)(100 - eff * 100);
124 dd(natid att, natid def_own, coord ax, coord ay, int noisy, int defending)
127 struct sctstr firing;
130 if (opt_NO_FORT_FIRE) /* Forts can't fire! */
137 snxtsct_dist(&ns, ax, ay, 8);
138 while (nxtsct(&ns, &firing) && dam < 80) {
139 if (!feels_like_helping(firing.sct_own, def_own, att))
141 /* XXX defdef damage is additive, but ship or land unit damage isn't */
142 dam += sb(att, def_own, &firing, ax, ay, noisy, defending);
148 * Shall @cn attempt to help @friend against @foe?
151 feels_like_helping(natid cn, natid friend, natid foe)
154 return 0; /* never helps anybody */
156 return 0; /* don't help anybody against self */
158 return 1; /* help self against anybody else */
159 /* third party helps ally if at war with foe: */
160 return relations_with(cn, friend) == ALLIED
161 && relations_with(cn, foe) == AT_WAR;
166 * See if the sector being fired at will defend itself.
169 sb(natid att, natid def, struct sctstr *sp, coord tx, coord ty, int noisy,
181 range = roundrange(fortrange(sp));
182 range2 = mapdist(sp->sct_x, sp->sct_y, tx, ty);
185 damage = fort_fire(sp);
189 if (sp->sct_own != def)
191 "%s fired on %s in %s in defense of %s, doing %d damage!\n",
192 xyas(sp->sct_x, sp->sct_y, sp->sct_own), cname(att),
193 xyas(tx, ty, sp->sct_own), cname(def), damage);
195 nreport(sp->sct_own, N_FIRE_BACK, att, 1);
197 nreport(sp->sct_own, N_FIRE_F_ATTACK, att, 1);
199 pr("Incoming shell! %d damage done.\007\n", damage);