# 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 "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 RealSpriteGroup::~RealSpriteGroup()
    00021 {
    00022   free(this->loaded);
    00023   free(this->loading);
    00024 }
    00025
    00026 DeterministicSpriteGroup::~DeterministicSpriteGroup()
    00027 {
    00028   free(this->adjusts);
    00029   free(this->ranges);
    00030 }
    00031
    00032 RandomizedSpriteGroup::~RandomizedSpriteGroup()
    00033 {
    00034   free(this->groups);
    00035 }
    00036
    00037 TemporaryStorageArray<int32, 0x110> _temp_store;
    00038
    00039
    00040 static inline uint32 GetVariable(const ResolverObject *object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
    00041 {
    00042   /* First handle variables common with Action7/9/D */
    00043   uint32 value;
    00044   if (GetGlobalVariable(variable, &value, object->grffile)) return value;
    00045
    00046   /* Non-common variable */
    00047   switch (variable) {
    00048     case 0x0C: return object->callback;
    00049     case 0x10: return object->callback_param1;
    00050     case 0x18: return object->callback_param2;
    00051     case 0x1C: return object->last_value;
    00052
    00053     case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
    00054
    00055     case 0x7D: return _temp_store.GetValue(parameter);
    00056
    00057     case 0x7F:
    00058       if (object == NULL || object->grffile == NULL) return 0;
    00059       return object->grffile->GetParam(parameter);
    00060
    00061     /* Not a common variable, so evaluate the feature specific variables */
    00062     default: return scope->GetVariable(variable, parameter, available);
    00063   }
    00064 }
    00065
    00066 ScopeResolver::ScopeResolver(ResolverObject *ro)
    00067 {
    00068   this->ro = ro;
    00069 }
    00070
    00071 ScopeResolver::~ScopeResolver() {}
    00072
    00077 /* virtual */ uint32 ScopeResolver::GetRandomBits() const
    00078 {
    00079   return 0;
    00080 }
    00081
    00086 /* virtual */ uint32 ScopeResolver::GetTriggers() const
    00087 {
    00088   return 0;
    00089 }
    00090
    00095 /* virtual */ void ScopeResolver::SetTriggers(int triggers) const {}
    00096
    00104 /* virtual */ uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
    00105 {
    00106   DEBUG(grf, 1, "Unhandled scope variable 0x%X", variable);
    00107   *available = false;
    00108   return UINT_MAX;
    00109 }
    00110
    00116 /* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
    00117
    00125 ResolverObject::ResolverObject(const GRFFile *grffile, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
    00126     : default_scope(this)
    00127 {
    00128   this->callback = callback;
    00129   this->callback_param1 = callback_param1;
    00130   this->callback_param2 = callback_param2;
    00131   this->ResetState();
    00132
    00133   this->grffile = grffile;
    00134 }
    00135
    00136 ResolverObject::~ResolverObject() {}
    00137
    00143 /* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
    00144 {
    00145   return NULL;
    00146 }
    00147
    00154 /* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
    00155 {
    00156   return &this->default_scope;
    00157 }
    00158
    00165 static uint32 RotateRight(uint32 val, uint32 rot)
    00166 {
    00167   /* Do not rotate more than necessary */
    00168   rot %= 32;
    00169
    00170   return (val >> rot) | (val << (32 - rot));
    00171 }
    00172
    00173
    00174 /* Evaluate an adjustment for a variable of the given size.
    00175  * U is the unsigned type and S is the signed type to use. */
    00176 template <typename U, typename S>
    00177 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ScopeResolver *scope, U last_value, uint32 value)
    00178 {
    00179   value >>= adjust->shift_num;
    00180   value  &= adjust->and_mask;
    00181
    00182   if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
    00183
    00184   switch (adjust->type) {
    00185     case DSGA_TYPE_DIV:  value /= (S)adjust->divmod_val; break;
    00186     case DSGA_TYPE_MOD:  value %= (U)adjust->divmod_val; break;
    00187     case DSGA_TYPE_NONE: break;
    00188   }
    00189
    00190   switch (adjust->operation) {
    00191     case DSGA_OP_ADD:  return last_value + value;
    00192     case DSGA_OP_SUB:  return last_value - value;
    00193     case DSGA_OP_SMIN: return min((S)last_value, (S)value);
    00194     case DSGA_OP_SMAX: return max((S)last_value, (S)value);
    00195     case DSGA_OP_UMIN: return min((U)last_value, (U)value);
    00196     case DSGA_OP_UMAX: return max((U)last_value, (U)value);
    00197     case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
    00198     case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
    00199     case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
    00200     case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
    00201     case DSGA_OP_MUL:  return last_value * value;
    00202     case DSGA_OP_AND:  return last_value & value;
    00203     case DSGA_OP_OR:   return last_value | value;
    00204     case DSGA_OP_XOR:  return last_value ^ value;
    00205     case DSGA_OP_STO:  _temp_store.StoreValue((U)value, (S)last_value); return last_value;
    00206     case DSGA_OP_RST:  return value;
    00207     case DSGA_OP_STOP: scope->StorePSA((U)value, (S)last_value); return last_value;
    00208     case DSGA_OP_ROR:  return RotateRight(last_value, value);
    00209     case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
    00210     case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
    00211     case DSGA_OP_SHL:  return (U)last_value << ((U)value & 0x1F); // mask 'value' to 5 bits, which should behave the same on all architectures.
    00212     case DSGA_OP_SHR:  return (U)last_value >> ((U)value & 0x1F);
    00213     case DSGA_OP_SAR:  return (S)last_value >> ((U)value & 0x1F);
    00214     default:           return value;
    00215   }
    00216 }
    00217
    00218
    00219 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject *object) const
    00220 {
    00221   uint32 last_value = 0;
    00222   uint32 value = 0;
    00223   uint i;
    00224
    00225   ScopeResolver *scope = object->GetScope(this->var_scope);
    00226
    00227   for (i = 0; i < this->num_adjusts; i++) {
    00228     DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
    00229
    00230     /* Try to get the variable. We shall assume it is available, unless told otherwise. */
    00231     bool available = true;
    00232     if (adjust->variable == 0x7E) {
    00233       const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object);
    00234       if (subgroup == NULL) {
    00235         value = CALLBACK_FAILED;
    00236       } else {
    00237         value = subgroup->GetCallbackResult();
    00238       }
    00239
    00240       /* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
    00241     } else if (adjust->variable == 0x7B) {
    00242       value = GetVariable(object, scope, adjust->parameter, last_value, &available);
    00243     } else {
    00244       value = GetVariable(object, scope, adjust->variable, adjust->parameter, &available);
    00245     }
    00246
    00247     if (!available) {
    00248       /* Unsupported variable: skip further processing and return either
    00249        * the group from the first range or the default group. */
    00250       return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object);
    00251     }
    00252
    00253     switch (this->size) {
    00254       case DSG_SIZE_BYTE:  value = EvalAdjustT<uint8,  int8> (adjust, scope, last_value, value); break;
    00255       case DSG_SIZE_WORD:  value = EvalAdjustT<uint16, int16>(adjust, scope, last_value, value); break;
    00256       case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, scope, last_value, value); break;
    00257       default: NOT_REACHED();
    00258     }
    00259     last_value = value;
    00260   }
    00261
    00262   object->last_value = last_value;
    00263
    00264   if (this->num_ranges == 0) {
    00265     /* nvar == 0 is a special case -- we turn our value into a callback result */
    00266     if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
    00267     static CallbackResultSpriteGroup nvarzero(0, true);
    00268     nvarzero.result = value;
    00269     return &nvarzero;
    00270   }
    00271
    00272   for (i = 0; i < this->num_ranges; i++) {
    00273     if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
    00274       return SpriteGroup::Resolve(this->ranges[i].group, object);
    00275     }
    00276   }
    00277
    00278   return SpriteGroup::Resolve(this->default_group, object);
    00279 }
    00280
    00281
    00282 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const
    00283 {
    00284   ScopeResolver *scope = object->GetScope(this->var_scope, this->count);
    00285   if (object->trigger != 0) {
    00286     /* Handle triggers */
    00287     /* Magic code that may or may not do the right things... */
    00288     byte waiting_triggers = scope->GetTriggers();
    00289     byte match = this->triggers & (waiting_triggers | object->trigger);
    00290     bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
    00291
    00292     if (res) {
    00293       waiting_triggers &= ~match;
    00294       object->reseed[this->var_scope] |= (this->num_groups - 1) << this->lowest_randbit;
    00295     } else {
    00296       waiting_triggers |= object->trigger;
    00297     }
    00298
    00299     scope->SetTriggers(waiting_triggers);
    00300   }
    00301
    00302   uint32 mask  = (this->num_groups - 1) << this->lowest_randbit;
    00303   byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
    00304
    00305   return SpriteGroup::Resolve(this->groups[index], object);
    00306 }
    00307
    00308
    00309 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject *object) const
    00310 {
    00311   return object->ResolveReal(this);
    00312 }
    00313
    00321 const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8 *stage) const
    00322 {
    00323   if (!this->dts.NeedsPreprocessing()) {
    00324     if (stage != NULL && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
    00325     return &this->dts;
    00326   }
    00327
    00328   static DrawTileSprites result;
    00329   uint8 actual_stage = stage != NULL ? *stage : 0;
    00330   this->dts.PrepareLayout(0, 0, 0, actual_stage, false);
    00331   this->dts.ProcessRegisters(0, 0, false);
    00332   result.seq = this->dts.GetLayout(&result.ground);
    00333
    00334   /* Stage has been processed by PrepareLayout(), set it to zero. */
    00335   if (stage != NULL) *stage = 0;
    00336
    00337   return &result;
    00338 }