]> git.pond.sub.org Git - empserver/blob - src/lib/subs/detonate.c
7adf979662e74862dde43791f0e3d35a90ea32ad
[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     int issea;
77
78     mpr(bombown, "Releasing RV's for %s detonation...\n",
79         airburst ? "airburst" : "groundburst");
80
81     getsect(x, y, &sect);
82     issea = sect.sct_type == SCT_WATER;
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     np->nuk_effic = 0;
89     putnuke(np->nuk_uid, np);
90
91     snxtsct_dist(&ns, x, y, rad);
92     while (nxtsct(&ns, &sect)) {
93         /* Nukes falling on water affect only 1 sector */
94         if ((sect.sct_x != x) && issea)
95             continue;
96         if ((sect.sct_y != y) && issea)
97             continue;
98         own = sect.sct_own;
99         type = sect.sct_type;
100         if ((damage = nukedamage(ncp, ns.curdist, airburst)) <= 0)
101             continue;
102         if (type == SCT_SANCT) {
103             mpr(bombown, "bounced off %s\n", xyas(ns.x, ns.y, bombown));
104             mpr(own, "%s nuclear device bounced off %s\n",
105                 cname(bombown), xyas(ns.x, ns.y, bombown));
106             nreport(bombown, N_NUKE, own, 1);
107             continue;
108         }
109         sect_damage(&sect, damage);
110         if (opt_FALLOUT) {
111             fallout = sect.sct_fallout;
112             if (ncp->n_flags & N_NEUT)
113                 fallout += damage * 30;
114             else
115                 fallout += damage * 3;
116             sect.sct_fallout = MIN(fallout, FALLOUT_MAX);
117         }
118         if (damage > 100) {
119             sect.sct_oldown = 0;
120             sect.sct_own = 0;
121             if (type == SCT_WATER || type == SCT_BSPAN ||
122                 type == SCT_BTOWER) {
123                 bp = "left nothing but water in %s\n";
124                 if (type != SCT_WATER) {
125                     sect.sct_newtype = SCT_WATER;
126                     sect.sct_type = SCT_WATER;
127                 }
128             } else {
129                 sect.sct_newtype = SCT_WASTE;
130                 sect.sct_type = SCT_WASTE;
131                 bp = "turned %s into a radioactive wasteland\n";
132             }
133         } else {
134             sprintf(buf, "did %d%%%% damage in %%s\n", damage);
135             bp = buf;
136         }
137         (void)putsect(&sect);
138         if (type != SCT_WATER)
139             nreport(bombown, N_NUKE, own, 1);
140         mpr(bombown, bp, xyas(ns.x, ns.y, bombown));
141         if (own != bombown && own != 0) {
142             (void)sprintf(buf2, bp, xyas(ns.x, ns.y, own));
143             mpr(own, "%s nuclear device %s\n", cname(bombown), buf2);
144         }
145     }
146
147     snxtitem_dist(&ni, EF_PLANE, x, y, rad);
148     while (nxtitem(&ni, &plane)) {
149         /* Nukes falling on water affect only 1 sector */
150         if ((plane.pln_x != x) && issea)
151             continue;
152         if ((plane.pln_y != y) && issea)
153             continue;
154         if ((own = plane.pln_own) == 0)
155             continue;
156         if (plane.pln_flags & PLN_LAUNCHED)
157             continue;
158         damage = nukedamage(ncp, ni.curdist, airburst) - plane.pln_harden;
159         if (damage <= 0)
160             continue;
161         if (plane.pln_ship >= 0) {
162             /* Are we on a sub? */
163             getship(plane.pln_ship, &ship);
164
165             if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
166                 struct sctstr sect1;
167
168                 /* Should we damage this sub? */
169                 getsect(ship.shp_x, ship.shp_y, &sect1);
170
171                 if (sect1.sct_type == SCT_BSPAN ||
172                     sect1.sct_type == SCT_BTOWER ||
173                     sect1.sct_type == SCT_WATER) {
174                     /* Ok, we're not in a harbor or trapped
175                        inland.  Now, did we get pasted
176                        directly? */
177                     if (ship.shp_x != x || ship.shp_y != y) {
178                         /* Nope, so don't mess with it */
179                         continue;
180                     }
181                 }
182             }
183         }
184         planedamage(&plane, damage);
185         if (own == bombown) {
186             mpr(bombown, "%s at %s reports %d%% damage\n",
187                 prplane(&plane),
188                 xyas(plane.pln_x, plane.pln_y, own), damage);
189         } else {
190             mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
191                 cname(bombown), damage,
192                 prplane(&plane), xyas(plane.pln_x, plane.pln_y, own));
193         }
194         putplane(ni.cur, &plane);
195     }
196
197     snxtitem_dist(&ni, EF_LAND, x, y, rad);
198     while (nxtitem(&ni, &land)) {
199         /* Nukes falling on water affect only 1 sector */
200         if ((land.lnd_x != x) && issea)
201             continue;
202         if ((land.lnd_y != y) && issea)
203             continue;
204         if ((own = land.lnd_own) == 0)
205             continue;
206         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
207             continue;
208
209         if (land.lnd_ship >= 0) {
210             /* Are we on a sub? */
211             getship(land.lnd_ship, &ship);
212
213             if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
214                 struct sctstr sect1;
215
216                 /* Should we damage this sub? */
217                 getsect(ship.shp_x, ship.shp_y, &sect1);
218
219                 if (sect1.sct_type == SCT_BSPAN ||
220                     sect1.sct_type == SCT_BTOWER ||
221                     sect1.sct_type == SCT_WATER) {
222                     /* Ok, we're not in a harbor or trapped
223                        inland.  Now, did we get pasted
224                        directly? */
225                     if (ship.shp_x != x || ship.shp_y != y) {
226                         /* Nope, so don't mess with it */
227                         continue;
228                     }
229                 }
230             }
231         }
232         land_damage(&land, damage);
233         if (own == bombown) {
234             mpr(bombown, "%s at %s reports %d%% damage\n",
235                 prland(&land), xyas(land.lnd_x, land.lnd_y, own), damage);
236         } else {
237             mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
238                 cname(bombown), damage,
239                 prland(&land), xyas(land.lnd_x, land.lnd_y, own));
240         }
241         putland(land.lnd_uid, &land);
242     }
243
244     snxtitem_dist(&ni, EF_SHIP, x, y, rad);
245     while (nxtitem(&ni, &ship)) {
246         /* Nukes falling on water affect only 1 sector */
247         if ((ship.shp_x != x) && issea)
248             continue;
249         if ((ship.shp_y != y) && issea)
250             continue;
251         if ((own = ship.shp_own) == 0)
252             continue;
253         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
254             continue;
255         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
256             struct sctstr sect1;
257
258             /* Should we damage this sub? */
259             getsect(ship.shp_x, ship.shp_y, &sect1);
260
261             if (sect1.sct_type == SCT_BSPAN ||
262                 sect1.sct_type == SCT_BTOWER ||
263                 sect1.sct_type == SCT_WATER) {
264                 /* Ok, we're not in a harbor or trapped
265                    inland.  Now, did we get pasted
266                    directly? */
267                 if (ship.shp_x != x || ship.shp_y != y) {
268                     /* Nope, so don't mess with it */
269                     continue;
270                 }
271             }
272         }
273         ship_damage(&ship, damage);
274         if (own == bombown) {
275             mpr(bombown, "%s at %s reports %d%% damage\n",
276                 prship(&ship), xyas(ship.shp_x, ship.shp_y, own), damage);
277         } else {
278             mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
279                 cname(bombown), damage, prship(&ship),
280                 xyas(ship.shp_x, ship.shp_y, own));
281         }
282         putship(ship.shp_uid, &ship);
283     }
284
285     snxtitem_dist(&ni, EF_NUKE, x, y, rad);
286     while (nxtitem(&ni, &nuke)) {
287         /* Nukes falling on water affect only 1 sector */
288         if ((nuke.nuk_x != x) && issea)
289             continue;
290         if ((nuke.nuk_y != y) && issea)
291             continue;
292         if ((own = nuke.nuk_own) == 0)
293             continue;
294         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
295             continue;
296         if (roll(100) >= damage)
297             continue;
298         nuke.nuk_effic = 0;
299         if (own == bombown) {
300             mpr(bombown, "%s at %s destroyed\n",
301                 prnuke(&nuke), xyas(nuke.nuk_x, nuke.nuk_y, own));
302         } else {
303             mpr(own, "%s at %s destroyed\n",
304                 prnuke(&nuke), xyas(nuke.nuk_x, nuke.nuk_y, own));
305         }
306         putnuke(ni.cur, &nuke);
307     }
308
309     return nukedamage(ncp, 0, airburst);
310 }
311
312
313 /*
314  * silly to be sure.
315  */
316 static void
317 kaboom(int x, int y, int rad, natid cn)
318 {
319     mpr(cn, "\n\nK A B O O ");
320     while (rad-- > 1)
321         mpr(cn, "O O ");
322     mpr(cn, "M ! in %s\n\n", xyas(x, y, cn));
323 }