OpenTTD Source: src/newgrf_station.h Source File

OpenTTD Source 15.0-RC2

newgrf_station.h
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef NEWGRF_STATION_H
11#define NEWGRF_STATION_H
12
13#include "core/enum_type.hpp"
14#include "bridge_type.h"
15#include "newgrf_animation_type.h"
16#include "newgrf_badge_type.h"
17#include "newgrf_callbacks.h"
18#include "newgrf_class.h"
19#include "newgrf_commons.h"
20#include "cargo_type.h"
21#include "station_type.h"
22#include "rail_type.h"
23#include "newgrf_spritegroup.h"
24#include "newgrf_town.h"
25
27 struct StationScopeResolver : public ScopeResolver {
28 TileIndex tile;
29 struct BaseStation *st;
30 const struct StationSpec *statspec;
31 CargoType cargo_type;
32 Axis axis;
33
39 struct Cache {
40 std::optional<uint32_t> v40;
41 std::optional<uint32_t> v41;
42 std::optional<uint32_t> v45;
43 std::optional<uint32_t> v46;
44 std::optional<uint32_t> v47;
45 std::optional<uint32_t> v49;
46 };
47 mutable Cache cache;
48
56 StationScopeResolver(ResolverObject &ro, const StationSpec *statspec, BaseStation *st, TileIndex tile)
57 : ScopeResolver(ro), tile(tile), st(st), statspec(statspec), cargo_type(INVALID_CARGO), axis(INVALID_AXIS)
58 {
59 }
60
61 uint32_t GetRandomBits() const override;
62 uint32_t GetRandomTriggers() const override;
63
64 uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
65};
66
68 struct StationResolverObject : public SpecializedResolverObject<StationRandomTriggers> {
69 StationScopeResolver station_scope;
70 std::optional<TownScopeResolver> town_scope = std::nullopt;
71
72 StationResolverObject(const StationSpec *statspec, BaseStation *st, TileIndex tile,
73 CallbackID callback = CBID_NO_CALLBACK, uint32_t callback_param1 = 0, uint32_t callback_param2 = 0);
74
75 TownScopeResolver *GetTown();
76
77 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override
78 {
79 switch (scope) {
80 case VSG_SCOPE_SELF:
81 return &this->station_scope;
82
83 case VSG_SCOPE_PARENT: {
84 TownScopeResolver *tsr = this->GetTown();
85 if (tsr != nullptr) return tsr;
86 [[fallthrough]];
87 }
88
89 default:
90 return ResolverObject::GetScope(scope, relative);
91 }
92 }
93
94 const SpriteGroup *ResolveReal(const RealSpriteGroup &group) const override;
95
96 GrfSpecFeature GetFeature() const override;
97 uint32_t GetDebugID() const override;
98};
99
100static const uint32_t STATION_CLASS_LABEL_DEFAULT = 'DFLT';
101static const uint32_t STATION_CLASS_LABEL_WAYPOINT = 'WAYP';
102
103 enum StationClassID : uint16_t {
104 STAT_CLASS_BEGIN = 0,
105 STAT_CLASS_DFLT = 0,
106 STAT_CLASS_WAYP,
107 STAT_CLASS_MAX = UINT16_MAX,
108};
109
111 DECLARE_INCREMENT_DECREMENT_OPERATORS(StationClassID)
112
113enum class StationSpecFlag : uint8_t {
114 SeparateGround = 0,
115 DivByStationSize = 1,
116 Cb141RandomBits = 2,
117 CustomFoundations = 3,
118 ExtendedFoundations = 4,
119};
120using StationSpecFlags = EnumBitSet<StationSpecFlag, uint8_t>;
121
123 struct StationSpec : NewGRFSpecBase<StationClassID> {
124 StationSpec() : name(0),
125 disallowed_platforms(0), disallowed_lengths(0),
126 cargo_threshold(0), cargo_triggers(0),
127 callback_mask(0), flags(0)
128 {}
129
130 CargoGRFFileProps grf_prop;
131 StringID name;
132
137 uint8_t disallowed_platforms;
142 uint8_t disallowed_lengths;
143
152 std::vector<NewGRFSpriteLayout> renderdata;
153
158 uint16_t cargo_threshold;
159
160 CargoTypes cargo_triggers;
161
162 StationCallbackMasks callback_mask;
163
164 StationSpecFlags flags;
165
166 enum class TileFlag : uint8_t {
167 Pylons = 0,
168 NoWires = 1,
169 Blocked = 2,
170 };
171 using TileFlags = EnumBitSet<TileFlag, uint8_t>;
172 std::vector<TileFlags> tileflags;
173 std::vector<BridgeableTileInfo> bridgeable_info;
174
175 AnimationInfo<StationAnimationTriggers> animation;
176
178 std::unordered_map<uint16_t, std::vector<uint8_t>> layouts;
179
180 std::vector<BadgeID> badges;
181};
182
184 using StationClass = NewGRFClass<StationSpec, StationClassID, STAT_CLASS_MAX>;
185
186const StationSpec *GetStationSpec(TileIndex t);
187
194 inline uint16_t GetStationLayoutKey(uint8_t platforms, uint8_t length)
195{
196 return (length << 8U) | platforms;
197}
198
204 inline bool IsWaypointClass(const StationClass &cls)
205{
206 return cls.global_id == STATION_CLASS_LABEL_WAYPOINT || GB(cls.global_id, 24, 8) == UINT8_MAX;
207}
208
209/* Evaluate a tile's position within a station, and return the result a bitstuffed format. */
210uint32_t GetPlatformInfo(Axis axis, uint8_t tile, int platforms, int length, int x, int y, bool centred);
211
212 SpriteID GetCustomStationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint32_t var10 = 0);
213void GetCustomStationRelocation(SpriteLayoutProcessor &processor, const StationSpec *statspec, BaseStation *st, TileIndex tile);
214 SpriteID GetCustomStationFoundationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint layout, uint edge_info);
215uint16_t GetStationCallback(CallbackID callback, uint32_t param1, uint32_t param2, const StationSpec *statspec, BaseStation *st, TileIndex tile, std::span<int32_t> regs100 = {});
216 CommandCost PerformStationTileSlopeCheck(TileIndex north_tile, TileIndex cur_tile, const StationSpec *statspec, Axis axis, uint8_t plat_len, uint8_t numtracks);
217
218std::optional<uint8_t> AllocateSpecToStation(const StationSpec *spec, BaseStation *st);
219void AssignSpecToStation(const StationSpec *spec, BaseStation *st, uint8_t specindex);
220void DeallocateSpecFromStation(BaseStation *st, uint8_t specindex);
221bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station);
222
223void AnimateStationTile(TileIndex tile);
224void TriggerStationAnimation(BaseStation *st, TileIndex tile, StationAnimationTrigger trigger, CargoType cargo_type = INVALID_CARGO);
225void TriggerStationRandomisation(BaseStation *st, TileIndex tile, StationRandomTrigger trigger, CargoType cargo_type = INVALID_CARGO);
226void StationUpdateCachedTriggers(BaseStation *st);
227
228#endif /* NEWGRF_STATION_H */
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
bridge_type.h
Header file for bridge types.
cargo_type.h
Types related to cargoes...
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
CommandCost
Common return value for all commands.
Definition command_type.h:24
EnumBitSet< StationSpecFlag, uint8_t >
NewGRFClass
Struct containing information relating to NewGRF classes for stations and airports.
Definition newgrf_class.h:26
NewGRFClass::global_id
uint32_t global_id
Global ID for class, e.g. 'DFLT', 'WAYP', etc.
Definition newgrf_class.h:48
SpriteLayoutProcessor
Add dynamic register values to a sprite layout.
Definition newgrf_commons.h:142
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition direction_type.h:123
INVALID_AXIS
@ INVALID_AXIS
Flag for an invalid Axis.
Definition direction_type.h:127
enum_type.hpp
Type (helpers) for enums.
DECLARE_INCREMENT_DECREMENT_OPERATORS
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:63
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
GrfSpecFeature
GrfSpecFeature
Definition newgrf.h:68
newgrf_animation_type.h
Definitions related to NewGRF animation.
newgrf_badge_type.h
Types related to NewGRF badges.
newgrf_callbacks.h
Callbacks that NewGRFs could implement.
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition newgrf_callbacks.h:22
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition newgrf_callbacks.h:24
newgrf_class.h
Header file for classes to be used by e.g.
newgrf_commons.h
This file simplifies and embeds a common mechanism of loading/saving and mapping of grf entities.
newgrf_spritegroup.h
Action 2 handling.
VarSpriteGroupScope
VarSpriteGroupScope
Definition newgrf_spritegroup.h:82
VSG_SCOPE_SELF
@ VSG_SCOPE_SELF
Resolved object itself.
Definition newgrf_spritegroup.h:85
VSG_SCOPE_PARENT
@ VSG_SCOPE_PARENT
Related object of the resolved one.
Definition newgrf_spritegroup.h:86
StationSpecFlag
StationSpecFlag
Allow incrementing of StationClassID variables.
Definition newgrf_station.h:113
StationSpecFlag::Cb141RandomBits
@ Cb141RandomBits
Callback 141 needs random bits.
StationSpecFlag::CustomFoundations
@ CustomFoundations
Draw custom foundations.
StationSpecFlag::SeparateGround
@ SeparateGround
Use different sprite set for ground sprites.
StationSpecFlag::ExtendedFoundations
@ ExtendedFoundations
Extended foundation block instead of simple.
StationSpecFlag::DivByStationSize
@ DivByStationSize
Divide cargo amount by station size.
GetPlatformInfo
uint32_t GetPlatformInfo(Axis axis, uint8_t tile, int platforms, int length, int x, int y, bool centred)
Evaluate a tile's position within a station, and return the result in a bit-stuffed format.
Definition newgrf_station.cpp:120
GetCustomStationRelocation
SpriteID GetCustomStationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint32_t var10=0)
Resolve sprites for drawing a station tile.
Definition newgrf_station.cpp:613
TriggerStationRandomisation
void TriggerStationRandomisation(BaseStation *st, TileIndex tile, StationRandomTrigger trigger, CargoType cargo_type=INVALID_CARGO)
Trigger station randomisation.
Definition newgrf_station.cpp:950
GetCustomStationFoundationRelocation
SpriteID GetCustomStationFoundationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint layout, uint edge_info)
Resolve the sprites for custom station foundations.
Definition newgrf_station.cpp:647
GetStationLayoutKey
uint16_t GetStationLayoutKey(uint8_t platforms, uint8_t length)
Get the station layout key for a given station layout size.
Definition newgrf_station.h:194
StationUpdateCachedTriggers
void StationUpdateCachedTriggers(BaseStation *st)
Update the cached animation trigger bitmask for a station.
Definition newgrf_station.cpp:1026
DrawStationTile
bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station)
Draw representation of a station tile for GUI purposes.
Definition newgrf_station.cpp:804
AssignSpecToStation
void AssignSpecToStation(const StationSpec *spec, BaseStation *st, uint8_t specindex)
Assign a previously allocated StationSpec specindex to a Station.
Definition newgrf_station.cpp:740
DeallocateSpecFromStation
void DeallocateSpecFromStation(BaseStation *st, uint8_t specindex)
Deallocate a StationSpec from a Station.
Definition newgrf_station.cpp:757
PerformStationTileSlopeCheck
CommandCost PerformStationTileSlopeCheck(TileIndex north_tile, TileIndex cur_tile, const StationSpec *statspec, Axis axis, uint8_t plat_len, uint8_t numtracks)
Check the slope of a tile of a new station.
Definition newgrf_station.cpp:677
AllocateSpecToStation
std::optional< uint8_t > AllocateSpecToStation(const StationSpec *spec, BaseStation *st)
Allocate a StationSpec to a Station.
Definition newgrf_station.cpp:705
IsWaypointClass
bool IsWaypointClass(const StationClass &cls)
Test if a StationClass is the waypoint class.
Definition newgrf_station.h:204
StationClassID
StationClassID
Definition newgrf_station.h:103
STAT_CLASS_MAX
@ STAT_CLASS_MAX
Maximum number of classes.
Definition newgrf_station.h:107
STAT_CLASS_BEGIN
@ STAT_CLASS_BEGIN
the lowest valid value
Definition newgrf_station.h:104
STAT_CLASS_DFLT
@ STAT_CLASS_DFLT
Default station class.
Definition newgrf_station.h:105
STAT_CLASS_WAYP
@ STAT_CLASS_WAYP
Waypoint class.
Definition newgrf_station.h:106
newgrf_town.h
Functions to handle the town part of NewGRF towns.
rail_type.h
The different types of rail.
RailType
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
station_type.h
Types related to stations.
StationRandomTrigger
StationRandomTrigger
Randomisation triggers for stations and roadstops.
Definition station_type.h:77
StationAnimationTrigger
StationAnimationTrigger
Animation triggers for stations and roadstops.
Definition station_type.h:88
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
AnimationInfo
Information about animation.
Definition newgrf_animation_type.h:21
BaseStation
Base class for all station-ish types.
Definition base_station_base.h:58
CargoGRFFileProps
Sprite groups indexed by CargoType.
Definition newgrf_commons.h:455
NewGRFSpecBase
Definition newgrf_class.h:17
RealSpriteGroup
Definition newgrf_spritegroup.h:64
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition newgrf_spritegroup.h:288
ResolverObject::callback_param2
uint32_t callback_param2
Second parameter (var 18) of the callback.
Definition newgrf_spritegroup.h:342
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition newgrf_spritegroup.h:340
ResolverObject::callback_param1
uint32_t callback_param1
First parameter (var 10) of the callback.
Definition newgrf_spritegroup.h:341
ResolverObject::GetScope
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0)
Get a resolver for the scope.
Definition newgrf_spritegroup.cpp:133
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
Definition newgrf_spritegroup.h:269
ScopeResolver::ro
ResolverObject & ro
Surrounding resolver object.
Definition newgrf_spritegroup.h:270
SpecializedResolverObject
Specialization of ResolverObject with type-safe access to RandomTriggers.
Definition newgrf_spritegroup.h:450
SpriteGroup
Definition newgrf_spritegroup.h:47
StationResolverObject
Station resolver.
Definition newgrf_station.h:68
StationResolverObject::station_scope
StationScopeResolver station_scope
The station scope resolver.
Definition newgrf_station.h:69
StationResolverObject::GetDebugID
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Definition newgrf_station.cpp:560
StationResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
Definition newgrf_station.h:77
StationResolverObject::GetTown
TownScopeResolver * GetTown()
Get the town scope associated with a station, if it exists.
Definition newgrf_station.cpp:256
StationResolverObject::town_scope
std::optional< TownScopeResolver > town_scope
The town scope resolver (created on the first call).
Definition newgrf_station.h:70
StationResolverObject::ResolveReal
const SpriteGroup * ResolveReal(const RealSpriteGroup &group) const override
Get the real sprites of the grf.
Definition newgrf_station.cpp:505
StationResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition newgrf_station.cpp:555
StationScopeResolver::Cache
Station variable cache This caches 'expensive' station variable lookups which iterate over several ti...
Definition newgrf_station.h:39
StationScopeResolver
Scope resolver for stations.
Definition newgrf_station.h:27
StationScopeResolver::cargo_type
CargoType cargo_type
Type of cargo of the station.
Definition newgrf_station.h:31
StationScopeResolver::st
struct BaseStation * st
Instance of the station.
Definition newgrf_station.h:29
StationScopeResolver::GetRandomTriggers
uint32_t GetRandomTriggers() const override
Get the triggers.
Definition newgrf_station.cpp:245
StationScopeResolver::GetVariable
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
Definition newgrf_station.cpp:271
StationScopeResolver::GetRandomBits
uint32_t GetRandomBits() const override
Get a few random bits.
Definition newgrf_station.cpp:239
StationScopeResolver::StationScopeResolver
StationScopeResolver(ResolverObject &ro, const StationSpec *statspec, BaseStation *st, TileIndex tile)
Constructor for station scopes.
Definition newgrf_station.h:56
StationScopeResolver::statspec
const struct StationSpec * statspec
Station (type) specification.
Definition newgrf_station.h:30
StationScopeResolver::axis
Axis axis
Station axis, used only for the slope check callback.
Definition newgrf_station.h:32
StationScopeResolver::tile
TileIndex tile
Tile of the station.
Definition newgrf_station.h:28
StationSpec
Station specification.
Definition newgrf_station.h:123
StationSpec::bridgeable_info
std::vector< BridgeableTileInfo > bridgeable_info
Per tile layout bridge information.
Definition newgrf_station.h:173
StationSpec::disallowed_lengths
uint8_t disallowed_lengths
Bitmask of platform lengths available for the station.
Definition newgrf_station.h:142
StationSpec::name
StringID name
Name of this station.
Definition newgrf_station.h:131
StationSpec::disallowed_platforms
uint8_t disallowed_platforms
Bitmask of number of platforms available for the station.
Definition newgrf_station.h:137
StationSpec::cargo_threshold
uint16_t cargo_threshold
Cargo threshold for choosing between little and lots of cargo.
Definition newgrf_station.h:158
StationSpec::grf_prop
CargoGRFFileProps grf_prop
Link to NewGRF.
Definition newgrf_station.h:130
StationSpec::layouts
std::unordered_map< uint16_t, std::vector< uint8_t > > layouts
Custom platform layouts, keyed by platform and length combined.
Definition newgrf_station.h:178
StationSpec::tileflags
std::vector< TileFlags > tileflags
List of tile flags.
Definition newgrf_station.h:172
StationSpec::cargo_triggers
CargoTypes cargo_triggers
Bitmask of cargo types which cause trigger re-randomizing.
Definition newgrf_station.h:160
StationSpec::renderdata
std::vector< NewGRFSpriteLayout > renderdata
Number of tile layouts.
Definition newgrf_station.h:152
StationSpec::callback_mask
StationCallbackMasks callback_mask
Bitmask of station callbacks that have to be called.
Definition newgrf_station.h:162
StationSpec::TileFlag
TileFlag
Definition newgrf_station.h:166
StationSpec::TileFlag::NoWires
@ NoWires
Tile should NOT contain catenary wires.
StationSpec::TileFlag::Pylons
@ Pylons
Tile should contain catenary pylons.
StationSpec::TileFlag::Blocked
@ Blocked
Tile is blocked to vehicles.
StationSpec::flags
StationSpecFlags flags
Bitmask of flags, bit 0: use different sprite set; bit 1: divide cargo about by station size.
Definition newgrf_station.h:164
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
TownScopeResolver
Scope resolver for a town.
Definition newgrf_town.h:22