00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "variables.h"
00008 #include "landscape.h"
00009 #include "oldpool.h"
00010 #include "newgrf.h"
00011 #include "newgrf_callbacks.h"
00012 #include "newgrf_spritegroup.h"
00013 #include "sprite.h"
00014 #include "date_func.h"
00015 #include "settings_type.h"
00016
00017 static void SpriteGroupPoolCleanBlock(uint start_item, uint end_item);
00018
00019 static uint _spritegroup_count = 0;
00020 STATIC_OLD_POOL(SpriteGroup, SpriteGroup, 9, 250, NULL, SpriteGroupPoolCleanBlock)
00021
00022 static void DestroySpriteGroup(SpriteGroup *group)
00023 {
00024
00025
00026 switch (group->type) {
00027 case SGT_REAL:
00028 free((SpriteGroup**)group->g.real.loaded);
00029 free((SpriteGroup**)group->g.real.loading);
00030 break;
00031
00032 case SGT_DETERMINISTIC:
00033 free(group->g.determ.adjusts);
00034 free(group->g.determ.ranges);
00035 break;
00036
00037 case SGT_RANDOMIZED:
00038 free((SpriteGroup**)group->g.random.groups);
00039 break;
00040
00041 case SGT_TILELAYOUT:
00042 free((void*)group->g.layout.dts->seq);
00043 free(group->g.layout.dts);
00044 break;
00045
00046 default:
00047 break;
00048 }
00049 }
00050
00051 static void SpriteGroupPoolCleanBlock(uint start_item, uint end_item)
00052 {
00053 uint i;
00054
00055 for (i = start_item; i <= end_item; i++) {
00056 DestroySpriteGroup(GetSpriteGroup(i));
00057 }
00058 }
00059
00060
00061
00062 SpriteGroup *AllocateSpriteGroup()
00063 {
00064
00065 if (_spritegroup_count == GetSpriteGroupPoolSize()) {
00066 if (!_SpriteGroup_pool.AddBlockToPool()) return NULL;
00067 }
00068
00069 return GetSpriteGroup(_spritegroup_count++);
00070 }
00071
00072
00073 void InitializeSpriteGroupPool()
00074 {
00075 _SpriteGroup_pool.CleanPool();
00076
00077 _spritegroup_count = 0;
00078 }
00079
00080 TemporaryStorageArray<uint32, 0x110> _temp_store;
00081
00082
00083 static inline uint32 GetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00084 {
00085
00086 uint32 value;
00087 if (GetGlobalVariable(variable, &value)) return value;
00088
00089
00090 switch (variable) {
00091 case 0x0C: return object->callback;
00092 case 0x10: return object->callback_param1;
00093 case 0x18: return object->callback_param2;
00094 case 0x1C: return object->last_value;
00095
00096 case 0x7D: return _temp_store.Get(parameter);
00097
00098
00099 default: return object->GetVariable(object, variable, parameter, available);
00100 }
00101 }
00102
00103
00110 static uint32 RotateRight(uint32 val, uint32 rot)
00111 {
00112
00113 rot %= 32;
00114
00115 return (val >> rot) | (val << (32 - rot));
00116 }
00117
00118
00119
00120
00121 template <typename U, typename S>
00122 static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ResolverObject *object, U last_value, uint32 value)
00123 {
00124 value >>= adjust->shift_num;
00125 value &= adjust->and_mask;
00126
00127 if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
00128
00129 switch (adjust->type) {
00130 case DSGA_TYPE_DIV: value /= (S)adjust->divmod_val; break;
00131 case DSGA_TYPE_MOD: value %= (U)adjust->divmod_val; break;
00132 case DSGA_TYPE_NONE: break;
00133 }
00134
00135 switch (adjust->operation) {
00136 case DSGA_OP_ADD: return last_value + value;
00137 case DSGA_OP_SUB: return last_value - value;
00138 case DSGA_OP_SMIN: return min((S)last_value, (S)value);
00139 case DSGA_OP_SMAX: return max((S)last_value, (S)value);
00140 case DSGA_OP_UMIN: return min((U)last_value, (U)value);
00141 case DSGA_OP_UMAX: return max((U)last_value, (U)value);
00142 case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
00143 case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
00144 case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
00145 case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
00146 case DSGA_OP_MUL: return last_value * value;
00147 case DSGA_OP_AND: return last_value & value;
00148 case DSGA_OP_OR: return last_value | value;
00149 case DSGA_OP_XOR: return last_value ^ value;
00150 case DSGA_OP_STO: _temp_store.Store(value, last_value); return last_value;
00151 case DSGA_OP_RST: return value;
00152 case DSGA_OP_STOP: if (object->psa != NULL) object->psa->Store(value, last_value); return last_value;
00153 case DSGA_OP_ROR: return RotateRight(last_value, value);
00154 case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
00155 case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
00156 default: return value;
00157 }
00158 }
00159
00160
00161 static inline const SpriteGroup *ResolveVariable(const SpriteGroup *group, ResolverObject *object)
00162 {
00163 static SpriteGroup nvarzero;
00164 uint32 last_value = 0;
00165 uint32 value = 0;
00166 uint i;
00167
00168 object->scope = group->g.determ.var_scope;
00169
00170 for (i = 0; i < group->g.determ.num_adjusts; i++) {
00171 DeterministicSpriteGroupAdjust *adjust = &group->g.determ.adjusts[i];
00172
00173
00174 bool available = true;
00175 if (adjust->variable == 0x7E) {
00176 ResolverObject subobject = *object;
00177 subobject.procedure_call = true;
00178 const SpriteGroup *subgroup = Resolve(adjust->subroutine, &subobject);
00179 if (subgroup == NULL || subgroup->type != SGT_CALLBACK) {
00180 value = CALLBACK_FAILED;
00181 } else {
00182 value = subgroup->g.callback.result;
00183 }
00184 } else {
00185 value = GetVariable(object, adjust->variable, adjust->parameter, &available);
00186 }
00187
00188 if (!available) {
00189
00190
00191 return Resolve(group->g.determ.num_ranges > 0 ? group->g.determ.ranges[0].group : group->g.determ.default_group, object);
00192 }
00193
00194 switch (group->g.determ.size) {
00195 case DSG_SIZE_BYTE: value = EvalAdjustT<uint8, int8> (adjust, object, last_value, value); break;
00196 case DSG_SIZE_WORD: value = EvalAdjustT<uint16, int16>(adjust, object, last_value, value); break;
00197 case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, object, last_value, value); break;
00198 default: NOT_REACHED(); break;
00199 }
00200 last_value = value;
00201 }
00202
00203 object->last_value = last_value;
00204
00205 if (group->g.determ.num_ranges == 0) {
00206
00207 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
00208 nvarzero.type = SGT_CALLBACK;
00209 nvarzero.g.callback.result = value;
00210 return &nvarzero;
00211 }
00212
00213 for (i = 0; i < group->g.determ.num_ranges; i++) {
00214 if (group->g.determ.ranges[i].low <= value && value <= group->g.determ.ranges[i].high) {
00215 return Resolve(group->g.determ.ranges[i].group, object);
00216 }
00217 }
00218
00219 return Resolve(group->g.determ.default_group, object);
00220 }
00221
00222
00223 static inline const SpriteGroup *ResolveRandom(const SpriteGroup *group, ResolverObject *object)
00224 {
00225 uint32 mask;
00226 byte index;
00227
00228 object->scope = group->g.random.var_scope;
00229
00230 if (object->trigger != 0) {
00231
00232
00233 byte waiting_triggers = object->GetTriggers(object);
00234 byte match = group->g.random.triggers & (waiting_triggers | object->trigger);
00235 bool res;
00236
00237 res = (group->g.random.cmp_mode == RSG_CMP_ANY) ?
00238 (match != 0) : (match == group->g.random.triggers);
00239
00240 if (res) {
00241 waiting_triggers &= ~match;
00242 object->reseed |= (group->g.random.num_groups - 1) << group->g.random.lowest_randbit;
00243 } else {
00244 waiting_triggers |= object->trigger;
00245 }
00246
00247 object->SetTriggers(object, waiting_triggers);
00248 }
00249
00250 mask = (group->g.random.num_groups - 1) << group->g.random.lowest_randbit;
00251 index = (object->GetRandomBits(object) & mask) >> group->g.random.lowest_randbit;
00252
00253 return Resolve(group->g.random.groups[index], object);
00254 }
00255
00256
00257
00258 const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject *object)
00259 {
00260
00261 if (group == NULL) return NULL;
00262
00263 switch (group->type) {
00264 case SGT_REAL: return object->ResolveReal(object, group);
00265 case SGT_DETERMINISTIC: return ResolveVariable(group, object);
00266 case SGT_RANDOMIZED: return ResolveRandom(group, object);
00267 default: return group;
00268 }
00269 }