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