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 map/rail.h Map tile accessors for railway tiles. */
13 #include "../stdafx.h"
14 #include "../core/bitmath_func.hpp"
15 #include "../tile/rail.h"
22 #include "../direction_type.h"
23 #include "../direction_func.h"
24 #include "../track_type.h"
25 #include "../track_func.h"
26 #include "../rail_type.h"
27 #include "../company_type.h"
30 * Gets the track bits of the given tile
31 * @param tile the tile to get the track bits from
32 * @return the track bits of the tile
34 static inline TrackBits
GetTrackBits(TileIndex tile
)
36 return tile_get_trackbits(&_mc
[tile
]);
40 * Sets the track bits of the given tile
41 * @param t the tile to set the track bits of
42 * @param b the new track bits for the tile
44 static inline void SetTrackBits(TileIndex t
, TrackBits b
)
46 tile_set_trackbits(&_mc
[t
], b
);
50 * Returns whether the given track is present on the given tile.
51 * @param tile the tile to check the track presence of
52 * @param track the track to search for on the tile
53 * @pre IsRailwayTile(tile)
54 * @return true if and only if the given track exists on the tile
56 static inline bool HasTrack(TileIndex tile
, Track track
)
58 return tile_has_track(&_mc
[tile
], track
);
63 * Gets the rail type of the given tile
64 * @param t the tile to get the rail type from
65 * @return the rail type of the tile
67 static inline RailType
GetRailType(TileIndex t
, Track track
= INVALID_TRACK
)
69 return tile_get_rail_type(&_mc
[t
], track
);
73 * Sets the rail type of the given tile
74 * @param t the tile to set the rail type of
75 * @param r the new rail type for the tile
77 static inline void SetRailType(TileIndex t
, RailType r
, Track track
= INVALID_TRACK
)
79 tile_set_rail_type(&_mc
[t
], r
, track
);
83 * Gets the rail type of the rail inciding on a given tile side
84 * @param t the tile to get the rail type from
85 * @param dir the side to get the track to check
86 * @return the rail type of the tracks in the tile inciding on the given side, or INVALID_RAILTYPE if there are none
88 static inline RailType
GetSideRailType(TileIndex t
, DiagDirection dir
)
90 return tile_get_side_rail_type(&_mc
[t
], dir
);
94 * Gets the rail type of a rail bridge
95 * @param t the tile to get the rail type from
96 * @return the rail type of the bridge
98 static inline RailType
GetBridgeRailType(TileIndex t
)
100 return tile_get_bridge_rail_type(&_mc
[t
]);
105 * Returns the reserved track bits of the tile
106 * @pre IsRailwayTile(t)
107 * @param t the tile to query
108 * @return the track bits
110 static inline TrackBits
GetRailReservationTrackBits(TileIndex t
)
112 return tile_get_reservation_trackbits(&_mc
[t
]);
116 * Sets the reserved track bits of the tile
117 * @pre IsRailwayTile(t) && !TracksOverlap(b)
118 * @param t the tile to change
119 * @param b the track bits
121 static inline void SetTrackReservation(TileIndex t
, TrackBits b
)
123 tile_set_reservation_trackbits(&_mc
[t
], b
);
127 * Try to reserve a specific track on a tile
128 * @pre IsRailwayTile(t) && HasTrack(tile, t)
129 * @param tile the tile
130 * @param t the rack to reserve
131 * @return true if successful
133 static inline bool TryReserveTrack(TileIndex tile
, Track t
)
135 assert(IsRailwayTile(tile
));
136 assert(HasTrack(tile
, t
));
137 TrackBits bits
= TrackToTrackBits(t
);
138 TrackBits res
= GetRailReservationTrackBits(tile
);
139 if ((res
& bits
) != TRACK_BIT_NONE
) return false; // already reserved
141 if (TracksOverlap(res
)) return false; // crossing reservation present
142 SetTrackReservation(tile
, res
);
147 * Lift the reservation of a specific track on a tile
148 * @pre IsRailwayTile(t) && HasTrack(tile, t)
149 * @param tile the tile
150 * @param t the track to free
152 static inline void UnreserveTrack(TileIndex tile
, Track t
)
154 assert(IsRailwayTile(tile
));
155 assert(HasTrack(tile
, t
));
156 TrackBits res
= GetRailReservationTrackBits(tile
);
157 res
&= ~TrackToTrackBits(t
);
158 SetTrackReservation(tile
, res
);
163 * Get the signal byte for a signal
164 * @param tile The tile whose signal byte to get
165 * @param track The track whose signal byte to get
166 * @pre IsRailwayTile(tile)
168 static inline SignalPair
*maptile_signalpair(TileIndex tile
, Track track
)
170 return tile_signalpair(&_mc
[tile
], track
);
175 * Clear signals on a track
176 * @param tile the tile to clear the signals from
177 * @param track the track to clear the signals from
179 static inline void ClearSignals(TileIndex tile
, Track track
)
181 tile_clear_signals(&_mc
[tile
], track
);
185 * Get whether the given signals are present (Along/AgainstTrackDir)
186 * @param tile the tile to get the present signals for
187 * @param track the track to get the present signals for
188 * @return the signals that are present
190 static inline uint
GetPresentSignals(TileIndex tile
, Track track
)
192 return tile_get_present_signals(&_mc
[tile
], track
);
196 * Set whether the given signals are present (Along/AgainstTrackDir)
197 * @param tile the tile to set the present signals for
198 * @param track the track to set the present signals for
199 * @param signals the signals that have to be present
201 static inline void SetPresentSignals(TileIndex tile
, Track track
, uint signals
)
203 tile_set_present_signals(&_mc
[tile
], track
, signals
);
207 * Checks for the presence of signals (either way) on the given track on the
210 static inline bool HasSignalOnTrack(TileIndex tile
, Track track
)
212 return tile_has_track_signals(&_mc
[tile
], track
);
216 * Checks for the presence of signals along the given trackdir on the given
219 * Along meaning if you are currently driving on the given trackdir, this is
220 * the signal that is facing us (for which we stop when it's red).
222 static inline bool HasSignalOnTrackdir(TileIndex tile
, Trackdir trackdir
)
224 return tile_has_trackdir_signal(&_mc
[tile
], trackdir
);
229 * Get the states of the signals (Along/AgainstTrackDir)
230 * @param tile the tile to get the states for
231 * @param track the tile to get the states for
232 * @return the state of the signals
234 static inline uint
GetSignalStates(TileIndex tile
, Track track
)
236 return tile_get_signal_states(&_mc
[tile
], track
);
240 * Gets the state of the signal along the given trackdir.
242 * Along meaning if you are currently driving on the given trackdir, this is
243 * the signal that is facing us (for which we stop when it's red).
245 static inline SignalState
GetSignalStateByTrackdir(TileIndex tile
, Trackdir trackdir
)
247 return tile_get_signal_state(&_mc
[tile
], trackdir
);
251 * Sets the state of the signal along the given trackdir.
253 static inline void SetSignalStateByTrackdir(TileIndex tile
, Trackdir trackdir
, SignalState state
)
255 tile_set_signal_state(&_mc
[tile
], trackdir
, state
);
259 static inline SignalType
GetSignalType(TileIndex t
, Track track
)
261 return tile_get_signal_type(&_mc
[t
], track
);
264 static inline void SetSignalType(TileIndex t
, Track track
, SignalType s
)
266 tile_set_signal_type(&_mc
[t
], track
, s
);
269 static inline SignalVariant
GetSignalVariant(TileIndex t
, Track track
)
271 return tile_get_signal_variant(&_mc
[t
], track
);
274 static inline void SetSignalVariant(TileIndex t
, Track track
, SignalVariant v
)
276 tile_set_signal_variant(&_mc
[t
], track
, v
);
280 static inline RailGroundType
GetRailGroundType(TileIndex t
)
282 return tile_get_rail_ground(&_mc
[t
]);
285 static inline void SetRailGroundType(TileIndex t
, RailGroundType rgt
)
287 tile_set_rail_ground(&_mc
[t
], rgt
);
292 * Determines the type of rail bridge on a tile
293 * @param t The tile to analyze
294 * @pre IsRailBridgeTile(t)
295 * @return The bridge type
297 static inline uint
GetRailBridgeType(TileIndex t
)
299 return tile_get_rail_bridge_type(&_mc
[t
]);
303 * Set the type of rail bridge on a tile
304 * @param t The tile to set
305 * @param type The type to set
307 static inline void SetRailBridgeType(TileIndex t
, uint type
)
309 tile_set_rail_bridge_type(&_mc
[t
], type
);
313 * Check if a rail bridge is an extended bridge head
314 * @param t The tile to check
315 * @return Whether there are track bits set other than the axis track bit
317 static inline bool IsExtendedRailBridge(TileIndex t
)
319 return tile_is_rail_custom_bridgehead(&_mc
[t
]);
324 * Get the reservation state of the rail bridge middle part
325 * @pre IsRailBridgeTile(t)
327 * @return reservation state
329 static inline bool HasBridgeMiddleReservation(TileIndex t
)
331 return tile_is_bridge_middle_reserved(&_mc
[t
]);
335 * Set the reservation state of the rail bridge middle part
336 * @pre IsRailBridgeTile(t)
338 * @param b the reservation state
340 static inline void SetBridgeMiddleReservation(TileIndex t
, bool b
)
342 tile_set_bridge_middle_reserved(&_mc
[t
], b
);
346 static inline void MakeRailNormal(TileIndex t
, Owner o
, TrackBits b
, RailType r
)
348 tile_make_railway(&_mc
[t
], o
, b
, r
);
352 * Make a bridge ramp for rails.
353 * @param t the tile to make a bridge ramp
354 * @param o the new owner of the bridge ramp
355 * @param bridgetype the type of bridge this bridge ramp belongs to
356 * @param d the direction this ramp must be facing
357 * @param r the rail type of the bridge
359 static inline void MakeRailBridgeRamp(TileIndex t
, Owner o
, uint bridgetype
, DiagDirection d
, RailType r
)
361 tile_make_rail_bridge(&_mc
[t
], o
, bridgetype
, d
, r
);
365 * Make a normal rail tile from a rail bridge ramp.
366 * @param t the tile to make a normal rail
367 * @note trackbits will have to be adjusted when this function is called
369 static inline void MakeNormalRailFromBridge(TileIndex t
)
371 tile_make_railway_from_bridge(&_mc
[t
]);
375 * Make a rail bridge tile from a normal rail track.
376 * @param t the tile to make a rail bridge
377 * @param bridgetype the type of bridge this bridge ramp belongs to
378 * @param d the direction this ramp must be facing
379 * @note trackbits will have to be adjusted when this function is called
381 static inline void MakeRailBridgeFromRail(TileIndex t
, uint bridgetype
, DiagDirection d
)
383 tile_make_rail_bridge_from_track(&_mc
[t
], bridgetype
, d
);
386 #endif /* MAP_RAIL_H */