]> git.pond.sub.org Git - empserver/blob - src/lib/subs/fortdef.c
c67173a91c4e12c99a4ee45d01f1ddbeba710710
[empserver] / src / lib / subs / fortdef.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  fortdef.c: Fort defends an area.
29  *
30  *  Known contributors to this file:
31  *
32  */
33
34 /*
35  * The base routines can also be used for general purposes.
36  * Noisy tells whther to send teles, print things, etc.
37  * Defending tells whether they are being defensive, or offensive.
38  */
39
40 #include <config.h>
41
42 #include "file.h"
43 #include "nat.h"
44 #include "news.h"
45 #include "optlist.h"
46 #include "prototypes.h"
47 #include "sect.h"
48 #include "ship.h"
49
50 #define NOISY   1
51
52 static int sb(natid, natid, struct sctstr *, coord, coord, int, int);
53
54 /*
55  * See if any nearby ships will open up on the attacker
56  * Return damage done to attacker, if any.
57  * Subtracts shells used for firing.  Responding ships
58  * require military, shells, and guns.
59  */
60 int
61 shipdef(natid att, natid own, coord x, coord y)
62 {
63     return sd(att, own, x, y, NOISY, 1, 1);
64 }
65
66 int
67 sd(natid att, natid own, coord x, coord y, int noisy, int defending,
68    int usesubs)
69 {
70     int range;
71     double eff;
72     struct shpstr ship;
73     struct nstr_item ni;
74     int dam;
75
76     if (own == 0)
77         return 0;
78     if (att == own)
79         return 0;
80     eff = 1.0;
81     snxtitem_dist(&ni, EF_SHIP, x, y, 8);
82     while (nxtitem(&ni, &ship) && eff > 0.30) {
83         if (!feels_like_helping(ship.shp_own, own, att))
84             continue;
85
86         if ((mchr[(int)ship.shp_type].m_flags & M_SUB) && !usesubs)
87             continue;
88         range = roundrange(shp_fire_range(&ship));
89         if (range < ni.curdist)
90             continue;
91         dam = shp_fire(&ship);
92         putship(ship.shp_uid, &ship);
93         if (dam < 0)
94             continue;
95         if (defending)
96             nreport(ship.shp_own, N_FIRE_BACK, att, 1);
97         else
98             nreport(ship.shp_own, N_FIRE_S_ATTACK, att, 1);
99         eff *= (1.0 - (0.01 * dam));
100         if (noisy) {
101             pr_beep();
102             pr("Incoming shells do %d damage!\n", dam);
103         }
104         if (noisy || (ship.shp_own != own)) {
105             if (ship.shp_own == own)
106                 wu(0, own, "%s fired on %s at %s doing %d damage.\n",
107                    prship(&ship), cname(att), xyas(x, y, own), dam);
108             else {
109                 if (defending)
110                     wu(0, ship.shp_own,
111                        "%s fired on %s at %s in defense of %s, doing %d damage.\n",
112                        prship(&ship), cname(att), xyas(x, y, ship.shp_own),
113                        cname(own), dam);
114                 else
115                     wu(0, ship.shp_own,
116                        "%s supported %s attacks against %s at %s, doing %d damage.\n",
117                        prship(&ship), cname(own), cname(att),
118                        xyas(x, y, ship.shp_own), dam);
119             }
120         }
121     }
122     return (int)(100 - eff * 100);
123 }
124
125 int
126 dd(natid att, natid def_own, coord ax, coord ay, int noisy, int defending)
127 {
128     int dam;
129     struct sctstr firing;
130     struct nstr_sect ns;
131
132     if (opt_NO_FORT_FIRE)       /* Forts can't fire! */
133         return 0;
134     if (def_own == 0)
135         return 0;
136     if (att == def_own)
137         return 0;
138     dam = 0;
139     snxtsct_dist(&ns, ax, ay, 8);
140     while (nxtsct(&ns, &firing) && dam < 80) {
141         if (!feels_like_helping(firing.sct_own, def_own, att))
142             continue;
143         /* XXX defdef damage is additive, but ship or land unit damage isn't */
144         dam += sb(att, def_own, &firing, ax, ay, noisy, defending);
145     }
146     return dam;
147 }
148
149 /*
150  * Shall CN attempt to help FRIEND against FOE?
151  */
152 int
153 feels_like_helping(natid cn, natid friend, natid foe)
154 {
155     if (cn == 0)
156         return 0;               /* never helps anybody */
157     if (cn == foe)
158         return 0;               /* don't help anybody against self */
159     if (cn == friend)
160         return 1;               /* help self against anybody else */
161     /* third party helps ally if at war with foe: */
162     return getrel(getnatp(cn), friend) == ALLIED
163         && getrel(getnatp(cn), foe) == AT_WAR;
164 }
165
166 /* Shoot back
167  *
168  * See if the sector being fired at will defend itself.
169  */
170 static int
171 sb(natid att, natid def, struct sctstr *sp, coord tx, coord ty, int noisy,
172    int defending)
173 {
174     int damage;
175     natid own;
176     int range, range2;
177
178     own = sp->sct_own;
179     if (own == 0)
180         return 0;
181     if (att == own)
182         return 0;
183     range = roundrange(fortrange(sp));
184     range2 = mapdist(sp->sct_x, sp->sct_y, tx, ty);
185     if (range < range2)
186         return 0;
187     damage = fort_fire(sp);
188     putsect(sp);
189     if (damage < 0)
190         return 0;
191     if (sp->sct_own != def)
192         wu(0, sp->sct_own,
193            "%s fired on %s in %s in defense of %s, doing %d damage!\n",
194            xyas(sp->sct_x, sp->sct_y, sp->sct_own), cname(att),
195            xyas(tx, ty, sp->sct_own), cname(def), damage);
196     if (defending)
197         nreport(sp->sct_own, N_FIRE_BACK, att, 1);
198     else
199         nreport(sp->sct_own, N_FIRE_F_ATTACK, att, 1);
200     if (noisy)
201         pr("Incoming shell! %d damage done.\007\n", damage);
202     return damage;
203 }