OpenTTD Source: src/base_station_base.h Source File

OpenTTD Source 15.3

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
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"
17#include "timer/timer_game_calendar.h"
18
19typedef Pool<BaseStation, StationID, 32> StationPool;
20extern StationPool _station_pool;
21
22template <typename T>
23 struct SpecMapping {
24 const T *spec = nullptr;
25 uint32_t grfid = 0;
26 uint16_t localidx = 0;
27};
28
29 struct RoadStopTileData {
30 TileIndex tile = INVALID_TILE;
31 uint8_t random_bits = 0;
32 uint8_t animation_frame = 0;
33};
34
36 struct 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
58 struct BaseStation : StationPool::PoolItem<&_station_pool> {
59 TileIndex xy = INVALID_TILE;
60 TrackedViewportSign sign{};
61 uint8_t delete_ctr = 0;
62
63 std::string name{};
64 StringID string_id = INVALID_STRING_ID;
65 mutable std::string cached_name;
66
67 Town *town = nullptr;
68 Owner owner = INVALID_OWNER;
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 StationRandomTriggers waiting_random_triggers;
78 StationAnimationTriggers cached_anim_triggers;
79 StationAnimationTriggers cached_roadstop_anim_triggers;
80 CargoTypes cached_cargo_triggers{};
81 CargoTypes cached_roadstop_cargo_triggers{};
82
83 TileArea train_station{INVALID_TILE, 0, 0};
84 StationRect rect{};
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 TileArea GetTileArea(StationType type) const = 0;
137
144 virtual uint GetPlatformLength(TileIndex tile) const = 0;
145
153 virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
154
160 static inline BaseStation *GetByTile(TileIndex tile)
161 {
162 return BaseStation::Get(GetStationIndex(tile));
163 }
164
171 inline bool IsInUse() const
172 {
173 return this->facilities.Any({StationFacility::Train, StationFacility::TruckStop, StationFacility::BusStop, StationFacility::Airport, StationFacility::Dock});
174 }
175
176 inline uint8_t GetRoadStopRandomBits(TileIndex tile) const
177 {
178 for (const RoadStopTileData &tile_data : this->custom_roadstop_tile_data) {
179 if (tile_data.tile == tile) return tile_data.random_bits;
180 }
181 return 0;
182 }
183
184 inline uint8_t GetRoadStopAnimationFrame(TileIndex tile) const
185 {
186 for (const RoadStopTileData &tile_data : this->custom_roadstop_tile_data) {
187 if (tile_data.tile == tile) return tile_data.animation_frame;
188 }
189 return 0;
190 }
191
192private:
193 bool SetRoadStopTileData(TileIndex tile, uint8_t data, bool animation);
194
195public:
196 inline void SetRoadStopRandomBits(TileIndex tile, uint8_t random_bits) { this->SetRoadStopTileData(tile, random_bits, false); }
197 inline bool SetRoadStopAnimationFrame(TileIndex tile, uint8_t frame) { return this->SetRoadStopTileData(tile, frame, true); }
198 void RemoveRoadStopTileData(TileIndex tile);
199
200 static void PostDestructor(size_t index);
201
202private:
203 void FillCachedName() const;
204};
205
210template <class T, bool Tis_waypoint>
211 struct SpecializedStation : public BaseStation {
212 static constexpr StationFacilities EXPECTED_FACIL = Tis_waypoint ? StationFacility::Waypoint : StationFacilities{};
213
218 inline SpecializedStation(TileIndex tile) :
219 BaseStation(tile)
220 {
221 this->facilities = EXPECTED_FACIL;
222 }
223
229 static inline bool IsExpected(const BaseStation *st)
230 {
231 return st->facilities.Test(StationFacility::Waypoint) == Tis_waypoint;
232 }
233
239 static inline bool IsValidID(auto index)
240 {
241 return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
242 }
243
248 static inline T *Get(auto index)
249 {
250 return (T *)BaseStation::Get(index);
251 }
252
257 static inline T *GetIfValid(auto index)
258 {
259 return IsValidID(index) ? Get(index) : nullptr;
260 }
261
267 static inline T *GetByTile(TileIndex tile)
268 {
269 return GetIfValid(GetStationIndex(tile));
270 }
271
277 static inline T *From(BaseStation *st)
278 {
279 assert(IsExpected(st));
280 return (T *)st;
281 }
282
288 static inline const T *From(const BaseStation *st)
289 {
290 assert(IsExpected(st));
291 return (const T *)st;
292 }
293
299 static Pool::IterateWrapper<T> Iterate(size_t from = 0) { return Pool::IterateWrapper<T>(from); }
300};
301
308 template <class T> std::vector<SpecMapping<T>> &GetStationSpecList(BaseStation *bst);
309template <> inline std::vector<SpecMapping<StationSpec>> &GetStationSpecList<StationSpec>(BaseStation *bst) { return bst->speclist; }
310template <> inline std::vector<SpecMapping<RoadStopSpec>> &GetStationSpecList<RoadStopSpec>(BaseStation *bst) { return bst->roadstop_speclist; }
311
312#endif /* BASE_STATION_BASE_H */
GetStationSpecList
std::vector< SpecMapping< T > > & GetStationSpecList(BaseStation *bst)
Get spec mapping list for each supported custom spec type.
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Any
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Definition base_bitset_type.hpp:170
CommandCost
Common return value for all commands.
Definition command_type.h:24
EnumBitSet< StationFacility, uint8_t >
command_type.h
Types related to commands.
INVALID_OWNER
static constexpr Owner INVALID_OWNER
An invalid owner.
Definition company_type.h:30
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
pool_type.hpp
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
station_map.h
Maps accessors for stations.
GetStationIndex
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:28
StationFacility::Dock
@ Dock
Station with a dock.
StationFacility::Waypoint
@ Waypoint
Station is a waypoint.
StationFacility::TruckStop
@ TruckStop
Station with truck stops.
StationFacility::Train
@ Train
Station with train station.
StationFacility::Airport
@ Airport
Station with an airport.
StationFacility::BusStop
@ BusStop
Station with bus stops.
StationType
StationType
Station types.
Definition station_type.h:29
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition strings_type.h:20
BaseStation
Base class for all station-ish types.
Definition base_station_base.h:58
BaseStation::GetNewGRFVariable
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.
BaseStation::roadstop_speclist
std::vector< SpecMapping< RoadStopSpec > > roadstop_speclist
List of road stop specs of this station.
Definition base_station_base.h:72
BaseStation::GetPlatformLength
virtual uint GetPlatformLength(TileIndex tile) const =0
Obtain the length of a platform.
BaseStation::string_id
StringID string_id
Default name (town area) of station.
Definition base_station_base.h:64
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition base_station_base.h:59
BaseStation::speclist
std::vector< SpecMapping< StationSpec > > speclist
List of rail station specs of this station.
Definition base_station_base.h:71
BaseStation::GetTileArea
virtual TileArea GetTileArea(StationType type) const =0
Get the tile area for a given station type.
BaseStation::facilities
StationFacilities facilities
The facilities that this station has.
Definition base_station_base.h:69
BaseStation::cached_name
std::string cached_name
NOSAVE: Cache of the resolved name of the station, if not using a custom name.
Definition base_station_base.h:65
BaseStation::train_station
TileArea train_station
Tile area the train 'station' part covers.
Definition base_station_base.h:83
BaseStation::GetPlatformLength
virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const =0
Determines the REMAINING length of a platform, starting at (and including) the given tile.
BaseStation::cached_anim_triggers
StationAnimationTriggers cached_anim_triggers
NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen.
Definition base_station_base.h:78
BaseStation::owner
Owner owner
The owner of this station.
Definition base_station_base.h:68
BaseStation::UpdateVirtCoord
virtual void UpdateVirtCoord()=0
Update the coordinated of the sign (as shown in the viewport).
BaseStation::delete_ctr
uint8_t delete_ctr
Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is ...
Definition base_station_base.h:61
BaseStation::cached_roadstop_anim_triggers
StationAnimationTriggers cached_roadstop_anim_triggers
NOSAVE: Combined animation trigger bitmask for road stops, used to determine if trigger processing sh...
Definition base_station_base.h:79
BaseStation::rect
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Definition base_station_base.h:84
BaseStation::PostDestructor
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
BaseStation::IsInUse
bool IsInUse() const
Check whether the base station currently is in use; in use means that it is not scheduled for deletio...
Definition base_station_base.h:171
BaseStation::BaseStation
BaseStation(TileIndex tile)
Initialize the base station.
Definition base_station_base.h:92
BaseStation::town
Town * town
The town this station is associated with.
Definition base_station_base.h:67
BaseStation::GetByTile
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
Definition base_station_base.h:160
BaseStation::TileBelongsToRailStation
virtual bool TileBelongsToRailStation(TileIndex tile) const =0
Check whether a specific tile belongs to this station.
BaseStation::cached_roadstop_cargo_triggers
CargoTypes cached_roadstop_cargo_triggers
NOSAVE: Combined cargo trigger bitmask for road stops.
Definition base_station_base.h:81
BaseStation::random_bits
uint16_t random_bits
Random bits assigned to this station.
Definition base_station_base.h:76
BaseStation::sign
TrackedViewportSign sign
NOSAVE: Dimensions of sign.
Definition base_station_base.h:60
BaseStation::custom_roadstop_tile_data
std::vector< RoadStopTileData > custom_roadstop_tile_data
List of custom road stop tile data.
Definition base_station_base.h:86
BaseStation::build_date
TimerGameCalendar::Date build_date
Date of construction.
Definition base_station_base.h:74
BaseStation::name
std::string name
Custom name.
Definition base_station_base.h:63
BaseStation::waiting_random_triggers
StationRandomTriggers waiting_random_triggers
Waiting triggers (NewGRF), shared by all station parts/tiles, road stops, ... essentially useless and...
Definition base_station_base.h:77
BaseStation::cached_cargo_triggers
CargoTypes cached_cargo_triggers
NOSAVE: Combined cargo trigger bitmask.
Definition base_station_base.h:80
OrthogonalTileArea
Represents the covered area of e.g.
Definition tilearea_type.h:18
PoolID< uint16_t, struct StationIDTag, 64000, 0xFFFF >
Pool::IterateWrapper
Definition pool_type.hpp:227
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:285
Pool::PoolItem<&_station_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem<&_station_pool >::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
Pool::PoolItem<&_station_pool >::IsValidID
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition pool_type.hpp:376
Pool
Base class for all pools.
Definition pool_type.hpp:133
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition newgrf_spritegroup.h:288
RoadStopTileData
Definition base_station_base.h:29
SpecMapping
Definition base_station_base.h:23
SpecMapping::spec
const T * spec
Custom spec.
Definition base_station_base.h:24
SpecMapping::grfid
uint32_t grfid
GRF ID of this custom spec.
Definition base_station_base.h:25
SpecMapping::localidx
uint16_t localidx
Local ID within GRF of this custom spec.
Definition base_station_base.h:26
SpecializedStation
Class defining several overloaded accessors so we don't have to cast base stations that often.
Definition base_station_base.h:211
SpecializedStation::IsValidID
static bool IsValidID(auto index)
Tests whether given index is a valid index for station of this type.
Definition base_station_base.h:239
SpecializedStation::From
static const T * From(const BaseStation *st)
Converts a const BaseStation to const SpecializedStation with type checking.
Definition base_station_base.h:288
SpecializedStation::IsExpected
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
Definition base_station_base.h:229
SpecializedStation::GetByTile
static T * GetByTile(TileIndex tile)
Get the station belonging to a specific tile.
Definition base_station_base.h:267
SpecializedStation::Iterate
static Pool::IterateWrapper< T > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
Definition base_station_base.h:299
SpecializedStation::SpecializedStation
SpecializedStation(TileIndex tile)
Set station type correctly.
Definition base_station_base.h:218
SpecializedStation::Get
static T * Get(auto index)
Gets station with given index.
Definition base_station_base.h:248
SpecializedStation::GetIfValid
static T * GetIfValid(auto index)
Returns station if the index is a valid index for this station type.
Definition base_station_base.h:257
SpecializedStation::EXPECTED_FACIL
static constexpr StationFacilities EXPECTED_FACIL
Specialized type.
Definition base_station_base.h:212
SpecializedStation::From
static T * From(BaseStation *st)
Converts a BaseStation to SpecializedStation with type checking.
Definition base_station_base.h:277
StationRect
StationRect - used to track station spread out rectangle - cheaper than scanning whole map.
Definition base_station_base.h:36
StationRect::ScanForStationTiles
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
StationRect::PtInExtendedRect
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
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Town
Town data structure.
Definition town.h:63
TrackedViewportSign
Specialised ViewportSign that tracks whether it is valid for entering into a Kdtree.
Definition viewport_type.h:60
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
timer_game_calendar.h
Definition of the game-calendar-timer.
viewport_type.h
Types related to viewports.