From a767a040e83e7d13cf8ff93f4f816faed9c7cc5b Mon Sep 17 00:00:00 2001
From: Mike Buland <eichlan@xagasoft.com>
Date: Thu, 19 Jan 2012 12:23:09 -0700
Subject: Gats encoding of saved games works.

---
 default.bld              |  2 +-
 src/gamestate.cpp        | 85 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/gamestate.h          |  3 ++
 src/interfaceconsole.cpp | 11 +++++++
 4 files changed, 100 insertions(+), 1 deletion(-)

diff --git a/default.bld b/default.bld
index 3a5453a..66c3825 100644
--- a/default.bld
+++ b/default.bld
@@ -53,7 +53,7 @@ target "stage"
 	FLEXFLAGS="-osrc/parser.yy.c --header-file=src/parser.yy.h";
 	BISONFLAGS="-d";
 
-	LDFLAGS += "-lbu++ -ldl -Llibgats -lgats";
+	LDFLAGS += "-Llibgats -lgats -lbu++ -ldl";
 }
 
 /*
diff --git a/src/gamestate.cpp b/src/gamestate.cpp
index b87f658..e2ab61a 100644
--- a/src/gamestate.cpp
+++ b/src/gamestate.cpp
@@ -27,6 +27,91 @@ Gats::Object *GameState::toGats() const
 	Gats::Dictionary *pRoot = new Gats::Dictionary;
 
 	Gats::Dictionary *pSit = pRoot->insertDict("situations");
+	for( ScopeHash::const_iterator i = hsSituation.begin(); i; i++ )
+	{
+		pSit->insert( i.getKey(), scopeToGats( *i ) );
+	}
+	pRoot->insert("global", scopeToGats( &sGlobal ) );
+	pRoot->insert("player", scopeToGats( &sPlayer ) );
+	pRoot->insert("curSituation", sCurSituation );
+	pRoot->insert("curPrompt", sPrompt );
+
+	return pRoot;
+}
+
+Gats::Object *GameState::scopeToGats( const Scope *pSrc ) const
+{
+	Gats::Dictionary *pDst = new Gats::Dictionary();
+	for( Scope::const_iterator i = pSrc->begin(); i; i++ )
+	{
+		pDst->insert( i.getKey(), variableToGats( *i ) );
+	}
+
+	return pDst;
+}
+
+Gats::Object *GameState::variableToGats( const Variable &rVar ) const
+{
+	Gats::List *pLst = new Gats::List();
+	pLst->append( rVar.getType() );
+	switch( rVar.getType() )
+	{
+		case Variable::tNull:
+			break;
+
+		case Variable::tBool:
+			pLst->append( rVar.getBool() );
+			break;
+
+		case Variable::tInt:
+			pLst->append( rVar.getInt() );
+			break;
+
+		case Variable::tFloat:
+			pLst->append( rVar.getFloat() );
+			break;
+
+		case Variable::tSituation:
+			pLst->append( rVar.getString() );
+			break;
+
+		case Variable::tVariable:
+			pLst->append( rVar.getVariableRef().sid );
+			pLst->append( rVar.getVariableRef().sName );
+			break;
+
+		case Variable::tVarPtr:
+			sio << "No!!!" << sio.nl;
+			break;
+
+		case Variable::tList:
+			{
+				Gats::List *pSub = pLst->appendList();
+				for( Variable::VariableArray::const_iterator i =
+					 rVar.getList().begin(); i; i++ )
+				{
+					pSub->append( variableToGats( *i ) );
+				}
+			}
+			break;
+
+		case Variable::tDictionary:
+			{
+				Gats::List *pSub = pLst->appendList();
+				for( Variable::VariableHash::const_iterator i =
+					 rVar.getHash().begin(); i; i++ )
+				{
+					pSub->append( variableToGats( i.getKey() ) );
+					pSub->append( variableToGats( *i ) );
+				}
+			}
+			break;
+
+		case Variable::tString:
+			pLst->append( rVar.getString() );
+			break;
+	}
+	return pLst;
 }
 
 void GameState::parse( class AstBranch *pAst )
diff --git a/src/gamestate.h b/src/gamestate.h
index 1d6d491..5825cfa 100644
--- a/src/gamestate.h
+++ b/src/gamestate.h
@@ -52,6 +52,9 @@ public:
 private:
 	void parse( const AstBranch::NodeList &lCode );
 
+	Gats::Object *scopeToGats( const Scope *pSrc ) const;
+	Gats::Object *variableToGats( const Variable &rVar ) const;
+
 private:
 	typedef Bu::List<Scope *> ScopeList;
 	typedef Bu::Hash<Bu::String, Scope *> ScopeHash;
diff --git a/src/interfaceconsole.cpp b/src/interfaceconsole.cpp
index caa13bf..e07bffe 100644
--- a/src/interfaceconsole.cpp
+++ b/src/interfaceconsole.cpp
@@ -8,6 +8,9 @@
 #include <bu/plugger.h>
 
 #include "smlrenderervt100.h"
+#include <gats/types.h>
+#include <gats/gatsstream.h>
+#include <bu/file.h>
 
 PluginInterface3( plugin_interface_console, console, InterfaceConsole,
 		Interface, "Mike Buland", 1, 0 );
@@ -27,6 +30,7 @@ void InterfaceConsole::run( Game *pGame )
 	GameState gs( pGame, this );
 	gs.init();
 
+	int iStep = 0;
 	while( gs.isRunning() )
 	{
 		char buf[1024];
@@ -34,6 +38,13 @@ void InterfaceConsole::run( Game *pGame )
 		fgets( buf, 1024, stdin );
 
 		gs.execCommand( buf );
+
+		Gats::Object *pObj = gs.toGats();
+		File fOut( Bu::String("test-%1.save").arg( iStep++ ), File::WriteNew );
+		Gats::GatsStream gs( fOut );
+		gs.writeObject( pObj );
+
+		delete pObj;
 	}	
 }
 
-- 
cgit v1.2.3