newgrf_spritegroup.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_spritegroup.cpp 15711 2009-03-14 18:16:29Z rubidium $ */
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       ResolverObject subobject = *object;
00177       subobject.procedure_call = true;
00178       const SpriteGroup *subgroup = Resolve(adjust->subroutine, &subobject);
00179       if (subgroup == NULL || subgroup->type != SGT_CALLBACK) {
00180         value = CALLBACK_FAILED;
00181       } else {
00182         value = subgroup->g.callback.result;
00183       }
00184     } else {
00185       value = GetVariable(object, adjust->variable, adjust->parameter, &available);
00186     }
00187 
00188     if (!available) {
00189       /* Unsupported property: skip further processing and return either
00190        * the group from the first range or the default group. */
00191       return Resolve(group->g.determ.num_ranges > 0 ? group->g.determ.ranges[0].group : group->g.determ.default_group, object);
00192     }
00193 
00194     switch (group->g.determ.size) {
00195       case DSG_SIZE_BYTE:  value = EvalAdjustT<uint8,  int8> (adjust, object, last_value, value); break;
00196       case DSG_SIZE_WORD:  value = EvalAdjustT<uint16, int16>(adjust, object, last_value, value); break;
00197       case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, object, last_value, value); break;
00198       default: NOT_REACHED(); break;
00199     }
00200     last_value = value;
00201   }
00202 
00203   object->last_value = last_value;
00204 
00205   if (group->g.determ.num_ranges == 0) {
00206     /* nvar == 0 is a special case -- we turn our value into a callback result */
00207     if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
00208     nvarzero.type = SGT_CALLBACK;
00209     nvarzero.g.callback.result = value;
00210     return &nvarzero;
00211   }
00212 
00213   for (i = 0; i < group->g.determ.num_ranges; i++) {
00214     if (group->g.determ.ranges[i].low <= value && value <= group->g.determ.ranges[i].high) {
00215       return Resolve(group->g.determ.ranges[i].group, object);
00216     }
00217   }
00218 
00219   return Resolve(group->g.determ.default_group, object);
00220 }
00221 
00222 
00223 static inline const SpriteGroup *ResolveRandom(const SpriteGroup *group, ResolverObject *object)
00224 {
00225   uint32 mask;
00226   byte index;
00227 
00228   object->scope = group->g.random.var_scope;
00229   object->count = group->g.random.count;
00230 
00231   if (object->trigger != 0) {
00232     /* Handle triggers */
00233     /* Magic code that may or may not do the right things... */
00234     byte waiting_triggers = object->GetTriggers(object);
00235     byte match = group->g.random.triggers & (waiting_triggers | object->trigger);
00236     bool res;
00237 
00238     res = (group->g.random.cmp_mode == RSG_CMP_ANY) ?
00239       (match != 0) : (match == group->g.random.triggers);
00240 
00241     if (res) {
00242       waiting_triggers &= ~match;
00243       object->reseed |= (group->g.random.num_groups - 1) << group->g.random.lowest_randbit;
00244     } else {
00245       waiting_triggers |= object->trigger;
00246     }
00247 
00248     object->SetTriggers(object, waiting_triggers);
00249   }
00250 
00251   mask  = (group->g.random.num_groups - 1) << group->g.random.lowest_randbit;
00252   index = (object->GetRandomBits(object) & mask) >> group->g.random.lowest_randbit;
00253 
00254   return Resolve(group->g.random.groups[index], object);
00255 }
00256 
00257 
00258 /* ResolverObject (re)entry point */
00259 const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject *object)
00260 {
00261   /* We're called even if there is no group, so quietly return nothing */
00262   if (group == NULL) return NULL;
00263 
00264   switch (group->type) {
00265     case SGT_REAL:          return object->ResolveReal(object, group);
00266     case SGT_DETERMINISTIC: return ResolveVariable(group, object);
00267     case SGT_RANDOMIZED:    return ResolveRandom(group, object);
00268     default:                return group;
00269   }
00270 }

Generated on Thu Oct 1 11:03:15 2009 for OpenTTD by  doxygen 1.5.6