From 4559ca444daacfd02ebb05f1657148a2b4cf3d8b Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Wed, 20 Feb 2013 22:46:55 +0100 Subject: Introduced Script::Context This should allow to finally call functions to lua without having to care about working around situations where a lua call causes a c++ call which needs to call to lua again. Tested against the source of tales repository data. --- src/scripting/script.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'src/scripting/script.cpp') diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp index 63ab7ff4..72943d43 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -40,7 +40,7 @@ Script::Ref Script::mUpdateCallback; Script::Script(): mCurrentThread(0), - mMap(0) + mContext(0) {} Script::~Script() @@ -95,14 +95,14 @@ static char *skipPotentialBom(char *text) return (strncmp(text, utf8Bom, bomLength) == 0) ? text + bomLength : text; } -bool Script::loadFile(const std::string &name) +bool Script::loadFile(const std::string &name, const Context &context) { int size; char *buffer = ResourceManager::loadFile(name, size); if (buffer) { mScriptFile = name; - load(skipPotentialBom(buffer), name.c_str()); + load(skipPotentialBom(buffer), name.c_str(), context); free(buffer); return true; } else { @@ -114,7 +114,8 @@ void Script::loadNPC(const std::string &name, int id, ManaServ::BeingGender gender, int x, int y, - const char *prog) + const char *prog, + MapComposite *map) { if (!mCreateNpcDelayedCallback.isValid()) { @@ -122,14 +123,23 @@ void Script::loadNPC(const std::string &name, "Could not enabled NPC"); return; } - load(prog, name.c_str()); + Context context; + context.map = map; + load(prog, name.c_str(), context); prepare(mCreateNpcDelayedCallback); push(name); push(id); push(gender); push(x); push(y); - execute(); + execute(context); +} + +int Script::execute(MapComposite *map) +{ + Context context; + context.map = map; + return execute(context); } @@ -153,8 +163,7 @@ static void fastRemoveOne(std::vector &vector, T value) Script::Thread::Thread(Script *script) : mScript(script), - mState(ThreadPending), - mMap(0) + mState(ThreadPending) { script->mThreads.push_back(this); } -- cgit v1.2.3-70-g09d2