From 7d559cbae9c713e15ae0c943a4d571122bf29bc6 Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Tue, 6 May 2008 18:49:04 +0000 Subject: Implemented script trigger areas which call a script function when an object steps into them. --- ChangeLog | 8 ++++++++ data/test.lua | 16 +++++++++++++++ src/game-server/trigger.cpp | 13 ++++++++++++ src/game-server/trigger.hpp | 15 ++++++++++++++ src/scripting/lua.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+) diff --git a/ChangeLog b/ChangeLog index e0a959f2..5474c763 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2008-05-06 Philipp Sehmisch + + * src/game-server/trigger.cpp, src/game-server/trigger.h, + src/scripting/lua.cpp: Implemented script trigger areas which call a + script function when an object steps into them. + * data/test.lua: Used script trigger areas to send all objects which + step into one of the areas onto a patrol path. + 2008-05-03 Philipp Sehmisch * data/test.lua, data/scripts/libtmw.lua, data/scripts/npclib.lua: Added diff --git a/data/test.lua b/data/test.lua index 4b14261e..0edc15e3 100644 --- a/data/test.lua +++ b/data/test.lua @@ -22,8 +22,24 @@ atinit(function() create_npc(201, 51 * 32 + 16, 25 * 32 + 16, npc4_talk, npclib.walkaround_wide) create_npc(126, 45 * 32 + 16, 25 * 32 + 16, npc5_talk, npclib.walkaround_map) create_npc(122, 58 * 32 + 16, 15 * 32 + 16, npc6_talk, nil) + + create_npc(200, 63 * 32 + 16, 31 * 32 + 16, nil, nil) + tmw.trigger_create(56 * 32, 30 * 32, 64, 64, "patrol_waypoint", 1) + tmw.trigger_create(63 * 32, 30 * 32, 64, 64, "patrol_waypoint", 2) + end) + +function patrol_waypoint(obj, id) + if (id == 1) then + tmw.being_walk(obj, 64 * 32, 31 * 32, 400) + end + if (id == 2) then + tmw.being_walk(obj, 57 * 32, 31 * 32, 400) + end +end + + function npc1_talk(npc, ch) do_message(npc, ch, "Hello! I am the testing NPC.") do_message(npc, ch, "This message is just here for testing intertwined connections.") diff --git a/src/game-server/trigger.cpp b/src/game-server/trigger.cpp index c0c9bd85..3309c754 100644 --- a/src/game-server/trigger.cpp +++ b/src/game-server/trigger.cpp @@ -28,6 +28,8 @@ #include "game-server/movingobject.hpp" #include "game-server/state.hpp" +#include "utils/logger.h" + void WarpAction::process(Object *obj) { if (obj->getType() == OBJECT_CHARACTER) @@ -36,6 +38,17 @@ void WarpAction::process(Object *obj) } } +void ScriptAction::process(Object *obj) +{ + LOG_DEBUG("Script trigger area activated: "<prepare(mFunction); + mScript->push(obj); + mScript->push(mArg); + mScript->execute(); +} + void TriggerArea::update() { for (MovingObjectIterator i(getMap()->getInsideRectangleIterator(mZone)); i; ++i) diff --git a/src/game-server/trigger.hpp b/src/game-server/trigger.hpp index d6e4d2c9..3ca52fba 100644 --- a/src/game-server/trigger.hpp +++ b/src/game-server/trigger.hpp @@ -26,6 +26,7 @@ #include "point.h" #include "game-server/thing.hpp" +#include "scripting/script.hpp" class Object; @@ -49,6 +50,20 @@ class WarpAction : public TriggerAction unsigned short mX, mY; }; +class ScriptAction : public TriggerAction +{ + public: + ScriptAction(Script *script, std::string function, int arg) + : mScript(script), mFunction(function), mArg(arg) {} + + virtual void process(Object *obj); + + private: + Script *mScript; //Script object to be called + std::string mFunction; //Name of the function called in the script object + int mArg; //Argument passed to script function (meaning is function-specific) +}; + class TriggerArea : public Thing { public: diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 46463dfc..62fd64a4 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -41,6 +41,7 @@ extern "C" { #include "game-server/npc.hpp" #include "game-server/quest.hpp" #include "game-server/state.hpp" +#include "game-server/trigger.hpp" #include "net/messageout.hpp" #include "scripting/script.hpp" #include "utils/logger.h" @@ -547,6 +548,54 @@ static int LuaChr_SetQuest(lua_State *s) return 0; } +/** + * Creates a trigger area. Whenever an object enters this area + * a Lua function is called. + * tmw.trigger_create (x, y, width, height, function, id) + */ +static int LuaTrigger_Create(lua_State *s) +{ + //TODO: argument check + if (!lua_isnumber(s, 1) || + !lua_isnumber(s, 2) || + !lua_isnumber(s, 3) || + !lua_isnumber(s, 4) || + !lua_isstring(s, 5) || + !lua_isnumber(s, 6)) + { + raiseScriptError(s, "trigger_create called with incorrect parameters."); + return 0; + } + + lua_pushlightuserdata(s, (void *)®istryKey); + lua_gettable(s, LUA_REGISTRYINDEX); + Script *script = static_cast