From 3232d0069421a1585e7e42f503c1ed7b1910891c Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 18 Jan 2012 23:58:30 -0700 Subject: Added functions join, string, and keys. --- demo.stage | 28 +++++++--------------------- src/functionjoin.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/functionjoin.h | 16 ++++++++++++++++ src/functionkeys.cpp | 32 ++++++++++++++++++++++++++++++++ src/functionkeys.h | 16 ++++++++++++++++ src/functionstring.cpp | 17 +++++++++++++++++ src/functionstring.h | 16 ++++++++++++++++ src/game.cpp | 6 ++++++ 8 files changed, 146 insertions(+), 21 deletions(-) create mode 100644 src/functionjoin.cpp create mode 100644 src/functionjoin.h create mode 100644 src/functionkeys.cpp create mode 100644 src/functionkeys.h create mode 100644 src/functionstring.cpp create mode 100644 src/functionstring.h diff --git a/demo.stage b/demo.stage index c644522..852deee 100644 --- a/demo.stage +++ b/demo.stage @@ -31,21 +31,10 @@ global { if exists(situation.exits) then { - out = "Obvious exits are: "; - bFirst = true; - for each dir : dest in situation.exits do - { - if bFirst then - { - bFirst = false; - } - else - { - out += ", "; - } - out += dir; - } - display( out ); + display( "Obvious exits are: " + join( + keys( situation.exits ), + ", " + ) ); } else { @@ -77,17 +66,13 @@ global */ command: "inventory" { - out = 'You are carrying: '; - for each item in player.inventory do - { - out += " " + item; - } + out = 'You are carrying: ' + join( player.inventory, ", " ); display( out ); } // You should always have a global exit, quit, escape, something for // dev-testing at least. - command: "exit" + command: "quit" { exit(); } @@ -147,6 +132,7 @@ situation <> 'east': <>, 'west': <> }; + situation.items = ['stick']; } enter { diff --git a/src/functionjoin.cpp b/src/functionjoin.cpp new file mode 100644 index 0000000..71ee879 --- /dev/null +++ b/src/functionjoin.cpp @@ -0,0 +1,36 @@ +#include "functionjoin.h" + +#include "gamestate.h" + +#include +using namespace Bu; + +FunctionJoin::FunctionJoin() +{ +} + +FunctionJoin::~FunctionJoin() +{ +} + +void FunctionJoin::call( class GameState &gState ) +{ + Variable vSep = gState.popDeref().to( Variable::tString ); + Variable vLst = gState.popDeref(); + + if( vLst.getType() != Variable::tList ) + throw Bu::ExceptionBase("First parameter of join must be a list."); + + Bu::String sOut; + for( Variable::VariableArray::const_iterator i = vLst.getList().begin(); + i; i++ ) + { + if( i != vLst.getList().begin() ) + sOut += vSep.getString(); + sOut += (*i).to( Variable::tString ).getString(); + } + + + gState.push( sOut ); +} + diff --git a/src/functionjoin.h b/src/functionjoin.h new file mode 100644 index 0000000..7c46e7f --- /dev/null +++ b/src/functionjoin.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_JOIN_H +#define FUNCTION_JOIN_H + +#include "function.h" + +class FunctionJoin : public Function +{ +public: + FunctionJoin(); + virtual ~FunctionJoin(); + + virtual Bu::String getName() const { return "join"; } + virtual void call( class GameState &gState ); +}; + +#endif diff --git a/src/functionkeys.cpp b/src/functionkeys.cpp new file mode 100644 index 0000000..12c1098 --- /dev/null +++ b/src/functionkeys.cpp @@ -0,0 +1,32 @@ +#include "functionkeys.h" + +#include "gamestate.h" + +#include +using namespace Bu; + +FunctionKeys::FunctionKeys() +{ +} + +FunctionKeys::~FunctionKeys() +{ +} + +void FunctionKeys::call( class GameState &gState ) +{ + Variable vDict = gState.popDeref(); + + if( vDict.getType() != Variable::tDictionary ) + throw Bu::ExceptionBase("Parameter to keys must be a dictionary."); + + Variable vLst( Variable::tList ); + for( Variable::VariableHash::const_iterator i = vDict.getHash().begin(); + i; i++ ) + { + vLst += i.getKey(); + } + + gState.push( vLst ); +} + diff --git a/src/functionkeys.h b/src/functionkeys.h new file mode 100644 index 0000000..ccc3bdd --- /dev/null +++ b/src/functionkeys.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_KEYS_H +#define FUNCTION_KEYS_H + +#include "function.h" + +class FunctionKeys : public Function +{ +public: + FunctionKeys(); + virtual ~FunctionKeys(); + + virtual Bu::String getName() const { return "keys"; } + virtual void call( class GameState &gState ); +}; + +#endif diff --git a/src/functionstring.cpp b/src/functionstring.cpp new file mode 100644 index 0000000..ca5a3aa --- /dev/null +++ b/src/functionstring.cpp @@ -0,0 +1,17 @@ +#include "functionstring.h" + +#include "gamestate.h" + +FunctionString::FunctionString() +{ +} + +FunctionString::~FunctionString() +{ +} + +void FunctionString::call( class GameState &gState ) +{ + gState.push( gState.popDeref().to( Variable::tString ) ); +} + diff --git a/src/functionstring.h b/src/functionstring.h new file mode 100644 index 0000000..36e4f51 --- /dev/null +++ b/src/functionstring.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_STRING_H +#define FUNCTION_STRING_H + +#include "function.h" + +class FunctionString : public Function +{ +public: + FunctionString(); + virtual ~FunctionString(); + + virtual Bu::String getName() const { return "string"; } + virtual void call( class GameState &gState ); +}; + +#endif diff --git a/src/game.cpp b/src/game.cpp index 4971c4b..37989bd 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -8,6 +8,9 @@ #include "functioninteger.h" #include "functionfloat.h" #include "functiondebugstring.h" +#include "functionstring.h" +#include "functionjoin.h" +#include "functionkeys.h" Game::Game() { @@ -20,6 +23,9 @@ Game::Game() addFunction( new FunctionInteger() ); addFunction( new FunctionFloat() ); addFunction( new FunctionDebugString() ); + addFunction( new FunctionString() ); + addFunction( new FunctionJoin() ); + addFunction( new FunctionKeys() ); } Game::~Game() -- cgit v1.2.3