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