# newgrf_spritegroup.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_spritegroup.cpp 26541 2014-04-29 18:18:52Z frosch $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "newgrf_spritegroup.h"
    00015 #include "core/pool_func.hpp"
    00016
    00017 SpriteGroupPool _spritegroup_pool("SpriteGroup");
    00018 INSTANTIATE_POOL_METHODS(SpriteGroup)
    00019
    00020 TemporaryStorageArray<int32, 0x110> _temp_store;
    00021
    00022
    00033 /* static */ const SpriteGroup *SpriteGroup::Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level)
    00034 {
    00035   if (group == NULL) return NULL;
    00036   if (top_level) {
    00037     _temp_store.ClearChanges();
    00038   }
    00039   return group->Resolve(object);
    00040 }
    00041
    00042 RealSpriteGroup::~RealSpriteGroup()
    00043 {
    00044   free(this->loaded);
    00045   free(this->loading);
    00046 }
    00047
    00048 DeterministicSpriteGroup::~DeterministicSpriteGroup()
    00049 {
    00050   free(this->adjusts);
    00051   free(this->ranges);
    00052 }
    00053
    00054 RandomizedSpriteGroup::~RandomizedSpriteGroup()
    00055 {
    00056   free(this->groups);
    00057 }
    00058
    00059 static inline uint32 GetVariable(const ResolverObject &object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
    00060 {
    00061   /* First handle variables common with Action7/9/D */
    00062   uint32 value;
    00063   if (GetGlobalVariable(variable, &value, object.grffile)) return value;
    00064
    00065   /* Non-common variable */
    00066   switch (variable) {
    00067     case 0x0C: return object.callback;
    00068     case 0x10: return object.callback_param1;
    00069     case 0x18: return object.callback_param2;
    00070     case 0x1C: return object.last_value;
    00071
    00072     case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
    00073
    00074     case 0x7D: return _temp_store.GetValue(parameter);
    00075
    00076     case 0x7F:
    00077       if (object.grffile == NULL) return 0;
    00078       return object.grffile->GetParam(parameter);
    00079
    00080     /* Not a common variable, so evaluate the feature specific variables */
    00081     default: return scope->GetVariable(variable, parameter, available);
    00082   }
    00083 }
    00084
    00085 ScopeResolver::ScopeResolver(ResolverObject &ro)
    00086     : ro(ro)
    00087 {
    00088 }
    00089
    00090 ScopeResolver::~ScopeResolver() {}
    00091
    00096 /* virtual */ uint32 ScopeResolver::GetRandomBits() const
    00097 {
    00098   return 0;
    00099 }
    00100
    00105 /* virtual */ uint32 ScopeResolver::GetTriggers() const
    00106 {
    00107   return 0;
    00108 }
    00109
    00114 /* virtual */ void ScopeResolver::SetTriggers(int triggers) const {}
    00115
    00123 /* virtual */ uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
    00124 {
    00125   DEBUG(grf, 1, "Unhandled scope variable 0x%X", variable);
    00126   *available = false;
    00127   return UINT_MAX;
    00128 }
    00129
    00135 /* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
    00136
    00144 ResolverObject::ResolverObject(const GRFFile *grffile, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
    00145     : default_scope(*this)
    00146 {
    00147   this->callback = callback;
    00148   this->callback_param1 = callback_param1;
    00149   this->callback_param2 = callback_param2;
    00150   this->ResetState();
    00151
    00152   this->grffile = grffile;
    00153   this->root_spritegroup = NULL;
    00154 }
    00155
    00156 ResolverObject::~ResolverObject() {}
    00157
    00163 /* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
    00164 {
    00165   return NULL;
    00166 }
    00167
    00174 /* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
    00175 {
    00176   return &this->default_scope;
    00177 }
    00178
    00185 static uint32 RotateRight(uint32 val, uint32 rot)
    00186 {
    00187   /* Do not rotate more than necessary */
    00188   rot %= 32;
    00189
    00190   return (val >> rot) | (val << (32 - rot));
    00191 }
    00192
    00193
    00194 /* Evaluate an adjustment for a variable of the given size.
    00195  * U is the unsigned type and S is the signed type to use. */
    00196 template <typename U, typename S>
    00197 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ScopeResolver *scope, U last_value, uint32 value)
    00198 {
    00199   value >>= adjust->shift_num;
    00200   value  &= adjust->and_mask;
    00201
    00202   if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
    00203
    00204   switch (adjust->type) {
    00205     case DSGA_TYPE_DIV:  value /= (S)adjust->divmod_val; break;
    00206     case DSGA_TYPE_MOD:  value %= (U)adjust->divmod_val; break;
    00207     case DSGA_TYPE_NONE: break;
    00208   }
    00209
    00210   switch (adjust->operation) {
    00211     case DSGA_OP_ADD:  return last_value + value;
    00212     case DSGA_OP_SUB:  return last_value - value;
    00213     case DSGA_OP_SMIN: return min((S)last_value, (S)value);
    00214     case DSGA_OP_SMAX: return max((S)last_value, (S)value);
    00215     case DSGA_OP_UMIN: return min((U)last_value, (U)value);
    00216     case DSGA_OP_UMAX: return max((U)last_value, (U)value);
    00217     case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
    00218     case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
    00219     case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
    00220     case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
    00221     case DSGA_OP_MUL:  return last_value * value;
    00222     case DSGA_OP_AND:  return last_value & value;
    00223     case DSGA_OP_OR:   return last_value | value;
    00224     case DSGA_OP_XOR:  return last_value ^ value;
    00225     case DSGA_OP_STO:  _temp_store.StoreValue((U)value, (S)last_value); return last_value;
    00226     case DSGA_OP_RST:  return value;
    00227     case DSGA_OP_STOP: scope->StorePSA((U)value, (S)last_value); return last_value;
    00228     case DSGA_OP_ROR:  return RotateRight(last_value, value);
    00229     case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
    00230     case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
    00231     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.
    00232     case DSGA_OP_SHR:  return (uint32)(U)last_value >> ((U)value & 0x1F);
    00233     case DSGA_OP_SAR:  return (int32)(S)last_value >> ((U)value & 0x1F);
    00234     default:           return value;
    00235   }
    00236 }
    00237
    00238
    00239 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) const
    00240 {
    00241   uint32 last_value = 0;
    00242   uint32 value = 0;
    00243   uint i;
    00244
    00245   ScopeResolver *scope = object.GetScope(this->var_scope);
    00246
    00247   for (i = 0; i < this->num_adjusts; i++) {
    00248     DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
    00249
    00250     /* Try to get the variable. We shall assume it is available, unless told otherwise. */
    00251     bool available = true;
    00252     if (adjust->variable == 0x7E) {
    00253       const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object, false);
    00254       if (subgroup == NULL) {
    00255         value = CALLBACK_FAILED;
    00256       } else {
    00257         value = subgroup->GetCallbackResult();
    00258       }
    00259
    00260       /* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
    00261     } else if (adjust->variable == 0x7B) {
    00262       value = GetVariable(object, scope, adjust->parameter, last_value, &available);
    00263     } else {
    00264       value = GetVariable(object, scope, adjust->variable, adjust->parameter, &available);
    00265     }
    00266
    00267     if (!available) {
    00268       /* Unsupported variable: skip further processing and return either
    00269        * the group from the first range or the default group. */
    00270       return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object, false);
    00271     }
    00272
    00273     switch (this->size) {
    00274       case DSG_SIZE_BYTE:  value = EvalAdjustT<uint8,  int8> (adjust, scope, last_value, value); break;
    00275       case DSG_SIZE_WORD:  value = EvalAdjustT<uint16, int16>(adjust, scope, last_value, value); break;
    00276       case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, scope, last_value, value); break;
    00277       default: NOT_REACHED();
    00278     }
    00279     last_value = value;
    00280   }
    00281
    00282   object.last_value = last_value;
    00283
    00284   if (this->num_ranges == 0) {
    00285     /* nvar == 0 is a special case -- we turn our value into a callback result */
    00286     if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
    00287     static CallbackResultSpriteGroup nvarzero(0, true);
    00288     nvarzero.result = value;
    00289     return &nvarzero;
    00290   }
    00291
    00292   for (i = 0; i < this->num_ranges; i++) {
    00293     if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
    00294       return SpriteGroup::Resolve(this->ranges[i].group, object, false);
    00295     }
    00296   }
    00297
    00298   return SpriteGroup::Resolve(this->default_group, object, false);
    00299 }
    00300
    00301
    00302 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject &object) const
    00303 {
    00304   ScopeResolver *scope = object.GetScope(this->var_scope, this->count);
    00305   if (object.trigger != 0) {
    00306     /* Handle triggers */
    00307     /* Magic code that may or may not do the right things... */
    00308     byte waiting_triggers = scope->GetTriggers();
    00309     byte match = this->triggers & (waiting_triggers | object.trigger);
    00310     bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
    00311
    00312     if (res) {
    00313       waiting_triggers &= ~match;
    00314       object.reseed[this->var_scope] |= (this->num_groups - 1) << this->lowest_randbit;
    00315     } else {
    00316       waiting_triggers |= object.trigger;
    00317     }
    00318
    00319     scope->SetTriggers(waiting_triggers);
    00320   }
    00321
    00322   uint32 mask  = (this->num_groups - 1) << this->lowest_randbit;
    00323   byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
    00324
    00325   return SpriteGroup::Resolve(this->groups[index], object, false);
    00326 }
    00327
    00328
    00329 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject &object) const
    00330 {
    00331   return object.ResolveReal(this);
    00332 }
    00333
    00341 const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8 *stage) const
    00342 {
    00343   if (!this->dts.NeedsPreprocessing()) {
    00344     if (stage != NULL && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
    00345     return &this->dts;
    00346   }
    00347
    00348   static DrawTileSprites result;
    00349   uint8 actual_stage = stage != NULL ? *stage : 0;
    00350   this->dts.PrepareLayout(0, 0, 0, actual_stage, false);
    00351   this->dts.ProcessRegisters(0, 0, false);
    00352   result.seq = this->dts.GetLayout(&result.ground);
    00353
    00354   /* Stage has been processed by PrepareLayout(), set it to zero. */
    00355   if (stage != NULL) *stage = 0;
    00356
    00357   return &result;
    00358 }