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