OpenTTD Source 20260621-master-g720d10536d
rail_map.h
Go to the documentation of this file.
1/*
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef RAIL_MAP_H
11#define RAIL_MAP_H
12
13#include "rail_type.h"
14#include "depot_type.h"
15#include "signal_func.h"
16#include "track_func.h"
17#include "tile_map.h"
18#include "water_map.h"
19#include "signal_type.h"
20
21
23enum class RailTileType : uint8_t {
24 Normal = 0,
25 Signals = 1,
26 Depot = 3,
27};
28
37{
38 assert(IsTileType(t, TileType::Railway));
39 return static_cast<RailTileType>(GB(t.m5(), 6, 2));
40}
41
49[[debug_inline]] inline static bool IsPlainRail(Tile t)
50{
52 return rtt == RailTileType::Normal || rtt == RailTileType::Signals;
53}
54
60[[debug_inline]] inline static bool IsPlainRailTile(Tile t)
61{
63}
64
65
72inline bool HasSignals(Tile t)
73{
75}
76
83inline void SetHasSignals(Tile tile, bool signals)
84{
85 assert(IsPlainRailTile(tile));
87}
88
95[[debug_inline]] inline static bool IsRailDepot(Tile t)
96{
98}
99
105[[debug_inline]] inline static bool IsRailDepotTile(Tile t)
106{
108}
109
116{
117 return (RailType)GB(t.m8(), 0, 6);
118}
119
125inline void SetRailType(Tile t, RailType r)
126{
127 SB(t.m8(), 0, 6, r);
128}
129
130
137{
138 assert(IsPlainRailTile(tile));
139 return static_cast<TrackBits>(GB(tile.m5(), 0, 6));
140}
141
147inline void SetTrackBits(Tile t, TrackBits b)
148{
149 assert(IsPlainRailTile(t));
150 SB(t.m5(), 0, 6, b.base());
151}
152
160inline bool HasTrack(Tile tile, Track track)
161{
162 return GetTrackBits(tile).Test(track);
163}
164
172{
173 return (DiagDirection)GB(t.m5(), 0, 2);
174}
175
186
187
195{
196 assert(IsPlainRailTile(t));
197 uint8_t track_b = GB(t.m2(), 8, 3);
198 if (track_b == 0) return {};
199 Track track = static_cast<Track>(track_b - 1); // map array saves Track+1
200 return (HasBit(t.m2(), 11) ? TrackToOppositeTrack(track) : TrackBits{}) | track;
201}
202
210{
211 assert(IsPlainRailTile(t));
212 assert(!TracksOverlap(b));
213 Track track = RemoveFirstTrack(b);
214 SB(t.m2(), 8, 3, IsValidTrack(track) ? to_underlying(track) + 1 : 0);
215 AssignBit(t.m2(), 11, b.Any());
216}
217
225inline bool TryReserveTrack(Tile tile, Track t)
226{
227 assert(HasTrack(tile, t));
229 if (res.Test(t)) return false; // already reserved
230 res.Set(t);
231 if (TracksOverlap(res)) return false; // crossing reservation present
232 SetTrackReservation(tile, res);
233 return true;
234}
235
242inline void UnreserveTrack(Tile tile, Track t)
243{
244 assert(HasTrack(tile, t));
246 res.Reset(t);
247 SetTrackReservation(tile, res);
248}
249
257{
258 assert(IsRailDepot(t));
259 return HasBit(t.m5(), 4);
260}
261
268inline void SetDepotReservation(Tile t, bool b)
269{
270 assert(IsRailDepot(t));
271 AssignBit(t.m5(), 4, b);
272}
273
284
285
292{
293 return s == SignalType::Path || s == SignalType::PathOneWay;
294}
295
304{
306 uint8_t pos = (track == Track::Lower || track == Track::Right) ? 4 : 0;
307 return static_cast<SignalType>(GB(t.m2(), pos, 3));
308}
309
317inline void SetSignalType(Tile t, Track track, SignalType s)
318{
320 uint8_t pos = (track == Track::Lower || track == Track::Right) ? 4 : 0;
321 SB(t.m2(), pos, 3, to_underlying(s));
322 if (!IsValidTrack(track)) SB(t.m2(), 4, 3, to_underlying(s));
323}
324
332inline bool IsPresignalEntry(Tile t, Track track)
333{
334 return GetSignalType(t, track) == SignalType::Entry || GetSignalType(t, track) == SignalType::Combo;
335}
336
344inline bool IsPresignalExit(Tile t, Track track)
345{
346 return GetSignalType(t, track) == SignalType::Exit || GetSignalType(t, track) == SignalType::Combo;
347}
348
357inline bool IsOnewaySignal(Tile t, Track track)
358{
359 return GetSignalType(t, track) != SignalType::Path;
360}
361
368inline void CycleSignalSide(Tile t, Track track)
369{
370 uint8_t sig;
371 uint8_t pos = (track == Track::Lower || track == Track::Right) ? 4 : 6;
372
373 sig = GB(t.m3(), pos, 2);
374 if (--sig == 0) sig = IsPbsSignal(GetSignalType(t, track)) ? 2 : 3;
375 SB(t.m3(), pos, 2, sig);
376}
377
386{
387 uint8_t pos = (track == Track::Lower || track == Track::Right) ? 7 : 3;
388 return static_cast<SignalVariant>(GB(t.m2(), pos, 1));
389}
390
398inline void SetSignalVariant(Tile t, Track track, SignalVariant v)
399{
400 uint8_t pos = (track == Track::Lower || track == Track::Right) ? 7 : 3;
401 SB(t.m2(), pos, 1, to_underlying(v));
402 if (!IsValidTrack(track)) SB(t.m2(), 7, 1, to_underlying(v));
403}
404
410inline void SetSignalStates(Tile tile, uint state)
411{
412 SB(tile.m4(), 4, 4, state);
413}
414
420inline uint GetSignalStates(Tile tile)
421{
422 return GB(tile.m4(), 4, 4);
423}
424
431inline SignalState GetSingleSignalState(Tile t, uint8_t signalbit)
432{
433 return static_cast<SignalState>(HasBit(GetSignalStates(t), signalbit));
434}
435
441inline void SetPresentSignals(Tile tile, uint signals)
442{
443 SB(tile.m3(), 4, 4, signals);
444}
445
451inline uint GetPresentSignals(Tile tile)
452{
453 return GB(tile.m3(), 4, 4);
454}
455
462inline bool IsSignalPresent(Tile t, uint8_t signalbit)
463{
464 return HasBit(GetPresentSignals(t), signalbit);
465}
466
474inline bool HasSignalOnTrack(Tile tile, Track track)
475{
476 assert(IsValidTrack(track));
477 return GetRailTileType(tile) == RailTileType::Signals && (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
478}
479
490inline bool HasSignalOnTrackdir(Tile tile, Trackdir trackdir)
491{
492 assert (IsValidTrackdir(trackdir));
494}
495
506{
507 assert(IsValidTrackdir(trackdir));
508 assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
509 return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
511}
512
519inline void SetSignalStateByTrackdir(Tile tile, Trackdir trackdir, SignalState state)
520{
521 if (state == SignalState::Green) { // set 1
522 SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
523 } else {
524 SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
525 }
526}
527
535{
536 return IsTileType(tile, TileType::Railway) && HasSignalOnTrackdir(tile, td) &&
538}
539
548{
550 !HasSignalOnTrackdir(tile, td) && IsOnewaySignal(tile, TrackdirToTrack(td));
551}
552
560{
561 return IsTileType(tile, TileType::Railway) && HasSignalOnTrackdir(tile, td) &&
563}
564
566
585
592{
593 SB(t.m4(), 0, 4, to_underlying(rgt));
594}
595
602{
603 return static_cast<RailGroundType>(GB(t.m4(), 0, 4));
604}
605
615
616
624inline void MakeRailNormal(Tile t, Owner o, TrackBits b, RailType r)
625{
627 SetTileOwner(t, o);
628 SetDockingTile(t, false);
629 t.m2() = 0;
630 t.m3() = 0;
631 t.m4() = 0;
632 t.m5() = to_underlying(RailTileType::Normal) << 6 | b.base();
633 SB(t.m6(), 2, 6, 0);
634 t.m7() = 0;
635 t.m8() = r;
636}
637
644{
645 assert(IsRailDepotTile(tile));
646 SB(tile.m5(), 0, 2, to_underlying(dir));
647}
648
657inline void MakeRailDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RailType rail_type)
658{
660 SetTileOwner(tile, owner);
661 SetDockingTile(tile, false);
662 tile.m2() = depot_id.base();
663 tile.m3() = 0;
664 tile.m4() = 0;
666 SB(tile.m6(), 2, 6, 0);
667 tile.m7() = 0;
668 tile.m8() = rail_type;
669}
670
671#endif /* RAIL_MAP_H */
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
uint8_t & m5()
General purpose.
Definition map_func.h:156
uint8_t & m4()
General purpose.
Definition map_func.h:145
uint8_t & m6()
General purpose.
Definition map_func.h:167
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:178
uint8_t & m3()
General purpose.
Definition map_func.h:134
uint16_t & m8()
General purpose.
Definition map_func.h:189
uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:123
Header files for depots (not hangars).
PoolID< uint16_t, struct DepotIDTag, 64000, 0xFFFF > DepotID
Type for the unique identifier of depots.
Definition depot_type.h:15
DiagDirection
Enumeration for diagonal directions.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
bool HasOnewaySignalBlockingTrackdir(Tile tile, Trackdir td)
Is a one-way signal blocking the trackdir?
Definition rail_map.h:547
static RailTileType GetRailTileType(Tile t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition rail_map.h:36
static bool IsPlainRail(Tile t)
Returns whether this is plain rails, with or without signals.
Definition rail_map.h:49
uint GetSignalStates(Tile tile)
Set the states of the signals (Along/AgainstTrackDir).
Definition rail_map.h:420
void MakeRailDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RailType rail_type)
Make a rail depot.
Definition rail_map.h:657
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
bool HasSignalOnTrackdir(Tile tile, Trackdir trackdir)
Checks for the presence of signals along the given trackdir on the given rail tile.
Definition rail_map.h:490
static bool IsRailDepot(Tile t)
Is this rail tile a rail depot?
Definition rail_map.h:95
SignalVariant GetSignalVariant(Tile t, Track track)
Get the signal variant for a track on a tile.
Definition rail_map.h:385
RailGroundType GetRailGroundType(Tile t)
Get the ground type for rail tiles.
Definition rail_map.h:601
TrackBits GetTrackBits(Tile tile)
Gets the track bits of the given tile.
Definition rail_map.h:136
static bool IsPlainRailTile(Tile t)
Checks whether the tile is a rail tile or rail tile with signals.
Definition rail_map.h:60
bool IsPbsSignal(SignalType s)
Checks whether the given signal is a path based signal.
Definition rail_map.h:291
void CycleSignalSide(Tile t, Track track)
Cycle to the next signal side at the given track on a tile.
Definition rail_map.h:368
void MakeRailNormal(Tile t, Owner o, TrackBits b, RailType r)
Make the given tile a normal rail.
Definition rail_map.h:624
RailTileType
Different types of Rail-related tiles.
Definition rail_map.h:23
@ Normal
Normal rail tile without signals.
Definition rail_map.h:24
@ Depot
Depot (one entrance).
Definition rail_map.h:26
@ Signals
Normal rail tile with signals.
Definition rail_map.h:25
Track GetRailDepotTrack(Tile t)
Returns the track of a depot, ignoring direction.
Definition rail_map.h:182
void SetTrackReservation(Tile t, TrackBits b)
Sets the reserved track bits of the tile.
Definition rail_map.h:209
bool IsSignalPresent(Tile t, uint8_t signalbit)
Checks whether the given signals is present.
Definition rail_map.h:462
DiagDirection GetRailDepotDirection(Tile t)
Returns the direction the depot is facing to.
Definition rail_map.h:171
void SetTrackBits(Tile t, TrackBits b)
Sets the track bits of the given tile.
Definition rail_map.h:147
RailGroundType
The ground 'under' the rail.
Definition rail_map.h:568
@ FenceSENW
Grass with a fence at the NW and SE edges.
Definition rail_map.h:573
@ FenceVert2
Grass with a fence at the western side.
Definition rail_map.h:578
@ HalfTileSnow
Snow only on higher part of slope (steep or one corner raised).
Definition rail_map.h:583
@ Barren
Nothing (dirt).
Definition rail_map.h:569
@ FenceNESW
Grass with a fence at the NE and SW edges.
Definition rail_map.h:576
@ FenceHoriz2
Grass with a fence at the northern side.
Definition rail_map.h:580
@ SnowOrDesert
Icy or sandy.
Definition rail_map.h:581
@ HalfTileWater
Grass with a fence and shore or water on the free halftile.
Definition rail_map.h:582
@ FenceVert1
Grass with a fence at the eastern side.
Definition rail_map.h:577
@ FenceNW
Grass with a fence at the NW edge.
Definition rail_map.h:571
@ FenceSE
Grass with a fence at the SE edge.
Definition rail_map.h:572
@ FenceNE
Grass with a fence at the NE edge.
Definition rail_map.h:574
@ Grass
Grassy.
Definition rail_map.h:570
@ FenceSW
Grass with a fence at the SW edge.
Definition rail_map.h:575
@ FenceHoriz1
Grass with a fence at the southern side.
Definition rail_map.h:579
void SetSignalStateByTrackdir(Tile tile, Trackdir trackdir, SignalState state)
Sets the state of the signal along the given trackdir.
Definition rail_map.h:519
uint GetPresentSignals(Tile tile)
Get whether the given signals are present (Along/AgainstTrackDir).
Definition rail_map.h:451
bool HasSignalOnTrack(Tile tile, Track track)
Checks for the presence of signals (either way) on the given track on the given rail tile.
Definition rail_map.h:474
bool HasPbsSignalOnTrackdir(Tile tile, Trackdir td)
Is a pbs signal present along the trackdir?
Definition rail_map.h:534
bool IsOnewaySignal(Tile t, Track track)
Is the signal at the given track on a tile a one way signal?
Definition rail_map.h:357
void SetPresentSignals(Tile tile, uint signals)
Set whether the given signals are present (Along/AgainstTrackDir).
Definition rail_map.h:441
bool IsPresignalExit(Tile t, Track track)
Is the signal at the given track on a tile a presignal exit signal?
Definition rail_map.h:344
void SetRailGroundType(Tile t, RailGroundType rgt)
Set the ground type for rail tiles.
Definition rail_map.h:591
void SetRailDepotExitDirection(Tile tile, DiagDirection dir)
Sets the exit direction of a rail depot.
Definition rail_map.h:643
SignalType GetSignalType(Tile t, Track track)
Get the signal type for a track on a tile.
Definition rail_map.h:303
void UnreserveTrack(Tile tile, Track t)
Lift the reservation of a specific track on a tile.
Definition rail_map.h:242
RailType GetTileRailType(Tile tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition rail.cpp:39
void SetDepotReservation(Tile t, bool b)
Set the reservation state of the depot.
Definition rail_map.h:268
TrackBits GetDepotReservationTrackBits(Tile t)
Get the reserved track bits for a depot.
Definition rail_map.h:280
TrackBits GetRailReservationTrackBits(Tile t)
Returns the reserved track bits of the tile.
Definition rail_map.h:194
void SetSignalType(Tile t, Track track, SignalType s)
Set the signal type for a track on a tile.
Definition rail_map.h:317
bool HasDepotReservation(Tile t)
Get the reservation state of the depot.
Definition rail_map.h:256
void SetHasSignals(Tile tile, bool signals)
Add/remove the 'has signal' bit from the RailTileType.
Definition rail_map.h:83
bool IsSnowOrDesertRailGround(Tile t)
Is the given rail tile snowy or deserty.
Definition rail_map.h:611
bool TryReserveTrack(Tile tile, Track t)
Try to reserve a specific track on a tile.
Definition rail_map.h:225
bool HasSignals(Tile t)
Checks if a rail tile has signals.
Definition rail_map.h:72
SignalState GetSignalStateByTrackdir(Tile tile, Trackdir trackdir)
Gets the state of the signal along the given trackdir.
Definition rail_map.h:505
void SetSignalVariant(Tile t, Track track, SignalVariant v)
Set the signal variant for a track on a tile.
Definition rail_map.h:398
static bool IsRailDepotTile(Tile t)
Is this tile rail tile and a rail depot?
Definition rail_map.h:105
bool HasBlockSignalOnTrackdir(Tile tile, Trackdir td)
Check whether a block signal is present along the trackdir.
Definition rail_map.h:559
bool IsPresignalEntry(Tile t, Track track)
Is the signal at the given track on a tile a presignal entry signal?
Definition rail_map.h:332
bool HasTrack(Tile tile, Track track)
Returns whether the given track is present on the given tile.
Definition rail_map.h:160
SignalState GetSingleSignalState(Tile t, uint8_t signalbit)
Get the state of a single signal.
Definition rail_map.h:431
void SetSignalStates(Tile tile, uint state)
Set the states of the signals (Along/AgainstTrackDir).
Definition rail_map.h:410
void SetRailType(Tile t, RailType r)
Sets the rail type of the given tile.
Definition rail_map.h:125
The different types of rail.
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
Functions related to signals.
uint8_t 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
uint8_t 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:48
Types and classes related to signals.
SignalType
Type of signal, i.e.
Definition signal_type.h:24
@ Path
normal path signal.
Definition signal_type.h:29
@ Entry
presignal block entry.
Definition signal_type.h:26
@ PathOneWay
no-entry path signal.
Definition signal_type.h:30
@ Combo
presignal inter-block.
Definition signal_type.h:28
@ Exit
presignal block exit.
Definition signal_type.h:27
SignalState
These are states in which a signal can be.
Definition signal_type.h:40
@ Green
The signal is green.
Definition signal_type.h:42
@ Red
The signal is red.
Definition signal_type.h:41
SignalVariant
Variant of the signal, i.e.
Definition signal_type.h:16
#define debug_inline
When making a (pure) debug build, the compiler will by default disable inlining of functions.
Definition stdafx.h:222
Map writing/reading functions for tiles.
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition tile_map.h:131
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition tile_map.h:198
@ Railway
A tile with railway.
Definition tile_type.h:50
Different conversion functions from one kind of track to another.
Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition track_func.h:235
Track TrackToOppositeTrack(Track t)
Find the opposite track to a given track.
Definition track_func.h:204
Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition track_func.h:220
bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition track_func.h:540
Track RemoveFirstTrack(TrackBits &tracks)
Removes first Track from TrackBits and returns it.
Definition track_func.h:106
bool IsValidTrackdir(Trackdir trackdir)
Checks if a Trackdir is valid for non-road vehicles.
Definition track_func.h:48
bool IsValidTrack(Track track)
Checks if a Track is valid.
Definition track_func.h:24
Track DiagDirToDiagTrack(DiagDirection diagdir)
Maps a DiagDirection to the associated diagonal Track.
Definition track_func.h:419
EnumBitSet< Track, uint8_t > TrackBits
Bitset of Track elements.
Definition track_type.h:43
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:63
Track
These are used to specify a single track.
Definition track_type.h:19
@ Right
Track in the right corner of the tile (east).
Definition track_type.h:26
@ Lower
Track in the lower corner of the tile (south).
Definition track_type.h:24
Map accessors for water tiles.
void SetDockingTile(Tile t, bool b)
Set the docking tile state of a tile.
Definition water_map.h:364