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/>.
8 /** @file rail_map.h Hides the direct accesses to the map array with map accessors */
13 #include "rail_type.h"
14 #include "depot_type.h"
15 #include "signal_func.h"
16 #include "track_func.h"
18 #include "water_map.h"
19 #include "signal_type.h"
22 /** Different types of Rail-related tiles */
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)
30 * Returns the RailTileType (normal with or without signals,
32 * @param t the tile to get the information from
33 * @pre IsTileType(t, MP_RAILWAY)
34 * @return the RailTileType
36 static inline RailTileType
GetRailTileType(TileIndex t
)
38 assert(IsTileType(t
, MP_RAILWAY
));
39 return (RailTileType
)GB(_m
[t
].m5
, 6, 2);
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)
49 static inline bool IsPlainRail(TileIndex t
)
51 RailTileType rtt
= GetRailTileType(t
);
52 return rtt
== RAIL_TILE_NORMAL
|| rtt
== RAIL_TILE_SIGNALS
;
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)
60 static inline bool IsPlainRailTile(TileIndex t
)
62 return IsTileType(t
, MP_RAILWAY
) && IsPlainRail(t
);
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
72 static inline bool HasSignals(TileIndex t
)
74 return GetRailTileType(t
) == RAIL_TILE_SIGNALS
;
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)
83 static inline void SetHasSignals(TileIndex tile
, bool signals
)
85 assert(IsPlainRailTile(tile
));
86 SB(_m
[tile
].m5
, 6, 1, signals
);
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
95 static inline bool IsRailDepot(TileIndex t
)
97 return GetRailTileType(t
) == RAIL_TILE_DEPOT
;
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
105 static inline bool IsRailDepotTile(TileIndex t
)
107 return IsTileType(t
, MP_RAILWAY
) && IsRailDepot(t
);
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
115 static inline RailType
GetRailType(TileIndex t
)
117 return (RailType
)GB(_me
[t
].m8
, 0, 6);
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
125 static inline void SetRailType(TileIndex t
, RailType r
)
127 SB(_me
[t
].m8
, 0, 6, r
);
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
136 static inline TrackBits
GetTrackBits(TileIndex tile
)
138 assert(IsPlainRailTile(tile
));
139 return (TrackBits
)GB(_m
[tile
].m5
, 0, 6);
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
147 static inline void SetTrackBits(TileIndex t
, TrackBits b
)
149 assert(IsPlainRailTile(t
));
150 SB(_m
[t
].m5
, 0, 6, b
);
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
160 static inline bool HasTrack(TileIndex tile
, Track track
)
162 return HasBit(GetTrackBits(tile
), track
);
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
171 static inline DiagDirection
GetRailDepotDirection(TileIndex t
)
173 return (DiagDirection
)GB(_m
[t
].m5
, 0, 2);
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
182 static inline Track
GetRailDepotTrack(TileIndex t
)
184 return DiagDirToDiagTrack(GetRailDepotDirection(t
));
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
194 static inline TrackBits
GetRailReservationTrackBits(TileIndex t
)
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));
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
209 static inline void SetTrackReservation(TileIndex t
, TrackBits b
)
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
));
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
226 static inline bool TryReserveTrack(TileIndex tile
, Track t
)
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
233 if (TracksOverlap(res
)) return false; // crossing reservation present
234 SetTrackReservation(tile
, res
);
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
244 static inline void UnreserveTrack(TileIndex tile
, Track t
)
246 assert(HasTrack(tile
, t
));
247 TrackBits res
= GetRailReservationTrackBits(tile
);
248 res
&= ~TrackToTrackBits(t
);
249 SetTrackReservation(tile
, res
);
253 * Get the reservation state of the depot
254 * @pre IsRailDepot(t)
255 * @param t the depot tile
256 * @return reservation state
258 static inline bool HasDepotReservation(TileIndex t
)
260 assert(IsRailDepot(t
));
261 return HasBit(_m
[t
].m5
, 4);
265 * Set the reservation state of the depot
266 * @pre IsRailDepot(t)
267 * @param t the depot tile
268 * @param b the reservation state
270 static inline void SetDepotReservation(TileIndex t
, bool b
)
272 assert(IsRailDepot(t
));
273 SB(_m
[t
].m5
, 4, 1, (byte
)b
);
277 * Get the reserved track bits for a depot
278 * @pre IsRailDepot(t)
280 * @return reserved track bits
282 static inline TrackBits
GetDepotReservationTrackBits(TileIndex t
)
284 return HasDepotReservation(t
) ? TrackToTrackBits(GetRailDepotTrack(t
)) : TRACK_BIT_NONE
;
288 static inline bool IsPbsSignal(SignalType s
)
290 return s
== SIGTYPE_PBS
|| s
== SIGTYPE_PBS_ONEWAY
;
293 static inline SignalType
GetSignalType(TileIndex t
, Track track
)
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);
300 static inline void SetSignalType(TileIndex t
, Track track
, SignalType s
)
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
);
308 static inline bool IsPresignalEntry(TileIndex t
, Track track
)
310 return GetSignalType(t
, track
) == SIGTYPE_ENTRY
|| GetSignalType(t
, track
) == SIGTYPE_COMBO
;
313 static inline bool IsPresignalExit(TileIndex t
, Track track
)
315 return GetSignalType(t
, track
) == SIGTYPE_EXIT
|| GetSignalType(t
, track
) == SIGTYPE_COMBO
;
318 /** One-way signals can't be passed the 'wrong' way. */
319 static inline bool IsOnewaySignal(TileIndex t
, Track track
)
321 return GetSignalType(t
, track
) != SIGTYPE_PBS
;
324 static inline void CycleSignalSide(TileIndex t
, Track track
)
327 byte pos
= (track
== TRACK_LOWER
|| track
== TRACK_RIGHT
) ? 4 : 6;
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
);
334 static inline SignalVariant
GetSignalVariant(TileIndex t
, Track track
)
336 byte pos
= (track
== TRACK_LOWER
|| track
== TRACK_RIGHT
) ? 7 : 3;
337 return (SignalVariant
)GB(_m
[t
].m2
, pos
, 1);
340 static inline void SetSignalVariant(TileIndex t
, Track track
, SignalVariant v
)
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
);
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
352 static inline void SetSignalStates(TileIndex tile
, uint state
)
354 SB(_m
[tile
].m4
, 4, 4, state
);
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
362 static inline uint
GetSignalStates(TileIndex tile
)
364 return GB(_m
[tile
].m4
, 4, 4);
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
373 static inline SignalState
GetSingleSignalState(TileIndex t
, byte signalbit
)
375 return (SignalState
)HasBit(GetSignalStates(t
), signalbit
);
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
383 static inline void SetPresentSignals(TileIndex tile
, uint signals
)
385 SB(_m
[tile
].m3
, 4, 4, signals
);
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
393 static inline uint
GetPresentSignals(TileIndex tile
)
395 return GB(_m
[tile
].m3
, 4, 4);
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
404 static inline bool IsSignalPresent(TileIndex t
, byte signalbit
)
406 return HasBit(GetPresentSignals(t
), signalbit
);
410 * Checks for the presence of signals (either way) on the given track on the
413 static inline bool HasSignalOnTrack(TileIndex tile
, Track track
)
415 assert(IsValidTrack(track
));
416 return GetRailTileType(tile
) == RAIL_TILE_SIGNALS
&& (GetPresentSignals(tile
) & SignalOnTrack(track
)) != 0;
420 * Checks for the presence of signals along the given trackdir on the given
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).
426 static inline bool HasSignalOnTrackdir(TileIndex tile
, Trackdir trackdir
)
428 assert (IsValidTrackdir(trackdir
));
429 return GetRailTileType(tile
) == RAIL_TILE_SIGNALS
&& GetPresentSignals(tile
) & SignalAlongTrackdir(trackdir
);
433 * Gets the state of the signal along the given trackdir.
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).
438 static inline SignalState
GetSignalStateByTrackdir(TileIndex tile
, Trackdir trackdir
)
440 assert(IsValidTrackdir(trackdir
));
441 assert(HasSignalOnTrack(tile
, TrackdirToTrack(trackdir
)));
442 return GetSignalStates(tile
) & SignalAlongTrackdir(trackdir
) ?
443 SIGNAL_STATE_GREEN
: SIGNAL_STATE_RED
;
447 * Sets the state of the signal along the given trackdir.
449 static inline void SetSignalStateByTrackdir(TileIndex tile
, Trackdir trackdir
, SignalState state
)
451 if (state
== SIGNAL_STATE_GREEN
) { // set 1
452 SetSignalStates(tile
, GetSignalStates(tile
) | SignalAlongTrackdir(trackdir
));
454 SetSignalStates(tile
, GetSignalStates(tile
) & ~SignalAlongTrackdir(trackdir
));
459 * Is a pbs signal present along the trackdir?
460 * @param tile the tile to check
461 * @param td the trackdir to check
463 static inline bool HasPbsSignalOnTrackdir(TileIndex tile
, Trackdir td
)
465 return IsTileType(tile
, MP_RAILWAY
) && HasSignalOnTrackdir(tile
, td
) &&
466 IsPbsSignal(GetSignalType(tile
, TrackdirToTrack(td
)));
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
475 static inline bool HasOnewaySignalBlockingTrackdir(TileIndex tile
, Trackdir td
)
477 return IsTileType(tile
, MP_RAILWAY
) && HasSignalOnTrackdir(tile
, ReverseTrackdir(td
)) &&
478 !HasSignalOnTrackdir(tile
, td
) && IsOnewaySignal(tile
, TrackdirToTrack(td
));
482 RailType
GetTileRailType(TileIndex tile
);
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)
503 static inline void SetRailGroundType(TileIndex t
, RailGroundType rgt
)
505 SB(_m
[t
].m4
, 0, 4, rgt
);
508 static inline RailGroundType
GetRailGroundType(TileIndex t
)
510 return (RailGroundType
)GB(_m
[t
].m4
, 0, 4);
513 static inline bool IsSnowRailGround(TileIndex t
)
515 return GetRailGroundType(t
) == RAIL_GROUND_ICE_DESERT
;
519 static inline void MakeRailNormal(TileIndex t
, Owner o
, TrackBits b
, RailType r
)
521 SetTileType(t
, MP_RAILWAY
);
523 SetDockingTile(t
, false);
527 _m
[t
].m5
= RAIL_TILE_NORMAL
<< 6 | b
;
528 SB(_me
[t
].m6
, 2, 4, 0);
534 static inline void MakeRailDepot(TileIndex t
, Owner o
, DepotID did
, DiagDirection d
, RailType r
)
536 SetTileType(t
, MP_RAILWAY
);
538 SetDockingTile(t
, false);
542 _m
[t
].m5
= RAIL_TILE_DEPOT
<< 6 | d
;
543 SB(_me
[t
].m6
, 2, 4, 0);
548 #endif /* RAIL_MAP_H */