OpenTTD Source  14.0-beta3
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 
23  const StationSpec *spec;
24  uint32_t grfid;
25  uint16_t localidx;
26 };
27 
29  const RoadStopSpec *spec;
30  uint32_t grfid;
31  uint16_t localidx;
32 };
33 
35  TileIndex tile;
36  uint8_t random_bits;
37  uint8_t animation_frame;
38 };
39 
41 struct StationRect : public Rect {
42  enum StationRectMode
43  {
44  ADD_TEST = 0,
45  ADD_TRY,
46  ADD_FORCE
47  };
48 
49  StationRect();
50  void MakeEmpty();
51  bool PtInExtendedRect(int x, int y, int distance = 0) const;
52  bool IsEmpty() const;
53  CommandCost BeforeAddTile(TileIndex tile, StationRectMode mode);
54  CommandCost BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
55  bool AfterRemoveTile(BaseStation *st, TileIndex tile);
56  bool AfterRemoveRect(BaseStation *st, TileArea ta);
57 
58  static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
59 
60  StationRect& operator = (const Rect &src);
61 };
62 
64 struct BaseStation : StationPool::PoolItem<&_station_pool> {
67  byte delete_ctr;
68 
69  std::string name;
71  mutable std::string cached_name;
72 
76 
77  std::vector<StationSpecList> speclist;
78  std::vector<RoadStopSpecList> roadstop_speclist;
79 
80  TimerGameCalendar::Date build_date;
81 
82  uint16_t random_bits;
86  CargoTypes cached_cargo_triggers;
88 
91 
92  std::vector<RoadStopTileData> custom_roadstop_tile_data;
93 
99  xy(tile),
101  {
102  }
103 
104  virtual ~BaseStation();
105 
111  virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
112 
121  virtual uint32_t GetNewGRFVariable(const struct ResolverObject &object, byte variable, byte parameter, bool *available) const = 0;
122 
126  virtual void UpdateVirtCoord() = 0;
127 
128  inline const std::string &GetCachedName() const
129  {
130  if (!this->name.empty()) return this->name;
131  if (this->cached_name.empty()) this->FillCachedName();
132  return this->cached_name;
133  }
134 
135  virtual void MoveSign(TileIndex new_xy)
136  {
137  this->xy = new_xy;
138  this->UpdateVirtCoord();
139  }
140 
146  virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
147 
148 
155  virtual uint GetPlatformLength(TileIndex tile) const = 0;
156 
164  virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
165 
171  static inline BaseStation *GetByTile(TileIndex tile)
172  {
173  return BaseStation::Get(GetStationIndex(tile));
174  }
175 
182  inline bool IsInUse() const
183  {
184  return (this->facilities & ~FACIL_WAYPOINT) != 0;
185  }
186 
187  inline byte GetRoadStopRandomBits(TileIndex tile) const
188  {
189  for (const RoadStopTileData &tile_data : this->custom_roadstop_tile_data) {
190  if (tile_data.tile == tile) return tile_data.random_bits;
191  }
192  return 0;
193  }
194 
195  inline byte GetRoadStopAnimationFrame(TileIndex tile) const
196  {
197  for (const RoadStopTileData &tile_data : this->custom_roadstop_tile_data) {
198  if (tile_data.tile == tile) return tile_data.animation_frame;
199  }
200  return 0;
201  }
202 
203 private:
204  void SetRoadStopTileData(TileIndex tile, byte data, bool animation);
205 
206 public:
207  inline void SetRoadStopRandomBits(TileIndex tile, byte random_bits) { this->SetRoadStopTileData(tile, random_bits, false); }
208  inline void SetRoadStopAnimationFrame(TileIndex tile, byte frame) { this->SetRoadStopTileData(tile, frame, true); }
209  void RemoveRoadStopTileData(TileIndex tile);
210 
211  static void PostDestructor(size_t index);
212 
213 private:
214  void FillCachedName() const;
215 };
216 
221 template <class T, bool Tis_waypoint>
223  static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE;
224 
230  BaseStation(tile)
231  {
232  this->facilities = EXPECTED_FACIL;
233  }
234 
240  static inline bool IsExpected(const BaseStation *st)
241  {
242  return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
243  }
244 
250  static inline bool IsValidID(size_t index)
251  {
253  }
254 
259  static inline T *Get(size_t index)
260  {
261  return (T *)BaseStation::Get(index);
262  }
263 
268  static inline T *GetIfValid(size_t index)
269  {
270  return IsValidID(index) ? Get(index) : nullptr;
271  }
272 
278  static inline T *GetByTile(TileIndex tile)
279  {
280  return GetIfValid(GetStationIndex(tile));
281  }
282 
288  static inline T *From(BaseStation *st)
289  {
290  assert(IsExpected(st));
291  return (T *)st;
292  }
293 
299  static inline const T *From(const BaseStation *st)
300  {
301  assert(IsExpected(st));
302  return (const T *)st;
303  }
304 
310  static Pool::IterateWrapper<T> Iterate(size_t from = 0) { return Pool::IterateWrapper<T>(from); }
311 };
312 
313 #endif /* BASE_STATION_BASE_H */
BaseStation::facilities
StationFacility facilities
The facilities that this station has.
Definition: base_station_base.h:75
BaseStation::speclist
std::vector< StationSpecList > speclist
List of rail station specs of this station.
Definition: base_station_base.h:77
StationRect
StationRect - used to track station spread out rectangle - cheaper than scanning whole map.
Definition: base_station_base.h:41
BaseStation::cached_roadstop_cargo_triggers
CargoTypes cached_roadstop_cargo_triggers
NOSAVE: Combined cargo trigger bitmask for road stops.
Definition: base_station_base.h:87
Pool::PoolItem<&_station_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:335
StationSpecList::grfid
uint32_t grfid
GRF ID of this custom station.
Definition: base_station_base.h:24
BaseStation::roadstop_speclist
std::vector< RoadStopSpecList > roadstop_speclist
List of road stop specs of this station.
Definition: base_station_base.h:78
RoadStopSpec
Road stop specification.
Definition: newgrf_roadstop.h:122
BaseStation::GetTileArea
virtual void GetTileArea(TileArea *ta, StationType type) const =0
Get the tile area for a given station type.
BaseStation::waiting_triggers
byte waiting_triggers
Waiting triggers (NewGRF) for this station.
Definition: base_station_base.h:83
BaseStation::town
Town * town
The town this station is associated with.
Definition: base_station_base.h:73
timer_game_calendar.h
BaseStation::GetByTile
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
Definition: base_station_base.h:171
StationSpecList
Definition: base_station_base.h:22
SpecializedStation
Class defining several overloaded accessors so we don't have to cast base stations that often.
Definition: base_station_base.h:222
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Pool::PoolItem<&_station_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:234
_station_pool
StationPool _station_pool
The pool of stations.
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:553
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:307
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
FACIL_NONE
@ FACIL_NONE
The station has no facilities at all.
Definition: station_type.h:51
SpecializedStation::Get
static T * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:259
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:250
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
BaseStation::owner
Owner owner
The owner of this station.
Definition: base_station_base.h:74
BaseStation::TileBelongsToRailStation
virtual bool TileBelongsToRailStation(TileIndex tile) const =0
Check whether a specific tile belongs to this station.
BaseStation::string_id
StringID string_id
Default name (town area) of station.
Definition: base_station_base.h:70
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:310
BaseStation::cached_cargo_triggers
CargoTypes cached_cargo_triggers
NOSAVE: Combined cargo trigger bitmask.
Definition: base_station_base.h:86
StationType
StationType
Station types.
Definition: station_type.h:31
RoadStopSpecList
Definition: base_station_base.h:28
BaseStation::sign
TrackedViewportSign sign
NOSAVE: Dimensions of sign.
Definition: base_station_base.h:66
CommandCost
Common return value for all commands.
Definition: command_type.h:23
BaseStation::train_station
TileArea train_station
Tile area the train 'station' part covers.
Definition: base_station_base.h:89
TrackedViewportSign
Specialised ViewportSign that tracks whether it is valid for entering into a Kdtree.
Definition: viewport_type.h:49
BaseStation::rect
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Definition: base_station_base.h:90
SpecializedStation::IsExpected
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
Definition: base_station_base.h:240
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:18
Pool::IterateWrapper
Definition: pool_type.hpp:174
BaseStation::GetNewGRFVariable
virtual uint32_t GetNewGRFVariable(const struct ResolverObject &object, byte variable, byte parameter, bool *available) const =0
Helper function to get a NewGRF variable that isn't implemented by the base class.
BaseStation::name
std::string name
Custom name.
Definition: base_station_base.h:69
RoadStopTileData
Definition: base_station_base.h:34
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
SpecializedStation::From
static T * From(BaseStation *st)
Converts a BaseStation to SpecializedStation with type checking.
Definition: base_station_base.h:288
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:618
StationFacility
StationFacility
The facilities a station might be having.
Definition: station_type.h:50
FACIL_WAYPOINT
@ FACIL_WAYPOINT
Station is a waypoint.
Definition: station_type.h:57
command_type.h
SpecializedStation::EXPECTED_FACIL
static const StationFacility EXPECTED_FACIL
Specialized type.
Definition: base_station_base.h:223
RoadStopSpecList::grfid
uint32_t grfid
GRF ID of this custom road stop.
Definition: base_station_base.h:30
StationSpec
Station specification.
Definition: newgrf_station.h:112
Pool
Base class for all pools.
Definition: pool_type.hpp:80
GetStationIndex
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition: station_map.h:28
SpecializedStation::GetByTile
static T * GetByTile(TileIndex tile)
Get the station belonging to a specific tile.
Definition: base_station_base.h:278
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:71
BaseStation::BaseStation
BaseStation(TileIndex tile)
Initialize the base station.
Definition: base_station_base.h:98
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:65
BaseStation
Base class for all station-ish types.
Definition: base_station_base.h:64
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:84
BaseStation::delete_ctr
byte 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:67
BaseStation::random_bits
uint16_t random_bits
Random bits assigned to this station.
Definition: base_station_base.h:82
StationSpecList::localidx
uint16_t localidx
Station ID within GRF of station.
Definition: base_station_base.h:25
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:85
station_map.h
Town
Town data structure.
Definition: town.h:50
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
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:268
RoadStopSpecList::localidx
uint16_t localidx
Station ID within GRF of road stop.
Definition: base_station_base.h:31
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:182
pool_type.hpp
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:324
viewport_type.h
BaseStation::GetPlatformLength
virtual uint GetPlatformLength(TileIndex tile) const =0
Obtain the length of a platform.
BaseStation::UpdateVirtCoord
virtual void UpdateVirtCoord()=0
Update the coordinated of the sign (as shown in the viewport).
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
BaseStation::custom_roadstop_tile_data
std::vector< RoadStopTileData > custom_roadstop_tile_data
List of custom road stop tile data.
Definition: base_station_base.h:92
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:233
BaseStation::build_date
TimerGameCalendar::Date build_date
Date of construction.
Definition: base_station_base.h:80