OpenTTD Source 20261112-master-gbcbec5e63f
base_station_base.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 BASE_STATION_BASE_H
11#define BASE_STATION_BASE_H
12
13#include "core/pool_type.hpp"
14#include "viewport_type.h"
15#include "station_map.h"
17
18typedef Pool<BaseStation, StationID, 32> StationPool;
19extern StationPool _station_pool;
20
21template <typename T>
23 const T *spec = nullptr;
25 uint16_t localidx = 0;
26};
27
30 uint8_t random_bits = 0;
31 uint8_t animation_frame = 0;
32};
33
35struct BaseStation : StationPool::PoolItem<&_station_pool> {
38 uint8_t delete_ctr = 0;
39
40 std::string name{};
42 mutable std::string cached_name;
43
44 Town *town = nullptr;
47
48 std::vector<SpecMapping<StationSpec>> speclist{};
49 std::vector<SpecMapping<RoadStopSpec>> roadstop_speclist{};
50
52
53 uint16_t random_bits = 0;
54 std::unordered_map<TileIndex, StationRandomTriggers> tile_waiting_random_triggers;
60
63
64 std::vector<RoadStopTileData> custom_roadstop_tile_data{};
65
71 BaseStation(StationID index, TileIndex tile) : StationPool::PoolItem<&_station_pool>(index), xy(tile) {}
72
74 virtual ~BaseStation();
75
81 virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
82
91 virtual uint32_t GetNewGRFVariable(const struct ResolverObject &object, uint8_t variable, uint8_t parameter, bool &available) const = 0;
92
96 virtual void UpdateVirtCoord() = 0;
97
98 inline const std::string &GetCachedName() const
99 {
100 if (!this->name.empty()) return this->name;
101 if (this->cached_name.empty()) this->FillCachedName();
102 return this->cached_name;
103 }
104
109 virtual void MoveSign(TileIndex new_xy)
110 {
111 this->xy = new_xy;
112 this->UpdateVirtCoord();
113 }
114
120 virtual TileArea GetTileArea(StationType type) const = 0;
121
128 virtual uint GetPlatformLength(TileIndex tile) const = 0;
129
137 virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
138
144 static inline BaseStation *GetByTile(TileIndex tile)
145 {
146 return BaseStation::Get(GetStationIndex(tile));
147 }
148
159
160 inline uint8_t GetRoadStopRandomBits(TileIndex tile) const
161 {
162 for (const RoadStopTileData &tile_data : this->custom_roadstop_tile_data) {
163 if (tile_data.tile == tile) return tile_data.random_bits;
164 }
165 return 0;
166 }
167
168 inline uint8_t GetRoadStopAnimationFrame(TileIndex tile) const
169 {
170 for (const RoadStopTileData &tile_data : this->custom_roadstop_tile_data) {
171 if (tile_data.tile == tile) return tile_data.animation_frame;
172 }
173 return 0;
174 }
175
176private:
177 bool SetRoadStopTileData(TileIndex tile, uint8_t data, bool animation);
178
179public:
180 inline void SetRoadStopRandomBits(TileIndex tile, uint8_t random_bits) { this->SetRoadStopTileData(tile, random_bits, false); }
181 inline bool SetRoadStopAnimationFrame(TileIndex tile, uint8_t frame) { return this->SetRoadStopTileData(tile, frame, true); }
182 void RemoveRoadStopTileData(TileIndex tile);
183
184 static void PostDestructor(size_t index);
185
186private:
187 void FillCachedName() const;
188};
189
194template <class T, bool Tis_waypoint>
197
203 inline SpecializedStation(StationID index, TileIndex tile) :
204 BaseStation(index, tile)
205 {
206 this->facilities = EXPECTED_FACIL;
207 }
208
214 static inline bool IsExpected(const BaseStation *st)
215 {
216 return st->facilities.Test(StationFacility::Waypoint) == Tis_waypoint;
217 }
218
224 static inline bool IsValidID(auto index)
225 {
227 }
228
234 static inline T *Get(auto index)
235 {
236 return (T *)BaseStation::Get(index);
237 }
238
244 static inline T *GetIfValid(auto index)
245 {
246 return IsValidID(index) ? Get(index) : nullptr;
247 }
248
254 static inline T *GetByTile(TileIndex tile)
255 {
256 return GetIfValid(GetStationIndex(tile));
257 }
258
264 template <typename... Targs>
265 static inline T *Create(Targs &&... args)
266 {
267 return BaseStation::Create<T>(std::forward<Targs&&>(args)...);
268 }
269
276 template <typename... Targs>
277 static inline T *CreateAtIndex(StationID index, Targs &&... args)
278 {
279 return BaseStation::CreateAtIndex<T>(index, std::forward<Targs&&>(args)...);
280 }
281
287 static inline T *From(BaseStation *st)
288 {
289 assert(IsExpected(st));
290 return (T *)st;
291 }
292
298 static inline const T *From(const BaseStation *st)
299 {
300 assert(IsExpected(st));
301 return (const T *)st;
302 }
303
309 static Pool::IterateWrapper<T> Iterate(size_t from = 0) { return Pool::IterateWrapper<T>(from); }
310};
311
318template <class T> std::vector<SpecMapping<T>> &GetStationSpecList(BaseStation *bst);
320template <> inline std::vector<SpecMapping<StationSpec>> &GetStationSpecList<StationSpec>(BaseStation *bst) { return bst->speclist; }
322template <> inline std::vector<SpecMapping<RoadStopSpec>> &GetStationSpecList<RoadStopSpec>(BaseStation *bst) { return bst->roadstop_speclist; }
323
324#endif /* BASE_STATION_BASE_H */
std::vector< SpecMapping< StationSpec > > & GetStationSpecList< StationSpec >(BaseStation *bst)
Get spec mapping list for each supported custom spec type.
std::vector< SpecMapping< T > > & GetStationSpecList(BaseStation *bst)
Get spec mapping list for each supported custom spec type.
std::vector< SpecMapping< RoadStopSpec > > & GetStationSpecList< RoadStopSpec >(BaseStation *bst)
Get spec mapping list for each supported custom spec type.
EnumBitSet< CargoType, uint64_t > CargoTypes
Bitset of CargoType elements.
Definition cargo_type.h:115
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
StrongType::Typedef< int32_t, DateTag< struct Calendar >, StrongType::Compare, StrongType::Integer > Date
static constexpr Owner INVALID_OWNER
An invalid owner.
DiagDirection
Enumeration for diagonal directions.
#define T
Climate temperate.
Definition engines.h:91
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
@ RoadStopTileData
Saveload version: 340, GitHub pull request: 12883 Move storage of road stop tile data,...
Definition saveload.h:393
Maps accessors for stations.
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:26
EnumBitSet< StationAnimationTrigger, uint16_t > StationAnimationTriggers
Bitset of StationAnimationTrigger elements.
@ Dock
Station with a dock.
@ Waypoint
Station is a waypoint.
@ TruckStop
Station with truck stops.
@ Train
Station with train station.
@ Airport
Station with an airport.
@ BusStop
Station with bus stops.
EnumBitSet< StationRandomTrigger, uint8_t > StationRandomTriggers
Bitset of StationRandomTrigger elements.
StationType
Station types.
EnumBitSet< StationFacility, uint8_t > StationFacilities
Bitset of StationFacility elements.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
static constexpr StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames).
Base class for all station-ish types.
virtual uint32_t GetNewGRFVariable(const struct ResolverObject &object, uint8_t variable, uint8_t parameter, bool &available) const =0
Helper function to get a NewGRF variable that isn't implemented by the base class.
std::vector< SpecMapping< RoadStopSpec > > roadstop_speclist
List of road stop specs of this station.
virtual uint GetPlatformLength(TileIndex tile) const =0
Obtain the length of a platform.
StringID string_id
Default name (town area) of station.
TileIndex xy
Base tile of the station.
virtual void MoveSign(TileIndex new_xy)
Move this station's main coordinate somewhere else.
std::vector< SpecMapping< StationSpec > > speclist
List of rail station specs of this station.
virtual TileArea GetTileArea(StationType type) const =0
Get the tile area for a given station type.
StationFacilities facilities
The facilities that this station has.
virtual ~BaseStation()
Ensure the destructor of the sub classes are called as well.
Definition station.cpp:52
std::string cached_name
NOSAVE: Cache of the resolved name of the station, if not using a custom name.
TileArea train_station
Tile area the train 'station' part covers.
virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const =0
Determines the REMAINING length of a platform, starting at (and including) the given tile.
StationAnimationTriggers cached_anim_triggers
NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen.
Owner owner
The owner of this station.
virtual void UpdateVirtCoord()=0
Update the coordinated of the sign (as shown in the viewport).
uint8_t delete_ctr
Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is ...
StationAnimationTriggers cached_roadstop_anim_triggers
NOSAVE: Combined animation trigger bitmask for road stops, used to determine if trigger processing sh...
static void PostDestructor(size_t index)
Invalidating of the JoinStation window has to be done after removing item from the pool.
Definition station.cpp:171
bool IsInUse() const
Check whether the base station currently is in use; in use means that it is not scheduled for deletio...
Town * town
The town this station is associated with.
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
virtual bool TileBelongsToRailStation(TileIndex tile) const =0
Check whether a specific tile belongs to this station.
CargoTypes cached_roadstop_cargo_triggers
NOSAVE: Combined cargo trigger bitmask for road stops.
uint16_t random_bits
Random bits assigned to this station.
TrackedViewportSign sign
NOSAVE: Dimensions of sign.
std::vector< RoadStopTileData > custom_roadstop_tile_data
List of custom road stop tile data.
TimerGameCalendar::Date build_date
Date of construction.
std::string name
Custom name.
TileArea spread
NOSAVE: Station spread tile area.
StationRandomTriggers waiting_random_triggers
Waiting triggers (NewGRF), shared by all station parts/tiles, road stops, ... essentially useless and...
BaseStation(StationID index, TileIndex tile)
Initialize the base station.
CargoTypes cached_cargo_triggers
NOSAVE: Combined cargo trigger bitmask.
static T * Create(Targs &&... args)
static T * CreateAtIndex(StationID index, Targs &&... args)
static BaseStation * Get(auto index)
Base class for all pools.
Interface for SpriteGroup-s to access the gamestate.
const T * spec
Custom spec.
uint16_t localidx
Local ID within GRF of this custom spec.
GrfID grfid
GRF ID of this custom spec.
SpecializedStation(StationID index, TileIndex tile)
Set station type correctly.
static bool IsValidID(auto index)
Tests whether given index is a valid index for station of this type.
static const T * From(const BaseStation *st)
Converts a const BaseStation to const SpecializedStation with type checking.
static T * Create(Targs &&... args)
Creates a new T-object in the station pool.
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
static T * GetByTile(TileIndex tile)
Get the station belonging to a specific tile.
static Pool::IterateWrapper< T > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
static T * CreateAtIndex(StationID index, Targs &&... args)
Creates a new T-object in the station pool.
static T * Get(auto index)
Gets station with given index.
static T * GetIfValid(auto index)
Returns station if the index is a valid index for this station type.
static constexpr StationFacilities EXPECTED_FACIL
Specialized type.
static T * From(BaseStation *st)
Converts a BaseStation to SpecializedStation with type checking.
Town data structure.
Definition town.h:64
Specialised ViewportSign that tracks whether it is valid for entering into a Kdtree.
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
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition of the game-calendar-timer.
Types related to viewports.