Changeset - r25642:6743de9f1df8
[Not reviewed]
master
0 9 0
Peter Nelson - 3 years ago 2021-06-04 11:52:00
peter1138@openttd.org
Codechange: Deduplicate ResolveReal() for resolvers that don't use real sprite groups.

This may change behaviour when multiple loading/loaded stages are provided, as the various copies checked in different orders, however only one result is expected in these cases anyway.
9 files changed with 3 insertions and 62 deletions:
0 comments (0 inline, 0 general)
src/newgrf_airport.cpp
Show inline comments
 
@@ -48,26 +48,24 @@ struct AirportResolverObject : public Re
 

	
 
	AirportResolverObject(TileIndex tile, Station *st, byte airport_id, byte layout,
 
			CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
 

	
 
	ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
 
	{
 
		switch (scope) {
 
			case VSG_SCOPE_SELF: return &this->airport_scope;
 
			default: return ResolverObject::GetScope(scope, relative);
 
		}
 
	}
 

	
 
	const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
 

	
 
	GrfSpecFeature GetFeature() const override;
 
	uint32 GetDebugID() const override;
 
};
 

	
 
/**
 
 * Reset airport classes to their default state.
 
 * This includes initialising the defaults classes with an empty
 
 * entry, for standard airports.
 
 */
 
template <typename Tspec, typename Tid, Tid Tmax>
 
/* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
 
{
 
@@ -210,34 +208,24 @@ void AirportOverrideManager::SetEntitySp
 

	
 
	switch (variable) {
 
		/* Get a variable from the persistent storage */
 
		case 0x7C: return (this->st->airport.psa != nullptr) ? this->st->airport.psa->GetValue(parameter) : 0;
 

	
 
		case 0xF0: return this->st->facilities;
 
		case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
 
	}
 

	
 
	return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
 
}
 

	
 
/* virtual */ const SpriteGroup *AirportResolverObject::ResolveReal(const RealSpriteGroup *group) const
 
{
 
	/* Airport action 2s should always have only 1 "loaded" state, but some
 
	 * times things don't follow the spec... */
 
	if (!group->loaded.empty())  return group->loaded[0];
 
	if (!group->loading.empty()) return group->loading[0];
 

	
 
	return nullptr;
 
}
 

	
 
GrfSpecFeature AirportResolverObject::GetFeature() const
 
{
 
	return GSF_AIRPORTS;
 
}
 

	
 
uint32 AirportResolverObject::GetDebugID() const
 
{
 
	return AirportSpec::Get(this->airport_scope.airport_id)->grf_prop.local_id;
 
}
 

	
 
/* virtual */ uint32 AirportScopeResolver::GetRandomBits() const
 
{
src/newgrf_canal.cpp
Show inline comments
 
@@ -40,26 +40,24 @@ struct CanalResolverObject : public Reso
 

	
 
	CanalResolverObject(CanalFeature feature, TileIndex tile,
 
			CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
 

	
 
	ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
 
	{
 
		switch (scope) {
 
			case VSG_SCOPE_SELF: return &this->canal_scope;
 
			default: return ResolverObject::GetScope(scope, relative);
 
		}
 
	}
 

	
 
	const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
 

	
 
	GrfSpecFeature GetFeature() const override;
 
	uint32 GetDebugID() const override;
 
};
 

	
 
/* virtual */ uint32 CanalScopeResolver::GetRandomBits() const
 
{
 
	/* Return random bits only for water tiles, not station tiles */
 
	return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
 
}
 

	
 
/* virtual */ uint32 CanalScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
 
{
 
@@ -99,32 +97,24 @@ struct CanalResolverObject : public Reso
 
		}
 

	
 
		/* Random data for river or canal tiles, otherwise zero */
 
		case 0x83: return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
 
	}
 

	
 
	DEBUG(grf, 1, "Unhandled canal variable 0x%02X", variable);
 

	
 
	*available = false;
 
	return UINT_MAX;
 
}
 

	
 

	
 
/* virtual */ const SpriteGroup *CanalResolverObject::ResolveReal(const RealSpriteGroup *group) const
 
{
 
	if (group->loaded.empty()) return nullptr;
 

	
 
	return group->loaded[0];
 
}
 

	
 
GrfSpecFeature CanalResolverObject::GetFeature() const
 
{
 
	return GSF_CANALS;
 
}
 

	
 
uint32 CanalResolverObject::GetDebugID() const
 
{
 
	return this->feature;
 
}
 

	
 
/**
 
 * Canal resolver constructor.
src/newgrf_cargo.cpp
Show inline comments
 
@@ -10,40 +10,28 @@
 
#include "stdafx.h"
 
#include "debug.h"
 
#include "newgrf_spritegroup.h"
 

	
 
#include "safeguards.h"
 

	
 
/** Resolver of cargo. */
 
struct CargoResolverObject : public ResolverObject {
 
	const CargoSpec *cargospec;
 

	
 
	CargoResolverObject(const CargoSpec *cs, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
 

	
 
	const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
 

	
 
	GrfSpecFeature GetFeature() const override;
 
	uint32 GetDebugID() const override;
 
};
 

	
 
/* virtual */ const SpriteGroup *CargoResolverObject::ResolveReal(const RealSpriteGroup *group) const
 
{
 
	/* Cargo action 2s should always have only 1 "loaded" state, but some
 
	 * times things don't follow the spec... */
 
	if (!group->loaded.empty())  return group->loaded[0];
 
	if (!group->loading.empty()) return group->loading[0];
 

	
 
	return nullptr;
 
}
 

	
 
GrfSpecFeature CargoResolverObject::GetFeature() const
 
{
 
	return GSF_CARGOES;
 
}
 

	
 
uint32 CargoResolverObject::GetDebugID() const
 
{
 
	return this->cargospec->label;
 
}
 

	
 
/**
 
 * Constructor of the cargo resolver.
src/newgrf_generic.cpp
Show inline comments
 
@@ -54,26 +54,24 @@ struct GenericResolverObject : public Re
 
	GenericScopeResolver generic_scope;
 

	
 
	GenericResolverObject(bool ai_callback, CallbackID callback = CBID_NO_CALLBACK);
 

	
 
	ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
 
	{
 
		switch (scope) {
 
			case VSG_SCOPE_SELF: return &this->generic_scope;
 
			default: return ResolverObject::GetScope(scope, relative);
 
		}
 
	}
 

	
 
	const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
 

	
 
	GrfSpecFeature GetFeature() const override
 
	{
 
		return (GrfSpecFeature)this->generic_scope.feature;
 
	}
 

	
 
	uint32 GetDebugID() const override
 
	{
 
		return 0;
 
	}
 
};
 

	
 
struct GenericCallback {
 
@@ -138,32 +136,24 @@ void AddGenericCallback(uint8 feature, c
 
			case 0x88: return this->station_size;
 

	
 
			default: break;
 
		}
 
	}
 

	
 
	DEBUG(grf, 1, "Unhandled generic feature variable 0x%02X", variable);
 

	
 
	*available = false;
 
	return UINT_MAX;
 
}
 

	
 

	
 
/* virtual */ const SpriteGroup *GenericResolverObject::ResolveReal(const RealSpriteGroup *group) const
 
{
 
	if (group->loaded.empty()) return nullptr;
 

	
 
	return group->loaded[0];
 
}
 

	
 
/**
 
 * Generic resolver.
 
 * @param ai_callback Callback comes from the AI.
 
 * @param callback Callback ID.
 
 */
 
GenericResolverObject::GenericResolverObject(bool ai_callback, CallbackID callback) : ResolverObject(nullptr, callback), generic_scope(*this, ai_callback)
 
{
 
}
 

	
 

	
 
/**
 
 * Follow a generic feature callback list and return the first successful
src/newgrf_railtype.cpp
Show inline comments
 
@@ -49,31 +49,24 @@
 
				t = ClosestTownFromTile(this->tile, UINT_MAX);
 
			}
 
			return t != nullptr ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
 
		}
 
	}
 

	
 
	DEBUG(grf, 1, "Unhandled rail type tile variable 0x%X", variable);
 

	
 
	*available = false;
 
	return UINT_MAX;
 
}
 

	
 
/* virtual */ const SpriteGroup *RailTypeResolverObject::ResolveReal(const RealSpriteGroup *group) const
 
{
 
	if (!group->loading.empty()) return group->loading[0];
 
	if (!group->loaded.empty())  return group->loaded[0];
 
	return nullptr;
 
}
 

	
 
GrfSpecFeature RailTypeResolverObject::GetFeature() const
 
{
 
	return GSF_RAILTYPES;
 
}
 

	
 
uint32 RailTypeResolverObject::GetDebugID() const
 
{
 
	return this->railtype_scope.rti->label;
 
}
 

	
 
/**
 
 * Resolver object for rail types.
src/newgrf_railtype.h
Show inline comments
 
@@ -40,25 +40,23 @@ struct RailTypeResolverObject : public R
 
	RailTypeScopeResolver railtype_scope; ///< Resolver for the railtype scope.
 

	
 
	RailTypeResolverObject(const RailtypeInfo *rti, TileIndex tile, TileContext context, RailTypeSpriteGroup rtsg, uint32 param1 = 0, uint32 param2 = 0);
 

	
 
	ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
 
	{
 
		switch (scope) {
 
			case VSG_SCOPE_SELF: return &this->railtype_scope;
 
			default:             return ResolverObject::GetScope(scope, relative);
 
		}
 
	}
 

	
 
	const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
 

	
 
	GrfSpecFeature GetFeature() const override;
 
	uint32 GetDebugID() const override;
 
};
 

	
 
SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context = TCX_NORMAL, uint *num_results = nullptr);
 
SpriteID GetCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui = false);
 

	
 
RailType GetRailTypeTranslation(uint8 railtype, const GRFFile *grffile);
 
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile);
 

	
 
#endif /* NEWGRF_RAILTYPE_H */
src/newgrf_roadtype.cpp
Show inline comments
 
@@ -49,31 +49,24 @@
 
				t = ClosestTownFromTile(this->tile, UINT_MAX);
 
			}
 
			return t != nullptr ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
 
		}
 
	}
 

	
 
	DEBUG(grf, 1, "Unhandled road type tile variable 0x%X", variable);
 

	
 
	*available = false;
 
	return UINT_MAX;
 
}
 

	
 
/* virtual */ const SpriteGroup *RoadTypeResolverObject::ResolveReal(const RealSpriteGroup *group) const
 
{
 
	if (!group->loading.empty()) return group->loading[0];
 
	if (!group->loaded.empty())  return group->loaded[0];
 
	return nullptr;
 
}
 

	
 
GrfSpecFeature RoadTypeResolverObject::GetFeature() const
 
{
 
	RoadType rt = GetRoadTypeByLabel(this->roadtype_scope.rti->label, false);
 
	switch (GetRoadTramType(rt)) {
 
		case RTT_ROAD: return GSF_ROADTYPES;
 
		case RTT_TRAM: return GSF_TRAMTYPES;
 
		default: return GSF_INVALID;
 
	}
 
}
 

	
 
uint32 RoadTypeResolverObject::GetDebugID() const
 
{
src/newgrf_roadtype.h
Show inline comments
 
@@ -31,24 +31,22 @@ struct RoadTypeResolverObject : public R
 
	RoadTypeScopeResolver roadtype_scope; ///< Resolver for the roadtype scope.
 

	
 
	RoadTypeResolverObject(const RoadTypeInfo *rti, TileIndex tile, TileContext context, RoadTypeSpriteGroup rtsg, uint32 param1 = 0, uint32 param2 = 0);
 

	
 
	ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
 
	{
 
		switch (scope) {
 
			case VSG_SCOPE_SELF: return &this->roadtype_scope;
 
			default:             return ResolverObject::GetScope(scope, relative);
 
		}
 
	}
 

	
 
	const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
 

	
 
	GrfSpecFeature GetFeature() const override;
 
	uint32 GetDebugID() const override;
 
};
 

	
 
SpriteID GetCustomRoadSprite(const RoadTypeInfo *rti, TileIndex tile, RoadTypeSpriteGroup rtsg, TileContext context = TCX_NORMAL, uint *num_results = nullptr);
 

	
 
RoadType GetRoadTypeTranslation(RoadTramType rtt, uint8 tracktype, const GRFFile *grffile);
 
uint8 GetReverseRoadTypeTranslation(RoadType roadtype, const GRFFile *grffile);
 

	
 
#endif /* NEWGRF_ROADTYPE_H */
src/newgrf_spritegroup.cpp
Show inline comments
 
@@ -115,24 +115,27 @@ static inline uint32 GetVariable(const R
 
 * @param reg Position to store into.
 
 * @param value Value to store.
 
 */
 
/* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
 

	
 
/**
 
 * Get the real sprites of the grf.
 
 * @param group Group to get.
 
 * @return The available sprite group.
 
 */
 
/* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
 
{
 
	if (!group->loaded.empty())  return group->loaded[0];
 
	if (!group->loading.empty()) return group->loading[0];
 

	
 
	return nullptr;
 
}
 

	
 
/**
 
 * Get a resolver for the \a scope.
 
 * @param scope Scope to return.
 
 * @param relative Additional parameter for #VSG_SCOPE_RELATIVE.
 
 * @return The resolver for the requested scope.
 
 */
 
/* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
 
{
 
	return &this->default_scope;
0 comments (0 inline, 0 general)