OpenTTD
rail_map.h
Go to the documentation of this file.
1 /* $Id: rail_map.h 26878 2014-09-21 11:23:33Z rubidium $ */
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 "signal_type.h"
21 
22 
28 };
29 
38 {
39  assert(IsTileType(t, MP_RAILWAY));
40  return (RailTileType)GB(_m[t].m5, 6, 2);
41 }
42 
50 static inline bool IsPlainRail(TileIndex t)
51 {
53  return rtt == RAIL_TILE_NORMAL || rtt == RAIL_TILE_SIGNALS;
54 }
55 
61 static inline bool IsPlainRailTile(TileIndex t)
62 {
63  return IsTileType(t, MP_RAILWAY) && IsPlainRail(t);
64 }
65 
66 
73 static inline bool HasSignals(TileIndex t)
74 {
76 }
77 
84 static inline void SetHasSignals(TileIndex tile, bool signals)
85 {
86  assert(IsPlainRailTile(tile));
87  SB(_m[tile].m5, 6, 1, signals);
88 }
89 
96 static inline bool IsRailDepot(TileIndex t)
97 {
98  return GetRailTileType(t) == RAIL_TILE_DEPOT;
99 }
100 
106 static inline bool IsRailDepotTile(TileIndex t)
107 {
108  return IsTileType(t, MP_RAILWAY) && IsRailDepot(t);
109 }
110 
117 {
118  return (RailType)GB(_m[t].m3, 0, 4);
119 }
120 
126 static inline void SetRailType(TileIndex t, RailType r)
127 {
128  SB(_m[t].m3, 0, 4, r);
129 }
130 
131 
137 static inline TrackBits GetTrackBits(TileIndex tile)
138 {
139  assert(IsPlainRailTile(tile));
140  return (TrackBits)GB(_m[tile].m5, 0, 6);
141 }
142 
148 static inline void SetTrackBits(TileIndex t, TrackBits b)
149 {
150  assert(IsPlainRailTile(t));
151  SB(_m[t].m5, 0, 6, b);
152 }
153 
161 static inline bool HasTrack(TileIndex tile, Track track)
162 {
163  return HasBit(GetTrackBits(tile), track);
164 }
165 
173 {
174  return (DiagDirection)GB(_m[t].m5, 0, 2);
175 }
176 
184 {
186 }
187 
188 
196 {
197  assert(IsPlainRailTile(t));
198  byte track_b = GB(_m[t].m2, 8, 3);
199  Track track = (Track)(track_b - 1); // map array saves Track+1
200  if (track_b == 0) return TRACK_BIT_NONE;
201  return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
202 }
203 
210 static inline void SetTrackReservation(TileIndex t, TrackBits b)
211 {
212  assert(IsPlainRailTile(t));
213  assert(b != INVALID_TRACK_BIT);
214  assert(!TracksOverlap(b));
215  Track track = RemoveFirstTrack(&b);
216  SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track + 1);
217  SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
218 }
219 
227 static inline bool TryReserveTrack(TileIndex tile, Track t)
228 {
229  assert(HasTrack(tile, t));
230  TrackBits bits = TrackToTrackBits(t);
232  if ((res & bits) != TRACK_BIT_NONE) return false; // already reserved
233  res |= bits;
234  if (TracksOverlap(res)) return false; // crossing reservation present
235  SetTrackReservation(tile, res);
236  return true;
237 }
238 
245 static inline void UnreserveTrack(TileIndex tile, Track t)
246 {
247  assert(HasTrack(tile, t));
249  res &= ~TrackToTrackBits(t);
250  SetTrackReservation(tile, res);
251 }
252 
259 static inline bool HasDepotReservation(TileIndex t)
260 {
261  assert(IsRailDepot(t));
262  return HasBit(_m[t].m5, 4);
263 }
264 
271 static inline void SetDepotReservation(TileIndex t, bool b)
272 {
273  assert(IsRailDepot(t));
274  SB(_m[t].m5, 4, 1, (byte)b);
275 }
276 
284 {
286 }
287 
288 
289 static inline bool IsPbsSignal(SignalType s)
290 {
291  return s == SIGTYPE_PBS || s == SIGTYPE_PBS_ONEWAY;
292 }
293 
294 static inline SignalType GetSignalType(TileIndex t, Track track)
295 {
296  assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
297  byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
298  return (SignalType)GB(_m[t].m2, pos, 3);
299 }
300 
301 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
302 {
303  assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
304  byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
305  SB(_m[t].m2, pos, 3, s);
306  if (track == INVALID_TRACK) SB(_m[t].m2, 4, 3, s);
307 }
308 
309 static inline bool IsPresignalEntry(TileIndex t, Track track)
310 {
311  return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO;
312 }
313 
314 static inline bool IsPresignalExit(TileIndex t, Track track)
315 {
316  return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO;
317 }
318 
320 static inline bool IsOnewaySignal(TileIndex t, Track track)
321 {
322  return GetSignalType(t, track) != SIGTYPE_PBS;
323 }
324 
325 static inline void CycleSignalSide(TileIndex t, Track track)
326 {
327  byte sig;
328  byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
329 
330  sig = GB(_m[t].m3, pos, 2);
331  if (--sig == 0) sig = IsPbsSignal(GetSignalType(t, track)) ? 2 : 3;
332  SB(_m[t].m3, pos, 2, sig);
333 }
334 
335 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
336 {
337  byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
338  return (SignalVariant)GB(_m[t].m2, pos, 1);
339 }
340 
341 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
342 {
343  byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
344  SB(_m[t].m2, pos, 1, v);
345  if (track == INVALID_TRACK) SB(_m[t].m2, 7, 1, v);
346 }
347 
353 static inline void SetSignalStates(TileIndex tile, uint state)
354 {
355  SB(_m[tile].m4, 4, 4, state);
356 }
357 
363 static inline uint GetSignalStates(TileIndex tile)
364 {
365  return GB(_m[tile].m4, 4, 4);
366 }
367 
374 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
375 {
376  return (SignalState)HasBit(GetSignalStates(t), signalbit);
377 }
378 
384 static inline void SetPresentSignals(TileIndex tile, uint signals)
385 {
386  SB(_m[tile].m3, 4, 4, signals);
387 }
388 
394 static inline uint GetPresentSignals(TileIndex tile)
395 {
396  return GB(_m[tile].m3, 4, 4);
397 }
398 
405 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
406 {
407  return HasBit(GetPresentSignals(t), signalbit);
408 }
409 
414 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
415 {
416  assert(IsValidTrack(track));
417  return GetRailTileType(tile) == RAIL_TILE_SIGNALS && (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
418 }
419 
427 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
428 {
429  assert (IsValidTrackdir(trackdir));
430  return GetRailTileType(tile) == RAIL_TILE_SIGNALS && GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
431 }
432 
440 {
441  assert(IsValidTrackdir(trackdir));
442  assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
443  return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
445 }
446 
450 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
451 {
452  if (state == SIGNAL_STATE_GREEN) { // set 1
453  SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
454  } else {
455  SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
456  }
457 }
458 
464 static inline bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
465 {
466  return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, td) &&
467  IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)));
468 }
469 
477 {
478  return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, ReverseTrackdir(td)) &&
479  !HasSignalOnTrackdir(tile, td) && IsOnewaySignal(tile, TrackdirToTrack(td));
480 }
481 
482 
484 
502 };
503 
504 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
505 {
506  SB(_m[t].m4, 0, 4, rgt);
507 }
508 
509 static inline RailGroundType GetRailGroundType(TileIndex t)
510 {
511  return (RailGroundType)GB(_m[t].m4, 0, 4);
512 }
513 
514 static inline bool IsSnowRailGround(TileIndex t)
515 {
516  return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
517 }
518 
519 
520 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
521 {
523  SetTileOwner(t, o);
524  _m[t].m2 = 0;
525  _m[t].m3 = r;
526  _m[t].m4 = 0;
527  _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
528  SB(_me[t].m6, 2, 4, 0);
529  _me[t].m7 = 0;
530 }
531 
532 
533 static inline void MakeRailDepot(TileIndex t, Owner o, DepotID did, DiagDirection d, RailType r)
534 {
536  SetTileOwner(t, o);
537  _m[t].m2 = did;
538  _m[t].m3 = r;
539  _m[t].m4 = 0;
540  _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
541  SB(_me[t].m6, 2, 4, 0);
542  _me[t].m7 = 0;
543 }
544 
545 #endif /* RAIL_MAP_H */
Grass with a fence at the northern side.
Definition: rail_map.h:498
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:427
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:27
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:191
Grass with a fence at the NE and SW edges.
Definition: rail_map.h:494
presignal block exit
Definition: signal_type.h:28
SignalType
Type of signal, i.e.
Definition: signal_type.h:25
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
static SignalState GetSingleSignalState(TileIndex t, byte signalbit)
Get the state of a single signal.
Definition: rail_map.h:374
static void UnreserveTrack(TileIndex tile, Track t)
Lift the reservation of a specific track on a tile.
Definition: rail_map.h:245
presignal block entry
Definition: signal_type.h:27
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:414
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:22
Track
These are used to specify a single track.
Definition: track_type.h:21
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:227
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:495
static bool IsOnewaySignal(TileIndex t, Track track)
One-way signals can&#39;t be passed the &#39;wrong&#39; way.
Definition: rail_map.h:320
Icy or sandy.
Definition: rail_map.h:499
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:493
static Track DiagDirToDiagTrack(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track incidating with that diagdir.
Definition: track_func.h:500
Nothing (dirt)
Definition: rail_map.h:487
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:84
static void SetTrackBits(TileIndex t, TrackBits b)
Sets the track bits of the given tile.
Definition: rail_map.h:148
static uint GetSignalStates(TileIndex tile)
Set the states of the signals (Along/AgainstTrackDir)
Definition: rail_map.h:363
static void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
Sets the state of the signal along the given trackdir.
Definition: rail_map.h:450
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:491
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:26
static uint GetPresentSignals(TileIndex tile)
Get whether the given signals are present (Along/AgainstTrackDir)
Definition: rail_map.h:394
Snow only on higher part of slope (steep or one corner raised)
Definition: rail_map.h:501
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:41
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
Flag for an invalid trackbits value.
Definition: track_type.h:60
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a give tiletype.
Definition: tile_map.h:143
static bool IsSignalPresent(TileIndex t, byte signalbit)
Checks whether the given signals is present.
Definition: rail_map.h:405
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:489
static DiagDirection GetRailDepotDirection(TileIndex t)
Returns the direction the depot is facing to.
Definition: rail_map.h:172
Track in the right corner of the tile (east)
Definition: track_type.h:28
RailType GetTileRailType(TileIndex tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition: rail.cpp:157
static TrackBits GetRailReservationTrackBits(TileIndex t)
Returns the reserved track bits of the tile.
Definition: rail_map.h:195
Track in the lower corner of the tile (south)
Definition: track_type.h:26
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
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:74
static bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
Is a pbs signal present along the trackdir?
Definition: rail_map.h:464
Grass with a fence at the NE edge.
Definition: rail_map.h:492
static void SetSignalStates(TileIndex tile, uint state)
Set the states of the signals (Along/AgainstTrackDir)
Definition: rail_map.h:353
static void SetPresentSignals(TileIndex tile, uint signals)
Set whether the given signals are present (Along/AgainstTrackDir)
Definition: rail_map.h:384
Grass with a fence and shore or water on the free halftile.
Definition: rail_map.h:500
static Track GetRailDepotTrack(TileIndex t)
Returns the track of a depot, ignoring direction.
Definition: rail_map.h:183
DiagDirection
Enumeration for diagonal directions.
static bool IsPlainRailTile(TileIndex t)
Checks whether the tile is a rail tile or rail tile with signals.
Definition: rail_map.h:61
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:73
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:37
static TrackBits GetTrackBits(TileIndex tile)
Gets the track bits of the given tile.
Definition: rail_map.h:137
static TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition: track_func.h:87
Flag for an invalid track.
Definition: track_type.h:30
static bool IsRailDepot(TileIndex t)
Is this rail tile a rail depot?
Definition: rail_map.h:96
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:106
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:283
RailTileType
Different types of Rail-related tiles.
Definition: rail_map.h:24
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:124
static bool HasTrack(TileIndex tile, Track track)
Returns whether the given track is present on the given tile.
Definition: rail_map.h:161
Normal rail tile without signals.
Definition: rail_map.h:25
The signal is red.
Definition: signal_type.h:47
RailGroundType
The ground &#39;under&#39; the rail.
Definition: rail_map.h:486
Grass with a fence at the SE edge.
Definition: rail_map.h:490
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static bool IsPlainRail(TileIndex t)
Returns whether this is plain rails, with or without signals.
Definition: rail_map.h:50
Map writing/reading functions for tiles.
static bool HasDepotReservation(TileIndex t)
Get the reservation state of the depot.
Definition: rail_map.h:259
Owner
Enum for all companies/owners.
Definition: company_type.h:20
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:476
Functions related to signals.
Grass with a fence at the southern side.
Definition: rail_map.h:497
static void SetTrackReservation(TileIndex t, TrackBits b)
Sets the reserved track bits of the tile.
Definition: rail_map.h:210
static SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
Gets the state of the signal along the given trackdir.
Definition: rail_map.h:439
SignalVariant
Variant of the signal, i.e.
Definition: signal_type.h:18
normal pbs signal
Definition: signal_type.h:30
byte m3
General purpose.
Definition: map_type.h:24
No track.
Definition: track_type.h:42
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:116
static bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition: track_func.h:633
Grass with a fence at the western side.
Definition: rail_map.h:496
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:126
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:271