summaryrefslogtreecommitdiff
path: root/src/interfacegats.cpp
blob: d2afce307f73cb28597c39f05833f81a0c7ba28a (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "interfacegats.h"

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

#include "smlrendererhtml.h"

#include <bu/plugger.h>
#include <bu/sio.h>
#include <bu/file.h>
#include <bu/streamstack.h>
#include <gats/gatsstream.h>
#include <gats/types.h>
#include <stdlib.h>

using namespace Bu;

PluginInterface3( plugin_interface_gats, gats, InterfaceGats,
		Interface, "Mike Buland", 1, 0 );

InterfaceGats::InterfaceGats() :
	fResult( mbResult )
{
}

InterfaceGats::~InterfaceGats()
{
}

void InterfaceGats::run( class Game *pGame )
{
	GameState gs( pGame, this );

	Gats::GatsStream gsIn( sioRaw );
	{
		Gats::Object *pObj = gsIn.readObject();

		if( pObj )
		{
			gs.fromGats( dynamic_cast<Gats::Dictionary *>(pObj) );
			
			delete pObj;

			Situation *pSit = gs.getCurSituation();
			switch( pSit->getInputType() )
			{
				case Situation::inputCommand:
					gs.execCommand( Options::getInstance().sCommand );
					break;

				case Situation::inputOption:
					gs.execOption( strtol( Options::getInstance().sCommand.getStr(), NULL, 10 ) );
					break;
			}
		}
		else
		{
			gs.init();
		}
	}

	{
		Gats::Dictionary *pDict;
		if( gs.isRunning() )
		{
			pDict = gs.toGats();
			pDict->insertBool("running", true );
		}
		else
		{
			pDict = new Gats::Dictionary();
			pDict->insertBool("running", false );
		}
		
		Situation *pSit = gs.getCurSituation();
		pDict->insert("input", (int)pSit->getInputType() );
		switch( pSit->getInputType() )
		{
			case Situation::inputCommand:
				break;

			case Situation::inputOption:
				{
					Gats::List *pLst = pDict->insertList("options");
					const CommandSet::CommandList &cl =
						pSit->getCommandSet().getCommandList();
					for( CommandSet::CommandList::const_iterator i = cl.begin();
						 i; i++ )
					{
						pLst->append( (*i)->getRoot() );
					}
				}
				break;
		}
		
		pDict->insert("result", mbResult.getString() );
		gsIn.writeObject( pDict );
		delete pDict;
	}
}

void InterfaceGats::display( const SmlNode *pSml )
{
	SmlRendererHtml rend;
	rend.render( fResult, pSml );
}