0
|
1 |
#include "stdafx.h"
|
|
2 |
#include "ttd.h"
|
|
3 |
#include "vehicle.h"
|
|
4 |
#include "viewport.h"
|
|
5 |
#include "command.h"
|
|
6 |
#include "player.h"
|
|
7 |
#include "town.h"
|
|
8 |
|
|
9 |
extern void DrawCanalWater(uint tile);
|
|
10 |
|
|
11 |
static const byte _bridge_available_year[MAX_BRIDGES] = {
|
|
12 |
0, 0, 10, 0, 10, 10, 10, 10, 10, 10, 75, 85, 90
|
|
13 |
};
|
|
14 |
|
|
15 |
static const byte _bridge_minlen[MAX_BRIDGES] = {
|
|
16 |
0, 0, 0, 2, 3, 3, 3, 3, 3, 0, 2, 2, 2
|
|
17 |
};
|
|
18 |
|
|
19 |
static const byte _bridge_maxlen[MAX_BRIDGES] = {
|
|
20 |
16, 2, 5, 10, 16, 16, 7, 8, 9, 2, 16, 32, 32,
|
|
21 |
};
|
|
22 |
|
|
23 |
const uint16 _bridge_type_price_mod[MAX_BRIDGES] = {
|
|
24 |
80, 112, 144, 168, 185, 192, 224, 232, 248, 240, 255, 380, 510,
|
|
25 |
};
|
|
26 |
|
|
27 |
const uint16 _bridge_speeds[MAX_BRIDGES] = {
|
|
28 |
0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x0A0, 0x0D0, 0x0F0, 0x100, 0x140, 0x200, 0x260
|
|
29 |
};
|
|
30 |
|
|
31 |
const PalSpriteID _bridge_sprites[MAX_BRIDGES] = {
|
|
32 |
0x0A24, 0x31E8A26, 0x0A25, 0x3208A22,
|
|
33 |
0x0A22, 0x3218A22, 0x0A23, 0x31C8A23,
|
|
34 |
0x31E8A23, 0x0A27, 0x0A28, 0x3218A28,
|
|
35 |
0x3238A28,
|
|
36 |
};
|
|
37 |
|
|
38 |
const StringID _bridge_material[MAX_BRIDGES] = {
|
|
39 |
STR_5012_WOODEN,
|
|
40 |
STR_5013_CONCRETE,
|
|
41 |
STR_500F_GIRDER_STEEL,
|
|
42 |
STR_5011_SUSPENSION_CONCRETE,
|
|
43 |
STR_500E_SUSPENSION_STEEL,
|
|
44 |
STR_500E_SUSPENSION_STEEL,
|
|
45 |
STR_5010_CANTILEVER_STEEL,
|
|
46 |
STR_5010_CANTILEVER_STEEL,
|
|
47 |
STR_5010_CANTILEVER_STEEL,
|
|
48 |
STR_500F_GIRDER_STEEL,
|
|
49 |
STR_5014_TUBULAR_STEEL,
|
|
50 |
STR_5014_TUBULAR_STEEL,
|
|
51 |
STR_BRIDGE_TUBULAR_SILICON,
|
|
52 |
};
|
|
53 |
|
|
54 |
// calculate the price factor for building a long bridge.
|
|
55 |
// basically the cost delta is 1,1, 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5, 6,6,6,6,6,6, 7,7,7,7,7,7,7, 8,8,8,8,8,8,8,8,
|
|
56 |
int CalcBridgeLenCostFactor(int x)
|
|
57 |
{
|
|
58 |
int n,r;
|
|
59 |
if (x < 2) return x;
|
|
60 |
x -= 2;
|
|
61 |
for(n=0,r=2;;n++) {
|
|
62 |
if (x <= n) return r + x * n;
|
|
63 |
r += n * n;
|
|
64 |
x -= n;
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
enum {
|
|
69 |
// foundation, whole tile is leveled up (tileh's 7, 11, 13, 14) --> 3 corners raised
|
|
70 |
BRIDGE_FULL_LEVELED_FOUNDATION = 1 << 7 | 1 << 11 | 1 << 13 | 1 << 14,
|
|
71 |
// foundation, tile is partly leveled up (tileh's 1, 2, 4, 8) --> 1 corner raised
|
|
72 |
BRIDGE_PARTLY_LEVELED_FOUNDATION = 1 << 1 | 1 << 2 | 1 << 4 | 1 << 8,
|
|
73 |
// no foundations (X,Y direction) (tileh's 0, 3, 6, 9, 12)
|
|
74 |
BRIDGE_NO_FOUNDATION = 1 << 0 | 1 << 3 | 1 << 6 | 1 << 9 | 1 << 12,
|
|
75 |
};
|
|
76 |
|
|
77 |
/* check if bridge can be built on slope
|
|
78 |
* direction 0 = X-axis, direction 1 = Y-axis
|
|
79 |
* is_start_tile = false <-- end tile
|
|
80 |
* is_start_tile = true <-- start tile
|
|
81 |
*/
|
|
82 |
static uint32 CheckBridgeSlope(uint direction, uint tileh, bool is_start_tile)
|
|
83 |
{
|
|
84 |
if (!(tileh & 0x10)) { // disable building on very steep slopes
|
|
85 |
|
|
86 |
if (is_start_tile) {
|
|
87 |
/* check slope at start tile
|
|
88 |
- no extra cost
|
|
89 |
- direction X: tiles 0,12
|
|
90 |
- direction Y: tiles 0, 9
|
|
91 |
*/
|
|
92 |
if ((direction?0x201:0x1001) & (1 << tileh))
|
|
93 |
return 0;
|
|
94 |
|
|
95 |
// disallow certain start tiles to avoid certain crooked bridges
|
|
96 |
if (tileh == 2)
|
|
97 |
return CMD_ERROR;
|
|
98 |
|
|
99 |
}
|
|
100 |
else {
|
|
101 |
/* check slope at end tile
|
|
102 |
- no extra cost
|
|
103 |
- direction X: tiles 0, 3
|
|
104 |
- direction Y: tiles 0, 6
|
|
105 |
*/
|
|
106 |
if ((direction?0x41:0x9) & (1 << tileh))
|
|
107 |
return 0;
|
|
108 |
|
|
109 |
// disallow certain end tiles to avoid certain crooked bridges
|
|
110 |
if (tileh == 8)
|
|
111 |
return CMD_ERROR;
|
|
112 |
|
|
113 |
}
|
|
114 |
|
|
115 |
/* disallow common start/end tiles to avoid certain crooked bridges e.g.
|
|
116 |
* start-tile: X 2,1 Y 2,4 (2 was disabled before)
|
|
117 |
* end-tile: X 8,4 Y 8,1 (8 was disabled before)
|
|
118 |
*/
|
|
119 |
if ( (tileh == 1 && (is_start_tile != (bool)direction)) ||
|
|
120 |
(tileh == 4 && (is_start_tile == (bool)direction)) )
|
|
121 |
return CMD_ERROR;
|
|
122 |
|
|
123 |
// slope foundations
|
|
124 |
if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << tileh) || BRIDGE_PARTLY_LEVELED_FOUNDATION & (1 << tileh))
|
|
125 |
return _price.terraform;
|
|
126 |
}
|
|
127 |
|
|
128 |
return CMD_ERROR;
|
|
129 |
}
|
|
130 |
|
|
131 |
uint32 GetBridgeLength(TileIndex begin, TileIndex end)
|
|
132 |
{
|
|
133 |
int x1, y1, x2, y2; // coordinates of starting and end tiles
|
|
134 |
x1 = GET_TILE_X(begin);
|
|
135 |
y1 = GET_TILE_Y(begin);
|
|
136 |
x2 = GET_TILE_X(end);
|
|
137 |
y2 = GET_TILE_Y(end);
|
|
138 |
|
|
139 |
return abs((x2 + y2 - x1 - y1)) - 1;
|
|
140 |
}
|
|
141 |
|
|
142 |
bool CheckBridge_Stuff(byte bridge_type, int bridge_len)
|
|
143 |
{
|
|
144 |
int max; // max possible length of a bridge (with patch 100)
|
|
145 |
|
|
146 |
if (_bridge_available_year[bridge_type] > _cur_year)
|
|
147 |
return false;
|
|
148 |
|
|
149 |
max = _bridge_maxlen[bridge_type];
|
|
150 |
if (max >= 16 && _patches.longbridges)
|
|
151 |
max = 100;
|
|
152 |
|
|
153 |
if (bridge_len < _bridge_minlen[bridge_type] || bridge_len > max)
|
|
154 |
return false;
|
|
155 |
|
|
156 |
return true;
|
|
157 |
}
|
|
158 |
|
|
159 |
/* Build a Bridge
|
|
160 |
* x,y - end tile coord
|
|
161 |
* p1 - packed start tile coords (~ dx)
|
|
162 |
* p2&0xFF - bridge type (hi bh)
|
|
163 |
* p2>>8 - rail type. &0x80 means road bridge.
|
|
164 |
*/
|
|
165 |
int32 CmdBuildBridge(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
166 |
{
|
|
167 |
int bridge_type;
|
|
168 |
byte rail_or_road, railtype, m5;
|
|
169 |
int sx,sy;
|
|
170 |
TileInfo ti_start, ti_end, ti; /* OPT: only 2 of those are ever used */
|
|
171 |
int bridge_len, odd_middle_part;
|
|
172 |
uint direction;
|
|
173 |
int i;
|
|
174 |
int32 cost, terraformcost, ret;
|
|
175 |
|
|
176 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
177 |
|
|
178 |
/* unpack parameters */
|
|
179 |
bridge_type = p2 & 0xFF;
|
|
180 |
railtype = (byte)(p2 >> 8);
|
|
181 |
|
|
182 |
// type of bridge
|
|
183 |
if (railtype & 0x80) {
|
|
184 |
railtype = 0;
|
|
185 |
rail_or_road = 2;
|
|
186 |
} else {
|
|
187 |
rail_or_road = 0;
|
|
188 |
}
|
|
189 |
|
|
190 |
sx = GET_TILE_X(p1) * 16;
|
|
191 |
sy = GET_TILE_Y(p1) * 16;
|
|
192 |
|
|
193 |
direction = 0;
|
|
194 |
|
|
195 |
/* check if valid, and make sure that (x,y) are smaller than (sx,sy) */
|
|
196 |
if (x == sx) {
|
|
197 |
if (y == sy)
|
|
198 |
return_cmd_error(STR_5008_CANNOT_START_AND_END_ON);
|
|
199 |
direction = 1;
|
|
200 |
if (y > sy) {
|
|
201 |
intswap(y,sy);
|
|
202 |
intswap(x,sx);
|
|
203 |
}
|
|
204 |
} else if (y == sy) {
|
|
205 |
if (x > sx) {
|
|
206 |
intswap(y,sy);
|
|
207 |
intswap(x,sx);
|
|
208 |
}
|
|
209 |
} else
|
|
210 |
return_cmd_error(STR_500A_START_AND_END_MUST_BE_IN);
|
|
211 |
|
|
212 |
/* retrieve landscape height and ensure it's on land */
|
|
213 |
if (
|
|
214 |
((FindLandscapeHeight(&ti_end, sx, sy),
|
|
215 |
ti_end.type == MP_WATER) && ti_end.map5 == 0) ||
|
|
216 |
((FindLandscapeHeight(&ti_start, x, y),
|
|
217 |
ti_start.type == MP_WATER) && ti_start.map5 == 0))
|
|
218 |
return_cmd_error(STR_02A0_ENDS_OF_BRIDGE_MUST_BOTH);
|
|
219 |
|
|
220 |
if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << ti_start.tileh)) {
|
|
221 |
ti_start.z += 8;
|
|
222 |
ti_start.tileh = 0;
|
|
223 |
}
|
|
224 |
|
|
225 |
if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << ti_end.tileh)) {
|
|
226 |
ti_end.z += 8;
|
|
227 |
ti_end.tileh = 0;
|
|
228 |
}
|
|
229 |
|
|
230 |
if (ti_start.z != ti_end.z)
|
|
231 |
return_cmd_error(STR_5009_LEVEL_LAND_OR_WATER_REQUIRED);
|
|
232 |
|
|
233 |
_error_message = STR_500C;
|
|
234 |
|
|
235 |
/* try and clear the start landscape */
|
|
236 |
if ((ret=DoCommandByTile(ti_start.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR)) == CMD_ERROR)
|
|
237 |
return CMD_ERROR;
|
|
238 |
cost = ret;
|
|
239 |
|
|
240 |
terraformcost = CheckBridgeSlope(direction, ti_start.tileh, true); // true - bridge-start-tile, false - bridge-end-tile
|
|
241 |
|
|
242 |
// the AI and towns are not allowed to use bridges on slopes.
|
|
243 |
if (terraformcost == CMD_ERROR ||
|
|
244 |
(terraformcost && (_is_ai_player || _current_player == OWNER_TOWN || !_patches.build_on_slopes)))
|
|
245 |
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
|
|
246 |
|
|
247 |
cost += terraformcost;
|
|
248 |
|
|
249 |
/* try and clear the end landscape */
|
|
250 |
if ((ret=DoCommandByTile(ti_end.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR)) == CMD_ERROR)
|
|
251 |
return CMD_ERROR;
|
|
252 |
cost += ret;
|
|
253 |
|
|
254 |
terraformcost = CheckBridgeSlope(direction, ti_end.tileh, false); // false - end tile slope check
|
|
255 |
|
|
256 |
// the AI and towns are not allowed to use bridges on slopes.
|
|
257 |
if (terraformcost == CMD_ERROR ||
|
|
258 |
(terraformcost && (_is_ai_player || _current_player == OWNER_TOWN || !_patches.build_on_slopes)))
|
|
259 |
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
|
|
260 |
|
|
261 |
cost += terraformcost;
|
|
262 |
|
|
263 |
/* do the drill? */
|
|
264 |
if (flags & DC_EXEC) {
|
|
265 |
/* build the start tile */
|
|
266 |
ModifyTile(ti_start.tile,
|
|
267 |
MP_SETTYPE(MP_TUNNELBRIDGE) |
|
|
268 |
MP_MAP2 | MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5,
|
|
269 |
(bridge_type << 4), /* map2 */
|
|
270 |
railtype, /* map3_lo */
|
|
271 |
0x80 | direction | rail_or_road /* map5 */
|
|
272 |
);
|
|
273 |
|
|
274 |
/* build the end tile */
|
|
275 |
ModifyTile(ti_end.tile,
|
|
276 |
MP_SETTYPE(MP_TUNNELBRIDGE) |
|
|
277 |
MP_MAP2 | MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5,
|
|
278 |
(bridge_type << 4), /* map2 */
|
|
279 |
railtype, /* map3_lo */
|
|
280 |
0x80 | 0x20 | direction | rail_or_road /* map5 */
|
|
281 |
);
|
|
282 |
}
|
|
283 |
|
|
284 |
/* set various params */
|
|
285 |
bridge_len = ((sx + sy - x - y) >> 4) - 1;
|
|
286 |
|
|
287 |
//position of middle part of the odd bridge (larger than MAX(i) otherwise)
|
|
288 |
odd_middle_part=(bridge_len%2)?(bridge_len/2):bridge_len;
|
|
289 |
|
|
290 |
for(i = 0; i != bridge_len; i++) {
|
|
291 |
if (direction != 0)
|
|
292 |
y+=16;
|
|
293 |
else
|
|
294 |
x+=16;
|
|
295 |
|
|
296 |
FindLandscapeHeight(&ti, x, y);
|
|
297 |
|
|
298 |
_error_message = STR_5009_LEVEL_LAND_OR_WATER_REQUIRED;
|
|
299 |
if (ti.tileh != 0 && ti.z >= ti_start.z)
|
|
300 |
return CMD_ERROR;
|
|
301 |
|
|
302 |
// Find ship below
|
|
303 |
if ( ti.type == MP_WATER && !EnsureNoVehicle(ti.tile) )
|
|
304 |
{
|
|
305 |
_error_message = STR_980E_SHIP_IN_THE_WAY;
|
|
306 |
return CMD_ERROR;
|
|
307 |
}
|
|
308 |
|
|
309 |
if (ti.type == MP_WATER) {
|
|
310 |
if (ti.map5 != 0) goto not_valid_below;
|
|
311 |
m5 = 0xC8;
|
|
312 |
} else if (ti.type == MP_RAILWAY) {
|
|
313 |
if (direction == 0) {
|
|
314 |
if (ti.map5 != 2) goto not_valid_below;
|
|
315 |
} else {
|
|
316 |
if (ti.map5 != 1) goto not_valid_below;
|
|
317 |
}
|
|
318 |
m5 = 0xE0;
|
|
319 |
} else if (ti.type == MP_STREET) {
|
|
320 |
if (direction == 0) {
|
|
321 |
if (ti.map5 != 5) goto not_valid_below;
|
|
322 |
} else {
|
|
323 |
if (ti.map5 != 10) goto not_valid_below;
|
|
324 |
}
|
|
325 |
m5 = 0xE8;
|
|
326 |
} else {
|
|
327 |
not_valid_below:;
|
|
328 |
/* try and clear the middle landscape */
|
|
329 |
if ((ret=DoCommandByTile(ti.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR)) == CMD_ERROR)
|
|
330 |
return CMD_ERROR;
|
|
331 |
cost += ret;
|
|
332 |
m5 = 0xC0;
|
|
333 |
}
|
|
334 |
|
|
335 |
/* do middle part of bridge */
|
|
336 |
if (flags & DC_EXEC) {
|
|
337 |
_map5[ti.tile] = (byte)(m5 | direction | rail_or_road);
|
|
338 |
_map_type_and_height[ti.tile] &= ~0xF0;
|
|
339 |
_map_type_and_height[ti.tile] |= MP_TUNNELBRIDGE << 4;
|
|
340 |
|
|
341 |
//bridges pieces sequence (middle parts)
|
|
342 |
// bridge len 1: 0
|
|
343 |
// bridge len 2: 0 1
|
|
344 |
// bridge len 3: 0 4 1
|
|
345 |
// bridge len 4: 0 2 3 1
|
|
346 |
// bridge len 5: 0 2 5 3 1
|
|
347 |
// bridge len 6: 0 2 3 2 3 1
|
|
348 |
// bridge len 7: 0 2 3 4 2 3 1
|
|
349 |
// #0 - alwats as first, #1 - always as last (if len>1)
|
|
350 |
// #2,#3 are to pair in order
|
|
351 |
// for odd bridges: #5 is going in the bridge middle if on even position, #4 on odd (counting from 0)
|
|
352 |
|
|
353 |
if(i==0) //first tile
|
|
354 |
m5 = 0;
|
|
355 |
else if (i==bridge_len-1) //last tile
|
|
356 |
m5 = 1;
|
|
357 |
else if(i==odd_middle_part) //we are on the middle of odd bridge: #5 on even pos, #4 on odd
|
|
358 |
m5 = 5 - (i%2);
|
|
359 |
else {
|
|
360 |
// generate #2 and #3 in turns [i%2==0], after the middle of odd bridge
|
|
361 |
// this sequence swaps [... XOR (i>odd_middle_part)],
|
|
362 |
// for even bridges XOR does not apply as odd_middle_part==bridge_len
|
|
363 |
m5 = 2 + ((i%2==0)^(i>odd_middle_part));
|
|
364 |
}
|
|
365 |
|
|
366 |
_map2[ti.tile] = (bridge_type << 4) | m5;
|
|
367 |
_map3_lo[ti.tile] &= 0xF;
|
|
368 |
_map3_lo[ti.tile] |= (byte)(railtype << 4);
|
|
369 |
|
|
370 |
MarkTileDirtyByTile(ti.tile);
|
|
371 |
}
|
|
372 |
}
|
|
373 |
|
|
374 |
SetSignalsOnBothDir(ti_start.tile, (direction&1) ? 1 : 0);
|
|
375 |
|
|
376 |
/* for human player that builds the bridge he gets a selection to choose from bridges (DC_QUERY_COST)
|
|
377 |
It's unnecessary to execute this command every time for every bridge. So it is done only
|
|
378 |
and cost is computed in "bridge_gui.c". For AI, Towns this has to be of course calculated
|
|
379 |
*/
|
|
380 |
if (!(flags & DC_QUERY_COST)) {
|
|
381 |
bridge_len += 2; // begin and end tiles/ramps
|
|
382 |
|
|
383 |
if (_current_player < MAX_PLAYERS && IS_HUMAN_PLAYER(_current_player))
|
|
384 |
bridge_len = CalcBridgeLenCostFactor(bridge_len);
|
|
385 |
|
|
386 |
cost += ((bridge_len * _price.build_bridge) * _bridge_type_price_mod[bridge_type]) >> 8;
|
|
387 |
}
|
|
388 |
|
|
389 |
return cost;
|
|
390 |
}
|
|
391 |
|
|
392 |
static bool DoCheckTunnelInWay(uint tile, uint z, uint dir)
|
|
393 |
{
|
|
394 |
TileInfo ti;
|
|
395 |
int delta;
|
|
396 |
|
|
397 |
delta = _tileoffs_by_dir[dir];
|
|
398 |
|
|
399 |
do {
|
|
400 |
tile -= delta;
|
|
401 |
FindLandscapeHeightByTile(&ti, tile);
|
|
402 |
} while (z < ti.z);
|
|
403 |
|
|
404 |
if (z == ti.z && ti.type == MP_TUNNELBRIDGE && (ti.map5&0xF0) == 0 && (ti.map5&3) == dir) {
|
|
405 |
_error_message = STR_5003_ANOTHER_TUNNEL_IN_THE_WAY;
|
|
406 |
return false;
|
|
407 |
}
|
|
408 |
|
|
409 |
return true;
|
|
410 |
}
|
|
411 |
|
|
412 |
bool CheckTunnelInWay(uint tile, int z)
|
|
413 |
{
|
|
414 |
return DoCheckTunnelInWay(tile,z,0) &&
|
|
415 |
DoCheckTunnelInWay(tile,z,1) &&
|
|
416 |
DoCheckTunnelInWay(tile,z,2) &&
|
|
417 |
DoCheckTunnelInWay(tile,z,3);
|
|
418 |
}
|
|
419 |
|
|
420 |
static byte _build_tunnel_bh;
|
|
421 |
static byte _build_tunnel_railtype;
|
|
422 |
|
|
423 |
static int32 DoBuildTunnel(int x, int y, int x2, int y2, uint32 flags, uint exc_tile)
|
|
424 |
{
|
|
425 |
uint end_tile;
|
|
426 |
int direction;
|
|
427 |
int32 cost, ret;
|
|
428 |
TileInfo ti;
|
|
429 |
uint z;
|
|
430 |
|
|
431 |
if ( (uint) x > TILE_X_MAX * 16 - 1 || (uint) y > TILE_X_MAX * 16 - 1)
|
|
432 |
return CMD_ERROR;
|
|
433 |
|
|
434 |
/* check if valid, and make sure that (x,y) is smaller than (x2,y2) */
|
|
435 |
direction = 0;
|
|
436 |
if (x == x2) {
|
|
437 |
if (y == y2)
|
|
438 |
return_cmd_error(STR_5008_CANNOT_START_AND_END_ON);
|
|
439 |
direction++;
|
|
440 |
if (y > y2) {
|
|
441 |
intswap(y,y2);
|
|
442 |
intswap(x,x2);
|
|
443 |
exc_tile|=2;
|
|
444 |
}
|
|
445 |
} else if (y == y2) {
|
|
446 |
if (x > x2) {
|
|
447 |
intswap(y,y2);
|
|
448 |
intswap(x,x2);
|
|
449 |
exc_tile|=2;
|
|
450 |
}
|
|
451 |
} else
|
|
452 |
return_cmd_error(STR_500A_START_AND_END_MUST_BE_IN);
|
|
453 |
|
|
454 |
cost = 0;
|
|
455 |
|
|
456 |
FindLandscapeHeight(&ti, x2, y2);
|
|
457 |
end_tile = ti.tile;
|
|
458 |
z = ti.z;
|
|
459 |
|
|
460 |
if (exc_tile != 3) {
|
|
461 |
if ( (direction ? 9U : 12U) != ti.tileh)
|
|
462 |
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
|
|
463 |
ret = DoCommandByTile(ti.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
464 |
if (ret == CMD_ERROR)
|
|
465 |
return CMD_ERROR;
|
|
466 |
cost += ret;
|
|
467 |
}
|
|
468 |
cost += _price.build_tunnel;
|
|
469 |
|
|
470 |
for(;;) {
|
|
471 |
if (direction) y2-=16; else x2-=16;
|
|
472 |
|
|
473 |
if (x2 == x && y2 == y)
|
|
474 |
break;
|
|
475 |
|
|
476 |
FindLandscapeHeight(&ti, x2, y2);
|
|
477 |
if (ti.z <= z)
|
|
478 |
return_cmd_error(STR_5002);
|
|
479 |
|
|
480 |
if (!_cheats.crossing_tunnels.value && !CheckTunnelInWay(ti.tile, z))
|
|
481 |
return CMD_ERROR;
|
|
482 |
|
|
483 |
cost += _price.build_tunnel;
|
|
484 |
cost += (cost >> 3);
|
|
485 |
|
|
486 |
if (cost >= 400000000)
|
|
487 |
cost = 400000000;
|
|
488 |
}
|
|
489 |
|
|
490 |
FindLandscapeHeight(&ti, x2, y2);
|
|
491 |
if (ti.z != z)
|
|
492 |
return_cmd_error(STR_5004);
|
|
493 |
|
|
494 |
if (exc_tile != 1) {
|
|
495 |
if ( (direction ? 6U : 3U) != ti.tileh)
|
|
496 |
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
|
|
497 |
|
|
498 |
ret = DoCommandByTile(ti.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
499 |
if (ret == CMD_ERROR)
|
|
500 |
return CMD_ERROR;
|
|
501 |
cost += ret;
|
|
502 |
}
|
|
503 |
|
|
504 |
if (flags & DC_EXEC) {
|
|
505 |
ModifyTile(ti.tile,
|
|
506 |
MP_SETTYPE(MP_TUNNELBRIDGE) |
|
|
507 |
MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5,
|
|
508 |
_build_tunnel_railtype, /* map3lo */
|
|
509 |
((_build_tunnel_bh << 1) | 2) - direction /* map5 */
|
|
510 |
);
|
|
511 |
|
|
512 |
ModifyTile(end_tile,
|
|
513 |
MP_SETTYPE(MP_TUNNELBRIDGE) |
|
|
514 |
MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5,
|
|
515 |
_build_tunnel_railtype, /* map3lo */
|
|
516 |
(_build_tunnel_bh << 1) | (direction ? 3:0)/* map5 */
|
|
517 |
);
|
|
518 |
|
|
519 |
UpdateSignalsOnSegment(end_tile, direction?7:1);
|
|
520 |
}
|
|
521 |
|
|
522 |
return cost + _price.build_tunnel;
|
|
523 |
}
|
|
524 |
|
|
525 |
/* Build Tunnel
|
|
526 |
* x,y - start tile coord
|
|
527 |
* p1 - railtype
|
|
528 |
* p2 - ptr to uint that recieves end tile
|
|
529 |
*/
|
|
530 |
int32 CmdBuildTunnel(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
531 |
{
|
|
532 |
TileInfo ti, tiorg;
|
|
533 |
int direction;
|
|
534 |
uint z;
|
|
535 |
static const int8 _build_tunnel_coord_mod[4+1] = { -16, 0, 16, 0, -16 };
|
|
536 |
static const byte _build_tunnel_tileh[4] = {3, 9, 12, 6};
|
|
537 |
uint excavated_tile;
|
|
538 |
|
|
539 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
540 |
|
|
541 |
_build_tunnel_railtype = (byte)(p1 & 0xFF);
|
|
542 |
_build_tunnel_bh = (byte)(p1 >> 8);
|
|
543 |
|
|
544 |
_build_tunnel_endtile = 0;
|
|
545 |
excavated_tile = 0;
|
|
546 |
|
|
547 |
FindLandscapeHeight(&tiorg, x, y);
|
|
548 |
|
|
549 |
if (!EnsureNoVehicle(tiorg.tile))
|
|
550 |
return CMD_ERROR;
|
|
551 |
|
|
552 |
if (!(direction=0, tiorg.tileh==12) &&
|
|
553 |
!(direction++, tiorg.tileh==6) &&
|
|
554 |
!(direction++, tiorg.tileh==3) &&
|
|
555 |
!(direction++, tiorg.tileh==9) )
|
|
556 |
return_cmd_error(STR_500B_SITE_UNSUITABLE_FOR_TUNNEL);
|
|
557 |
|
|
558 |
z = tiorg.z;
|
|
559 |
do {
|
|
560 |
x += _build_tunnel_coord_mod[direction];
|
|
561 |
y += _build_tunnel_coord_mod[direction+1];
|
|
562 |
FindLandscapeHeight(&ti, x, y);
|
|
563 |
} while (z != ti.z);
|
|
564 |
_build_tunnel_endtile = ti.tile;
|
|
565 |
|
|
566 |
|
|
567 |
if (!EnsureNoVehicle(ti.tile))
|
|
568 |
return CMD_ERROR;
|
|
569 |
|
|
570 |
if (ti.tileh != _build_tunnel_tileh[direction]) {
|
|
571 |
if (DoCommandByTile(ti.tile, ti.tileh & ~_build_tunnel_tileh[direction], 0, flags, CMD_TERRAFORM_LAND) == CMD_ERROR)
|
|
572 |
return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND);
|
|
573 |
excavated_tile = 1;
|
|
574 |
}
|
|
575 |
|
|
576 |
if (flags & DC_EXEC && DoBuildTunnel(x,y,tiorg.x,tiorg.y,flags&~DC_EXEC,excavated_tile) == CMD_ERROR)
|
|
577 |
return CMD_ERROR;
|
|
578 |
|
|
579 |
return DoBuildTunnel(x,y,tiorg.x, tiorg.y,flags,excavated_tile);
|
|
580 |
}
|
|
581 |
|
|
582 |
static const byte _updsignals_tunnel_dir[4] = { 5, 7, 1, 3};
|
|
583 |
|
|
584 |
static uint CheckTunnelBusy(uint tile, int *length)
|
|
585 |
{
|
|
586 |
int z = GetTileZ(tile);
|
|
587 |
byte m5 = _map5[tile];
|
|
588 |
int delta = _tileoffs_by_dir[m5 & 3], len = 0;
|
|
589 |
uint starttile = tile;
|
|
590 |
Vehicle *v;
|
|
591 |
|
|
592 |
do { tile += delta; len++; } while (!IS_TILETYPE(tile, MP_TUNNELBRIDGE) || _map5[tile]&0xF0 || (byte)(_map5[tile] ^ 2) != m5 || GetTileZ(tile) != z);
|
|
593 |
|
|
594 |
if ((v=FindVehicleBetween(starttile, tile, z)) != NULL) {
|
|
595 |
_error_message = v->type == VEH_Train ? STR_5000_TRAIN_IN_TUNNEL : STR_5001_ROAD_VEHICLE_IN_TUNNEL;
|
|
596 |
return (uint)-1;
|
|
597 |
}
|
|
598 |
|
|
599 |
if (length) *length = len;
|
|
600 |
return tile;
|
|
601 |
}
|
|
602 |
|
|
603 |
static int32 DoClearTunnel(uint tile, uint32 flags)
|
|
604 |
{
|
|
605 |
Town *t;
|
|
606 |
uint endtile;
|
|
607 |
int length;
|
|
608 |
|
|
609 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
610 |
|
|
611 |
// in scenario editor you can always destroy tunnels
|
|
612 |
if (_game_mode != GM_EDITOR && !CheckTileOwnership(tile)) {
|
|
613 |
if (!(_patches.extra_dynamite || _cheats.magic_bulldozer.value) || _map_owner[tile] != OWNER_TOWN)
|
|
614 |
return CMD_ERROR;
|
|
615 |
}
|
|
616 |
|
|
617 |
endtile = CheckTunnelBusy(tile, &length);
|
|
618 |
if (endtile == (uint)-1) return CMD_ERROR;
|
|
619 |
|
|
620 |
_build_tunnel_endtile = endtile;
|
|
621 |
|
|
622 |
t = ClosestTownFromTile(tile, (uint)-1); //needed for town rating penalty
|
|
623 |
// check if you're allowed to remove the tunnel owned by a town
|
|
624 |
// removal allowal depends on difficulty settings
|
|
625 |
if(_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR ) {
|
|
626 |
if (!CheckforTownRating(tile, flags, t, TUNNELBRIDGE_REMOVE)) {
|
|
627 |
SET_DPARAM16(0, t->index);
|
|
628 |
return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
|
|
629 |
}
|
|
630 |
}
|
|
631 |
|
|
632 |
if (flags & DC_EXEC) {
|
|
633 |
DoClearSquare(tile);
|
|
634 |
DoClearSquare(endtile);
|
|
635 |
UpdateSignalsOnSegment(tile, _updsignals_tunnel_dir[_map5[tile]&3]);
|
|
636 |
UpdateSignalsOnSegment(endtile, _updsignals_tunnel_dir[_map5[endtile]&3]);
|
|
637 |
if (_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR)
|
|
638 |
ChangeTownRating(t, -250, 0);
|
|
639 |
}
|
|
640 |
return _price.clear_tunnel * (length + 1);
|
|
641 |
}
|
|
642 |
|
|
643 |
static uint FindEdgesOfBridge(uint tile, uint *endtile)
|
|
644 |
{
|
|
645 |
int direction = _map5[tile] & 1;
|
|
646 |
uint start;
|
|
647 |
|
|
648 |
// find start of bridge
|
|
649 |
for(;;) {
|
|
650 |
if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xE0) == 0x80)
|
|
651 |
break;
|
|
652 |
tile += direction ? TILE_XY(0,-1) : TILE_XY(-1,0);
|
|
653 |
}
|
|
654 |
|
|
655 |
start = tile;
|
|
656 |
|
|
657 |
// find end of bridge
|
|
658 |
for(;;) {
|
|
659 |
if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xE0) == 0xA0)
|
|
660 |
break;
|
|
661 |
tile += direction ? TILE_XY(0,1) : TILE_XY(1,0);
|
|
662 |
}
|
|
663 |
|
|
664 |
*endtile = tile;
|
|
665 |
|
|
666 |
return start;
|
|
667 |
}
|
|
668 |
|
|
669 |
static int32 DoClearBridge(uint tile, uint32 flags)
|
|
670 |
{
|
|
671 |
uint endtile;
|
|
672 |
Vehicle *v;
|
|
673 |
Town *t;
|
|
674 |
int direction;
|
|
675 |
|
|
676 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
677 |
|
|
678 |
direction = _map5[tile]&1;
|
|
679 |
|
|
680 |
/* delete stuff under the middle part if there's a transport route there..? */
|
|
681 |
if ((_map5[tile] & 0xE0) == 0xE0) {
|
|
682 |
int32 cost;
|
|
683 |
|
|
684 |
// check if we own the tile below the bridge..
|
|
685 |
if (_current_player != OWNER_WATER && (!CheckTileOwnership(tile) || !EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile)) ))
|
|
686 |
return CMD_ERROR;
|
|
687 |
|
|
688 |
cost = (_map5[tile] & 8) ? _price.remove_road * 2 : _price.remove_rail;
|
|
689 |
|
|
690 |
if (flags & DC_EXEC) {
|
|
691 |
_map5[tile] = _map5[tile] & ~0x38;
|
|
692 |
_map_owner[tile] = OWNER_NONE;
|
|
693 |
MarkTileDirtyByTile(tile);
|
|
694 |
}
|
|
695 |
return cost;
|
|
696 |
|
|
697 |
/* delete canal under bridge */
|
|
698 |
} else if(_map5[tile]==0xC8 && GET_TILEHEIGHT(tile)!=0) {
|
|
699 |
int32 cost;
|
|
700 |
|
|
701 |
// check for vehicles under bridge
|
|
702 |
if ( !EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile)) )
|
|
703 |
return CMD_ERROR;
|
|
704 |
cost = _price.clear_water;
|
|
705 |
if (flags & DC_EXEC) {
|
|
706 |
_map5[tile] = _map5[tile] & ~0x38;
|
|
707 |
_map_owner[tile] = OWNER_NONE;
|
|
708 |
MarkTileDirtyByTile(tile);
|
|
709 |
}
|
|
710 |
return cost;
|
|
711 |
}
|
|
712 |
|
|
713 |
tile = FindEdgesOfBridge(tile, &endtile);
|
|
714 |
|
|
715 |
// floods, scenario editor can always destroy bridges
|
|
716 |
if (_current_player != OWNER_WATER && _game_mode != GM_EDITOR && !CheckTileOwnership(tile)) {
|
|
717 |
if (!(_patches.extra_dynamite || _cheats.magic_bulldozer.value) || _map_owner[tile] != OWNER_TOWN)
|
|
718 |
return CMD_ERROR;
|
|
719 |
}
|
|
720 |
|
|
721 |
if (!EnsureNoVehicle(tile) || !EnsureNoVehicle(endtile))
|
|
722 |
return CMD_ERROR;
|
|
723 |
|
|
724 |
/* Make sure there's no vehicle on the bridge
|
|
725 |
Omit tile and endtile, since these are already checked, thus solving the problem
|
|
726 |
of bridges over water, or higher bridges, where z is not increased, eg level bridge
|
|
727 |
*/
|
|
728 |
tile += direction ? TILE_XY(0, 1) : TILE_XY( 1,0);
|
|
729 |
endtile -= direction ? TILE_XY(0, 1) : TILE_XY( 1,0);
|
|
730 |
if ((v=FindVehicleBetween(tile, endtile, GET_TILEHEIGHT(tile) + 8)) != NULL) {
|
|
731 |
VehicleInTheWayErrMsg(v);
|
|
732 |
return CMD_ERROR;
|
|
733 |
}
|
|
734 |
|
|
735 |
/* Put the tiles back to start/end position */
|
|
736 |
tile -= direction ? TILE_XY(0, 1) : TILE_XY( 1,0);
|
|
737 |
endtile += direction ? TILE_XY(0, 1) : TILE_XY( 1,0);
|
|
738 |
|
|
739 |
|
|
740 |
t = ClosestTownFromTile(tile, (uint)-1); //needed for town rating penalty
|
|
741 |
// check if you're allowed to remove the bridge owned by a town.
|
|
742 |
// removal allowal depends on difficulty settings
|
|
743 |
if(_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR) {
|
|
744 |
if (!CheckforTownRating(tile, flags, t, TUNNELBRIDGE_REMOVE))
|
|
745 |
return CMD_ERROR;
|
|
746 |
}
|
|
747 |
|
|
748 |
if (flags & DC_EXEC) {
|
|
749 |
byte m5;
|
|
750 |
uint c = tile;
|
|
751 |
uint16 new_data;
|
|
752 |
|
|
753 |
//checks if the owner is town then decrease town rating by 250 until
|
|
754 |
// you have a "Poor" (0) town rating
|
|
755 |
if (_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR)
|
|
756 |
ChangeTownRating(t, -250, 0);
|
|
757 |
|
|
758 |
do {
|
|
759 |
m5 = _map5[c];
|
|
760 |
|
|
761 |
if (m5 & 0x40) {
|
|
762 |
if (m5 & 0x20) {
|
|
763 |
static const uint16 _new_data_table[] = {0x1002, 0x1001, 0x2005, 0x200A, 0, 0, 0, 0};
|
|
764 |
new_data = _new_data_table[((m5 & 0x18) >> 2) | (m5&1)];
|
|
765 |
} else {
|
|
766 |
if (!(m5 & 0x18)) goto clear_it;
|
|
767 |
new_data = 0x6000;
|
|
768 |
}
|
|
769 |
|
|
770 |
_map_type_and_height[c] &= 0x0F;
|
|
771 |
_map_type_and_height[c] |= new_data >> 8;
|
|
772 |
_map5[c] = (byte)new_data;
|
|
773 |
_map2[c] = 0;
|
|
774 |
|
|
775 |
MarkTileDirtyByTile(c);
|
|
776 |
|
|
777 |
} else {
|
|
778 |
clear_it:;
|
|
779 |
DoClearSquare(c);
|
|
780 |
}
|
|
781 |
c += direction ? TILE_XY(0,1) : TILE_XY(1,0);
|
|
782 |
} while (c <= endtile);
|
|
783 |
|
|
784 |
SetSignalsOnBothDir(tile, direction);
|
|
785 |
SetSignalsOnBothDir(endtile, direction);
|
|
786 |
|
|
787 |
}
|
|
788 |
|
|
789 |
return ((((endtile - tile) >> (direction?8:0))&0xFF)+1) * _price.clear_bridge;
|
|
790 |
}
|
|
791 |
|
|
792 |
static int32 ClearTile_TunnelBridge(uint tile, byte flags) {
|
|
793 |
int32 ret;
|
|
794 |
byte m5 = _map5[tile];
|
|
795 |
|
|
796 |
if ((m5 & 0xF0) == 0) {
|
|
797 |
if (flags & DC_AUTO)
|
|
798 |
return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST);
|
|
799 |
return DoClearTunnel(tile, flags);
|
|
800 |
} else if (m5 & 0x80) {
|
|
801 |
if (flags & DC_AUTO)
|
|
802 |
return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
|
|
803 |
|
|
804 |
ret = DoClearBridge(tile, flags);
|
|
805 |
if (ret == CMD_ERROR)
|
|
806 |
return CMD_ERROR;
|
|
807 |
|
|
808 |
return ret;
|
|
809 |
} else {
|
|
810 |
return CMD_ERROR;
|
|
811 |
}
|
|
812 |
}
|
|
813 |
|
|
814 |
int32 DoConvertTunnelBridgeRail(uint tile, uint totype, bool exec)
|
|
815 |
{
|
|
816 |
uint endtile;
|
|
817 |
int length;
|
|
818 |
Vehicle *v;
|
|
819 |
|
|
820 |
if ((_map5[tile] & 0xFC) == 0x00) {
|
|
821 |
// railway tunnel
|
|
822 |
if (!CheckTileOwnership(tile)) return CMD_ERROR;
|
|
823 |
|
|
824 |
if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR;
|
|
825 |
|
|
826 |
endtile = CheckTunnelBusy(tile, &length);
|
|
827 |
if (endtile == (uint)-1) return CMD_ERROR;
|
|
828 |
|
|
829 |
if (exec) {
|
|
830 |
_map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype;
|
|
831 |
_map3_lo[endtile] = (_map3_lo[endtile] & 0xF0) + totype;
|
|
832 |
MarkTileDirtyByTile(tile);
|
|
833 |
MarkTileDirtyByTile(endtile);
|
|
834 |
}
|
|
835 |
return (length + 1) * (_price.build_rail >> 1);
|
|
836 |
} else if ((_map5[tile] & 0xF8) == 0xE0) {
|
|
837 |
// bridge middle part with rail below
|
|
838 |
// only check for train under bridge
|
|
839 |
if (!CheckTileOwnership(tile) || !EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile)))
|
|
840 |
return CMD_ERROR;
|
|
841 |
|
|
842 |
// tile is already of requested type?
|
|
843 |
if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR;
|
|
844 |
// change type.
|
|
845 |
if (exec) {
|
|
846 |
_map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype;
|
|
847 |
MarkTileDirtyByTile(tile);
|
|
848 |
}
|
|
849 |
return _price.build_rail >> 1;
|
|
850 |
} else if ((_map5[tile]&0xC6) == 0x80) {
|
|
851 |
uint starttile;
|
|
852 |
int32 cost;
|
|
853 |
|
|
854 |
if (!CheckTileOwnership(tile)) return CMD_ERROR;
|
|
855 |
|
|
856 |
// railway bridge
|
|
857 |
starttile = tile = FindEdgesOfBridge(tile, &endtile);
|
|
858 |
// Make sure there's no vehicle on the bridge
|
|
859 |
if ((v=FindVehicleBetween(tile, endtile, 0xff)) != NULL) {
|
|
860 |
VehicleInTheWayErrMsg(v);
|
|
861 |
return CMD_ERROR;
|
|
862 |
}
|
|
863 |
|
|
864 |
if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR;
|
|
865 |
cost = 0;
|
|
866 |
do {
|
|
867 |
if (exec) {
|
|
868 |
if (tile == starttile || tile == endtile) {
|
|
869 |
_map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype;
|
|
870 |
} else {
|
|
871 |
_map3_lo[tile] = (_map3_lo[tile] & 0x0F) + (totype << 4);
|
|
872 |
}
|
|
873 |
MarkTileDirtyByTile(tile);
|
|
874 |
}
|
|
875 |
cost += (_price.build_rail>>1);
|
|
876 |
tile += _map5[tile]&1 ? TILE_XY(0,1) : TILE_XY(1,0);
|
|
877 |
} while (tile <= endtile);
|
|
878 |
|
|
879 |
return cost;
|
|
880 |
} else
|
|
881 |
return CMD_ERROR;
|
|
882 |
}
|
|
883 |
|
|
884 |
|
|
885 |
// fast routine for getting the height of a middle bridge tile. 'tile' MUST be a middle bridge tile.
|
|
886 |
static uint GetBridgeHeight(const TileInfo *ti)
|
|
887 |
{
|
|
888 |
uint delta;
|
|
889 |
TileInfo ti_end;
|
|
890 |
uint tile = ti->tile;
|
|
891 |
uint z_correction = 0;
|
|
892 |
|
|
893 |
// find the end tile of the bridge.
|
|
894 |
delta = (_map5[tile] & 1) ? TILE_XY(0,1) : TILE_XY(1,0);
|
|
895 |
do {
|
|
896 |
assert((_map5[tile] & 0xC0) == 0xC0); // bridge and middle part
|
|
897 |
tile += delta;
|
|
898 |
} while (_map5[tile] & 0x40); // while bridge middle parts
|
|
899 |
|
|
900 |
// if the end of the bridge is on a tileh 7, the z coordinate is 1 tile too low
|
|
901 |
// correct it.
|
|
902 |
FindLandscapeHeightByTile(&ti_end, tile);
|
|
903 |
if (HASBIT(1 << 7, ti_end.tileh))
|
|
904 |
z_correction += 8;
|
|
905 |
|
|
906 |
// return the height there (the height of the NORTH CORNER)
|
|
907 |
return GET_TILEHEIGHT(tile) + z_correction;
|
|
908 |
}
|
|
909 |
|
|
910 |
static const byte _bridge_foundations[2][16] = {
|
|
911 |
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
|
912 |
{1,16,18,3,20,5,0,7,22,0,10,11,12,13,14},
|
|
913 |
{1,15,17,0,19,5,6,7,21,9,10,11, 0,13,14},
|
|
914 |
};
|
|
915 |
|
|
916 |
extern const byte _track_sloped_sprites[14];
|
|
917 |
extern const byte _road_sloped_sprites[14];
|
|
918 |
|
|
919 |
#include "table/tunnel_land.h"
|
|
920 |
|
|
921 |
static void DrawBridgePillars(TileInfo *ti, int x, int y, int z)
|
|
922 |
{
|
|
923 |
const uint32 *b;
|
|
924 |
uint32 image;
|
|
925 |
int piece;
|
|
926 |
|
|
927 |
b = _bridge_poles_table[_map2[ti->tile]>>4];
|
|
928 |
|
|
929 |
// Draw first piece
|
|
930 |
// (necessary for cantilever bridges)
|
|
931 |
image = b[12 + (ti->map5&0x01)];
|
|
932 |
piece = _map2[ti->tile]&0xF;
|
|
933 |
if( image!=0 && piece!=0 )
|
|
934 |
DrawGroundSpriteAt(image, x,y, z);
|
|
935 |
|
|
936 |
image = b[(ti->map5&0x01)*6 + piece];
|
|
937 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) image = (image & 0x3FFF) | 0x03224000;
|
|
938 |
|
|
939 |
if (image != 0) {
|
|
940 |
int back_height, front_height, i=z;
|
|
941 |
const byte *p;
|
|
942 |
|
|
943 |
static const byte _tileh_bits[4][8] = {
|
|
944 |
{2,1,8,4, 16,11,0,9},
|
|
945 |
{1,8,4,2, 11,16,9,0},
|
|
946 |
{4,8,1,2, 16,11,0,9},
|
|
947 |
{2,4,8,1, 11,16,9,0},
|
|
948 |
};
|
|
949 |
|
|
950 |
p = _tileh_bits[(image & 1) * 2 + (ti->map5&0x01)];
|
|
951 |
front_height = ti->z + ((ti->tileh & p[0])?8:0);
|
|
952 |
back_height = ti->z + ((ti->tileh & p[1])?8:0);
|
|
953 |
if (ti->tileh & 0x10) {
|
|
954 |
if (!(ti->tileh & p[2])) front_height += 8;
|
|
955 |
if (!(ti->tileh & p[3])) back_height += 8;
|
|
956 |
}
|
|
957 |
|
|
958 |
for(; z>=front_height || z>=back_height; z=z-8) {
|
|
959 |
if (z>=front_height) AddSortableSpriteToDraw(image, x,y, p[4], p[5], 0x28, z); // front facing pillar
|
|
960 |
if (z>=back_height && z<i-8) AddSortableSpriteToDraw(image, x - p[6], y - p[7], p[4], p[5], 0x28, z); // back facing pillar
|
|
961 |
}
|
|
962 |
}
|
|
963 |
}
|
|
964 |
|
|
965 |
static uint GetBridgeFoundation(uint tileh, byte direction) {
|
|
966 |
int i;
|
|
967 |
// normal level sloped building (7, 11, 13, 14)
|
|
968 |
if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << tileh))
|
|
969 |
return tileh;
|
|
970 |
|
|
971 |
// inclined sloped building
|
|
972 |
if ( ((i=0, tileh == 1) || (i+=2, tileh == 2) || (i+=2, tileh == 4) || (i+=2, tileh == 8)) &&
|
|
973 |
( direction == 0 || (i++, direction == 1)) )
|
|
974 |
return i + 15;
|
|
975 |
|
|
976 |
return 0;
|
|
977 |
}
|
|
978 |
|
|
979 |
static void DrawTile_TunnelBridge(TileInfo *ti)
|
|
980 |
{
|
|
981 |
uint32 image;
|
|
982 |
uint tmp;
|
|
983 |
const uint32 *b;
|
|
984 |
bool ice = _map3_hi[ti->tile] & 0x80;
|
|
985 |
|
|
986 |
// draw tunnel?
|
|
987 |
if ( (byte)(ti->map5&0xF0) == 0) {
|
|
988 |
/* railway type */
|
|
989 |
image = (_map3_lo[ti->tile] & 0xF) * 8;
|
|
990 |
|
|
991 |
/* ice? */
|
|
992 |
if (ice)
|
|
993 |
image += 32;
|
|
994 |
|
|
995 |
image += _draw_tunnel_table_1[(ti->map5 >> 2) & 0x3];
|
|
996 |
image += (ti->map5 & 3) << 1;
|
|
997 |
DrawGroundSprite(image);
|
|
998 |
|
|
999 |
AddSortableSpriteToDraw(image+1, ti->x + 15, ti->y + 15, 1, 1, 8, (byte)ti->z);
|
|
1000 |
// draw bridge?
|
|
1001 |
} else if ((byte)ti->map5 & 0x80) {
|
|
1002 |
// get type of track on the bridge.
|
|
1003 |
tmp = _map3_lo[ti->tile];
|
|
1004 |
if (ti->map5 & 0x40) tmp >>= 4;
|
|
1005 |
tmp &= 0xF;
|
|
1006 |
|
|
1007 |
// 0 = rail bridge
|
|
1008 |
// 1 = road bridge
|
|
1009 |
// 2 = monorail bridge
|
|
1010 |
// 3 = maglev bridge
|
|
1011 |
|
|
1012 |
// add direction and fix stuff.
|
|
1013 |
if (tmp != 0) tmp++;
|
|
1014 |
tmp = (ti->map5&3) + (tmp*2);
|
|
1015 |
|
|
1016 |
if (!(ti->map5 & 0x40)) { // bridge ramps
|
|
1017 |
|
|
1018 |
if (!(BRIDGE_NO_FOUNDATION & (1 << ti->tileh))) { // no foundations for 0, 3, 6, 9, 12
|
|
1019 |
int f = GetBridgeFoundation(ti->tileh, ti->map5 & 0x1); // pass direction
|
|
1020 |
if (f) DrawFoundation(ti, f);
|
|
1021 |
|
|
1022 |
// default sloped sprites..
|
|
1023 |
if (ti->tileh != 0) image = _track_sloped_sprites[ti->tileh - 1] + 0x3F3;
|
|
1024 |
}
|
|
1025 |
|
|
1026 |
// bridge ending.
|
|
1027 |
b = _bridge_sprite_table[_map2[ti->tile]>>4][6];
|
|
1028 |
b += (tmp&(3<<1))*4; /* actually ((tmp>>2)&3)*8 */
|
|
1029 |
b += (tmp&1); // direction
|
|
1030 |
if (ti->tileh == 0) b += 4; // sloped "entrance" ?
|
|
1031 |
if (ti->map5 & 0x20) b += 2; // which side
|
|
1032 |
|
|
1033 |
image = *b;
|
|
1034 |
|
|
1035 |
if (!ice) {
|
|
1036 |
DrawClearLandTile(ti, 3);
|
|
1037 |
} else {
|
|
1038 |
DrawGroundSprite(0x11C6 + _tileh_to_sprite[ti->tileh]);
|
|
1039 |
}
|
|
1040 |
|
|
1041 |
// draw ramp
|
|
1042 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) image = (image & 0x3FFF) | 0x03224000;
|
|
1043 |
AddSortableSpriteToDraw(image, ti->x, ti->y, 16, 16, 7, ti->z);
|
|
1044 |
} else {
|
|
1045 |
// bridge middle part.
|
|
1046 |
uint z;
|
|
1047 |
int x,y;
|
|
1048 |
|
|
1049 |
image = (ti->map5 >> 3) & 3; // type of stuff under bridge (only defined for 0,1)
|
|
1050 |
assert(image <= 1);
|
|
1051 |
|
|
1052 |
if (!(ti->map5 & 0x20)) {
|
|
1053 |
// draw land under bridge
|
|
1054 |
if (ice) image += 2; // ice too?
|
|
1055 |
DrawGroundSprite(_bridge_land_below[image] + _tileh_to_sprite[ti->tileh]);
|
|
1056 |
|
|
1057 |
// draw canal water?
|
|
1058 |
if (ti->map5 & 8 && ti->z != 0) DrawCanalWater(ti->tile);
|
|
1059 |
} else {
|
|
1060 |
// draw transport route under bridge
|
|
1061 |
|
|
1062 |
// draw foundation?
|
|
1063 |
if (ti->tileh) {
|
|
1064 |
int f = _bridge_foundations[ti->map5&1][ti->tileh];
|
|
1065 |
if (f) DrawFoundation(ti, f);
|
|
1066 |
}
|
|
1067 |
|
|
1068 |
if (!(image&1)) {
|
|
1069 |
// railway
|
|
1070 |
image = 0x3F3 + (ti->map5 & 1);
|
|
1071 |
if (ti->tileh != 0) image = _track_sloped_sprites[ti->tileh - 1] + 0x3F3;
|
|
1072 |
image += (_map3_lo[ti->tile] & 0xF) * TRACKTYPE_SPRITE_PITCH;
|
|
1073 |
if (ice) image += 26; // ice?
|
|
1074 |
} else {
|
|
1075 |
// road
|
|
1076 |
image = 1332 + (ti->map5 & 1);
|
|
1077 |
if (ti->tileh != 0) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
|
|
1078 |
if (ice) image += 19; // ice?
|
|
1079 |
}
|
|
1080 |
DrawGroundSprite(image);
|
|
1081 |
}
|
|
1082 |
// get bridge sprites
|
|
1083 |
b = _bridge_sprite_table[_map2[ti->tile]>>4][_map2[ti->tile]&0xF] + tmp * 4;
|
|
1084 |
|
|
1085 |
z = GetBridgeHeight(ti) + 5;
|
|
1086 |
|
|
1087 |
// draw rail
|
|
1088 |
image = b[0];
|
|
1089 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) image = (image & 0x3FFF) | 0x03224000;
|
|
1090 |
AddSortableSpriteToDraw(image, ti->x, ti->y, (ti->map5&1)?11:16, (ti->map5&1)?16:11, 1, z);
|
|
1091 |
|
|
1092 |
x = ti->x;
|
|
1093 |
y = ti->y;
|
|
1094 |
image = b[1];
|
|
1095 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) image = (image & 0x3FFF) | 0x03224000;
|
|
1096 |
|
|
1097 |
// draw roof
|
|
1098 |
if (ti->map5&1) {
|
|
1099 |
x += 12;
|
|
1100 |
if (image&0x3FFF) AddSortableSpriteToDraw(image, x,y, 1, 16, 0x28, z);
|
|
1101 |
} else {
|
|
1102 |
y += 12;
|
|
1103 |
if (image&0x3FFF) AddSortableSpriteToDraw(image, x,y, 16, 1, 0x28, z);
|
|
1104 |
}
|
|
1105 |
|
|
1106 |
if (ti->z + 5 == z ) {
|
|
1107 |
// draw poles below for small bridges
|
|
1108 |
image = b[2];
|
|
1109 |
if (image) {
|
|
1110 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) image = (image & 0x3FFF) | 0x03224000;
|
|
1111 |
DrawGroundSpriteAt(image, x, y, z);
|
|
1112 |
}
|
|
1113 |
} else if (_patches.bridge_pillars) {
|
|
1114 |
// draw pillars below for high bridges
|
|
1115 |
DrawBridgePillars(ti, x, y, z);
|
|
1116 |
}
|
|
1117 |
}
|
|
1118 |
}
|
|
1119 |
}
|
|
1120 |
|
|
1121 |
static uint GetSlopeZ_TunnelBridge(TileInfo *ti) {
|
|
1122 |
uint z = ti->z;
|
|
1123 |
uint x = ti->x & 0xF;
|
|
1124 |
uint y = ti->y & 0xF;
|
|
1125 |
|
|
1126 |
// swap directions if Y tunnel/bridge to let the code handle the X case only.
|
|
1127 |
if (ti->map5 & 1) intswap(x,y);
|
|
1128 |
|
|
1129 |
// to the side of the tunnel/bridge?
|
|
1130 |
if (IS_INT_INSIDE(y, 5, 10+1)) {
|
|
1131 |
// tunnel?
|
|
1132 |
if ( (ti->map5 & 0xF0) == 0)
|
|
1133 |
return z;
|
|
1134 |
|
|
1135 |
// bridge?
|
|
1136 |
if ( ti->map5 & 0x80 ) {
|
|
1137 |
// bridge ending?
|
|
1138 |
if (!(ti->map5 & 0x40)) {
|
|
1139 |
if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << ti->tileh)) // 7, 11, 13, 14
|
|
1140 |
z += 8;
|
|
1141 |
|
|
1142 |
// no ramp for bridge ending
|
|
1143 |
if ((BRIDGE_PARTLY_LEVELED_FOUNDATION & (1 << ti->tileh) || BRIDGE_NO_FOUNDATION & (1 << ti->tileh)) && ti->tileh != 0) {
|
|
1144 |
return z + 8;
|
|
1145 |
|
|
1146 |
} else if (!(ti->map5 & 0x20)) { // northern / southern ending
|
|
1147 |
// ramp
|
|
1148 |
return (z + (x>>1) + 1);
|
|
1149 |
} else {
|
|
1150 |
// ramp in opposite dir
|
|
1151 |
return (z + ((x^0xF)>>1));
|
|
1152 |
}
|
|
1153 |
|
|
1154 |
// bridge middle part
|
|
1155 |
} else {
|
|
1156 |
// build on slopes?
|
|
1157 |
if (ti->tileh) z+=8;
|
|
1158 |
|
|
1159 |
// keep the same elevation because we're on the bridge?
|
|
1160 |
if (_get_z_hint >= z + 8)
|
|
1161 |
return _get_z_hint;
|
|
1162 |
|
|
1163 |
// actually on the bridge, but not yet in the shared area.
|
|
1164 |
if (!IS_INT_INSIDE(x, 5, 10+1))
|
|
1165 |
return GetBridgeHeight(ti) + 8;
|
|
1166 |
|
|
1167 |
// in the shared area, assume that we're below the bridge, cause otherwise the hint would've caught it.
|
|
1168 |
// if rail or road below then it means it's possibly build on slope below the bridge.
|
|
1169 |
if (ti->map5 & 0x20) {
|
|
1170 |
uint f = _bridge_foundations[ti->map5&1][ti->tileh];
|
|
1171 |
// make sure that the slope is not inclined foundation
|
|
1172 |
if (IS_BYTE_INSIDE(f, 1, 15)) return z;
|
|
1173 |
|
|
1174 |
// change foundation type?
|
|
1175 |
if (f) ti->tileh = _inclined_tileh[f - 15];
|
|
1176 |
}
|
|
1177 |
|
|
1178 |
// no transport route, fallback to default
|
|
1179 |
}
|
|
1180 |
}
|
|
1181 |
} else {
|
|
1182 |
// if it's a bridge middle with transport route below, then we need to compensate for build on slopes
|
|
1183 |
if ( (ti->map5 & (0x80 + 0x40 + 0x20)) == (0x80 + 0x40 + 0x20)) {
|
|
1184 |
uint f;
|
|
1185 |
if (ti->tileh) z += 8;
|
|
1186 |
f = _bridge_foundations[ti->map5&1][ti->tileh];
|
|
1187 |
if (IS_BYTE_INSIDE(f, 1, 15)) return z;
|
|
1188 |
if (f) ti->tileh = _inclined_tileh[f - 15];
|
|
1189 |
}
|
|
1190 |
}
|
|
1191 |
|
|
1192 |
// default case
|
|
1193 |
z = ti->z;
|
|
1194 |
return GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + z;
|
|
1195 |
}
|
|
1196 |
|
|
1197 |
static void GetAcceptedCargo_TunnelBridge(uint tile, AcceptedCargo *ac)
|
|
1198 |
{
|
|
1199 |
/* not used */
|
|
1200 |
}
|
|
1201 |
|
|
1202 |
static const StringID _bridge_tile_str[(MAX_BRIDGES + 3) + (MAX_BRIDGES + 3)] = {
|
|
1203 |
STR_501F_WOODEN_RAIL_BRIDGE,
|
|
1204 |
STR_5020_CONCRETE_RAIL_BRIDGE,
|
|
1205 |
STR_501C_STEEL_GIRDER_RAIL_BRIDGE,
|
|
1206 |
STR_501E_REINFORCED_CONCRETE_SUSPENSION,
|
|
1207 |
STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE,
|
|
1208 |
STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE,
|
|
1209 |
STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE,
|
|
1210 |
STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE,
|
|
1211 |
STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE,
|
|
1212 |
STR_501C_STEEL_GIRDER_RAIL_BRIDGE,
|
|
1213 |
STR_5027_TUBULAR_RAIL_BRIDGE,
|
|
1214 |
STR_5027_TUBULAR_RAIL_BRIDGE,
|
|
1215 |
STR_5027_TUBULAR_RAIL_BRIDGE,
|
|
1216 |
0,0,0,
|
|
1217 |
|
|
1218 |
STR_5025_WOODEN_ROAD_BRIDGE,
|
|
1219 |
STR_5026_CONCRETE_ROAD_BRIDGE,
|
|
1220 |
STR_5022_STEEL_GIRDER_ROAD_BRIDGE,
|
|
1221 |
STR_5024_REINFORCED_CONCRETE_SUSPENSION,
|
|
1222 |
STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE,
|
|
1223 |
STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE,
|
|
1224 |
STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE,
|
|
1225 |
STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE,
|
|
1226 |
STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE,
|
|
1227 |
STR_5022_STEEL_GIRDER_ROAD_BRIDGE,
|
|
1228 |
STR_5028_TUBULAR_ROAD_BRIDGE,
|
|
1229 |
STR_5028_TUBULAR_ROAD_BRIDGE,
|
|
1230 |
STR_5028_TUBULAR_ROAD_BRIDGE,
|
|
1231 |
0,0,0,
|
|
1232 |
};
|
|
1233 |
|
|
1234 |
static void GetTileDesc_TunnelBridge(uint tile, TileDesc *td)
|
|
1235 |
{
|
|
1236 |
int delta;
|
|
1237 |
|
|
1238 |
if ((_map5[tile] & 0x80) == 0) {
|
|
1239 |
td->str = STR_5017_RAILROAD_TUNNEL + ((_map5[tile] >> 2) & 3);
|
|
1240 |
} else {
|
|
1241 |
td->str = _bridge_tile_str[ (_map2[tile] >> 4) + (((_map5[tile]>>1)&3)<<4) ];
|
|
1242 |
|
|
1243 |
/* scan to the end of the bridge, that's where the owner is stored */
|
|
1244 |
if (_map5[tile] & 0x40) {
|
|
1245 |
delta = _map5[tile] & 1 ? TILE_XY(0,-1) : TILE_XY(-1,0);
|
|
1246 |
do tile += delta; while (_map5[tile] & 0x40);
|
|
1247 |
}
|
|
1248 |
}
|
|
1249 |
td->owner = _map_owner[tile];
|
|
1250 |
}
|
|
1251 |
|
|
1252 |
|
|
1253 |
static void AnimateTile_TunnelBridge(uint tile)
|
|
1254 |
{
|
|
1255 |
/* not used */
|
|
1256 |
}
|
|
1257 |
|
|
1258 |
static void TileLoop_TunnelBridge(uint tile)
|
|
1259 |
{
|
|
1260 |
if (_opt.landscape == LT_HILLY) {
|
|
1261 |
if ( GetTileZ(tile) > _opt.snow_line) {
|
|
1262 |
if (!(_map3_hi[tile] & 0x80)) {
|
|
1263 |
_map3_hi[tile] |= 0x80;
|
|
1264 |
MarkTileDirtyByTile(tile);
|
|
1265 |
}
|
|
1266 |
} else {
|
|
1267 |
if (_map3_hi[tile] & 0x80) {
|
|
1268 |
_map3_hi[tile] &= ~0x80;
|
|
1269 |
MarkTileDirtyByTile(tile);
|
|
1270 |
}
|
|
1271 |
}
|
|
1272 |
} else if (_opt.landscape == LT_DESERT) {
|
|
1273 |
if (GetMapExtraBits(tile) == 1 && !(_map3_hi[tile]&0x80)) {
|
|
1274 |
_map3_hi[tile] |= 0x80;
|
|
1275 |
MarkTileDirtyByTile(tile);
|
|
1276 |
}
|
|
1277 |
}
|
|
1278 |
|
|
1279 |
// if it's a bridge with water below, call tileloop_water on it.
|
|
1280 |
if ((_map5[tile] & 0xF8) == 0xC8) TileLoop_Water(tile);
|
|
1281 |
}
|
|
1282 |
|
|
1283 |
static void ClickTile_TunnelBridge(uint tile)
|
|
1284 |
{
|
|
1285 |
/* not used */
|
|
1286 |
}
|
|
1287 |
|
|
1288 |
|
|
1289 |
static uint32 GetTileTrackStatus_TunnelBridge(uint tile, int mode)
|
|
1290 |
{
|
|
1291 |
uint32 result;
|
|
1292 |
byte t, m5 = _map5[tile];
|
|
1293 |
|
|
1294 |
if ((m5 & 0xF0) == 0) {
|
|
1295 |
if (((m5 & 0xC) >> 1) == mode) { /* XXX: possible problem when switching mode constants */
|
|
1296 |
return m5&1 ? 0x202 : 0x101;
|
|
1297 |
}
|
|
1298 |
} else if (m5 & 0x80) {
|
|
1299 |
result = 0;
|
|
1300 |
if ((m5 & 6) == mode) {
|
|
1301 |
result = m5&1 ? 0x202 : 0x101;
|
|
1302 |
}
|
|
1303 |
if (m5 & 0x40) {
|
|
1304 |
t = m5;
|
|
1305 |
if (!(t & 0x20)) {
|
|
1306 |
if ((t &= 0x18) != 8)
|
|
1307 |
return result;
|
|
1308 |
t = (t&0xE7) | 0x30;
|
|
1309 |
}
|
|
1310 |
|
|
1311 |
if ((t & 0x18) >> 2 != mode)
|
|
1312 |
return result;
|
|
1313 |
|
|
1314 |
result ^= m5&1 ? 0x101 : 0x202;
|
|
1315 |
}
|
|
1316 |
return result;
|
|
1317 |
}
|
|
1318 |
return 0;
|
|
1319 |
}
|
|
1320 |
|
|
1321 |
static void ChangeTileOwner_TunnelBridge(uint tile, byte old_player, byte new_player)
|
|
1322 |
{
|
|
1323 |
if (_map_owner[tile] != old_player)
|
|
1324 |
return;
|
|
1325 |
|
|
1326 |
if (new_player != 255) {
|
|
1327 |
_map_owner[tile] = new_player;
|
|
1328 |
} else {
|
|
1329 |
if((_map5[tile] & 0xC0)==0xC0) {
|
|
1330 |
// the stuff BELOW the middle part is owned by the deleted player.
|
|
1331 |
if (!(_map5[tile] & (1 << 4 | 1 << 3))) {
|
|
1332 |
// convert railway into grass.
|
|
1333 |
_map5[tile] &= ~(1 << 5 | 1 << 4 | 1 << 3); // no transport route under bridge anymore..
|
|
1334 |
} else {
|
|
1335 |
// for road, change the owner of the road to local authority
|
|
1336 |
_map_owner[tile] = OWNER_NONE;
|
|
1337 |
}
|
|
1338 |
} else {
|
|
1339 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
1340 |
}
|
|
1341 |
}
|
|
1342 |
}
|
|
1343 |
|
|
1344 |
|
|
1345 |
static const byte _tunnel_fractcoord_1[4] = {0x8E,0x18,0x81,0xE8};
|
|
1346 |
static const byte _tunnel_fractcoord_2[4] = {0x81,0x98,0x87,0x38};
|
|
1347 |
static const byte _tunnel_fractcoord_3[4] = {0x82,0x88,0x86,0x48};
|
|
1348 |
static const byte _exit_tunnel_track[4] = {1,2,1,2};
|
|
1349 |
|
|
1350 |
static const byte _road_exit_tunnel_state[4] = {8, 9, 0, 1};
|
|
1351 |
static const byte _road_exit_tunnel_frame[4] = {2, 7, 9, 4};
|
|
1352 |
|
|
1353 |
static const byte _tunnel_fractcoord_4[4] = {0x52, 0x85, 0x98, 0x29};
|
|
1354 |
static const byte _tunnel_fractcoord_5[4] = {0x92, 0x89, 0x58, 0x25};
|
|
1355 |
static const byte _tunnel_fractcoord_6[4] = {0x92, 0x89, 0x56, 0x45};
|
|
1356 |
static const byte _tunnel_fractcoord_7[4] = {0x52, 0x85, 0x96, 0x49};
|
|
1357 |
|
|
1358 |
static uint32 VehicleEnter_TunnelBridge(Vehicle *v, uint tile, int x, int y)
|
|
1359 |
{
|
|
1360 |
int z;
|
|
1361 |
int dir, vdir;
|
|
1362 |
byte fc;
|
|
1363 |
int h;
|
|
1364 |
|
|
1365 |
if ((_map5[tile] & 0xF0) == 0) {
|
|
1366 |
z = GetSlopeZ(x, y) - v->z_pos;
|
|
1367 |
if (myabs(z) > 2)
|
|
1368 |
return 8;
|
|
1369 |
|
|
1370 |
if (v->type == VEH_Train) {
|
|
1371 |
fc = (x&0xF)+(y<<4);
|
|
1372 |
|
|
1373 |
dir = _map5[tile] & 3;
|
|
1374 |
vdir = v->direction >> 1;
|
|
1375 |
|
|
1376 |
if (v->u.rail.track != 0x40 && dir == vdir) {
|
|
1377 |
if (v->subtype == 0 && fc == _tunnel_fractcoord_1[dir]) {
|
|
1378 |
if (v->spritenum < 4)
|
|
1379 |
SndPlayVehicleFx(3, v);
|
|
1380 |
return 0;
|
|
1381 |
}
|
|
1382 |
if (fc == _tunnel_fractcoord_2[dir]) {
|
|
1383 |
v->tile = tile;
|
|
1384 |
v->u.rail.track = 0x40;
|
|
1385 |
v->vehstatus |= VS_HIDDEN;
|
|
1386 |
return 4;
|
|
1387 |
}
|
|
1388 |
}
|
|
1389 |
|
|
1390 |
if (dir == (vdir^2) && fc == _tunnel_fractcoord_3[dir] && z == 0) {
|
22
|
1391 |
/* We're at the tunnel exit ?? */
|
0
|
1392 |
v->tile = tile;
|
|
1393 |
v->u.rail.track = _exit_tunnel_track[dir];
|
|
1394 |
v->vehstatus &= ~VS_HIDDEN;
|
|
1395 |
return 4;
|
|
1396 |
}
|
|
1397 |
} else if (v->type == VEH_Road) {
|
|
1398 |
fc = (x&0xF)+(y<<4);
|
|
1399 |
dir = _map5[tile] & 3;
|
|
1400 |
vdir = v->direction >> 1;
|
|
1401 |
|
|
1402 |
// Enter tunnel?
|
|
1403 |
if (v->u.road.state != 0xFF && dir == vdir) {
|
|
1404 |
if (fc == _tunnel_fractcoord_4[dir] ||
|
|
1405 |
fc == _tunnel_fractcoord_5[dir]) {
|
|
1406 |
|
|
1407 |
v->tile = tile;
|
|
1408 |
v->u.road.state = 0xFF;
|
|
1409 |
v->vehstatus |= VS_HIDDEN;
|
|
1410 |
return 4;
|
|
1411 |
} else {
|
|
1412 |
return 0;
|
|
1413 |
}
|
|
1414 |
}
|
|
1415 |
|
|
1416 |
if (dir == (vdir^2) && (
|
22
|
1417 |
/* We're at the tunnel exit ?? */
|
0
|
1418 |
fc == _tunnel_fractcoord_6[dir] ||
|
|
1419 |
fc == _tunnel_fractcoord_7[dir]) &&
|
|
1420 |
z == 0) {
|
|
1421 |
v->tile = tile;
|
|
1422 |
v->u.road.state = _road_exit_tunnel_state[dir];
|
|
1423 |
v->u.road.frame = _road_exit_tunnel_frame[dir];
|
|
1424 |
v->vehstatus &= ~VS_HIDDEN;
|
|
1425 |
return 4;
|
|
1426 |
}
|
|
1427 |
}
|
|
1428 |
} else if (_map5[tile] & 0x80) {
|
|
1429 |
if (v->type == VEH_Road || (v->type == VEH_Train && v->subtype == 0)) {
|
|
1430 |
if (GetTileSlope(tile, &h) != 0 || myabs(h - v->z_pos) > 2) {
|
|
1431 |
/* modify speed of vehicle */
|
|
1432 |
uint16 spd = _bridge_speeds[_map2[tile] >> 4];
|
|
1433 |
if (v->type == VEH_Road) spd<<=1;
|
|
1434 |
if (spd < v->cur_speed)
|
|
1435 |
v->cur_speed = spd;
|
|
1436 |
}
|
|
1437 |
}
|
|
1438 |
}
|
|
1439 |
return 0;
|
|
1440 |
}
|
|
1441 |
|
|
1442 |
uint GetVehicleOutOfTunnelTile(Vehicle *v)
|
|
1443 |
{
|
|
1444 |
uint tile = v->tile;
|
|
1445 |
int delta_tile;
|
|
1446 |
byte z;
|
|
1447 |
|
|
1448 |
/* locate either ending of the tunnel */
|
|
1449 |
delta_tile = (v->direction&2) ? TILE_XY(0,1) : TILE_XY(1,0);
|
|
1450 |
z = v->z_pos;
|
|
1451 |
for(;;) {
|
|
1452 |
TileInfo ti;
|
|
1453 |
FindLandscapeHeightByTile(&ti, tile);
|
|
1454 |
|
|
1455 |
if (ti.type == MP_TUNNELBRIDGE && (ti.map5 & 0xF0)==0 && (byte)ti.z == z)
|
|
1456 |
break;
|
|
1457 |
|
|
1458 |
tile += delta_tile;
|
|
1459 |
}
|
|
1460 |
return tile;
|
|
1461 |
}
|
|
1462 |
|
|
1463 |
const TileTypeProcs _tile_type_tunnelbridge_procs = {
|
|
1464 |
DrawTile_TunnelBridge, /* draw_tile_proc */
|
|
1465 |
GetSlopeZ_TunnelBridge, /* get_slope_z_proc */
|
|
1466 |
ClearTile_TunnelBridge, /* clear_tile_proc */
|
|
1467 |
GetAcceptedCargo_TunnelBridge, /* get_accepted_cargo_proc */
|
|
1468 |
GetTileDesc_TunnelBridge, /* get_tile_desc_proc */
|
|
1469 |
GetTileTrackStatus_TunnelBridge,/* get_tile_track_status_proc */
|
|
1470 |
ClickTile_TunnelBridge, /* click_tile_proc */
|
|
1471 |
AnimateTile_TunnelBridge, /* animate_tile_proc */
|
|
1472 |
TileLoop_TunnelBridge, /* tile_loop_clear */
|
|
1473 |
ChangeTileOwner_TunnelBridge, /* change_tile_owner_clear */
|
|
1474 |
NULL, /* get_produced_cargo_proc */
|
|
1475 |
VehicleEnter_TunnelBridge, /* vehicle_enter_tile_proc */
|
|
1476 |
NULL, /* vehicle_leave_tile_proc */
|
|
1477 |
};
|