OpenTTD: src/newgrf_generic.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_generic.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_generic.cpp 27984 2018-03-11 13:19:41Z frosch $ */
2
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 */
9
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>
20
21 #include "safeguards.h"
22
24 struct GenericScopeResolver : public ScopeResolver {
25 CargoID cargo_type;
26 uint8 default_selection;
27 uint8 src_industry;
28 uint8 dst_industry;
29 uint8 distance;
30 AIConstructionEvent event;
31 uint8 count;
32 uint8 station_size;
33
39 GenericScopeResolver(ResolverObject &ro, bool ai_callback)
40 : ScopeResolver(ro), cargo_type(0), default_selection(0), src_industry(0), dst_industry(0), distance(0),
41 event(), count(0), station_size(0), ai_callback(ai_callback)
42 {
43 }
44
45 /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
46
47 private:
48 bool ai_callback;
49 };
50
51
53 struct GenericResolverObject : public ResolverObject {
54 GenericScopeResolver generic_scope;
55
56 GenericResolverObject(bool ai_callback, CallbackID callback = CBID_NO_CALLBACK);
57
58 /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
59 {
60 switch (scope) {
61 case VSG_SCOPE_SELF: return &this->generic_scope;
62 default: return ResolverObject::GetScope(scope, relative);
63 }
64 }
65
66 /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
67 };
68
69 struct GenericCallback {
70 const GRFFile *file;
71 const SpriteGroup *group;
72
73 GenericCallback(const GRFFile *file, const SpriteGroup *group) :
74 file(file),
75 group(group)
76 { }
77 };
78
79 typedef std::list<GenericCallback> GenericCallbackList;
80
81 static GenericCallbackList _gcl[GSF_END];
82
83
87 void ResetGenericCallbacks()
88 {
89 for (uint8 feature = 0; feature < lengthof(_gcl); feature++) {
90 _gcl[feature].clear();
91 }
92 }
93
94
101 void AddGenericCallback(uint8 feature, const GRFFile *file, const SpriteGroup *group)
102 {
103 if (feature >= lengthof(_gcl)) {
104 grfmsg(5, "AddGenericCallback: Unsupported feature 0x%02X", feature);
105 return;
106 }
107
108 /* Generic feature callbacks are evaluated in reverse (i.e. the last group
109 * to be added is evaluated first, etc) thus we push the group to the
110 * beginning of the list so a standard iterator will do the right thing. */
111 _gcl[feature].push_front(GenericCallback(file, group));
112 }
113
114 /* virtual */ uint32 GenericScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
115 {
116 if (this->ai_callback) {
117 switch (variable) {
118 case 0x40: return this->ro.grffile->cargo_map[this->cargo_type];
119
120 case 0x80: return this->cargo_type;
121 case 0x81: return CargoSpec::Get(this->cargo_type)->bitnum;
122 case 0x82: return this->default_selection;
123 case 0x83: return this->src_industry;
124 case 0x84: return this->dst_industry;
125 case 0x85: return this->distance;
126 case 0x86: return this->event;
127 case 0x87: return this->count;
128 case 0x88: return this->station_size;
129
130 default: break;
131 }
132 }
133
134 DEBUG(grf, 1, "Unhandled generic feature variable 0x%02X", variable);
135
136 *available = false;
137 return UINT_MAX;
138 }
139
140
141 /* virtual */ const SpriteGroup *GenericResolverObject::ResolveReal(const RealSpriteGroup *group) const
142 {
143 if (group->num_loaded == 0) return NULL;
144
145 return group->loaded[0];
146 }
147
153 GenericResolverObject::GenericResolverObject(bool ai_callback, CallbackID callback) : ResolverObject(NULL, callback), generic_scope(*this, ai_callback)
154 {
155 }
156
157
169 static uint16 GetGenericCallbackResult(uint8 feature, ResolverObject &object, uint32 param1_grfv7, uint32 param1_grfv8, const GRFFile **file)
170 {
171 assert(feature < lengthof(_gcl));
172
173 /* Test each feature callback sprite group. */
174 for (GenericCallbackList::const_iterator it = _gcl[feature].begin(); it != _gcl[feature].end(); ++it) {
175 object.grffile = it->file;
176 object.root_spritegroup = it->group;
177 /* Set callback param based on GRF version. */
178 object.callback_param1 = it->file->grf_version >= 8 ? param1_grfv8 : param1_grfv7;
179 uint16 result = object.ResolveCallback();
180 if (result == CALLBACK_FAILED) continue;
181
182 /* Return NewGRF file if necessary */
183 if (file != NULL) *file = it->file;
184
185 return result;
186 }
187
188 /* No callback returned a valid result, so we've failed. */
189 return CALLBACK_FAILED;
190 }
191
192
209 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)
210 {
211 GenericResolverObject object(true, CBID_GENERIC_AI_PURCHASE_SELECTION);
212
213 if (src_industry != IT_AI_UNKNOWN && src_industry != IT_AI_TOWN) {
214 const IndustrySpec *is = GetIndustrySpec(src_industry);
215 /* If this is no original industry, use the substitute type */
216 if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) src_industry = is->grf_prop.subst_id;
217 }
218
219 if (dst_industry != IT_AI_UNKNOWN && dst_industry != IT_AI_TOWN) {
220 const IndustrySpec *is = GetIndustrySpec(dst_industry);
221 /* If this is no original industry, use the substitute type */
222 if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) dst_industry = is->grf_prop.subst_id;
223 }
224
225 object.generic_scope.cargo_type = cargo_type;
226 object.generic_scope.default_selection = default_selection;
227 object.generic_scope.src_industry = src_industry;
228 object.generic_scope.dst_industry = dst_industry;
229 object.generic_scope.distance = distance;
230 object.generic_scope.event = event;
231 object.generic_scope.count = count;
232 object.generic_scope.station_size = station_size;
233
234 uint16 callback = GetGenericCallbackResult(feature, object, 0, 0, file);
235 if (callback != CALLBACK_FAILED) callback = GB(callback, 0, 8);
236 return callback;
237 }
238
239
244 void AmbientSoundEffectCallback(TileIndex tile)
245 {
246 assert(IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES) || IsTileType(tile, MP_WATER));
247
248 /* Only run every 1/200-th time. */
249 uint32 r; // Save for later
250 if (!Chance16R(1, 200, r) || !_settings_client.sound.ambient) return;
251
252 /* Prepare resolver object. */
253 GenericResolverObject object(false, CBID_SOUNDS_AMBIENT_EFFECT);
254
255 uint32 param1_v7 = GetTileType(tile) << 28 | Clamp(TileHeight(tile), 0, 15) << 24 | GB(r, 16, 8) << 16 | GetTerrainType(tile);
256 uint32 param1_v8 = GetTileType(tile) << 24 | GetTileZ(tile) << 16 | GB(r, 16, 8) << 8 | (HasTileWaterClass(tile) ? GetWaterClass(tile) : 0) << 3 | GetTerrainType(tile);
257
258 /* Run callback. */
259 const GRFFile *grf_file;
260 uint16 callback = GetGenericCallbackResult(GSF_SOUNDFX, object, param1_v7, param1_v8, &grf_file);
261
262 if (callback != CALLBACK_FAILED) PlayTileSound(grf_file, callback, tile);
263 }