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