diff options
author | Philipp Sehmisch <tmw@crushnet.org> | 2008-05-06 18:49:04 +0000 |
---|---|---|
committer | Philipp Sehmisch <tmw@crushnet.org> | 2008-05-06 18:49:04 +0000 |
commit | 7d559cbae9c713e15ae0c943a4d571122bf29bc6 (patch) | |
tree | d3edd6921006c28c62db25621f9ad1b44d1f0e69 /src | |
parent | 832584f65adff69659181d6fc884f779a79d62a9 (diff) | |
download | manaserv-7d559cbae9c713e15ae0c943a4d571122bf29bc6.tar.gz manaserv-7d559cbae9c713e15ae0c943a4d571122bf29bc6.tar.bz2 manaserv-7d559cbae9c713e15ae0c943a4d571122bf29bc6.tar.xz manaserv-7d559cbae9c713e15ae0c943a4d571122bf29bc6.zip |
Implemented script trigger areas which call a script function when an object steps into them.
Diffstat (limited to 'src')
-rw-r--r-- | src/game-server/trigger.cpp | 13 | ||||
-rw-r--r-- | src/game-server/trigger.hpp | 15 | ||||
-rw-r--r-- | src/scripting/lua.cpp | 50 |
3 files changed, 78 insertions, 0 deletions
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: "<<mFunction<<"("<<obj<<", "<<mArg<<")"); + if (!mScript) return; + if (mFunction == "") return; + mScript->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<Script *>(lua_touserdata(s, -1)); + int x = lua_tointeger(s, 1); + int y = lua_tointeger(s, 2); + int width = lua_tointeger(s, 3); + int height = lua_tointeger(s, 4); + std::string function = lua_tostring(s, 5); + int id = lua_tointeger(s, 6); + + LOG_INFO("Created script trigger at "<<x<<":"<<y<<" ("<<width<<"x"<<height<<") function: "<<function<<" ("<<id<<")"); + + MapComposite *m = script->getMap(); + + if (!m) + { + raiseScriptError(s, "trigger_create called for nonexistent a map."); + return 0; + } + + ScriptAction *action = new ScriptAction(script, function, id); + Rectangle r = { x, y, width, height }; + TriggerArea *area = new TriggerArea(m, r, action); + + bool ret = GameState::insert(area); + lua_pushboolean(s, ret); + return 1; +} + LuaScript::LuaScript(): nbArgs(-1) { @@ -568,6 +617,7 @@ LuaScript::LuaScript(): { "being_walk", &LuaBeing_Walk }, { "posX", &LuaPosX }, { "posY", &LuaPosY }, + { "trigger_create", &LuaTrigger_Create }, { NULL, NULL } }; luaL_register(mState, "tmw", callbacks); |