]> git.pond.sub.org Git - empserver/blob - src/lib/subs/bridgefall.c
Don't beep when plane, land unit or nuke die on collapsing bridge
[empserver] / src / lib / subs / bridgefall.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *  bridgefall.c: Knock a bridge down
29  *
30  *  Known contributors to this file:
31  *     Steve McClure, 1998
32  *     Markus Armbruster, 2004-2009
33  */
34
35 #include <config.h>
36
37 #include "file.h"
38 #include "land.h"
39 #include "lost.h"
40 #include "misc.h"
41 #include "nat.h"
42 #include "nsc.h"
43 #include "nuke.h"
44 #include "optlist.h"
45 #include "path.h"
46 #include "plague.h"
47 #include "plane.h"
48 #include "prototypes.h"
49 #include "sect.h"
50 #include "xy.h"
51
52 static void knockdown(struct sctstr *);
53
54 /*
55  * Check bridges at and around SP after damage to SP.
56  * If SP is an inefficent bridge, splash it.
57  * If SP can't support a bridge, splash unsupported adjacent bridges.
58  * Write back splashed bridges, except for SP; writing that one is
59  * left to the caller.
60  */
61 void
62 bridge_damaged(struct sctstr *sp)
63 {
64     int des;
65
66     if (sp->sct_effic >= SCT_MINEFF)
67         return;
68
69     des = sp->sct_type;
70     if (des == SCT_BSPAN || des == SCT_BTOWER)
71         knockdown(sp);
72     if ((des == SCT_BHEAD || des == SCT_BTOWER) && !opt_EASY_BRIDGES)
73         bridgefall(sp);
74 }
75
76 void
77 bridgefall(struct sctstr *sp)
78 {
79     int i;
80     int j;
81     struct sctstr sect;
82     struct sctstr bh_sect;
83     int nx;
84     int ny;
85     int nnx;
86     int nny;
87
88     if (CANT_HAPPEN(opt_EASY_BRIDGES))
89         return;
90
91     for (i = 1; i <= 6; i++) {
92         nx = sp->sct_x + diroff[i][0];
93         ny = sp->sct_y + diroff[i][1];
94         getsect(nx, ny, &sect);
95         if (sect.sct_type != SCT_BSPAN)
96             continue;
97         for (j = 1; j <= 6; j++) {
98             nnx = nx + diroff[j][0];
99             nny = ny + diroff[j][1];
100             if (nnx == sp->sct_x && nny == sp->sct_y)
101                 continue;
102             getsect(nnx, nny, &bh_sect);
103             if (bh_sect.sct_type == SCT_BHEAD &&
104                 bh_sect.sct_newtype == SCT_BHEAD)
105                 break;
106             if (bh_sect.sct_type == SCT_BTOWER)
107                 break;
108         }
109         if (j > 6) {
110             knockdown(&sect);
111             putsect(&sect);
112         }
113     }
114 }
115
116 /* Knock down a bridge span.  Note that this does NOT write the
117  * sector out to the database, it's up to the caller to do that. */
118 static void
119 knockdown(struct sctstr *sp)
120 {
121     struct lndstr land;
122     struct plnstr plane;
123     struct nukstr nuke;
124     struct nstr_item ni;
125
126     mpr(sp->sct_own,
127         "Crumble... SCREEEECH!  Splash! Bridge%s falls at %s!\n",
128         sp->sct_type == SCT_BTOWER ? " tower" : "",
129         xyas(sp->sct_x, sp->sct_y, sp->sct_own));
130     if (!SCT_MINES_ARE_SEAMINES(sp))
131         sp->sct_mines = 0;
132     sp->sct_type = SCT_WATER;
133     sp->sct_newtype = SCT_WATER;
134     sp->sct_own = 0;
135     sp->sct_oldown = 0;
136     sp->sct_mobil = 0;
137     sp->sct_effic = 0;
138
139     /* Sink all the units */
140     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
141     while (nxtitem(&ni, &land)) {
142         if (land.lnd_own == 0)
143             continue;
144         if (land.lnd_ship >= 0)
145             continue;
146         mpr(land.lnd_own, "     AARGH! %s tumbles to its doom!\n",
147             prland(&land));
148         land.lnd_effic = 0;
149         putland(land.lnd_uid, &land);
150     }
151     /* Sink all the planes */
152     snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
153     while (nxtitem(&ni, &plane)) {
154         if (plane.pln_own == 0)
155             continue;
156         if (plane.pln_flags & PLN_LAUNCHED)
157             continue;
158         if (plane.pln_ship >= 0)
159             continue;
160         mpr(plane.pln_own, "     AARGH! %s tumbles to its doom!\n",
161             prplane(&plane));
162         plane.pln_effic = 0;
163         putplane(plane.pln_uid, &plane);
164     }
165     /* Sink all the nukes */
166     snxtitem_xy(&ni, EF_NUKE, sp->sct_x, sp->sct_y);
167     while (nxtitem(&ni, &nuke)) {
168         if (nuke.nuk_own == 0)
169             continue;
170         if (nuke.nuk_plane >= 0)
171             continue;
172         mpr(nuke.nuk_own, "     %s sinks to the bottom of the sea!\n",
173             prnuke(&nuke));
174         nuke.nuk_effic = 0;
175         putnuke(nuke.nuk_uid, &nuke);
176     }
177     memset(sp->sct_item, 0, sizeof(sp->sct_item));
178     memset(sp->sct_del, 0, sizeof(sp->sct_del));
179     memset(sp->sct_dist, 0, sizeof(sp->sct_dist));
180     sp->sct_pstage = PLG_HEALTHY;
181     sp->sct_ptime = 0;
182     sp->sct_che = 0;
183     sp->sct_che_target = 0;
184 }