Fix ICU iterators on leading/trailing whitespace
[openttd/fttd.git] / src / base_station_base.h
blob681dcb565eed648726e3dbb1809edd3b968d4798
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 struct StationSpecList {
23 const StationSpec *spec;
24 uint32 grfid; ///< GRF ID of this custom station
25 uint8 localidx; ///< Station ID within GRF of station
29 /** StationRect - used to track station spread out rectangle - cheaper than scanning whole map */
30 struct StationRect : public Rect {
31 enum StationRectMode
33 ADD_TEST = 0,
34 ADD_TRY,
35 ADD_FORCE
38 StationRect();
39 void MakeEmpty();
40 bool PtInExtendedRect(int x, int y, int distance = 0) const;
41 bool IsEmpty() const;
42 CommandCost BeforeAddTile(TileIndex tile, StationRectMode mode);
43 CommandCost BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
44 bool AfterRemoveTile(BaseStation *st, TileIndex tile);
45 bool AfterRemoveRect(BaseStation *st, TileArea ta);
47 static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
49 StationRect& operator = (const Rect &src);
52 /** Base class for all station-ish types */
53 struct BaseStation : PooledItem <BaseStation, StationID, 32, 64000> {
54 TileIndex xy; ///< Base tile of the station
55 ViewportSign sign; ///< NOSAVE: Dimensions of sign
56 byte delete_ctr; ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is deleted.
58 char *name; ///< Custom name
59 StringID string_id; ///< Default name (town area) of station
61 Town *town; ///< The town this station is associated with
62 OwnerByte owner; ///< The owner of this station
63 StationFacilityByte facilities; ///< The facilities that this station has
65 uint8 num_specs; ///< Number of specs in the speclist
66 StationSpecList *speclist; ///< List of station specs of this station
68 Date build_date; ///< Date of construction
70 uint16 random_bits; ///< Random bits assigned to this station
71 byte waiting_triggers; ///< Waiting triggers (NewGRF) for this station
72 uint8 cached_anim_triggers; ///< NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen.
73 uint32 cached_cargo_triggers; ///< NOSAVE: Combined cargo trigger bitmask
75 TileArea train_station; ///< Tile area the train 'station' part covers
76 StationRect rect; ///< NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions
78 /**
79 * Initialize the base station.
80 * @param tile The location of the station sign
82 BaseStation(TileIndex tile) :
83 xy(tile),
84 train_station(INVALID_TILE, 0, 0)
88 virtual ~BaseStation();
90 /**
91 * Check whether a specific tile belongs to this station.
92 * @param tile the tile to check
93 * @return true if the tile belongs to this station
95 virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
97 /**
98 * Helper function to get a NewGRF variable that isn't implemented by the base class.
99 * @param object the resolver object related to this query
100 * @param variable that is queried
101 * @param parameter parameter for that variable
102 * @param available will return false if ever the variable asked for does not exist
103 * @return the value stored in the corresponding variable
105 virtual uint32 GetNewGRFVariable(const struct ResolverObject &object, byte variable, byte parameter, bool *available) const = 0;
108 * Update the coordinated of the sign (as shown in the viewport).
110 virtual void UpdateVirtCoord() = 0;
113 * Get the tile area for a given station type.
114 * @param ta tile area to fill.
115 * @param type the type of the area
117 virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
120 * Calculates the tile of the given station type that is closest to a given tile.
121 * @param tile The tile from where to calculate the distance
122 * @param station_type the station type to get the closest tile of
123 * @return The closest station tile to the given tile.
125 TileIndex GetClosestTile(TileIndex tile, StationType station_type) const
127 TileArea ta;
128 this->GetTileArea(&ta, station_type);
130 /* If the station does not have the given station type, use the station sign */
131 tile = ta.get_closest_tile(tile);
132 return (tile != INVALID_TILE) ? tile : this->xy;
137 * Obtain the length of a platform
138 * @pre tile must be a rail station tile
139 * @param tile A tile that contains the platform in question
140 * @return The length of the platform
142 virtual uint GetPlatformLength(TileIndex tile) const = 0;
145 * Determines the REMAINING length of a platform, starting at (and including)
146 * the given tile.
147 * @param tile the tile from which to start searching. Must be a rail station tile
148 * @param dir The direction in which to search.
149 * @return The platform length
151 virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
154 * Get the base station belonging to a specific tile.
155 * @param tile The tile to get the base station from.
156 * @return the station associated with that tile.
158 static inline BaseStation *GetByTile(TileIndex tile)
160 return BaseStation::Get(GetStationIndex(tile));
164 * Check whether the base station currently is in use; in use means
165 * that it is not scheduled for deletion and that it still has some
166 * facilities left.
167 * @return true if still in use
169 inline bool IsInUse() const
171 return (this->facilities & ~FACIL_WAYPOINT) != 0;
174 static void PostDestructor(size_t index);
177 #define FOR_ALL_BASE_STATIONS(var) FOR_ALL_ITEMS_FROM(BaseStation, station_index, var, 0)
180 * Class defining several overloaded accessors so we don't
181 * have to cast base stations that often
183 template <class T, bool Tis_waypoint>
184 struct SpecializedStation : public BaseStation {
185 static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE; ///< Specialized type
188 * Set station type correctly
189 * @param tile The base tile of the station.
191 inline SpecializedStation<T, Tis_waypoint>(TileIndex tile) :
192 BaseStation(tile)
194 this->facilities = EXPECTED_FACIL;
198 * Helper for checking whether the given station is of this type.
199 * @param st the station to check.
200 * @return true if the station is the type we expect it to be.
202 static inline bool IsExpected(const BaseStation *st)
204 return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
208 * Tests whether given index is a valid index for station of this type
209 * @param index tested index
210 * @return is this index valid index of T?
212 static inline bool IsValidID(size_t index)
214 return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
218 * Gets station with given index
219 * @return pointer to station with given index casted to T *
221 static inline T *Get(size_t index)
223 return (T *)BaseStation::Get(index);
227 * Returns station if the index is a valid index for this station type
228 * @return pointer to station with given index if it's a station of this type
230 static inline T *GetIfValid(size_t index)
232 return IsValidID(index) ? Get(index) : NULL;
236 * Get the station belonging to a specific tile.
237 * @param tile The tile to get the station from.
238 * @return the station associated with that tile.
240 static inline T *GetByTile(TileIndex tile)
242 return GetIfValid(GetStationIndex(tile));
246 * Converts a BaseStation to SpecializedStation with type checking.
247 * @param st BaseStation pointer
248 * @return pointer to SpecializedStation
250 static inline T *From(BaseStation *st)
252 assert(IsExpected(st));
253 return (T *)st;
257 * Converts a const BaseStation to const SpecializedStation with type checking.
258 * @param st BaseStation pointer
259 * @return pointer to SpecializedStation
261 static inline const T *From(const BaseStation *st)
263 assert(IsExpected(st));
264 return (const T *)st;
268 #define FOR_ALL_BASE_STATIONS_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, station_index, var, 0) if (name::IsExpected(var))
270 #endif /* BASE_STATION_BASE_H */