2006-04-20 20:51:57 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
/** @file newgrf_spritegroup.cpp */
|
|
|
|
|
2006-04-20 20:51:57 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "openttd.h"
|
2006-04-27 19:53:58 +00:00
|
|
|
#include "variables.h"
|
2007-03-20 13:47:00 +00:00
|
|
|
#include "landscape.h"
|
2006-12-03 17:27:43 +00:00
|
|
|
#include "oldpool.h"
|
2008-02-12 15:43:10 +00:00
|
|
|
#include "newgrf.h"
|
2007-01-12 11:20:34 +00:00
|
|
|
#include "newgrf_callbacks.h"
|
2006-04-27 19:53:58 +00:00
|
|
|
#include "newgrf_spritegroup.h"
|
2007-03-19 11:27:30 +00:00
|
|
|
#include "sprite.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2008-01-13 14:37:30 +00:00
|
|
|
#include "settings_type.h"
|
2006-04-20 20:51:57 +00:00
|
|
|
|
2006-10-28 10:55:26 +00:00
|
|
|
static void SpriteGroupPoolCleanBlock(uint start_item, uint end_item);
|
2006-04-20 20:51:57 +00:00
|
|
|
|
|
|
|
static uint _spritegroup_count = 0;
|
2007-01-02 18:56:03 +00:00
|
|
|
STATIC_OLD_POOL(SpriteGroup, SpriteGroup, 9, 250, NULL, SpriteGroupPoolCleanBlock)
|
2006-04-20 20:51:57 +00:00
|
|
|
|
2006-11-05 08:01:47 +00:00
|
|
|
static void DestroySpriteGroup(SpriteGroup *group)
|
2006-08-26 19:47:13 +00:00
|
|
|
{
|
|
|
|
/* Free dynamically allocated memory */
|
2006-10-19 12:21:35 +00:00
|
|
|
/* XXX Cast away the consts due to MSVC being buggy... */
|
2006-08-26 19:47:13 +00:00
|
|
|
switch (group->type) {
|
|
|
|
case SGT_REAL:
|
2006-10-19 12:21:35 +00:00
|
|
|
free((SpriteGroup**)group->g.real.loaded);
|
|
|
|
free((SpriteGroup**)group->g.real.loading);
|
2006-08-26 19:47:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SGT_DETERMINISTIC:
|
|
|
|
free(group->g.determ.adjusts);
|
|
|
|
free(group->g.determ.ranges);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SGT_RANDOMIZED:
|
2006-10-19 12:21:35 +00:00
|
|
|
free((SpriteGroup**)group->g.random.groups);
|
2006-08-26 19:47:13 +00:00
|
|
|
break;
|
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
case SGT_TILELAYOUT:
|
|
|
|
free((void*)group->g.layout.dts->seq);
|
|
|
|
free(group->g.layout.dts);
|
|
|
|
break;
|
|
|
|
|
2006-08-26 19:47:13 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-04-20 20:51:57 +00:00
|
|
|
|
|
|
|
static void SpriteGroupPoolCleanBlock(uint start_item, uint end_item)
|
|
|
|
{
|
|
|
|
uint i;
|
|
|
|
|
|
|
|
for (i = start_item; i <= end_item; i++) {
|
2006-10-28 10:55:26 +00:00
|
|
|
DestroySpriteGroup(GetSpriteGroup(i));
|
2006-04-20 20:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Allocate a new SpriteGroup */
|
2007-03-07 11:47:46 +00:00
|
|
|
SpriteGroup *AllocateSpriteGroup()
|
2006-04-20 20:51:57 +00:00
|
|
|
{
|
|
|
|
/* This is totally different to the other pool allocators, as we never remove an item from the pool. */
|
2006-10-28 10:55:26 +00:00
|
|
|
if (_spritegroup_count == GetSpriteGroupPoolSize()) {
|
2007-08-03 20:18:38 +00:00
|
|
|
if (!_SpriteGroup_pool.AddBlockToPool()) return NULL;
|
2006-04-20 20:51:57 +00:00
|
|
|
}
|
|
|
|
|
2006-10-28 10:55:26 +00:00
|
|
|
return GetSpriteGroup(_spritegroup_count++);
|
2006-04-20 20:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitializeSpriteGroupPool()
|
2006-04-20 20:51:57 +00:00
|
|
|
{
|
2007-08-03 20:18:38 +00:00
|
|
|
_SpriteGroup_pool.CleanPool();
|
2006-04-20 20:51:57 +00:00
|
|
|
|
|
|
|
_spritegroup_count = 0;
|
|
|
|
}
|
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
|
|
|
|
2008-03-10 15:31:07 +00:00
|
|
|
static inline bool Is8BitCallback(const ResolverObject *object)
|
|
|
|
{
|
|
|
|
/* Var 0x7E procedure results are always 15 bit */
|
2008-03-11 12:55:41 +00:00
|
|
|
if (object == NULL || object->procedure_call) return false;
|
2008-03-10 15:31:07 +00:00
|
|
|
|
|
|
|
switch (object->callback) {
|
|
|
|
/* All these functions are 15 bit callbacks */
|
2008-03-21 11:10:36 +00:00
|
|
|
case CBID_STATION_SPRITE_LAYOUT:
|
2008-03-10 15:31:07 +00:00
|
|
|
case CBID_VEHICLE_REFIT_CAPACITY:
|
|
|
|
case CBID_HOUSE_COLOUR:
|
|
|
|
case CBID_HOUSE_CARGO_ACCEPTANCE:
|
|
|
|
case CBID_INDUSTRY_LOCATION:
|
|
|
|
case CBID_HOUSE_ACCEPT_CARGO:
|
|
|
|
case CBID_INDTILE_CARGO_ACCEPTANCE:
|
|
|
|
case CBID_INDTILE_ACCEPT_CARGO:
|
|
|
|
case CBID_VEHICLE_COLOUR_MAPPING:
|
|
|
|
case CBID_HOUSE_PRODUCE_CARGO:
|
2008-03-11 12:55:41 +00:00
|
|
|
case CBID_INDTILE_SHAPE_CHECK: // depends on grf version, masked to 8 bit in PerformIndustryTileSlopeCheck() if needed
|
2008-03-10 15:31:07 +00:00
|
|
|
case CBID_VEHICLE_SOUND_EFFECT:
|
|
|
|
case CBID_VEHICLE_MODIFY_PROPERTY: // depends on queried property
|
|
|
|
case CBID_CARGO_PROFIT_CALC:
|
|
|
|
case CBID_SOUNDS_AMBIENT_EFFECT:
|
|
|
|
case CBID_CARGO_STATION_RATING_CALC:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* The rest is a 8 bit callback, which should be truncated properly */
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2007-09-22 12:59:43 +00:00
|
|
|
case 0x7D: return _temp_store.Get(parameter);
|
2007-04-21 07:27:16 +00:00
|
|
|
|
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.
|
|
|
|
* U is the unsigned type and S is the signed type to use. */
|
|
|
|
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
|
|
|
|
|
|
|
|
2006-04-28 20:04:57 +00:00
|
|
|
static inline const SpriteGroup *ResolveVariable(const SpriteGroup *group, ResolverObject *object)
|
2006-04-27 19:53:58 +00:00
|
|
|
{
|
|
|
|
static SpriteGroup nvarzero;
|
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;
|
|
|
|
|
|
|
|
object->scope = group->g.determ.var_scope;
|
|
|
|
|
|
|
|
for (i = 0; i < group->g.determ.num_adjusts; i++) {
|
|
|
|
DeterministicSpriteGroupAdjust *adjust = &group->g.determ.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;
|
2007-09-22 12:59:43 +00:00
|
|
|
const SpriteGroup *subgroup = Resolve(adjust->subroutine, &subobject);
|
2007-01-12 11:20:34 +00:00
|
|
|
if (subgroup == NULL || subgroup->type != SGT_CALLBACK) {
|
|
|
|
value = CALLBACK_FAILED;
|
|
|
|
} else {
|
|
|
|
value = subgroup->g.callback.result;
|
|
|
|
}
|
|
|
|
} 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. */
|
2007-09-22 12:59:43 +00:00
|
|
|
return Resolve(group->g.determ.num_ranges > 0 ? group->g.determ.ranges[0].group : group->g.determ.default_group, object);
|
2006-05-23 19:36:50 +00:00
|
|
|
}
|
|
|
|
|
2006-04-27 19:53:58 +00:00
|
|
|
switch (group->g.determ.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;
|
2006-04-27 19:53:58 +00:00
|
|
|
default: NOT_REACHED(); break;
|
|
|
|
}
|
|
|
|
last_value = value;
|
|
|
|
}
|
|
|
|
|
2007-01-11 14:40:12 +00:00
|
|
|
object->last_value = last_value;
|
|
|
|
|
2006-05-23 19:36:50 +00:00
|
|
|
if (group->g.determ.num_ranges == 0) {
|
|
|
|
/* nvar == 0 is a special case -- we turn our value into a callback result */
|
|
|
|
nvarzero.type = SGT_CALLBACK;
|
2008-03-10 15:31:07 +00:00
|
|
|
nvarzero.g.callback.result = GB(value, 0, Is8BitCallback(object) ? 8 : 15);
|
2006-05-23 19:36:50 +00:00
|
|
|
return &nvarzero;
|
|
|
|
}
|
2006-04-27 19:53:58 +00:00
|
|
|
|
2006-05-23 19:36:50 +00:00
|
|
|
for (i = 0; i < group->g.determ.num_ranges; i++) {
|
2007-01-11 14:25:17 +00:00
|
|
|
if (group->g.determ.ranges[i].low <= value && value <= group->g.determ.ranges[i].high) {
|
2007-09-22 12:59:43 +00:00
|
|
|
return Resolve(group->g.determ.ranges[i].group, object);
|
2006-04-27 19:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-22 12:59:43 +00:00
|
|
|
return Resolve(group->g.determ.default_group, object);
|
2006-04-27 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-28 20:04:57 +00:00
|
|
|
static inline const SpriteGroup *ResolveRandom(const SpriteGroup *group, ResolverObject *object)
|
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;
|
|
|
|
|
|
|
|
object->scope = group->g.random.var_scope;
|
|
|
|
|
|
|
|
if (object->trigger != 0) {
|
|
|
|
/* Handle triggers */
|
|
|
|
/* Magic code that may or may not do the right things... */
|
|
|
|
byte waiting_triggers = object->GetTriggers(object);
|
2007-11-11 18:22:06 +00:00
|
|
|
byte match = group->g.random.triggers & (waiting_triggers | object->trigger);
|
2006-04-27 19:53:58 +00:00
|
|
|
bool res;
|
|
|
|
|
|
|
|
res = (group->g.random.cmp_mode == RSG_CMP_ANY) ?
|
|
|
|
(match != 0) : (match == group->g.random.triggers);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
waiting_triggers &= ~match;
|
|
|
|
object->reseed |= (group->g.random.num_groups - 1) << group->g.random.lowest_randbit;
|
|
|
|
} else {
|
|
|
|
waiting_triggers |= object->trigger;
|
|
|
|
}
|
|
|
|
|
|
|
|
object->SetTriggers(object, waiting_triggers);
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = (group->g.random.num_groups - 1) << group->g.random.lowest_randbit;
|
|
|
|
index = (object->GetRandomBits(object) & mask) >> group->g.random.lowest_randbit;
|
|
|
|
|
2007-09-22 12:59:43 +00:00
|
|
|
return Resolve(group->g.random.groups[index], object);
|
2006-04-27 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ResolverObject (re)entry point */
|
2007-09-22 12:59:43 +00:00
|
|
|
const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject *object)
|
2006-04-27 19:53:58 +00:00
|
|
|
{
|
|
|
|
/* We're called even if there is no group, so quietly return nothing */
|
|
|
|
if (group == NULL) return NULL;
|
|
|
|
|
|
|
|
switch (group->type) {
|
2006-05-03 15:46:21 +00:00
|
|
|
case SGT_REAL: return object->ResolveReal(object, group);
|
2006-04-27 19:53:58 +00:00
|
|
|
case SGT_DETERMINISTIC: return ResolveVariable(group, object);
|
|
|
|
case SGT_RANDOMIZED: return ResolveRandom(group, object);
|
2008-03-10 15:31:07 +00:00
|
|
|
case SGT_CALLBACK: {
|
|
|
|
if (!Is8BitCallback(object)) return group;
|
|
|
|
|
|
|
|
static SpriteGroup result8bit;
|
|
|
|
result8bit.type = SGT_CALLBACK;
|
|
|
|
result8bit.g.callback.result = GB(group->g.callback.result, 0, 8);
|
|
|
|
return &result8bit;
|
|
|
|
}
|
2006-04-27 19:53:58 +00:00
|
|
|
default: return group;
|
|
|
|
}
|
|
|
|
}
|