diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gamestate.cpp | 119 | ||||
-rw-r--r-- | src/gamestate.h | 5 | ||||
-rw-r--r-- | src/interfacegats.cpp | 29 |
3 files changed, 152 insertions, 1 deletions
diff --git a/src/gamestate.cpp b/src/gamestate.cpp index e2ab61a..852926e 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp | |||
@@ -39,6 +39,21 @@ Gats::Object *GameState::toGats() const | |||
39 | return pRoot; | 39 | return pRoot; |
40 | } | 40 | } |
41 | 41 | ||
42 | void GameState::fromGats( Gats::Dictionary *pRoot ) | ||
43 | { | ||
44 | sPrompt = pRoot->getStr("curPrompt"); | ||
45 | sCurSituation = pRoot->getStr("curSituation"); | ||
46 | gatsToScope( pRoot->getDict("global"), &sGlobal ); | ||
47 | gatsToScope( pRoot->getDict("player"), &sPlayer ); | ||
48 | for( Gats::Dictionary::iterator i = pRoot->getDict("situations")->begin(); | ||
49 | i; i++ ) | ||
50 | { | ||
51 | Scope *pSc = new Scope(); | ||
52 | gatsToScope( dynamic_cast<Gats::Dictionary *>(*i), pSc ); | ||
53 | hsSituation.insert( i.getKey(), pSc ); | ||
54 | } | ||
55 | } | ||
56 | |||
42 | Gats::Object *GameState::scopeToGats( const Scope *pSrc ) const | 57 | Gats::Object *GameState::scopeToGats( const Scope *pSrc ) const |
43 | { | 58 | { |
44 | Gats::Dictionary *pDst = new Gats::Dictionary(); | 59 | Gats::Dictionary *pDst = new Gats::Dictionary(); |
@@ -60,7 +75,7 @@ Gats::Object *GameState::variableToGats( const Variable &rVar ) const | |||
60 | break; | 75 | break; |
61 | 76 | ||
62 | case Variable::tBool: | 77 | case Variable::tBool: |
63 | pLst->append( rVar.getBool() ); | 78 | pLst->append( new Gats::Boolean( rVar.getBool() ) ); |
64 | break; | 79 | break; |
65 | 80 | ||
66 | case Variable::tInt: | 81 | case Variable::tInt: |
@@ -114,6 +129,108 @@ Gats::Object *GameState::variableToGats( const Variable &rVar ) const | |||
114 | return pLst; | 129 | return pLst; |
115 | } | 130 | } |
116 | 131 | ||
132 | void GameState::gatsToScope( Gats::Dictionary *pRoot, Scope *pSrc ) const | ||
133 | { | ||
134 | for( Gats::Dictionary::iterator i = pRoot->begin(); i; i++ ) | ||
135 | { | ||
136 | pSrc->insert( i.getKey(), gatsToVariable( | ||
137 | dynamic_cast<Gats::List *>(*i) | ||
138 | ) ); | ||
139 | } | ||
140 | } | ||
141 | |||
142 | Variable GameState::gatsToVariable( Gats::List *pLst ) const | ||
143 | { | ||
144 | Gats::List::iterator i = pLst->begin(); | ||
145 | |||
146 | switch( dynamic_cast<Gats::Integer *>(*i)->getValue() ) | ||
147 | { | ||
148 | case Variable::tNull: | ||
149 | return Variable( Variable::tNull ); | ||
150 | break; | ||
151 | |||
152 | case Variable::tBool: | ||
153 | return Variable( | ||
154 | dynamic_cast<Gats::Boolean *>(*(i+1))->getValue() | ||
155 | ); | ||
156 | break; | ||
157 | |||
158 | case Variable::tInt: | ||
159 | return Variable( | ||
160 | dynamic_cast<Gats::Integer *>(*(i+1))->getValue() | ||
161 | ); | ||
162 | break; | ||
163 | |||
164 | case Variable::tFloat: | ||
165 | return Variable( | ||
166 | dynamic_cast<Gats::Float *>(*(i+1))->getValue() | ||
167 | ); | ||
168 | break; | ||
169 | |||
170 | case Variable::tSituation: | ||
171 | return Variable::newSituationName( | ||
172 | *dynamic_cast<Gats::String *>(*(i+1)) | ||
173 | ); | ||
174 | break; | ||
175 | |||
176 | case Variable::tVariable: | ||
177 | return Variable::newVariableName( | ||
178 | *dynamic_cast<Gats::String *>(*(i+2)), | ||
179 | (ScopeId)dynamic_cast<Gats::Integer *>(*(i+1))->getValue() | ||
180 | ); | ||
181 | break; | ||
182 | |||
183 | case Variable::tVarPtr: | ||
184 | sio << "No!!!" << sio.nl; | ||
185 | break; | ||
186 | |||
187 | case Variable::tList: | ||
188 | { | ||
189 | Variable vRet( Variable::tList ); | ||
190 | for( Gats::List::iterator k = | ||
191 | dynamic_cast<Gats::List *>(*(i+1))->begin(); k; k++ ) | ||
192 | { | ||
193 | vRet += gatsToVariable( | ||
194 | dynamic_cast<Gats::List *>(*k) | ||
195 | ); | ||
196 | } | ||
197 | return vRet; | ||
198 | } | ||
199 | break; | ||
200 | |||
201 | case Variable::tDictionary: | ||
202 | { | ||
203 | Variable vRet( Variable::tDictionary ); | ||
204 | for( Gats::List::iterator k = | ||
205 | dynamic_cast<Gats::List *>(*(i+1))->begin(); k; k++ ) | ||
206 | { | ||
207 | Variable vKey = gatsToVariable( | ||
208 | dynamic_cast<Gats::List *>(*k) | ||
209 | ); | ||
210 | k++; | ||
211 | if( !k ) | ||
212 | { | ||
213 | throw Bu::ExceptionBase("Premature end of dict."); | ||
214 | } | ||
215 | Variable vValue = gatsToVariable( | ||
216 | dynamic_cast<Gats::List *>(*k) | ||
217 | ); | ||
218 | vRet.insert( vKey, vValue ); | ||
219 | } | ||
220 | return vRet; | ||
221 | } | ||
222 | break; | ||
223 | |||
224 | case Variable::tString: | ||
225 | return Variable( | ||
226 | *dynamic_cast<Gats::String *>(*(i+1)) | ||
227 | ); | ||
228 | break; | ||
229 | } | ||
230 | |||
231 | throw Bu::ExceptionBase("Type unknown: %d", dynamic_cast<Gats::Integer *>(*i)->getValue() ); | ||
232 | } | ||
233 | |||
117 | void GameState::parse( class AstBranch *pAst ) | 234 | void GameState::parse( class AstBranch *pAst ) |
118 | { | 235 | { |
119 | if( pAst->getType() != AstNode::tScope ) | 236 | if( pAst->getType() != AstNode::tScope ) |
diff --git a/src/gamestate.h b/src/gamestate.h index 5825cfa..2249c25 100644 --- a/src/gamestate.h +++ b/src/gamestate.h | |||
@@ -12,6 +12,8 @@ class Interface; | |||
12 | namespace Gats | 12 | namespace Gats |
13 | { | 13 | { |
14 | class Object; | 14 | class Object; |
15 | class Dictionary; | ||
16 | class List; | ||
15 | } | 17 | } |
16 | 18 | ||
17 | class GameState | 19 | class GameState |
@@ -21,6 +23,7 @@ public: | |||
21 | virtual ~GameState(); | 23 | virtual ~GameState(); |
22 | 24 | ||
23 | Gats::Object *toGats() const; | 25 | Gats::Object *toGats() const; |
26 | void fromGats( Gats::Dictionary *pRoot ); | ||
24 | 27 | ||
25 | Interface *getInterface() { return pIface; } | 28 | Interface *getInterface() { return pIface; } |
26 | 29 | ||
@@ -54,6 +57,8 @@ private: | |||
54 | 57 | ||
55 | Gats::Object *scopeToGats( const Scope *pSrc ) const; | 58 | Gats::Object *scopeToGats( const Scope *pSrc ) const; |
56 | Gats::Object *variableToGats( const Variable &rVar ) const; | 59 | Gats::Object *variableToGats( const Variable &rVar ) const; |
60 | void gatsToScope( Gats::Dictionary *pRoot, Scope *pSrc ) const; | ||
61 | Variable gatsToVariable( Gats::List *pLst ) const; | ||
57 | 62 | ||
58 | private: | 63 | private: |
59 | typedef Bu::List<Scope *> ScopeList; | 64 | typedef Bu::List<Scope *> ScopeList; |
diff --git a/src/interfacegats.cpp b/src/interfacegats.cpp index 696c6b6..b6ad859 100644 --- a/src/interfacegats.cpp +++ b/src/interfacegats.cpp | |||
@@ -1,10 +1,15 @@ | |||
1 | #include "interfacegats.h" | 1 | #include "interfacegats.h" |
2 | 2 | ||
3 | #include "smlnode.h" | 3 | #include "smlnode.h" |
4 | #include "gamestate.h" | ||
5 | |||
4 | #include "smlrendererhtml.h" | 6 | #include "smlrendererhtml.h" |
5 | 7 | ||
6 | #include <bu/plugger.h> | 8 | #include <bu/plugger.h> |
7 | #include <bu/sio.h> | 9 | #include <bu/sio.h> |
10 | #include <bu/file.h> | ||
11 | #include <gats/gatsstream.h> | ||
12 | #include <gats/types.h> | ||
8 | 13 | ||
9 | using namespace Bu; | 14 | using namespace Bu; |
10 | 15 | ||
@@ -21,6 +26,30 @@ InterfaceGats::~InterfaceGats() | |||
21 | 26 | ||
22 | void InterfaceGats::run( class Game *pGame ) | 27 | void InterfaceGats::run( class Game *pGame ) |
23 | { | 28 | { |
29 | GameState gs( pGame, this ); | ||
30 | |||
31 | { | ||
32 | Gats::GatsStream gsIn( sioRaw ); | ||
33 | Gats::Object *pObj = gsIn.readObject(); | ||
34 | |||
35 | if( pObj ) | ||
36 | { | ||
37 | gs.fromGats( dynamic_cast<Gats::Dictionary *>(pObj) ); | ||
38 | |||
39 | delete pObj; | ||
40 | } | ||
41 | else | ||
42 | { | ||
43 | gs.init(); | ||
44 | } | ||
45 | } | ||
46 | |||
47 | gs.execCommand("status"); | ||
48 | |||
49 | //{ | ||
50 | // Gats::GatsStream gs | ||
51 | //pObj = gs.toGats(); | ||
52 | |||
24 | } | 53 | } |
25 | 54 | ||
26 | void InterfaceGats::display( const SmlNode *pSml ) | 55 | void InterfaceGats::display( const SmlNode *pSml ) |