OpenTTD Source 20260206-master-g4d4e37dbf1
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 "command_type.h"
15#include "viewport_type.h"
16#include "station_map.h"
18
19typedef Pool<BaseStation, StationID, 32> StationPool;
20extern StationPool _station_pool;
21
22template <typename T>
24 const T *spec = nullptr;
25 uint32_t grfid = 0;
26 uint16_t localidx = 0;
27};
28
31 uint8_t random_bits = 0;
32 uint8_t animation_frame = 0;
33};
34
36struct StationRect : public Rect {
37 enum StationRectMode : uint8_t {
38 ADD_TEST = 0,
39 ADD_TRY,
40 ADD_FORCE
41 };
42
43 StationRect();
44 void MakeEmpty();
45 bool PtInExtendedRect(int x, int y, int distance = 0) const;
46 bool IsEmpty() const;
47 CommandCost BeforeAddTile(TileIndex tile, StationRectMode mode);
48 CommandCost BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
49 bool AfterRemoveTile(BaseStation *st, TileIndex tile);
50 bool AfterRemoveRect(BaseStation *st, TileArea ta);
51
52 static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
53
54 StationRect& operator = (const Rect &src);
55};
56
58struct BaseStation : StationPool::PoolItem<&_station_pool> {
61 uint8_t delete_ctr = 0;
62
63 std::string name{};
65 mutable std::string cached_name;
66
67 Town *town = nullptr;
69 StationFacilities facilities{};
70
71 std::vector<SpecMapping<StationSpec>> speclist{};
72 std::vector<SpecMapping<RoadStopSpec>> roadstop_speclist{};
73
74 TimerGameCalendar::Date build_date{};
75
76 uint16_t random_bits = 0;
77 std::unordered_map<TileIndex, StationRandomTriggers> tile_waiting_random_triggers;
78 StationRandomTriggers waiting_random_triggers;
79 StationAnimationTriggers cached_anim_triggers;
80 StationAnimationTriggers cached_roadstop_anim_triggers;
81 CargoTypes cached_cargo_triggers{};
83
86
87 std::vector<RoadStopTileData> custom_roadstop_tile_data{};
88
94 BaseStation(StationID index, TileIndex tile) : StationPool::PoolItem<&_station_pool>(index), xy(tile) {}
95
96 virtual ~BaseStation();
97
103 virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
104
113 virtual uint32_t GetNewGRFVariable(const struct ResolverObject &object, uint8_t variable, uint8_t parameter, bool &available) const = 0;
114
118 virtual void UpdateVirtCoord() = 0;
119
120 inline const std::string &GetCachedName() const
121 {
122 if (!this->name.empty()) return this->name;
123 if (this->cached_name.empty()) this->FillCachedName();
124 return this->cached_name;
125 }
126
131 virtual void MoveSign(TileIndex new_xy)
132 {
133 this->xy = new_xy;
134 this->UpdateVirtCoord();
135 }
136
142 virtual TileArea GetTileArea(StationType type) const = 0;
143
150 virtual uint GetPlatformLength(TileIndex tile) const = 0;
151
159 virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
160
166 static inline BaseStation *GetByTile(TileIndex tile)
167 {
168 return BaseStation::Get(GetStationIndex(tile));
169 }
170
181
182 inline uint8_t GetRoadStopRandomBits(TileIndex tile) const
183 {
184 for (const RoadStopTileData &tile_data : this->custom_roadstop_tile_data) {
185 if (tile_data.tile == tile) return tile_data.random_bits;
186 }
187 return 0;
188 }
189
190 inline uint8_t GetRoadStopAnimationFrame(TileIndex tile) const
191 {
192 for (const RoadStopTileData &tile_data : this->custom_roadstop_tile_data) {
193 if (tile_data.tile == tile) return tile_data.animation_frame;
194 }
195 return 0;
196 }
197
198private:
199 bool SetRoadStopTileData(TileIndex tile, uint8_t data, bool animation);
200
201public:
202 inline void SetRoadStopRandomBits(TileIndex tile, uint8_t random_bits) { this->SetRoadStopTileData(tile, random_bits, false); }
203 inline bool SetRoadStopAnimationFrame(TileIndex tile, uint8_t frame) { return this->SetRoadStopTileData(tile, frame, true); }
204 void RemoveRoadStopTileData(TileIndex tile);
205
206 static void PostDestructor(size_t index);
207
208private:
209 void FillCachedName() const;
210};
211
216template <class T, bool Tis_waypoint>
218 static constexpr StationFacilities EXPECTED_FACIL = Tis_waypoint ? StationFacility::Waypoint : StationFacilities{};
219
225 inline SpecializedStation(StationID index, TileIndex tile) :
226 BaseStation(index, tile)
227 {
228 this->facilities = EXPECTED_FACIL;
229 }
230
236 static inline bool IsExpected(const BaseStation *st)
237 {
238 return st->facilities.Test(StationFacility::Waypoint) == Tis_waypoint;
239 }
240
246 static inline bool IsValidID(auto index)
247 {
249 }
250
255 static inline T *Get(auto index)
256 {
257 return (T *)BaseStation::Get(index);
258 }
259
264 static inline T *GetIfValid(auto index)
265 {
266 return IsValidID(index) ? Get(index) : nullptr;
267 }
268
274 static inline T *GetByTile(TileIndex tile)
275 {
276 return GetIfValid(GetStationIndex(tile));
277 }
278
284 template <typename... Targs>
285 static inline T *Create(Targs &&... args)
286 {
287 return BaseStation::Create<T>(std::forward<Targs&&>(args)...);
288 }
289
296 template <typename... Targs>
297 static inline T *CreateAtIndex(StationID index, Targs &&... args)
298 {
299 return BaseStation::CreateAtIndex<T>(index, std::forward<Targs&&>(args)...);
300 }
301
307 static inline T *From(BaseStation *st)
308 {
309 assert(IsExpected(st));
310 return (T *)st;
311 }
312
318 static inline const T *From(const BaseStation *st)
319 {
320 assert(IsExpected(st));
321 return (const T *)st;
322 }
323
329 static Pool::IterateWrapper<T> Iterate(size_t from = 0) { return Pool::IterateWrapper<T>(from); }
330};
331
338template <class T> std::vector<SpecMapping<T>> &GetStationSpecList(BaseStation *bst);
339template <> inline std::vector<SpecMapping<StationSpec>> &GetStationSpecList<StationSpec>(BaseStation *bst) { return bst->speclist; }
340template <> inline std::vector<SpecMapping<RoadStopSpec>> &GetStationSpecList<RoadStopSpec>(BaseStation *bst) { return bst->roadstop_speclist; }
341
342#endif /* BASE_STATION_BASE_H */
std::vector< SpecMapping< T > > & GetStationSpecList(BaseStation *bst)
Get spec mapping list for each supported custom spec type.
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.
Common return value for all commands.
Types related to commands.
static constexpr Owner INVALID_OWNER
An invalid owner.
DiagDirection
Enumeration for diagonal directions.
#define Rect
Macro that prevents name conflicts between included headers.
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
Maps accessors for stations.
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:28
@ 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.
StationType
Station types.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
static const 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.
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...
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
static void PostDestructor(size_t index)
Invalidating of the JoinStation window has to be done after removing item from the pool.
Definition station.cpp:170
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.
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 * CreateAtIndex(StationID index, Targs &&... args)
static BaseStation * Get(auto index)
static T * Create(Targs &&... args)
Base class for all pools.
Interface for SpriteGroup-s to access the gamestate.
const T * spec
Custom spec.
uint32_t grfid
GRF ID of this custom spec.
uint16_t localidx
Local ID within GRF 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.
StationRect - used to track station spread out rectangle - cheaper than scanning whole map.
static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a)
Check whether station tiles of the given station id exist in the given rectangle.
Definition station.cpp:629
bool PtInExtendedRect(int x, int y, int distance=0) const
Determines whether a given point (x, y) is within a certain distance of the station rectangle.
Definition station.cpp:564
Town data structure.
Definition town.h:63
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.