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