OpenTTD Source 20241224-master-gee860a5c8e
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 <http://www.gnu.org/licenses/>.
6 */
7
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 "station_func.h"
17#include "rail.h"
18#include "road.h"
19
20typedef uint8_t StationGfx;
21
28inline StationID GetStationIndex(Tile t)
29{
30 assert(IsTileType(t, MP_STATION));
31 return (StationID)t.m2();
32}
33
34
35static const int GFX_DOCK_BASE_WATER_PART = 4;
37
45{
46 assert(IsTileType(t, MP_STATION));
47 return (StationType)GB(t.m6(), 3, 4);
48}
49
57{
58 assert(GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS);
59 return GetStationType(t) == STATION_TRUCK ? ROADSTOP_TRUCK : ROADSTOP_BUS;
60}
61
69{
70 assert(IsTileType(t, MP_STATION));
71 return t.m5();
72}
73
80inline void SetStationGfx(Tile t, StationGfx gfx)
81{
82 assert(IsTileType(t, MP_STATION));
83 t.m5() = gfx;
84}
85
92inline bool IsRailStation(Tile t)
93{
94 return GetStationType(t) == STATION_RAIL;
95}
96
103{
104 return IsTileType(t, MP_STATION) && IsRailStation(t);
105}
106
113inline bool IsRailWaypoint(Tile t)
114{
115 return GetStationType(t) == STATION_WAYPOINT;
116}
117
124{
125 return IsTileType(t, MP_STATION) && IsRailWaypoint(t);
126}
127
135inline bool HasStationRail(Tile t)
136{
137 return IsRailStation(t) || IsRailWaypoint(t);
138}
139
147{
148 return IsTileType(t, MP_STATION) && HasStationRail(t);
149}
150
157inline bool IsAirport(Tile t)
158{
159 return GetStationType(t) == STATION_AIRPORT;
160}
161
167inline bool IsAirportTile(Tile t)
168{
169 return IsTileType(t, MP_STATION) && IsAirport(t);
170}
171
172bool IsHangar(Tile t);
173
180inline bool IsTruckStop(Tile t)
181{
182 return GetStationType(t) == STATION_TRUCK;
183}
184
191inline bool IsBusStop(Tile t)
192{
193 return GetStationType(t) == STATION_BUS;
194}
195
202inline bool IsRoadWaypoint(Tile t)
203{
204 return GetStationType(t) == STATION_ROADWAYPOINT;
205}
206
213{
214 return IsTileType(t, MP_STATION) && IsRoadWaypoint(t);
215}
216
224{
225 assert(IsTileType(t, MP_STATION));
226 return IsTruckStop(t) || IsBusStop(t);
227}
228
235{
236 return IsTileType(t, MP_STATION) && IsStationRoadStop(t);
237}
238
245inline bool IsAnyRoadStop(Tile t)
246{
247 assert(IsTileType(t, MP_STATION));
248 return IsTruckStop(t) || IsBusStop(t) || IsRoadWaypoint(t);
249}
250
257{
258 return IsTileType(t, MP_STATION) && IsAnyRoadStop(t);
259}
260
270
280
282
289{
290 assert(IsRoadWaypointTile(tile));
291 return (Roadside)GB(tile.m3(), 2, 2);
292}
293
299static inline void SetRoadWaypointRoadside(Tile tile, Roadside s)
300{
301 assert(IsRoadWaypointTile(tile));
302 SB(tile.m3(), 2, 2, s);
303}
304
311{
312 assert(IsRoadWaypointTile(t));
313 return HasBit(t.m8(), 15);
314}
315
321{
322 assert(IsRoadWaypointTile(t));
323 ToggleBit(t.m8(), 15);
324}
325
333{
334 assert(IsAirport(t));
336}
337
345{
346 assert(IsBayRoadStopTile(t));
347 return static_cast<DiagDirection>(GetStationGfx(t));
348}
349
357{
358 assert(IsDriveThroughStopTile(t));
359 return static_cast<Axis>(GetStationGfx(t) - GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
360}
361
368inline bool IsOilRig(Tile t)
369{
370 return GetStationType(t) == STATION_OILRIG;
371}
372
379inline bool IsDock(Tile t)
380{
381 return GetStationType(t) == STATION_DOCK;
382}
383
389inline bool IsDockTile(Tile t)
390{
391 return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
392}
393
400inline bool IsBuoy(Tile t)
401{
402 return GetStationType(t) == STATION_BUOY;
403}
404
410inline bool IsBuoyTile(Tile t)
411{
412 return IsTileType(t, MP_STATION) && IsBuoy(t);
413}
414
420inline bool IsHangarTile(Tile t)
421{
422 return IsTileType(t, MP_STATION) && IsHangar(t);
423}
424
432{
433 assert(HasStationRail(t));
434 return HasBit(t.m3(), 0);
435}
436
443inline void SetStationTileBlocked(Tile t, bool b)
444{
445 assert(HasStationRail(t));
446 AssignBit(t.m3(), 0, b);
447}
448
456{
457 assert(HasStationRail(t));
458 return HasBit(t.m3(), 1);
459}
460
467inline void SetStationTileHaveWires(Tile t, bool b)
468{
469 assert(HasStationRail(t));
470 AssignBit(t.m3(), 1, b);
471}
472
480{
481 assert(HasStationRail(t));
482 return HasBit(t.m3(), 2);
483}
484
491inline void SetStationTileHavePylons(Tile t, bool b)
492{
493 assert(HasStationRail(t));
494 AssignBit(t.m3(), 2, b);
495}
496
504{
505 assert(HasStationRail(t));
506 return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
507}
508
516{
518}
519
530
544inline bool IsCompatibleTrainStationTile(Tile test_tile, Tile station_tile)
545{
546 assert(IsRailStationTile(station_tile));
547 return IsRailStationTile(test_tile) && !IsStationTileBlocked(test_tile) &&
548 IsCompatibleRail(GetRailType(test_tile), GetRailType(station_tile)) &&
549 GetRailStationAxis(test_tile) == GetRailStationAxis(station_tile) &&
550 GetStationIndex(test_tile) == GetStationIndex(station_tile);
551}
552
560{
561 assert(HasStationRail(t));
562 return HasBit(t.m6(), 2);
563}
564
571inline void SetRailStationReservation(Tile t, bool b)
572{
573 assert(HasStationRail(t));
574 AssignBit(t.m6(), 2, b);
575}
576
587
596{
597 StationGfx gfx = GetStationGfx(t);
598 assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
599 return (DiagDirection)(gfx);
600}
601
605inline bool IsDockWaterPart(Tile t)
606{
607 assert(IsDockTile(t));
608 StationGfx gfx = GetStationGfx(t);
609 return gfx >= GFX_DOCK_BASE_WATER_PART;
610}
611
619{
620 assert(HasStationTileRail(t));
621 return t.m4() != 0;
622}
623
630inline void SetCustomStationSpecIndex(Tile t, uint8_t specindex)
631{
632 assert(HasStationTileRail(t));
633 t.m4() = specindex;
634}
635
643{
644 assert(HasStationTileRail(t));
645 return t.m4();
646}
647
655{
656 assert(IsAnyRoadStopTile(t));
657 return GB(t.m8(), 0, 6) != 0;
658}
659
666inline void SetCustomRoadStopSpecIndex(Tile t, uint8_t specindex)
667{
668 assert(IsAnyRoadStopTile(t));
669 SB(t.m8(), 0, 6, specindex);
670}
671
679{
680 assert(IsAnyRoadStopTile(t));
681 return GB(t.m8(), 0, 6);
682}
683
690inline void SetStationTileRandomBits(Tile t, uint8_t random_bits)
691{
692 assert(IsTileType(t, MP_STATION));
693 SB(t.m3(), 4, 4, random_bits);
694}
695
703{
704 assert(IsTileType(t, MP_STATION));
705 return GB(t.m3(), 4, 4);
706}
707
717inline void MakeStation(Tile t, Owner o, StationID sid, StationType st, uint8_t section, WaterClass wc = WATER_CLASS_INVALID)
718{
720 SetTileOwner(t, o);
721 SetWaterClass(t, wc);
722 SetDockingTile(t, false);
723 t.m2() = sid;
724 t.m3() = 0;
725 t.m4() = 0;
726 t.m5() = section;
727 SB(t.m6(), 2, 1, 0);
728 SB(t.m6(), 3, 4, st);
729 t.m7() = 0;
730 t.m8() = 0;
731}
732
742inline void MakeRailStation(Tile t, Owner o, StationID sid, Axis a, uint8_t section, RailType rt)
743{
744 MakeStation(t, o, sid, STATION_RAIL, section + a);
745 SetRailType(t, rt);
747}
748
758inline void MakeRailWaypoint(Tile t, Owner o, StationID sid, Axis a, uint8_t section, RailType rt)
759{
760 MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
761 SetRailType(t, rt);
763}
764
775inline void MakeRoadStop(Tile t, Owner o, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, DiagDirection d)
776{
777 MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
778 SetRoadTypes(t, road_rt, tram_rt);
779 SetRoadOwner(t, RTT_ROAD, o);
780 SetRoadOwner(t, RTT_TRAM, o);
781}
782
795inline void MakeDriveThroughRoadStop(Tile t, Owner station, Owner road, Owner tram, StationID sid, StationType rst, RoadType road_rt, RoadType tram_rt, Axis a)
796{
797 MakeStation(t, station, sid, rst, GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
798 SetRoadTypes(t, road_rt, tram_rt);
799 SetRoadOwner(t, RTT_ROAD, road);
800 SetRoadOwner(t, RTT_TRAM, tram);
801}
802
811inline void MakeAirport(Tile t, Owner o, StationID sid, uint8_t section, WaterClass wc)
812{
813 MakeStation(t, o, sid, STATION_AIRPORT, section, wc);
814}
815
822inline void MakeBuoy(Tile t, StationID sid, WaterClass wc)
823{
824 /* Make the owner of the buoy tile the same as the current owner of the
825 * water tile. In this way, we can reset the owner of the water to its
826 * original state when the buoy gets removed. */
827 MakeStation(t, GetTileOwner(t), sid, STATION_BUOY, 0, wc);
828}
829
838inline void MakeDock(Tile t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
839{
840 MakeStation(t, o, sid, STATION_DOCK, d);
841 MakeStation(TileIndex(t) + TileOffsByDiagDir(d), o, sid, STATION_DOCK, GFX_DOCK_BASE_WATER_PART + DiagDirToAxis(d), wc);
842}
843
850inline void MakeOilrig(Tile t, StationID sid, WaterClass wc)
851{
852 MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0, wc);
853}
854
855#endif /* STATION_MAP_H */
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
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.
debug_inline 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 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
debug_inline uint16_t & m8()
General purpose.
Definition map_func.h:197
debug_inline uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:125
debug_inline uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:185
debug_inline uint8_t & m4()
General purpose.
Definition map_func.h:149
debug_inline uint8_t & m6()
General purpose.
Definition map_func.h:173
debug_inline uint8_t & m3()
General purpose.
Definition map_func.h:137
debug_inline uint8_t & m5()
General purpose.
Definition map_func.h:161
Owner
Enum for all companies/owners.
@ OWNER_NONE
The tile has no ownership.
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Axis
Allow incrementing of DiagDirDiff variables.
@ AXIS_X
The X axis.
@ AXIS_Y
The y axis.
DiagDirection
Enumeration for diagonal directions.
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:567
uint8_t StationGfx
Copy from station_map.h.
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:322
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:27
Road specific functions.
Map accessors for roads.
void SetRoadOwner(Tile t, RoadTramType rtt, Owner o)
Set the owner of a specific road type.
Definition road_map.h:251
void SetRoadTypes(Tile t, RoadType road_rt, RoadType tram_rt)
Set the present road types of a tile.
Definition road_map.h:619
Roadside
The possible road side decorations.
Definition road_map.h:477
RoadType
The different roadtypes we support.
Definition road_type.h:25
Functions related to stations.
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:44
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:68
void SetStationGfx(Tile t, StationGfx gfx)
Set the station graphics of this tile.
Definition station_map.h:80
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.
TrackBits GetRailStationTrackBits(Tile t)
Get the trackbits of a rail station 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.
void MakeStation(Tile t, Owner o, StationID sid, StationType st, uint8_t section, WaterClass wc=WATER_CLASS_INVALID)
Make the given tile a station tile.
static const int GFX_DOCK_BASE_WATER_PART
The offset for the water parts.
Definition station_map.h:35
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:28
bool HasStationTileRail(Tile t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint?
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:92
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 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:20
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? In other words, is this station tile a rail station or rail waypoint?
static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
The offset for the drive through parts.
Definition station_map.h:36
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:56
void SetCustomRoadStopSpecIndex(Tile t, uint8_t specindex)
Set the custom road stop spec for this tile.
RoadStopType
Types of RoadStops.
@ ROADSTOP_BUS
A standard stop for buses.
@ ROADSTOP_TRUCK
A standard stop for trucks.
StationType
Station types.
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
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
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:87
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
TrackBits AxisToTrackBits(Axis a)
Maps an Axis to the corresponding TrackBits value.
Definition track_func.h:88
Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track AXIS_X -> TRACK_X AXIS_Y -> TRACK_Y Uses the fact that the...
Definition track_func.h:66
TrackBits
Allow incrementing of Track variables.
Definition track_type.h:35
@ TRACK_BIT_NONE
No track.
Definition track_type.h:36
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:124
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition water_map.h:39
@ WATER_CLASS_INVALID
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition water_map.h:43
void SetDockingTile(Tile t, bool b)
Set the docking tile state of a tile.
Definition water_map.h:361