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