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