(svn r24678) -Codechange: Introduce scope resolver base class and prepare for adding derived classes.

This commit is contained in:
alberth 2012-11-10 20:37:31 +00:00
parent e8cc7d8009
commit 6a619b40d5
15 changed files with 173 additions and 29 deletions

View File

@ -217,7 +217,7 @@ static void NewAirportResolver(ResolverObject *res, TileIndex tile, Station *st,
res->GetTriggers = AirportGetTriggers; res->GetTriggers = AirportGetTriggers;
res->SetTriggers = AirportSetTriggers; res->SetTriggers = AirportSetTriggers;
res->GetVariable = AirportGetVariable; res->GetVariable = AirportGetVariable;
res->ResolveReal = AirportResolveReal; res->ResolveRealMethod = AirportResolveReal;
res->StorePSA = AirportStorePSA; res->StorePSA = AirportStorePSA;
res->u.airport.st = st; res->u.airport.st = st;

View File

@ -226,7 +226,7 @@ static void AirportTileResolver(ResolverObject *res, const AirportTileSpec *ats,
res->GetTriggers = NULL; res->GetTriggers = NULL;
res->SetTriggers = NULL; res->SetTriggers = NULL;
res->GetVariable = AirportTileGetVariable; res->GetVariable = AirportTileGetVariable;
res->ResolveReal = AirportTileResolveReal; res->ResolveRealMethod = AirportTileResolveReal;
res->StorePSA = NULL; res->StorePSA = NULL;
assert(st != NULL); assert(st != NULL);

View File

@ -82,7 +82,7 @@ static void NewCanalResolver(ResolverObject *res, TileIndex tile, const GRFFile
res->GetTriggers = &CanalGetTriggers; res->GetTriggers = &CanalGetTriggers;
res->SetTriggers = &CanalSetTriggers; res->SetTriggers = &CanalSetTriggers;
res->GetVariable = &CanalGetVariable; res->GetVariable = &CanalGetVariable;
res->ResolveReal = &CanalResolveReal; res->ResolveRealMethod = &CanalResolveReal;
res->u.canal.tile = tile; res->u.canal.tile = tile;

View File

@ -57,7 +57,7 @@ static void NewCargoResolver(ResolverObject *res, const CargoSpec *cs)
res->GetTriggers = &CargoGetTriggers; res->GetTriggers = &CargoGetTriggers;
res->SetTriggers = &CargoSetTriggers; res->SetTriggers = &CargoSetTriggers;
res->GetVariable = &CargoGetVariable; res->GetVariable = &CargoGetVariable;
res->ResolveReal = &CargoResolveReal; res->ResolveRealMethod = &CargoResolveReal;
res->u.cargo.cs = cs; res->u.cargo.cs = cs;

View File

@ -162,7 +162,7 @@ public:
ResolverObject ro; ResolverObject ro;
memset(&ro, 0, sizeof(ro)); memset(&ro, 0, sizeof(ro));
this->Resolve(&ro, index); this->Resolve(&ro, index);
return ro.GetVariable(&ro, var, param, avail); return ro.GetScope(ro.scope)->GetVariable(var, param, avail);
} }
/** /**

View File

@ -938,7 +938,7 @@ static inline void NewVehicleResolver(ResolverObject *res, EngineID engine_type,
res->GetTriggers = &VehicleGetTriggers; res->GetTriggers = &VehicleGetTriggers;
res->SetTriggers = &VehicleSetTriggers; res->SetTriggers = &VehicleSetTriggers;
res->GetVariable = &VehicleGetVariable; res->GetVariable = &VehicleGetVariable;
res->ResolveReal = &VehicleResolveReal; res->ResolveRealMethod = &VehicleResolveReal;
res->u.vehicle.self = v; res->u.vehicle.self = v;
res->u.vehicle.parent = (v != NULL) ? v->First() : v; res->u.vehicle.parent = (v != NULL) ? v->First() : v;
@ -1330,7 +1330,7 @@ void FillNewGRFVehicleCache(const Vehicle *v)
/* Only resolve when the cache isn't valid. */ /* Only resolve when the cache isn't valid. */
if (HasBit(v->grf_cache.cache_valid, cache_entries[i][1])) continue; if (HasBit(v->grf_cache.cache_valid, cache_entries[i][1])) continue;
bool stub; bool stub;
ro.GetVariable(&ro, cache_entries[i][0], 0, &stub); ro.GetScope(ro.scope)->GetVariable(cache_entries[i][0], 0, &stub);
} }
/* Make sure really all bits are set. */ /* Make sure really all bits are set. */

View File

@ -130,7 +130,7 @@ static inline void NewGenericResolver(ResolverObject *res, bool ai_callback)
res->GetTriggers = &GenericCallbackGetTriggers; res->GetTriggers = &GenericCallbackGetTriggers;
res->SetTriggers = &GenericCallbackSetTriggers; res->SetTriggers = &GenericCallbackSetTriggers;
res->GetVariable = ai_callback ? &GenericAiCallbackGetVariable : &GenericCallbackGetVariable; res->GetVariable = ai_callback ? &GenericAiCallbackGetVariable : &GenericCallbackGetVariable;
res->ResolveReal = &GenericCallbackResolveReal; res->ResolveRealMethod = &GenericCallbackResolveReal;
res->callback = CBID_NO_CALLBACK; res->callback = CBID_NO_CALLBACK;
res->callback_param1 = 0; res->callback_param1 = 0;

View File

@ -423,7 +423,7 @@ static void NewHouseResolver(ResolverObject *res, HouseID house_id, TileIndex ti
res->GetTriggers = HouseGetTriggers; res->GetTriggers = HouseGetTriggers;
res->SetTriggers = HouseSetTriggers; res->SetTriggers = HouseSetTriggers;
res->GetVariable = HouseGetVariable; res->GetVariable = HouseGetVariable;
res->ResolveReal = HouseResolveReal; res->ResolveRealMethod = HouseResolveReal;
res->StorePSA = HouseStorePSA; res->StorePSA = HouseStorePSA;
res->u.house.tile = tile; res->u.house.tile = tile;

View File

@ -410,7 +410,7 @@ static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *i
res->GetTriggers = IndustryGetTriggers; res->GetTriggers = IndustryGetTriggers;
res->SetTriggers = IndustrySetTriggers; res->SetTriggers = IndustrySetTriggers;
res->GetVariable = IndustryGetVariable; res->GetVariable = IndustryGetVariable;
res->ResolveReal = IndustryResolveReal; res->ResolveRealMethod = IndustryResolveReal;
res->StorePSA = IndustryStorePSA; res->StorePSA = IndustryStorePSA;
res->u.industry.tile = tile; res->u.industry.tile = tile;

View File

@ -176,7 +176,7 @@ static void NewIndustryTileResolver(ResolverObject *res, IndustryGfx gfx, TileIn
res->GetTriggers = IndustryTileGetTriggers; res->GetTriggers = IndustryTileGetTriggers;
res->SetTriggers = IndustryTileSetTriggers; res->SetTriggers = IndustryTileSetTriggers;
res->GetVariable = IndustryTileGetVariable; res->GetVariable = IndustryTileGetVariable;
res->ResolveReal = IndustryTileResolveReal; res->ResolveRealMethod = IndustryTileResolveReal;
res->StorePSA = IndustryTileStorePSA; res->StorePSA = IndustryTileStorePSA;
res->u.industry.tile = tile; res->u.industry.tile = tile;

View File

@ -398,7 +398,7 @@ static void NewObjectResolver(ResolverObject *res, const ObjectSpec *spec, Objec
res->GetTriggers = ObjectGetTriggers; res->GetTriggers = ObjectGetTriggers;
res->SetTriggers = ObjectSetTriggers; res->SetTriggers = ObjectSetTriggers;
res->GetVariable = ObjectGetVariable; res->GetVariable = ObjectGetVariable;
res->ResolveReal = ObjectResolveReal; res->ResolveRealMethod = ObjectResolveReal;
res->StorePSA = ObjectStorePSA; res->StorePSA = ObjectStorePSA;
res->u.object.o = o; res->u.object.o = o;

View File

@ -83,7 +83,7 @@ static inline void NewRailTypeResolver(ResolverObject *res, TileIndex tile, Tile
res->GetTriggers = &RailTypeGetTriggers; res->GetTriggers = &RailTypeGetTriggers;
res->SetTriggers = &RailTypeSetTriggers; res->SetTriggers = &RailTypeSetTriggers;
res->GetVariable = &RailTypeGetVariable; res->GetVariable = &RailTypeGetVariable;
res->ResolveReal = &RailTypeResolveReal; res->ResolveRealMethod = &RailTypeResolveReal;
res->u.routes.tile = tile; res->u.routes.tile = tile;
res->u.routes.context = context; res->u.routes.context = context;

View File

@ -10,6 +10,7 @@
/** @file newgrf_spritegroup.cpp Handling of primarily NewGRF action 2. */ /** @file newgrf_spritegroup.cpp Handling of primarily NewGRF action 2. */
#include "stdafx.h" #include "stdafx.h"
#include "debug.h"
#include "newgrf_spritegroup.h" #include "newgrf_spritegroup.h"
#include "core/pool_func.hpp" #include "core/pool_func.hpp"
@ -36,7 +37,7 @@ RandomizedSpriteGroup::~RandomizedSpriteGroup()
TemporaryStorageArray<int32, 0x110> _temp_store; TemporaryStorageArray<int32, 0x110> _temp_store;
static inline uint32 GetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available) static inline uint32 GetVariable(const ResolverObject *object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
{ {
/* First handle variables common with Action7/9/D */ /* First handle variables common with Action7/9/D */
uint32 value; uint32 value;
@ -49,7 +50,7 @@ static inline uint32 GetVariable(const ResolverObject *object, byte variable, ui
case 0x18: return object->callback_param2; case 0x18: return object->callback_param2;
case 0x1C: return object->last_value; case 0x1C: return object->last_value;
case 0x5F: return (object->GetRandomBits(object) << 8) | object->GetTriggers(object); case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
case 0x7D: return _temp_store.GetValue(parameter); case 0x7D: return _temp_store.GetValue(parameter);
@ -58,10 +59,119 @@ static inline uint32 GetVariable(const ResolverObject *object, byte variable, ui
return object->grffile->GetParam(parameter); return object->grffile->GetParam(parameter);
/* Not a common variable, so evalute the feature specific variables */ /* Not a common variable, so evalute the feature specific variables */
default: return object->GetVariable(object, variable, parameter, available); default: return scope->GetVariable(variable, parameter, available);
} }
} }
ScopeResolver::ScopeResolver(ResolverObject *ro)
{
this->ro = ro;
}
ScopeResolver::~ScopeResolver() {}
/**
* Get a few random bits. Default implementation has no random bits.
* @return Random bits.
*/
/* virtual */ uint32 ScopeResolver::GetRandomBits() const
{
return 0;
}
/**
* Get the triggers. Base class returns \c 0 to prevent trouble.
* @return The triggers.
*/
/* virtual */ uint32 ScopeResolver::GetTriggers() const
{
return 0;
}
/**
* Set the triggers. Base class implementation does nothing.
* @param triggers Triggers to set.
*/
/* virtual */ void ScopeResolver::SetTriggers(int triggers) const {}
/**
* Get a variable value. Default implementation has no available variables.
* @param variable Variable to read
* @param parameter Parameter for 60+x variables
* @param[out] available Set to false, in case the variable does not exist.
* @return Value
*/
/* virtual */ uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
{
DEBUG(grf, 1, "Unhandled scope variable 0x%X", variable);
*available = false;
return UINT_MAX;
}
/**
* Store a value into the persistent storage area (PSA). Default implementation does nothing (for newgrf classes without storage).
* @param pos Position to store into.
* @param value Value to store.
*/
/* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
TempScopeResolver::TempScopeResolver(ResolverObject *ro) : ScopeResolver(ro) {}
/* virtual */ uint32 TempScopeResolver::GetRandomBits() const
{
return this->ro->GetRandomBits(this->ro);
}
/* virtual */ uint32 TempScopeResolver::GetTriggers() const
{
return this->ro->GetTriggers(this->ro);
}
/* virtual */ void TempScopeResolver::SetTriggers(int triggers) const
{
this->ro->SetTriggers(this->ro, triggers);
}
/* virtual */ uint32 TempScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
{
return this->ro->GetVariable(this->ro, variable, parameter, available);
}
/* virtual */ void TempScopeResolver::StorePSA(uint reg, int32 value)
{
if (this->ro->StorePSA != NULL) this->ro->StorePSA(this->ro, reg, value);
}
ResolverObject::ResolverObject() : temp_scope(this) {} // XXX Temporary
ResolverObject::ResolverObject(const GRFFile *grffile, CallbackID callback, uint32 callback_param1, uint32 callback_param2) : temp_scope(this)
{
this->callback = callback;
this->callback_param1 = callback_param1;
this->callback_param2 = callback_param2;
this->ResetState();
this->grffile = grffile;
}
ResolverObject::~ResolverObject() {}
/* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
{
return this->ResolveRealMethod(this, group);
}
/**
* Get a specific ScopeResolver.
* @param scope Scope to return.
* @param relative Additional parameter for #VSG_SCOPE_RELATIVE.
* @return ScopeResolver.
*/
/* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
{
return &this->temp_scope;
}
/** /**
* Rotate val rot times to the right * Rotate val rot times to the right
@ -111,7 +221,7 @@ static U EvalAdjustT(const DeterministicSpriteGroupAdjust *adjust, ResolverObjec
case DSGA_OP_XOR: return last_value ^ value; case DSGA_OP_XOR: return last_value ^ value;
case DSGA_OP_STO: _temp_store.StoreValue((U)value, (S)last_value); return last_value; case DSGA_OP_STO: _temp_store.StoreValue((U)value, (S)last_value); return last_value;
case DSGA_OP_RST: return value; case DSGA_OP_RST: return value;
case DSGA_OP_STOP: if (object->StorePSA != NULL) object->StorePSA(object, (U)value, (S)last_value); return last_value; case DSGA_OP_STOP: object->GetScope(object->scope)->StorePSA((U)value, (S)last_value); return last_value;
case DSGA_OP_ROR: return RotateRight(last_value, value); 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_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); case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
@ -148,9 +258,9 @@ const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject *object) con
* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */ * Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
object->scope = this->var_scope; object->scope = this->var_scope;
} else if (adjust->variable == 0x7B) { } else if (adjust->variable == 0x7B) {
value = GetVariable(object, adjust->parameter, last_value, &available); value = GetVariable(object, object->GetScope(this->var_scope), adjust->parameter, last_value, &available);
} else { } else {
value = GetVariable(object, adjust->variable, adjust->parameter, &available); value = GetVariable(object, object->GetScope(this->var_scope), adjust->variable, adjust->parameter, &available);
} }
if (!available) { if (!available) {
@ -190,16 +300,14 @@ const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject *object) con
const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const
{ {
uint32 mask;
byte index;
object->scope = this->var_scope; object->scope = this->var_scope;
object->count = this->count; object->count = this->count;
ScopeResolver *scope = object->GetScope(this->var_scope, this->count);
if (object->trigger != 0) { if (object->trigger != 0) {
/* Handle triggers */ /* Handle triggers */
/* Magic code that may or may not do the right things... */ /* Magic code that may or may not do the right things... */
byte waiting_triggers = object->GetTriggers(object); byte waiting_triggers = scope->GetTriggers();
byte match = this->triggers & (waiting_triggers | object->trigger); byte match = this->triggers & (waiting_triggers | object->trigger);
bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers); bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
@ -210,11 +318,11 @@ const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const
waiting_triggers |= object->trigger; waiting_triggers |= object->trigger;
} }
object->SetTriggers(object, waiting_triggers); scope->SetTriggers(waiting_triggers);
} }
mask = (this->num_groups - 1) << this->lowest_randbit; uint32 mask = (this->num_groups - 1) << this->lowest_randbit;
index = (object->GetRandomBits(object) & mask) >> this->lowest_randbit; byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
return SpriteGroup::Resolve(this->groups[index], object); return SpriteGroup::Resolve(this->groups[index], object);
} }
@ -222,7 +330,7 @@ const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const
const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject *object) const const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject *object) const
{ {
return object->ResolveReal(object, this); return object->ResolveReal(this);
} }
/** /**

View File

@ -301,8 +301,40 @@ struct IndustryProductionSpriteGroup : SpriteGroup {
uint8 again; uint8 again;
}; };
struct ResolverObject;
struct ScopeResolver {
ResolverObject *ro;
ScopeResolver(ResolverObject *ro);
virtual ~ScopeResolver();
virtual uint32 GetRandomBits() const;
virtual uint32 GetTriggers() const;
virtual void SetTriggers(int triggers) const;
virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
virtual void StorePSA(uint reg, int32 value);
};
struct TempScopeResolver : public ScopeResolver {
TempScopeResolver(ResolverObject *ro);
virtual uint32 GetRandomBits() const;
virtual uint32 GetTriggers() const;
virtual void SetTriggers(int triggers) const;
virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
virtual void StorePSA(uint reg, int32 value);
};
struct ResolverObject { struct ResolverObject {
ResolverObject();
ResolverObject(const GRFFile *grffile, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
virtual ~ResolverObject();
TempScopeResolver temp_scope; ///< Temporary scope resolver to refer back to the methods of #ResolverObject.
CallbackID callback; CallbackID callback;
uint32 callback_param1; uint32 callback_param1;
uint32 callback_param2; uint32 callback_param2;
@ -382,9 +414,13 @@ struct ResolverObject {
uint32 (*GetTriggers)(const struct ResolverObject*); uint32 (*GetTriggers)(const struct ResolverObject*);
void (*SetTriggers)(const struct ResolverObject*, int); void (*SetTriggers)(const struct ResolverObject*, int);
uint32 (*GetVariable)(const struct ResolverObject *object, byte variable, uint32 parameter, bool *available); uint32 (*GetVariable)(const struct ResolverObject *object, byte variable, uint32 parameter, bool *available);
const SpriteGroup *(*ResolveReal)(const struct ResolverObject*, const RealSpriteGroup*); const SpriteGroup *(*ResolveRealMethod)(const struct ResolverObject*, const RealSpriteGroup*);
void (*StorePSA)(struct ResolverObject*, uint, int32); void (*StorePSA)(struct ResolverObject*, uint, int32);
virtual const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
virtual ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0);
/** /**
* Returns the OR-sum of all bits that need reseeding * Returns the OR-sum of all bits that need reseeding
* independent of the scope they were accessed with. * independent of the scope they were accessed with.

View File

@ -550,7 +550,7 @@ static void NewStationResolver(ResolverObject *res, const StationSpec *statspec,
res->GetTriggers = StationGetTriggers; res->GetTriggers = StationGetTriggers;
res->SetTriggers = StationSetTriggers; res->SetTriggers = StationSetTriggers;
res->GetVariable = StationGetVariable; res->GetVariable = StationGetVariable;
res->ResolveReal = StationResolveReal; res->ResolveRealMethod = StationResolveReal;
res->StorePSA = StationStorePSA; res->StorePSA = StationStorePSA;
res->u.station.st = st; res->u.station.st = st;