OpenTTD

newgrf_spritegroup.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_spritegroup.cpp 27989 2018-03-11 15:08:51Z 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 <algorithm>
14 #include "debug.h"
15 #include "newgrf_spritegroup.h"
16 #include "core/pool_func.hpp"
17
18 #include "safeguards.h"
19
20 SpriteGroupPool _spritegroup_pool("SpriteGroup");
21 INSTANTIATE_POOL_METHODS(SpriteGroup)
22
23 TemporaryStorageArray<int32, 0x110> _temp_store;
24
25
36 /* static */ const SpriteGroup *SpriteGroup::Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level)
37 {
38 if (group == NULL) return NULL;
39 if (top_level) {
40 _temp_store.ClearChanges();
41 }
42 return group->Resolve(object);
43 }
44
45 RealSpriteGroup::~RealSpriteGroup()
46 {
47 free(this->loaded);
48 free(this->loading);
49 }
50
51 DeterministicSpriteGroup::~DeterministicSpriteGroup()
52 {
53 free(this->adjusts);
54 free(this->ranges);
55 }
56
57 RandomizedSpriteGroup::~RandomizedSpriteGroup()
58 {
59 free(this->groups);
60 }
61
62 static inline uint32 GetVariable(const ResolverObject &object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
63 {
64 uint32 value;
65 switch (variable) {
66 case 0x0C: return object.callback;
67 case 0x10: return object.callback_param1;
68 case 0x18: return object.callback_param2;
69 case 0x1C: return object.last_value;
70
71 case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
72
73 case 0x7D: return _temp_store.GetValue(parameter);
74
75 case 0x7F:
76 if (object.grffile == NULL) return 0;
77 return object.grffile->GetParam(parameter);
78
79 default:
80 /* First handle variables common with Action7/9/D */
81 if (variable < 0x40 && GetGlobalVariable(variable, &value, object.grffile)) return value;
82 /* Not a common variable, so evaluate the feature specific variables */
83 return scope->GetVariable(variable, parameter, available);
84 }
85 }
86
91 /* virtual */ uint32 ScopeResolver::GetRandomBits() const
92 {
93 return 0;
94 }
95
100 /* virtual */ uint32 ScopeResolver::GetTriggers() const
101 {
102 return 0;
103 }
104
112 /* virtual */ uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
113 {
114 DEBUG(grf, 1, "Unhandled scope variable 0x%X", variable);
115 *available = false;
116 return UINT_MAX;
117 }
118
124 /* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
125
131 /* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
132 {
133 return NULL;
134 }
135
142 /* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
143 {
144 return &this->default_scope;
145 }
146
153 static uint32 RotateRight(uint32 val, uint32 rot)
154 {
155 /* Do not rotate more than necessary */
156 rot %= 32;
157
158 return (val >> rot) | (val << (32 - rot));
159 }
160
161
162 /* Evaluate an adjustment for a variable of the given size.
163 * U is the unsigned type and S is the signed type to use. */
164 template <typename U, typename S>
165 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ScopeResolver *scope, U last_value, uint32 value)
166 {
167 value >>= adjust->shift_num;
168 value &= adjust->and_mask;
169
170 switch (adjust->type) {
171 case DSGA_TYPE_DIV: value = ((S)value + (S)adjust->add_val) / (S)adjust->divmod_val; break;
172 case DSGA_TYPE_MOD: value = ((S)value + (S)adjust->add_val) % (S)adjust->divmod_val; break;
173 case DSGA_TYPE_NONE: break;
174 }
175
176 switch (adjust->operation) {
177 case DSGA_OP_ADD: return last_value + value;
178 case DSGA_OP_SUB: return last_value - value;
179 case DSGA_OP_SMIN: return min((S)last_value, (S)value);
180 case DSGA_OP_SMAX: return max((S)last_value, (S)value);
181 case DSGA_OP_UMIN: return min((U)last_value, (U)value);
182 case DSGA_OP_UMAX: return max((U)last_value, (U)value);
183 case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
184 case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
185 case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
186 case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
187 case DSGA_OP_MUL: return last_value * value;
188 case DSGA_OP_AND: return last_value & value;
189 case DSGA_OP_OR: return last_value | value;
190 case DSGA_OP_XOR: return last_value ^ value;
191 case DSGA_OP_STO: _temp_store.StoreValue((U)value, (S)last_value); return last_value;
192 case DSGA_OP_RST: return value;
193 case DSGA_OP_STOP: scope->StorePSA((U)value, (S)last_value); return last_value;
194 case DSGA_OP_ROR: return RotateRight(last_value, value);
195 case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
196 case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
197 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.
198 case DSGA_OP_SHR: return (uint32)(U)last_value >> ((U)value & 0x1F);
199 case DSGA_OP_SAR: return (int32)(S)last_value >> ((U)value & 0x1F);
200 default: return value;
201 }
202 }
203
204
205 static bool RangeHighComparator(const DeterministicSpriteGroupRange& range, uint32 value)
206 {
207 return range.high < value;
208 }
209
210 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) const
211 {
212 uint32 last_value = 0;
213 uint32 value = 0;
214 uint i;
215
216 ScopeResolver *scope = object.GetScope(this->var_scope);
217
218 for (i = 0; i < this->num_adjusts; i++) {
219 DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
220
221 /* Try to get the variable. We shall assume it is available, unless told otherwise. */
222 bool available = true;
223 if (adjust->variable == 0x7E) {
224 const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object, false);
225 if (subgroup == NULL) {
226 value = CALLBACK_FAILED;
227 } else {
228 value = subgroup->GetCallbackResult();
229 }
230
231 /* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
232 } else if (adjust->variable == 0x7B) {
233 value = GetVariable(object, scope, adjust->parameter, last_value, &available);
234 } else {
235 value = GetVariable(object, scope, adjust->variable, adjust->parameter, &available);
236 }
237
238 if (!available) {
239 /* Unsupported variable: skip further processing and return either
240 * the group from the first range or the default group. */
241 return SpriteGroup::Resolve(this->error_group, object, false);
242 }
243
244 switch (this->size) {
245 case DSG_SIZE_BYTE: value = EvalAdjustT<uint8, int8> (adjust, scope, last_value, value); break;
246 case DSG_SIZE_WORD: value = EvalAdjustT<uint16, int16>(adjust, scope, last_value, value); break;
247 case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, scope, last_value, value); break;
248 default: NOT_REACHED();
249 }
250 last_value = value;
251 }
252
253 object.last_value = last_value;
254
255 if (this->calculated_result) {
256 /* nvar == 0 is a special case -- we turn our value into a callback result */
257 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
258 static CallbackResultSpriteGroup nvarzero(0, true);
259 nvarzero.result = value;
260 return &nvarzero;
261 }
262
263 if (this->num_ranges > 4) {
264 DeterministicSpriteGroupRange *lower = std::lower_bound(this->ranges + 0, this->ranges + this->num_ranges, value, RangeHighComparator);
265 if (lower != this->ranges + this->num_ranges && lower->low <= value) {
266 assert(lower->low <= value && value <= lower->high);
267 return SpriteGroup::Resolve(lower->group, object, false);
268 }
269 } else {
270 for (i = 0; i < this->num_ranges; i++) {
271 if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
272 return SpriteGroup::Resolve(this->ranges[i].group, object, false);
273 }
274 }
275 }
276
277 return SpriteGroup::Resolve(this->default_group, object, false);
278 }
279
280
281 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject &object) const
282 {
283 ScopeResolver *scope = object.GetScope(this->var_scope, this->count);
284 if (object.callback == CBID_RANDOM_TRIGGER) {
285 /* Handle triggers */
286 byte match = this->triggers & object.waiting_triggers;
287 bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
288
289 if (res) {
290 object.used_triggers |= match;
291 object.reseed[this->var_scope] |= (this->num_groups - 1) << this->lowest_randbit;
292 }
293 }
294
295 uint32 mask = (this->num_groups - 1) << this->lowest_randbit;
296 byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
297
298 return SpriteGroup::Resolve(this->groups[index], object, false);
299 }
300
301
302 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject &object) const
303 {
304 return object.ResolveReal(this);
305 }
306
314 const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8 *stage) const
315 {
316 if (!this->dts.NeedsPreprocessing()) {
317 if (stage != NULL && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
318 return &this->dts;
319 }
320
321 static DrawTileSprites result;
322 uint8 actual_stage = stage != NULL ? *stage : 0;
323 this->dts.PrepareLayout(0, 0, 0, actual_stage, false);
324 this->dts.ProcessRegisters(0, 0, false);
325 result.seq = this->dts.GetLayout(&result.ground);
326
327 /* Stage has been processed by PrepareLayout(), set it to zero. */
328 if (stage != NULL) *stage = 0;
329
330 return &result;
331 }
DSGA_OP_ROR
rotate a b positions to the right
Definition: newgrf_spritegroup.h:141
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope)...
Definition: newgrf_spritegroup.h:291
DeterministicSpriteGroup::Resolve
const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.cpp:210
DSGA_OP_OR
a | b
Definition: newgrf_spritegroup.h:136
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:406
DSGA_OP_UMOD
(unsigned) a & b
Definition: newgrf_spritegroup.h:133
VarSpriteGroupScope
VarSpriteGroupScope
Definition: newgrf_spritegroup.h:100
debug.h
Functions related to debugging.
ResolverObject::ResolveReal
virtual const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const
Get the real sprites of the grf.
Definition: newgrf_spritegroup.cpp:131
TemporaryStorageArray
Class for temporary storage of data.
Definition: newgrf_storage.h:152
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:310
DSGA_OP_UCMP
(unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
Definition: newgrf_spritegroup.h:143
Pool::PoolItem<&_spritegroup_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
ResolverObject::GetScope
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0)
Get a resolver for the scope.
Definition: newgrf_spritegroup.cpp:142
DSGA_OP_SDIV
(signed) a / b
Definition: newgrf_spritegroup.h:130
DSGA_OP_MUL
a * b
Definition: newgrf_spritegroup.h:134
CallbackResultSpriteGroup
Definition: newgrf_spritegroup.h:218
max
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
DrawTileSprites::seq
const DrawTileSeqStruct * seq
Array of child sprites. Terminated with a terminator entry.
Definition: sprite.h:62
DSGA_OP_SAR
(signed) a >> b
Definition: newgrf_spritegroup.h:146
RealSpriteGroup
Definition: newgrf_spritegroup.h:79
pool_func.hpp
Some methods of Pool are placed here in order to reduce compilation time and binary size...
SpriteGroup::Resolve
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.h:62
newgrf_spritegroup.h
Action 2 handling.
TileLayoutSpriteGroup::ProcessRegisters
const DrawTileSprites * ProcessRegisters(uint8 *stage) const
Process registers and the construction stage into the sprite layout.
Definition: newgrf_spritegroup.cpp:314
DrawTileSprites
Ground palette sprite of a tile, together with its sprite layout.
Definition: sprite.h:60
DeterministicSpriteGroupAdjust
Definition: newgrf_spritegroup.h:150
DSGA_OP_RST
return b
Definition: newgrf_spritegroup.h:139
DSGA_OP_SMOD
(signed) a % b
Definition: newgrf_spritegroup.h:131
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
safeguards.h
A number of safeguards to prevent using unsafe methods.
SpriteGroup
Definition: newgrf_spritegroup.h:58
DSGA_OP_SUB
a - b
Definition: newgrf_spritegroup.h:125
TemporaryStorageArray::StoreValue
void StoreValue(uint pos, int32 value)
Stores some value at a given position.
Definition: newgrf_storage.h:170
DSGA_OP_XOR
a ^ b
Definition: newgrf_spritegroup.h:137
DSGA_OP_SMIN
(signed) min(a, b)
Definition: newgrf_spritegroup.h:126
DSGA_OP_STO
store a into temporary storage, indexed by b. return a
Definition: newgrf_spritegroup.h:138
RealSpriteGroup::Resolve
const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.cpp:302
min
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
DSGA_OP_UMIN
(unsigned) min(a, b)
Definition: newgrf_spritegroup.h:128
RandomizedSpriteGroup::Resolve
const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.cpp:281
DSGA_OP_ADD
a + b
Definition: newgrf_spritegroup.h:124
DeterministicSpriteGroupRange
Definition: newgrf_spritegroup.h:163
ScopeResolver::GetVariable
virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const
Get a variable value.
Definition: newgrf_spritegroup.cpp:112
Pool
Base class for all pools.
Definition: pool_type.hpp:83
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:224
DrawTileSprites::ground
PalSpriteID ground
Palette and sprite for the ground.
Definition: sprite.h:61
DSGA_OP_UDIV
(unsigned) a / b
Definition: newgrf_spritegroup.h:132
GetGlobalVariable
bool GetGlobalVariable(byte param, uint32 *value, const GRFFile *grffile)
Reads a variable common to VarAction2 and Action7/9/D.
Definition: newgrf.cpp:5755
RotateRight
static uint32 RotateRight(uint32 val, uint32 rot)
Rotate val rot times to the right.
Definition: newgrf_spritegroup.cpp:153
ScopeResolver::StorePSA
virtual void StorePSA(uint reg, int32 value)
Store a value into the persistent storage area (PSA).
Definition: newgrf_spritegroup.cpp:124
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:34
DSGA_OP_SCMP
(signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
Definition: newgrf_spritegroup.h:142
DSGA_OP_STOP
store a into persistent storage, indexed by b, return a
Definition: newgrf_spritegroup.h:140
DSGA_OP_SHR
(unsigned) a >> b
Definition: newgrf_spritegroup.h:145
DSGA_OP_SMAX
(signed) max(a, b)
Definition: newgrf_spritegroup.h:127
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
ScopeResolver::GetRandomBits
virtual uint32 GetRandomBits() const
Get a few random bits.
Definition: newgrf_spritegroup.cpp:91
CBID_RANDOM_TRIGGER
Set when calling a randomizing trigger (almost undocumented).
Definition: newgrf_callbacks.h:27
DSGA_OP_UMAX
(unsigned) max(a, b)
Definition: newgrf_spritegroup.h:129
GetConstructionStageOffset
static uint GetConstructionStageOffset(uint construction_stage, uint num_sprites)
Determines which sprite to use from a spriteset for a specific construction stage.
Definition: newgrf_commons.h:77
TemporaryStorageArray::GetValue
TYPE GetValue(uint pos) const
Gets the value from a given position.
Definition: newgrf_storage.h:184
DeterministicSpriteGroupAdjust::parameter
byte parameter
Used for variables between 0x60 and 0x7F inclusive.
Definition: newgrf_spritegroup.h:154
DSGA_OP_AND
a & b
Definition: newgrf_spritegroup.h:135
ScopeResolver::GetTriggers
virtual uint32 GetTriggers() const
Get the triggers.
Definition: newgrf_spritegroup.cpp:100
DSGA_OP_SHL
a << b
Definition: newgrf_spritegroup.h:144