Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / base_station_base.h
blob2fb0be860f42893e04789d5ec9443a8c95125ec1
1 /* $Id$ */
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 */
10 /** @file base_station_base.h Base classes/functions for base stations. */
12 #ifndef BASE_STATION_BASE_H
13 #define BASE_STATION_BASE_H
15 #include "core/pool_type.hpp"
16 #include "core/geometry_type.hpp"
17 #include "command_type.h"
18 #include "viewport_type.h"
19 #include "station_type.h"
20 #include "map/station.h"
22 typedef Pool<BaseStation, StationID, 32, 64000> StationPool;
23 extern StationPool _station_pool;
25 struct StationSpecList {
26 const StationSpec *spec;
27 uint32 grfid; ///< GRF ID of this custom station
28 uint8 localidx; ///< Station ID within GRF of station
32 /** StationRect - used to track station spread out rectangle - cheaper than scanning whole map */
33 struct StationRect : public Rect {
34 enum StationRectMode
36 ADD_TEST = 0,
37 ADD_TRY,
38 ADD_FORCE
41 StationRect();
42 void MakeEmpty();
43 bool PtInExtendedRect(int x, int y, int distance = 0) const;
44 bool IsEmpty() const;
45 CommandCost BeforeAddTile(TileIndex tile, StationRectMode mode);
46 CommandCost BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
47 bool AfterRemoveTile(BaseStation *st, TileIndex tile);
48 bool AfterRemoveRect(BaseStation *st, TileArea ta);
50 static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
52 StationRect& operator = (const Rect &src);
55 /** Base class for all station-ish types */
56 struct BaseStation : StationPool::PoolItem<&_station_pool> {
57 TileIndex xy; ///< Base tile of the station
58 ViewportSign sign; ///< NOSAVE: Dimensions of sign
59 byte delete_ctr; ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is deleted.
61 char *name; ///< Custom name
62 StringID string_id; ///< Default name (town area) of station
64 Town *town; ///< The town this station is associated with
65 OwnerByte owner; ///< The owner of this station
66 StationFacilityByte facilities; ///< The facilities that this station has
68 uint8 num_specs; ///< Number of specs in the speclist
69 StationSpecList *speclist; ///< List of station specs of this station
71 Date build_date; ///< Date of construction
73 uint16 random_bits; ///< Random bits assigned to this station
74 byte waiting_triggers; ///< Waiting triggers (NewGRF) for this station
75 uint8 cached_anim_triggers; ///< NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen.
76 uint32 cached_cargo_triggers; ///< NOSAVE: Combined cargo trigger bitmask
78 TileArea train_station; ///< Tile area the train 'station' part covers
79 StationRect rect; ///< NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions
81 /**
82 * Initialize the base station.
83 * @param tile The location of the station sign
85 BaseStation(TileIndex tile) :
86 xy(tile),
87 train_station(INVALID_TILE, 0, 0)
91 virtual ~BaseStation();
93 /**
94 * Check whether a specific tile belongs to this station.
95 * @param tile the tile to check
96 * @return true if the tile belongs to this station
98 virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
101 * Helper function to get a NewGRF variable that isn't implemented by the base class.
102 * @param object the resolver object related to this query
103 * @param variable that is queried
104 * @param parameter parameter for that variable
105 * @param available will return false if ever the variable asked for does not exist
106 * @return the value stored in the corresponding variable
108 virtual uint32 GetNewGRFVariable(const struct ResolverObject &object, byte variable, byte parameter, bool *available) const = 0;
111 * Update the coordinated of the sign (as shown in the viewport).
113 virtual void UpdateVirtCoord() = 0;
116 * Get the tile area for a given station type.
117 * @param ta tile area to fill.
118 * @param type the type of the area
120 virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
123 * Calculates the tile of the given station type that is closest to a given tile.
124 * @param tile The tile from where to calculate the distance
125 * @param station_type the station type to get the closest tile of
126 * @return The closest station tile to the given tile.
128 TileIndex GetClosestTile(TileIndex tile, StationType station_type) const
130 TileArea ta;
131 this->GetTileArea(&ta, station_type);
133 /* If the station does not have the given station type, use the station sign */
134 tile = ta.get_closest_tile(tile);
135 return (tile != INVALID_TILE) ? tile : this->xy;
140 * Obtain the length of a platform
141 * @pre tile must be a rail station tile
142 * @param tile A tile that contains the platform in question
143 * @return The length of the platform
145 virtual uint GetPlatformLength(TileIndex tile) const = 0;
148 * Determines the REMAINING length of a platform, starting at (and including)
149 * the given tile.
150 * @param tile the tile from which to start searching. Must be a rail station tile
151 * @param dir The direction in which to search.
152 * @return The platform length
154 virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
157 * Get the base station belonging to a specific tile.
158 * @param tile The tile to get the base station from.
159 * @return the station associated with that tile.
161 static inline BaseStation *GetByTile(TileIndex tile)
163 return BaseStation::Get(GetStationIndex(tile));
167 * Check whether the base station currently is in use; in use means
168 * that it is not scheduled for deletion and that it still has some
169 * facilities left.
170 * @return true if still in use
172 inline bool IsInUse() const
174 return (this->facilities & ~FACIL_WAYPOINT) != 0;
177 static void PostDestructor(size_t index);
180 #define FOR_ALL_BASE_STATIONS(var) FOR_ALL_ITEMS_FROM(BaseStation, station_index, var, 0)
183 * Class defining several overloaded accessors so we don't
184 * have to cast base stations that often
186 template <class T, bool Tis_waypoint>
187 struct SpecializedStation : public BaseStation {
188 static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE; ///< Specialized type
191 * Set station type correctly
192 * @param tile The base tile of the station.
194 inline SpecializedStation<T, Tis_waypoint>(TileIndex tile) :
195 BaseStation(tile)
197 this->facilities = EXPECTED_FACIL;
201 * Helper for checking whether the given station is of this type.
202 * @param st the station to check.
203 * @return true if the station is the type we expect it to be.
205 static inline bool IsExpected(const BaseStation *st)
207 return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
211 * Tests whether given index is a valid index for station of this type
212 * @param index tested index
213 * @return is this index valid index of T?
215 static inline bool IsValidID(size_t index)
217 return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
221 * Gets station with given index
222 * @return pointer to station with given index casted to T *
224 static inline T *Get(size_t index)
226 return (T *)BaseStation::Get(index);
230 * Returns station if the index is a valid index for this station type
231 * @return pointer to station with given index if it's a station of this type
233 static inline T *GetIfValid(size_t index)
235 return IsValidID(index) ? Get(index) : NULL;
239 * Get the station belonging to a specific tile.
240 * @param tile The tile to get the station from.
241 * @return the station associated with that tile.
243 static inline T *GetByTile(TileIndex tile)
245 return GetIfValid(GetStationIndex(tile));
249 * Converts a BaseStation to SpecializedStation with type checking.
250 * @param st BaseStation pointer
251 * @return pointer to SpecializedStation
253 static inline T *From(BaseStation *st)
255 assert(IsExpected(st));
256 return (T *)st;
260 * Converts a const BaseStation to const SpecializedStation with type checking.
261 * @param st BaseStation pointer
262 * @return pointer to SpecializedStation
264 static inline const T *From(const BaseStation *st)
266 assert(IsExpected(st));
267 return (const T *)st;
271 #define FOR_ALL_BASE_STATIONS_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, station_index, var, 0) if (name::IsExpected(var))
273 #endif /* BASE_STATION_BASE_H */