author | truelight |
Sat, 11 Sep 2004 09:40:19 +0000 | |
changeset 200 | 03b8104d1479 |
parent 193 | 0a7025304867 |
child 203 | d16d6e91bff8 |
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" |
|
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] = { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
20 |
16, 2, 5, 10, 16, 16, 7, 8, 9, 2, 16, 32, 32, |
0 | 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, |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
34 |
0x31E8A23, 0x0A27, 0x0A28, 0x3218A28, |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
35 |
0x3238A28, |
0 | 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 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
91 |
*/ |
0 | 92 |
if ((direction?0x201:0x1001) & (1 << tileh)) |
93 |
return 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
94 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
95 |
// disallow certain start tiles to avoid certain crooked bridges |
0 | 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 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
109 |
// disallow certain end tiles to avoid certain crooked bridges |
0 | 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 |
*/ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
119 |
if ( (tileh == 1 && (is_start_tile != (bool)direction)) || |
0 | 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); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
138 |
|
0 | 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 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
159 |
/* Build a Bridge |
0 | 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); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
177 |
|
0 | 178 |
/* unpack parameters */ |
179 |
bridge_type = p2 & 0xFF; |
|
180 |
railtype = (byte)(p2 >> 8); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
181 |
|
0 | 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 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
241 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
39
diff
changeset
|
242 |
// 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
|
243 |
if (terraformcost == CMD_ERROR || |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
39
diff
changeset
|
244 |
(terraformcost && ((!_patches.ainew_active && _is_ai_player) || _current_player == OWNER_TOWN || !_patches.build_on_slopes))) |
0 | 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; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
253 |
|
0 | 254 |
terraformcost = CheckBridgeSlope(direction, ti_end.tileh, false); // false - end tile slope check |
255 |
||
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
39
diff
changeset
|
256 |
// 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
|
257 |
if (terraformcost == CMD_ERROR || |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
39
diff
changeset
|
258 |
(terraformcost && ((!_patches.ainew_active && _is_ai_player) || _current_player == OWNER_TOWN || !_patches.build_on_slopes))) |
0 | 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 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
302 |
// Find ship below |
0 | 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) |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
342 |
// bridge len 1: 0 |
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 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
349 |
// #0 - alwats as first, #1 - always as last (if len>1) |
0 | 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 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
354 |
m5 = 0; |
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 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
361 |
// this sequence swaps [... XOR (i>odd_middle_part)], |
0 | 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 |
*/ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
380 |
if (!(flags & DC_QUERY_COST)) { |
0 | 381 |
bridge_len += 2; // begin and end tiles/ramps |
382 |
||
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
39
diff
changeset
|
383 |
if (_current_player < MAX_PLAYERS && !(_is_ai_player && !_patches.ainew_active)) |
0 | 384 |
bridge_len = CalcBridgeLenCostFactor(bridge_len); |
385 |
||
200
03b8104d1479
(svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents:
193
diff
changeset
|
386 |
cost += (((int64)bridge_len * (int64)_price.build_bridge) * (int64)_bridge_type_price_mod[bridge_type]) >> 8; |
0 | 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; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
578 |
|
0 | 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 |
||
98 | 584 |
uint CheckTunnelBusy(uint tile, int *length) |
0 | 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; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
591 |
|
0 | 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 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
598 |
|
0 | 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; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
621 |
|
0 | 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) { |
|
38
d20549a6ea13
(svn r39) -Fix [1008605] Signals not updated after ClearTunnel Bug [985920] (TrueLight)
darkvater
parents:
22
diff
changeset
|
633 |
// 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
|
634 |
// 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
|
635 |
byte tile_dir = _map5[tile]&3; |
d20549a6ea13
(svn r39) -Fix [1008605] Signals not updated after ClearTunnel Bug [985920] (TrueLight)
darkvater
parents:
22
diff
changeset
|
636 |
byte endtile_dir = _map5[endtile]&3; |
0 | 637 |
DoClearSquare(tile); |
638 |
DoClearSquare(endtile); |
|
38
d20549a6ea13
(svn r39) -Fix [1008605] Signals not updated after ClearTunnel Bug [985920] (TrueLight)
darkvater
parents:
22
diff
changeset
|
639 |
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
|
640 |
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
|
641 |
if (_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR) |
0 | 642 |
ChangeTownRating(t, -250, 0); |
643 |
} |
|
644 |
return _price.clear_tunnel * (length + 1); |
|
645 |
} |
|
646 |
||
647 |
static uint FindEdgesOfBridge(uint tile, uint *endtile) |
|
648 |
{ |
|
649 |
int direction = _map5[tile] & 1; |
|
650 |
uint start; |
|
651 |
||
652 |
// find start of bridge |
|
653 |
for(;;) { |
|
654 |
if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xE0) == 0x80) |
|
655 |
break; |
|
656 |
tile += direction ? TILE_XY(0,-1) : TILE_XY(-1,0); |
|
657 |
} |
|
658 |
||
659 |
start = tile; |
|
660 |
||
661 |
// find end of bridge |
|
662 |
for(;;) { |
|
663 |
if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xE0) == 0xA0) |
|
664 |
break; |
|
665 |
tile += direction ? TILE_XY(0,1) : TILE_XY(1,0); |
|
666 |
} |
|
667 |
||
668 |
*endtile = tile; |
|
669 |
||
670 |
return start; |
|
671 |
} |
|
672 |
||
673 |
static int32 DoClearBridge(uint tile, uint32 flags) |
|
674 |
{ |
|
675 |
uint endtile; |
|
676 |
Vehicle *v; |
|
677 |
Town *t; |
|
678 |
int direction; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
679 |
|
0 | 680 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); |
681 |
||
682 |
direction = _map5[tile]&1; |
|
683 |
||
684 |
/* delete stuff under the middle part if there's a transport route there..? */ |
|
685 |
if ((_map5[tile] & 0xE0) == 0xE0) { |
|
686 |
int32 cost; |
|
687 |
||
688 |
// check if we own the tile below the bridge.. |
|
689 |
if (_current_player != OWNER_WATER && (!CheckTileOwnership(tile) || !EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile)) )) |
|
690 |
return CMD_ERROR; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
691 |
|
0 | 692 |
cost = (_map5[tile] & 8) ? _price.remove_road * 2 : _price.remove_rail; |
693 |
||
694 |
if (flags & DC_EXEC) { |
|
695 |
_map5[tile] = _map5[tile] & ~0x38; |
|
696 |
_map_owner[tile] = OWNER_NONE; |
|
697 |
MarkTileDirtyByTile(tile); |
|
698 |
} |
|
699 |
return cost; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
700 |
|
0 | 701 |
/* delete canal under bridge */ |
702 |
} else if(_map5[tile]==0xC8 && GET_TILEHEIGHT(tile)!=0) { |
|
703 |
int32 cost; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
704 |
|
0 | 705 |
// check for vehicles under bridge |
706 |
if ( !EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile)) ) |
|
707 |
return CMD_ERROR; |
|
708 |
cost = _price.clear_water; |
|
709 |
if (flags & DC_EXEC) { |
|
710 |
_map5[tile] = _map5[tile] & ~0x38; |
|
711 |
_map_owner[tile] = OWNER_NONE; |
|
712 |
MarkTileDirtyByTile(tile); |
|
713 |
} |
|
714 |
return cost; |
|
715 |
} |
|
716 |
||
717 |
tile = FindEdgesOfBridge(tile, &endtile); |
|
718 |
||
719 |
// floods, scenario editor can always destroy bridges |
|
720 |
if (_current_player != OWNER_WATER && _game_mode != GM_EDITOR && !CheckTileOwnership(tile)) { |
|
721 |
if (!(_patches.extra_dynamite || _cheats.magic_bulldozer.value) || _map_owner[tile] != OWNER_TOWN) |
|
722 |
return CMD_ERROR; |
|
723 |
} |
|
724 |
||
725 |
if (!EnsureNoVehicle(tile) || !EnsureNoVehicle(endtile)) |
|
726 |
return CMD_ERROR; |
|
727 |
||
728 |
/* Make sure there's no vehicle on the bridge |
|
729 |
Omit tile and endtile, since these are already checked, thus solving the problem |
|
730 |
of bridges over water, or higher bridges, where z is not increased, eg level bridge |
|
731 |
*/ |
|
732 |
tile += direction ? TILE_XY(0, 1) : TILE_XY( 1,0); |
|
733 |
endtile -= direction ? TILE_XY(0, 1) : TILE_XY( 1,0); |
|
734 |
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
|
735 |
VehicleInTheWayErrMsg(v); |
0 | 736 |
return CMD_ERROR; |
737 |
} |
|
738 |
||
739 |
/* Put the tiles back to start/end position */ |
|
740 |
tile -= direction ? TILE_XY(0, 1) : TILE_XY( 1,0); |
|
741 |
endtile += direction ? TILE_XY(0, 1) : TILE_XY( 1,0); |
|
742 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
743 |
|
0 | 744 |
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
|
745 |
// check if you're allowed to remove the bridge owned by a town. |
0 | 746 |
// removal allowal depends on difficulty settings |
747 |
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
|
748 |
if (!CheckforTownRating(tile, flags, t, TUNNELBRIDGE_REMOVE)) |
0 | 749 |
return CMD_ERROR; |
750 |
} |
|
751 |
||
752 |
if (flags & DC_EXEC) { |
|
753 |
byte m5; |
|
754 |
uint c = tile; |
|
755 |
uint16 new_data; |
|
756 |
||
757 |
//checks if the owner is town then decrease town rating by 250 until |
|
758 |
// 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
|
759 |
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
|
760 |
ChangeTownRating(t, -250, 0); |
0 | 761 |
|
762 |
do { |
|
763 |
m5 = _map5[c]; |
|
764 |
||
765 |
if (m5 & 0x40) { |
|
766 |
if (m5 & 0x20) { |
|
767 |
static const uint16 _new_data_table[] = {0x1002, 0x1001, 0x2005, 0x200A, 0, 0, 0, 0}; |
|
768 |
new_data = _new_data_table[((m5 & 0x18) >> 2) | (m5&1)]; |
|
769 |
} else { |
|
770 |
if (!(m5 & 0x18)) goto clear_it; |
|
771 |
new_data = 0x6000; |
|
772 |
} |
|
773 |
||
774 |
_map_type_and_height[c] &= 0x0F; |
|
775 |
_map_type_and_height[c] |= new_data >> 8; |
|
776 |
_map5[c] = (byte)new_data; |
|
777 |
_map2[c] = 0; |
|
778 |
||
779 |
MarkTileDirtyByTile(c); |
|
780 |
||
781 |
} else { |
|
782 |
clear_it:; |
|
783 |
DoClearSquare(c); |
|
784 |
} |
|
785 |
c += direction ? TILE_XY(0,1) : TILE_XY(1,0); |
|
786 |
} while (c <= endtile); |
|
787 |
||
788 |
SetSignalsOnBothDir(tile, direction); |
|
789 |
SetSignalsOnBothDir(endtile, direction); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
790 |
|
0 | 791 |
} |
792 |
||
793 |
return ((((endtile - tile) >> (direction?8:0))&0xFF)+1) * _price.clear_bridge; |
|
794 |
} |
|
795 |
||
796 |
static int32 ClearTile_TunnelBridge(uint tile, byte flags) { |
|
797 |
int32 ret; |
|
798 |
byte m5 = _map5[tile]; |
|
799 |
||
800 |
if ((m5 & 0xF0) == 0) { |
|
801 |
if (flags & DC_AUTO) |
|
802 |
return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST); |
|
803 |
return DoClearTunnel(tile, flags); |
|
804 |
} else if (m5 & 0x80) { |
|
805 |
if (flags & DC_AUTO) |
|
806 |
return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); |
|
807 |
||
808 |
ret = DoClearBridge(tile, flags); |
|
809 |
if (ret == CMD_ERROR) |
|
810 |
return CMD_ERROR; |
|
811 |
||
812 |
return ret; |
|
813 |
} else { |
|
814 |
return CMD_ERROR; |
|
815 |
} |
|
816 |
} |
|
817 |
||
818 |
int32 DoConvertTunnelBridgeRail(uint tile, uint totype, bool exec) |
|
819 |
{ |
|
820 |
uint endtile; |
|
821 |
int length; |
|
822 |
Vehicle *v; |
|
823 |
||
824 |
if ((_map5[tile] & 0xFC) == 0x00) { |
|
825 |
// railway tunnel |
|
826 |
if (!CheckTileOwnership(tile)) return CMD_ERROR; |
|
827 |
||
828 |
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
|
829 |
|
0 | 830 |
endtile = CheckTunnelBusy(tile, &length); |
831 |
if (endtile == (uint)-1) return CMD_ERROR; |
|
832 |
||
833 |
if (exec) { |
|
834 |
_map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype; |
|
835 |
_map3_lo[endtile] = (_map3_lo[endtile] & 0xF0) + totype; |
|
836 |
MarkTileDirtyByTile(tile); |
|
837 |
MarkTileDirtyByTile(endtile); |
|
838 |
} |
|
839 |
return (length + 1) * (_price.build_rail >> 1); |
|
840 |
} else if ((_map5[tile] & 0xF8) == 0xE0) { |
|
841 |
// bridge middle part with rail below |
|
842 |
// only check for train under bridge |
|
843 |
if (!CheckTileOwnership(tile) || !EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile))) |
|
844 |
return CMD_ERROR; |
|
845 |
||
846 |
// tile is already of requested type? |
|
847 |
if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR; |
|
848 |
// change type. |
|
849 |
if (exec) { |
|
850 |
_map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype; |
|
851 |
MarkTileDirtyByTile(tile); |
|
852 |
} |
|
853 |
return _price.build_rail >> 1; |
|
854 |
} else if ((_map5[tile]&0xC6) == 0x80) { |
|
855 |
uint starttile; |
|
856 |
int32 cost; |
|
857 |
||
858 |
if (!CheckTileOwnership(tile)) return CMD_ERROR; |
|
859 |
||
860 |
// railway bridge |
|
861 |
starttile = tile = FindEdgesOfBridge(tile, &endtile); |
|
862 |
// Make sure there's no vehicle on the bridge |
|
863 |
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
|
864 |
VehicleInTheWayErrMsg(v); |
0 | 865 |
return CMD_ERROR; |
866 |
} |
|
867 |
||
868 |
if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR; |
|
869 |
cost = 0; |
|
870 |
do { |
|
871 |
if (exec) { |
|
872 |
if (tile == starttile || tile == endtile) { |
|
873 |
_map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype; |
|
874 |
} else { |
|
875 |
_map3_lo[tile] = (_map3_lo[tile] & 0x0F) + (totype << 4); |
|
876 |
} |
|
877 |
MarkTileDirtyByTile(tile); |
|
878 |
} |
|
879 |
cost += (_price.build_rail>>1); |
|
880 |
tile += _map5[tile]&1 ? TILE_XY(0,1) : TILE_XY(1,0); |
|
881 |
} while (tile <= endtile); |
|
882 |
||
883 |
return cost; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
884 |
} else |
0 | 885 |
return CMD_ERROR; |
886 |
} |
|
887 |
||
888 |
||
889 |
// 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
|
890 |
uint GetBridgeHeight(const TileInfo *ti) |
0 | 891 |
{ |
892 |
uint delta; |
|
893 |
TileInfo ti_end; |
|
894 |
uint tile = ti->tile; |
|
895 |
uint z_correction = 0; |
|
896 |
||
897 |
// find the end tile of the bridge. |
|
898 |
delta = (_map5[tile] & 1) ? TILE_XY(0,1) : TILE_XY(1,0); |
|
899 |
do { |
|
900 |
assert((_map5[tile] & 0xC0) == 0xC0); // bridge and middle part |
|
901 |
tile += delta; |
|
902 |
} while (_map5[tile] & 0x40); // while bridge middle parts |
|
903 |
||
904 |
// if the end of the bridge is on a tileh 7, the z coordinate is 1 tile too low |
|
905 |
// correct it. |
|
906 |
FindLandscapeHeightByTile(&ti_end, tile); |
|
907 |
if (HASBIT(1 << 7, ti_end.tileh)) |
|
908 |
z_correction += 8; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
909 |
|
0 | 910 |
// return the height there (the height of the NORTH CORNER) |
911 |
return GET_TILEHEIGHT(tile) + z_correction; |
|
912 |
} |
|
913 |
||
914 |
static const byte _bridge_foundations[2][16] = { |
|
915 |
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
916 |
{1,16,18,3,20,5,0,7,22,0,10,11,12,13,14}, |
|
917 |
{1,15,17,0,19,5,6,7,21,9,10,11, 0,13,14}, |
|
918 |
}; |
|
919 |
||
920 |
extern const byte _track_sloped_sprites[14]; |
|
921 |
extern const byte _road_sloped_sprites[14]; |
|
922 |
||
923 |
#include "table/tunnel_land.h" |
|
924 |
||
925 |
static void DrawBridgePillars(TileInfo *ti, int x, int y, int z) |
|
926 |
{ |
|
927 |
const uint32 *b; |
|
928 |
uint32 image; |
|
929 |
int piece; |
|
930 |
||
931 |
b = _bridge_poles_table[_map2[ti->tile]>>4]; |
|
932 |
||
933 |
// Draw first piece |
|
934 |
// (necessary for cantilever bridges) |
|
935 |
image = b[12 + (ti->map5&0x01)]; |
|
936 |
piece = _map2[ti->tile]&0xF; |
|
937 |
if( image!=0 && piece!=0 ) |
|
938 |
DrawGroundSpriteAt(image, x,y, z); |
|
939 |
||
940 |
image = b[(ti->map5&0x01)*6 + piece]; |
|
941 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) image = (image & 0x3FFF) | 0x03224000; |
|
942 |
||
943 |
if (image != 0) { |
|
944 |
int back_height, front_height, i=z; |
|
945 |
const byte *p; |
|
946 |
||
947 |
static const byte _tileh_bits[4][8] = { |
|
948 |
{2,1,8,4, 16,11,0,9}, |
|
949 |
{1,8,4,2, 11,16,9,0}, |
|
950 |
{4,8,1,2, 16,11,0,9}, |
|
951 |
{2,4,8,1, 11,16,9,0}, |
|
952 |
}; |
|
953 |
||
954 |
p = _tileh_bits[(image & 1) * 2 + (ti->map5&0x01)]; |
|
955 |
front_height = ti->z + ((ti->tileh & p[0])?8:0); |
|
956 |
back_height = ti->z + ((ti->tileh & p[1])?8:0); |
|
957 |
if (ti->tileh & 0x10) { |
|
958 |
if (!(ti->tileh & p[2])) front_height += 8; |
|
959 |
if (!(ti->tileh & p[3])) back_height += 8; |
|
960 |
} |
|
961 |
||
962 |
for(; z>=front_height || z>=back_height; z=z-8) { |
|
963 |
if (z>=front_height) AddSortableSpriteToDraw(image, x,y, p[4], p[5], 0x28, z); // front facing pillar |
|
964 |
if (z>=back_height && z<i-8) AddSortableSpriteToDraw(image, x - p[6], y - p[7], p[4], p[5], 0x28, z); // back facing pillar |
|
965 |
} |
|
966 |
} |
|
967 |
} |
|
968 |
||
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
39
diff
changeset
|
969 |
uint GetBridgeFoundation(uint tileh, byte direction) { |
0 | 970 |
int i; |
971 |
// normal level sloped building (7, 11, 13, 14) |
|
972 |
if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << tileh)) |
|
973 |
return tileh; |
|
974 |
||
975 |
// inclined sloped building |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
976 |
if ( ((i=0, tileh == 1) || (i+=2, tileh == 2) || (i+=2, tileh == 4) || (i+=2, tileh == 8)) && |
0 | 977 |
( direction == 0 || (i++, direction == 1)) ) |
978 |
return i + 15; |
|
979 |
||
980 |
return 0; |
|
981 |
} |
|
982 |
||
983 |
static void DrawTile_TunnelBridge(TileInfo *ti) |
|
984 |
{ |
|
985 |
uint32 image; |
|
986 |
uint tmp; |
|
987 |
const uint32 *b; |
|
988 |
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
|
989 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
990 |
// draw tunnel? |
0 | 991 |
if ( (byte)(ti->map5&0xF0) == 0) { |
992 |
/* railway type */ |
|
993 |
image = (_map3_lo[ti->tile] & 0xF) * 8; |
|
994 |
||
995 |
/* ice? */ |
|
996 |
if (ice) |
|
997 |
image += 32; |
|
998 |
||
999 |
image += _draw_tunnel_table_1[(ti->map5 >> 2) & 0x3]; |
|
1000 |
image += (ti->map5 & 3) << 1; |
|
1001 |
DrawGroundSprite(image); |
|
1002 |
||
1003 |
AddSortableSpriteToDraw(image+1, ti->x + 15, ti->y + 15, 1, 1, 8, (byte)ti->z); |
|
1004 |
// draw bridge? |
|
1005 |
} else if ((byte)ti->map5 & 0x80) { |
|
1006 |
// get type of track on the bridge. |
|
1007 |
tmp = _map3_lo[ti->tile]; |
|
1008 |
if (ti->map5 & 0x40) tmp >>= 4; |
|
1009 |
tmp &= 0xF; |
|
1010 |
||
1011 |
// 0 = rail bridge |
|
1012 |
// 1 = road bridge |
|
1013 |
// 2 = monorail bridge |
|
1014 |
// 3 = maglev bridge |
|
1015 |
||
1016 |
// add direction and fix stuff. |
|
1017 |
if (tmp != 0) tmp++; |
|
1018 |
tmp = (ti->map5&3) + (tmp*2); |
|
1019 |
||
1020 |
if (!(ti->map5 & 0x40)) { // bridge ramps |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1021 |
|
0 | 1022 |
if (!(BRIDGE_NO_FOUNDATION & (1 << ti->tileh))) { // no foundations for 0, 3, 6, 9, 12 |
1023 |
int f = GetBridgeFoundation(ti->tileh, ti->map5 & 0x1); // pass direction |
|
1024 |
if (f) DrawFoundation(ti, f); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1025 |
|
0 | 1026 |
// default sloped sprites.. |
1027 |
if (ti->tileh != 0) image = _track_sloped_sprites[ti->tileh - 1] + 0x3F3; |
|
1028 |
} |
|
1029 |
||
1030 |
// bridge ending. |
|
1031 |
b = _bridge_sprite_table[_map2[ti->tile]>>4][6]; |
|
1032 |
b += (tmp&(3<<1))*4; /* actually ((tmp>>2)&3)*8 */ |
|
1033 |
b += (tmp&1); // direction |
|
1034 |
if (ti->tileh == 0) b += 4; // sloped "entrance" ? |
|
1035 |
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
|
1036 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1037 |
image = *b; |
0 | 1038 |
|
1039 |
if (!ice) { |
|
1040 |
DrawClearLandTile(ti, 3); |
|
1041 |
} else { |
|
1042 |
DrawGroundSprite(0x11C6 + _tileh_to_sprite[ti->tileh]); |
|
1043 |
} |
|
1044 |
||
1045 |
// draw ramp |
|
1046 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) image = (image & 0x3FFF) | 0x03224000; |
|
1047 |
AddSortableSpriteToDraw(image, ti->x, ti->y, 16, 16, 7, ti->z); |
|
1048 |
} else { |
|
1049 |
// bridge middle part. |
|
1050 |
uint z; |
|
1051 |
int x,y; |
|
1052 |
||
1053 |
image = (ti->map5 >> 3) & 3; // type of stuff under bridge (only defined for 0,1) |
|
1054 |
assert(image <= 1); |
|
1055 |
||
1056 |
if (!(ti->map5 & 0x20)) { |
|
1057 |
// draw land under bridge |
|
1058 |
if (ice) image += 2; // ice too? |
|
1059 |
DrawGroundSprite(_bridge_land_below[image] + _tileh_to_sprite[ti->tileh]); |
|
1060 |
||
1061 |
// draw canal water? |
|
1062 |
if (ti->map5 & 8 && ti->z != 0) DrawCanalWater(ti->tile); |
|
1063 |
} else { |
|
1064 |
// draw transport route under bridge |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1065 |
|
0 | 1066 |
// draw foundation? |
1067 |
if (ti->tileh) { |
|
1068 |
int f = _bridge_foundations[ti->map5&1][ti->tileh]; |
|
1069 |
if (f) DrawFoundation(ti, f); |
|
1070 |
} |
|
1071 |
||
1072 |
if (!(image&1)) { |
|
1073 |
// railway |
|
1074 |
image = 0x3F3 + (ti->map5 & 1); |
|
1075 |
if (ti->tileh != 0) image = _track_sloped_sprites[ti->tileh - 1] + 0x3F3; |
|
1076 |
image += (_map3_lo[ti->tile] & 0xF) * TRACKTYPE_SPRITE_PITCH; |
|
1077 |
if (ice) image += 26; // ice? |
|
1078 |
} else { |
|
1079 |
// road |
|
1080 |
image = 1332 + (ti->map5 & 1); |
|
1081 |
if (ti->tileh != 0) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F; |
|
1082 |
if (ice) image += 19; // ice? |
|
1083 |
} |
|
1084 |
DrawGroundSprite(image); |
|
1085 |
} |
|
1086 |
// get bridge sprites |
|
1087 |
b = _bridge_sprite_table[_map2[ti->tile]>>4][_map2[ti->tile]&0xF] + tmp * 4; |
|
1088 |
||
1089 |
z = GetBridgeHeight(ti) + 5; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1090 |
|
0 | 1091 |
// draw rail |
1092 |
image = b[0]; |
|
1093 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) image = (image & 0x3FFF) | 0x03224000; |
|
1094 |
AddSortableSpriteToDraw(image, ti->x, ti->y, (ti->map5&1)?11:16, (ti->map5&1)?16:11, 1, z); |
|
1095 |
||
1096 |
x = ti->x; |
|
1097 |
y = ti->y; |
|
1098 |
image = b[1]; |
|
1099 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) image = (image & 0x3FFF) | 0x03224000; |
|
1100 |
||
1101 |
// draw roof |
|
1102 |
if (ti->map5&1) { |
|
1103 |
x += 12; |
|
1104 |
if (image&0x3FFF) AddSortableSpriteToDraw(image, x,y, 1, 16, 0x28, z); |
|
1105 |
} else { |
|
1106 |
y += 12; |
|
1107 |
if (image&0x3FFF) AddSortableSpriteToDraw(image, x,y, 16, 1, 0x28, z); |
|
1108 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1109 |
|
0 | 1110 |
if (ti->z + 5 == z ) { |
1111 |
// draw poles below for small bridges |
|
1112 |
image = b[2]; |
|
1113 |
if (image) { |
|
1114 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) image = (image & 0x3FFF) | 0x03224000; |
|
1115 |
DrawGroundSpriteAt(image, x, y, z); |
|
1116 |
} |
|
1117 |
} else if (_patches.bridge_pillars) { |
|
1118 |
// draw pillars below for high bridges |
|
1119 |
DrawBridgePillars(ti, x, y, z); |
|
1120 |
} |
|
1121 |
} |
|
1122 |
} |
|
1123 |
} |
|
1124 |
||
1125 |
static uint GetSlopeZ_TunnelBridge(TileInfo *ti) { |
|
1126 |
uint z = ti->z; |
|
1127 |
uint x = ti->x & 0xF; |
|
1128 |
uint y = ti->y & 0xF; |
|
1129 |
||
1130 |
// swap directions if Y tunnel/bridge to let the code handle the X case only. |
|
1131 |
if (ti->map5 & 1) intswap(x,y); |
|
1132 |
||
1133 |
// to the side of the tunnel/bridge? |
|
1134 |
if (IS_INT_INSIDE(y, 5, 10+1)) { |
|
1135 |
// tunnel? |
|
1136 |
if ( (ti->map5 & 0xF0) == 0) |
|
1137 |
return z; |
|
1138 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1139 |
// bridge? |
0 | 1140 |
if ( ti->map5 & 0x80 ) { |
1141 |
// bridge ending? |
|
1142 |
if (!(ti->map5 & 0x40)) { |
|
1143 |
if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << ti->tileh)) // 7, 11, 13, 14 |
|
1144 |
z += 8; |
|
1145 |
||
1146 |
// no ramp for bridge ending |
|
1147 |
if ((BRIDGE_PARTLY_LEVELED_FOUNDATION & (1 << ti->tileh) || BRIDGE_NO_FOUNDATION & (1 << ti->tileh)) && ti->tileh != 0) { |
|
1148 |
return z + 8; |
|
1149 |
||
1150 |
} else if (!(ti->map5 & 0x20)) { // northern / southern ending |
|
1151 |
// ramp |
|
1152 |
return (z + (x>>1) + 1); |
|
1153 |
} else { |
|
1154 |
// ramp in opposite dir |
|
1155 |
return (z + ((x^0xF)>>1)); |
|
1156 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1157 |
|
0 | 1158 |
// bridge middle part |
1159 |
} else { |
|
1160 |
// build on slopes? |
|
1161 |
if (ti->tileh) z+=8; |
|
1162 |
||
1163 |
// 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
|
1164 |
if (_get_z_hint >= z + 8) |
0 | 1165 |
return _get_z_hint; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1166 |
|
0 | 1167 |
// 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
|
1168 |
if (!IS_INT_INSIDE(x, 5, 10+1)) |
0 | 1169 |
return GetBridgeHeight(ti) + 8; |
1170 |
||
1171 |
// in the shared area, assume that we're below the bridge, cause otherwise the hint would've caught it. |
|
1172 |
// if rail or road below then it means it's possibly build on slope below the bridge. |
|
1173 |
if (ti->map5 & 0x20) { |
|
1174 |
uint f = _bridge_foundations[ti->map5&1][ti->tileh]; |
|
1175 |
// make sure that the slope is not inclined foundation |
|
1176 |
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
|
1177 |
|
0 | 1178 |
// change foundation type? |
1179 |
if (f) ti->tileh = _inclined_tileh[f - 15]; |
|
1180 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1181 |
|
0 | 1182 |
// no transport route, fallback to default |
1183 |
} |
|
1184 |
} |
|
1185 |
} else { |
|
1186 |
// if it's a bridge middle with transport route below, then we need to compensate for build on slopes |
|
1187 |
if ( (ti->map5 & (0x80 + 0x40 + 0x20)) == (0x80 + 0x40 + 0x20)) { |
|
1188 |
uint f; |
|
1189 |
if (ti->tileh) z += 8; |
|
1190 |
f = _bridge_foundations[ti->map5&1][ti->tileh]; |
|
1191 |
if (IS_BYTE_INSIDE(f, 1, 15)) return z; |
|
1192 |
if (f) ti->tileh = _inclined_tileh[f - 15]; |
|
1193 |
} |
|
1194 |
} |
|
1195 |
||
1196 |
// default case |
|
1197 |
z = ti->z; |
|
1198 |
return GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + z; |
|
1199 |
} |
|
1200 |
||
39 | 1201 |
static uint GetSlopeTileh_TunnelBridge(TileInfo *ti) { |
1202 |
// not accurate, but good enough for slope graphics drawing |
|
1203 |
return 0; |
|
1204 |
} |
|
1205 |
||
1206 |
||
0 | 1207 |
static void GetAcceptedCargo_TunnelBridge(uint tile, AcceptedCargo *ac) |
1208 |
{ |
|
1209 |
/* not used */ |
|
1210 |
} |
|
1211 |
||
1212 |
static const StringID _bridge_tile_str[(MAX_BRIDGES + 3) + (MAX_BRIDGES + 3)] = { |
|
1213 |
STR_501F_WOODEN_RAIL_BRIDGE, |
|
1214 |
STR_5020_CONCRETE_RAIL_BRIDGE, |
|
1215 |
STR_501C_STEEL_GIRDER_RAIL_BRIDGE, |
|
1216 |
STR_501E_REINFORCED_CONCRETE_SUSPENSION, |
|
1217 |
STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE, |
|
1218 |
STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE, |
|
1219 |
STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE, |
|
1220 |
STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE, |
|
1221 |
STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE, |
|
1222 |
STR_501C_STEEL_GIRDER_RAIL_BRIDGE, |
|
1223 |
STR_5027_TUBULAR_RAIL_BRIDGE, |
|
1224 |
STR_5027_TUBULAR_RAIL_BRIDGE, |
|
1225 |
STR_5027_TUBULAR_RAIL_BRIDGE, |
|
1226 |
0,0,0, |
|
1227 |
||
1228 |
STR_5025_WOODEN_ROAD_BRIDGE, |
|
1229 |
STR_5026_CONCRETE_ROAD_BRIDGE, |
|
1230 |
STR_5022_STEEL_GIRDER_ROAD_BRIDGE, |
|
1231 |
STR_5024_REINFORCED_CONCRETE_SUSPENSION, |
|
1232 |
STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE, |
|
1233 |
STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE, |
|
1234 |
STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE, |
|
1235 |
STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE, |
|
1236 |
STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE, |
|
1237 |
STR_5022_STEEL_GIRDER_ROAD_BRIDGE, |
|
1238 |
STR_5028_TUBULAR_ROAD_BRIDGE, |
|
1239 |
STR_5028_TUBULAR_ROAD_BRIDGE, |
|
1240 |
STR_5028_TUBULAR_ROAD_BRIDGE, |
|
1241 |
0,0,0, |
|
1242 |
}; |
|
1243 |
||
1244 |
static void GetTileDesc_TunnelBridge(uint tile, TileDesc *td) |
|
1245 |
{ |
|
1246 |
int delta; |
|
1247 |
||
1248 |
if ((_map5[tile] & 0x80) == 0) { |
|
1249 |
td->str = STR_5017_RAILROAD_TUNNEL + ((_map5[tile] >> 2) & 3); |
|
1250 |
} else { |
|
1251 |
td->str = _bridge_tile_str[ (_map2[tile] >> 4) + (((_map5[tile]>>1)&3)<<4) ]; |
|
1252 |
||
1253 |
/* scan to the end of the bridge, that's where the owner is stored */ |
|
1254 |
if (_map5[tile] & 0x40) { |
|
1255 |
delta = _map5[tile] & 1 ? TILE_XY(0,-1) : TILE_XY(-1,0); |
|
1256 |
do tile += delta; while (_map5[tile] & 0x40); |
|
1257 |
} |
|
1258 |
} |
|
1259 |
td->owner = _map_owner[tile]; |
|
1260 |
} |
|
1261 |
||
1262 |
||
1263 |
static void AnimateTile_TunnelBridge(uint tile) |
|
1264 |
{ |
|
1265 |
/* not used */ |
|
1266 |
} |
|
1267 |
||
1268 |
static void TileLoop_TunnelBridge(uint tile) |
|
1269 |
{ |
|
1270 |
if (_opt.landscape == LT_HILLY) { |
|
1271 |
if ( GetTileZ(tile) > _opt.snow_line) { |
|
1272 |
if (!(_map3_hi[tile] & 0x80)) { |
|
1273 |
_map3_hi[tile] |= 0x80; |
|
1274 |
MarkTileDirtyByTile(tile); |
|
1275 |
} |
|
1276 |
} else { |
|
1277 |
if (_map3_hi[tile] & 0x80) { |
|
1278 |
_map3_hi[tile] &= ~0x80; |
|
1279 |
MarkTileDirtyByTile(tile); |
|
1280 |
} |
|
1281 |
} |
|
1282 |
} else if (_opt.landscape == LT_DESERT) { |
|
1283 |
if (GetMapExtraBits(tile) == 1 && !(_map3_hi[tile]&0x80)) { |
|
1284 |
_map3_hi[tile] |= 0x80; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1285 |
MarkTileDirtyByTile(tile); |
0 | 1286 |
} |
1287 |
} |
|
1288 |
||
1289 |
// if it's a bridge with water below, call tileloop_water on it. |
|
1290 |
if ((_map5[tile] & 0xF8) == 0xC8) TileLoop_Water(tile); |
|
1291 |
} |
|
1292 |
||
1293 |
static void ClickTile_TunnelBridge(uint tile) |
|
1294 |
{ |
|
1295 |
/* not used */ |
|
1296 |
} |
|
1297 |
||
1298 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1299 |
static uint32 GetTileTrackStatus_TunnelBridge(uint tile, TransportType mode) |
0 | 1300 |
{ |
1301 |
uint32 result; |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1302 |
byte m5 = _map5[tile]; |
0 | 1303 |
|
1304 |
if ((m5 & 0xF0) == 0) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1305 |
/* This is a tunnel */ |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1306 |
if (((m5 & 0xC) >> 2) == mode) { |
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1307 |
/* Tranport in the tunnel is compatible */ |
0 | 1308 |
return m5&1 ? 0x202 : 0x101; |
1309 |
} |
|
1310 |
} else if (m5 & 0x80) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1311 |
/* This is a bridge */ |
0 | 1312 |
result = 0; |
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1313 |
if (((m5 & 0x6) >> 1) == mode) { |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1314 |
/* Transport over the bridge is compatible */ |
0 | 1315 |
result = m5&1 ? 0x202 : 0x101; |
1316 |
} |
|
1317 |
if (m5 & 0x40) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1318 |
/* Bridge middle part */ |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1319 |
if (!(m5 & 0x20)) { |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1320 |
/* Clear ground or water underneath */ |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1321 |
if ((m5 &= 0x18) != 8) |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1322 |
/* Clear ground */ |
0 | 1323 |
return result; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1324 |
else |
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1325 |
if (mode != TRANSPORT_WATER) |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1326 |
return result; |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1327 |
} else { |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1328 |
/* Transport underneath */ |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1329 |
if ((m5 & 0x18) >> 3 != mode) |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1330 |
/* Incompatible transport underneath */ |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1331 |
return result; |
0 | 1332 |
} |
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1333 |
/* If we've not returned yet, there is a compatible |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1334 |
* transport or water beneath, so we can add it to |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1335 |
* result */ |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1336 |
/* Why is this xor'd ? Can't it just be or'd? */ |
0 | 1337 |
result ^= m5&1 ? 0x101 : 0x202; |
1338 |
} |
|
1339 |
return result; |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1340 |
} else { |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
98
diff
changeset
|
1341 |
assert(0); /* This should never occur */ |
0 | 1342 |
} |
1343 |
return 0; |
|
1344 |
} |
|
1345 |
||
1346 |
static void ChangeTileOwner_TunnelBridge(uint tile, byte old_player, byte new_player) |
|
1347 |
{ |
|
1348 |
if (_map_owner[tile] != old_player) |
|
1349 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1350 |
|
0 | 1351 |
if (new_player != 255) { |
1352 |
_map_owner[tile] = new_player; |
|
1353 |
} else { |
|
1354 |
if((_map5[tile] & 0xC0)==0xC0) { |
|
1355 |
// the stuff BELOW the middle part is owned by the deleted player. |
|
1356 |
if (!(_map5[tile] & (1 << 4 | 1 << 3))) { |
|
1357 |
// convert railway into grass. |
|
1358 |
_map5[tile] &= ~(1 << 5 | 1 << 4 | 1 << 3); // no transport route under bridge anymore.. |
|
1359 |
} else { |
|
1360 |
// for road, change the owner of the road to local authority |
|
1361 |
_map_owner[tile] = OWNER_NONE; |
|
1362 |
} |
|
1363 |
} else { |
|
1364 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
|
1365 |
} |
|
1366 |
} |
|
1367 |
} |
|
1368 |
||
1369 |
||
1370 |
static const byte _tunnel_fractcoord_1[4] = {0x8E,0x18,0x81,0xE8}; |
|
1371 |
static const byte _tunnel_fractcoord_2[4] = {0x81,0x98,0x87,0x38}; |
|
1372 |
static const byte _tunnel_fractcoord_3[4] = {0x82,0x88,0x86,0x48}; |
|
1373 |
static const byte _exit_tunnel_track[4] = {1,2,1,2}; |
|
1374 |
||
1375 |
static const byte _road_exit_tunnel_state[4] = {8, 9, 0, 1}; |
|
1376 |
static const byte _road_exit_tunnel_frame[4] = {2, 7, 9, 4}; |
|
1377 |
||
1378 |
static const byte _tunnel_fractcoord_4[4] = {0x52, 0x85, 0x98, 0x29}; |
|
1379 |
static const byte _tunnel_fractcoord_5[4] = {0x92, 0x89, 0x58, 0x25}; |
|
1380 |
static const byte _tunnel_fractcoord_6[4] = {0x92, 0x89, 0x56, 0x45}; |
|
1381 |
static const byte _tunnel_fractcoord_7[4] = {0x52, 0x85, 0x96, 0x49}; |
|
1382 |
||
1383 |
static uint32 VehicleEnter_TunnelBridge(Vehicle *v, uint tile, int x, int y) |
|
1384 |
{ |
|
1385 |
int z; |
|
1386 |
int dir, vdir; |
|
1387 |
byte fc; |
|
1388 |
int h; |
|
1389 |
||
1390 |
if ((_map5[tile] & 0xF0) == 0) { |
|
1391 |
z = GetSlopeZ(x, y) - v->z_pos; |
|
1392 |
if (myabs(z) > 2) |
|
1393 |
return 8; |
|
1394 |
||
1395 |
if (v->type == VEH_Train) { |
|
1396 |
fc = (x&0xF)+(y<<4); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1397 |
|
0 | 1398 |
dir = _map5[tile] & 3; |
1399 |
vdir = v->direction >> 1; |
|
1400 |
||
1401 |
if (v->u.rail.track != 0x40 && dir == vdir) { |
|
1402 |
if (v->subtype == 0 && fc == _tunnel_fractcoord_1[dir]) { |
|
1403 |
if (v->spritenum < 4) |
|
1404 |
SndPlayVehicleFx(3, v); |
|
1405 |
return 0; |
|
1406 |
} |
|
1407 |
if (fc == _tunnel_fractcoord_2[dir]) { |
|
1408 |
v->tile = tile; |
|
1409 |
v->u.rail.track = 0x40; |
|
1410 |
v->vehstatus |= VS_HIDDEN; |
|
1411 |
return 4; |
|
1412 |
} |
|
1413 |
} |
|
1414 |
||
1415 |
if (dir == (vdir^2) && fc == _tunnel_fractcoord_3[dir] && z == 0) { |
|
22 | 1416 |
/* We're at the tunnel exit ?? */ |
0 | 1417 |
v->tile = tile; |
1418 |
v->u.rail.track = _exit_tunnel_track[dir]; |
|
1419 |
v->vehstatus &= ~VS_HIDDEN; |
|
1420 |
return 4; |
|
1421 |
} |
|
1422 |
} else if (v->type == VEH_Road) { |
|
1423 |
fc = (x&0xF)+(y<<4); |
|
1424 |
dir = _map5[tile] & 3; |
|
1425 |
vdir = v->direction >> 1; |
|
1426 |
||
1427 |
// Enter tunnel? |
|
1428 |
if (v->u.road.state != 0xFF && dir == vdir) { |
|
1429 |
if (fc == _tunnel_fractcoord_4[dir] || |
|
1430 |
fc == _tunnel_fractcoord_5[dir]) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1431 |
|
0 | 1432 |
v->tile = tile; |
1433 |
v->u.road.state = 0xFF; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1434 |
v->vehstatus |= VS_HIDDEN; |
0 | 1435 |
return 4; |
1436 |
} else { |
|
1437 |
return 0; |
|
1438 |
} |
|
1439 |
} |
|
1440 |
||
1441 |
if (dir == (vdir^2) && ( |
|
22 | 1442 |
/* We're at the tunnel exit ?? */ |
0 | 1443 |
fc == _tunnel_fractcoord_6[dir] || |
1444 |
fc == _tunnel_fractcoord_7[dir]) && |
|
1445 |
z == 0) { |
|
1446 |
v->tile = tile; |
|
1447 |
v->u.road.state = _road_exit_tunnel_state[dir]; |
|
1448 |
v->u.road.frame = _road_exit_tunnel_frame[dir]; |
|
1449 |
v->vehstatus &= ~VS_HIDDEN; |
|
1450 |
return 4; |
|
1451 |
} |
|
1452 |
} |
|
1453 |
} else if (_map5[tile] & 0x80) { |
|
1454 |
if (v->type == VEH_Road || (v->type == VEH_Train && v->subtype == 0)) { |
|
1455 |
if (GetTileSlope(tile, &h) != 0 || myabs(h - v->z_pos) > 2) { |
|
1456 |
/* modify speed of vehicle */ |
|
1457 |
uint16 spd = _bridge_speeds[_map2[tile] >> 4]; |
|
1458 |
if (v->type == VEH_Road) spd<<=1; |
|
1459 |
if (spd < v->cur_speed) |
|
1460 |
v->cur_speed = spd; |
|
1461 |
} |
|
1462 |
} |
|
1463 |
} |
|
1464 |
return 0; |
|
1465 |
} |
|
1466 |
||
1467 |
uint GetVehicleOutOfTunnelTile(Vehicle *v) |
|
1468 |
{ |
|
1469 |
uint tile = v->tile; |
|
1470 |
int delta_tile; |
|
1471 |
byte z; |
|
1472 |
||
1473 |
/* locate either ending of the tunnel */ |
|
1474 |
delta_tile = (v->direction&2) ? TILE_XY(0,1) : TILE_XY(1,0); |
|
1475 |
z = v->z_pos; |
|
1476 |
for(;;) { |
|
1477 |
TileInfo ti; |
|
1478 |
FindLandscapeHeightByTile(&ti, tile); |
|
1479 |
||
1480 |
if (ti.type == MP_TUNNELBRIDGE && (ti.map5 & 0xF0)==0 && (byte)ti.z == z) |
|
1481 |
break; |
|
1482 |
||
1483 |
tile += delta_tile; |
|
1484 |
} |
|
1485 |
return tile; |
|
1486 |
} |
|
1487 |
||
1488 |
const TileTypeProcs _tile_type_tunnelbridge_procs = { |
|
1489 |
DrawTile_TunnelBridge, /* draw_tile_proc */ |
|
1490 |
GetSlopeZ_TunnelBridge, /* get_slope_z_proc */ |
|
1491 |
ClearTile_TunnelBridge, /* clear_tile_proc */ |
|
1492 |
GetAcceptedCargo_TunnelBridge, /* get_accepted_cargo_proc */ |
|
1493 |
GetTileDesc_TunnelBridge, /* get_tile_desc_proc */ |
|
1494 |
GetTileTrackStatus_TunnelBridge,/* get_tile_track_status_proc */ |
|
1495 |
ClickTile_TunnelBridge, /* click_tile_proc */ |
|
1496 |
AnimateTile_TunnelBridge, /* animate_tile_proc */ |
|
1497 |
TileLoop_TunnelBridge, /* tile_loop_clear */ |
|
1498 |
ChangeTileOwner_TunnelBridge, /* change_tile_owner_clear */ |
|
1499 |
NULL, /* get_produced_cargo_proc */ |
|
1500 |
VehicleEnter_TunnelBridge, /* vehicle_enter_tile_proc */ |
|
1501 |
NULL, /* vehicle_leave_tile_proc */ |
|
39 | 1502 |
GetSlopeTileh_TunnelBridge, /* get_slope_tileh_proc */ |
0 | 1503 |
}; |