# newgrf_spritegroup.cpp

Go to the documentation of this file.

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