diff options
Diffstat (limited to 'src/gamestate.cpp')
-rw-r--r-- | src/gamestate.cpp | 119 |
1 files changed, 118 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 ) |