]> git.pond.sub.org Git - empserver/blob - src/lib/subs/bridgefall.c
Fix confused and buggy bridge splashing code
[empserver] / src / lib / subs / bridgefall.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  */
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 *, struct emp_qelem *);
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  * Don't drown planes in LIST when splashing 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, struct emp_qelem *list)
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, list);
72     if ((des == SCT_BHEAD || des == SCT_BTOWER) && !opt_EASY_BRIDGES)
73         bridgefall(sp, list);
74 }
75
76 void
77 bridgefall(struct sctstr *sp, struct emp_qelem *list)
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     for (i = 1; i <= 6; i++) {
89         nx = sp->sct_x + diroff[i][0];
90         ny = sp->sct_y + diroff[i][1];
91         getsect(nx, ny, &sect);
92         if (sect.sct_type != SCT_BSPAN)
93             continue;
94         for (j = 1; j <= 6; j++) {
95             nnx = nx + diroff[j][0];
96             nny = ny + diroff[j][1];
97             if (nnx == sp->sct_x && nny == sp->sct_y)
98                 continue;
99             getsect(nnx, nny, &bh_sect);
100             if (bh_sect.sct_type == SCT_BHEAD &&
101                 bh_sect.sct_newtype == SCT_BHEAD)
102                 break;
103             if (bh_sect.sct_type == SCT_BTOWER)
104                 break;
105             /* With EASY_BRIDGES, it just has to be next to any
106                land */
107             if (opt_EASY_BRIDGES) {
108                 if (bh_sect.sct_type != SCT_WATER &&
109                     bh_sect.sct_type != SCT_BSPAN)
110                     break;
111             }
112         }
113         if (j > 6) {
114             knockdown(&sect, list);
115             putsect(&sect);
116         }
117     }
118 }
119
120 /* Knock down a bridge span.  Note that this does NOT write the
121  * sector out to the database, it's up to the caller to do that. */
122 static void
123 knockdown(struct sctstr *sp, struct emp_qelem *list)
124 {
125     struct lndstr land;
126     struct plnstr plane;
127     struct nukstr nuke;
128     struct nstr_item ni;
129     struct natstr *np;
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     sp->sct_type = SCT_WATER;
136     sp->sct_newtype = SCT_WATER;
137     makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
138     sp->sct_own = 0;
139     sp->sct_oldown = 0;
140     sp->sct_mobil = 0;
141     sp->sct_effic = 0;
142
143     /* Sink all the units */
144     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
145     while (nxtitem(&ni, &land)) {
146         if (land.lnd_own == 0)
147             continue;
148         if (land.lnd_ship >= 0)
149             continue;
150         np = getnatp(land.lnd_own);
151         if (np->nat_flags & NF_BEEP)
152             mpr(land.lnd_own, "\07");
153         mpr(land.lnd_own, "     AARGH! %s tumbles to its doom!\n",
154             prland(&land));
155         land.lnd_effic = 0;
156         putland(land.lnd_uid, &land);
157     }
158     /* Sink all the planes */
159     snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
160     while (nxtitem(&ni, &plane)) {
161         if (plane.pln_own == 0)
162             continue;
163         if (plane.pln_flags & PLN_LAUNCHED)
164             continue;
165         if (plane.pln_ship >= 0)
166             continue;
167         /* Is this plane flying in this list? */
168         if (ac_isflying(&plane, list))
169             continue;
170         np = getnatp(plane.pln_own);
171         if (np->nat_flags & NF_BEEP)
172             mpr(plane.pln_own, "\07");
173         mpr(plane.pln_own, "     AARGH! %s tumbles to its doom!\n",
174             prplane(&plane));
175         plane.pln_effic = 0;
176         putplane(plane.pln_uid, &plane);
177     }
178     /* Sink all the nukes */
179     snxtitem_xy(&ni, EF_NUKE, sp->sct_x, sp->sct_y);
180     while (nxtitem(&ni, &nuke)) {
181         if (nuke.nuk_own == 0)
182             continue;
183         if (nuke.nuk_plane >= 0)
184             continue;
185         np = getnatp(nuke.nuk_own);
186         if (np->nat_flags & NF_BEEP)
187             mpr(nuke.nuk_own, "\07");
188         mpr(nuke.nuk_own, "     %s sinks to the bottom of the sea!\n",
189             prnuke(&nuke));
190         nuke.nuk_effic = 0;
191         putnuke(nuke.nuk_uid, &nuke);
192     }
193     memset(sp->sct_item, 0, sizeof(sp->sct_item));
194     memset(sp->sct_del, 0, sizeof(sp->sct_del));
195     memset(sp->sct_dist, 0, sizeof(sp->sct_dist));
196     sp->sct_pstage = PLG_HEALTHY;
197     sp->sct_ptime = 0;
198     sp->sct_che = 0;
199     sp->sct_che_target = 0;
200 }