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