OpenTTD Source: src/newgrf_station.h Source File

OpenTTD Source 15.0-beta1

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 <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef NEWGRF_STATION_H
11#define NEWGRF_STATION_H
12
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"
23
25 struct StationScopeResolver : public ScopeResolver {
26 TileIndex tile;
27 struct BaseStation *st;
28 const struct StationSpec *statspec;
29 CargoID cargo_type;
30 Axis axis;
31
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)
41 {
42 }
43
44 uint32_t GetRandomBits() const override;
45 uint32_t GetTriggers() const override;
46
47 uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
48};
49
51 struct StationResolverObject : public ResolverObject {
52 StationScopeResolver station_scope;
53 std::optional<TownScopeResolver> town_scope = std::nullopt;
54
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);
57
58 TownScopeResolver *GetTown();
59
60 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override
61 {
62 switch (scope) {
63 case VSG_SCOPE_SELF:
64 return &this->station_scope;
65
66 case VSG_SCOPE_PARENT: {
67 TownScopeResolver *tsr = this->GetTown();
68 if (tsr != nullptr) return tsr;
69 [[fallthrough]];
70 }
71
72 default:
73 return ResolverObject::GetScope(scope, relative);
74 }
75 }
76
77 const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
78
79 GrfSpecFeature GetFeature() const override;
80 uint32_t GetDebugID() const override;
81};
82
83static const uint32_t STATION_CLASS_LABEL_DEFAULT = 'DFLT';
84static const uint32_t STATION_CLASS_LABEL_WAYPOINT = 'WAYP';
85
86 enum StationClassID : uint16_t {
87 STAT_CLASS_BEGIN = 0,
88 STAT_CLASS_DFLT = 0,
89 STAT_CLASS_WAYP,
90 STAT_CLASS_MAX = UINT16_MAX,
91};
92
94 DECLARE_POSTFIX_INCREMENT(StationClassID)
95
96enum StationSpecFlags {
97 SSF_SEPARATE_GROUND,
98 SSF_DIV_BY_STATION_SIZE,
99 SSF_CB141_RANDOM_BITS,
100 SSF_CUSTOM_FOUNDATIONS,
101 SSF_EXTENDED_FOUNDATIONS,
102};
103
105 enum StationRandomTrigger {
106 SRT_NEW_CARGO,
107 SRT_CARGO_TAKEN,
108 SRT_TRAIN_ARRIVES,
109 SRT_TRAIN_DEPARTS,
110 SRT_TRAIN_LOADS,
111 SRT_PATH_RESERVATION,
112};
113
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}) {}
127 GRFFilePropsBase<NUM_CARGO + 3> grf_prop;
128 StringID name;
129
134 uint8_t disallowed_platforms;
139 uint8_t disallowed_lengths;
140
149 std::vector<NewGRFSpriteLayout> renderdata;
150
155 uint16_t cargo_threshold;
156
157 CargoTypes cargo_triggers;
158
159 uint8_t callback_mask;
160
161 uint8_t flags;
162
163 enum class TileFlags : uint8_t {
164 None = 0,
165 Pylons = 1U << 0,
166 NoWires = 1U << 1,
167 Blocked = 1U << 2,
168 };
169 std::vector<TileFlags> tileflags;
170
171 AnimationInfo animation;
172
174 std::unordered_map<uint16_t, std::vector<uint8_t>> layouts;
175};
176 DECLARE_ENUM_AS_BIT_SET(StationSpec::TileFlags);
177
179 using StationClass = NewGRFClass<StationSpec, StationClassID, STAT_CLASS_MAX>;
180
181const StationSpec *GetStationSpec(TileIndex t);
182
189 inline uint16_t GetStationLayoutKey(uint8_t platforms, uint8_t length)
190{
191 return (length << 8U) | platforms;
192}
193
199 inline bool IsWaypointClass(const StationClass &cls)
200{
201 return cls.global_id == STATION_CLASS_LABEL_WAYPOINT || GB(cls.global_id, 24, 8) == UINT8_MAX;
202}
203
204/* Evaluate a tile's position within a station, and return the result a bitstuffed format. */
205uint32_t GetPlatformInfo(Axis axis, uint8_t tile, int platforms, int length, int x, int y, bool centred);
206
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);
209uint16_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);
211
212/* Allocate a StationSpec to a Station. This is called once per build operation. */
213int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec);
214
215/* Deallocate a StationSpec from a Station. Called when removing a single station tile. */
216void DeallocateSpecFromStation(BaseStation *st, uint8_t specindex);
217
218/* Draw representation of a station tile for GUI purposes. */
219bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station);
220
221void AnimateStationTile(TileIndex tile);
222void TriggerStationAnimation(BaseStation *st, TileIndex tile, StationAnimationTrigger trigger, CargoID cargo_type = INVALID_CARGO);
223void TriggerStationRandomisation(Station *st, TileIndex tile, StationRandomTrigger trigger, CargoID cargo_type = INVALID_CARGO);
224void StationUpdateCachedTriggers(BaseStation *st);
225
226#endif /* NEWGRF_STATION_H */
AnimatedTileState::None
@ None
Tile is not animated.
GB
debug_inline 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
cargo_type.h
Types related to cargoes...
CargoID
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
CommandCost
Common return value for all commands.
Definition command_type.h:23
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
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition direction_type.h:116
INVALID_AXIS
@ INVALID_AXIS
Flag for an invalid Axis.
Definition direction_type.h:120
enum_type.hpp
Type (helpers) for enums.
DECLARE_ENUM_AS_BIT_SET
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
Operators to allow to work with enum as with type safe bit set in C++.
Definition enum_type.hpp:35
DECLARE_POSTFIX_INCREMENT
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition enum_type.hpp:18
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:18
GrfSpecFeature
GrfSpecFeature
Definition newgrf.h:67
newgrf_animation_type.h
Definitions related to NewGRF animation.
StationAnimationTrigger
StationAnimationTrigger
Animation triggers for station.
Definition newgrf_animation_type.h:26
newgrf_callbacks.h
Callbacks that NewGRFs could implement.
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition newgrf_callbacks.h:20
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition newgrf_callbacks.h:22
newgrf_class.h
Header file for classes to be used by e.g.
newgrf_commons.h
This file simplyfies 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:97
VSG_SCOPE_SELF
@ VSG_SCOPE_SELF
Resolved object itself.
Definition newgrf_spritegroup.h:100
VSG_SCOPE_PARENT
@ VSG_SCOPE_PARENT
Related object of the resolved one.
Definition newgrf_spritegroup.h:101
AllocateSpecToStation
int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec)
Allocate a StationSpec to a Station.
Definition newgrf_station.cpp:685
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:104
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:611
StationSpecFlags
StationSpecFlags
Allow incrementing of StationClassID variables.
Definition newgrf_station.h:96
SSF_SEPARATE_GROUND
@ SSF_SEPARATE_GROUND
Use different sprite set for ground sprites.
Definition newgrf_station.h:97
SSF_CB141_RANDOM_BITS
@ SSF_CB141_RANDOM_BITS
Callback 141 needs random bits.
Definition newgrf_station.h:99
SSF_CUSTOM_FOUNDATIONS
@ SSF_CUSTOM_FOUNDATIONS
Draw custom foundations.
Definition newgrf_station.h:100
SSF_EXTENDED_FOUNDATIONS
@ SSF_EXTENDED_FOUNDATIONS
Extended foundation block instead of simple.
Definition newgrf_station.h:101
SSF_DIV_BY_STATION_SIZE
@ SSF_DIV_BY_STATION_SIZE
Divide cargo amount by station size.
Definition newgrf_station.h:98
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:628
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:189
TriggerStationRandomisation
void TriggerStationRandomisation(Station *st, TileIndex tile, StationRandomTrigger trigger, CargoID cargo_type=INVALID_CARGO)
Trigger station randomisation.
Definition newgrf_station.cpp:919
StationUpdateCachedTriggers
void StationUpdateCachedTriggers(BaseStation *st)
Update the cached animation trigger bitmask for a station.
Definition newgrf_station.cpp:994
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:775
DeallocateSpecFromStation
void DeallocateSpecFromStation(BaseStation *st, uint8_t specindex)
Deallocate a StationSpec from a Station.
Definition newgrf_station.cpp:727
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:657
StationRandomTrigger
StationRandomTrigger
Randomisation triggers for stations.
Definition newgrf_station.h:105
SRT_TRAIN_LOADS
@ SRT_TRAIN_LOADS
Trigger platform when train loads/unloads.
Definition newgrf_station.h:110
SRT_TRAIN_ARRIVES
@ SRT_TRAIN_ARRIVES
Trigger platform when train arrives.
Definition newgrf_station.h:108
SRT_CARGO_TAKEN
@ SRT_CARGO_TAKEN
Trigger station when cargo is completely taken.
Definition newgrf_station.h:107
SRT_TRAIN_DEPARTS
@ SRT_TRAIN_DEPARTS
Trigger platform when train leaves.
Definition newgrf_station.h:109
SRT_PATH_RESERVATION
@ SRT_PATH_RESERVATION
Trigger platform when train reserves path.
Definition newgrf_station.h:111
SRT_NEW_CARGO
@ SRT_NEW_CARGO
Trigger station on new cargo arrival.
Definition newgrf_station.h:106
IsWaypointClass
bool IsWaypointClass(const StationClass &cls)
Test if a StationClass is the waypoint class.
Definition newgrf_station.h:199
StationClassID
StationClassID
Definition newgrf_station.h:86
STAT_CLASS_MAX
@ STAT_CLASS_MAX
Maximum number of classes.
Definition newgrf_station.h:90
STAT_CLASS_BEGIN
@ STAT_CLASS_BEGIN
the lowest valid value
Definition newgrf_station.h:87
STAT_CLASS_DFLT
@ STAT_CLASS_DFLT
Default station class.
Definition newgrf_station.h:88
STAT_CLASS_WAYP
@ STAT_CLASS_WAYP
Waypoint class.
Definition newgrf_station.h:89
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:27
station_type.h
Types related to stations.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:16
AnimationInfo
Information about animation.
Definition newgrf_animation_type.h:18
BaseStation
Base class for all station-ish types.
Definition base_station_base.h:59
GRFFilePropsBase
Data related to the handling of grf files.
Definition newgrf_commons.h:310
NewGRFSpecBase
Definition newgrf_class.h:17
RealSpriteGroup
Definition newgrf_spritegroup.h:79
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition newgrf_spritegroup.h:296
ResolverObject::callback_param2
uint32_t callback_param2
Second parameter (var 18) of the callback.
Definition newgrf_spritegroup.h:316
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition newgrf_spritegroup.h:314
ResolverObject::callback_param1
uint32_t callback_param1
First parameter (var 10) of the callback.
Definition newgrf_spritegroup.h:315
ResolverObject::GetScope
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0)
Get a resolver for the scope.
Definition newgrf_spritegroup.cpp:135
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
Definition newgrf_spritegroup.h:277
ScopeResolver::ro
ResolverObject & ro
Surrounding resolver object.
Definition newgrf_spritegroup.h:278
SpriteGroup
Definition newgrf_spritegroup.h:57
StationResolverObject
Station resolver.
Definition newgrf_station.h:51
StationResolverObject::station_scope
StationScopeResolver station_scope
The station scope resolver.
Definition newgrf_station.h:52
StationResolverObject::GetDebugID
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Definition newgrf_station.cpp:555
StationResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
Definition newgrf_station.h:60
StationResolverObject::GetTown
TownScopeResolver * GetTown()
Get the town scope associated with a station, if it exists.
Definition newgrf_station.cpp:255
StationResolverObject::town_scope
std::optional< TownScopeResolver > town_scope
The town scope resolver (created on the first call).
Definition newgrf_station.h:53
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:550
StationScopeResolver
Scope resolver for stations.
Definition newgrf_station.h:25
StationScopeResolver::st
struct BaseStation * st
Instance of the station.
Definition newgrf_station.h:27
StationScopeResolver::GetTriggers
uint32_t GetTriggers() const override
Get the triggers.
Definition newgrf_station.cpp:229
StationScopeResolver::GetVariable
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
Definition newgrf_station.cpp:270
StationScopeResolver::GetRandomBits
uint32_t GetRandomBits() const override
Get a few random bits.
Definition newgrf_station.cpp:223
StationScopeResolver::StationScopeResolver
StationScopeResolver(ResolverObject &ro, const StationSpec *statspec, BaseStation *st, TileIndex tile)
Constructor for station scopes.
Definition newgrf_station.h:39
StationScopeResolver::statspec
const struct StationSpec * statspec
Station (type) specification.
Definition newgrf_station.h:28
StationScopeResolver::axis
Axis axis
Station axis, used only for the slope check callback.
Definition newgrf_station.h:30
StationScopeResolver::tile
TileIndex tile
Tile of the station.
Definition newgrf_station.h:26
StationScopeResolver::cargo_type
CargoID cargo_type
Type of cargo of the station.
Definition newgrf_station.h:29
StationSpec
Station specification.
Definition newgrf_station.h:115
StationSpec::callback_mask
uint8_t callback_mask
Bitmask of station callbacks that have to be called.
Definition newgrf_station.h:159
StationSpec::disallowed_lengths
uint8_t disallowed_lengths
Bitmask of platform lengths available for the station.
Definition newgrf_station.h:139
StationSpec::name
StringID name
Name of this station.
Definition newgrf_station.h:128
StationSpec::disallowed_platforms
uint8_t disallowed_platforms
Bitmask of number of platforms available for the station.
Definition newgrf_station.h:134
StationSpec::flags
uint8_t flags
Bitmask of flags, bit 0: use different sprite set; bit 1: divide cargo about by station size.
Definition newgrf_station.h:161
StationSpec::cargo_threshold
uint16_t cargo_threshold
Cargo threshold for choosing between little and lots of cargo.
Definition newgrf_station.h:155
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:174
StationSpec::TileFlags
TileFlags
Definition newgrf_station.h:163
StationSpec::TileFlags::NoWires
@ NoWires
Tile should NOT contain catenary wires.
StationSpec::TileFlags::Pylons
@ Pylons
Tile should contain catenary pylons.
StationSpec::TileFlags::Blocked
@ Blocked
Tile is blocked to vehicles.
StationSpec::tileflags
std::vector< TileFlags > tileflags
List of tile flags.
Definition newgrf_station.h:169
StationSpec::grf_prop
GRFFilePropsBase< NUM_CARGO+3 > grf_prop
Properties related the the grf file.
Definition newgrf_station.h:127
StationSpec::cargo_triggers
CargoTypes cargo_triggers
Bitmask of cargo types which cause trigger re-randomizing.
Definition newgrf_station.h:157
StationSpec::renderdata
std::vector< NewGRFSpriteLayout > renderdata
Number of tile layouts.
Definition newgrf_station.h:149
Station
Station data structure.
Definition station_base.h:439
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