]> git.pond.sub.org Git - empserver/blob - src/lib/subs/detonate.c
Update bmap when nuclear detonation makes wasteland
[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 "map.h"
41 #include "misc.h"
42 #include "nat.h"
43 #include "news.h"
44 #include "nsc.h"
45 #include "nuke.h"
46 #include "optlist.h"
47 #include "plane.h"
48 #include "player.h"
49 #include "prototypes.h"
50 #include "sect.h"
51 #include "ship.h"
52 #include "xy.h"
53
54 static void kaboom(int x, int y, int rad, natid cn);
55
56 int
57 detonate(struct nukstr *np, coord x, coord y, int airburst)
58 {
59     int nuketype = np->nuk_type;
60     natid bombown = np->nuk_own;
61     struct nchrstr *ncp;
62     struct plnstr plane;
63     struct sctstr sect;
64     struct shpstr ship;
65     struct lndstr land;
66     struct nukstr nuke;
67     char *bp;
68     char buf[128];
69     char buf2[128];
70     natid own;
71     int type;
72     int damage;
73     int fallout;
74     int rad;
75     struct nstr_sect ns;
76     struct nstr_item ni;
77     int changed = 0;
78
79     mpr(bombown, "Releasing RV's for %s detonation...\n",
80         airburst ? "airburst" : "groundburst");
81
82     getsect(x, y, &sect);
83     ncp = &nchr[nuketype];
84     kaboom(x, y, ncp->n_blast, bombown);
85     rad = ncp->n_blast;
86     if (!airburst)
87         rad = rad * 2 / 3;
88     if (sect.sct_type == SCT_WATER)
89         rad = 0;     /* Nukes falling on water affect only 1 sector */
90     np->nuk_effic = 0;
91     putnuke(np->nuk_uid, np);
92
93     snxtsct_dist(&ns, x, y, rad);
94     while (nxtsct(&ns, &sect)) {
95         own = sect.sct_own;
96         type = sect.sct_type;
97         if ((damage = nukedamage(ncp, ns.curdist, airburst)) <= 0)
98             continue;
99         if (type == SCT_SANCT) {
100             mpr(bombown, "bounced off %s\n", xyas(ns.x, ns.y, bombown));
101             mpr(own, "%s nuclear device bounced off %s\n",
102                 cname(bombown), xyas(ns.x, ns.y, bombown));
103             nreport(bombown, N_NUKE, own, 1);
104             continue;
105         }
106         sect_damage(&sect, damage);
107         if (opt_FALLOUT) {
108             fallout = sect.sct_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             changed |= map_set(player->cnum, sect.sct_x, sect.sct_y,
131                                dchr[sect.sct_type].d_mnem, 0);
132         } else {
133             sprintf(buf, "did %d%%%% damage in %%s\n", damage);
134             bp = buf;
135         }
136         (void)putsect(&sect);
137         if (type != SCT_WATER)
138             nreport(bombown, N_NUKE, own, 1);
139         mpr(bombown, bp, xyas(ns.x, ns.y, bombown));
140         if (own != bombown && own != 0) {
141             (void)sprintf(buf2, bp, xyas(ns.x, ns.y, own));
142             mpr(own, "%s nuclear device %s\n", cname(bombown), buf2);
143         }
144     }
145
146     if (changed)
147         writebmap(player->cnum);
148
149     snxtitem_dist(&ni, EF_PLANE, x, y, rad);
150     while (nxtitem(&ni, &plane)) {
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         if ((own = land.lnd_own) == 0)
197             continue;
198         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
199             continue;
200
201         if (land.lnd_ship >= 0) {
202             /* Are we on a sub? */
203             getship(land.lnd_ship, &ship);
204
205             if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
206                 struct sctstr sect1;
207
208                 /* Should we damage this sub? */
209                 getsect(ship.shp_x, ship.shp_y, &sect1);
210
211                 if (sect1.sct_type == SCT_BSPAN ||
212                     sect1.sct_type == SCT_BTOWER ||
213                     sect1.sct_type == SCT_WATER) {
214                     /* Ok, we're not in a harbor or trapped
215                        inland.  Now, did we get pasted
216                        directly? */
217                     if (ship.shp_x != x || ship.shp_y != y) {
218                         /* Nope, so don't mess with it */
219                         continue;
220                     }
221                 }
222             }
223         }
224         land_damage(&land, damage);
225         if (own == bombown) {
226             mpr(bombown, "%s at %s reports %d%% damage\n",
227                 prland(&land), xyas(land.lnd_x, land.lnd_y, own), damage);
228         } else {
229             mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
230                 cname(bombown), damage,
231                 prland(&land), xyas(land.lnd_x, land.lnd_y, own));
232         }
233         putland(land.lnd_uid, &land);
234     }
235
236     snxtitem_dist(&ni, EF_SHIP, x, y, rad);
237     while (nxtitem(&ni, &ship)) {
238         if ((own = ship.shp_own) == 0)
239             continue;
240         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
241             continue;
242         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
243             struct sctstr sect1;
244
245             /* Should we damage this sub? */
246             getsect(ship.shp_x, ship.shp_y, &sect1);
247
248             if (sect1.sct_type == SCT_BSPAN ||
249                 sect1.sct_type == SCT_BTOWER ||
250                 sect1.sct_type == SCT_WATER) {
251                 /* Ok, we're not in a harbor or trapped
252                    inland.  Now, did we get pasted
253                    directly? */
254                 if (ship.shp_x != x || ship.shp_y != y) {
255                     /* Nope, so don't mess with it */
256                     continue;
257                 }
258             }
259         }
260         ship_damage(&ship, damage);
261         if (own == bombown) {
262             mpr(bombown, "%s at %s reports %d%% damage\n",
263                 prship(&ship), xyas(ship.shp_x, ship.shp_y, own), damage);
264         } else {
265             mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
266                 cname(bombown), damage, prship(&ship),
267                 xyas(ship.shp_x, ship.shp_y, own));
268         }
269         putship(ship.shp_uid, &ship);
270     }
271
272     snxtitem_dist(&ni, EF_NUKE, x, y, rad);
273     while (nxtitem(&ni, &nuke)) {
274         if ((own = nuke.nuk_own) == 0)
275             continue;
276         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
277             continue;
278         if (roll(100) >= damage)
279             continue;
280         nuke.nuk_effic = 0;
281         if (own == bombown) {
282             mpr(bombown, "%s at %s destroyed\n",
283                 prnuke(&nuke), xyas(nuke.nuk_x, nuke.nuk_y, own));
284         } else {
285             mpr(own, "%s at %s destroyed\n",
286                 prnuke(&nuke), xyas(nuke.nuk_x, nuke.nuk_y, own));
287         }
288         putnuke(ni.cur, &nuke);
289     }
290
291     return nukedamage(ncp, 0, airburst);
292 }
293
294
295 /*
296  * silly to be sure.
297  */
298 static void
299 kaboom(int x, int y, int rad, natid cn)
300 {
301     mpr(cn, "\n\nK A B O O ");
302     while (rad-- > 1)
303         mpr(cn, "O O ");
304     mpr(cn, "M ! in %s\n\n", xyas(x, y, cn));
305 }