summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/game-server/trigger.cpp17
-rw-r--r--src/game-server/trigger.h5
-rw-r--r--src/scripting/lua.cpp27
-rw-r--r--src/scripting/luascript.cpp11
-rw-r--r--src/scripting/luascript.h3
-rw-r--r--src/scripting/script.h6
6 files changed, 30 insertions, 39 deletions
diff --git a/src/game-server/trigger.cpp b/src/game-server/trigger.cpp
index c4cec2f4..dd1392e1 100644
--- a/src/game-server/trigger.cpp
+++ b/src/game-server/trigger.cpp
@@ -27,6 +27,8 @@
#include "utils/logger.h"
+#include <cassert>
+
void WarpAction::process(Actor *obj)
{
if (obj->getType() == OBJECT_CHARACTER)
@@ -35,13 +37,20 @@ void WarpAction::process(Actor *obj)
}
}
+ScriptAction::ScriptAction(Script *script, Script::Ref callback, int arg) :
+ mScript(script),
+ mCallback(callback),
+ mArg(arg)
+{
+ assert(mCallback.isValid());
+}
+
void ScriptAction::process(Actor *obj)
{
- LOG_DEBUG("Script trigger area activated: " << mFunction
+ LOG_DEBUG("Script trigger area activated: "
<< "(" << obj << ", " << mArg << ")");
- if (!mScript || mFunction.empty())
- return;
- mScript->prepare(mFunction);
+
+ mScript->prepare(mCallback);
mScript->push(obj);
mScript->push(mArg);
mScript->execute();
diff --git a/src/game-server/trigger.h b/src/game-server/trigger.h
index f6f0e170..2a3aa611 100644
--- a/src/game-server/trigger.h
+++ b/src/game-server/trigger.h
@@ -50,14 +50,13 @@ class WarpAction : public TriggerAction
class ScriptAction : public TriggerAction
{
public:
- ScriptAction(Script *script, const std::string &function, int arg)
- : mScript(script), mFunction(function), mArg(arg) {}
+ ScriptAction(Script *script, Script::Ref callback, int arg);
virtual void process(Actor *obj);
private:
Script *mScript; // Script object to be called
- std::string mFunction; // Name of the function called in the script object
+ Script::Ref mCallback; // Reference to the function to call
int mArg; // Argument passed to script function (meaning is function-specific)
};
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index 59e7d2e1..9bd35ddd 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -1681,10 +1681,10 @@ static int chr_set_quest(lua_State *s)
}
/**
- * mana.trigger_create(int x, int y, int width, int height,
- * string function, int id)
- * Creates a trigger area. Whenever an actor enters this area, a Lua function
- * is called.
+ * mana.trigger_create(int x, int y, int width, int height, function, int id,
+ * boolean once)
+ * Creates a trigger area. Whenever an actor enters this area, the given Lua
+ * function is called.
*/
static int trigger_create(lua_State *s)
{
@@ -1692,8 +1692,7 @@ static int trigger_create(lua_State *s)
const int y = luaL_checkint(s, 2);
const int width = luaL_checkint(s, 3);
const int height = luaL_checkint(s, 4);
- //TODO: Turn the function string to a lua function pointer
- const char *function = luaL_checkstring(s, 5);
+ luaL_checktype(s, 5, LUA_TFUNCTION);
const int id = luaL_checkint(s, 6);
if (!lua_isboolean(s, 7))
@@ -1703,12 +1702,6 @@ static int trigger_create(lua_State *s)
}
Script *script = getScript(s);
- bool once = lua_toboolean(s, 7);
-
- LOG_INFO("Created script trigger at " << x << ":" << y
- << " (" << width << "x" << height << ") function: " << function
- << " (" << id << ")");
-
MapComposite *m = script->getMap();
if (!m)
@@ -1717,10 +1710,20 @@ static int trigger_create(lua_State *s)
return 0;
}
+ const bool once = lua_toboolean(s, 7);
+
+ Script::Ref function;
+ lua_pushvalue(s, 5);
+ script->assignCallback(function);
+ lua_pop(s, 1);
+
ScriptAction *action = new ScriptAction(script, function, id);
Rectangle r = { x, y, width, height };
TriggerArea *area = new TriggerArea(m, r, action, once);
+ LOG_INFO("Created script trigger at " << x << "," << y
+ << " (" << width << "x" << height << ") id: " << id);
+
bool ret = GameState::insert(area);
lua_pushboolean(s, ret);
return 1;
diff --git a/src/scripting/luascript.cpp b/src/scripting/luascript.cpp
index 27361579..231dece1 100644
--- a/src/scripting/luascript.cpp
+++ b/src/scripting/luascript.cpp
@@ -47,15 +47,6 @@ void LuaScript::prepare(Ref function)
lua_rawgeti(mState, LUA_REGISTRYINDEX, function.value);
assert(lua_isfunction(mState, -1));
nbArgs = 0;
- mCurFunction = "<callback>"; // We don't know the function name
-}
-
-void LuaScript::prepare(const std::string &name)
-{
- assert(nbArgs == -1);
- lua_getglobal(mState, name.c_str());
- nbArgs = 0;
- mCurFunction = name;
}
void LuaScript::push(int v)
@@ -112,7 +103,6 @@ int LuaScript::execute()
LOG_WARN("Lua Script Error" << std::endl
<< " Script : " << mScriptFile << std::endl
- << " Function: " << mCurFunction << std::endl
<< " Error : " << (s ? s : "") << std::endl);
lua_pop(mState, 1);
return 0;
@@ -120,7 +110,6 @@ int LuaScript::execute()
res = lua_tointeger(mState, -1);
lua_pop(mState, 1);
return res;
- mCurFunction.clear();
}
void LuaScript::assignCallback(Script::Ref &function)
diff --git a/src/scripting/luascript.h b/src/scripting/luascript.h
index ecd249ae..6ea44bac 100644
--- a/src/scripting/luascript.h
+++ b/src/scripting/luascript.h
@@ -46,8 +46,6 @@ class LuaScript : public Script
void prepare(Ref function);
- void prepare(const std::string &);
-
void push(int);
void push(const std::string &);
@@ -86,7 +84,6 @@ class LuaScript : public Script
private:
lua_State *mState;
int nbArgs;
- std::string mCurFunction;
static Ref mQuestReplyCallback;
static Ref mPostReplyCallback;
diff --git a/src/scripting/script.h b/src/scripting/script.h
index bbfc7691..2fa3a0bf 100644
--- a/src/scripting/script.h
+++ b/src/scripting/script.h
@@ -102,12 +102,6 @@ class Script
virtual void prepare(Ref function) = 0;
/**
- * Prepares a call to the given function.
- * Only one function can be prepared at once.
- */
- virtual void prepare(const std::string &name) = 0;
-
- /**
* Pushes an integer argument for the function being prepared.
*/
virtual void push(int) = 0;