OpenTTD Source: src/base_station_base.h Source File

OpenTTD Source 15.0-beta1

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"
17#include "timer/timer_game_calendar.h"
18
19typedef Pool<BaseStation, StationID, 32, 64000> StationPool;
20extern StationPool _station_pool;
21
22template <typename T>
23 struct SpecMapping {
24 const T *spec;
25 uint32_t grfid;
26 uint16_t localidx;
27};
28
29 struct RoadStopTileData {
30 TileIndex tile;
31 uint8_t random_bits;
32 uint8_t animation_frame;
33};
34
36 struct StationRect : public Rect {
37 enum StationRectMode
38 {
39 ADD_TEST = 0,
40 ADD_TRY,
41 ADD_FORCE
42 };
43
44 StationRect();
45 void MakeEmpty();
46 bool PtInExtendedRect(int x, int y, int distance = 0) const;
47 bool IsEmpty() const;
48 CommandCost BeforeAddTile(TileIndex tile, StationRectMode mode);
49 CommandCost BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
50 bool AfterRemoveTile(BaseStation *st, TileIndex tile);
51 bool AfterRemoveRect(BaseStation *st, TileArea ta);
52
53 static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
54
55 StationRect& operator = (const Rect &src);
56};
57
59 struct BaseStation : StationPool::PoolItem<&_station_pool> {
60 TileIndex xy;
61 TrackedViewportSign sign;
62 uint8_t delete_ctr;
63
64 std::string name;
65 StringID string_id;
66 mutable std::string cached_name;
67
68 Town *town;
69 Owner owner;
70 StationFacility facilities;
71
72 std::vector<SpecMapping<StationSpec>> speclist;
73 std::vector<SpecMapping<RoadStopSpec>> roadstop_speclist;
74
75 TimerGameCalendar::Date build_date;
76
77 uint16_t random_bits;
78 uint8_t waiting_triggers;
79 uint8_t cached_anim_triggers;
80 uint8_t cached_roadstop_anim_triggers;
81 CargoTypes cached_cargo_triggers;
82 CargoTypes cached_roadstop_cargo_triggers;
83
84 TileArea train_station;
85 StationRect rect;
86
87 std::vector<RoadStopTileData> custom_roadstop_tile_data;
88
93 BaseStation(TileIndex tile) :
94 xy(tile),
95 train_station(INVALID_TILE, 0, 0)
96 {
97 }
98
99 virtual ~BaseStation();
100
106 virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
107
116 virtual uint32_t GetNewGRFVariable(const struct ResolverObject &object, uint8_t variable, uint8_t parameter, bool &available) const = 0;
117
121 virtual void UpdateVirtCoord() = 0;
122
123 inline const std::string &GetCachedName() const
124 {
125 if (!this->name.empty()) return this->name;
126 if (this->cached_name.empty()) this->FillCachedName();
127 return this->cached_name;
128 }
129
130 virtual void MoveSign(TileIndex new_xy)
131 {
132 this->xy = new_xy;
133 this->UpdateVirtCoord();
134 }
135
141 virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
142
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
177 inline bool IsInUse() const
178 {
179 return (this->facilities & ~FACIL_WAYPOINT) != 0;
180 }
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>
217 struct SpecializedStation : public BaseStation {
218 static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE;
219
224 inline SpecializedStation(TileIndex tile) :
225 BaseStation(tile)
226 {
227 this->facilities = EXPECTED_FACIL;
228 }
229
235 static inline bool IsExpected(const BaseStation *st)
236 {
237 return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
238 }
239
245 static inline bool IsValidID(size_t index)
246 {
247 return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
248 }
249
254 static inline T *Get(size_t index)
255 {
256 return (T *)BaseStation::Get(index);
257 }
258
263 static inline T *GetIfValid(size_t index)
264 {
265 return IsValidID(index) ? Get(index) : nullptr;
266 }
267
273 static inline T *GetByTile(TileIndex tile)
274 {
275 return GetIfValid(GetStationIndex(tile));
276 }
277
283 static inline T *From(BaseStation *st)
284 {
285 assert(IsExpected(st));
286 return (T *)st;
287 }
288
294 static inline const T *From(const BaseStation *st)
295 {
296 assert(IsExpected(st));
297 return (const T *)st;
298 }
299
305 static Pool::IterateWrapper<T> Iterate(size_t from = 0) { return Pool::IterateWrapper<T>(from); }
306};
307
314 template <class T> std::vector<SpecMapping<T>> &GetStationSpecList(BaseStation *bst);
315template <> inline std::vector<SpecMapping<StationSpec>> &GetStationSpecList<StationSpec>(BaseStation *bst) { return bst->speclist; }
316template <> inline std::vector<SpecMapping<RoadStopSpec>> &GetStationSpecList<RoadStopSpec>(BaseStation *bst) { return bst->roadstop_speclist; }
317
318#endif /* BASE_STATION_BASE_H */
GetStationSpecList
std::vector< SpecMapping< T > > & GetStationSpecList(BaseStation *bst)
Get spec mapping list for each supported custom spec type.
CommandCost
Common return value for all commands.
Definition command_type.h:23
command_type.h
Types related to commands.
Owner
Owner
Enum for all companies/owners.
Definition company_type.h:18
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:73
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
StationFacility
The facilities a station might be having.
Definition station_type.h:52
FACIL_WAYPOINT
@ FACIL_WAYPOINT
Station is a waypoint.
Definition station_type.h:59
FACIL_NONE
@ FACIL_NONE
The station has no facilities at all.
Definition station_type.h:53
StationType
StationType
Station types.
Definition station_type.h:31
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:16
BaseStation
Base class for all station-ish types.
Definition base_station_base.h:59
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::facilities
StationFacility facilities
The facilities that this station has.
Definition base_station_base.h:70
BaseStation::roadstop_speclist
std::vector< SpecMapping< RoadStopSpec > > roadstop_speclist
List of road stop specs of this station.
Definition base_station_base.h:73
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:65
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition base_station_base.h:60
BaseStation::speclist
std::vector< SpecMapping< StationSpec > > speclist
List of rail station specs of this station.
Definition base_station_base.h:72
BaseStation::cached_anim_triggers
uint8_t cached_anim_triggers
NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen.
Definition base_station_base.h:79
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:66
BaseStation::train_station
TileArea train_station
Tile area the train 'station' part covers.
Definition base_station_base.h:84
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_roadstop_anim_triggers
uint8_t cached_roadstop_anim_triggers
NOSAVE: Combined animation trigger bitmask for road stops, used to determine if trigger processing sh...
Definition base_station_base.h:80
BaseStation::owner
Owner owner
The owner of this station.
Definition base_station_base.h:69
BaseStation::GetTileArea
virtual void GetTileArea(TileArea *ta, StationType type) const =0
Get the tile area for a given station type.
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:62
BaseStation::rect
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Definition base_station_base.h:85
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:166
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:177
BaseStation::BaseStation
BaseStation(TileIndex tile)
Initialize the base station.
Definition base_station_base.h:93
BaseStation::town
Town * town
The town this station is associated with.
Definition base_station_base.h:68
BaseStation::GetByTile
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
Definition base_station_base.h:166
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:82
BaseStation::random_bits
uint16_t random_bits
Random bits assigned to this station.
Definition base_station_base.h:77
BaseStation::sign
TrackedViewportSign sign
NOSAVE: Dimensions of sign.
Definition base_station_base.h:61
BaseStation::custom_roadstop_tile_data
std::vector< RoadStopTileData > custom_roadstop_tile_data
List of custom road stop tile data.
Definition base_station_base.h:87
BaseStation::waiting_triggers
uint8_t waiting_triggers
Waiting triggers (NewGRF) for this station.
Definition base_station_base.h:78
BaseStation::build_date
TimerGameCalendar::Date build_date
Date of construction.
Definition base_station_base.h:75
BaseStation::name
std::string name
Custom name.
Definition base_station_base.h:64
BaseStation::cached_cargo_triggers
CargoTypes cached_cargo_triggers
NOSAVE: Combined cargo trigger bitmask.
Definition base_station_base.h:81
OrthogonalTileArea
Represents the covered area of e.g.
Definition tilearea_type.h:18
Pool::IterateWrapper
Definition pool_type.hpp:178
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:237
Pool::PoolItem<&_station_pool >::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:238
Pool::PoolItem<&_station_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition pool_type.hpp:328
Pool::PoolItem<&_station_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition pool_type.hpp:339
Pool
Base class for all pools.
Definition pool_type.hpp:80
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:75
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition newgrf_spritegroup.h:296
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:217
SpecializedStation::From
static const T * From(const BaseStation *st)
Converts a const BaseStation to const SpecializedStation with type checking.
Definition base_station_base.h:294
SpecializedStation::EXPECTED_FACIL
static const StationFacility EXPECTED_FACIL
Specialized type.
Definition base_station_base.h:218
SpecializedStation::IsExpected
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
Definition base_station_base.h:235
SpecializedStation::GetByTile
static T * GetByTile(TileIndex tile)
Get the station belonging to a specific tile.
Definition base_station_base.h:273
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:305
SpecializedStation::SpecializedStation
SpecializedStation(TileIndex tile)
Set station type correctly.
Definition base_station_base.h:224
SpecializedStation::Get
static T * Get(size_t index)
Gets station with given index.
Definition base_station_base.h:254
SpecializedStation::GetIfValid
static T * GetIfValid(size_t index)
Returns station if the index is a valid index for this station type.
Definition base_station_base.h:263
SpecializedStation::IsValidID
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
Definition base_station_base.h:245
SpecializedStation::From
static T * From(BaseStation *st)
Converts a BaseStation to SpecializedStation with type checking.
Definition base_station_base.h:283
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:625
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:560
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:54
TrackedViewportSign
Specialised ViewportSign that tracks whether it is valid for entering into a Kdtree.
Definition viewport_type.h:51
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.