Cleaned up a bit

This commit is contained in:
Sachith Hasaranga Seneviratne
2014-03-21 15:31:28 +05:30
parent 4334d9a022
commit c1b8927b31
6 changed files with 80 additions and 162 deletions

View File

@@ -47,6 +47,7 @@
#include "race/race_manager.hpp"
#include "replay/replay_play.hpp"
#include "replay/replay_recorder.hpp"
#include "scriptengine/script_engine.hpp"
#include "states_screens/dialogs/race_paused_dialog.hpp"
#include "states_screens/race_gui_base.hpp"
#include "states_screens/main_menu_screen.hpp"
@@ -152,6 +153,7 @@ void World::init()
// Grab the track file
m_track = track_manager->getTrack(race_manager->getTrackName());
m_script_engine = new ScriptEngine();
if(!m_track)
{
std::ostringstream msg;

View File

@@ -41,7 +41,7 @@ class Controller;
class PhysicalObject;
class Physics;
class Track;
class ScriptEngine;
namespace irr
{
namespace scene { class ISceneNode; }
@@ -115,6 +115,9 @@ protected:
/** Pointer to the track. The track is managed by world. */
Track* m_track;
/**Pointer to scripting engine */
ScriptEngine* m_script_engine;
/** Pointer to the race GUI. The race GUI is handled by world. */
RaceGUIBase *m_race_gui;
@@ -301,6 +304,10 @@ public:
// ------------------------------------------------------------------------
/** Returns a pointer to the track. */
Track *getTrack() const { return m_track; }
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
/** Returns a pointer to the Scripting Engine. */
ScriptEngine *getScriptEngine() const { return m_script_engine; }
// ------------------------------------------------------------------------
/** The code that draws the timer should call this first to know
* whether the game mode wants a timer drawn. */

View File

@@ -3,16 +3,6 @@
#include <assert.h> // assert()
#include <string.h> // strstr()
#include "states_screens/dialogs/tutorial_message_dialog.hpp"
#ifdef _LINUX_
#include <sys/time.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#else
//#include <conio.h> // kbhit(), getch()
//#include <windows.h> // timeGetTime()
#endif
#include <angelscript.h>
#include "script_engine.hpp"
#include "scriptstdstring.h"
@@ -20,47 +10,35 @@
#include "tracks/track_object_manager.hpp"
#include "tracks/track.hpp"
//#define UINT unsigned int
//typedef unsigned int DWORD;
using namespace irr;
// Linux does have a getch() function in the curses library, but it doesn't
// work like it does on DOS. So this does the same thing, with out the need
// of the curses library.
/*int getch()
{
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
asIScriptEngine *m_engine;
// Function prototypes for binding. TODO:put these in their right place
void configureEngine(asIScriptEngine *engine);
int compileScript(asIScriptEngine *engine,std::string scriptName);
void printString(std::string &str);
void printString_Generic(asIScriptGeneric *gen);
ch = getchar();
ScriptEngine::ScriptEngine(){
// Create the script engine
m_engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
if( m_engine == 0 )
{
std::cout << "Failed to create script engine." << std::endl;
}
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
// Configure the script engine with all the functions,
// and variables that the script should be able to use.
configureEngine(m_engine);
}
*/
//#endif
// Function prototypes
int RunApplication(std::string);
void ConfigureEngine(asIScriptEngine *engine);
int CompileScript(asIScriptEngine *engine,std::string scriptName);
void PrintString(std::string &str);
void PrintString_Generic(asIScriptGeneric *gen);
//void timeGetTime_Generic(asIScriptGeneric *gen);
//void LineCallback(asIScriptContext *ctx, DWORD *timeOut);
ScriptEngineOne::ScriptEngineOne(){
ScriptEngine::~ScriptEngine(){
// Release the engine
m_engine->Release();
}
// Displays the message specified in displayMessage( string message ) within the script
void dispmsg(asIScriptGeneric *gen){
void displayMessage(asIScriptGeneric *gen){
std::string *input = (std::string*)gen->GetArgAddress(0);
irr::core::stringw msgtodisp;
irr::core::stringw out = irr::core::stringw((*input).c_str()); //irr::core::stringw supported by message dialogs
@@ -70,64 +48,27 @@ void disableAnimation(asIScriptGeneric *gen){
std::string *str = (std::string*)gen->GetArgAddress(0);
World::getWorld()->getTrack()->getTrackObjectManager()->disable(*str);
}
std::string ScriptEngineOne::doit(std::string scriptName)
void ScriptEngine::runScript(std::string scriptName)
{
//displaymsg();
fprintf(stderr, "inside engine");
RunApplication(scriptName);
// Wait until the user presses a key
//std::cout << std::endl << "Press any key to quit." << std::endl;
//while(!getch());
return "wot";
}
void MessageCallback(const asSMessageInfo *msg, void *param)
{
const char *type = "ERR ";
if( msg->type == asMSGTYPE_WARNING )
type = "WARN";
else if( msg->type == asMSGTYPE_INFORMATION )
type = "INFO";
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}
int RunApplication(std::string scriptName)
{
int r;
std::cout<<scriptName;
// Create the script engine
asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
if( engine == 0 )
{
std::cout << "Failed to create script engine." << std::endl;
return -1;
}
// The script compiler will write any compiler messages to the callback.
//engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
// Configure the script engine with all the functions,
// and variables that the script should be able to use.
ConfigureEngine(engine);
// Compile the script code
r = CompileScript(engine,scriptName);
r = compileScript(m_engine,scriptName);
if( r < 0 )
{
engine->Release();
return -1;
m_engine->Release();
return;
}
// Create a context that will execute the script.
asIScriptContext *ctx = engine->CreateContext();
asIScriptContext *ctx = m_engine->CreateContext();
if( ctx == 0 )
{
std::cout << "Failed to create the context." << std::endl;
engine->Release();
return -1;
m_engine->Release();
return;
}
// We don't want to allow the script to hang the application, e.g. with an
@@ -141,20 +82,20 @@ int RunApplication(std::string scriptName)
{
std::cout << "Failed to set the line callback function." << std::endl;
ctx->Release();
engine->Release();
return -1;
m_engine->Release();
return;
}
// Find the function for the function we want to execute.
//This is how you call a normal function with arguments
//asIScriptFunction *func = engine->GetModule(0)->GetFunctionByDecl("void onTrigger(float, float)");
asIScriptFunction *func = engine->GetModule(0)->GetFunctionByDecl("void onTrigger()");
asIScriptFunction *func = m_engine->GetModule(0)->GetFunctionByDecl("void onTrigger()");
if( func == 0 )
{
std::cout << "The function 'void onTrigger()' was not found." << std::endl;
ctx->Release();
engine->Release();
return -1;
m_engine->Release();
return;
}
// Prepare the script context with the function we wish to execute. Prepare()
@@ -167,20 +108,17 @@ int RunApplication(std::string scriptName)
{
std::cout << "Failed to prepare the context." << std::endl;
ctx->Release();
engine->Release();
return -1;
m_engine->Release();
return;
}
// Now we need to pass the parameters to the script function.
//ctx->SetArgFloat(0, 3.14159265359f);
//ctx->SetArgFloat(1, 2.71828182846f);
// Now we can pass parameters to the script function.
//ctx->setArgType(index, value);
//for example : ctx->SetArgFloat(0, 3.14159265359f);
// Execute the function
std::cout << "Executing the script." << std::endl;
std::cout << "---" << std::endl;
r = ctx->Execute();
std::cout << "---" << std::endl;
if( r != asEXECUTION_FINISHED )
{
// The execution didn't finish as we had planned. Determine why.
@@ -203,26 +141,20 @@ int RunApplication(std::string scriptName)
}
else
{
// Retrieve the return value from the context
// Retrieve the return value from the context here (for scripts that return values)
// <type> returnValue = ctx->getReturnType(); for example
//float returnValue = ctx->GetReturnFloat();
//std::cout << "The script function returned: " << returnValue << std::endl;
}
// We must release the contexts when no longer using them
ctx->Release();
// Release the engine
engine->Release();
return 0;
}
void ConfigureEngine(asIScriptEngine *engine)
void configureEngine(asIScriptEngine *engine)
{
int r;
// Register the script string type
// Look at the implementation for this function for more information
// on how to register a custom string type, and other object types.
RegisterStdString(engine);
if( !strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY") )
@@ -233,16 +165,16 @@ void ConfigureEngine(asIScriptEngine *engine)
// with a lot of if's. If an error occurs in release mode it will
// be caught when a script is being built, so it is not necessary
// to do the verification here as well.
r = engine->RegisterGlobalFunction("void Print(string &in)", asFUNCTION(PrintString), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterGlobalFunction("void Print(string &in)", asFUNCTION(printString), asCALL_CDECL); assert( r >= 0 );
}
else
{
// Notice how the registration is almost identical to the above.
r = engine->RegisterGlobalFunction("void Print(string &in)", asFUNCTION(PrintString_Generic), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterGlobalFunction("void Print(string &in)", asFUNCTION(printString_Generic), asCALL_GENERIC); assert( r >= 0 );
}
r = engine->RegisterGlobalFunction("void displayMessage(string &in)", asFUNCTION(dispmsg), asCALL_GENERIC); assert(r>=0);
r = engine->RegisterGlobalFunction("void displayMessage(string &in)", asFUNCTION(displayMessage), asCALL_GENERIC); assert(r>=0);
r = engine->RegisterGlobalFunction("void disableAnimation(string &in)", asFUNCTION(disableAnimation), asCALL_GENERIC); assert(r>=0);
// It is possible to register the functions, properties, and types in
// configuration groups as well. When compiling the scripts it then
@@ -252,21 +184,19 @@ void ConfigureEngine(asIScriptEngine *engine)
// without having to recompile all the scripts.
}
int CompileScript(asIScriptEngine *engine, std::string scriptName)
int compileScript(asIScriptEngine *engine, std::string scriptName)
{
int r;
// We will load the script from a file on the disk.
//std::string load_dir = "D:\\Github\\stk\\stk-code\\src\\scriptengine\\";
//std::string load_dir = "D:\\Github\\stk\\stk-code\\src\\scriptengine\\";
std::string load_dir = "//media//New Volume//Github//stk//stk-code//src//scriptengine//";
// For now we will load the script directtly from a file on the disk.
//TODO use filemanager to do this.
std::string load_dir = "D:\\Github\\stk\\stk-code\\src\\scriptengine\\";
//std::string load_dir = "//media//New Volume//Github//stk//stk-code//src//scriptengine//";
load_dir += scriptName + ".as";
FILE *f = fopen(load_dir.c_str(), "rb");
//FILE *f = fopen("D:\\Uni Torrents\\angelscript_2.28.1\\sdk\\samples\\tutorial\\bin\\script.as", "rb");
//FILE *f = fopen("//media//New Volume//Uni Torrents//angelscript_2.28.1//sdk//samples//tutorial//bin//script.as", "rb");
if( f == 0 )
{
std::cout << "Failed to open the script file 'script.as'." << std::endl;
std::cout << "Failed to open the script file " + scriptName + ".as" << std::endl;
return -1;
}
@@ -330,13 +260,13 @@ int CompileScript(asIScriptEngine *engine, std::string scriptName)
// Function implementation with native calling convention
void PrintString(std::string &str)
void printString(std::string &str)
{
std::cout << str;
}
// Function implementation with generic script interface
void PrintString_Generic(asIScriptGeneric *gen)
void printString_Generic(asIScriptGeneric *gen)
{
std::string *str = (std::string*)gen->GetArgAddress(0);
std::cout << *str;

View File

@@ -17,31 +17,22 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef HEADER_SCRIPT_ENGINE_ONE_HPP
#define HEADER_SCRIPT_ENGINE_ONE_HPP
#ifndef HEADER_SCRIPT_ENGINE_HPP
#define HEADER_SCRIPT_ENGINE_HPP
#include<string>
class TrackObjectPresentation;
class ScriptEngineOne
class ScriptEngine
{
public:
ScriptEngineOne();
// ~ScriptEngine();
void displayMessage();
ScriptEngine();
~ScriptEngine();
int five(){
return 5;
}
std::string doit(std::string scriptName);
std::string getout(){
return outval;
}
void runScript(std::string scriptName);
private:
std::string outval;
}; // class ScriptEngine
#endif

View File

@@ -716,33 +716,20 @@ void TrackObjectPresentationActionTrigger::onTriggerItemApproached(Item* who)
}
else if (m_action == "tutorial_bananas")
{
ScriptEngine* m_script_engine = World::getWorld()->getScriptEngine();
m_action_active = false;
ScriptEngineOne* m_script_engine = new ScriptEngineOne();
m_script_engine->doit(m_action);
m_script_engine->runScript(m_action);
}
else if (m_action == "haybail")
{
/*if (World::getWorld()->getTrack()->getTrackObjectManager()->getStatus("hayBail.b3d"))
World::getWorld()->getTrack()->getTrackObjectManager()->disable("hayBail.b3d");
else
World::getWorld()->getTrack()->getTrackObjectManager()->enable("hayBail.b3d");
/*to activate this add the following line to stk-assets/farm/scene.xml
<object type="action-trigger" action="haybail" distance="30.0" xyz="100.72 10.20 -26.22" hpr="0.0 -0.0 0.0" scale="7.00 7.00 7.00"/>
*/
//Check performance
/*
clock_t t1,t2;
t1=clock();
for (int i=0;i<20;i++){
ScriptEngineOne* m_script_engine = new ScriptEngineOne();
m_script_engine->doit(m_action);
t2=clock();
float diff ((double)t2-(double)t1);
std::cout << "System Time in milliseconds is " << 1000*diff/CLOCKS_PER_SEC<<std::endl;
*/
ScriptEngineOne* m_script_engine = new ScriptEngineOne();
m_script_engine->doit(m_action);
m_action_active=false;
ScriptEngine* m_script_engine = World::getWorld()->getScriptEngine();
m_script_engine->runScript(m_action);
}
else if (m_action == "tutorial_giftboxes")
{

View File

@@ -299,6 +299,7 @@ private:
bool m_action_active;
public: