(svn r25652) -Fix: Improve text caret movement for complex scripts.
[openttd/fttd.git] / src / newgrf_generic.cpp
blob75e25c3d40ca10419ba8f0a5fd9f783f5d442f2c
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 "water_map.h"
19 #include <list>
21 /** Scope resolver for generic objects and properties. */
22 struct GenericScopeResolver : public ScopeResolver {
23 CargoID cargo_type;
24 uint8 default_selection;
25 uint8 src_industry; ///< Source industry substitute type. 0xFF for "town", 0xFE for "unknown".
26 uint8 dst_industry; ///< Destination industry substitute type. 0xFF for "town", 0xFE for "unknown".
27 uint8 distance;
28 AIConstructionEvent event;
29 uint8 count;
30 uint8 station_size;
32 GenericScopeResolver(ResolverObject *ro, bool ai_callback);
34 /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
36 private:
37 bool ai_callback; ///< Callback comes from the AI.
41 /** Resolver object for generic objects/properties. */
42 struct GenericResolverObject : public ResolverObject {
43 GenericScopeResolver generic_scope;
45 GenericResolverObject(bool ai_callback, CallbackID callback = CBID_NO_CALLBACK);
47 /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
49 switch (scope) {
50 case VSG_SCOPE_SELF: return &this->generic_scope;
51 default: return ResolverObject::GetScope(scope, relative);
55 /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
58 struct GenericCallback {
59 const GRFFile *file;
60 const SpriteGroup *group;
62 GenericCallback(const GRFFile *file, const SpriteGroup *group) :
63 file(file),
64 group(group)
65 { }
68 typedef std::list<GenericCallback> GenericCallbackList;
70 static GenericCallbackList _gcl[GSF_END];
73 /**
74 * Reset all generic feature callback sprite groups.
76 void ResetGenericCallbacks()
78 for (uint8 feature = 0; feature < lengthof(_gcl); feature++) {
79 _gcl[feature].clear();
84 /**
85 * Add a generic feature callback sprite group to the appropriate feature list.
86 * @param feature The feature for the callback.
87 * @param file The GRF of the callback.
88 * @param group The sprite group of the callback.
90 void AddGenericCallback(uint8 feature, const GRFFile *file, const SpriteGroup *group)
92 if (feature >= lengthof(_gcl)) {
93 grfmsg(5, "AddGenericCallback: Unsupported feature 0x%02X", feature);
94 return;
97 /* Generic feature callbacks are evaluated in reverse (i.e. the last group
98 * to be added is evaluated first, etc) thus we push the group to the
99 * beginning of the list so a standard iterator will do the right thing. */
100 _gcl[feature].push_front(GenericCallback(file, group));
103 /* virtual */ uint32 GenericScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
105 if (this->ai_callback) {
106 switch (variable) {
107 case 0x40: return this->ro->grffile->cargo_map[this->cargo_type];
109 case 0x80: return this->cargo_type;
110 case 0x81: return CargoSpec::Get(this->cargo_type)->bitnum;
111 case 0x82: return this->default_selection;
112 case 0x83: return this->src_industry;
113 case 0x84: return this->dst_industry;
114 case 0x85: return this->distance;
115 case 0x86: return this->event;
116 case 0x87: return this->count;
117 case 0x88: return this->station_size;
119 default: break;
123 DEBUG(grf, 1, "Unhandled generic feature variable 0x%02X", variable);
125 *available = false;
126 return UINT_MAX;
130 /* virtual */ const SpriteGroup *GenericResolverObject::ResolveReal(const RealSpriteGroup *group) const
132 if (group->num_loaded == 0) return NULL;
134 return group->loaded[0];
138 * Generic resolver.
139 * @param ai_callback Callback comes from the AI.
140 * @param callback Callback ID.
142 GenericResolverObject::GenericResolverObject(bool ai_callback, CallbackID callback) : ResolverObject(NULL, callback), generic_scope(this, ai_callback)
147 * Generic scope resolver.
148 * @param ro Surrounding resolver.
149 * @param ai_callback Callback comes from the AI.
151 GenericScopeResolver::GenericScopeResolver(ResolverObject *ro, bool ai_callback) : ScopeResolver(ro)
153 this->cargo_type = 0;
154 this->default_selection = 0;
155 this->src_industry = 0;
156 this->dst_industry = 0;
157 this->distance = 0;
158 this->event = (AIConstructionEvent)0;
159 this->count = 0;
160 this->station_size = 0;
161 this->ai_callback = ai_callback;
166 * Follow a generic feature callback list and return the first successful
167 * answer
168 * @param feature GRF Feature of callback
169 * @param object pre-populated resolver object
170 * @param param1_grfv7 callback_param1 for GRFs up to version 7.
171 * @param param1_grfv8 callback_param1 for GRFs from version 8 on.
172 * @param [out] file Optionally returns the GRFFile which made the final decision for the callback result.
173 * May be NULL if not required.
174 * @return callback value if successful or CALLBACK_FAILED
176 static uint16 GetGenericCallbackResult(uint8 feature, ResolverObject *object, uint32 param1_grfv7, uint32 param1_grfv8, const GRFFile **file)
178 assert(feature < lengthof(_gcl));
180 /* Test each feature callback sprite group. */
181 for (GenericCallbackList::const_iterator it = _gcl[feature].begin(); it != _gcl[feature].end(); ++it) {
182 const SpriteGroup *group = it->group;
183 object->grffile = it->file;
184 /* Set callback param based on GRF version. */
185 object->callback_param1 = it->file->grf_version >= 8 ? param1_grfv8 : param1_grfv7;
186 group = SpriteGroup::Resolve(group, object);
187 if (group == NULL || group->GetCallbackResult() == CALLBACK_FAILED) continue;
189 /* Return NewGRF file if necessary */
190 if (file != NULL) *file = it->file;
192 return group->GetCallbackResult();
195 /* No callback returned a valid result, so we've failed. */
196 return CALLBACK_FAILED;
201 * 'Execute' an AI purchase selection callback
203 * @param feature GRF Feature to call callback for.
204 * @param cargo_type Cargotype to pass to callback. (Variable 80)
205 * @param default_selection 'Default selection' to pass to callback. (Variable 82)
206 * @param src_industry 'Source industry type' to pass to callback. (Variable 83)
207 * @param dst_industry 'Destination industry type' to pass to callback. (Variable 84)
208 * @param distance 'Distance between source and destination' to pass to callback. (Variable 85)
209 * @param event 'AI construction event' to pass to callback. (Variable 86)
210 * @param count 'Construction number' to pass to callback. (Variable 87)
211 * @param station_size 'Station size' to pass to callback. (Variable 88)
212 * @param [out] file Optionally returns the GRFFile which made the final decision for the callback result.
213 * May be NULL if not required.
214 * @return callback value if successful or CALLBACK_FAILED
216 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)
218 GenericResolverObject object(true, CBID_GENERIC_AI_PURCHASE_SELECTION);
220 if (src_industry != IT_AI_UNKNOWN && src_industry != IT_AI_TOWN) {
221 const IndustrySpec *is = GetIndustrySpec(src_industry);
222 /* If this is no original industry, use the substitute type */
223 if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) src_industry = is->grf_prop.subst_id;
226 if (dst_industry != IT_AI_UNKNOWN && dst_industry != IT_AI_TOWN) {
227 const IndustrySpec *is = GetIndustrySpec(dst_industry);
228 /* If this is no original industry, use the substitute type */
229 if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) dst_industry = is->grf_prop.subst_id;
232 object.generic_scope.cargo_type = cargo_type;
233 object.generic_scope.default_selection = default_selection;
234 object.generic_scope.src_industry = src_industry;
235 object.generic_scope.dst_industry = dst_industry;
236 object.generic_scope.distance = distance;
237 object.generic_scope.event = event;
238 object.generic_scope.count = count;
239 object.generic_scope.station_size = station_size;
241 uint16 callback = GetGenericCallbackResult(feature, &object, 0, 0, file);
242 if (callback != CALLBACK_FAILED) callback = GB(callback, 0, 8);
243 return callback;
248 * 'Execute' the ambient sound effect callback.
249 * @param tile Tile the sound effect should be generated for.
251 void AmbientSoundEffectCallback(TileIndex tile)
253 assert(IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES) || IsTileType(tile, MP_WATER));
255 /* Only run every 1/200-th time. */
256 uint32 r; // Save for later
257 if (!Chance16R(1, 200, r) || !_settings_client.sound.ambient) return;
259 /* Prepare resolver object. */
260 GenericResolverObject object(false, CBID_SOUNDS_AMBIENT_EFFECT);
262 uint32 param1_v7 = GetTileType(tile) << 28 | Clamp(TileHeight(tile), 0, 15) << 24 | GB(r, 16, 8) << 16 | GetTerrainType(tile);
263 uint32 param1_v8 = GetTileType(tile) << 24 | GetTileZ(tile) << 16 | GB(r, 16, 8) << 8 | (HasTileWaterClass(tile) ? GetWaterClass(tile) : 0) << 3 | GetTerrainType(tile);
265 /* Run callback. */
266 const GRFFile *grf_file;
267 uint16 callback = GetGenericCallbackResult(GSF_SOUNDFX, &object, param1_v7, param1_v8, &grf_file);
269 if (callback != CALLBACK_FAILED) PlayTileSound(grf_file, callback, tile);