]> 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-2008, 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 "file.h"
43 #include "nat.h"
44 #include "news.h"
45 #include "optlist.h"
46 #include "prototypes.h"
47 #include "sect.h"
48 #include "ship.h"
49
50 #define NOISY   1
51
52 static int sb(natid, natid, struct sctstr *, coord, coord, int, int);
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     int 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 = roundrange(effrange(ship.shp_frnge, ship.shp_tech));
99         if (range < ni.curdist)
100             continue;
101         /* must have gun, shell, and milit to fire */
102         shell = ship.shp_item[I_SHELL];
103         if (shell < ship.shp_glim)
104             shell += supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
105                                    I_SHELL, shell - ship.shp_glim);
106         nshot = MIN(MIN(ship.shp_item[I_GUN], shell), ship.shp_item[I_MILIT]);
107         nshot = MIN(nshot, ship.shp_glim);
108         if (nshot <= 0)
109             continue;
110         ship.shp_item[I_SHELL] = shell - nshot;
111         putship(ship.shp_uid, &ship);
112         if (defending)
113             nreport(ship.shp_own, N_FIRE_BACK, att, 1);
114         else
115             nreport(ship.shp_own, N_FIRE_S_ATTACK, att, 1);
116         dam = seagun(ship.shp_effic, nshot);
117         eff *= (1.0 - (0.01 * dam));
118         if (noisy) {
119             pr_beep();
120             pr("Incoming shell%s %d damage!\n",
121                nshot == 1 ? " does" : "s do", dam);
122         }
123         if (noisy || (ship.shp_own != own)) {
124             if (ship.shp_own == own)
125                 wu(0, own, "%s fired on %s at %s doing %d damage.\n",
126                    prship(&ship), cname(att), xyas(x, y, own), dam);
127             else {
128                 if (defending)
129                     wu(0, ship.shp_own,
130                        "%s fired on %s at %s in defense of %s, doing %d damage.\n",
131                        prship(&ship), cname(att), xyas(x, y, ship.shp_own),
132                        cname(own), dam);
133                 else
134                     wu(0, ship.shp_own,
135                        "%s supported %s attacks against %s at %s, doing %d damage.\n",
136                        prship(&ship), cname(own), cname(att),
137                        xyas(x, y, ship.shp_own), dam);
138             }
139         }
140     }
141     return (int)(100 - eff * 100);
142 }
143
144 int
145 dd(natid att, natid def_own, coord ax, coord ay, int noisy, int defending)
146 {
147     int dam, rel, rel2;
148     struct sctstr firing;
149     struct nstr_sect ns;
150
151     if (opt_NO_FORT_FIRE)       /* Forts can't fire! */
152         return 0;
153     if (def_own == 0)
154         return 0;
155     if (att == def_own)
156         return 0;
157     dam = 0;
158     snxtsct_dist(&ns, ax, ay, 8);
159     while (nxtsct(&ns, &firing) && dam < 80) {
160         if (firing.sct_own == att)
161             continue;
162         if (firing.sct_own == 0)
163             continue;
164         rel = getrel(getnatp(firing.sct_own), def_own);
165         rel2 = getrel(getnatp(firing.sct_own), att);
166         if (firing.sct_own != def_own && (rel != ALLIED || rel2 != AT_WAR))
167             continue;
168         /* XXX defdef damage is additive, but ship or land unit damage isn't */
169         dam += sb(att, def_own, &firing, ax, ay, noisy, defending);
170     }
171     return dam;
172 }
173
174 /* Shoot back
175  *
176  * See if the sector being fired at will defend itself.
177  */
178 static int
179 sb(natid att, natid def, struct sctstr *sp, coord tx, coord ty, int noisy,
180    int defending)
181 {
182     int damage;
183     natid own;
184     int shell;
185     int range;
186     int range2, gun;
187
188     own = sp->sct_own;
189     if (own == 0)
190         return 0;
191     if (att == own)
192         return 0;
193     range = roundrange(fortrange(sp));
194     range2 = mapdist(sp->sct_x, sp->sct_y, tx, ty);
195     if (range < range2)
196         return 0;
197     gun = sp->sct_item[I_GUN];
198     if (gun == 0)
199         return 0;
200     shell = sp->sct_item[I_SHELL];
201     if (shell <= 0)
202         shell += supply_commod(sp->sct_own, sp->sct_x, sp->sct_y,
203                                I_SHELL, 1);
204     if (shell <= 0)
205         return 0;
206     sp->sct_item[I_SHELL] = shell - 1;
207     putsect(sp);
208     damage = landgun((int)sp->sct_effic, gun);
209     if (sp->sct_own != def)
210         wu(0, sp->sct_own,
211            "%s fired on %s in %s in defense of %s, doing %d damage!\n",
212            xyas(sp->sct_x, sp->sct_y, sp->sct_own), cname(att),
213            xyas(tx, ty, sp->sct_own), cname(def), damage);
214     if (defending)
215         nreport(sp->sct_own, N_FIRE_BACK, att, 1);
216     else
217         nreport(sp->sct_own, N_FIRE_F_ATTACK, att, 1);
218     if (noisy)
219         pr("Incoming shell! %d damage done.\007\n", damage);
220     return damage;
221 }