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/>.
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"
17 #include "timer/timer_game_calendar.h"
19 typedef Pool
<BaseStation
, StationID
, 32, 64000> StationPool
;
20 extern StationPool _station_pool
;
24 const T
*spec
; ///< Custom spec.
25 uint32_t grfid
; ///< GRF ID of this custom spec.
26 uint16_t localidx
; ///< Local ID within GRF of this custom spec.
29 struct RoadStopTileData
{
32 uint8_t animation_frame
;
35 /** StationRect - used to track station spread out rectangle - cheaper than scanning whole map */
36 struct StationRect
: public Rect
{
46 bool PtInExtendedRect(int x
, int y
, int distance
= 0) const;
48 CommandCost
BeforeAddTile(TileIndex tile
, StationRectMode mode
);
49 CommandCost
BeforeAddRect(TileIndex tile
, int w
, int h
, StationRectMode mode
);
50 bool AfterRemoveTile(BaseStation
*st
, TileIndex tile
);
51 bool AfterRemoveRect(BaseStation
*st
, TileArea ta
);
53 static bool ScanForStationTiles(StationID st_id
, int left_a
, int top_a
, int right_a
, int bottom_a
);
55 StationRect
& operator = (const Rect
&src
);
58 /** Base class for all station-ish types */
59 struct BaseStation
: StationPool::PoolItem
<&_station_pool
> {
60 TileIndex xy
; ///< Base tile of the station
61 TrackedViewportSign sign
; ///< NOSAVE: Dimensions of sign
62 uint8_t delete_ctr
; ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is deleted.
64 std::string name
; ///< Custom name
65 StringID string_id
; ///< Default name (town area) of station
66 mutable std::string cached_name
; ///< NOSAVE: Cache of the resolved name of the station, if not using a custom name
68 Town
*town
; ///< The town this station is associated with
69 Owner owner
; ///< The owner of this station
70 StationFacility facilities
; ///< The facilities that this station has
72 std::vector
<SpecMapping
<StationSpec
>> speclist
; ///< List of rail station specs of this station.
73 std::vector
<SpecMapping
<RoadStopSpec
>> roadstop_speclist
; ///< List of road stop specs of this station
75 TimerGameCalendar::Date build_date
; ///< Date of construction
77 uint16_t random_bits
; ///< Random bits assigned to this station
78 uint8_t waiting_triggers
; ///< Waiting triggers (NewGRF) for this station
79 uint8_t cached_anim_triggers
; ///< NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen.
80 uint8_t cached_roadstop_anim_triggers
; ///< NOSAVE: Combined animation trigger bitmask for road stops, used to determine if trigger processing should happen.
81 CargoTypes cached_cargo_triggers
; ///< NOSAVE: Combined cargo trigger bitmask
82 CargoTypes cached_roadstop_cargo_triggers
; ///< NOSAVE: Combined cargo trigger bitmask for road stops
84 TileArea train_station
; ///< Tile area the train 'station' part covers
85 StationRect rect
; ///< NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions
87 std::vector
<RoadStopTileData
> custom_roadstop_tile_data
; ///< List of custom road stop tile data
90 * Initialize the base station.
91 * @param tile The location of the station sign
93 BaseStation(TileIndex tile
) :
95 train_station(INVALID_TILE
, 0, 0)
99 virtual ~BaseStation();
102 * Check whether a specific tile belongs to this station.
103 * @param tile the tile to check
104 * @return true if the tile belongs to this station
106 virtual bool TileBelongsToRailStation(TileIndex tile
) const = 0;
109 * Helper function to get a NewGRF variable that isn't implemented by the base class.
110 * @param object the resolver object related to this query
111 * @param variable that is queried
112 * @param parameter parameter for that variable
113 * @param available will return false if ever the variable asked for does not exist
114 * @return the value stored in the corresponding variable
116 virtual uint32_t GetNewGRFVariable(const struct ResolverObject
&object
, uint8_t variable
, uint8_t parameter
, bool &available
) const = 0;
119 * Update the coordinated of the sign (as shown in the viewport).
121 virtual void UpdateVirtCoord() = 0;
123 inline const std::string
&GetCachedName() const
125 if (!this->name
.empty()) return this->name
;
126 if (this->cached_name
.empty()) this->FillCachedName();
127 return this->cached_name
;
130 virtual void MoveSign(TileIndex new_xy
)
133 this->UpdateVirtCoord();
137 * Get the tile area for a given station type.
138 * @param ta tile area to fill.
139 * @param type the type of the area
141 virtual void GetTileArea(TileArea
*ta
, StationType type
) const = 0;
145 * Obtain the length of a platform
146 * @pre tile must be a rail station tile
147 * @param tile A tile that contains the platform in question
148 * @return The length of the platform
150 virtual uint
GetPlatformLength(TileIndex tile
) const = 0;
153 * Determines the REMAINING length of a platform, starting at (and including)
155 * @param tile the tile from which to start searching. Must be a rail station tile
156 * @param dir The direction in which to search.
157 * @return The platform length
159 virtual uint
GetPlatformLength(TileIndex tile
, DiagDirection dir
) const = 0;
162 * Get the base station belonging to a specific tile.
163 * @param tile The tile to get the base station from.
164 * @return the station associated with that tile.
166 static inline BaseStation
*GetByTile(TileIndex tile
)
168 return BaseStation::Get(GetStationIndex(tile
));
172 * Check whether the base station currently is in use; in use means
173 * that it is not scheduled for deletion and that it still has some
175 * @return true if still in use
177 inline bool IsInUse() const
179 return (this->facilities
& ~FACIL_WAYPOINT
) != 0;
182 inline uint8_t GetRoadStopRandomBits(TileIndex tile
) const
184 for (const RoadStopTileData
&tile_data
: this->custom_roadstop_tile_data
) {
185 if (tile_data
.tile
== tile
) return tile_data
.random_bits
;
190 inline uint8_t GetRoadStopAnimationFrame(TileIndex tile
) const
192 for (const RoadStopTileData
&tile_data
: this->custom_roadstop_tile_data
) {
193 if (tile_data
.tile
== tile
) return tile_data
.animation_frame
;
199 bool SetRoadStopTileData(TileIndex tile
, uint8_t data
, bool animation
);
202 inline void SetRoadStopRandomBits(TileIndex tile
, uint8_t random_bits
) { this->SetRoadStopTileData(tile
, random_bits
, false); }
203 inline bool SetRoadStopAnimationFrame(TileIndex tile
, uint8_t frame
) { return this->SetRoadStopTileData(tile
, frame
, true); }
204 void RemoveRoadStopTileData(TileIndex tile
);
206 static void PostDestructor(size_t index
);
209 void FillCachedName() const;
213 * Class defining several overloaded accessors so we don't
214 * have to cast base stations that often
216 template <class T
, bool Tis_waypoint
>
217 struct SpecializedStation
: public BaseStation
{
218 static const StationFacility EXPECTED_FACIL
= Tis_waypoint
? FACIL_WAYPOINT
: FACIL_NONE
; ///< Specialized type
221 * Set station type correctly
222 * @param tile The base tile of the station.
224 inline SpecializedStation(TileIndex tile
) :
227 this->facilities
= EXPECTED_FACIL
;
231 * Helper for checking whether the given station is of this type.
232 * @param st the station to check.
233 * @return true if the station is the type we expect it to be.
235 static inline bool IsExpected(const BaseStation
*st
)
237 return (st
->facilities
& FACIL_WAYPOINT
) == EXPECTED_FACIL
;
241 * Tests whether given index is a valid index for station of this type
242 * @param index tested index
243 * @return is this index valid index of T?
245 static inline bool IsValidID(size_t index
)
247 return BaseStation::IsValidID(index
) && IsExpected(BaseStation::Get(index
));
251 * Gets station with given index
252 * @return pointer to station with given index casted to T *
254 static inline T
*Get(size_t index
)
256 return (T
*)BaseStation::Get(index
);
260 * Returns station if the index is a valid index for this station type
261 * @return pointer to station with given index if it's a station of this type
263 static inline T
*GetIfValid(size_t index
)
265 return IsValidID(index
) ? Get(index
) : nullptr;
269 * Get the station belonging to a specific tile.
270 * @param tile The tile to get the station from.
271 * @return the station associated with that tile.
273 static inline T
*GetByTile(TileIndex tile
)
275 return GetIfValid(GetStationIndex(tile
));
279 * Converts a BaseStation to SpecializedStation with type checking.
280 * @param st BaseStation pointer
281 * @return pointer to SpecializedStation
283 static inline T
*From(BaseStation
*st
)
285 assert(IsExpected(st
));
290 * Converts a const BaseStation to const SpecializedStation with type checking.
291 * @param st BaseStation pointer
292 * @return pointer to SpecializedStation
294 static inline const T
*From(const BaseStation
*st
)
296 assert(IsExpected(st
));
297 return (const T
*)st
;
301 * Returns an iterable ensemble of all valid stations of type T
302 * @param from index of the first station to consider
303 * @return an iterable ensemble of all valid stations of type T
305 static Pool::IterateWrapper
<T
> Iterate(size_t from
= 0) { return Pool::IterateWrapper
<T
>(from
); }
309 * Get spec mapping list for each supported custom spec type.
310 * @tparam T Spec type.
311 * @param bst Station of custom spec list.
312 * @return Speclist of custom spec type.
314 template <class T
> std::vector
<SpecMapping
<T
>> &GetStationSpecList(BaseStation
*bst
);
315 template <> inline std::vector
<SpecMapping
<StationSpec
>> &GetStationSpecList
<StationSpec
>(BaseStation
*bst
) { return bst
->speclist
; }
316 template <> inline std::vector
<SpecMapping
<RoadStopSpec
>> &GetStationSpecList
<RoadStopSpec
>(BaseStation
*bst
) { return bst
->roadstop_speclist
; }
318 #endif /* BASE_STATION_BASE_H */