1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8 /** @file rail_map.h Hides the direct accesses to the map array with map accessors */
9
10 #ifndef RAIL_MAP_H
11 #define RAIL_MAP_H
12
13 #include "rail_type.h"
14 #include "depot_type.h"
15 #include "signal_func.h"
16 #include "track_func.h"
17 #include "tile_map.h"
18 #include "water_map.h"
19 #include "signal_type.h"
20
21
22 /** Different types of Rail-related tiles */
23 enum RailTileType {
24 RAIL_TILE_NORMAL = 0, ///< Normal rail tile without signals
25 RAIL_TILE_SIGNALS = 1, ///< Normal rail tile with signals
26 RAIL_TILE_DEPOT = 3, ///< Depot (one entrance)
27 };
28
29 /**
30 * Returns the RailTileType (normal with or without signals,
31 * waypoint or depot).
32 * @param t the tile to get the information from
33 * @pre IsTileType(t, MP_RAILWAY)
34 * @return the RailTileType
35 */
GetRailTileType(TileIndex t)36 static inline RailTileType GetRailTileType(TileIndex t)
37 {
38 assert(IsTileType(t, MP_RAILWAY));
39 return (RailTileType)GB(_m[t].m5, 6, 2);
40 }
41
42 /**
43 * Returns whether this is plain rails, with or without signals. Iow, if this
44 * tiles RailTileType is RAIL_TILE_NORMAL or RAIL_TILE_SIGNALS.
45 * @param t the tile to get the information from
46 * @pre IsTileType(t, MP_RAILWAY)
47 * @return true if and only if the tile is normal rail (with or without signals)
48 */
IsPlainRail(TileIndex t)49 static inline bool IsPlainRail(TileIndex t)
50 {
51 RailTileType rtt = GetRailTileType(t);
52 return rtt == RAIL_TILE_NORMAL || rtt == RAIL_TILE_SIGNALS;
53 }
54
55 /**
56 * Checks whether the tile is a rail tile or rail tile with signals.
57 * @param t the tile to get the information from
58 * @return true if and only if the tile is normal rail (with or without signals)
59 */
IsPlainRailTile(TileIndex t)60 static inline bool IsPlainRailTile(TileIndex t)
61 {
62 return IsTileType(t, MP_RAILWAY) && IsPlainRail(t);
63 }
64
65
66 /**
67 * Checks if a rail tile has signals.
68 * @param t the tile to get the information from
69 * @pre IsTileType(t, MP_RAILWAY)
70 * @return true if and only if the tile has signals
71 */
HasSignals(TileIndex t)72 static inline bool HasSignals(TileIndex t)
73 {
74 return GetRailTileType(t) == RAIL_TILE_SIGNALS;
75 }
76
77 /**
78 * Add/remove the 'has signal' bit from the RailTileType
79 * @param tile the tile to add/remove the signals to/from
80 * @param signals whether the rail tile should have signals or not
81 * @pre IsPlainRailTile(tile)
82 */
SetHasSignals(TileIndex tile,bool signals)83 static inline void SetHasSignals(TileIndex tile, bool signals)
84 {
85 assert(IsPlainRailTile(tile));
86 SB(_m[tile].m5, 6, 1, signals);
87 }
88
89 /**
90 * Is this rail tile a rail depot?
91 * @param t the tile to get the information from
92 * @pre IsTileType(t, MP_RAILWAY)
93 * @return true if and only if the tile is a rail depot
94 */
IsRailDepot(TileIndex t)95 static inline bool IsRailDepot(TileIndex t)
96 {
97 return GetRailTileType(t) == RAIL_TILE_DEPOT;
98 }
99
100 /**
101 * Is this tile rail tile and a rail depot?
102 * @param t the tile to get the information from
103 * @return true if and only if the tile is a rail depot
104 */
IsRailDepotTile(TileIndex t)105 static inline bool IsRailDepotTile(TileIndex t)
106 {
107 return IsTileType(t, MP_RAILWAY) && IsRailDepot(t);
108 }
109
110 /**
111 * Gets the rail type of the given tile
112 * @param t the tile to get the rail type from
113 * @return the rail type of the tile
114 */
GetRailType(TileIndex t)115 static inline RailType GetRailType(TileIndex t)
116 {
117 return (RailType)GB(_me[t].m8, 0, 6);
118 }
119
120 /**
121 * Sets the rail type of the given tile
122 * @param t the tile to set the rail type of
123 * @param r the new rail type for the tile
124 */
SetRailType(TileIndex t,RailType r)125 static inline void SetRailType(TileIndex t, RailType r)
126 {
127 SB(_me[t].m8, 0, 6, r);
128 }
129
130
131 /**
132 * Gets the track bits of the given tile
133 * @param tile the tile to get the track bits from
134 * @return the track bits of the tile
135 */
GetTrackBits(TileIndex tile)136 static inline TrackBits GetTrackBits(TileIndex tile)
137 {
138 assert(IsPlainRailTile(tile));
139 return (TrackBits)GB(_m[tile].m5, 0, 6);
140 }
141
142 /**
143 * Sets the track bits of the given tile
144 * @param t the tile to set the track bits of
145 * @param b the new track bits for the tile
146 */
SetTrackBits(TileIndex t,TrackBits b)147 static inline void SetTrackBits(TileIndex t, TrackBits b)
148 {
149 assert(IsPlainRailTile(t));
150 SB(_m[t].m5, 0, 6, b);
151 }
152
153 /**
154 * Returns whether the given track is present on the given tile.
155 * @param tile the tile to check the track presence of
156 * @param track the track to search for on the tile
157 * @pre IsPlainRailTile(tile)
158 * @return true if and only if the given track exists on the tile
159 */
HasTrack(TileIndex tile,Track track)160 static inline bool HasTrack(TileIndex tile, Track track)
161 {
162 return HasBit(GetTrackBits(tile), track);
163 }
164
165 /**
166 * Returns the direction the depot is facing to
167 * @param t the tile to get the depot facing from
168 * @pre IsRailDepotTile(t)
169 * @return the direction the depot is facing
170 */
GetRailDepotDirection(TileIndex t)171 static inline DiagDirection GetRailDepotDirection(TileIndex t)
172 {
173 return (DiagDirection)GB(_m[t].m5, 0, 2);
174 }
175
176 /**
177 * Returns the track of a depot, ignoring direction
178 * @pre IsRailDepotTile(t)
179 * @param t the tile to get the depot track from
180 * @return the track of the depot
181 */
GetRailDepotTrack(TileIndex t)182 static inline Track GetRailDepotTrack(TileIndex t)
183 {
184 return DiagDirToDiagTrack(GetRailDepotDirection(t));
185 }
186
187
188 /**
189 * Returns the reserved track bits of the tile
190 * @pre IsPlainRailTile(t)
191 * @param t the tile to query
192 * @return the track bits
193 */
GetRailReservationTrackBits(TileIndex t)194 static inline TrackBits GetRailReservationTrackBits(TileIndex t)
195 {
196 assert(IsPlainRailTile(t));
197 byte track_b = GB(_m[t].m2, 8, 3);
198 Track track = (Track)(track_b - 1); // map array saves Track+1
199 if (track_b == 0) return TRACK_BIT_NONE;
200 return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
201 }
202
203 /**
204 * Sets the reserved track bits of the tile
205 * @pre IsPlainRailTile(t) && !TracksOverlap(b)
206 * @param t the tile to change
207 * @param b the track bits
208 */
SetTrackReservation(TileIndex t,TrackBits b)209 static inline void SetTrackReservation(TileIndex t, TrackBits b)
210 {
211 assert(IsPlainRailTile(t));
212 assert(b != INVALID_TRACK_BIT);
213 assert(!TracksOverlap(b));
214 Track track = RemoveFirstTrack(&b);
215 SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track + 1);
216 SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
217 }
218
219 /**
220 * Try to reserve a specific track on a tile
221 * @pre IsPlainRailTile(t) && HasTrack(tile, t)
222 * @param tile the tile
223 * @param t the rack to reserve
224 * @return true if successful
225 */
TryReserveTrack(TileIndex tile,Track t)226 static inline bool TryReserveTrack(TileIndex tile, Track t)
227 {
228 assert(HasTrack(tile, t));
229 TrackBits bits = TrackToTrackBits(t);
230 TrackBits res = GetRailReservationTrackBits(tile);
231 if ((res & bits) != TRACK_BIT_NONE) return false; // already reserved
232 res |= bits;
233 if (TracksOverlap(res)) return false; // crossing reservation present
234 SetTrackReservation(tile, res);
235 return true;
236 }
237
238 /**
239 * Lift the reservation of a specific track on a tile
240 * @pre IsPlainRailTile(t) && HasTrack(tile, t)
241 * @param tile the tile
242 * @param t the track to free
243 */
UnreserveTrack(TileIndex tile,Track t)244 static inline void UnreserveTrack(TileIndex tile, Track t)
245 {
246 assert(HasTrack(tile, t));
247 TrackBits res = GetRailReservationTrackBits(tile);
248 res &= ~TrackToTrackBits(t);
249 SetTrackReservation(tile, res);
250 }
251
252 /**
253 * Get the reservation state of the depot
254 * @pre IsRailDepot(t)
255 * @param t the depot tile
256 * @return reservation state
257 */
HasDepotReservation(TileIndex t)258 static inline bool HasDepotReservation(TileIndex t)
259 {
260 assert(IsRailDepot(t));
261 return HasBit(_m[t].m5, 4);
262 }
263
264 /**
265 * Set the reservation state of the depot
266 * @pre IsRailDepot(t)
267 * @param t the depot tile
268 * @param b the reservation state
269 */
SetDepotReservation(TileIndex t,bool b)270 static inline void SetDepotReservation(TileIndex t, bool b)
271 {
272 assert(IsRailDepot(t));
273 SB(_m[t].m5, 4, 1, (byte)b);
274 }
275
276 /**
277 * Get the reserved track bits for a depot
278 * @pre IsRailDepot(t)
279 * @param t the tile
280 * @return reserved track bits
281 */
GetDepotReservationTrackBits(TileIndex t)282 static inline TrackBits GetDepotReservationTrackBits(TileIndex t)
283 {
284 return HasDepotReservation(t) ? TrackToTrackBits(GetRailDepotTrack(t)) : TRACK_BIT_NONE;
285 }
286
287
IsPbsSignal(SignalType s)288 static inline bool IsPbsSignal(SignalType s)
289 {
290 return s == SIGTYPE_PBS || s == SIGTYPE_PBS_ONEWAY;
291 }
292
GetSignalType(TileIndex t,Track track)293 static inline SignalType GetSignalType(TileIndex t, Track track)
294 {
295 assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
296 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
297 return (SignalType)GB(_m[t].m2, pos, 3);
298 }
299
SetSignalType(TileIndex t,Track track,SignalType s)300 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
301 {
302 assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
303 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
304 SB(_m[t].m2, pos, 3, s);
305 if (track == INVALID_TRACK) SB(_m[t].m2, 4, 3, s);
306 }
307
IsPresignalEntry(TileIndex t,Track track)308 static inline bool IsPresignalEntry(TileIndex t, Track track)
309 {
310 return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO;
311 }
312
IsPresignalExit(TileIndex t,Track track)313 static inline bool IsPresignalExit(TileIndex t, Track track)
314 {
315 return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO;
316 }
317
318 /** One-way signals can't be passed the 'wrong' way. */
IsOnewaySignal(TileIndex t,Track track)319 static inline bool IsOnewaySignal(TileIndex t, Track track)
320 {
321 return GetSignalType(t, track) != SIGTYPE_PBS;
322 }
323
CycleSignalSide(TileIndex t,Track track)324 static inline void CycleSignalSide(TileIndex t, Track track)
325 {
326 byte sig;
327 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
328
329 sig = GB(_m[t].m3, pos, 2);
330 if (--sig == 0) sig = IsPbsSignal(GetSignalType(t, track)) ? 2 : 3;
331 SB(_m[t].m3, pos, 2, sig);
332 }
333
GetSignalVariant(TileIndex t,Track track)334 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
335 {
336 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
337 return (SignalVariant)GB(_m[t].m2, pos, 1);
338 }
339
SetSignalVariant(TileIndex t,Track track,SignalVariant v)340 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
341 {
342 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
343 SB(_m[t].m2, pos, 1, v);
344 if (track == INVALID_TRACK) SB(_m[t].m2, 7, 1, v);
345 }
346
347 /**
348 * Set the states of the signals (Along/AgainstTrackDir)
349 * @param tile the tile to set the states for
350 * @param state the new state
351 */
SetSignalStates(TileIndex tile,uint state)352 static inline void SetSignalStates(TileIndex tile, uint state)
353 {
354 SB(_m[tile].m4, 4, 4, state);
355 }
356
357 /**
358 * Set the states of the signals (Along/AgainstTrackDir)
359 * @param tile the tile to set the states for
360 * @return the state of the signals
361 */
GetSignalStates(TileIndex tile)362 static inline uint GetSignalStates(TileIndex tile)
363 {
364 return GB(_m[tile].m4, 4, 4);
365 }
366
367 /**
368 * Get the state of a single signal
369 * @param t the tile to get the signal state for
370 * @param signalbit the signal
371 * @return the state of the signal
372 */
GetSingleSignalState(TileIndex t,byte signalbit)373 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
374 {
375 return (SignalState)HasBit(GetSignalStates(t), signalbit);
376 }
377
378 /**
379 * Set whether the given signals are present (Along/AgainstTrackDir)
380 * @param tile the tile to set the present signals for
381 * @param signals the signals that have to be present
382 */
SetPresentSignals(TileIndex tile,uint signals)383 static inline void SetPresentSignals(TileIndex tile, uint signals)
384 {
385 SB(_m[tile].m3, 4, 4, signals);
386 }
387
388 /**
389 * Get whether the given signals are present (Along/AgainstTrackDir)
390 * @param tile the tile to get the present signals for
391 * @return the signals that are present
392 */
GetPresentSignals(TileIndex tile)393 static inline uint GetPresentSignals(TileIndex tile)
394 {
395 return GB(_m[tile].m3, 4, 4);
396 }
397
398 /**
399 * Checks whether the given signals is present
400 * @param t the tile to check on
401 * @param signalbit the signal
402 * @return true if and only if the signal is present
403 */
IsSignalPresent(TileIndex t,byte signalbit)404 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
405 {
406 return HasBit(GetPresentSignals(t), signalbit);
407 }
408
409 /**
410 * Checks for the presence of signals (either way) on the given track on the
411 * given rail tile.
412 */
HasSignalOnTrack(TileIndex tile,Track track)413 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
414 {
415 assert(IsValidTrack(track));
416 return GetRailTileType(tile) == RAIL_TILE_SIGNALS && (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
417 }
418
419 /**
420 * Checks for the presence of signals along the given trackdir on the given
421 * rail tile.
422 *
423 * Along meaning if you are currently driving on the given trackdir, this is
424 * the signal that is facing us (for which we stop when it's red).
425 */
HasSignalOnTrackdir(TileIndex tile,Trackdir trackdir)426 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
427 {
428 assert (IsValidTrackdir(trackdir));
429 return GetRailTileType(tile) == RAIL_TILE_SIGNALS && GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
430 }
431
432 /**
433 * Gets the state of the signal along the given trackdir.
434 *
435 * Along meaning if you are currently driving on the given trackdir, this is
436 * the signal that is facing us (for which we stop when it's red).
437 */
GetSignalStateByTrackdir(TileIndex tile,Trackdir trackdir)438 static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
439 {
440 assert(IsValidTrackdir(trackdir));
441 assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
442 return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
443 SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
444 }
445
446 /**
447 * Sets the state of the signal along the given trackdir.
448 */
SetSignalStateByTrackdir(TileIndex tile,Trackdir trackdir,SignalState state)449 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
450 {
451 if (state == SIGNAL_STATE_GREEN) { // set 1
452 SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
453 } else {
454 SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
455 }
456 }
457
458 /**
459 * Is a pbs signal present along the trackdir?
460 * @param tile the tile to check
461 * @param td the trackdir to check
462 */
HasPbsSignalOnTrackdir(TileIndex tile,Trackdir td)463 static inline bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
464 {
465 return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, td) &&
466 IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)));
467 }
468
469 /**
470 * Is a one-way signal blocking the trackdir? A one-way signal on the
471 * trackdir against will block, but signals on both trackdirs won't.
472 * @param tile the tile to check
473 * @param td the trackdir to check
474 */
HasOnewaySignalBlockingTrackdir(TileIndex tile,Trackdir td)475 static inline bool HasOnewaySignalBlockingTrackdir(TileIndex tile, Trackdir td)
476 {
477 return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, ReverseTrackdir(td)) &&
478 !HasSignalOnTrackdir(tile, td) && IsOnewaySignal(tile, TrackdirToTrack(td));
479 }
480
481
482 RailType GetTileRailType(TileIndex tile);
483
484 /** The ground 'under' the rail */
485 enum RailGroundType {
486 RAIL_GROUND_BARREN = 0, ///< Nothing (dirt)
487 RAIL_GROUND_GRASS = 1, ///< Grassy
488 RAIL_GROUND_FENCE_NW = 2, ///< Grass with a fence at the NW edge
489 RAIL_GROUND_FENCE_SE = 3, ///< Grass with a fence at the SE edge
490 RAIL_GROUND_FENCE_SENW = 4, ///< Grass with a fence at the NW and SE edges
491 RAIL_GROUND_FENCE_NE = 5, ///< Grass with a fence at the NE edge
492 RAIL_GROUND_FENCE_SW = 6, ///< Grass with a fence at the SW edge
493 RAIL_GROUND_FENCE_NESW = 7, ///< Grass with a fence at the NE and SW edges
494 RAIL_GROUND_FENCE_VERT1 = 8, ///< Grass with a fence at the eastern side
495 RAIL_GROUND_FENCE_VERT2 = 9, ///< Grass with a fence at the western side
496 RAIL_GROUND_FENCE_HORIZ1 = 10, ///< Grass with a fence at the southern side
497 RAIL_GROUND_FENCE_HORIZ2 = 11, ///< Grass with a fence at the northern side
498 RAIL_GROUND_ICE_DESERT = 12, ///< Icy or sandy
499 RAIL_GROUND_WATER = 13, ///< Grass with a fence and shore or water on the free halftile
500 RAIL_GROUND_HALF_SNOW = 14, ///< Snow only on higher part of slope (steep or one corner raised)
501 };
502
SetRailGroundType(TileIndex t,RailGroundType rgt)503 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
504 {
505 SB(_m[t].m4, 0, 4, rgt);
506 }
507
GetRailGroundType(TileIndex t)508 static inline RailGroundType GetRailGroundType(TileIndex t)
509 {
510 return (RailGroundType)GB(_m[t].m4, 0, 4);
511 }
512
IsSnowRailGround(TileIndex t)513 static inline bool IsSnowRailGround(TileIndex t)
514 {
515 return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
516 }
517
518
MakeRailNormal(TileIndex t,Owner o,TrackBits b,RailType r)519 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
520 {
521 SetTileType(t, MP_RAILWAY);
522 SetTileOwner(t, o);
523 SetDockingTile(t, false);
524 _m[t].m2 = 0;
525 _m[t].m3 = 0;
526 _m[t].m4 = 0;
527 _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
528 SB(_me[t].m6, 2, 4, 0);
529 _me[t].m7 = 0;
530 _me[t].m8 = r;
531 }
532
533
MakeRailDepot(TileIndex t,Owner o,DepotID did,DiagDirection d,RailType r)534 static inline void MakeRailDepot(TileIndex t, Owner o, DepotID did, DiagDirection d, RailType r)
535 {
536 SetTileType(t, MP_RAILWAY);
537 SetTileOwner(t, o);
538 SetDockingTile(t, false);
539 _m[t].m2 = did;
540 _m[t].m3 = 0;
541 _m[t].m4 = 0;
542 _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
543 SB(_me[t].m6, 2, 4, 0);
544 _me[t].m7 = 0;
545 _me[t].m8 = r;
546 }
547
548 #endif /* RAIL_MAP_H */
549