OpenTTD Source 20250529-master-g10c159a79f
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"
15#include "newgrf_badge_type.h"
16#include "newgrf_callbacks.h"
17#include "newgrf_class.h"
18#include "newgrf_commons.h"
19#include "cargo_type.h"
20#include "station_type.h"
21#include "rail_type.h"
22#include "newgrf_spritegroup.h"
23#include "newgrf_town.h"
24
28 struct BaseStation *st;
29 const struct StationSpec *statspec;
32
38 struct Cache {
39 std::optional<uint32_t> v40;
40 std::optional<uint32_t> v41;
41 std::optional<uint32_t> v45;
42 std::optional<uint32_t> v46;
43 std::optional<uint32_t> v47;
44 std::optional<uint32_t> v49;
45 };
46 mutable Cache cache;
47
59
60 uint32_t GetRandomBits() const override;
61 uint32_t GetRandomTriggers() const override;
62
63 uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
64};
65
67struct StationResolverObject : public SpecializedResolverObject<StationRandomTriggers> {
69 std::optional<TownScopeResolver> town_scope = std::nullopt;
70
71 StationResolverObject(const StationSpec *statspec, BaseStation *st, TileIndex tile,
73
75
76 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override
77 {
78 switch (scope) {
79 case VSG_SCOPE_SELF:
80 return &this->station_scope;
81
82 case VSG_SCOPE_PARENT: {
83 TownScopeResolver *tsr = this->GetTown();
84 if (tsr != nullptr) return tsr;
85 [[fallthrough]];
86 }
87
88 default:
89 return ResolverObject::GetScope(scope, relative);
90 }
91 }
92
93 const SpriteGroup *ResolveReal(const RealSpriteGroup &group) const override;
94
95 GrfSpecFeature GetFeature() const override;
96 uint32_t GetDebugID() const override;
97};
98
99static const uint32_t STATION_CLASS_LABEL_DEFAULT = 'DFLT';
100static const uint32_t STATION_CLASS_LABEL_WAYPOINT = 'WAYP';
101
108
111
112enum class StationSpecFlag : uint8_t {
113 SeparateGround = 0,
114 DivByStationSize = 1,
115 Cb141RandomBits = 2,
118};
120
122struct StationSpec : NewGRFSpecBase<StationClassID> {
123 StationSpec() : name(0),
126 callback_mask(0), flags(0)
127 {}
128
131
142
151 std::vector<NewGRFSpriteLayout> renderdata;
152
158
159 CargoTypes cargo_triggers;
160
162
164
165 enum class TileFlag : uint8_t {
166 Pylons = 0,
167 NoWires = 1,
168 Blocked = 2,
169 };
170 using TileFlags = EnumBitSet<TileFlag, uint8_t>;
171 std::vector<TileFlags> tileflags;
172
174
176 std::unordered_map<uint16_t, std::vector<uint8_t>> layouts;
177
178 std::vector<BadgeID> badges;
179};
180
183
184const StationSpec *GetStationSpec(TileIndex t);
185
192inline uint16_t GetStationLayoutKey(uint8_t platforms, uint8_t length)
193{
194 return (length << 8U) | platforms;
195}
196
202inline bool IsWaypointClass(const StationClass &cls)
203{
204 return cls.global_id == STATION_CLASS_LABEL_WAYPOINT || GB(cls.global_id, 24, 8) == UINT8_MAX;
205}
206
207/* Evaluate a tile's position within a station, and return the result a bitstuffed format. */
208uint32_t GetPlatformInfo(Axis axis, uint8_t tile, int platforms, int length, int x, int y, bool centred);
209
210SpriteID GetCustomStationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint32_t var10 = 0);
211void GetCustomStationRelocation(SpriteLayoutProcessor &processor, const StationSpec *statspec, BaseStation *st, TileIndex tile);
212SpriteID GetCustomStationFoundationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint layout, uint edge_info);
213uint16_t GetStationCallback(CallbackID callback, uint32_t param1, uint32_t param2, const StationSpec *statspec, BaseStation *st, TileIndex tile, std::span<int32_t> regs100 = {});
214CommandCost PerformStationTileSlopeCheck(TileIndex north_tile, TileIndex cur_tile, const StationSpec *statspec, Axis axis, uint8_t plat_len, uint8_t numtracks);
215
216/* Allocate a StationSpec to a Station. This is called once per build operation. */
217int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec);
218
219/* Deallocate a StationSpec from a Station. Called when removing a single station tile. */
220void DeallocateSpecFromStation(BaseStation *st, uint8_t specindex);
221
222/* Draw representation of a station tile for GUI purposes. */
223bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station);
224
225void AnimateStationTile(TileIndex tile);
226void TriggerStationAnimation(BaseStation *st, TileIndex tile, StationAnimationTrigger trigger, CargoType cargo_type = INVALID_CARGO);
227void TriggerStationRandomisation(BaseStation *st, TileIndex tile, StationRandomTrigger trigger, CargoType cargo_type = INVALID_CARGO);
229
230#endif /* NEWGRF_STATION_H */
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.
Types related to cargoes...
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
Common return value for all commands.
Struct containing information relating to NewGRF classes for stations and airports.
uint32_t global_id
Global ID for class, e.g. 'DFLT', 'WAYP', etc.
Add dynamic register values to a sprite layout.
Axis
Allow incrementing of DiagDirDiff variables.
@ INVALID_AXIS
Flag for an invalid Axis.
Type (helpers) for enums.
#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
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
GrfSpecFeature
Definition newgrf.h:69
Definitions related to NewGRF animation.
Types related to NewGRF badges.
Callbacks that NewGRFs could implement.
CallbackID
List of implemented NewGRF callbacks.
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Header file for classes to be used by e.g.
This file simplyfies and embeds a common mechanism of loading/saving and mapping of grf entities.
Action 2 handling.
VarSpriteGroupScope
@ VSG_SCOPE_SELF
Resolved object itself.
@ VSG_SCOPE_PARENT
Related object of the resolved one.
StationSpecFlag
Allow incrementing of StationClassID variables.
@ Cb141RandomBits
Callback 141 needs random bits.
@ CustomFoundations
Draw custom foundations.
@ SeparateGround
Use different sprite set for ground sprites.
@ ExtendedFoundations
Extended foundation block instead of simple.
@ DivByStationSize
Divide cargo amount by station size.
int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec)
Allocate a StationSpec to a Station.
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.
SpriteID GetCustomStationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint32_t var10=0)
Resolve sprites for drawing a station tile.
void TriggerStationRandomisation(BaseStation *st, TileIndex tile, StationRandomTrigger trigger, CargoType cargo_type=INVALID_CARGO)
Trigger station randomisation.
SpriteID GetCustomStationFoundationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint layout, uint edge_info)
Resolve the sprites for custom station foundations.
uint16_t GetStationLayoutKey(uint8_t platforms, uint8_t length)
Get the station layout key for a given station layout size.
void StationUpdateCachedTriggers(BaseStation *st)
Update the cached animation trigger bitmask for a station.
bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station)
Draw representation of a station tile for GUI purposes.
void DeallocateSpecFromStation(BaseStation *st, uint8_t specindex)
Deallocate a StationSpec from a Station.
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.
bool IsWaypointClass(const StationClass &cls)
Test if a StationClass is the waypoint class.
StationClassID
@ STAT_CLASS_MAX
Maximum number of classes.
@ STAT_CLASS_BEGIN
the lowest valid value
@ STAT_CLASS_DFLT
Default station class.
@ STAT_CLASS_WAYP
Waypoint class.
Functions to handle the town part of NewGRF towns.
The different types of rail.
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
Types related to stations.
StationRandomTrigger
Randomisation triggers for stations and roadstops.
StationAnimationTrigger
Animation triggers for stations and roadstops.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Information about animation.
Base class for all station-ish types.
Sprite groups indexed by CargoType.
Interface for SpriteGroup-s to access the gamestate.
uint32_t callback_param2
Second parameter (var 18) of the callback.
CallbackID callback
Callback being resolved.
uint32_t callback_param1
First parameter (var 10) of the callback.
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0)
Get a resolver for the scope.
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
ResolverObject & ro
Surrounding resolver object.
Specialization of ResolverObject with type-safe access to RandomTriggers.
Station resolver.
StationScopeResolver station_scope
The station scope resolver.
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
TownScopeResolver * GetTown()
Get the town scope associated with a station, if it exists.
std::optional< TownScopeResolver > town_scope
The town scope resolver (created on the first call).
const SpriteGroup * ResolveReal(const RealSpriteGroup &group) const override
Get the real sprites of the grf.
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Station variable cache This caches 'expensive' station variable lookups which iterate over several ti...
Scope resolver for stations.
CargoType cargo_type
Type of cargo of the station.
struct BaseStation * st
Instance of the station.
uint32_t GetRandomTriggers() const override
Get the triggers.
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
uint32_t GetRandomBits() const override
Get a few random bits.
StationScopeResolver(ResolverObject &ro, const StationSpec *statspec, BaseStation *st, TileIndex tile)
Constructor for station scopes.
const struct StationSpec * statspec
Station (type) specification.
Axis axis
Station axis, used only for the slope check callback.
TileIndex tile
Tile of the station.
Station specification.
uint8_t disallowed_lengths
Bitmask of platform lengths available for the station.
StringID name
Name of this station.
uint8_t disallowed_platforms
Bitmask of number of platforms available for the station.
uint16_t cargo_threshold
Cargo threshold for choosing between little and lots of cargo.
CargoGRFFileProps grf_prop
Link to NewGRF.
std::unordered_map< uint16_t, std::vector< uint8_t > > layouts
Custom platform layouts, keyed by platform and length combined.
std::vector< TileFlags > tileflags
List of tile flags.
CargoTypes cargo_triggers
Bitmask of cargo types which cause trigger re-randomizing.
std::vector< NewGRFSpriteLayout > renderdata
Number of tile layouts.
StationCallbackMasks callback_mask
Bitmask of station callbacks that have to be called.
@ NoWires
Tile should NOT contain catenary wires.
@ Pylons
Tile should contain catenary pylons.
@ Blocked
Tile is blocked to vehicles.
StationSpecFlags flags
Bitmask of flags, bit 0: use different sprite set; bit 1: divide cargo about by station size.
Scope resolver for a town.
Definition newgrf_town.h:22