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 debug_inline
static RailTileType
GetRailTileType(Tile t
)
38 assert(IsTileType(t
, MP_RAILWAY
));
39 return (RailTileType
)GB(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 debug_inline
static bool IsPlainRail(Tile 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 debug_inline
static bool IsPlainRailTile(Tile 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 inline bool HasSignals(Tile 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 inline void SetHasSignals(Tile tile
, bool signals
)
85 assert(IsPlainRailTile(tile
));
86 AssignBit(tile
.m5(), 6, 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 debug_inline
static bool IsRailDepot(Tile 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 debug_inline
static bool IsRailDepotTile(Tile 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 inline RailType
GetRailType(Tile t
)
117 return (RailType
)GB(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 inline void SetRailType(Tile t
, RailType 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 inline TrackBits
GetTrackBits(Tile tile
)
138 assert(IsPlainRailTile(tile
));
139 return (TrackBits
)GB(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 inline void SetTrackBits(Tile t
, TrackBits b
)
149 assert(IsPlainRailTile(t
));
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 inline bool HasTrack(Tile 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 inline DiagDirection
GetRailDepotDirection(Tile t
)
173 return (DiagDirection
)GB(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 inline Track
GetRailDepotTrack(Tile 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 inline TrackBits
GetRailReservationTrackBits(Tile t
)
196 assert(IsPlainRailTile(t
));
197 uint8_t track_b
= GB(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(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 inline void SetTrackReservation(Tile t
, TrackBits b
)
211 assert(IsPlainRailTile(t
));
212 assert(b
!= INVALID_TRACK_BIT
);
213 assert(!TracksOverlap(b
));
214 Track track
= RemoveFirstTrack(&b
);
215 SB(t
.m2(), 8, 3, track
== INVALID_TRACK
? 0 : track
+ 1);
216 AssignBit(t
.m2(), 11, 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 inline bool TryReserveTrack(Tile 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 inline void UnreserveTrack(Tile 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 inline bool HasDepotReservation(Tile t
)
260 assert(IsRailDepot(t
));
261 return HasBit(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 inline void SetDepotReservation(Tile t
, bool b
)
272 assert(IsRailDepot(t
));
273 AssignBit(t
.m5(), 4, b
);
277 * Get the reserved track bits for a depot
278 * @pre IsRailDepot(t)
280 * @return reserved track bits
282 inline TrackBits
GetDepotReservationTrackBits(Tile t
)
284 return HasDepotReservation(t
) ? TrackToTrackBits(GetRailDepotTrack(t
)) : TRACK_BIT_NONE
;
288 inline bool IsPbsSignal(SignalType s
)
290 return s
== SIGTYPE_PBS
|| s
== SIGTYPE_PBS_ONEWAY
;
293 inline SignalType
GetSignalType(Tile t
, Track track
)
295 assert(GetRailTileType(t
) == RAIL_TILE_SIGNALS
);
296 uint8_t pos
= (track
== TRACK_LOWER
|| track
== TRACK_RIGHT
) ? 4 : 0;
297 return (SignalType
)GB(t
.m2(), pos
, 3);
300 inline void SetSignalType(Tile t
, Track track
, SignalType s
)
302 assert(GetRailTileType(t
) == RAIL_TILE_SIGNALS
);
303 uint8_t pos
= (track
== TRACK_LOWER
|| track
== TRACK_RIGHT
) ? 4 : 0;
304 SB(t
.m2(), pos
, 3, s
);
305 if (track
== INVALID_TRACK
) SB(t
.m2(), 4, 3, s
);
308 inline bool IsPresignalEntry(Tile t
, Track track
)
310 return GetSignalType(t
, track
) == SIGTYPE_ENTRY
|| GetSignalType(t
, track
) == SIGTYPE_COMBO
;
313 inline bool IsPresignalExit(Tile 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 inline bool IsOnewaySignal(Tile t
, Track track
)
321 return GetSignalType(t
, track
) != SIGTYPE_PBS
;
324 inline void CycleSignalSide(Tile t
, Track track
)
327 uint8_t pos
= (track
== TRACK_LOWER
|| track
== TRACK_RIGHT
) ? 4 : 6;
329 sig
= GB(t
.m3(), pos
, 2);
330 if (--sig
== 0) sig
= IsPbsSignal(GetSignalType(t
, track
)) ? 2 : 3;
331 SB(t
.m3(), pos
, 2, sig
);
334 inline SignalVariant
GetSignalVariant(Tile t
, Track track
)
336 uint8_t pos
= (track
== TRACK_LOWER
|| track
== TRACK_RIGHT
) ? 7 : 3;
337 return (SignalVariant
)GB(t
.m2(), pos
, 1);
340 inline void SetSignalVariant(Tile t
, Track track
, SignalVariant v
)
342 uint8_t pos
= (track
== TRACK_LOWER
|| track
== TRACK_RIGHT
) ? 7 : 3;
343 SB(t
.m2(), pos
, 1, v
);
344 if (track
== INVALID_TRACK
) SB(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 inline void SetSignalStates(Tile tile
, uint state
)
354 SB(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 inline uint
GetSignalStates(Tile tile
)
364 return GB(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 inline SignalState
GetSingleSignalState(Tile t
, uint8_t 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 inline void SetPresentSignals(Tile tile
, uint signals
)
385 SB(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 inline uint
GetPresentSignals(Tile tile
)
395 return GB(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 inline bool IsSignalPresent(Tile t
, uint8_t signalbit
)
406 return HasBit(GetPresentSignals(t
), signalbit
);
410 * Checks for the presence of signals (either way) on the given track on the
413 inline bool HasSignalOnTrack(Tile 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 inline bool HasSignalOnTrackdir(Tile 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 inline SignalState
GetSignalStateByTrackdir(Tile 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 inline void SetSignalStateByTrackdir(Tile 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 inline bool HasPbsSignalOnTrackdir(Tile 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 inline bool HasOnewaySignalBlockingTrackdir(Tile tile
, Trackdir td
)
477 return IsTileType(tile
, MP_RAILWAY
) && HasSignalOnTrackdir(tile
, ReverseTrackdir(td
)) &&
478 !HasSignalOnTrackdir(tile
, td
) && IsOnewaySignal(tile
, TrackdirToTrack(td
));
482 RailType
GetTileRailType(Tile 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 inline void SetRailGroundType(Tile t
, RailGroundType rgt
)
505 SB(t
.m4(), 0, 4, rgt
);
508 inline RailGroundType
GetRailGroundType(Tile t
)
510 return (RailGroundType
)GB(t
.m4(), 0, 4);
513 inline bool IsSnowRailGround(Tile t
)
515 return GetRailGroundType(t
) == RAIL_GROUND_ICE_DESERT
;
519 inline void MakeRailNormal(Tile t
, Owner o
, TrackBits b
, RailType r
)
521 SetTileType(t
, MP_RAILWAY
);
523 SetDockingTile(t
, false);
527 t
.m5() = RAIL_TILE_NORMAL
<< 6 | b
;
534 * Sets the exit direction of a rail depot.
535 * @param tile Tile of the depot.
536 * @param dir Direction of the depot exit.
538 inline void SetRailDepotExitDirection(Tile tile
, DiagDirection dir
)
540 assert(IsRailDepotTile(tile
));
541 SB(tile
.m5(), 0, 2, dir
);
546 * @param tile Tile to make a depot on.
547 * @param owner New owner of the depot.
548 * @param depot_id New depot ID.
549 * @param dir Direction of the depot exit.
550 * @param rail_type Rail type of the depot.
552 inline void MakeRailDepot(Tile tile
, Owner owner
, DepotID depot_id
, DiagDirection dir
, RailType rail_type
)
554 SetTileType(tile
, MP_RAILWAY
);
555 SetTileOwner(tile
, owner
);
556 SetDockingTile(tile
, false);
557 tile
.m2() = depot_id
;
560 tile
.m5() = RAIL_TILE_DEPOT
<< 6 | dir
;
561 SB(tile
.m6(), 2, 4, 0);
563 tile
.m8() = rail_type
;
566 #endif /* RAIL_MAP_H */