OpenTTD Source 20250524-master-gc366e6a48e
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 <http://www.gnu.org/licenses/>.
6 */
7
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
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(GSF_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
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
88static GenericCallbackList _gcl[GSF_END];
89
90
95{
96 for (auto &gcl : _gcl) {
97 gcl.clear();
98 }
99}
100
101
108void AddGenericCallback(GrfSpecFeature feature, const GRFFile *file, const SpriteGroup *group)
109{
110 if (feature >= lengthof(_gcl)) {
111 GrfMsg(5, "AddGenericCallback: Unsupported feature 0x{:02X}", feature);
112 return;
113 }
114
115 /* Generic feature callbacks are evaluated in reverse (i.e. the last group
116 * to be added is evaluated first, etc) thus we push the group to the
117 * beginning of the list so a standard iterator will do the right thing. */
118 _gcl[feature].push_front(GenericCallback(file, group));
119}
120
121/* virtual */ uint32_t GenericScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
122{
123 if (this->ai_callback) {
124 switch (variable) {
125 case 0x40: return this->ro.grffile->cargo_map[this->cargo_type];
126
127 case 0x80: return this->cargo_type;
128 case 0x81: return CargoSpec::Get(this->cargo_type)->bitnum;
129 case 0x82: return this->default_selection;
130 case 0x83: return this->src_industry;
131 case 0x84: return this->dst_industry;
132 case 0x85: return this->distance;
133 case 0x86: return this->event;
134 case 0x87: return this->count;
135 case 0x88: return this->station_size;
136
137 default: break;
138 }
139 }
140
141 Debug(grf, 1, "Unhandled generic feature variable 0x{:02X}", variable);
142
143 available = false;
144 return UINT_MAX;
145}
146
152GenericResolverObject::GenericResolverObject(bool ai_callback, CallbackID callback) : ResolverObject(nullptr, callback), generic_scope(*this, ai_callback)
153{
154}
155
156
167static std::pair<const GRFFile *, uint16_t> GetGenericCallbackResult(GrfSpecFeature feature, ResolverObject &object, uint32_t param1_grfv7, uint32_t param1_grfv8, std::span<int32_t> regs100 = {})
168{
169 assert(feature < lengthof(_gcl));
170
171 /* Test each feature callback sprite group. */
172 for (const auto &it : _gcl[feature]) {
173 object.grffile = it.file;
174 object.root_spritegroup = it.group;
175 /* Set callback param based on GRF version. */
176 object.callback_param1 = it.file->grf_version >= 8 ? param1_grfv8 : param1_grfv7;
177 uint16_t result = object.ResolveCallback(regs100);
178 if (result == CALLBACK_FAILED) continue;
179
180 return {it.file, result};
181 }
182
183 /* No callback returned a valid result, so we've failed. */
184 return {nullptr, CALLBACK_FAILED};
185}
186
187
202std::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)
203{
205
206 if (src_industry != IT_AI_UNKNOWN && src_industry != IT_AI_TOWN) {
207 const IndustrySpec *is = GetIndustrySpec(src_industry);
208 /* If this is no original industry, use the substitute type */
209 if (is->grf_prop.subst_id != IT_INVALID) src_industry = is->grf_prop.subst_id;
210 }
211
212 if (dst_industry != IT_AI_UNKNOWN && dst_industry != IT_AI_TOWN) {
213 const IndustrySpec *is = GetIndustrySpec(dst_industry);
214 /* If this is no original industry, use the substitute type */
215 if (is->grf_prop.subst_id != IT_INVALID) dst_industry = is->grf_prop.subst_id;
216 }
217
218 object.generic_scope.cargo_type = cargo_type;
219 object.generic_scope.default_selection = default_selection;
220 object.generic_scope.src_industry = src_industry;
221 object.generic_scope.dst_industry = dst_industry;
222 object.generic_scope.distance = distance;
223 object.generic_scope.event = event;
224 object.generic_scope.count = count;
225 object.generic_scope.station_size = station_size;
226 object.generic_scope.feature = feature;
227
228 auto callback = GetGenericCallbackResult(feature, object, 0, 0);
229 if (callback.second != CALLBACK_FAILED && callback.first->grf_version < 8) callback.second = GB(callback.second, 0, 8);
230 return callback;
231}
232
233
239{
240 assert(IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES) || IsTileType(tile, MP_WATER));
241
242 /* Only run every 1/200-th time. */
243 uint32_t r; // Save for later
244 if (!Chance16R(1, 200, r) || !_settings_client.sound.ambient) return;
245
246 /* Prepare resolver object. */
248 object.generic_scope.feature = GSF_SOUNDFX;
249
250 uint32_t param1_v7 = GetTileType(tile) << 28 | Clamp(TileHeight(tile), 0, 15) << 24 | GB(r, 16, 8) << 16 | GetTerrainType(tile);
251 uint32_t param1_v8 = GetTileType(tile) << 24 | GetTileZ(tile) << 16 | GB(r, 16, 8) << 8 | (HasTileWaterClass(tile) ? GetWaterClass(tile) : 0) << 3 | GetTerrainType(tile);
252
253 /* Run callback. */
254 auto callback = GetGenericCallbackResult(GSF_SOUNDFX, object, param1_v7, param1_v8);
255
256 if (callback.second != CALLBACK_FAILED) PlayTileSound(callback.first, callback.second, tile);
257}
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.
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
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:69
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:100
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 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
@ 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:59
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:137
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition cargotype.h:76
SoundSettings sound
sound effect settings
Dynamic data of a loaded NewGRF.
Definition newgrf.h:115
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoType -> local ID)
Definition newgrf.h:137
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.
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
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.
bool ambient
Play ambient, industry and town sounds.
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.cpp:116
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
static debug_inline uint TileHeight(Tile tile)
Returns the height of a tile.
Definition tile_map.h:29
@ MP_TREES
Tile got trees.
Definition tile_type.h:52
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:48
@ MP_WATER
Water tile.
Definition tile_type.h:54
Map accessors for water tiles.
bool HasTileWaterClass(Tile t)
Checks whether the tile has an waterclass associated.
Definition water_map.h:101
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition water_map.h:112