OpenTTD
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  char *name;
59 
63 
64  uint8 num_specs;
66 
68 
69  uint16 random_bits;
72  CargoTypes cached_cargo_triggers;
73 
76 
82  xy(tile),
83  train_station(INVALID_TILE, 0, 0)
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  virtual void MoveSign(TileIndex new_xy)
112  {
113  this->xy = new_xy;
114  this->UpdateVirtCoord();
115  }
116 
122  virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
123 
124 
131  virtual uint GetPlatformLength(TileIndex tile) const = 0;
132 
140  virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
141 
147  static inline BaseStation *GetByTile(TileIndex tile)
148  {
149  return BaseStation::Get(GetStationIndex(tile));
150  }
151 
158  inline bool IsInUse() const
159  {
160  return (this->facilities & ~FACIL_WAYPOINT) != 0;
161  }
162 
163  static void PostDestructor(size_t index);
164 };
165 
170 template <class T, bool Tis_waypoint>
172  static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE;
173 
179  BaseStation(tile)
180  {
181  this->facilities = EXPECTED_FACIL;
182  }
183 
189  static inline bool IsExpected(const BaseStation *st)
190  {
191  return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
192  }
193 
199  static inline bool IsValidID(size_t index)
200  {
201  return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
202  }
203 
208  static inline T *Get(size_t index)
209  {
210  return (T *)BaseStation::Get(index);
211  }
212 
217  static inline T *GetIfValid(size_t index)
218  {
219  return IsValidID(index) ? Get(index) : nullptr;
220  }
221 
227  static inline T *GetByTile(TileIndex tile)
228  {
229  return GetIfValid(GetStationIndex(tile));
230  }
231 
237  static inline T *From(BaseStation *st)
238  {
239  assert(IsExpected(st));
240  return (T *)st;
241  }
242 
248  static inline const T *From(const BaseStation *st)
249  {
250  assert(IsExpected(st));
251  return (const T *)st;
252  }
253 
259  static Pool::IterateWrapper<T> Iterate(size_t from = 0) { return Pool::IterateWrapper<T>(from); }
260 };
261 
262 #endif /* BASE_STATION_BASE_H */
Owner
Enum for all companies/owners.
Definition: company_type.h:18
StationFacility facilities
The facilities that this station has.
Maps accessors for stations.
uint8 num_specs
Number of specs in the speclist.
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
Interface for SpriteGroup-s to access the gamestate.
Station specification.
bool IsInUse() const
Check whether the base station currently is in use; in use means that it is not scheduled for deletio...
The station has no facilities at all.
Definition: station_type.h:51
TileArea train_station
Tile area the train &#39;station&#39; part covers.
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
BaseStation(TileIndex tile)
Initialize the base station.
StationPool _station_pool
The pool of stations.
static Pool::IterateWrapper< T > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
Common return value for all commands.
Definition: command_type.h:23
Town * town
The town this station is associated with.
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle...
Types related to commands.
byte delete_ctr
Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is ...
Specialised ViewportSign that tracks whether it is valid for entering into a Kdtree.
Definition: viewport_type.h:57
Types related to viewports.
Represents the covered area of e.g.
Definition: tilearea_type.h:16
StationSpecList * speclist
List of station specs of this station.
StationRect - used to track station spread out rectangle - cheaper than scanning whole map...
uint8 cached_anim_triggers
NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen...
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Base class for all PoolItems.
Definition: pool_type.hpp:188
Base class for all pools.
Definition: pool_type.hpp:82
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
static StationID GetStationIndex(TileIndex t)
Get StationID from a tile.
Definition: station_map.h:28
StationType
Station types.
Definition: station_type.h:32
StationFacility
The facilities a station might be having.
Definition: station_type.h:50
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
Station is a waypoint.
Definition: station_type.h:57
CargoTypes cached_cargo_triggers
NOSAVE: Combined cargo trigger bitmask.
TileIndex xy
Base tile of the station.
Town data structure.
Definition: town.h:53
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
uint8 localidx
Station ID within GRF of station.
Owner owner
The owner of this station.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
uint16 random_bits
Random bits assigned to this station.
int32 Date
The type to store our dates in.
Definition: date_type.h:14
DiagDirection
Enumeration for diagonal directions.
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
Specification of a rectangle with absolute coordinates of all edges.
TrackedViewportSign sign
NOSAVE: Dimensions of sign.
byte waiting_triggers
Waiting triggers (NewGRF) for this station.
char * name
Custom name.
StringID string_id
Default name (town area) of station.
Base class for all station-ish types.
uint32 grfid
GRF ID of this custom station.
Class defining several overloaded accessors so we don&#39;t have to cast base stations that often...
Date build_date
Date of construction.