diff options
Diffstat (limited to 'src/scripting/lua.cpp')
-rw-r--r-- | src/scripting/lua.cpp | 69 |
1 files changed, 67 insertions, 2 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index b7e61065..56f60f81 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -1380,7 +1380,7 @@ static int monster_remove(lua_State *s) } /** - * chr_get_quest(Character*, string): nil or string + * chr_get_quest(Character*, string): string * Callback for getting a quest variable. Starts a recovery and returns * immediatly, if the variable is not known yet. */ @@ -1399,7 +1399,8 @@ static int chr_get_quest(lua_State *s) lua_pushstring(s, value.c_str()); return 1; } - QuestCallback f = { &LuaScript::getQuestCallback, getScript(s) }; + QuestCallback *f = new QuestThreadCallback(&LuaScript::getQuestCallback, + getScript(s)); recoverQuestVar(q, name, f); thread->mState = Script::ThreadExpectingString; @@ -1407,6 +1408,68 @@ static int chr_get_quest(lua_State *s) } /** + * chr_request_quest(Character*, string, Ref function) + * Requests the questvar from the account server. This will make it available in + * the quest cache after some time. The passwed function will be called back as + * soon the quest var is available. + */ +static int chr_request_quest(lua_State *s) +{ + Character *ch = checkCharacter(s, 1); + const char *name = luaL_checkstring(s, 2); + luaL_argcheck(s, name[0] != 0, 2, "empty variable name"); + luaL_checktype(s, 3, LUA_TFUNCTION); + + std::string value; + bool res = getQuestVar(ch, name, value); + if (res) + { + // Already cached, call passed callback immediately + Script *script = getScript(s); + Script::Ref callback; + script->assignCallback(callback); + + // Backup the map since execute will reset it + MapComposite *map = script->getMap(); + + script->prepare(callback); + script->push(ch); + script->push(name); + script->push(value); + script->execute(); + + // Restore map + script->setMap(map); + return 0; + } + + QuestCallback *f = new QuestRefCallback(getScript(s), name); + recoverQuestVar(ch, name, f); + + return 0; +} + +/** + * chr_try_get_quest(Character*, string): nil or string + * Callback for checking if a quest variable is available in cache. It will + * return the variable if it is or nil if it is not in cache. + */ +static int chr_try_get_quest(lua_State *s) +{ + Character *q = checkCharacter(s, 1); + const char *name = luaL_checkstring(s, 2); + luaL_argcheck(s, name[0] != 0, 2, "empty variable name"); + + std::string value; + bool res = getQuestVar(q, name, value); + if (res) + lua_pushstring(s, value.c_str()); + else + lua_pushnil(s); + return 1; +} + +/** * getvar_map(string): string * Gets the value of a persistent map variable. */ @@ -2443,6 +2506,8 @@ LuaScript::LuaScript(): { "chr_get_level", &chr_get_level }, { "chr_get_quest", &chr_get_quest }, { "chr_set_quest", &chr_set_quest }, + { "chr_request_quest", &chr_request_quest }, + { "chr_try_get_quest", &chr_try_get_quest }, { "getvar_map", &getvar_map }, { "setvar_map", &setvar_map }, { "getvar_world", &getvar_world }, |