OpenTTD Source: src/newgrf_generic.cpp Source File

OpenTTD Source 15.0-beta1

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
21 struct GenericScopeResolver : public ScopeResolver {
22 CargoID cargo_type;
23 uint8_t default_selection;
24 uint8_t src_industry;
25 uint8_t dst_industry;
26 uint8_t distance;
27 AIConstructionEvent event;
28 uint8_t count;
29 uint8_t station_size;
30
31 uint8_t feature;
32
38 GenericScopeResolver(ResolverObject &ro, bool ai_callback)
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:
47 bool ai_callback;
48};
49
50
52 struct GenericResolverObject : public ResolverObject {
53 GenericScopeResolver generic_scope;
54
55 GenericResolverObject(bool ai_callback, CallbackID callback = CBID_NO_CALLBACK);
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
76 struct 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
88static GenericCallbackList _gcl[GSF_END];
89
90
94 void ResetGenericCallbacks()
95{
96 for (auto &gcl : _gcl) {
97 gcl.clear();
98 }
99}
100
101
108 void AddGenericCallback(uint8_t 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
167 static uint16_t GetGenericCallbackResult(uint8_t feature, ResolverObject &object, uint32_t param1_grfv7, uint32_t param1_grfv8, const GRFFile **file)
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();
178 if (result == CALLBACK_FAILED) continue;
179
180 /* Return NewGRF file if necessary */
181 if (file != nullptr) *file = it.file;
182
183 return result;
184 }
185
186 /* No callback returned a valid result, so we've failed. */
187 return CALLBACK_FAILED;
188}
189
190
206uint16_t GetAiPurchaseCallbackResult(uint8_t feature, CargoID cargo_type, uint8_t default_selection, IndustryType src_industry, IndustryType dst_industry, uint8_t distance, AIConstructionEvent event, uint8_t count, uint8_t station_size, const GRFFile **file)
207{
208 GenericResolverObject object(true, CBID_GENERIC_AI_PURCHASE_SELECTION);
209
210 if (src_industry != IT_AI_UNKNOWN && src_industry != IT_AI_TOWN) {
211 const IndustrySpec *is = GetIndustrySpec(src_industry);
212 /* If this is no original industry, use the substitute type */
213 if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) src_industry = is->grf_prop.subst_id;
214 }
215
216 if (dst_industry != IT_AI_UNKNOWN && dst_industry != IT_AI_TOWN) {
217 const IndustrySpec *is = GetIndustrySpec(dst_industry);
218 /* If this is no original industry, use the substitute type */
219 if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) dst_industry = is->grf_prop.subst_id;
220 }
221
222 object.generic_scope.cargo_type = cargo_type;
223 object.generic_scope.default_selection = default_selection;
224 object.generic_scope.src_industry = src_industry;
225 object.generic_scope.dst_industry = dst_industry;
226 object.generic_scope.distance = distance;
227 object.generic_scope.event = event;
228 object.generic_scope.count = count;
229 object.generic_scope.station_size = station_size;
230 object.generic_scope.feature = feature;
231
232 uint16_t callback = GetGenericCallbackResult(feature, object, 0, 0, file);
233 if (callback != CALLBACK_FAILED) callback = GB(callback, 0, 8);
234 return callback;
235}
236
237
242 void AmbientSoundEffectCallback(TileIndex tile)
243{
244 assert(IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES) || IsTileType(tile, MP_WATER));
245
246 /* Only run every 1/200-th time. */
247 uint32_t r; // Save for later
248 if (!Chance16R(1, 200, r) || !_settings_client.sound.ambient) return;
249
250 /* Prepare resolver object. */
251 GenericResolverObject object(false, CBID_SOUNDS_AMBIENT_EFFECT);
252 object.generic_scope.feature = GSF_SOUNDFX;
253
254 uint32_t param1_v7 = GetTileType(tile) << 28 | Clamp(TileHeight(tile), 0, 15) << 24 | GB(r, 16, 8) << 16 | GetTerrainType(tile);
255 uint32_t param1_v8 = GetTileType(tile) << 24 | GetTileZ(tile) << 16 | GB(r, 16, 8) << 8 | (HasTileWaterClass(tile) ? GetWaterClass(tile) : 0) << 3 | GetTerrainType(tile);
256
257 /* Run callback. */
258 const GRFFile *grf_file;
259 uint16_t callback = GetGenericCallbackResult(GSF_SOUNDFX, object, param1_v7, param1_v8, &grf_file);
260
261 if (callback != CALLBACK_FAILED) PlayTileSound(grf_file, callback, tile);
262}
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
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
GetIndustrySpec
const IndustrySpec * GetIndustrySpec(IndustryType thistype)
Accessor for array _industry_specs.
Definition industry_cmd.cpp:123
INVALID_INDUSTRYTYPE
static const IndustryType INVALID_INDUSTRYTYPE
one above amount is considered invalid
Definition industry_type.h:27
industrytype.h
Industry type specs.
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
GrfSpecFeature
GrfSpecFeature
Definition newgrf.h:67
GSF_INVALID
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:94
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition newgrf_callbacks.h:20
CBID_SOUNDS_AMBIENT_EFFECT
@ CBID_SOUNDS_AMBIENT_EFFECT
Select an ambient sound to play for a given type of tile.
Definition newgrf_callbacks.h:197
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition newgrf_callbacks.h:22
CBID_GENERIC_AI_PURCHASE_SELECTION
@ CBID_GENERIC_AI_PURCHASE_SELECTION
AI construction/purchase selection.
Definition newgrf_callbacks.h:57
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition newgrf_callbacks.h:427
GetTerrainType
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 ...
Definition newgrf_commons.cpp:335
AddGenericCallback
void AddGenericCallback(uint8_t feature, const GRFFile *file, const SpriteGroup *group)
Add a generic feature callback sprite group to the appropriate feature list.
Definition newgrf_generic.cpp:108
GetGenericCallbackResult
static uint16_t GetGenericCallbackResult(uint8_t feature, ResolverObject &object, uint32_t param1_grfv7, uint32_t param1_grfv8, const GRFFile **file)
Follow a generic feature callback list and return the first successful answer.
Definition newgrf_generic.cpp:167
AmbientSoundEffectCallback
void AmbientSoundEffectCallback(TileIndex tile)
'Execute' the ambient sound effect callback.
Definition newgrf_generic.cpp:242
GetAiPurchaseCallbackResult
uint16_t GetAiPurchaseCallbackResult(uint8_t feature, CargoID cargo_type, uint8_t default_selection, IndustryType src_industry, IndustryType dst_industry, uint8_t distance, AIConstructionEvent event, uint8_t count, uint8_t station_size, const GRFFile **file)
'Execute' an AI purchase selection callback
Definition newgrf_generic.cpp:206
ResetGenericCallbacks
void ResetGenericCallbacks()
Reset all generic feature callback sprite groups.
Definition newgrf_generic.cpp:94
IT_AI_UNKNOWN
static const IndustryType IT_AI_UNKNOWN
The AI has no specific industry in mind.
Definition newgrf_generic.h:44
AIConstructionEvent
AIConstructionEvent
AI events for asking the NewGRF for information.
Definition newgrf_generic.h:20
IT_AI_TOWN
static const IndustryType IT_AI_TOWN
The AI actually wants to transport to/from a town, not an industry.
Definition newgrf_generic.h:45
PlayTileSound
void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
Play a NewGRF sound effect at the location of a specific tile.
Definition newgrf_sound.cpp:176
newgrf_sound.h
Functions related to NewGRF provided sounds.
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
random_func.hpp
Pseudo random number generator.
Chance16R
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.
Definition random_func.hpp:154
safeguards.h
A number of safeguards to prevent using unsafe methods.
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:56
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:280
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition cargotype.h:139
CargoSpec::bitnum
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition cargotype.h:78
ClientSettings::sound
SoundSettings sound
sound effect settings
Definition settings_type.h:613
GRFFile
Dynamic data of a loaded NewGRF.
Definition newgrf.h:108
GRFFile::cargo_map
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoID -> local ID)
Definition newgrf.h:130
GenericCallback
Definition newgrf_generic.cpp:76
GenericResolverObject
Resolver object for generic objects/properties.
Definition newgrf_generic.cpp:52
GenericResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
Definition newgrf_generic.cpp:57
GenericResolverObject::GetDebugID
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Definition newgrf_generic.cpp:70
GenericResolverObject::GenericResolverObject
GenericResolverObject(bool ai_callback, CallbackID callback=CBID_NO_CALLBACK)
Generic resolver.
Definition newgrf_generic.cpp:152
GenericResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition newgrf_generic.cpp:65
GenericScopeResolver
Scope resolver for generic objects and properties.
Definition newgrf_generic.cpp:21
GenericScopeResolver::dst_industry
uint8_t dst_industry
Destination industry substitute type. 0xFF for "town", 0xFE for "unknown".
Definition newgrf_generic.cpp:25
GenericScopeResolver::GetVariable
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
Definition newgrf_generic.cpp:121
GenericScopeResolver::ai_callback
bool ai_callback
Callback comes from the AI.
Definition newgrf_generic.cpp:47
GenericScopeResolver::GenericScopeResolver
GenericScopeResolver(ResolverObject &ro, bool ai_callback)
Generic scope resolver.
Definition newgrf_generic.cpp:38
GenericScopeResolver::src_industry
uint8_t src_industry
Source industry substitute type. 0xFF for "town", 0xFE for "unknown".
Definition newgrf_generic.cpp:24
IndustrySpec
Defines the data structure for constructing industry.
Definition industrytype.h:101
IndustrySpec::grf_prop
GRFFileProps grf_prop
properties related to the grf file
Definition industrytype.h:132
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition newgrf_spritegroup.h:296
ResolverObject::grffile
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
Definition newgrf_spritegroup.h:324
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
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
SoundSettings::ambient
bool ambient
Play ambient, industry and town sounds.
Definition settings_type.h:252
SpriteGroup
Definition newgrf_spritegroup.h:57
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.cpp:116
GetTileType
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
TileHeight
static debug_inline uint TileHeight(Tile tile)
Returns the height of a tile.
Definition tile_map.h:29
MP_TREES
@ MP_TREES
Tile got trees.
Definition tile_type.h:52
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:48
MP_WATER
@ MP_WATER
Water tile.
Definition tile_type.h:54
water_map.h
Map accessors for water tiles.
HasTileWaterClass
bool HasTileWaterClass(Tile t)
Checks whether the tile has an waterclass associated.
Definition water_map.h:101
GetWaterClass
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition water_map.h:112