]> git.pond.sub.org Git - empserver/blob - src/lib/subs/detonate.c
Fix value of detonate() when target sector takes no damage
[empserver] / src / lib / subs / detonate.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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  *  detonate.c: Detonate a nuclear device in a sector.
29  *
30  *  Known contributors to this file:
31  *     Steve McClure, 1998-2000
32  */
33
34 #include <config.h>
35
36 #include "file.h"
37 #include "land.h"
38 #include "lost.h"
39 #include "misc.h"
40 #include "nat.h"
41 #include "news.h"
42 #include "nsc.h"
43 #include "nuke.h"
44 #include "optlist.h"
45 #include "plane.h"
46 #include "player.h"
47 #include "prototypes.h"
48 #include "sect.h"
49 #include "ship.h"
50 #include "xy.h"
51
52 static void kaboom(int x, int y, int rad, natid cn);
53
54 int
55 detonate(struct nukstr *np, coord x, coord y, int airburst)
56 {
57     int nuketype = np->nuk_type;
58     natid bombown = np->nuk_own;
59     struct nchrstr *ncp;
60     struct plnstr plane;
61     struct sctstr sect;
62     struct shpstr ship;
63     struct lndstr land;
64     struct nukstr nuke;
65     char *bp;
66     char buf[128];
67     char buf2[128];
68     natid own;
69     int type;
70     int damage;
71     int fallout;
72     int rad;
73     struct nstr_sect ns;
74     struct nstr_item ni;
75     int issea;
76
77     getsect(x, y, &sect);
78     issea = sect.sct_type == SCT_WATER;
79     ncp = &nchr[nuketype];
80     kaboom(x, y, ncp->n_blast, bombown);
81     rad = ncp->n_blast;
82     if (!airburst)
83         rad = rad * 2 / 3;
84     np->nuk_effic = 0;
85     putnuke(np->nuk_uid, np);
86
87     snxtsct_dist(&ns, x, y, rad);
88     while (nxtsct(&ns, &sect)) {
89         /* Nukes falling on water affect only 1 sector */
90         if ((sect.sct_x != x) && issea)
91             continue;
92         if ((sect.sct_y != y) && issea)
93             continue;
94         own = sect.sct_own;
95         type = sect.sct_type;
96         if ((damage = nukedamage(ncp, ns.curdist, airburst)) <= 0)
97             continue;
98         if (type == SCT_SANCT) {
99             mpr(bombown, "bounced off %s\n", xyas(ns.x, ns.y, bombown));
100             mpr(own, "%s nuclear device bounced off %s\n",
101                 cname(bombown), xyas(ns.x, ns.y, bombown));
102             nreport(bombown, N_NUKE, own, 1);
103             continue;
104         }
105         if (opt_FALLOUT)
106             fallout = sect.sct_fallout;
107         sect_damage(&sect, damage);
108         if (opt_FALLOUT) {
109             if (ncp->n_flags & N_NEUT)
110                 fallout += damage * 30;
111             else
112                 fallout += damage * 3;
113             sect.sct_fallout = MIN(fallout, FALLOUT_MAX);
114         }
115         if (damage > 100) {
116             sect.sct_oldown = 0;
117             sect.sct_own = 0;
118             if (type == SCT_WATER || type == SCT_BSPAN ||
119                 type == SCT_BTOWER) {
120                 bp = "left nothing but water in %s\n";
121                 if (type != SCT_WATER) {
122                     sect.sct_newtype = SCT_WATER;
123                     sect.sct_type = SCT_WATER;
124                 }
125             } else {
126                 sect.sct_newtype = SCT_WASTE;
127                 sect.sct_type = SCT_WASTE;
128                 bp = "turned %s into a radioactive wasteland\n";
129             }
130         } else {
131             sprintf(buf, "did %d%%%% damage in %%s\n", damage);
132             bp = buf;
133         }
134         (void)putsect(&sect);
135         if (type != SCT_WATER)
136             nreport(bombown, N_NUKE, own, 1);
137         mpr(bombown, bp, xyas(ns.x, ns.y, bombown));
138         if (own != bombown && own != 0) {
139             (void)sprintf(buf2, bp, xyas(ns.x, ns.y, own));
140             mpr(own, "%s nuclear device %s\n", cname(bombown), buf2);
141         }
142     }
143
144     snxtitem_dist(&ni, EF_PLANE, x, y, rad);
145     while (nxtitem(&ni, &plane)) {
146         /* Nukes falling on water affect only 1 sector */
147         if ((plane.pln_x != x) && issea)
148             continue;
149         if ((plane.pln_y != y) && issea)
150             continue;
151         if ((own = plane.pln_own) == 0)
152             continue;
153         if (plane.pln_flags & PLN_LAUNCHED)
154             continue;
155         damage = nukedamage(ncp, ni.curdist, airburst) - plane.pln_harden;
156         if (damage <= 0)
157             continue;
158         if (plane.pln_ship >= 0) {
159             /* Are we on a sub? */
160             getship(plane.pln_ship, &ship);
161
162             if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
163                 struct sctstr sect1;
164
165                 /* Should we damage this sub? */
166                 getsect(ship.shp_x, ship.shp_y, &sect1);
167
168                 if (sect1.sct_type == SCT_BSPAN ||
169                     sect1.sct_type == SCT_BTOWER ||
170                     sect1.sct_type == SCT_WATER) {
171                     /* Ok, we're not in a harbor or trapped
172                        inland.  Now, did we get pasted
173                        directly? */
174                     if (ship.shp_x != x || ship.shp_y != y) {
175                         /* Nope, so don't mess with it */
176                         continue;
177                     }
178                 }
179             }
180         }
181         planedamage(&plane, damage);
182         if (own == bombown) {
183             mpr(bombown, "%s at %s reports %d%% damage\n",
184                 prplane(&plane),
185                 xyas(plane.pln_x, plane.pln_y, own), damage);
186         } else {
187             mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
188                 cname(bombown), damage,
189                 prplane(&plane), xyas(plane.pln_x, plane.pln_y, own));
190         }
191         putplane(ni.cur, &plane);
192     }
193
194     snxtitem_dist(&ni, EF_LAND, x, y, rad);
195     while (nxtitem(&ni, &land)) {
196         /* Nukes falling on water affect only 1 sector */
197         if ((land.lnd_x != x) && issea)
198             continue;
199         if ((land.lnd_y != y) && issea)
200             continue;
201         if ((own = land.lnd_own) == 0)
202             continue;
203         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
204             continue;
205
206         if (land.lnd_ship >= 0) {
207             /* Are we on a sub? */
208             getship(land.lnd_ship, &ship);
209
210             if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
211                 struct sctstr sect1;
212
213                 /* Should we damage this sub? */
214                 getsect(ship.shp_x, ship.shp_y, &sect1);
215
216                 if (sect1.sct_type == SCT_BSPAN ||
217                     sect1.sct_type == SCT_BTOWER ||
218                     sect1.sct_type == SCT_WATER) {
219                     /* Ok, we're not in a harbor or trapped
220                        inland.  Now, did we get pasted
221                        directly? */
222                     if (ship.shp_x != x || ship.shp_y != y) {
223                         /* Nope, so don't mess with it */
224                         continue;
225                     }
226                 }
227             }
228         }
229         land_damage(&land, damage);
230         if (own == bombown) {
231             mpr(bombown, "%s at %s reports %d%% damage\n",
232                 prland(&land), xyas(land.lnd_x, land.lnd_y, own), damage);
233         } else {
234             mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
235                 cname(bombown), damage,
236                 prland(&land), xyas(land.lnd_x, land.lnd_y, own));
237         }
238         putland(land.lnd_uid, &land);
239     }
240
241     snxtitem_dist(&ni, EF_SHIP, x, y, rad);
242     while (nxtitem(&ni, &ship)) {
243         /* Nukes falling on water affect only 1 sector */
244         if ((ship.shp_x != x) && issea)
245             continue;
246         if ((ship.shp_y != y) && issea)
247             continue;
248         if ((own = ship.shp_own) == 0)
249             continue;
250         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
251             continue;
252         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
253             struct sctstr sect1;
254
255             /* Should we damage this sub? */
256             getsect(ship.shp_x, ship.shp_y, &sect1);
257
258             if (sect1.sct_type == SCT_BSPAN ||
259                 sect1.sct_type == SCT_BTOWER ||
260                 sect1.sct_type == SCT_WATER) {
261                 /* Ok, we're not in a harbor or trapped
262                    inland.  Now, did we get pasted
263                    directly? */
264                 if (ship.shp_x != x || ship.shp_y != y) {
265                     /* Nope, so don't mess with it */
266                     continue;
267                 }
268             }
269         }
270         ship_damage(&ship, damage);
271         if (own == bombown) {
272             mpr(bombown, "%s at %s reports %d%% damage\n",
273                 prship(&ship), xyas(ship.shp_x, ship.shp_y, own), damage);
274         } else {
275             mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
276                 cname(bombown), damage, prship(&ship),
277                 xyas(ship.shp_x, ship.shp_y, own));
278         }
279         putship(ship.shp_uid, &ship);
280     }
281
282     snxtitem_dist(&ni, EF_NUKE, x, y, rad);
283     while (nxtitem(&ni, &nuke)) {
284         /* Nukes falling on water affect only 1 sector */
285         if ((nuke.nuk_x != x) && issea)
286             continue;
287         if ((nuke.nuk_y != y) && issea)
288             continue;
289         if ((own = nuke.nuk_own) == 0)
290             continue;
291         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
292             continue;
293         if (roll(100) >= damage)
294             continue;
295         nuke.nuk_effic = 0;
296         if (own == bombown) {
297             mpr(bombown, "%s at %s destroyed\n",
298                 prnuke(&nuke), xyas(nuke.nuk_x, nuke.nuk_y, own));
299         } else {
300             mpr(own, "%s at %s destroyed\n",
301                 prnuke(&nuke), xyas(nuke.nuk_x, nuke.nuk_y, own));
302         }
303         putnuke(ni.cur, &nuke);
304     }
305
306     return nukedamage(ncp, 0, airburst);
307 }
308
309
310 /*
311  * silly to be sure.
312  */
313 static void
314 kaboom(int x, int y, int rad, natid cn)
315 {
316     mpr(cn, "\n\nK A B O O ");
317     while (rad-- > 1)
318         mpr(cn, "O O ");
319     mpr(cn, "M ! in %s\n\n", xyas(x, y, cn));
320 }