Move resolving of action 3 into ResolverObject constructor
[openttd/fttd.git] / src / newgrf_spritegroup.cpp
blob946cd5330e895b5d3f6e947832f0aca110301777
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_spritegroup.cpp Handling of primarily NewGRF action 2. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "newgrf_spritegroup.h"
15 #include "core/pool_func.hpp"
17 template<> SpriteGroup::Pool SpriteGroup::PoolItem::pool ("SpriteGroup");
18 INSTANTIATE_POOL_METHODS(SpriteGroup)
20 TemporaryStorageArray<int32, 0x110> _temp_store;
23 /**
24 * ResolverObject (re)entry point.
25 * This cannot be made a call to a virtual function because virtual functions
26 * do not like NULL and checking for NULL *everywhere* is more cumbersome than
27 * this little helper function.
28 * @param group the group to resolve for
29 * @param object information needed to resolve the group
30 * @param top_level true if this is a top-level SpriteGroup, false if used nested in another SpriteGroup.
31 * @return the resolved group
33 /* static */ const SpriteGroup *SpriteGroup::Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level)
35 if (group == NULL) return NULL;
36 if (top_level) {
37 _temp_store.ClearChanges();
39 return group->Resolve(object);
42 RealSpriteGroup::~RealSpriteGroup()
44 free(this->loaded);
45 free(this->loading);
48 DeterministicSpriteGroup::~DeterministicSpriteGroup()
50 free(this->adjusts);
51 free(this->ranges);
54 RandomizedSpriteGroup::~RandomizedSpriteGroup()
56 free(this->groups);
59 static inline uint32 GetVariable(const ResolverObject &object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
61 /* First handle variables common with Action7/9/D */
62 uint32 value;
63 if (GetGlobalVariable(variable, &value, object.grffile)) return value;
65 /* Non-common variable */
66 switch (variable) {
67 case 0x0C: return object.callback;
68 case 0x10: return object.callback_param1;
69 case 0x18: return object.callback_param2;
70 case 0x1C: return object.last_value;
72 case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
74 case 0x7D: return _temp_store.GetValue(parameter);
76 case 0x7F:
77 if (object.grffile == NULL) return 0;
78 return object.grffile->GetParam(parameter);
80 /* Not a common variable, so evaluate the feature specific variables */
81 default: return scope->GetVariable(variable, parameter, available);
85 ScopeResolver::ScopeResolver(ResolverObject &ro)
86 : ro(ro)
90 ScopeResolver::~ScopeResolver() {}
92 /**
93 * Get a few random bits. Default implementation has no random bits.
94 * @return Random bits.
96 /* virtual */ uint32 ScopeResolver::GetRandomBits() const
98 return 0;
102 * Get the triggers. Base class returns \c 0 to prevent trouble.
103 * @return The triggers.
105 /* virtual */ uint32 ScopeResolver::GetTriggers() const
107 return 0;
111 * Set the triggers. Base class implementation does nothing.
112 * @param triggers Triggers to set.
114 /* virtual */ void ScopeResolver::SetTriggers(int triggers) const {}
117 * Get a variable value. Default implementation has no available variables.
118 * @param variable Variable to read
119 * @param parameter Parameter for 60+x variables
120 * @param[out] available Set to false, in case the variable does not exist.
121 * @return Value
123 /* virtual */ uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
125 DEBUG(grf, 1, "Unhandled scope variable 0x%X", variable);
126 *available = false;
127 return UINT_MAX;
131 * Store a value into the persistent storage area (PSA). Default implementation does nothing (for newgrf classes without storage).
132 * @param pos Position to store into.
133 * @param value Value to store.
135 /* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
138 * Resolver constructor.
139 * @param grffile NewGRF file associated with the object (or \c NULL if none).
140 * @param callback Callback code being resolved (default value is #CBID_NO_CALLBACK).
141 * @param callback_param1 First parameter (var 10) of the callback (only used when \a callback is also set).
142 * @param callback_param2 Second parameter (var 18) of the callback (only used when \a callback is also set).
144 ResolverObject::ResolverObject(const GRFFile *grffile, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
145 : default_scope(*this)
147 this->callback = callback;
148 this->callback_param1 = callback_param1;
149 this->callback_param2 = callback_param2;
150 this->ResetState();
152 this->grffile = grffile;
153 this->root_spritegroup = NULL;
156 ResolverObject::~ResolverObject() {}
159 * Get the real sprites of the grf.
160 * @param group Group to get.
161 * @return The available sprite group.
163 /* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
165 return NULL;
169 * Get a resolver for the \a scope.
170 * @param scope Scope to return.
171 * @param relative Additional parameter for #VSG_SCOPE_RELATIVE.
172 * @return The resolver for the requested scope.
174 /* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
176 return &this->default_scope;
180 * Rotate val rot times to the right
181 * @param val the value to rotate
182 * @param rot the amount of times to rotate
183 * @return the rotated value
185 static uint32 RotateRight(uint32 val, uint32 rot)
187 /* Do not rotate more than necessary */
188 rot %= 32;
190 return (val >> rot) | (val << (32 - rot));
194 /* Evaluate an adjustment for a variable of the given size.
195 * U is the unsigned type and S is the signed type to use. */
196 template <typename U, typename S>
197 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ScopeResolver *scope, U last_value, uint32 value)
199 value >>= adjust->shift_num;
200 value &= adjust->and_mask;
202 if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
204 switch (adjust->type) {
205 case DSGA_TYPE_DIV: value /= (S)adjust->divmod_val; break;
206 case DSGA_TYPE_MOD: value %= (U)adjust->divmod_val; break;
207 case DSGA_TYPE_NONE: break;
210 switch (adjust->operation) {
211 case DSGA_OP_ADD: return last_value + value;
212 case DSGA_OP_SUB: return last_value - value;
213 case DSGA_OP_SMIN: return min((S)last_value, (S)value);
214 case DSGA_OP_SMAX: return max((S)last_value, (S)value);
215 case DSGA_OP_UMIN: return min((U)last_value, (U)value);
216 case DSGA_OP_UMAX: return max((U)last_value, (U)value);
217 case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
218 case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
219 case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
220 case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
221 case DSGA_OP_MUL: return last_value * value;
222 case DSGA_OP_AND: return last_value & value;
223 case DSGA_OP_OR: return last_value | value;
224 case DSGA_OP_XOR: return last_value ^ value;
225 case DSGA_OP_STO: _temp_store.StoreValue((U)value, (S)last_value); return last_value;
226 case DSGA_OP_RST: return value;
227 case DSGA_OP_STOP: scope->StorePSA((U)value, (S)last_value); return last_value;
228 case DSGA_OP_ROR: return RotateRight(last_value, value);
229 case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
230 case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
231 case DSGA_OP_SHL: return (U)last_value << ((U)value & 0x1F); // mask 'value' to 5 bits, which should behave the same on all architectures.
232 case DSGA_OP_SHR: return (U)last_value >> ((U)value & 0x1F);
233 case DSGA_OP_SAR: return (S)last_value >> ((U)value & 0x1F);
234 default: return value;
239 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) const
241 uint32 last_value = 0;
242 uint32 value = 0;
243 uint i;
245 ScopeResolver *scope = object.GetScope(this->var_scope);
247 for (i = 0; i < this->num_adjusts; i++) {
248 DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
250 /* Try to get the variable. We shall assume it is available, unless told otherwise. */
251 bool available = true;
252 if (adjust->variable == 0x7E) {
253 const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object, false);
254 if (subgroup == NULL) {
255 value = CALLBACK_FAILED;
256 } else {
257 value = subgroup->GetCallbackResult();
260 /* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
261 } else if (adjust->variable == 0x7B) {
262 value = GetVariable(object, scope, adjust->parameter, last_value, &available);
263 } else {
264 value = GetVariable(object, scope, adjust->variable, adjust->parameter, &available);
267 if (!available) {
268 /* Unsupported variable: skip further processing and return either
269 * the group from the first range or the default group. */
270 return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object, false);
273 switch (this->size) {
274 case DSG_SIZE_BYTE: value = EvalAdjustT<uint8, int8> (adjust, scope, last_value, value); break;
275 case DSG_SIZE_WORD: value = EvalAdjustT<uint16, int16>(adjust, scope, last_value, value); break;
276 case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, scope, last_value, value); break;
277 default: NOT_REACHED();
279 last_value = value;
282 object.last_value = last_value;
284 if (this->num_ranges == 0) {
285 /* nvar == 0 is a special case -- we turn our value into a callback result */
286 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
287 static CallbackResultSpriteGroup nvarzero(0, true);
288 nvarzero.result = value;
289 return &nvarzero;
292 for (i = 0; i < this->num_ranges; i++) {
293 if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
294 return SpriteGroup::Resolve(this->ranges[i].group, object, false);
298 return SpriteGroup::Resolve(this->default_group, object, false);
302 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject &object) const
304 ScopeResolver *scope = object.GetScope(this->var_scope, this->count);
305 if (object.trigger != 0) {
306 /* Handle triggers */
307 /* Magic code that may or may not do the right things... */
308 byte waiting_triggers = scope->GetTriggers();
309 byte match = this->triggers & (waiting_triggers | object.trigger);
310 bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
312 if (res) {
313 waiting_triggers &= ~match;
314 object.reseed[this->var_scope] |= (this->num_groups - 1) << this->lowest_randbit;
315 } else {
316 waiting_triggers |= object.trigger;
319 scope->SetTriggers(waiting_triggers);
322 uint32 mask = (this->num_groups - 1) << this->lowest_randbit;
323 byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
325 return SpriteGroup::Resolve(this->groups[index], object, false);
329 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject &object) const
331 return object.ResolveReal(this);
335 * Process registers and the construction stage into the sprite layout.
336 * The passed construction stage might get reset to zero, if it gets incorporated into the layout
337 * during the preprocessing.
338 * @param [in, out] stage Construction stage (0-3), or NULL if not applicable.
339 * @return sprite layout to draw.
341 const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8 *stage) const
343 if (!this->dts.NeedsPreprocessing()) {
344 if (stage != NULL && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
345 return &this->dts;
348 static DrawTileSprites result;
349 uint8 actual_stage = stage != NULL ? *stage : 0;
350 this->dts.PrepareLayout(0, 0, 0, actual_stage, false);
351 this->dts.ProcessRegisters(0, 0, false);
352 result.seq = this->dts.GetLayout(&result.ground);
354 /* Stage has been processed by PrepareLayout(), set it to zero. */
355 if (stage != NULL) *stage = 0;
357 return &result;