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