diff options
author | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-08-28 17:03:44 +0000 |
---|---|---|
committer | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-08-28 17:03:44 +0000 |
commit | e678c7cc1574d5739f83da2b0d493b44e3a7a42e (patch) | |
tree | a184372b02622d82bc85a9827a835de186b0e2ed /src/scripting/lua.cpp | |
parent | c4a2cd54d72f776d6e37eae7a8c67caa81269f4f (diff) | |
download | manaserv-e678c7cc1574d5739f83da2b0d493b44e3a7a42e.tar.gz manaserv-e678c7cc1574d5739f83da2b0d493b44e3a7a42e.tar.bz2 manaserv-e678c7cc1574d5739f83da2b0d493b44e3a7a42e.tar.xz manaserv-e678c7cc1574d5739f83da2b0d493b44e3a7a42e.zip |
Implemented quest variables.
Diffstat (limited to 'src/scripting/lua.cpp')
-rw-r--r-- | src/scripting/lua.cpp | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 50d36349..3eb3ecff 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -37,6 +37,7 @@ extern "C" { #include "game-server/itemmanager.hpp" #include "game-server/mapmanager.hpp" #include "game-server/npc.hpp" +#include "game-server/quest.hpp" #include "game-server/state.hpp" #include "net/messageout.hpp" #include "scripting/script.hpp" @@ -68,6 +69,9 @@ class LuaScript: public Script int execute(); + static void getQuestCallback(Character *, std::string const &, + std::string const &, void *); + private: lua_State *mState; @@ -320,7 +324,7 @@ static int LuaChr_InvCount(lua_State *s) } /** - * Callback for trading between a a player and an NPC. + * Callback for trading between a player and an NPC. * tmw.npc_trade(npc, character, bool sell, table items) */ static int LuaNpc_Trade(lua_State *s) @@ -362,6 +366,69 @@ static int LuaNpc_Trade(lua_State *s) return 0; } +/** + * Called when the server has recovered the value of a quest variable. + */ +void LuaScript::getQuestCallback(Character *q, std::string const &name, + std::string const &value, void *data) +{ + LuaScript *s = static_cast< LuaScript * >(data); + assert(s->nbArgs == -1); + lua_getglobal(s->mState, "quest_reply"); + lua_pushlightuserdata(s->mState, q); + lua_pushstring(s->mState, name.c_str()); + lua_pushstring(s->mState, value.c_str()); + s->nbArgs = 3; + s->execute(); +} + +/** + * Callback for getting a quest variable. Starts a recovery and returns + * immediatly, if the variable is not known yet. + * tmw.chr_get_chest(character, string): nil or string + */ +static int LuaChr_GetQuest(lua_State *s) +{ + Character *q = getCharacter(s, 1); + char const *m = lua_tostring(s, 2); + if (!m || m[0] == 0) + { + LOG_WARN("LuaChr_GetQuest called with incorrect parameters."); + return 0; + } + std::string value, name = m; + bool res = getQuestVar(q, name, value); + if (res) + { + lua_pushstring(s, value.c_str()); + return 1; + } + lua_pushlightuserdata(s, (void *)®istryKey); + lua_gettable(s, LUA_REGISTRYINDEX); + Script *t = static_cast<Script *>(lua_touserdata(s, -1)); + QuestCallback f = { &LuaScript::getQuestCallback, t }; + recoverQuestVar(q, name, f); + return 0; +} + +/** + * Callback for setting a quest variable. + * tmw.chr_set_chest(character, string, string) + */ +static int LuaChr_SetQuest(lua_State *s) +{ + Character *q = getCharacter(s, 1); + char const *m = lua_tostring(s, 2); + char const *n = lua_tostring(s, 3); + if (!m || !n || m[0] == 0) + { + LOG_WARN("LuaChr_SetQuest called with incorrect parameters."); + return 0; + } + setQuestVar(q, m, n); + return 0; +} + LuaScript::LuaScript(): nbArgs(-1) { @@ -377,6 +444,8 @@ LuaScript::LuaScript(): { "chr_warp", &LuaChr_Warp }, { "chr_inv_change", &LuaChr_InvChange }, { "chr_inv_count", &LuaChr_InvCount }, + { "chr_get_quest", &LuaChr_GetQuest }, + { "chr_set_quest", &LuaChr_SetQuest }, { NULL, NULL } }; luaL_register(mState, "tmw", callbacks); |