0
|
1 |
#include "stdafx.h"
|
|
2 |
#include "ttd.h"
|
|
3 |
#include "vehicle.h"
|
|
4 |
#include "viewport.h"
|
|
5 |
#include "command.h"
|
|
6 |
#include "pathfind.h"
|
|
7 |
#include "town.h"
|
|
8 |
|
|
9 |
void ShowTrainDepotWindow(uint tile);
|
|
10 |
|
|
11 |
enum {
|
|
12 |
RAIL_TYPE_NORMAL = 0,
|
|
13 |
RAIL_TYPE_SIGNALS = 0x40,
|
|
14 |
RAIL_TYPE_SPECIAL = 0x80, // If this bit is set, then it's not a regular track.
|
|
15 |
RAIL_TYPE_DEPOT = 0xC0,
|
|
16 |
RAIL_TYPE_MASK = 0xC0,
|
|
17 |
|
|
18 |
RAIL_BIT_DIAG1 = 1, // 0
|
|
19 |
RAIL_BIT_DIAG2 = 2, // 1
|
|
20 |
RAIL_BIT_UPPER = 4, // 2
|
|
21 |
RAIL_BIT_LOWER = 8, // 3
|
|
22 |
RAIL_BIT_LEFT = 16, // 4
|
|
23 |
RAIL_BIT_RIGHT = 32, // 5
|
|
24 |
RAIL_BIT_MASK = 0x3F,
|
|
25 |
|
|
26 |
RAIL_DEPOT_TRACK_MASK = 1,
|
|
27 |
RAIL_DEPOT_DIR = 3,
|
|
28 |
RAIL_DEPOT_UNUSED_BITS = 0x3C,
|
|
29 |
|
|
30 |
RAIL_TYPE_CHECKPOINT = 0xC4,
|
|
31 |
RAIL_CHECKPOINT_TRACK_MASK = 1,
|
|
32 |
RAIL_CHECKPOINT_UNUSED_BITS = 0x3E,
|
|
33 |
};
|
|
34 |
|
|
35 |
#define IS_RAIL_DEPOT(x) (((x) & (RAIL_TYPE_DEPOT|RAIL_DEPOT_UNUSED_BITS)) == RAIL_TYPE_DEPOT)
|
|
36 |
#define IS_RAIL_CHECKPOINT(x) (((x) & (RAIL_TYPE_CHECKPOINT|RAIL_CHECKPOINT_UNUSED_BITS)) == RAIL_TYPE_CHECKPOINT)
|
|
37 |
|
|
38 |
/* Format of rail map5 byte.
|
|
39 |
* 00 abcdef => Normal rail
|
|
40 |
* 01 abcdef => Rail with signals
|
|
41 |
* 10 ?????? => Unused
|
|
42 |
* 11 ????dd => Depot
|
|
43 |
*/
|
|
44 |
|
|
45 |
/* 4
|
|
46 |
* ---------
|
|
47 |
* |\ /|
|
|
48 |
* | \ 1/ |
|
|
49 |
* | \ / |
|
|
50 |
* | \ / |
|
|
51 |
* 16| \ |32
|
|
52 |
* | / \2 |
|
|
53 |
* | / \ |
|
|
54 |
* | / \ |
|
|
55 |
* |/ \|
|
|
56 |
* ---------
|
|
57 |
* 8
|
|
58 |
*/
|
|
59 |
|
|
60 |
|
|
61 |
// Constants for lower part of Map2 byte.
|
|
62 |
enum RailMap2Lower4 {
|
|
63 |
RAIL_MAP2LO_GROUND_MASK = 0xF,
|
|
64 |
RAIL_GROUND_BROWN = 0,
|
|
65 |
RAIL_GROUND_GREEN = 1,
|
|
66 |
RAIL_GROUND_FENCE_NW = 2,
|
|
67 |
RAIL_GROUND_FENCE_SE = 3,
|
|
68 |
RAIL_GROUND_FENCE_SENW = 4,
|
|
69 |
RAIL_GROUND_FENCE_NE = 5,
|
|
70 |
RAIL_GROUND_FENCE_SW = 6,
|
|
71 |
RAIL_GROUND_FENCE_NESW = 7,
|
|
72 |
RAIL_GROUND_FENCE_VERT1 = 8,
|
|
73 |
RAIL_GROUND_FENCE_VERT2 = 9,
|
|
74 |
RAIL_GROUND_FENCE_HORIZ1 = 10,
|
|
75 |
RAIL_GROUND_FENCE_HORIZ2 = 11,
|
|
76 |
RAIL_GROUND_ICE_DESERT = 12,
|
|
77 |
};
|
|
78 |
|
|
79 |
|
|
80 |
/* MAP2 byte: abcd???? => Signal On?
|
|
81 |
* MAP3LO byte: abcd???? => Signal Exists?
|
|
82 |
* MAP2 byte: ????abcd => Type of ground.
|
|
83 |
* MAP3LO byte: ????abcd => Type of rail.
|
|
84 |
* MAP5: 00abcdef => rail
|
|
85 |
* 01abcdef => rail w/ signals
|
|
86 |
* 10uuuuuu => unused
|
|
87 |
* 11uuuudd => rail depot
|
|
88 |
*/
|
|
89 |
|
|
90 |
static bool CheckTrackCombination(byte map5, byte trackbits, byte flags)
|
|
91 |
{
|
|
92 |
_error_message = STR_1001_IMPOSSIBLE_TRACK_COMBINATION;
|
|
93 |
|
|
94 |
if ((map5&RAIL_TYPE_MASK) == RAIL_TYPE_SIGNALS) {
|
|
95 |
|
|
96 |
if (map5 & trackbits) {
|
|
97 |
_error_message = STR_1007_ALREADY_BUILT;
|
|
98 |
return false;
|
|
99 |
}
|
|
100 |
|
|
101 |
map5 |= trackbits;
|
|
102 |
return (map5 == (RAIL_TYPE_SIGNALS|RAIL_BIT_UPPER|RAIL_BIT_LOWER) || map5 == (RAIL_TYPE_SIGNALS|RAIL_BIT_LEFT|RAIL_BIT_RIGHT));
|
|
103 |
|
|
104 |
} else if ((map5&RAIL_TYPE_MASK) == RAIL_TYPE_NORMAL) {
|
|
105 |
_error_message = STR_1007_ALREADY_BUILT;
|
|
106 |
if (map5 & trackbits)
|
|
107 |
return false;
|
|
108 |
|
|
109 |
// Computer players are not allowed to intersect pieces of rail.
|
|
110 |
if (!(flags&DC_NO_RAIL_OVERLAP))
|
|
111 |
return true;
|
|
112 |
|
|
113 |
map5 |= trackbits;
|
|
114 |
return (map5 == (RAIL_BIT_UPPER|RAIL_BIT_LOWER) || map5 == (RAIL_BIT_LEFT|RAIL_BIT_RIGHT));
|
|
115 |
} else {
|
|
116 |
return false;
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
|
|
121 |
static const byte _valid_tileh_slopes[3][15] = {
|
|
122 |
|
|
123 |
// set of normal ones
|
|
124 |
{
|
|
125 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
126 |
RAIL_BIT_RIGHT,
|
|
127 |
RAIL_BIT_UPPER,
|
|
128 |
RAIL_BIT_DIAG1,
|
|
129 |
|
|
130 |
RAIL_BIT_LEFT,
|
|
131 |
0,
|
|
132 |
RAIL_BIT_DIAG2,
|
|
133 |
RAIL_BIT_LOWER,
|
|
134 |
|
|
135 |
RAIL_BIT_LOWER,
|
|
136 |
RAIL_BIT_DIAG2,
|
|
137 |
0,
|
|
138 |
RAIL_BIT_LEFT,
|
|
139 |
|
|
140 |
RAIL_BIT_DIAG1,
|
|
141 |
RAIL_BIT_UPPER,
|
|
142 |
RAIL_BIT_RIGHT,
|
|
143 |
},
|
|
144 |
|
|
145 |
// allowed rail for an evenly raised platform
|
|
146 |
{
|
|
147 |
0,
|
|
148 |
RAIL_BIT_LEFT,
|
|
149 |
RAIL_BIT_LOWER,
|
|
150 |
RAIL_BIT_DIAG2 | RAIL_BIT_LOWER | RAIL_BIT_LEFT,
|
|
151 |
|
|
152 |
RAIL_BIT_RIGHT,
|
|
153 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
154 |
RAIL_BIT_DIAG1 | RAIL_BIT_LOWER | RAIL_BIT_RIGHT,
|
|
155 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
156 |
|
|
157 |
RAIL_BIT_UPPER,
|
|
158 |
RAIL_BIT_DIAG1 | RAIL_BIT_UPPER | RAIL_BIT_LEFT,
|
|
159 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
160 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
161 |
|
|
162 |
RAIL_BIT_DIAG2 | RAIL_BIT_UPPER | RAIL_BIT_RIGHT,
|
|
163 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
164 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
165 |
},
|
|
166 |
|
|
167 |
// allowed rail on coast tile
|
|
168 |
{
|
|
169 |
0,
|
|
170 |
RAIL_BIT_LEFT,
|
|
171 |
RAIL_BIT_LOWER,
|
|
172 |
RAIL_BIT_DIAG2|RAIL_BIT_LEFT|RAIL_BIT_LOWER,
|
|
173 |
|
|
174 |
RAIL_BIT_RIGHT,
|
|
175 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
176 |
RAIL_BIT_DIAG1|RAIL_BIT_RIGHT|RAIL_BIT_LOWER,
|
|
177 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
178 |
|
|
179 |
RAIL_BIT_UPPER,
|
|
180 |
RAIL_BIT_DIAG1|RAIL_BIT_LEFT|RAIL_BIT_UPPER,
|
|
181 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
182 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
183 |
|
|
184 |
RAIL_BIT_DIAG2|RAIL_BIT_RIGHT|RAIL_BIT_UPPER,
|
|
185 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
186 |
RAIL_BIT_DIAG1|RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LOWER|RAIL_BIT_LEFT|RAIL_BIT_RIGHT,
|
|
187 |
}
|
|
188 |
};
|
|
189 |
|
|
190 |
static uint GetRailFoundation(uint tileh, uint bits)
|
|
191 |
{
|
|
192 |
int i;
|
|
193 |
|
|
194 |
if ((~_valid_tileh_slopes[0][tileh] & bits) == 0)
|
|
195 |
return 0;
|
|
196 |
|
|
197 |
if ((~_valid_tileh_slopes[1][tileh] & bits) == 0)
|
|
198 |
return tileh;
|
|
199 |
|
|
200 |
if ( ((i=0, tileh == 1) || (i+=2, tileh == 2) || (i+=2, tileh == 4) || (i+=2, tileh == 8)) && (bits == RAIL_BIT_DIAG1 || (i++, bits == RAIL_BIT_DIAG2)))
|
|
201 |
return i + 15;
|
|
202 |
|
|
203 |
return 0;
|
|
204 |
}
|
|
205 |
|
|
206 |
//
|
|
207 |
static uint32 CheckRailSlope(int tileh, uint rail_bits, uint existing, uint tile)
|
|
208 |
{
|
|
209 |
// never allow building on top of steep tiles
|
|
210 |
if (!(tileh & 0x10)) {
|
|
211 |
rail_bits |= existing;
|
|
212 |
|
|
213 |
// don't allow building on the lower side of a coast
|
|
214 |
if (IS_TILETYPE(tile, MP_WATER) && ~_valid_tileh_slopes[2][tileh] & rail_bits) {
|
|
215 |
return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER);
|
|
216 |
}
|
|
217 |
|
|
218 |
// no special foundation
|
|
219 |
if ((~_valid_tileh_slopes[0][tileh] & rail_bits) == 0)
|
|
220 |
return 0;
|
|
221 |
|
|
222 |
if (((~_valid_tileh_slopes[1][tileh] & rail_bits) == 0) || // whole tile is leveled up
|
|
223 |
((rail_bits == RAIL_BIT_DIAG1 || rail_bits == RAIL_BIT_DIAG2) && (tileh == 1 || tileh == 2 || tileh == 4 || tileh == 8))) { // partly up
|
|
224 |
return existing ? 0 : _price.terraform;
|
|
225 |
}
|
|
226 |
}
|
|
227 |
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
|
|
228 |
}
|
|
229 |
|
|
230 |
/* Build a single track.
|
|
231 |
* p1 - railroad type normal/maglev
|
|
232 |
* p2 - tile direction
|
|
233 |
*/
|
|
234 |
|
|
235 |
int32 CmdBuildSingleRail(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
236 |
{
|
|
237 |
TileInfo ti;
|
|
238 |
int32 ret, cost = 0;
|
|
239 |
byte rail_bit = 1 << p2;
|
|
240 |
byte rail_type = (byte)(p1 & 0xF);
|
|
241 |
uint tile;
|
|
242 |
byte existing = 0;
|
|
243 |
bool need_clear = false;
|
|
244 |
|
|
245 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
246 |
|
|
247 |
_error_message = STR_1007_ALREADY_BUILT;
|
|
248 |
|
|
249 |
FindLandscapeHeight(&ti, x, y);
|
|
250 |
tile = ti.tile;
|
|
251 |
|
|
252 |
// allow building rail under bridge
|
|
253 |
if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile))
|
|
254 |
return CMD_ERROR;
|
|
255 |
|
|
256 |
if (ti.type == MP_TUNNELBRIDGE) {
|
|
257 |
/* BUILD ON BRIDGE CODE */
|
|
258 |
if (!EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile)))
|
|
259 |
return CMD_ERROR;
|
|
260 |
|
|
261 |
if ((ti.map5 & 0xF8) == 0xC0) {
|
|
262 |
if (ti.tileh & 0x10 || rail_bit != (byte)((ti.map5 & 1) ? 1 : 2)) goto need_clear;
|
|
263 |
|
|
264 |
if (!(flags & DC_EXEC))
|
|
265 |
return _price.build_rail;
|
|
266 |
|
|
267 |
_map5[tile] = (ti.map5 & 0xC7) | 0x20;
|
|
268 |
goto set_ownership;
|
|
269 |
} else if ((ti.map5 & 0xF8) == 0xE0) {
|
|
270 |
if ((_map3_lo[tile] & 0xF) != (int)p1) goto need_clear;
|
|
271 |
if (rail_bit != (byte)((ti.map5 & 1) ? 1 : 2)) goto need_clear;
|
|
272 |
return CMD_ERROR;
|
|
273 |
} else
|
|
274 |
goto need_clear;
|
|
275 |
} else if (ti.type == MP_STREET) {
|
|
276 |
byte m5;
|
|
277 |
/* BUILD ON STREET CODE */
|
|
278 |
if (ti.tileh != 0) goto need_clear;
|
|
279 |
|
|
280 |
if (!(ti.map5 & 0xF0)) {
|
|
281 |
if ((ti.map5 & 0x0F) == 0xA) {
|
|
282 |
if (rail_bit != 2) goto need_clear;
|
|
283 |
m5 = 0x10;
|
|
284 |
} else if ((ti.map5 & 0x0F) == 0x5) {
|
|
285 |
if (rail_bit != 1) goto need_clear;
|
|
286 |
m5 = 0x18;
|
|
287 |
} else
|
|
288 |
goto need_clear;
|
|
289 |
|
|
290 |
if (!(flags & DC_EXEC))
|
|
291 |
return _price.build_rail;
|
|
292 |
|
|
293 |
ModifyTile(tile,
|
|
294 |
MP_SETTYPE(MP_STREET) |
|
|
295 |
MP_MAP3LO | MP_MAP3HI | MP_MAPOWNER_CURRENT | MP_MAP5,
|
|
296 |
_map_owner[tile], /* map3_lo */
|
|
297 |
p1, /* map3_hi */
|
|
298 |
m5 /* map5 */
|
|
299 |
);
|
|
300 |
goto fix_signals;
|
|
301 |
} else if (!(ti.map5 & 0xE0)) {
|
|
302 |
if (rail_bit != (byte)((ti.map5 & 8) ? 1 : 2)) goto need_clear;
|
|
303 |
return CMD_ERROR;
|
|
304 |
} else
|
|
305 |
goto need_clear;
|
|
306 |
} else if (ti.type == MP_RAILWAY) {
|
|
307 |
|
|
308 |
/* BUILD ON RAILWAY CODE */
|
|
309 |
if (_map_owner[tile] != _current_player || (byte)(_map3_lo[tile]&0xF) != rail_type)
|
|
310 |
goto need_clear;
|
|
311 |
if (!CheckTrackCombination(ti.map5, rail_bit, (byte)flags))
|
|
312 |
return CMD_ERROR;
|
|
313 |
|
|
314 |
existing = ti.map5 & 0x3F;
|
|
315 |
} else {
|
|
316 |
|
|
317 |
/* DEFAULT BUILD ON CODE */
|
|
318 |
need_clear:;
|
|
319 |
/* isnot_railway */
|
|
320 |
if (!(flags & DC_EXEC)) {
|
|
321 |
ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
322 |
if (ret == CMD_ERROR) return CMD_ERROR;
|
|
323 |
cost += ret;
|
|
324 |
}
|
|
325 |
need_clear = true;
|
|
326 |
}
|
|
327 |
|
|
328 |
ret = CheckRailSlope(ti.tileh, rail_bit, existing, tile);
|
|
329 |
if (ret & 0x80000000)
|
|
330 |
return ret;
|
|
331 |
cost += ret;
|
|
332 |
|
|
333 |
// the AI is not allowed to used foundationed tiles.
|
|
334 |
if (ret && (_is_ai_player || !_patches.build_on_slopes))
|
|
335 |
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
|
|
336 |
|
|
337 |
if (flags & DC_EXEC && need_clear) {
|
|
338 |
ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
339 |
if (ret == CMD_ERROR) return CMD_ERROR;
|
|
340 |
cost += ret;
|
|
341 |
}
|
|
342 |
|
|
343 |
if (flags & DC_EXEC) {
|
|
344 |
_map_type_and_height[tile] &= 0xF;
|
|
345 |
_map_type_and_height[tile] |= MP_RAILWAY << 4;
|
|
346 |
_map5[tile] |= rail_bit;
|
|
347 |
_map2[tile] &= ~RAIL_MAP2LO_GROUND_MASK;
|
|
348 |
|
|
349 |
// In case it's a tile without signals, clear the signal bits. Why?
|
|
350 |
if ((_map5[tile] & RAIL_TYPE_MASK) != RAIL_TYPE_SIGNALS)
|
|
351 |
_map2[tile] &= ~0xF0;
|
|
352 |
|
|
353 |
set_ownership:
|
|
354 |
_map_owner[tile] = _current_player;
|
|
355 |
|
|
356 |
_map3_lo[tile] &= ~0xF;
|
|
357 |
_map3_lo[tile] |= rail_type;
|
|
358 |
|
|
359 |
MarkTileDirtyByTile(tile);
|
|
360 |
|
|
361 |
fix_signals:
|
|
362 |
SetSignalsOnBothDir(tile, (byte)p2);
|
|
363 |
}
|
|
364 |
|
|
365 |
return cost + _price.build_rail;
|
|
366 |
}
|
|
367 |
|
|
368 |
static const byte _signals_table[] = {
|
|
369 |
0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0, // direction 1
|
|
370 |
0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0 // direction 2
|
|
371 |
};
|
|
372 |
|
|
373 |
static const byte _signals_table_other[] = {
|
|
374 |
0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0, // direction 1
|
|
375 |
0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0 // direction 2
|
|
376 |
};
|
|
377 |
|
|
378 |
static const byte _signals_table_both[] = {
|
|
379 |
0xC0, 0xC0, 0xC0, 0x30, 0xC0, 0x30, 0, 0, // both directions combined
|
|
380 |
0xC0, 0xC0, 0xC0, 0x30, 0xC0, 0x30, 0, 0
|
|
381 |
};
|
|
382 |
|
|
383 |
|
|
384 |
/* Remove a single track.
|
|
385 |
* p1 - unused
|
|
386 |
* p2 - tile direction
|
|
387 |
*/
|
|
388 |
|
|
389 |
int32 CmdRemoveSingleRail(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
390 |
{
|
|
391 |
TileInfo ti;
|
|
392 |
byte rail_bit = 1 << p2;
|
|
393 |
byte m5;
|
|
394 |
uint tile;
|
|
395 |
|
|
396 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
397 |
|
|
398 |
FindLandscapeHeight(&ti, x, y);
|
|
399 |
|
|
400 |
tile = ti.tile;
|
|
401 |
|
|
402 |
if (!((1<<ti.type) & ((1<<MP_TUNNELBRIDGE)|(1<<MP_STREET)|(1<<MP_RAILWAY))))
|
|
403 |
return CMD_ERROR;
|
|
404 |
|
|
405 |
if (_current_player != OWNER_WATER && !CheckTileOwnership(tile))
|
|
406 |
return CMD_ERROR;
|
|
407 |
|
|
408 |
// allow building rail under bridge
|
|
409 |
if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile))
|
|
410 |
return CMD_ERROR;
|
|
411 |
|
|
412 |
if (ti.type == MP_TUNNELBRIDGE) {
|
|
413 |
if (!EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile)))
|
|
414 |
return CMD_ERROR;
|
|
415 |
|
|
416 |
if ((ti.map5 & 0xF8) != 0xE0)
|
|
417 |
return CMD_ERROR;
|
|
418 |
|
|
419 |
if ( ((ti.map5 & 1) ? 1 : 2) != rail_bit )
|
|
420 |
return CMD_ERROR;
|
|
421 |
|
|
422 |
if (!(flags & DC_EXEC))
|
|
423 |
return _price.remove_rail;
|
|
424 |
|
|
425 |
_map_owner[tile] = OWNER_NONE;
|
|
426 |
_map5[tile] = ti.map5 & 0xC7;
|
|
427 |
} else if (ti.type == MP_STREET) {
|
|
428 |
if (!(ti.map5 & 0xF0))
|
|
429 |
return CMD_ERROR;
|
|
430 |
|
|
431 |
if (ti.map5 & 0xE0)
|
|
432 |
return CMD_ERROR;
|
|
433 |
|
|
434 |
if (ti.map5 & 8) {
|
|
435 |
m5 = 5;
|
|
436 |
if (rail_bit != 1)
|
|
437 |
return CMD_ERROR;
|
|
438 |
} else {
|
|
439 |
m5 = 10;
|
|
440 |
if (rail_bit != 2)
|
|
441 |
return CMD_ERROR;
|
|
442 |
}
|
|
443 |
|
|
444 |
if (!(flags & DC_EXEC))
|
|
445 |
return _price.remove_rail;
|
|
446 |
|
|
447 |
_map5[tile] = m5;
|
|
448 |
_map_owner[tile] = _map3_lo[tile];
|
|
449 |
_map2[tile] = 0;
|
|
450 |
} else {
|
|
451 |
assert(ti.type == MP_RAILWAY);
|
|
452 |
|
|
453 |
if (ti.map5 & RAIL_TYPE_SPECIAL)
|
|
454 |
return CMD_ERROR;
|
|
455 |
|
|
456 |
if (!(ti.map5 & rail_bit))
|
|
457 |
return CMD_ERROR;
|
|
458 |
|
|
459 |
// don't allow remove if there are signals on the track
|
|
460 |
if (ti.map5 & RAIL_TYPE_SIGNALS) {
|
|
461 |
if (_map3_lo[tile] & _signals_table_both[p2])
|
|
462 |
return CMD_ERROR;
|
|
463 |
}
|
|
464 |
|
|
465 |
if (!(flags & DC_EXEC))
|
|
466 |
return _price.remove_rail;
|
|
467 |
|
|
468 |
if ( (_map5[tile] ^= rail_bit) == 0) {
|
|
469 |
DoClearSquare(tile);
|
|
470 |
goto skip_mark_dirty;
|
|
471 |
}
|
|
472 |
}
|
|
473 |
|
|
474 |
/* mark_dirty */
|
|
475 |
MarkTileDirtyByTile(tile);
|
|
476 |
|
|
477 |
skip_mark_dirty:;
|
|
478 |
|
|
479 |
SetSignalsOnBothDir(tile, (byte)p2);
|
|
480 |
|
|
481 |
return _price.remove_rail;
|
|
482 |
}
|
|
483 |
|
|
484 |
static const struct {
|
|
485 |
int8 xinc[16];
|
|
486 |
int8 yinc[16];
|
|
487 |
byte initial[16];
|
|
488 |
} _railbit = {{
|
|
489 |
// 0 1 2 3 4 5
|
|
490 |
16, 0,-16, 0, 16, 0, 0, 0,
|
|
491 |
-16, 0, 0, 16, 0,-16, 0, 0,
|
|
492 |
},{
|
|
493 |
0, 16, 0, 16, 0, 16, 0, 0,
|
|
494 |
0, -16, -16, 0,-16, 0, 0, 0,
|
|
495 |
},{
|
|
496 |
5, 1, 0,4, // normal
|
|
497 |
2, 1, 8,3, // x > sx
|
|
498 |
10, 9, 0,11, // y > sy
|
|
499 |
13, 9, 8,12, // x > sx && y > sy
|
|
500 |
}};
|
|
501 |
|
|
502 |
|
|
503 |
/* Build either NE or NW sequence of tracks.
|
|
504 |
* p1 0:15 - start pt X
|
|
505 |
* p1 16:31 - start pt y
|
|
506 |
*
|
|
507 |
* p2 0:3 - rail type
|
|
508 |
* p2 4:7 - rail direction
|
|
509 |
*/
|
|
510 |
|
|
511 |
|
|
512 |
int32 CmdBuildRailroadTrack(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
513 |
{
|
|
514 |
int sx, sy;
|
|
515 |
int32 ret, total_cost = 0;
|
|
516 |
int railbit;
|
|
517 |
|
|
518 |
if (flags & DC_EXEC)
|
|
519 |
SndPlayTileFx(0x1E, TILE_FROM_XY(x,y));
|
|
520 |
|
|
521 |
/* unpack start point */
|
|
522 |
sx = (p1 & 0xFFFF) & ~0xF;
|
|
523 |
sy = (p1 >> 16) & ~0xF;
|
|
524 |
|
|
525 |
railbit = _railbit.initial[(p2 >> 4) + (x > sx ? 4 : 0) + (y > sy ? 8 : 0)];
|
|
526 |
|
|
527 |
for(;;) {
|
|
528 |
ret = DoCommand(x, y, p2&0xF, railbit&7, flags, CMD_BUILD_SINGLE_RAIL);
|
|
529 |
|
|
530 |
if (ret == CMD_ERROR) {
|
|
531 |
if (_error_message != STR_1007_ALREADY_BUILT)
|
|
532 |
break;
|
|
533 |
} else
|
|
534 |
total_cost += ret;
|
|
535 |
|
|
536 |
if (x == sx && y == sy)
|
|
537 |
break;
|
|
538 |
|
|
539 |
x += _railbit.xinc[railbit];
|
|
540 |
y += _railbit.yinc[railbit];
|
|
541 |
|
|
542 |
// toggle railbit for the diagonal tiles
|
|
543 |
if (railbit & 0x6) railbit ^= 1;
|
|
544 |
}
|
|
545 |
|
|
546 |
if (total_cost == 0)
|
|
547 |
return CMD_ERROR;
|
|
548 |
|
|
549 |
return total_cost;
|
|
550 |
}
|
|
551 |
|
|
552 |
|
|
553 |
/* Remove either NE or NW sequence of tracks.
|
|
554 |
* p1 0:15 - start pt X
|
|
555 |
* p1 16:31 - start pt y
|
|
556 |
*
|
|
557 |
* p2 0:3 - rail type
|
|
558 |
* p2 4:7 - rail direction
|
|
559 |
*/
|
|
560 |
|
|
561 |
|
|
562 |
int32 CmdRemoveRailroadTrack(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
563 |
{
|
|
564 |
int sx, sy;
|
|
565 |
int32 ret, total_cost = 0;
|
|
566 |
int railbit;
|
|
567 |
|
|
568 |
if (flags & DC_EXEC)
|
|
569 |
SndPlayTileFx(0x1E, TILE_FROM_XY(x,y));
|
|
570 |
|
|
571 |
/* unpack start point */
|
|
572 |
sx = (p1 & 0xFFFF) & ~0xF;
|
|
573 |
sy = (p1 >> 16) & ~0xF;
|
|
574 |
|
|
575 |
railbit = _railbit.initial[(p2 >> 4) + (x > sx ? 4 : 0) + (y > sy ? 8 : 0)];
|
|
576 |
|
|
577 |
for(;;) {
|
|
578 |
ret = DoCommand(x, y, p2&0xF, railbit&7, flags, CMD_REMOVE_SINGLE_RAIL);
|
|
579 |
if (ret != CMD_ERROR) total_cost += ret;
|
|
580 |
if (x == sx && y == sy)
|
|
581 |
break;
|
|
582 |
x += _railbit.xinc[railbit];
|
|
583 |
y += _railbit.yinc[railbit];
|
|
584 |
// toggle railbit for the diagonal tiles
|
|
585 |
if (railbit & 0x6) railbit ^= 1;
|
|
586 |
}
|
|
587 |
|
|
588 |
if (total_cost == 0)
|
|
589 |
return CMD_ERROR;
|
|
590 |
|
|
591 |
return total_cost;
|
|
592 |
}
|
|
593 |
|
|
594 |
/* Build a train depot
|
|
595 |
* p1 = rail type
|
|
596 |
* p2 = depot direction
|
|
597 |
*/
|
|
598 |
int32 CmdBuildTrainDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
599 |
{
|
|
600 |
uint tile = TILE_FROM_XY(x,y);
|
|
601 |
int32 cost, ret;
|
|
602 |
Depot *dep;
|
|
603 |
uint tileh;
|
|
604 |
|
|
605 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
606 |
|
|
607 |
if (!EnsureNoVehicle(tile)) return CMD_ERROR;
|
|
608 |
|
|
609 |
tileh = GetTileSlope(tile, NULL);
|
|
610 |
if (tileh != 0) {
|
|
611 |
if (_is_ai_player || !_patches.build_on_slopes || (tileh & 0x10 || !((0x4C >> p2) & tileh) ))
|
|
612 |
return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
|
|
613 |
}
|
|
614 |
|
|
615 |
ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
616 |
if (ret == CMD_ERROR) return CMD_ERROR;
|
|
617 |
cost = ret;
|
|
618 |
|
|
619 |
dep = AllocateDepot();
|
|
620 |
if (dep == NULL)
|
|
621 |
return CMD_ERROR;
|
|
622 |
|
|
623 |
if (flags & DC_EXEC) {
|
|
624 |
if (_current_player == _local_player)
|
|
625 |
_last_built_train_depot_tile = (TileIndex)tile;
|
|
626 |
|
|
627 |
ModifyTile(tile,
|
|
628 |
MP_SETTYPE(MP_RAILWAY) |
|
|
629 |
MP_MAP3LO | MP_MAPOWNER_CURRENT | MP_MAP5,
|
|
630 |
p1, /* map3_lo */
|
|
631 |
p2 | RAIL_TYPE_DEPOT /* map5 */
|
|
632 |
);
|
|
633 |
|
|
634 |
dep->xy = tile;
|
|
635 |
dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
|
|
636 |
|
|
637 |
SetSignalsOnBothDir(tile, (p2&1) ? 2 : 1);
|
|
638 |
|
|
639 |
}
|
|
640 |
|
|
641 |
return cost + _price.build_train_depot;
|
|
642 |
}
|
|
643 |
|
|
644 |
static void MakeDefaultCheckpointName(Checkpoint *cp)
|
|
645 |
{
|
|
646 |
int townidx = ClosestTownFromTile(cp->xy, (uint)-1)->index;
|
|
647 |
Checkpoint *cc;
|
|
648 |
bool used_checkpoint[64];
|
|
649 |
int i;
|
|
650 |
|
|
651 |
memset(used_checkpoint, 0, sizeof(used_checkpoint));
|
|
652 |
|
|
653 |
// find an unused checkpoint number belonging to this town
|
|
654 |
for(cc = _checkpoints; cc != endof(_checkpoints); cc++) {
|
|
655 |
if (cc->xy && cc->town_or_string & 0xC000 && (cc->town_or_string & 0xFF) == townidx)
|
|
656 |
used_checkpoint[(cc->town_or_string >> 8) & 0x3F] = true;
|
|
657 |
}
|
|
658 |
|
|
659 |
for(i=0; used_checkpoint[i] && i!=lengthof(used_checkpoint)-1; i++) {}
|
|
660 |
cp->town_or_string = 0xC000 + (i << 8) + townidx;
|
|
661 |
}
|
|
662 |
|
|
663 |
// find a deleted checkpoint close to a tile.
|
|
664 |
static Checkpoint *FindDeletedCheckpointCloseTo(uint tile)
|
|
665 |
{
|
|
666 |
Checkpoint *cp,*best = NULL;
|
|
667 |
uint thres = 8, cur_dist;
|
|
668 |
|
|
669 |
for(cp = _checkpoints; cp != endof(_checkpoints); cp++) {
|
|
670 |
if (cp->deleted && cp->xy) {
|
|
671 |
cur_dist = GetTileDist(tile, cp->xy);
|
|
672 |
if (cur_dist < thres) {
|
|
673 |
thres = cur_dist;
|
|
674 |
best = cp;
|
|
675 |
}
|
|
676 |
}
|
|
677 |
}
|
|
678 |
return best;
|
|
679 |
}
|
|
680 |
|
|
681 |
/* Convert existing rail to checkpoint */
|
|
682 |
|
|
683 |
int32 CmdBuildTrainCheckpoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
684 |
{
|
|
685 |
uint tile = TILE_FROM_XY(x,y);
|
|
686 |
Checkpoint *cp;
|
|
687 |
uint tileh;
|
|
688 |
uint dir;
|
|
689 |
|
|
690 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
691 |
|
|
692 |
if (!IS_TILETYPE(tile, MP_RAILWAY) || ((dir=0, _map5[tile] != 1) && (dir=1, _map5[tile] != 2)))
|
|
693 |
return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
|
|
694 |
|
|
695 |
if (!CheckTileOwnership(tile))
|
|
696 |
return CMD_ERROR;
|
|
697 |
|
|
698 |
if (!EnsureNoVehicle(tile)) return CMD_ERROR;
|
|
699 |
|
|
700 |
tileh = GetTileSlope(tile, NULL);
|
|
701 |
if (tileh != 0) {
|
|
702 |
if (!_patches.build_on_slopes || tileh & 0x10 || !(tileh & (0x3 << dir)) || !(tileh & ~(0x3 << dir)))
|
|
703 |
return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
|
|
704 |
}
|
|
705 |
|
|
706 |
// check if there is an already existing, deleted, checkpoint close to us that we can reuse.
|
|
707 |
cp = FindDeletedCheckpointCloseTo(tile);
|
|
708 |
if (cp == NULL) {
|
|
709 |
cp = AllocateCheckpoint();
|
|
710 |
if (cp == NULL) return CMD_ERROR;
|
|
711 |
cp->town_or_string = 0;
|
|
712 |
}
|
|
713 |
|
|
714 |
if (flags & DC_EXEC) {
|
|
715 |
ModifyTile(tile, MP_MAP5, RAIL_TYPE_CHECKPOINT | dir);
|
|
716 |
|
|
717 |
cp->deleted = 0;
|
|
718 |
cp->xy = tile;
|
|
719 |
|
|
720 |
if (cp->town_or_string == 0) MakeDefaultCheckpointName(cp); else RedrawCheckpointSign(cp);
|
|
721 |
UpdateCheckpointSign(cp);
|
|
722 |
RedrawCheckpointSign(cp);
|
|
723 |
SetSignalsOnBothDir(tile, dir ? 2 : 1);
|
|
724 |
}
|
|
725 |
|
|
726 |
return _price.build_train_depot;
|
|
727 |
}
|
|
728 |
|
|
729 |
static void DoDeleteCheckpoint(Checkpoint *cp)
|
|
730 |
{
|
|
731 |
cp->xy = 0;
|
|
732 |
DeleteCommandFromVehicleSchedule(((cp-_checkpoints) << 8) + OT_GOTO_CHECKPOINT);
|
|
733 |
if (~cp->town_or_string & 0xC000) DeleteName(cp->town_or_string);
|
|
734 |
RedrawCheckpointSign(cp);
|
|
735 |
}
|
|
736 |
|
|
737 |
// delete checkpoints after a while
|
|
738 |
void CheckpointsDailyLoop()
|
|
739 |
{
|
|
740 |
Checkpoint *cp;
|
|
741 |
for(cp = _checkpoints; cp != endof(_checkpoints); cp++) {
|
|
742 |
if (cp->deleted && !--cp->deleted) {
|
|
743 |
DoDeleteCheckpoint(cp);
|
|
744 |
}
|
|
745 |
}
|
|
746 |
}
|
|
747 |
|
|
748 |
static int32 RemoveTrainCheckpoint(uint tile, uint32 flags, bool justremove)
|
|
749 |
{
|
|
750 |
Checkpoint *cp;
|
|
751 |
|
|
752 |
// make sure it's a checkpoint
|
|
753 |
if (!IS_TILETYPE(tile, MP_RAILWAY) || !IS_RAIL_CHECKPOINT(_map5[tile]))
|
|
754 |
return CMD_ERROR;
|
|
755 |
|
|
756 |
if (!CheckTileOwnership(tile) && !(_current_player==17))
|
|
757 |
return CMD_ERROR;
|
|
758 |
|
|
759 |
if (!EnsureNoVehicle(tile))
|
|
760 |
return CMD_ERROR;
|
|
761 |
|
|
762 |
if (flags & DC_EXEC) {
|
|
763 |
int direction = _map5[tile] & RAIL_CHECKPOINT_TRACK_MASK;
|
|
764 |
|
|
765 |
// mark the checkpoint deleted
|
|
766 |
for(cp=_checkpoints; cp->xy != (TileIndex)tile; cp++) {}
|
|
767 |
cp->deleted = 30; // let it live for this many days before we do the actual deletion.
|
|
768 |
RedrawCheckpointSign(cp);
|
|
769 |
|
|
770 |
if (justremove) {
|
|
771 |
ModifyTile(tile, MP_MAP5, 1<<direction);
|
|
772 |
} else {
|
|
773 |
DoClearSquare(tile);
|
|
774 |
SetSignalsOnBothDir(tile, direction);
|
|
775 |
}
|
|
776 |
}
|
|
777 |
|
|
778 |
return _price.remove_train_depot;
|
|
779 |
}
|
|
780 |
|
|
781 |
int32 CmdRemoveTrainCheckpoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
782 |
{
|
|
783 |
uint tile = TILE_FROM_XY(x,y);
|
|
784 |
return RemoveTrainCheckpoint(tile, flags, true);
|
|
785 |
}
|
|
786 |
|
|
787 |
|
|
788 |
// p1 = id of checkpoint
|
|
789 |
int32 CmdRenameCheckpoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
790 |
{
|
|
791 |
Checkpoint *cp;
|
|
792 |
StringID str;
|
|
793 |
|
|
794 |
if (_decode_parameters[0] != 0) {
|
|
795 |
str = AllocateName((byte*)_decode_parameters, 0);
|
|
796 |
if (str == 0) return CMD_ERROR;
|
|
797 |
|
|
798 |
if (flags & DC_EXEC) {
|
|
799 |
cp = &_checkpoints[p1];
|
|
800 |
if (~cp->town_or_string & 0xC000) DeleteName(cp->town_or_string);
|
|
801 |
cp->town_or_string = str;
|
|
802 |
UpdateCheckpointSign(cp);
|
|
803 |
MarkWholeScreenDirty();
|
|
804 |
} else {
|
|
805 |
DeleteName(str);
|
|
806 |
}
|
|
807 |
} else {
|
|
808 |
if (flags & DC_EXEC) {
|
|
809 |
cp = &_checkpoints[p1];
|
|
810 |
if (~cp->town_or_string & 0xC000) DeleteName(cp->town_or_string);
|
|
811 |
MakeDefaultCheckpointName(cp);
|
|
812 |
UpdateCheckpointSign(cp);
|
|
813 |
MarkWholeScreenDirty();
|
|
814 |
}
|
|
815 |
}
|
|
816 |
return 0;
|
|
817 |
}
|
|
818 |
|
|
819 |
|
|
820 |
/* build signals, p1 = track */
|
|
821 |
int32 CmdBuildSignals(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
822 |
{
|
|
823 |
uint tile;
|
|
824 |
byte m5;
|
|
825 |
int32 cost;
|
|
826 |
int track = p1 & 0x7;
|
|
827 |
|
|
828 |
assert(track >= 0 && track < 6);
|
|
829 |
assert(p2 == 0);
|
|
830 |
|
|
831 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
832 |
|
|
833 |
tile = TILE_FROM_XY(x,y);
|
|
834 |
|
|
835 |
if (!EnsureNoVehicle(tile))
|
|
836 |
return CMD_ERROR;
|
|
837 |
|
|
838 |
_error_message = STR_1005_NO_SUITABLE_RAILROAD_TRACK;
|
|
839 |
|
|
840 |
// must be railway, and not a depot, and it must have a track in the suggested position.
|
|
841 |
if (!IS_TILETYPE(tile, MP_RAILWAY) || (m5=_map5[tile], m5&0x80) || !HASBIT(m5, track))
|
|
842 |
return CMD_ERROR;
|
|
843 |
|
|
844 |
// check rail combination
|
|
845 |
{
|
|
846 |
byte m = m5 & RAIL_BIT_MASK;
|
|
847 |
if (m != RAIL_BIT_DIAG1 && m != RAIL_BIT_DIAG2 && m != RAIL_BIT_UPPER && m != RAIL_BIT_LOWER &&
|
|
848 |
m != RAIL_BIT_LEFT && m != RAIL_BIT_RIGHT && m != (RAIL_BIT_UPPER|RAIL_BIT_LOWER) && m != (RAIL_BIT_LEFT|RAIL_BIT_RIGHT))
|
|
849 |
return CMD_ERROR;
|
|
850 |
}
|
|
851 |
|
|
852 |
// check ownership
|
|
853 |
if (!CheckTileOwnership(tile))
|
|
854 |
return CMD_ERROR;
|
|
855 |
|
|
856 |
// If it had signals previously it's no cost to build.
|
|
857 |
cost = 0;
|
|
858 |
if (!(m5 & RAIL_TYPE_SIGNALS)) {
|
|
859 |
cost = _price.build_signals;
|
|
860 |
}
|
|
861 |
|
|
862 |
if (flags & DC_EXEC) {
|
|
863 |
|
|
864 |
|
|
865 |
if (!(m5 & RAIL_TYPE_SIGNALS)) {
|
|
866 |
_map5[tile] |= RAIL_TYPE_SIGNALS; // change into signals
|
|
867 |
_map2[tile] |= 0xF0; // all signals are on
|
|
868 |
_map3_lo[tile] &= ~0xF0; // no signals built by default
|
|
869 |
_map3_hi[tile] = (p1 & 8) ? 4 : 0;// initial presignal state, semaphores depend on ctrl key
|
|
870 |
goto ignore_presig;
|
|
871 |
}
|
|
872 |
|
|
873 |
if (!(p1 & 8)) {
|
|
874 |
byte a,b,c,d;
|
|
875 |
ignore_presig:
|
|
876 |
a = _signals_table[track]; // signal for this track in one direction
|
|
877 |
b = _signals_table[track + 8]; // signal for this track in the other direction
|
|
878 |
c = a | b;
|
|
879 |
d = _map3_lo[tile] & c; // mask of built signals. it only affects &0xF0
|
|
880 |
|
|
881 |
// Alternate between a|b, b, a
|
|
882 |
if ( d != 0 && d != a) {
|
|
883 |
c = (d==c)?b:a;
|
|
884 |
}
|
|
885 |
|
|
886 |
_map3_lo[tile] = (_map3_lo[tile]&~(a|b)) | c;
|
|
887 |
} else {
|
|
888 |
// toggle between the signal types. Using low 2 bits of map3_hi.
|
|
889 |
_map3_hi[tile] = (_map3_hi[tile] & ~3) | ((_map3_hi[tile] + 1) & 3);
|
|
890 |
}
|
|
891 |
|
|
892 |
MarkTileDirtyByTile(tile);
|
|
893 |
SetSignalsOnBothDir(tile, track);
|
|
894 |
}
|
|
895 |
|
|
896 |
return cost;
|
|
897 |
}
|
|
898 |
|
|
899 |
/* Remove signals
|
|
900 |
* p1 = unused
|
|
901 |
* p2 = unused
|
|
902 |
*/
|
|
903 |
|
|
904 |
int32 CmdRemoveSignals(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
905 |
{
|
|
906 |
TileInfo ti;
|
|
907 |
uint tile;
|
|
908 |
|
|
909 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
910 |
|
|
911 |
FindLandscapeHeight(&ti, x, y);
|
|
912 |
tile = ti.tile;
|
|
913 |
|
|
914 |
/* No vehicle behind. */
|
|
915 |
if (!EnsureNoVehicle(tile))
|
|
916 |
return CMD_ERROR;
|
|
917 |
|
|
918 |
/* Signals can only exist on railways */
|
|
919 |
if (ti.type != MP_RAILWAY)
|
|
920 |
return CMD_ERROR;
|
|
921 |
|
|
922 |
/* Map5 mode is 0x40 when there's signals */
|
|
923 |
if ((ti.map5 & RAIL_TYPE_MASK) != RAIL_TYPE_SIGNALS)
|
|
924 |
return CMD_ERROR;
|
|
925 |
|
|
926 |
/* Who owns the tile? */
|
|
927 |
if (_current_player != OWNER_WATER && !CheckTileOwnership(tile))
|
|
928 |
return CMD_ERROR;
|
|
929 |
|
|
930 |
/* Do it? */
|
|
931 |
if (flags & DC_EXEC) {
|
|
932 |
byte bits = _map5[tile];
|
|
933 |
_map5[tile] &= ~RAIL_TYPE_SIGNALS;
|
|
934 |
_map2[tile] &= ~0xF0;
|
|
935 |
|
|
936 |
/* TTDBUG: this code contains a bug, if a tile contains 2 signals
|
|
937 |
* on separate tracks, it won't work properly for the 2nd track */
|
|
938 |
SetSignalsOnBothDir(tile, FIND_FIRST_BIT(bits & RAIL_BIT_MASK));
|
|
939 |
|
|
940 |
MarkTileDirtyByTile(tile);
|
|
941 |
}
|
|
942 |
|
|
943 |
return _price.remove_signals;
|
|
944 |
}
|
|
945 |
|
|
946 |
typedef int32 DoConvertRailProc(uint tile, uint totype, bool exec);
|
|
947 |
|
|
948 |
static int32 DoConvertRail(uint tile, uint totype, bool exec)
|
|
949 |
{
|
|
950 |
if (!CheckTileOwnership(tile) || !EnsureNoVehicle(tile))
|
|
951 |
return CMD_ERROR;
|
|
952 |
|
|
953 |
// tile is already of requested type?
|
|
954 |
if ( (uint)(_map3_lo[tile] & 0xF) == totype)
|
|
955 |
return CMD_ERROR;
|
|
956 |
|
|
957 |
// change type.
|
|
958 |
if (exec) {
|
|
959 |
_map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype;
|
|
960 |
MarkTileDirtyByTile(tile);
|
|
961 |
}
|
|
962 |
|
|
963 |
return _price.build_rail >> 1;
|
|
964 |
}
|
|
965 |
|
|
966 |
extern int32 DoConvertStationRail(uint tile, uint totype, bool exec);
|
|
967 |
extern int32 DoConvertStreetRail(uint tile, uint totype, bool exec);
|
|
968 |
extern int32 DoConvertTunnelBridgeRail(uint tile, uint totype, bool exec);
|
|
969 |
|
|
970 |
// p1 = start tile
|
|
971 |
// p2 = new railtype
|
|
972 |
int32 CmdConvertRail(int ex, int ey, uint32 flags, uint32 p1, uint32 p2)
|
|
973 |
{
|
|
974 |
int32 ret, cost, money;
|
|
975 |
int sx,sy,x,y;
|
|
976 |
|
|
977 |
// make sure sx,sy are smaller than ex,ey
|
|
978 |
sx = GET_TILE_X(p1)*16;
|
|
979 |
sy = GET_TILE_Y(p1)*16;
|
|
980 |
if (ex < sx) intswap(ex, sx);
|
|
981 |
if (ey < sy) intswap(ey, sy);
|
|
982 |
|
|
983 |
money = GetAvailableMoneyForCommand();
|
|
984 |
ret = false;
|
|
985 |
cost = 0;
|
|
986 |
for(x=sx; x<=ex; x+=16) {
|
|
987 |
for(y=sy; y<=ey; y+=16) {
|
|
988 |
uint tile = TILE_FROM_XY(x,y);
|
|
989 |
DoConvertRailProc *p;
|
|
990 |
|
|
991 |
if (IS_TILETYPE(tile, MP_RAILWAY)) p = DoConvertRail;
|
|
992 |
else if (IS_TILETYPE(tile, MP_STATION)) p = DoConvertStationRail;
|
|
993 |
else if (IS_TILETYPE(tile, MP_STREET)) p = DoConvertStreetRail;
|
|
994 |
else if (IS_TILETYPE(tile, MP_TUNNELBRIDGE)) p = DoConvertTunnelBridgeRail;
|
|
995 |
else continue;
|
|
996 |
|
|
997 |
ret = p(tile, p2, false);
|
|
998 |
if (ret == CMD_ERROR) continue;
|
|
999 |
cost += ret;
|
|
1000 |
|
|
1001 |
if (flags & DC_EXEC) {
|
|
1002 |
if ( (money -= ret) < 0) { _additional_cash_required = ret; return cost - ret; }
|
|
1003 |
p(tile, p2, true);
|
|
1004 |
}
|
|
1005 |
}
|
|
1006 |
}
|
|
1007 |
if (cost == 0) cost = CMD_ERROR;
|
|
1008 |
return cost;
|
|
1009 |
}
|
|
1010 |
|
|
1011 |
static int32 RemoveTrainDepot(uint tile, uint32 flags)
|
|
1012 |
{
|
|
1013 |
if (!CheckTileOwnership(tile) && !(_current_player==17))
|
|
1014 |
return CMD_ERROR;
|
|
1015 |
|
|
1016 |
if (!EnsureNoVehicle(tile))
|
|
1017 |
return CMD_ERROR;
|
|
1018 |
|
|
1019 |
if (flags & DC_EXEC) {
|
|
1020 |
int track = _map5[tile] & RAIL_DEPOT_TRACK_MASK;
|
|
1021 |
|
|
1022 |
DoDeleteDepot(tile);
|
|
1023 |
SetSignalsOnBothDir(tile, track);
|
|
1024 |
}
|
|
1025 |
|
|
1026 |
return _price.remove_train_depot;
|
|
1027 |
}
|
|
1028 |
|
|
1029 |
static int32 ClearTile_Track(uint tile, byte flags) {
|
|
1030 |
int32 cost,ret;
|
|
1031 |
int i;
|
|
1032 |
byte m5;
|
|
1033 |
|
|
1034 |
m5 = _map5[tile];
|
|
1035 |
|
|
1036 |
if (flags & DC_AUTO) {
|
|
1037 |
if (m5 & RAIL_TYPE_SPECIAL)
|
|
1038 |
return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
|
|
1039 |
|
|
1040 |
if (_map_owner[tile] != _current_player)
|
|
1041 |
return_cmd_error(STR_1024_AREA_IS_OWNED_BY_ANOTHER);
|
|
1042 |
|
|
1043 |
return_cmd_error(STR_1008_MUST_REMOVE_RAILROAD_TRACK);
|
|
1044 |
}
|
|
1045 |
|
|
1046 |
cost = 0;
|
|
1047 |
|
|
1048 |
|
|
1049 |
if ( (m5 & RAIL_TYPE_MASK) == RAIL_TYPE_NORMAL) {
|
|
1050 |
regular_track:;
|
|
1051 |
for(i=0; m5; i++, m5>>=1) {
|
|
1052 |
if (m5 & 1) {
|
|
1053 |
ret = DoCommandByTile(tile, 0, i, flags, CMD_REMOVE_SINGLE_RAIL);
|
|
1054 |
if (ret == CMD_ERROR)
|
|
1055 |
return CMD_ERROR;
|
|
1056 |
cost += ret;
|
|
1057 |
}
|
|
1058 |
}
|
|
1059 |
return cost;
|
|
1060 |
|
|
1061 |
} else if ((m5 & RAIL_TYPE_MASK)==RAIL_TYPE_SIGNALS) {
|
|
1062 |
cost = DoCommandByTile(tile, 0, 0, flags, CMD_REMOVE_SIGNALS);
|
|
1063 |
if (cost == CMD_ERROR)
|
|
1064 |
return CMD_ERROR;
|
|
1065 |
m5 &= RAIL_BIT_MASK;
|
|
1066 |
if (flags & DC_EXEC)
|
|
1067 |
goto regular_track;
|
|
1068 |
return cost + _price.remove_rail;
|
|
1069 |
} else if ( (m5 & (RAIL_TYPE_MASK|RAIL_DEPOT_UNUSED_BITS)) == RAIL_TYPE_DEPOT) {
|
|
1070 |
return RemoveTrainDepot(tile, flags);
|
|
1071 |
} else if ( (m5 & (RAIL_TYPE_MASK|RAIL_CHECKPOINT_UNUSED_BITS)) == RAIL_TYPE_CHECKPOINT) {
|
|
1072 |
return RemoveTrainCheckpoint(tile, flags, false);
|
|
1073 |
} else
|
|
1074 |
return CMD_ERROR;
|
|
1075 |
}
|
|
1076 |
|
|
1077 |
|
|
1078 |
|
|
1079 |
typedef struct DrawTrackSeqStruct {
|
|
1080 |
uint16 image;
|
|
1081 |
byte subcoord_x;
|
|
1082 |
byte subcoord_y;
|
|
1083 |
byte width;
|
|
1084 |
byte height;
|
|
1085 |
} DrawTrackSeqStruct;
|
|
1086 |
|
|
1087 |
#include "table/track_land.h"
|
|
1088 |
|
|
1089 |
// used for presignals
|
|
1090 |
static const SpriteID _signal_base_sprites[16] = {
|
|
1091 |
0x4FB,
|
|
1092 |
0x1323,
|
|
1093 |
0x1333,
|
|
1094 |
0x1343,
|
|
1095 |
|
|
1096 |
// use semaphores instead of signals?
|
|
1097 |
0x1353,
|
|
1098 |
0x1363,
|
|
1099 |
0x1373,
|
|
1100 |
0x1383,
|
|
1101 |
|
|
1102 |
// mirrored versions
|
|
1103 |
0x4FB,
|
|
1104 |
0x1323,
|
|
1105 |
0x1333,
|
|
1106 |
0x1343,
|
|
1107 |
|
|
1108 |
0x13C6,
|
|
1109 |
0x13D6,
|
|
1110 |
0x13E6,
|
|
1111 |
0x13F6,
|
|
1112 |
};
|
|
1113 |
|
|
1114 |
// used to determine the side of the road for the signal
|
|
1115 |
static const byte _signal_position[24] = {
|
|
1116 |
/* original: left side position */
|
|
1117 |
0x58,0x1E,0xE1,0xB9,0x01,0xA3,0x4B,0xEE,0x3B,0xD4,0x43,0xBD,
|
|
1118 |
/* patch: ride side position */
|
|
1119 |
0x1E,0xAC,0x64,0xE1,0x4A,0x10,0xEE,0xC5,0xDB,0x34,0x4D,0xB3
|
|
1120 |
};
|
|
1121 |
|
|
1122 |
static void DrawSignalHelper(TileInfo *ti, byte condition, uint32 image_and_pos)
|
|
1123 |
{
|
|
1124 |
bool otherside = _opt.road_side & _patches.signal_side;
|
|
1125 |
|
|
1126 |
uint v = _signal_position[(image_and_pos & 0xF) + (otherside ? 12 : 0)];
|
|
1127 |
uint x = ti->x | (v&0xF);
|
|
1128 |
uint y = ti->y | (v>>4);
|
|
1129 |
uint sprite = _signal_base_sprites[(_map3_hi[ti->tile] & 7) + (otherside ? 8 : 0)] + (image_and_pos>>4) + ((condition != 0) ? 1 : 0);
|
|
1130 |
AddSortableSpriteToDraw(sprite, x, y, 1, 1, 10, GetSlopeZ(x,y));
|
|
1131 |
}
|
|
1132 |
|
|
1133 |
static uint32 _drawtile_track_palette;
|
|
1134 |
|
|
1135 |
|
|
1136 |
static void DrawTrackFence_NW(TileInfo *ti)
|
|
1137 |
{
|
|
1138 |
uint32 image = 0x515;
|
|
1139 |
if (ti->tileh != 0) {
|
|
1140 |
image = 0x519;
|
|
1141 |
if (!(ti->tileh & 2)) {
|
|
1142 |
image = 0x51B;
|
|
1143 |
}
|
|
1144 |
}
|
|
1145 |
AddSortableSpriteToDraw(image | _drawtile_track_palette,
|
|
1146 |
ti->x, ti->y+1, 16, 1, 4, ti->z);
|
|
1147 |
}
|
|
1148 |
|
|
1149 |
static void DrawTrackFence_SE(TileInfo *ti)
|
|
1150 |
{
|
|
1151 |
uint32 image = 0x515;
|
|
1152 |
if (ti->tileh != 0) {
|
|
1153 |
image = 0x519;
|
|
1154 |
if (!(ti->tileh & 2)) {
|
|
1155 |
image = 0x51B;
|
|
1156 |
}
|
|
1157 |
}
|
|
1158 |
AddSortableSpriteToDraw(image | _drawtile_track_palette,
|
|
1159 |
ti->x, ti->y+15, 16, 1, 4, ti->z);
|
|
1160 |
}
|
|
1161 |
|
|
1162 |
static void DrawTrackFence_NW_SE(TileInfo *ti)
|
|
1163 |
{
|
|
1164 |
DrawTrackFence_NW(ti);
|
|
1165 |
DrawTrackFence_SE(ti);
|
|
1166 |
}
|
|
1167 |
|
|
1168 |
static void DrawTrackFence_NE(TileInfo *ti)
|
|
1169 |
{
|
|
1170 |
uint32 image = 0x516;
|
|
1171 |
if (ti->tileh != 0) {
|
|
1172 |
image = 0x51A;
|
|
1173 |
if (!(ti->tileh & 2)) {
|
|
1174 |
image = 0x51C;
|
|
1175 |
}
|
|
1176 |
}
|
|
1177 |
AddSortableSpriteToDraw(image | _drawtile_track_palette,
|
|
1178 |
ti->x+1, ti->y, 1, 16, 4, ti->z);
|
|
1179 |
}
|
|
1180 |
|
|
1181 |
static void DrawTrackFence_SW(TileInfo *ti)
|
|
1182 |
{
|
|
1183 |
uint32 image = 0x516;
|
|
1184 |
if (ti->tileh != 0) {
|
|
1185 |
image = 0x51A;
|
|
1186 |
if (!(ti->tileh & 2)) {
|
|
1187 |
image = 0x51C;
|
|
1188 |
}
|
|
1189 |
}
|
|
1190 |
AddSortableSpriteToDraw(image | _drawtile_track_palette,
|
|
1191 |
ti->x+15, ti->y, 1, 16, 4, ti->z);
|
|
1192 |
}
|
|
1193 |
|
|
1194 |
static void DrawTrackFence_NE_SW(TileInfo *ti)
|
|
1195 |
{
|
|
1196 |
DrawTrackFence_NE(ti);
|
|
1197 |
DrawTrackFence_SW(ti);
|
|
1198 |
}
|
|
1199 |
|
|
1200 |
static void DrawTrackFence_NS_1(TileInfo *ti)
|
|
1201 |
{
|
|
1202 |
int z = ti->z;
|
|
1203 |
if (ti->tileh & 1)
|
|
1204 |
z += 8;
|
|
1205 |
AddSortableSpriteToDraw(0x517 | _drawtile_track_palette,
|
|
1206 |
ti->x + 8, ti->y + 8, 1, 1, 4, z);
|
|
1207 |
}
|
|
1208 |
|
|
1209 |
static void DrawTrackFence_NS_2(TileInfo *ti)
|
|
1210 |
{
|
|
1211 |
int z = ti->z;
|
|
1212 |
if (ti->tileh & 4)
|
|
1213 |
z += 8;
|
|
1214 |
AddSortableSpriteToDraw(0x517 | _drawtile_track_palette,
|
|
1215 |
ti->x + 8, ti->y + 8, 1, 1, 4, z);
|
|
1216 |
}
|
|
1217 |
|
|
1218 |
static void DrawTrackFence_WE_1(TileInfo *ti)
|
|
1219 |
{
|
|
1220 |
int z = ti->z;
|
|
1221 |
if (ti->tileh & 8)
|
|
1222 |
z += 8;
|
|
1223 |
AddSortableSpriteToDraw(0x518 | _drawtile_track_palette,
|
|
1224 |
ti->x + 8, ti->y + 8, 1, 1, 4, z);
|
|
1225 |
}
|
|
1226 |
|
|
1227 |
static void DrawTrackFence_WE_2(TileInfo *ti)
|
|
1228 |
{
|
|
1229 |
int z = ti->z;
|
|
1230 |
if (ti->tileh & 2)
|
|
1231 |
z += 8;
|
|
1232 |
AddSortableSpriteToDraw(0x518 | _drawtile_track_palette,
|
|
1233 |
ti->x + 8, ti->y + 8, 1, 1, 4, z);
|
|
1234 |
}
|
|
1235 |
|
|
1236 |
static void DetTrackDrawProc_Null(TileInfo *ti)
|
|
1237 |
{
|
|
1238 |
/* nothing should be here */
|
|
1239 |
}
|
|
1240 |
|
|
1241 |
typedef void DetailedTrackProc(TileInfo *ti);
|
|
1242 |
DetailedTrackProc * const _detailed_track_proc[16] = {
|
|
1243 |
DetTrackDrawProc_Null,
|
|
1244 |
DetTrackDrawProc_Null,
|
|
1245 |
|
|
1246 |
DrawTrackFence_NW,
|
|
1247 |
DrawTrackFence_SE,
|
|
1248 |
DrawTrackFence_NW_SE,
|
|
1249 |
|
|
1250 |
DrawTrackFence_NE,
|
|
1251 |
DrawTrackFence_SW,
|
|
1252 |
DrawTrackFence_NE_SW,
|
|
1253 |
|
|
1254 |
DrawTrackFence_NS_1,
|
|
1255 |
DrawTrackFence_NS_2,
|
|
1256 |
|
|
1257 |
DrawTrackFence_WE_1,
|
|
1258 |
DrawTrackFence_WE_2,
|
|
1259 |
|
|
1260 |
DetTrackDrawProc_Null,
|
|
1261 |
DetTrackDrawProc_Null,
|
|
1262 |
DetTrackDrawProc_Null,
|
|
1263 |
DetTrackDrawProc_Null,
|
|
1264 |
};
|
|
1265 |
|
|
1266 |
static void DrawTile_Track(TileInfo *ti)
|
|
1267 |
{
|
|
1268 |
uint32 tracktype_offs, image;
|
|
1269 |
byte m5;
|
|
1270 |
|
|
1271 |
_drawtile_track_palette = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_map_owner[ti->tile]));
|
|
1272 |
|
|
1273 |
tracktype_offs = (_map3_lo[ti->tile] & 0xF) * TRACKTYPE_SPRITE_PITCH;
|
|
1274 |
|
|
1275 |
m5 = (byte)ti->map5;
|
|
1276 |
if (!(m5 & RAIL_TYPE_SPECIAL)) {
|
|
1277 |
bool special;
|
|
1278 |
|
|
1279 |
m5 &= RAIL_BIT_MASK;
|
|
1280 |
|
|
1281 |
special = false;
|
|
1282 |
|
|
1283 |
// select the sprite to use based on the map5 byte.
|
|
1284 |
(image = 0x3F3, m5 == RAIL_BIT_DIAG2) ||
|
|
1285 |
(image++, m5 == RAIL_BIT_DIAG1) ||
|
|
1286 |
(image++, m5 == RAIL_BIT_UPPER) ||
|
|
1287 |
(image++, m5 == RAIL_BIT_LOWER) ||
|
|
1288 |
(image++, m5 == RAIL_BIT_RIGHT) ||
|
|
1289 |
(image++, m5 == RAIL_BIT_LEFT) ||
|
|
1290 |
(image++, m5 == (RAIL_BIT_DIAG1|RAIL_BIT_DIAG2)) ||
|
|
1291 |
|
|
1292 |
(image = 0x40B, m5 == (RAIL_BIT_UPPER|RAIL_BIT_LOWER)) ||
|
|
1293 |
(image++, m5 == (RAIL_BIT_LEFT|RAIL_BIT_RIGHT)) ||
|
|
1294 |
|
|
1295 |
(special=true, false) ||
|
|
1296 |
|
|
1297 |
(image = 0x3FA, !(m5 & (RAIL_BIT_RIGHT|RAIL_BIT_UPPER|RAIL_BIT_DIAG1))) ||
|
|
1298 |
(image++, !(m5 & (RAIL_BIT_LEFT|RAIL_BIT_LOWER|RAIL_BIT_DIAG1))) ||
|
|
1299 |
(image++, !(m5 & (RAIL_BIT_LEFT|RAIL_BIT_UPPER|RAIL_BIT_DIAG2))) ||
|
|
1300 |
(image++, !(m5 & (RAIL_BIT_RIGHT|RAIL_BIT_LOWER|RAIL_BIT_DIAG2))) ||
|
|
1301 |
(image++, true);
|
|
1302 |
|
|
1303 |
if (ti->tileh != 0) {
|
|
1304 |
int f = GetRailFoundation(ti->tileh, ti->map5 & 0x3F);
|
|
1305 |
if (f) DrawFoundation(ti, f);
|
|
1306 |
|
|
1307 |
// default sloped sprites..
|
|
1308 |
if (ti->tileh != 0) image = _track_sloped_sprites[ti->tileh - 1] + 0x3F3;
|
|
1309 |
}
|
|
1310 |
|
|
1311 |
if ((_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK)==RAIL_GROUND_BROWN)
|
|
1312 |
image = (image & 0xFFFF) | 0x3178000; // use a brown palette
|
|
1313 |
else if ((_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK)==RAIL_GROUND_ICE_DESERT)
|
|
1314 |
image += 26;
|
|
1315 |
|
|
1316 |
DrawGroundSprite(image + tracktype_offs);
|
|
1317 |
|
|
1318 |
if (special) {
|
|
1319 |
if (m5 & RAIL_BIT_DIAG1) DrawGroundSprite(0x3ED + tracktype_offs);
|
|
1320 |
if (m5 & RAIL_BIT_DIAG2) DrawGroundSprite(0x3EE + tracktype_offs);
|
|
1321 |
if (m5 & RAIL_BIT_UPPER) DrawGroundSprite(0x3EF + tracktype_offs);
|
|
1322 |
if (m5 & RAIL_BIT_LOWER) DrawGroundSprite(0x3F0 + tracktype_offs);
|
|
1323 |
if (m5 & RAIL_BIT_LEFT) DrawGroundSprite(0x3F2 + tracktype_offs);
|
|
1324 |
if (m5 & RAIL_BIT_RIGHT) DrawGroundSprite(0x3F1 + tracktype_offs);
|
|
1325 |
}
|
|
1326 |
|
|
1327 |
if (_display_opt & DO_FULL_DETAIL) {
|
|
1328 |
_detailed_track_proc[_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK](ti);
|
|
1329 |
}
|
|
1330 |
|
|
1331 |
/* draw signals also? */
|
|
1332 |
if (!(ti->map5 & RAIL_TYPE_SIGNALS))
|
|
1333 |
return;
|
|
1334 |
|
|
1335 |
{
|
|
1336 |
byte m23;
|
|
1337 |
|
|
1338 |
m23 = (_map3_lo[ti->tile] >> 4) | (_map2[ti->tile] & 0xF0);
|
|
1339 |
|
|
1340 |
#define HAS_SIGNAL(x) (m23 & (byte)(0x1 << (x)))
|
|
1341 |
#define ISON_SIGNAL(x) (m23 & (byte)(0x10 << (x)))
|
|
1342 |
#define MAYBE_DRAW_SIGNAL(x,y,z) if (HAS_SIGNAL(x)) DrawSignalHelper(ti, ISON_SIGNAL(x), ((y-0x4FB) << 4)|(z))
|
|
1343 |
|
|
1344 |
if (!(m5 & RAIL_BIT_DIAG2)) {
|
|
1345 |
if (!(m5 & RAIL_BIT_DIAG1)) {
|
|
1346 |
if (m5 & RAIL_BIT_LEFT) {
|
|
1347 |
MAYBE_DRAW_SIGNAL(2, 0x509, 0);
|
|
1348 |
MAYBE_DRAW_SIGNAL(3, 0x507, 1);
|
|
1349 |
}
|
|
1350 |
if (m5 & RAIL_BIT_RIGHT) {
|
|
1351 |
MAYBE_DRAW_SIGNAL(0, 0x509, 2);
|
|
1352 |
MAYBE_DRAW_SIGNAL(1, 0x507, 3);
|
|
1353 |
}
|
|
1354 |
if (m5 & RAIL_BIT_UPPER) {
|
|
1355 |
MAYBE_DRAW_SIGNAL(3, 0x505, 4);
|
|
1356 |
MAYBE_DRAW_SIGNAL(2, 0x503, 5);
|
|
1357 |
}
|
|
1358 |
if (m5 & RAIL_BIT_LOWER) {
|
|
1359 |
MAYBE_DRAW_SIGNAL(1, 0x505, 6);
|
|
1360 |
MAYBE_DRAW_SIGNAL(0, 0x503, 7);
|
|
1361 |
}
|
|
1362 |
} else {
|
|
1363 |
MAYBE_DRAW_SIGNAL(3, 0x4FB, 8);
|
|
1364 |
MAYBE_DRAW_SIGNAL(2, 0x4FD, 9);
|
|
1365 |
}
|
|
1366 |
} else {
|
|
1367 |
MAYBE_DRAW_SIGNAL(3, 0x4FF, 10);
|
|
1368 |
MAYBE_DRAW_SIGNAL(2, 0x501, 11);
|
|
1369 |
}
|
|
1370 |
}
|
|
1371 |
} else {
|
|
1372 |
const byte *s;
|
|
1373 |
const DrawTrackSeqStruct *drss;
|
|
1374 |
|
|
1375 |
if (!(m5 & (RAIL_TYPE_MASK&~RAIL_TYPE_SPECIAL)))
|
|
1376 |
return;
|
|
1377 |
|
|
1378 |
if (ti->tileh != 0) { DrawFoundation(ti, ti->tileh); }
|
|
1379 |
|
|
1380 |
s = _track_depot_layout_table[m5 & 0x3F];
|
|
1381 |
|
|
1382 |
image = *(uint16*)s;
|
|
1383 |
if (image & 0x8000) image = (image & 0x7FFF) + tracktype_offs;
|
|
1384 |
|
|
1385 |
// adjust ground tile for desert
|
|
1386 |
// (don't adjust for arctic, because snow in depots looks weird)
|
|
1387 |
if ((_map2[ti->tile] & RAIL_MAP2LO_GROUND_MASK)==RAIL_GROUND_ICE_DESERT && _opt.landscape == LT_DESERT)
|
|
1388 |
{
|
|
1389 |
if(image!=3981)
|
|
1390 |
image += 26; // tile with tracks
|
|
1391 |
else
|
|
1392 |
image = 4550; // flat ground
|
|
1393 |
}
|
|
1394 |
|
|
1395 |
DrawGroundSprite(image);
|
|
1396 |
|
|
1397 |
drss = (const DrawTrackSeqStruct*)(s + sizeof(uint16));
|
|
1398 |
|
|
1399 |
while ((image=drss->image) != 0) {
|
|
1400 |
if (image & 0x8000)
|
|
1401 |
image |= _drawtile_track_palette;
|
|
1402 |
image += tracktype_offs;
|
|
1403 |
if (!(_display_opt & DO_TRANS_BUILDINGS))
|
|
1404 |
image = (image & 0x3FFF) | 0x3224000;
|
|
1405 |
AddSortableSpriteToDraw(image, ti->x | drss->subcoord_x,
|
|
1406 |
ti->y | drss->subcoord_y, drss->width, drss->height, 0x17, ti->z);
|
|
1407 |
drss++;
|
|
1408 |
}
|
|
1409 |
}
|
|
1410 |
}
|
|
1411 |
|
|
1412 |
void DrawTrainDepotSprite(int x, int y, int image, int railtype)
|
|
1413 |
{
|
|
1414 |
uint32 ormod, img;
|
|
1415 |
const DrawTrackSeqStruct *dtss;
|
|
1416 |
const byte *t;
|
|
1417 |
|
|
1418 |
/* baseimage */
|
|
1419 |
railtype *= TRACKTYPE_SPRITE_PITCH;
|
|
1420 |
|
|
1421 |
ormod = PLAYER_SPRITE_COLOR(_local_player);
|
|
1422 |
|
|
1423 |
t = _track_depot_layout_table[image];
|
|
1424 |
|
|
1425 |
x+=33;
|
|
1426 |
y+=17;
|
|
1427 |
|
|
1428 |
img = *(uint16*)t;
|
|
1429 |
if (img & 0x8000) img = (img & 0x7FFF) + railtype;
|
|
1430 |
DrawSprite(img, x, y);
|
|
1431 |
|
|
1432 |
for(dtss = (DrawTrackSeqStruct *)(t + sizeof(uint16)); dtss->image != 0; dtss++) {
|
|
1433 |
Point pt = RemapCoords(dtss->subcoord_x, dtss->subcoord_y, 0);
|
|
1434 |
image = dtss->image;
|
|
1435 |
if (image & 0x8000) image |= ormod;
|
|
1436 |
DrawSprite(image + railtype, x + pt.x, y + pt.y);
|
|
1437 |
}
|
|
1438 |
}
|
|
1439 |
|
|
1440 |
#define NUM_SSD_ENTRY 256
|
|
1441 |
#define NUM_SSD_STACK 32
|
|
1442 |
|
|
1443 |
typedef struct SetSignalsData {
|
|
1444 |
int cur;
|
|
1445 |
int cur_stack;
|
|
1446 |
bool stop;
|
|
1447 |
bool has_presignal;
|
|
1448 |
|
|
1449 |
// presignal info
|
|
1450 |
int presignal_exits;
|
|
1451 |
int presignal_exits_free;
|
|
1452 |
|
|
1453 |
// these are used to keep track of the signals that change.
|
|
1454 |
byte bit[NUM_SSD_ENTRY];
|
|
1455 |
TileIndex tile[NUM_SSD_ENTRY];
|
|
1456 |
|
|
1457 |
// these are used to keep track of the stack that modifies presignals recursively
|
|
1458 |
TileIndex next_tile[NUM_SSD_STACK];
|
|
1459 |
byte next_dir[NUM_SSD_STACK];
|
|
1460 |
|
|
1461 |
} SetSignalsData;
|
|
1462 |
|
|
1463 |
static bool SetSignalsEnumProc(uint tile, SetSignalsData *ssd, int track, uint length, byte *state)
|
|
1464 |
{
|
|
1465 |
// the tile has signals?
|
|
1466 |
if (IS_TILETYPE(tile, MP_RAILWAY)) {
|
|
1467 |
if ((_map5[tile]&RAIL_TYPE_MASK) == RAIL_TYPE_SIGNALS) {
|
|
1468 |
if ((_map3_lo[tile] & _signals_table_both[track]) != 0) {
|
|
1469 |
|
|
1470 |
// is the signal pointing in to the segment existing?
|
|
1471 |
if ((_map3_lo[tile] & _signals_table[track]) != 0) {
|
|
1472 |
// yes, add the signal to the list of signals
|
|
1473 |
if (ssd->cur != NUM_SSD_ENTRY) {
|
|
1474 |
ssd->tile[ssd->cur] = tile; // remember the tile index
|
|
1475 |
ssd->bit[ssd->cur] = track; // and the controlling bit number
|
|
1476 |
ssd->cur++;
|
|
1477 |
}
|
|
1478 |
|
|
1479 |
// remember if this block has a presignal.
|
|
1480 |
ssd->has_presignal |= (_map3_hi[tile]&1);
|
|
1481 |
}
|
|
1482 |
|
|
1483 |
// is this an exit signal that points out from the segment?
|
|
1484 |
if ((_map3_hi[tile]&2) && _map3_lo[tile]&_signals_table_other[track]) {
|
|
1485 |
ssd->presignal_exits++;
|
|
1486 |
if ((_map2[tile]&_signals_table_other[track]) != 0)
|
|
1487 |
ssd->presignal_exits_free++;
|
|
1488 |
}
|
|
1489 |
|
|
1490 |
return true;
|
|
1491 |
}
|
|
1492 |
} else if (IS_RAIL_DEPOT(_map5[tile]))
|
|
1493 |
return true; // don't look further if the tile is a depot
|
|
1494 |
}
|
|
1495 |
return false;
|
|
1496 |
}
|
|
1497 |
|
|
1498 |
static void SetSignalsAfterProc(TrackPathFinder *tpf)
|
|
1499 |
{
|
|
1500 |
SetSignalsData *ssd = tpf->userdata;
|
|
1501 |
Vehicle *v;
|
|
1502 |
uint tile, hash, val, offs;
|
|
1503 |
TrackPathFinderLink *link;
|
|
1504 |
|
|
1505 |
ssd->stop = false;
|
|
1506 |
|
|
1507 |
// for each train, check if it is in the segment.
|
|
1508 |
// then we know that the signal should be red.
|
|
1509 |
FOR_ALL_VEHICLES(v) {
|
|
1510 |
if (v->type != VEH_Train)
|
|
1511 |
continue;
|
|
1512 |
|
|
1513 |
tile = v->tile;
|
|
1514 |
if (v->u.rail.track == 0x40) { tile = GetVehicleOutOfTunnelTile(v); }
|
|
1515 |
|
|
1516 |
hash = PATHFIND_HASH_TILE(tile);
|
|
1517 |
|
|
1518 |
val = tpf->hash_head[hash];
|
|
1519 |
if (val == 0)
|
|
1520 |
continue;
|
|
1521 |
|
|
1522 |
if (!(val & 0x8000)) {
|
|
1523 |
if ((TileIndex)tile == tpf->hash_tile[hash])
|
|
1524 |
goto check_val;
|
|
1525 |
} else {
|
|
1526 |
offs = tpf->hash_tile[hash];
|
|
1527 |
do {
|
|
1528 |
link = PATHFIND_GET_LINK_PTR(tpf, offs);
|
|
1529 |
if ( (TileIndex)tile == link->tile) {
|
|
1530 |
val = link->flags;
|
|
1531 |
check_val:;
|
|
1532 |
// the train is on the track, in either direction?
|
|
1533 |
if (val & (v->u.rail.track + (v->u.rail.track<<8))) {
|
|
1534 |
ssd->stop = true;
|
|
1535 |
return;
|
|
1536 |
}
|
|
1537 |
break;
|
|
1538 |
}
|
|
1539 |
} while ((offs=link->next) != 0xFFFF);
|
|
1540 |
}
|
|
1541 |
}
|
|
1542 |
}
|
|
1543 |
|
|
1544 |
static const byte _dir_from_track[14] = {
|
|
1545 |
0,1,0,1,2,1, 0,0,
|
|
1546 |
2,3,3,2,3,0,
|
|
1547 |
};
|
|
1548 |
|
|
1549 |
|
|
1550 |
static void ChangeSignalStates(SetSignalsData *ssd)
|
|
1551 |
{
|
|
1552 |
int i;
|
|
1553 |
|
|
1554 |
// thinking about presignals...
|
|
1555 |
// the presignal is green if,
|
|
1556 |
// if no train is in the segment AND
|
|
1557 |
// there is at least one green exit signal OR
|
|
1558 |
// there are no exit signals in the segment
|
|
1559 |
|
|
1560 |
// then mark the signals in the segment accordingly
|
|
1561 |
for(i=0; i!=ssd->cur; i++) {
|
|
1562 |
uint tile = ssd->tile[i];
|
|
1563 |
byte bit = _signals_table[ssd->bit[i]];
|
|
1564 |
byte m2 = _map2[tile];
|
|
1565 |
|
|
1566 |
// presignals don't turn green if there is at least one presignal exit and none are free
|
|
1567 |
if (_map3_hi[tile] & 1) {
|
|
1568 |
int ex = ssd->presignal_exits, exfree = ssd->presignal_exits_free;
|
|
1569 |
|
|
1570 |
// subtract for dual combo signals so they don't count themselves
|
|
1571 |
if (_map3_hi[tile]&2 && _map3_lo[tile]&_signals_table_other[ssd->bit[i]]) {
|
|
1572 |
ex--;
|
|
1573 |
if ((_map2[tile]&_signals_table_other[ssd->bit[i]]) != 0) exfree--;
|
|
1574 |
}
|
|
1575 |
|
|
1576 |
// if we have exits and none are free, make red.
|
|
1577 |
if (ex && !exfree) goto make_red;
|
|
1578 |
}
|
|
1579 |
|
|
1580 |
// check if the signal is unaffected.
|
|
1581 |
if (ssd->stop) {
|
|
1582 |
make_red:
|
|
1583 |
// turn red
|
|
1584 |
if ( (bit&m2) == 0 )
|
|
1585 |
continue;
|
|
1586 |
} else {
|
|
1587 |
// turn green
|
|
1588 |
if ( (bit&m2) != 0 )
|
|
1589 |
continue;
|
|
1590 |
}
|
|
1591 |
|
|
1592 |
// Update signals on the other side of this exit signal, it changed.
|
|
1593 |
// If this segment has presignals, then we treat exit signals going into the segment as normal signals.
|
|
1594 |
if (_map3_hi[tile]&2 && (_map3_hi[tile]&1 || !ssd->has_presignal)) {
|
|
1595 |
if (ssd->cur_stack != NUM_SSD_STACK) {
|
|
1596 |
ssd->next_tile[ssd->cur_stack] = tile;
|
|
1597 |
ssd->next_dir[ssd->cur_stack] = _dir_from_track[ssd->bit[i]];
|
|
1598 |
ssd->cur_stack++;
|
|
1599 |
} else {
|
|
1600 |
printf("NUM_SSD_STACK too small\n");
|
|
1601 |
}
|
|
1602 |
}
|
|
1603 |
|
|
1604 |
// it changed, so toggle it
|
|
1605 |
_map2[tile] = m2 ^ bit;
|
|
1606 |
MarkTileDirtyByTile(tile);
|
|
1607 |
}
|
|
1608 |
}
|
|
1609 |
|
|
1610 |
|
|
1611 |
bool UpdateSignalsOnSegment(uint tile, byte direction)
|
|
1612 |
{
|
|
1613 |
SetSignalsData ssd;
|
|
1614 |
int result = -1;
|
|
1615 |
|
|
1616 |
ssd.cur_stack = 0;
|
|
1617 |
direction>>=1;
|
|
1618 |
|
|
1619 |
for(;;) {
|
|
1620 |
// go through one segment and update all signals pointing into that segment.
|
|
1621 |
ssd.cur = ssd.presignal_exits = ssd.presignal_exits_free = 0;
|
|
1622 |
ssd.has_presignal = false;
|
|
1623 |
|
|
1624 |
FollowTrack(tile, 0xC000, direction, (TPFEnumProc*)SetSignalsEnumProc, SetSignalsAfterProc, &ssd);
|
|
1625 |
ChangeSignalStates(&ssd);
|
|
1626 |
|
|
1627 |
// remember the result only for the first iteration.
|
|
1628 |
if (result < 0) result = ssd.stop;
|
|
1629 |
|
|
1630 |
// if any exit signals were changed, we need to keep going to modify the stuff behind those.
|
|
1631 |
if(!ssd.cur_stack)
|
|
1632 |
break;
|
|
1633 |
|
|
1634 |
// one or more exit signals were changed, so we need to update another segment too.
|
|
1635 |
tile = ssd.next_tile[--ssd.cur_stack];
|
|
1636 |
direction = ssd.next_dir[ssd.cur_stack];
|
|
1637 |
}
|
|
1638 |
|
|
1639 |
return (bool)result;
|
|
1640 |
}
|
|
1641 |
|
|
1642 |
void SetSignalsOnBothDir(uint tile, byte track)
|
|
1643 |
{
|
|
1644 |
static const byte _search_dir_1[6] = {1, 3, 1, 3, 5, 3};
|
|
1645 |
static const byte _search_dir_2[6] = {5, 7, 7, 5, 7, 1};
|
|
1646 |
|
|
1647 |
UpdateSignalsOnSegment(tile, _search_dir_1[track]);
|
|
1648 |
UpdateSignalsOnSegment(tile, _search_dir_2[track]);
|
|
1649 |
}
|
|
1650 |
|
|
1651 |
uint GetSlopeZ_Track(TileInfo *ti)
|
|
1652 |
{
|
|
1653 |
uint z = ti->z;
|
|
1654 |
int th = ti->tileh;
|
|
1655 |
|
|
1656 |
// check if it's a foundation
|
|
1657 |
if (ti->tileh != 0) {
|
|
1658 |
if ((ti->map5 & 0x80) == 0) {
|
|
1659 |
uint f = GetRailFoundation(ti->tileh, ti->map5 & 0x3F);
|
|
1660 |
if (f != 0) {
|
|
1661 |
if (f < 15) {
|
|
1662 |
// leveled foundation
|
|
1663 |
return z + 8;
|
|
1664 |
}
|
|
1665 |
// inclined foundation
|
|
1666 |
th = _inclined_tileh[f - 15];
|
|
1667 |
}
|
|
1668 |
} else if ((ti->map5 & 0xC0) == 0xC0) {
|
|
1669 |
// depot or checkpoint
|
|
1670 |
return z + 8;
|
|
1671 |
}
|
|
1672 |
return GetPartialZ(ti->x&0xF, ti->y&0xF, th) + z;
|
|
1673 |
}
|
|
1674 |
return z;
|
|
1675 |
}
|
|
1676 |
|
|
1677 |
static void GetAcceptedCargo_Track(uint tile, AcceptedCargo *ac)
|
|
1678 |
{
|
|
1679 |
/* not used */
|
|
1680 |
}
|
|
1681 |
|
|
1682 |
static void AnimateTile_Track(uint tile)
|
|
1683 |
{
|
|
1684 |
/* not used */
|
|
1685 |
}
|
|
1686 |
|
|
1687 |
static void TileLoop_Track(uint tile)
|
|
1688 |
{
|
|
1689 |
byte a2;
|
|
1690 |
byte rail;
|
|
1691 |
byte m2;
|
|
1692 |
byte owner;
|
|
1693 |
|
|
1694 |
m2 = _map2[tile] & 0xF;
|
|
1695 |
|
|
1696 |
/* special code for alps landscape */
|
|
1697 |
if (_opt.landscape == LT_HILLY) {
|
|
1698 |
/* convert into snow? */
|
|
1699 |
if (GetTileZ(tile) > _opt.snow_line) {
|
|
1700 |
a2 = RAIL_GROUND_ICE_DESERT;
|
|
1701 |
goto modify_me;
|
|
1702 |
}
|
|
1703 |
|
|
1704 |
/* special code for desert landscape */
|
|
1705 |
} else if (_opt.landscape == LT_DESERT) {
|
|
1706 |
/* convert into desert? */
|
|
1707 |
if (GetMapExtraBits(tile) == 1) {
|
|
1708 |
a2 = RAIL_GROUND_ICE_DESERT;
|
|
1709 |
goto modify_me;
|
|
1710 |
}
|
|
1711 |
}
|
|
1712 |
|
|
1713 |
// Don't continue tile loop for depots
|
|
1714 |
if (_map5[tile] & RAIL_TYPE_SPECIAL)
|
|
1715 |
return;
|
|
1716 |
|
|
1717 |
a2 = RAIL_GROUND_GREEN;
|
|
1718 |
|
|
1719 |
if (m2 != RAIL_GROUND_BROWN) { /* wait until bottom is green */
|
|
1720 |
/* determine direction of fence */
|
|
1721 |
rail = _map5[tile] & RAIL_BIT_MASK;
|
|
1722 |
|
|
1723 |
if (rail == RAIL_BIT_UPPER) {
|
|
1724 |
a2 = RAIL_GROUND_FENCE_HORIZ1;
|
|
1725 |
} else if (rail == RAIL_BIT_LOWER) {
|
|
1726 |
a2 = RAIL_GROUND_FENCE_HORIZ2;
|
|
1727 |
} else if (rail == RAIL_BIT_LEFT) {
|
|
1728 |
a2 = RAIL_GROUND_FENCE_VERT1;
|
|
1729 |
} else if (rail == RAIL_BIT_RIGHT) {
|
|
1730 |
a2 = RAIL_GROUND_FENCE_VERT2;
|
|
1731 |
} else {
|
|
1732 |
owner = _map_owner[tile];
|
|
1733 |
|
|
1734 |
if ( (!(rail&(RAIL_BIT_DIAG2|RAIL_BIT_UPPER|RAIL_BIT_LEFT)) && (rail&RAIL_BIT_DIAG1)) || rail==(RAIL_BIT_LOWER|RAIL_BIT_RIGHT)) {
|
|
1735 |
if (!IS_TILETYPE(tile + TILE_XY(0,-1), MP_RAILWAY) ||
|
|
1736 |
owner != _map_owner[tile + TILE_XY(0,-1)] ||
|
|
1737 |
(_map5[tile + TILE_XY(0,-1)]==RAIL_BIT_UPPER || _map5[tile + TILE_XY(0,-1)]==RAIL_BIT_LEFT))
|
|
1738 |
a2 = RAIL_GROUND_FENCE_NW;
|
|
1739 |
}
|
|
1740 |
|
|
1741 |
if ( (!(rail&(RAIL_BIT_DIAG2|RAIL_BIT_LOWER|RAIL_BIT_RIGHT)) && (rail&RAIL_BIT_DIAG1)) || rail==(RAIL_BIT_UPPER|RAIL_BIT_LEFT)) {
|
|
1742 |
if (!IS_TILETYPE(tile + TILE_XY(0,1), MP_RAILWAY) ||
|
|
1743 |
owner != _map_owner[tile + TILE_XY(0,1)] ||
|
|
1744 |
(_map5[tile + TILE_XY(0,1)]==RAIL_BIT_LOWER || _map5[tile + TILE_XY(0,1)]==RAIL_BIT_RIGHT))
|
|
1745 |
a2 = (a2 == RAIL_GROUND_FENCE_NW) ? RAIL_GROUND_FENCE_SENW : RAIL_GROUND_FENCE_SE;
|
|
1746 |
}
|
|
1747 |
|
|
1748 |
if ( (!(rail&(RAIL_BIT_DIAG1|RAIL_BIT_UPPER|RAIL_BIT_RIGHT)) && (rail&RAIL_BIT_DIAG2)) || rail==(RAIL_BIT_LOWER|RAIL_BIT_LEFT)) {
|
|
1749 |
if (!IS_TILETYPE(tile + TILE_XY(-1,0), MP_RAILWAY) ||
|
|
1750 |
owner != _map_owner[tile + TILE_XY(-1,0)] ||
|
|
1751 |
(_map5[tile + TILE_XY(-1,0)]==RAIL_BIT_UPPER || _map5[tile + TILE_XY(-1,0)]==RAIL_BIT_RIGHT))
|
|
1752 |
a2 = RAIL_GROUND_FENCE_NE;
|
|
1753 |
}
|
|
1754 |
|
|
1755 |
if ( (!(rail&(RAIL_BIT_DIAG1|RAIL_BIT_LOWER|RAIL_BIT_LEFT)) && (rail&RAIL_BIT_DIAG2)) || rail==(RAIL_BIT_UPPER|RAIL_BIT_RIGHT)) {
|
|
1756 |
if (!IS_TILETYPE(tile + TILE_XY(1,0), MP_RAILWAY) ||
|
|
1757 |
owner != _map_owner[tile + TILE_XY(1,0)] ||
|
|
1758 |
(_map5[tile + TILE_XY(1,0)]==RAIL_BIT_LOWER || _map5[tile + TILE_XY(1,0)]==RAIL_BIT_LEFT))
|
|
1759 |
a2 = (a2 == RAIL_GROUND_FENCE_NE) ? RAIL_GROUND_FENCE_NESW : RAIL_GROUND_FENCE_SW;
|
|
1760 |
}
|
|
1761 |
}
|
|
1762 |
}
|
|
1763 |
|
|
1764 |
modify_me:;
|
|
1765 |
/* tile changed? */
|
|
1766 |
if ( m2 != a2) {
|
|
1767 |
_map2[tile] = (_map2[tile] & ~RAIL_MAP2LO_GROUND_MASK) | a2;
|
|
1768 |
MarkTileDirtyByTile(tile);
|
|
1769 |
}
|
|
1770 |
}
|
|
1771 |
|
|
1772 |
|
|
1773 |
static uint32 GetTileTrackStatus_Track(uint tile, int mode) {
|
|
1774 |
byte m5, a, b;
|
|
1775 |
uint32 ret;
|
|
1776 |
|
|
1777 |
if (mode != 0)
|
|
1778 |
return 0;
|
|
1779 |
|
|
1780 |
m5 = _map5[tile];
|
|
1781 |
|
|
1782 |
if (!(m5 & RAIL_TYPE_SPECIAL)) {
|
|
1783 |
ret = (m5 | (m5 << 8)) & 0x3F3F;
|
|
1784 |
if (!(m5 & RAIL_TYPE_SIGNALS)) {
|
|
1785 |
if ( (ret & 0xFF) == 3)
|
|
1786 |
ret |= 0x40;
|
|
1787 |
} else {
|
|
1788 |
/* has_signals */
|
|
1789 |
|
|
1790 |
a = _map3_lo[tile];
|
|
1791 |
b = _map2[tile];
|
|
1792 |
|
|
1793 |
b &= a;
|
|
1794 |
|
|
1795 |
if ((a & 0xC0) == 0) { b |= 0xC0; }
|
|
1796 |
if ((a & 0x30) == 0) { b |= 0x30; }
|
|
1797 |
|
|
1798 |
if ( (b & 0x80) == 0) ret |= 0x10070000;
|
|
1799 |
if ( (b & 0x40) == 0) ret |= 0x7100000;
|
|
1800 |
if ( (b & 0x20) == 0) ret |= 0x20080000;
|
|
1801 |
if ( (b & 0x10) == 0) ret |= 0x8200000;
|
|
1802 |
}
|
|
1803 |
} else if (m5 & 0x40) {
|
|
1804 |
static const byte _train_spec_tracks[6] = {1,2,1,2,1,2};
|
|
1805 |
m5 = _train_spec_tracks[m5 & 0x3F];
|
|
1806 |
ret = (m5 << 8) + m5;
|
|
1807 |
} else
|
|
1808 |
return 0;
|
|
1809 |
|
|
1810 |
return ret;
|
|
1811 |
}
|
|
1812 |
|
|
1813 |
static void ClickTile_Track(uint tile)
|
|
1814 |
{
|
|
1815 |
if (IS_RAIL_DEPOT(_map5[tile]))
|
|
1816 |
ShowTrainDepotWindow(tile);
|
|
1817 |
else if (IS_RAIL_CHECKPOINT(_map5[tile]))
|
|
1818 |
ShowRenameCheckpointWindow(&_checkpoints[GetCheckpointByTile(tile)]);
|
|
1819 |
|
|
1820 |
}
|
|
1821 |
|
|
1822 |
static void GetTileDesc_Track(uint tile, TileDesc *td)
|
|
1823 |
{
|
|
1824 |
byte m5 = _map5[tile];
|
|
1825 |
if (!(m5 & 0x80)) {
|
|
1826 |
if(!(m5 & 0x40)) // no signals
|
|
1827 |
td->str = STR_1021_RAILROAD_TRACK;
|
|
1828 |
else
|
|
1829 |
{
|
|
1830 |
switch(_map3_hi[tile] & 0x03)
|
|
1831 |
{
|
|
1832 |
case 0: td->str = STR_RAILROAD_TRACK_WITH_NORMAL_SIGNALS;
|
|
1833 |
break;
|
|
1834 |
case 1: td->str = STR_RAILROAD_TRACK_WITH_PRESIGNALS;
|
|
1835 |
break;
|
|
1836 |
case 2: td->str = STR_RAILROAD_TRACK_WITH_EXITSIGNALS;
|
|
1837 |
break;
|
|
1838 |
case 3: td->str = STR_RAILROAD_TRACK_WITH_COMBOSIGNALS;
|
|
1839 |
break;
|
|
1840 |
}
|
|
1841 |
}
|
|
1842 |
} else {
|
|
1843 |
td->str = m5 < 0xC4 ? STR_1023_RAILROAD_TRAIN_DEPOT : STR_LANDINFO_CHECKPOINT;
|
|
1844 |
}
|
|
1845 |
td->owner = _map_owner[tile];
|
|
1846 |
}
|
|
1847 |
|
|
1848 |
static void ChangeTileOwner_Track(uint tile, byte old_player, byte new_player)
|
|
1849 |
{
|
|
1850 |
if (_map_owner[tile] != old_player)
|
|
1851 |
return;
|
|
1852 |
|
|
1853 |
if (new_player != 255) {
|
|
1854 |
_map_owner[tile] = new_player;
|
|
1855 |
} else {
|
|
1856 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
1857 |
}
|
|
1858 |
}
|
|
1859 |
|
|
1860 |
static const byte _fractcoords_behind[4] = { 0x8F, 0x8, 0x80, 0xF8 };
|
|
1861 |
static const byte _fractcoords_enter[4] = { 0x8A, 0x48, 0x84, 0xA8 };
|
|
1862 |
static const byte _fractcoords_leave[4] = { 0x81, 0xD8, 0x8D, 0x18 };
|
|
1863 |
static const byte _enter_directions[4] = {5, 7, 1, 3};
|
|
1864 |
static const byte _leave_directions[4] = {1, 3, 5, 7};
|
|
1865 |
static const byte _depot_track_mask[4] = {1, 2, 1, 2};
|
|
1866 |
|
|
1867 |
uint32 VehicleEnter_Track(Vehicle *v, uint tile, int x, int y)
|
|
1868 |
{
|
|
1869 |
byte fract_coord;
|
|
1870 |
int dir;
|
|
1871 |
|
|
1872 |
// this routine applies only to trains in depot tiles
|
|
1873 |
if (v->type != VEH_Train || !IS_RAIL_DEPOT(_map5[tile]))
|
|
1874 |
return 0;
|
|
1875 |
|
|
1876 |
/* depot direction */
|
|
1877 |
dir = _map5[tile] & RAIL_DEPOT_DIR;
|
|
1878 |
|
|
1879 |
fract_coord = (x & 0xF) + ((y & 0xF) << 4);
|
|
1880 |
if (_fractcoords_behind[dir] == fract_coord) {
|
|
1881 |
/* make sure a train is not entering the tile from behind */
|
|
1882 |
return 8;
|
|
1883 |
} else if (_fractcoords_enter[dir] == fract_coord) {
|
|
1884 |
if (_enter_directions[dir] == v->direction) {
|
|
1885 |
/* enter the depot */
|
|
1886 |
v->u.rail.track = 0x80,
|
|
1887 |
v->vehstatus |= VS_HIDDEN; /* hide it */
|
|
1888 |
v->direction ^= 4;
|
|
1889 |
if (v->next == NULL)
|
|
1890 |
TrainEnterDepot(v, tile);
|
|
1891 |
v->tile = tile;
|
|
1892 |
InvalidateWindow(WC_VEHICLE_DEPOT, tile);
|
|
1893 |
return 4;
|
|
1894 |
}
|
|
1895 |
} else if (_fractcoords_leave[dir] == fract_coord) {
|
|
1896 |
if (_leave_directions[dir] == v->direction) {
|
|
1897 |
/* leave the depot? */
|
|
1898 |
if ((v=v->next) != NULL) {
|
|
1899 |
v->vehstatus &= ~VS_HIDDEN;
|
|
1900 |
v->u.rail.track = _depot_track_mask[dir];
|
|
1901 |
}
|
|
1902 |
}
|
|
1903 |
}
|
|
1904 |
|
|
1905 |
return 0;
|
|
1906 |
}
|
|
1907 |
|
|
1908 |
void InitializeRail()
|
|
1909 |
{
|
|
1910 |
_last_built_train_depot_tile = 0;
|
|
1911 |
}
|
|
1912 |
|
|
1913 |
const TileTypeProcs _tile_type_rail_procs = {
|
|
1914 |
DrawTile_Track, /* draw_tile_proc */
|
|
1915 |
GetSlopeZ_Track, /* get_slope_z_proc */
|
|
1916 |
ClearTile_Track, /* clear_tile_proc */
|
|
1917 |
GetAcceptedCargo_Track, /* get_accepted_cargo_proc */
|
|
1918 |
GetTileDesc_Track, /* get_tile_desc_proc */
|
|
1919 |
GetTileTrackStatus_Track, /* get_tile_track_status_proc */
|
|
1920 |
ClickTile_Track, /* click_tile_proc */
|
|
1921 |
AnimateTile_Track, /* animate_tile_proc */
|
|
1922 |
TileLoop_Track, /* tile_loop_clear */
|
|
1923 |
ChangeTileOwner_Track, /* change_tile_owner_clear */
|
|
1924 |
NULL, /* get_produced_cargo_proc */
|
|
1925 |
VehicleEnter_Track, /* vehicle_enter_tile_proc */
|
|
1926 |
NULL, /* vehicle_leave_tile_proc */
|
|
1927 |
};
|