# newgrf_spritegroup.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_spritegroup.cpp 17996 2009-11-07 16:37:48Z frosch $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "oldpool.h"
    00007 #include "newgrf.h"
    00008 #include "newgrf_spritegroup.h"
    00009 #include "sprite.h"
    00010
    00011 static void SpriteGroupPoolCleanBlock(uint start_item, uint end_item);
    00012
    00013 static uint _spritegroup_count = 0;
    00014 STATIC_OLD_POOL(SpriteGroup, SpriteGroup, 9, 250, NULL, SpriteGroupPoolCleanBlock)
    00015
    00016 static void DestroySpriteGroup(SpriteGroup *group)
    00017 {
    00018   /* Free dynamically allocated memory */
    00019   /* XXX Cast away the consts due to MSVC being buggy... */
    00020   switch (group->type) {
    00021     case SGT_REAL:
    00022       free((SpriteGroup**)group->g.real.loaded);
    00023       free((SpriteGroup**)group->g.real.loading);
    00024       break;
    00025
    00026     case SGT_DETERMINISTIC:
    00027       free(group->g.determ.adjusts);
    00028       free(group->g.determ.ranges);
    00029       break;
    00030
    00031     case SGT_RANDOMIZED:
    00032       free((SpriteGroup**)group->g.random.groups);
    00033       break;
    00034
    00035     case SGT_TILELAYOUT:
    00036       free((void*)group->g.layout.dts->seq);
    00037       free(group->g.layout.dts);
    00038       break;
    00039
    00040     default:
    00041       break;
    00042   }
    00043 }
    00044
    00045 static void SpriteGroupPoolCleanBlock(uint start_item, uint end_item)
    00046 {
    00047   uint i;
    00048
    00049   for (i = start_item; i <= end_item; i++) {
    00050     DestroySpriteGroup(GetSpriteGroup(i));
    00051   }
    00052 }
    00053
    00054
    00055 /* Allocate a new SpriteGroup */
    00056 SpriteGroup *AllocateSpriteGroup()
    00057 {
    00058   /* This is totally different to the other pool allocators, as we never remove an item from the pool. */
    00059   if (_spritegroup_count == GetSpriteGroupPoolSize()) {
    00060     if (!_SpriteGroup_pool.AddBlockToPool()) return NULL;
    00061   }
    00062
    00063   return GetSpriteGroup(_spritegroup_count++);
    00064 }
    00065
    00066
    00067 void InitializeSpriteGroupPool()
    00068 {
    00069   _SpriteGroup_pool.CleanPool();
    00070
    00071   _spritegroup_count = 0;
    00072 }
    00073
    00074 TemporaryStorageArray<uint32, 0x110> _temp_store;
    00075
    00076
    00077 static inline uint32 GetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
    00078 {
    00079   /* First handle variables common with Action7/9/D */
    00080   uint32 value;
    00081   if (GetGlobalVariable(variable, &value)) return value;
    00082
    00083   /* Non-common variable */
    00084   switch (variable) {
    00085     case 0x0C: return object->callback;
    00086     case 0x10: return object->callback_param1;
    00087     case 0x18: return object->callback_param2;
    00088     case 0x1C: return object->last_value;
    00089
    00090     case 0x5F: return (object->GetRandomBits(object) << 8) | object->GetTriggers(object);
    00091
    00092     case 0x7D: return _temp_store.Get(parameter);
    00093
    00094     case 0x7F:
    00095       if (object == NULL || object->grffile == NULL || parameter >= object->grffile->param_end) return 0;
    00096       return object->grffile->param[parameter];
    00097
    00098     /* Not a common variable, so evalute the feature specific variables */
    00099     default: return object->GetVariable(object, variable, parameter, available);
    00100   }
    00101 }
    00102
    00103
    00110 static uint32 RotateRight(uint32 val, uint32 rot)
    00111 {
    00112   /* Do not rotate more than necessary */
    00113   rot %= 32;
    00114
    00115   return (val >> rot) | (val << (32 - rot));
    00116 }
    00117
    00118
    00119 /* Evaluate an adjustment for a variable of the given size.
    00120  * U is the unsigned type and S is the signed type to use. */
    00121 template <typename U, typename S>
    00122 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ResolverObject *object, U last_value, uint32 value)
    00123 {
    00124   value >>= adjust->shift_num;
    00125   value  &= adjust->and_mask;
    00126
    00127   if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
    00128
    00129   switch (adjust->type) {
    00130     case DSGA_TYPE_DIV:  value /= (S)adjust->divmod_val; break;
    00131     case DSGA_TYPE_MOD:  value %= (U)adjust->divmod_val; break;
    00132     case DSGA_TYPE_NONE: break;
    00133   }
    00134
    00135   switch (adjust->operation) {
    00136     case DSGA_OP_ADD:  return last_value + value;
    00137     case DSGA_OP_SUB:  return last_value - value;
    00138     case DSGA_OP_SMIN: return min((S)last_value, (S)value);
    00139     case DSGA_OP_SMAX: return max((S)last_value, (S)value);
    00140     case DSGA_OP_UMIN: return min((U)last_value, (U)value);
    00141     case DSGA_OP_UMAX: return max((U)last_value, (U)value);
    00142     case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
    00143     case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
    00144     case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
    00145     case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
    00146     case DSGA_OP_MUL:  return last_value * value;
    00147     case DSGA_OP_AND:  return last_value & value;
    00148     case DSGA_OP_OR:   return last_value | value;
    00149     case DSGA_OP_XOR:  return last_value ^ value;
    00150     case DSGA_OP_STO:  _temp_store.Store(value, last_value); return last_value;
    00151     case DSGA_OP_RST:  return value;
    00152     case DSGA_OP_STOP: if (object->psa != NULL) object->psa->Store(value, last_value); return last_value;
    00153     case DSGA_OP_ROR:  return RotateRight(last_value, value);
    00154     case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
    00155     case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
    00156     default:           return value;
    00157   }
    00158 }
    00159
    00160
    00161 static inline const SpriteGroup *ResolveVariable(const SpriteGroup *group, ResolverObject *object)
    00162 {
    00163   static SpriteGroup nvarzero;
    00164   uint32 last_value = 0;
    00165   uint32 value = 0;
    00166   uint i;
    00167
    00168   object->scope = group->g.determ.var_scope;
    00169
    00170   for (i = 0; i < group->g.determ.num_adjusts; i++) {
    00171     DeterministicSpriteGroupAdjust *adjust = &group->g.determ.adjusts[i];
    00172
    00173     /* Try to get the variable. We shall assume it is available, unless told otherwise. */
    00174     bool available = true;
    00175     if (adjust->variable == 0x7E) {
    00176       const SpriteGroup *subgroup = Resolve(adjust->subroutine, object);
    00177       if (subgroup == NULL || subgroup->type != SGT_CALLBACK) {
    00178         value = CALLBACK_FAILED;
    00179       } else {
    00180         value = subgroup->g.callback.result;
    00181       }
    00182
    00183       /* Reset values to current scope.
    00184        * Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
    00185       object->scope = group->g.determ.var_scope;
    00186     } else {
    00187       value = GetVariable(object, adjust->variable, adjust->parameter, &available);
    00188     }
    00189
    00190     if (!available) {
    00191       /* Unsupported property: skip further processing and return either
    00192        * the group from the first range or the default group. */
    00193       return Resolve(group->g.determ.num_ranges > 0 ? group->g.determ.ranges[0].group : group->g.determ.default_group, object);
    00194     }
    00195
    00196     switch (group->g.determ.size) {
    00197       case DSG_SIZE_BYTE:  value = EvalAdjustT<uint8,  int8> (adjust, object, last_value, value); break;
    00198       case DSG_SIZE_WORD:  value = EvalAdjustT<uint16, int16>(adjust, object, last_value, value); break;
    00199       case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, object, last_value, value); break;
    00200       default: NOT_REACHED(); break;
    00201     }
    00202     last_value = value;
    00203   }
    00204
    00205   object->last_value = last_value;
    00206
    00207   if (group->g.determ.num_ranges == 0) {
    00208     /* nvar == 0 is a special case -- we turn our value into a callback result */
    00209     if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
    00210     nvarzero.type = SGT_CALLBACK;
    00211     nvarzero.g.callback.result = value;
    00212     return &nvarzero;
    00213   }
    00214
    00215   for (i = 0; i < group->g.determ.num_ranges; i++) {
    00216     if (group->g.determ.ranges[i].low <= value && value <= group->g.determ.ranges[i].high) {
    00217       return Resolve(group->g.determ.ranges[i].group, object);
    00218     }
    00219   }
    00220
    00221   return Resolve(group->g.determ.default_group, object);
    00222 }
    00223
    00224
    00225 static inline const SpriteGroup *ResolveRandom(const SpriteGroup *group, ResolverObject *object)
    00226 {
    00227   uint32 mask;
    00228   byte index;
    00229
    00230   object->scope = group->g.random.var_scope;
    00231   object->count = group->g.random.count;
    00232
    00233   if (object->trigger != 0) {
    00234     /* Handle triggers */
    00235     /* Magic code that may or may not do the right things... */
    00236     byte waiting_triggers = object->GetTriggers(object);
    00237     byte match = group->g.random.triggers & (waiting_triggers | object->trigger);
    00238     bool res;
    00239
    00240     res = (group->g.random.cmp_mode == RSG_CMP_ANY) ?
    00241       (match != 0) : (match == group->g.random.triggers);
    00242
    00243     if (res) {
    00244       waiting_triggers &= ~match;
    00245       object->reseed |= (group->g.random.num_groups - 1) << group->g.random.lowest_randbit;
    00246     } else {
    00247       waiting_triggers |= object->trigger;
    00248     }
    00249
    00250     object->SetTriggers(object, waiting_triggers);
    00251   }
    00252
    00253   mask  = (group->g.random.num_groups - 1) << group->g.random.lowest_randbit;
    00254   index = (object->GetRandomBits(object) & mask) >> group->g.random.lowest_randbit;
    00255
    00256   return Resolve(group->g.random.groups[index], object);
    00257 }
    00258
    00259
    00260 /* ResolverObject (re)entry point */
    00261 const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject *object)
    00262 {
    00263   /* We're called even if there is no group, so quietly return nothing */
    00264   if (group == NULL) return NULL;
    00265
    00266   switch (group->type) {
    00267     case SGT_REAL:          return object->ResolveReal(object, group);
    00268     case SGT_DETERMINISTIC: return ResolveVariable(group, object);
    00269     case SGT_RANDOMIZED:    return ResolveRandom(group, object);
    00270     default:                return group;
    00271   }
    00272 }