]> git.pond.sub.org Git - empserver/blob - src/lib/subs/fortdef.c
Update copyright notice.
[empserver] / src / lib / subs / fortdef.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 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.
25  *
26  *  ---
27  *
28  *  fortdef.c: Fort defends an area.
29  * 
30  *  Known contributors to this file:
31  *    
32  */
33 /*
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.
37  */
38
39 #include "misc.h"
40 #include "xy.h"
41 #include "nat.h"
42 #include "sect.h"
43 #include "ship.h"
44 #include "land.h"
45 #include "news.h"
46 #include "nsc.h"
47 #include "file.h"
48 #include "optlist.h"
49 #include "prototypes.h"
50
51 #define NOISY   1
52
53 /*
54  * See if any nearby ships will open up on the attacker
55  * Return damage done to attacker, if any.
56  * Subtracts shells used for firing.  Responding ships
57  * require military, shells, and guns.
58  */
59 int
60 shipdef(natid att, natid own, coord x, coord y)
61 {
62     return sd(att, own, x, y, NOISY, 1, 1);
63 }
64
65 int
66 sd(natid att, natid own, coord x, coord y, int noisy, int defending,
67    int usesubs)
68 {
69     int nshot;
70     double range;
71     double eff;
72     struct shpstr ship;
73     struct nstr_item ni;
74     int shell;
75     int dam, rel, rel2;
76
77     if (own == 0)
78         return 0;
79     if (att == own)
80         return 0;
81     eff = 1.0;
82     snxtitem_dist(&ni, EF_SHIP, x, y, 8);
83     while (nxtitem(&ni, &ship) && eff > 0.30) {
84         if (ship.shp_own == att)
85             continue;
86         if (ship.shp_own == 0)
87             continue;
88
89         rel = getrel(getnatp(ship.shp_own), own);
90         rel2 = getrel(getnatp(ship.shp_own), att);
91         if ((ship.shp_own != own) && ((rel != ALLIED) || (rel2 != AT_WAR)))
92             continue;
93         if (ship.shp_effic < 60)
94             continue;
95         if ((mchr[(int)ship.shp_type].m_flags & M_SUB) && (!usesubs))
96             continue;
97         range = techfact(ship.shp_tech,
98                          ship.shp_frnge * ship.shp_effic / 200.0);
99         range = (double)roundrange(range);
100         if (range < ni.curdist)
101             continue;
102         /* must have gun, shell, and milit to fire */
103         shell = ship.shp_item[I_SHELL];
104         if (shell < ship.shp_glim)
105             shell += supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
106                                    I_SHELL, shell - ship.shp_glim);
107         nshot = min(min(ship.shp_item[I_GUN], shell), ship.shp_item[I_MILIT]);
108         nshot = min(nshot, ship.shp_glim);
109         if (nshot <= 0)
110             continue;
111         ship.shp_item[I_SHELL] = shell - nshot;
112         putship(ship.shp_uid, &ship);
113         if (defending)
114             nreport(ship.shp_own, N_FIRE_BACK, att, 1);
115         else
116             nreport(ship.shp_own, N_FIRE_S_ATTACK, att, 1);
117         dam = seagun(ship.shp_effic, nshot);
118         eff *= (1.0 - (0.01 * dam));
119         if (noisy) {
120             pr_beep();
121             pr("Incoming shell%s %d damage!\n",
122                nshot == 1 ? " does" : "s do", dam);
123         }
124         if (noisy || (ship.shp_own != own)) {
125             if (ship.shp_own == own)
126                 wu(0, own, "%s fired on %s at %s doing %d damage.\n",
127                    prship(&ship), cname(att), xyas(x, y, own), dam);
128             else {
129                 if (defending)
130                     wu(0, ship.shp_own,
131                        "%s fired on %s at %s in defense of %s, doing %d damage.\n",
132                        prship(&ship), cname(att), xyas(x, y, ship.shp_own),
133                        cname(own), dam);
134                 else
135                     wu(0, ship.shp_own,
136                        "%s supported %s attacks against %s at %s, doing %d damage.\n",
137                        prship(&ship), cname(own), cname(att), xyas(x, y,
138                                                                    ship.
139                                                                    shp_own),
140                        dam);
141             }
142         }
143     }
144     return (int)100 - (eff * 100);
145 }
146
147 /*
148  * Determine if any nearby gun-equipped sectors are within
149  * range and able to fire at an attacker.  Firing sectors
150  * need to have guns, shells, and military.  Sector being
151  * attacked is x,y -- attacker is at ax,ay.
152  */
153 #if 0
154 /* defdef isn't called anywhere, and uses wrong
155  * number of arguments for dd */
156 int
157 defdef(att, def_own, defval, ax, ay)
158 natid att;
159 natid def_own;
160 int defval;
161 coord ax;
162 coord ay;
163 {
164     return dd(att, def_own, defval, ax, ay, NOISY, 1);
165 }
166 #endif
167
168 int
169 dd(natid att, natid def_own, coord ax, coord ay, int noisy, int defending)
170 {
171     int dam, rel, rel2;
172     double tech;
173     double range;
174     struct sctstr firing;
175     struct nstr_sect ns;
176
177     if (opt_NO_FORT_FIRE)       /* Forts can't fire! */
178         return 0;
179     if (def_own == 0)
180         return 0;
181     if (att == def_own)
182         return 0;
183     tech = tfactfire(def_own, 1.0);
184     dam = 0;
185     snxtsct_dist(&ns, ax, ay, 8);
186     while (nxtsct(&ns, &firing) && dam < 80) {
187         if (firing.sct_own == att)
188             continue;
189         if (firing.sct_own == 0)
190             continue;
191         if (firing.sct_effic < (u_char)FORTEFF)
192             continue;
193         rel = getrel(getnatp(firing.sct_own), def_own);
194         rel2 = getrel(getnatp(firing.sct_own), att);
195         if ((firing.sct_own != def_own) &&
196             ((rel != ALLIED) || (rel2 != AT_WAR)))
197             continue;
198
199         range = tfactfire(def_own, 7.0);
200         if (firing.sct_effic > 59)
201             range++;
202         /* Here we round down the range, and then add 1 to it
203            to determine if we could possibly hit the sector.  If
204            we do, we call sb where the range is re-calculated and
205            the percentages are checked. */
206         range = (double)((int)(range) + 1);
207         if (range < ns.curdist)
208             continue;
209         /* XXX defdef damage is additive, but ship or land unit damage isn't */
210         dam += sb(att, def_own, &firing, ax, ay, noisy, defending);
211     }
212     return dam;
213 }
214
215 /* Shoot back
216  *
217  * See if the sector being fired at will defend itself.
218  */
219 int
220 sb(natid att, natid def, struct sctstr *sp, coord tx, coord ty, int noisy,
221    int defending)
222 {
223     int damage;
224     natid own;
225     int shell;
226     double range;
227     int range2, gun;
228
229     if (sp->sct_type != SCT_FORTR) {
230         /* XXX I don't like this restriction */
231         return 0;
232     }
233
234     if (sp->sct_effic < (u_char)FORTEFF)
235         return 0;
236
237     own = sp->sct_own;
238     if (own == 0)
239         return 0;
240     if (att == own)
241         return 0;
242     range = tfactfire(own, 7.0);
243     if (sp->sct_effic > 59)
244         range++;
245     range = (double)roundrange(range);
246     range2 = mapdist((int)sp->sct_x, (int)sp->sct_y, tx, ty);
247     if (range < range2)
248         return 0;
249     gun = sp->sct_item[I_GUN];
250     if (gun == 0)
251         return 0;
252     shell = sp->sct_item[I_SHELL];
253     if (shell <= 0)
254         shell += supply_commod(sp->sct_own, sp->sct_x, sp->sct_y, I_SHELL,
255                                1);
256     if (shell <= 0)
257         return 0;
258     sp->sct_item[I_SHELL] = shell - 1;
259     putsect(sp);
260     damage = landgun((int)sp->sct_effic, gun);
261     if (sp->sct_own != def)
262         wu(0, sp->sct_own,
263            "%s fired on %s in %s in defense of %s, doing %d damage!\n",
264            xyas(sp->sct_x, sp->sct_y, sp->sct_own), cname(att), xyas(tx,
265                                                                      ty,
266                                                                      sp->
267                                                                      sct_own),
268            cname(def), damage);
269     if (defending)
270         nreport(sp->sct_own, N_FIRE_BACK, att, 1);
271     else
272         nreport(sp->sct_own, N_FIRE_F_ATTACK, att, 1);
273     if (noisy)
274         pr("Incoming shell! %d damage done.\007\n", damage);
275     return damage;
276 }