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 TemporaryStorageArray<int32, 0x110> _temp_store;
00039
00040
00041 static inline uint32 GetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00042 {
00043
00044 uint32 value;
00045 if (GetGlobalVariable(variable, &value)) return value;
00046
00047
00048 switch (variable) {
00049 case 0x0C: return object->callback;
00050 case 0x10: return object->callback_param1;
00051 case 0x18: return object->callback_param2;
00052 case 0x1C: return object->last_value;
00053
00054 case 0x5F: return (object->GetRandomBits(object) << 8) | object->GetTriggers(object);
00055
00056 case 0x7D: return _temp_store.Get(parameter);
00057
00058 case 0x7F:
00059 if (object == NULL || object->grffile == NULL) return 0;
00060 return object->grffile->GetParam(parameter);
00061
00062
00063 default: return object->GetVariable(object, variable, parameter, available);
00064 }
00065 }
00066
00067
00074 static uint32 RotateRight(uint32 val, uint32 rot)
00075 {
00076
00077 rot %= 32;
00078
00079 return (val >> rot) | (val << (32 - rot));
00080 }
00081
00082
00083
00084
00085 template <typename U, typename S>
00086 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ResolverObject *object, U last_value, uint32 value)
00087 {
00088 value >>= adjust->shift_num;
00089 value &= adjust->and_mask;
00090
00091 if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
00092
00093 switch (adjust->type) {
00094 case DSGA_TYPE_DIV: value /= (S)adjust->divmod_val; break;
00095 case DSGA_TYPE_MOD: value %= (U)adjust->divmod_val; break;
00096 case DSGA_TYPE_NONE: break;
00097 }
00098
00099 switch (adjust->operation) {
00100 case DSGA_OP_ADD: return last_value + value;
00101 case DSGA_OP_SUB: return last_value - value;
00102 case DSGA_OP_SMIN: return min((S)last_value, (S)value);
00103 case DSGA_OP_SMAX: return max((S)last_value, (S)value);
00104 case DSGA_OP_UMIN: return min((U)last_value, (U)value);
00105 case DSGA_OP_UMAX: return max((U)last_value, (U)value);
00106 case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
00107 case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
00108 case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
00109 case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
00110 case DSGA_OP_MUL: return last_value * value;
00111 case DSGA_OP_AND: return last_value & value;
00112 case DSGA_OP_OR: return last_value | value;
00113 case DSGA_OP_XOR: return last_value ^ value;
00114 case DSGA_OP_STO: _temp_store.Store((U)value, (S)last_value); return last_value;
00115 case DSGA_OP_RST: return value;
00116 case DSGA_OP_STOP: if (object->psa != NULL) object->psa->Store((U)value, (S)last_value); return last_value;
00117 case DSGA_OP_ROR: return RotateRight(last_value, value);
00118 case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
00119 case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
00120 case DSGA_OP_SHL: return (U)last_value << ((U)value & 0x1F);
00121 case DSGA_OP_SHR: return (U)last_value >> ((U)value & 0x1F);
00122 case DSGA_OP_SAR: return (S)last_value >> ((U)value & 0x1F);
00123 default: return value;
00124 }
00125 }
00126
00127
00128 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject *object) const
00129 {
00130 uint32 last_value = 0;
00131 uint32 value = 0;
00132 uint i;
00133
00134 object->scope = this->var_scope;
00135
00136 for (i = 0; i < this->num_adjusts; i++) {
00137 DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
00138
00139
00140 bool available = true;
00141 if (adjust->variable == 0x7E) {
00142 const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, object);
00143 if (subgroup == NULL) {
00144 value = CALLBACK_FAILED;
00145 } else {
00146 value = subgroup->GetCallbackResult();
00147 }
00148
00149
00150
00151 object->scope = this->var_scope;
00152 } else if (adjust->variable == 0x7B) {
00153 value = GetVariable(object, adjust->parameter, last_value, &available);
00154 } else {
00155 value = GetVariable(object, adjust->variable, adjust->parameter, &available);
00156 }
00157
00158 if (!available) {
00159
00160
00161 return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object);
00162 }
00163
00164 switch (this->size) {
00165 case DSG_SIZE_BYTE: value = EvalAdjustT<uint8, int8> (adjust, object, last_value, value); break;
00166 case DSG_SIZE_WORD: value = EvalAdjustT<uint16, int16>(adjust, object, last_value, value); break;
00167 case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, object, last_value, value); break;
00168 default: NOT_REACHED();
00169 }
00170 last_value = value;
00171 }
00172
00173 object->last_value = last_value;
00174
00175 if (this->num_ranges == 0) {
00176
00177 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
00178 static CallbackResultSpriteGroup nvarzero(0);
00179 nvarzero.result = value;
00180 return &nvarzero;
00181 }
00182
00183 for (i = 0; i < this->num_ranges; i++) {
00184 if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
00185 return SpriteGroup::Resolve(this->ranges[i].group, object);
00186 }
00187 }
00188
00189 return SpriteGroup::Resolve(this->default_group, object);
00190 }
00191
00192
00193 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const
00194 {
00195 uint32 mask;
00196 byte index;
00197
00198 object->scope = this->var_scope;
00199 object->count = this->count;
00200
00201 if (object->trigger != 0) {
00202
00203
00204 byte waiting_triggers = object->GetTriggers(object);
00205 byte match = this->triggers & (waiting_triggers | object->trigger);
00206 bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
00207
00208 if (res) {
00209 waiting_triggers &= ~match;
00210 object->reseed |= (this->num_groups - 1) << this->lowest_randbit;
00211 } else {
00212 waiting_triggers |= object->trigger;
00213 }
00214
00215 object->SetTriggers(object, waiting_triggers);
00216 }
00217
00218 mask = (this->num_groups - 1) << this->lowest_randbit;
00219 index = (object->GetRandomBits(object) & mask) >> this->lowest_randbit;
00220
00221 return SpriteGroup::Resolve(this->groups[index], object);
00222 }
00223
00224
00225 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject *object) const
00226 {
00227 return object->ResolveReal(object, this);
00228 }