summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--demo.stage84
-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
-rw-r--r--test.stage19
9 files changed, 125 insertions, 53 deletions
diff --git a/demo.stage b/demo.stage
index 00e7d94..e0e9768 100644
--- a/demo.stage
+++ b/demo.stage
@@ -45,47 +45,44 @@ global
45 } 45 }
46 } 46 }
47 47
48 // You should always have a global exit, quit, escape, something for 48 command: "take" item
49 // dev-testing at least.
50 command: "exit"
51 { 49 {
52 exit(); 50 if exists(situation.items) then
51 {
52 if item in situation.items then
53 {
54 situation.items -= item;
55 player.inventory += item;
56 display("You take the " + item);
57 return();
58 }
59 }
60 display("You don't see that anywhere.");
53 } 61 }
54}
55
56function square( x )
57{
58 return( x*x );
59}
60 62
61function sillyDisplay( txt, extra ) 63 command: "inventory"
62{
63 display("!~! " + txt + " !~!");
64 if extra then
65 { 64 {
66 display("And then some extra!"); 65 out = 'You are carrying: ';
66 for each item in player.inventory do
67 {
68 out += " " + item;
69 }
70 display( out );
67 } 71 }
68 else 72
73 // You should always have a global exit, quit, escape, something for
74 // dev-testing at least.
75 command: "exit"
69 { 76 {
70 display("...no extra for you"); 77 exit();
71 } 78 }
72} 79}
73 80
74function myGoto( txt )
75{
76 display( txt );
77 goto( txt );
78}
79
80function getThing()
81{
82 display( situation.thing );
83}
84
85situation <<start>> 81situation <<start>>
86{ 82{
87 setup 83 setup
88 { 84 {
85 player.inventory = [];
89 goto( <<diningRoom>> ); 86 goto( <<diningRoom>> );
90 } 87 }
91 88
@@ -102,6 +99,7 @@ situation <<diningRoom>>
102 'south': <<livingRoom>>, 99 'south': <<livingRoom>>,
103 'east': <<kitchen>> 100 'east': <<kitchen>>
104 }; 101 };
102 situation.items = ['postcard'];
105 } 103 }
106 enter 104 enter
107 { 105 {
@@ -127,15 +125,43 @@ situation <<livingRoom>>
127 125
128situation <<kitchen>> 126situation <<kitchen>>
129{ 127{
128 command: "open" "cupboard"
129 {
130 if not situation.bCupboardOpen then
131 {
132 situation.bCupboardOpen = true;
133 if "pan" in situation.cupboardItems then
134 {
135 display("You open the cupboard, it's mostly empty. There is a
136 single frying pan inside.");
137 situation.cupboardItems -= "pan";
138 situation.items += "pan";
139
140 }
141 else
142 {
143 display("You open the cupboard, it's empty.");
144 }
145 }
146 else
147 {
148 display("The cupboard is already open.");
149 }
150 }
151
130 setup 152 setup
131 { 153 {
132 situation.exits = { 154 situation.exits = {
133 'west': <<diningRoom>> 155 'west': <<diningRoom>>
134 }; 156 };
157 situation.bCupboardOpen = false;
158 situation.cupboardItems = ['pan'];
159 situation.items = [];
135 } 160 }
136 161
137 enter 162 enter
138 { 163 {
139 display('''Kitchen!'''); 164 display('''You are standing in the kitchen. There is an electric
165 range, a microwave, cupboards, a fridge, and a window.''');
140 } 166 }
141} 167}
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}
diff --git a/test.stage b/test.stage
index deef755..3fe467c 100644
--- a/test.stage
+++ b/test.stage
@@ -17,24 +17,7 @@ situation <<start>>
17{ 17{
18 setup 18 setup
19 { 19 {
20 dict = {1: "Hello"}; 20 display( random( 1, 3 ) );
21 dict['bob'] = 'yup';
22 display( dict );
23 dict -= 1;
24 display( dict );
25
26 lst = [55];
27 lst += 112;
28 display( lst );
29 lst -= 55;
30 display( lst );
31 lst += "hi";
32 lst += "Things";
33 display("---For each test---");
34 for each x : y in dict do
35 {
36 display( x );
37 }
38 exit(); 21 exit();
39 } 22 }
40 23