Fix crash when setting separation mode for vehicles with no orders list.
[openttd-joker.git] / src / rail_map.h
blob5c797aead941afc2d20477c9a0fa4257f3fd3548
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file rail_map.h Hides the direct accesses to the map array with map accessors */
12 #ifndef RAIL_MAP_H
13 #define RAIL_MAP_H
15 #include "rail.h"
16 #include "rail_type.h"
17 #include "depot_type.h"
18 #include "signal_func.h"
19 #include "track_func.h"
20 #include "tile_map.h"
21 #include "signal_type.h"
22 #include "tunnelbridge_map.h"
25 /** Different types of Rail-related tiles */
26 enum RailTileType {
27 RAIL_TILE_NORMAL = 0, ///< Normal rail tile without signals
28 RAIL_TILE_SIGNALS = 1, ///< Normal rail tile with signals
29 RAIL_TILE_DEPOT = 3, ///< Depot (one entrance)
32 static const RailTypeLabel _planning_tracks_label = 1347174734u;
33 static const RailTypeLabel _pipeline_tracks_label = 1346981957u;
34 static const RailTypeLabel _wires_tracks_label = 1413827653u;
36 RailType GetTileRailType(TileIndex tile);
38 /**
39 * Returns the RailTileType (normal with or without signals,
40 * waypoint or depot).
41 * @param t the tile to get the information from
42 * @pre IsTileType(t, MP_RAILWAY)
43 * @return the RailTileType
45 static inline RailTileType GetRailTileType(TileIndex t)
47 assert(IsTileType(t, MP_RAILWAY));
48 return (RailTileType)GB(_m[t].m5, 6, 2);
51 /**
52 * Returns whether this is plain rails, with or without signals. Iow, if this
53 * tiles RailTileType is RAIL_TILE_NORMAL or RAIL_TILE_SIGNALS.
54 * @param t the tile to get the information from
55 * @pre IsTileType(t, MP_RAILWAY)
56 * @return true if and only if the tile is normal rail (with or without signals)
58 static inline bool IsPlainRail(TileIndex t)
60 RailTileType rtt = GetRailTileType(t);
61 return rtt == RAIL_TILE_NORMAL || rtt == RAIL_TILE_SIGNALS;
64 /**
65 * Checks whether the tile is a rail tile or rail tile with signals.
66 * @param t the tile to get the information from
67 * @return true if and only if the tile is normal rail (with or without signals)
69 static inline bool IsPlainRailTile(TileIndex t)
71 return IsTileType(t, MP_RAILWAY) && IsPlainRail(t);
75 /**
76 * Checks if a rail tile has signals.
77 * @param t the tile to get the information from
78 * @pre IsTileType(t, MP_RAILWAY)
79 * @return true if and only if the tile has signals
81 static inline bool HasSignals(TileIndex t)
83 return GetRailTileType(t) == RAIL_TILE_SIGNALS;
86 /**
87 * Add/remove the 'has signal' bit from the RailTileType
88 * @param tile the tile to add/remove the signals to/from
89 * @param signals whether the rail tile should have signals or not
90 * @pre IsPlainRailTile(tile)
92 static inline void SetHasSignals(TileIndex tile, bool signals)
94 assert(IsPlainRailTile(tile));
95 SB(_m[tile].m5, 6, 1, signals);
98 /**
99 * Is this rail tile a rail depot?
100 * @param t the tile to get the information from
101 * @pre IsTileType(t, MP_RAILWAY)
102 * @return true if and only if the tile is a rail depot
104 static inline bool IsRailDepot(TileIndex t)
106 return GetRailTileType(t) == RAIL_TILE_DEPOT;
110 * Is this tile rail tile and a rail depot?
111 * @param t the tile to get the information from
112 * @return true if and only if the tile is a rail depot
114 static inline bool IsRailDepotTile(TileIndex t)
116 return IsTileType(t, MP_RAILWAY) && IsRailDepot(t);
120 * Gets the rail type of the given tile
121 * @param t the tile to get the rail type from
122 * @return the rail type of the tile
124 static inline RailType GetRailType(TileIndex t)
126 return (RailType)((GB(_m[t].m1, 7, 1) << 4) | GB(_m[t].m3, 0, 4));
130 * Sets the rail type of the given tile
131 * @param t the tile to set the rail type of
132 * @param r the new rail type for the tile
134 static inline void SetRailType(TileIndex t, RailType r)
136 SB(_m[t].m1, 7, 1, GB(r, 4, 1));
137 SB(_m[t].m3, 0, 4, GB(r, 0, 4));
142 * Gets the track bits of the given tile
143 * @param tile the tile to get the track bits from
144 * @return the track bits of the tile
146 static inline TrackBits GetTrackBits(TileIndex tile)
148 assert(IsPlainRailTile(tile));
149 return (TrackBits)GB(_m[tile].m5, 0, 6);
153 * Sets the track bits of the given tile
154 * @param t the tile to set the track bits of
155 * @param b the new track bits for the tile
157 static inline void SetTrackBits(TileIndex t, TrackBits b)
159 assert(IsPlainRailTile(t));
160 SB(_m[t].m5, 0, 6, b);
164 * Returns whether the given track is present on the given tile.
165 * @param tile the tile to check the track presence of
166 * @param track the track to search for on the tile
167 * @pre IsPlainRailTile(tile)
168 * @return true if and only if the given track exists on the tile
170 static inline bool HasTrack(TileIndex tile, Track track)
172 return HasBit(GetTrackBits(tile), track);
176 * Returns the direction the depot is facing to
177 * @param t the tile to get the depot facing from
178 * @pre IsRailDepotTile(t)
179 * @return the direction the depot is facing
181 static inline DiagDirection GetRailDepotDirection(TileIndex t)
183 return (DiagDirection)GB(_m[t].m5, 0, 2);
187 * Returns the track of a depot, ignoring direction
188 * @pre IsRailDepotTile(t)
189 * @param t the tile to get the depot track from
190 * @return the track of the depot
192 static inline Track GetRailDepotTrack(TileIndex t)
194 return DiagDirToDiagTrack(GetRailDepotDirection(t));
199 * Returns the reserved track bits of the tile
200 * @pre IsPlainRailTile(t)
201 * @param t the tile to query
202 * @return the track bits
204 static inline TrackBits GetRailReservationTrackBits(TileIndex t)
206 assert(IsPlainRailTile(t));
207 byte track_b = GB(_m[t].m2, 8, 3);
208 Track track = (Track)(track_b - 1); // map array saves Track+1
209 if (track_b == 0) return TRACK_BIT_NONE;
210 return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
214 * Sets the reserved track bits of the tile
215 * @pre IsPlainRailTile(t) && !TracksOverlap(b)
216 * @param t the tile to change
217 * @param b the track bits
219 static inline void SetTrackReservation(TileIndex t, TrackBits b)
221 assert(IsPlainRailTile(t));
222 assert(b != INVALID_TRACK_BIT);
223 assert(!TracksOverlap(b));
224 Track track = RemoveFirstTrack(&b);
225 SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track + 1);
226 SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
230 * Try to reserve a specific track on a tile
231 * @pre IsPlainRailTile(t) && HasTrack(tile, t)
232 * @param tile the tile
233 * @param t the rack to reserve
234 * @return true if successful
236 static inline bool TryReserveTrack(TileIndex tile, Track t)
238 assert(HasTrack(tile, t));
239 TrackBits bits = TrackToTrackBits(t);
240 TrackBits res = GetRailReservationTrackBits(tile);
241 if ((res & bits) != TRACK_BIT_NONE) return false; // already reserved
242 res |= bits;
243 if (TracksOverlap(res)) return false; // crossing reservation present
244 SetTrackReservation(tile, res);
245 return true;
249 * Lift the reservation of a specific track on a tile
250 * @pre IsPlainRailTile(t) && HasTrack(tile, t)
251 * @param tile the tile
252 * @param t the track to free
254 static inline void UnreserveTrack(TileIndex tile, Track t)
256 assert(HasTrack(tile, t));
257 TrackBits res = GetRailReservationTrackBits(tile);
258 res &= ~TrackToTrackBits(t);
259 SetTrackReservation(tile, res);
263 * Get the reservation state of the depot
264 * @pre IsRailDepot(t)
265 * @param t the depot tile
266 * @return reservation state
268 static inline bool HasDepotReservation(TileIndex t)
270 assert(IsRailDepot(t));
271 return HasBit(_m[t].m5, 4);
275 * Set the reservation state of the depot
276 * @pre IsRailDepot(t)
277 * @param t the depot tile
278 * @param b the reservation state
280 static inline void SetDepotReservation(TileIndex t, bool b)
282 assert(IsRailDepot(t));
283 SB(_m[t].m5, 4, 1, (byte)b);
287 * Get the reserved track bits for a depot
288 * @pre IsRailDepot(t)
289 * @param t the tile
290 * @return reserved track bits
292 static inline TrackBits GetDepotReservationTrackBits(TileIndex t)
294 return HasDepotReservation(t) ? TrackToTrackBits(GetRailDepotTrack(t)) : TRACK_BIT_NONE;
297 static inline bool IsPbsSignal(SignalType s)
299 return s == SIGTYPE_PBS || s == SIGTYPE_PBS_ONEWAY;
303 * Tests if the given signal type is a PBS or a logic signal.
304 * @param s The signal type to test
305 * @return Returns true if the given signal type is a PBS or a logic signal
307 static inline bool IsPbsOrLogicSignal(SignalType s)
309 return IsPbsSignal(s) || s == SIGTYPE_LOGIC;
312 static inline SignalType GetSignalType(TileIndex t, Track track)
314 assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
315 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
316 return (SignalType)GB(_m[t].m2, pos, 3);
319 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
321 assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
322 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
323 SB(_m[t].m2, pos, 3, s);
324 if (track == INVALID_TRACK) SB(_m[t].m2, 4, 3, s);
327 static inline bool IsPresignalEntry(TileIndex t, Track track)
329 return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO;
332 static inline bool IsPresignalExit(TileIndex t, Track track)
334 return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO;
338 * Tests whether the signal on the given tile and track is a logic signal.
339 * @param t The tile to test
340 * @param track The track to test
341 * @return Returns true if the signal on the given tile and track is a logic signal.
343 static inline bool IsLogicSignal(TileIndex t, Track track)
345 return GetSignalType(t, track) == SIGTYPE_LOGIC;
348 /** One-way signals can't be passed the 'wrong' way. */
349 static inline bool IsOnewaySignal(TileIndex t, Track track)
351 return GetSignalType(t, track) != SIGTYPE_PBS;
354 static inline void CycleSignalSide(TileIndex t, Track track)
356 byte sig;
357 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
359 sig = GB(_m[t].m3, pos, 2);
360 if (--sig == 0) sig = IsPbsOrLogicSignal(GetSignalType(t, track)) ? 2 : 3;
361 SB(_m[t].m3, pos, 2, sig);
364 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
366 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
367 return (SignalVariant)GB(_m[t].m2, pos, 1);
370 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
372 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
373 SB(_m[t].m2, pos, 1, v);
374 if (track == INVALID_TRACK) SB(_m[t].m2, 7, 1, v);
378 * Set the states of the signals (Along/AgainstTrackDir)
379 * @param tile the tile to set the states for
380 * @param state the new state
382 static inline void SetSignalStates(TileIndex tile, uint state)
384 SB(_m[tile].m4, 4, 4, state);
388 * Set the states of the signals (Along/AgainstTrackDir)
389 * @param tile the tile to set the states for
390 * @return the state of the signals
392 static inline uint GetSignalStates(TileIndex tile)
394 return GB(_m[tile].m4, 4, 4);
398 * Get the state of a single signal
399 * @param t the tile to get the signal state for
400 * @param signalbit the signal
401 * @return the state of the signal
403 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
405 return (SignalState)HasBit(GetSignalStates(t), signalbit);
409 * Set whether the given signals are present (Along/AgainstTrackDir)
410 * @param tile the tile to set the present signals for
411 * @param signals the signals that have to be present
413 static inline void SetPresentSignals(TileIndex tile, uint signals)
415 SB(_m[tile].m3, 4, 4, signals);
419 * Get whether the given signals are present (Along/AgainstTrackDir)
420 * @param tile the tile to get the present signals for
421 * @return the signals that are present
423 static inline uint GetPresentSignals(TileIndex tile)
425 return GB(_m[tile].m3, 4, 4);
429 * Checks whether the given signals is present
430 * @param t the tile to check on
431 * @param signalbit the signal
432 * @return true if and only if the signal is present
434 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
436 return HasBit(GetPresentSignals(t), signalbit);
440 * Checks for the presence of signals (either way) on the given track on the
441 * given rail tile.
443 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
445 assert(IsValidTrack(track));
446 return GetRailTileType(tile) == RAIL_TILE_SIGNALS && (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
450 * Checks for the presence of signals along the given trackdir on the given
451 * rail tile.
453 * Along meaning if you are currently driving on the given trackdir, this is
454 * the signal that is facing us (for which we stop when it's red).
456 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
458 assert (IsValidTrackdir(trackdir));
459 return GetRailTileType(tile) == RAIL_TILE_SIGNALS && GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
463 * Gets the state of the signal along the given trackdir.
465 * Along meaning if you are currently driving on the given trackdir, this is
466 * the signal that is facing us (for which we stop when it's red).
468 static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
470 assert(IsValidTrackdir(trackdir));
471 assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
472 return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
473 SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
477 * Sets the state of the signal along the given trackdir.
479 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
481 if (state == SIGNAL_STATE_GREEN) { // set 1
482 SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
483 } else {
484 SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
489 * Is a pbs signal present along the trackdir?
490 * @param tile the tile to check
491 * @param td the trackdir to check
493 static inline bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
495 return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, td) &&
496 IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)));
500 * Is a one-way signal blocking the trackdir? A one-way signal on the
501 * trackdir against will block, but signals on both trackdirs won't.
502 * @param tile the tile to check
503 * @param td the trackdir to check
505 static inline bool HasOnewaySignalBlockingTrackdir(TileIndex tile, Trackdir td)
507 if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, ReverseTrackdir(td)) &&
508 !HasSignalOnTrackdir(tile, td) && IsOnewaySignal(tile, TrackdirToTrack(td))) {
509 return true;
511 if (IsTileType(tile, MP_TUNNELBRIDGE) && IsTunnelBridgeSignalSimulationExit(tile) &&
512 DiagDirToDiagTrackdir(GetTunnelBridgeDirection(tile)) == td) {
513 return true;
515 return false;
519 * Return the rail 'age'.
521 static inline byte GetRailAge(TileIndex ti)
523 assert(IsPlainRailTile(ti));
525 /* using high bits of m2: (ok for depots and clear tracks only) */
526 /* m2 used by PBS/YAPP, shifting to m7 -Phazorx */
527 return GB(_me[ti].m7,0,8);
531 * Does signal tile have "one or more trace restrict mappings present" bit set
532 * @param tile the tile to check
534 static inline bool IsRestrictedSignal(TileIndex tile)
536 assert(GetRailTileType(tile) == RAIL_TILE_SIGNALS);
537 return GB(_m[tile].m2, 12, 1) != 0;
541 * Set signal tile "one or more trace restrict mappings present" bit
542 * @param tile the tile to set
544 static inline void SetRestrictedSignal(TileIndex tile, bool is_restricted)
546 assert(GetRailTileType(tile) == RAIL_TILE_SIGNALS);
547 SB(_m[tile].m2, 12, 1, is_restricted);
552 * Set the rail 'age'.
554 static inline void SetRailAge(TileIndex ti, byte new_age)
556 assert(IsPlainRailTile(ti));
558 /* using high bits of m2: (ok for depots and clear tracks only) */
559 /* m2 used by PBS/YAPP, shifting to m7 -Phazorx */
560 SB(_me[ti].m7, 0, 8, new_age);
563 /** The ground 'under' the rail */
564 enum RailGroundType {
565 RAIL_GROUND_BARREN = 0, ///< Nothing (dirt)
566 RAIL_GROUND_GRASS = 1, ///< Grassy
567 RAIL_GROUND_FENCE_NW = 2, ///< Grass with a fence at the NW edge
568 RAIL_GROUND_FENCE_SE = 3, ///< Grass with a fence at the SE edge
569 RAIL_GROUND_FENCE_SENW = 4, ///< Grass with a fence at the NW and SE edges
570 RAIL_GROUND_FENCE_NE = 5, ///< Grass with a fence at the NE edge
571 RAIL_GROUND_FENCE_SW = 6, ///< Grass with a fence at the SW edge
572 RAIL_GROUND_FENCE_NESW = 7, ///< Grass with a fence at the NE and SW edges
573 RAIL_GROUND_FENCE_VERT1 = 8, ///< Grass with a fence at the eastern side
574 RAIL_GROUND_FENCE_VERT2 = 9, ///< Grass with a fence at the western side
575 RAIL_GROUND_FENCE_HORIZ1 = 10, ///< Grass with a fence at the southern side
576 RAIL_GROUND_FENCE_HORIZ2 = 11, ///< Grass with a fence at the northern side
577 RAIL_GROUND_ICE_DESERT = 12, ///< Icy or sandy
578 RAIL_GROUND_WATER = 13, ///< Grass with a fence and shore or water on the free halftile
579 RAIL_GROUND_HALF_SNOW = 14, ///< Snow only on higher part of slope (steep or one corner raised)
582 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
584 SB(_m[t].m4, 0, 4, rgt);
587 static inline RailGroundType GetRailGroundType(TileIndex t)
589 return (RailGroundType)GB(_m[t].m4, 0, 4);
592 static inline bool IsSnowRailGround(TileIndex t)
594 return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
598 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
600 SetTileType(t, MP_RAILWAY);
601 SetTileOwner(t, o);
602 _m[t].m2 = 0;
603 SetRailType(t, r);
604 _m[t].m4 = 0;
605 _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
606 SetRailAge(t, 0);
607 SB(_me[t].m6, 2, 4, 0);
608 _me[t].m7 = 0;
612 static inline void MakeRailDepot(TileIndex t, Owner o, DepotID did, DiagDirection d, RailType r)
614 SetTileType(t, MP_RAILWAY);
615 SetTileOwner(t, o);
616 _m[t].m2 = did;
617 SetRailType(t, r);
618 _m[t].m4 = 0;
619 _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
620 SB(_me[t].m6, 2, 4, 0);
621 _me[t].m7 = 0;
624 static inline byte GetTrackGrowthPhase(TileIndex ti)
626 return GetRailAge(ti) >> 6;
629 static inline void ResetRailAge(TileIndex ti)
631 assert(IsPlainRailTile(ti));
632 SetRailAge(ti, 0);
635 #endif /* RAIL_MAP_H */