Add a method in NewGRFInspectWindow to resolve FeatureIndex
[openttd/fttd.git] / src / base_station_base.h
blob7646e20c9efdf88dcc531278220eacb7750da3aa
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;
124 * Obtain the length of a platform
125 * @pre tile must be a rail station tile
126 * @param tile A tile that contains the platform in question
127 * @return The length of the platform
129 virtual uint GetPlatformLength(TileIndex tile) const = 0;
132 * Determines the REMAINING length of a platform, starting at (and including)
133 * the given tile.
134 * @param tile the tile from which to start searching. Must be a rail station tile
135 * @param dir The direction in which to search.
136 * @return The platform length
138 virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
141 * Get the base station belonging to a specific tile.
142 * @param tile The tile to get the base station from.
143 * @return the station associated with that tile.
145 static inline BaseStation *GetByTile(TileIndex tile)
147 return BaseStation::Get(GetStationIndex(tile));
151 * Check whether the base station currently is in use; in use means
152 * that it is not scheduled for deletion and that it still has some
153 * facilities left.
154 * @return true if still in use
156 inline bool IsInUse() const
158 return (this->facilities & ~FACIL_WAYPOINT) != 0;
161 static void PostDestructor(size_t index);
164 #define FOR_ALL_BASE_STATIONS(var) FOR_ALL_ITEMS_FROM(BaseStation, station_index, var, 0)
167 * Class defining several overloaded accessors so we don't
168 * have to cast base stations that often
170 template <class T, bool Tis_waypoint>
171 struct SpecializedStation : public BaseStation {
172 static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE; ///< Specialized type
175 * Set station type correctly
176 * @param tile The base tile of the station.
178 inline SpecializedStation<T, Tis_waypoint>(TileIndex tile) :
179 BaseStation(tile)
181 this->facilities = EXPECTED_FACIL;
185 * Helper for checking whether the given station is of this type.
186 * @param st the station to check.
187 * @return true if the station is the type we expect it to be.
189 static inline bool IsExpected(const BaseStation *st)
191 return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
195 * Tests whether given index is a valid index for station of this type
196 * @param index tested index
197 * @return is this index valid index of T?
199 static inline bool IsValidID(size_t index)
201 return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
205 * Gets station with given index
206 * @return pointer to station with given index casted to T *
208 static inline T *Get(size_t index)
210 return (T *)BaseStation::Get(index);
214 * Returns station if the index is a valid index for this station type
215 * @return pointer to station with given index if it's a station of this type
217 static inline T *GetIfValid(size_t index)
219 return IsValidID(index) ? Get(index) : NULL;
223 * Get the station belonging to a specific tile.
224 * @param tile The tile to get the station from.
225 * @return the station associated with that tile.
227 static inline T *GetByTile(TileIndex tile)
229 return GetIfValid(GetStationIndex(tile));
233 * Converts a BaseStation to SpecializedStation with type checking.
234 * @param st BaseStation pointer
235 * @return pointer to SpecializedStation
237 static inline T *From(BaseStation *st)
239 assert(IsExpected(st));
240 return (T *)st;
244 * Converts a const BaseStation to const SpecializedStation with type checking.
245 * @param st BaseStation pointer
246 * @return pointer to SpecializedStation
248 static inline const T *From(const BaseStation *st)
250 assert(IsExpected(st));
251 return (const T *)st;
255 #define FOR_ALL_BASE_STATIONS_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, station_index, var, 0) if (name::IsExpected(var))
257 #endif /* BASE_STATION_BASE_H */