OpenTTD Source  1.11.2
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");
20 
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 RealSpriteGroup::~RealSpriteGroup()
57 {
58  free(this->loaded);
59  free(this->loading);
60 }
61 
62 DeterministicSpriteGroup::~DeterministicSpriteGroup()
63 {
64  free(this->adjusts);
65  free(this->ranges);
66 }
67 
68 RandomizedSpriteGroup::~RandomizedSpriteGroup()
69 {
70  free(this->groups);
71 }
72 
73 static inline uint32 GetVariable(const ResolverObject &object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
74 {
75  uint32 value;
76  switch (variable) {
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;
81 
82  case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
83 
84  case 0x7D: return _temp_store.GetValue(parameter);
85 
86  case 0x7F:
87  if (object.grffile == nullptr) return 0;
88  return object.grffile->GetParam(parameter);
89 
90  default:
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);
95  }
96 }
97 
102 /* virtual */ uint32 ScopeResolver::GetRandomBits() const
103 {
104  return 0;
105 }
106 
111 /* virtual */ uint32 ScopeResolver::GetTriggers() const
112 {
113  return 0;
114 }
115 
123 /* virtual */ uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
124 {
125  DEBUG(grf, 1, "Unhandled scope variable 0x%X", variable);
126  *available = false;
127  return UINT_MAX;
128 }
129 
135 /* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
136 
142 /* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
143 {
144  return nullptr;
145 }
146 
153 /* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
154 {
155  return &this->default_scope;
156 }
157 
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)
162 {
163  value >>= adjust->shift_num;
164  value &= adjust->and_mask;
165 
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;
170  }
171 
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;
197  }
198 }
199 
200 
201 static bool RangeHighComparator(const DeterministicSpriteGroupRange& range, uint32 value)
202 {
203  return range.high < value;
204 }
205 
207 {
208  uint32 last_value = 0;
209  uint32 value = 0;
210  uint i;
211 
212  ScopeResolver *scope = object.GetScope(this->var_scope);
213 
214  for (i = 0; i < this->num_adjusts; i++) {
215  DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
216 
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;
223  } else {
224  value = subgroup->GetCallbackResult();
225  }
226 
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);
230  } else {
231  value = GetVariable(object, scope, adjust->variable, adjust->parameter, &available);
232  }
233 
234  if (!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);
238  }
239 
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();
245  }
246  last_value = value;
247  }
248 
249  object.last_value = last_value;
250 
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;
256  return &nvarzero;
257  }
258 
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);
264  }
265  } else {
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);
269  }
270  }
271  }
272 
273  return SpriteGroup::Resolve(this->default_group, object, false);
274 }
275 
276 
278 {
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);
284 
285  if (res) {
286  object.used_triggers |= match;
287  object.reseed[this->var_scope] |= (this->num_groups - 1) << this->lowest_randbit;
288  }
289  }
290 
291  uint32 mask = (this->num_groups - 1) << this->lowest_randbit;
292  byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
293 
294  return SpriteGroup::Resolve(this->groups[index], object, false);
295 }
296 
297 
299 {
300  return object.ResolveReal(this);
301 }
302 
311 {
312  if (!this->dts.NeedsPreprocessing()) {
313  if (stage != nullptr && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
314  return &this->dts;
315  }
316 
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);
322 
323  /* Stage has been processed by PrepareLayout(), set it to zero. */
324  if (stage != nullptr) *stage = 0;
325 
326  return &result;
327 }
DSGA_OP_SCMP
@ DSGA_OP_SCMP
(signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
Definition: newgrf_spritegroup.h:142
VarSpriteGroupScope
VarSpriteGroupScope
Definition: newgrf_spritegroup.h:100
RealSpriteGroup::loaded
const SpriteGroup ** loaded
List of loaded groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:92
DSGA_OP_STOP
@ DSGA_OP_STOP
store a into persistent storage, indexed by b, return a
Definition: newgrf_spritegroup.h:140
DeterministicSpriteGroupRange
Definition: newgrf_spritegroup.h:163
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:125
RandomizedSpriteGroup::Resolve
const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.cpp:277
ScopeResolver::GetTriggers
virtual uint32 GetTriggers() const
Get the triggers.
Definition: newgrf_spritegroup.cpp:111
RandomizedSpriteGroup::cmp_mode
RandomizedSpriteGroupCompareMode cmp_mode
Check for these triggers:
Definition: newgrf_spritegroup.h:202
DeterministicSpriteGroupAdjust::parameter
byte parameter
Used for variables between 0x60 and 0x7F inclusive.
Definition: newgrf_spritegroup.h:154
Pool::PoolItem<&_spritegroup_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
DSGA_OP_STO
@ DSGA_OP_STO
store a into temporary storage, indexed by b. return a
Definition: newgrf_spritegroup.h:138
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:315
DSGA_OP_AND
@ DSGA_OP_AND
a & b
Definition: newgrf_spritegroup.h:135
DSGA_OP_SMAX
@ DSGA_OP_SMAX
(signed) max(a, b)
Definition: newgrf_spritegroup.h:127
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:135
TileLayoutSpriteGroup::ProcessRegisters
const DrawTileSprites * ProcessRegisters(uint8 *stage) const
Process registers and the construction stage into the sprite layout.
Definition: newgrf_spritegroup.cpp:310
DSGA_OP_XOR
@ DSGA_OP_XOR
a ^ b
Definition: newgrf_spritegroup.h:137
DSGA_OP_ADD
@ DSGA_OP_ADD
a + b
Definition: newgrf_spritegroup.h:124
DSGA_OP_ROR
@ 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:296
RandomizedSpriteGroup::var_scope
VarSpriteGroupScope var_scope
Take this object:
Definition: newgrf_spritegroup.h:200
ScopeResolver::GetRandomBits
virtual uint32 GetRandomBits() const
Get a few random bits.
Definition: newgrf_spritegroup.cpp:102
DSGA_OP_OR
@ DSGA_OP_OR
a | b
Definition: newgrf_spritegroup.h:136
RandomizedSpriteGroup::num_groups
byte num_groups
must be power of 2
Definition: newgrf_spritegroup.h:207
DrawTileSprites::ground
PalSpriteID ground
Palette and sprite for the ground.
Definition: sprite.h:59
RealSpriteGroup::loading
const SpriteGroup ** loading
List of loading groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:93
DSGA_OP_UMIN
@ DSGA_OP_UMIN
(unsigned) min(a, b)
Definition: newgrf_spritegroup.h:128
ResolverObject::default_scope
ScopeResolver default_scope
Default implementation of the grf scope.
Definition: newgrf_spritegroup.h:331
RealSpriteGroup::Resolve
const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.cpp:298
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
CallbackResultSpriteGroup
Definition: newgrf_spritegroup.h:218
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:153
RandomizedSpriteGroup::groups
const SpriteGroup ** groups
Take the group with appropriate index:
Definition: newgrf_spritegroup.h:209
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:132
stdafx.h
DSGA_OP_SHR
@ DSGA_OP_SHR
(unsigned) a >> b
Definition: newgrf_spritegroup.h:145
DeterministicSpriteGroup::Resolve
const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.cpp:206
newgrf_spritegroup.h
DeterministicSpriteGroupAdjust
Definition: newgrf_spritegroup.h:150
DSGA_OP_MUL
@ DSGA_OP_MUL
a * b
Definition: newgrf_spritegroup.h:134
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:404
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:133
GetGlobalVariable
bool GetGlobalVariable(byte param, uint32 *value, const GRFFile *grffile)
Reads a variable common to VarAction2 and Action7/9/D.
Definition: newgrf.cpp:6243
RealSpriteGroup
Definition: newgrf_spritegroup.h:79
DSGA_OP_SHL
@ DSGA_OP_SHL
a << b
Definition: newgrf_spritegroup.h:144
DSGA_OP_UMAX
@ DSGA_OP_UMAX
(unsigned) max(a, b)
Definition: newgrf_spritegroup.h:129
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:123
DSGA_OP_SAR
@ DSGA_OP_SAR
(signed) a >> b
Definition: newgrf_spritegroup.h:146
DSGA_OP_SMIN
@ DSGA_OP_SMIN
(signed) min(a, b)
Definition: newgrf_spritegroup.h:126
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
DrawTileSprites::seq
const DrawTileSeqStruct * seq
Array of child sprites. Terminated with a terminator entry.
Definition: sprite.h:60
RandomizedSpriteGroup::lowest_randbit
byte lowest_randbit
Look for this in the per-object randomized bitmask:
Definition: newgrf_spritegroup.h:206
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:143
DSGA_OP_SMOD
@ DSGA_OP_SMOD
(signed) a % b
Definition: newgrf_spritegroup.h:131
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:142
pool_func.hpp
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:456
newgrf_profiling.h
DSGA_OP_RST
@ DSGA_OP_RST
return b
Definition: newgrf_spritegroup.h:139
SpriteGroup
Definition: newgrf_spritegroup.h:57
DSGA_OP_SDIV
@ DSGA_OP_SDIV
(signed) a / b
Definition: newgrf_spritegroup.h:130
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
debug.h