OpenTTD
rail_map.h
Go to the documentation of this file.
1 /* $Id$ */
2 
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  */
9 
12 #ifndef RAIL_MAP_H
13 #define RAIL_MAP_H
14 
15 #include "rail_type.h"
16 #include "depot_type.h"
17 #include "signal_func.h"
18 #include "track_func.h"
19 #include "tile_map.h"
20 #include "water_map.h"
21 #include "signal_type.h"
22 
23 
29 };
30 
39 {
40  assert(IsTileType(t, MP_RAILWAY));
41  return (RailTileType)GB(_m[t].m5, 6, 2);
42 }
43 
51 static inline bool IsPlainRail(TileIndex t)
52 {
54  return rtt == RAIL_TILE_NORMAL || rtt == RAIL_TILE_SIGNALS;
55 }
56 
62 static inline bool IsPlainRailTile(TileIndex t)
63 {
64  return IsTileType(t, MP_RAILWAY) && IsPlainRail(t);
65 }
66 
67 
74 static inline bool HasSignals(TileIndex t)
75 {
77 }
78 
85 static inline void SetHasSignals(TileIndex tile, bool signals)
86 {
87  assert(IsPlainRailTile(tile));
88  SB(_m[tile].m5, 6, 1, signals);
89 }
90 
97 static inline bool IsRailDepot(TileIndex t)
98 {
99  return GetRailTileType(t) == RAIL_TILE_DEPOT;
100 }
101 
107 static inline bool IsRailDepotTile(TileIndex t)
108 {
109  return IsTileType(t, MP_RAILWAY) && IsRailDepot(t);
110 }
111 
118 {
119  return (RailType)GB(_me[t].m8, 0, 6);
120 }
121 
127 static inline void SetRailType(TileIndex t, RailType r)
128 {
129  SB(_me[t].m8, 0, 6, r);
130 }
131 
132 
138 static inline TrackBits GetTrackBits(TileIndex tile)
139 {
140  assert(IsPlainRailTile(tile));
141  return (TrackBits)GB(_m[tile].m5, 0, 6);
142 }
143 
149 static inline void SetTrackBits(TileIndex t, TrackBits b)
150 {
151  assert(IsPlainRailTile(t));
152  SB(_m[t].m5, 0, 6, b);
153 }
154 
162 static inline bool HasTrack(TileIndex tile, Track track)
163 {
164  return HasBit(GetTrackBits(tile), track);
165 }
166 
174 {
175  return (DiagDirection)GB(_m[t].m5, 0, 2);
176 }
177 
185 {
187 }
188 
189 
197 {
198  assert(IsPlainRailTile(t));
199  byte track_b = GB(_m[t].m2, 8, 3);
200  Track track = (Track)(track_b - 1); // map array saves Track+1
201  if (track_b == 0) return TRACK_BIT_NONE;
202  return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
203 }
204 
211 static inline void SetTrackReservation(TileIndex t, TrackBits b)
212 {
213  assert(IsPlainRailTile(t));
214  assert(b != INVALID_TRACK_BIT);
215  assert(!TracksOverlap(b));
216  Track track = RemoveFirstTrack(&b);
217  SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track + 1);
218  SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
219 }
220 
228 static inline bool TryReserveTrack(TileIndex tile, Track t)
229 {
230  assert(HasTrack(tile, t));
231  TrackBits bits = TrackToTrackBits(t);
233  if ((res & bits) != TRACK_BIT_NONE) return false; // already reserved
234  res |= bits;
235  if (TracksOverlap(res)) return false; // crossing reservation present
236  SetTrackReservation(tile, res);
237  return true;
238 }
239 
246 static inline void UnreserveTrack(TileIndex tile, Track t)
247 {
248  assert(HasTrack(tile, t));
250  res &= ~TrackToTrackBits(t);
251  SetTrackReservation(tile, res);
252 }
253 
260 static inline bool HasDepotReservation(TileIndex t)
261 {
262  assert(IsRailDepot(t));
263  return HasBit(_m[t].m5, 4);
264 }
265 
272 static inline void SetDepotReservation(TileIndex t, bool b)
273 {
274  assert(IsRailDepot(t));
275  SB(_m[t].m5, 4, 1, (byte)b);
276 }
277 
285 {
287 }
288 
289 
290 static inline bool IsPbsSignal(SignalType s)
291 {
292  return s == SIGTYPE_PBS || s == SIGTYPE_PBS_ONEWAY;
293 }
294 
295 static inline SignalType GetSignalType(TileIndex t, Track track)
296 {
297  assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
298  byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
299  return (SignalType)GB(_m[t].m2, pos, 3);
300 }
301 
302 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
303 {
304  assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
305  byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
306  SB(_m[t].m2, pos, 3, s);
307  if (track == INVALID_TRACK) SB(_m[t].m2, 4, 3, s);
308 }
309 
310 static inline bool IsPresignalEntry(TileIndex t, Track track)
311 {
312  return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO;
313 }
314 
315 static inline bool IsPresignalExit(TileIndex t, Track track)
316 {
317  return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO;
318 }
319 
321 static inline bool IsOnewaySignal(TileIndex t, Track track)
322 {
323  return GetSignalType(t, track) != SIGTYPE_PBS;
324 }
325 
326 static inline void CycleSignalSide(TileIndex t, Track track)
327 {
328  byte sig;
329  byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
330 
331  sig = GB(_m[t].m3, pos, 2);
332  if (--sig == 0) sig = IsPbsSignal(GetSignalType(t, track)) ? 2 : 3;
333  SB(_m[t].m3, pos, 2, sig);
334 }
335 
336 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
337 {
338  byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
339  return (SignalVariant)GB(_m[t].m2, pos, 1);
340 }
341 
342 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
343 {
344  byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
345  SB(_m[t].m2, pos, 1, v);
346  if (track == INVALID_TRACK) SB(_m[t].m2, 7, 1, v);
347 }
348 
354 static inline void SetSignalStates(TileIndex tile, uint state)
355 {
356  SB(_m[tile].m4, 4, 4, state);
357 }
358 
364 static inline uint GetSignalStates(TileIndex tile)
365 {
366  return GB(_m[tile].m4, 4, 4);
367 }
368 
375 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
376 {
377  return (SignalState)HasBit(GetSignalStates(t), signalbit);
378 }
379 
385 static inline void SetPresentSignals(TileIndex tile, uint signals)
386 {
387  SB(_m[tile].m3, 4, 4, signals);
388 }
389 
395 static inline uint GetPresentSignals(TileIndex tile)
396 {
397  return GB(_m[tile].m3, 4, 4);
398 }
399 
406 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
407 {
408  return HasBit(GetPresentSignals(t), signalbit);
409 }
410 
415 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
416 {
417  assert(IsValidTrack(track));
418  return GetRailTileType(tile) == RAIL_TILE_SIGNALS && (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
419 }
420 
428 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
429 {
430  assert (IsValidTrackdir(trackdir));
431  return GetRailTileType(tile) == RAIL_TILE_SIGNALS && GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
432 }
433 
441 {
442  assert(IsValidTrackdir(trackdir));
443  assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
444  return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
446 }
447 
451 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
452 {
453  if (state == SIGNAL_STATE_GREEN) { // set 1
454  SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
455  } else {
456  SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
457  }
458 }
459 
465 static inline bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
466 {
467  return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, td) &&
468  IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)));
469 }
470 
478 {
479  return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, ReverseTrackdir(td)) &&
480  !HasSignalOnTrackdir(tile, td) && IsOnewaySignal(tile, TrackdirToTrack(td));
481 }
482 
483 
485 
503 };
504 
505 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
506 {
507  SB(_m[t].m4, 0, 4, rgt);
508 }
509 
510 static inline RailGroundType GetRailGroundType(TileIndex t)
511 {
512  return (RailGroundType)GB(_m[t].m4, 0, 4);
513 }
514 
515 static inline bool IsSnowRailGround(TileIndex t)
516 {
517  return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
518 }
519 
520 
521 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
522 {
524  SetTileOwner(t, o);
525  SetDockingTile(t, false);
526  _m[t].m2 = 0;
527  _m[t].m3 = 0;
528  _m[t].m4 = 0;
529  _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
530  SB(_me[t].m6, 2, 4, 0);
531  _me[t].m7 = 0;
532  _me[t].m8 = r;
533 }
534 
535 
536 static inline void MakeRailDepot(TileIndex t, Owner o, DepotID did, DiagDirection d, RailType r)
537 {
539  SetTileOwner(t, o);
540  SetDockingTile(t, false);
541  _m[t].m2 = did;
542  _m[t].m3 = 0;
543  _m[t].m4 = 0;
544  _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
545  SB(_me[t].m6, 2, 4, 0);
546  _me[t].m7 = 0;
547  _me[t].m8 = r;
548 }
549 
550 #endif /* RAIL_MAP_H */
Owner
Enum for all companies/owners.
Definition: company_type.h:20
Grass with a fence at the northern side.
Definition: rail_map.h:499
static bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
Checks for the presence of signals along the given trackdir on the given rail tile.
Definition: rail_map.h:428
presignal inter-block
Definition: signal_type.h:29
uint16 DepotID
Type for the unique identifier of depots.
Definition: depot_type.h:15
Depot (one entrance)
Definition: rail_map.h:28
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:200
Flag for an invalid track.
Definition: track_type.h:30
Grass with a fence at the NE and SW edges.
Definition: rail_map.h:495
presignal block exit
Definition: signal_type.h:28
SignalType
Type of signal, i.e.
Definition: signal_type.h:25
static SignalState GetSingleSignalState(TileIndex t, byte signalbit)
Get the state of a single signal.
Definition: rail_map.h:375
static void UnreserveTrack(TileIndex tile, Track t)
Lift the reservation of a specific track on a tile.
Definition: rail_map.h:246
presignal block entry
Definition: signal_type.h:27
Track in the lower corner of the tile (south)
Definition: track_type.h:26
static Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:272
byte m7
Primarily used for newgrf support.
Definition: map_type.h:37
static bool HasSignalOnTrack(TileIndex tile, Track track)
Checks for the presence of signals (either way) on the given track on the given rail tile...
Definition: rail_map.h:415
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:22
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:72
Tile * _m
Tiles of the map.
Definition: map.cpp:32
static bool TryReserveTrack(TileIndex tile, Track t)
Try to reserve a specific track on a tile.
Definition: rail_map.h:228
SignalState
These are states in which a signal can be.
Definition: signal_type.h:46
Grass with a fence at the eastern side.
Definition: rail_map.h:496
static bool IsOnewaySignal(TileIndex t, Track track)
One-way signals can&#39;t be passed the &#39;wrong&#39; way.
Definition: rail_map.h:321
Icy or sandy.
Definition: rail_map.h:500
static Track TrackToOppositeTrack(Track t)
Find the opposite track to a given track.
Definition: track_func.h:241
Grass with a fence at the SW edge.
Definition: rail_map.h:494
static Track DiagDirToDiagTrack(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track incidating with that diagdir.
Definition: track_func.h:522
Nothing (dirt)
Definition: rail_map.h:488
A railway.
Definition: tile_type.h:44
static void SetHasSignals(TileIndex tile, bool signals)
Add/remove the &#39;has signal&#39; bit from the RailTileType.
Definition: rail_map.h:85
static void SetTrackBits(TileIndex t, TrackBits b)
Sets the track bits of the given tile.
Definition: rail_map.h:149
static uint GetSignalStates(TileIndex tile)
Set the states of the signals (Along/AgainstTrackDir)
Definition: rail_map.h:364
static void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
Sets the state of the signal along the given trackdir.
Definition: rail_map.h:451
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Grass with a fence at the NW and SE edges.
Definition: rail_map.h:492
static byte SignalOnTrack(Track track)
Maps a Track to the bits that store the status of the two signals that can be present on the given tr...
Definition: signal_func.h:44
Normal rail tile with signals.
Definition: rail_map.h:27
static uint GetPresentSignals(TileIndex tile)
Get whether the given signals are present (Along/AgainstTrackDir)
Definition: rail_map.h:395
Track in the right corner of the tile (east)
Definition: track_type.h:28
Snow only on higher part of slope (steep or one corner raised)
Definition: rail_map.h:502
Header files for depots (not hangars)
Types and classes related to signals.
static bool IsValidTrackdir(Trackdir trackdir)
Checks if a Trackdir is valid for non-road vehicles.
Definition: track_func.h:62
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:152
static bool IsSignalPresent(TileIndex t, byte signalbit)
Checks whether the given signals is present.
Definition: rail_map.h:406
static bool IsValidTrack(Track track)
Checks if a Track is valid.
Definition: track_func.h:38
Grass with a fence at the NW edge.
Definition: rail_map.h:490
static DiagDirection GetRailDepotDirection(TileIndex t)
Returns the direction the depot is facing to.
Definition: rail_map.h:173
RailType GetTileRailType(TileIndex tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition: rail.cpp:157
Map accessors for water tiles.
static TrackBits GetRailReservationTrackBits(TileIndex t)
Returns the reserved track bits of the tile.
Definition: rail_map.h:196
no-entry signal
Definition: signal_type.h:31
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:33
byte m5
General purpose.
Definition: map_type.h:26
static bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
Is a pbs signal present along the trackdir?
Definition: rail_map.h:465
Grass with a fence at the NE edge.
Definition: rail_map.h:493
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:40
static void SetSignalStates(TileIndex tile, uint state)
Set the states of the signals (Along/AgainstTrackDir)
Definition: rail_map.h:354
static void SetPresentSignals(TileIndex tile, uint signals)
Set whether the given signals are present (Along/AgainstTrackDir)
Definition: rail_map.h:385
Grass with a fence and shore or water on the free halftile.
Definition: rail_map.h:501
static Track GetRailDepotTrack(TileIndex t)
Returns the track of a depot, ignoring direction.
Definition: rail_map.h:184
static bool IsPlainRailTile(TileIndex t)
Checks whether the tile is a rail tile or rail tile with signals.
Definition: rail_map.h:62
static Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition: track_func.h:257
static bool HasSignals(TileIndex t)
Checks if a rail tile has signals.
Definition: rail_map.h:74
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:38
static TrackBits GetTrackBits(TileIndex tile)
Gets the track bits of the given tile.
Definition: rail_map.h:138
Flag for an invalid trackbits value.
Definition: track_type.h:59
static TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition: track_func.h:87
No track.
Definition: track_type.h:41
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
static bool IsRailDepot(TileIndex t)
Is this rail tile a rail depot?
Definition: rail_map.h:97
static Track RemoveFirstTrack(TrackBits *tracks)
Removes first Track from TrackBits and returns it.
Definition: track_func.h:141
static bool IsRailDepotTile(TileIndex t)
Is this tile rail tile and a rail depot?
Definition: rail_map.h:107
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static byte SignalAlongTrackdir(Trackdir trackdir)
Maps a trackdir to the bit that stores its status in the map arrays, in the direction along with the ...
Definition: signal_func.h:24
static TrackBits GetDepotReservationTrackBits(TileIndex t)
Get the reserved track bits for a depot.
Definition: rail_map.h:284
Track
These are used to specify a single track.
Definition: track_type.h:21
RailTileType
Different types of Rail-related tiles.
Definition: rail_map.h:25
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:133
static bool HasTrack(TileIndex tile, Track track)
Returns whether the given track is present on the given tile.
Definition: rail_map.h:162
Normal rail tile without signals.
Definition: rail_map.h:26
The signal is red.
Definition: signal_type.h:47
uint16 m8
General purpose.
Definition: map_type.h:38
RailGroundType
The ground &#39;under&#39; the rail.
Definition: rail_map.h:487
Grass with a fence at the SE edge.
Definition: rail_map.h:491
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
DiagDirection
Enumeration for diagonal directions.
static bool IsPlainRail(TileIndex t)
Returns whether this is plain rails, with or without signals.
Definition: rail_map.h:51
Map writing/reading functions for tiles.
static bool HasDepotReservation(TileIndex t)
Get the reservation state of the depot.
Definition: rail_map.h:260
Different conversion functions from one kind of track to another.
static bool HasOnewaySignalBlockingTrackdir(TileIndex tile, Trackdir td)
Is a one-way signal blocking the trackdir? A one-way signal on the trackdir against will block...
Definition: rail_map.h:477
Functions related to signals.
Grass with a fence at the southern side.
Definition: rail_map.h:498
static void SetTrackReservation(TileIndex t, TrackBits b)
Sets the reserved track bits of the tile.
Definition: rail_map.h:211
static SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
Gets the state of the signal along the given trackdir.
Definition: rail_map.h:440
SignalVariant
Variant of the signal, i.e.
Definition: signal_type.h:18
normal pbs signal
Definition: signal_type.h:30
static void SetDockingTile(TileIndex t, bool b)
Set the docking tile state of a tile.
Definition: water_map.h:357
byte m3
General purpose.
Definition: map_type.h:24
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:117
static bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition: track_func.h:655
Grass with a fence at the western side.
Definition: rail_map.h:497
The signal is green.
Definition: signal_type.h:48
static void SetRailType(TileIndex t, RailType r)
Sets the rail type of the given tile.
Definition: rail_map.h:127
The different types of rail.
byte m4
General purpose.
Definition: map_type.h:25
static void SetDepotReservation(TileIndex t, bool b)
Set the reservation state of the depot.
Definition: rail_map.h:272