summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/functiondisplay.cpp7
-rw-r--r--src/functionrandom.cpp30
-rw-r--r--src/functionrandom.h16
-rw-r--r--src/game.cpp3
-rw-r--r--src/gamestate.cpp3
-rw-r--r--src/main.cpp5
-rw-r--r--src/variable.cpp11
7 files changed, 69 insertions, 6 deletions
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()
15 15
16void FunctionDisplay::call( class GameState &gState ) 16void FunctionDisplay::call( class GameState &gState )
17{ 17{
18// Bu::String s = gState.popDeref().to( Variable::tString ).getString(); 18 Bu::String s = gState.popDeref().to( Variable::tString ).getString();
19 19 sio << format( s ) << sio.nl;
20// sio << format( s ) << sio.nl;
21 20
21/*
22 Variable v = gState.popDeref(); 22 Variable v = gState.popDeref();
23 sio << "Display: " << v << sio.nl; 23 sio << "Display: " << v << sio.nl;
24 */
24} 25}
25 26
26Bu::String FunctionDisplay::format( const Bu::String &sSrc ) 27Bu::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 @@
1#include "functionrandom.h"
2
3#include "gamestate.h"
4
5#include <stdlib.h>
6
7FunctionRandom::FunctionRandom()
8{
9}
10
11FunctionRandom::~FunctionRandom()
12{
13}
14
15void FunctionRandom::call( class GameState &gState )
16{
17 Variable vHigh = gState.popDeref();
18 Variable vLow = gState.popDeref();
19
20 if( vHigh.getType() != vLow.getType() )
21 throw Bu::ExceptionBase("Different types in random!");
22
23 if( vLow.getType() == Variable::tInt )
24 {
25 gState.push( Variable( (int64_t)(
26 (random()%(vHigh.getInt()-vLow.getInt()+1ll))+vLow.getInt()
27 ) ) );
28 }
29}
30
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 @@
1#ifndef FUNCTION_RANDOM_H
2#define FUNCTION_RANDOM_H
3
4#include "function.h"
5
6class FunctionRandom : public Function
7{
8public:
9 FunctionRandom();
10 virtual ~FunctionRandom();
11
12 virtual Bu::String getName() const { return "random"; }
13 virtual void call( class GameState &gState );
14};
15
16#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 @@
4#include "functionexists.h" 4#include "functionexists.h"
5#include "functiondelete.h" 5#include "functiondelete.h"
6#include "functionexit.h" 6#include "functionexit.h"
7#include "functionrandom.h"
7 8
8Game::Game() 9Game::Game()
9{ 10{
11 hGlobalParam.insert("start", Variable::newSituationName("start") );
10 addFunction( new FunctionDisplay() ); 12 addFunction( new FunctionDisplay() );
11 addFunction( new FunctionExists() ); 13 addFunction( new FunctionExists() );
12 addFunction( new FunctionDelete() ); 14 addFunction( new FunctionDelete() );
13 addFunction( new FunctionExit() ); 15 addFunction( new FunctionExit() );
16 addFunction( new FunctionRandom() );
14} 17}
15 18
16Game::~Game() 19Game::~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 )
465 { 465 {
466 Variable v = popDeref(); 466 Variable v = popDeref();
467 Variable x = popDeref(); 467 Variable x = popDeref();
468 if( v.getType() == Variable::tDictionary ) 468 if( v.getType() == Variable::tDictionary ||
469 v.getType() == Variable::tList )
469 { 470 {
470 push( Variable( v.has( x ) ) ); 471 push( Variable( v.has( x ) ) );
471 } 472 }
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 @@
3#include "gamestate.h" 3#include "gamestate.h"
4#include "parser.tab.h" 4#include "parser.tab.h"
5 5
6#include <stdlib.h>
7#include <time.h>
8
6#include <bu/sio.h> 9#include <bu/sio.h>
7using namespace Bu; 10using namespace Bu;
8 11
@@ -28,6 +31,8 @@ int main( int argc, char *argv[] )
28 31
29 fclose( in ); 32 fclose( in );
30 33
34 srandom( time( NULL ) );
35
31 Game *pGame = bld.getGame(); 36 Game *pGame = bld.getGame();
32 37
33 GameState gs( pGame ); 38 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 )
242{ 242{
243 if( eType == tDictionary ) 243 if( eType == tDictionary )
244 return hValue->has( vKey ); 244 return hValue->has( vKey );
245// else if( eType == tList ) 245 else if( eType == tList )
246// return lValue->contains( vKey ); 246 {
247 for( VariableList::const_iterator i = lValue->begin(); i; i++ )
248 {
249 if( (*i) == vKey )
250 return true;
251 }
252 return false;
253 }
247 else 254 else
248 throw Bu::ExceptionBase("Insert on non-dictionary."); 255 throw Bu::ExceptionBase("Insert on non-dictionary.");
249} 256}