OpenTTD Source  13.2.1
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 
20 
22  const StationSpec *spec;
23  uint32 grfid;
24  uint8 localidx;
25 };
26 
27 
29 struct StationRect : public Rect {
30  enum StationRectMode
31  {
32  ADD_TEST = 0,
33  ADD_TRY,
34  ADD_FORCE
35  };
36 
37  StationRect();
38  void MakeEmpty();
39  bool PtInExtendedRect(int x, int y, int distance = 0) const;
40  bool IsEmpty() const;
41  CommandCost BeforeAddTile(TileIndex tile, StationRectMode mode);
42  CommandCost BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
43  bool AfterRemoveTile(BaseStation *st, TileIndex tile);
44  bool AfterRemoveRect(BaseStation *st, TileArea ta);
45 
46  static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
47 
48  StationRect& operator = (const Rect &src);
49 };
50 
52 struct BaseStation : StationPool::PoolItem<&_station_pool> {
55  byte delete_ctr;
56 
57  std::string name;
59  mutable std::string cached_name;
60 
64 
65  std::vector<StationSpecList> speclist;
66 
68 
69  uint16 random_bits;
72  CargoTypes cached_cargo_triggers;
73 
76 
82  xy(tile),
84  {
85  }
86 
87  virtual ~BaseStation();
88 
94  virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
95 
104  virtual uint32 GetNewGRFVariable(const struct ResolverObject &object, byte variable, byte parameter, bool *available) const = 0;
105 
109  virtual void UpdateVirtCoord() = 0;
110 
111  inline const char *GetCachedName() const
112  {
113  if (!this->name.empty()) return this->name.c_str();
114  if (this->cached_name.empty()) this->FillCachedName();
115  return this->cached_name.c_str();
116  }
117 
118  virtual void MoveSign(TileIndex new_xy)
119  {
120  this->xy = new_xy;
121  this->UpdateVirtCoord();
122  }
123 
129  virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
130 
131 
138  virtual uint GetPlatformLength(TileIndex tile) const = 0;
139 
147  virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
148 
154  static inline BaseStation *GetByTile(TileIndex tile)
155  {
156  return BaseStation::Get(GetStationIndex(tile));
157  }
158 
165  inline bool IsInUse() const
166  {
167  return (this->facilities & ~FACIL_WAYPOINT) != 0;
168  }
169 
170  static void PostDestructor(size_t index);
171 
172 private:
173  void FillCachedName() const;
174 };
175 
180 template <class T, bool Tis_waypoint>
182  static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE;
183 
189  BaseStation(tile)
190  {
191  this->facilities = EXPECTED_FACIL;
192  }
193 
199  static inline bool IsExpected(const BaseStation *st)
200  {
201  return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
202  }
203 
209  static inline bool IsValidID(size_t index)
210  {
212  }
213 
218  static inline T *Get(size_t index)
219  {
220  return (T *)BaseStation::Get(index);
221  }
222 
227  static inline T *GetIfValid(size_t index)
228  {
229  return IsValidID(index) ? Get(index) : nullptr;
230  }
231 
237  static inline T *GetByTile(TileIndex tile)
238  {
239  return GetIfValid(GetStationIndex(tile));
240  }
241 
247  static inline T *From(BaseStation *st)
248  {
249  assert(IsExpected(st));
250  return (T *)st;
251  }
252 
258  static inline const T *From(const BaseStation *st)
259  {
260  assert(IsExpected(st));
261  return (const T *)st;
262  }
263 
269  static Pool::IterateWrapper<T> Iterate(size_t from = 0) { return Pool::IterateWrapper<T>(from); }
270 };
271 
272 #endif /* BASE_STATION_BASE_H */
BaseStation::facilities
StationFacility facilities
The facilities that this station has.
Definition: base_station_base.h:63
BaseStation::speclist
std::vector< StationSpecList > speclist
List of rail station specs of this station.
Definition: base_station_base.h:65
StationRect
StationRect - used to track station spread out rectangle - cheaper than scanning whole map.
Definition: base_station_base.h:29
Pool::PoolItem<&_station_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
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:70
BaseStation::town
Town * town
The town this station is associated with.
Definition: base_station_base.h:61
BaseStation::GetByTile
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
Definition: base_station_base.h:154
StationSpecList
Definition: base_station_base.h:21
SpecializedStation
Class defining several overloaded accessors so we don't have to cast base stations that often.
Definition: base_station_base.h:181
StationSpecList::localidx
uint8 localidx
Station ID within GRF of station.
Definition: base_station_base.h:24
Pool::PoolItem<&_station_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
_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:525
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:307
INVALID_TILE
static constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:108
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
FACIL_NONE
@ FACIL_NONE
The station has no facilities at all.
Definition: station_type.h:52
SpecializedStation::Get
static T * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:218
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:209
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:62
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:58
BaseStation::cached_anim_triggers
uint8 cached_anim_triggers
NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen.
Definition: base_station_base.h:71
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:269
BaseStation::cached_cargo_triggers
CargoTypes cached_cargo_triggers
NOSAVE: Combined cargo trigger bitmask.
Definition: base_station_base.h:72
StationSpecList::grfid
uint32 grfid
GRF ID of this custom station.
Definition: base_station_base.h:23
StationType
StationType
Station types.
Definition: station_type.h:32
BaseStation::sign
TrackedViewportSign sign
NOSAVE: Dimensions of sign.
Definition: base_station_base.h:54
CommandCost
Common return value for all commands.
Definition: command_type.h:24
BaseStation::train_station
TileArea train_station
Tile area the train 'station' part covers.
Definition: base_station_base.h:74
TrackedViewportSign
Specialised ViewportSign that tracks whether it is valid for entering into a Kdtree.
Definition: viewport_type.h:49
BaseStation::random_bits
uint16 random_bits
Random bits assigned to this station.
Definition: base_station_base.h:69
BaseStation::rect
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Definition: base_station_base.h:75
SpecializedStation::IsExpected
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
Definition: base_station_base.h:199
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:18
Pool::IterateWrapper
Definition: pool_type.hpp:175
BaseStation::name
std::string name
Custom name.
Definition: base_station_base.h:57
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
SpecializedStation::From
static T * From(BaseStation *st)
Converts a BaseStation to SpecializedStation with type checking.
Definition: base_station_base.h:247
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:590
StationFacility
StationFacility
The facilities a station might be having.
Definition: station_type.h:51
FACIL_WAYPOINT
@ FACIL_WAYPOINT
Station is a waypoint.
Definition: station_type.h:58
command_type.h
SpecializedStation::EXPECTED_FACIL
static const StationFacility EXPECTED_FACIL
Specialized type.
Definition: base_station_base.h:182
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
StationSpec
Station specification.
Definition: newgrf_station.h:113
Pool
Base class for all pools.
Definition: pool_type.hpp:81
SpecializedStation::GetByTile
static T * GetByTile(TileIndex tile)
Get the station belonging to a specific tile.
Definition: base_station_base.h:237
GetStationIndex
static StationID GetStationIndex(TileIndex t)
Get StationID from a tile.
Definition: station_map.h:28
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:59
BaseStation::BaseStation
BaseStation(TileIndex tile)
Initialize the base station.
Definition: base_station_base.h:81
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:53
BaseStation
Base class for all station-ish types.
Definition: base_station_base.h:52
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:55
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:168
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:227
BaseStation::GetNewGRFVariable
virtual uint32 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::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:165
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:326
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:69
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:234
BaseStation::build_date
Date build_date
Date of construction.
Definition: base_station_base.h:67