OpenTTD Source 20260421-master-gc2fbc6fdeb
newgrf_generic.cpp
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
9
10#include "stdafx.h"
11#include "debug.h"
12#include "newgrf_spritegroup.h"
13#include "industrytype.h"
14#include "core/random_func.hpp"
15#include "newgrf_sound.h"
16#include "water_map.h"
17
18#include "safeguards.h"
19
21struct GenericScopeResolver : public ScopeResolver {
22 CargoType cargo_type;
23 uint8_t default_selection;
24 uint8_t src_industry;
25 uint8_t dst_industry;
26 uint8_t distance;
28 uint8_t count;
29 uint8_t station_size;
30
31 GrfSpecFeature feature;
32
39 : ScopeResolver(ro), cargo_type(0), default_selection(0), src_industry(0), dst_industry(0), distance(0),
40 event(), count(0), station_size(0), feature(GrfSpecFeature::Invalid), ai_callback(ai_callback)
41 {
42 }
43
44 uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
45
46private:
48};
49
50
53 GenericScopeResolver generic_scope;
54
56
57 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override
58 {
59 switch (scope) {
60 case VSG_SCOPE_SELF: return &this->generic_scope;
61 default: return ResolverObject::GetScope(scope, relative);
62 }
63 }
64
65 GrfSpecFeature GetFeature() const override
66 {
67 return (GrfSpecFeature)this->generic_scope.feature;
68 }
69
70 uint32_t GetDebugID() const override
71 {
72 return 0;
73 }
74};
75
76struct GenericCallback {
77 const GRFFile *file;
78 const SpriteGroup *group;
79
80 GenericCallback(const GRFFile *file, const SpriteGroup *group) :
81 file(file),
82 group(group)
83 { }
84};
85
86typedef std::list<GenericCallback> GenericCallbackList;
87
90
91
96{
97 for (auto &gcl : _gcl) {
98 gcl.clear();
99 }
100}
101
102
109void AddGenericCallback(GrfSpecFeature feature, const GRFFile *file, const SpriteGroup *group)
110{
111 if (to_underlying(feature) >= std::size(_gcl)) {
112 GrfMsg(5, "AddGenericCallback: Unsupported feature 0x{:02X}", feature);
113 return;
114 }
115
116 /* Generic feature callbacks are evaluated in reverse (i.e. the last group
117 * to be added is evaluated first, etc) thus we push the group to the
118 * beginning of the list so a standard iterator will do the right thing. */
119 _gcl[feature].push_front(GenericCallback(file, group));
120}
121
122/* virtual */ uint32_t GenericScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
123{
124 if (this->ai_callback) {
125 switch (variable) {
126 case 0x40: return this->ro.grffile->cargo_map[this->cargo_type];
127
128 case 0x80: return this->cargo_type;
129 case 0x81: return CargoSpec::Get(this->cargo_type)->bitnum;
130 case 0x82: return this->default_selection;
131 case 0x83: return this->src_industry;
132 case 0x84: return this->dst_industry;
133 case 0x85: return this->distance;
134 case 0x86: return this->event;
135 case 0x87: return this->count;
136 case 0x88: return this->station_size;
137
138 default: break;
139 }
140 }
141
142 Debug(grf, 1, "Unhandled generic feature variable 0x{:02X}", variable);
143
144 available = false;
145 return UINT_MAX;
146}
147
153GenericResolverObject::GenericResolverObject(bool ai_callback, CallbackID callback) : ResolverObject(nullptr, callback), generic_scope(*this, ai_callback)
154{
155}
156
157
168static std::pair<const GRFFile *, uint16_t> GetGenericCallbackResult(GrfSpecFeature feature, ResolverObject &object, uint32_t param1_grfv7, uint32_t param1_grfv8, std::span<int32_t> regs100 = {})
169{
170 assert(to_underlying(feature) < std::size(_gcl));
171
172 /* Test each feature callback sprite group. */
173 for (const auto &it : _gcl[feature]) {
174 object.grffile = it.file;
175 object.root_spritegroup = it.group;
176 /* Set callback param based on GRF version. */
177 object.callback_param1 = it.file->grf_version >= 8 ? param1_grfv8 : param1_grfv7;
178 uint16_t result = object.ResolveCallback(regs100);
179 if (result == CALLBACK_FAILED) continue;
180
181 return {it.file, result};
182 }
183
184 /* No callback returned a valid result, so we've failed. */
185 return {nullptr, CALLBACK_FAILED};
186}
187
188
203std::pair<const GRFFile *, uint16_t> GetAiPurchaseCallbackResult(GrfSpecFeature feature, CargoType cargo_type, uint8_t default_selection, IndustryType src_industry, IndustryType dst_industry, uint8_t distance, AIConstructionEvent event, uint8_t count, uint8_t station_size)
204{
206
207 if (src_industry != IT_AI_UNKNOWN && src_industry != IT_AI_TOWN) {
208 const IndustrySpec *is = GetIndustrySpec(src_industry);
209 /* If this is no original industry, use the substitute type */
210 if (is->grf_prop.subst_id != IT_INVALID) src_industry = is->grf_prop.subst_id;
211 }
212
213 if (dst_industry != IT_AI_UNKNOWN && dst_industry != IT_AI_TOWN) {
214 const IndustrySpec *is = GetIndustrySpec(dst_industry);
215 /* If this is no original industry, use the substitute type */
216 if (is->grf_prop.subst_id != IT_INVALID) dst_industry = is->grf_prop.subst_id;
217 }
218
219 object.generic_scope.cargo_type = cargo_type;
220 object.generic_scope.default_selection = default_selection;
221 object.generic_scope.src_industry = src_industry;
222 object.generic_scope.dst_industry = dst_industry;
223 object.generic_scope.distance = distance;
224 object.generic_scope.event = event;
225 object.generic_scope.count = count;
226 object.generic_scope.station_size = station_size;
227 object.generic_scope.feature = feature;
228
229 auto callback = GetGenericCallbackResult(feature, object, 0, 0);
230 if (callback.second != CALLBACK_FAILED && callback.first->grf_version < 8) callback.second = GB(callback.second, 0, 8);
231 return callback;
232}
233
234
240{
242
243 /* Only run every 1/200-th time. */
244 uint32_t r; // Save for later
245 if (!Chance16R(1, 200, r) || !_settings_client.sound.ambient) return;
246
247 /* Prepare resolver object. */
249 object.generic_scope.feature = GrfSpecFeature::SoundEffects;
250
251 uint32_t param1_v7 = to_underlying(GetTileType(tile)) << 28 | Clamp(TileHeight(tile), 0, 15) << 24 | GB(r, 16, 8) << 16 | GetTerrainType(tile);
252 uint32_t param1_v8 = to_underlying(GetTileType(tile)) << 24 | GetTileZ(tile) << 16 | GB(r, 16, 8) << 8 | (HasTileWaterClass(tile) ? to_underlying(GetWaterClass(tile)) : 0) << 3 | GetTerrainType(tile);
253
254 /* Run callback. */
255 auto callback = GetGenericCallbackResult(GrfSpecFeature::SoundEffects, object, param1_v7, param1_v8);
256
257 if (callback.second != CALLBACK_FAILED) PlayTileSound(callback.first, callback.second, tile);
258}
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
@ Invalid
Invalid town production effect.
Definition cargotype.h:46
A sort-of mixin that implements 'at(pos)' and 'operator[](pos)' only for a specific enum class.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
const IndustrySpec * GetIndustrySpec(IndustryType thistype)
Accessor for array _industry_specs.
Industry type specs.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
GrfSpecFeature
Definition newgrf.h:72
@ SoundEffects
Sound effects feature.
Definition newgrf.h:85
@ End
End marker.
Definition newgrf.h:95
CallbackID
List of implemented NewGRF callbacks.
@ CBID_SOUNDS_AMBIENT_EFFECT
Select an ambient sound to play for a given type of tile.
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
@ CBID_GENERIC_AI_PURCHASE_SELECTION
AI construction/purchase selection.
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
uint32_t GetTerrainType(TileIndex tile, TileContext context)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
std::pair< const GRFFile *, uint16_t > GetAiPurchaseCallbackResult(GrfSpecFeature feature, CargoType cargo_type, uint8_t default_selection, IndustryType src_industry, IndustryType dst_industry, uint8_t distance, AIConstructionEvent event, uint8_t count, uint8_t station_size)
'Execute' an AI purchase selection callback
void AmbientSoundEffectCallback(TileIndex tile)
'Execute' the ambient sound effect callback.
static std::pair< const GRFFile *, uint16_t > GetGenericCallbackResult(GrfSpecFeature feature, ResolverObject &object, uint32_t param1_grfv7, uint32_t param1_grfv8, std::span< int32_t > regs100={})
Follow a generic feature callback list and return the first successful answer.
void AddGenericCallback(GrfSpecFeature feature, const GRFFile *file, const SpriteGroup *group)
Add a generic feature callback sprite group to the appropriate feature list.
void ResetGenericCallbacks()
Reset all generic feature callback sprite groups.
static EnumClassIndexContainer< std::array< GenericCallbackList, to_underlying(GrfSpecFeature::End)>, GrfSpecFeature > _gcl
Generic callbacks for each feature.
static const IndustryType IT_AI_UNKNOWN
The AI has no specific industry in mind.
AIConstructionEvent
AI events for asking the NewGRF for information.
static const IndustryType IT_AI_TOWN
The AI actually wants to transport to/from a town, not an industry.
void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
Play a NewGRF sound effect at the location of a specific tile.
Functions related to NewGRF provided sounds.
Action 2 handling.
VarSpriteGroupScope
Shared by deterministic and random groups.
@ VSG_SCOPE_SELF
Resolved object itself.
Pseudo random number generator.
bool Chance16R(const uint32_t a, const uint32_t b, uint32_t &r, const std::source_location location=std::source_location::current())
Flips a coin with a given probability and saves the randomize-number in a variable.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Definition of base types and functions in a cross-platform compatible way.
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:139
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition cargotype.h:77
Dynamic data of a loaded NewGRF.
Definition newgrf.h:118
Resolver object for generic objects/properties.
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
GenericResolverObject(bool ai_callback, CallbackID callback=CBID_NO_CALLBACK)
Generic resolver.
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Scope resolver for generic objects and properties.
uint8_t dst_industry
Destination industry substitute type. 0xFF for "town", 0xFE for "unknown".
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
bool ai_callback
Callback comes from the AI.
GenericScopeResolver(ResolverObject &ro, bool ai_callback)
Generic scope resolver.
uint8_t src_industry
Source industry substitute type. 0xFF for "town", 0xFE for "unknown".
Defines the data structure for constructing industry.
SubstituteGRFFileProps grf_prop
properties related to the grf file
Interface for SpriteGroup-s to access the gamestate.
ResolverObject(const GRFFile *grffile, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Resolver constructor.
CallbackID callback
Callback being resolved.
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.
Common wrapper for all the different sprite group types.
uint16_t subst_id
The id of the entity to replace.
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.cpp:115
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
static uint TileHeight(Tile tile)
Returns the height of a tile.
Definition tile_map.h:29
static TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
@ Water
Water tile.
Definition tile_type.h:55
@ Trees
Tile with one or more trees.
Definition tile_type.h:53
@ Clear
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:49
Map accessors for water tiles.
bool HasTileWaterClass(Tile t)
Checks whether the tile has an waterclass associated.
Definition water_map.h:103
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition water_map.h:114