changeset 19753:c5819770a5c8 draft

(svn r24686) -Codechange: Add resolver classes for airport tiles.
author alberth <alberth@openttd.org>
date Sat, 10 Nov 2012 20:42:19 +0000
parents b77c9878dd17
children d99582593731
files src/newgrf_airporttiles.cpp src/newgrf_airporttiles.h src/table/newgrf_debug_data.h
diffstat 3 files changed, 63 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/src/newgrf_airporttiles.cpp
+++ b/src/newgrf_airporttiles.cpp
@@ -99,7 +99,7 @@
 }
 
 
-static const SpriteGroup *AirportTileResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
+/* virtual */ const SpriteGroup *AirportTileResolverObject::ResolveReal(const RealSpriteGroup *group) const
 {
 	/* AirportTile do not have 'real' groups. */
 	return NULL;
@@ -165,46 +165,39 @@
 	return 0xFF << 8 | ats->grf_prop.subst_id; // so just give him the substitute
 }
 
-static uint32 AirportTileGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
+/* virtual */ uint32 AirportTileScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
 {
-	const Station *st = object->u.airport.st;
-	TileIndex tile    = object->u.airport.tile;
-	assert(st != NULL);
-
-	if (object->scope == VSG_SCOPE_PARENT) {
-		DEBUG(grf, 1, "Parent scope for airport tiles unavailable");
-		*available = false;
-		return UINT_MAX;
-	}
+	assert(this->st != NULL);
 
 	extern uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile);
 
 	switch (variable) {
 		/* Terrain type */
-		case 0x41: return GetTerrainType(tile);
+		case 0x41: return GetTerrainType(this->tile);
 
 		/* Current town zone of the tile in the nearest town */
-		case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(tile, UINT_MAX), tile);
+		case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile, UINT_MAX), this->tile);
 
 		/* Position relative to most northern airport tile. */
-		case 0x43: return GetRelativePosition(tile, st->airport.tile);
+		case 0x43: return GetRelativePosition(this->tile, this->st->airport.tile);
 
 		/* Animation frame of tile */
-		case 0x44: return GetAnimationFrame(tile);
+		case 0x44: return GetAnimationFrame(this->tile);
 
 		/* Land info of nearby tiles */
-		case 0x60: return GetNearbyAirportTileInformation(parameter, tile, st->index, object->grffile->grf_version >= 8);
+		case 0x60: return GetNearbyAirportTileInformation(parameter, this->tile, this->st->index, this->ro->grffile->grf_version >= 8);
 
 		/* Animation stage of nearby tiles */
-		case 0x61:
-			tile = GetNearbyTile(parameter, tile);
-			if (st->TileBelongsToAirport(tile)) {
+		case 0x61: {
+			TileIndex tile = GetNearbyTile(parameter, this->tile);
+			if (this->st->TileBelongsToAirport(tile)) {
 				return GetAnimationFrame(tile);
 			}
 			return UINT_MAX;
+		}
 
 		/* Get airport tile ID at offset */
-		case 0x62: return GetAirportTileIDAtOffset(GetNearbyTile(parameter, tile), st, object->grffile->grfid);
+		case 0x62: return GetAirportTileIDAtOffset(GetNearbyTile(parameter, this->tile), this->st, this->ro->grffile->grfid);
 	}
 
 	DEBUG(grf, 1, "Unhandled airport tile variable 0x%X", variable);
@@ -213,46 +206,30 @@
 	return UINT_MAX;
 }
 
-static uint32 AirportTileGetRandomBits(const ResolverObject *object)
+/* virtual */ uint32 AirportTileScopeResolver::GetRandomBits() const
 {
-	const Station *st = object->u.airport.st;
-	const TileIndex tile = object->u.airport.tile;
-	return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
+	return (this->st == NULL ? 0 : this->st->random_bits) | (this->tile == INVALID_TILE ? 0 : GetStationTileRandomBits(this->tile) << 16);
 }
 
-static void AirportTileResolver(ResolverObject *res, const AirportTileSpec *ats, TileIndex tile, Station *st)
+AirportTileResolverObject::AirportTileResolverObject(const AirportTileSpec *ats, TileIndex tile, Station *st,
+		CallbackID callback, uint32 callback_param1, uint32 callback_param2)
+	: ResolverObject(ats->grf_prop.grffile, callback, callback_param1, callback_param2), tiles_scope(this, ats, tile, st)
 {
-	res->GetRandomBits = AirportTileGetRandomBits;
-	res->GetTriggers   = NULL;
-	res->SetTriggers   = NULL;
-	res->GetVariable   = AirportTileGetVariable;
-	res->ResolveRealMethod = AirportTileResolveReal;
-	res->StorePSA      = NULL;
+}
 
+AirportTileScopeResolver::AirportTileScopeResolver(ResolverObject *ro, const AirportTileSpec *ats, TileIndex tile, Station *st) : ScopeResolver(ro)
+{
 	assert(st != NULL);
-	res->u.airport.airport_id = st->airport.type;
-	res->u.airport.st         = st;
-	res->u.airport.tile       = tile;
 
-	res->callback        = CBID_NO_CALLBACK;
-	res->callback_param1 = 0;
-	res->callback_param2 = 0;
-	res->ResetState();
-
-	res->grffile         = ats->grf_prop.grffile;
+	this->st = st;
+	this->airport_id = st->airport.type;
+	this->tile = tile;
 }
 
 uint16 GetAirportTileCallback(CallbackID callback, uint32 param1, uint32 param2, const AirportTileSpec *ats, Station *st, TileIndex tile, int extra_data = 0)
 {
-	ResolverObject object;
-	const SpriteGroup *group;
-
-	AirportTileResolver(&object, ats, tile, st);
-	object.callback = callback;
-	object.callback_param1 = param1;
-	object.callback_param2 = param2;
-
-	group = SpriteGroup::Resolve(ats->grf_prop.spritegroup[0], &object);
+	AirportTileResolverObject object(ats, tile, st, callback, param1, param2);
+	const SpriteGroup *group = SpriteGroup::Resolve(ats->grf_prop.spritegroup[0], &object);
 	if (group == NULL) return CALLBACK_FAILED;
 
 	return group->GetCallbackResult();
@@ -278,9 +255,6 @@
 
 bool DrawNewAirportTile(TileInfo *ti, Station *st, StationGfx gfx, const AirportTileSpec *airts)
 {
-	const SpriteGroup *group;
-	ResolverObject object;
-
 	if (ti->tileh != SLOPE_FLAT) {
 		bool draw_old_one = true;
 		if (HasBit(airts->callback_mask, CBM_AIRT_DRAW_FOUNDATIONS)) {
@@ -292,9 +266,8 @@
 		if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
 	}
 
-	AirportTileResolver(&object, airts, ti->tile, st);
-
-	group = SpriteGroup::Resolve(airts->grf_prop.spritegroup[0], &object);
+	AirportTileResolverObject object(airts, ti->tile, st);
+	const SpriteGroup *group = SpriteGroup::Resolve(airts->grf_prop.spritegroup[0], &object);
 	if (group == NULL || group->type != SGT_TILELAYOUT) {
 		return false;
 	}
@@ -338,12 +311,3 @@
 	}
 }
 
-/**
- * Resolve an airport tile's spec and such so we can get a variable.
- * @param ro    The resolver object to fill.
- * @param index The airport tile to get the data from.
- */
-void GetAirportTileTypeResolver(ResolverObject *ro, uint index)
-{
-	AirportTileResolver(ro, AirportTileSpec::GetByTile(index), index, Station::GetByTile(index));
-}
--- a/src/newgrf_airporttiles.h
+++ b/src/newgrf_airporttiles.h
@@ -16,6 +16,35 @@
 #include "station_map.h"
 #include "newgrf_animation_type.h"
 #include "newgrf_commons.h"
+#include "newgrf_spritegroup.h"
+
+struct AirportTileScopeResolver : public ScopeResolver {
+	struct Station *st;  ///< Station of the airport for which the callback is run, or \c NULL for build gui.
+	byte airport_id;     ///< Type of airport for which the callback is run.
+	TileIndex tile;      ///< Tile for the callback, only valid for airporttile callbacks.
+
+	AirportTileScopeResolver(ResolverObject *ro, const AirportTileSpec *ats, TileIndex tile, Station *st);
+
+	/* virtual */ uint32 GetRandomBits() const;
+	/* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
+};
+
+struct AirportTileResolverObject : public ResolverObject {
+	AirportTileScopeResolver tiles_scope;
+
+	AirportTileResolverObject(const AirportTileSpec *ats, TileIndex tile, Station *st,
+			CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
+
+	/* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
+	{
+		switch (scope) {
+			case VSG_SCOPE_SELF: return &tiles_scope;
+			default: return &this->default_scope; // XXX return ResolverObject::GetScope(scope, relative);
+		}
+	}
+
+	/* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
+};
 
 /**
  * Defines the data structure of each indivudual tile of an airport.
--- a/src/table/newgrf_debug_data.h
+++ b/src/table/newgrf_debug_data.h
@@ -434,7 +434,12 @@
 	const void *GetSpec(uint index) const                { return AirportTileSpec::Get(GetAirportGfx(index)); }
 	void SetStringParameters(uint index) const           { this->SetObjectAtStringParameters(STR_STATION_NAME, GetStationIndex(index), index); }
 	uint32 GetGRFID(uint index) const                    { return (this->IsInspectable(index)) ? AirportTileSpec::Get(GetAirportGfx(index))->grf_prop.grffile->grfid : 0; }
-	void Resolve(ResolverObject *ro, uint32 index) const { extern void GetAirportTileTypeResolver(ResolverObject *ro, uint index); GetAirportTileTypeResolver(ro, index); }
+
+	/* virtual */ uint Resolve(uint index, uint var, uint param, bool *avail) const
+	{
+		AirportTileResolverObject ro(AirportTileSpec::GetByTile(index), index, Station::GetByTile(index));
+		return ro.GetScope(ro.scope)->GetVariable(var, param, avail);
+	}
 };
 
 static const NIFeature _nif_airporttile = {