OpenTTD Source 20250312-master-gcdcc6b491d
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 <http://www.gnu.org/licenses/>.
6 */
7
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
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
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;
70
71 std::vector<SpecMapping<StationSpec>> speclist{};
72 std::vector<SpecMapping<RoadStopSpec>> roadstop_speclist{};
73
75
76 uint16_t random_bits = 0;
77 uint8_t waiting_triggers = 0;
80 CargoTypes cached_cargo_triggers{};
82
85
86 std::vector<RoadStopTileData> custom_roadstop_tile_data{};
87
92 BaseStation(TileIndex tile) : xy(tile) {}
93
94 virtual ~BaseStation();
95
101 virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
102
111 virtual uint32_t GetNewGRFVariable(const struct ResolverObject &object, uint8_t variable, uint8_t parameter, bool &available) const = 0;
112
116 virtual void UpdateVirtCoord() = 0;
117
118 inline const std::string &GetCachedName() const
119 {
120 if (!this->name.empty()) return this->name;
121 if (this->cached_name.empty()) this->FillCachedName();
122 return this->cached_name;
123 }
124
125 virtual void MoveSign(TileIndex new_xy)
126 {
127 this->xy = new_xy;
128 this->UpdateVirtCoord();
129 }
130
136 virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
137
138
145 virtual uint GetPlatformLength(TileIndex tile) const = 0;
146
154 virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
155
161 static inline BaseStation *GetByTile(TileIndex tile)
162 {
163 return BaseStation::Get(GetStationIndex(tile));
164 }
165
176
177 inline uint8_t GetRoadStopRandomBits(TileIndex tile) const
178 {
179 for (const RoadStopTileData &tile_data : this->custom_roadstop_tile_data) {
180 if (tile_data.tile == tile) return tile_data.random_bits;
181 }
182 return 0;
183 }
184
185 inline uint8_t GetRoadStopAnimationFrame(TileIndex tile) const
186 {
187 for (const RoadStopTileData &tile_data : this->custom_roadstop_tile_data) {
188 if (tile_data.tile == tile) return tile_data.animation_frame;
189 }
190 return 0;
191 }
192
193private:
194 bool SetRoadStopTileData(TileIndex tile, uint8_t data, bool animation);
195
196public:
197 inline void SetRoadStopRandomBits(TileIndex tile, uint8_t random_bits) { this->SetRoadStopTileData(tile, random_bits, false); }
198 inline bool SetRoadStopAnimationFrame(TileIndex tile, uint8_t frame) { return this->SetRoadStopTileData(tile, frame, true); }
199 void RemoveRoadStopTileData(TileIndex tile);
200
201 static void PostDestructor(size_t index);
202
203private:
204 void FillCachedName() const;
205};
206
211template <class T, bool Tis_waypoint>
214
220 BaseStation(tile)
221 {
223 }
224
230 static inline bool IsExpected(const BaseStation *st)
231 {
232 return st->facilities.Test(StationFacility::Waypoint) == Tis_waypoint;
233 }
234
240 static inline bool IsValidID(auto index)
241 {
243 }
244
249 static inline T *Get(auto index)
250 {
251 return (T *)BaseStation::Get(index);
252 }
253
258 static inline T *GetIfValid(auto index)
259 {
260 return IsValidID(index) ? Get(index) : nullptr;
261 }
262
268 static inline T *GetByTile(TileIndex tile)
269 {
270 return GetIfValid(GetStationIndex(tile));
271 }
272
278 static inline T *From(BaseStation *st)
279 {
280 assert(IsExpected(st));
281 return (T *)st;
282 }
283
289 static inline const T *From(const BaseStation *st)
290 {
291 assert(IsExpected(st));
292 return (const T *)st;
293 }
294
300 static Pool::IterateWrapper<T> Iterate(size_t from = 0) { return Pool::IterateWrapper<T>(from); }
301};
302
309template <class T> std::vector<SpecMapping<T>> &GetStationSpecList(BaseStation *bst);
310template <> inline std::vector<SpecMapping<StationSpec>> &GetStationSpecList<StationSpec>(BaseStation *bst) { return bst->speclist; }
311template <> inline std::vector<SpecMapping<RoadStopSpec>> &GetStationSpecList<RoadStopSpec>(BaseStation *bst) { return bst->roadstop_speclist; }
312
313#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.
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.
std::vector< SpecMapping< StationSpec > > speclist
List of rail station specs of this station.
StationFacilities facilities
The facilities that this station has.
uint8_t cached_anim_triggers
NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen.
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.
uint8_t cached_roadstop_anim_triggers
NOSAVE: Combined animation trigger bitmask for road stops, used to determine if trigger processing sh...
Owner owner
The owner of this station.
virtual void GetTileArea(TileArea *ta, StationType type) const =0
Get the tile area for a given station type.
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 ...
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:169
bool IsInUse() const
Check whether the base station currently is in use; in use means that it is not scheduled for deletio...
BaseStation(TileIndex tile)
Initialize the base station.
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.
uint8_t waiting_triggers
Waiting triggers (NewGRF) for this station.
TimerGameCalendar::Date build_date
Date of construction.
std::string name
Custom name.
CargoTypes cached_cargo_triggers
NOSAVE: Combined cargo trigger bitmask.
Represents the covered area of e.g.
Base class for all PoolItems.
static Titem * Get(auto index)
Returns Titem with given index.
Tindex index
Index of this pool item.
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Base class for all pools.
Specification of a rectangle with absolute coordinates of all edges.
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.
Class defining several overloaded accessors so we don't have to cast base stations that often.
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 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.
SpecializedStation(TileIndex tile)
Set station type correctly.
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:628
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:563
Town data structure.
Definition town.h:52
Specialised ViewportSign that tracks whether it is valid for entering into a Kdtree.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
Definition of the game-calendar-timer.
Types related to viewports.