2006-04-20 20:51:57 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file newgrf_spritegroup.cpp Handling of primarily NewGRF action 2. */
|
2007-03-21 03:06:21 +00:00
|
|
|
|
2006-04-20 20:51:57 +00:00
|
|
|
#include "stdafx.h"
|
2008-02-12 15:43:10 +00:00
|
|
|
#include "newgrf.h"
|
2006-04-27 19:53:58 +00:00
|
|
|
#include "newgrf_spritegroup.h"
|
2007-03-19 11:27:30 +00:00
|
|
|
#include "sprite.h"
|
2009-05-22 15:13:50 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2006-04-20 20:51:57 +00:00
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
SpriteGroupPool _spritegroup_pool("SpriteGroup");
|
|
|
|
INSTANTIATE_POOL_METHODS(SpriteGroup)
|
2006-04-20 20:51:57 +00:00
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
RealSpriteGroup::~RealSpriteGroup()
|
2006-08-26 19:47:13 +00:00
|
|
|
{
|
2009-05-24 20:29:04 +00:00
|
|
|
free((void*)this->loaded);
|
|
|
|
free((void*)this->loading);
|
2009-05-23 12:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeterministicSpriteGroup::~DeterministicSpriteGroup()
|
|
|
|
{
|
|
|
|
free(this->adjusts);
|
|
|
|
free(this->ranges);
|
|
|
|
}
|
|
|
|
|
|
|
|
RandomizedSpriteGroup::~RandomizedSpriteGroup()
|
|
|
|
{
|
2009-05-24 20:29:04 +00:00
|
|
|
free((void*)this->groups);
|
2009-05-23 12:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TileLayoutSpriteGroup::~TileLayoutSpriteGroup()
|
|
|
|
{
|
|
|
|
free((void*)this->dts->seq);
|
|
|
|
free(this->dts);
|
2006-04-20 20:51:57 +00:00
|
|
|
}
|
2006-04-27 19:53:58 +00:00
|
|
|
|
2007-09-22 23:55:34 +00:00
|
|
|
TemporaryStorageArray<uint32, 0x110> _temp_store;
|
2007-04-21 07:27:16 +00:00
|
|
|
|
2006-04-27 19:53:58 +00:00
|
|
|
|
2006-05-23 19:36:50 +00:00
|
|
|
static inline uint32 GetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
|
2006-04-27 19:53:58 +00:00
|
|
|
{
|
2008-02-12 15:43:10 +00:00
|
|
|
/* First handle variables common with Action7/9/D */
|
|
|
|
uint32 value;
|
2008-02-14 11:58:36 +00:00
|
|
|
if (GetGlobalVariable(variable, &value)) return value;
|
2008-02-12 15:43:10 +00:00
|
|
|
|
|
|
|
/* Non-common variable */
|
2006-04-27 19:53:58 +00:00
|
|
|
switch (variable) {
|
|
|
|
case 0x0C: return object->callback;
|
|
|
|
case 0x10: return object->callback_param1;
|
|
|
|
case 0x18: return object->callback_param2;
|
|
|
|
case 0x1C: return object->last_value;
|
|
|
|
|
2008-06-17 07:05:17 +00:00
|
|
|
case 0x5F: return (object->GetRandomBits(object) << 8) | object->GetTriggers(object);
|
|
|
|
|
2007-09-22 12:59:43 +00:00
|
|
|
case 0x7D: return _temp_store.Get(parameter);
|
2007-04-21 07:27:16 +00:00
|
|
|
|
2008-07-30 18:23:12 +00:00
|
|
|
case 0x7F:
|
|
|
|
if (object == NULL || object->grffile == NULL || parameter >= object->grffile->param_end) return 0;
|
|
|
|
return object->grffile->param[parameter];
|
|
|
|
|
2006-04-27 19:53:58 +00:00
|
|
|
/* Not a common variable, so evalute the feature specific variables */
|
2006-05-23 19:36:50 +00:00
|
|
|
default: return object->GetVariable(object, variable, parameter, available);
|
2006-04-27 19:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-22 20:29:17 +00:00
|
|
|
/**
|
|
|
|
* Rotate val rot times to the right
|
|
|
|
* @param val the value to rotate
|
|
|
|
* @param rot the amount of times to rotate
|
|
|
|
* @return the rotated value
|
|
|
|
*/
|
|
|
|
static uint32 RotateRight(uint32 val, uint32 rot)
|
|
|
|
{
|
|
|
|
/* Do not rotate more than necessary */
|
|
|
|
rot %= 32;
|
|
|
|
|
|
|
|
return (val >> rot) | (val << (32 - rot));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-11 10:58:53 +00:00
|
|
|
/* Evaluate an adjustment for a variable of the given size.
|
2009-03-14 18:16:29 +00:00
|
|
|
* U is the unsigned type and S is the signed type to use. */
|
2007-01-11 10:58:53 +00:00
|
|
|
template <typename U, typename S>
|
2007-09-22 13:56:38 +00:00
|
|
|
static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ResolverObject *object, U last_value, uint32 value)
|
2007-01-11 10:58:53 +00:00
|
|
|
{
|
|
|
|
value >>= adjust->shift_num;
|
|
|
|
value &= adjust->and_mask;
|
|
|
|
|
|
|
|
if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
|
2006-04-27 19:53:58 +00:00
|
|
|
|
2007-01-11 10:58:53 +00:00
|
|
|
switch (adjust->type) {
|
|
|
|
case DSGA_TYPE_DIV: value /= (S)adjust->divmod_val; break;
|
|
|
|
case DSGA_TYPE_MOD: value %= (U)adjust->divmod_val; break;
|
|
|
|
case DSGA_TYPE_NONE: break;
|
|
|
|
}
|
2006-04-27 19:53:58 +00:00
|
|
|
|
2007-01-11 10:58:53 +00:00
|
|
|
switch (adjust->operation) {
|
|
|
|
case DSGA_OP_ADD: return last_value + value;
|
|
|
|
case DSGA_OP_SUB: return last_value - value;
|
2007-01-11 11:05:01 +00:00
|
|
|
case DSGA_OP_SMIN: return min((S)last_value, (S)value);
|
|
|
|
case DSGA_OP_SMAX: return max((S)last_value, (S)value);
|
2007-01-11 10:58:53 +00:00
|
|
|
case DSGA_OP_UMIN: return min((U)last_value, (U)value);
|
|
|
|
case DSGA_OP_UMAX: return max((U)last_value, (U)value);
|
2007-05-14 17:37:34 +00:00
|
|
|
case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
|
|
|
|
case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
|
|
|
|
case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
|
|
|
|
case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
|
2007-01-11 10:58:53 +00:00
|
|
|
case DSGA_OP_MUL: return last_value * value;
|
|
|
|
case DSGA_OP_AND: return last_value & value;
|
|
|
|
case DSGA_OP_OR: return last_value | value;
|
|
|
|
case DSGA_OP_XOR: return last_value ^ value;
|
2007-09-22 12:59:43 +00:00
|
|
|
case DSGA_OP_STO: _temp_store.Store(value, last_value); return last_value;
|
2007-04-21 07:27:16 +00:00
|
|
|
case DSGA_OP_RST: return value;
|
2007-09-22 13:56:38 +00:00
|
|
|
case DSGA_OP_STOP: if (object->psa != NULL) object->psa->Store(value, last_value); return last_value;
|
2007-09-22 20:29:17 +00:00
|
|
|
case DSGA_OP_ROR: return RotateRight(last_value, value);
|
|
|
|
case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
|
|
|
|
case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
|
2007-01-11 10:58:53 +00:00
|
|
|
default: return value;
|
|
|
|
}
|
|
|
|
}
|
2006-04-27 19:53:58 +00:00
|
|
|
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject *object) const
|
2006-04-27 19:53:58 +00:00
|
|
|
{
|
2007-01-11 14:40:12 +00:00
|
|
|
uint32 last_value = 0;
|
2007-01-11 14:25:17 +00:00
|
|
|
uint32 value = 0;
|
2006-04-27 19:53:58 +00:00
|
|
|
uint i;
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
object->scope = this->var_scope;
|
2006-04-27 19:53:58 +00:00
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
for (i = 0; i < this->num_adjusts; i++) {
|
|
|
|
DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
|
2006-05-23 19:36:50 +00:00
|
|
|
|
|
|
|
/* Try to get the variable. We shall assume it is available, unless told otherwise. */
|
|
|
|
bool available = true;
|
2007-01-12 11:20:34 +00:00
|
|
|
if (adjust->variable == 0x7E) {
|
2007-01-12 16:44:08 +00:00
|
|
|
ResolverObject subobject = *object;
|
2008-03-10 15:31:07 +00:00
|
|
|
subobject.procedure_call = true;
|
2009-05-23 15:25:52 +00:00
|
|
|
const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, &subobject);
|
2009-05-23 12:13:42 +00:00
|
|
|
if (subgroup == NULL) {
|
2007-01-12 11:20:34 +00:00
|
|
|
value = CALLBACK_FAILED;
|
|
|
|
} else {
|
2009-05-23 12:13:42 +00:00
|
|
|
value = subgroup->GetCallbackResult();
|
2007-01-12 11:20:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value = GetVariable(object, adjust->variable, adjust->parameter, &available);
|
|
|
|
}
|
2006-05-23 19:36:50 +00:00
|
|
|
|
|
|
|
if (!available) {
|
|
|
|
/* Unsupported property: skip further processing and return either
|
|
|
|
* the group from the first range or the default group. */
|
2009-05-23 15:25:52 +00:00
|
|
|
return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object);
|
2006-05-23 19:36:50 +00:00
|
|
|
}
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
switch (this->size) {
|
2007-09-22 13:56:38 +00:00
|
|
|
case DSG_SIZE_BYTE: value = EvalAdjustT<uint8, int8> (adjust, object, last_value, value); break;
|
|
|
|
case DSG_SIZE_WORD: value = EvalAdjustT<uint16, int16>(adjust, object, last_value, value); break;
|
|
|
|
case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, object, last_value, value); break;
|
2009-05-26 15:46:24 +00:00
|
|
|
default: NOT_REACHED();
|
2006-04-27 19:53:58 +00:00
|
|
|
}
|
|
|
|
last_value = value;
|
|
|
|
}
|
|
|
|
|
2007-01-11 14:40:12 +00:00
|
|
|
object->last_value = last_value;
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
if (this->num_ranges == 0) {
|
2006-05-23 19:36:50 +00:00
|
|
|
/* nvar == 0 is a special case -- we turn our value into a callback result */
|
2008-05-04 22:32:25 +00:00
|
|
|
if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
|
2009-05-23 12:13:42 +00:00
|
|
|
static CallbackResultSpriteGroup nvarzero(0);
|
|
|
|
nvarzero.result = value;
|
2006-05-23 19:36:50 +00:00
|
|
|
return &nvarzero;
|
|
|
|
}
|
2006-04-27 19:53:58 +00:00
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
for (i = 0; i < this->num_ranges; i++) {
|
|
|
|
if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
|
|
|
|
return SpriteGroup::Resolve(this->ranges[i].group, object);
|
2006-04-27 19:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
return SpriteGroup::Resolve(this->default_group, object);
|
2006-04-27 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const
|
2006-04-27 19:53:58 +00:00
|
|
|
{
|
2006-05-04 19:42:50 +00:00
|
|
|
uint32 mask;
|
2006-04-27 19:53:58 +00:00
|
|
|
byte index;
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
object->scope = this->var_scope;
|
|
|
|
object->count = this->count;
|
2006-04-27 19:53:58 +00:00
|
|
|
|
|
|
|
if (object->trigger != 0) {
|
|
|
|
/* Handle triggers */
|
|
|
|
/* Magic code that may or may not do the right things... */
|
|
|
|
byte waiting_triggers = object->GetTriggers(object);
|
2009-05-23 15:25:52 +00:00
|
|
|
byte match = this->triggers & (waiting_triggers | object->trigger);
|
|
|
|
bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
|
2006-04-27 19:53:58 +00:00
|
|
|
|
|
|
|
if (res) {
|
|
|
|
waiting_triggers &= ~match;
|
2009-05-23 15:25:52 +00:00
|
|
|
object->reseed |= (this->num_groups - 1) << this->lowest_randbit;
|
2006-04-27 19:53:58 +00:00
|
|
|
} else {
|
|
|
|
waiting_triggers |= object->trigger;
|
|
|
|
}
|
|
|
|
|
|
|
|
object->SetTriggers(object, waiting_triggers);
|
|
|
|
}
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
mask = (this->num_groups - 1) << this->lowest_randbit;
|
|
|
|
index = (object->GetRandomBits(object) & mask) >> this->lowest_randbit;
|
2006-04-27 19:53:58 +00:00
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
return SpriteGroup::Resolve(this->groups[index], object);
|
2006-04-27 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject *object) const
|
2006-04-27 19:53:58 +00:00
|
|
|
{
|
2009-05-23 15:25:52 +00:00
|
|
|
return object->ResolveReal(object, this);
|
2006-04-27 19:53:58 +00:00
|
|
|
}
|