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