OpenTTD
base_station_base.h
Go to the documentation of this file.
1 /* $Id: base_station_base.h 26085 2013-11-24 14:41:19Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef BASE_STATION_BASE_H
13 #define BASE_STATION_BASE_H
14 
15 #include "core/pool_type.hpp"
16 #include "command_type.h"
17 #include "viewport_type.h"
18 #include "station_map.h"
19 
22 
24  const StationSpec *spec;
25  uint32 grfid;
26  uint8 localidx;
27 };
28 
29 
31 struct StationRect : public Rect {
32  enum StationRectMode
33  {
34  ADD_TEST = 0,
35  ADD_TRY,
36  ADD_FORCE
37  };
38 
39  StationRect();
40  void MakeEmpty();
41  bool PtInExtendedRect(int x, int y, int distance = 0) const;
42  bool IsEmpty() const;
43  CommandCost BeforeAddTile(TileIndex tile, StationRectMode mode);
44  CommandCost BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
45  bool AfterRemoveTile(BaseStation *st, TileIndex tile);
46  bool AfterRemoveRect(BaseStation *st, TileArea ta);
47 
48  static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
49 
50  StationRect& operator = (const Rect &src);
51 };
52 
54 struct BaseStation : StationPool::PoolItem<&_station_pool> {
57  byte delete_ctr;
58 
59  char *name;
61 
65 
66  uint8 num_specs;
68 
70 
71  uint16 random_bits;
75 
78 
84  xy(tile),
85  train_station(INVALID_TILE, 0, 0)
86  {
87  }
88 
89  virtual ~BaseStation();
90 
96  virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
97 
106  virtual uint32 GetNewGRFVariable(const struct ResolverObject &object, byte variable, byte parameter, bool *available) const = 0;
107 
111  virtual void UpdateVirtCoord() = 0;
112 
118  virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
119 
120 
127  virtual uint GetPlatformLength(TileIndex tile) const = 0;
128 
136  virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
137 
143  static inline BaseStation *GetByTile(TileIndex tile)
144  {
145  return BaseStation::Get(GetStationIndex(tile));
146  }
147 
154  inline bool IsInUse() const
155  {
156  return (this->facilities & ~FACIL_WAYPOINT) != 0;
157  }
158 
159  static void PostDestructor(size_t index);
160 };
161 
162 #define FOR_ALL_BASE_STATIONS(var) FOR_ALL_ITEMS_FROM(BaseStation, station_index, var, 0)
163 
168 template <class T, bool Tis_waypoint>
170  static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE;
171 
177  BaseStation(tile)
178  {
179  this->facilities = EXPECTED_FACIL;
180  }
181 
187  static inline bool IsExpected(const BaseStation *st)
188  {
189  return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
190  }
191 
197  static inline bool IsValidID(size_t index)
198  {
199  return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
200  }
201 
206  static inline T *Get(size_t index)
207  {
208  return (T *)BaseStation::Get(index);
209  }
210 
215  static inline T *GetIfValid(size_t index)
216  {
217  return IsValidID(index) ? Get(index) : NULL;
218  }
219 
225  static inline T *GetByTile(TileIndex tile)
226  {
227  return GetIfValid(GetStationIndex(tile));
228  }
229 
235  static inline T *From(BaseStation *st)
236  {
237  assert(IsExpected(st));
238  return (T *)st;
239  }
240 
246  static inline const T *From(const BaseStation *st)
247  {
248  assert(IsExpected(st));
249  return (const T *)st;
250  }
251 };
252 
253 #define FOR_ALL_BASE_STATIONS_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, station_index, var, 0) if (name::IsExpected(var))
254 
255 #endif /* BASE_STATION_BASE_H */
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:246
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...
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.
StationFacility
The facilities a station might be having.
Definition: station_type.h:53
BaseStation(TileIndex tile)
Initialize the base station.
StationPool _station_pool
The pool of stations.
Common return value for all commands.
Definition: command_type.h:25
Town * town
The town this station is associated with.
Defintion 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 ...
Types related to viewports.
Station is a waypoint.
Definition: station_type.h:60
ViewportSign sign
NOSAVE: Dimensions of sign.
Location information about a sign as seen on the viewport.
Definition: viewport_type.h:48
Represents the covered area of e.g.
Definition: tilearea_type.h:18
StationSpecList * speclist
List of station specs of this station.
StationRect - used to track station spread out rectangle - cheaper than scanning whole map...
StationFacilityByte facilities
The facilities that this station has.
DiagDirection
Enumeration for diagonal directions.
uint8 cached_anim_triggers
NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen...
uint32 cached_cargo_triggers
NOSAVE: Combined cargo trigger bitmask.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
Base class for all PoolItems.
Definition: pool_type.hpp:146
Base class for all pools.
Definition: pool_type.hpp:83
OwnerByte owner
The owner of this station.
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:29
StationType
Station types.
Definition: station_type.h:35
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
TileIndex xy
Base tile of the station.
Town data structure.
Definition: town.h:55
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.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
uint16 random_bits
Random bits assigned to this station.
int32 Date
The type to store our dates in.
Definition: date_type.h:16
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
Specification of a rectangle with absolute coordinates of all edges.
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.
The station has no facilities at all.
Definition: station_type.h:54
Class defining several overloaded accessors so we don&#39;t have to cast base stations that often...
Date build_date
Date of construction.