]> git.pond.sub.org Git - empserver/blob - src/lib/subs/fortdef.c
(effrange, torprange, fortrange): New.
[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 static int sb(natid, natid, struct sctstr *, coord, coord, int, int);
57
58 /*
59  * See if any nearby ships will open up on the attacker
60  * Return damage done to attacker, if any.
61  * Subtracts shells used for firing.  Responding ships
62  * require military, shells, and guns.
63  */
64 int
65 shipdef(natid att, natid own, coord x, coord y)
66 {
67     return sd(att, own, x, y, NOISY, 1, 1);
68 }
69
70 int
71 sd(natid att, natid own, coord x, coord y, int noisy, int defending,
72    int usesubs)
73 {
74     int nshot;
75     int range;
76     double eff;
77     struct shpstr ship;
78     struct nstr_item ni;
79     int shell;
80     int dam, rel, rel2;
81
82     if (own == 0)
83         return 0;
84     if (att == own)
85         return 0;
86     eff = 1.0;
87     snxtitem_dist(&ni, EF_SHIP, x, y, 8);
88     while (nxtitem(&ni, &ship) && eff > 0.30) {
89         if (ship.shp_own == att)
90             continue;
91         if (ship.shp_own == 0)
92             continue;
93
94         rel = getrel(getnatp(ship.shp_own), own);
95         rel2 = getrel(getnatp(ship.shp_own), att);
96         if ((ship.shp_own != own) && ((rel != ALLIED) || (rel2 != AT_WAR)))
97             continue;
98         if (ship.shp_effic < 60)
99             continue;
100         if ((mchr[(int)ship.shp_type].m_flags & M_SUB) && !usesubs)
101             continue;
102         range = roundrange(effrange(ship.shp_frnge, ship.shp_tech));
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),
141                        xyas(x, y, ship.shp_own), dam);
142             }
143         }
144     }
145     return (int)(100 - eff * 100);
146 }
147
148 int
149 dd(natid att, natid def_own, coord ax, coord ay, int noisy, int defending)
150 {
151     int dam, rel, rel2;
152     struct sctstr firing;
153     struct nstr_sect ns;
154
155     if (opt_NO_FORT_FIRE)       /* Forts can't fire! */
156         return 0;
157     if (def_own == 0)
158         return 0;
159     if (att == def_own)
160         return 0;
161     dam = 0;
162     snxtsct_dist(&ns, ax, ay, 8);
163     while (nxtsct(&ns, &firing) && dam < 80) {
164         if (firing.sct_own == att)
165             continue;
166         if (firing.sct_own == 0)
167             continue;
168         rel = getrel(getnatp(firing.sct_own), def_own);
169         rel2 = getrel(getnatp(firing.sct_own), att);
170         if (firing.sct_own != def_own && (rel != ALLIED || rel2 != AT_WAR))
171             continue;
172         /* XXX defdef damage is additive, but ship or land unit damage isn't */
173         dam += sb(att, def_own, &firing, ax, ay, noisy, defending);
174     }
175     return dam;
176 }
177
178 /* Shoot back
179  *
180  * See if the sector being fired at will defend itself.
181  */
182 static int
183 sb(natid att, natid def, struct sctstr *sp, coord tx, coord ty, int noisy,
184    int defending)
185 {
186     int damage;
187     natid own;
188     int shell;
189     int range;
190     int range2, gun;
191
192     own = sp->sct_own;
193     if (own == 0)
194         return 0;
195     if (att == own)
196         return 0;
197     range = roundrange(fortrange(sp));
198     range2 = mapdist(sp->sct_x, sp->sct_y, tx, ty);
199     if (range < range2)
200         return 0;
201     gun = sp->sct_item[I_GUN];
202     if (gun == 0)
203         return 0;
204     shell = sp->sct_item[I_SHELL];
205     if (shell <= 0)
206         shell += supply_commod(sp->sct_own, sp->sct_x, sp->sct_y,
207                                I_SHELL, 1);
208     if (shell <= 0)
209         return 0;
210     sp->sct_item[I_SHELL] = shell - 1;
211     putsect(sp);
212     damage = landgun((int)sp->sct_effic, gun);
213     if (sp->sct_own != def)
214         wu(0, sp->sct_own,
215            "%s fired on %s in %s in defense of %s, doing %d damage!\n",
216            xyas(sp->sct_x, sp->sct_y, sp->sct_own), cname(att),
217            xyas(tx, ty, sp->sct_own), cname(def), damage);
218     if (defending)
219         nreport(sp->sct_own, N_FIRE_BACK, att, 1);
220     else
221         nreport(sp->sct_own, N_FIRE_F_ATTACK, att, 1);
222     if (noisy)
223         pr("Incoming shell! %d damage done.\007\n", damage);
224     return damage;
225 }