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 newgrf_station.h Header file for NewGRF stations */
10 #ifndef NEWGRF_STATION_H
11 #define NEWGRF_STATION_H
13 #include "core/enum_type.hpp"
14 #include "newgrf_animation_type.h"
15 #include "newgrf_callbacks.h"
16 #include "newgrf_class.h"
17 #include "newgrf_commons.h"
18 #include "cargo_type.h"
19 #include "station_type.h"
20 #include "rail_type.h"
21 #include "newgrf_spritegroup.h"
22 #include "newgrf_town.h"
24 /** Scope resolver for stations. */
25 struct StationScopeResolver
: public ScopeResolver
{
26 TileIndex tile
; ///< %Tile of the station.
27 struct BaseStation
*st
; ///< Instance of the station.
28 const struct StationSpec
*statspec
; ///< Station (type) specification.
29 CargoID cargo_type
; ///< Type of cargo of the station.
30 Axis axis
; ///< Station axis, used only for the slope check callback.
33 * Constructor for station scopes.
34 * @param ro Surrounding resolver.
35 * @param statspec Station (type) specification.
36 * @param st Instance of the station.
37 * @param tile %Tile of the station.
39 StationScopeResolver(ResolverObject
&ro
, const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
)
40 : ScopeResolver(ro
), tile(tile
), st(st
), statspec(statspec
), cargo_type(INVALID_CARGO
), axis(INVALID_AXIS
)
44 uint32_t GetRandomBits() const override
;
45 uint32_t GetTriggers() const override
;
47 uint32_t GetVariable(uint8_t variable
, [[maybe_unused
]] uint32_t parameter
, bool &available
) const override
;
50 /** Station resolver. */
51 struct StationResolverObject
: public ResolverObject
{
52 StationScopeResolver station_scope
; ///< The station scope resolver.
53 std::optional
<TownScopeResolver
> town_scope
= std::nullopt
; ///< The town scope resolver (created on the first call).
55 StationResolverObject(const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
,
56 CallbackID callback
= CBID_NO_CALLBACK
, uint32_t callback_param1
= 0, uint32_t callback_param2
= 0);
58 TownScopeResolver
*GetTown();
60 ScopeResolver
*GetScope(VarSpriteGroupScope scope
= VSG_SCOPE_SELF
, uint8_t relative
= 0) override
64 return &this->station_scope
;
66 case VSG_SCOPE_PARENT
: {
67 TownScopeResolver
*tsr
= this->GetTown();
68 if (tsr
!= nullptr) return tsr
;
73 return ResolverObject::GetScope(scope
, relative
);
77 const SpriteGroup
*ResolveReal(const RealSpriteGroup
*group
) const override
;
79 GrfSpecFeature
GetFeature() const override
;
80 uint32_t GetDebugID() const override
;
83 static const uint32_t STATION_CLASS_LABEL_DEFAULT
= 'DFLT';
84 static const uint32_t STATION_CLASS_LABEL_WAYPOINT
= 'WAYP';
86 enum StationClassID
: uint16_t {
87 STAT_CLASS_BEGIN
= 0, ///< the lowest valid value
88 STAT_CLASS_DFLT
= 0, ///< Default station class.
89 STAT_CLASS_WAYP
, ///< Waypoint class.
90 STAT_CLASS_MAX
= UINT16_MAX
, ///< Maximum number of classes.
93 /** Allow incrementing of StationClassID variables */
94 DECLARE_POSTFIX_INCREMENT(StationClassID
)
96 enum StationSpecFlags
{
97 SSF_SEPARATE_GROUND
, ///< Use different sprite set for ground sprites.
98 SSF_DIV_BY_STATION_SIZE
, ///< Divide cargo amount by station size.
99 SSF_CB141_RANDOM_BITS
, ///< Callback 141 needs random bits.
100 SSF_CUSTOM_FOUNDATIONS
, ///< Draw custom foundations.
101 SSF_EXTENDED_FOUNDATIONS
, ///< Extended foundation block instead of simple.
104 /** Randomisation triggers for stations */
105 enum StationRandomTrigger
{
106 SRT_NEW_CARGO
, ///< Trigger station on new cargo arrival.
107 SRT_CARGO_TAKEN
, ///< Trigger station when cargo is completely taken.
108 SRT_TRAIN_ARRIVES
, ///< Trigger platform when train arrives.
109 SRT_TRAIN_DEPARTS
, ///< Trigger platform when train leaves.
110 SRT_TRAIN_LOADS
, ///< Trigger platform when train loads/unloads.
111 SRT_PATH_RESERVATION
, ///< Trigger platform when train reserves path.
114 /** Station specification. */
115 struct StationSpec
: NewGRFSpecBase
<StationClassID
> {
116 StationSpec() : name(0),
117 disallowed_platforms(0), disallowed_lengths(0),
118 cargo_threshold(0), cargo_triggers(0),
119 callback_mask(0), flags(0),
120 animation({0, 0, 0, 0}) {}
122 * Properties related the the grf file.
123 * NUM_CARGO real cargo plus three pseudo cargo sprite groups.
124 * Used for obtaining the sprite offset of custom sprites, and for
125 * evaluating callbacks.
127 GRFFilePropsBase
<NUM_CARGO
+ 3> grf_prop
;
128 StringID name
; ///< Name of this station.
131 * Bitmask of number of platforms available for the station.
132 * 0..6 correspond to 1..7, while bit 7 corresponds to >7 platforms.
134 uint8_t disallowed_platforms
;
136 * Bitmask of platform lengths available for the station.
137 * 0..6 correspond to 1..7, while bit 7 corresponds to >7 tiles long.
139 uint8_t disallowed_lengths
;
142 * Number of tile layouts.
143 * A minimum of 8 is required is required for stations.
144 * 0-1 = plain platform
145 * 2-3 = platform with building
146 * 4-5 = platform with roof, left side
147 * 6-7 = platform with roof, right side
149 std::vector
<NewGRFSpriteLayout
> renderdata
; ///< Array of tile layouts.
152 * Cargo threshold for choosing between little and lots of cargo
153 * @note little/lots are equivalent to the moving/loading states for vehicles
155 uint16_t cargo_threshold
;
157 CargoTypes cargo_triggers
; ///< Bitmask of cargo types which cause trigger re-randomizing
159 uint8_t callback_mask
; ///< Bitmask of station callbacks that have to be called
161 uint8_t flags
; ///< Bitmask of flags, bit 0: use different sprite set; bit 1: divide cargo about by station size
163 enum class TileFlags
: uint8_t {
165 Pylons
= 1U << 0, ///< Tile should contain catenary pylons.
166 NoWires
= 1U << 1, ///< Tile should NOT contain catenary wires.
167 Blocked
= 1U << 2, ///< Tile is blocked to vehicles.
169 std::vector
<TileFlags
> tileflags
; ///< List of tile flags.
171 AnimationInfo animation
;
173 /** Custom platform layouts, keyed by platform and length combined. */
174 std::unordered_map
<uint16_t, std::vector
<uint8_t>> layouts
;
176 DECLARE_ENUM_AS_BIT_SET(StationSpec::TileFlags
);
178 /** Class containing information relating to station classes. */
179 using StationClass
= NewGRFClass
<StationSpec
, StationClassID
, STAT_CLASS_MAX
>;
181 const StationSpec
*GetStationSpec(TileIndex t
);
184 * Get the station layout key for a given station layout size.
185 * @param platforms Number of platforms.
186 * @param length Length of platforms.
187 * @returns Key of station layout.
189 inline uint16_t GetStationLayoutKey(uint8_t platforms
, uint8_t length
)
191 return (length
<< 8U) | platforms
;
195 * Test if a StationClass is the waypoint class.
196 * @param cls StationClass to test.
197 * @return true if the class is the waypoint class.
199 inline bool IsWaypointClass(const StationClass
&cls
)
201 return cls
.global_id
== STATION_CLASS_LABEL_WAYPOINT
|| GB(cls
.global_id
, 24, 8) == UINT8_MAX
;
204 /* Evaluate a tile's position within a station, and return the result a bitstuffed format. */
205 uint32_t GetPlatformInfo(Axis axis
, uint8_t tile
, int platforms
, int length
, int x
, int y
, bool centred
);
207 SpriteID
GetCustomStationRelocation(const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
, uint32_t var10
= 0);
208 SpriteID
GetCustomStationFoundationRelocation(const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
, uint layout
, uint edge_info
);
209 uint16_t GetStationCallback(CallbackID callback
, uint32_t param1
, uint32_t param2
, const StationSpec
*statspec
, BaseStation
*st
, TileIndex tile
);
210 CommandCost
PerformStationTileSlopeCheck(TileIndex north_tile
, TileIndex cur_tile
, const StationSpec
*statspec
, Axis axis
, uint8_t plat_len
, uint8_t numtracks
);
212 /* Allocate a StationSpec to a Station. This is called once per build operation. */
213 int AllocateSpecToStation(const StationSpec
*statspec
, BaseStation
*st
, bool exec
);
215 /* Deallocate a StationSpec from a Station. Called when removing a single station tile. */
216 void DeallocateSpecFromStation(BaseStation
*st
, uint8_t specindex
);
218 /* Draw representation of a station tile for GUI purposes. */
219 bool DrawStationTile(int x
, int y
, RailType railtype
, Axis axis
, StationClassID sclass
, uint station
);
221 void AnimateStationTile(TileIndex tile
);
222 void TriggerStationAnimation(BaseStation
*st
, TileIndex tile
, StationAnimationTrigger trigger
, CargoID cargo_type
= INVALID_CARGO
);
223 void TriggerStationRandomisation(Station
*st
, TileIndex tile
, StationRandomTrigger trigger
, CargoID cargo_type
= INVALID_CARGO
);
224 void StationUpdateCachedTriggers(BaseStation
*st
);
226 #endif /* NEWGRF_STATION_H */