OpenTTD Source  20241108-master-g80f628063a
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  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 
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 
46 private:
47  bool ai_callback;
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 
86 typedef std::list<GenericCallback> GenericCallbackList;
87 
88 static GenericCallbackList _gcl[GSF_END];
89 
90 
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 
152 GenericResolverObject::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 
206 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)
207 {
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 
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. */
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 }
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
const IndustrySpec * GetIndustrySpec(IndustryType thistype)
Accessor for array _industry_specs.
static const IndustryType INVALID_INDUSTRYTYPE
one above amount is considered invalid
Definition: industry_type.h:27
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:67
@ GSF_INVALID
An invalid spec feature.
Definition: newgrf.h:94
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 ...
void AddGenericCallback(uint8_t feature, const GRFFile *file, const SpriteGroup *group)
Add a generic feature callback sprite group to the appropriate feature list.
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.
void AmbientSoundEffectCallback(TileIndex tile)
'Execute' the ambient sound effect callback.
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
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:56
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:280
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition: cargotype.h:73
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:134
SoundSettings sound
sound effect settings
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:108
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoID -> local ID)
Definition: newgrf.h:131
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".
bool ai_callback
Callback comes from the AI.
uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override
Get a variable value.
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.
Definition: industrytype.h:101
GRFFileProps grf_prop
properties related to the grf file
Definition: industrytype.h:134
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