00001
00002
00003
00004
00005
00006
00007
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 TemporaryStorageArray<int32, 0x110> _temp_store;
00021
00022
00033 const SpriteGroup *SpriteGroup::Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level)
00034 {
00035 if (group == NULL) return NULL;
00036 if (top_level) {
00037 _temp_store.ClearChanges();
00038 }
00039 return group->Resolve(object);
00040 }
00041
00042 RealSpriteGroup::~RealSpriteGroup()
00043 {
00044 free(this->loaded);
00045 free(this->loading);
00046 }
00047
00048 DeterministicSpriteGroup::~DeterministicSpriteGroup()
00049 {
00050 free(this->adjusts);
00051 free(this->ranges);
00052 }
00053
00054 RandomizedSpriteGroup::~RandomizedSpriteGroup()
00055 {
00056 free(this->groups);
00057 }
00058
00059 static inline uint32 GetVariable(const ResolverObject &object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
00060 {
00061
00062 uint32 value;
00063 if (GetGlobalVariable(variable, &value, object.grffile)) return value;
00064
00065
00066 switch (variable) {
00067 case 0x0C: return object.callback;
00068 case 0x10: return object.callback_param1;
00069 case 0x18: return object.callback_param2;
00070 case 0x1C: return object.last_value;
00071
00072 case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
00073
00074 case 0x7D: return _temp_store.GetValue(parameter);
00075
00076 case 0x7F:
00077 if (object.grffile == NULL) return 0;
00078 return object.grffile->GetParam(parameter);
00079
00080
00081 default: return scope->GetVariable(variable, parameter, available);
00082 }
00083 }
00084
00085 ScopeResolver::ScopeResolver(ResolverObject &ro)
00086 : ro(ro)
00087 {
00088 }
00089
00090 ScopeResolver::~ScopeResolver() {}
00091
00096 uint32 ScopeResolver::GetRandomBits() const
00097 {
00098 return 0;
00099 }
00100
00105 uint32 ScopeResolver::GetTriggers() const
00106 {
00107 return 0;
00108 }
00109
00114 void ScopeResolver::SetTriggers(int triggers) const {}
00115
00123 uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
00124 {
00125 DEBUG(grf, 1, "Unhandled scope variable 0x%X", variable);
00126 *available = false;
00127 return UINT_MAX;
00128 }
00129
00135 void ScopeResolver::StorePSA(uint reg, int32 value) {}
00136
00144 ResolverObject::ResolverObject(const GRFFile *grffile, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
00145 : default_scope(*this)
00146 {
00147 this->callback = callback;
00148 this->callback_param1 = callback_param1;
00149 this->callback_param2 = callback_param2;
00150 this->ResetState();
00151
00152 this->grffile = grffile;
00153 this->root_spritegroup = NULL;
00154 }
00155
00156 ResolverObject::~ResolverObject() {}
00157
00163 const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
00164 {
00165 return NULL;
00166 }
00167
00174 ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
00175 {
00176 return &this->default_scope;
00177 }
00178
00185 static uint32 RotateRight(uint32 val, uint32 rot)
00186 {
00187
00188 rot %= 32;
00189
00190 return (val >> rot) | (val << (32 - rot));
00191 }
00192
00193
00194
00195
00196 template <typename U, typename S>
00197 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ScopeResolver *scope, U last_value, uint32 value)
00198 {
00199 value >>= adjust->shift_num;
00200 value &= adjust->and_mask;
00201
00202 if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
00203
00204 switch (adjust->type) {
00205 case DSGA_TYPE_DIV: value /= (S)adjust->divmod_val; break;
00206 case DSGA_TYPE_MOD: value %= (U)adjust->divmod_val; break;
00207 case DSGA_TYPE_NONE: break;
00208 }
00209
00210 switch (adjust->operation) {
00211 case DSGA_OP_ADD: return last_value + value;
00212 case DSGA_OP_SUB: return last_value - value;
00213 case DSGA_OP_SMIN: return min((S)last_value, (S)value);
00214 case DSGA_OP_SMAX: return max((S)last_value, (S)value);
00215 case DSGA_OP_UMIN: return min((U)last_value, (U)value);
00216 case DSGA_OP_UMAX: return max((U)last_value, (U)value);
00217 case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
00218 case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
00219 case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
00220 case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
00221 case DSGA_OP_MUL: return last_value * value;
00222 case DSGA_OP_AND: return last_value & value;
00223 case DSGA_OP_OR: return last_value | value;
00224 case DSGA_OP_XOR: return last_value ^ value;
00225 case DSGA_OP_STO: _temp_store.StoreValue((U)value, (S)last_value); return last_value;
00226 case DSGA_OP_RST: return value;
00227 case DSGA_OP_STOP: scope->StorePSA((U)value, (S)last_value); return last_value;
00228 case DSGA_OP_ROR: return RotateRight(last_value, value);
00229 case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
00230 case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
00231 case DSGA_OP_SHL: return (U)last_value << ((U)value & 0x1F);
00232 case DSGA_OP_SHR: return (U)last_value >> ((U)value & 0x1F);
00233 case DSGA_OP_SAR: return (S)last_value >> ((U)value & 0x1F);
00234 default: return value;
00235 }
00236 }
00237
00238
00239 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) const
00240 {
00241 uint32 last_value = 0;
00242 uint32 value = 0;
00243 uint i;
00244
00245 ScopeResolver *scope = object.GetScope(this->var_scope);
00246
00247 for (i = 0; i < this->num_adjusts; i++) {
00248 DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
00249
00250
00251 bool available = true;
00252 if (adjust->variable == 0x7E) {
00253 const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object, false);
00254 if (subgroup == NULL) {
00255 value = CALLBACK_FAILED;
00256 } else {
00257 value = subgroup->GetCallbackResult();
00258 }
00259
00260
00261 } else if (adjust->variable == 0x7B) {
00262 value = GetVariable(object, scope, adjust->parameter, last_value, &available);
00263 } else {
00264 value = GetVariable(object, scope, adjust->variable, adjust->parameter, &available);
00265 }
00266
00267 if (!available) {
00268
00269
00270 return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object, false);
00271 }
00272
00273 switch (this->size) {
00274 case DSG_SIZE_BYTE: value = EvalAdjustT<uint8, int8> (adjust, scope, last_value, value); break;
00275 case DSG_SIZE_WORD: value = EvalAdjustT<uint16, int16>(adjust, scope, last_value, value); break;
00276 case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, scope, last_value, value); break;
00277 default: NOT_REACHED();
00278 }
00279 last_value = value;
00280 }
00281
00282 object.last_value = last_value;
00283
00284 if (this->num_ranges == 0) {
00285
00286 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
00287 static CallbackResultSpriteGroup nvarzero(0, true);
00288 nvarzero.result = value;
00289 return &nvarzero;
00290 }
00291
00292 for (i = 0; i < this->num_ranges; i++) {
00293 if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
00294 return SpriteGroup::Resolve(this->ranges[i].group, object, false);
00295 }
00296 }
00297
00298 return SpriteGroup::Resolve(this->default_group, object, false);
00299 }
00300
00301
00302 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject &object) const
00303 {
00304 ScopeResolver *scope = object.GetScope(this->var_scope, this->count);
00305 if (object.trigger != 0) {
00306
00307
00308 byte waiting_triggers = scope->GetTriggers();
00309 byte match = this->triggers & (waiting_triggers | object.trigger);
00310 bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
00311
00312 if (res) {
00313 waiting_triggers &= ~match;
00314 object.reseed[this->var_scope] |= (this->num_groups - 1) << this->lowest_randbit;
00315 } else {
00316 waiting_triggers |= object.trigger;
00317 }
00318
00319 scope->SetTriggers(waiting_triggers);
00320 }
00321
00322 uint32 mask = (this->num_groups - 1) << this->lowest_randbit;
00323 byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
00324
00325 return SpriteGroup::Resolve(this->groups[index], object, false);
00326 }
00327
00328
00329 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject &object) const
00330 {
00331 return object.ResolveReal(this);
00332 }
00333
00341 const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8 *stage) const
00342 {
00343 if (!this->dts.NeedsPreprocessing()) {
00344 if (stage != NULL && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
00345 return &this->dts;
00346 }
00347
00348 static DrawTileSprites result;
00349 uint8 actual_stage = stage != NULL ? *stage : 0;
00350 this->dts.PrepareLayout(0, 0, 0, actual_stage, false);
00351 this->dts.ProcessRegisters(0, 0, false);
00352 result.seq = this->dts.GetLayout(&result.ground);
00353
00354
00355 if (stage != NULL) *stage = 0;
00356
00357 return &result;
00358 }