OpenTTD Source: src/newgrf_roadstop.h Source File

OpenTTD Source 15.3

newgrf_roadstop.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
12#ifndef NEWGRF_ROADSTATION_H
13#define NEWGRF_ROADSTATION_H
14
15#include "bridge_type.h"
16#include "newgrf_animation_type.h"
17#include "newgrf_spritegroup.h"
18#include "newgrf_badge_type.h"
19#include "newgrf_callbacks.h"
20#include "newgrf_class.h"
21#include "newgrf_commons.h"
22#include "newgrf_town.h"
23#include "road.h"
24
25struct TileInfo;
26
28 static const int NUM_ROADSTOPS_PER_GRF = UINT16_MAX - 1;
29
30static const uint32_t ROADSTOP_CLASS_LABEL_DEFAULT = 'DFLT';
31static const uint32_t ROADSTOP_CLASS_LABEL_WAYPOINT = 'WAYP';
32
33 enum RoadStopClassID : uint16_t {
34 ROADSTOP_CLASS_BEGIN = 0,
35 ROADSTOP_CLASS_DFLT = 0,
36 ROADSTOP_CLASS_WAYP,
37 ROADSTOP_CLASS_MAX = UINT16_MAX,
38};
39 DECLARE_INCREMENT_DECREMENT_OPERATORS(RoadStopClassID)
40
41
45enum RoadStopAvailabilityType : uint8_t {
46 ROADSTOPTYPE_PASSENGER,
47 ROADSTOPTYPE_FREIGHT,
48 ROADSTOPTYPE_ALL,
49
50 ROADSTOPTYPE_END,
51};
52
57 enum class RoadStopDrawMode : uint8_t {
58 Road = 0,
59 Overlay = 1,
60 WaypGround = 2,
61};
62using RoadStopDrawModes = EnumBitSet<RoadStopDrawMode, uint8_t>;
63
64 enum class RoadStopSpecFlag : uint8_t {
65 Cb141RandomBits = 0,
66 NoCatenary = 2,
67 DriveThroughOnly = 3,
68 NoAutoRoadConnection = 4,
69 RoadOnly = 5,
70 TramOnly = 6,
71 DrawModeRegister = 8,
72};
73using RoadStopSpecFlags = EnumBitSet<RoadStopSpecFlag, uint8_t>;
74
75 enum RoadStopView : uint8_t {
76 RSV_BAY_NE = 0,
77 RSV_BAY_SE = 1,
78 RSV_BAY_SW = 2,
79 RSV_BAY_NW = 3,
80 RSV_DRIVE_THROUGH_X = 4,
81 RSV_DRIVE_THROUGH_Y = 5,
82};
83
85 struct RoadStopScopeResolver : public ScopeResolver {
86 TileIndex tile{};
87 struct BaseStation *st = nullptr;
88 const struct RoadStopSpec *roadstopspec = nullptr;
89 CargoType cargo_type{};
90 StationType type{};
91 uint8_t view = 0;
92 RoadType roadtype{};
93
94 RoadStopScopeResolver(ResolverObject &ro, BaseStation *st, const RoadStopSpec *roadstopspec, TileIndex tile, RoadType roadtype, StationType type, uint8_t view = 0)
95 : ScopeResolver(ro), tile(tile), st(st), roadstopspec(roadstopspec), type(type), view(view), roadtype(roadtype)
96 {
97 }
98
99 uint32_t GetRandomBits() const override;
100 uint32_t GetRandomTriggers() const override;
101
102 uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
103};
104
106 struct RoadStopResolverObject : public SpecializedResolverObject<StationRandomTriggers> {
107 RoadStopScopeResolver roadstop_scope;
108 std::optional<TownScopeResolver> town_scope = std::nullopt;
109
110 RoadStopResolverObject(const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, RoadType roadtype, StationType type, uint8_t view, CallbackID callback = CBID_NO_CALLBACK, uint32_t param1 = 0, uint32_t param2 = 0);
111
112 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override
113 {
114 switch (scope) {
115 case VSG_SCOPE_SELF: return &this->roadstop_scope;
116 case VSG_SCOPE_PARENT: {
117 TownScopeResolver *tsr = this->GetTown();
118 if (tsr != nullptr) return tsr;
119 [[fallthrough]];
120 }
121 default: return ResolverObject::GetScope(scope, relative);
122 }
123 }
124
125 TownScopeResolver *GetTown();
126};
127
129 struct RoadStopSpec : NewGRFSpecBase<RoadStopClassID> {
130 CargoGRFFileProps grf_prop;
131 StringID name;
132
133 RoadStopAvailabilityType stop_type = ROADSTOPTYPE_ALL;
134 RoadStopDrawModes draw_mode = {RoadStopDrawMode::Road, RoadStopDrawMode::Overlay};
135 RoadStopCallbackMasks callback_mask{};
136 RoadStopSpecFlags flags{};
137
138 CargoTypes cargo_triggers = 0;
139
140 AnimationInfo<StationAnimationTriggers> animation;
141
142 uint8_t build_cost_multiplier = 16;
143 uint8_t clear_cost_multiplier = 16;
144
145 std::array<BridgeableTileInfo, 6> bridgeable_info{};
146 std::vector<BadgeID> badges;
147
152 Money GetBuildCost(Price category) const { return GetPrice(category, this->build_cost_multiplier, this->grf_prop.grffile, -4); }
153
158 Money GetClearCost(Price category) const { return GetPrice(category, this->clear_cost_multiplier, this->grf_prop.grffile, -4); }
159
160 static const RoadStopSpec *Get(uint16_t index);
161};
162
163using RoadStopClass = NewGRFClass<RoadStopSpec, RoadStopClassID, ROADSTOP_CLASS_MAX>;
164
165std::optional<SpriteLayoutProcessor> GetRoadStopLayout(TileInfo *ti, const RoadStopSpec *spec, BaseStation *st, StationType type, int view, std::span<int32_t> regs100 = {});
166void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec, StationType type, int view);
167
168uint16_t GetRoadStopCallback(CallbackID callback, uint32_t param1, uint32_t param2, const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, RoadType roadtype, StationType type, uint8_t view, std::span<int32_t> regs100 = {});
169
170void AnimateRoadStopTile(TileIndex tile);
171uint8_t GetRoadStopTileAnimationSpeed(TileIndex tile);
172void TriggerRoadStopAnimation(BaseStation *st, TileIndex tile, StationAnimationTrigger trigger, CargoType cargo_type = INVALID_CARGO);
173void TriggerRoadStopRandomisation(BaseStation *st, TileIndex tile, StationRandomTrigger trigger, CargoType cargo_type = INVALID_CARGO);
174
175bool GetIfNewStopsByType(RoadStopType rs, RoadType roadtype);
176bool GetIfClassHasNewStopsByType(const RoadStopClass *roadstopclass, RoadStopType rs, RoadType roadtype);
177bool GetIfStopIsForType(const RoadStopSpec *roadstopspec, RoadStopType rs, RoadType roadtype);
178
179const RoadStopSpec *GetRoadStopSpec(TileIndex t);
180std::optional<uint8_t> AllocateSpecToRoadStop(const RoadStopSpec *spec, BaseStation *st);
181void AssignSpecToRoadStop(const RoadStopSpec *spec, BaseStation *st, uint8_t specindex);
182void DeallocateSpecFromRoadStop(BaseStation *st, uint8_t specindex);
183void RoadStopUpdateCachedTriggers(BaseStation *st);
184
190 inline bool IsWaypointClass(const RoadStopClass &cls)
191{
192 return cls.global_id == ROADSTOP_CLASS_LABEL_WAYPOINT || GB(cls.global_id, 24, 8) == UINT8_MAX;
193}
194
195#endif /* NEWGRF_ROADSTATION_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.
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
EnumBitSet< RoadStopDrawMode, 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
OverflowSafeInt< int64_t >
GetPrice
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition economy.cpp:949
Price
Price
Enumeration of all base prices for use with Prices.
Definition economy_type.h:95
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
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.
RoadStopDrawMode
RoadStopDrawMode
Different draw modes to disallow rendering of some parts of the stop or road.
Definition newgrf_roadstop.h:57
RoadStopDrawMode::Overlay
@ Overlay
Drive-through stops: Draw the road overlay, e.g. pavement.
RoadStopDrawMode::WaypGround
@ WaypGround
Waypoints: Draw the sprite layout ground tile (on top of the road)
RoadStopDrawMode::Road
@ Road
Bay stops: Draw the road itself.
IsWaypointClass
bool IsWaypointClass(const RoadStopClass &cls)
Test if a RoadStopClass is the waypoint class.
Definition newgrf_roadstop.h:190
TriggerRoadStopRandomisation
void TriggerRoadStopRandomisation(BaseStation *st, TileIndex tile, StationRandomTrigger trigger, CargoType cargo_type=INVALID_CARGO)
Trigger road stop randomisation.
Definition newgrf_roadstop.cpp:418
RoadStopClassID
RoadStopClassID
Definition newgrf_roadstop.h:33
ROADSTOP_CLASS_BEGIN
@ ROADSTOP_CLASS_BEGIN
The lowest valid value.
Definition newgrf_roadstop.h:34
ROADSTOP_CLASS_DFLT
@ ROADSTOP_CLASS_DFLT
Default road stop class.
Definition newgrf_roadstop.h:35
ROADSTOP_CLASS_MAX
@ ROADSTOP_CLASS_MAX
Maximum number of classes.
Definition newgrf_roadstop.h:37
ROADSTOP_CLASS_WAYP
@ ROADSTOP_CLASS_WAYP
Waypoint class.
Definition newgrf_roadstop.h:36
DeallocateSpecFromRoadStop
void DeallocateSpecFromRoadStop(BaseStation *st, uint8_t specindex)
Deallocate a RoadStopSpec from a Station.
Definition newgrf_roadstop.cpp:618
AllocateSpecToRoadStop
std::optional< uint8_t > AllocateSpecToRoadStop(const RoadStopSpec *spec, BaseStation *st)
Allocate a RoadStopSpec to a Station.
Definition newgrf_roadstop.cpp:568
RoadStopUpdateCachedTriggers
void RoadStopUpdateCachedTriggers(BaseStation *st)
Update the cached animation trigger bitmask for a station.
Definition newgrf_roadstop.cpp:659
AssignSpecToRoadStop
void AssignSpecToRoadStop(const RoadStopSpec *spec, BaseStation *st, uint8_t specindex)
Assign a previously allocated RoadStopSpec specindex to a Station.
Definition newgrf_roadstop.cpp:601
RoadStopAvailabilityType
RoadStopAvailabilityType
Various different options for availability, restricting the roadstop to be only for busses or for tru...
Definition newgrf_roadstop.h:45
ROADSTOPTYPE_FREIGHT
@ ROADSTOPTYPE_FREIGHT
This RoadStop is for freight (truck) stops.
Definition newgrf_roadstop.h:47
ROADSTOPTYPE_ALL
@ ROADSTOPTYPE_ALL
This RoadStop is for both types of station road stops.
Definition newgrf_roadstop.h:48
ROADSTOPTYPE_PASSENGER
@ ROADSTOPTYPE_PASSENGER
This RoadStop is for passenger (bus) stops.
Definition newgrf_roadstop.h:46
GetIfClassHasNewStopsByType
bool GetIfClassHasNewStopsByType(const RoadStopClass *roadstopclass, RoadStopType rs, RoadType roadtype)
Checks if the given RoadStopClass has any specs assigned to it, compatible with the given RoadStopTyp...
Definition newgrf_roadstop.cpp:513
DrawRoadStopTile
void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec, StationType type, int view)
Draw representation of a road stop tile for GUI purposes.
Definition newgrf_roadstop.cpp:282
GetIfNewStopsByType
bool GetIfNewStopsByType(RoadStopType rs, RoadType roadtype)
Checks if there's any new stations by a specific RoadStopType.
Definition newgrf_roadstop.cpp:494
NUM_ROADSTOPS_PER_GRF
static const int NUM_ROADSTOPS_PER_GRF
The maximum amount of roadstops a single GRF is allowed to add.
Definition newgrf_roadstop.h:28
RoadStopView
RoadStopView
Definition newgrf_roadstop.h:75
RSV_BAY_NW
@ RSV_BAY_NW
Bay road stop, facing Northwest.
Definition newgrf_roadstop.h:79
RSV_BAY_SW
@ RSV_BAY_SW
Bay road stop, facing Southwest.
Definition newgrf_roadstop.h:78
RSV_BAY_SE
@ RSV_BAY_SE
Bay road stop, facing Southeast.
Definition newgrf_roadstop.h:77
RSV_BAY_NE
@ RSV_BAY_NE
Bay road stop, facing Northeast.
Definition newgrf_roadstop.h:76
RSV_DRIVE_THROUGH_X
@ RSV_DRIVE_THROUGH_X
Drive through road stop, X axis.
Definition newgrf_roadstop.h:80
RSV_DRIVE_THROUGH_Y
@ RSV_DRIVE_THROUGH_Y
Drive through road stop, Y axis.
Definition newgrf_roadstop.h:81
RoadStopSpecFlag
RoadStopSpecFlag
Definition newgrf_roadstop.h:64
RoadStopSpecFlag::NoCatenary
@ NoCatenary
Do not show catenary.
RoadStopSpecFlag::NoAutoRoadConnection
@ NoAutoRoadConnection
No auto road connection.
RoadStopSpecFlag::RoadOnly
@ RoadOnly
Only show in the road build menu (not tram).
RoadStopSpecFlag::Cb141RandomBits
@ Cb141RandomBits
Callback 141 needs random bits.
RoadStopSpecFlag::TramOnly
@ TramOnly
Only show in the tram build menu (not road).
RoadStopSpecFlag::DriveThroughOnly
@ DriveThroughOnly
Stop is drive-through only.
RoadStopSpecFlag::DrawModeRegister
@ DrawModeRegister
Read draw mode from register 0x100.
GetIfStopIsForType
bool GetIfStopIsForType(const RoadStopSpec *roadstopspec, RoadStopType rs, RoadType roadtype)
Checks if the given RoadStopSpec is compatible with the given RoadStopType.
Definition newgrf_roadstop.cpp:528
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
newgrf_town.h
Functions to handle the town part of NewGRF towns.
road.h
Road specific functions.
RoadType
RoadType
The different roadtypes we support.
Definition road_type.h:23
RoadStopType
RoadStopType
Types of RoadStops.
Definition station_type.h:43
StationType
StationType
Station types.
Definition station_type.h:29
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
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition newgrf_commons.h:318
NewGRFSpecBase
Definition newgrf_class.h:17
NewGRFSpecBase< RoadStopClassID >::index
uint16_t index
Index within class of this spec, invalid until inserted into class.
Definition newgrf_class.h:19
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition newgrf_spritegroup.h:288
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition newgrf_spritegroup.h:340
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
RoadStopResolverObject
Road stop resolver.
Definition newgrf_roadstop.h:106
RoadStopResolverObject::town_scope
std::optional< TownScopeResolver > town_scope
The town scope resolver (created on the first call).
Definition newgrf_roadstop.h:108
RoadStopResolverObject::roadstop_scope
RoadStopScopeResolver roadstop_scope
The stop scope resolver.
Definition newgrf_roadstop.h:107
RoadStopResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
Definition newgrf_roadstop.h:112
RoadStopScopeResolver
Scope resolver for road stops.
Definition newgrf_roadstop.h:85
RoadStopScopeResolver::view
uint8_t view
Station axis.
Definition newgrf_roadstop.h:91
RoadStopScopeResolver::roadtype
RoadType roadtype
Road type (used when no tile)
Definition newgrf_roadstop.h:92
RoadStopScopeResolver::GetVariable
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
Definition newgrf_roadstop.cpp:72
RoadStopScopeResolver::type
StationType type
Station type.
Definition newgrf_roadstop.h:90
RoadStopScopeResolver::roadstopspec
const struct RoadStopSpec * roadstopspec
Station (type) specification.
Definition newgrf_roadstop.h:88
RoadStopScopeResolver::st
struct BaseStation * st
Instance of the station.
Definition newgrf_roadstop.h:87
RoadStopScopeResolver::GetRandomTriggers
uint32_t GetRandomTriggers() const override
Get the triggers.
Definition newgrf_roadstop.cpp:67
RoadStopScopeResolver::cargo_type
CargoType cargo_type
Type of cargo of the station.
Definition newgrf_roadstop.h:89
RoadStopScopeResolver::tile
TileIndex tile
Tile of the station.
Definition newgrf_roadstop.h:86
RoadStopScopeResolver::GetRandomBits
uint32_t GetRandomBits() const override
Get a few random bits.
Definition newgrf_roadstop.cpp:56
RoadStopSpec
Road stop specification.
Definition newgrf_roadstop.h:129
RoadStopSpec::name
StringID name
Name of this stop.
Definition newgrf_roadstop.h:131
RoadStopSpec::clear_cost_multiplier
uint8_t clear_cost_multiplier
Clear cost multiplier per tile.
Definition newgrf_roadstop.h:143
RoadStopSpec::GetBuildCost
Money GetBuildCost(Price category) const
Get the cost for building a road stop of this type.
Definition newgrf_roadstop.h:152
RoadStopSpec::build_cost_multiplier
uint8_t build_cost_multiplier
Build cost multiplier per tile.
Definition newgrf_roadstop.h:142
RoadStopSpec::bridgeable_info
std::array< BridgeableTileInfo, 6 > bridgeable_info
Per tile layout bridge information.
Definition newgrf_roadstop.h:145
RoadStopSpec::cargo_triggers
CargoTypes cargo_triggers
Bitmask of cargo types which cause trigger re-randomizing.
Definition newgrf_roadstop.h:138
RoadStopSpec::grf_prop
CargoGRFFileProps grf_prop
Link to NewGRF.
Definition newgrf_roadstop.h:130
RoadStopSpec::GetClearCost
Money GetClearCost(Price category) const
Get the cost for clearing a road stop of this type.
Definition newgrf_roadstop.h:158
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
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
TileInfo
Tile information, used while rendering the tile.
Definition tile_cmd.h:30
TownScopeResolver
Scope resolver for a town.
Definition newgrf_town.h:22