From e10e9ee07c03d7ee5752b8122ecddaef40dfd5d5 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 6 Feb 2012 01:21:36 -0700 Subject: Option input type situations almost done. Interfaces just need some work. --- src/command.h | 2 ++ src/commandset.h | 4 +++- src/gamebuilder.cpp | 20 ++++++++++++++++++-- src/gamebuilder.h | 5 ++++- src/gamestate.cpp | 10 ++++++++++ src/gamestate.h | 3 +++ src/interfaceconsole.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++--- src/parser.y | 9 +++++---- src/situation.cpp | 7 ++++++- src/situation.h | 12 +++++++++++- 10 files changed, 104 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/command.h b/src/command.h index f82e87e..cc49996 100644 --- a/src/command.h +++ b/src/command.h @@ -9,6 +9,8 @@ public: Command(); virtual ~Command(); + Bu::String getRoot() const { return lChunks.first().sValue; } + void addLiteral( const Bu::String &sValue ); void addParam( const Bu::String &sValue ); diff --git a/src/commandset.h b/src/commandset.h index 579e1d8..aff2e4c 100644 --- a/src/commandset.h +++ b/src/commandset.h @@ -14,8 +14,10 @@ public: bool dispatch( class GameState &gState, const Bu::StringList &lCmd ); -private: typedef Bu::List CommandList; + const CommandList &getCommandList() const { return lCommand; } + +private: CommandList lCommand; }; diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index 83cbe79..353e254 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp @@ -96,9 +96,9 @@ void GameBuilder::endFunction() pGame->hFunction.insert( pCurFnc->getName(), pCurFnc ); } -void GameBuilder::beginSituation( const Bu::String &sName ) +void GameBuilder::beginSituation( const Bu::String &sName, Situation::InputType tInput ) { - pCurSit = new Situation( sName ); + pCurSit = new Situation( sName, tInput ); //sio << "New situation: " << sName << sio.nl; } @@ -219,3 +219,19 @@ void GameBuilder::closeCommand() pCurCmd = NULL; } +void GameBuilder::beginOption( const Bu::String &sValue ) +{ + pCurNode = pCurRoot = new AstBranch( AstNode::tScope ); + pCurCmd = new Command(); + pCurCmd->addLiteral( sValue ); +} + +void GameBuilder::closeOption() +{ + pCurCmd->setAst( pCurRoot ); + pCurRoot = pCurNode = NULL; + //pCurCmd->print(); + pCurSit->csLocal.addCommand( pCurCmd ); + pCurCmd = NULL; +} + diff --git a/src/gamebuilder.h b/src/gamebuilder.h index f3e3a1f..80df1bc 100644 --- a/src/gamebuilder.h +++ b/src/gamebuilder.h @@ -25,7 +25,7 @@ public: void endFunctionParams(); void endFunction(); - void beginSituation( const Bu::String &sName ); + void beginSituation( const Bu::String &sName, Situation::InputType tInput ); void beginSituationMode( Situation::Mode m ); void closeSituationMode(); void endSituation(); @@ -44,6 +44,9 @@ public: void addCommandParam( const Bu::String &sValue ); void endCommandParams(); void closeCommand(); + + void beginOption( const Bu::String &sValue ); + void closeOption(); private: class Game *pGame; diff --git a/src/gamestate.cpp b/src/gamestate.cpp index ec6bf72..0a82b99 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -233,6 +233,11 @@ Variable GameState::gatsToVariable( Gats::List *pLst ) const throw Bu::ExceptionBase("Type unknown: %d", dynamic_cast(*i)->getValue() ); } +class Situation *GameState::getCurSituation() +{ + return pGame->getSituation( sCurSituation ); +} + void GameState::parse( class AstBranch *pAst ) { if( pAst->getType() != AstNode::tScope ) @@ -297,6 +302,11 @@ void GameState::execCommand( const Bu::String &sCmd ) } } +void GameState::execOption( int idx ) +{ + pGame->getSituation( sCurSituation )->execOption( *this, idx ); +} + bool GameState::hasVariable( const Bu::String &sName, ScopeId id ) { switch( id ) diff --git a/src/gamestate.h b/src/gamestate.h index 8ff92f5..4209ebf 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -39,6 +39,7 @@ public: void callFunction( const Bu::String &sName ); void execCommand( const Bu::String &sCmd ); + void execOption( int idx ); bool hasVariable( const Bu::String &sName, ScopeId id ); void delVariable( const Bu::String &sName, ScopeId id ); @@ -52,6 +53,8 @@ public: bool isRunning() const { return bRunning; } Bu::String getPrompt() const { return sPrompt; } + class Situation *getCurSituation(); + private: void parse( const AstBranch::NodeList &lCode ); diff --git a/src/interfaceconsole.cpp b/src/interfaceconsole.cpp index 49be5c1..e652da8 100644 --- a/src/interfaceconsole.cpp +++ b/src/interfaceconsole.cpp @@ -2,6 +2,8 @@ #include "smlnode.h" #include "gamestate.h" +#include "situation.h" +#include "command.h" #include @@ -12,6 +14,8 @@ #include #include +#include + PluginInterface3( plugin_interface_console, console, InterfaceConsole, Interface, "Mike Buland", 1, 0 ); @@ -34,10 +38,45 @@ void InterfaceConsole::run( Game *pGame ) while( gs.isRunning() ) { char buf[1024]; - sio << sio.nl << "command> " << sio.flush; - fgets( buf, 1024, stdin ); + switch( gs.getCurSituation()->getInputType() ) + { + case Situation::inputCommand: + sio << sio.nl << "command> " << sio.flush; + fgets( buf, 1024, stdin ); + + gs.execCommand( buf ); + break; + + case Situation::inputOption: + sio << sio.nl; + { + const CommandSet::CommandList &cl = + gs.getCurSituation()->getCommandSet().getCommandList(); - gs.execCommand( buf ); + int j = 1; + for( CommandSet::CommandList::const_iterator i = cl.begin(); + i; i++ ) + { + sio << " " << j << ") " << (*i)->getRoot() << sio.nl; + j++; + } + sio << "Enter your selection: " << sio.flush; + fgets( buf, 1024, stdin ); + if( buf[0] < '1' || buf[0] > '9' ) + j = -1; + else + j = strtol( buf, NULL, 10 ); + if( j < 1 || j > cl.getSize() ) + { + sio << "Invalid selection, try again." << sio.nl; + } + else + { + gs.execOption( j-1 ); + } + } + break; + } } } diff --git a/src/parser.y b/src/parser.y index e127627..b50b897 100644 --- a/src/parser.y +++ b/src/parser.y @@ -121,9 +121,9 @@ bodyDecl: | bodyDecl function ; -situationMode: tokSituation tokSituationName { bld.beginSituation( *($2) ); } - | tokCommand tokSituation tokSituationName { bld.beginSituation( *($2) ); } - | tokOption tokSituation tokSituationName { bld.beginSituation( *($2) ); } +situationMode: tokSituation tokSituationName { bld.beginSituation( *($2), Situation::inputCommand ); } + | tokCommand tokSituation tokSituationName { bld.beginSituation( *($3), Situation::inputCommand ); } + | tokOption tokSituation tokSituationName { bld.beginSituation( *($3), Situation::inputOption ); } ; situation: situationMode '{' situationMembers '}' { bld.endSituation(); } @@ -284,7 +284,8 @@ dictValues: expr ':' expr { bld.addNode( AstNode::tInsert ); } | dictValues ',' expr ':' expr { bld.addNode( AstNode::tInsert ); } ; -optionDecl: tokOption ':' tokString '{' cmpltExprList '}' +optionDecl: tokOption ':' tokString { bld.beginOption( *$3 ); } '{' + cmpltExprList '}' { bld.closeOption(); } ; commandDecl: tokCommand ':' tokString { bld.beginCommand( *$3 ); } diff --git a/src/situation.cpp b/src/situation.cpp index 86dd5c5..54096cc 100644 --- a/src/situation.cpp +++ b/src/situation.cpp @@ -5,8 +5,9 @@ #include -Situation::Situation( const Bu::String &sName ) : +Situation::Situation( const Bu::String &sName, InputType tInput ) : sName( sName ), + tInput( tInput ), pAstSetup( NULL ), pAstEnter( NULL ) { @@ -59,6 +60,10 @@ bool Situation::execCommand( class GameState &gState, return csLocal.dispatch( gState, lCmd ); } +void Situation::execOption( class GameState &gState, int idx ) +{ +} + Bu::Formatter &operator<<( Bu::Formatter &f, Situation::Mode m ) { switch( m ) diff --git a/src/situation.h b/src/situation.h index 365533a..794b308 100644 --- a/src/situation.h +++ b/src/situation.h @@ -9,10 +9,17 @@ class Situation { friend class GameBuilder; public: - Situation( const Bu::String &sName ); + enum InputType + { + inputCommand, + inputOption, + }; + + Situation( const Bu::String &sName, InputType tInput ); virtual ~Situation(); Bu::String getName() const { return sName; } + InputType getInputType() const { return tInput; } enum Mode { @@ -25,9 +32,12 @@ public: void addCommand( Command *pCmd ); bool execCommand( class GameState &gState, const Bu::StringList &lCmd ); + void execOption( class GameState &gState, int idx ); + const CommandSet &getCommandSet() const { return csLocal; } private: Bu::String sName; + InputType tInput; CommandSet csLocal; class AstBranch *pAstSetup; class AstBranch *pAstEnter; -- cgit v1.2.3