newgrf_spritegroup.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
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
00050 uint32 value;
00051 if (GetGlobalVariable(variable, &value)) return value;
00052
00053
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
00069 default: return object->GetVariable(object, variable, parameter, available);
00070 }
00071 }
00072
00073
00080 static uint32 RotateRight(uint32 val, uint32 rot)
00081 {
00082
00083 rot %= 32;
00084
00085 return (val >> rot) | (val << (32 - rot));
00086 }
00087
00088
00089
00090
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
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
00153
00154 object->scope = this->var_scope;
00155 } else {
00156 value = GetVariable(object, adjust->variable, adjust->parameter, &available);
00157 }
00158
00159 if (!available) {
00160
00161
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
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
00204
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 }