summaryrefslogtreecommitdiff
path: root/src/gamestate.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-02-08 00:22:34 -0700
committerMike Buland <eichlan@xagasoft.com>2012-02-08 00:22:34 -0700
commit11554a9e5c92a98129b42598495abe7503d9208c (patch)
tree91182c1737204d60bcbbc038f2743e60d09faedb /src/gamestate.cpp
parent3a1a20a9bc7008abb7de241c44cc0d7a3d4a77d9 (diff)
downloadstage-11554a9e5c92a98129b42598495abe7503d9208c.tar.gz
stage-11554a9e5c92a98129b42598495abe7503d9208c.tar.bz2
stage-11554a9e5c92a98129b42598495abe7503d9208c.tar.xz
stage-11554a9e5c92a98129b42598495abe7503d9208c.zip
Everything seems to work now, goto, return, etc.
Diffstat (limited to 'src/gamestate.cpp')
-rw-r--r--src/gamestate.cpp89
1 files changed, 53 insertions, 36 deletions
diff --git a/src/gamestate.cpp b/src/gamestate.cpp
index 38c6612..0434e6d 100644
--- a/src/gamestate.cpp
+++ b/src/gamestate.cpp
@@ -237,10 +237,10 @@ class Situation *GameState::getCurSituation()
237{ 237{
238 return pGame->getSituation( sCurSituation ); 238 return pGame->getSituation( sCurSituation );
239} 239}
240 240/*
241void GameState::run( class AstBranch *pAst ) 241void GameState::run( class AstBranch *pAst )
242{ 242{
243 pushScope( pAst ); 243// pushScope( pAst );
244 run(); 244 run();
245// if( lsLocal.getSize() == iDepth ) 245// if( lsLocal.getSize() == iDepth )
246// delete lsLocal.peekPop(); 246// delete lsLocal.peekPop();
@@ -256,7 +256,7 @@ void GameState::pushScope( class AstBranch *pAst )
256// int iDepth = lsLocal.getSize(); 256// int iDepth = lsLocal.getSize();
257 sProg.push( pAst->getNodeList().begin() ); 257 sProg.push( pAst->getNodeList().begin() );
258} 258}
259 259*/
260void GameState::init() 260void GameState::init()
261{ 261{
262 Variable vStart = pGame->getParam("start"); 262 Variable vStart = pGame->getParam("start");
@@ -264,7 +264,6 @@ void GameState::init()
264 throw Bu::ExceptionBase("game.start is not set to a situation name."); 264 throw Bu::ExceptionBase("game.start is not set to a situation name.");
265 265
266 gotoSituation( vStart.getString() ); 266 gotoSituation( vStart.getString() );
267 run();
268} 267}
269 268
270void GameState::gotoSituation( const Bu::String &sName ) 269void GameState::gotoSituation( const Bu::String &sName )
@@ -276,17 +275,23 @@ void GameState::gotoSituation( const Bu::String &sName )
276 275
277 Situation *pSit = pGame->getSituation( sName ); 276 Situation *pSit = pGame->getSituation( sName );
278 sCurSituation = sName; 277 sCurSituation = sName;
278 sGoto.clear();
279 if( !hsSituation.has( sName ) ) 279 if( !hsSituation.has( sName ) )
280 { 280 {
281 hsSituation.insert( sName, new Scope() ); 281 hsSituation.insert( sName, new Scope() );
282 pSit->exec( *this, Situation::modeEnter );
283 pSit->exec( *this, Situation::modeSetup ); 282 pSit->exec( *this, Situation::modeSetup );
283 if( !sGoto.isSet() )
284 pSit->exec( *this, Situation::modeEnter );
284 } 285 }
285 else 286 else
286 { 287 {
287 pSit->exec( *this, Situation::modeEnter ); 288 pSit->exec( *this, Situation::modeEnter );
288 } 289 }
289 290
291 if( sGoto.isSet() )
292 {
293 gotoSituation( sGoto );
294 }
290} 295}
291 296
292void GameState::callFunction( const Bu::String &sName ) 297void GameState::callFunction( const Bu::String &sName )
@@ -308,11 +313,17 @@ void GameState::execCommand( const Bu::String &sCmd )
308 return; 313 return;
309 } 314 }
310 } 315 }
316
317 if( sGoto.isSet() )
318 gotoSituation( sGoto );
311} 319}
312 320
313void GameState::execOption( int idx ) 321void GameState::execOption( int idx )
314{ 322{
315 pGame->getSituation( sCurSituation )->execOption( *this, idx ); 323 pGame->getSituation( sCurSituation )->execOption( *this, idx );
324
325 if( sGoto.isSet() )
326 gotoSituation( sGoto );
316} 327}
317 328
318bool GameState::hasVariable( const Bu::String &sName, ScopeId id ) 329bool GameState::hasVariable( const Bu::String &sName, ScopeId id )
@@ -523,14 +534,27 @@ Variable GameState::popDeref()
523 return v; 534 return v;
524} 535}
525 536
526void GameState::run() 537void GameState::run( const class AstBranch *pAst, bool bNewScope )
527{ 538{
528 bEscape = false; 539 if( pAst->getType() != AstNode::tScope )
540 throw Bu::ExceptionBase("Nope, nothing doing, you can't run a non-scope AstBranch.");
541
542 run( pAst->getNodeList(), bNewScope );
543}
544
545void GameState::run( const AstBranch::NodeList &lNode, bool bNewScope )
546{
547 ProgramStack sProg;
548
549 if( bNewScope )
550 lsLocal.push( new Scope() );
551 sProg.push( lNode.begin() );
552
529 while( !sProg.isEmpty() ) 553 while( !sProg.isEmpty() )
530 { 554 {
531 ProgramCounter &i = sProg.peek(); 555 ProgramCounter &i = sProg.peek();
532 sio << "Stack: " << lStack << sio.nl; 556// sio << "Stack: " << lStack << sio.nl;
533 sio << "exec: " << (*i)->getType() << sio.nl; 557// sio << "exec: " << (*i)->getType() << sio.nl;
534 switch( (*i)->getType() ) 558 switch( (*i)->getType() )
535 { 559 {
536 // tLeaf 560 // tLeaf
@@ -712,10 +736,8 @@ void GameState::run()
712 Variable x = popDeref(); 736 Variable x = popDeref();
713 if( x.getType() != Variable::tSituation ) 737 if( x.getType() != Variable::tSituation )
714 throw Bu::ExceptionBase("You cannot goto anything but a situation."); 738 throw Bu::ExceptionBase("You cannot goto anything but a situation.");
715 sProg.clear(); 739 sGoto = x.getString();
716 gotoSituation( x.getString() ); 740 return;
717 bEscape = true;
718 continue;
719 } 741 }
720 break; 742 break;
721 743
@@ -729,11 +751,9 @@ void GameState::run()
729 break; 751 break;
730 752
731 case AstNode::tReturn: 753 case AstNode::tReturn:
732 sProg.pop(); 754 if( bNewScope )
733// bReturnOnly = true; 755 lsLocal.pop();
734// bEscape = true; 756 return;
735// return;
736 break;
737 757
738 case AstNode::tAppend: 758 case AstNode::tAppend:
739 { 759 {
@@ -791,18 +811,10 @@ void GameState::run()
791 break; 811 break;
792 812
793 case AstNode::tFuncCall: 813 case AstNode::tFuncCall:
794 sio << "Calling function now: " << sio.nl;
795 {
796 ProgramCounter c = i;
797 i++;
798 callFunction( 814 callFunction(
799 dynamic_cast<const AstLeafLiteral *>(*c) 815 dynamic_cast<const AstLeafLiteral *>(*i)
800 ->getValue().getString() 816 ->getValue().getString()
801 ); 817 );
802 }
803 continue;
804 bReturnOnly = false;
805 bEscape = false;
806 break; 818 break;
807 819
808 // tBranch 820 // tBranch
@@ -838,7 +850,7 @@ void GameState::run()
838 } 850 }
839 } 851 }
840 break; 852 break;
841/* 853
842 case AstNode::tForEach: 854 case AstNode::tForEach:
843 { 855 {
844 AstBranch::NodeList lFe = 856 AstBranch::NodeList lFe =
@@ -868,7 +880,8 @@ void GameState::run()
868 getValue().getVariableRef(); 880 getValue().getVariableRef();
869 } 881 }
870 882
871 parse( dynamic_cast<const AstBranch *>(*iIn)->getNodeList() ); 883 run( dynamic_cast<const AstBranch *>(*iIn),
884 false );
872 Variable vIn = popDeref(); 885 Variable vIn = popDeref();
873 886
874 const AstBranch::NodeList &rDo = 887 const AstBranch::NodeList &rDo =
@@ -883,7 +896,7 @@ void GameState::run()
883 if( bUseKey ) 896 if( bUseKey )
884 setVariable( vrKey.sName, i.getKey(), vrKey.sid ); 897 setVariable( vrKey.sName, i.getKey(), vrKey.sid );
885 setVariable( vrValue.sName, i.getValue(), vrValue.sid ); 898 setVariable( vrValue.sName, i.getValue(), vrValue.sid );
886 parse( rDo ); 899 run( rDo, false );
887 } 900 }
888 } 901 }
889 else if( vIn.getType() == Variable::tList ) 902 else if( vIn.getType() == Variable::tList )
@@ -897,7 +910,7 @@ void GameState::run()
897 rList.begin(); i; i++ ) 910 rList.begin(); i; i++ )
898 { 911 {
899 setVariable( vrValue.sName, *i, vrValue.sid ); 912 setVariable( vrValue.sName, *i, vrValue.sid );
900 parse( rDo ); 913 run( rDo, false );
901 } 914 }
902 } 915 }
903 } 916 }
@@ -912,27 +925,31 @@ void GameState::run()
912 925
913 for(;;) 926 for(;;)
914 { 927 {
915 parse( dynamic_cast<const AstBranch *>(*iTest)-> 928 run( dynamic_cast<const AstBranch *>(*iTest)->
916 getNodeList() ); 929 getNodeList(), false );
917 Variable v = popDeref(); 930 Variable v = popDeref();
918 if( v.getType() != Variable::tBool ) 931 if( v.getType() != Variable::tBool )
919 throw Bu::ExceptionBase("conditional did not evaluate to boolean."); 932 throw Bu::ExceptionBase("conditional did not evaluate to boolean.");
920 if( !v.getBool() ) 933 if( !v.getBool() )
921 break; 934 break;
922 935
923 parse( dynamic_cast<const AstBranch *>(*iDo)-> 936 run( dynamic_cast<const AstBranch *>(*iDo)->
924 getNodeList() ); 937 getNodeList(), false );
925 } 938 }
926 } 939 }
927 break; 940 break;
928 */
929 } 941 }
930 942
943 if( sGoto.isSet() )
944 return;
945
931 ++sProg.peek(); 946 ++sProg.peek();
932 while( !sProg.isEmpty() && !sProg.peek() ) 947 while( !sProg.isEmpty() && !sProg.peek() )
933 { 948 {
934 sProg.pop(); 949 sProg.pop();
935 } 950 }
936 } 951 }
952 if( bNewScope )
953 lsLocal.pop();
937} 954}
938 955