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),
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 */