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 }
00154
00155 ResolverObject::~ResolverObject() {}
00156
00162 const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
00163 {
00164 return NULL;
00165 }
00166
00173 ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
00174 {
00175 return &this->default_scope;
00176 }
00177
00184 static uint32 RotateRight(uint32 val, uint32 rot)
00185 {
00186
00187 rot %= 32;
00188
00189 return (val >> rot) | (val << (32 - rot));
00190 }
00191
00192
00193
00194
00195 template <typename U, typename S>
00196 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ScopeResolver *scope, U last_value, uint32 value)
00197 {
00198 value >>= adjust->shift_num;
00199 value &= adjust->and_mask;
00200
00201 if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
00202
00203 switch (adjust->type) {
00204 case DSGA_TYPE_DIV: value /= (S)adjust->divmod_val; break;
00205 case DSGA_TYPE_MOD: value %= (U)adjust->divmod_val; break;
00206 case DSGA_TYPE_NONE: break;
00207 }
00208
00209 switch (adjust->operation) {
00210 case DSGA_OP_ADD: return last_value + value;
00211 case DSGA_OP_SUB: return last_value - value;
00212 case DSGA_OP_SMIN: return min((S)last_value, (S)value);
00213 case DSGA_OP_SMAX: return max((S)last_value, (S)value);
00214 case DSGA_OP_UMIN: return min((U)last_value, (U)value);
00215 case DSGA_OP_UMAX: return max((U)last_value, (U)value);
00216 case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
00217 case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
00218 case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
00219 case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
00220 case DSGA_OP_MUL: return last_value * value;
00221 case DSGA_OP_AND: return last_value & value;
00222 case DSGA_OP_OR: return last_value | value;
00223 case DSGA_OP_XOR: return last_value ^ value;
00224 case DSGA_OP_STO: _temp_store.StoreValue((U)value, (S)last_value); return last_value;
00225 case DSGA_OP_RST: return value;
00226 case DSGA_OP_STOP: scope->StorePSA((U)value, (S)last_value); return last_value;
00227 case DSGA_OP_ROR: return RotateRight(last_value, value);
00228 case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
00229 case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
00230 case DSGA_OP_SHL: return (U)last_value << ((U)value & 0x1F);
00231 case DSGA_OP_SHR: return (U)last_value >> ((U)value & 0x1F);
00232 case DSGA_OP_SAR: return (S)last_value >> ((U)value & 0x1F);
00233 default: return value;
00234 }
00235 }
00236
00237
00238 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) const
00239 {
00240 uint32 last_value = 0;
00241 uint32 value = 0;
00242 uint i;
00243
00244 ScopeResolver *scope = object.GetScope(this->var_scope);
00245
00246 for (i = 0; i < this->num_adjusts; i++) {
00247 DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
00248
00249
00250 bool available = true;
00251 if (adjust->variable == 0x7E) {
00252 const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object, false);
00253 if (subgroup == NULL) {
00254 value = CALLBACK_FAILED;
00255 } else {
00256 value = subgroup->GetCallbackResult();
00257 }
00258
00259
00260 } else if (adjust->variable == 0x7B) {
00261 value = GetVariable(object, scope, adjust->parameter, last_value, &available);
00262 } else {
00263 value = GetVariable(object, scope, adjust->variable, adjust->parameter, &available);
00264 }
00265
00266 if (!available) {
00267
00268
00269 return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object, false);
00270 }
00271
00272 switch (this->size) {
00273 case DSG_SIZE_BYTE: value = EvalAdjustT<uint8, int8> (adjust, scope, last_value, value); break;
00274 case DSG_SIZE_WORD: value = EvalAdjustT<uint16, int16>(adjust, scope, last_value, value); break;
00275 case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, scope, last_value, value); break;
00276 default: NOT_REACHED();
00277 }
00278 last_value = value;
00279 }
00280
00281 object.last_value = last_value;
00282
00283 if (this->num_ranges == 0) {
00284
00285 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
00286 static CallbackResultSpriteGroup nvarzero(0, true);
00287 nvarzero.result = value;
00288 return &nvarzero;
00289 }
00290
00291 for (i = 0; i < this->num_ranges; i++) {
00292 if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
00293 return SpriteGroup::Resolve(this->ranges[i].group, object, false);
00294 }
00295 }
00296
00297 return SpriteGroup::Resolve(this->default_group, object, false);
00298 }
00299
00300
00301 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject &object) const
00302 {
00303 ScopeResolver *scope = object.GetScope(this->var_scope, this->count);
00304 if (object.trigger != 0) {
00305
00306
00307 byte waiting_triggers = scope->GetTriggers();
00308 byte match = this->triggers & (waiting_triggers | object.trigger);
00309 bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
00310
00311 if (res) {
00312 waiting_triggers &= ~match;
00313 object.reseed[this->var_scope] |= (this->num_groups - 1) << this->lowest_randbit;
00314 } else {
00315 waiting_triggers |= object.trigger;
00316 }
00317
00318 scope->SetTriggers(waiting_triggers);
00319 }
00320
00321 uint32 mask = (this->num_groups - 1) << this->lowest_randbit;
00322 byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
00323
00324 return SpriteGroup::Resolve(this->groups[index], object, false);
00325 }
00326
00327
00328 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject &object) const
00329 {
00330 return object.ResolveReal(this);
00331 }
00332
00340 const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8 *stage) const
00341 {
00342 if (!this->dts.NeedsPreprocessing()) {
00343 if (stage != NULL && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
00344 return &this->dts;
00345 }
00346
00347 static DrawTileSprites result;
00348 uint8 actual_stage = stage != NULL ? *stage : 0;
00349 this->dts.PrepareLayout(0, 0, 0, actual_stage, false);
00350 this->dts.ProcessRegisters(0, 0, false);
00351 result.seq = this->dts.GetLayout(&result.ground);
00352
00353
00354 if (stage != NULL) *stage = 0;
00355
00356 return &result;
00357 }