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