OpenTTD Source  1.11.0-beta2
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  uint8 num_specs;
67 
69 
70  uint16 random_bits;
73  CargoTypes cached_cargo_triggers;
74 
77 
83  xy(tile),
85  {
86  }
87 
88  virtual ~BaseStation();
89 
95  virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
96 
105  virtual uint32 GetNewGRFVariable(const struct ResolverObject &object, byte variable, byte parameter, bool *available) const = 0;
106 
110  virtual void UpdateVirtCoord() = 0;
111 
112  inline const char *GetCachedName() const
113  {
114  if (!this->name.empty()) return this->name.c_str();
115  if (this->cached_name.empty()) this->FillCachedName();
116  return this->cached_name.c_str();
117  }
118 
119  virtual void MoveSign(TileIndex new_xy)
120  {
121  this->xy = new_xy;
122  this->UpdateVirtCoord();
123  }
124 
130  virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
131 
132 
139  virtual uint GetPlatformLength(TileIndex tile) const = 0;
140 
148  virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
149 
155  static inline BaseStation *GetByTile(TileIndex tile)
156  {
157  return BaseStation::Get(GetStationIndex(tile));
158  }
159 
166  inline bool IsInUse() const
167  {
168  return (this->facilities & ~FACIL_WAYPOINT) != 0;
169  }
170 
171  static void PostDestructor(size_t index);
172 
173 private:
174  void FillCachedName() const;
175 };
176 
181 template <class T, bool Tis_waypoint>
183  static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE;
184 
190  BaseStation(tile)
191  {
192  this->facilities = EXPECTED_FACIL;
193  }
194 
200  static inline bool IsExpected(const BaseStation *st)
201  {
202  return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
203  }
204 
210  static inline bool IsValidID(size_t index)
211  {
213  }
214 
219  static inline T *Get(size_t index)
220  {
221  return (T *)BaseStation::Get(index);
222  }
223 
228  static inline T *GetIfValid(size_t index)
229  {
230  return IsValidID(index) ? Get(index) : nullptr;
231  }
232 
238  static inline T *GetByTile(TileIndex tile)
239  {
240  return GetIfValid(GetStationIndex(tile));
241  }
242 
248  static inline T *From(BaseStation *st)
249  {
250  assert(IsExpected(st));
251  return (T *)st;
252  }
253 
259  static inline const T *From(const BaseStation *st)
260  {
261  assert(IsExpected(st));
262  return (const T *)st;
263  }
264 
270  static Pool::IterateWrapper<T> Iterate(size_t from = 0) { return Pool::IterateWrapper<T>(from); }
271 };
272 
273 #endif /* BASE_STATION_BASE_H */
BaseStation::facilities
StationFacility facilities
The facilities that this station has.
Definition: base_station_base.h:63
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
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:329
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:71
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:155
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:182
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:227
_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:502
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:315
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:219
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:210
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::num_specs
uint8 num_specs
Number of specs in the speclist.
Definition: base_station_base.h:65
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:72
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:270
BaseStation::cached_cargo_triggers
CargoTypes cached_cargo_triggers
NOSAVE: Combined cargo trigger bitmask.
Definition: base_station_base.h:73
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:23
BaseStation::train_station
TileArea train_station
Tile area the train 'station' part covers.
Definition: base_station_base.h:75
TrackedViewportSign
Specialised ViewportSign that tracks whether it is valid for entering into a Kdtree.
Definition: viewport_type.h:57
BaseStation::random_bits
uint16 random_bits
Random bits assigned to this station.
Definition: base_station_base.h:70
BaseStation::rect
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Definition: base_station_base.h:76
SpecializedStation::IsExpected
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
Definition: base_station_base.h:200
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:16
Pool::IterateWrapper
Definition: pool_type.hpp:171
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:248
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:567
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:183
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:117
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:238
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:82
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
BaseStation::speclist
StationSpecList * speclist
List of station specs of this station.
Definition: base_station_base.h:66
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:170
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:228
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
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:166
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:318
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:47
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:226
BaseStation::build_date
Date build_date
Date of construction.
Definition: base_station_base.h:68