summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--newgrf_spritegroup.c179
-rw-r--r--newgrf_spritegroup.h35
2 files changed, 213 insertions, 1 deletions
diff --git a/newgrf_spritegroup.c b/newgrf_spritegroup.c
index b796c7946..3eee2c903 100644
--- a/newgrf_spritegroup.c
+++ b/newgrf_spritegroup.c
@@ -2,8 +2,10 @@
#include "stdafx.h"
#include "openttd.h"
+#include "variables.h"
+#include "macros.h"
#include "pool.h"
-#include "sprite.h"
+#include "newgrf_spritegroup.h"
enum {
SPRITEGROUP_POOL_BLOCK_SIZE_BITS = 4, /* (1 << 4) == 16 items */
@@ -67,3 +69,178 @@ void InitializeSpriteGroupPool(void)
_spritegroup_count = 0;
}
+
+
+static const SpriteGroup *ResolveReal(const SpriteGroup *group, ResolverObject *object)
+{
+ bool in_motion;
+ uint set;
+
+ set = object->ResolveReal(object, group->g.real.num_loaded, group->g.real.num_loading, &in_motion);
+
+ assert((in_motion && set < group->g.real.num_loaded) || (!in_motion && set < group->g.real.num_loading));
+
+ return Resolve(in_motion ? group->g.real.loaded[set] : group->g.real.loading[set], object);
+}
+
+
+static uint32 GetVariable(const ResolverObject *object, byte variable, byte parameter)
+{
+ /* Return common variables */
+ switch (variable) {
+ case 0x00: return _date;
+ case 0x01: return _cur_year;
+ case 0x02: return _cur_month;
+ case 0x03: return _opt.landscape;
+ case 0x09: return _date_fract;
+ case 0x0A: return _tick_counter;
+ case 0x0C: return object->callback;
+ case 0x10: return object->callback_param1;
+ case 0x11: return 0;
+ case 0x18: return object->callback_param2;
+ case 0x1A: return -1;
+ case 0x1B: return GB(_display_opt, 0, 6);
+ case 0x1C: return object->last_value;
+ case 0x20: return _opt.landscape == LT_HILLY ? _opt.snow_line : 0xFF;
+
+ /* Not a common variable, so evalute the feature specific variables */
+ default: return object->GetVariable(object, variable, parameter);
+ }
+}
+
+
+/* Evaluate an adjustment for a variable of the given size. This is a bit of
+ * an unwieldy macro, but it saves triplicating the code. */
+#define BUILD_EVAL_ADJUST(size, usize) \
+static size EvalAdjust_ ## size(const DeterministicSpriteGroupAdjust *adjust, size last_value, size value) \
+{ \
+ value >>= adjust->shift_num; \
+ value &= adjust->and_mask; \
+\
+ if (adjust->type != DSGA_TYPE_NONE) value += (size)adjust->add_val; \
+\
+ switch (adjust->type) { \
+ case DSGA_TYPE_DIV: value /= (size)adjust->divmod_val; break; \
+ case DSGA_TYPE_MOD: value %= (size)adjust->divmod_val; break; \
+ case DSGA_TYPE_NONE: break; \
+ } \
+\
+ switch (adjust->operation) { \
+ case DSGA_OP_ADD: return last_value + value; \
+ case DSGA_OP_SUB: return last_value - value; \
+ case DSGA_OP_SMIN: return min(last_value, value); \
+ case DSGA_OP_SMAX: return max(last_value, value); \
+ case DSGA_OP_UMIN: return min((usize)last_value, (usize)value); \
+ case DSGA_OP_UMAX: return max((usize)last_value, (usize)value); \
+ case DSGA_OP_SDIV: return last_value / value; \
+ case DSGA_OP_SMOD: return last_value % value; \
+ case DSGA_OP_UDIV: return (usize)last_value / (usize)value; \
+ case DSGA_OP_UMOD: return (usize)last_value % (usize)value; \
+ 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; \
+ default: return value; \
+ } \
+}
+
+
+BUILD_EVAL_ADJUST(int8, uint8)
+BUILD_EVAL_ADJUST(int16, uint16)
+BUILD_EVAL_ADJUST(int32, uint32)
+
+
+static const SpriteGroup *ResolveVariable(const SpriteGroup *group, ResolverObject *object)
+{
+ static SpriteGroup nvarzero;
+ const SpriteGroup *target;
+ int32 last_value = object->last_value;
+ int32 value = -1;
+ 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];
+ value = GetVariable(object, adjust->variable, adjust->parameter);
+ switch (group->g.determ.size) {
+ case DSG_SIZE_BYTE: value = EvalAdjust_int8(adjust, last_value, value); break;
+ case DSG_SIZE_WORD: value = EvalAdjust_int16(adjust, last_value, value); break;
+ case DSG_SIZE_DWORD: value = EvalAdjust_int32(adjust, last_value, value); break;
+ default: NOT_REACHED(); break;
+ }
+ last_value = value;
+ }
+
+ if (value == -1) {
+ /* Unsupported property */
+ target = group->g.determ.num_ranges > 0 ? group->g.determ.ranges[0].group : group->g.determ.default_group;
+ } else {
+ 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;
+ nvarzero.g.callback.result = GB(value, 0, 15) | 0x8000;
+ return &nvarzero;
+ }
+
+ target = group->g.determ.default_group;
+ for (i = 0; i < group->g.determ.num_ranges; i++) {
+ if (group->g.determ.ranges[i].low <= (uint32)value && (uint32)value <= group->g.determ.ranges[i].high) {
+ target = group->g.determ.ranges[i].group;
+ break;
+ }
+ }
+ }
+
+ return Resolve(target, object);
+}
+
+
+static const SpriteGroup *ResolveRandom(const SpriteGroup *group, ResolverObject *object)
+{
+ byte mask;
+ 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);
+ byte match = group->g.random.triggers & (waiting_triggers | object->trigger);
+ 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;
+
+ return Resolve(group->g.random.groups[index], object);
+}
+
+
+/* ResolverObject (re)entry point */
+const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject *object)
+{
+ /* We're called even if there is no group, so quietly return nothing */
+ if (group == NULL) return NULL;
+
+ switch (group->type) {
+ case SGT_REAL: return ResolveReal(group, object);
+ case SGT_DETERMINISTIC: return ResolveVariable(group, object);
+ case SGT_RANDOMIZED: return ResolveRandom(group, object);
+ default: return group;
+ }
+}
+
diff --git a/newgrf_spritegroup.h b/newgrf_spritegroup.h
index bd03f00a8..36680f451 100644
--- a/newgrf_spritegroup.h
+++ b/newgrf_spritegroup.h
@@ -154,4 +154,39 @@ struct SpriteGroup {
SpriteGroup *AllocateSpriteGroup(void);
void InitializeSpriteGroupPool(void);
+
+typedef struct ResolverObject {
+ byte callback;
+ uint32 callback_param1;
+ uint32 callback_param2;
+
+ byte trigger;
+ uint32 last_value;
+ uint32 reseed;
+ VarSpriteGroupScope scope;
+
+ union {
+ struct {
+ const struct Vehicle *self;
+ const struct Vehicle *parent;
+ } vehicle;
+ struct {
+ TileIndex tile;
+ const struct Station *st;
+ const struct StationSpec *statspec;
+ } station;
+ };
+
+ uint32 (*GetRandomBits)(const struct ResolverObject*);
+ uint32 (*GetTriggers)(const struct ResolverObject*);
+ void (*SetTriggers)(const struct ResolverObject*, int);
+ uint32 (*GetVariable)(const struct ResolverObject*, byte, byte);
+ uint32 (*ResolveReal)(const struct ResolverObject*, uint, uint, bool*);
+} ResolverObject;
+
+
+/* Base sprite group resolver */
+const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject *object);
+
+
#endif /* NEWGRF_SPRITEGROUP_H */