Click here to Skip to main content
15,885,365 members
Articles / Desktop Programming / MFC

2D LUA Based Robot Simulator

Rate me:
Please Sign up or sign in to vote.
4.89/5 (26 votes)
14 Apr 2014Public Domain9 min read 130.7K   7.9K   119  
An article on designing your own robot simulator
#include "StdAfx.h"
#include "robot.h"
#include "Evaluate.h"
///////////////////////////////////////////////////////////////////////////////

CMyScript::CMyScript(CLuaVirtualMachine &vm)
:CLuaScript (vm)
{
	m_iMethodBase = RegisterFunction ("setspeed");
	RegisterFunction ("readsensor");
	RegisterFunction ("getangle");
	RegisterFunction ("textline");
	RegisterFunction ("setactiverobot");
	RegisterFunction ("getnumberofrobot");
}

CMyScript::~CMyScript()
{
}

int CMyScript::ScriptCalling (CLuaVirtualMachine& vm, int iFunctionNumber)
{
	switch (iFunctionNumber - m_iMethodBase)
	{
	case 0:
		return setspeed(vm);
	case 1:
		return readsensor(vm);
	case 2:
		return getangle(vm);
	case 3:
		return textline(vm);
	case 4:
		return setactiverobot(vm);
	case 5:
		return getnumberofrobot(vm);
	}
	return -1;
}

void CMyScript::HandleReturns (CLuaVirtualMachine& vm, const char *strFunc)
{
}

int CMyScript::setspeed(CLuaVirtualMachine& vm)
{
	lua_State *state = (lua_State *) vm;

	float wRight = (float) lua_tonumber (state, -1);
	float wLeft = (float) lua_tonumber (state, -2);

	m_robot->SetSpeed(wLeft, wRight);

	Sleep(2);
	return 0;
}

int CMyScript::readsensor(CLuaVirtualMachine& vm)
{
	lua_State *state = (lua_State *) vm;
	int index = (int) lua_tonumber (state, -1);
	m_robot->GetSensorValue(index);
	float retVal = m_robot->GetSensorValue(index);
	lua_pushnumber (state, retVal);

	Sleep(2);	
	return 1;	// We are returning 1 stack
}

int CMyScript::getangle(CLuaVirtualMachine& vm)
{
	lua_State *state = (lua_State *) vm;
	float retVal = m_robot->GetAngle();
	lua_pushnumber (state, retVal);

	Sleep(2);	
	return 1;	// We are returning 1 stack
}

int CMyScript::textline(CLuaVirtualMachine &vm)
{
	lua_State *state = (lua_State *) vm;
	size_t len;
	const char *msg = lua_tolstring (state, -1, &len);
	
	sprintf(m_msg, "DEBUG: %s", msg);

	Sleep(2);
	return 0;
}

int CMyScript::setactiverobot(CLuaVirtualMachine &vm)
{
	lua_State *state = (lua_State *) vm;
	int index = (int) lua_tonumber (state, -1);

	m_robot->SetActiveRobot(index);

	Sleep(2);
	return 0;
}

int CMyScript::getnumberofrobot(CLuaVirtualMachine& vm)
{
	lua_State *state = (lua_State *) vm;
	int retVal = m_robot->GetNumberOfRobot();
	lua_pushnumber (state, retVal);

	Sleep(2);	
	return 1;	// We are returning 1 stack
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

CEvaluate::CEvaluate(void)
{
	// Init virtual machine
	m_luaVM.InitialiseVM ();
	strcpy(m_errorMsg, "\n");
}

CEvaluate::~CEvaluate(void)
{

}

int CEvaluate::run()
{
	// Get "main" function
	int iTopS = lua_gettop ((lua_State *) m_luaVM);
	m_ms->SelectScriptFunction ("main");
	m_ms->Go();

	// Clean up
	delete m_dbg;
	delete m_ms;

	m_bDone = true;

	return 0;
}

DWORD WINAPI CEvaluate::workerThread(LPVOID param)
{
	CEvaluate *newobj = (CEvaluate *)param;
	newobj->run();

	return 0;
}

int CEvaluate::init(CString codes, CRobot *robot)
{
	//Init scripter and debugger
	m_bDone = false;
	m_ms = new CMyScript(m_luaVM);
	m_dbg = new CLuaDebugger(m_luaVM);

	m_dbg->SetCount (10);
	m_dbg->SetHooksFlag (DBG_MASK_LINE); // Line Hook

	m_ms->m_robot = robot;
	m_ms->m_msg = getMsg();

	// Error happens
	if (m_ms->CompileBuffer((unsigned char *)codes.GetBuffer(0), codes.GetLength()) == false){
		m_dbg->GetErrorMsg(m_errorMsg);

		// Clean up
		delete m_dbg;
		delete m_ms;

		return -1; // FAIL
	}

	DWORD thid;
	HANDLE hThread = CreateThread(NULL, 0, workerThread, this, 0, &thid);
	if (!hThread)
		return -1;

	return 0; // SUCCESS
}

char *CEvaluate::getMsg()
{	
	return m_errorMsg;
}

void CEvaluate::setQuitFlag()
{
	m_dbg->Abort();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Student
Indonesia Indonesia
http://kataauralius.com/

Comments and Discussions