From d790ced1f7af3a96c7dba96ea4eb9262f70eb58b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 19 Jan 2012 13:14:14 -0700 Subject: The gats interface is nearly done. --- src/gamestate.cpp | 119 +++++++++++++++++++++++++++++++++++++++++++++++++- src/gamestate.h | 5 +++ src/interfacegats.cpp | 29 ++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) 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 return pRoot; } +void GameState::fromGats( Gats::Dictionary *pRoot ) +{ + sPrompt = pRoot->getStr("curPrompt"); + sCurSituation = pRoot->getStr("curSituation"); + gatsToScope( pRoot->getDict("global"), &sGlobal ); + gatsToScope( pRoot->getDict("player"), &sPlayer ); + for( Gats::Dictionary::iterator i = pRoot->getDict("situations")->begin(); + i; i++ ) + { + Scope *pSc = new Scope(); + gatsToScope( dynamic_cast(*i), pSc ); + hsSituation.insert( i.getKey(), pSc ); + } +} + Gats::Object *GameState::scopeToGats( const Scope *pSrc ) const { Gats::Dictionary *pDst = new Gats::Dictionary(); @@ -60,7 +75,7 @@ Gats::Object *GameState::variableToGats( const Variable &rVar ) const break; case Variable::tBool: - pLst->append( rVar.getBool() ); + pLst->append( new Gats::Boolean( rVar.getBool() ) ); break; case Variable::tInt: @@ -114,6 +129,108 @@ Gats::Object *GameState::variableToGats( const Variable &rVar ) const return pLst; } +void GameState::gatsToScope( Gats::Dictionary *pRoot, Scope *pSrc ) const +{ + for( Gats::Dictionary::iterator i = pRoot->begin(); i; i++ ) + { + pSrc->insert( i.getKey(), gatsToVariable( + dynamic_cast(*i) + ) ); + } +} + +Variable GameState::gatsToVariable( Gats::List *pLst ) const +{ + Gats::List::iterator i = pLst->begin(); + + switch( dynamic_cast(*i)->getValue() ) + { + case Variable::tNull: + return Variable( Variable::tNull ); + break; + + case Variable::tBool: + return Variable( + dynamic_cast(*(i+1))->getValue() + ); + break; + + case Variable::tInt: + return Variable( + dynamic_cast(*(i+1))->getValue() + ); + break; + + case Variable::tFloat: + return Variable( + dynamic_cast(*(i+1))->getValue() + ); + break; + + case Variable::tSituation: + return Variable::newSituationName( + *dynamic_cast(*(i+1)) + ); + break; + + case Variable::tVariable: + return Variable::newVariableName( + *dynamic_cast(*(i+2)), + (ScopeId)dynamic_cast(*(i+1))->getValue() + ); + break; + + case Variable::tVarPtr: + sio << "No!!!" << sio.nl; + break; + + case Variable::tList: + { + Variable vRet( Variable::tList ); + for( Gats::List::iterator k = + dynamic_cast(*(i+1))->begin(); k; k++ ) + { + vRet += gatsToVariable( + dynamic_cast(*k) + ); + } + return vRet; + } + break; + + case Variable::tDictionary: + { + Variable vRet( Variable::tDictionary ); + for( Gats::List::iterator k = + dynamic_cast(*(i+1))->begin(); k; k++ ) + { + Variable vKey = gatsToVariable( + dynamic_cast(*k) + ); + k++; + if( !k ) + { + throw Bu::ExceptionBase("Premature end of dict."); + } + Variable vValue = gatsToVariable( + dynamic_cast(*k) + ); + vRet.insert( vKey, vValue ); + } + return vRet; + } + break; + + case Variable::tString: + return Variable( + *dynamic_cast(*(i+1)) + ); + break; + } + + throw Bu::ExceptionBase("Type unknown: %d", dynamic_cast(*i)->getValue() ); +} + void GameState::parse( class AstBranch *pAst ) { 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; namespace Gats { class Object; + class Dictionary; + class List; } class GameState @@ -21,6 +23,7 @@ public: virtual ~GameState(); Gats::Object *toGats() const; + void fromGats( Gats::Dictionary *pRoot ); Interface *getInterface() { return pIface; } @@ -54,6 +57,8 @@ private: Gats::Object *scopeToGats( const Scope *pSrc ) const; Gats::Object *variableToGats( const Variable &rVar ) const; + void gatsToScope( Gats::Dictionary *pRoot, Scope *pSrc ) const; + Variable gatsToVariable( Gats::List *pLst ) const; private: typedef Bu::List 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 @@ #include "interfacegats.h" #include "smlnode.h" +#include "gamestate.h" + #include "smlrendererhtml.h" #include #include +#include +#include +#include using namespace Bu; @@ -21,6 +26,30 @@ InterfaceGats::~InterfaceGats() void InterfaceGats::run( class Game *pGame ) { + GameState gs( pGame, this ); + + { + Gats::GatsStream gsIn( sioRaw ); + Gats::Object *pObj = gsIn.readObject(); + + if( pObj ) + { + gs.fromGats( dynamic_cast(pObj) ); + + delete pObj; + } + else + { + gs.init(); + } + } + + gs.execCommand("status"); + + //{ + // Gats::GatsStream gs + //pObj = gs.toGats(); + } void InterfaceGats::display( const SmlNode *pSml ) -- cgit v1.2.3