OpenTTD Source  20241120-master-g6d3adc6169
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 
21 
22 template <typename T>
23 struct SpecMapping {
24  const T *spec;
25  uint32_t grfid;
26  uint16_t localidx;
27 };
28 
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> {
62  uint8_t delete_ctr;
63 
64  std::string name;
66  mutable std::string cached_name;
67 
71 
72  std::vector<SpecMapping<StationSpec>> speclist;
73  std::vector<SpecMapping<RoadStopSpec>> roadstop_speclist;
74 
76 
77  uint16_t random_bits;
78  uint8_t waiting_triggers;
81  CargoTypes cached_cargo_triggers;
83 
86 
87  std::vector<RoadStopTileData> custom_roadstop_tile_data;
88 
94  xy(tile),
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 
198 private:
199  bool SetRoadStopTileData(TileIndex tile, uint8_t data, bool animation);
200 
201 public:
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 
208 private:
209  void FillCachedName() const;
210 };
211 
216 template <class T, bool Tis_waypoint>
218  static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE;
219 
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  {
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);
315 template <> inline std::vector<SpecMapping<StationSpec>> &GetStationSpecList<StationSpec>(BaseStation *bst) { return bst->speclist; }
316 template <> inline std::vector<SpecMapping<RoadStopSpec>> &GetStationSpecList<RoadStopSpec>(BaseStation *bst) { return bst->roadstop_speclist; }
317 
318 #endif /* BASE_STATION_BASE_H */
std::vector< SpecMapping< T > > & GetStationSpecList(BaseStation *bst)
Get spec mapping list for each supported custom spec type.
Common return value for all commands.
Definition: command_type.h:23
Types related to commands.
Owner
Enum for all companies/owners.
Definition: company_type.h:18
DiagDirection
Enumeration for diagonal directions.
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
StationPool _station_pool("Station")
The pool of stations.
Maps accessors for stations.
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition: station_map.h:28
StationFacility
The facilities a station might be having.
Definition: station_type.h:52
@ FACIL_WAYPOINT
Station is a waypoint.
Definition: station_type.h:59
@ FACIL_NONE
The station has no facilities at all.
Definition: station_type.h:53
StationType
Station types.
Definition: station_type.h:31
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
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.
StationFacility facilities
The facilities that this station has.
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.
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:166
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.
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.
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
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.
Definition: tilearea_type.h:18
Base class for all PoolItems.
Definition: pool_type.hpp:237
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:328
Base class for all pools.
Definition: pool_type.hpp:80
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 T * GetIfValid(size_t index)
Returns station if the index is a valid index for this station type.
static const StationFacility EXPECTED_FACIL
Specialized type.
static Pool::IterateWrapper< T > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
static T * Get(size_t index)
Gets station with given index.
static T * GetByTile(TileIndex tile)
Get the station belonging to a specific tile.
SpecializedStation(TileIndex tile)
Set station type correctly.
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this 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:625
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
Town data structure.
Definition: town.h:54
Specialised ViewportSign that tracks whether it is valid for entering into a Kdtree.
Definition: viewport_type.h:51
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.