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