Change: Let AI developers edit non-editable AI/Game Script Parameters (#8895)
[openttd-github.git] / src / base_station_base.h
blob2ad09ca21cf39ea4b1e7dbfd3d1f881f608b75f5
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file base_station_base.h Base classes/functions for base stations. */
10 #ifndef BASE_STATION_BASE_H
11 #define BASE_STATION_BASE_H
13 #include "core/pool_type.hpp"
14 #include "command_type.h"
15 #include "viewport_type.h"
16 #include "station_map.h"
18 typedef Pool<BaseStation, StationID, 32, 64000> StationPool;
19 extern StationPool _station_pool;
21 struct StationSpecList {
22 const StationSpec *spec;
23 uint32 grfid; ///< GRF ID of this custom station
24 uint8 localidx; ///< Station ID within GRF of station
28 /** StationRect - used to track station spread out rectangle - cheaper than scanning whole map */
29 struct StationRect : public Rect {
30 enum StationRectMode
32 ADD_TEST = 0,
33 ADD_TRY,
34 ADD_FORCE
37 StationRect();
38 void MakeEmpty();
39 bool PtInExtendedRect(int x, int y, int distance = 0) const;
40 bool IsEmpty() const;
41 CommandCost BeforeAddTile(TileIndex tile, StationRectMode mode);
42 CommandCost BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
43 bool AfterRemoveTile(BaseStation *st, TileIndex tile);
44 bool AfterRemoveRect(BaseStation *st, TileArea ta);
46 static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
48 StationRect& operator = (const Rect &src);
51 /** Base class for all station-ish types */
52 struct BaseStation : StationPool::PoolItem<&_station_pool> {
53 TileIndex xy; ///< Base tile of the station
54 TrackedViewportSign sign; ///< NOSAVE: Dimensions of sign
55 byte delete_ctr; ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is deleted.
57 std::string name; ///< Custom name
58 StringID string_id; ///< Default name (town area) of station
59 mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the station, if not using a custom name
61 Town *town; ///< The town this station is associated with
62 Owner owner; ///< The owner of this station
63 StationFacility facilities; ///< The facilities that this station has
65 std::vector<StationSpecList> speclist; ///< List of rail station specs of this station.
67 Date build_date; ///< Date of construction
69 uint16 random_bits; ///< Random bits assigned to this station
70 byte waiting_triggers; ///< Waiting triggers (NewGRF) for this station
71 uint8 cached_anim_triggers; ///< NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen.
72 CargoTypes cached_cargo_triggers; ///< NOSAVE: Combined cargo trigger bitmask
74 TileArea train_station; ///< Tile area the train 'station' part covers
75 StationRect rect; ///< NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions
77 /**
78 * Initialize the base station.
79 * @param tile The location of the station sign
81 BaseStation(TileIndex tile) :
82 xy(tile),
83 train_station(INVALID_TILE, 0, 0)
87 virtual ~BaseStation();
89 /**
90 * Check whether a specific tile belongs to this station.
91 * @param tile the tile to check
92 * @return true if the tile belongs to this station
94 virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
96 /**
97 * Helper function to get a NewGRF variable that isn't implemented by the base class.
98 * @param object the resolver object related to this query
99 * @param variable that is queried
100 * @param parameter parameter for that variable
101 * @param available will return false if ever the variable asked for does not exist
102 * @return the value stored in the corresponding variable
104 virtual uint32 GetNewGRFVariable(const struct ResolverObject &object, byte variable, byte parameter, bool *available) const = 0;
107 * Update the coordinated of the sign (as shown in the viewport).
109 virtual void UpdateVirtCoord() = 0;
111 inline const char *GetCachedName() const
113 if (!this->name.empty()) return this->name.c_str();
114 if (this->cached_name.empty()) this->FillCachedName();
115 return this->cached_name.c_str();
118 virtual void MoveSign(TileIndex new_xy)
120 this->xy = new_xy;
121 this->UpdateVirtCoord();
125 * Get the tile area for a given station type.
126 * @param ta tile area to fill.
127 * @param type the type of the area
129 virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
133 * Obtain the length of a platform
134 * @pre tile must be a rail station tile
135 * @param tile A tile that contains the platform in question
136 * @return The length of the platform
138 virtual uint GetPlatformLength(TileIndex tile) const = 0;
141 * Determines the REMAINING length of a platform, starting at (and including)
142 * the given tile.
143 * @param tile the tile from which to start searching. Must be a rail station tile
144 * @param dir The direction in which to search.
145 * @return The platform length
147 virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
150 * Get the base station belonging to a specific tile.
151 * @param tile The tile to get the base station from.
152 * @return the station associated with that tile.
154 static inline BaseStation *GetByTile(TileIndex tile)
156 return BaseStation::Get(GetStationIndex(tile));
160 * Check whether the base station currently is in use; in use means
161 * that it is not scheduled for deletion and that it still has some
162 * facilities left.
163 * @return true if still in use
165 inline bool IsInUse() const
167 return (this->facilities & ~FACIL_WAYPOINT) != 0;
170 static void PostDestructor(size_t index);
172 private:
173 void FillCachedName() const;
177 * Class defining several overloaded accessors so we don't
178 * have to cast base stations that often
180 template <class T, bool Tis_waypoint>
181 struct SpecializedStation : public BaseStation {
182 static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE; ///< Specialized type
185 * Set station type correctly
186 * @param tile The base tile of the station.
188 inline SpecializedStation<T, Tis_waypoint>(TileIndex tile) :
189 BaseStation(tile)
191 this->facilities = EXPECTED_FACIL;
195 * Helper for checking whether the given station is of this type.
196 * @param st the station to check.
197 * @return true if the station is the type we expect it to be.
199 static inline bool IsExpected(const BaseStation *st)
201 return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
205 * Tests whether given index is a valid index for station of this type
206 * @param index tested index
207 * @return is this index valid index of T?
209 static inline bool IsValidID(size_t index)
211 return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
215 * Gets station with given index
216 * @return pointer to station with given index casted to T *
218 static inline T *Get(size_t index)
220 return (T *)BaseStation::Get(index);
224 * Returns station if the index is a valid index for this station type
225 * @return pointer to station with given index if it's a station of this type
227 static inline T *GetIfValid(size_t index)
229 return IsValidID(index) ? Get(index) : nullptr;
233 * Get the station belonging to a specific tile.
234 * @param tile The tile to get the station from.
235 * @return the station associated with that tile.
237 static inline T *GetByTile(TileIndex tile)
239 return GetIfValid(GetStationIndex(tile));
243 * Converts a BaseStation to SpecializedStation with type checking.
244 * @param st BaseStation pointer
245 * @return pointer to SpecializedStation
247 static inline T *From(BaseStation *st)
249 assert(IsExpected(st));
250 return (T *)st;
254 * Converts a const BaseStation to const SpecializedStation with type checking.
255 * @param st BaseStation pointer
256 * @return pointer to SpecializedStation
258 static inline const T *From(const BaseStation *st)
260 assert(IsExpected(st));
261 return (const T *)st;
265 * Returns an iterable ensemble of all valid stations of type T
266 * @param from index of the first station to consider
267 * @return an iterable ensemble of all valid stations of type T
269 static Pool::IterateWrapper<T> Iterate(size_t from = 0) { return Pool::IterateWrapper<T>(from); }
272 #endif /* BASE_STATION_BASE_H */