Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / newgrf_generic.cpp
blobf633715fa80778b6fa3fd299a87ed889ca7b3030
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 */
8 /** @file newgrf_generic.cpp Handling of generic feature callbacks. */
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"
18 #include "safeguards.h"
20 /** Scope resolver for generic objects and properties. */
21 struct GenericScopeResolver : public ScopeResolver {
22 CargoID cargo_type;
23 uint8_t default_selection;
24 uint8_t src_industry; ///< Source industry substitute type. 0xFF for "town", 0xFE for "unknown".
25 uint8_t dst_industry; ///< Destination industry substitute type. 0xFF for "town", 0xFE for "unknown".
26 uint8_t distance;
27 AIConstructionEvent event;
28 uint8_t count;
29 uint8_t station_size;
31 uint8_t feature;
33 /**
34 * Generic scope resolver.
35 * @param ro Surrounding resolver.
36 * @param ai_callback Callback comes from the AI.
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)
44 uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
46 private:
47 bool ai_callback; ///< Callback comes from the AI.
51 /** Resolver object for generic objects/properties. */
52 struct GenericResolverObject : public ResolverObject {
53 GenericScopeResolver generic_scope;
55 GenericResolverObject(bool ai_callback, CallbackID callback = CBID_NO_CALLBACK);
57 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override
59 switch (scope) {
60 case VSG_SCOPE_SELF: return &this->generic_scope;
61 default: return ResolverObject::GetScope(scope, relative);
65 GrfSpecFeature GetFeature() const override
67 return (GrfSpecFeature)this->generic_scope.feature;
70 uint32_t GetDebugID() const override
72 return 0;
76 struct GenericCallback {
77 const GRFFile *file;
78 const SpriteGroup *group;
80 GenericCallback(const GRFFile *file, const SpriteGroup *group) :
81 file(file),
82 group(group)
83 { }
86 typedef std::list<GenericCallback> GenericCallbackList;
88 static GenericCallbackList _gcl[GSF_END];
91 /**
92 * Reset all generic feature callback sprite groups.
94 void ResetGenericCallbacks()
96 for (auto &gcl : _gcl) {
97 gcl.clear();
103 * Add a generic feature callback sprite group to the appropriate feature list.
104 * @param feature The feature for the callback.
105 * @param file The GRF of the callback.
106 * @param group The sprite group of the callback.
108 void AddGenericCallback(uint8_t feature, const GRFFile *file, const SpriteGroup *group)
110 if (feature >= lengthof(_gcl)) {
111 GrfMsg(5, "AddGenericCallback: Unsupported feature 0x{:02X}", feature);
112 return;
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));
121 /* virtual */ uint32_t GenericScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
123 if (this->ai_callback) {
124 switch (variable) {
125 case 0x40: return this->ro.grffile->cargo_map[this->cargo_type];
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;
137 default: break;
141 Debug(grf, 1, "Unhandled generic feature variable 0x{:02X}", variable);
143 available = false;
144 return UINT_MAX;
148 * Generic resolver.
149 * @param ai_callback Callback comes from the AI.
150 * @param callback Callback ID.
152 GenericResolverObject::GenericResolverObject(bool ai_callback, CallbackID callback) : ResolverObject(nullptr, callback), generic_scope(*this, ai_callback)
158 * Follow a generic feature callback list and return the first successful
159 * answer
160 * @param feature GRF Feature of callback
161 * @param object pre-populated resolver object
162 * @param param1_grfv7 callback_param1 for GRFs up to version 7.
163 * @param param1_grfv8 callback_param1 for GRFs from version 8 on.
164 * @param[out] file Optionally returns the GRFFile which made the final decision for the callback result. May be nullptr if not required.
165 * @return callback value if successful or CALLBACK_FAILED
167 static uint16_t GetGenericCallbackResult(uint8_t feature, ResolverObject &object, uint32_t param1_grfv7, uint32_t param1_grfv8, const GRFFile **file)
169 assert(feature < lengthof(_gcl));
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;
180 /* Return NewGRF file if necessary */
181 if (file != nullptr) *file = it.file;
183 return result;
186 /* No callback returned a valid result, so we've failed. */
187 return CALLBACK_FAILED;
192 * 'Execute' an AI purchase selection callback
194 * @param feature GRF Feature to call callback for.
195 * @param cargo_type Cargotype to pass to callback. (Variable 80)
196 * @param default_selection 'Default selection' to pass to callback. (Variable 82)
197 * @param src_industry 'Source industry type' to pass to callback. (Variable 83)
198 * @param dst_industry 'Destination industry type' to pass to callback. (Variable 84)
199 * @param distance 'Distance between source and destination' to pass to callback. (Variable 85)
200 * @param event 'AI construction event' to pass to callback. (Variable 86)
201 * @param count 'Construction number' to pass to callback. (Variable 87)
202 * @param station_size 'Station size' to pass to callback. (Variable 88)
203 * @param[out] file Optionally returns the GRFFile which made the final decision for the callback result. May be nullptr if not required.
204 * @return callback value if successful or CALLBACK_FAILED
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)
208 GenericResolverObject object(true, CBID_GENERIC_AI_PURCHASE_SELECTION);
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;
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;
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;
232 uint16_t callback = GetGenericCallbackResult(feature, object, 0, 0, file);
233 if (callback != CALLBACK_FAILED) callback = GB(callback, 0, 8);
234 return callback;
239 * 'Execute' the ambient sound effect callback.
240 * @param tile Tile the sound effect should be generated for.
242 void AmbientSoundEffectCallback(TileIndex tile)
244 assert(IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES) || IsTileType(tile, MP_WATER));
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;
250 /* Prepare resolver object. */
251 GenericResolverObject object(false, CBID_SOUNDS_AMBIENT_EFFECT);
252 object.generic_scope.feature = GSF_SOUNDFX;
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);
257 /* Run callback. */
258 const GRFFile *grf_file;
259 uint16_t callback = GetGenericCallbackResult(GSF_SOUNDFX, object, param1_v7, param1_v8, &grf_file);
261 if (callback != CALLBACK_FAILED) PlayTileSound(grf_file, callback, tile);