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/>.
8 /** @file newgrf_spritegroup.cpp Handling of primarily NewGRF action 2. */
12 #include "newgrf_spritegroup.h"
13 #include "newgrf_profiling.h"
14 #include "core/pool_func.hpp"
16 #include "safeguards.h"
18 SpriteGroupPool
_spritegroup_pool("SpriteGroup");
19 INSTANTIATE_POOL_METHODS(SpriteGroup
)
21 TemporaryStorageArray
<int32
, 0x110> _temp_store
;
25 * ResolverObject (re)entry point.
26 * This cannot be made a call to a virtual function because virtual functions
27 * do not like nullptr and checking for nullptr *everywhere* is more cumbersome than
28 * this little helper function.
29 * @param group the group to resolve for
30 * @param object information needed to resolve the group
31 * @param top_level true if this is a top-level SpriteGroup, false if used nested in another SpriteGroup.
32 * @return the resolved group
34 /* static */ const SpriteGroup
*SpriteGroup::Resolve(const SpriteGroup
*group
, ResolverObject
&object
, bool top_level
)
36 if (group
== nullptr) return nullptr;
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
; });
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
);
51 profiler
->RecursiveResolve();
52 return group
->Resolve(object
);
56 RealSpriteGroup::~RealSpriteGroup()
62 DeterministicSpriteGroup::~DeterministicSpriteGroup()
68 RandomizedSpriteGroup::~RandomizedSpriteGroup()
73 static inline uint32
GetVariable(const ResolverObject
&object
, ScopeResolver
*scope
, byte variable
, uint32 parameter
, bool *available
)
77 case 0x0C: return object
.callback
;
78 case 0x10: return object
.callback_param1
;
79 case 0x18: return object
.callback_param2
;
80 case 0x1C: return object
.last_value
;
82 case 0x5F: return (scope
->GetRandomBits() << 8) | scope
->GetTriggers();
84 case 0x7D: return _temp_store
.GetValue(parameter
);
87 if (object
.grffile
== nullptr) return 0;
88 return object
.grffile
->GetParam(parameter
);
91 /* First handle variables common with Action7/9/D */
92 if (variable
< 0x40 && GetGlobalVariable(variable
, &value
, object
.grffile
)) return value
;
93 /* Not a common variable, so evaluate the feature specific variables */
94 return scope
->GetVariable(variable
, parameter
, available
);
99 * Get a few random bits. Default implementation has no random bits.
100 * @return Random bits.
102 /* virtual */ uint32
ScopeResolver::GetRandomBits() const
108 * Get the triggers. Base class returns \c 0 to prevent trouble.
109 * @return The triggers.
111 /* virtual */ uint32
ScopeResolver::GetTriggers() 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.
123 /* virtual */ uint32
ScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
125 DEBUG(grf
, 1, "Unhandled scope variable 0x%X", variable
);
131 * Store a value into the persistent storage area (PSA). Default implementation does nothing (for newgrf classes without storage).
132 * @param reg Position to store into.
133 * @param value Value to store.
135 /* virtual */ void ScopeResolver::StorePSA(uint reg
, int32 value
) {}
138 * Get the real sprites of the grf.
139 * @param group Group to get.
140 * @return The available sprite group.
142 /* virtual */ const SpriteGroup
*ResolverObject::ResolveReal(const RealSpriteGroup
*group
) const
148 * Get a resolver for the \a scope.
149 * @param scope Scope to return.
150 * @param relative Additional parameter for #VSG_SCOPE_RELATIVE.
151 * @return The resolver for the requested scope.
153 /* virtual */ ScopeResolver
*ResolverObject::GetScope(VarSpriteGroupScope scope
, byte relative
)
155 return &this->default_scope
;
158 /* Evaluate an adjustment for a variable of the given size.
159 * U is the unsigned type and S is the signed type to use. */
160 template <typename U
, typename S
>
161 static U
EvalAdjustT(const DeterministicSpriteGroupAdjust
*adjust
, ScopeResolver
*scope
, U last_value
, uint32 value
)
163 value
>>= adjust
->shift_num
;
164 value
&= adjust
->and_mask
;
166 switch (adjust
->type
) {
167 case DSGA_TYPE_DIV
: value
= ((S
)value
+ (S
)adjust
->add_val
) / (S
)adjust
->divmod_val
; break;
168 case DSGA_TYPE_MOD
: value
= ((S
)value
+ (S
)adjust
->add_val
) % (S
)adjust
->divmod_val
; break;
169 case DSGA_TYPE_NONE
: break;
172 switch (adjust
->operation
) {
173 case DSGA_OP_ADD
: return last_value
+ value
;
174 case DSGA_OP_SUB
: return last_value
- value
;
175 case DSGA_OP_SMIN
: return std::min
<S
>(last_value
, value
);
176 case DSGA_OP_SMAX
: return std::max
<S
>(last_value
, value
);
177 case DSGA_OP_UMIN
: return std::min
<U
>(last_value
, value
);
178 case DSGA_OP_UMAX
: return std::max
<U
>(last_value
, value
);
179 case DSGA_OP_SDIV
: return value
== 0 ? (S
)last_value
: (S
)last_value
/ (S
)value
;
180 case DSGA_OP_SMOD
: return value
== 0 ? (S
)last_value
: (S
)last_value
% (S
)value
;
181 case DSGA_OP_UDIV
: return value
== 0 ? (U
)last_value
: (U
)last_value
/ (U
)value
;
182 case DSGA_OP_UMOD
: return value
== 0 ? (U
)last_value
: (U
)last_value
% (U
)value
;
183 case DSGA_OP_MUL
: return last_value
* value
;
184 case DSGA_OP_AND
: return last_value
& value
;
185 case DSGA_OP_OR
: return last_value
| value
;
186 case DSGA_OP_XOR
: return last_value
^ value
;
187 case DSGA_OP_STO
: _temp_store
.StoreValue((U
)value
, (S
)last_value
); return last_value
;
188 case DSGA_OP_RST
: return value
;
189 case DSGA_OP_STOP
: scope
->StorePSA((U
)value
, (S
)last_value
); return last_value
;
190 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.
191 case DSGA_OP_SCMP
: return ((S
)last_value
== (S
)value
) ? 1 : ((S
)last_value
< (S
)value
? 0 : 2);
192 case DSGA_OP_UCMP
: return ((U
)last_value
== (U
)value
) ? 1 : ((U
)last_value
< (U
)value
? 0 : 2);
193 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.
194 case DSGA_OP_SHR
: return (uint32
)(U
)last_value
>> ((U
)value
& 0x1F);
195 case DSGA_OP_SAR
: return (int32
)(S
)last_value
>> ((U
)value
& 0x1F);
196 default: return value
;
201 static bool RangeHighComparator(const DeterministicSpriteGroupRange
& range
, uint32 value
)
203 return range
.high
< value
;
206 const SpriteGroup
*DeterministicSpriteGroup::Resolve(ResolverObject
&object
) const
208 uint32 last_value
= 0;
212 ScopeResolver
*scope
= object
.GetScope(this->var_scope
);
214 for (i
= 0; i
< this->num_adjusts
; i
++) {
215 DeterministicSpriteGroupAdjust
*adjust
= &this->adjusts
[i
];
217 /* Try to get the variable. We shall assume it is available, unless told otherwise. */
218 bool available
= true;
219 if (adjust
->variable
== 0x7E) {
220 const SpriteGroup
*subgroup
= SpriteGroup::Resolve(adjust
->subroutine
, object
, false);
221 if (subgroup
== nullptr) {
222 value
= CALLBACK_FAILED
;
224 value
= subgroup
->GetCallbackResult();
227 /* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
228 } else if (adjust
->variable
== 0x7B) {
229 value
= GetVariable(object
, scope
, adjust
->parameter
, last_value
, &available
);
231 value
= GetVariable(object
, scope
, adjust
->variable
, adjust
->parameter
, &available
);
235 /* Unsupported variable: skip further processing and return either
236 * the group from the first range or the default group. */
237 return SpriteGroup::Resolve(this->error_group
, object
, false);
240 switch (this->size
) {
241 case DSG_SIZE_BYTE
: value
= EvalAdjustT
<uint8
, int8
> (adjust
, scope
, last_value
, value
); break;
242 case DSG_SIZE_WORD
: value
= EvalAdjustT
<uint16
, int16
>(adjust
, scope
, last_value
, value
); break;
243 case DSG_SIZE_DWORD
: value
= EvalAdjustT
<uint32
, int32
>(adjust
, scope
, last_value
, value
); break;
244 default: NOT_REACHED();
249 object
.last_value
= last_value
;
251 if (this->calculated_result
) {
252 /* nvar == 0 is a special case -- we turn our value into a callback result */
253 if (value
!= CALLBACK_FAILED
) value
= GB(value
, 0, 15);
254 static CallbackResultSpriteGroup
nvarzero(0, true);
255 nvarzero
.result
= value
;
259 if (this->num_ranges
> 4) {
260 DeterministicSpriteGroupRange
*lower
= std::lower_bound(this->ranges
+ 0, this->ranges
+ this->num_ranges
, value
, RangeHighComparator
);
261 if (lower
!= this->ranges
+ this->num_ranges
&& lower
->low
<= value
) {
262 assert(lower
->low
<= value
&& value
<= lower
->high
);
263 return SpriteGroup::Resolve(lower
->group
, object
, false);
266 for (i
= 0; i
< this->num_ranges
; i
++) {
267 if (this->ranges
[i
].low
<= value
&& value
<= this->ranges
[i
].high
) {
268 return SpriteGroup::Resolve(this->ranges
[i
].group
, object
, false);
273 return SpriteGroup::Resolve(this->default_group
, object
, false);
277 const SpriteGroup
*RandomizedSpriteGroup::Resolve(ResolverObject
&object
) const
279 ScopeResolver
*scope
= object
.GetScope(this->var_scope
, this->count
);
280 if (object
.callback
== CBID_RANDOM_TRIGGER
) {
281 /* Handle triggers */
282 byte match
= this->triggers
& object
.waiting_triggers
;
283 bool res
= (this->cmp_mode
== RSG_CMP_ANY
) ? (match
!= 0) : (match
== this->triggers
);
286 object
.used_triggers
|= match
;
287 object
.reseed
[this->var_scope
] |= (this->num_groups
- 1) << this->lowest_randbit
;
291 uint32 mask
= (this->num_groups
- 1) << this->lowest_randbit
;
292 byte index
= (scope
->GetRandomBits() & mask
) >> this->lowest_randbit
;
294 return SpriteGroup::Resolve(this->groups
[index
], object
, false);
298 const SpriteGroup
*RealSpriteGroup::Resolve(ResolverObject
&object
) const
300 return object
.ResolveReal(this);
304 * Process registers and the construction stage into the sprite layout.
305 * The passed construction stage might get reset to zero, if it gets incorporated into the layout
306 * during the preprocessing.
307 * @param[in,out] stage Construction stage (0-3), or nullptr if not applicable.
308 * @return sprite layout to draw.
310 const DrawTileSprites
*TileLayoutSpriteGroup::ProcessRegisters(uint8
*stage
) const
312 if (!this->dts
.NeedsPreprocessing()) {
313 if (stage
!= nullptr && this->dts
.consistent_max_offset
> 0) *stage
= GetConstructionStageOffset(*stage
, this->dts
.consistent_max_offset
);
317 static DrawTileSprites result
;
318 uint8 actual_stage
= stage
!= nullptr ? *stage
: 0;
319 this->dts
.PrepareLayout(0, 0, 0, actual_stage
, false);
320 this->dts
.ProcessRegisters(0, 0, false);
321 result
.seq
= this->dts
.GetLayout(&result
.ground
);
323 /* Stage has been processed by PrepareLayout(), set it to zero. */
324 if (stage
!= nullptr) *stage
= 0;