changeset 19746:65c85e05d795 draft

(svn r24679) -Codechange: Add resolver classes for towns.
author alberth <alberth@openttd.org>
date Sat, 10 Nov 2012 20:38:46 +0000
parents 3a40c69fc098
children 405f15db535f
files src/newgrf_spritegroup.cpp src/newgrf_spritegroup.h src/newgrf_town.cpp src/newgrf_town.h
diffstat 4 files changed, 55 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/newgrf_spritegroup.cpp
+++ b/src/newgrf_spritegroup.cpp
@@ -143,9 +143,10 @@
 	if (this->ro->StorePSA != NULL) this->ro->StorePSA(this->ro, reg, value);
 }
 
-ResolverObject::ResolverObject() : temp_scope(this) {} // XXX Temporary
+ResolverObject::ResolverObject() : default_scope(this), temp_scope(this) {} // XXX Temporary
 
-ResolverObject::ResolverObject(const GRFFile *grffile, CallbackID callback, uint32 callback_param1, uint32 callback_param2) : temp_scope(this)
+ResolverObject::ResolverObject(const GRFFile *grffile, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
+		: default_scope(this), temp_scope(this)
 {
 	this->callback = callback;
 	this->callback_param1 = callback_param1;
--- a/src/newgrf_spritegroup.h
+++ b/src/newgrf_spritegroup.h
@@ -333,6 +333,7 @@
 	ResolverObject(const GRFFile *grffile, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
 	virtual ~ResolverObject();
 
+	ScopeResolver default_scope; ///< Default implementation of the grf scope.
 	TempScopeResolver temp_scope; ///< Temporary scope resolver to refer back to the methods of #ResolverObject.
 
 	CallbackID callback;
--- a/src/newgrf_town.cpp
+++ b/src/newgrf_town.cpp
@@ -12,7 +12,18 @@
 #include "stdafx.h"
 #include "debug.h"
 #include "town.h"
-#include "newgrf_spritegroup.h"
+#include "newgrf_town.h"
+
+TownScopeResolver::TownScopeResolver(ResolverObject *ro, Town *t, bool readonly) : ScopeResolver(ro)
+{
+	this->t = t;
+	this->readonly = readonly;
+}
+
+/* virtual */ uint32 TownScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
+{
+	return TownGetVariable(variable, parameter, available, this->t, this->ro->grffile);
+}
 
 /**
  * This function implements the town variables that newGRF defines.
@@ -126,6 +137,12 @@
 	return UINT_MAX;
 }
 
+/* virtual */ void TownScopeResolver::StorePSA(uint pos, int32 value)
+{
+	if (this->readonly) return;
+	TownStorePSA(this->t, this->ro->grffile, pos, value);
+}
+
 /**
  * Store a value in town persistent storage.
  * @param t Town owning the persistent storage.
@@ -162,3 +179,9 @@
 	psa->StoreValue(pos, value);
 	t->psa_list.push_back(psa);
 }
+
+TownResolverObject::TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly)
+		: ResolverObject(grffile), town_scope(this, t, readonly)
+{
+}
+
--- a/src/newgrf_town.h
+++ b/src/newgrf_town.h
@@ -13,11 +13,37 @@
 #define NEWGRF_TOWN_H
 
 #include "town_type.h"
+#include "newgrf_spritegroup.h"
 
 /* Currently there is no direct town resolver; we only need to get town
  * variable results from inside stations, house tiles and industries,
- * and to check the town's persistent storage. */
+ * and to check the town's persistent storage.
+ * XXX Remove the functions. */
 uint32 TownGetVariable(byte variable, uint32 parameter, bool *available, Town *t, const struct GRFFile *caller_grffile);
 void TownStorePSA(Town *t, const struct GRFFile *caller_grffile, uint pos, int32 value);
 
+struct TownScopeResolver : public ScopeResolver {
+	Town *t;
+	bool readonly;
+
+	TownScopeResolver(ResolverObject *ro, Town *t, bool readonly);
+
+	virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
+	virtual void StorePSA(uint reg, int32 value);
+};
+
+struct TownResolverObject : public ResolverObject {
+	TownScopeResolver town_scope;
+
+	TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly);
+
+	/* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
+	{
+		switch (scope) {
+			case VSG_SCOPE_SELF: return &town_scope;
+			default: return &this->default_scope; // XXX return ResolverObject::GetScope(scope, relative);
+		}
+	}
+};
+
 #endif /* NEWGRF_TOWN_H */