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<int32, 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) return 0;
00066 return object->grffile->GetParam(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((U)value, (S)last_value); return last_value;
00121 case DSGA_OP_RST: return value;
00122 case DSGA_OP_STOP: if (object->psa != NULL) object->psa->Store((U)value, (S)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 case DSGA_OP_SHL: return (U)last_value << ((U)value & 0x1F);
00127 case DSGA_OP_SHR: return (U)last_value >> ((U)value & 0x1F);
00128 case DSGA_OP_SAR: return (S)last_value >> ((U)value & 0x1F);
00129 default: return value;
00130 }
00131 }
00132
00133
00134 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject *object) const
00135 {
00136 uint32 last_value = 0;
00137 uint32 value = 0;
00138 uint i;
00139
00140 object->scope = this->var_scope;
00141
00142 for (i = 0; i < this->num_adjusts; i++) {
00143 DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
00144
00145
00146 bool available = true;
00147 if (adjust->variable == 0x7E) {
00148 const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object);
00149 if (subgroup == NULL) {
00150 value = CALLBACK_FAILED;
00151 } else {
00152 value = subgroup->GetCallbackResult();
00153 }
00154
00155
00156
00157 object->scope = this->var_scope;
00158 } else if (adjust->variable == 0x7B) {
00159 value = GetVariable(object, adjust->parameter, last_value, &available);
00160 } else {
00161 value = GetVariable(object, adjust->variable, adjust->parameter, &available);
00162 }
00163
00164 if (!available) {
00165
00166
00167 return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object);
00168 }
00169
00170 switch (this->size) {
00171 case DSG_SIZE_BYTE: value = EvalAdjustT<uint8, int8> (adjust, object, last_value, value); break;
00172 case DSG_SIZE_WORD: value = EvalAdjustT<uint16, int16>(adjust, object, last_value, value); break;
00173 case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, object, last_value, value); break;
00174 default: NOT_REACHED();
00175 }
00176 last_value = value;
00177 }
00178
00179 object->last_value = last_value;
00180
00181 if (this->num_ranges == 0) {
00182
00183 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
00184 static CallbackResultSpriteGroup nvarzero(0);
00185 nvarzero.result = value;
00186 return &nvarzero;
00187 }
00188
00189 for (i = 0; i < this->num_ranges; i++) {
00190 if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
00191 return SpriteGroup::Resolve(this->ranges[i].group, object);
00192 }
00193 }
00194
00195 return SpriteGroup::Resolve(this->default_group, object);
00196 }
00197
00198
00199 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const
00200 {
00201 uint32 mask;
00202 byte index;
00203
00204 object->scope = this->var_scope;
00205 object->count = this->count;
00206
00207 if (object->trigger != 0) {
00208
00209
00210 byte waiting_triggers = object->GetTriggers(object);
00211 byte match = this->triggers & (waiting_triggers | object->trigger);
00212 bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
00213
00214 if (res) {
00215 waiting_triggers &= ~match;
00216 object->reseed |= (this->num_groups - 1) << this->lowest_randbit;
00217 } else {
00218 waiting_triggers |= object->trigger;
00219 }
00220
00221 object->SetTriggers(object, waiting_triggers);
00222 }
00223
00224 mask = (this->num_groups - 1) << this->lowest_randbit;
00225 index = (object->GetRandomBits(object) & mask) >> this->lowest_randbit;
00226
00227 return SpriteGroup::Resolve(this->groups[index], object);
00228 }
00229
00230
00231 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject *object) const
00232 {
00233 return object->ResolveReal(object, this);
00234 }