Add support for deleted functions
[openttd/fttd.git] / src / newgrf_generic.cpp
bloba6868f7319bc6f9699d46728df8de3507af62e9f
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file newgrf_generic.cpp Handling of generic feature callbacks. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "newgrf_spritegroup.h"
15 #include "industrytype.h"
16 #include "core/random_func.hpp"
17 #include "newgrf_sound.h"
18 #include "map/ground.h"
19 #include "map/water.h"
20 #include "map/slope.h"
21 #include <list>
23 /** Scope resolver for generic objects and properties. */
24 struct GenericScopeResolver : public ScopeResolver {
25 CargoID cargo_type;
26 uint8 default_selection;
27 uint8 src_industry; ///< Source industry substitute type. 0xFF for "town", 0xFE for "unknown".
28 uint8 dst_industry; ///< Destination industry substitute type. 0xFF for "town", 0xFE for "unknown".
29 uint8 distance;
30 AIConstructionEvent event;
31 uint8 count;
32 uint8 station_size;
34 GenericScopeResolver(ResolverObject &ro, bool ai_callback);
36 /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
38 private:
39 bool ai_callback; ///< Callback comes from the AI.
43 /** Resolver object for generic objects/properties. */
44 struct GenericResolverObject : public ResolverObject {
45 GenericScopeResolver generic_scope;
47 GenericResolverObject(bool ai_callback, CallbackID callback = CBID_NO_CALLBACK);
49 /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
51 switch (scope) {
52 case VSG_SCOPE_SELF: return &this->generic_scope;
53 default: return ResolverObject::GetScope(scope, relative);
57 /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
60 struct GenericCallback {
61 const GRFFile *file;
62 const SpriteGroup *group;
64 GenericCallback(const GRFFile *file, const SpriteGroup *group) :
65 file(file),
66 group(group)
67 { }
70 typedef std::list<GenericCallback> GenericCallbackList;
72 static GenericCallbackList _gcl[GSF_END];
75 /**
76 * Reset all generic feature callback sprite groups.
78 void ResetGenericCallbacks()
80 for (uint8 feature = 0; feature < lengthof(_gcl); feature++) {
81 _gcl[feature].clear();
86 /**
87 * Add a generic feature callback sprite group to the appropriate feature list.
88 * @param feature The feature for the callback.
89 * @param file The GRF of the callback.
90 * @param group The sprite group of the callback.
92 void AddGenericCallback(uint8 feature, const GRFFile *file, const SpriteGroup *group)
94 if (feature >= lengthof(_gcl)) {
95 grfmsg(5, "AddGenericCallback: Unsupported feature 0x%02X", feature);
96 return;
99 /* Generic feature callbacks are evaluated in reverse (i.e. the last group
100 * to be added is evaluated first, etc) thus we push the group to the
101 * beginning of the list so a standard iterator will do the right thing. */
102 _gcl[feature].push_front(GenericCallback(file, group));
105 /* virtual */ uint32 GenericScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
107 if (this->ai_callback) {
108 switch (variable) {
109 case 0x40: return this->ro.grffile->cargo_map[this->cargo_type];
111 case 0x80: return this->cargo_type;
112 case 0x81: return CargoSpec::Get(this->cargo_type)->bitnum;
113 case 0x82: return this->default_selection;
114 case 0x83: return this->src_industry;
115 case 0x84: return this->dst_industry;
116 case 0x85: return this->distance;
117 case 0x86: return this->event;
118 case 0x87: return this->count;
119 case 0x88: return this->station_size;
121 default: break;
125 DEBUG(grf, 1, "Unhandled generic feature variable 0x%02X", variable);
127 *available = false;
128 return UINT_MAX;
132 /* virtual */ const SpriteGroup *GenericResolverObject::ResolveReal(const RealSpriteGroup *group) const
134 if (group->num_loaded == 0) return NULL;
136 return group->loaded[0];
140 * Generic resolver.
141 * @param ai_callback Callback comes from the AI.
142 * @param callback Callback ID.
144 GenericResolverObject::GenericResolverObject(bool ai_callback, CallbackID callback) : ResolverObject(NULL, callback), generic_scope(*this, ai_callback)
149 * Generic scope resolver.
150 * @param ro Surrounding resolver.
151 * @param ai_callback Callback comes from the AI.
153 GenericScopeResolver::GenericScopeResolver(ResolverObject &ro, bool ai_callback) : ScopeResolver(ro)
155 this->cargo_type = 0;
156 this->default_selection = 0;
157 this->src_industry = 0;
158 this->dst_industry = 0;
159 this->distance = 0;
160 this->event = (AIConstructionEvent)0;
161 this->count = 0;
162 this->station_size = 0;
163 this->ai_callback = ai_callback;
168 * Follow a generic feature callback list and return the first successful
169 * answer
170 * @param feature GRF Feature of callback
171 * @param object pre-populated resolver object
172 * @param param1_grfv7 callback_param1 for GRFs up to version 7.
173 * @param param1_grfv8 callback_param1 for GRFs from version 8 on.
174 * @param [out] file Optionally returns the GRFFile which made the final decision for the callback result.
175 * May be NULL if not required.
176 * @return callback value if successful or CALLBACK_FAILED
178 static uint16 GetGenericCallbackResult(uint8 feature, ResolverObject &object, uint32 param1_grfv7, uint32 param1_grfv8, const GRFFile **file)
180 assert(feature < lengthof(_gcl));
182 /* Test each feature callback sprite group. */
183 for (GenericCallbackList::const_iterator it = _gcl[feature].begin(); it != _gcl[feature].end(); ++it) {
184 const SpriteGroup *group = it->group;
185 object.grffile = it->file;
186 /* Set callback param based on GRF version. */
187 object.callback_param1 = it->file->grf_version >= 8 ? param1_grfv8 : param1_grfv7;
188 group = SpriteGroup::Resolve(group, object);
189 if (group == NULL || group->GetCallbackResult() == CALLBACK_FAILED) continue;
191 /* Return NewGRF file if necessary */
192 if (file != NULL) *file = it->file;
194 return group->GetCallbackResult();
197 /* No callback returned a valid result, so we've failed. */
198 return CALLBACK_FAILED;
203 * 'Execute' an AI purchase selection callback
205 * @param feature GRF Feature to call callback for.
206 * @param cargo_type Cargotype to pass to callback. (Variable 80)
207 * @param default_selection 'Default selection' to pass to callback. (Variable 82)
208 * @param src_industry 'Source industry type' to pass to callback. (Variable 83)
209 * @param dst_industry 'Destination industry type' to pass to callback. (Variable 84)
210 * @param distance 'Distance between source and destination' to pass to callback. (Variable 85)
211 * @param event 'AI construction event' to pass to callback. (Variable 86)
212 * @param count 'Construction number' to pass to callback. (Variable 87)
213 * @param station_size 'Station size' to pass to callback. (Variable 88)
214 * @param [out] file Optionally returns the GRFFile which made the final decision for the callback result.
215 * May be NULL if not required.
216 * @return callback value if successful or CALLBACK_FAILED
218 uint16 GetAiPurchaseCallbackResult(uint8 feature, CargoID cargo_type, uint8 default_selection, IndustryType src_industry, IndustryType dst_industry, uint8 distance, AIConstructionEvent event, uint8 count, uint8 station_size, const GRFFile **file)
220 GenericResolverObject object(true, CBID_GENERIC_AI_PURCHASE_SELECTION);
222 if (src_industry != IT_AI_UNKNOWN && src_industry != IT_AI_TOWN) {
223 const IndustrySpec *is = GetIndustrySpec(src_industry);
224 /* If this is no original industry, use the substitute type */
225 if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) src_industry = is->grf_prop.subst_id;
228 if (dst_industry != IT_AI_UNKNOWN && dst_industry != IT_AI_TOWN) {
229 const IndustrySpec *is = GetIndustrySpec(dst_industry);
230 /* If this is no original industry, use the substitute type */
231 if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) dst_industry = is->grf_prop.subst_id;
234 object.generic_scope.cargo_type = cargo_type;
235 object.generic_scope.default_selection = default_selection;
236 object.generic_scope.src_industry = src_industry;
237 object.generic_scope.dst_industry = dst_industry;
238 object.generic_scope.distance = distance;
239 object.generic_scope.event = event;
240 object.generic_scope.count = count;
241 object.generic_scope.station_size = station_size;
243 uint16 callback = GetGenericCallbackResult(feature, object, 0, 0, file);
244 if (callback != CALLBACK_FAILED) callback = GB(callback, 0, 8);
245 return callback;
250 * 'Execute' the ambient sound effect callback.
251 * @param tile Tile the sound effect should be generated for.
253 void AmbientSoundEffectCallback(TileIndex tile)
255 assert(IsGroundTile(tile) || IsWaterTile(tile));
257 /* Only run every 1/200-th time. */
258 uint32 r; // Save for later
259 if (!Chance16R(1, 200, r) || !_settings_client.sound.ambient) return;
261 /* Prepare resolver object. */
262 GenericResolverObject object(false, CBID_SOUNDS_AMBIENT_EFFECT);
264 int oldtype = IsWaterTile(tile) ? 6 : IsTreeTile(tile) ? 4 : 0;
265 uint32 param1_v7 = oldtype << 28 | Clamp(TileHeight(tile), 0, 15) << 24 | GB(r, 16, 8) << 16 | GetTerrainType(tile);
266 uint32 param1_v8 = oldtype << 24 | GetTileZ(tile) << 16 | GB(r, 16, 8) << 8 | (HasTileWaterClass(tile) ? GetWaterClass(tile) : 0) << 3 | GetTerrainType(tile);
268 /* Run callback. */
269 const GRFFile *grf_file;
270 uint16 callback = GetGenericCallbackResult(GSF_SOUNDFX, object, param1_v7, param1_v8, &grf_file);
272 if (callback != CALLBACK_FAILED) PlayTileSound(grf_file, callback, tile);