OpenTTD Source: src/newgrf_roadstop.h Source File

OpenTTD Source 15.0-beta1

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 <http://www.gnu.org/licenses/>.
6 */
7
12#ifndef NEWGRF_ROADSTATION_H
13#define NEWGRF_ROADSTATION_H
14
15#include "newgrf_animation_type.h"
16#include "newgrf_spritegroup.h"
17#include "newgrf_class.h"
18#include "newgrf_commons.h"
19#include "newgrf_town.h"
20#include "road.h"
21
23 static const int NUM_ROADSTOPS_PER_GRF = UINT16_MAX - 1;
24
25static const uint32_t ROADSTOP_CLASS_LABEL_DEFAULT = 'DFLT';
26static const uint32_t ROADSTOP_CLASS_LABEL_WAYPOINT = 'WAYP';
27
28 enum RoadStopClassID : uint16_t {
29 ROADSTOP_CLASS_BEGIN = 0,
30 ROADSTOP_CLASS_DFLT = 0,
31 ROADSTOP_CLASS_WAYP,
32 ROADSTOP_CLASS_MAX = UINT16_MAX,
33};
34 DECLARE_POSTFIX_INCREMENT(RoadStopClassID)
35
36/* Some Triggers etc. */
37enum RoadStopRandomTrigger {
38 RSRT_NEW_CARGO,
39 RSRT_CARGO_TAKEN,
40 RSRT_VEH_ARRIVES,
41 RSRT_VEH_DEPARTS,
42 RSRT_VEH_LOADS,
43};
44
49 enum RoadStopAvailabilityType : uint8_t {
50 ROADSTOPTYPE_PASSENGER,
51 ROADSTOPTYPE_FREIGHT,
52 ROADSTOPTYPE_ALL,
53
54 ROADSTOPTYPE_END,
55};
56
61 enum RoadStopDrawMode : uint8_t {
62 ROADSTOP_DRAW_MODE_NONE = 0,
63 ROADSTOP_DRAW_MODE_ROAD = 1 << 0,
64 ROADSTOP_DRAW_MODE_OVERLAY = 1 << 1,
65 ROADSTOP_DRAW_MODE_WAYP_GROUND = 1 << 2,
66};
67 DECLARE_ENUM_AS_BIT_SET(RoadStopDrawMode)
68
69enum RoadStopSpecFlags {
70 RSF_CB141_RANDOM_BITS = 0,
71 RSF_NO_CATENARY = 2,
72 RSF_DRIVE_THROUGH_ONLY = 3,
73 RSF_NO_AUTO_ROAD_CONNECTION = 4,
74 RSF_BUILD_MENU_ROAD_ONLY = 5,
75 RSF_BUILD_MENU_TRAM_ONLY = 6,
76 RSF_DRAW_MODE_REGISTER = 8,
77};
78
79 enum RoadStopView {
80 RSV_BAY_NE = 0,
81 RSV_BAY_SE = 1,
82 RSV_BAY_SW = 2,
83 RSV_BAY_NW = 3,
84 RSV_DRIVE_THROUGH_X = 4,
85 RSV_DRIVE_THROUGH_Y = 5,
86};
87
89 struct RoadStopScopeResolver : public ScopeResolver {
90 TileIndex tile;
91 struct BaseStation *st;
92 const struct RoadStopSpec *roadstopspec;
93 CargoID cargo_type;
94 StationType type;
95 uint8_t view;
96 RoadType roadtype;
97
98 RoadStopScopeResolver(ResolverObject &ro, BaseStation *st, const RoadStopSpec *roadstopspec, TileIndex tile, RoadType roadtype, StationType type, uint8_t view = 0)
99 : ScopeResolver(ro), tile(tile), st(st), roadstopspec(roadstopspec), type(type), view(view), roadtype(roadtype)
100 {
101 }
102
103 uint32_t GetRandomBits() const override;
104 uint32_t GetTriggers() const override;
105
106 uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
107};
108
110 struct RoadStopResolverObject : public ResolverObject {
111 RoadStopScopeResolver roadstop_scope;
112 std::optional<TownScopeResolver> town_scope = std::nullopt;
113
114 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);
115
116 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override
117 {
118 switch (scope) {
119 case VSG_SCOPE_SELF: return &this->roadstop_scope;
120 case VSG_SCOPE_PARENT: {
121 TownScopeResolver *tsr = this->GetTown();
122 if (tsr != nullptr) return tsr;
123 [[fallthrough]];
124 }
125 default: return ResolverObject::GetScope(scope, relative);
126 }
127 }
128
129 TownScopeResolver *GetTown();
130
131 const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
132};
133
135 struct RoadStopSpec : NewGRFSpecBase<RoadStopClassID> {
142 GRFFilePropsBase<NUM_CARGO + 3> grf_prop;
143 StringID name;
144
145 RoadStopAvailabilityType stop_type = ROADSTOPTYPE_ALL;
146 RoadStopDrawMode draw_mode = ROADSTOP_DRAW_MODE_ROAD | ROADSTOP_DRAW_MODE_OVERLAY;
147 uint8_t callback_mask = 0;
148 uint16_t flags = 0;
149
150 CargoTypes cargo_triggers = 0;
151
152 AnimationInfo animation;
153
154 uint8_t bridge_height[6];
155 uint8_t bridge_disallowed_pillars[6];
156
157 uint8_t build_cost_multiplier = 16;
158 uint8_t clear_cost_multiplier = 16;
159
164 Money GetBuildCost(Price category) const { return GetPrice(category, this->build_cost_multiplier, this->grf_prop.grffile, -4); }
165
170 Money GetClearCost(Price category) const { return GetPrice(category, this->clear_cost_multiplier, this->grf_prop.grffile, -4); }
171
172 static const RoadStopSpec *Get(uint16_t index);
173};
174
175using RoadStopClass = NewGRFClass<RoadStopSpec, RoadStopClassID, ROADSTOP_CLASS_MAX>;
176
177void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec, StationType type, int view);
178
179uint16_t GetRoadStopCallback(CallbackID callback, uint32_t param1, uint32_t param2, const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, RoadType roadtype, StationType type, uint8_t view);
180
181void AnimateRoadStopTile(TileIndex tile);
182uint8_t GetRoadStopTileAnimationSpeed(TileIndex tile);
183void TriggerRoadStopAnimation(BaseStation *st, TileIndex tile, StationAnimationTrigger trigger, CargoID cargo_type = INVALID_CARGO);
184void TriggerRoadStopRandomisation(Station *st, TileIndex tile, RoadStopRandomTrigger trigger, CargoID cargo_type = INVALID_CARGO);
185
186bool GetIfNewStopsByType(RoadStopType rs, RoadType roadtype);
187bool GetIfClassHasNewStopsByType(const RoadStopClass *roadstopclass, RoadStopType rs, RoadType roadtype);
188bool GetIfStopIsForType(const RoadStopSpec *roadstopspec, RoadStopType rs, RoadType roadtype);
189
190const RoadStopSpec *GetRoadStopSpec(TileIndex t);
191int AllocateSpecToRoadStop(const RoadStopSpec *statspec, BaseStation *st, bool exec);
192void DeallocateSpecFromRoadStop(BaseStation *st, uint8_t specindex);
193void RoadStopUpdateCachedTriggers(BaseStation *st);
194
200 inline bool IsWaypointClass(const RoadStopClass &cls)
201{
202 return cls.global_id == ROADSTOP_CLASS_LABEL_WAYPOINT || GB(cls.global_id, 24, 8) == UINT8_MAX;
203}
204
205#endif /* NEWGRF_ROADSTATION_H */
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
CargoID
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
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:966
Price
Price
Enumeration of all base prices for use with Prices.
Definition economy_type.h:89
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
newgrf_animation_type.h
Definitions related to NewGRF animation.
StationAnimationTrigger
StationAnimationTrigger
Animation triggers for station.
Definition newgrf_animation_type.h:26
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.
RoadStopDrawMode
RoadStopDrawMode
Different draw modes to disallow rendering of some parts of the stop or road.
Definition newgrf_roadstop.h:61
ROADSTOP_DRAW_MODE_ROAD
@ ROADSTOP_DRAW_MODE_ROAD
Bay stops: Draw the road itself.
Definition newgrf_roadstop.h:63
ROADSTOP_DRAW_MODE_OVERLAY
@ ROADSTOP_DRAW_MODE_OVERLAY
Drive-through stops: Draw the road overlay, e.g. pavement.
Definition newgrf_roadstop.h:64
ROADSTOP_DRAW_MODE_WAYP_GROUND
@ ROADSTOP_DRAW_MODE_WAYP_GROUND
Waypoints: Draw the sprite layout ground tile (on top of the road)
Definition newgrf_roadstop.h:65
RoadStopRandomTrigger
RoadStopRandomTrigger
Definition newgrf_roadstop.h:37
RSRT_VEH_ARRIVES
@ RSRT_VEH_ARRIVES
Trigger roadstop when road vehicle arrives.
Definition newgrf_roadstop.h:40
RSRT_CARGO_TAKEN
@ RSRT_CARGO_TAKEN
Trigger roadstop when cargo is completely taken.
Definition newgrf_roadstop.h:39
RSRT_NEW_CARGO
@ RSRT_NEW_CARGO
Trigger roadstop on arrival of new cargo.
Definition newgrf_roadstop.h:38
RSRT_VEH_DEPARTS
@ RSRT_VEH_DEPARTS
Trigger roadstop when road vehicle leaves.
Definition newgrf_roadstop.h:41
RSRT_VEH_LOADS
@ RSRT_VEH_LOADS
Trigger roadstop when road vehicle loads.
Definition newgrf_roadstop.h:42
IsWaypointClass
bool IsWaypointClass(const RoadStopClass &cls)
Test if a RoadStopClass is the waypoint class.
Definition newgrf_roadstop.h:200
RoadStopView
RoadStopView
Definition newgrf_roadstop.h:79
RSV_BAY_NW
@ RSV_BAY_NW
Bay road stop, facing Northwest.
Definition newgrf_roadstop.h:83
RSV_BAY_SW
@ RSV_BAY_SW
Bay road stop, facing Southwest.
Definition newgrf_roadstop.h:82
RSV_BAY_SE
@ RSV_BAY_SE
Bay road stop, facing Southeast.
Definition newgrf_roadstop.h:81
RSV_BAY_NE
@ RSV_BAY_NE
Bay road stop, facing Northeast.
Definition newgrf_roadstop.h:80
RSV_DRIVE_THROUGH_X
@ RSV_DRIVE_THROUGH_X
Drive through road stop, X axis.
Definition newgrf_roadstop.h:84
RSV_DRIVE_THROUGH_Y
@ RSV_DRIVE_THROUGH_Y
Drive through road stop, Y axis.
Definition newgrf_roadstop.h:85
RoadStopClassID
RoadStopClassID
Definition newgrf_roadstop.h:28
ROADSTOP_CLASS_BEGIN
@ ROADSTOP_CLASS_BEGIN
The lowest valid value.
Definition newgrf_roadstop.h:29
ROADSTOP_CLASS_DFLT
@ ROADSTOP_CLASS_DFLT
Default road stop class.
Definition newgrf_roadstop.h:30
ROADSTOP_CLASS_MAX
@ ROADSTOP_CLASS_MAX
Maximum number of classes.
Definition newgrf_roadstop.h:32
ROADSTOP_CLASS_WAYP
@ ROADSTOP_CLASS_WAYP
Waypoint class.
Definition newgrf_roadstop.h:31
RoadStopUpdateCachedTriggers
void RoadStopUpdateCachedTriggers(BaseStation *st)
Update the cached animation trigger bitmask for a station.
Definition newgrf_roadstop.cpp:623
RoadStopAvailabilityType
RoadStopAvailabilityType
Various different options for availability, restricting the roadstop to be only for busses or for tru...
Definition newgrf_roadstop.h:49
ROADSTOPTYPE_FREIGHT
@ ROADSTOPTYPE_FREIGHT
This RoadStop is for freight (truck) stops.
Definition newgrf_roadstop.h:51
ROADSTOPTYPE_ALL
@ ROADSTOPTYPE_ALL
This RoadStop is for both types of station road stops.
Definition newgrf_roadstop.h:52
ROADSTOPTYPE_PASSENGER
@ ROADSTOPTYPE_PASSENGER
This RoadStop is for passenger (bus) stops.
Definition newgrf_roadstop.h:50
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:500
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:281
RoadStopSpecFlags
RoadStopSpecFlags
Definition newgrf_roadstop.h:69
RSF_NO_AUTO_ROAD_CONNECTION
@ RSF_NO_AUTO_ROAD_CONNECTION
No auto road connection.
Definition newgrf_roadstop.h:73
RSF_NO_CATENARY
@ RSF_NO_CATENARY
Do not show catenary.
Definition newgrf_roadstop.h:71
RSF_CB141_RANDOM_BITS
@ RSF_CB141_RANDOM_BITS
Callback 141 needs random bits.
Definition newgrf_roadstop.h:70
RSF_DRAW_MODE_REGISTER
@ RSF_DRAW_MODE_REGISTER
Read draw mode from register 0x100.
Definition newgrf_roadstop.h:76
RSF_DRIVE_THROUGH_ONLY
@ RSF_DRIVE_THROUGH_ONLY
Stop is drive-through only.
Definition newgrf_roadstop.h:72
RSF_BUILD_MENU_ROAD_ONLY
@ RSF_BUILD_MENU_ROAD_ONLY
Only show in the road build menu (not tram).
Definition newgrf_roadstop.h:74
RSF_BUILD_MENU_TRAM_ONLY
@ RSF_BUILD_MENU_TRAM_ONLY
Only show in the tram build menu (not road).
Definition newgrf_roadstop.h:75
GetIfNewStopsByType
bool GetIfNewStopsByType(RoadStopType rs, RoadType roadtype)
Checks if there's any new stations by a specific RoadStopType.
Definition newgrf_roadstop.cpp:481
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:23
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:515
TriggerRoadStopRandomisation
void TriggerRoadStopRandomisation(Station *st, TileIndex tile, RoadStopRandomTrigger trigger, CargoID cargo_type=INVALID_CARGO)
Trigger road stop randomisation.
Definition newgrf_roadstop.cpp:408
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
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:25
RoadStopType
RoadStopType
Types of RoadStops.
Definition station_type.h:45
StationType
StationType
Station types.
Definition station_type.h:31
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
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition newgrf_commons.h:313
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
RealSpriteGroup
Definition newgrf_spritegroup.h:79
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition newgrf_spritegroup.h:296
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition newgrf_spritegroup.h:314
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
RoadStopResolverObject
Road stop resolver.
Definition newgrf_roadstop.h:110
RoadStopResolverObject::ResolveReal
const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const override
Get the real sprites of the grf.
Definition newgrf_roadstop.cpp:214
RoadStopResolverObject::town_scope
std::optional< TownScopeResolver > town_scope
The town scope resolver (created on the first call).
Definition newgrf_roadstop.h:112
RoadStopResolverObject::roadstop_scope
RoadStopScopeResolver roadstop_scope
The stop scope resolver.
Definition newgrf_roadstop.h:111
RoadStopResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
Definition newgrf_roadstop.h:116
RoadStopScopeResolver
Scope resolver for road stops.
Definition newgrf_roadstop.h:89
RoadStopScopeResolver::view
uint8_t view
Station axis.
Definition newgrf_roadstop.h:95
RoadStopScopeResolver::GetTriggers
uint32_t GetTriggers() const override
Get the triggers.
Definition newgrf_roadstop.cpp:62
RoadStopScopeResolver::roadtype
RoadType roadtype
Road type (used when no tile)
Definition newgrf_roadstop.h:96
RoadStopScopeResolver::GetVariable
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
Definition newgrf_roadstop.cpp:67
RoadStopScopeResolver::type
StationType type
Station type.
Definition newgrf_roadstop.h:94
RoadStopScopeResolver::roadstopspec
const struct RoadStopSpec * roadstopspec
Station (type) specification.
Definition newgrf_roadstop.h:92
RoadStopScopeResolver::cargo_type
CargoID cargo_type
Type of cargo of the station.
Definition newgrf_roadstop.h:93
RoadStopScopeResolver::st
struct BaseStation * st
Instance of the station.
Definition newgrf_roadstop.h:91
RoadStopScopeResolver::tile
TileIndex tile
Tile of the station.
Definition newgrf_roadstop.h:90
RoadStopScopeResolver::GetRandomBits
uint32_t GetRandomBits() const override
Get a few random bits.
Definition newgrf_roadstop.cpp:51
RoadStopSpec
Road stop specification.
Definition newgrf_roadstop.h:135
RoadStopSpec::name
StringID name
Name of this stop.
Definition newgrf_roadstop.h:143
RoadStopSpec::bridge_height
uint8_t bridge_height[6]
Minimum height for a bridge above, 0 for none.
Definition newgrf_roadstop.h:154
RoadStopSpec::clear_cost_multiplier
uint8_t clear_cost_multiplier
Clear cost multiplier per tile.
Definition newgrf_roadstop.h:158
RoadStopSpec::GetBuildCost
Money GetBuildCost(Price category) const
Get the cost for building a road stop of this type.
Definition newgrf_roadstop.h:164
RoadStopSpec::build_cost_multiplier
uint8_t build_cost_multiplier
Build cost multiplier per tile.
Definition newgrf_roadstop.h:157
RoadStopSpec::cargo_triggers
CargoTypes cargo_triggers
Bitmask of cargo types which cause trigger re-randomizing.
Definition newgrf_roadstop.h:150
RoadStopSpec::bridge_disallowed_pillars
uint8_t bridge_disallowed_pillars[6]
Disallowed pillar flags for a bridge above.
Definition newgrf_roadstop.h:155
RoadStopSpec::grf_prop
GRFFilePropsBase< NUM_CARGO+3 > grf_prop
Properties related the the grf file.
Definition newgrf_roadstop.h:142
RoadStopSpec::GetClearCost
Money GetClearCost(Price category) const
Get the cost for clearing a road stop of this type.
Definition newgrf_roadstop.h:170
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
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