Changeset - r11991:9a2b087e564c
[Not reviewed]
master
0 10 0
rubidium - 15 years ago 2009-05-23 15:25:52
rubidium@openttd.org
(svn r16402) -Codechange: make Resolve a function of SpriteGroup
10 files changed with 67 insertions and 56 deletions:
0 comments (0 inline, 0 general)
src/newgrf_canal.cpp
Show inline comments
 
@@ -96,7 +96,7 @@ SpriteID GetCanalSprite(CanalFeature fea
 

	
 
	NewCanalResolver(&object, tile, _water_feature[feature].grffile);
 

	
 
	group = Resolve(_water_feature[feature].group, &object);
 
	group = SpriteGroup::Resolve(_water_feature[feature].group, &object);
 
	if (group == NULL) return 0;
 

	
 
	return group->GetResult();
src/newgrf_cargo.cpp
Show inline comments
 
@@ -74,7 +74,7 @@ SpriteID GetCustomCargoSprite(const Carg
 

	
 
	NewCargoResolver(&object, cs);
 

	
 
	group = Resolve(cs->group, &object);
 
	group = SpriteGroup::Resolve(cs->group, &object);
 
	if (group == NULL) return 0;
 

	
 
	return group->GetResult();
 
@@ -91,7 +91,7 @@ uint16 GetCargoCallback(CallbackID callb
 
	object.callback_param1 = param1;
 
	object.callback_param2 = param2;
 

	
 
	group = Resolve(cs->group, &object);
 
	group = SpriteGroup::Resolve(cs->group, &object);
 
	if (group == NULL) return CALLBACK_FAILED;
 

	
 
	return group->GetCallbackResult();
src/newgrf_engine.cpp
Show inline comments
 
@@ -912,7 +912,7 @@ SpriteID GetCustomEngineSprite(EngineID 
 

	
 
	NewVehicleResolver(&object, engine, v);
 

	
 
	group = Resolve(GetVehicleSpriteGroup(engine, v), &object);
 
	group = SpriteGroup::Resolve(GetVehicleSpriteGroup(engine, v), &object);
 
	if (group == NULL || group->GetNumResults() == 0) return 0;
 

	
 
	return group->GetResult() + (direction % group->GetNumResults());
 
@@ -934,7 +934,7 @@ SpriteID GetRotorOverrideSprite(EngineID
 
	object.info_view = info_view;
 

	
 
	const SpriteGroup *group = GetWagonOverrideSpriteSet(engine, CT_DEFAULT, engine);
 
	group = Resolve(group, &object);
 
	group = SpriteGroup::Resolve(group, &object);
 

	
 
	if (group == NULL || group->GetNumResults() == 0) return 0;
 

	
 
@@ -975,7 +975,7 @@ uint16 GetVehicleCallback(CallbackID cal
 
	object.callback_param1 = param1;
 
	object.callback_param2 = param2;
 

	
 
	group = Resolve(GetVehicleSpriteGroup(engine, v, false), &object);
 
	group = SpriteGroup::Resolve(GetVehicleSpriteGroup(engine, v, false), &object);
 
	if (group == NULL) return CALLBACK_FAILED;
 

	
 
	return group->GetCallbackResult();
 
@@ -1004,7 +1004,7 @@ uint16 GetVehicleCallbackParent(Callback
 

	
 
	object.u.vehicle.parent = parent;
 

	
 
	group = Resolve(GetVehicleSpriteGroup(engine, v, false), &object);
 
	group = SpriteGroup::Resolve(GetVehicleSpriteGroup(engine, v, false), &object);
 
	if (group == NULL) return CALLBACK_FAILED;
 

	
 
	return group->GetCallbackResult();
 
@@ -1043,7 +1043,7 @@ static void DoTriggerVehicle(Vehicle *v,
 
	object.callback = CBID_RANDOM_TRIGGER;
 
	object.trigger = trigger;
 

	
 
	group = Resolve(GetVehicleSpriteGroup(v->engine_type, v), &object);
 
	group = SpriteGroup::Resolve(GetVehicleSpriteGroup(v->engine_type, v), &object);
 
	if (group == NULL) return;
 

	
 
	new_random_bits = Random();
src/newgrf_generic.cpp
Show inline comments
 
@@ -139,7 +139,7 @@ static uint16 GetGenericCallbackResult(u
 
	/* Test each feature callback sprite group. */
 
	for (GenericCallbackList::const_iterator it = _gcl[feature].begin(); it != _gcl[feature].end(); ++it) {
 
		const SpriteGroup *group = it->group;
 
		group = Resolve(group, object);
 
		group = SpriteGroup::Resolve(group, object);
 
		if (group == NULL) continue;
 

	
 
		/* Return NewGRF file if necessary */
src/newgrf_house.cpp
Show inline comments
 
@@ -354,7 +354,7 @@ uint16 GetHouseCallback(CallbackID callb
 
	object.callback_param1 = param1;
 
	object.callback_param2 = param2;
 

	
 
	group = Resolve(GetHouseSpecs(house_id)->spritegroup, &object);
 
	group = SpriteGroup::Resolve(GetHouseSpecs(house_id)->spritegroup, &object);
 
	if (group == NULL) return CALLBACK_FAILED;
 

	
 
	return group->GetCallbackResult();
 
@@ -422,7 +422,7 @@ void DrawNewHouseTile(TileInfo *ti, Hous
 

	
 
	NewHouseResolver(&object, house_id, ti->tile, GetTownByTile(ti->tile));
 

	
 
	group = Resolve(hs->spritegroup, &object);
 
	group = SpriteGroup::Resolve(hs->spritegroup, &object);
 
	if (group == NULL || group->type != SGT_TILELAYOUT) {
 
		/* XXX: This is for debugging purposes really, and shouldn't stay. */
 
		DrawGroundSprite(SPR_SHADOW_CELL, PAL_NONE);
 
@@ -601,7 +601,7 @@ static void DoTriggerHouse(TileIndex til
 
	object.callback = CBID_RANDOM_TRIGGER;
 
	object.trigger = trigger;
 

	
 
	const SpriteGroup *group = Resolve(hs->spritegroup, &object);
 
	const SpriteGroup *group = SpriteGroup::Resolve(hs->spritegroup, &object);
 
	if (group == NULL) return;
 

	
 
	byte new_random_bits = Random();
src/newgrf_industries.cpp
Show inline comments
 
@@ -387,7 +387,7 @@ uint16 GetIndustryCallback(CallbackID ca
 
	object.callback_param1 = param1;
 
	object.callback_param2 = param2;
 

	
 
	group = Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
 
	group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
 
	if (group == NULL) return CALLBACK_FAILED;
 

	
 
	return group->GetCallbackResult();
 
@@ -461,7 +461,7 @@ bool CheckIfCallBackAllowsCreation(TileI
 
	object.callback = CBID_INDUSTRY_LOCATION;
 
	_industry_creation_random_bits = seed;
 

	
 
	group = Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
 
	group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
 

	
 
	/* Unlike the "normal" cases, not having a valid result means we allow
 
	 * the building of the industry, as that's how it's done in TTDP. */
 
@@ -531,7 +531,7 @@ void IndustryProductionCallback(Industry
 
		}
 

	
 
		SB(object.callback_param2, 8, 16, loop);
 
		const SpriteGroup *tgroup = Resolve(spec->grf_prop.spritegroup, &object);
 
		const SpriteGroup *tgroup = SpriteGroup::Resolve(spec->grf_prop.spritegroup, &object);
 
		if (tgroup == NULL || tgroup->type != SGT_INDUSTRY_PRODUCTION) break;
 
		const IndustryProductionSpriteGroup *group = (const IndustryProductionSpriteGroup *)tgroup;
 

	
src/newgrf_industrytiles.cpp
Show inline comments
 
@@ -221,7 +221,7 @@ uint16 GetIndustryTileCallback(CallbackI
 
	object.callback_param1 = param1;
 
	object.callback_param2 = param2;
 

	
 
	group = Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup, &object);
 
	group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup, &object);
 
	if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
 

	
 
	return group->GetCallbackResult();
 
@@ -245,7 +245,7 @@ bool DrawNewIndustryTile(TileInfo *ti, I
 

	
 
	NewIndustryTileResolver(&object, gfx, ti->tile, i);
 

	
 
	group = Resolve(inds->grf_prop.spritegroup, &object);
 
	group = SpriteGroup::Resolve(inds->grf_prop.spritegroup, &object);
 
	if (group == NULL || group->type != SGT_TILELAYOUT) {
 
		return false;
 
	} else {
 
@@ -416,7 +416,7 @@ static void DoTriggerIndustryTile(TileIn
 
	object.callback = CBID_RANDOM_TRIGGER;
 
	object.trigger = trigger;
 

	
 
	const SpriteGroup *group = Resolve(itspec->grf_prop.spritegroup, &object);
 
	const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup, &object);
 
	if (group == NULL) return;
 

	
 
	byte new_random_bits = Random();
src/newgrf_spritegroup.cpp
Show inline comments
 
@@ -121,23 +121,23 @@ static U EvalAdjustT(const Deterministic
 
}
 

	
 

	
 
static inline const SpriteGroup *ResolveVariable(const DeterministicSpriteGroup *group, ResolverObject *object)
 
const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject *object) const
 
{
 
	uint32 last_value = 0;
 
	uint32 value = 0;
 
	uint i;
 

	
 
	object->scope = group->var_scope;
 
	object->scope = this->var_scope;
 

	
 
	for (i = 0; i < group->num_adjusts; i++) {
 
		DeterministicSpriteGroupAdjust *adjust = &group->adjusts[i];
 
	for (i = 0; i < this->num_adjusts; i++) {
 
		DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
 

	
 
		/* Try to get the variable. We shall assume it is available, unless told otherwise. */
 
		bool available = true;
 
		if (adjust->variable == 0x7E) {
 
			ResolverObject subobject = *object;
 
			subobject.procedure_call = true;
 
			const SpriteGroup *subgroup = Resolve(adjust->subroutine, &subobject);
 
			const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust->subroutine, &subobject);
 
			if (subgroup == NULL) {
 
				value = CALLBACK_FAILED;
 
			} else {
 
@@ -150,10 +150,10 @@ static inline const SpriteGroup *Resolve
 
		if (!available) {
 
			/* Unsupported property: skip further processing and return either
 
			 * the group from the first range or the default group. */
 
			return Resolve(group->num_ranges > 0 ? group->ranges[0].group : group->default_group, object);
 
			return SpriteGroup::Resolve(this->num_ranges > 0 ? this->ranges[0].group : this->default_group, object);
 
		}
 

	
 
		switch (group->size) {
 
		switch (this->size) {
 
			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;
 
@@ -164,7 +164,7 @@ static inline const SpriteGroup *Resolve
 

	
 
	object->last_value = last_value;
 

	
 
	if (group->num_ranges == 0) {
 
	if (this->num_ranges == 0) {
 
		/* nvar == 0 is a special case -- we turn our value into a callback result */
 
		if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
 
		static CallbackResultSpriteGroup nvarzero(0);
 
@@ -172,34 +172,34 @@ static inline const SpriteGroup *Resolve
 
		return &nvarzero;
 
	}
 

	
 
	for (i = 0; i < group->num_ranges; i++) {
 
		if (group->ranges[i].low <= value && value <= group->ranges[i].high) {
 
			return Resolve(group->ranges[i].group, object);
 
	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);
 
		}
 
	}
 

	
 
	return Resolve(group->default_group, object);
 
	return SpriteGroup::Resolve(this->default_group, object);
 
}
 

	
 

	
 
static inline const SpriteGroup *ResolveRandom(const RandomizedSpriteGroup *group, ResolverObject *object)
 
const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const
 
{
 
	uint32 mask;
 
	byte index;
 

	
 
	object->scope = group->var_scope;
 
	object->count = group->count;
 
	object->scope = this->var_scope;
 
	object->count = this->count;
 

	
 
	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->triggers & (waiting_triggers | object->trigger);
 
		bool res = (group->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == group->triggers);
 
		byte match = this->triggers & (waiting_triggers | object->trigger);
 
		bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
 

	
 
		if (res) {
 
			waiting_triggers &= ~match;
 
			object->reseed |= (group->num_groups - 1) << group->lowest_randbit;
 
			object->reseed |= (this->num_groups - 1) << this->lowest_randbit;
 
		} else {
 
			waiting_triggers |= object->trigger;
 
		}
 
@@ -207,23 +207,14 @@ static inline const SpriteGroup *Resolve
 
		object->SetTriggers(object, waiting_triggers);
 
	}
 

	
 
	mask  = (group->num_groups - 1) << group->lowest_randbit;
 
	index = (object->GetRandomBits(object) & mask) >> group->lowest_randbit;
 
	mask  = (this->num_groups - 1) << this->lowest_randbit;
 
	index = (object->GetRandomBits(object) & mask) >> this->lowest_randbit;
 

	
 
	return Resolve(group->groups[index], object);
 
	return SpriteGroup::Resolve(this->groups[index], object);
 
}
 

	
 

	
 
/* ResolverObject (re)entry point */
 
const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject *object)
 
const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject *object) const
 
{
 
	/* 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 object->ResolveReal(object, (RealSpriteGroup *)group);
 
		case SGT_DETERMINISTIC: return ResolveVariable((DeterministicSpriteGroup *)group, object);
 
		case SGT_RANDOMIZED:    return ResolveRandom((RandomizedSpriteGroup *)group, object);
 
		default:                return group;
 
	return object->ResolveReal(object, this);
 
	}
 
}
src/newgrf_spritegroup.h
Show inline comments
 
@@ -50,6 +50,8 @@ extern SpriteGroupPool _spritegroup_pool
 
struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
 
protected:
 
	SpriteGroup(SpriteGroupType type) : type(type) {}
 
	/** Base sprite group resolver */
 
	virtual const SpriteGroup *Resolve(struct ResolverObject *object) const { return this; };
 

	
 
public:
 
	virtual ~SpriteGroup() {}
 
@@ -59,6 +61,20 @@ public:
 
	virtual SpriteID GetResult() const { return 0; }
 
	virtual byte GetNumResults() const { return 0; }
 
	virtual uint16 GetCallbackResult() const { return CALLBACK_FAILED; }
 

	
 
	/**
 
	 * ResolverObject (re)entry point.
 
	 * This cannot be made a call to a virtual function because virtual functions
 
	 * do not like NULL and checking for NULL *everywhere* is more cumbersome than
 
	 * this little helper function.
 
	 * @param group the group to resolve for
 
	 * @param object information needed to resolve the group
 
	 * @return the resolved group
 
	 */
 
	static const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject *object)
 
	{
 
		return group == NULL ? NULL : group->Resolve(object);
 
	}
 
};
 

	
 

	
 
@@ -79,6 +95,9 @@ struct RealSpriteGroup : SpriteGroup {
 
	byte num_loading;      ///< Number of loading groups
 
	const SpriteGroup **loaded;  ///< List of loaded groups (can be SpriteIDs or Callback results)
 
	const SpriteGroup **loading; ///< List of loading groups (can be SpriteIDs or Callback results)
 

	
 
protected:
 
	const SpriteGroup *Resolve(ResolverObject *object) const;
 
};
 

	
 
/* Shared by deterministic and random groups. */
 
@@ -159,6 +178,9 @@ struct DeterministicSpriteGroup : Sprite
 

	
 
	/* Dynamically allocated, this is the sole owner */
 
	const SpriteGroup *default_group;
 

	
 
protected:
 
	const SpriteGroup *Resolve(ResolverObject *object) const;
 
};
 

	
 
enum RandomizedSpriteGroupCompareMode {
 
@@ -180,6 +202,9 @@ struct RandomizedSpriteGroup : SpriteGro
 
	byte num_groups; ///< must be power of 2
 

	
 
	const SpriteGroup **groups; ///< Take the group with appropriate index:
 

	
 
protected:
 
	const SpriteGroup *Resolve(ResolverObject *object) const;
 
};
 

	
 

	
 
@@ -316,9 +341,4 @@ struct ResolverObject {
 
	ResolverObject() : procedure_call(false) { }
 
};
 

	
 

	
 
/* Base sprite group resolver */
 
const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject *object);
 

	
 

	
 
#endif /* NEWGRF_SPRITEGROUP_H */
src/newgrf_station.cpp
Show inline comments
 
@@ -633,7 +633,7 @@ static const SpriteGroup *ResolveStation
 
	/* Invalidate all cached vars */
 
	_svc.valid = 0;
 

	
 
	return Resolve(group, object);
 
	return SpriteGroup::Resolve(group, object);
 
}
 

	
 
SpriteID GetCustomStationRelocation(const StationSpec *statspec, const Station *st, TileIndex tile)
0 comments (0 inline, 0 general)