From 55e6f570f5760e970c6523458914b5e4c63a6ce4 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 31 Dec 2011 00:13:13 -0700 Subject: Random function added, other fixes. --- demo.stage | 84 ++++++++++++++++++++++++++++++++----------------- src/functiondisplay.cpp | 7 +++-- src/functionrandom.cpp | 30 ++++++++++++++++++ src/functionrandom.h | 16 ++++++++++ src/game.cpp | 3 ++ src/gamestate.cpp | 3 +- src/main.cpp | 5 +++ src/variable.cpp | 11 +++++-- test.stage | 19 +---------- 9 files changed, 125 insertions(+), 53 deletions(-) create mode 100644 src/functionrandom.cpp create mode 100644 src/functionrandom.h diff --git a/demo.stage b/demo.stage index 00e7d94..e0e9768 100644 --- a/demo.stage +++ b/demo.stage @@ -45,47 +45,44 @@ global } } - // You should always have a global exit, quit, escape, something for - // dev-testing at least. - command: "exit" + command: "take" item { - exit(); + if exists(situation.items) then + { + if item in situation.items then + { + situation.items -= item; + player.inventory += item; + display("You take the " + item); + return(); + } + } + display("You don't see that anywhere."); } -} - -function square( x ) -{ - return( x*x ); -} -function sillyDisplay( txt, extra ) -{ - display("!~! " + txt + " !~!"); - if extra then + command: "inventory" { - display("And then some extra!"); + out = 'You are carrying: '; + for each item in player.inventory do + { + out += " " + item; + } + display( out ); } - else + + // You should always have a global exit, quit, escape, something for + // dev-testing at least. + command: "exit" { - display("...no extra for you"); + exit(); } } -function myGoto( txt ) -{ - display( txt ); - goto( txt ); -} - -function getThing() -{ - display( situation.thing ); -} - situation <> { setup { + player.inventory = []; goto( <> ); } @@ -102,6 +99,7 @@ situation <> 'south': <>, 'east': <> }; + situation.items = ['postcard']; } enter { @@ -127,15 +125,43 @@ situation <> situation <> { + command: "open" "cupboard" + { + if not situation.bCupboardOpen then + { + situation.bCupboardOpen = true; + if "pan" in situation.cupboardItems then + { + display("You open the cupboard, it's mostly empty. There is a + single frying pan inside."); + situation.cupboardItems -= "pan"; + situation.items += "pan"; + + } + else + { + display("You open the cupboard, it's empty."); + } + } + else + { + display("The cupboard is already open."); + } + } + setup { situation.exits = { 'west': <> }; + situation.bCupboardOpen = false; + situation.cupboardItems = ['pan']; + situation.items = []; } enter { - display('''Kitchen!'''); + display('''You are standing in the kitchen. There is an electric + range, a microwave, cupboards, a fridge, and a window.'''); } } diff --git a/src/functiondisplay.cpp b/src/functiondisplay.cpp index ab37a08..7328293 100644 --- a/src/functiondisplay.cpp +++ b/src/functiondisplay.cpp @@ -15,12 +15,13 @@ FunctionDisplay::~FunctionDisplay() void FunctionDisplay::call( class GameState &gState ) { -// Bu::String s = gState.popDeref().to( Variable::tString ).getString(); - -// sio << format( s ) << sio.nl; + Bu::String s = gState.popDeref().to( Variable::tString ).getString(); + sio << format( s ) << sio.nl; +/* Variable v = gState.popDeref(); sio << "Display: " << v << sio.nl; + */ } Bu::String FunctionDisplay::format( const Bu::String &sSrc ) diff --git a/src/functionrandom.cpp b/src/functionrandom.cpp new file mode 100644 index 0000000..2665a14 --- /dev/null +++ b/src/functionrandom.cpp @@ -0,0 +1,30 @@ +#include "functionrandom.h" + +#include "gamestate.h" + +#include + +FunctionRandom::FunctionRandom() +{ +} + +FunctionRandom::~FunctionRandom() +{ +} + +void FunctionRandom::call( class GameState &gState ) +{ + Variable vHigh = gState.popDeref(); + Variable vLow = gState.popDeref(); + + if( vHigh.getType() != vLow.getType() ) + throw Bu::ExceptionBase("Different types in random!"); + + if( vLow.getType() == Variable::tInt ) + { + gState.push( Variable( (int64_t)( + (random()%(vHigh.getInt()-vLow.getInt()+1ll))+vLow.getInt() + ) ) ); + } +} + diff --git a/src/functionrandom.h b/src/functionrandom.h new file mode 100644 index 0000000..b30f110 --- /dev/null +++ b/src/functionrandom.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_RANDOM_H +#define FUNCTION_RANDOM_H + +#include "function.h" + +class FunctionRandom : public Function +{ +public: + FunctionRandom(); + virtual ~FunctionRandom(); + + virtual Bu::String getName() const { return "random"; } + virtual void call( class GameState &gState ); +}; + +#endif diff --git a/src/game.cpp b/src/game.cpp index 910cec2..c19b039 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -4,13 +4,16 @@ #include "functionexists.h" #include "functiondelete.h" #include "functionexit.h" +#include "functionrandom.h" Game::Game() { + hGlobalParam.insert("start", Variable::newSituationName("start") ); addFunction( new FunctionDisplay() ); addFunction( new FunctionExists() ); addFunction( new FunctionDelete() ); addFunction( new FunctionExit() ); + addFunction( new FunctionRandom() ); } Game::~Game() diff --git a/src/gamestate.cpp b/src/gamestate.cpp index dcae848..2032d5c 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -465,7 +465,8 @@ void GameState::parse( const AstBranch::NodeList &lCode ) { Variable v = popDeref(); Variable x = popDeref(); - if( v.getType() == Variable::tDictionary ) + if( v.getType() == Variable::tDictionary || + v.getType() == Variable::tList ) { push( Variable( v.has( x ) ) ); } diff --git a/src/main.cpp b/src/main.cpp index b3dbba7..1f532d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,9 @@ #include "gamestate.h" #include "parser.tab.h" +#include +#include + #include using namespace Bu; @@ -28,6 +31,8 @@ int main( int argc, char *argv[] ) fclose( in ); + srandom( time( NULL ) ); + Game *pGame = bld.getGame(); GameState gs( pGame ); diff --git a/src/variable.cpp b/src/variable.cpp index 5296848..a3fb4a2 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -242,8 +242,15 @@ bool Variable::has( const Variable &vKey ) { if( eType == tDictionary ) return hValue->has( vKey ); -// else if( eType == tList ) -// return lValue->contains( vKey ); + else if( eType == tList ) + { + for( VariableList::const_iterator i = lValue->begin(); i; i++ ) + { + if( (*i) == vKey ) + return true; + } + return false; + } else throw Bu::ExceptionBase("Insert on non-dictionary."); } diff --git a/test.stage b/test.stage index deef755..3fe467c 100644 --- a/test.stage +++ b/test.stage @@ -17,24 +17,7 @@ situation <> { setup { - dict = {1: "Hello"}; - dict['bob'] = 'yup'; - display( dict ); - dict -= 1; - display( dict ); - - lst = [55]; - lst += 112; - display( lst ); - lst -= 55; - display( lst ); - lst += "hi"; - lst += "Things"; - display("---For each test---"); - for each x : y in dict do - { - display( x ); - } + display( random( 1, 3 ) ); exit(); } -- cgit v1.2.3