summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-01-19 12:23:09 -0700
committerMike Buland <eichlan@xagasoft.com>2012-01-19 12:23:09 -0700
commita767a040e83e7d13cf8ff93f4f816faed9c7cc5b (patch)
tree655041e2404e1cc8e3b9c4799bcbdd40cc58040e
parent09eb0733854da12146570060e84f5f7b4203e1bc (diff)
downloadstage-a767a040e83e7d13cf8ff93f4f816faed9c7cc5b.tar.gz
stage-a767a040e83e7d13cf8ff93f4f816faed9c7cc5b.tar.bz2
stage-a767a040e83e7d13cf8ff93f4f816faed9c7cc5b.tar.xz
stage-a767a040e83e7d13cf8ff93f4f816faed9c7cc5b.zip
Gats encoding of saved games works.
-rw-r--r--default.bld2
-rw-r--r--src/gamestate.cpp85
-rw-r--r--src/gamestate.h3
-rw-r--r--src/interfaceconsole.cpp11
4 files changed, 100 insertions, 1 deletions
diff --git a/default.bld b/default.bld
index 3a5453a..66c3825 100644
--- a/default.bld
+++ b/default.bld
@@ -53,7 +53,7 @@ target "stage"
53 FLEXFLAGS="-osrc/parser.yy.c --header-file=src/parser.yy.h"; 53 FLEXFLAGS="-osrc/parser.yy.c --header-file=src/parser.yy.h";
54 BISONFLAGS="-d"; 54 BISONFLAGS="-d";
55 55
56 LDFLAGS += "-lbu++ -ldl -Llibgats -lgats"; 56 LDFLAGS += "-Llibgats -lgats -lbu++ -ldl";
57} 57}
58 58
59/* 59/*
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
27 Gats::Dictionary *pRoot = new Gats::Dictionary; 27 Gats::Dictionary *pRoot = new Gats::Dictionary;
28 28
29 Gats::Dictionary *pSit = pRoot->insertDict("situations"); 29 Gats::Dictionary *pSit = pRoot->insertDict("situations");
30 for( ScopeHash::const_iterator i = hsSituation.begin(); i; i++ )
31 {
32 pSit->insert( i.getKey(), scopeToGats( *i ) );
33 }
34 pRoot->insert("global", scopeToGats( &sGlobal ) );
35 pRoot->insert("player", scopeToGats( &sPlayer ) );
36 pRoot->insert("curSituation", sCurSituation );
37 pRoot->insert("curPrompt", sPrompt );
38
39 return pRoot;
40}
41
42Gats::Object *GameState::scopeToGats( const Scope *pSrc ) const
43{
44 Gats::Dictionary *pDst = new Gats::Dictionary();
45 for( Scope::const_iterator i = pSrc->begin(); i; i++ )
46 {
47 pDst->insert( i.getKey(), variableToGats( *i ) );
48 }
49
50 return pDst;
51}
52
53Gats::Object *GameState::variableToGats( const Variable &rVar ) const
54{
55 Gats::List *pLst = new Gats::List();
56 pLst->append( rVar.getType() );
57 switch( rVar.getType() )
58 {
59 case Variable::tNull:
60 break;
61
62 case Variable::tBool:
63 pLst->append( rVar.getBool() );
64 break;
65
66 case Variable::tInt:
67 pLst->append( rVar.getInt() );
68 break;
69
70 case Variable::tFloat:
71 pLst->append( rVar.getFloat() );
72 break;
73
74 case Variable::tSituation:
75 pLst->append( rVar.getString() );
76 break;
77
78 case Variable::tVariable:
79 pLst->append( rVar.getVariableRef().sid );
80 pLst->append( rVar.getVariableRef().sName );
81 break;
82
83 case Variable::tVarPtr:
84 sio << "No!!!" << sio.nl;
85 break;
86
87 case Variable::tList:
88 {
89 Gats::List *pSub = pLst->appendList();
90 for( Variable::VariableArray::const_iterator i =
91 rVar.getList().begin(); i; i++ )
92 {
93 pSub->append( variableToGats( *i ) );
94 }
95 }
96 break;
97
98 case Variable::tDictionary:
99 {
100 Gats::List *pSub = pLst->appendList();
101 for( Variable::VariableHash::const_iterator i =
102 rVar.getHash().begin(); i; i++ )
103 {
104 pSub->append( variableToGats( i.getKey() ) );
105 pSub->append( variableToGats( *i ) );
106 }
107 }
108 break;
109
110 case Variable::tString:
111 pLst->append( rVar.getString() );
112 break;
113 }
114 return pLst;
30} 115}
31 116
32void GameState::parse( class AstBranch *pAst ) 117void 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:
52private: 52private:
53 void parse( const AstBranch::NodeList &lCode ); 53 void parse( const AstBranch::NodeList &lCode );
54 54
55 Gats::Object *scopeToGats( const Scope *pSrc ) const;
56 Gats::Object *variableToGats( const Variable &rVar ) const;
57
55private: 58private:
56 typedef Bu::List<Scope *> ScopeList; 59 typedef Bu::List<Scope *> ScopeList;
57 typedef Bu::Hash<Bu::String, Scope *> ScopeHash; 60 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 @@
8#include <bu/plugger.h> 8#include <bu/plugger.h>
9 9
10#include "smlrenderervt100.h" 10#include "smlrenderervt100.h"
11#include <gats/types.h>
12#include <gats/gatsstream.h>
13#include <bu/file.h>
11 14
12PluginInterface3( plugin_interface_console, console, InterfaceConsole, 15PluginInterface3( plugin_interface_console, console, InterfaceConsole,
13 Interface, "Mike Buland", 1, 0 ); 16 Interface, "Mike Buland", 1, 0 );
@@ -27,6 +30,7 @@ void InterfaceConsole::run( Game *pGame )
27 GameState gs( pGame, this ); 30 GameState gs( pGame, this );
28 gs.init(); 31 gs.init();
29 32
33 int iStep = 0;
30 while( gs.isRunning() ) 34 while( gs.isRunning() )
31 { 35 {
32 char buf[1024]; 36 char buf[1024];
@@ -34,6 +38,13 @@ void InterfaceConsole::run( Game *pGame )
34 fgets( buf, 1024, stdin ); 38 fgets( buf, 1024, stdin );
35 39
36 gs.execCommand( buf ); 40 gs.execCommand( buf );
41
42 Gats::Object *pObj = gs.toGats();
43 File fOut( Bu::String("test-%1.save").arg( iStep++ ), File::WriteNew );
44 Gats::GatsStream gs( fOut );
45 gs.writeObject( pObj );
46
47 delete pObj;
37 } 48 }
38} 49}
39 50