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