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