From 393d066045fda5beee4748dc66754d70b4c82d36 Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Sat, 31 Jan 2009 21:49:58 +0100 Subject: Added on_remove callback to lua API --- src/scripting/lua.cpp | 18 ++++++++++-------- src/scripting/luascript.cpp | 11 ++++++++++- src/scripting/luascript.hpp | 2 ++ src/scripting/script.cpp | 2 +- src/scripting/script.hpp | 13 ++++++++----- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index b9f30f4e..f9d2021d 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -768,15 +768,16 @@ static int chr_get_post(lua_State *s) } /** - * Makes the server call the lua function deathEvent - * with the being ID when the being dies. - * tmw.note_on_death (being) + * Makes the server call the lua functions deathEvent + * and removeEvent when the being dies or is removed + * from the map. + * tmw.being_register (being) */ -static int note_on_death(lua_State *s) +static int being_register(lua_State *s) { if (!lua_islightuserdata(s, 1) || lua_gettop(s) != 1) { - raiseScriptError(s, "lua_noteOnDeath called with incorrect parameters."); + raiseScriptError(s, "being_register called with incorrect parameters."); return 0; } @@ -786,14 +787,15 @@ static int note_on_death(lua_State *s) Being *being = getBeing(s, 1); if (!being) { - raiseScriptError(s, "lua_noteOnDeath called for nonexistent being."); + raiseScriptError(s, "being_register called for nonexistent being."); return 0; } - being->addListener(t->getScriptDeathListener()); + being->addListener(t->getScriptListener()); return 0; } + /** * Triggers a special effect from the clients effects.xml * tmw.effect_create (id, x, y) @@ -1006,7 +1008,7 @@ LuaScript::LuaScript(): { "trigger_create", &trigger_create }, { "chatmessage", &chatmessage }, { "get_beings_in_circle", &get_beings_in_circle }, - { "note_on_death", ¬e_on_death }, + { "being_register", &being_register }, { "effect_create", &effect_create }, { "test_tableget", &test_tableget }, { "get_map_id", &get_map_id }, diff --git a/src/scripting/luascript.cpp b/src/scripting/luascript.cpp index d2083952..c88b95fd 100644 --- a/src/scripting/luascript.cpp +++ b/src/scripting/luascript.cpp @@ -112,8 +112,17 @@ void LuaScript::processDeathEvent(Being *being) //TODO: get and push a list of creatures who contributed to killing the // being. This might be very interesting for scripting quests. execute(); +} + +void LuaScript::processRemoveEvent(Thing *being) +{ + prepare("remove_notification"); + push(being); + //TODO: get and push a list of creatures who contributed to killing the + // being. This might be very interesting for scripting quests. + execute(); - being->removeListener(getScriptDeathListener()); + being->removeListener(getScriptListener()); } /** diff --git a/src/scripting/luascript.hpp b/src/scripting/luascript.hpp index 9f1097f7..d4dc116e 100644 --- a/src/scripting/luascript.hpp +++ b/src/scripting/luascript.hpp @@ -65,6 +65,8 @@ class LuaScript: public Script void processDeathEvent(Being* thing); + void processRemoveEvent(Thing* thing); + private: lua_State *mState; diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp index a20efb5c..f334eb19 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -33,7 +33,7 @@ static Engines *engines = NULL; Script::Script(): mMap(NULL), - mEventListener(&scriptDeathEventDispatch) + mEventListener(&scriptEventDispatch) {} void Script::registerEngine(std::string const &name, Factory f) diff --git a/src/scripting/script.hpp b/src/scripting/script.hpp index c87bf182..24a415ef 100644 --- a/src/scripting/script.hpp +++ b/src/scripting/script.hpp @@ -124,11 +124,13 @@ class Script MapComposite *getMap() const { return mMap; } - EventListener *getScriptDeathListener() + EventListener *getScriptListener() { return &mEventListener; } virtual void processDeathEvent(Being* thing) = 0; + virtual void processRemoveEvent(Thing* thing) = 0; + protected: std::string mScriptFile; @@ -136,18 +138,19 @@ class Script MapComposite *mMap; EventListener mEventListener; /**< Tracking of being deaths. */ - friend struct ScriptDeathEventDispatch; + friend struct ScriptEventDispatch; }; -struct ScriptDeathEventDispatch: EventDispatch +struct ScriptEventDispatch: EventDispatch { - ScriptDeathEventDispatch() + ScriptEventDispatch() { typedef EventListenerFactory< Script, &Script::mEventListener > Factory; died = &Factory::create< Being, &Script::processDeathEvent >::function; + removed = &Factory::create< Thing, &Script::processRemoveEvent >::function; } }; -static ScriptDeathEventDispatch scriptDeathEventDispatch; +static ScriptEventDispatch scriptEventDispatch; #endif -- cgit v1.2.3-70-g09d2