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/>.
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 "command_type.h"
17 #include "viewport_type.h"
18 #include "station_type.h"
19 #include "map/station.h"
20 #include "map/tilearea.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 /** Base class for all station-ish types */
30 struct BaseStation
: PooledItem
<BaseStation
, StationID
, 32, 64000> {
31 TileIndex xy
; ///< Base tile of the station
32 ViewportSign sign
; ///< NOSAVE: Dimensions of sign
33 byte delete_ctr
; ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is deleted.
35 char *name
; ///< Custom name
36 StringID string_id
; ///< Default name (town area) of station
38 Town
*town
; ///< The town this station is associated with
39 OwnerByte owner
; ///< The owner of this station
40 StationFacilityByte facilities
; ///< The facilities that this station has
42 uint8 num_specs
; ///< Number of specs in the speclist
43 StationSpecList
*speclist
; ///< List of station specs of this station
45 Date build_date
; ///< Date of construction
47 uint16 random_bits
; ///< Random bits assigned to this station
48 byte waiting_triggers
; ///< Waiting triggers (NewGRF) for this station
49 uint8 cached_anim_triggers
; ///< NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen.
50 uint32 cached_cargo_triggers
; ///< NOSAVE: Combined cargo trigger bitmask
52 TileArea train_station
; ///< Tile area the train 'station' part covers
53 TileArea rect
; ///< NOSAVE: Station spread out rectangle
56 * Initialize the base station.
57 * @param tile The location of the station sign
59 BaseStation(TileIndex tile
) :
61 train_station(INVALID_TILE
, 0, 0)
65 virtual ~BaseStation();
68 * Check whether a specific tile belongs to this station.
69 * @param tile the tile to check
70 * @return true if the tile belongs to this station
72 bool TileBelongsToStation (TileIndex tile
) const
74 return IsStationTile(tile
) && GetStationIndex(tile
) == this->index
;
78 * Check whether a specific tile belongs to this rail station.
79 * @param tile the tile to check
80 * @return true if the tile belongs to this station
82 bool TileBelongsToRailStation(TileIndex tile
) const
84 return HasStationTileRail(tile
) && GetStationIndex(tile
) == this->index
;
88 * Helper function to get a NewGRF variable that isn't implemented by the base class.
89 * @param grffile GRF file related to this query
90 * @param variable that is queried
91 * @param parameter parameter for that variable
92 * @param available will return false if ever the variable asked for does not exist
93 * @return the value stored in the corresponding variable
95 virtual uint32
GetNewGRFVariable (const struct GRFFile
*grffile
, byte variable
, byte parameter
, bool *available
) const = 0;
98 * Update the coordinated of the sign (as shown in the viewport).
100 virtual void UpdateVirtCoord() = 0;
103 * Get the tile area for a given station type.
104 * @param ta tile area to fill.
105 * @param type the type of the area
107 virtual void GetTileArea(TileArea
*ta
, StationType type
) const = 0;
109 /* Test if adding an area would exceed the maximum station spread. */
110 bool TestAddRect (const TileArea
&ta
);
112 /* Update station area after removing a rectangle. */
113 void AfterRemoveRect (const TileArea
&ta
);
115 /** Update station area after removing a tile. */
116 void AfterRemoveTile (TileIndex tile
)
118 this->AfterRemoveRect (TileArea(tile
));
122 * Calculates the tile of the given station type that is closest to a given tile.
123 * @param tile The tile from where to calculate the distance
124 * @param station_type the station type to get the closest tile of
125 * @return The closest station tile to the given tile.
127 TileIndex
GetClosestTile(TileIndex tile
, StationType station_type
) const
130 this->GetTileArea(&ta
, station_type
);
132 /* If the station does not have the given station type, use the station sign */
133 tile
= ta
.get_closest_tile(tile
);
134 return (tile
!= INVALID_TILE
) ? tile
: this->xy
;
139 * Obtain the length of a platform
140 * @pre tile must be a rail station tile
141 * @param tile A tile that contains the platform in question
142 * @return The length of the platform
144 virtual uint
GetPlatformLength(TileIndex tile
) const = 0;
147 * Determines the REMAINING length of a platform, starting at (and including)
149 * @param tile the tile from which to start searching. Must be a rail station tile
150 * @param dir The direction in which to search.
151 * @return The platform length
153 virtual uint
GetPlatformLength(TileIndex tile
, DiagDirection dir
) const = 0;
156 * Get the base station belonging to a specific tile.
157 * @param tile The tile to get the base station from.
158 * @return the station associated with that tile.
160 static inline BaseStation
*GetByTile(TileIndex tile
)
162 return BaseStation::Get(GetStationIndex(tile
));
165 /** Check if this station is a waypoint. */
166 inline bool IsWaypoint (void) const
168 return (this->facilities
& FACIL_WAYPOINT
) != 0;
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 static void PostDestructor(size_t index
);
185 #define FOR_ALL_BASE_STATIONS(var) FOR_ALL_ITEMS_FROM(BaseStation, station_index, var, 0)
188 * Class defining several overloaded accessors so we don't
189 * have to cast base stations that often
191 template <class T
, bool Tis_waypoint
>
192 struct SpecializedStation
: public BaseStation
{
193 static const bool IS_WAYPOINT
= Tis_waypoint
;
196 * Set station type correctly
197 * @param tile The base tile of the station.
199 inline SpecializedStation
<T
, Tis_waypoint
>(TileIndex tile
) :
202 this->facilities
= Tis_waypoint
? FACIL_WAYPOINT
: FACIL_NONE
;
206 * Tests whether given index is a valid index for station of this type
207 * @param index tested index
208 * @return is this index valid index of T?
210 static inline bool IsValidID(size_t index
)
212 return BaseStation::IsValidID(index
) && BaseStation::Get(index
)->IsWaypoint() == IS_WAYPOINT
;
216 * Gets station with given index
217 * @return pointer to station with given index casted to T *
219 static inline T
*Get(size_t index
)
221 return (T
*)BaseStation::Get(index
);
225 * Returns station if the index is a valid index for this station type
226 * @return pointer to station with given index if it's a station of this type
228 static inline T
*GetIfValid(size_t index
)
230 return IsValidID(index
) ? Get(index
) : NULL
;
234 * Get the station belonging to a specific tile.
235 * @param tile The tile to get the station from.
236 * @return the station associated with that tile.
238 static inline T
*GetByTile(TileIndex tile
)
240 return GetIfValid(GetStationIndex(tile
));
244 * Converts a BaseStation to SpecializedStation with type checking.
245 * @param st BaseStation pointer
246 * @return pointer to SpecializedStation
248 static inline T
*From(BaseStation
*st
)
250 assert(st
->IsWaypoint() == IS_WAYPOINT
);
255 * Converts a const BaseStation to const SpecializedStation with type checking.
256 * @param st BaseStation pointer
257 * @return pointer to SpecializedStation
259 static inline const T
*From(const BaseStation
*st
)
261 assert(st
->IsWaypoint() == IS_WAYPOINT
);
262 return (const T
*)st
;
266 #define FOR_ALL_BASE_STATIONS_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, station_index, var, 0) if (var->IsWaypoint() == name::IS_WAYPOINT)
268 #endif /* BASE_STATION_BASE_H */