OpenTTD Source 20261112-master-gbcbec5e63f
station_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 STATION_MAP_H
11#define STATION_MAP_H
12
13#include "rail_map.h"
14#include "road_map.h"
15#include "water_map.h"
16#include "rail.h"
17
18typedef uint8_t StationGfx;
19
26inline StationID GetStationIndex(Tile t)
27{
28 assert(IsTileType(t, TileType::Station));
29 return static_cast<StationID>(t.m2());
30}
31
32
33static const int GFX_DOCK_BASE_WATER_PART = 4;
35
43{
44 assert(IsTileType(t, TileType::Station));
45 return static_cast<StationType>(GB(t.m6(), 3, 4));
46}
47
59
67{
68 assert(IsTileType(t, TileType::Station));
69 return t.m5();
70}
71
78inline void SetStationGfx(Tile t, StationGfx gfx)
79{
80 assert(IsTileType(t, TileType::Station));
81 t.m5() = gfx;
82}
83
90inline bool IsRailStation(Tile t)
91{
93}
94
101{
103}
104
111inline bool IsRailWaypoint(Tile t)
112{
114}
115
122{
124}
125
133inline bool HasStationRail(Tile t)
134{
135 return IsRailStation(t) || IsRailWaypoint(t);
136}
137
145{
147}
148
155inline bool IsAirport(Tile t)
156{
158}
159
165inline bool IsAirportTile(Tile t)
166{
167 return IsTileType(t, TileType::Station) && IsAirport(t);
168}
169
170bool IsHangar(Tile t);
171
178inline bool IsTruckStop(Tile t)
179{
181}
182
189inline bool IsBusStop(Tile t)
190{
191 return GetStationType(t) == StationType::Bus;
192}
193
200inline bool IsRoadWaypoint(Tile t)
201{
203}
204
211{
213}
214
222{
223 assert(IsTileType(t, TileType::Station));
224 return IsTruckStop(t) || IsBusStop(t);
225}
226
233{
235}
236
243inline bool IsAnyRoadStop(Tile t)
244{
245 assert(IsTileType(t, TileType::Station));
246 return IsTruckStop(t) || IsBusStop(t) || IsRoadWaypoint(t);
247}
248
255{
257}
258
268
278
280
287{
288 assert(IsRoadWaypointTile(tile));
289 return static_cast<Roadside>(GB(tile.m3(), 2, 2));
290}
291
297static inline void SetRoadWaypointRoadside(Tile tile, Roadside s)
298{
299 assert(IsRoadWaypointTile(tile));
300 SB(tile.m3(), 2, 2, to_underlying(s));
301}
302
309{
310 assert(IsRoadWaypointTile(t));
311 return HasBit(t.m8(), 15);
312}
313
319{
320 assert(IsRoadWaypointTile(t));
321 ToggleBit(t.m8(), 15);
322}
323
331{
332 assert(IsAirport(t));
334}
335
343{
344 assert(IsBayRoadStopTile(t));
345 return static_cast<DiagDirection>(GetStationGfx(t));
346}
347
355{
356 assert(IsDriveThroughStopTile(t));
357 return static_cast<Axis>(GetStationGfx(t) - GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
358}
359
366inline bool IsOilRig(Tile t)
367{
369}
370
377inline bool IsDock(Tile t)
378{
380}
381
387inline bool IsDockTile(Tile t)
388{
390}
391
398inline bool IsBuoy(Tile t)
399{
401}
402
408inline bool IsBuoyTile(Tile t)
409{
410 return IsTileType(t, TileType::Station) && IsBuoy(t);
411}
412
418inline bool IsHangarTile(Tile t)
419{
420 return IsTileType(t, TileType::Station) && IsHangar(t);
421}
422
430{
431 assert(HasStationRail(t));
432 return HasBit(t.m3(), 0);
433}
434
441inline void SetStationTileBlocked(Tile t, bool b)
442{
443 assert(HasStationRail(t));
444 AssignBit(t.m3(), 0, b);
445}
446
454{
455 assert(HasStationRail(t));
456 return HasBit(t.m3(), 1);
457}
458
465inline void SetStationTileHaveWires(Tile t, bool b)
466{
467 assert(HasStationRail(t));
468 AssignBit(t.m3(), 1, b);
469}
470
478{
479 assert(HasStationRail(t));
480 return HasBit(t.m3(), 2);
481}
482
489inline void SetStationTileHavePylons(Tile t, bool b)
490{
491 assert(HasStationRail(t));
492 AssignBit(t.m3(), 2, b);
493}
494
502{
503 assert(HasStationRail(t));
504 return HasBit(GetStationGfx(t), 0) ? Axis::Y : Axis::X;
505}
506
514{
516}
517
531inline bool IsCompatibleTrainStationTile(Tile test_tile, Tile station_tile)
532{
533 assert(IsRailStationTile(station_tile));
534 return IsRailStationTile(test_tile) && !IsStationTileBlocked(test_tile) &&
535 IsCompatibleRail(GetRailType(test_tile), GetRailType(station_tile)) &&
536 GetRailStationAxis(test_tile) == GetRailStationAxis(station_tile) &&
537 GetStationIndex(test_tile) == GetStationIndex(station_tile);
538}
539
547{
548 assert(HasStationRail(t));
549 return HasBit(t.m6(), 2);
550}
551
558inline void SetRailStationReservation(Tile t, bool b)
559{
560 assert(HasStationRail(t));
561 AssignBit(t.m6(), 2, b);
562}
563
574
583{
584 StationGfx gfx = GetStationGfx(t);
585 assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
586 return static_cast<DiagDirection>(gfx);
587}
588
595inline bool IsDockWaterPart(Tile t)
596{
597 assert(IsDockTile(t));
598 StationGfx gfx = GetStationGfx(t);
599 return gfx >= GFX_DOCK_BASE_WATER_PART;
600}
601
609{
610 assert(HasStationTileRail(t));
611 return t.m4() != 0;
612}
613
620inline void SetCustomStationSpecIndex(Tile t, uint8_t specindex)
621{
622 assert(HasStationTileRail(t));
623 t.m4() = specindex;
624}
625
633{
634 assert(HasStationTileRail(t));
635 return t.m4();
636}
637
645{
646 assert(IsAnyRoadStopTile(t));
647 return GB(t.m8(), 0, 6) != 0;
648}
649
656inline void SetCustomRoadStopSpecIndex(Tile t, uint8_t specindex)
657{
658 assert(IsAnyRoadStopTile(t));
659 SB(t.m8(), 0, 6, specindex);
660}
661
669{
670 assert(IsAnyRoadStopTile(t));
671 return GB(t.m8(), 0, 6);
672}
673
680inline void SetStationTileRandomBits(Tile t, uint8_t random_bits)
681{
682 assert(IsTileType(t, TileType::Station));
683 SB(t.m3(), 4, 4, random_bits);
684}
685
693{
694 assert(IsTileType(t, TileType::Station));
695 return GB(t.m3(), 4, 4);
696}
697
707inline void MakeStation(Tile t, Owner o, StationID sid, StationType st, uint8_t section, WaterClass wc = WaterClass::Invalid)
708{
710 SetTileOwner(t, o);
711 SetWaterClass(t, wc);
712 SetDockingTile(t, false);
713 t.m2() = sid.base();
714 t.m3() = 0;
715 t.m4() = 0;
716 t.m5() = section;
717 SB(t.m6(), 2, 1, 0);
718 SB(t.m6(), 3, 4, to_underlying(st));
719 SB(t.m6(), 7, 1, 0);
720 t.m7() = 0;
721 t.m8() = 0;
722}
723
733inline void MakeRailStation(Tile t, Owner o, StationID sid, Axis a, uint8_t section, RailType rt)
734{
735 MakeStation(t, o, sid, StationType::Rail, section + to_underlying(a));
736 SetRailType(t, rt);
738}
739
749inline void MakeRailWaypoint(Tile t, Owner o, StationID sid, Axis a, uint8_t section, RailType rt)
750{
751 MakeStation(t, o, sid, StationType::RailWaypoint, section + to_underlying(a));
752 SetRailType(t, rt);
754}
755
766inline void MakeRoadStop(Tile t, Owner o, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, DiagDirection d)
767{
769 SetRoadTypes(t, road_rt, tram_rt);
772}
773
786inline void MakeDriveThroughRoadStop(Tile t, Owner station, Owner road, Owner tram, StationID sid, StationType rst, RoadType road_rt, RoadType tram_rt, Axis a)
787{
789 SetRoadTypes(t, road_rt, tram_rt);
792}
793
802inline void MakeAirport(Tile t, Owner o, StationID sid, uint8_t section, WaterClass wc)
803{
804 MakeStation(t, o, sid, StationType::Airport, section, wc);
805}
806
813inline void MakeBuoy(Tile t, StationID sid, WaterClass wc)
814{
815 /* Make the owner of the buoy tile the same as the current owner of the
816 * water tile. In this way, we can reset the owner of the water to its
817 * original state when the buoy gets removed. */
818 MakeStation(t, GetTileOwner(t), sid, StationType::Buoy, 0, wc);
819}
820
829inline void MakeDock(Tile t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
830{
833}
834
841inline void MakeOilrig(Tile t, StationID sid, WaterClass wc)
842{
844}
845
846#endif /* STATION_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 T ToggleBit(T &x, const uint8_t y)
Toggles a bit in a variable.
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
static constexpr Owner OWNER_NONE
The tile has no ownership.
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Axis
Enumeration for the two axis X and Y.
@ X
The X axis.
@ Y
The y axis.
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
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:574
Rail specific functions.
bool IsCompatibleRail(RailType enginetype, RailType tiletype)
Checks if an engine of the given RailType can drive on a tile with a given RailType.
Definition rail.h:371
Hides the direct accesses to the map array with map accessors.
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
void SetRailType(Tile t, RailType r)
Sets the rail type of the given tile.
Definition rail_map.h:125
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:26
Map accessors for roads.
void SetRoadOwner(Tile t, RoadTramType rtt, Owner o)
Set the owner of a specific road type.
Definition road_map.h:261
void SetRoadTypes(Tile t, RoadType road_rt, RoadType tram_rt)
Set the present road types of a tile.
Definition road_map.h:630
Roadside
The possible road side decorations.
Definition road_map.h:483
RoadType
The different roadtypes we support.
Definition road_type.h:24
@ Tram
Tram type.
Definition road_type.h:40
@ Road
Road type.
Definition road_type.h:39
void MakeAirport(Tile t, Owner o, StationID sid, uint8_t section, WaterClass wc)
Make the given tile an airport tile.
StationType GetStationType(Tile t)
Get the station type of this tile.
Definition station_map.h:42
bool IsStationRoadStopTile(Tile t)
Is tile t a road stop station?
StationGfx GetStationGfx(Tile t)
Get the station graphics of this tile.
Definition station_map.h:66
void SetStationGfx(Tile t, StationGfx gfx)
Set the station graphics of this tile.
Definition station_map.h:78
void MakeRoadStop(Tile t, Owner o, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, DiagDirection d)
Make the given tile a roadstop tile.
void SetCustomStationSpecIndex(Tile t, uint8_t specindex)
Set the custom station spec for this tile.
void SetStationTileHaveWires(Tile t, bool b)
Set the catenary wires state of the rail station.
bool IsAirportTile(Tile t)
Is this tile a station tile and an airport tile?
bool IsAirport(Tile t)
Is this station tile an airport?
bool IsBayRoadStopTile(Tile t)
Is tile t a bay (non-drive through) road stop station?
bool IsRailWaypointTile(Tile t)
Is this tile a station tile and a rail waypoint?
StationGfx GetTranslatedAirportTileID(StationGfx gfx)
Do airporttile gfx ID translation for NewGRFs.
uint GetCustomRoadStopSpecIndex(Tile t)
Get the custom road stop spec for this tile.
static const int GFX_DOCK_BASE_WATER_PART
The offset for the water parts.
Definition station_map.h:33
bool IsHangar(Tile t)
Check whether the given tile is a hangar.
bool IsBuoy(Tile t)
Is tile t a buoy tile?
bool IsCompatibleTrainStationTile(Tile test_tile, Tile station_tile)
Check if a tile is a valid continuation to a railstation tile.
bool IsRoadWaypoint(Tile t)
Is the station at t a road waypoint?
void MakeDriveThroughRoadStop(Tile t, Owner station, Owner road, Owner tram, StationID sid, StationType rst, RoadType road_rt, RoadType tram_rt, Axis a)
Make the given tile a drivethrough roadstop tile.
bool IsDriveThroughStopTile(Tile t)
Is tile t a drive through road stop station or waypoint?
uint8_t GetStationTileRandomBits(Tile t)
Get the random bits of a station tile.
static Roadside GetRoadWaypointRoadside(Tile tile)
Get the decorations of a road waypoint.
bool IsRailStationTile(Tile t)
Is this tile a station tile and a rail station?
Track GetRailStationTrack(Tile t)
Get the rail track of a rail station tile.
static void ToggleRoadWaypointOnSnowOrDesert(Tile t)
Toggle the snow/desert state of a road waypoint tile.
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:26
bool HasStationTileRail(Tile t)
Has this station tile a rail?
StationGfx GetAirportGfx(Tile t)
Get the station graphics of this airport tile.
bool CanStationTileHaveWires(Tile t)
Can tile t have catenary wires?
uint GetCustomStationSpecIndex(Tile t)
Get the custom station spec for this tile.
bool IsRailWaypoint(Tile t)
Is this station tile a rail waypoint?
bool IsRailStation(Tile t)
Is this station tile a rail station?
Definition station_map.h:90
bool IsHangarTile(Tile t)
Is tile t an hangar tile?
bool IsDockTile(Tile t)
Is tile t a dock tile?
static void SetRoadWaypointRoadside(Tile tile, Roadside s)
Set the decorations of a road waypoint.
void MakeStation(Tile t, Owner o, StationID sid, StationType st, uint8_t section, WaterClass wc=WaterClass::Invalid)
Make the given tile a station tile.
void SetStationTileRandomBits(Tile t, uint8_t random_bits)
Set the random bits for a station tile.
void SetRailStationReservation(Tile t, bool b)
Set the reservation state of the rail station.
bool IsAnyRoadStop(Tile t)
Is the station at t a road station?
void MakeOilrig(Tile t, StationID sid, WaterClass wc)
Make the given tile an oilrig tile.
DiagDirection GetDockDirection(Tile t)
Get the direction of a dock.
Axis GetRailStationAxis(Tile t)
Get the rail direction of a rail station.
static bool IsRoadWaypointOnSnowOrDesert(Tile t)
Check if a road waypoint tile has snow/desert.
void MakeRailWaypoint(Tile t, Owner o, StationID sid, Axis a, uint8_t section, RailType rt)
Make the given tile a rail waypoint tile.
bool IsRoadWaypointTile(Tile t)
Is this tile a station tile and a road waypoint?
bool IsStationTileBlocked(Tile t)
Is tile t a blocked tile?
bool IsTruckStop(Tile t)
Is the station at t a truck stop?
uint8_t StationGfx
Index of station graphics.
Definition station_map.h:18
bool IsStationRoadStop(Tile t)
Is the station at t a road station?
bool IsBusStop(Tile t)
Is the station at t a bus stop?
bool IsCustomStationSpecIndex(Tile t)
Is there a custom rail station spec on this tile?
bool HasStationRail(Tile t)
Has this station tile a rail?
static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
The offset for the drive through parts.
Definition station_map.h:34
Axis GetDriveThroughStopAxis(Tile t)
Gets the axis of the drive through stop.
void SetStationTileHavePylons(Tile t, bool b)
Set the catenary pylon state of the rail station.
bool CanStationTileHavePylons(Tile t)
Can tile t have catenary pylons?
bool IsOilRig(Tile t)
Is tile t part of an oilrig?
bool IsBuoyTile(Tile t)
Is tile t a buoy tile?
bool IsDockWaterPart(Tile t)
Check whether a dock tile is the tile on water.
void MakeBuoy(Tile t, StationID sid, WaterClass wc)
Make the given tile a buoy tile.
void MakeRailStation(Tile t, Owner o, StationID sid, Axis a, uint8_t section, RailType rt)
Make the given tile a rail station tile.
bool HasStationReservation(Tile t)
Get the reservation state of the rail station.
bool IsCustomRoadStopSpecIndex(Tile t)
Is there a custom road stop spec on this tile?
void MakeDock(Tile t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
Make the given tile a dock tile.
DiagDirection GetBayRoadStopDir(Tile t)
Gets the direction the bay road stop entrance points towards.
bool IsDock(Tile t)
Is tile t a dock tile?
bool IsAnyRoadStopTile(Tile t)
Is tile t a road stop station?
void SetStationTileBlocked(Tile t, bool b)
Set the blocked state of the rail station.
TrackBits GetStationReservationTrackBits(Tile t)
Get the reserved track bits for a waypoint.
RoadStopType GetRoadStopType(Tile t)
Get the road stop type of this tile.
Definition station_map.h:54
void SetCustomRoadStopSpecIndex(Tile t, uint8_t specindex)
Set the custom road stop spec for this tile.
RoadStopType
Types of RoadStops.
@ Bus
A standard stop for buses.
@ Truck
A standard stop for trucks.
StationType
Station types.
@ Dock
Ship port.
@ Rail
Railways/train station.
@ Bus
Road stop for busses.
@ Truck
Road stop for trucks.
@ RailWaypoint
Waypoint for trains.
@ Buoy
Waypoint for ships.
@ RoadWaypoint
Waypoint for trucks and busses.
@ Oilrig
Heliport on an oil rig.
@ Airport
Airports and heliports, excluding the ones on oil rigs.
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
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition tile_map.h:198
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
@ Station
A tile of a station or airport.
Definition tile_type.h:54
Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track Axis::X -> Track::X Axis::Y -> Track::Y Uses the fact that...
Definition track_func.h:62
EnumBitSet< Track, uint8_t > TrackBits
Bitset of Track elements.
Definition track_type.h:43
Track
These are used to specify a single track.
Definition track_type.h:19
Map accessors for water tiles.
void SetWaterClass(Tile t, WaterClass wc)
Set the water class at a tile.
Definition water_map.h:128
WaterClass
classes of water (for WaterTileType::Clear water tile type).
Definition water_map.h:41
@ Invalid
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition water_map.h:45
void SetDockingTile(Tile t, bool b)
Set the docking tile state of a tile.
Definition water_map.h:367