summaryrefslogtreecommitdiffstats
path: root/src/scripting/luascript.cpp
diff options
context:
space:
mode:
authorErik Schilling <ablu.erikschilling@googlemail.com>2013-02-20 22:46:55 +0100
committerErik Schilling <ablu.erikschilling@googlemail.com>2013-02-21 07:29:13 +0100
commit4559ca444daacfd02ebb05f1657148a2b4cf3d8b (patch)
tree8410fe19f397e70d43a8542c6ba7339d61d9bc0e /src/scripting/luascript.cpp
parentad1d58b795681cad74642c0f4818b66a3f869794 (diff)
downloadmanaserv-4559ca444daacfd02ebb05f1657148a2b4cf3d8b.tar.gz
manaserv-4559ca444daacfd02ebb05f1657148a2b4cf3d8b.tar.xz
manaserv-4559ca444daacfd02ebb05f1657148a2b4cf3d8b.zip
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.
Diffstat (limited to 'src/scripting/luascript.cpp')
-rw-r--r--src/scripting/luascript.cpp25
1 files changed, 15 insertions, 10 deletions
diff --git a/src/scripting/luascript.cpp b/src/scripting/luascript.cpp
index f4ea39a..e9f4492 100644
--- a/src/scripting/luascript.cpp
+++ b/src/scripting/luascript.cpp
@@ -118,10 +118,12 @@ void LuaScript::push(const std::list<InventoryItem> &itemList)
++nbArgs;
}
-int LuaScript::execute()
+int LuaScript::execute(const Context &context)
{
assert(nbArgs >= 0);
+ const Context *previousContext = mContext;
+ mContext = &context;
const int tmpNbArgs = nbArgs;
nbArgs = -1;
@@ -139,7 +141,7 @@ int LuaScript::execute()
}
res = lua_tointeger(mCurrentState, -1);
lua_pop(mCurrentState, 1);
- setMap(0);
+ mContext = previousContext;
return res;
}
@@ -148,7 +150,9 @@ bool LuaScript::resume()
assert(nbArgs >= 0);
assert(mCurrentThread);
- setMap(mCurrentThread->mMap);
+ const Context *previousContext = mContext;
+ mContext = &mCurrentThread->getContext();
+
const int tmpNbArgs = nbArgs;
nbArgs = -1;
#if LUA_VERSION_NUM < 502
@@ -156,7 +160,6 @@ bool LuaScript::resume()
#else
int result = lua_resume(mCurrentState, NULL, tmpNbArgs);
#endif
- setMap(0);
if (result == 0) // Thread is done
{
@@ -181,6 +184,7 @@ bool LuaScript::resume()
}
lua_settop(mCurrentState, 0);
+ mContext = previousContext;
const bool done = result != LUA_YIELD;
if (done)
@@ -215,8 +219,11 @@ void LuaScript::unref(Ref &ref)
}
}
-void LuaScript::load(const char *prog, const char *name)
+void LuaScript::load(const char *prog, const char *name,
+ const Context &context)
{
+ const Context *previousContext = mContext;
+ mContext = &context;
int res = luaL_loadbuffer(mRootState, prog, std::strlen(prog), name);
if (res)
{
@@ -238,19 +245,18 @@ void LuaScript::load(const char *prog, const char *name)
<< lua_tostring(mRootState, -1));
lua_pop(mRootState, 1);
}
- setMap(0);
+ mContext = previousContext;
}
void LuaScript::processDeathEvent(Being *entity)
{
if (mDeathNotificationCallback.isValid())
{
- setMap(entity->getMap());
prepare(mDeathNotificationCallback);
push(entity);
//TODO: get and push a list of creatures who contributed to killing the
// being. This might be very interesting for scripting quests.
- execute();
+ Script::execute(entity->getMap());
}
}
@@ -258,12 +264,11 @@ void LuaScript::processRemoveEvent(Entity *entity)
{
if (mRemoveNotificationCallback.isValid())
{
- setMap(entity->getMap());
prepare(mRemoveNotificationCallback);
push(entity);
//TODO: get and push a list of creatures who contributed to killing the
// being. This might be very interesting for scripting quests.
- execute();
+ Script::execute(entity->getMap());
}
}