# newgrf_spritegroup.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_spritegroup.cpp 19551 2010-04-03 19:48:01Z rubidium $ */
    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 "newgrf.h"
    00014 #include "newgrf_spritegroup.h"
    00015 #include "sprite.h"
    00016 #include "core/bitmath_func.hpp"
    00017 #include "core/pool_func.hpp"
    00018
    00019 SpriteGroupPool _spritegroup_pool("SpriteGroup");
    00020 INSTANTIATE_POOL_METHODS(SpriteGroup)
    00021
    00022 RealSpriteGroup::~RealSpriteGroup()
    00023 {
    00024   free((void*)this->loaded);
    00025   free((void*)this->loading);
    00026 }
    00027
    00028 DeterministicSpriteGroup::~DeterministicSpriteGroup()
    00029 {
    00030   free(this->adjusts);
    00031   free(this->ranges);
    00032 }
    00033
    00034 RandomizedSpriteGroup::~RandomizedSpriteGroup()
    00035 {
    00036   free((void*)this->groups);
    00037 }
    00038
    00039 TileLayoutSpriteGroup::~TileLayoutSpriteGroup()
    00040 {
    00041   free((void*)this->dts->seq);
    00042   free(this->dts);
    00043 }
    00044
    00045 TemporaryStorageArray<int32, 0x110> _temp_store;
    00046
    00047
    00048 static inline uint32 GetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
    00049 {
    00050   /* First handle variables common with Action7/9/D */
    00051   uint32 value;
    00052   if (GetGlobalVariable(variable, &value)) return value;
    00053
    00054   /* Non-common variable */
    00055   switch (variable) {
    00056     case 0x0C: return object->callback;
    00057     case 0x10: return object->callback_param1;
    00058     case 0x18: return object->callback_param2;
    00059     case 0x1C: return object->last_value;
    00060
    00061     case 0x5F: return (object->GetRandomBits(object) << 8) | object->GetTriggers(object);
    00062
    00063     case 0x7D: return _temp_store.Get(parameter);
    00064
    00065     case 0x7F:
    00066       if (object == NULL || object->grffile == NULL) return 0;
    00067       return object->grffile->GetParam(parameter);
    00068
    00069     /* Not a common variable, so evalute the feature specific variables */
    00070     default: return object->GetVariable(object, variable, parameter, available);
    00071   }
    00072 }
    00073
    00074
    00081 static uint32 RotateRight(uint32 val, uint32 rot)
    00082 {
    00083   /* Do not rotate more than necessary */
    00084   rot %= 32;
    00085
    00086   return (val >> rot) | (val << (32 - rot));
    00087 }
    00088
    00089
    00090 /* Evaluate an adjustment for a variable of the given size.
    00091  * U is the unsigned type and S is the signed type to use. */
    00092 template <typename U, typename S>
    00093 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ResolverObject *object, U last_value, uint32 value)
    00094 {
    00095   value >>= adjust->shift_num;
    00096   value  &= adjust->and_mask;
    00097
    00098   if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
    00099
    00100   switch (adjust->type) {
    00101     case DSGA_TYPE_DIV:  value /= (S)adjust->divmod_val; break;
    00102     case DSGA_TYPE_MOD:  value %= (U)adjust->divmod_val; break;
    00103     case DSGA_TYPE_NONE: break;
    00104   }
    00105
    00106   switch (adjust->operation) {
    00107     case DSGA_OP_ADD:  return last_value + value;
    00108     case DSGA_OP_SUB:  return last_value - value;
    00109     case DSGA_OP_SMIN: return min((S)last_value, (S)value);
    00110     case DSGA_OP_SMAX: return max((S)last_value, (S)value);
    00111     case DSGA_OP_UMIN: return min((U)last_value, (U)value);
    00112     case DSGA_OP_UMAX: return max((U)last_value, (U)value);
    00113     case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
    00114     case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
    00115     case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
    00116     case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
    00117     case DSGA_OP_MUL:  return last_value * value;
    00118     case DSGA_OP_AND:  return last_value & value;
    00119     case DSGA_OP_OR:   return last_value | value;
    00120     case DSGA_OP_XOR:  return last_value ^ value;
    00121     case DSGA_OP_STO:  _temp_store.Store((U)value, (S)last_value); return last_value;
    00122     case DSGA_OP_RST:  return value;
    00123     case DSGA_OP_STOP: if (object->psa != NULL) object->psa->Store((U)value, (S)last_value); return last_value;
    00124     case DSGA_OP_ROR:  return RotateRight(last_value, value);
    00125     case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
    00126     case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
    00127     default:           return value;
    00128   }
    00129 }
    00130
    00131
    00132 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject *object) const
    00133 {
    00134   uint32 last_value = 0;
    00135   uint32 value = 0;
    00136   uint i;
    00137
    00138   object->scope = this->var_scope;
    00139
    00140   for (i = 0; i < this->num_adjusts; i++) {
    00141     DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
    00142
    00143     /* Try to get the variable. We shall assume it is available, unless told otherwise. */
    00144     bool available = true;
    00145     if (adjust->variable == 0x7E) {
    00146       const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object);
    00147       if (subgroup == NULL) {
    00148         value = CALLBACK_FAILED;
    00149       } else {
    00150         value = subgroup->GetCallbackResult();
    00151       }
    00152
    00153       /* Reset values to current scope.
    00154        * Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
    00155       object->scope = this->var_scope;
    00156     } else {
    00157       value = GetVariable(object, adjust->variable, adjust->parameter, &available);
    00158     }
    00159
    00160     if (!available) {
    00161       /* Unsupported property: skip further processing and return either
    00162        * the group from the first range or the default group. */
    00163       return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object);
    00164     }
    00165
    00166     switch (this->size) {
    00167       case DSG_SIZE_BYTE:  value = EvalAdjustT<uint8,  int8> (adjust, object, last_value, value); break;
    00168       case DSG_SIZE_WORD:  value = EvalAdjustT<uint16, int16>(adjust, object, last_value, value); break;
    00169       case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, object, last_value, value); break;
    00170       default: NOT_REACHED();
    00171     }
    00172     last_value = value;
    00173   }
    00174
    00175   object->last_value = last_value;
    00176
    00177   if (this->num_ranges == 0) {
    00178     /* nvar == 0 is a special case -- we turn our value into a callback result */
    00179     if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
    00180     static CallbackResultSpriteGroup nvarzero(0);
    00181     nvarzero.result = value;
    00182     return &nvarzero;
    00183   }
    00184
    00185   for (i = 0; i < this->num_ranges; i++) {
    00186     if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
    00187       return SpriteGroup::Resolve(this->ranges[i].group, object);
    00188     }
    00189   }
    00190
    00191   return SpriteGroup::Resolve(this->default_group, object);
    00192 }
    00193
    00194
    00195 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const
    00196 {
    00197   uint32 mask;
    00198   byte index;
    00199
    00200   object->scope = this->var_scope;
    00201   object->count = this->count;
    00202
    00203   if (object->trigger != 0) {
    00204     /* Handle triggers */
    00205     /* Magic code that may or may not do the right things... */
    00206     byte waiting_triggers = object->GetTriggers(object);
    00207     byte match = this->triggers & (waiting_triggers | object->trigger);
    00208     bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
    00209
    00210     if (res) {
    00211       waiting_triggers &= ~match;
    00212       object->reseed |= (this->num_groups - 1) << this->lowest_randbit;
    00213     } else {
    00214       waiting_triggers |= object->trigger;
    00215     }
    00216
    00217     object->SetTriggers(object, waiting_triggers);
    00218   }
    00219
    00220   mask  = (this->num_groups - 1) << this->lowest_randbit;
    00221   index = (object->GetRandomBits(object) & mask) >> this->lowest_randbit;
    00222
    00223   return SpriteGroup::Resolve(this->groups[index], object);
    00224 }
    00225
    00226
    00227 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject *object) const
    00228 {
    00229   return object->ResolveReal(object, this);
    00230 }