OpenTTD

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

- File List
- Globals

newgrf_spritegroup.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_spritegroup.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
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 "core/pool_func.hpp"
16
17 #include "safeguards.h"
18
19 SpriteGroupPool _spritegroup_pool("SpriteGroup");
20 INSTANTIATE_POOL_METHODS(SpriteGroup)
21
22 TemporaryStorageArray<int32, 0x110> _temp_store;
23
24
35 /* static */ const SpriteGroup *SpriteGroup::Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level)
36 {
37 if (group == NULL) return NULL;
38 if (top_level) {
39 _temp_store.ClearChanges();
40 }
41 return group->Resolve(object);
42 }
43
44 RealSpriteGroup::~RealSpriteGroup()
45 {
46 free(this->loaded);
47 free(this->loading);
48 }
49
50 DeterministicSpriteGroup::~DeterministicSpriteGroup()
51 {
52 free(this->adjusts);
53 free(this->ranges);
54 }
55
56 RandomizedSpriteGroup::~RandomizedSpriteGroup()
57 {
58 free(this->groups);
59 }
60
61 static inline uint32 GetVariable(const ResolverObject &object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
62 {
63 /* First handle variables common with Action7/9/D */
64 uint32 value;
65 if (GetGlobalVariable(variable, &value, object.grffile)) return value;
66
67 /* Non-common variable */
68 switch (variable) {
69 case 0x0C: return object.callback;
70 case 0x10: return object.callback_param1;
71 case 0x18: return object.callback_param2;
72 case 0x1C: return object.last_value;
73
74 case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
75
76 case 0x7D: return _temp_store.GetValue(parameter);
77
78 case 0x7F:
79 if (object.grffile == NULL) return 0;
80 return object.grffile->GetParam(parameter);
81
82 /* Not a common variable, so evaluate the feature specific variables */
83 default: return scope->GetVariable(variable, parameter, available);
84 }
85 }
86
87 ScopeResolver::ScopeResolver(ResolverObject &ro)
88 : ro(ro)
89 {
90 }
91
92 ScopeResolver::~ScopeResolver() {}
93
98 /* virtual */ uint32 ScopeResolver::GetRandomBits() const
99 {
100 return 0;
101 }
102
107 /* virtual */ uint32 ScopeResolver::GetTriggers() const
108 {
109 return 0;
110 }
111
116 /* virtual */ void ScopeResolver::SetTriggers(int triggers) const {}
117
125 /* virtual */ uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
126 {
127 DEBUG(grf, 1, "Unhandled scope variable 0x%X", variable);
128 *available = false;
129 return UINT_MAX;
130 }
131
137 /* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
138
146 ResolverObject::ResolverObject(const GRFFile *grffile, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
147 : default_scope(*this)
148 {
149 this->callback = callback;
150 this->callback_param1 = callback_param1;
151 this->callback_param2 = callback_param2;
152 this->ResetState();
153
154 this->grffile = grffile;
155 this->root_spritegroup = NULL;
156 }
157
158 ResolverObject::~ResolverObject() {}
159
165 /* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
166 {
167 return NULL;
168 }
169
176 /* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
177 {
178 return &this->default_scope;
179 }
180
187 static uint32 RotateRight(uint32 val, uint32 rot)
188 {
189 /* Do not rotate more than necessary */
190 rot %= 32;
191
192 return (val >> rot) | (val << (32 - rot));
193 }
194
195
196 /* Evaluate an adjustment for a variable of the given size.
197 * U is the unsigned type and S is the signed type to use. */
198 template <typename U, typename S>
199 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ScopeResolver *scope, U last_value, uint32 value)
200 {
201 value >>= adjust->shift_num;
202 value &= adjust->and_mask;
203
204 if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
205
206 switch (adjust->type) {
207 case DSGA_TYPE_DIV: value /= (S)adjust->divmod_val; break;
208 case DSGA_TYPE_MOD: value %= (U)adjust->divmod_val; break;
209 case DSGA_TYPE_NONE: break;
210 }
211
212 switch (adjust->operation) {
213 case DSGA_OP_ADD: return last_value + value;
214 case DSGA_OP_SUB: return last_value - value;
215 case DSGA_OP_SMIN: return min((S)last_value, (S)value);
216 case DSGA_OP_SMAX: return max((S)last_value, (S)value);
217 case DSGA_OP_UMIN: return min((U)last_value, (U)value);
218 case DSGA_OP_UMAX: return max((U)last_value, (U)value);
219 case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
220 case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
221 case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
222 case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
223 case DSGA_OP_MUL: return last_value * value;
224 case DSGA_OP_AND: return last_value & value;
225 case DSGA_OP_OR: return last_value | value;
226 case DSGA_OP_XOR: return last_value ^ value;
227 case DSGA_OP_STO: _temp_store.StoreValue((U)value, (S)last_value); return last_value;
228 case DSGA_OP_RST: return value;
229 case DSGA_OP_STOP: scope->StorePSA((U)value, (S)last_value); return last_value;
230 case DSGA_OP_ROR: return RotateRight(last_value, value);
231 case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
232 case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
233 case DSGA_OP_SHL: return (uint32)(U)last_value << ((U)value & 0x1F); // Same behaviour as in ParamSet, mask 'value' to 5 bits, which should behave the same on all architectures.
234 case DSGA_OP_SHR: return (uint32)(U)last_value >> ((U)value & 0x1F);
235 case DSGA_OP_SAR: return (int32)(S)last_value >> ((U)value & 0x1F);
236 default: return value;
237 }
238 }
239
240
241 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) const
242 {
243 uint32 last_value = 0;
244 uint32 value = 0;
245 uint i;
246
247 ScopeResolver *scope = object.GetScope(this->var_scope);
248
249 for (i = 0; i < this->num_adjusts; i++) {
250 DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
251
252 /* Try to get the variable. We shall assume it is available, unless told otherwise. */
253 bool available = true;
254 if (adjust->variable == 0x7E) {
255 const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object, false);
256 if (subgroup == NULL) {
257 value = CALLBACK_FAILED;
258 } else {
259 value = subgroup->GetCallbackResult();
260 }
261
262 /* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
263 } else if (adjust->variable == 0x7B) {
264 value = GetVariable(object, scope, adjust->parameter, last_value, &available);
265 } else {
266 value = GetVariable(object, scope, adjust->variable, adjust->parameter, &available);
267 }
268
269 if (!available) {
270 /* Unsupported variable: skip further processing and return either
271 * the group from the first range or the default group. */
272 return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object, false);
273 }
274
275 switch (this->size) {
276 case DSG_SIZE_BYTE: value = EvalAdjustT<uint8, int8> (adjust, scope, last_value, value); break;
277 case DSG_SIZE_WORD: value = EvalAdjustT<uint16, int16>(adjust, scope, last_value, value); break;
278 case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, scope, last_value, value); break;
279 default: NOT_REACHED();
280 }
281 last_value = value;
282 }
283
284 object.last_value = last_value;
285
286 if (this->num_ranges == 0) {
287 /* nvar == 0 is a special case -- we turn our value into a callback result */
288 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
289 static CallbackResultSpriteGroup nvarzero(0, true);
290 nvarzero.result = value;
291 return &nvarzero;
292 }
293
294 for (i = 0; i < this->num_ranges; i++) {
295 if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
296 return SpriteGroup::Resolve(this->ranges[i].group, object, false);
297 }
298 }
299
300 return SpriteGroup::Resolve(this->default_group, object, false);
301 }
302
303
304 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject &object) const
305 {
306 ScopeResolver *scope = object.GetScope(this->var_scope, this->count);
307 if (object.trigger != 0) {
308 /* Handle triggers */
309 /* Magic code that may or may not do the right things... */
310 byte waiting_triggers = scope->GetTriggers();
311 byte match = this->triggers & (waiting_triggers | object.trigger);
312 bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
313
314 if (res) {
315 waiting_triggers &= ~match;
316 object.reseed[this->var_scope] |= (this->num_groups - 1) << this->lowest_randbit;
317 } else {
318 waiting_triggers |= object.trigger;
319 }
320
321 scope->SetTriggers(waiting_triggers);
322 }
323
324 uint32 mask = (this->num_groups - 1) << this->lowest_randbit;
325 byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
326
327 return SpriteGroup::Resolve(this->groups[index], object, false);
328 }
329
330
331 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject &object) const
332 {
333 return object.ResolveReal(this);
334 }
335
343 const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8 *stage) const
344 {
345 if (!this->dts.NeedsPreprocessing()) {
346 if (stage != NULL && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
347 return &this->dts;
348 }
349
350 static DrawTileSprites result;
351 uint8 actual_stage = stage != NULL ? *stage : 0;
352 this->dts.PrepareLayout(0, 0, 0, actual_stage, false);
353 this->dts.ProcessRegisters(0, 0, false);
354 result.seq = this->dts.GetLayout(&result.ground);
355
356 /* Stage has been processed by PrepareLayout(), set it to zero. */
357 if (stage != NULL) *stage = 0;
358
359 return &result;
360 }