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