OpenTTD Source: src/newgrf_spritegroup.cpp Source File

OpenTTD Source 13.2.1

newgrf_spritegroup.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "newgrf_spritegroup.h"
13 #include "newgrf_profiling.h"
14 #include "core/pool_func.hpp"
15
16 #include "safeguards.h"
17
18 SpriteGroupPool _spritegroup_pool("SpriteGroup");
19 INSTANTIATE_POOL_METHODS(SpriteGroup)
20
21 TemporaryStorageArray<int32, 0x110> _temp_store;
22
23
34 /* static */ const SpriteGroup *SpriteGroup::Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level)
35 {
36 if (group == nullptr) return nullptr;
37
38 const GRFFile *grf = object.grffile;
39 auto profiler = std::find_if(_newgrf_profilers.begin(), _newgrf_profilers.end(), [&](const NewGRFProfiler &pr) { return pr.grffile == grf; });
40
41 if (profiler == _newgrf_profilers.end() || !profiler->active) {
42 if (top_level) _temp_store.ClearChanges();
43 return group->Resolve(object);
44 } else if (top_level) {
45 profiler->BeginResolve(object);
46 _temp_store.ClearChanges();
47 const SpriteGroup *result = group->Resolve(object);
48 profiler->EndResolve(result);
49 return result;
50 } else {
51 profiler->RecursiveResolve();
52 return group->Resolve(object);
53 }
54 }
55
56 static inline uint32 GetVariable(const ResolverObject &object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
57 {
58 uint32 value;
59 switch (variable) {
60 case 0x0C: return object.callback;
61 case 0x10: return object.callback_param1;
62 case 0x18: return object.callback_param2;
63 case 0x1C: return object.last_value;
64
65 case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
66
67 case 0x7D: return _temp_store.GetValue(parameter);
68
69 case 0x7F:
70 if (object.grffile == nullptr) return 0;
71 return object.grffile->GetParam(parameter);
72
73 default:
74 /* First handle variables common with Action7/9/D */
75 if (variable < 0x40 && GetGlobalVariable(variable, &value, object.grffile)) return value;
76 /* Not a common variable, so evaluate the feature specific variables */
77 return scope->GetVariable(variable, parameter, available);
78 }
79 }
80
85 /* virtual */ uint32 ScopeResolver::GetRandomBits() const
86 {
87 return 0;
88 }
89
94 /* virtual */ uint32 ScopeResolver::GetTriggers() const
95 {
96 return 0;
97 }
98
106 /* virtual */ uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
107 {
108 Debug(grf, 1, "Unhandled scope variable 0x{:X}", variable);
109 *available = false;
110 return UINT_MAX;
111 }
112
118 /* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
119
125 /* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
126 {
127 if (!group->loaded.empty()) return group->loaded[0];
128 if (!group->loading.empty()) return group->loading[0];
129
130 return nullptr;
131 }
132
139 /* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
140 {
141 return &this->default_scope;
142 }
143
144 /* Evaluate an adjustment for a variable of the given size.
145 * U is the unsigned type and S is the signed type to use. */
146 template <typename U, typename S>
147 static U EvalAdjustT(const DeterministicSpriteGroupAdjust &adjust, ScopeResolver *scope, U last_value, uint32 value)
148 {
149 value >>= adjust.shift_num;
150 value &= adjust.and_mask;
151
152 switch (adjust.type) {
153 case DSGA_TYPE_DIV: value = ((S)value + (S)adjust.add_val) / (S)adjust.divmod_val; break;
154 case DSGA_TYPE_MOD: value = ((S)value + (S)adjust.add_val) % (S)adjust.divmod_val; break;
155 case DSGA_TYPE_NONE: break;
156 }
157
158 switch (adjust.operation) {
159 case DSGA_OP_ADD: return last_value + value;
160 case DSGA_OP_SUB: return last_value - value;
161 case DSGA_OP_SMIN: return std::min<S>(last_value, value);
162 case DSGA_OP_SMAX: return std::max<S>(last_value, value);
163 case DSGA_OP_UMIN: return std::min<U>(last_value, value);
164 case DSGA_OP_UMAX: return std::max<U>(last_value, value);
165 case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
166 case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
167 case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
168 case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
169 case DSGA_OP_MUL: return last_value * value;
170 case DSGA_OP_AND: return last_value & value;
171 case DSGA_OP_OR: return last_value | value;
172 case DSGA_OP_XOR: return last_value ^ value;
173 case DSGA_OP_STO: _temp_store.StoreValue((U)value, (S)last_value); return last_value;
174 case DSGA_OP_RST: return value;
175 case DSGA_OP_STOP: scope->StorePSA((U)value, (S)last_value); return last_value;
176 case DSGA_OP_ROR: return ROR<uint32>((U)last_value, (U)value & 0x1F); // mask 'value' to 5 bits, which should behave the same on all architectures.
177 case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
178 case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
179 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.
180 case DSGA_OP_SHR: return (uint32)(U)last_value >> ((U)value & 0x1F);
181 case DSGA_OP_SAR: return (int32)(S)last_value >> ((U)value & 0x1F);
182 default: return value;
183 }
184 }
185
186
187 static bool RangeHighComparator(const DeterministicSpriteGroupRange& range, uint32 value)
188 {
189 return range.high < value;
190 }
191
192 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) const
193 {
194 uint32 last_value = 0;
195 uint32 value = 0;
196
197 ScopeResolver *scope = object.GetScope(this->var_scope);
198
199 for (const auto &adjust : this->adjusts) {
200 /* Try to get the variable. We shall assume it is available, unless told otherwise. */
201 bool available = true;
202 if (adjust.variable == 0x7E) {
203 const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust.subroutine, object, false);
204 if (subgroup == nullptr) {
205 value = CALLBACK_FAILED;
206 } else {
207 value = subgroup->GetCallbackResult();
208 }
209
210 /* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
211 } else if (adjust.variable == 0x7B) {
212 value = GetVariable(object, scope, adjust.parameter, last_value, &available);
213 } else {
214 value = GetVariable(object, scope, adjust.variable, adjust.parameter, &available);
215 }
216
217 if (!available) {
218 /* Unsupported variable: skip further processing and return either
219 * the group from the first range or the default group. */
220 return SpriteGroup::Resolve(this->error_group, object, false);
221 }
222
223 switch (this->size) {
224 case DSG_SIZE_BYTE: value = EvalAdjustT<uint8, int8> (adjust, scope, last_value, value); break;
225 case DSG_SIZE_WORD: value = EvalAdjustT<uint16, int16>(adjust, scope, last_value, value); break;
226 case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, scope, last_value, value); break;
227 default: NOT_REACHED();
228 }
229 last_value = value;
230 }
231
232 object.last_value = last_value;
233
234 if (this->calculated_result) {
235 /* nvar == 0 is a special case -- we turn our value into a callback result */
236 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
237 static CallbackResultSpriteGroup nvarzero(0, true);
238 nvarzero.result = value;
239 return &nvarzero;
240 }
241
242 if (this->ranges.size() > 4) {
243 const auto &lower = std::lower_bound(this->ranges.begin(), this->ranges.end(), value, RangeHighComparator);
244 if (lower != this->ranges.end() && lower->low <= value) {
245 assert(lower->low <= value && value <= lower->high);
246 return SpriteGroup::Resolve(lower->group, object, false);
247 }
248 } else {
249 for (const auto &range : this->ranges) {
250 if (range.low <= value && value <= range.high) {
251 return SpriteGroup::Resolve(range.group, object, false);
252 }
253 }
254 }
255
256 return SpriteGroup::Resolve(this->default_group, object, false);
257 }
258
259
260 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject &object) const
261 {
262 ScopeResolver *scope = object.GetScope(this->var_scope, this->count);
263 if (object.callback == CBID_RANDOM_TRIGGER) {
264 /* Handle triggers */
265 byte match = this->triggers & object.waiting_triggers;
266 bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
267
268 if (res) {
269 object.used_triggers |= match;
270 object.reseed[this->var_scope] |= (this->groups.size() - 1) << this->lowest_randbit;
271 }
272 }
273
274 uint32 mask = ((uint)this->groups.size() - 1) << this->lowest_randbit;
275 byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
276
277 return SpriteGroup::Resolve(this->groups[index], object, false);
278 }
279
280
281 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject &object) const
282 {
283 return object.ResolveReal(this);
284 }
285
293 const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8 *stage) const
294 {
295 if (!this->dts.NeedsPreprocessing()) {
296 if (stage != nullptr && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
297 return &this->dts;
298 }
299
300 static DrawTileSprites result;
301 uint8 actual_stage = stage != nullptr ? *stage : 0;
302 this->dts.PrepareLayout(0, 0, 0, actual_stage, false);
303 this->dts.ProcessRegisters(0, 0, false);
304 result.seq = this->dts.GetLayout(&result.ground);
305
306 /* Stage has been processed by PrepareLayout(), set it to zero. */
307 if (stage != nullptr) *stage = 0;
308
309 return &result;
310 }
DSGA_OP_SCMP
@ DSGA_OP_SCMP
(signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
Definition: newgrf_spritegroup.h:139
VarSpriteGroupScope
VarSpriteGroupScope
Definition: newgrf_spritegroup.h:97
DSGA_OP_STOP
@ DSGA_OP_STOP
store a into persistent storage, indexed by b, return a
Definition: newgrf_spritegroup.h:137
DeterministicSpriteGroupRange
Definition: newgrf_spritegroup.h:160
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:32
NewGRFSpriteLayout::PrepareLayout
uint32 PrepareLayout(uint32 orig_offset, uint32 newgrf_ground_offset, uint32 newgrf_offset, uint constr_stage, bool separate_ground) const
Prepares a sprite layout before resolving action-1-2-3 chains.
Definition: newgrf_commons.cpp:660
DSGA_OP_SUB
@ DSGA_OP_SUB
a - b
Definition: newgrf_spritegroup.h:122
RandomizedSpriteGroup::Resolve
const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.cpp:260
ScopeResolver::GetTriggers
virtual uint32 GetTriggers() const
Get the triggers.
Definition: newgrf_spritegroup.cpp:94
RandomizedSpriteGroup::cmp_mode
RandomizedSpriteGroupCompareMode cmp_mode
Check for these triggers:
Definition: newgrf_spritegroup.h:195
DeterministicSpriteGroupAdjust::parameter
byte parameter
Used for variables between 0x60 and 0x7F inclusive.
Definition: newgrf_spritegroup.h:151
Pool::PoolItem<&_spritegroup_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
DSGA_OP_STO
@ DSGA_OP_STO
store a into temporary storage, indexed by b. return a
Definition: newgrf_spritegroup.h:135
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:307
DSGA_OP_AND
@ DSGA_OP_AND
a & b
Definition: newgrf_spritegroup.h:132
DSGA_OP_SMAX
@ DSGA_OP_SMAX
(signed) max(a, b)
Definition: newgrf_spritegroup.h:124
NewGRFSpriteLayout::ProcessRegisters
void ProcessRegisters(uint8 resolved_var10, uint32 resolved_sprite, bool separate_ground) const
Evaluates the register modifiers and integrates them into the preprocessed sprite layout.
Definition: newgrf_commons.cpp:731
ScopeResolver::StorePSA
virtual void StorePSA(uint reg, int32 value)
Store a value into the persistent storage area (PSA).
Definition: newgrf_spritegroup.cpp:118
TileLayoutSpriteGroup::ProcessRegisters
const DrawTileSprites * ProcessRegisters(uint8 *stage) const
Process registers and the construction stage into the sprite layout.
Definition: newgrf_spritegroup.cpp:293
DSGA_OP_XOR
@ DSGA_OP_XOR
a ^ b
Definition: newgrf_spritegroup.h:134
DSGA_OP_ADD
@ DSGA_OP_ADD
a + b
Definition: newgrf_spritegroup.h:121
DSGA_OP_ROR
@ DSGA_OP_ROR
rotate a b positions to the right
Definition: newgrf_spritegroup.h:138
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
Definition: newgrf_spritegroup.h:288
RandomizedSpriteGroup::var_scope
VarSpriteGroupScope var_scope
Take this object:
Definition: newgrf_spritegroup.h:193
ScopeResolver::GetRandomBits
virtual uint32 GetRandomBits() const
Get a few random bits.
Definition: newgrf_spritegroup.cpp:85
DSGA_OP_OR
@ DSGA_OP_OR
a | b
Definition: newgrf_spritegroup.h:133
RealSpriteGroup::loading
std::vector< const SpriteGroup * > loading
List of loading groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:90
DSGA_OP_UMIN
@ DSGA_OP_UMIN
(unsigned) min(a, b)
Definition: newgrf_spritegroup.h:125
ResolverObject::default_scope
ScopeResolver default_scope
Default implementation of the grf scope.
Definition: newgrf_spritegroup.h:323
RealSpriteGroup::Resolve
const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.cpp:281
CallbackResultSpriteGroup
Definition: newgrf_spritegroup.h:210
safeguards.h
NewGRFSpriteLayout::NeedsPreprocessing
bool NeedsPreprocessing() const
Tests whether this spritelayout needs preprocessing by PrepareLayout() and ProcessRegisters(),...
Definition: newgrf_commons.h:150
DrawTileSprites
Ground palette sprite of a tile, together with its sprite layout.
Definition: sprite.h:58
ResolverObject::GetScope
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0)
Get a resolver for the scope.
Definition: newgrf_spritegroup.cpp:139
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:75
DSGA_OP_UDIV
@ DSGA_OP_UDIV
(unsigned) a / b
Definition: newgrf_spritegroup.h:129
stdafx.h
DSGA_OP_SHR
@ DSGA_OP_SHR
(unsigned) a >> b
Definition: newgrf_spritegroup.h:142
RealSpriteGroup::loaded
std::vector< const SpriteGroup * > loaded
List of loaded groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:89
DeterministicSpriteGroup::Resolve
const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.cpp:192
newgrf_spritegroup.h
DeterministicSpriteGroupAdjust
Definition: newgrf_spritegroup.h:147
DSGA_OP_MUL
@ DSGA_OP_MUL
a * b
Definition: newgrf_spritegroup.h:131
SpriteGroup::Resolve
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.h:61
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:408
Pool
Base class for all pools.
Definition: pool_type.hpp:81
NewGRFProfiler
Callback profiler for NewGRF development.
Definition: newgrf_profiling.h:26
DSGA_OP_UMOD
@ DSGA_OP_UMOD
(unsigned) a & b
Definition: newgrf_spritegroup.h:130
GetGlobalVariable
bool GetGlobalVariable(byte param, uint32 *value, const GRFFile *grffile)
Reads a variable common to VarAction2 and Action7/9/D.
Definition: newgrf.cpp:6338
RealSpriteGroup
Definition: newgrf_spritegroup.h:79
DSGA_OP_SHL
@ DSGA_OP_SHL
a << b
Definition: newgrf_spritegroup.h:141
DSGA_OP_UMAX
@ DSGA_OP_UMAX
(unsigned) max(a, b)
Definition: newgrf_spritegroup.h:126
NewGRFSpriteLayout::consistent_max_offset
uint consistent_max_offset
Number of sprites in all referenced spritesets.
Definition: newgrf_commons.h:120
ScopeResolver::GetVariable
virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const
Get a variable value.
Definition: newgrf_spritegroup.cpp:106
DSGA_OP_SAR
@ DSGA_OP_SAR
(signed) a >> b
Definition: newgrf_spritegroup.h:143
DSGA_OP_SMIN
@ DSGA_OP_SMIN
(signed) min(a, b)
Definition: newgrf_spritegroup.h:123
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
RandomizedSpriteGroup::lowest_randbit
byte lowest_randbit
Look for this in the per-object randomized bitmask:
Definition: newgrf_spritegroup.h:199
TemporaryStorageArray
Class for temporary storage of data.
Definition: newgrf_storage.h:150
NewGRFSpriteLayout::GetLayout
const DrawTileSeqStruct * GetLayout(PalSpriteID *ground) const
Returns the result spritelayout after preprocessing.
Definition: newgrf_commons.h:163
DSGA_OP_UCMP
@ DSGA_OP_UCMP
(unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
Definition: newgrf_spritegroup.h:140
DSGA_OP_SMOD
@ DSGA_OP_SMOD
(signed) a % b
Definition: newgrf_spritegroup.h:128
CBID_RANDOM_TRIGGER
@ CBID_RANDOM_TRIGGER
Set when calling a randomizing trigger (almost undocumented).
Definition: newgrf_callbacks.h:25
ResolverObject::ResolveReal
virtual const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const
Get the real sprites of the grf.
Definition: newgrf_spritegroup.cpp:125
pool_func.hpp
newgrf_profiling.h
DSGA_OP_RST
@ DSGA_OP_RST
return b
Definition: newgrf_spritegroup.h:136
RandomizedSpriteGroup::groups
std::vector< const SpriteGroup * > groups
Take the group with appropriate index:
Definition: newgrf_spritegroup.h:201
SpriteGroup
Definition: newgrf_spritegroup.h:57
DSGA_OP_SDIV
@ DSGA_OP_SDIV
(signed) a / b
Definition: newgrf_spritegroup.h:127
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:106
debug.h