]> git.pond.sub.org Git - empserver/blob - src/lib/subs/detonate.c
(opt_NEUTRON, buil, denotate, show_nuke_capab, show_nuke_build,
[empserver] / src / lib / subs / detonate.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
35 #include "player.h"
36 #include "xy.h"
37 #include "nat.h"
38 #include "file.h"
39 #include "sect.h"
40 #include "nuke.h"
41 #include "ship.h"
42 #include "land.h"
43 #include "news.h"
44 #include "plane.h"
45 #include "nsc.h"
46 #include "optlist.h"
47 #include "prototypes.h"
48
49 static void kaboom(int x, int y, int rad, natid cn);
50
51 int
52 detonate(struct plnstr *pp, int x, int y)
53 {
54     int nuketype = pp->pln_nuketype;
55     natid bombown = pp->pln_own;
56     int airburst = (pp->pln_flags & PLN_AIRBURST);
57     struct nchrstr *ncp;
58     struct plnstr plane;
59     struct sctstr sect;
60     struct shpstr ship;
61     struct lndstr land;
62     struct nukstr nuke;
63     s_char *bp;
64     s_char buf[128];
65     s_char buf2[128];
66     natid own;
67     int type;
68     int damage;
69     int fallout;
70     int rad;
71     struct nstr_sect ns;
72     struct nstr_item ni;
73     int issea = 0;
74     int retval;
75
76     pp->pln_nuketype = -1;
77     getsect(x, y, &sect);
78     if (sect.sct_type == SCT_WATER)
79         issea = 1;
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     snxtsct_dist(&ns, x, y, rad);
86     while (nxtsct(&ns, &sect)) {
87         /* Nukes falling on water affect only 1 sector */
88         if ((sect.sct_x != x) && issea)
89             continue;
90         if ((sect.sct_y != y) && issea)
91             continue;
92         own = sect.sct_own;
93         type = sect.sct_type;
94         if ((damage = nukedamage(ncp, ns.curdist, airburst)) <= 0)
95             continue;
96         if (type == SCT_SANCT) {
97             mpr(bombown, "bounced off %s\n", xyas(ns.x, ns.y, bombown));
98             if (own != 0)
99                 mpr(own, "%s nuclear device bounced off %s\n",
100                     cname(bombown), xyas(ns.x, ns.y, bombown));
101             nreport(bombown, N_NUKE, own, 1);
102             continue;
103         }
104         if (opt_FALLOUT)
105             fallout = sect.sct_fallout;
106         sect_damage(&sect, damage, 0);
107         if (sect.sct_x == x && sect.sct_y == y)
108             retval = damage;
109         if (opt_FALLOUT) {
110             if (ncp->n_flags & N_NEUT)
111                 fallout += damage * 30;
112             else
113                 fallout += damage * 3;
114             sect.sct_fallout = min(fallout, FALLOUT_MAX);
115         }
116         if (damage > 100) {
117             makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
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         if ((type == SCT_CAPIT || type == SCT_MOUNT) && damage >= 100)
137             caploss(&sect, own, "\n%s lost its capital!\n\n");
138         (void)putsect(&sect);
139         if (type != SCT_WATER)
140             nreport(bombown, N_NUKE, own, 1);
141         mpr(bombown, bp, xyas(ns.x, ns.y, bombown));
142         if (own != bombown && own != 0) {
143             (void)sprintf(buf2, bp, xyas(ns.x, ns.y, own));
144             mpr(own, "%s nuclear device %s\n", cname(bombown), buf2);
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) && (airburst != 2))
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             if (own != 0)
191                 mpr(own,
192                     "%s nuclear device did %d%% damage to %s at %s\n",
193                     cname(bombown), damage,
194                     prplane(&plane), xyas(plane.pln_x, plane.pln_y, own));
195         }
196         putplane(ni.cur, &plane);
197     }
198     snxtitem_dist(&ni, EF_LAND, x, y, rad);
199     while (nxtitem(&ni, &land)) {
200         /* Nukes falling on water affect only 1 sector */
201         if ((land.lnd_x != x) && issea)
202             continue;
203         if ((land.lnd_y != y) && issea)
204             continue;
205         if ((own = land.lnd_own) == 0)
206             continue;
207         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
208             continue;
209
210         if (land.lnd_ship >= 0) {
211             /* Are we on a sub? */
212             getship(land.lnd_ship, &ship);
213
214             if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
215                 struct sctstr sect1;
216
217                 /* Should we damage this sub? */
218                 getsect(ship.shp_x, ship.shp_y, &sect1);
219
220                 if (sect1.sct_type == SCT_BSPAN ||
221                     sect1.sct_type == SCT_BTOWER ||
222                     sect1.sct_type == SCT_WATER) {
223                     /* Ok, we're not in a harbor or trapped
224                        inland.  Now, did we get pasted
225                        directly? */
226                     if (ship.shp_x != x || ship.shp_y != y) {
227                         /* Nope, so don't mess with it */
228                         continue;
229                     }
230                 }
231             }
232         }
233         land_damage(&land, damage);
234         if (own == bombown) {
235             mpr(bombown, "%s at %s reports %d%% damage\n",
236                 prland(&land), xyas(land.lnd_x, land.lnd_y, own), damage);
237         } else {
238             if (own != 0)
239                 mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
240                     cname(bombown), damage,
241                     prland(&land), xyas(land.lnd_x, land.lnd_y, own));
242         }
243         putland(land.lnd_uid, &land);
244     }
245     snxtitem_dist(&ni, EF_SHIP, x, y, rad);
246     while (nxtitem(&ni, &ship)) {
247         /* Nukes falling on water affect only 1 sector */
248         if ((ship.shp_x != x) && issea)
249             continue;
250         if ((ship.shp_y != y) && issea)
251             continue;
252         if ((own = ship.shp_own) == 0)
253             continue;
254         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
255             continue;
256         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
257             struct sctstr sect1;
258
259             /* Should we damage this sub? */
260             getsect(ship.shp_x, ship.shp_y, &sect1);
261
262             if (sect1.sct_type == SCT_BSPAN ||
263                 sect1.sct_type == SCT_BTOWER ||
264                 sect1.sct_type == SCT_WATER) {
265                 /* Ok, we're not in a harbor or trapped
266                    inland.  Now, did we get pasted
267                    directly? */
268                 if (ship.shp_x != x || ship.shp_y != y) {
269                     /* Nope, so don't mess with it */
270                     continue;
271                 }
272             }
273         }
274         ship_damage(&ship, damage);
275         if (own == bombown) {
276             mpr(bombown, "%s at %s reports %d%% damage\n",
277                 prship(&ship), xyas(ship.shp_x, ship.shp_y, own), damage);
278         } else {
279             if (own != 0)
280                 mpr(own, "%s nuclear device did %d%% damage to %s at %s\n",
281                     cname(bombown), damage, prship(&ship),
282                     xyas(ship.shp_x, ship.shp_y, own));
283         }
284         putship(ship.shp_uid, &ship);
285     }
286     snxtitem_dist(&ni, EF_NUKE, x, y, rad);
287     while (nxtitem(&ni, &nuke)) {
288         /* Nukes falling on water affect only 1 sector */
289         if ((nuke.nuk_x != x) && issea)
290             continue;
291         if ((nuke.nuk_y != y) && issea)
292             continue;
293         if ((own = nuke.nuk_own) == 0)
294             continue;
295         if ((damage = nukedamage(ncp, ni.curdist, airburst)) <= 0)
296             continue;
297         if (roll(100) >= damage)
298             continue;
299         makelost(EF_NUKE, nuke.nuk_own, nuke.nuk_uid, nuke.nuk_x,
300                  nuke.nuk_y);
301         nuke.nuk_own = 0;
302         if (own == bombown) {
303             mpr(bombown, "nuclear stockpile #%d at %s destroyed\n",
304                 ni.cur, xyas(nuke.nuk_x, nuke.nuk_y, own));
305         } else {
306             if (own != 0)
307                 mpr(own, "nuclear stockpile #%d at %s destroyed\n",
308                     ni.cur, xyas(nuke.nuk_x, nuke.nuk_y, own));
309         }
310         putnuke(ni.cur, &nuke);
311     }
312     return retval;
313 }
314
315
316 /*
317  * silly to be sure.
318  */
319 static void
320 kaboom(int x, int y, int rad, natid cn)
321 {
322     mpr(cn, "\n\nK A B ");
323     while (rad-- > 0)
324         mpr(cn, "O O ");
325     mpr(cn, "M ! in %s\n\n", xyas(x, y, cn));
326 }