Fix old map array tunnel head conversion
[openttd/fttd.git] / src / newgrf_spritegroup.cpp
blob37c6a1bb246d97ef4651c320d281820ff0fe5893
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 std::deque <ttd_unique_ptr <SpriteGroup> > SpriteGroup::pool;
19 TemporaryStorageArray<int32, 0x110> _temp_store;
22 /**
23 * ResolverObject (re)entry point.
24 * This cannot be made a call to a virtual function because virtual functions
25 * do not like NULL and checking for NULL *everywhere* is more cumbersome than
26 * this little helper function.
27 * @param group the group to resolve for
28 * @param object information needed to resolve the group
29 * @param top_level true if this is a top-level SpriteGroup, false if used nested in another SpriteGroup.
30 * @return the resolved group
32 /* static */ const SpriteGroup *SpriteGroup::Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level)
34 if (group == NULL) return NULL;
35 if (top_level) {
36 _temp_store.ClearChanges();
38 return group->Resolve(object);
42 static inline uint32 GetVariable(const ResolverObject &object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
44 /* First handle variables common with Action7/9/D */
45 uint32 value;
46 if (GetGlobalVariable(variable, &value, object.grffile)) return value;
48 /* Non-common variable */
49 switch (variable) {
50 case 0x0C: return object.callback;
51 case 0x10: return object.callback_param1;
52 case 0x18: return object.callback_param2;
53 case 0x1C: return object.last_value;
55 case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
57 case 0x7D: return _temp_store.GetValue(parameter);
59 case 0x7F:
60 if (object.grffile == NULL) return 0;
61 return object.grffile->GetParam(parameter);
63 /* Not a common variable, so evaluate the feature specific variables */
64 default: return scope->GetVariable(variable, parameter, available);
68 /**
69 * Get a few random bits. Default implementation has no random bits.
70 * @return Random bits.
72 /* virtual */ uint32 ScopeResolver::GetRandomBits() const
74 return 0;
77 /**
78 * Get the triggers. Base class returns \c 0 to prevent trouble.
79 * @return The triggers.
81 /* virtual */ uint32 ScopeResolver::GetTriggers() const
83 return 0;
86 /**
87 * Get a variable value. Default implementation has no available variables.
88 * @param variable Variable to read
89 * @param parameter Parameter for 60+x variables
90 * @param[out] available Set to false, in case the variable does not exist.
91 * @return Value
93 /* virtual */ uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
95 DEBUG(grf, 1, "Unhandled scope variable 0x%X", variable);
96 *available = false;
97 return UINT_MAX;
101 * Store a value into the persistent storage area (PSA). Default implementation does nothing (for newgrf classes without storage).
102 * @param pos Position to store into.
103 * @param value Value to store.
105 /* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
108 * Resolver constructor.
109 * @param grffile NewGRF file associated with the object (or \c NULL if none).
110 * @param callback Callback code being resolved (default value is #CBID_NO_CALLBACK).
111 * @param callback_param1 First parameter (var 10) of the callback (only used when \a callback is also set).
112 * @param callback_param2 Second parameter (var 18) of the callback (only used when \a callback is also set).
114 ResolverObject::ResolverObject(const GRFFile *grffile, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
115 : grffile(grffile), default_scope()
117 this->callback = callback;
118 this->callback_param1 = callback_param1;
119 this->callback_param2 = callback_param2;
120 this->ResetState();
123 ResolverObject::~ResolverObject() {}
126 * Get the real sprites of the grf.
127 * @param group Group to get.
128 * @return The available sprite group.
130 /* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
132 return NULL;
136 * Get a resolver for the \a scope.
137 * @param scope Scope to return.
138 * @param relative Additional parameter for #VSG_SCOPE_RELATIVE.
139 * @return The resolver for the requested scope.
141 /* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
143 return &this->default_scope;
147 * Rotate val rot times to the right
148 * @param val the value to rotate
149 * @param rot the amount of times to rotate
150 * @return the rotated value
152 static uint32 RotateRight(uint32 val, uint32 rot)
154 /* Do not rotate more than necessary */
155 rot %= 32;
157 return (val >> rot) | (val << (32 - rot));
161 /* Evaluate an adjustment for a variable of the given size.
162 * U is the unsigned type and S is the signed type to use. */
163 template <typename U, typename S>
164 static U EvalAdjustT (const DeterministicSpriteGroup::Adjust *adjust, ScopeResolver *scope, U last_value, uint32 value)
166 value >>= adjust->shift_num;
167 value &= adjust->and_mask;
169 if (adjust->type != 0) {
170 value += (S)adjust->add_val;
172 if (adjust->type == 1) {
173 value = (S)value / (S)adjust->divmod_val;
174 } else {
175 value = (S)value % (S)adjust->divmod_val;
179 switch (adjust->operation) {
180 case 0: return last_value + value;
181 case 1: return last_value - value;
182 case 2: return min ((S)last_value, (S)value);
183 case 3: return max ((S)last_value, (S)value);
184 case 4: return min ((U)last_value, (U)value);
185 case 5: return max ((U)last_value, (U)value);
186 case 6: return value == 0 ? (S)last_value : (S)last_value / (S)value;
187 case 7: return value == 0 ? (S)last_value : (S)last_value % (S)value;
188 case 8: return value == 0 ? (U)last_value : (U)last_value / (U)value;
189 case 9: return value == 0 ? (U)last_value : (U)last_value % (U)value;
190 case 10: return last_value * value;
191 case 11: return last_value & value;
192 case 12: return last_value | value;
193 case 13: return last_value ^ value;
194 case 14: _temp_store.StoreValue ((U)value, (S)last_value); return last_value;
195 case 15: return value;
196 case 16: scope->StorePSA ((U)value, (S)last_value); return last_value;
197 case 17: return RotateRight (last_value, value);
198 case 18: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
199 case 19: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
200 case 20: 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.
201 case 21: return (uint32)(U)last_value >> ((U)value & 0x1F);
202 case 22: return (int32) (S)last_value >> ((U)value & 0x1F);
203 default: return value;
208 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) const
210 uint32 last_value = 0;
211 uint32 value = 0;
212 uint i;
214 ScopeResolver *scope = object.GetScope(this->var_scope);
216 for (i = 0; i < this->num_adjusts; i++) {
217 Adjust *adjust = &this->adjusts[i];
219 /* Try to get the variable. We shall assume it is available, unless told otherwise. */
220 bool available = true;
221 if (adjust->variable == 0x7E) {
222 const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object, false);
223 if (subgroup == NULL) {
224 value = CALLBACK_FAILED;
225 } else {
226 value = subgroup->GetCallbackResult();
229 /* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
230 } else {
231 byte variable = adjust->variable;
232 uint32 parameter = adjust->parameter;
233 if (variable == 0x7B) {
234 variable = parameter;
235 parameter = last_value;
237 value = GetVariable (object, scope, variable, parameter, &available);
240 if (!available) {
241 /* Unsupported variable: skip further processing and return either
242 * the group from the first range or the default group. */
243 return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object, false);
246 switch (this->size) {
247 case 0: value = EvalAdjustT<uint8, int8> (adjust, scope, last_value, value); break;
248 case 1: value = EvalAdjustT<uint16, int16>(adjust, scope, last_value, value); break;
249 case 2: value = EvalAdjustT<uint32, int32>(adjust, scope, last_value, value); break;
250 default: NOT_REACHED();
252 last_value = value;
255 object.last_value = last_value;
257 if (this->num_ranges == 0) {
258 /* nvar == 0 is a special case -- we turn our value into a callback result */
259 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
260 static CallbackResultSpriteGroup nvarzero(0, true);
261 nvarzero.result = value;
262 return &nvarzero;
265 for (i = 0; i < this->num_ranges; i++) {
266 if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
267 return SpriteGroup::Resolve(this->ranges[i].group, object, false);
271 return SpriteGroup::Resolve(this->default_group, object, false);
275 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject &object) const
277 ScopeResolver *scope = object.GetScope(this->var_scope, this->count);
278 if (object.callback == CBID_RANDOM_TRIGGER) {
279 /* Handle triggers */
280 byte match = this->triggers & object.waiting_triggers;
281 bool res = this->cmp_mode ? (match == this->triggers) : (match != 0);
283 if (res) {
284 object.used_triggers |= match;
285 object.reseed[this->var_scope] |= (this->num_groups - 1) << this->lowest_randbit;
289 uint32 mask = (this->num_groups - 1) << this->lowest_randbit;
290 byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
292 return SpriteGroup::Resolve(this->groups[index], object, false);
296 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject &object) const
298 return object.ResolveReal(this);
302 * Process registers and the construction stage into the sprite layout.
303 * The passed construction stage might get reset to zero, if it gets incorporated into the layout
304 * during the preprocessing.
305 * @param group Source layout.
306 * @param stage Construction stage (0-3).
308 TileLayoutSpriteGroup::Result::Result (const TileLayoutSpriteGroup *group, byte stage)
310 if (!group->dts.NeedsPreprocessing()) {
311 this->seq = group->dts.seq;
312 this->ground = group->dts.ground;
313 uint n = group->dts.consistent_max_offset;
314 this->stage = (n > 0) ? GetConstructionStageOffset (stage, n) : 0;
315 return;
318 this->prepare (&group->dts, stage);
319 this->process (&group->dts);
320 this->ground = this->get_ground();
321 this->seq = this->get_seq();
323 /* Stage has been processed by PrepareLayout(), set it to zero. */
324 this->stage = 0;