summaryrefslogtreecommitdiff
path: root/src/interfaceconsole.cpp
blob: e652da804cfa20a99c1a4a19252836fe0936298f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "interfaceconsole.h"

#include "smlnode.h"
#include "gamestate.h"
#include "situation.h"
#include "command.h"

#include <bu/sio.h>

#include <bu/plugger.h>

#include "smlrenderervt100.h"
#include <gats/types.h>
#include <gats/gatsstream.h>
#include <bu/file.h>

#include <stdlib.h>

PluginInterface3( plugin_interface_console, console, InterfaceConsole,
		Interface, "Mike Buland", 1, 0 );

using namespace Bu;

InterfaceConsole::InterfaceConsole()
{
}

InterfaceConsole::~InterfaceConsole()
{
}

void InterfaceConsole::run( Game *pGame )
{
	GameState gs( pGame, this );
	gs.init();

	int iStep = 0;
	while( gs.isRunning() )
	{
		char buf[1024];
		switch( gs.getCurSituation()->getInputType() )
		{
			case Situation::inputCommand:
				sio << sio.nl << "command> " << sio.flush;
				fgets( buf, 1024, stdin );

				gs.execCommand( buf );
				break;

			case Situation::inputOption:
				sio << sio.nl;
				{
					const CommandSet::CommandList &cl = 
						gs.getCurSituation()->getCommandSet().getCommandList();

					int j = 1;
					for( CommandSet::CommandList::const_iterator i = cl.begin();
						 i; i++ )
					{
						sio << "  " << j << ") " << (*i)->getRoot() << sio.nl;
						j++;
					}
					sio << "Enter your selection: " << sio.flush;
					fgets( buf, 1024, stdin );
					if( buf[0] < '1' || buf[0] > '9' )
						j = -1;
					else
						j = strtol( buf, NULL, 10 );
					if( j < 1 || j > cl.getSize() )
					{
						sio << "Invalid selection, try again." << sio.nl;
					}
					else
					{
						gs.execOption( j-1 );
					}
				}
				break;
		}
	}	
}

void InterfaceConsole::display( const class SmlNode *pSml )
{
	SmlRendererVt100 rend;
	rend.render( sio, pSml );
}