diff --git a/docs/python-scripts/modify_lua_script_interfaces.py b/docs/python-scripts/modify_lua_script_interfaces.py new file mode 100644 index 00000000000..881a8453d90 --- /dev/null +++ b/docs/python-scripts/modify_lua_script_interfaces.py @@ -0,0 +1,211 @@ +import os +import re +import argparse + +# Functions that modify the file + +def remove_constructor_and_destructor(content): + # Remove the class constructor and destructor, including a blank line after the destructor + pattern = r"explicit\s+\w+\(lua_State\* L\)\s*:\s*LuaScriptInterface\(\"[^\"]+\"\)\s*\{[^\}]*\}\n\s*~\w+\(\)\s*override\s*=\s*default;\n\s*\n?" + content = re.sub(pattern, "", content, flags=re.DOTALL) + return content + +def remove_include_luascript(content): + # Remove the specified include and the blank line below it + pattern = r'#include\s+"lua/scripts/luascript.hpp"\n\n?' + content = re.sub(pattern, "", content) + return content + +def remove_final_luascriptinterface(content): + # Remove "final : LuaScriptInterface", "public LuaScriptInterface", "final :", and "LuaScriptInterface" alone + pattern = r'final\s*:\s*public\s+LuaScriptInterface|public\s+LuaScriptInterface|final\s*:\s*|LuaScriptInterface\s*' + content = re.sub(pattern, "", content) + # Remove extra spaces between the class name and the opening block + content = re.sub(r'class\s+(\w+)\s*\{', r'class \1 {', content) + return content + +def move_init_function_to_cpp(hpp_content, cpp_content, class_name): + # Extracts the init function from the hpp, keeping the signature + pattern = r'static void init\(lua_State\* L\)\s*\{[^\}]*\}' + match = re.search(pattern, hpp_content, flags=re.DOTALL) + if match: + # Keep the function signature in the hpp, but remove the body + init_function_signature = "static void init(lua_State* L);" + hpp_content = re.sub(pattern, init_function_signature, hpp_content, flags=re.DOTALL) + + # Adjust the function signature for the cpp + init_function = match.group() + init_function = re.sub(r'static void\s+', f'void {class_name}::', init_function) + # Remove extra indentation + init_function = init_function.replace(' \n\t\t', '\n\t') + # Remove the extra tab from the function closure + init_function = init_function.replace('\n\t}', '\n}') + + # Add a blank line before and after the function + init_function = f"\n{init_function}\n" + + # Add the function to the beginning of the cpp, after the includes + last_include = re.findall(r'#include\s+<[^>]+>|#include\s+"[^"]+"', cpp_content) + if last_include: + last_include_pos = cpp_content.rfind(last_include[-1]) + len(last_include[-1]) + cpp_content = cpp_content[:last_include_pos] + "\n" + init_function + cpp_content[last_include_pos:] + else: + cpp_content = init_function + cpp_content + return hpp_content, cpp_content + +def add_include_to_cpp(cpp_content): + # Add the new include after the last include, if it is not already present + include_statement = '#include "lua/functions/lua_functions_loader.hpp"' + if include_statement not in cpp_content: + # Locate the last include + last_include = re.findall(r'#include\s+<[^>]+>|#include\s+"[^"]+"', cpp_content) + if last_include: + last_include_pos = cpp_content.rfind(last_include[-1]) + len(last_include[-1]) + # Make sure there are not multiple line breaks before the include + cpp_content = cpp_content[:last_include_pos].rstrip() + "\n" + include_statement + "\n\n" + cpp_content[last_include_pos:].lstrip() + return cpp_content + +def process_files(hpp_file_path, cpp_file_path): + with open(hpp_file_path, 'r', encoding='utf-8') as hpp_file: + hpp_content = hpp_file.read() + + with open(cpp_file_path, 'r', encoding='utf-8') as cpp_file: + cpp_content = cpp_file.read() + + # Get the class name from the hpp file + class_name_match = re.search(r'class\s+(\w+)', hpp_content) + class_name = class_name_match.group(1) if class_name_match else None + + if class_name: + # Apply all modifications + hpp_content = remove_constructor_and_destructor(hpp_content) + hpp_content = remove_include_luascript(hpp_content) + hpp_content = remove_final_luascriptinterface(hpp_content) + hpp_content, cpp_content = move_init_function_to_cpp(hpp_content, cpp_content, class_name) + cpp_content = add_include_to_cpp(cpp_content) + + # Save the modified files + with open(hpp_file_path, 'w', encoding='utf-8') as hpp_file: + hpp_file.write(hpp_content) + + with open(cpp_file_path, 'w', encoding='utf-8') as cpp_file: + cpp_file.write(cpp_content) + + print(f'Modifications applied to: {hpp_file_path} and {cpp_file_path}') + +def main(directory): + # Scan the specified folder and find all .hpp and .cpp files + for root, _, files in os.walk(directory): + for file in files: + if file.endswith('.hpp'): + hpp_file_path = os.path.join(root, file) + cpp_file_path = hpp_file_path.replace('.hpp', '.cpp') + if os.path.exists(cpp_file_path): + process_files(hpp_file_path, cpp_file_path) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Apply modifications to .hpp and .cpp files according to specifications.') + parser.add_argument('directory', type=str, nargs='?', default='../../src/lua/functions/', help='Path of the directory to be scanned.') + args = parser.parse_args() + + main(args.directory) + +# Functions you want to migrate to static calls +functions_to_convert = [ + "getNumber", + "getBoolean", + "getString", + "getUserdata", + "getScriptEnv", + "getCreature", + "getPlayer", + "getPosition", + "getOutfit", + "getThing", + "getUserdataShared", + "getUserdataType", + "getRawUserDataShared", + "getErrorDesc", + "getFormatedLoggerMessage", + "getField", + "getFieldString", + "getVariant", + "getGuild", + "isTable", + "isString", + "isNumber", + "isBoolean", + "isNil", + "isFunction", + "isUserdata", + "reportError", + "reportErrorFunc", + "pushInstantSpell", + "pushVariant", + "pushOutfit", + "pushCylinder", + "pushBoolean", + "pushString", + "pushUserdata", + "pushPosition", + "setMetatable", + "setWeakMetatable", + "setCreatureMetatable", + "setItemMetatable", + "setField", + "registerVariable", + "registerGlobalMethod", + "registerGlobalVariable", + "registerGlobalBoolean", + "registerMethod", + "registerClass", + "registerTable", + "registerSharedClass", + "registerMetaMethod", +] + +# Files you want to exclude from scanning (relative paths) +files_to_exclude = [ + os.path.normpath("lua_functions_loader.cpp"), + os.path.normpath("lua_functions_loader.hpp") +] + +def convert_to_static(file_path): + with open(file_path, 'r', encoding='utf-8') as file: + content = file.read() + + original_content = content # Keep the original content to check for changes + + for function in functions_to_convert: + # Regex to capture function calls and replace them with Lua::function + # Skip calls that are part of g_configManager() and handle both regular and template functions + pattern = r'(?)?\(' + replacement = rf'Lua::{function}\1(' + content = re.sub(pattern, replacement, content) + + if content != original_content: + with open(file_path, 'w', encoding='utf-8') as file: + file.write(content) + print(f'File converted: {file_path}') + else: + print(f'No changes made to file: {file_path}') + +def main(directory): + # Scan the specified folder and find all .cpp and .hpp files + for root, _, files in os.walk(directory): + for file in files: + if file.endswith(('.cpp', '.hpp')): + file_path = os.path.normpath(os.path.join(root, file)) + + # Check if the file is in the exclusion list + if any(os.path.basename(file_path) == exclude_file for exclude_file in files_to_exclude): + print(f'File ignored: {file_path}') + continue # Skip the specified files + + convert_to_static(file_path) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Convert functions to static calls.') + parser.add_argument('directory', type=str, nargs='?', default='../../src/lua/functions/', help='Path of the directory to be scanned.') + + main(args.directory) diff --git a/src/canary_server.cpp b/src/canary_server.cpp index 687fa51b6cd..8dfbcfc954c 100644 --- a/src/canary_server.cpp +++ b/src/canary_server.cpp @@ -9,6 +9,7 @@ #include "canary_server.hpp" +#include "core.hpp" #include "config/configmanager.hpp" #include "creatures/npcs/npcs.hpp" #include "creatures/players/grouping/familiars.hpp" @@ -31,8 +32,7 @@ #include "server/network/protocol/protocollogin.hpp" #include "server/network/protocol/protocolstatus.hpp" #include "server/network/webhook/webhook.hpp" - -#include "core.hpp" +#include "creatures/players/vocations/vocation.hpp" CanaryServer::CanaryServer( Logger &logger, @@ -322,6 +322,7 @@ void CanaryServer::initializeDatabase() { && !DatabaseManager::optimizeTables()) { logger.debug("No tables were optimized"); } + g_logger().info("Database connection established!"); } void CanaryServer::loadModules() { diff --git a/src/creatures/combat/combat.cpp b/src/creatures/combat/combat.cpp index 6dcd0f229b3..11b5bceb563 100644 --- a/src/creatures/combat/combat.cpp +++ b/src/creatures/combat/combat.cpp @@ -15,12 +15,14 @@ #include "creatures/monsters/monster.hpp" #include "creatures/monsters/monsters.hpp" #include "creatures/players/grouping/party.hpp" +#include "creatures/players/player.hpp" #include "creatures/players/imbuements/imbuements.hpp" #include "creatures/players/wheel/player_wheel.hpp" #include "game/game.hpp" #include "game/scheduling/dispatcher.hpp" #include "io/iobestiary.hpp" #include "io/ioprey.hpp" +#include "creatures/players/vocations/vocation.hpp" #include "items/weapons/weapons.hpp" #include "lib/metrics/metrics.hpp" #include "lua/callbacks/event_callback.hpp" diff --git a/src/creatures/combat/spells.cpp b/src/creatures/combat/spells.cpp index 0d66602ddca..47ae56c7dd6 100644 --- a/src/creatures/combat/spells.cpp +++ b/src/creatures/combat/spells.cpp @@ -20,7 +20,8 @@ #include "game/game.hpp" #include "lua/global/lua_variant.hpp" #include "lua/scripts/lua_environment.hpp" -#include "lua/scripts/luascript.hpp" +#include "lua/scripts/scripts.hpp" +#include "lib/di/container.hpp" std::array(WheelSpellBoost_t::TOTAL_COUNT)> wheelOfDestinyRegularBoost = { 0 }; std::array(WheelSpellBoost_t::TOTAL_COUNT)> wheelOfDestinyUpgradedBoost = { 0 }; @@ -156,7 +157,7 @@ bool Spells::registerRuneLuaEvent(const std::shared_ptr &rune) { "[{}] duplicate registered rune with id: {}, for script: {}", __FUNCTION__, id, - rune->getScriptInterface()->getLoadingScriptName() + rune->getRuneSpellScriptInterface()->getLoadingScriptName() ); } return inserted; @@ -277,29 +278,45 @@ Position Spells::getCasterPosition(const std::shared_ptr &creature, Di return getNextPosition(dir, creature->getPosition()); } +LuaScriptInterface* BaseSpell::getScriptInterface() const { + return &g_scripts().getScriptInterface(); +} + +bool BaseSpell::loadScriptId() { + LuaScriptInterface &luaInterface = g_scripts().getScriptInterface(); + m_spellScriptId = luaInterface.getEvent(); + if (m_spellScriptId == -1) { + g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName()); + return false; + } + + return true; +} + +int32_t BaseSpell::getScriptId() const { + return m_spellScriptId; +} + +void BaseSpell::setScriptId(int32_t newScriptId) { + m_spellScriptId = newScriptId; +} + +bool BaseSpell::isLoadedScriptId() const { + return m_spellScriptId != 0; +} + CombatSpell::CombatSpell(const std::shared_ptr &newCombat, bool newNeedTarget, bool newNeedDirection) : - Script(&g_spells().getScriptInterface()), m_combat(newCombat), needDirection(newNeedDirection), needTarget(newNeedTarget) { - // Empty -} - -bool CombatSpell::loadScriptCombat() { - m_combat = g_luaEnvironment().getCombatObject(g_luaEnvironment().lastCombatId); - return m_combat != nullptr; } std::shared_ptr CombatSpell::getCombat() const { return m_combat; } -std::string CombatSpell::getScriptTypeName() const { - return "onCastSpell"; -} - bool CombatSpell::castSpell(const std::shared_ptr &creature) { - if (isLoadedCallback()) { + if (isLoadedScriptId()) { LuaVariant var; var.type = VARIANT_POSITION; @@ -346,7 +363,7 @@ bool CombatSpell::castSpell(const std::shared_ptr &creature, const std return false; } - if (isLoadedCallback()) { + if (isLoadedScriptId()) { LuaVariant var; if (combat->hasArea()) { var.type = VARIANT_POSITION; @@ -412,6 +429,8 @@ bool CombatSpell::executeCastSpell(const std::shared_ptr &creature, co return getScriptInterface()->callFunction(2); } +Spell::Spell() = default; + bool Spell::playerSpellCheck(const std::shared_ptr &player) const { if (player->hasFlag(PlayerFlags_t::CannotUseSpells)) { return false; @@ -1030,6 +1049,8 @@ void Spell::setLockedPZ(bool b) { pzLocked = b; } +InstantSpell::InstantSpell() = default; + bool InstantSpell::playerCastInstant(const std::shared_ptr &player, std::string ¶m) const { if (!playerSpellCheck(player)) { return false; @@ -1161,10 +1182,6 @@ bool InstantSpell::canThrowSpell(const std::shared_ptr &creature, cons return true; } -std::string InstantSpell::getScriptTypeName() const { - return "onCastSpell"; -} - bool InstantSpell::castSpell(const std::shared_ptr &creature) { LuaVariant var; var.instantName = getName(); @@ -1294,6 +1311,33 @@ bool InstantSpell::canCast(const std::shared_ptr &player) const { return false; } +LuaScriptInterface* RuneSpell::getRuneSpellScriptInterface() const { + return &g_scripts().getScriptInterface(); +} + +bool RuneSpell::loadRuneSpellScriptId() { + LuaScriptInterface &luaInterface = g_scripts().getScriptInterface(); + m_runeSpellScriptId = luaInterface.getEvent(); + if (m_runeSpellScriptId == -1) { + g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName()); + return false; + } + + return true; +} + +int32_t RuneSpell::getRuneSpellScriptId() const { + return m_runeSpellScriptId; +} + +void RuneSpell::setRuneSpellScriptId(int32_t newScriptId) { + m_runeSpellScriptId = newScriptId; +} + +bool RuneSpell::isRuneSpellLoadedScriptId() const { + return m_runeSpellScriptId != 0; +} + ReturnValue RuneSpell::canExecuteAction(const std::shared_ptr &player, const Position &toPos) { if (player->hasFlag(PlayerFlags_t::CannotUseSpells)) { return RETURNVALUE_CANNOTUSETHISOBJECT; @@ -1329,7 +1373,7 @@ bool RuneSpell::executeUse(const std::shared_ptr &player, const std::sha } // If script not loaded correctly, return - if (!isLoadedCallback()) { + if (!isRuneSpellLoadedScriptId()) { return false; } @@ -1391,13 +1435,9 @@ bool RuneSpell::castSpell(const std::shared_ptr &creature, const std:: return internalCastSpell(creature, var, false); } -std::string RuneSpell::getScriptTypeName() const { - return "onCastSpell"; -} - bool RuneSpell::internalCastSpell(const std::shared_ptr &creature, const LuaVariant &var, bool isHotkey) const { bool result; - if (isLoadedCallback()) { + if (isRuneSpellLoadedScriptId()) { result = executeCastSpell(creature, var, isHotkey); } else { result = false; @@ -1415,11 +1455,11 @@ bool RuneSpell::executeCastSpell(const std::shared_ptr &creature, cons } ScriptEnvironment* env = LuaEnvironment::getScriptEnv(); - env->setScriptId(getScriptId(), getScriptInterface()); + env->setScriptId(getRuneSpellScriptId(), getRuneSpellScriptInterface()); - lua_State* L = getScriptInterface()->getLuaState(); + lua_State* L = getRuneSpellScriptInterface()->getLuaState(); - getScriptInterface()->pushFunction(getScriptId()); + getRuneSpellScriptInterface()->pushFunction(getRuneSpellScriptId()); LuaScriptInterface::pushUserdata(L, creature); LuaScriptInterface::setCreatureMetatable(L, -1, creature); @@ -1428,7 +1468,7 @@ bool RuneSpell::executeCastSpell(const std::shared_ptr &creature, cons LuaScriptInterface::pushBoolean(L, isHotkey); - return getScriptInterface()->callFunction(3); + return getRuneSpellScriptInterface()->callFunction(3); } bool RuneSpell::isInstant() const { diff --git a/src/creatures/combat/spells.hpp b/src/creatures/combat/spells.hpp index ab34fa6dbce..5d9edb70dd6 100644 --- a/src/creatures/combat/spells.hpp +++ b/src/creatures/combat/spells.hpp @@ -10,7 +10,6 @@ #pragma once #include "lua/creature/actions.hpp" -#include "lua/scripts/scripts.hpp" enum class WheelSpellBoost_t : uint8_t; enum class WheelSpellGrade_t : uint8_t; @@ -18,12 +17,17 @@ enum class WheelSpellGrade_t : uint8_t; class InstantSpell; class RuneSpell; class Spell; +class Combat; +class Player; +class Creature; +class LuaScriptInterface; struct LuaVariant; +struct Position; using VocSpellMap = std::map; -class Spells final : public Scripts { +class Spells { public: Spells(); ~Spells(); @@ -78,11 +82,20 @@ class BaseSpell { virtual bool castSpell(const std::shared_ptr &creature) = 0; virtual bool castSpell(const std::shared_ptr &creature, const std::shared_ptr &target) = 0; + LuaScriptInterface* getScriptInterface() const; + bool loadScriptId(); + int32_t getScriptId() const; + void setScriptId(int32_t newScriptId); + bool isLoadedScriptId() const; + SoundEffect_t soundImpactEffect = SoundEffect_t::SILENCE; SoundEffect_t soundCastEffect = SoundEffect_t::SPELL_OR_RUNE; + +protected: + int32_t m_spellScriptId {}; }; -class CombatSpell final : public Script, public BaseSpell, public std::enable_shared_from_this { +class CombatSpell final : public BaseSpell, public std::enable_shared_from_this { public: // Constructor CombatSpell(const std::shared_ptr &newCombat, bool newNeedTarget, bool newNeedDirection); @@ -97,12 +110,9 @@ class CombatSpell final : public Script, public BaseSpell, public std::enable_sh // Scripting spell bool executeCastSpell(const std::shared_ptr &creature, const LuaVariant &var) const; - bool loadScriptCombat(); std::shared_ptr getCombat() const; private: - std::string getScriptTypeName() const override; - std::shared_ptr m_combat; bool needDirection; @@ -111,7 +121,7 @@ class CombatSpell final : public Script, public BaseSpell, public std::enable_sh class Spell : public BaseSpell { public: - Spell() = default; + Spell(); [[nodiscard]] const std::string &getName() const; void setName(std::string n); @@ -267,10 +277,9 @@ class Spell : public BaseSpell { friend class SpellFunctions; }; -class InstantSpell final : public Script, public Spell { +class InstantSpell final : public Spell { public: - using Script::Script; - + InstantSpell(); bool playerCastInstant(const std::shared_ptr &player, std::string ¶m) const; bool castSpell(const std::shared_ptr &creature) override; @@ -294,8 +303,6 @@ class InstantSpell final : public Script, public Spell { bool canThrowSpell(const std::shared_ptr &creature, const std::shared_ptr &target) const; private: - [[nodiscard]] std::string getScriptTypeName() const override; - bool needDirection = false; bool hasParam = false; bool hasPlayerNameParam = false; @@ -307,6 +314,12 @@ class RuneSpell final : public Action, public Spell { public: using Action::Action; + LuaScriptInterface* getRuneSpellScriptInterface() const; + bool loadRuneSpellScriptId(); + int32_t getRuneSpellScriptId() const; + void setRuneSpellScriptId(int32_t newScriptId); + bool isRuneSpellLoadedScriptId() const; + ReturnValue canExecuteAction(const std::shared_ptr &player, const Position &toPos) override; bool hasOwnErrorHandler() override; std::shared_ptr getTarget(const std::shared_ptr &, const std::shared_ptr &targetCreature, const Position &, uint8_t) const override; @@ -326,10 +339,10 @@ class RuneSpell final : public Action, public Spell { void setCharges(uint32_t c); private: - [[nodiscard]] std::string getScriptTypeName() const override; - bool internalCastSpell(const std::shared_ptr &creature, const LuaVariant &var, bool isHotkey) const; + int32_t m_runeSpellScriptId = 0; + uint16_t runeId = 0; uint32_t charges = 0; bool hasCharges = false; diff --git a/src/creatures/monsters/monsters.cpp b/src/creatures/monsters/monsters.cpp index 87fd967ab46..cd33e12e248 100644 --- a/src/creatures/monsters/monsters.cpp +++ b/src/creatures/monsters/monsters.cpp @@ -16,6 +16,7 @@ #include "game/game.hpp" #include "items/weapons/weapons.hpp" #include "lua/scripts/luascript.hpp" +#include "lib/di/container.hpp" void MonsterType::loadLoot(const std::shared_ptr &monsterType, LootBlock lootBlock) const { if (lootBlock.childLoot.empty()) { diff --git a/src/creatures/npcs/npcs.cpp b/src/creatures/npcs/npcs.cpp index cff44588d71..53ed757336f 100644 --- a/src/creatures/npcs/npcs.cpp +++ b/src/creatures/npcs/npcs.cpp @@ -14,6 +14,7 @@ #include "lua/scripts/lua_environment.hpp" #include "lua/scripts/luascript.hpp" #include "lua/scripts/scripts.hpp" +#include "lib/di/container.hpp" bool NpcType::canSpawn(const Position &pos) const { bool canSpawn = true; diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index 0b54f3f0bac..c6f58e2f6c4 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -62,6 +62,7 @@ #include "lua/creature/events.hpp" #include "lua/creature/movement.hpp" #include "map/spectators.hpp" +#include "creatures/players/vocations/vocation.hpp" MuteCountMap Player::muteCountMap; @@ -5022,8 +5023,8 @@ ItemsTierCountList Player::getDepotChestItemsId() const { ItemsTierCountList Player::getDepotInboxItemsId() const { ItemsTierCountList itemMap; - const auto &inbox = getInbox(); - const auto &container = inbox->getContainer(); + const auto &inboxPtr = getInbox(); + const auto &container = inboxPtr->getContainer(); if (container) { for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) { const auto &item = *it; diff --git a/src/game/functions/game_reload.cpp b/src/game/functions/game_reload.cpp index 10f20595042..5de3e98b0b0 100644 --- a/src/game/functions/game_reload.cpp +++ b/src/game/functions/game_reload.cpp @@ -22,6 +22,7 @@ #include "lua/modules/modules.hpp" #include "lua/scripts/lua_environment.hpp" #include "lua/scripts/scripts.hpp" +#include "creatures/players/vocations/vocation.hpp" GameReload::GameReload() = default; GameReload::~GameReload() = default; diff --git a/src/game/game.cpp b/src/game/game.cpp index af8c56c1d75..188bfb83d4f 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -62,6 +62,7 @@ #include "server/server.hpp" #include "utils/tools.hpp" #include "utils/wildcardtree.hpp" +#include "creatures/players/vocations/vocation.hpp" #include "enums/account_coins.hpp" #include "enums/account_errors.hpp" @@ -8243,8 +8244,8 @@ void Game::checkPlayersRecord() { uint32_t previousRecord = playersRecord; playersRecord = playersOnline; - for (auto &[key, it] : g_globalEvents().getEventMap(GLOBALEVENT_RECORD)) { - it->executeRecord(playersRecord, previousRecord); + for (const auto &[key, globalEvent] : g_globalEvents().getEventMap(GLOBALEVENT_RECORD)) { + globalEvent->executeRecord(playersRecord, previousRecord); } updatePlayersRecord(); } diff --git a/src/game/game_definitions.hpp b/src/game/game_definitions.hpp index e122cdf979f..50fa1307764 100644 --- a/src/game/game_definitions.hpp +++ b/src/game/game_definitions.hpp @@ -54,7 +54,7 @@ enum Faction_t { FACTION_LAST = FACTION_FAFNAR, }; -enum LightState_t { +enum LightState_t : uint8_t { LIGHT_STATE_DAY, LIGHT_STATE_NIGHT, LIGHT_STATE_SUNSET, diff --git a/src/game/scheduling/task.cpp b/src/game/scheduling/task.cpp index b3a79f7ab18..1edd246a702 100644 --- a/src/game/scheduling/task.cpp +++ b/src/game/scheduling/task.cpp @@ -15,22 +15,22 @@ std::atomic_uint_fast64_t Task::LAST_EVENT_ID = 0; -Task::Task(uint32_t expiresAfterMs, std::function &&f, std::string_view context, const std::source_location &location) : - func(std::move(f)), context(context), functionName(location.function_name()), utime(OTSYS_TIME()), +Task::Task(uint32_t expiresAfterMs, std::function &&f, std::string_view context) : + func(std::move(f)), context(context), utime(OTSYS_TIME()), expiration(expiresAfterMs > 0 ? OTSYS_TIME() + expiresAfterMs : 0) { if (this->context.empty()) { - g_logger().error("[{}]: task context cannot be empty! Function: {}", __FUNCTION__, functionName); + g_logger().error("[{}]: task context cannot be empty!", __FUNCTION__); return; } assert(!this->context.empty() && "Context cannot be empty!"); } -Task::Task(std::function &&f, std::string_view context, uint32_t delay, bool cycle /* = false*/, bool log /*= true*/, const std::source_location &location) : - func(std::move(f)), context(context), functionName(location.function_name()), utime(OTSYS_TIME() + delay), delay(delay), +Task::Task(std::function &&f, std::string_view context, uint32_t delay, bool cycle /* = false*/, bool log /*= true*/) : + func(std::move(f)), context(context), utime(OTSYS_TIME() + delay), delay(delay), cycle(cycle), log(log) { if (this->context.empty()) { - g_logger().error("[{}]: task context cannot be empty! Function: {}", __FUNCTION__, functionName); + g_logger().error("[{}]: task context cannot be empty!", __FUNCTION__); return; } @@ -48,15 +48,15 @@ bool Task::execute() const { } if (hasExpired()) { - g_logger().info("The task '{}' has expired, it has not been executed in {}. Function: {}", getContext(), expiration - utime, functionName); + g_logger().info("The task '{}' has expired, it has not been executed in {}.", getContext(), expiration - utime); return false; } if (log) { if (hasTraceableContext()) { - g_logger().trace("Executing task {}. Function: {}", getContext(), functionName); + g_logger().trace("Executing task {}.", getContext()); } else { - g_logger().debug("Executing task {}. Function: {}", getContext(), functionName); + g_logger().debug("Executing task {}.", getContext()); } } diff --git a/src/game/scheduling/task.hpp b/src/game/scheduling/task.hpp index 948bdea0215..c01dbe2f676 100644 --- a/src/game/scheduling/task.hpp +++ b/src/game/scheduling/task.hpp @@ -13,9 +13,9 @@ class Dispatcher; class Task { public: - Task(uint32_t expiresAfterMs, std::function &&f, std::string_view context, const std::source_location &location = std::source_location::current()); + Task(uint32_t expiresAfterMs, std::function &&f, std::string_view context); - Task(std::function &&f, std::string_view context, uint32_t delay, bool cycle = false, bool log = true, const std::source_location &location = std::source_location::current()); + Task(std::function &&f, std::string_view context, uint32_t delay, bool cycle = false, bool log = true); ~Task() = default; @@ -37,10 +37,6 @@ class Task { return context; } - [[nodiscard]] std::string_view getFunctionName() const { - return functionName; - } - [[nodiscard]] auto getTime() const { return utime; } @@ -106,7 +102,6 @@ class Task { std::function func; std::string context; - std::string functionName; int64_t utime = 0; int64_t expiration = 0; diff --git a/src/items/functions/item/item_parse.cpp b/src/items/functions/item/item_parse.cpp index 761740bfc2d..49f23609807 100644 --- a/src/items/functions/item/item_parse.cpp +++ b/src/items/functions/item/item_parse.cpp @@ -15,6 +15,7 @@ #include "utils/pugicast.hpp" #include "utils/tools.hpp" #include "creatures/combat/combat.hpp" +#include "lua/scripts/scripts.hpp" void ItemParse::initParse(const std::string &stringValue, pugi::xml_node attributeNode, pugi::xml_attribute valueAttribute, ItemType &itemType) { // Parse all item attributes @@ -974,7 +975,7 @@ void ItemParse::parseHouseRelated(std::string_view stringValue, pugi::xml_attrib void ItemParse::createAndRegisterScript(ItemType &itemType, pugi::xml_node attributeNode, MoveEvent_t eventType /*= MOVE_EVENT_NONE*/, WeaponType_t weaponType /*= WEAPON_NONE*/) { std::shared_ptr moveevent; if (eventType != MOVE_EVENT_NONE) { - moveevent = std::make_shared(&g_moveEvents().getScriptInterface()); + moveevent = std::make_shared(); moveevent->setItemId(itemType.id); moveevent->setEventType(eventType); @@ -996,11 +997,11 @@ void ItemParse::createAndRegisterScript(ItemType &itemType, pugi::xml_node attri std::shared_ptr weapon = nullptr; if (weaponType != WEAPON_NONE) { if (weaponType == WEAPON_DISTANCE || weaponType == WEAPON_AMMO || weaponType == WEAPON_MISSILE) { - weapon = std::make_shared(&g_weapons().getScriptInterface()); + weapon = std::make_shared(); } else if (weaponType == WEAPON_WAND) { - weapon = std::make_shared(&g_weapons().getScriptInterface()); + weapon = std::make_shared(); } else { - weapon = std::make_shared(&g_weapons().getScriptInterface()); + weapon = std::make_shared(); } weapon->weaponType = weaponType; diff --git a/src/items/weapons/weapons.cpp b/src/items/weapons/weapons.cpp index f27732f3b59..4eb84d246c6 100644 --- a/src/items/weapons/weapons.cpp +++ b/src/items/weapons/weapons.cpp @@ -13,12 +13,19 @@ #include "creatures/combat/combat.hpp" #include "game/game.hpp" #include "lua/creature/events.hpp" +#include "creatures/players/vocations/vocation.hpp" +#include "lib/di/container.hpp" +#include "lua/scripts/scripts.hpp" #include "lua/global/lua_variant.hpp" #include "creatures/players/player.hpp" Weapons::Weapons() = default; Weapons::~Weapons() = default; +Weapons &Weapons::getInstance() { + return inject(); +} + WeaponShared_ptr Weapons::getWeapon(const std::shared_ptr &item) const { if (!item) { return nullptr; @@ -77,6 +84,35 @@ int32_t Weapons::getMaxWeaponDamage(uint32_t level, int32_t attackSkill, int32_t } } +Weapon::Weapon() = default; + +LuaScriptInterface* Weapon::getScriptInterface() const { + return &g_scripts().getScriptInterface(); +} + +bool Weapon::loadScriptId() { + LuaScriptInterface &luaInterface = g_scripts().getScriptInterface(); + m_scriptId = luaInterface.getEvent(); + if (m_scriptId == -1) { + g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName()); + return false; + } + + return true; +} + +int32_t Weapon::getScriptId() const { + return m_scriptId; +} + +void Weapon::setScriptId(int32_t newScriptId) { + m_scriptId = newScriptId; +} + +bool Weapon::isLoadedScriptId() const { + return m_scriptId != 0; +} + void Weapon::configureWeapon(const ItemType &it) { id = it.id; } @@ -203,7 +239,7 @@ void Weapon::internalUseWeapon(const std::shared_ptr &player, const std: } } - if (isLoadedCallback()) { + if (isLoadedScriptId()) { if (cleavePercent != 0) { return; } @@ -259,7 +295,7 @@ void Weapon::internalUseWeapon(const std::shared_ptr &player, const std: } void Weapon::internalUseWeapon(const std::shared_ptr &player, const std::shared_ptr &item, const std::shared_ptr &tile) const { - if (isLoadedCallback()) { + if (isLoadedScriptId()) { LuaVariant var; var.type = VARIANT_TARGETPOSITION; var.pos = tile->getPosition(); @@ -424,8 +460,31 @@ bool Weapon::calculateSkillFormula(const std::shared_ptr &player, int32_ return shouldCalculateSecondaryDamage; } -WeaponMelee::WeaponMelee(LuaScriptInterface* interface) : - Weapon(interface) { +void Weapon::addVocWeaponMap(const std::string &vocName) { + const int32_t vocationId = g_vocations().getVocationId(vocName); + if (vocationId != -1) { + vocWeaponMap[vocationId] = true; + } +} + +std::shared_ptr Weapon::getCombat() const { + if (!m_combat) { + g_logger().error("Weapon::getCombat() - m_combat is nullptr"); + return nullptr; + } + + return m_combat; +} + +std::shared_ptr Weapon::getCombat() { + if (!m_combat) { + m_combat = std::make_shared(); + } + + return m_combat; +} + +WeaponMelee::WeaponMelee() { // Add combat type and blocked attributes to the weapon params.blockedByArmor = true; params.blockedByShield = true; @@ -573,8 +632,7 @@ int32_t WeaponMelee::getWeaponDamage(const std::shared_ptr &player, cons return -normal_random(minValue, (maxValue * static_cast(player->getVocation()->meleeDamageMultiplier))); } -WeaponDistance::WeaponDistance(LuaScriptInterface* interface) : - Weapon(interface) { +WeaponDistance::WeaponDistance() { // Add combat type and distance effect to the weapon params.blockedByArmor = true; params.combatType = COMBAT_PHYSICALDAMAGE; @@ -874,6 +932,8 @@ bool WeaponDistance::getSkillType(const std::shared_ptr &player, const s return true; } +WeaponWand::WeaponWand() = default; + void WeaponWand::configureWeapon(const ItemType &it) { params.distanceEffect = it.shootType; const_cast(it).combatType = params.combatType; diff --git a/src/items/weapons/weapons.hpp b/src/items/weapons/weapons.hpp index c31813d09e0..dca7d814033 100644 --- a/src/items/weapons/weapons.hpp +++ b/src/items/weapons/weapons.hpp @@ -9,23 +9,29 @@ #pragma once -#include "lua/scripts/luascript.hpp" -#include "lua/scripts/scripts.hpp" -#include "creatures/combat/combat.hpp" #include "utils/utils_definitions.hpp" -#include "creatures/players/vocations/vocation.hpp" +#include "creatures/creatures_definitions.hpp" +#include "creatures/combat/combat.hpp" class Weapon; class WeaponMelee; class WeaponDistance; class WeaponWand; +class LuaScriptInterface; +class Combat; +class Player; +class Creature; +class Item; +class ItemType; +class Vocation; +class Tile; struct LuaVariant; using WeaponUnique_ptr = std::unique_ptr; using WeaponShared_ptr = std::shared_ptr; -class Weapons final : public Scripts { +class Weapons { public: Weapons(); ~Weapons(); @@ -34,9 +40,7 @@ class Weapons final : public Scripts { Weapons(const Weapons &) = delete; Weapons &operator=(const Weapons &) = delete; - static Weapons &getInstance() { - return inject(); - } + static Weapons &getInstance(); WeaponShared_ptr getWeapon(const std::shared_ptr &item) const; @@ -52,10 +56,9 @@ class Weapons final : public Scripts { constexpr auto g_weapons = Weapons::getInstance; -class Weapon : public Script { +class Weapon { public: - using Script::Script; - + Weapon(); virtual void configureWeapon(const ItemType &it); virtual bool interruptSwing() const { return false; @@ -161,12 +164,7 @@ class Weapon : public Script { wieldInfo |= info; } - void addVocWeaponMap(const std::string &vocName) { - const int32_t vocationId = g_vocations().getVocationId(vocName); - if (vocationId != -1) { - vocWeaponMap[vocationId] = true; - } - } + void addVocWeaponMap(const std::string &vocName); const std::string &getVocationString() const { return vocationString; @@ -203,30 +201,25 @@ class Weapon : public Script { return weaponType; } - std::shared_ptr getCombat() const { - if (!m_combat) { - g_logger().error("Weapon::getCombat() - m_combat is nullptr"); - return nullptr; - } + std::shared_ptr getCombat() const; - return m_combat; - } - - std::shared_ptr getCombat() { - if (!m_combat) { - m_combat = std::make_shared(); - } - - return m_combat; - } + std::shared_ptr getCombat(); bool calculateSkillFormula(const std::shared_ptr &player, int32_t &attackSkill, int32_t &attackValue, float &attackFactor, int16_t &elementAttack, CombatDamage &damage, bool useCharges = false) const; + LuaScriptInterface* getScriptInterface() const; + bool loadScriptId(); + int32_t getScriptId() const; + void setScriptId(int32_t newScriptId); + bool isLoadedScriptId() const; + protected: void internalUseWeapon(const std::shared_ptr &player, const std::shared_ptr &item, const std::shared_ptr &target, int32_t damageModifier, int32_t cleavePercent = 0) const; void internalUseWeapon(const std::shared_ptr &player, const std::shared_ptr &item, const std::shared_ptr &tile) const; private: + int32_t m_scriptId {}; + virtual bool getSkillType(const std::shared_ptr &, const std::shared_ptr &, skills_t &, uint32_t &) const { return false; } @@ -275,11 +268,7 @@ class Weapon : public Script { class WeaponMelee final : public Weapon { public: - explicit WeaponMelee(LuaScriptInterface* interface); - - std::string getScriptTypeName() const override { - return "onUseWeapon"; - } + explicit WeaponMelee(); void configureWeapon(const ItemType &it) override; @@ -300,11 +289,7 @@ class WeaponMelee final : public Weapon { class WeaponDistance final : public Weapon { public: - explicit WeaponDistance(LuaScriptInterface* interface); - - std::string getScriptTypeName() const override { - return "onUseWeapon"; - } + explicit WeaponDistance(); void configureWeapon(const ItemType &it) override; bool interruptSwing() const override { @@ -329,11 +314,7 @@ class WeaponDistance final : public Weapon { class WeaponWand : public Weapon { public: - using Weapon::Weapon; - - std::string getScriptTypeName() const override { - return "onUseWeapon"; - } + explicit WeaponWand(); void configureWeapon(const ItemType &it) override; diff --git a/src/lua/callbacks/event_callback.cpp b/src/lua/callbacks/event_callback.cpp index 1675cdef440..16e0aa182d0 100644 --- a/src/lua/callbacks/event_callback.cpp +++ b/src/lua/callbacks/event_callback.cpp @@ -14,6 +14,7 @@ #include "game/zones/zone.hpp" #include "items/containers/container.hpp" #include "items/item.hpp" +#include "lua/scripts/scripts.hpp" /** * @class EventCallback @@ -24,26 +25,52 @@ * * @see Script */ -EventCallback::EventCallback(LuaScriptInterface* scriptInterface, const std::string &callbackName, bool skipDuplicationCheck) : - Script(scriptInterface), m_callbackName(callbackName), m_skipDuplicationCheck(skipDuplicationCheck) { -} +EventCallback::EventCallback(const std::string &callbackName, bool skipDuplicationCheck) : + m_callbackName(callbackName), m_skipDuplicationCheck(skipDuplicationCheck) { } -std::string EventCallback::getName() const { - return m_callbackName; +LuaScriptInterface* EventCallback::getScriptInterface() const { + return &g_scripts().getScriptInterface(); } -bool EventCallback::skipDuplicationCheck() const { - return m_skipDuplicationCheck; +bool EventCallback::loadScriptId() { + LuaScriptInterface &luaInterface = g_scripts().getScriptInterface(); + m_scriptId = luaInterface.getEvent(); + if (m_scriptId == -1) { + g_logger().error("[EventCallback::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName()); + return false; + } + + return true; } std::string EventCallback::getScriptTypeName() const { return m_scriptTypeName; } -void EventCallback::setScriptTypeName(const std::string_view newName) { +void EventCallback::setScriptTypeName(std::string_view newName) { m_scriptTypeName = newName; } +int32_t EventCallback::getScriptId() const { + return m_scriptId; +} + +void EventCallback::setScriptId(int32_t newScriptId) { + m_scriptId = newScriptId; +} + +bool EventCallback::isLoadedScriptId() const { + return m_scriptId != 0; +} + +std::string EventCallback::getName() const { + return m_callbackName; +} + +bool EventCallback::skipDuplicationCheck() const { + return m_skipDuplicationCheck; +} + EventCallback_t EventCallback::getType() const { return m_callbackType; } diff --git a/src/lua/callbacks/event_callback.hpp b/src/lua/callbacks/event_callback.hpp index 514c7feafff..c7df7929774 100644 --- a/src/lua/callbacks/event_callback.hpp +++ b/src/lua/callbacks/event_callback.hpp @@ -13,7 +13,6 @@ #include "creatures/creatures_definitions.hpp" #include "items/items_definitions.hpp" #include "utils/utils_definitions.hpp" -#include "lua/scripts/scripts.hpp" class Creature; class Player; @@ -22,6 +21,25 @@ class Party; class ItemType; class Monster; class Zone; +class LuaScriptInterface; +class Thing; +class Item; +class Cylinder; +class Npc; +class Container; + +struct Position; +struct CombatDamage; +struct Outfit_t; + +enum Direction : uint8_t; +enum ReturnValue : uint16_t; +enum SpeakClasses : uint8_t; +enum Slots_t : uint8_t; +enum ZoneType_t : uint8_t; +enum skills_t : int8_t; +enum CombatType_t : uint8_t; +enum TextColor_t : uint8_t; /** * @class EventCallback @@ -31,19 +49,23 @@ class Zone; * registration, and execution of custom behavior tied to specific game events. * @note It inherits from the Script class, providing scripting capabilities. */ -class EventCallback final : public Script { +class EventCallback { private: EventCallback_t m_callbackType = EventCallback_t::none; ///< The type of the event callback. std::string m_scriptTypeName; ///< The name associated with the script type. std::string m_callbackName; ///< The name of the callback. bool m_skipDuplicationCheck = false; ///< Whether the callback is silent error for already registered log error. + int32_t m_scriptId {}; + public: - /** - * @brief Constructor that initializes the EventCallback with a given script interface. - * @param scriptInterface Pointer to the LuaScriptInterface object. - */ - explicit EventCallback(LuaScriptInterface* scriptInterface, const std::string &callbackName, bool silentAlreadyRegistered); + explicit EventCallback(const std::string &callbackName, bool silentAlreadyRegistered); + + LuaScriptInterface* getScriptInterface() const; + bool loadScriptId(); + int32_t getScriptId() const; + void setScriptId(int32_t newScriptId); + bool isLoadedScriptId() const; /** * @brief Retrieves the callback name. @@ -61,7 +83,7 @@ class EventCallback final : public Script { * @brief Retrieves the script type name. * @return The script type name as a string. */ - std::string getScriptTypeName() const override; + std::string getScriptTypeName() const; /** * @brief Sets a new script type name. diff --git a/src/lua/callbacks/events_callbacks.hpp b/src/lua/callbacks/events_callbacks.hpp index 8913ee1dc4b..df321bbd02d 100644 --- a/src/lua/callbacks/events_callbacks.hpp +++ b/src/lua/callbacks/events_callbacks.hpp @@ -11,7 +11,6 @@ #include "lua/callbacks/callbacks_definitions.hpp" #include "lua/callbacks/event_callback.hpp" -#include "lua/scripts/luascript.hpp" class EventCallback; @@ -81,7 +80,7 @@ class EventsCallbacks { } for (const auto &entry : it->second) { - if (entry.callback && entry.callback->isLoadedCallback()) { + if (entry.callback && entry.callback->isLoadedScriptId()) { std::invoke(callbackFunc, *entry.callback, args...); } } @@ -102,7 +101,7 @@ class EventsCallbacks { } for (const auto &entry : it->second) { - if (entry.callback && entry.callback->isLoadedCallback()) { + if (entry.callback && entry.callback->isLoadedScriptId()) { ReturnValue callbackResult = std::invoke(callbackFunc, *entry.callback, args...); if (callbackResult != RETURNVALUE_NOERROR) { return callbackResult; @@ -128,7 +127,7 @@ class EventsCallbacks { } for (const auto &entry : it->second) { - if (entry.callback && entry.callback->isLoadedCallback()) { + if (entry.callback && entry.callback->isLoadedScriptId()) { bool callbackResult = std::invoke(callbackFunc, *entry.callback, args...); allCallbacksSucceeded &= callbackResult; } diff --git a/src/lua/creature/actions.cpp b/src/lua/creature/actions.cpp index b79cb5d03c7..2ee02fac9ab 100644 --- a/src/lua/creature/actions.cpp +++ b/src/lua/creature/actions.cpp @@ -19,10 +19,16 @@ #include "items/containers/depot/depotlocker.hpp" #include "items/containers/rewards/reward.hpp" #include "items/containers/rewards/rewardchest.hpp" +#include "lua/scripts/scripts.hpp" +#include "lib/di/container.hpp" Actions::Actions() = default; Actions::~Actions() = default; +Actions &Actions::getInstance() { + return inject(); +} + void Actions::clear() { useItemMap.clear(); uniqueItemMap.clear(); @@ -273,7 +279,7 @@ ReturnValue Actions::internalUseItem(const std::shared_ptr &player, cons } if (action != nullptr) { - if (action->isLoadedCallback()) { + if (action->isLoadedScriptId()) { if (action->executeUse(player, item, pos, nullptr, pos, isHotkey)) { return RETURNVALUE_NOERROR; } @@ -495,8 +501,34 @@ void Actions::showUseHotkeyMessage(const std::shared_ptr &player, const */ // Action constructor -Action::Action(LuaScriptInterface* interface) : - Script(interface) { } +Action::Action() = default; + +LuaScriptInterface* Action::getScriptInterface() const { + return &g_scripts().getScriptInterface(); +} + +bool Action::loadScriptId() { + LuaScriptInterface &luaInterface = g_scripts().getScriptInterface(); + m_scriptId = luaInterface.getEvent(); + if (m_scriptId == -1) { + g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName()); + return false; + } + + return true; +} + +int32_t Action::getScriptId() const { + return m_scriptId; +} + +void Action::setScriptId(int32_t newScriptId) { + m_scriptId = newScriptId; +} + +bool Action::isLoadedScriptId() const { + return m_scriptId != 0; +} ReturnValue Action::canExecuteAction(const std::shared_ptr &player, const Position &toPos) { if (!allowFarUse) { diff --git a/src/lua/creature/actions.hpp b/src/lua/creature/actions.hpp index ed5c6adc399..f0d73811354 100644 --- a/src/lua/creature/actions.hpp +++ b/src/lua/creature/actions.hpp @@ -9,16 +9,20 @@ #pragma once -#include "lua/scripts/scripts.hpp" #include "declarations.hpp" -#include "lua/scripts/luascript.hpp" class Action; +class LuaScriptInterface; +class Player; +class Item; +class Creature; +class Thing; + struct Position; -class Action : public Script { +class Action { public: - explicit Action(LuaScriptInterface* interface); + explicit Action(); // Scripting virtual bool executeUse(const std::shared_ptr &player, const std::shared_ptr &item, const Position &fromPosition, const std::shared_ptr &target, const Position &toPosition, bool isHotkey); @@ -104,10 +108,14 @@ class Action : public Script { virtual std::shared_ptr getTarget(const std::shared_ptr &player, const std::shared_ptr &targetCreature, const Position &toPosition, uint8_t toStackPos) const; + LuaScriptInterface* getScriptInterface() const; + bool loadScriptId(); + int32_t getScriptId() const; + void setScriptId(int32_t newScriptId); + bool isLoadedScriptId() const; + private: - std::string getScriptTypeName() const override { - return "onUse"; - } + int32_t m_scriptId {}; std::function &player, const std::shared_ptr &item, @@ -130,7 +138,7 @@ class Action : public Script { friend class Actions; }; -class Actions final : public Scripts { +class Actions { public: Actions(); ~Actions(); @@ -139,9 +147,7 @@ class Actions final : public Scripts { Actions(const Actions &) = delete; Actions &operator=(const Actions &) = delete; - static Actions &getInstance() { - return inject(); - } + static Actions &getInstance(); bool useItem(const std::shared_ptr &player, const Position &pos, uint8_t index, const std::shared_ptr &item, bool isHotkey); bool useItemEx(const std::shared_ptr &player, const Position &fromPos, const Position &toPos, uint8_t toStackPos, const std::shared_ptr &item, bool isHotkey, const std::shared_ptr &creature = nullptr); diff --git a/src/lua/creature/creatureevent.cpp b/src/lua/creature/creatureevent.cpp index 575394b605c..c13c8180556 100644 --- a/src/lua/creature/creatureevent.cpp +++ b/src/lua/creature/creatureevent.cpp @@ -11,6 +11,8 @@ #include "creatures/players/player.hpp" #include "items/item.hpp" +#include "lua/scripts/scripts.hpp" +#include "lib/di/container.hpp" void CreatureEvents::clear() { for (const auto &[name, event] : creatureEvents) { @@ -104,8 +106,34 @@ bool CreatureEvents::playerAdvance( ======================= */ -CreatureEvent::CreatureEvent(LuaScriptInterface* interface) : - Script(interface) { } +CreatureEvent::CreatureEvent() = default; + +LuaScriptInterface* CreatureEvent::getScriptInterface() const { + return &g_scripts().getScriptInterface(); +} + +bool CreatureEvent::loadScriptId() { + LuaScriptInterface &luaInterface = g_scripts().getScriptInterface(); + m_scriptId = luaInterface.getEvent(); + if (m_scriptId == -1) { + g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName()); + return false; + } + + return true; +} + +int32_t CreatureEvent::getScriptId() const { + return m_scriptId; +} + +void CreatureEvent::setScriptId(int32_t newScriptId) { + m_scriptId = newScriptId; +} + +bool CreatureEvent::isLoadedScriptId() const { + return m_scriptId != 0; +} void CreatureEvents::removeInvalidEvents() { std::erase_if(creatureEvents, [](const auto &pair) { @@ -160,15 +188,11 @@ std::string CreatureEvent::getScriptTypeName() const { void CreatureEvent::copyEvent(const std::shared_ptr &creatureEvent) { setScriptId(creatureEvent->getScriptId()); - setScriptInterface(creatureEvent->getScriptInterface()); - setLoadedCallback(creatureEvent->isLoadedCallback()); loaded = creatureEvent->loaded; } void CreatureEvent::clearEvent() { setScriptId(0); - setScriptInterface(nullptr); - setLoadedCallback(false); loaded = false; } diff --git a/src/lua/creature/creatureevent.hpp b/src/lua/creature/creatureevent.hpp index 5000a4b2e0a..1d5c612a68c 100644 --- a/src/lua/creature/creatureevent.hpp +++ b/src/lua/creature/creatureevent.hpp @@ -9,14 +9,21 @@ #pragma once -#include "lua/scripts/scripts.hpp" +#include "lua/lua_definitions.hpp" class CreatureEvent; class LuaScriptInterface; +class Creature; +class Player; +class Item; -class CreatureEvent final : public Script { +struct CombatDamage; + +enum skills_t : int8_t; + +class CreatureEvent { public: - explicit CreatureEvent(LuaScriptInterface* interface); + explicit CreatureEvent(); CreatureEventType_t getEventType() const { return type; @@ -53,17 +60,23 @@ class CreatureEvent final : public Script { void executeHealthChange(const std::shared_ptr &creature, const std::shared_ptr &attacker, CombatDamage &damage) const; void executeManaChange(const std::shared_ptr &creature, const std::shared_ptr &attacker, CombatDamage &damage) const; void executeExtendedOpcode(const std::shared_ptr &player, uint8_t opcode, const std::string &buffer) const; - // + + std::string getScriptTypeName() const; + LuaScriptInterface* getScriptInterface() const; + bool loadScriptId(); + int32_t getScriptId() const; + void setScriptId(int32_t newScriptId); + bool isLoadedScriptId() const; private: - std::string getScriptTypeName() const override; + int32_t m_scriptId {}; std::string eventName; CreatureEventType_t type = CREATURE_EVENT_NONE; bool loaded = false; }; -class CreatureEvents final : public Scripts { +class CreatureEvents { public: CreatureEvents() = default; diff --git a/src/lua/creature/movement.cpp b/src/lua/creature/movement.cpp index 684f0844ee1..6804cdfa7e9 100644 --- a/src/lua/creature/movement.cpp +++ b/src/lua/creature/movement.cpp @@ -9,6 +9,7 @@ #include "lua/creature/movement.hpp" +#include "lib/di/container.hpp" #include "creatures/combat/combat.hpp" #include "creatures/combat/condition.hpp" #include "creatures/players/player.hpp" @@ -16,6 +17,14 @@ #include "lua/callbacks/event_callback.hpp" #include "lua/callbacks/events_callbacks.hpp" #include "lua/creature/events.hpp" +#include "lua/scripts/scripts.hpp" +#include "creatures/players/vocations/vocation.hpp" +#include "items/item.hpp" +#include "lua/functions/events/move_event_functions.hpp" + +MoveEvents &MoveEvents::getInstance() { + return inject(); +} void MoveEvents::clear() { uniqueIdMap.clear(); @@ -384,8 +393,8 @@ uint32_t MoveEvents::onItemMove(const std::shared_ptr &item, const std::sh MoveEvent class ================ */ -MoveEvent::MoveEvent(LuaScriptInterface* interface) : - Script(interface) { } + +MoveEvent::MoveEvent() = default; std::string MoveEvent::getScriptTypeName() const { switch (eventType) { @@ -411,6 +420,33 @@ std::string MoveEvent::getScriptTypeName() const { } } +LuaScriptInterface* MoveEvent::getScriptInterface() const { + return &g_scripts().getScriptInterface(); +} + +bool MoveEvent::loadScriptId() { + LuaScriptInterface &luaInterface = g_scripts().getScriptInterface(); + m_scriptId = luaInterface.getEvent(); + if (m_scriptId == -1) { + g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName()); + return false; + } + + return true; +} + +int32_t MoveEvent::getScriptId() const { + return m_scriptId; +} + +void MoveEvent::setScriptId(int32_t newScriptId) { + m_scriptId = newScriptId; +} + +bool MoveEvent::isLoadedScriptId() const { + return m_scriptId != 0; +} + uint32_t MoveEvent::StepInField(const std::shared_ptr &creature, const std::shared_ptr &item, const Position &) { if (creature == nullptr) { g_logger().error("[MoveEvent::StepInField] - Creature is nullptr"); @@ -676,7 +712,7 @@ void MoveEvent::setEventType(MoveEvent_t type) { } uint32_t MoveEvent::fireStepEvent(const std::shared_ptr &creature, const std::shared_ptr &item, const Position &pos) const { - if (isLoadedCallback()) { + if (isLoadedScriptId()) { return executeStep(creature, item, pos); } else { return stepFunction(creature, item, pos); @@ -714,23 +750,24 @@ bool MoveEvent::executeStep(const std::shared_ptr &creature, const std return false; } + const auto scriptInterface = getScriptInterface(); ScriptEnvironment* env = LuaScriptInterface::getScriptEnv(); - env->setScriptId(getScriptId(), getScriptInterface()); + env->setScriptId(getScriptId(), scriptInterface); - lua_State* L = getScriptInterface()->getLuaState(); + lua_State* L = scriptInterface->getLuaState(); - getScriptInterface()->pushFunction(getScriptId()); + scriptInterface->pushFunction(getScriptId()); LuaScriptInterface::pushUserdata(L, creature); LuaScriptInterface::setCreatureMetatable(L, -1, creature); LuaScriptInterface::pushThing(L, item); LuaScriptInterface::pushPosition(L, pos); LuaScriptInterface::pushPosition(L, fromPosition); - return getScriptInterface()->callFunction(4); + return scriptInterface->callFunction(4); } uint32_t MoveEvent::fireEquip(const std::shared_ptr &player, const std::shared_ptr &item, Slots_t toSlot, bool isCheck) { - if (isLoadedCallback()) { + if (isLoadedScriptId()) { if (!equipFunction || equipFunction(static_self_cast(), player, item, toSlot, isCheck) == 1) { if (executeEquip(player, item, toSlot, isCheck)) { return 1; @@ -768,7 +805,7 @@ bool MoveEvent::executeEquip(const std::shared_ptr &player, const std::s } uint32_t MoveEvent::fireAddRemItem(const std::shared_ptr &item, const std::shared_ptr &fromTile, const Position &pos) const { - if (isLoadedCallback()) { + if (isLoadedScriptId()) { return executeAddRemItem(item, fromTile, pos); } else { return moveFunction(item, fromTile, pos); @@ -800,7 +837,7 @@ bool MoveEvent::executeAddRemItem(const std::shared_ptr &item, const std:: } uint32_t MoveEvent::fireAddRemItem(const std::shared_ptr &item, const Position &pos) const { - if (isLoadedCallback()) { + if (isLoadedScriptId()) { return executeAddRemItem(item, pos); } else { return moveFunction(item, nullptr, pos); @@ -829,3 +866,8 @@ bool MoveEvent::executeAddRemItem(const std::shared_ptr &item, const Posit return getScriptInterface()->callFunction(2); } + +void MoveEvent::addVocEquipMap(const std::string &vocName) { + const uint16_t vocationId = g_vocations().getVocationId(vocName); + vocEquipMap[vocationId] = true; +} diff --git a/src/lua/creature/movement.hpp b/src/lua/creature/movement.hpp index 58f42296f40..bb4554b94d2 100644 --- a/src/lua/creature/movement.hpp +++ b/src/lua/creature/movement.hpp @@ -10,12 +10,13 @@ #pragma once #include "declarations.hpp" -#include "items/item.hpp" -#include "lua/functions/events/move_event_functions.hpp" -#include "lua/scripts/scripts.hpp" -#include "creatures/players/vocations/vocation.hpp" class MoveEvent; +class LuaScriptInterface; +class Item; +class Tile; +class Creature; +class Player; struct MoveEventList { std::list> moveEvent[MOVE_EVENT_LAST]; @@ -23,7 +24,7 @@ struct MoveEventList { using VocEquipMap = std::map; -class MoveEvents final : public Scripts { +class MoveEvents { public: MoveEvents() = default; ~MoveEvents() = default; @@ -32,9 +33,7 @@ class MoveEvents final : public Scripts { MoveEvents(const MoveEvents &) = delete; MoveEvents &operator=(const MoveEvents &) = delete; - static MoveEvents &getInstance() { - return inject(); - } + static MoveEvents &getInstance(); uint32_t onCreatureMove(const std::shared_ptr &creature, const std::shared_ptr &tile, MoveEvent_t eventType); uint32_t onPlayerEquip(const std::shared_ptr &player, const std::shared_ptr &item, Slots_t slot, bool isCheck); @@ -129,9 +128,9 @@ class MoveEvents final : public Scripts { constexpr auto g_moveEvents = MoveEvents::getInstance; -class MoveEvent final : public Script, public SharedObject { +class MoveEvent final : public SharedObject { public: - explicit MoveEvent(LuaScriptInterface* interface); + explicit MoveEvent(); MoveEvent_t getEventType() const; void setEventType(MoveEvent_t type); @@ -175,12 +174,7 @@ class MoveEvent final : public Script, public SharedObject { const std::map &getVocEquipMap() const { return vocEquipMap; } - void addVocEquipMap(const std::string &vocName) { - const uint16_t vocationId = g_vocations().getVocationId(vocName); - if (vocationId != 65535) { - vocEquipMap[vocationId] = true; - } - } + void addVocEquipMap(const std::string &vocName); bool getTileItem() const { return tileItem; } @@ -245,8 +239,15 @@ class MoveEvent final : public Script, public SharedObject { static uint32_t EquipItem(const std::shared_ptr &moveEvent, const std::shared_ptr &player, const std::shared_ptr &item, Slots_t slot, bool boolean); static uint32_t DeEquipItem(const std::shared_ptr &, const std::shared_ptr &player, const std::shared_ptr &item, Slots_t slot, bool boolean); + std::string getScriptTypeName() const; + bool loadScriptId(); + int32_t getScriptId() const; + void setScriptId(int32_t newScriptId); + bool isLoadedScriptId() const; + LuaScriptInterface* getScriptInterface() const; + private: - std::string getScriptTypeName() const override; + int32_t m_scriptId {}; uint32_t slot = SLOTP_WHEREEVER; diff --git a/src/lua/creature/talkaction.cpp b/src/lua/creature/talkaction.cpp index 77323d9acf2..abe9303a5ba 100644 --- a/src/lua/creature/talkaction.cpp +++ b/src/lua/creature/talkaction.cpp @@ -13,10 +13,15 @@ #include "creatures/players/grouping/groups.hpp" #include "creatures/players/player.hpp" #include "lua/scripts/scripts.hpp" +#include "lib/di/container.hpp" TalkActions::TalkActions() = default; TalkActions::~TalkActions() = default; +TalkActions &TalkActions::getInstance() { + return inject(); +} + void TalkActions::clear() { talkActions.clear(); } @@ -80,6 +85,33 @@ TalkActionResult_t TalkActions::checkPlayerCanSayTalkAction(const std::shared_pt return TALKACTION_CONTINUE; } +LuaScriptInterface* TalkAction::getScriptInterface() const { + return &g_scripts().getScriptInterface(); +} + +bool TalkAction::loadScriptId() { + LuaScriptInterface &luaInterface = g_scripts().getScriptInterface(); + m_scriptId = luaInterface.getEvent(); + if (m_scriptId == -1) { + g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName()); + return false; + } + + return true; +} + +int32_t TalkAction::getScriptId() const { + return m_scriptId; +} + +void TalkAction::setScriptId(int32_t newScriptId) { + m_scriptId = newScriptId; +} + +bool TalkAction::isLoadedScriptId() const { + return m_scriptId != 0; +} + bool TalkAction::executeSay(const std::shared_ptr &player, const std::string &words, const std::string ¶m, SpeakClasses type) const { // onSay(player, words, param, type) if (!LuaScriptInterface::reserveScriptEnv()) { @@ -101,7 +133,7 @@ bool TalkAction::executeSay(const std::shared_ptr &player, const std::st LuaScriptInterface::pushString(L, words); LuaScriptInterface::pushString(L, param); - lua_pushnumber(L, type); + LuaScriptInterface::pushNumber(L, static_cast(type)); return getScriptInterface()->callFunction(4); } diff --git a/src/lua/creature/talkaction.hpp b/src/lua/creature/talkaction.hpp index d11a5487924..4040a10f873 100644 --- a/src/lua/creature/talkaction.hpp +++ b/src/lua/creature/talkaction.hpp @@ -10,19 +10,16 @@ #pragma once #include "account/account.hpp" -#include "lua/global/baseevents.hpp" #include "utils/utils_definitions.hpp" #include "declarations.hpp" -#include "lua/scripts/luascript.hpp" -#include "lua/scripts/scripts.hpp" +class Player; +class LuaScriptInterface; class TalkAction; using TalkAction_ptr = std::shared_ptr; -class TalkAction final : public Script { +class TalkAction final { public: - using Script::Script; - const std::string &getWords() const { return m_word; } @@ -58,10 +55,14 @@ class TalkAction final : public Script { void setGroupType(uint8_t newGroupType); const uint8_t &getGroupType() const; + LuaScriptInterface* getScriptInterface() const; + bool loadScriptId(); + int32_t getScriptId() const; + void setScriptId(int32_t newScriptId); + bool isLoadedScriptId() const; + private: - std::string getScriptTypeName() const override { - return "onSay"; - } + int32_t m_scriptId {}; std::string m_word; std::string m_description; @@ -69,7 +70,7 @@ class TalkAction final : public Script { uint8_t m_groupType = 0; }; -class TalkActions final : public Scripts { +class TalkActions { public: TalkActions(); ~TalkActions(); @@ -78,9 +79,7 @@ class TalkActions final : public Scripts { TalkActions(const TalkActions &) = delete; TalkActions &operator=(const TalkActions &) = delete; - static TalkActions &getInstance() { - return inject(); - } + static TalkActions &getInstance(); bool checkWord(const std::shared_ptr &player, SpeakClasses type, const std::string &words, std::string_view word, const TalkAction_ptr &talkActionPtr) const; TalkActionResult_t checkPlayerCanSayTalkAction(const std::shared_ptr &player, SpeakClasses type, const std::string &words) const; diff --git a/src/lua/functions/core/CMakeLists.txt b/src/lua/functions/core/CMakeLists.txt index 1cf919da3ec..e9ac9899a6c 100644 --- a/src/lua/functions/core/CMakeLists.txt +++ b/src/lua/functions/core/CMakeLists.txt @@ -6,7 +6,6 @@ target_sources(${PROJECT_NAME}_lib PRIVATE game/lua_enums.cpp game/modal_window_functions.cpp game/zone_functions.cpp - libs/bit_functions.cpp libs/db_functions.cpp libs/result_functions.cpp libs/logger_functions.cpp diff --git a/src/lua/functions/core/game/bank_functions.cpp b/src/lua/functions/core/game/bank_functions.cpp index ce608939304..88665337a42 100644 --- a/src/lua/functions/core/game/bank_functions.cpp +++ b/src/lua/functions/core/game/bank_functions.cpp @@ -12,16 +12,29 @@ #include "creatures/players/player.hpp" #include "game/bank/bank.hpp" #include "game/game.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void BankFunctions::init(lua_State* L) { + Lua::registerTable(L, "Bank"); + Lua::registerMethod(L, "Bank", "credit", BankFunctions::luaBankCredit); + Lua::registerMethod(L, "Bank", "debit", BankFunctions::luaBankDebit); + Lua::registerMethod(L, "Bank", "balance", BankFunctions::luaBankBalance); + Lua::registerMethod(L, "Bank", "hasBalance", BankFunctions::luaBankHasBalance); + Lua::registerMethod(L, "Bank", "transfer", BankFunctions::luaBankTransfer); + Lua::registerMethod(L, "Bank", "transferToGuild", BankFunctions::luaBankTransferToGuild); + Lua::registerMethod(L, "Bank", "withdraw", BankFunctions::luaBankWithdraw); + Lua::registerMethod(L, "Bank", "deposit", BankFunctions::luaBankDeposit); +} int BankFunctions::luaBankCredit(lua_State* L) { // Bank.credit(playerOrGuild, amount) const auto &bank = getBank(L, 1); if (bank == nullptr) { - reportErrorFunc("Bank is nullptr"); + Lua::reportErrorFunc("Bank is nullptr"); return 1; } - const uint64_t amount = getNumber(L, 2); - pushBoolean(L, bank->credit(amount)); + const uint64_t amount = Lua::getNumber(L, 2); + Lua::pushBoolean(L, bank->credit(amount)); return 1; } @@ -29,11 +42,11 @@ int BankFunctions::luaBankDebit(lua_State* L) { // Bank.debit(playerOrGuild, amount) const auto &bank = getBank(L, 1); if (bank == nullptr) { - reportErrorFunc("Bank is nullptr"); + Lua::reportErrorFunc("Bank is nullptr"); return 1; } - const uint64_t amount = getNumber(L, 2); - pushBoolean(L, bank->debit(amount)); + const uint64_t amount = Lua::getNumber(L, 2); + Lua::pushBoolean(L, bank->debit(amount)); return 1; } @@ -41,15 +54,15 @@ int BankFunctions::luaBankBalance(lua_State* L) { // Bank.balance(playerOrGuild[, amount]]) const auto &bank = getBank(L, 1); if (bank == nullptr) { - reportErrorFunc("Bank is nullptr"); + Lua::reportErrorFunc("Bank is nullptr"); return 1; } if (lua_gettop(L) == 1) { lua_pushnumber(L, bank->balance()); return 1; } - const uint64_t amount = getNumber(L, 2); - pushBoolean(L, bank->balance(amount)); + const uint64_t amount = Lua::getNumber(L, 2); + Lua::pushBoolean(L, bank->balance(amount)); return 1; } @@ -57,11 +70,11 @@ int BankFunctions::luaBankHasBalance(lua_State* L) { // Bank.hasBalance(playerOrGuild, amount) const auto &bank = getBank(L, 1); if (bank == nullptr) { - reportErrorFunc("Bank is nullptr"); + Lua::reportErrorFunc("Bank is nullptr"); return 1; } - const uint64_t amount = getNumber(L, 2); - pushBoolean(L, bank->hasBalance(amount)); + const uint64_t amount = Lua::getNumber(L, 2); + Lua::pushBoolean(L, bank->hasBalance(amount)); return 1; } @@ -70,17 +83,17 @@ int BankFunctions::luaBankTransfer(lua_State* L) { const auto &source = getBank(L, 1); if (source == nullptr) { g_logger().debug("BankFunctions::luaBankTransfer: source is null"); - reportErrorFunc("Bank is nullptr"); + Lua::reportErrorFunc("Bank is nullptr"); return 1; } const auto &destination = getBank(L, 2); if (destination == nullptr) { g_logger().debug("BankFunctions::luaBankTransfer: destination is null"); - reportErrorFunc("Bank is nullptr"); + Lua::reportErrorFunc("Bank is nullptr"); return 1; } - const uint64_t amount = getNumber(L, 3); - pushBoolean(L, source->transferTo(destination, amount)); + const uint64_t amount = Lua::getNumber(L, 3); + Lua::pushBoolean(L, source->transferTo(destination, amount)); return 1; } @@ -88,83 +101,83 @@ int BankFunctions::luaBankTransferToGuild(lua_State* L) { // Bank.transfer(fromPlayerOrGuild, toGuild, amount) const auto &source = getBank(L, 1); if (source == nullptr) { - reportErrorFunc("Source is nullptr"); + Lua::reportErrorFunc("Source is nullptr"); return 1; } const auto &destination = getBank(L, 2, true /* isGuild */); if (destination == nullptr) { - reportErrorFunc("Destination is nullptr"); + Lua::reportErrorFunc("Destination is nullptr"); return 1; } - const uint64_t amount = getNumber(L, 3); - pushBoolean(L, source->transferTo(destination, amount)); + const uint64_t amount = Lua::getNumber(L, 3); + Lua::pushBoolean(L, source->transferTo(destination, amount)); return 1; } int BankFunctions::luaBankWithdraw(lua_State* L) { // Bank.withdraw(player, amount[, source = player]) - const auto &player = getPlayer(L, 1); + const auto &player = Lua::getPlayer(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const uint64_t amount = getNumber(L, 2); + const uint64_t amount = Lua::getNumber(L, 2); if (lua_gettop(L) == 2) { auto bank = std::make_shared(player); - pushBoolean(L, bank->withdraw(player, amount)); + Lua::pushBoolean(L, bank->withdraw(player, amount)); return 1; } const auto &source = getBank(L, 3); if (source == nullptr) { - reportErrorFunc("Source is nullptr"); + Lua::reportErrorFunc("Source is nullptr"); return 1; } - pushBoolean(L, source->withdraw(player, amount)); + Lua::pushBoolean(L, source->withdraw(player, amount)); return 1; } int BankFunctions::luaBankDeposit(lua_State* L) { // Bank.deposit(player, amount[, destination = player]) - const auto &player = getPlayer(L, 1); + const auto &player = Lua::getPlayer(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } auto bank = std::make_shared(player); uint64_t amount = 0; if (lua_isnumber(L, 2)) { - amount = getNumber(L, 2); + amount = Lua::getNumber(L, 2); } else if (lua_isnil(L, 2)) { amount = player->getMoney(); } if (lua_gettop(L) == 2) { - pushBoolean(L, g_game().removeMoney(player, amount) && bank->credit(amount)); + Lua::pushBoolean(L, g_game().removeMoney(player, amount) && bank->credit(amount)); return 1; } const auto &destination = getBank(L, 3); if (destination == nullptr) { - reportErrorFunc("Destination is nullptr"); + Lua::reportErrorFunc("Destination is nullptr"); return 1; } - pushBoolean(L, g_game().removeMoney(player, amount) && destination->credit(amount)); + Lua::pushBoolean(L, g_game().removeMoney(player, amount) && destination->credit(amount)); return 1; } std::shared_ptr BankFunctions::getBank(lua_State* L, int32_t arg, bool isGuild /*= false*/) { - if (getUserdataType(L, arg) == LuaData_t::Guild) { - return std::make_shared(getGuild(L, arg)); + if (Lua::getUserdataType(L, arg) == LuaData_t::Guild) { + return std::make_shared(Lua::getGuild(L, arg)); } if (isGuild) { - const auto &guild = getGuild(L, arg, true); + const auto &guild = Lua::getGuild(L, arg, true); if (!guild) { return nullptr; } return std::make_shared(guild); } - const auto &player = getPlayer(L, arg, true); + const auto &player = Lua::getPlayer(L, arg, true); if (!player) { return nullptr; } diff --git a/src/lua/functions/core/game/bank_functions.hpp b/src/lua/functions/core/game/bank_functions.hpp index f2a94a40435..a93de48424b 100644 --- a/src/lua/functions/core/game/bank_functions.hpp +++ b/src/lua/functions/core/game/bank_functions.hpp @@ -9,29 +9,11 @@ #pragma once -#include "lua/scripts/luascript.hpp" - class Bank; -class BankFunctions final : LuaScriptInterface { +class BankFunctions { public: - explicit BankFunctions(lua_State* L) : - LuaScriptInterface("BankFunctions") { - init(L); - } - ~BankFunctions() override = default; - - static void init(lua_State* L) { - registerTable(L, "Bank"); - registerMethod(L, "Bank", "credit", BankFunctions::luaBankCredit); - registerMethod(L, "Bank", "debit", BankFunctions::luaBankDebit); - registerMethod(L, "Bank", "balance", BankFunctions::luaBankBalance); - registerMethod(L, "Bank", "hasBalance", BankFunctions::luaBankHasBalance); - registerMethod(L, "Bank", "transfer", BankFunctions::luaBankTransfer); - registerMethod(L, "Bank", "transferToGuild", BankFunctions::luaBankTransferToGuild); - registerMethod(L, "Bank", "withdraw", BankFunctions::luaBankWithdraw); - registerMethod(L, "Bank", "deposit", BankFunctions::luaBankDeposit); - } + static void init(lua_State* L); private: static int luaBankCredit(lua_State* L); diff --git a/src/lua/functions/core/game/config_functions.cpp b/src/lua/functions/core/game/config_functions.cpp index d8ee6e3ea77..045ea7519f1 100644 --- a/src/lua/functions/core/game/config_functions.cpp +++ b/src/lua/functions/core/game/config_functions.cpp @@ -10,20 +10,21 @@ #include "lua/functions/core/game/config_functions.hpp" #include "config/configmanager.hpp" +#include "lua/functions/lua_functions_loader.hpp" void ConfigFunctions::init(lua_State* L) { - registerTable(L, "configManager"); - registerMethod(L, "configManager", "getString", luaConfigManagerGetString); - registerMethod(L, "configManager", "getNumber", luaConfigManagerGetNumber); - registerMethod(L, "configManager", "getBoolean", luaConfigManagerGetBoolean); - registerMethod(L, "configManager", "getFloat", luaConfigManagerGetFloat); + Lua::registerTable(L, "configManager"); + Lua::registerMethod(L, "configManager", "getString", luaConfigManagerGetString); + Lua::registerMethod(L, "configManager", "getNumber", luaConfigManagerGetNumber); + Lua::registerMethod(L, "configManager", "getBoolean", luaConfigManagerGetBoolean); + Lua::registerMethod(L, "configManager", "getFloat", luaConfigManagerGetFloat); #define registerMagicEnumIn(L, tableName, enumValue) \ do { \ auto name = magic_enum::enum_name(enumValue).data(); \ - registerVariable(L, tableName, name, value); \ + Lua::registerVariable(L, tableName, name, value); \ } while (0) - registerTable(L, "configKeys"); + Lua::registerTable(L, "configKeys"); for (auto value : magic_enum::enum_values()) { auto enumName = magic_enum::enum_name(value).data(); if (enumName) { @@ -35,35 +36,35 @@ void ConfigFunctions::init(lua_State* L) { } int ConfigFunctions::luaConfigManagerGetString(lua_State* L) { - const auto key = getNumber(L, -1); + const auto key = Lua::getNumber(L, -1); if (!key) { - reportErrorFunc("Wrong enum"); + Lua::reportErrorFunc("Wrong enum"); return 1; } - pushString(L, g_configManager().getString(getNumber(L, -1))); + Lua::pushString(L, g_configManager().getString(Lua::getNumber(L, -1))); return 1; } int ConfigFunctions::luaConfigManagerGetNumber(lua_State* L) { - const auto key = getNumber(L, -1); + const auto key = Lua::getNumber(L, -1); if (!key) { - reportErrorFunc("Wrong enum"); + Lua::reportErrorFunc("Wrong enum"); return 1; } - lua_pushnumber(L, g_configManager().getNumber(getNumber(L, -1))); + lua_pushnumber(L, g_configManager().getNumber(Lua::getNumber(L, -1))); return 1; } int ConfigFunctions::luaConfigManagerGetBoolean(lua_State* L) { - const auto key = getNumber(L, -1); + const auto key = Lua::getNumber(L, -1); if (!key) { - reportErrorFunc("Wrong enum"); + Lua::reportErrorFunc("Wrong enum"); return 1; } - pushBoolean(L, g_configManager().getBoolean(getNumber(L, -1))); + Lua::pushBoolean(L, g_configManager().getBoolean(Lua::getNumber(L, -1))); return 1; } @@ -71,14 +72,14 @@ int ConfigFunctions::luaConfigManagerGetFloat(lua_State* L) { // configManager.getFloat(key, shouldRound = true) // Ensure the first argument (key) is provided and is a valid enum - const auto key = getNumber(L, 1); + const auto key = Lua::getNumber(L, 1); if (!key) { - reportErrorFunc("Wrong enum"); + Lua::reportErrorFunc("Wrong enum"); return 1; } // Check if the second argument (shouldRound) is provided and is a boolean; default to true if not provided - bool shouldRound = getBoolean(L, 2, true); + bool shouldRound = Lua::getBoolean(L, 2, true); float value = g_configManager().getFloat(key); double finalValue = shouldRound ? static_cast(std::round(value * 100.0) / 100.0) : value; diff --git a/src/lua/functions/core/game/config_functions.hpp b/src/lua/functions/core/game/config_functions.hpp index 973ed5c1d63..c22144ee08e 100644 --- a/src/lua/functions/core/game/config_functions.hpp +++ b/src/lua/functions/core/game/config_functions.hpp @@ -10,16 +10,8 @@ #pragma once #include "declarations.hpp" -#include "lua/scripts/luascript.hpp" - -class ConfigFunctions final : LuaScriptInterface { +class ConfigFunctions { public: - explicit ConfigFunctions(lua_State* L) : - LuaScriptInterface("ConfigFunctions") { - init(L); - } - ~ConfigFunctions() override = default; - static void init(lua_State* L); private: diff --git a/src/lua/functions/core/game/game_functions.cpp b/src/lua/functions/core/game/game_functions.cpp index f1226d2e22c..aad4b71d06c 100644 --- a/src/lua/functions/core/game/game_functions.cpp +++ b/src/lua/functions/core/game/game_functions.cpp @@ -14,6 +14,7 @@ #include "creatures/monsters/monsters.hpp" #include "creatures/npcs/npc.hpp" #include "creatures/players/achievement/player_achievement.hpp" +#include "creatures/players/player.hpp" #include "game/functions/game_reload.hpp" #include "game/game.hpp" #include "game/scheduling/dispatcher.hpp" @@ -28,16 +29,94 @@ #include "lua/functions/events/event_callback_functions.hpp" #include "lua/scripts/lua_environment.hpp" #include "map/spectators.hpp" -#include "creatures/players/player.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void GameFunctions::init(lua_State* L) { + Lua::registerTable(L, "Game"); + + Lua::registerMethod(L, "Game", "createNpcType", GameFunctions::luaGameCreateNpcType); + Lua::registerMethod(L, "Game", "createMonsterType", GameFunctions::luaGameCreateMonsterType); + + Lua::registerMethod(L, "Game", "getSpectators", GameFunctions::luaGameGetSpectators); + + Lua::registerMethod(L, "Game", "getBoostedCreature", GameFunctions::luaGameGetBoostedCreature); + Lua::registerMethod(L, "Game", "getBestiaryList", GameFunctions::luaGameGetBestiaryList); + + Lua::registerMethod(L, "Game", "getPlayers", GameFunctions::luaGameGetPlayers); + Lua::registerMethod(L, "Game", "loadMap", GameFunctions::luaGameLoadMap); + Lua::registerMethod(L, "Game", "loadMapChunk", GameFunctions::luaGameloadMapChunk); + + Lua::registerMethod(L, "Game", "getExperienceForLevel", GameFunctions::luaGameGetExperienceForLevel); + Lua::registerMethod(L, "Game", "getMonsterCount", GameFunctions::luaGameGetMonsterCount); + Lua::registerMethod(L, "Game", "getPlayerCount", GameFunctions::luaGameGetPlayerCount); + Lua::registerMethod(L, "Game", "getNpcCount", GameFunctions::luaGameGetNpcCount); + Lua::registerMethod(L, "Game", "getMonsterTypes", GameFunctions::luaGameGetMonsterTypes); + + Lua::registerMethod(L, "Game", "getTowns", GameFunctions::luaGameGetTowns); + Lua::registerMethod(L, "Game", "getHouses", GameFunctions::luaGameGetHouses); + + Lua::registerMethod(L, "Game", "getGameState", GameFunctions::luaGameGetGameState); + Lua::registerMethod(L, "Game", "setGameState", GameFunctions::luaGameSetGameState); + + Lua::registerMethod(L, "Game", "getWorldType", GameFunctions::luaGameGetWorldType); + Lua::registerMethod(L, "Game", "setWorldType", GameFunctions::luaGameSetWorldType); + + Lua::registerMethod(L, "Game", "getReturnMessage", GameFunctions::luaGameGetReturnMessage); + + Lua::registerMethod(L, "Game", "createItem", GameFunctions::luaGameCreateItem); + Lua::registerMethod(L, "Game", "createContainer", GameFunctions::luaGameCreateContainer); + Lua::registerMethod(L, "Game", "createMonster", GameFunctions::luaGameCreateMonster); + Lua::registerMethod(L, "Game", "createNpc", GameFunctions::luaGameCreateNpc); + Lua::registerMethod(L, "Game", "generateNpc", GameFunctions::luaGameGenerateNpc); + Lua::registerMethod(L, "Game", "createTile", GameFunctions::luaGameCreateTile); + Lua::registerMethod(L, "Game", "createBestiaryCharm", GameFunctions::luaGameCreateBestiaryCharm); + + Lua::registerMethod(L, "Game", "createItemClassification", GameFunctions::luaGameCreateItemClassification); + + Lua::registerMethod(L, "Game", "getBestiaryCharm", GameFunctions::luaGameGetBestiaryCharm); + + Lua::registerMethod(L, "Game", "startRaid", GameFunctions::luaGameStartRaid); + + Lua::registerMethod(L, "Game", "getClientVersion", GameFunctions::luaGameGetClientVersion); + + Lua::registerMethod(L, "Game", "reload", GameFunctions::luaGameReload); + + Lua::registerMethod(L, "Game", "hasDistanceEffect", GameFunctions::luaGameHasDistanceEffect); + Lua::registerMethod(L, "Game", "hasEffect", GameFunctions::luaGameHasEffect); + Lua::registerMethod(L, "Game", "getOfflinePlayer", GameFunctions::luaGameGetOfflinePlayer); + Lua::registerMethod(L, "Game", "getNormalizedPlayerName", GameFunctions::luaGameGetNormalizedPlayerName); + Lua::registerMethod(L, "Game", "getNormalizedGuildName", GameFunctions::luaGameGetNormalizedGuildName); + + Lua::registerMethod(L, "Game", "addInfluencedMonster", GameFunctions::luaGameAddInfluencedMonster); + Lua::registerMethod(L, "Game", "removeInfluencedMonster", GameFunctions::luaGameRemoveInfluencedMonster); + Lua::registerMethod(L, "Game", "getInfluencedMonsters", GameFunctions::luaGameGetInfluencedMonsters); + Lua::registerMethod(L, "Game", "makeFiendishMonster", GameFunctions::luaGameMakeFiendishMonster); + Lua::registerMethod(L, "Game", "removeFiendishMonster", GameFunctions::luaGameRemoveFiendishMonster); + Lua::registerMethod(L, "Game", "getFiendishMonsters", GameFunctions::luaGameGetFiendishMonsters); + Lua::registerMethod(L, "Game", "getBoostedBoss", GameFunctions::luaGameGetBoostedBoss); + + Lua::registerMethod(L, "Game", "getLadderIds", GameFunctions::luaGameGetLadderIds); + Lua::registerMethod(L, "Game", "getDummies", GameFunctions::luaGameGetDummies); + + Lua::registerMethod(L, "Game", "getTalkActions", GameFunctions::luaGameGetTalkActions); + Lua::registerMethod(L, "Game", "getEventCallbacks", GameFunctions::luaGameGetEventCallbacks); + + Lua::registerMethod(L, "Game", "registerAchievement", GameFunctions::luaGameRegisterAchievement); + Lua::registerMethod(L, "Game", "getAchievementInfoById", GameFunctions::luaGameGetAchievementInfoById); + Lua::registerMethod(L, "Game", "getAchievementInfoByName", GameFunctions::luaGameGetAchievementInfoByName); + Lua::registerMethod(L, "Game", "getSecretAchievements", GameFunctions::luaGameGetSecretAchievements); + Lua::registerMethod(L, "Game", "getPublicAchievements", GameFunctions::luaGameGetPublicAchievements); + Lua::registerMethod(L, "Game", "getAchievements", GameFunctions::luaGameGetAchievements); +} // Game int GameFunctions::luaGameCreateMonsterType(lua_State* L) { // Game.createMonsterType(name[, variant = ""[, alternateName = ""]]) - if (isString(L, 1)) { - const auto name = getString(L, 1); + if (Lua::isString(L, 1)) { + const auto name = Lua::getString(L, 1); std::string uniqueName = name; - auto variant = getString(L, 2, ""); - const auto alternateName = getString(L, 3, ""); + auto variant = Lua::getString(L, 2, ""); + const auto alternateName = Lua::getString(L, 3, ""); std::set names; const auto monsterType = std::make_shared(name); if (!monsterType) { @@ -73,8 +152,8 @@ int GameFunctions::luaGameCreateMonsterType(lua_State* L) { } } - pushUserdata(L, monsterType); - setMetatable(L, -1, "MonsterType"); + Lua::pushUserdata(L, monsterType); + Lua::setMetatable(L, -1, "MonsterType"); } else { lua_pushnil(L); } @@ -87,13 +166,13 @@ int GameFunctions::luaGameCreateNpcType(lua_State* L) { int GameFunctions::luaGameGetSpectators(lua_State* L) { // Game.getSpectators(position[, multifloor = false[, onlyPlayer = false[, minRangeX = 0[, maxRangeX = 0[, minRangeY = 0[, maxRangeY = 0]]]]]]) - const Position &position = getPosition(L, 1); - const bool multifloor = getBoolean(L, 2, false); - const bool onlyPlayers = getBoolean(L, 3, false); - const auto minRangeX = getNumber(L, 4, 0); - const auto maxRangeX = getNumber(L, 5, 0); - const auto minRangeY = getNumber(L, 6, 0); - const auto maxRangeY = getNumber(L, 7, 0); + const Position &position = Lua::getPosition(L, 1); + const bool multifloor = Lua::getBoolean(L, 2, false); + const bool onlyPlayers = Lua::getBoolean(L, 3, false); + const auto minRangeX = Lua::getNumber(L, 4, 0); + const auto maxRangeX = Lua::getNumber(L, 5, 0); + const auto minRangeY = Lua::getNumber(L, 6, 0); + const auto maxRangeY = Lua::getNumber(L, 7, 0); Spectators spectators; @@ -107,8 +186,8 @@ int GameFunctions::luaGameGetSpectators(lua_State* L) { int index = 0; for (const auto &creature : spectators) { - pushUserdata(L, creature); - setCreatureMetatable(L, -1, creature); + Lua::pushUserdata(L, creature); + Lua::setCreatureMetatable(L, -1, creature); lua_rawseti(L, -2, ++index); } return 1; @@ -116,7 +195,7 @@ int GameFunctions::luaGameGetSpectators(lua_State* L) { int GameFunctions::luaGameGetBoostedCreature(lua_State* L) { // Game.getBoostedCreature() - pushString(L, g_game().getBoostedMonsterName()); + Lua::pushString(L, g_game().getBoostedMonsterName()); return 1; } @@ -124,34 +203,34 @@ int GameFunctions::luaGameGetBestiaryList(lua_State* L) { // Game.getBestiaryList([bool[string or BestiaryType_t]]) lua_newtable(L); int index = 0; - const bool name = getBoolean(L, 2, false); + const bool name = Lua::getBoolean(L, 2, false); if (lua_gettop(L) <= 2) { const std::map &mtype_list = g_game().getBestiaryList(); for (const auto &ita : mtype_list) { if (name) { - pushString(L, ita.second); + Lua::pushString(L, ita.second); } else { lua_pushnumber(L, ita.first); } lua_rawseti(L, -2, ++index); } } else { - if (isNumber(L, 2)) { - const std::map tmplist = g_iobestiary().findRaceByName("CANARY", false, getNumber(L, 2)); + if (Lua::isNumber(L, 2)) { + const std::map tmplist = g_iobestiary().findRaceByName("CANARY", false, Lua::getNumber(L, 2)); for (const auto &itb : tmplist) { if (name) { - pushString(L, itb.second); + Lua::pushString(L, itb.second); } else { lua_pushnumber(L, itb.first); } lua_rawseti(L, -2, ++index); } } else { - const std::map tmplist = g_iobestiary().findRaceByName(getString(L, 2)); + const std::map tmplist = g_iobestiary().findRaceByName(Lua::getString(L, 2)); for (const auto &itc : tmplist) { if (name) { - pushString(L, itc.second); + Lua::pushString(L, itc.second); } else { lua_pushnumber(L, itc.first); } @@ -168,8 +247,8 @@ int GameFunctions::luaGameGetPlayers(lua_State* L) { int index = 0; for (const auto &playerEntry : g_game().getPlayers()) { - pushUserdata(L, playerEntry.second); - setMetatable(L, -1, "Player"); + Lua::pushUserdata(L, playerEntry.second); + Lua::setMetatable(L, -1, "Player"); lua_rawseti(L, -2, ++index); } return 1; @@ -177,24 +256,24 @@ int GameFunctions::luaGameGetPlayers(lua_State* L) { int GameFunctions::luaGameLoadMap(lua_State* L) { // Game.loadMap(path) - const std::string &path = getString(L, 1); + const std::string &path = Lua::getString(L, 1); g_dispatcher().addEvent([path]() { g_game().loadMap(path); }, __FUNCTION__); return 0; } int GameFunctions::luaGameloadMapChunk(lua_State* L) { // Game.loadMapChunk(path, position, remove) - const std::string &path = getString(L, 1); - const Position &position = getPosition(L, 2); + const std::string &path = Lua::getString(L, 1); + const Position &position = Lua::getPosition(L, 2); g_dispatcher().addEvent([path, position]() { g_game().loadMap(path, position); }, __FUNCTION__); return 0; } int GameFunctions::luaGameGetExperienceForLevel(lua_State* L) { // Game.getExperienceForLevel(level) - const uint32_t level = getNumber(L, 1); + const uint32_t level = Lua::getNumber(L, 1); if (level == 0) { - reportErrorFunc("Level must be greater than 0."); + Lua::reportErrorFunc("Level must be greater than 0."); } else { lua_pushnumber(L, Player::getExpForLevel(level)); } @@ -225,8 +304,8 @@ int GameFunctions::luaGameGetMonsterTypes(lua_State* L) { lua_createtable(L, type.size(), 0); for (const auto &[typeName, mType] : type) { - pushUserdata(L, mType); - setMetatable(L, -1, "MonsterType"); + Lua::pushUserdata(L, mType); + Lua::setMetatable(L, -1, "MonsterType"); lua_setfield(L, -2, typeName.c_str()); } return 1; @@ -239,8 +318,8 @@ int GameFunctions::luaGameGetTowns(lua_State* L) { int index = 0; for (const auto &townEntry : towns) { - pushUserdata(L, townEntry.second); - setMetatable(L, -1, "Town"); + Lua::pushUserdata(L, townEntry.second); + Lua::setMetatable(L, -1, "Town"); lua_rawseti(L, -2, ++index); } return 1; @@ -253,8 +332,8 @@ int GameFunctions::luaGameGetHouses(lua_State* L) { int index = 0; for (const auto &houseEntry : houses) { - pushUserdata(L, houseEntry.second); - setMetatable(L, -1, "House"); + Lua::pushUserdata(L, houseEntry.second); + Lua::setMetatable(L, -1, "House"); lua_rawseti(L, -2, ++index); } return 1; @@ -268,9 +347,9 @@ int GameFunctions::luaGameGetGameState(lua_State* L) { int GameFunctions::luaGameSetGameState(lua_State* L) { // Game.setGameState(state) - const GameState_t state = getNumber(L, 1); + const GameState_t state = Lua::getNumber(L, 1); g_game().setGameState(state); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -282,33 +361,33 @@ int GameFunctions::luaGameGetWorldType(lua_State* L) { int GameFunctions::luaGameSetWorldType(lua_State* L) { // Game.setWorldType(type) - const WorldType_t type = getNumber(L, 1); + const WorldType_t type = Lua::getNumber(L, 1); g_game().setWorldType(type); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int GameFunctions::luaGameGetReturnMessage(lua_State* L) { // Game.getReturnMessage(value) - const ReturnValue value = getNumber(L, 1); - pushString(L, getReturnMessage(value)); + const ReturnValue value = Lua::getNumber(L, 1); + Lua::pushString(L, getReturnMessage(value)); return 1; } int GameFunctions::luaGameCreateItem(lua_State* L) { // Game.createItem(itemId or name[, count[, position]]) uint16_t itemId; - if (isNumber(L, 1)) { - itemId = getNumber(L, 1); + if (Lua::isNumber(L, 1)) { + itemId = Lua::getNumber(L, 1); } else { - itemId = Item::items.getItemIdByName(getString(L, 1)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 1)); if (itemId == 0) { lua_pushnil(L); return 1; } } - const auto count = getNumber(L, 2, 1); + const auto count = Lua::getNumber(L, 2, 1); int32_t itemCount = 1; int32_t subType = 1; @@ -325,7 +404,7 @@ int GameFunctions::luaGameCreateItem(lua_State* L) { Position position; if (lua_gettop(L) >= 3) { - position = getPosition(L, 3); + position = Lua::getPosition(L, 3); } const bool hasTable = itemCount > 1; @@ -368,18 +447,18 @@ int GameFunctions::luaGameCreateItem(lua_State* L) { return 1; } } else { - getScriptEnv()->addTempItem(item); + Lua::getScriptEnv()->addTempItem(item); item->setParent(VirtualCylinder::virtualCylinder); } if (hasTable) { lua_pushnumber(L, i); - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); lua_settable(L, -3); } else { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } } @@ -388,12 +467,12 @@ int GameFunctions::luaGameCreateItem(lua_State* L) { int GameFunctions::luaGameCreateContainer(lua_State* L) { // Game.createContainer(itemId, size[, position]) - const uint16_t size = getNumber(L, 2); + const uint16_t size = Lua::getNumber(L, 2); uint16_t id; - if (isNumber(L, 1)) { - id = getNumber(L, 1); + if (Lua::isNumber(L, 1)) { + id = Lua::getNumber(L, 1); } else { - id = Item::items.getItemIdByName(getString(L, 1)); + id = Item::items.getItemIdByName(Lua::getString(L, 1)); if (id == 0) { lua_pushnil(L); return 1; @@ -407,7 +486,7 @@ int GameFunctions::luaGameCreateContainer(lua_State* L) { } if (lua_gettop(L) >= 3) { - const Position &position = getPosition(L, 3); + const Position &position = Lua::getPosition(L, 3); const auto &tile = g_game().map.getTile(position); if (!tile) { lua_pushnil(L); @@ -416,18 +495,18 @@ int GameFunctions::luaGameCreateContainer(lua_State* L) { g_game().internalAddItem(tile, container, INDEX_WHEREEVER, FLAG_NOLIMIT); } else { - getScriptEnv()->addTempItem(container); + Lua::getScriptEnv()->addTempItem(container); container->setParent(VirtualCylinder::virtualCylinder); } - pushUserdata(L, container); - setMetatable(L, -1, "Container"); + Lua::pushUserdata(L, container); + Lua::setMetatable(L, -1, "Container"); return 1; } int GameFunctions::luaGameCreateMonster(lua_State* L) { // Game.createMonster(monsterName, position[, extended = false[, force = false[, master = nil]]]) - const auto &monster = Monster::createMonster(getString(L, 1)); + const auto &monster = Monster::createMonster(Lua::getString(L, 1)); if (!monster) { lua_pushnil(L); return 1; @@ -435,15 +514,15 @@ int GameFunctions::luaGameCreateMonster(lua_State* L) { bool isSummon = false; if (lua_gettop(L) >= 5) { - if (const auto &master = getCreature(L, 5)) { + if (const auto &master = Lua::getCreature(L, 5)) { monster->setMaster(master, true); isSummon = true; } } - const Position &position = getPosition(L, 2); - const bool extended = getBoolean(L, 3, false); - const bool force = getBoolean(L, 4, false); + const Position &position = Lua::getPosition(L, 2); + const bool extended = Lua::getBoolean(L, 3, false); + const bool force = Lua::getBoolean(L, 4, false); if (g_game().placeCreature(monster, position, extended, force)) { monster->onSpawn(); const auto &mtype = monster->getMonsterType(); @@ -455,8 +534,8 @@ int GameFunctions::luaGameCreateMonster(lua_State* L) { } } - pushUserdata(L, monster); - setMetatable(L, -1, "Monster"); + Lua::pushUserdata(L, monster); + Lua::setMetatable(L, -1, "Monster"); } else { if (isSummon) { monster->setMaster(nullptr); @@ -469,31 +548,31 @@ int GameFunctions::luaGameCreateMonster(lua_State* L) { int GameFunctions::luaGameGenerateNpc(lua_State* L) { // Game.generateNpc(npcName) - const auto &npc = Npc::createNpc(getString(L, 1)); + const auto &npc = Npc::createNpc(Lua::getString(L, 1)); if (!npc) { lua_pushnil(L); return 1; } else { - pushUserdata(L, npc); - setMetatable(L, -1, "Npc"); + Lua::pushUserdata(L, npc); + Lua::setMetatable(L, -1, "Npc"); } return 1; } int GameFunctions::luaGameCreateNpc(lua_State* L) { // Game.createNpc(npcName, position[, extended = false[, force = false]]) - const auto &npc = Npc::createNpc(getString(L, 1)); + const auto &npc = Npc::createNpc(Lua::getString(L, 1)); if (!npc) { lua_pushnil(L); return 1; } - const Position &position = getPosition(L, 2); - const bool extended = getBoolean(L, 3, false); - const bool force = getBoolean(L, 4, false); + const Position &position = Lua::getPosition(L, 2); + const bool extended = Lua::getBoolean(L, 3, false); + const bool force = Lua::getBoolean(L, 4, false); if (g_game().placeCreature(npc, position, extended, force)) { - pushUserdata(L, npc); - setMetatable(L, -1, "Npc"); + Lua::pushUserdata(L, npc); + Lua::setMetatable(L, -1, "Npc"); } else { lua_pushnil(L); } @@ -505,18 +584,18 @@ int GameFunctions::luaGameCreateTile(lua_State* L) { // Game.createTile(position[, isDynamic = false]) Position position; bool isDynamic; - if (isTable(L, 1)) { - position = getPosition(L, 1); - isDynamic = getBoolean(L, 2, false); + if (Lua::isTable(L, 1)) { + position = Lua::getPosition(L, 1); + isDynamic = Lua::getBoolean(L, 2, false); } else { - position.x = getNumber(L, 1); - position.y = getNumber(L, 2); - position.z = getNumber(L, 3); - isDynamic = getBoolean(L, 4, false); + position.x = Lua::getNumber(L, 1); + position.y = Lua::getNumber(L, 2); + position.z = Lua::getNumber(L, 3); + isDynamic = Lua::getBoolean(L, 4, false); } - pushUserdata(L, g_game().map.getOrCreateTile(position, isDynamic)); - setMetatable(L, -1, "Tile"); + Lua::pushUserdata(L, g_game().map.getOrCreateTile(position, isDynamic)); + Lua::setMetatable(L, -1, "Tile"); return 1; } @@ -527,8 +606,8 @@ int GameFunctions::luaGameGetBestiaryCharm(lua_State* L) { int index = 0; for (const auto &charmPtr : c_list) { - pushUserdata(L, charmPtr); - setMetatable(L, -1, "Charm"); + Lua::pushUserdata(L, charmPtr); + Lua::setMetatable(L, -1, "Charm"); lua_rawseti(L, -2, ++index); } return 1; @@ -536,9 +615,9 @@ int GameFunctions::luaGameGetBestiaryCharm(lua_State* L) { int GameFunctions::luaGameCreateBestiaryCharm(lua_State* L) { // Game.createBestiaryCharm(id) - if (const std::shared_ptr &charm = g_iobestiary().getBestiaryCharm(static_cast(getNumber(L, 1, 0)), true)) { - pushUserdata(L, charm); - setMetatable(L, -1, "Charm"); + if (const std::shared_ptr &charm = g_iobestiary().getBestiaryCharm(static_cast(Lua::getNumber(L, 1, 0)), true)) { + Lua::pushUserdata(L, charm); + Lua::setMetatable(L, -1, "Charm"); } else { lua_pushnil(L); } @@ -547,10 +626,10 @@ int GameFunctions::luaGameCreateBestiaryCharm(lua_State* L) { int GameFunctions::luaGameCreateItemClassification(lua_State* L) { // Game.createItemClassification(id) - const ItemClassification* itemClassification = g_game().getItemsClassification(getNumber(L, 1), true); + const ItemClassification* itemClassification = g_game().getItemsClassification(Lua::getNumber(L, 1), true); if (itemClassification) { - pushUserdata(L, itemClassification); - setMetatable(L, -1, "ItemClassification"); + Lua::pushUserdata(L, itemClassification); + Lua::setMetatable(L, -1, "ItemClassification"); } else { lua_pushnil(L); } @@ -559,7 +638,7 @@ int GameFunctions::luaGameCreateItemClassification(lua_State* L) { int GameFunctions::luaGameStartRaid(lua_State* L) { // Game.startRaid(raidName) - const std::string &raidName = getString(L, 1); + const std::string &raidName = Lua::getString(L, 1); const auto &raid = g_game().raids.getRaidByName(raidName); if (!raid || !raid->isLoaded()) { @@ -581,66 +660,66 @@ int GameFunctions::luaGameStartRaid(lua_State* L) { int GameFunctions::luaGameGetClientVersion(lua_State* L) { // Game.getClientVersion() lua_createtable(L, 0, 3); - setField(L, "min", CLIENT_VERSION); - setField(L, "max", CLIENT_VERSION); + Lua::setField(L, "min", CLIENT_VERSION); + Lua::setField(L, "max", CLIENT_VERSION); const std::string version = fmt::format("{}.{}", CLIENT_VERSION_UPPER, CLIENT_VERSION_LOWER); - setField(L, "string", version); + Lua::setField(L, "string", version); return 1; } int GameFunctions::luaGameReload(lua_State* L) { // Game.reload(reloadType) - const Reload_t reloadType = getNumber(L, 1); + const Reload_t reloadType = Lua::getNumber(L, 1); if (GameReload::getReloadNumber(reloadType) == GameReload::getReloadNumber(Reload_t::RELOAD_TYPE_NONE)) { - reportErrorFunc("Reload type is none"); - pushBoolean(L, false); + Lua::reportErrorFunc("Reload type is none"); + Lua::pushBoolean(L, false); return 0; } if (GameReload::getReloadNumber(reloadType) >= GameReload::getReloadNumber(Reload_t::RELOAD_TYPE_LAST)) { - reportErrorFunc("Reload type not exist"); - pushBoolean(L, false); + Lua::reportErrorFunc("Reload type not exist"); + Lua::pushBoolean(L, false); return 0; } - pushBoolean(L, GameReload::init(reloadType)); + Lua::pushBoolean(L, GameReload::init(reloadType)); lua_gc(g_luaEnvironment().getLuaState(), LUA_GCCOLLECT, 0); return 1; } int GameFunctions::luaGameHasEffect(lua_State* L) { // Game.hasEffect(effectId) - const uint16_t effectId = getNumber(L, 1); - pushBoolean(L, g_game().hasEffect(effectId)); + const uint16_t effectId = Lua::getNumber(L, 1); + Lua::pushBoolean(L, g_game().hasEffect(effectId)); return 1; } int GameFunctions::luaGameHasDistanceEffect(lua_State* L) { // Game.hasDistanceEffect(effectId) - const uint16_t effectId = getNumber(L, 1); - pushBoolean(L, g_game().hasDistanceEffect(effectId)); + const uint16_t effectId = Lua::getNumber(L, 1); + Lua::pushBoolean(L, g_game().hasDistanceEffect(effectId)); return 1; } int GameFunctions::luaGameGetOfflinePlayer(lua_State* L) { // Game.getOfflinePlayer(name or id) std::shared_ptr player = nullptr; - if (isNumber(L, 1)) { - const uint32_t id = getNumber(L, 1); + if (Lua::isNumber(L, 1)) { + const uint32_t id = Lua::getNumber(L, 1); if (id >= Player::getFirstID() && id <= Player::getLastID()) { player = g_game().getPlayerByID(id, true); } else { player = g_game().getPlayerByGUID(id, true); } - } else if (isString(L, 1)) { - const auto name = getString(L, 1); + } else if (Lua::isString(L, 1)) { + const auto name = Lua::getString(L, 1); player = g_game().getPlayerByName(name, true); } if (!player) { lua_pushnil(L); } else { - pushUserdata(L, player); - setMetatable(L, -1, "Player"); + Lua::pushUserdata(L, player); + Lua::setMetatable(L, -1, "Player"); } return 1; @@ -648,11 +727,11 @@ int GameFunctions::luaGameGetOfflinePlayer(lua_State* L) { int GameFunctions::luaGameGetNormalizedPlayerName(lua_State* L) { // Game.getNormalizedPlayerName(name[, isNewName = false]) - const auto name = getString(L, 1); - const auto isNewName = getBoolean(L, 2, false); + const auto name = Lua::getString(L, 1); + const auto isNewName = Lua::getBoolean(L, 2, false); const auto &player = g_game().getPlayerByName(name, true, isNewName); if (player) { - pushString(L, player->getName()); + Lua::pushString(L, player->getName()); } else { lua_pushnil(L); } @@ -661,10 +740,10 @@ int GameFunctions::luaGameGetNormalizedPlayerName(lua_State* L) { int GameFunctions::luaGameGetNormalizedGuildName(lua_State* L) { // Game.getNormalizedGuildName(name) - const auto name = getString(L, 1); + const auto name = Lua::getString(L, 1); const auto &guild = g_game().getGuildByName(name, true); if (guild) { - pushString(L, guild->getName()); + Lua::pushString(L, guild->getName()); } else { lua_pushnil(L); } @@ -673,10 +752,10 @@ int GameFunctions::luaGameGetNormalizedGuildName(lua_State* L) { int GameFunctions::luaGameAddInfluencedMonster(lua_State* L) { // Game.addInfluencedMonster(monster) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -686,8 +765,8 @@ int GameFunctions::luaGameAddInfluencedMonster(lua_State* L) { int GameFunctions::luaGameRemoveInfluencedMonster(lua_State* L) { // Game.removeInfluencedMonster(monsterId) - const uint32_t monsterId = getNumber(L, 1); - const auto create = getBoolean(L, 2, false); + const uint32_t monsterId = Lua::getNumber(L, 1); + const auto create = Lua::getBoolean(L, 2, false); lua_pushnumber(L, g_game().removeInfluencedMonster(monsterId, create)); return 1; } @@ -741,16 +820,16 @@ int GameFunctions::luaGameGetDummies(lua_State* L) { int GameFunctions::luaGameMakeFiendishMonster(lua_State* L) { // Game.makeFiendishMonster(monsterId[default= 0]) - const auto monsterId = getNumber(L, 1, 0); - const auto createForgeableMonsters = getBoolean(L, 2, false); + const auto monsterId = Lua::getNumber(L, 1, 0); + const auto createForgeableMonsters = Lua::getBoolean(L, 2, false); lua_pushnumber(L, g_game().makeFiendishMonster(monsterId, createForgeableMonsters)); return 1; } int GameFunctions::luaGameRemoveFiendishMonster(lua_State* L) { // Game.removeFiendishMonster(monsterId) - const uint32_t monsterId = getNumber(L, 1); - const auto create = getBoolean(L, 2, false); + const uint32_t monsterId = Lua::getNumber(L, 1); + const auto create = Lua::getBoolean(L, 2, false); lua_pushnumber(L, g_game().removeFiendishMonster(monsterId, create)); return 1; } @@ -772,7 +851,7 @@ int GameFunctions::luaGameGetFiendishMonsters(lua_State* L) { int GameFunctions::luaGameGetBoostedBoss(lua_State* L) { // Game.getBoostedBoss() - pushString(L, g_ioBosstiary().getBoostedBossName()); + Lua::pushString(L, g_ioBosstiary().getBoostedBossName()); return 1; } @@ -782,8 +861,8 @@ int GameFunctions::luaGameGetTalkActions(lua_State* L) { lua_createtable(L, static_cast(talkactionsMap.size()), 0); for (const auto &[talkName, talkactionSharedPtr] : talkactionsMap) { - pushUserdata(L, talkactionSharedPtr); - setMetatable(L, -1, "TalkAction"); + Lua::pushUserdata(L, talkactionSharedPtr); + Lua::setMetatable(L, -1, "TalkAction"); lua_setfield(L, -2, talkName.c_str()); } return 1; @@ -809,56 +888,56 @@ int GameFunctions::luaGameGetEventCallbacks(lua_State* L) { int GameFunctions::luaGameRegisterAchievement(lua_State* L) { // Game.registerAchievement(id, name, description, secret, grade, points) if (lua_gettop(L) < 6) { - reportErrorFunc("Achievement can only be registered with all params."); + Lua::reportErrorFunc("Achievement can only be registered with all params."); return 1; } - const uint16_t id = getNumber(L, 1); - const std::string name = getString(L, 2); - const std::string description = getString(L, 3); - const bool secret = getBoolean(L, 4); - const uint8_t grade = getNumber(L, 5); - const uint8_t points = getNumber(L, 6); + const uint16_t id = Lua::getNumber(L, 1); + const std::string name = Lua::getString(L, 2); + const std::string description = Lua::getString(L, 3); + const bool secret = Lua::getBoolean(L, 4); + const uint8_t grade = Lua::getNumber(L, 5); + const uint8_t points = Lua::getNumber(L, 6); g_game().registerAchievement(id, name, description, secret, grade, points); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int GameFunctions::luaGameGetAchievementInfoById(lua_State* L) { // Game.getAchievementInfoById(id) - const uint16_t id = getNumber(L, 1); + const uint16_t id = Lua::getNumber(L, 1); const Achievement achievement = g_game().getAchievementById(id); if (achievement.id == 0) { - reportErrorFunc("Achievement id is wrong"); + Lua::reportErrorFunc("Achievement id is wrong"); return 1; } lua_createtable(L, 0, 6); - setField(L, "id", achievement.id); - setField(L, "name", achievement.name); - setField(L, "description", achievement.description); - setField(L, "points", achievement.points); - setField(L, "grade", achievement.grade); - setField(L, "secret", achievement.secret); + Lua::setField(L, "id", achievement.id); + Lua::setField(L, "name", achievement.name); + Lua::setField(L, "description", achievement.description); + Lua::setField(L, "points", achievement.points); + Lua::setField(L, "grade", achievement.grade); + Lua::setField(L, "secret", achievement.secret); return 1; } int GameFunctions::luaGameGetAchievementInfoByName(lua_State* L) { // Game.getAchievementInfoByName(name) - const std::string name = getString(L, 1); + const std::string name = Lua::getString(L, 1); const Achievement achievement = g_game().getAchievementByName(name); if (achievement.id == 0) { - reportErrorFunc("Achievement name is wrong"); + Lua::reportErrorFunc("Achievement name is wrong"); return 1; } lua_createtable(L, 0, 6); - setField(L, "id", achievement.id); - setField(L, "name", achievement.name); - setField(L, "description", achievement.description); - setField(L, "points", achievement.points); - setField(L, "grade", achievement.grade); - setField(L, "secret", achievement.secret); + Lua::setField(L, "id", achievement.id); + Lua::setField(L, "name", achievement.name); + Lua::setField(L, "description", achievement.description); + Lua::setField(L, "points", achievement.points); + Lua::setField(L, "grade", achievement.grade); + Lua::setField(L, "secret", achievement.secret); return 1; } @@ -869,12 +948,12 @@ int GameFunctions::luaGameGetSecretAchievements(lua_State* L) { lua_createtable(L, achievements.size(), 0); for (const auto &achievement : achievements) { lua_createtable(L, 0, 6); - setField(L, "id", achievement.id); - setField(L, "name", achievement.name); - setField(L, "description", achievement.description); - setField(L, "points", achievement.points); - setField(L, "grade", achievement.grade); - setField(L, "secret", achievement.secret); + Lua::setField(L, "id", achievement.id); + Lua::setField(L, "name", achievement.name); + Lua::setField(L, "description", achievement.description); + Lua::setField(L, "points", achievement.points); + Lua::setField(L, "grade", achievement.grade); + Lua::setField(L, "secret", achievement.secret); lua_rawseti(L, -2, ++index); } return 1; @@ -887,12 +966,12 @@ int GameFunctions::luaGameGetPublicAchievements(lua_State* L) { lua_createtable(L, achievements.size(), 0); for (const auto &achievement : achievements) { lua_createtable(L, 0, 6); - setField(L, "id", achievement.id); - setField(L, "name", achievement.name); - setField(L, "description", achievement.description); - setField(L, "points", achievement.points); - setField(L, "grade", achievement.grade); - setField(L, "secret", achievement.secret); + Lua::setField(L, "id", achievement.id); + Lua::setField(L, "name", achievement.name); + Lua::setField(L, "description", achievement.description); + Lua::setField(L, "points", achievement.points); + Lua::setField(L, "grade", achievement.grade); + Lua::setField(L, "secret", achievement.secret); lua_rawseti(L, -2, ++index); } return 1; @@ -905,12 +984,12 @@ int GameFunctions::luaGameGetAchievements(lua_State* L) { lua_createtable(L, achievements.size(), 0); for (const auto &achievement_it : achievements) { lua_createtable(L, 0, 6); - setField(L, "id", achievement_it.first); - setField(L, "name", achievement_it.second.name); - setField(L, "description", achievement_it.second.description); - setField(L, "points", achievement_it.second.points); - setField(L, "grade", achievement_it.second.grade); - setField(L, "secret", achievement_it.second.secret); + Lua::setField(L, "id", achievement_it.first); + Lua::setField(L, "name", achievement_it.second.name); + Lua::setField(L, "description", achievement_it.second.description); + Lua::setField(L, "points", achievement_it.second.points); + Lua::setField(L, "grade", achievement_it.second.grade); + Lua::setField(L, "secret", achievement_it.second.secret); lua_rawseti(L, -2, ++index); } return 1; diff --git a/src/lua/functions/core/game/game_functions.hpp b/src/lua/functions/core/game/game_functions.hpp index 3c668c54fb4..6d332face9f 100644 --- a/src/lua/functions/core/game/game_functions.hpp +++ b/src/lua/functions/core/game/game_functions.hpp @@ -9,93 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class GameFunctions final : LuaScriptInterface { +class GameFunctions { public: - explicit GameFunctions(lua_State* L) : - LuaScriptInterface("GameFunctions") { - init(L); - } - ~GameFunctions() override = default; - - static void init(lua_State* L) { - registerTable(L, "Game"); - - registerMethod(L, "Game", "createNpcType", GameFunctions::luaGameCreateNpcType); - registerMethod(L, "Game", "createMonsterType", GameFunctions::luaGameCreateMonsterType); - - registerMethod(L, "Game", "getSpectators", GameFunctions::luaGameGetSpectators); - - registerMethod(L, "Game", "getBoostedCreature", GameFunctions::luaGameGetBoostedCreature); - registerMethod(L, "Game", "getBestiaryList", GameFunctions::luaGameGetBestiaryList); - - registerMethod(L, "Game", "getPlayers", GameFunctions::luaGameGetPlayers); - registerMethod(L, "Game", "loadMap", GameFunctions::luaGameLoadMap); - registerMethod(L, "Game", "loadMapChunk", GameFunctions::luaGameloadMapChunk); - - registerMethod(L, "Game", "getExperienceForLevel", GameFunctions::luaGameGetExperienceForLevel); - registerMethod(L, "Game", "getMonsterCount", GameFunctions::luaGameGetMonsterCount); - registerMethod(L, "Game", "getPlayerCount", GameFunctions::luaGameGetPlayerCount); - registerMethod(L, "Game", "getNpcCount", GameFunctions::luaGameGetNpcCount); - registerMethod(L, "Game", "getMonsterTypes", GameFunctions::luaGameGetMonsterTypes); - - registerMethod(L, "Game", "getTowns", GameFunctions::luaGameGetTowns); - registerMethod(L, "Game", "getHouses", GameFunctions::luaGameGetHouses); - - registerMethod(L, "Game", "getGameState", GameFunctions::luaGameGetGameState); - registerMethod(L, "Game", "setGameState", GameFunctions::luaGameSetGameState); - - registerMethod(L, "Game", "getWorldType", GameFunctions::luaGameGetWorldType); - registerMethod(L, "Game", "setWorldType", GameFunctions::luaGameSetWorldType); - - registerMethod(L, "Game", "getReturnMessage", GameFunctions::luaGameGetReturnMessage); - - registerMethod(L, "Game", "createItem", GameFunctions::luaGameCreateItem); - registerMethod(L, "Game", "createContainer", GameFunctions::luaGameCreateContainer); - registerMethod(L, "Game", "createMonster", GameFunctions::luaGameCreateMonster); - registerMethod(L, "Game", "createNpc", GameFunctions::luaGameCreateNpc); - registerMethod(L, "Game", "generateNpc", GameFunctions::luaGameGenerateNpc); - registerMethod(L, "Game", "createTile", GameFunctions::luaGameCreateTile); - registerMethod(L, "Game", "createBestiaryCharm", GameFunctions::luaGameCreateBestiaryCharm); - - registerMethod(L, "Game", "createItemClassification", GameFunctions::luaGameCreateItemClassification); - - registerMethod(L, "Game", "getBestiaryCharm", GameFunctions::luaGameGetBestiaryCharm); - - registerMethod(L, "Game", "startRaid", GameFunctions::luaGameStartRaid); - - registerMethod(L, "Game", "getClientVersion", GameFunctions::luaGameGetClientVersion); - - registerMethod(L, "Game", "reload", GameFunctions::luaGameReload); - - registerMethod(L, "Game", "hasDistanceEffect", GameFunctions::luaGameHasDistanceEffect); - registerMethod(L, "Game", "hasEffect", GameFunctions::luaGameHasEffect); - registerMethod(L, "Game", "getOfflinePlayer", GameFunctions::luaGameGetOfflinePlayer); - registerMethod(L, "Game", "getNormalizedPlayerName", GameFunctions::luaGameGetNormalizedPlayerName); - registerMethod(L, "Game", "getNormalizedGuildName", GameFunctions::luaGameGetNormalizedGuildName); - - registerMethod(L, "Game", "addInfluencedMonster", GameFunctions::luaGameAddInfluencedMonster); - registerMethod(L, "Game", "removeInfluencedMonster", GameFunctions::luaGameRemoveInfluencedMonster); - registerMethod(L, "Game", "getInfluencedMonsters", GameFunctions::luaGameGetInfluencedMonsters); - registerMethod(L, "Game", "makeFiendishMonster", GameFunctions::luaGameMakeFiendishMonster); - registerMethod(L, "Game", "removeFiendishMonster", GameFunctions::luaGameRemoveFiendishMonster); - registerMethod(L, "Game", "getFiendishMonsters", GameFunctions::luaGameGetFiendishMonsters); - registerMethod(L, "Game", "getBoostedBoss", GameFunctions::luaGameGetBoostedBoss); - - registerMethod(L, "Game", "getLadderIds", GameFunctions::luaGameGetLadderIds); - registerMethod(L, "Game", "getDummies", GameFunctions::luaGameGetDummies); - - registerMethod(L, "Game", "getTalkActions", GameFunctions::luaGameGetTalkActions); - registerMethod(L, "Game", "getEventCallbacks", GameFunctions::luaGameGetEventCallbacks); - - registerMethod(L, "Game", "registerAchievement", GameFunctions::luaGameRegisterAchievement); - registerMethod(L, "Game", "getAchievementInfoById", GameFunctions::luaGameGetAchievementInfoById); - registerMethod(L, "Game", "getAchievementInfoByName", GameFunctions::luaGameGetAchievementInfoByName); - registerMethod(L, "Game", "getSecretAchievements", GameFunctions::luaGameGetSecretAchievements); - registerMethod(L, "Game", "getPublicAchievements", GameFunctions::luaGameGetPublicAchievements); - registerMethod(L, "Game", "getAchievements", GameFunctions::luaGameGetAchievements); - } + static void init(lua_State* L); private: static int luaGameCreateMonsterType(lua_State* L); diff --git a/src/lua/functions/core/game/global_functions.cpp b/src/lua/functions/core/game/global_functions.cpp index 6befdc5eb67..6eb21910701 100644 --- a/src/lua/functions/core/game/global_functions.cpp +++ b/src/lua/functions/core/game/global_functions.cpp @@ -10,9 +10,11 @@ #include "lua/functions/core/game/global_functions.hpp" #include "config/configmanager.hpp" +#include "creatures/creature.hpp" #include "creatures/combat/condition.hpp" #include "creatures/interactions/chat.hpp" #include "creatures/players/wheel/player_wheel.hpp" +#include "creatures/players/player.hpp" #include "game/game.hpp" #include "game/scheduling/dispatcher.hpp" #include "game/scheduling/save_manager.hpp" @@ -22,6 +24,7 @@ #include "lua/scripts/lua_environment.hpp" #include "lua/scripts/script_environment.hpp" #include "server/network/protocol/protocolstatus.hpp" +#include "lua/functions/lua_functions_loader.hpp" #include "creatures/players/player.hpp" void GlobalFunctions::init(lua_State* L) { @@ -54,30 +57,30 @@ void GlobalFunctions::init(lua_State* L) { lua_register(L, "sendGuildChannelMessage", GlobalFunctions::luaSendGuildChannelMessage); lua_register(L, "stopEvent", GlobalFunctions::luaStopEvent); - registerGlobalVariable(L, "INDEX_WHEREEVER", INDEX_WHEREEVER); - registerGlobalBoolean(L, "VIRTUAL_PARENT", true); - registerGlobalMethod(L, "isType", GlobalFunctions::luaIsType); - registerGlobalMethod(L, "rawgetmetatable", GlobalFunctions::luaRawGetMetatable); - registerGlobalMethod(L, "createTable", GlobalFunctions::luaCreateTable); - registerGlobalMethod(L, "systemTime", GlobalFunctions::luaSystemTime); - registerGlobalMethod(L, "getFormattedTimeRemaining", GlobalFunctions::luaGetFormattedTimeRemaining); - registerGlobalMethod(L, "reportError", GlobalFunctions::luaReportError); + Lua::registerGlobalVariable(L, "INDEX_WHEREEVER", INDEX_WHEREEVER); + Lua::registerGlobalBoolean(L, "VIRTUAL_PARENT", true); + Lua::registerGlobalMethod(L, "isType", GlobalFunctions::luaIsType); + Lua::registerGlobalMethod(L, "rawgetmetatable", GlobalFunctions::luaRawGetMetatable); + Lua::registerGlobalMethod(L, "createTable", GlobalFunctions::luaCreateTable); + Lua::registerGlobalMethod(L, "systemTime", GlobalFunctions::luaSystemTime); + Lua::registerGlobalMethod(L, "getFormattedTimeRemaining", GlobalFunctions::luaGetFormattedTimeRemaining); + Lua::registerGlobalMethod(L, "reportError", GlobalFunctions::luaReportError); } int GlobalFunctions::luaDoPlayerAddItem(lua_State* L) { // doPlayerAddItem(cid, itemid, count/subtype, canDropOnMap) // doPlayerAddItem(cid, itemid, count, canDropOnMap, subtype) - const auto &player = getPlayer(L, 1); + const auto &player = Lua::getPlayer(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const uint16_t itemId = getNumber(L, 2); - const auto count = getNumber(L, 3, 1); - const bool canDropOnMap = getBoolean(L, 4, true); - auto subType = getNumber(L, 5, 1); + const uint16_t itemId = Lua::getNumber(L, 2); + const auto count = Lua::getNumber(L, 3, 1); + const bool canDropOnMap = Lua::getBoolean(L, 4, true); + auto subType = Lua::getNumber(L, 5, 1); const ItemType &it = Item::items[itemId]; int32_t itemCount; @@ -105,8 +108,8 @@ int GlobalFunctions::luaDoPlayerAddItem(lua_State* L) { const auto &newItem = Item::CreateItem(itemId, stackCount); if (!newItem) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -116,66 +119,66 @@ int GlobalFunctions::luaDoPlayerAddItem(lua_State* L) { ReturnValue ret = g_game().internalPlayerAddItem(player, newItem, canDropOnMap); if (ret != RETURNVALUE_NOERROR) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } if (--itemCount == 0) { if (newItem->getParent()) { - const uint32_t uid = getScriptEnv()->addThing(newItem); + const uint32_t uid = Lua::getScriptEnv()->addThing(newItem); lua_pushnumber(L, uid); return 1; } else { // stackable item stacked with existing object, newItem will be released - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } } } - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } int GlobalFunctions::luaIsValidUID(lua_State* L) { // isValidUID(uid) - pushBoolean(L, getScriptEnv()->getThingByUID(getNumber(L, -1)) != nullptr); + Lua::pushBoolean(L, Lua::getScriptEnv()->getThingByUID(Lua::getNumber(L, -1)) != nullptr); return 1; } int GlobalFunctions::luaIsDepot(lua_State* L) { // isDepot(uid) - const auto &container = getScriptEnv()->getContainerByUID(getNumber(L, -1)); - pushBoolean(L, container && container->getDepotLocker()); + const auto &container = Lua::getScriptEnv()->getContainerByUID(Lua::getNumber(L, -1)); + Lua::pushBoolean(L, container && container->getDepotLocker()); return 1; } int GlobalFunctions::luaIsMovable(lua_State* L) { // isMovable(uid) // isMovable(uid) - const auto &thing = getScriptEnv()->getThingByUID(getNumber(L, -1)); - pushBoolean(L, thing && thing->isPushable()); + const auto &thing = Lua::getScriptEnv()->getThingByUID(Lua::getNumber(L, -1)); + Lua::pushBoolean(L, thing && thing->isPushable()); return 1; } int GlobalFunctions::luaDoAddContainerItem(lua_State* L) { // doAddContainerItem(uid, itemid, count/subtype) - const uint32_t uid = getNumber(L, 1); + const uint32_t uid = Lua::getNumber(L, 1); - ScriptEnvironment* env = getScriptEnv(); + ScriptEnvironment* env = Lua::getScriptEnv(); const auto &container = env->getContainerByUID(uid); if (!container) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CONTAINER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CONTAINER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const uint16_t itemId = getNumber(L, 2); + const uint16_t itemId = Lua::getNumber(L, 2); const ItemType &it = Item::items[itemId]; int32_t itemCount = 1; int32_t subType = 1; - const auto count = getNumber(L, 3, 1); + const auto count = Lua::getNumber(L, 3, 1); if (it.hasSubType()) { if (it.stackable) { @@ -191,8 +194,8 @@ int GlobalFunctions::luaDoAddContainerItem(lua_State* L) { const int32_t stackCount = std::min(it.stackSize, subType); const auto &newItem = Item::CreateItem(itemId, stackCount); if (!newItem) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -202,7 +205,7 @@ int GlobalFunctions::luaDoAddContainerItem(lua_State* L) { ReturnValue ret = g_game().internalAddItem(container, newItem); if (ret != RETURNVALUE_NOERROR) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } @@ -211,31 +214,31 @@ int GlobalFunctions::luaDoAddContainerItem(lua_State* L) { lua_pushnumber(L, env->addThing(newItem)); } else { // stackable item stacked with existing object, newItem will be released - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } } - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } int GlobalFunctions::luaGetDepotId(lua_State* L) { // getDepotId(uid) - const uint32_t uid = getNumber(L, -1); + const uint32_t uid = Lua::getNumber(L, -1); - const auto &container = getScriptEnv()->getContainerByUID(uid); + const auto &container = Lua::getScriptEnv()->getContainerByUID(uid); if (!container) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CONTAINER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CONTAINER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } const auto &depotLocker = container->getDepotLocker(); if (!depotLocker) { - reportErrorFunc("Depot not found"); - pushBoolean(L, false); + Lua::reportErrorFunc("Depot not found"); + Lua::pushBoolean(L, false); return 1; } @@ -267,10 +270,10 @@ int GlobalFunctions::luaGetWorldUpTime(lua_State* L) { int GlobalFunctions::luaCreateCombatArea(lua_State* L) { // createCombatArea( {area}, {extArea} ) - const ScriptEnvironment* env = getScriptEnv(); + const ScriptEnvironment* env = Lua::getScriptEnv(); if (env->getScriptId() != EVENT_ID_LOADING) { - reportErrorFunc("This function can only be used while loading the script."); - pushBoolean(L, false); + Lua::reportErrorFunc("This function can only be used while loading the script."); + Lua::pushBoolean(L, false); return 1; } @@ -281,9 +284,9 @@ int GlobalFunctions::luaCreateCombatArea(lua_State* L) { if (parameters >= 2) { uint32_t rowsExtArea; std::list listExtArea; - if (!isTable(L, 2) || !getArea(L, listExtArea, rowsExtArea)) { - reportErrorFunc("Invalid extended area table."); - pushBoolean(L, false); + if (!Lua::isTable(L, 2) || !getArea(L, listExtArea, rowsExtArea)) { + Lua::reportErrorFunc("Invalid extended area table."); + Lua::pushBoolean(L, false); return 1; } area->setupExtArea(listExtArea, rowsExtArea); @@ -291,9 +294,9 @@ int GlobalFunctions::luaCreateCombatArea(lua_State* L) { uint32_t rowsArea = 0; std::list listArea; - if (!isTable(L, 1) || !getArea(L, listArea, rowsArea)) { - reportErrorFunc("Invalid area table."); - pushBoolean(L, false); + if (!Lua::isTable(L, 1) || !getArea(L, listArea, rowsArea)) { + Lua::reportErrorFunc("Invalid area table."); + Lua::pushBoolean(L, false); return 1; } @@ -304,73 +307,73 @@ int GlobalFunctions::luaCreateCombatArea(lua_State* L) { int GlobalFunctions::luaDoAreaCombatHealth(lua_State* L) { // doAreaCombatHealth(cid, type, pos, area, min, max, effect[, origin = ORIGIN_SPELL]) - const auto &creature = getCreature(L, 1); - if (!creature && (!isNumber(L, 1) || getNumber(L, 1) != 0)) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + const auto &creature = Lua::getCreature(L, 1); + if (!creature && (!Lua::isNumber(L, 1) || Lua::getNumber(L, 1) != 0)) { + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const uint32_t areaId = getNumber(L, 4); + const uint32_t areaId = Lua::getNumber(L, 4); const auto &area = g_luaEnvironment().getAreaObject(areaId); if (area || areaId == 0) { - const CombatType_t combatType = getNumber(L, 2); + const CombatType_t combatType = Lua::getNumber(L, 2); CombatParams params; params.combatType = combatType; - params.impactEffect = getNumber(L, 7); + params.impactEffect = Lua::getNumber(L, 7); CombatDamage damage; - damage.origin = getNumber(L, 8, ORIGIN_SPELL); + damage.origin = Lua::getNumber(L, 8, ORIGIN_SPELL); damage.primary.type = combatType; - damage.primary.value = normal_random(getNumber(L, 6), getNumber(L, 5)); + damage.primary.value = normal_random(Lua::getNumber(L, 6), Lua::getNumber(L, 5)); - damage.instantSpellName = getString(L, 9); - damage.runeSpellName = getString(L, 10); + damage.instantSpellName = Lua::getString(L, 9); + damage.runeSpellName = Lua::getString(L, 10); if (creature) { if (const auto &player = creature->getPlayer()) { player->wheel()->getCombatDataSpell(damage); } } - Combat::doCombatHealth(creature, getPosition(L, 3), area, damage, params); - pushBoolean(L, true); + Combat::doCombatHealth(creature, Lua::getPosition(L, 3), area, damage, params); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_AREA_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_AREA_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } int GlobalFunctions::luaDoTargetCombatHealth(lua_State* L) { // doTargetCombatHealth(cid, target, type, min, max, effect[, origin = ORIGIN_SPELL]) - const auto &creature = getCreature(L, 1); - if (!creature && (!isNumber(L, 1) || getNumber(L, 1) != 0)) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + const auto &creature = Lua::getCreature(L, 1); + if (!creature && (!Lua::isNumber(L, 1) || Lua::getNumber(L, 1) != 0)) { + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &target = getCreature(L, 2); + const auto &target = Lua::getCreature(L, 2); if (!target) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const CombatType_t combatType = getNumber(L, 3); + const CombatType_t combatType = Lua::getNumber(L, 3); CombatParams params; params.combatType = combatType; - params.impactEffect = getNumber(L, 6); + params.impactEffect = Lua::getNumber(L, 6); CombatDamage damage; - damage.origin = getNumber(L, 7, ORIGIN_SPELL); + damage.origin = Lua::getNumber(L, 7, ORIGIN_SPELL); damage.primary.type = combatType; - damage.primary.value = normal_random(getNumber(L, 4), getNumber(L, 5)); + damage.primary.value = normal_random(Lua::getNumber(L, 4), Lua::getNumber(L, 5)); - damage.instantSpellName = getString(L, 9); - damage.runeSpellName = getString(L, 10); + damage.instantSpellName = Lua::getString(L, 9); + damage.runeSpellName = Lua::getString(L, 10); if (creature) { if (const auto &player = creature->getPlayer()) { player->wheel()->getCombatDataSpell(damage); @@ -383,77 +386,77 @@ int GlobalFunctions::luaDoTargetCombatHealth(lua_State* L) { } Combat::doCombatHealth(creature, target, damage, params); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int GlobalFunctions::luaDoAreaCombatMana(lua_State* L) { // doAreaCombatMana(cid, pos, area, min, max, effect[, origin = ORIGIN_SPELL]) - const auto &creature = getCreature(L, 1); - if (!creature && (!isNumber(L, 1) || getNumber(L, 1) != 0)) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + const auto &creature = Lua::getCreature(L, 1); + if (!creature && (!Lua::isNumber(L, 1) || Lua::getNumber(L, 1) != 0)) { + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const uint32_t areaId = getNumber(L, 3); + const uint32_t areaId = Lua::getNumber(L, 3); const auto &area = g_luaEnvironment().getAreaObject(areaId); if (area || areaId == 0) { CombatParams params; - params.impactEffect = getNumber(L, 6); + params.impactEffect = Lua::getNumber(L, 6); CombatDamage damage; - damage.origin = getNumber(L, 7, ORIGIN_SPELL); + damage.origin = Lua::getNumber(L, 7, ORIGIN_SPELL); damage.primary.type = COMBAT_MANADRAIN; - damage.primary.value = normal_random(getNumber(L, 4), getNumber(L, 5)); + damage.primary.value = normal_random(Lua::getNumber(L, 4), Lua::getNumber(L, 5)); - damage.instantSpellName = getString(L, 8); - damage.runeSpellName = getString(L, 9); + damage.instantSpellName = Lua::getString(L, 8); + damage.runeSpellName = Lua::getString(L, 9); if (creature) { if (const auto &player = creature->getPlayer()) { player->wheel()->getCombatDataSpell(damage); } } - const Position pos = getPosition(L, 2); + const Position pos = Lua::getPosition(L, 2); Combat::doCombatMana(creature, pos, area, damage, params); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_AREA_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_AREA_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } int GlobalFunctions::luaDoTargetCombatMana(lua_State* L) { // doTargetCombatMana(cid, target, min, max, effect[, origin = ORIGIN_SPELL) - const auto &creature = getCreature(L, 1); - if (!creature && (!isNumber(L, 1) || getNumber(L, 1) != 0)) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + const auto &creature = Lua::getCreature(L, 1); + if (!creature && (!Lua::isNumber(L, 1) || Lua::getNumber(L, 1) != 0)) { + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &target = getCreature(L, 2); + const auto &target = Lua::getCreature(L, 2); if (!target) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } CombatParams params; - const auto minval = getNumber(L, 3); - const auto maxval = getNumber(L, 4); + const auto minval = Lua::getNumber(L, 3); + const auto maxval = Lua::getNumber(L, 4); params.aggressive = minval + maxval < 0; - params.impactEffect = getNumber(L, 5); + params.impactEffect = Lua::getNumber(L, 5); CombatDamage damage; - damage.origin = getNumber(L, 6, ORIGIN_SPELL); + damage.origin = Lua::getNumber(L, 6, ORIGIN_SPELL); damage.primary.type = COMBAT_MANADRAIN; damage.primary.value = normal_random(minval, maxval); - damage.instantSpellName = getString(L, 7); - damage.runeSpellName = getString(L, 8); + damage.instantSpellName = Lua::getString(L, 7); + damage.runeSpellName = Lua::getString(L, 8); if (creature) { if (const auto &player = creature->getPlayer()) { player->wheel()->getCombatDataSpell(damage); @@ -461,142 +464,142 @@ int GlobalFunctions::luaDoTargetCombatMana(lua_State* L) { } Combat::doCombatMana(creature, target, damage, params); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int GlobalFunctions::luaDoAreaCombatCondition(lua_State* L) { // doAreaCombatCondition(cid, pos, area, condition, effect) - const auto &creature = getCreature(L, 1); - if (!creature && (!isNumber(L, 1) || getNumber(L, 1) != 0)) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + const auto &creature = Lua::getCreature(L, 1); + if (!creature && (!Lua::isNumber(L, 1) || Lua::getNumber(L, 1) != 0)) { + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &condition = getUserdataShared(L, 4); + const auto &condition = Lua::getUserdataShared(L, 4); if (!condition) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const uint32_t areaId = getNumber(L, 3); + const uint32_t areaId = Lua::getNumber(L, 3); const auto &area = g_luaEnvironment().getAreaObject(areaId); if (area || areaId == 0) { CombatParams params; - params.impactEffect = getNumber(L, 5); + params.impactEffect = Lua::getNumber(L, 5); params.conditionList.emplace_back(condition); - Combat::doCombatCondition(creature, getPosition(L, 2), area, params); - pushBoolean(L, true); + Combat::doCombatCondition(creature, Lua::getPosition(L, 2), area, params); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_AREA_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_AREA_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } int GlobalFunctions::luaDoTargetCombatCondition(lua_State* L) { // doTargetCombatCondition(cid, target, condition, effect) - const auto &creature = getCreature(L, 1); - if (!creature && (!isNumber(L, 1) || getNumber(L, 1) != 0)) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + const auto &creature = Lua::getCreature(L, 1); + if (!creature && (!Lua::isNumber(L, 1) || Lua::getNumber(L, 1) != 0)) { + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &target = getCreature(L, 2); + const auto &target = Lua::getCreature(L, 2); if (!target) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &condition = getUserdataShared(L, 3); + const auto &condition = Lua::getUserdataShared(L, 3); if (!condition) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } CombatParams params; - params.impactEffect = getNumber(L, 4); + params.impactEffect = Lua::getNumber(L, 4); params.conditionList.emplace_back(condition->clone()); Combat::doCombatCondition(creature, target, params); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int GlobalFunctions::luaDoAreaCombatDispel(lua_State* L) { // doAreaCombatDispel(cid, pos, area, type, effect) - const auto &creature = getCreature(L, 1); - if (!creature && (!isNumber(L, 1) || getNumber(L, 1) != 0)) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + const auto &creature = Lua::getCreature(L, 1); + if (!creature && (!Lua::isNumber(L, 1) || Lua::getNumber(L, 1) != 0)) { + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const uint32_t areaId = getNumber(L, 3); + const uint32_t areaId = Lua::getNumber(L, 3); const auto &area = g_luaEnvironment().getAreaObject(areaId); if (area || areaId == 0) { CombatParams params; - params.impactEffect = getNumber(L, 5); - params.dispelType = getNumber(L, 4); - Combat::doCombatDispel(creature, getPosition(L, 2), area, params); + params.impactEffect = Lua::getNumber(L, 5); + params.dispelType = Lua::getNumber(L, 4); + Combat::doCombatDispel(creature, Lua::getPosition(L, 2), area, params); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_AREA_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_AREA_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } int GlobalFunctions::luaDoTargetCombatDispel(lua_State* L) { // doTargetCombatDispel(cid, target, type, effect) - const auto &creature = getCreature(L, 1); - if (!creature && (!isNumber(L, 1) || getNumber(L, 1) != 0)) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + const auto &creature = Lua::getCreature(L, 1); + if (!creature && (!Lua::isNumber(L, 1) || Lua::getNumber(L, 1) != 0)) { + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &target = getCreature(L, 2); + const auto &target = Lua::getCreature(L, 2); if (!target) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } CombatParams params; - params.dispelType = getNumber(L, 3); - params.impactEffect = getNumber(L, 4); + params.dispelType = Lua::getNumber(L, 3); + params.impactEffect = Lua::getNumber(L, 4); Combat::doCombatDispel(creature, target, params); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int GlobalFunctions::luaDoChallengeCreature(lua_State* L) { // doChallengeCreature(cid, target, targetChangeCooldown) - const auto &creature = getCreature(L, 1); + const auto &creature = Lua::getCreature(L, 1); if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &target = getCreature(L, 2); + const auto &target = Lua::getCreature(L, 2); if (!target) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const int targetChangeCooldown = getNumber(L, 3, 6000); + const int targetChangeCooldown = Lua::getNumber(L, 3, 6000); // This function must be defined to take and handle the targetChangeCooldown. target->challengeCreature(creature, targetChangeCooldown); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -604,17 +607,17 @@ int GlobalFunctions::luaAddEvent(lua_State* L) { // addEvent(callback, delay, ...) lua_State* globalState = g_luaEnvironment().getLuaState(); if (!globalState) { - reportErrorFunc("No valid script interface!"); - pushBoolean(L, false); + Lua::reportErrorFunc("No valid script interface!"); + Lua::pushBoolean(L, false); return 1; } else if (globalState != L) { lua_xmove(L, globalState, lua_gettop(L)); } const int parameters = lua_gettop(globalState); - if (!isFunction(globalState, -parameters)) { // -parameters means the first parameter from left to right - reportErrorFunc("callback parameter should be a function."); - pushBoolean(L, false); + if (!Lua::isFunction(globalState, -parameters)) { // -parameters means the first parameter from left to right + Lua::reportErrorFunc("callback parameter should be a function."); + Lua::pushBoolean(L, false); return 1; } @@ -626,7 +629,7 @@ int GlobalFunctions::luaAddEvent(lua_State* L) { } lua_rawgeti(L, -1, 't'); - LuaData_t type = getNumber(L, -1); + LuaData_t type = Lua::getNumber(L, -1); if (type != LuaData_t::Unknown && type <= LuaData_t::Npc) { indexes.emplace_back(i, type); } @@ -660,7 +663,7 @@ int GlobalFunctions::luaAddEvent(lua_State* L) { warningString += " is unsafe"; } - reportErrorFunc(warningString); + Lua::reportErrorFunc(warningString); } if (g_configManager().getBoolean(CONVERT_UNSAFE_SCRIPTS)) { @@ -697,12 +700,12 @@ int GlobalFunctions::luaAddEvent(lua_State* L) { eventDesc.parameters.push_back(luaL_ref(globalState, LUA_REGISTRYINDEX)); } - const uint32_t delay = std::max(100, getNumber(globalState, 2)); + const uint32_t delay = std::max(100, Lua::getNumber(globalState, 2)); lua_pop(globalState, 1); eventDesc.function = luaL_ref(globalState, LUA_REGISTRYINDEX); - eventDesc.scriptId = getScriptEnv()->getScriptId(); - eventDesc.scriptName = getScriptEnv()->getScriptInterface()->getLoadingScriptName(); + eventDesc.scriptId = Lua::getScriptEnv()->getScriptId(); + eventDesc.scriptName = Lua::getScriptEnv()->getScriptInterface()->getLoadingScriptName(); auto &lastTimerEventId = g_luaEnvironment().lastEventTimerId; eventDesc.eventId = g_dispatcher().scheduleEvent( @@ -720,17 +723,17 @@ int GlobalFunctions::luaStopEvent(lua_State* L) { // stopEvent(eventid) lua_State* globalState = g_luaEnvironment().getLuaState(); if (!globalState) { - reportErrorFunc("No valid script interface!"); - pushBoolean(L, false); + Lua::reportErrorFunc("No valid script interface!"); + Lua::pushBoolean(L, false); return 1; } - const uint32_t eventId = getNumber(L, 1); + const uint32_t eventId = Lua::getNumber(L, 1); auto &timerEvents = g_luaEnvironment().timerEvents; const auto it = timerEvents.find(eventId); if (it == timerEvents.end()) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } @@ -744,14 +747,14 @@ int GlobalFunctions::luaStopEvent(lua_State* L) { luaL_unref(globalState, LUA_REGISTRYINDEX, parameter); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int GlobalFunctions::luaSaveServer(lua_State* L) { g_globalEvents().save(); g_saveManager().scheduleAll(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -762,27 +765,27 @@ int GlobalFunctions::luaCleanMap(lua_State* L) { int GlobalFunctions::luaDebugPrint(lua_State* L) { // debugPrint(text) - reportErrorFunc(getString(L, -1)); + Lua::reportErrorFunc(Lua::getString(L, -1)); return 0; } int GlobalFunctions::luaIsInWar(lua_State* L) { // isInWar(cid, target) - const auto &player = getPlayer(L, 1); + const auto &player = Lua::getPlayer(L, 1); if (!player) { - reportErrorFunc(fmt::format("{} - Player", getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND))); - pushBoolean(L, false); + Lua::reportErrorFunc(fmt::format("{} - Player", Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND))); + Lua::pushBoolean(L, false); return 1; } - const auto &targetPlayer = getPlayer(L, 2); + const auto &targetPlayer = Lua::getPlayer(L, 2); if (!targetPlayer) { - reportErrorFunc(fmt::format("{} - TargetPlayer", getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND))); - pushBoolean(L, false); + Lua::reportErrorFunc(fmt::format("{} - TargetPlayer", Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND))); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, player->isInWar(targetPlayer)); + Lua::pushBoolean(L, player->isInWar(targetPlayer)); return 1; } @@ -790,44 +793,44 @@ int GlobalFunctions::luaGetWaypointPositionByName(lua_State* L) { // getWaypointPositionByName(name) auto &waypoints = g_game().map.waypoints; - const auto it = waypoints.find(getString(L, -1)); + const auto it = waypoints.find(Lua::getString(L, -1)); if (it != waypoints.end()) { - pushPosition(L, it->second); + Lua::pushPosition(L, it->second); } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } int GlobalFunctions::luaSendChannelMessage(lua_State* L) { // sendChannelMessage(channelId, type, message) - const uint16_t channelId = getNumber(L, 1); + const uint16_t channelId = Lua::getNumber(L, 1); const auto &channel = g_chat().getChannelById(channelId); if (!channel) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - const SpeakClasses type = getNumber(L, 2); - const std::string message = getString(L, 3); + const SpeakClasses type = Lua::getNumber(L, 2); + const std::string message = Lua::getString(L, 3); channel->sendToAll(message, type); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int GlobalFunctions::luaSendGuildChannelMessage(lua_State* L) { // sendGuildChannelMessage(guildId, type, message) - const uint32_t guildId = getNumber(L, 1); + const uint32_t guildId = Lua::getNumber(L, 1); const auto &channel = g_chat().getGuildChannelById(guildId); if (!channel) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - const SpeakClasses type = getNumber(L, 2); - const std::string message = getString(L, 3); + const SpeakClasses type = Lua::getNumber(L, 2); + const std::string message = Lua::getString(L, 3); channel->sendToAll(message, type); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -837,34 +840,34 @@ int GlobalFunctions::luaIsType(lua_State* L) { lua_getmetatable(L, -2); lua_rawgeti(L, -2, 'p'); - const uint_fast8_t parentsB = getNumber(L, 1); + const uint_fast8_t parentsB = Lua::getNumber(L, 1); lua_rawgeti(L, -3, 'h'); - const size_t hashB = getNumber(L, 1); + const size_t hashB = Lua::getNumber(L, 1); lua_rawgeti(L, -3, 'p'); - const uint_fast8_t parentsA = getNumber(L, 1); + const uint_fast8_t parentsA = Lua::getNumber(L, 1); for (uint_fast8_t i = parentsA; i < parentsB; ++i) { lua_getfield(L, -3, "__index"); lua_replace(L, -4); } lua_rawgeti(L, -4, 'h'); - const size_t hashA = getNumber(L, 1); + const size_t hashA = Lua::getNumber(L, 1); - pushBoolean(L, hashA == hashB); + Lua::pushBoolean(L, hashA == hashB); return 1; } int GlobalFunctions::luaRawGetMetatable(lua_State* L) { // rawgetmetatable(metatableName) - luaL_getmetatable(L, getString(L, 1).c_str()); + luaL_getmetatable(L, Lua::getString(L, 1).c_str()); return 1; } int GlobalFunctions::luaCreateTable(lua_State* L) { // createTable(arrayLength, keyLength) - lua_createtable(L, getNumber(L, 1), getNumber(L, 2)); + lua_createtable(L, Lua::getNumber(L, 1), Lua::getNumber(L, 2)); return 1; } @@ -876,31 +879,31 @@ int GlobalFunctions::luaSystemTime(lua_State* L) { int GlobalFunctions::luaGetFormattedTimeRemaining(lua_State* L) { // getFormattedTimeRemaining(time) - const time_t time = getNumber(L, 1); + const time_t time = Lua::getNumber(L, 1); lua_pushstring(L, getFormattedTimeRemaining(time).c_str()); return 1; } int GlobalFunctions::luaReportError(lua_State* L) { // reportError(errorDescription) - const auto errorDescription = getString(L, 1); - reportError(__func__, errorDescription, true); + const auto errorDescription = Lua::getString(L, 1); + Lua::reportError(__func__, errorDescription, true); return 1; } bool GlobalFunctions::getArea(lua_State* L, std::list &list, uint32_t &rows) { lua_pushnil(L); for (rows = 0; lua_next(L, -2) != 0; ++rows) { - if (!isTable(L, -1)) { + if (!Lua::isTable(L, -1)) { return false; } lua_pushnil(L); while (lua_next(L, -2) != 0) { - if (!isNumber(L, -1)) { + if (!Lua::isNumber(L, -1)) { return false; } - list.push_back(getNumber(L, -1)); + list.push_back(Lua::getNumber(L, -1)); lua_pop(L, 1); } diff --git a/src/lua/functions/core/game/global_functions.hpp b/src/lua/functions/core/game/global_functions.hpp index 9d4ce14b1cf..7ed0c177c88 100644 --- a/src/lua/functions/core/game/global_functions.hpp +++ b/src/lua/functions/core/game/global_functions.hpp @@ -9,16 +9,8 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class GlobalFunctions final : LuaScriptInterface { +class GlobalFunctions { public: - explicit GlobalFunctions(lua_State* L) : - LuaScriptInterface("GlobalFunctions") { - init(L); - } - ~GlobalFunctions() override = default; - static void init(lua_State* L); private: diff --git a/src/lua/functions/core/game/lua_enums.cpp b/src/lua/functions/core/game/lua_enums.cpp index 5a03fa246fa..2136a3e5927 100644 --- a/src/lua/functions/core/game/lua_enums.cpp +++ b/src/lua/functions/core/game/lua_enums.cpp @@ -15,6 +15,7 @@ #include "enums/item_attribute.hpp" #include "game/functions/game_reload.hpp" #include "io/io_bosstiary.hpp" +#include "lua/functions/lua_functions_loader.hpp" constexpr const char* soundNamespace = "SOUND_EFFECT_TYPE_"; @@ -22,7 +23,7 @@ constexpr const char* soundNamespace = "SOUND_EFFECT_TYPE_"; { \ auto number = magic_enum::enum_integer(enumClassType); \ auto name = magic_enum::enum_name(enumClassType).data(); \ - registerGlobalVariable(luaState, name, number); \ + Lua::registerGlobalVariable(luaState, name, number); \ } \ void(0) @@ -30,15 +31,15 @@ constexpr const char* soundNamespace = "SOUND_EFFECT_TYPE_"; { \ auto number = magic_enum::enum_integer(enumClassType); \ auto name = std::string(luaNamespace) + magic_enum::enum_name(enumClassType).data(); \ - registerGlobalVariable(luaState, name, number); \ + Lua::registerGlobalVariable(luaState, name, number); \ } \ void(0) -#define registerEnum(L, value) \ - { \ - std::string enumName = #value; \ - registerGlobalVariable(L, enumName.substr(enumName.find_last_of(':') + 1), value); \ - } \ +#define registerEnum(L, value) \ + { \ + std::string enumName = #value; \ + Lua::registerGlobalVariable(L, enumName.substr(enumName.find_last_of(':') + 1), value); \ + } \ void(0) /** @@ -55,7 +56,7 @@ is "SILENCE", the registered full name will be "SOUND_EFFECT_TYPE_SILENCE". { \ std::string enumName = #enumValue; \ std::string enumNameWithNamespace = std::string(luaNamespace) + std::string(enumName.substr(enumName.find_last_of(':') + 1)); \ - registerGlobalVariable(L, enumNameWithNamespace, enumValue); \ + Lua::registerGlobalVariable(L, enumNameWithNamespace, enumValue); \ } \ void(0) @@ -792,7 +793,7 @@ void LuaEnums::initItemAttributeEnums(lua_State* L) { auto number = magic_enum::enum_integer(value); // Creation of the "ITEM_ATTRIBUTE_" namespace for lua scripts std::string enumName = "ITEM_ATTRIBUTE_" + std::string(magic_enum::enum_name(value)); - registerGlobalVariable(L, enumName, static_cast(number)); + Lua::registerGlobalVariable(L, enumName, static_cast(number)); } } diff --git a/src/lua/functions/core/game/lua_enums.hpp b/src/lua/functions/core/game/lua_enums.hpp index 44c79da7a31..465cc96afc3 100644 --- a/src/lua/functions/core/game/lua_enums.hpp +++ b/src/lua/functions/core/game/lua_enums.hpp @@ -9,16 +9,8 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class LuaEnums final : LuaScriptInterface { +class LuaEnums { public: - explicit LuaEnums(lua_State* L) : - LuaScriptInterface("LuaEnums") { - init(L); - } - ~LuaEnums() override = default; - static void init(lua_State* L); private: diff --git a/src/lua/functions/core/game/modal_window_functions.cpp b/src/lua/functions/core/game/modal_window_functions.cpp index 95994450424..9d288716137 100644 --- a/src/lua/functions/core/game/modal_window_functions.cpp +++ b/src/lua/functions/core/game/modal_window_functions.cpp @@ -11,22 +11,52 @@ #include "creatures/players/player.hpp" #include "game/modal_window/modal_window.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ModalWindowFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "ModalWindow", "", ModalWindowFunctions::luaModalWindowCreate); + Lua::registerMetaMethod(L, "ModalWindow", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "ModalWindow", "getId", ModalWindowFunctions::luaModalWindowGetId); + Lua::registerMethod(L, "ModalWindow", "getTitle", ModalWindowFunctions::luaModalWindowGetTitle); + Lua::registerMethod(L, "ModalWindow", "getMessage", ModalWindowFunctions::luaModalWindowGetMessage); + + Lua::registerMethod(L, "ModalWindow", "setTitle", ModalWindowFunctions::luaModalWindowSetTitle); + Lua::registerMethod(L, "ModalWindow", "setMessage", ModalWindowFunctions::luaModalWindowSetMessage); + + Lua::registerMethod(L, "ModalWindow", "getButtonCount", ModalWindowFunctions::luaModalWindowGetButtonCount); + Lua::registerMethod(L, "ModalWindow", "getChoiceCount", ModalWindowFunctions::luaModalWindowGetChoiceCount); + + Lua::registerMethod(L, "ModalWindow", "addButton", ModalWindowFunctions::luaModalWindowAddButton); + Lua::registerMethod(L, "ModalWindow", "addChoice", ModalWindowFunctions::luaModalWindowAddChoice); + + Lua::registerMethod(L, "ModalWindow", "getDefaultEnterButton", ModalWindowFunctions::luaModalWindowGetDefaultEnterButton); + Lua::registerMethod(L, "ModalWindow", "setDefaultEnterButton", ModalWindowFunctions::luaModalWindowSetDefaultEnterButton); + + Lua::registerMethod(L, "ModalWindow", "getDefaultEscapeButton", ModalWindowFunctions::luaModalWindowGetDefaultEscapeButton); + Lua::registerMethod(L, "ModalWindow", "setDefaultEscapeButton", ModalWindowFunctions::luaModalWindowSetDefaultEscapeButton); + + Lua::registerMethod(L, "ModalWindow", "hasPriority", ModalWindowFunctions::luaModalWindowHasPriority); + Lua::registerMethod(L, "ModalWindow", "setPriority", ModalWindowFunctions::luaModalWindowSetPriority); + + Lua::registerMethod(L, "ModalWindow", "sendToPlayer", ModalWindowFunctions::luaModalWindowSendToPlayer); +} // ModalWindow int ModalWindowFunctions::luaModalWindowCreate(lua_State* L) { // ModalWindow(id, title, message) - const std::string &message = getString(L, 4); - const std::string &title = getString(L, 3); - uint32_t id = getNumber(L, 2); + const std::string &message = Lua::getString(L, 4); + const std::string &title = Lua::getString(L, 3); + uint32_t id = Lua::getNumber(L, 2); - pushUserdata(L, std::make_shared(id, title, message)); - setMetatable(L, -1, "ModalWindow"); + Lua::pushUserdata(L, std::make_shared(id, title, message)); + Lua::setMetatable(L, -1, "ModalWindow"); return 1; } int ModalWindowFunctions::luaModalWindowGetId(lua_State* L) { // modalWindow:getId() - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { lua_pushnumber(L, window->id); } else { @@ -37,9 +67,9 @@ int ModalWindowFunctions::luaModalWindowGetId(lua_State* L) { int ModalWindowFunctions::luaModalWindowGetTitle(lua_State* L) { // modalWindow:getTitle() - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { - pushString(L, window->title); + Lua::pushString(L, window->title); } else { lua_pushnil(L); } @@ -48,9 +78,9 @@ int ModalWindowFunctions::luaModalWindowGetTitle(lua_State* L) { int ModalWindowFunctions::luaModalWindowGetMessage(lua_State* L) { // modalWindow:getMessage() - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { - pushString(L, window->message); + Lua::pushString(L, window->message); } else { lua_pushnil(L); } @@ -59,11 +89,11 @@ int ModalWindowFunctions::luaModalWindowGetMessage(lua_State* L) { int ModalWindowFunctions::luaModalWindowSetTitle(lua_State* L) { // modalWindow:setTitle(text) - const std::string &text = getString(L, 2); - const auto &window = getUserdataShared(L, 1); + const std::string &text = Lua::getString(L, 2); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { window->title = text; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -72,11 +102,11 @@ int ModalWindowFunctions::luaModalWindowSetTitle(lua_State* L) { int ModalWindowFunctions::luaModalWindowSetMessage(lua_State* L) { // modalWindow:setMessage(text) - const std::string &text = getString(L, 2); - const auto &window = getUserdataShared(L, 1); + const std::string &text = Lua::getString(L, 2); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { window->message = text; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -85,7 +115,7 @@ int ModalWindowFunctions::luaModalWindowSetMessage(lua_State* L) { int ModalWindowFunctions::luaModalWindowGetButtonCount(lua_State* L) { // modalWindow:getButtonCount() - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { lua_pushnumber(L, window->buttons.size()); } else { @@ -96,7 +126,7 @@ int ModalWindowFunctions::luaModalWindowGetButtonCount(lua_State* L) { int ModalWindowFunctions::luaModalWindowGetChoiceCount(lua_State* L) { // modalWindow:getChoiceCount() - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { lua_pushnumber(L, window->choices.size()); } else { @@ -107,12 +137,12 @@ int ModalWindowFunctions::luaModalWindowGetChoiceCount(lua_State* L) { int ModalWindowFunctions::luaModalWindowAddButton(lua_State* L) { // modalWindow:addButton(id, text) - const std::string &text = getString(L, 3); - uint8_t id = getNumber(L, 2); - const auto &window = getUserdataShared(L, 1); + const std::string &text = Lua::getString(L, 3); + uint8_t id = Lua::getNumber(L, 2); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { window->buttons.emplace_back(text, id); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -121,12 +151,12 @@ int ModalWindowFunctions::luaModalWindowAddButton(lua_State* L) { int ModalWindowFunctions::luaModalWindowAddChoice(lua_State* L) { // modalWindow:addChoice(id, text) - const std::string &text = getString(L, 3); - uint8_t id = getNumber(L, 2); - const auto &window = getUserdataShared(L, 1); + const std::string &text = Lua::getString(L, 3); + uint8_t id = Lua::getNumber(L, 2); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { window->choices.emplace_back(text, id); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -135,7 +165,7 @@ int ModalWindowFunctions::luaModalWindowAddChoice(lua_State* L) { int ModalWindowFunctions::luaModalWindowGetDefaultEnterButton(lua_State* L) { // modalWindow:getDefaultEnterButton() - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { lua_pushnumber(L, window->defaultEnterButton); } else { @@ -146,10 +176,10 @@ int ModalWindowFunctions::luaModalWindowGetDefaultEnterButton(lua_State* L) { int ModalWindowFunctions::luaModalWindowSetDefaultEnterButton(lua_State* L) { // modalWindow:setDefaultEnterButton(buttonId) - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { - window->defaultEnterButton = getNumber(L, 2); - pushBoolean(L, true); + window->defaultEnterButton = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -158,7 +188,7 @@ int ModalWindowFunctions::luaModalWindowSetDefaultEnterButton(lua_State* L) { int ModalWindowFunctions::luaModalWindowGetDefaultEscapeButton(lua_State* L) { // modalWindow:getDefaultEscapeButton() - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { lua_pushnumber(L, window->defaultEscapeButton); } else { @@ -169,10 +199,10 @@ int ModalWindowFunctions::luaModalWindowGetDefaultEscapeButton(lua_State* L) { int ModalWindowFunctions::luaModalWindowSetDefaultEscapeButton(lua_State* L) { // modalWindow:setDefaultEscapeButton(buttonId) - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { - window->defaultEscapeButton = getNumber(L, 2); - pushBoolean(L, true); + window->defaultEscapeButton = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -181,9 +211,9 @@ int ModalWindowFunctions::luaModalWindowSetDefaultEscapeButton(lua_State* L) { int ModalWindowFunctions::luaModalWindowHasPriority(lua_State* L) { // modalWindow:hasPriority() - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { - pushBoolean(L, window->priority); + Lua::pushBoolean(L, window->priority); } else { lua_pushnil(L); } @@ -192,10 +222,10 @@ int ModalWindowFunctions::luaModalWindowHasPriority(lua_State* L) { int ModalWindowFunctions::luaModalWindowSetPriority(lua_State* L) { // modalWindow:setPriority(priority) - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { - window->priority = getBoolean(L, 2); - pushBoolean(L, true); + window->priority = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -204,18 +234,18 @@ int ModalWindowFunctions::luaModalWindowSetPriority(lua_State* L) { int ModalWindowFunctions::luaModalWindowSendToPlayer(lua_State* L) { // modalWindow:sendToPlayer(player) - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const auto &window = getUserdataShared(L, 1); + const auto &window = Lua::getUserdataShared(L, 1); if (window) { if (!player->hasModalWindowOpen(window->id)) { player->sendModalWindow(*window); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } diff --git a/src/lua/functions/core/game/modal_window_functions.hpp b/src/lua/functions/core/game/modal_window_functions.hpp index 1e2242d8570..83867888d89 100644 --- a/src/lua/functions/core/game/modal_window_functions.hpp +++ b/src/lua/functions/core/game/modal_window_functions.hpp @@ -9,44 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class ModalWindowFunctions final : LuaScriptInterface { +class ModalWindowFunctions { public: - explicit ModalWindowFunctions(lua_State* L) : - LuaScriptInterface("ModalWindowFunctions") { - init(L); - } - ~ModalWindowFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "ModalWindow", "", ModalWindowFunctions::luaModalWindowCreate); - registerMetaMethod(L, "ModalWindow", "__eq", ModalWindowFunctions::luaUserdataCompare); - - registerMethod(L, "ModalWindow", "getId", ModalWindowFunctions::luaModalWindowGetId); - registerMethod(L, "ModalWindow", "getTitle", ModalWindowFunctions::luaModalWindowGetTitle); - registerMethod(L, "ModalWindow", "getMessage", ModalWindowFunctions::luaModalWindowGetMessage); - - registerMethod(L, "ModalWindow", "setTitle", ModalWindowFunctions::luaModalWindowSetTitle); - registerMethod(L, "ModalWindow", "setMessage", ModalWindowFunctions::luaModalWindowSetMessage); - - registerMethod(L, "ModalWindow", "getButtonCount", ModalWindowFunctions::luaModalWindowGetButtonCount); - registerMethod(L, "ModalWindow", "getChoiceCount", ModalWindowFunctions::luaModalWindowGetChoiceCount); - - registerMethod(L, "ModalWindow", "addButton", ModalWindowFunctions::luaModalWindowAddButton); - registerMethod(L, "ModalWindow", "addChoice", ModalWindowFunctions::luaModalWindowAddChoice); - - registerMethod(L, "ModalWindow", "getDefaultEnterButton", ModalWindowFunctions::luaModalWindowGetDefaultEnterButton); - registerMethod(L, "ModalWindow", "setDefaultEnterButton", ModalWindowFunctions::luaModalWindowSetDefaultEnterButton); - - registerMethod(L, "ModalWindow", "getDefaultEscapeButton", ModalWindowFunctions::luaModalWindowGetDefaultEscapeButton); - registerMethod(L, "ModalWindow", "setDefaultEscapeButton", ModalWindowFunctions::luaModalWindowSetDefaultEscapeButton); - - registerMethod(L, "ModalWindow", "hasPriority", ModalWindowFunctions::luaModalWindowHasPriority); - registerMethod(L, "ModalWindow", "setPriority", ModalWindowFunctions::luaModalWindowSetPriority); - - registerMethod(L, "ModalWindow", "sendToPlayer", ModalWindowFunctions::luaModalWindowSendToPlayer); - } + static void init(lua_State* L); private: static int luaModalWindowCreate(lua_State* L); diff --git a/src/lua/functions/core/game/zone_functions.cpp b/src/lua/functions/core/game/zone_functions.cpp index e53c777d675..43fe586997a 100644 --- a/src/lua/functions/core/game/zone_functions.cpp +++ b/src/lua/functions/core/game/zone_functions.cpp @@ -11,111 +11,141 @@ #include "game/zones/zone.hpp" #include "game/game.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ZoneFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Zone", "", ZoneFunctions::luaZoneCreate); + Lua::registerMetaMethod(L, "Zone", "__eq", ZoneFunctions::luaZoneCompare); + + Lua::registerMethod(L, "Zone", "getName", ZoneFunctions::luaZoneGetName); + Lua::registerMethod(L, "Zone", "addArea", ZoneFunctions::luaZoneAddArea); + Lua::registerMethod(L, "Zone", "subtractArea", ZoneFunctions::luaZoneSubtractArea); + Lua::registerMethod(L, "Zone", "getRemoveDestination", ZoneFunctions::luaZoneGetRemoveDestination); + Lua::registerMethod(L, "Zone", "setRemoveDestination", ZoneFunctions::luaZoneSetRemoveDestination); + Lua::registerMethod(L, "Zone", "getPositions", ZoneFunctions::luaZoneGetPositions); + Lua::registerMethod(L, "Zone", "getCreatures", ZoneFunctions::luaZoneGetCreatures); + Lua::registerMethod(L, "Zone", "getPlayers", ZoneFunctions::luaZoneGetPlayers); + Lua::registerMethod(L, "Zone", "getMonsters", ZoneFunctions::luaZoneGetMonsters); + Lua::registerMethod(L, "Zone", "getNpcs", ZoneFunctions::luaZoneGetNpcs); + Lua::registerMethod(L, "Zone", "getItems", ZoneFunctions::luaZoneGetItems); + + Lua::registerMethod(L, "Zone", "removePlayers", ZoneFunctions::luaZoneRemovePlayers); + Lua::registerMethod(L, "Zone", "removeMonsters", ZoneFunctions::luaZoneRemoveMonsters); + Lua::registerMethod(L, "Zone", "removeNpcs", ZoneFunctions::luaZoneRemoveNpcs); + Lua::registerMethod(L, "Zone", "refresh", ZoneFunctions::luaZoneRefresh); + + Lua::registerMethod(L, "Zone", "setMonsterVariant", ZoneFunctions::luaZoneSetMonsterVariant); + + // static methods + Lua::registerMethod(L, "Zone", "getByPosition", ZoneFunctions::luaZoneGetByPosition); + Lua::registerMethod(L, "Zone", "getByName", ZoneFunctions::luaZoneGetByName); + Lua::registerMethod(L, "Zone", "getAll", ZoneFunctions::luaZoneGetAll); +} // Zone int ZoneFunctions::luaZoneCreate(lua_State* L) { // Zone(name) - const auto name = getString(L, 2); + const auto name = Lua::getString(L, 2); auto zone = Zone::getZone(name); if (!zone) { zone = Zone::addZone(name); } - pushUserdata(L, zone); - setMetatable(L, -1, "Zone"); + Lua::pushUserdata(L, zone); + Lua::setMetatable(L, -1, "Zone"); return 1; } int ZoneFunctions::luaZoneCompare(lua_State* L) { - const auto &zone1 = getUserdataShared(L, 1); - const auto &zone2 = getUserdataShared(L, 2); + const auto &zone1 = Lua::getUserdataShared(L, 1); + const auto &zone2 = Lua::getUserdataShared(L, 2); if (!zone1) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } if (!zone2) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, zone1->getName() == zone2->getName()); + Lua::pushBoolean(L, zone1->getName() == zone2->getName()); return 1; } int ZoneFunctions::luaZoneGetName(lua_State* L) { // Zone:getName() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - pushString(L, zone->getName()); + Lua::pushString(L, zone->getName()); return 1; } int ZoneFunctions::luaZoneAddArea(lua_State* L) { // Zone:addArea(fromPos, toPos) - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto fromPos = getPosition(L, 2); - const auto toPos = getPosition(L, 3); + const auto fromPos = Lua::getPosition(L, 2); + const auto toPos = Lua::getPosition(L, 3); const auto area = Area(fromPos, toPos); zone->addArea(area); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int ZoneFunctions::luaZoneSubtractArea(lua_State* L) { // Zone:subtractArea(fromPos, toPos) - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto fromPos = getPosition(L, 2); - const auto toPos = getPosition(L, 3); + const auto fromPos = Lua::getPosition(L, 2); + const auto toPos = Lua::getPosition(L, 3); const auto area = Area(fromPos, toPos); zone->subtractArea(area); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int ZoneFunctions::luaZoneGetRemoveDestination(lua_State* L) { // Zone:getRemoveDestination() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); return 1; } - pushPosition(L, zone->getRemoveDestination()); + Lua::pushPosition(L, zone->getRemoveDestination()); return 1; } int ZoneFunctions::luaZoneSetRemoveDestination(lua_State* L) { // Zone:setRemoveDestination(pos) - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); return 1; } - const auto pos = getPosition(L, 2); + const auto pos = Lua::getPosition(L, 2); zone->setRemoveDestination(pos); return 1; } int ZoneFunctions::luaZoneGetPositions(lua_State* L) { // Zone:getPositions() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } const auto positions = zone->getPositions(); @@ -124,7 +154,7 @@ int ZoneFunctions::luaZoneGetPositions(lua_State* L) { int index = 0; for (auto pos : positions) { index++; - pushPosition(L, pos); + Lua::pushPosition(L, pos); lua_rawseti(L, -2, index); } return 1; @@ -132,10 +162,10 @@ int ZoneFunctions::luaZoneGetPositions(lua_State* L) { int ZoneFunctions::luaZoneGetCreatures(lua_State* L) { // Zone:getCreatures() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } const auto &creatures = zone->getCreatures(); @@ -144,8 +174,8 @@ int ZoneFunctions::luaZoneGetCreatures(lua_State* L) { int index = 0; for (const auto &creature : creatures) { index++; - pushUserdata(L, creature); - setCreatureMetatable(L, -1, creature); + Lua::pushUserdata(L, creature); + Lua::setCreatureMetatable(L, -1, creature); lua_rawseti(L, -2, index); } return 1; @@ -153,10 +183,10 @@ int ZoneFunctions::luaZoneGetCreatures(lua_State* L) { int ZoneFunctions::luaZoneGetPlayers(lua_State* L) { // Zone:getPlayers() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } const auto &players = zone->getPlayers(); @@ -165,8 +195,8 @@ int ZoneFunctions::luaZoneGetPlayers(lua_State* L) { int index = 0; for (const auto &player : players) { index++; - pushUserdata(L, player); - setMetatable(L, -1, "Player"); + Lua::pushUserdata(L, player); + Lua::setMetatable(L, -1, "Player"); lua_rawseti(L, -2, index); } return 1; @@ -174,10 +204,10 @@ int ZoneFunctions::luaZoneGetPlayers(lua_State* L) { int ZoneFunctions::luaZoneGetMonsters(lua_State* L) { // Zone:getMonsters() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } const auto &monsters = zone->getMonsters(); @@ -186,8 +216,8 @@ int ZoneFunctions::luaZoneGetMonsters(lua_State* L) { int index = 0; for (const auto &monster : monsters) { index++; - pushUserdata(L, monster); - setMetatable(L, -1, "Monster"); + Lua::pushUserdata(L, monster); + Lua::setMetatable(L, -1, "Monster"); lua_rawseti(L, -2, index); } return 1; @@ -195,10 +225,10 @@ int ZoneFunctions::luaZoneGetMonsters(lua_State* L) { int ZoneFunctions::luaZoneGetNpcs(lua_State* L) { // Zone:getNpcs() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } const auto &npcs = zone->getNpcs(); @@ -207,8 +237,8 @@ int ZoneFunctions::luaZoneGetNpcs(lua_State* L) { int index = 0; for (const auto &npc : npcs) { index++; - pushUserdata(L, npc); - setMetatable(L, -1, "Npc"); + Lua::pushUserdata(L, npc); + Lua::setMetatable(L, -1, "Npc"); lua_rawseti(L, -2, index); } return 1; @@ -216,10 +246,10 @@ int ZoneFunctions::luaZoneGetNpcs(lua_State* L) { int ZoneFunctions::luaZoneGetItems(lua_State* L) { // Zone:getItems() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } const auto &items = zone->getItems(); @@ -228,8 +258,8 @@ int ZoneFunctions::luaZoneGetItems(lua_State* L) { int index = 0; for (const auto &item : items) { index++; - pushUserdata(L, item); - setMetatable(L, -1, "Item"); + Lua::pushUserdata(L, item); + Lua::setMetatable(L, -1, "Item"); lua_rawseti(L, -2, index); } return 1; @@ -237,10 +267,10 @@ int ZoneFunctions::luaZoneGetItems(lua_State* L) { int ZoneFunctions::luaZoneRemovePlayers(lua_State* L) { // Zone:removePlayers() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -250,10 +280,10 @@ int ZoneFunctions::luaZoneRemovePlayers(lua_State* L) { int ZoneFunctions::luaZoneRemoveMonsters(lua_State* L) { // Zone:removeMonsters() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } zone->removeMonsters(); @@ -262,10 +292,10 @@ int ZoneFunctions::luaZoneRemoveMonsters(lua_State* L) { int ZoneFunctions::luaZoneRemoveNpcs(lua_State* L) { // Zone:removeNpcs() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } zone->removeNpcs(); @@ -274,38 +304,38 @@ int ZoneFunctions::luaZoneRemoveNpcs(lua_State* L) { int ZoneFunctions::luaZoneSetMonsterVariant(lua_State* L) { // Zone:setMonsterVariant(variant) - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto variant = getString(L, 2); + const auto variant = Lua::getString(L, 2); if (variant.empty()) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } zone->setMonsterVariant(variant); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int ZoneFunctions::luaZoneGetByName(lua_State* L) { // Zone.getByName(name) - const auto name = getString(L, 1); + const auto name = Lua::getString(L, 1); const auto &zone = Zone::getZone(name); if (!zone) { lua_pushnil(L); return 1; } - pushUserdata(L, zone); - setMetatable(L, -1, "Zone"); + Lua::pushUserdata(L, zone); + Lua::setMetatable(L, -1, "Zone"); return 1; } int ZoneFunctions::luaZoneGetByPosition(lua_State* L) { // Zone.getByPosition(pos) - const auto pos = getPosition(L, 1); + const auto pos = Lua::getPosition(L, 1); const auto &tile = g_game().map.getTile(pos); if (!tile) { lua_pushnil(L); @@ -316,8 +346,8 @@ int ZoneFunctions::luaZoneGetByPosition(lua_State* L) { lua_createtable(L, static_cast(zones.size()), 0); for (const auto &zone : zones) { index++; - pushUserdata(L, zone); - setMetatable(L, -1, "Zone"); + Lua::pushUserdata(L, zone); + Lua::setMetatable(L, -1, "Zone"); lua_rawseti(L, -2, index); } return 1; @@ -330,8 +360,8 @@ int ZoneFunctions::luaZoneGetAll(lua_State* L) { int index = 0; for (const auto &zone : zones) { index++; - pushUserdata(L, zone); - setMetatable(L, -1, "Zone"); + Lua::pushUserdata(L, zone); + Lua::setMetatable(L, -1, "Zone"); lua_rawseti(L, -2, index); } return 1; @@ -339,9 +369,9 @@ int ZoneFunctions::luaZoneGetAll(lua_State* L) { int ZoneFunctions::luaZoneRefresh(lua_State* L) { // Zone:refresh() - const auto &zone = getUserdataShared(L, 1); + const auto &zone = Lua::getUserdataShared(L, 1); if (!zone) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND)); return 1; } zone->refresh(); diff --git a/src/lua/functions/core/game/zone_functions.hpp b/src/lua/functions/core/game/zone_functions.hpp index 0860595f0a8..cf1fa7cd73e 100644 --- a/src/lua/functions/core/game/zone_functions.hpp +++ b/src/lua/functions/core/game/zone_functions.hpp @@ -1,45 +1,10 @@ #pragma once -#include "lua/scripts/luascript.hpp" - class Zone; -class ZoneFunctions final : LuaScriptInterface { +class ZoneFunctions { public: - explicit ZoneFunctions(lua_State* L) : - LuaScriptInterface("ZoneFunctions") { - init(L); - } - ~ZoneFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Zone", "", ZoneFunctions::luaZoneCreate); - registerMetaMethod(L, "Zone", "__eq", ZoneFunctions::luaZoneCompare); - - registerMethod(L, "Zone", "getName", ZoneFunctions::luaZoneGetName); - registerMethod(L, "Zone", "addArea", ZoneFunctions::luaZoneAddArea); - registerMethod(L, "Zone", "subtractArea", ZoneFunctions::luaZoneSubtractArea); - registerMethod(L, "Zone", "getRemoveDestination", ZoneFunctions::luaZoneGetRemoveDestination); - registerMethod(L, "Zone", "setRemoveDestination", ZoneFunctions::luaZoneSetRemoveDestination); - registerMethod(L, "Zone", "getPositions", ZoneFunctions::luaZoneGetPositions); - registerMethod(L, "Zone", "getCreatures", ZoneFunctions::luaZoneGetCreatures); - registerMethod(L, "Zone", "getPlayers", ZoneFunctions::luaZoneGetPlayers); - registerMethod(L, "Zone", "getMonsters", ZoneFunctions::luaZoneGetMonsters); - registerMethod(L, "Zone", "getNpcs", ZoneFunctions::luaZoneGetNpcs); - registerMethod(L, "Zone", "getItems", ZoneFunctions::luaZoneGetItems); - - registerMethod(L, "Zone", "removePlayers", ZoneFunctions::luaZoneRemovePlayers); - registerMethod(L, "Zone", "removeMonsters", ZoneFunctions::luaZoneRemoveMonsters); - registerMethod(L, "Zone", "removeNpcs", ZoneFunctions::luaZoneRemoveNpcs); - registerMethod(L, "Zone", "refresh", ZoneFunctions::luaZoneRefresh); - - registerMethod(L, "Zone", "setMonsterVariant", ZoneFunctions::luaZoneSetMonsterVariant); - - // static methods - registerMethod(L, "Zone", "getByPosition", ZoneFunctions::luaZoneGetByPosition); - registerMethod(L, "Zone", "getByName", ZoneFunctions::luaZoneGetByName); - registerMethod(L, "Zone", "getAll", ZoneFunctions::luaZoneGetAll); - } + static void init(lua_State* L); private: static int luaZoneCreate(lua_State* L); diff --git a/src/lua/functions/core/libs/bit_functions.cpp b/src/lua/functions/core/libs/bit_functions.cpp deleted file mode 100644 index 94e5f709b5d..00000000000 --- a/src/lua/functions/core/libs/bit_functions.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Canary - A free and open-source MMORPG server emulator - * Copyright (©) 2019-2024 OpenTibiaBR - * Repository: https://github.com/opentibiabr/canary - * License: https://github.com/opentibiabr/canary/blob/main/LICENSE - * Contributors: https://github.com/opentibiabr/canary/graphs/contributors - * Website: https://docs.opentibiabr.com/ - */ - -#include "lua/functions/core/libs/bit_functions.hpp" - -#ifndef LUAJIT_VERSION -int GlobalFunctions::luaBitNot(lua_State* L) { - int32_t number = getNumber(L, -1); - lua_pushnumber(L, ~number); - return 1; -} - - #define MULTIOP(name, op) \ - int GlobalFunctions::luaBit##name(lua_State* L) \ { \ - int n = lua_gettop(L); \ - uint32_t w = getNumber(L, -1); \ - for (int i = 1; i < n; ++i) \ - w op getNumber(L, i); \ - lua_pushnumber(L, w); \ - return 1; \ - } - -MULTIOP(And, &=) -MULTIOP(Or, |=) -MULTIOP(Xor, ^=) - - #define SHIFTOP(name, op) \ - int GlobalFunctions::luaBit##name(lua_State* L) \ { \ - uint32_t n1 = getNumber(L, 1), n2 = getNumber(L, 2); \ - lua_pushnumber(L, (n1 op n2)); \ - return 1; \ - } - -SHIFTOP(LeftShift, <<) -SHIFTOP(RightShift, >>) -#endif diff --git a/src/lua/functions/core/libs/bit_functions.hpp b/src/lua/functions/core/libs/bit_functions.hpp deleted file mode 100644 index 3d117e74b55..00000000000 --- a/src/lua/functions/core/libs/bit_functions.hpp +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Canary - A free and open-source MMORPG server emulator - * Copyright (©) 2019-2024 OpenTibiaBR - * Repository: https://github.com/opentibiabr/canary - * License: https://github.com/opentibiabr/canary/blob/main/LICENSE - * Contributors: https://github.com/opentibiabr/canary/graphs/contributors - * Website: https://docs.opentibiabr.com/ - */ - -#pragma once - -#include "lua/scripts/luascript.hpp" - -class BitFunctions final : LuaScriptInterface { -public: - explicit BitFunctions(lua_State* L) : - LuaScriptInterface("BitFunctions") { - init(L); - } - ~BitFunctions() override = default; - - static void init(lua_State* L) { -#ifndef LUAJIT_VERSION - registerTable(L, "bit"); - registerMethod(L, "bit", "bnot", BitFunctions::luaBitNot); - registerMethod(L, "bit", "band", BitFunctions::luaBitAnd); - registerMethod(L, "bit", "bor", BitFunctions::luaBitOr); - registerMethod(L, "bit", "bxor", BitFunctions::luaBitXor); - registerMethod(L, "bit", "lshift", BitFunctions::luaBitLeftShift); - registerMethod(L, "bit", "rshift", BitFunctions::luaBitRightShift); -#endif - } - -private: -#ifndef LUAJIT_VERSION - static int luaBitAnd(lua_State* L); - static int luaBitLeftShift(lua_State* L); - static int luaBitNot(lua_State* L); - static int luaBitOr(lua_State* L); - static int luaBitRightShift(lua_State* L); - static int luaBitXor(lua_State* L); -#endif -}; diff --git a/src/lua/functions/core/libs/core_libs_functions.hpp b/src/lua/functions/core/libs/core_libs_functions.hpp index fdfa2d9de81..658b560c2f4 100644 --- a/src/lua/functions/core/libs/core_libs_functions.hpp +++ b/src/lua/functions/core/libs/core_libs_functions.hpp @@ -10,7 +10,6 @@ #pragma once #include "lua/scripts/luascript.hpp" -#include "lua/functions/core/libs/bit_functions.hpp" #include "lua/functions/core/libs/db_functions.hpp" #include "lua/functions/core/libs/result_functions.hpp" #include "lua/functions/core/libs/logger_functions.hpp" @@ -26,7 +25,6 @@ class CoreLibsFunctions final : LuaScriptInterface { ~CoreLibsFunctions() override = default; static void init(lua_State* L) { - BitFunctions::init(L); DBFunctions::init(L); ResultFunctions::init(L); LoggerFunctions::init(L); diff --git a/src/lua/functions/core/libs/db_functions.cpp b/src/lua/functions/core/libs/db_functions.cpp index 858b8faee6d..6b56c5cd148 100644 --- a/src/lua/functions/core/libs/db_functions.cpp +++ b/src/lua/functions/core/libs/db_functions.cpp @@ -12,9 +12,22 @@ #include "database/databasemanager.hpp" #include "database/databasetasks.hpp" #include "lua/scripts/lua_environment.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void DBFunctions::init(lua_State* L) { + Lua::registerTable(L, "db"); + Lua::registerMethod(L, "db", "query", DBFunctions::luaDatabaseExecute); + Lua::registerMethod(L, "db", "asyncQuery", DBFunctions::luaDatabaseAsyncExecute); + Lua::registerMethod(L, "db", "storeQuery", DBFunctions::luaDatabaseStoreQuery); + Lua::registerMethod(L, "db", "asyncStoreQuery", DBFunctions::luaDatabaseAsyncStoreQuery); + Lua::registerMethod(L, "db", "escapeString", DBFunctions::luaDatabaseEscapeString); + Lua::registerMethod(L, "db", "escapeBlob", DBFunctions::luaDatabaseEscapeBlob); + Lua::registerMethod(L, "db", "lastInsertId", DBFunctions::luaDatabaseLastInsertId); + Lua::registerMethod(L, "db", "tableExists", DBFunctions::luaDatabaseTableExists); +} int DBFunctions::luaDatabaseExecute(lua_State* L) { - pushBoolean(L, Database::getInstance().executeQuery(getString(L, -1))); + Lua::pushBoolean(L, Database::getInstance().executeQuery(Lua::getString(L, -1))); return 1; } @@ -22,36 +35,36 @@ int DBFunctions::luaDatabaseAsyncExecute(lua_State* L) { std::function callback; if (lua_gettop(L) > 1) { int32_t ref = luaL_ref(L, LUA_REGISTRYINDEX); - auto scriptId = getScriptEnv()->getScriptId(); + auto scriptId = Lua::getScriptEnv()->getScriptId(); callback = [ref, scriptId](const DBResult_ptr &, bool success) { lua_State* luaState = g_luaEnvironment().getLuaState(); if (!luaState) { return; } - if (!DBFunctions::reserveScriptEnv()) { + if (!Lua::reserveScriptEnv()) { luaL_unref(luaState, LUA_REGISTRYINDEX, ref); return; } lua_rawgeti(luaState, LUA_REGISTRYINDEX, ref); - pushBoolean(luaState, success); - const auto env = getScriptEnv(); + Lua::pushBoolean(luaState, success); + const auto env = Lua::getScriptEnv(); env->setScriptId(scriptId, &g_luaEnvironment()); g_luaEnvironment().callFunction(1); luaL_unref(luaState, LUA_REGISTRYINDEX, ref); }; } - g_databaseTasks().execute(getString(L, -1), callback); + g_databaseTasks().execute(Lua::getString(L, -1), callback); return 0; } int DBFunctions::luaDatabaseStoreQuery(lua_State* L) { - if (const DBResult_ptr &res = Database::getInstance().storeQuery(getString(L, -1))) { + if (const DBResult_ptr &res = Database::getInstance().storeQuery(Lua::getString(L, -1))) { lua_pushnumber(L, ScriptEnvironment::addResult(res)); } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } @@ -60,14 +73,14 @@ int DBFunctions::luaDatabaseAsyncStoreQuery(lua_State* L) { std::function callback; if (lua_gettop(L) > 1) { int32_t ref = luaL_ref(L, LUA_REGISTRYINDEX); - auto scriptId = getScriptEnv()->getScriptId(); + auto scriptId = Lua::getScriptEnv()->getScriptId(); callback = [ref, scriptId](const DBResult_ptr &result, bool) { lua_State* luaState = g_luaEnvironment().getLuaState(); if (!luaState) { return; } - if (!DBFunctions::reserveScriptEnv()) { + if (!Lua::reserveScriptEnv()) { luaL_unref(luaState, LUA_REGISTRYINDEX, ref); return; } @@ -76,27 +89,27 @@ int DBFunctions::luaDatabaseAsyncStoreQuery(lua_State* L) { if (result) { lua_pushnumber(luaState, ScriptEnvironment::addResult(result)); } else { - pushBoolean(luaState, false); + Lua::pushBoolean(luaState, false); } - const auto env = getScriptEnv(); + const auto env = Lua::getScriptEnv(); env->setScriptId(scriptId, &g_luaEnvironment()); g_luaEnvironment().callFunction(1); luaL_unref(luaState, LUA_REGISTRYINDEX, ref); }; } - g_databaseTasks().store(getString(L, -1), callback); + g_databaseTasks().store(Lua::getString(L, -1), callback); return 0; } int DBFunctions::luaDatabaseEscapeString(lua_State* L) { - pushString(L, Database::getInstance().escapeString(getString(L, -1))); + Lua::pushString(L, Database::getInstance().escapeString(Lua::getString(L, -1))); return 1; } int DBFunctions::luaDatabaseEscapeBlob(lua_State* L) { - const uint32_t length = getNumber(L, 2); - pushString(L, Database::getInstance().escapeBlob(getString(L, 1).c_str(), length)); + const uint32_t length = Lua::getNumber(L, 2); + Lua::pushString(L, Database::getInstance().escapeBlob(Lua::getString(L, 1).c_str(), length)); return 1; } @@ -106,6 +119,6 @@ int DBFunctions::luaDatabaseLastInsertId(lua_State* L) { } int DBFunctions::luaDatabaseTableExists(lua_State* L) { - pushBoolean(L, DatabaseManager::tableExists(getString(L, -1))); + Lua::pushBoolean(L, DatabaseManager::tableExists(Lua::getString(L, -1))); return 1; } diff --git a/src/lua/functions/core/libs/db_functions.hpp b/src/lua/functions/core/libs/db_functions.hpp index 6d95acdeae9..50181411add 100644 --- a/src/lua/functions/core/libs/db_functions.hpp +++ b/src/lua/functions/core/libs/db_functions.hpp @@ -9,27 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class DBFunctions final : LuaScriptInterface { +class DBFunctions { public: - explicit DBFunctions(lua_State* L) : - LuaScriptInterface("DBFunctions") { - init(L); - } - ~DBFunctions() override = default; - - static void init(lua_State* L) { - registerTable(L, "db"); - registerMethod(L, "db", "query", DBFunctions::luaDatabaseExecute); - registerMethod(L, "db", "asyncQuery", DBFunctions::luaDatabaseAsyncExecute); - registerMethod(L, "db", "storeQuery", DBFunctions::luaDatabaseStoreQuery); - registerMethod(L, "db", "asyncStoreQuery", DBFunctions::luaDatabaseAsyncStoreQuery); - registerMethod(L, "db", "escapeString", DBFunctions::luaDatabaseEscapeString); - registerMethod(L, "db", "escapeBlob", DBFunctions::luaDatabaseEscapeBlob); - registerMethod(L, "db", "lastInsertId", DBFunctions::luaDatabaseLastInsertId); - registerMethod(L, "db", "tableExists", DBFunctions::luaDatabaseTableExists); - } + static void init(lua_State* L); private: static int luaDatabaseAsyncExecute(lua_State* L); diff --git a/src/lua/functions/core/libs/kv_functions.cpp b/src/lua/functions/core/libs/kv_functions.cpp index d18a306944a..8061e2e9cd8 100644 --- a/src/lua/functions/core/libs/kv_functions.cpp +++ b/src/lua/functions/core/libs/kv_functions.cpp @@ -13,45 +13,62 @@ #include "kv/kv.hpp" #include "lua/scripts/lua_environment.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void KVFunctions::init(lua_State* L) { + Lua::registerTable(L, "kv"); + Lua::registerMethod(L, "kv", "scoped", KVFunctions::luaKVScoped); + Lua::registerMethod(L, "kv", "set", KVFunctions::luaKVSet); + Lua::registerMethod(L, "kv", "get", KVFunctions::luaKVGet); + Lua::registerMethod(L, "kv", "keys", KVFunctions::luaKVKeys); + Lua::registerMethod(L, "kv", "remove", KVFunctions::luaKVRemove); + + Lua::registerClass(L, "KV", ""); + Lua::registerMethod(L, "KV", "scoped", KVFunctions::luaKVScoped); + Lua::registerMethod(L, "KV", "set", KVFunctions::luaKVSet); + Lua::registerMethod(L, "KV", "get", KVFunctions::luaKVGet); + Lua::registerMethod(L, "KV", "keys", KVFunctions::luaKVKeys); + Lua::registerMethod(L, "KV", "remove", KVFunctions::luaKVRemove); +} int KVFunctions::luaKVScoped(lua_State* L) { // KV.scoped(key) | KV:scoped(key) - const auto key = getString(L, -1); + const auto key = Lua::getString(L, -1); - if (isUserdata(L, 1)) { - const auto &scopedKV = getUserdataShared(L, 1); + if (Lua::isUserdata(L, 1)) { + const auto &scopedKV = Lua::getUserdataShared(L, 1); const auto &newScope = scopedKV->scoped(key); - pushUserdata(L, newScope); - setMetatable(L, -1, "KV"); + Lua::pushUserdata(L, newScope); + Lua::setMetatable(L, -1, "KV"); return 1; } const auto &scopedKV = g_kv().scoped(key); - pushUserdata(L, scopedKV); - setMetatable(L, -1, "KV"); + Lua::pushUserdata(L, scopedKV); + Lua::setMetatable(L, -1, "KV"); return 1; } int KVFunctions::luaKVSet(lua_State* L) { // KV.set(key, value) | scopedKV:set(key, value) - const auto key = getString(L, -2); + const auto key = Lua::getString(L, -2); const auto &valueWrapper = getValueWrapper(L); if (!valueWrapper) { g_logger().warn("[{}] invalid param type", __FUNCTION__); - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - if (isUserdata(L, 1)) { - const auto &scopedKV = getUserdataShared(L, 1); + if (Lua::isUserdata(L, 1)) { + const auto &scopedKV = Lua::getUserdataShared(L, 1); scopedKV->set(key, valueWrapper.value()); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } g_kv().set(key, valueWrapper.value()); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -59,13 +76,13 @@ int KVFunctions::luaKVGet(lua_State* L) { // KV.get(key[, forceLoad = false]) | scopedKV:get(key[, forceLoad = false]) std::optional valueWrapper; bool forceLoad = false; - auto key = getString(L, -1); - if (isBoolean(L, -1)) { - forceLoad = getBoolean(L, -1); - key = getString(L, -2); + auto key = Lua::getString(L, -1); + if (Lua::isBoolean(L, -1)) { + forceLoad = Lua::getBoolean(L, -1); + key = Lua::getString(L, -2); } - if (isUserdata(L, 1)) { - const auto &scopedKV = getUserdataShared(L, 1); + if (Lua::isUserdata(L, 1)) { + const auto &scopedKV = Lua::getUserdataShared(L, 1); valueWrapper = scopedKV->get(key, forceLoad); } else { valueWrapper = g_kv().get(key, forceLoad); @@ -81,9 +98,9 @@ int KVFunctions::luaKVGet(lua_State* L) { int KVFunctions::luaKVRemove(lua_State* L) { // KV.remove(key) | scopedKV:remove(key) - const auto key = getString(L, -1); - if (isUserdata(L, 1)) { - const auto &scopedKV = getUserdataShared(L, 1); + const auto key = Lua::getString(L, -1); + if (Lua::isUserdata(L, 1)) { + const auto &scopedKV = Lua::getUserdataShared(L, 1); scopedKV->remove(key); } else { g_kv().remove(key); @@ -97,12 +114,12 @@ int KVFunctions::luaKVKeys(lua_State* L) { std::unordered_set keys; std::string prefix; - if (isString(L, -1)) { - prefix = getString(L, -1); + if (Lua::isString(L, -1)) { + prefix = Lua::getString(L, -1); } - if (isUserdata(L, 1)) { - const auto &scopedKV = getUserdataShared(L, 1); + if (Lua::isUserdata(L, 1)) { + const auto &scopedKV = Lua::getUserdataShared(L, 1); keys = scopedKV->keys(); } else { keys = g_kv().keys(prefix); @@ -111,24 +128,24 @@ int KVFunctions::luaKVKeys(lua_State* L) { int index = 0; lua_createtable(L, static_cast(keys.size()), 0); for (const auto &key : keys) { - pushString(L, key); + Lua::pushString(L, key); lua_rawseti(L, -2, ++index); } return 1; } std::optional KVFunctions::getValueWrapper(lua_State* L) { - if (isBoolean(L, -1)) { - return ValueWrapper(getBoolean(L, -1)); + if (Lua::isBoolean(L, -1)) { + return ValueWrapper(Lua::getBoolean(L, -1)); } - if (isNumber(L, -1)) { - return ValueWrapper(getNumber(L, -1)); + if (Lua::isNumber(L, -1)) { + return ValueWrapper(Lua::getNumber(L, -1)); } - if (isString(L, -1)) { - return ValueWrapper(getString(L, -1)); + if (Lua::isString(L, -1)) { + return ValueWrapper(Lua::getString(L, -1)); } - if (isTable(L, -1) && lua_objlen(L, -1) > 0) { + if (Lua::isTable(L, -1) && lua_objlen(L, -1) > 0) { ArrayType array; for (int i = 1; i <= lua_objlen(L, -1); ++i) { lua_rawgeti(L, -1, i); @@ -143,7 +160,7 @@ std::optional KVFunctions::getValueWrapper(lua_State* L) { return ValueWrapper(array); } - if (isTable(L, -1)) { + if (Lua::isTable(L, -1)) { MapType map; lua_pushnil(L); while (lua_next(L, -2) != 0) { @@ -152,13 +169,13 @@ std::optional KVFunctions::getValueWrapper(lua_State* L) { g_logger().warn("[{}] invalid param type", __FUNCTION__); return std::nullopt; } - map[getString(L, -2)] = std::make_shared(value.value()); + map[Lua::getString(L, -2)] = std::make_shared(value.value()); lua_pop(L, 1); } return ValueWrapper(map); } - if (isNil(L, -1)) { + if (Lua::isNil(L, -1)) { return std::nullopt; } @@ -167,7 +184,7 @@ std::optional KVFunctions::getValueWrapper(lua_State* L) { } void KVFunctions::pushStringValue(lua_State* L, const StringType &value) { - pushString(L, value); + Lua::pushString(L, value); } void KVFunctions::pushIntValue(lua_State* L, const IntType &value) { @@ -201,7 +218,7 @@ void KVFunctions::pushValueWrapper(lua_State* L, const ValueWrapper &valueWrappe if constexpr (std::is_same_v) { pushStringValue(L, arg); } else if constexpr (std::is_same_v) { - pushBoolean(L, arg); + Lua::pushBoolean(L, arg); } else if constexpr (std::is_same_v) { pushIntValue(L, arg); } else if constexpr (std::is_same_v) { diff --git a/src/lua/functions/core/libs/kv_functions.hpp b/src/lua/functions/core/libs/kv_functions.hpp index 4ef47333f2f..63f9eb8850b 100644 --- a/src/lua/functions/core/libs/kv_functions.hpp +++ b/src/lua/functions/core/libs/kv_functions.hpp @@ -9,8 +9,6 @@ #pragma once -#include "lua/scripts/luascript.hpp" - class ValueWrapper; #ifndef USE_PRECOMPILED_HEADERS @@ -25,29 +23,9 @@ using MapType = phmap::flat_hash_map> struct lua_State; -class KVFunctions final : LuaScriptInterface { +class KVFunctions { public: - explicit KVFunctions(lua_State* L) : - LuaScriptInterface("KVFunctions") { - init(L); - } - ~KVFunctions() override = default; - - static void init(lua_State* L) { - registerTable(L, "kv"); - registerMethod(L, "kv", "scoped", KVFunctions::luaKVScoped); - registerMethod(L, "kv", "set", KVFunctions::luaKVSet); - registerMethod(L, "kv", "get", KVFunctions::luaKVGet); - registerMethod(L, "kv", "keys", KVFunctions::luaKVKeys); - registerMethod(L, "kv", "remove", KVFunctions::luaKVRemove); - - registerClass(L, "KV", ""); - registerMethod(L, "KV", "scoped", KVFunctions::luaKVScoped); - registerMethod(L, "KV", "set", KVFunctions::luaKVSet); - registerMethod(L, "KV", "get", KVFunctions::luaKVGet); - registerMethod(L, "KV", "keys", KVFunctions::luaKVKeys); - registerMethod(L, "KV", "remove", KVFunctions::luaKVRemove); - } + static void init(lua_State* L); private: static int luaKVScoped(lua_State* L); diff --git a/src/lua/functions/core/libs/logger_functions.cpp b/src/lua/functions/core/libs/logger_functions.cpp index 741d35fec9d..c54f534813a 100644 --- a/src/lua/functions/core/libs/logger_functions.cpp +++ b/src/lua/functions/core/libs/logger_functions.cpp @@ -8,27 +8,28 @@ */ #include "lua/functions/core/libs/logger_functions.hpp" +#include "lua/functions/lua_functions_loader.hpp" void LoggerFunctions::init(lua_State* L) { // Kept for compatibility purposes only, it's deprecated - registerTable(L, "Spdlog"); - registerMethod(L, "Spdlog", "info", LoggerFunctions::luaSpdlogInfo); - registerMethod(L, "Spdlog", "warn", LoggerFunctions::luaSpdlogWarn); - registerMethod(L, "Spdlog", "error", LoggerFunctions::luaSpdlogError); - registerMethod(L, "Spdlog", "debug", LoggerFunctions::luaSpdlogDebug); + Lua::registerTable(L, "Spdlog"); + Lua::registerMethod(L, "Spdlog", "info", LoggerFunctions::luaSpdlogInfo); + Lua::registerMethod(L, "Spdlog", "warn", LoggerFunctions::luaSpdlogWarn); + Lua::registerMethod(L, "Spdlog", "error", LoggerFunctions::luaSpdlogError); + Lua::registerMethod(L, "Spdlog", "debug", LoggerFunctions::luaSpdlogDebug); - registerTable(L, "logger"); - registerMethod(L, "logger", "info", LoggerFunctions::luaLoggerInfo); - registerMethod(L, "logger", "warn", LoggerFunctions::luaLoggerWarn); - registerMethod(L, "logger", "error", LoggerFunctions::luaLoggerError); - registerMethod(L, "logger", "debug", LoggerFunctions::luaLoggerDebug); - registerMethod(L, "logger", "trace", LoggerFunctions::luaLoggerTrace); + Lua::registerTable(L, "logger"); + Lua::registerMethod(L, "logger", "info", LoggerFunctions::luaLoggerInfo); + Lua::registerMethod(L, "logger", "warn", LoggerFunctions::luaLoggerWarn); + Lua::registerMethod(L, "logger", "error", LoggerFunctions::luaLoggerError); + Lua::registerMethod(L, "logger", "debug", LoggerFunctions::luaLoggerDebug); + Lua::registerMethod(L, "logger", "trace", LoggerFunctions::luaLoggerTrace); } int LoggerFunctions::luaSpdlogInfo(lua_State* L) { // Spdlog.info(text) - if (isString(L, 1)) { - g_logger().info(getString(L, 1)); + if (Lua::isString(L, 1)) { + g_logger().info(Lua::getString(L, 1)); } else { lua_pushnil(L); } @@ -37,8 +38,8 @@ int LoggerFunctions::luaSpdlogInfo(lua_State* L) { int LoggerFunctions::luaSpdlogWarn(lua_State* L) { // Spdlog.warn(text) - if (isString(L, 1)) { - g_logger().warn(getString(L, 1)); + if (Lua::isString(L, 1)) { + g_logger().warn(Lua::getString(L, 1)); } else { lua_pushnil(L); } @@ -47,8 +48,8 @@ int LoggerFunctions::luaSpdlogWarn(lua_State* L) { int LoggerFunctions::luaSpdlogError(lua_State* L) { // Spdlog.error(text) - if (isString(L, 1)) { - g_logger().error(getString(L, 1)); + if (Lua::isString(L, 1)) { + g_logger().error(Lua::getString(L, 1)); } else { lua_pushnil(L); } @@ -57,8 +58,8 @@ int LoggerFunctions::luaSpdlogError(lua_State* L) { int LoggerFunctions::luaSpdlogDebug(lua_State* L) { // Spdlog.debug(text) - if (isString(L, 1)) { - g_logger().debug(getString(L, 1)); + if (Lua::isString(L, 1)) { + g_logger().debug(Lua::getString(L, 1)); } else { lua_pushnil(L); } @@ -68,30 +69,30 @@ int LoggerFunctions::luaSpdlogDebug(lua_State* L) { // Logger int LoggerFunctions::luaLoggerInfo(lua_State* L) { // logger.info(text) - if (isString(L, 1)) { - g_logger().info(getFormatedLoggerMessage(L)); + if (Lua::isString(L, 1)) { + g_logger().info(Lua::getFormatedLoggerMessage(L)); } else { - reportErrorFunc("First parameter needs to be a string"); + Lua::reportErrorFunc("First parameter needs to be a string"); } return 1; } int LoggerFunctions::luaLoggerWarn(lua_State* L) { // logger.warn(text) - if (isString(L, 1)) { - g_logger().warn(getFormatedLoggerMessage(L)); + if (Lua::isString(L, 1)) { + g_logger().warn(Lua::getFormatedLoggerMessage(L)); } else { - reportErrorFunc("First parameter needs to be a string"); + Lua::reportErrorFunc("First parameter needs to be a string"); } return 1; } int LoggerFunctions::luaLoggerError(lua_State* L) { // logger.error(text) - if (isString(L, 1)) { - g_logger().error(getFormatedLoggerMessage(L)); + if (Lua::isString(L, 1)) { + g_logger().error(Lua::getFormatedLoggerMessage(L)); } else { - reportErrorFunc("First parameter needs to be a string"); + Lua::reportErrorFunc("First parameter needs to be a string"); } return 1; @@ -99,20 +100,20 @@ int LoggerFunctions::luaLoggerError(lua_State* L) { int LoggerFunctions::luaLoggerDebug(lua_State* L) { // logger.debug(text) - if (isString(L, 1)) { - g_logger().debug(getFormatedLoggerMessage(L)); + if (Lua::isString(L, 1)) { + g_logger().debug(Lua::getFormatedLoggerMessage(L)); } else { - reportErrorFunc("First parameter needs to be a string"); + Lua::reportErrorFunc("First parameter needs to be a string"); } return 1; } int LoggerFunctions::luaLoggerTrace(lua_State* L) { // logger.trace(text) - if (isString(L, 1)) { - g_logger().trace(getFormatedLoggerMessage(L)); + if (Lua::isString(L, 1)) { + g_logger().trace(Lua::getFormatedLoggerMessage(L)); } else { - reportErrorFunc("First parameter needs to be a string"); + Lua::reportErrorFunc("First parameter needs to be a string"); } return 1; } diff --git a/src/lua/functions/core/libs/logger_functions.hpp b/src/lua/functions/core/libs/logger_functions.hpp index fb3c265f21d..aa5b4d3f80c 100644 --- a/src/lua/functions/core/libs/logger_functions.hpp +++ b/src/lua/functions/core/libs/logger_functions.hpp @@ -9,16 +9,8 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class LoggerFunctions final : public LuaScriptInterface { +class LoggerFunctions { public: - explicit LoggerFunctions(lua_State* L) : - LuaScriptInterface("LoggerFunctions") { - init(L); - } - ~LoggerFunctions() override = default; - static void init(lua_State* L); private: diff --git a/src/lua/functions/core/libs/metrics_functions.cpp b/src/lua/functions/core/libs/metrics_functions.cpp index 84d81fd214e..919f39da07e 100644 --- a/src/lua/functions/core/libs/metrics_functions.cpp +++ b/src/lua/functions/core/libs/metrics_functions.cpp @@ -10,17 +10,18 @@ #include "lua/functions/core/libs/metrics_functions.hpp" #include "lib/metrics/metrics.hpp" +#include "lua/functions/lua_functions_loader.hpp" void MetricsFunctions::init(lua_State* L) { - registerTable(L, "metrics"); - registerMethod(L, "metrics", "addCounter", MetricsFunctions::luaMetricsAddCounter); + Lua::registerTable(L, "metrics"); + Lua::registerMethod(L, "metrics", "addCounter", MetricsFunctions::luaMetricsAddCounter); } // Metrics int MetricsFunctions::luaMetricsAddCounter(lua_State* L) { // metrics.addCounter(name, value, attributes) - const auto name = getString(L, 1); - const auto value = getNumber(L, 2); + const auto name = Lua::getString(L, 1); + const auto value = Lua::getNumber(L, 2); const auto attributes = getAttributes(L, 3); g_metrics().addCounter(name, value, attributes); return 1; @@ -28,10 +29,10 @@ int MetricsFunctions::luaMetricsAddCounter(lua_State* L) { std::map MetricsFunctions::getAttributes(lua_State* L, int32_t index) { std::map attributes; - if (isTable(L, index)) { + if (Lua::isTable(L, index)) { lua_pushnil(L); while (lua_next(L, index) != 0) { - attributes[getString(L, -2)] = getString(L, -1); + attributes[Lua::getString(L, -2)] = Lua::getString(L, -1); lua_pop(L, 1); } } diff --git a/src/lua/functions/core/libs/metrics_functions.hpp b/src/lua/functions/core/libs/metrics_functions.hpp index 4155646cb51..8b375244476 100644 --- a/src/lua/functions/core/libs/metrics_functions.hpp +++ b/src/lua/functions/core/libs/metrics_functions.hpp @@ -9,16 +9,8 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class MetricsFunctions final : public LuaScriptInterface { +class MetricsFunctions { public: - explicit MetricsFunctions(lua_State* L) : - LuaScriptInterface("MetricsFunctions") { - init(L); - } - ~MetricsFunctions() override = default; - static void init(lua_State* L); private: diff --git a/src/lua/functions/core/libs/result_functions.cpp b/src/lua/functions/core/libs/result_functions.cpp index 2c55645b3b8..eda7bbad332 100644 --- a/src/lua/functions/core/libs/result_functions.cpp +++ b/src/lua/functions/core/libs/result_functions.cpp @@ -8,57 +8,67 @@ */ #include "lua/functions/core/libs/result_functions.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ResultFunctions::init(lua_State* L) { + Lua::registerTable(L, "Result"); + Lua::registerMethod(L, "Result", "getNumber", ResultFunctions::luaResultGetNumber); + Lua::registerMethod(L, "Result", "getString", ResultFunctions::luaResultGetString); + Lua::registerMethod(L, "Result", "getStream", ResultFunctions::luaResultGetStream); + Lua::registerMethod(L, "Result", "next", ResultFunctions::luaResultNext); + Lua::registerMethod(L, "Result", "free", ResultFunctions::luaResultFree); +} int ResultFunctions::luaResultGetNumber(lua_State* L) { - const auto &res = ScriptEnvironment::getResultByID(getNumber(L, 1)); + const auto &res = ScriptEnvironment::getResultByID(Lua::getNumber(L, 1)); if (!res) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - const std::string &s = getString(L, 2); + const std::string &s = Lua::getString(L, 2); lua_pushnumber(L, res->getNumber(s)); return 1; } int ResultFunctions::luaResultGetString(lua_State* L) { - const auto &res = ScriptEnvironment::getResultByID(getNumber(L, 1)); + const auto &res = ScriptEnvironment::getResultByID(Lua::getNumber(L, 1)); if (!res) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - const std::string &s = getString(L, 2); - pushString(L, res->getString(s)); + const std::string &s = Lua::getString(L, 2); + Lua::pushString(L, res->getString(s)); return 1; } int ResultFunctions::luaResultGetStream(lua_State* L) { - const auto &res = ScriptEnvironment::getResultByID(getNumber(L, 1)); + const auto &res = ScriptEnvironment::getResultByID(Lua::getNumber(L, 1)); if (!res) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } unsigned long length; - const char* stream = res->getStream(getString(L, 2), length); + const char* stream = res->getStream(Lua::getString(L, 2), length); lua_pushlstring(L, stream, length); lua_pushnumber(L, length); return 2; } int ResultFunctions::luaResultNext(lua_State* L) { - const auto &res = ScriptEnvironment::getResultByID(getNumber(L, -1)); + const auto &res = ScriptEnvironment::getResultByID(Lua::getNumber(L, -1)); if (!res) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, res->next()); + Lua::pushBoolean(L, res->next()); return 1; } int ResultFunctions::luaResultFree(lua_State* L) { - pushBoolean(L, ScriptEnvironment::removeResult(getNumber(L, -1))); + Lua::pushBoolean(L, ScriptEnvironment::removeResult(Lua::getNumber(L, -1))); return 1; } diff --git a/src/lua/functions/core/libs/result_functions.hpp b/src/lua/functions/core/libs/result_functions.hpp index 7e7f74f1add..3b979da3c86 100644 --- a/src/lua/functions/core/libs/result_functions.hpp +++ b/src/lua/functions/core/libs/result_functions.hpp @@ -9,24 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class ResultFunctions final : LuaScriptInterface { +class ResultFunctions { public: - explicit ResultFunctions(lua_State* L) : - LuaScriptInterface("ResultFunctions") { - init(L); - } - ~ResultFunctions() override = default; - - static void init(lua_State* L) { - registerTable(L, "Result"); - registerMethod(L, "Result", "getNumber", ResultFunctions::luaResultGetNumber); - registerMethod(L, "Result", "getString", ResultFunctions::luaResultGetString); - registerMethod(L, "Result", "getStream", ResultFunctions::luaResultGetStream); - registerMethod(L, "Result", "next", ResultFunctions::luaResultNext); - registerMethod(L, "Result", "free", ResultFunctions::luaResultFree); - } + static void init(lua_State* L); private: static int luaResultFree(lua_State* L); diff --git a/src/lua/functions/core/network/network_message_functions.cpp b/src/lua/functions/core/network/network_message_functions.cpp index 18bb454ee45..bfd3b86897e 100644 --- a/src/lua/functions/core/network/network_message_functions.cpp +++ b/src/lua/functions/core/network/network_message_functions.cpp @@ -12,17 +12,48 @@ #include "server/network/protocol/protocolgame.hpp" #include "creatures/players/player.hpp" #include "server/network/protocol/protocolstatus.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void NetworkMessageFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "NetworkMessage", "", NetworkMessageFunctions::luaNetworkMessageCreate); + Lua::registerMetaMethod(L, "NetworkMessage", "__eq", Lua::luaUserdataCompare); + Lua::registerMethod(L, "NetworkMessage", "delete", Lua::luaGarbageCollection); + + Lua::registerMethod(L, "NetworkMessage", "getByte", NetworkMessageFunctions::luaNetworkMessageGetByte); + Lua::registerMethod(L, "NetworkMessage", "getU16", NetworkMessageFunctions::luaNetworkMessageGetU16); + Lua::registerMethod(L, "NetworkMessage", "getU32", NetworkMessageFunctions::luaNetworkMessageGetU32); + Lua::registerMethod(L, "NetworkMessage", "getU64", NetworkMessageFunctions::luaNetworkMessageGetU64); + Lua::registerMethod(L, "NetworkMessage", "getString", NetworkMessageFunctions::luaNetworkMessageGetString); + Lua::registerMethod(L, "NetworkMessage", "getPosition", NetworkMessageFunctions::luaNetworkMessageGetPosition); + + Lua::registerMethod(L, "NetworkMessage", "addByte", NetworkMessageFunctions::luaNetworkMessageAddByte); + Lua::registerMethod(L, "NetworkMessage", "addU16", NetworkMessageFunctions::luaNetworkMessageAddU16); + Lua::registerMethod(L, "NetworkMessage", "addU32", NetworkMessageFunctions::luaNetworkMessageAddU32); + Lua::registerMethod(L, "NetworkMessage", "addU64", NetworkMessageFunctions::luaNetworkMessageAddU64); + Lua::registerMethod(L, "NetworkMessage", "add8", NetworkMessageFunctions::luaNetworkMessageAdd8); + Lua::registerMethod(L, "NetworkMessage", "add16", NetworkMessageFunctions::luaNetworkMessageAdd16); + Lua::registerMethod(L, "NetworkMessage", "add32", NetworkMessageFunctions::luaNetworkMessageAdd32); + Lua::registerMethod(L, "NetworkMessage", "add64", NetworkMessageFunctions::luaNetworkMessageAdd64); + Lua::registerMethod(L, "NetworkMessage", "addString", NetworkMessageFunctions::luaNetworkMessageAddString); + Lua::registerMethod(L, "NetworkMessage", "addPosition", NetworkMessageFunctions::luaNetworkMessageAddPosition); + Lua::registerMethod(L, "NetworkMessage", "addDouble", NetworkMessageFunctions::luaNetworkMessageAddDouble); + Lua::registerMethod(L, "NetworkMessage", "addItem", NetworkMessageFunctions::luaNetworkMessageAddItem); + + Lua::registerMethod(L, "NetworkMessage", "reset", NetworkMessageFunctions::luaNetworkMessageReset); + Lua::registerMethod(L, "NetworkMessage", "skipBytes", NetworkMessageFunctions::luaNetworkMessageSkipBytes); + Lua::registerMethod(L, "NetworkMessage", "sendToPlayer", NetworkMessageFunctions::luaNetworkMessageSendToPlayer); +} int NetworkMessageFunctions::luaNetworkMessageCreate(lua_State* L) { // NetworkMessage() - pushUserdata(L, std::make_shared()); - setMetatable(L, -1, "NetworkMessage"); + Lua::pushUserdata(L, std::make_shared()); + Lua::setMetatable(L, -1, "NetworkMessage"); return 1; } int NetworkMessageFunctions::luaNetworkMessageGetByte(lua_State* L) { // networkMessage:getByte() - const auto &message = getUserdataShared(L, 1); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { lua_pushnumber(L, message->getByte()); } else { @@ -33,7 +64,7 @@ int NetworkMessageFunctions::luaNetworkMessageGetByte(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageGetU16(lua_State* L) { // networkMessage:getU16() - const auto &message = getUserdataShared(L, 1); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { lua_pushnumber(L, message->get()); } else { @@ -44,7 +75,7 @@ int NetworkMessageFunctions::luaNetworkMessageGetU16(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageGetU32(lua_State* L) { // networkMessage:getU32() - const auto &message = getUserdataShared(L, 1); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { lua_pushnumber(L, message->get()); } else { @@ -55,7 +86,7 @@ int NetworkMessageFunctions::luaNetworkMessageGetU32(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageGetU64(lua_State* L) { // networkMessage:getU64() - const auto &message = getUserdataShared(L, 1); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { lua_pushnumber(L, message->get()); } else { @@ -65,10 +96,10 @@ int NetworkMessageFunctions::luaNetworkMessageGetU64(lua_State* L) { } int NetworkMessageFunctions::luaNetworkMessageGetString(lua_State* L) { - // networkMessage:getString() - const auto &message = getUserdataShared(L, 1); + // networkMessage:Lua::getString() + const auto &message = Lua::getUserdataShared(L, 1); if (message) { - pushString(L, message->getString()); + Lua::pushString(L, message->getString()); } else { lua_pushnil(L); } @@ -76,10 +107,10 @@ int NetworkMessageFunctions::luaNetworkMessageGetString(lua_State* L) { } int NetworkMessageFunctions::luaNetworkMessageGetPosition(lua_State* L) { - // networkMessage:getPosition() - const auto &message = getUserdataShared(L, 1); + // networkMessage:Lua::getPosition() + const auto &message = Lua::getUserdataShared(L, 1); if (message) { - pushPosition(L, message->getPosition()); + Lua::pushPosition(L, message->getPosition()); } else { lua_pushnil(L); } @@ -88,11 +119,11 @@ int NetworkMessageFunctions::luaNetworkMessageGetPosition(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddByte(lua_State* L) { // networkMessage:addByte(number) - const uint8_t number = getNumber(L, 2); - const auto &message = getUserdataShared(L, 1); + const uint8_t number = Lua::getNumber(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->addByte(number); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -101,11 +132,11 @@ int NetworkMessageFunctions::luaNetworkMessageAddByte(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddU16(lua_State* L) { // networkMessage:addU16(number) - const uint16_t number = getNumber(L, 2); - const auto &message = getUserdataShared(L, 1); + const uint16_t number = Lua::getNumber(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->add(number); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -114,11 +145,11 @@ int NetworkMessageFunctions::luaNetworkMessageAddU16(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddU32(lua_State* L) { // networkMessage:addU32(number) - const uint32_t number = getNumber(L, 2); - const auto &message = getUserdataShared(L, 1); + const uint32_t number = Lua::getNumber(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->add(number); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -127,11 +158,11 @@ int NetworkMessageFunctions::luaNetworkMessageAddU32(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddU64(lua_State* L) { // networkMessage:addU64(number) - const uint64_t number = getNumber(L, 2); - const auto &message = getUserdataShared(L, 1); + const uint64_t number = Lua::getNumber(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->add(number); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -140,11 +171,11 @@ int NetworkMessageFunctions::luaNetworkMessageAddU64(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAdd8(lua_State* L) { // networkMessage:add8(number) - const auto number = getNumber(L, 2); - const auto &message = getUserdataShared(L, 1); + const auto number = Lua::getNumber(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->add(number); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -153,11 +184,11 @@ int NetworkMessageFunctions::luaNetworkMessageAdd8(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAdd16(lua_State* L) { // networkMessage:add16(number) - const auto number = getNumber(L, 2); - const auto &message = getUserdataShared(L, 1); + const auto number = Lua::getNumber(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->add(number); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -166,11 +197,11 @@ int NetworkMessageFunctions::luaNetworkMessageAdd16(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAdd32(lua_State* L) { // networkMessage:add32(number) - const auto number = getNumber(L, 2); - const auto &message = getUserdataShared(L, 1); + const auto number = Lua::getNumber(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->add(number); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -179,11 +210,11 @@ int NetworkMessageFunctions::luaNetworkMessageAdd32(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAdd64(lua_State* L) { // networkMessage:add64(number) - const auto number = getNumber(L, 2); - const auto &message = getUserdataShared(L, 1); + const auto number = Lua::getNumber(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->add(number); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -192,12 +223,12 @@ int NetworkMessageFunctions::luaNetworkMessageAdd64(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddString(lua_State* L) { // networkMessage:addString(string, function) - const std::string &string = getString(L, 2); - const std::string &function = getString(L, 3); - const auto &message = getUserdataShared(L, 1); + const std::string &string = Lua::getString(L, 2); + const std::string &function = Lua::getString(L, 3); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->addString(string, std::source_location::current(), function); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -206,11 +237,11 @@ int NetworkMessageFunctions::luaNetworkMessageAddString(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddPosition(lua_State* L) { // networkMessage:addPosition(position) - const Position &position = getPosition(L, 2); - const auto &message = getUserdataShared(L, 1); + const Position &position = Lua::getPosition(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->addPosition(position); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -219,11 +250,11 @@ int NetworkMessageFunctions::luaNetworkMessageAddPosition(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddDouble(lua_State* L) { // networkMessage:addDouble(number) - const double number = getNumber(L, 2); - const auto &message = getUserdataShared(L, 1); + const double number = Lua::getNumber(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->addDouble(number); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -232,24 +263,24 @@ int NetworkMessageFunctions::luaNetworkMessageAddDouble(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageAddItem(lua_State* L) { // networkMessage:addItem(item, player) - const auto &item = getUserdataShared(L, 2); + const auto &item = Lua::getUserdataShared(L, 2); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); lua_pushnil(L); return 1; } - const auto &player = getUserdataShared(L, 3); + const auto &player = Lua::getUserdataShared(L, 3); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); lua_pushnil(L); return 1; } - const auto &message = getUserdataShared(L, 1); + const auto &message = Lua::getUserdataShared(L, 1); if (message && player->client) { player->client->AddItem(*message, item); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -258,10 +289,10 @@ int NetworkMessageFunctions::luaNetworkMessageAddItem(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageReset(lua_State* L) { // networkMessage:reset() - const auto &message = getUserdataShared(L, 1); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->reset(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -270,11 +301,11 @@ int NetworkMessageFunctions::luaNetworkMessageReset(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageSkipBytes(lua_State* L) { // networkMessage:skipBytes(number) - const int16_t number = getNumber(L, 2); - const auto &message = getUserdataShared(L, 1); + const int16_t number = Lua::getNumber(L, 2); + const auto &message = Lua::getUserdataShared(L, 1); if (message) { message->skipBytes(number); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -283,19 +314,19 @@ int NetworkMessageFunctions::luaNetworkMessageSkipBytes(lua_State* L) { int NetworkMessageFunctions::luaNetworkMessageSendToPlayer(lua_State* L) { // networkMessage:sendToPlayer(player) - const auto &message = getUserdataShared(L, 1); + const auto &message = Lua::getUserdataShared(L, 1); if (!message) { lua_pushnil(L); return 1; } - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } player->sendNetworkMessage(*message); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } diff --git a/src/lua/functions/core/network/network_message_functions.hpp b/src/lua/functions/core/network/network_message_functions.hpp index 783e06aec57..906d649f7ea 100644 --- a/src/lua/functions/core/network/network_message_functions.hpp +++ b/src/lua/functions/core/network/network_message_functions.hpp @@ -9,45 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class NetworkMessageFunctions final : LuaScriptInterface { +class NetworkMessageFunctions { public: - explicit NetworkMessageFunctions(lua_State* L) : - LuaScriptInterface("NetworkMessageFunctions") { - init(L); - } - ~NetworkMessageFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "NetworkMessage", "", NetworkMessageFunctions::luaNetworkMessageCreate); - registerMetaMethod(L, "NetworkMessage", "__eq", NetworkMessageFunctions::luaUserdataCompare); - registerMethod(L, "NetworkMessage", "delete", luaGarbageCollection); - - registerMethod(L, "NetworkMessage", "getByte", NetworkMessageFunctions::luaNetworkMessageGetByte); - registerMethod(L, "NetworkMessage", "getU16", NetworkMessageFunctions::luaNetworkMessageGetU16); - registerMethod(L, "NetworkMessage", "getU32", NetworkMessageFunctions::luaNetworkMessageGetU32); - registerMethod(L, "NetworkMessage", "getU64", NetworkMessageFunctions::luaNetworkMessageGetU64); - registerMethod(L, "NetworkMessage", "getString", NetworkMessageFunctions::luaNetworkMessageGetString); - registerMethod(L, "NetworkMessage", "getPosition", NetworkMessageFunctions::luaNetworkMessageGetPosition); - - registerMethod(L, "NetworkMessage", "addByte", NetworkMessageFunctions::luaNetworkMessageAddByte); - registerMethod(L, "NetworkMessage", "addU16", NetworkMessageFunctions::luaNetworkMessageAddU16); - registerMethod(L, "NetworkMessage", "addU32", NetworkMessageFunctions::luaNetworkMessageAddU32); - registerMethod(L, "NetworkMessage", "addU64", NetworkMessageFunctions::luaNetworkMessageAddU64); - registerMethod(L, "NetworkMessage", "add8", NetworkMessageFunctions::luaNetworkMessageAdd8); - registerMethod(L, "NetworkMessage", "add16", NetworkMessageFunctions::luaNetworkMessageAdd16); - registerMethod(L, "NetworkMessage", "add32", NetworkMessageFunctions::luaNetworkMessageAdd32); - registerMethod(L, "NetworkMessage", "add64", NetworkMessageFunctions::luaNetworkMessageAdd64); - registerMethod(L, "NetworkMessage", "addString", NetworkMessageFunctions::luaNetworkMessageAddString); - registerMethod(L, "NetworkMessage", "addPosition", NetworkMessageFunctions::luaNetworkMessageAddPosition); - registerMethod(L, "NetworkMessage", "addDouble", NetworkMessageFunctions::luaNetworkMessageAddDouble); - registerMethod(L, "NetworkMessage", "addItem", NetworkMessageFunctions::luaNetworkMessageAddItem); - - registerMethod(L, "NetworkMessage", "reset", NetworkMessageFunctions::luaNetworkMessageReset); - registerMethod(L, "NetworkMessage", "skipBytes", NetworkMessageFunctions::luaNetworkMessageSkipBytes); - registerMethod(L, "NetworkMessage", "sendToPlayer", NetworkMessageFunctions::luaNetworkMessageSendToPlayer); - } + static void init(lua_State* L); private: static int luaNetworkMessageCreate(lua_State* L); diff --git a/src/lua/functions/core/network/webhook_functions.cpp b/src/lua/functions/core/network/webhook_functions.cpp index 23cba449515..7a55b656a74 100644 --- a/src/lua/functions/core/network/webhook_functions.cpp +++ b/src/lua/functions/core/network/webhook_functions.cpp @@ -10,14 +10,20 @@ #include "lua/functions/core/network/webhook_functions.hpp" #include "server/network/webhook/webhook.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void WebhookFunctions::init(lua_State* L) { + Lua::registerTable(L, "Webhook"); + Lua::registerMethod(L, "Webhook", "sendMessage", WebhookFunctions::luaWebhookSendMessage); +} int WebhookFunctions::luaWebhookSendMessage(lua_State* L) { // Webhook.sendMessage(title, message, color, url = "WEBHOOK_DISCORD_URL") | // Webhook.sendMessage(message, url = "WEBHOOK_DISCORD_URL") - const std::string title = getString(L, 1); - const std::string message = getString(L, 2); - const auto color = getNumber(L, 3, 0); - const std::string url = getString(L, -1); + const std::string title = Lua::getString(L, 1); + const std::string message = Lua::getString(L, 2); + const auto color = Lua::getNumber(L, 3, 0); + const std::string url = Lua::getString(L, -1); if (url == title) { g_webhook().sendMessage(title); } else if (url == message) { diff --git a/src/lua/functions/core/network/webhook_functions.hpp b/src/lua/functions/core/network/webhook_functions.hpp index 83ecb377c80..c7f24115520 100644 --- a/src/lua/functions/core/network/webhook_functions.hpp +++ b/src/lua/functions/core/network/webhook_functions.hpp @@ -9,20 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class WebhookFunctions final : LuaScriptInterface { +class WebhookFunctions { public: - explicit WebhookFunctions(lua_State* L) : - LuaScriptInterface("WebhookFunctions") { - init(L); - } - ~WebhookFunctions() override = default; - - static void init(lua_State* L) { - registerTable(L, "Webhook"); - registerMethod(L, "Webhook", "sendMessage", WebhookFunctions::luaWebhookSendMessage); - } + static void init(lua_State* L); private: static int luaWebhookSendMessage(lua_State* L); diff --git a/src/lua/functions/creatures/combat/combat_functions.cpp b/src/lua/functions/creatures/combat/combat_functions.cpp index e07bce7a7f0..776f447952e 100644 --- a/src/lua/functions/creatures/combat/combat_functions.cpp +++ b/src/lua/functions/creatures/combat/combat_functions.cpp @@ -9,78 +9,100 @@ #include "lua/functions/creatures/combat/combat_functions.hpp" +#include "creatures/creature.hpp" #include "creatures/combat/combat.hpp" #include "creatures/combat/condition.hpp" #include "game/game.hpp" #include "lua/global/lua_variant.hpp" #include "lua/scripts/lua_environment.hpp" #include "creatures/players/player.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void CombatFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Combat", "", CombatFunctions::luaCombatCreate); + Lua::registerMetaMethod(L, "Combat", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Combat", "setParameter", CombatFunctions::luaCombatSetParameter); + Lua::registerMethod(L, "Combat", "setFormula", CombatFunctions::luaCombatSetFormula); + + Lua::registerMethod(L, "Combat", "setArea", CombatFunctions::luaCombatSetArea); + Lua::registerMethod(L, "Combat", "addCondition", CombatFunctions::luaCombatSetCondition); + Lua::registerMethod(L, "Combat", "setCallback", CombatFunctions::luaCombatSetCallback); + Lua::registerMethod(L, "Combat", "setOrigin", CombatFunctions::luaCombatSetOrigin); + + Lua::registerMethod(L, "Combat", "execute", CombatFunctions::luaCombatExecute); + + ConditionFunctions::init(L); + SpellFunctions::init(L); + VariantFunctions::init(L); +} int CombatFunctions::luaCombatCreate(lua_State* L) { // Combat() - pushUserdata(L, g_luaEnvironment().createCombatObject(getScriptEnv()->getScriptInterface())); - setMetatable(L, -1, "Combat"); + auto combat = std::make_shared(); + Lua::pushUserdata(L, combat); + Lua::setMetatable(L, -1, "Combat"); return 1; } int CombatFunctions::luaCombatSetParameter(lua_State* L) { // combat:setParameter(key, value) - const auto &combat = getUserdataShared(L, 1); + const auto &combat = Lua::getUserdataShared(L, 1); if (!combat) { lua_pushnil(L); return 1; } - const CombatParam_t key = getNumber(L, 2); + const CombatParam_t key = Lua::getNumber(L, 2); uint32_t value; - if (isBoolean(L, 3)) { - value = getBoolean(L, 3) ? 1 : 0; + if (Lua::isBoolean(L, 3)) { + value = Lua::getBoolean(L, 3) ? 1 : 0; } else { - value = getNumber(L, 3); + value = Lua::getNumber(L, 3); } combat->setParam(key, value); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CombatFunctions::luaCombatSetFormula(lua_State* L) { // combat:setFormula(type, mina, minb, maxa, maxb) - const auto &combat = getUserdataShared(L, 1); + const auto &combat = Lua::getUserdataShared(L, 1); if (!combat) { lua_pushnil(L); return 1; } - const formulaType_t type = getNumber(L, 2); - const double mina = getNumber(L, 3); - const double minb = getNumber(L, 4); - const double maxa = getNumber(L, 5); - const double maxb = getNumber(L, 6); + const formulaType_t type = Lua::getNumber(L, 2); + const double mina = Lua::getNumber(L, 3); + const double minb = Lua::getNumber(L, 4); + const double maxa = Lua::getNumber(L, 5); + const double maxb = Lua::getNumber(L, 6); combat->setPlayerCombatValues(type, mina, minb, maxa, maxb); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CombatFunctions::luaCombatSetArea(lua_State* L) { // combat:setArea(area) - if (getScriptEnv()->getScriptId() != EVENT_ID_LOADING) { - reportErrorFunc("This function can only be used while loading the script."); + if (Lua::getScriptEnv()->getScriptId() != EVENT_ID_LOADING) { + Lua::reportErrorFunc("This function can only be used while loading the script."); lua_pushnil(L); return 1; } - const std::unique_ptr &area = g_luaEnvironment().getAreaObject(getNumber(L, 2)); + const std::unique_ptr &area = g_luaEnvironment().getAreaObject(Lua::getNumber(L, 2)); if (!area) { - reportErrorFunc(getErrorDesc(LUA_ERROR_AREA_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_AREA_NOT_FOUND)); lua_pushnil(L); return 1; } - const auto &combat = getUserdataShared(L, 1); + const auto &combat = Lua::getUserdataShared(L, 1); if (combat) { auto areaClone = area->clone(); combat->setArea(areaClone); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -89,11 +111,11 @@ int CombatFunctions::luaCombatSetArea(lua_State* L) { int CombatFunctions::luaCombatSetCondition(lua_State* L) { // combat:addCondition(condition) - const std::shared_ptr &condition = getUserdataShared(L, 2); - auto* combat = getUserdata(L, 1); + const std::shared_ptr &condition = Lua::getUserdataShared(L, 2); + auto* combat = Lua::getUserdata(L, 1); if (combat && condition) { combat->addCondition(condition->clone()); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -102,13 +124,13 @@ int CombatFunctions::luaCombatSetCondition(lua_State* L) { int CombatFunctions::luaCombatSetCallback(lua_State* L) { // combat:setCallback(key, function) - const auto &combat = getUserdataShared(L, 1); + const auto &combat = Lua::getUserdataShared(L, 1); if (!combat) { lua_pushnil(L); return 1; } - const CallBackParam_t key = getNumber(L, 2); + const CallBackParam_t key = Lua::getNumber(L, 2); if (!combat->setCallback(key)) { lua_pushnil(L); return 1; @@ -120,17 +142,17 @@ int CombatFunctions::luaCombatSetCallback(lua_State* L) { return 1; } - const std::string &function = getString(L, 3); - pushBoolean(L, callback->loadCallBack(getScriptEnv()->getScriptInterface(), function)); + const std::string &function = Lua::getString(L, 3); + Lua::pushBoolean(L, callback->loadCallBack(Lua::getScriptEnv()->getScriptInterface(), function)); return 1; } int CombatFunctions::luaCombatSetOrigin(lua_State* L) { // combat:setOrigin(origin) - const auto &combat = getUserdataShared(L, 1); + const auto &combat = Lua::getUserdataShared(L, 1); if (combat) { - combat->setOrigin(getNumber(L, 2)); - pushBoolean(L, true); + combat->setOrigin(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -139,27 +161,28 @@ int CombatFunctions::luaCombatSetOrigin(lua_State* L) { int CombatFunctions::luaCombatExecute(lua_State* L) { // combat:execute(creature, variant) - const auto &combat = getUserdataShared(L, 1); + const auto &combat = Lua::getUserdataShared(L, 1); if (!combat) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - if (isUserdata(L, 2)) { - const LuaData_t type = getUserdataType(L, 2); - if (type != LuaData_t::Player && type != LuaData_t::Monster && type != LuaData_t::Npc) { - pushBoolean(L, false); + if (Lua::isUserdata(L, 2)) { + using enum LuaData_t; + const LuaData_t type = Lua::getUserdataType(L, 2); + if (type != Player && type != Monster && type != Npc) { + Lua::pushBoolean(L, false); return 1; } } - const auto &creature = getCreature(L, 2); + const auto &creature = Lua::getCreature(L, 2); if (!creature) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - const LuaVariant &variant = getVariant(L, 3); + const LuaVariant &variant = Lua::getVariant(L, 3); combat->setInstantSpellName(variant.instantName); combat->setRuneSpellName(variant.runeName); bool result = true; @@ -167,7 +190,7 @@ int CombatFunctions::luaCombatExecute(lua_State* L) { case VARIANT_NUMBER: { const std::shared_ptr &target = g_game().getCreatureByID(variant.number); if (!target) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } @@ -197,7 +220,7 @@ int CombatFunctions::luaCombatExecute(lua_State* L) { case VARIANT_STRING: { const std::shared_ptr &target = g_game().getPlayerByName(variant.text); if (!target) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } @@ -206,8 +229,8 @@ int CombatFunctions::luaCombatExecute(lua_State* L) { } case VARIANT_NONE: { - reportErrorFunc(getErrorDesc(LUA_ERROR_VARIANT_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_VARIANT_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -216,6 +239,6 @@ int CombatFunctions::luaCombatExecute(lua_State* L) { } } - pushBoolean(L, result); + Lua::pushBoolean(L, result); return 1; } diff --git a/src/lua/functions/creatures/combat/combat_functions.hpp b/src/lua/functions/creatures/combat/combat_functions.hpp index a24b7c63bdd..9cf0923eb67 100644 --- a/src/lua/functions/creatures/combat/combat_functions.hpp +++ b/src/lua/functions/creatures/combat/combat_functions.hpp @@ -9,37 +9,13 @@ #pragma once -#include "lua/scripts/luascript.hpp" #include "lua/functions/creatures/combat/condition_functions.hpp" #include "lua/functions/creatures/combat/spell_functions.hpp" #include "lua/functions/creatures/combat/variant_functions.hpp" -class CombatFunctions final : LuaScriptInterface { +class CombatFunctions { public: - explicit CombatFunctions(lua_State* L) : - LuaScriptInterface("CombatFunctions") { - init(L); - } - ~CombatFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Combat", "", CombatFunctions::luaCombatCreate); - registerMetaMethod(L, "Combat", "__eq", CombatFunctions::luaUserdataCompare); - - registerMethod(L, "Combat", "setParameter", CombatFunctions::luaCombatSetParameter); - registerMethod(L, "Combat", "setFormula", CombatFunctions::luaCombatSetFormula); - - registerMethod(L, "Combat", "setArea", CombatFunctions::luaCombatSetArea); - registerMethod(L, "Combat", "addCondition", CombatFunctions::luaCombatSetCondition); - registerMethod(L, "Combat", "setCallback", CombatFunctions::luaCombatSetCallback); - registerMethod(L, "Combat", "setOrigin", CombatFunctions::luaCombatSetOrigin); - - registerMethod(L, "Combat", "execute", CombatFunctions::luaCombatExecute); - - ConditionFunctions::init(L); - SpellFunctions::init(L); - VariantFunctions::init(L); - } + static void init(lua_State* L); private: static int luaCombatCreate(lua_State* L); diff --git a/src/lua/functions/creatures/combat/condition_functions.cpp b/src/lua/functions/creatures/combat/condition_functions.cpp index 1362f0f1f31..0f2f9a56759 100644 --- a/src/lua/functions/creatures/combat/condition_functions.cpp +++ b/src/lua/functions/creatures/combat/condition_functions.cpp @@ -12,23 +12,48 @@ #include "creatures/combat/condition.hpp" #include "enums/player_icons.hpp" #include "game/game.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ConditionFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Condition", "", ConditionFunctions::luaConditionCreate); + Lua::registerMetaMethod(L, "Condition", "__eq", Lua::luaUserdataCompare); + Lua::registerMetaMethod(L, "Condition", "__gc", ConditionFunctions::luaConditionDelete); + Lua::registerMethod(L, "Condition", "delete", ConditionFunctions::luaConditionDelete); + + Lua::registerMethod(L, "Condition", "getId", ConditionFunctions::luaConditionGetId); + Lua::registerMethod(L, "Condition", "getSubId", ConditionFunctions::luaConditionGetSubId); + Lua::registerMethod(L, "Condition", "getType", ConditionFunctions::luaConditionGetType); + Lua::registerMethod(L, "Condition", "getIcons", ConditionFunctions::luaConditionGetIcons); + Lua::registerMethod(L, "Condition", "getEndTime", ConditionFunctions::luaConditionGetEndTime); + + Lua::registerMethod(L, "Condition", "clone", ConditionFunctions::luaConditionClone); + + Lua::registerMethod(L, "Condition", "getTicks", ConditionFunctions::luaConditionGetTicks); + Lua::registerMethod(L, "Condition", "setTicks", ConditionFunctions::luaConditionSetTicks); + + Lua::registerMethod(L, "Condition", "setParameter", ConditionFunctions::luaConditionSetParameter); + Lua::registerMethod(L, "Condition", "setFormula", ConditionFunctions::luaConditionSetFormula); + Lua::registerMethod(L, "Condition", "setOutfit", ConditionFunctions::luaConditionSetOutfit); + + Lua::registerMethod(L, "Condition", "addDamage", ConditionFunctions::luaConditionAddDamage); +} int ConditionFunctions::luaConditionCreate(lua_State* L) { // Condition(conditionType, conditionId = CONDITIONID_COMBAT, subid = 0, isPersistent = false) - const ConditionType_t conditionType = getNumber(L, 2); + const ConditionType_t conditionType = Lua::getNumber(L, 2); if (conditionType == CONDITION_NONE) { - reportErrorFunc("Invalid condition type"); + Lua::reportErrorFunc("Invalid condition type"); return 1; } - const auto conditionId = getNumber(L, 3, CONDITIONID_COMBAT); - const auto subId = getNumber(L, 4, 0); - const bool isPersistent = getBoolean(L, 5, false); + const auto conditionId = Lua::getNumber(L, 3, CONDITIONID_COMBAT); + const auto subId = Lua::getNumber(L, 4, 0); + const bool isPersistent = Lua::getBoolean(L, 5, false); const auto &condition = Condition::createCondition(conditionId, conditionType, 0, 0, false, subId, isPersistent); if (condition) { - pushUserdata(L, condition); - setMetatable(L, -1, "Condition"); + Lua::pushUserdata(L, condition); + Lua::setMetatable(L, -1, "Condition"); } else { lua_pushnil(L); } @@ -37,7 +62,7 @@ int ConditionFunctions::luaConditionCreate(lua_State* L) { int ConditionFunctions::luaConditionDelete(lua_State* L) { // condition:delete() - std::shared_ptr* conditionPtr = getRawUserDataShared(L, 1); + std::shared_ptr* conditionPtr = Lua::getRawUserDataShared(L, 1); if (conditionPtr && *conditionPtr) { conditionPtr->reset(); } @@ -46,7 +71,7 @@ int ConditionFunctions::luaConditionDelete(lua_State* L) { int ConditionFunctions::luaConditionGetId(lua_State* L) { // condition:getId() - const auto &condition = getUserdataShared(L, 1); + const auto &condition = Lua::getUserdataShared(L, 1); if (condition) { lua_pushnumber(L, condition->getId()); } else { @@ -57,7 +82,7 @@ int ConditionFunctions::luaConditionGetId(lua_State* L) { int ConditionFunctions::luaConditionGetSubId(lua_State* L) { // condition:getSubId() - const auto &condition = getUserdataShared(L, 1); + const auto &condition = Lua::getUserdataShared(L, 1); if (condition) { lua_pushnumber(L, condition->getSubId()); } else { @@ -68,7 +93,7 @@ int ConditionFunctions::luaConditionGetSubId(lua_State* L) { int ConditionFunctions::luaConditionGetType(lua_State* L) { // condition:getType() - const auto &condition = getUserdataShared(L, 1); + const auto &condition = Lua::getUserdataShared(L, 1); if (condition) { lua_pushnumber(L, condition->getType()); } else { @@ -79,7 +104,7 @@ int ConditionFunctions::luaConditionGetType(lua_State* L) { int ConditionFunctions::luaConditionGetIcons(lua_State* L) { // condition:getIcons() - const auto &condition = getUserdataShared(L, 1); + const auto &condition = Lua::getUserdataShared(L, 1); if (condition) { const auto icons = condition->getIcons(); lua_newtable(L); // Creates a new table on the Lua stack @@ -96,7 +121,7 @@ int ConditionFunctions::luaConditionGetIcons(lua_State* L) { int ConditionFunctions::luaConditionGetEndTime(lua_State* L) { // condition:getEndTime() - const auto &condition = getUserdataShared(L, 1); + const auto &condition = Lua::getUserdataShared(L, 1); if (condition) { lua_pushnumber(L, condition->getEndTime()); } else { @@ -107,10 +132,10 @@ int ConditionFunctions::luaConditionGetEndTime(lua_State* L) { int ConditionFunctions::luaConditionClone(lua_State* L) { // condition:clone() - const auto &condition = getUserdataShared(L, 1); + const auto &condition = Lua::getUserdataShared(L, 1); if (condition) { - pushUserdata(L, condition->clone()); - setMetatable(L, -1, "Condition"); + Lua::pushUserdata(L, condition->clone()); + Lua::setMetatable(L, -1, "Condition"); } else { lua_pushnil(L); } @@ -119,7 +144,7 @@ int ConditionFunctions::luaConditionClone(lua_State* L) { int ConditionFunctions::luaConditionGetTicks(lua_State* L) { // condition:getTicks() - const auto &condition = getUserdataShared(L, 1); + const auto &condition = Lua::getUserdataShared(L, 1); if (condition) { lua_pushnumber(L, condition->getTicks()); } else { @@ -130,11 +155,11 @@ int ConditionFunctions::luaConditionGetTicks(lua_State* L) { int ConditionFunctions::luaConditionSetTicks(lua_State* L) { // condition:setTicks(ticks) - const int32_t ticks = getNumber(L, 2); - const auto &condition = getUserdataShared(L, 1); + const int32_t ticks = Lua::getNumber(L, 2); + const auto &condition = Lua::getUserdataShared(L, 1); if (condition) { condition->setTicks(ticks); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -143,34 +168,34 @@ int ConditionFunctions::luaConditionSetTicks(lua_State* L) { int ConditionFunctions::luaConditionSetParameter(lua_State* L) { // condition:setParameter(key, value) - const auto &condition = getUserdataShared(L, 1); + const auto &condition = Lua::getUserdataShared(L, 1); if (!condition) { lua_pushnil(L); return 1; } - const ConditionParam_t key = getNumber(L, 2); + const ConditionParam_t key = Lua::getNumber(L, 2); int32_t value; - if (isBoolean(L, 3)) { - value = getBoolean(L, 3) ? 1 : 0; + if (Lua::isBoolean(L, 3)) { + value = Lua::getBoolean(L, 3) ? 1 : 0; } else { - value = getNumber(L, 3); + value = Lua::getNumber(L, 3); } condition->setParam(key, value); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int ConditionFunctions::luaConditionSetFormula(lua_State* L) { // condition:setFormula(mina, minb, maxa, maxb) - const double maxb = getNumber(L, 5); - const double maxa = getNumber(L, 4); - const double minb = getNumber(L, 3); - const double mina = getNumber(L, 2); - const std::shared_ptr &condition = getUserdataShared(L, 1)->dynamic_self_cast(); + const double maxb = Lua::getNumber(L, 5); + const double maxa = Lua::getNumber(L, 4); + const double minb = Lua::getNumber(L, 3); + const double mina = Lua::getNumber(L, 2); + const std::shared_ptr &condition = Lua::getUserdataShared(L, 1)->dynamic_self_cast(); if (condition) { condition->setFormulaVars(mina, minb, maxa, maxb); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -182,28 +207,28 @@ int ConditionFunctions::luaConditionSetOutfit(lua_State* L) { // condition:setOutfit(lookTypeEx, lookType, lookHead, lookBody, lookLegs, lookFeet[, // lookAddons[, lookMount[, lookMountHead[, lookMountBody[, lookMountLegs[, lookMountFeet[, lookFamiliarsType]]]]]]]) Outfit_t outfit; - if (isTable(L, 2)) { - outfit = getOutfit(L, 2); + if (Lua::isTable(L, 2)) { + outfit = Lua::getOutfit(L, 2); } else { - outfit.lookFamiliarsType = getNumber(L, 14, outfit.lookFamiliarsType); - outfit.lookMountFeet = getNumber(L, 13, outfit.lookMountFeet); - outfit.lookMountLegs = getNumber(L, 12, outfit.lookMountLegs); - outfit.lookMountBody = getNumber(L, 11, outfit.lookMountBody); - outfit.lookMountHead = getNumber(L, 10, outfit.lookMountHead); - outfit.lookMount = getNumber(L, 9, outfit.lookMount); - outfit.lookAddons = getNumber(L, 8, outfit.lookAddons); - outfit.lookFeet = getNumber(L, 7); - outfit.lookLegs = getNumber(L, 6); - outfit.lookBody = getNumber(L, 5); - outfit.lookHead = getNumber(L, 4); - outfit.lookType = getNumber(L, 3); - outfit.lookTypeEx = getNumber(L, 2); - } - - const std::shared_ptr &condition = getUserdataShared(L, 1)->dynamic_self_cast(); + outfit.lookFamiliarsType = Lua::getNumber(L, 14, outfit.lookFamiliarsType); + outfit.lookMountFeet = Lua::getNumber(L, 13, outfit.lookMountFeet); + outfit.lookMountLegs = Lua::getNumber(L, 12, outfit.lookMountLegs); + outfit.lookMountBody = Lua::getNumber(L, 11, outfit.lookMountBody); + outfit.lookMountHead = Lua::getNumber(L, 10, outfit.lookMountHead); + outfit.lookMount = Lua::getNumber(L, 9, outfit.lookMount); + outfit.lookAddons = Lua::getNumber(L, 8, outfit.lookAddons); + outfit.lookFeet = Lua::getNumber(L, 7); + outfit.lookLegs = Lua::getNumber(L, 6); + outfit.lookBody = Lua::getNumber(L, 5); + outfit.lookHead = Lua::getNumber(L, 4); + outfit.lookType = Lua::getNumber(L, 3); + outfit.lookTypeEx = Lua::getNumber(L, 2); + } + + const std::shared_ptr &condition = Lua::getUserdataShared(L, 1)->dynamic_self_cast(); if (condition) { condition->setOutfit(outfit); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -212,12 +237,12 @@ int ConditionFunctions::luaConditionSetOutfit(lua_State* L) { int ConditionFunctions::luaConditionAddDamage(lua_State* L) { // condition:addDamage(rounds, time, value) - const int32_t value = getNumber(L, 4); - const int32_t time = getNumber(L, 3); - const int32_t rounds = getNumber(L, 2); - const std::shared_ptr &condition = getUserdataShared(L, 1)->dynamic_self_cast(); + const int32_t value = Lua::getNumber(L, 4); + const int32_t time = Lua::getNumber(L, 3); + const int32_t rounds = Lua::getNumber(L, 2); + const std::shared_ptr &condition = Lua::getUserdataShared(L, 1)->dynamic_self_cast(); if (condition) { - pushBoolean(L, condition->addDamage(rounds, time, value)); + Lua::pushBoolean(L, condition->addDamage(rounds, time, value)); } else { lua_pushnil(L); } diff --git a/src/lua/functions/creatures/combat/condition_functions.hpp b/src/lua/functions/creatures/combat/condition_functions.hpp index 931a4fc2f5e..e8a3e834207 100644 --- a/src/lua/functions/creatures/combat/condition_functions.hpp +++ b/src/lua/functions/creatures/combat/condition_functions.hpp @@ -9,39 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class ConditionFunctions final : LuaScriptInterface { +class ConditionFunctions { public: - explicit ConditionFunctions(lua_State* L) : - LuaScriptInterface("ConditionFunctions") { - init(L); - } - ~ConditionFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Condition", "", ConditionFunctions::luaConditionCreate); - registerMetaMethod(L, "Condition", "__eq", ConditionFunctions::luaUserdataCompare); - registerMetaMethod(L, "Condition", "__gc", ConditionFunctions::luaConditionDelete); - registerMethod(L, "Condition", "delete", ConditionFunctions::luaConditionDelete); - - registerMethod(L, "Condition", "getId", ConditionFunctions::luaConditionGetId); - registerMethod(L, "Condition", "getSubId", ConditionFunctions::luaConditionGetSubId); - registerMethod(L, "Condition", "getType", ConditionFunctions::luaConditionGetType); - registerMethod(L, "Condition", "getIcons", ConditionFunctions::luaConditionGetIcons); - registerMethod(L, "Condition", "getEndTime", ConditionFunctions::luaConditionGetEndTime); - - registerMethod(L, "Condition", "clone", ConditionFunctions::luaConditionClone); - - registerMethod(L, "Condition", "getTicks", ConditionFunctions::luaConditionGetTicks); - registerMethod(L, "Condition", "setTicks", ConditionFunctions::luaConditionSetTicks); - - registerMethod(L, "Condition", "setParameter", ConditionFunctions::luaConditionSetParameter); - registerMethod(L, "Condition", "setFormula", ConditionFunctions::luaConditionSetFormula); - registerMethod(L, "Condition", "setOutfit", ConditionFunctions::luaConditionSetOutfit); - - registerMethod(L, "Condition", "addDamage", ConditionFunctions::luaConditionAddDamage); - } + static void init(lua_State* L); private: static int luaConditionCreate(lua_State* L); diff --git a/src/lua/functions/creatures/combat/spell_functions.cpp b/src/lua/functions/creatures/combat/spell_functions.cpp index 1a19485afd4..0d2d4a955ef 100644 --- a/src/lua/functions/creatures/combat/spell_functions.cpp +++ b/src/lua/functions/creatures/combat/spell_functions.cpp @@ -13,6 +13,55 @@ #include "creatures/players/vocations/vocation.hpp" #include "items/item.hpp" #include "utils/tools.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void SpellFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Spell", "", SpellFunctions::luaSpellCreate); + Lua::registerMetaMethod(L, "Spell", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Spell", "onCastSpell", SpellFunctions::luaSpellOnCastSpell); + Lua::registerMethod(L, "Spell", "register", SpellFunctions::luaSpellRegister); + Lua::registerMethod(L, "Spell", "name", SpellFunctions::luaSpellName); + Lua::registerMethod(L, "Spell", "id", SpellFunctions::luaSpellId); + Lua::registerMethod(L, "Spell", "group", SpellFunctions::luaSpellGroup); + Lua::registerMethod(L, "Spell", "cooldown", SpellFunctions::luaSpellCooldown); + Lua::registerMethod(L, "Spell", "groupCooldown", SpellFunctions::luaSpellGroupCooldown); + Lua::registerMethod(L, "Spell", "level", SpellFunctions::luaSpellLevel); + Lua::registerMethod(L, "Spell", "magicLevel", SpellFunctions::luaSpellMagicLevel); + Lua::registerMethod(L, "Spell", "mana", SpellFunctions::luaSpellMana); + Lua::registerMethod(L, "Spell", "manaPercent", SpellFunctions::luaSpellManaPercent); + Lua::registerMethod(L, "Spell", "soul", SpellFunctions::luaSpellSoul); + Lua::registerMethod(L, "Spell", "range", SpellFunctions::luaSpellRange); + Lua::registerMethod(L, "Spell", "isPremium", SpellFunctions::luaSpellPremium); + Lua::registerMethod(L, "Spell", "isEnabled", SpellFunctions::luaSpellEnabled); + Lua::registerMethod(L, "Spell", "needTarget", SpellFunctions::luaSpellNeedTarget); + Lua::registerMethod(L, "Spell", "needWeapon", SpellFunctions::luaSpellNeedWeapon); + Lua::registerMethod(L, "Spell", "needLearn", SpellFunctions::luaSpellNeedLearn); + Lua::registerMethod(L, "Spell", "allowOnSelf", SpellFunctions::luaSpellAllowOnSelf); + Lua::registerMethod(L, "Spell", "setPzLocked", SpellFunctions::luaSpellPzLocked); + Lua::registerMethod(L, "Spell", "isSelfTarget", SpellFunctions::luaSpellSelfTarget); + Lua::registerMethod(L, "Spell", "isBlocking", SpellFunctions::luaSpellBlocking); + Lua::registerMethod(L, "Spell", "isAggressive", SpellFunctions::luaSpellAggressive); + Lua::registerMethod(L, "Spell", "vocation", SpellFunctions::luaSpellVocation); + + Lua::registerMethod(L, "Spell", "castSound", SpellFunctions::luaSpellCastSound); + Lua::registerMethod(L, "Spell", "impactSound", SpellFunctions::luaSpellImpactSound); + + // Only for InstantSpell. + Lua::registerMethod(L, "Spell", "words", SpellFunctions::luaSpellWords); + Lua::registerMethod(L, "Spell", "needDirection", SpellFunctions::luaSpellNeedDirection); + Lua::registerMethod(L, "Spell", "hasParams", SpellFunctions::luaSpellHasParams); + Lua::registerMethod(L, "Spell", "hasPlayerNameParam", SpellFunctions::luaSpellHasPlayerNameParam); + Lua::registerMethod(L, "Spell", "needCasterTargetOrDirection", SpellFunctions::luaSpellNeedCasterTargetOrDirection); + Lua::registerMethod(L, "Spell", "isBlockingWalls", SpellFunctions::luaSpellIsBlockingWalls); + + // Only for RuneSpells. + Lua::registerMethod(L, "Spell", "runeId", SpellFunctions::luaSpellRuneId); + Lua::registerMethod(L, "Spell", "charges", SpellFunctions::luaSpellCharges); + Lua::registerMethod(L, "Spell", "allowFarUse", SpellFunctions::luaSpellAllowFarUse); + Lua::registerMethod(L, "Spell", "blockWalls", SpellFunctions::luaSpellBlockWalls); + Lua::registerMethod(L, "Spell", "checkFloor", SpellFunctions::luaSpellCheckFloor); +} int SpellFunctions::luaSpellCreate(lua_State* L) { // Spell(words, name or id) to get an existing spell @@ -26,35 +75,35 @@ int SpellFunctions::luaSpellCreate(lua_State* L) { SpellType_t spellType = SPELL_UNDEFINED; - if (isNumber(L, 2)) { - uint16_t id = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + uint16_t id = Lua::getNumber(L, 2); const auto &rune = g_spells().getRuneSpell(id); if (rune) { - pushUserdata(L, rune); - setMetatable(L, -1, "Spell"); + Lua::pushUserdata(L, rune); + Lua::setMetatable(L, -1, "Spell"); return 1; } spellType = static_cast(id); - } else if (isString(L, 2)) { - const std::string arg = getString(L, 2); + } else if (Lua::isString(L, 2)) { + const std::string arg = Lua::getString(L, 2); auto instant = g_spells().getInstantSpellByName(arg); if (instant) { - pushUserdata(L, instant); - setMetatable(L, -1, "Spell"); + Lua::pushUserdata(L, instant); + Lua::setMetatable(L, -1, "Spell"); return 1; } instant = g_spells().getInstantSpell(arg); if (instant) { - pushUserdata(L, instant); - setMetatable(L, -1, "Spell"); + Lua::pushUserdata(L, instant); + Lua::setMetatable(L, -1, "Spell"); return 1; } const auto &rune = g_spells().getRuneSpellByName(arg); if (rune) { - pushUserdata(L, rune); - setMetatable(L, -1, "Spell"); + Lua::pushUserdata(L, rune); + Lua::setMetatable(L, -1, "Spell"); return 1; } @@ -67,15 +116,15 @@ int SpellFunctions::luaSpellCreate(lua_State* L) { } if (spellType == SPELL_INSTANT) { - const auto &spell = std::make_shared(getScriptEnv()->getScriptInterface()); - pushUserdata(L, spell); - setMetatable(L, -1, "Spell"); + const auto &spell = std::make_shared(); + Lua::pushUserdata(L, spell); + Lua::setMetatable(L, -1, "Spell"); spell->spellType = SPELL_INSTANT; return 1; } else if (spellType == SPELL_RUNE) { - const auto &runeSpell = std::make_shared(getScriptEnv()->getScriptInterface()); - pushUserdata(L, runeSpell); - setMetatable(L, -1, "Spell"); + const auto &runeSpell = std::make_shared(); + Lua::pushUserdata(L, runeSpell); + Lua::setMetatable(L, -1, "Spell"); runeSpell->spellType = SPELL_RUNE; return 1; } @@ -86,24 +135,22 @@ int SpellFunctions::luaSpellCreate(lua_State* L) { int SpellFunctions::luaSpellOnCastSpell(lua_State* L) { // spell:onCastSpell(callback) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (spell->spellType == SPELL_INSTANT) { const auto &instant = std::static_pointer_cast(spell); - if (!instant->loadCallback()) { - pushBoolean(L, false); + if (!instant->loadScriptId()) { + Lua::pushBoolean(L, false); return 1; } - instant->setLoadedCallback(true); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else if (spell->spellType == SPELL_RUNE) { const auto &rune = std::static_pointer_cast(spell); - if (!rune->loadCallback()) { - pushBoolean(L, false); + if (!rune->loadRuneSpellScriptId()) { + Lua::pushBoolean(L, false); return 1; } - rune->setLoadedCallback(true); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -113,23 +160,23 @@ int SpellFunctions::luaSpellOnCastSpell(lua_State* L) { int SpellFunctions::luaSpellRegister(lua_State* L) { // spell:register() - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (!spell) { - reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } if (spell->spellType == SPELL_INSTANT) { - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &instant = std::static_pointer_cast(spellBase); - if (!instant->isLoadedCallback()) { - pushBoolean(L, false); + if (!instant->isLoadedScriptId()) { + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, g_spells().registerInstantLuaEvent(instant)); + Lua::pushBoolean(L, g_spells().registerInstantLuaEvent(instant)); } else if (spell->spellType == SPELL_RUNE) { - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &rune = std::static_pointer_cast(spellBase); if (rune->getMagicLevel() != 0 || rune->getLevel() != 0) { // Change information in the ItemType to get accurate description @@ -142,11 +189,11 @@ int SpellFunctions::luaSpellRegister(lua_State* L) { iType.runeLevel = rune->getLevel(); iType.charges = rune->getCharges(); } - if (!rune->isLoadedCallback()) { - pushBoolean(L, false); + if (!rune->isRuneSpellLoadedScriptId()) { + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, g_spells().registerRuneLuaEvent(rune)); + Lua::pushBoolean(L, g_spells().registerRuneLuaEvent(rune)); } return 1; } @@ -154,13 +201,13 @@ int SpellFunctions::luaSpellRegister(lua_State* L) { int SpellFunctions::luaSpellName(lua_State* L) { // spell:name(name) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { - pushString(L, spell->getName()); + Lua::pushString(L, spell->getName()); } else { - spell->setName(getString(L, 2)); - pushBoolean(L, true); + spell->setName(Lua::getString(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -170,18 +217,18 @@ int SpellFunctions::luaSpellName(lua_State* L) { int SpellFunctions::luaSpellId(lua_State* L) { // spell:id(id) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (spell->spellType != SPELL_INSTANT && spell->spellType != SPELL_RUNE) { - reportErrorFunc("The method: 'spell:id(id)' is only for use of instant spells and rune spells"); - pushBoolean(L, false); + Lua::reportErrorFunc("The method: 'spell:id(id)' is only for use of instant spells and rune spells"); + Lua::pushBoolean(L, false); return 1; } if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getSpellId()); } else { - spell->setSpellId(getNumber(L, 2)); - pushBoolean(L, true); + spell->setSpellId(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -191,70 +238,70 @@ int SpellFunctions::luaSpellId(lua_State* L) { int SpellFunctions::luaSpellGroup(lua_State* L) { // spell:group(primaryGroup[, secondaryGroup]) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getGroup()); lua_pushnumber(L, spell->getSecondaryGroup()); return 2; } else if (lua_gettop(L) == 2) { - auto group = getNumber(L, 2); + auto group = Lua::getNumber(L, 2); if (group) { spell->setGroup(group); - pushBoolean(L, true); - } else if (isString(L, 2)) { - group = stringToSpellGroup(getString(L, 2)); + Lua::pushBoolean(L, true); + } else if (Lua::isString(L, 2)) { + group = stringToSpellGroup(Lua::getString(L, 2)); if (group != SPELLGROUP_NONE) { spell->setGroup(group); } else { g_logger().warn("[SpellFunctions::luaSpellGroup] - " "Unknown group: {}", - getString(L, 2)); - pushBoolean(L, false); + Lua::getString(L, 2)); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { g_logger().warn("[SpellFunctions::luaSpellGroup] - " "Unknown group: {}", - getString(L, 2)); - pushBoolean(L, false); + Lua::getString(L, 2)); + Lua::pushBoolean(L, false); return 1; } } else { - auto primaryGroup = getNumber(L, 2); - auto secondaryGroup = getNumber(L, 2); + auto primaryGroup = Lua::getNumber(L, 2); + auto secondaryGroup = Lua::getNumber(L, 2); if (primaryGroup && secondaryGroup) { spell->setGroup(primaryGroup); spell->setSecondaryGroup(secondaryGroup); - pushBoolean(L, true); - } else if (isString(L, 2) && isString(L, 3)) { - primaryGroup = stringToSpellGroup(getString(L, 2)); + Lua::pushBoolean(L, true); + } else if (Lua::isString(L, 2) && Lua::isString(L, 3)) { + primaryGroup = stringToSpellGroup(Lua::getString(L, 2)); if (primaryGroup != SPELLGROUP_NONE) { spell->setGroup(primaryGroup); } else { g_logger().warn("[SpellFunctions::luaSpellGroup] - " "Unknown primaryGroup: {}", - getString(L, 2)); - pushBoolean(L, false); + Lua::getString(L, 2)); + Lua::pushBoolean(L, false); return 1; } - secondaryGroup = stringToSpellGroup(getString(L, 3)); + secondaryGroup = stringToSpellGroup(Lua::getString(L, 3)); if (secondaryGroup != SPELLGROUP_NONE) { spell->setSecondaryGroup(secondaryGroup); } else { g_logger().warn("[SpellFunctions::luaSpellGroup] - " "Unknown secondaryGroup: {}", - getString(L, 3)); - pushBoolean(L, false); + Lua::getString(L, 3)); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { g_logger().warn("[SpellFunctions::luaSpellGroup] - " "Unknown primaryGroup: {} or secondaryGroup: {}", - getString(L, 2), getString(L, 3)); - pushBoolean(L, false); + Lua::getString(L, 2), Lua::getString(L, 3)); + Lua::pushBoolean(L, false); return 1; } } @@ -266,13 +313,13 @@ int SpellFunctions::luaSpellGroup(lua_State* L) { int SpellFunctions::luaSpellCastSound(lua_State* L) { // get: spell:castSound() set: spell:castSound(effect) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, static_cast(spell->soundCastEffect)); } else { - spell->soundCastEffect = static_cast(getNumber(L, 2)); - pushBoolean(L, true); + spell->soundCastEffect = static_cast(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -282,13 +329,13 @@ int SpellFunctions::luaSpellCastSound(lua_State* L) { int SpellFunctions::luaSpellImpactSound(lua_State* L) { // get: spell:impactSound() set: spell:impactSound(effect) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, static_cast(spell->soundImpactEffect)); } else { - spell->soundImpactEffect = static_cast(getNumber(L, 2)); - pushBoolean(L, true); + spell->soundImpactEffect = static_cast(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -298,13 +345,13 @@ int SpellFunctions::luaSpellImpactSound(lua_State* L) { int SpellFunctions::luaSpellCooldown(lua_State* L) { // spell:cooldown(cooldown) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getCooldown()); } else { - spell->setCooldown(getNumber(L, 2)); - pushBoolean(L, true); + spell->setCooldown(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -314,19 +361,19 @@ int SpellFunctions::luaSpellCooldown(lua_State* L) { int SpellFunctions::luaSpellGroupCooldown(lua_State* L) { // spell:groupCooldown(primaryGroupCd[, secondaryGroupCd]) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getGroupCooldown()); lua_pushnumber(L, spell->getSecondaryCooldown()); return 2; } else if (lua_gettop(L) == 2) { - spell->setGroupCooldown(getNumber(L, 2)); - pushBoolean(L, true); + spell->setGroupCooldown(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { - spell->setGroupCooldown(getNumber(L, 2)); - spell->setSecondaryCooldown(getNumber(L, 3)); - pushBoolean(L, true); + spell->setGroupCooldown(Lua::getNumber(L, 2)); + spell->setSecondaryCooldown(Lua::getNumber(L, 3)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -336,13 +383,13 @@ int SpellFunctions::luaSpellGroupCooldown(lua_State* L) { int SpellFunctions::luaSpellLevel(lua_State* L) { // spell:level(lvl) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getLevel()); } else { - spell->setLevel(getNumber(L, 2)); - pushBoolean(L, true); + spell->setLevel(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -352,13 +399,13 @@ int SpellFunctions::luaSpellLevel(lua_State* L) { int SpellFunctions::luaSpellMagicLevel(lua_State* L) { // spell:magicLevel(lvl) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getMagicLevel()); } else { - spell->setMagicLevel(getNumber(L, 2)); - pushBoolean(L, true); + spell->setMagicLevel(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -368,13 +415,13 @@ int SpellFunctions::luaSpellMagicLevel(lua_State* L) { int SpellFunctions::luaSpellMana(lua_State* L) { // spell:mana(mana) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getMana()); } else { - spell->setMana(getNumber(L, 2)); - pushBoolean(L, true); + spell->setMana(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -384,13 +431,13 @@ int SpellFunctions::luaSpellMana(lua_State* L) { int SpellFunctions::luaSpellManaPercent(lua_State* L) { // spell:manaPercent(percent) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getManaPercent()); } else { - spell->setManaPercent(getNumber(L, 2)); - pushBoolean(L, true); + spell->setManaPercent(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -400,13 +447,13 @@ int SpellFunctions::luaSpellManaPercent(lua_State* L) { int SpellFunctions::luaSpellSoul(lua_State* L) { // spell:soul(soul) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getSoulCost()); } else { - spell->setSoulCost(getNumber(L, 2)); - pushBoolean(L, true); + spell->setSoulCost(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -416,13 +463,13 @@ int SpellFunctions::luaSpellSoul(lua_State* L) { int SpellFunctions::luaSpellRange(lua_State* L) { // spell:range(range) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getRange()); } else { - spell->setRange(getNumber(L, 2)); - pushBoolean(L, true); + spell->setRange(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -432,13 +479,13 @@ int SpellFunctions::luaSpellRange(lua_State* L) { int SpellFunctions::luaSpellPremium(lua_State* L) { // spell:isPremium(bool) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { - pushBoolean(L, spell->isPremium()); + Lua::pushBoolean(L, spell->isPremium()); } else { - spell->setPremium(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setPremium(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -448,13 +495,13 @@ int SpellFunctions::luaSpellPremium(lua_State* L) { int SpellFunctions::luaSpellEnabled(lua_State* L) { // spell:isEnabled(bool) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { - pushBoolean(L, spell->isEnabled()); + Lua::pushBoolean(L, spell->isEnabled()); } else { - spell->setEnabled(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setEnabled(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -464,13 +511,13 @@ int SpellFunctions::luaSpellEnabled(lua_State* L) { int SpellFunctions::luaSpellNeedTarget(lua_State* L) { // spell:needTarget(bool) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getNeedTarget()); + Lua::pushBoolean(L, spell->getNeedTarget()); } else { - spell->setNeedTarget(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setNeedTarget(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -480,13 +527,13 @@ int SpellFunctions::luaSpellNeedTarget(lua_State* L) { int SpellFunctions::luaSpellNeedWeapon(lua_State* L) { // spell:needWeapon(bool) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getNeedWeapon()); + Lua::pushBoolean(L, spell->getNeedWeapon()); } else { - spell->setNeedWeapon(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setNeedWeapon(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -496,13 +543,13 @@ int SpellFunctions::luaSpellNeedWeapon(lua_State* L) { int SpellFunctions::luaSpellNeedLearn(lua_State* L) { // spell:needLearn(bool) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getNeedLearn()); + Lua::pushBoolean(L, spell->getNeedLearn()); } else { - spell->setNeedLearn(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setNeedLearn(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -512,12 +559,12 @@ int SpellFunctions::luaSpellNeedLearn(lua_State* L) { int SpellFunctions::luaSpellSelfTarget(lua_State* L) { // spell:isSelfTarget(bool) - if (const auto &spell = getUserdataShared(L, 1)) { + if (const auto &spell = Lua::getUserdataShared(L, 1)) { if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getSelfTarget()); + Lua::pushBoolean(L, spell->getSelfTarget()); } else { - spell->setSelfTarget(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setSelfTarget(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -527,15 +574,15 @@ int SpellFunctions::luaSpellSelfTarget(lua_State* L) { int SpellFunctions::luaSpellBlocking(lua_State* L) { // spell:isBlocking(blockingSolid, blockingCreature) - if (const auto &spell = getUserdataShared(L, 1)) { + if (const auto &spell = Lua::getUserdataShared(L, 1)) { if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getBlockingSolid()); - pushBoolean(L, spell->getBlockingCreature()); + Lua::pushBoolean(L, spell->getBlockingSolid()); + Lua::pushBoolean(L, spell->getBlockingCreature()); return 2; } else { - spell->setBlockingSolid(getBoolean(L, 2)); - spell->setBlockingCreature(getBoolean(L, 3)); - pushBoolean(L, true); + spell->setBlockingSolid(Lua::getBoolean(L, 2)); + spell->setBlockingCreature(Lua::getBoolean(L, 3)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -545,13 +592,13 @@ int SpellFunctions::luaSpellBlocking(lua_State* L) { int SpellFunctions::luaSpellAggressive(lua_State* L) { // spell:isAggressive(bool) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getAggressive()); + Lua::pushBoolean(L, spell->getAggressive()); } else { - spell->setAggressive(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setAggressive(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -561,13 +608,13 @@ int SpellFunctions::luaSpellAggressive(lua_State* L) { int SpellFunctions::luaSpellAllowOnSelf(lua_State* L) { // spell:allowOnSelf(bool) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getAllowOnSelf()); + Lua::pushBoolean(L, spell->getAllowOnSelf()); } else { - spell->setAllowOnSelf(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setAllowOnSelf(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -577,13 +624,13 @@ int SpellFunctions::luaSpellAllowOnSelf(lua_State* L) { int SpellFunctions::luaSpellPzLocked(lua_State* L) { // spell:isPzLocked(bool) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getLockedPZ()); + Lua::pushBoolean(L, spell->getLockedPZ()); } else { - spell->setLockedPZ(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setLockedPZ(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -593,7 +640,7 @@ int SpellFunctions::luaSpellPzLocked(lua_State* L) { int SpellFunctions::luaSpellVocation(lua_State* L) { // spell:vocation(vocation) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_createtable(L, 0, 0); @@ -603,14 +650,14 @@ int SpellFunctions::luaSpellVocation(lua_State* L) { std::string s = std::to_string(it); const char* pchar = s.c_str(); std::string name = g_vocations().getVocation(voc.first)->getVocName(); - setField(L, pchar, name); + Lua::setField(L, pchar, name); } - setMetatable(L, -1, "Spell"); + Lua::setMetatable(L, -1, "Spell"); } else { const int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc for (int i = 0; i < parameters; ++i) { - if (getString(L, 2 + i).find(';') != std::string::npos) { - std::vector vocList = explodeString(getString(L, 2 + i), ";"); + if (Lua::getString(L, 2 + i).find(';') != std::string::npos) { + std::vector vocList = explodeString(Lua::getString(L, 2 + i), ";"); const int32_t vocationId = g_vocations().getVocationId(vocList[0]); if (!vocList.empty()) { if (vocList[1] == "true") { @@ -620,11 +667,11 @@ int SpellFunctions::luaSpellVocation(lua_State* L) { } } } else { - const int32_t vocationId = g_vocations().getVocationId(getString(L, 2 + i)); + const int32_t vocationId = g_vocations().getVocationId(Lua::getString(L, 2 + i)); spell->addVocMap(vocationId, false); } } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -635,7 +682,7 @@ int SpellFunctions::luaSpellVocation(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellWords(lua_State* L) { // spell:words(words[, separator = ""]) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil @@ -645,17 +692,17 @@ int SpellFunctions::luaSpellWords(lua_State* L) { } if (lua_gettop(L) == 1) { - pushString(L, spell->getWords()); - pushString(L, spell->getSeparator()); + Lua::pushString(L, spell->getWords()); + Lua::pushString(L, spell->getSeparator()); return 2; } else { std::string sep; if (lua_gettop(L) == 3) { - sep = getString(L, 3); + sep = Lua::getString(L, 3); } - spell->setWords(getString(L, 2)); + spell->setWords(Lua::getString(L, 2)); spell->setSeparator(sep); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -666,7 +713,7 @@ int SpellFunctions::luaSpellWords(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellNeedDirection(lua_State* L) { // spell:needDirection(bool) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil @@ -676,10 +723,10 @@ int SpellFunctions::luaSpellNeedDirection(lua_State* L) { } if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getNeedDirection()); + Lua::pushBoolean(L, spell->getNeedDirection()); } else { - spell->setNeedDirection(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setNeedDirection(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -690,7 +737,7 @@ int SpellFunctions::luaSpellNeedDirection(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellHasParams(lua_State* L) { // spell:hasParams(bool) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil @@ -700,10 +747,10 @@ int SpellFunctions::luaSpellHasParams(lua_State* L) { } if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getHasParam()); + Lua::pushBoolean(L, spell->getHasParam()); } else { - spell->setHasParam(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setHasParam(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -714,7 +761,7 @@ int SpellFunctions::luaSpellHasParams(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellHasPlayerNameParam(lua_State* L) { // spell:hasPlayerNameParam(bool) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil @@ -724,10 +771,10 @@ int SpellFunctions::luaSpellHasPlayerNameParam(lua_State* L) { } if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getHasPlayerNameParam()); + Lua::pushBoolean(L, spell->getHasPlayerNameParam()); } else { - spell->setHasPlayerNameParam(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setHasPlayerNameParam(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -738,7 +785,7 @@ int SpellFunctions::luaSpellHasPlayerNameParam(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellNeedCasterTargetOrDirection(lua_State* L) { // spell:needCasterTargetOrDirection(bool) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil @@ -748,10 +795,10 @@ int SpellFunctions::luaSpellNeedCasterTargetOrDirection(lua_State* L) { } if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getNeedCasterTargetOrDirection()); + Lua::pushBoolean(L, spell->getNeedCasterTargetOrDirection()); } else { - spell->setNeedCasterTargetOrDirection(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setNeedCasterTargetOrDirection(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -762,7 +809,7 @@ int SpellFunctions::luaSpellNeedCasterTargetOrDirection(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellIsBlockingWalls(lua_State* L) { // spell:blockWalls(bool) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil @@ -772,10 +819,10 @@ int SpellFunctions::luaSpellIsBlockingWalls(lua_State* L) { } if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getBlockWalls()); + Lua::pushBoolean(L, spell->getBlockWalls()); } else { - spell->setBlockWalls(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setBlockWalls(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -786,7 +833,7 @@ int SpellFunctions::luaSpellIsBlockingWalls(lua_State* L) { // only for RuneSpells int SpellFunctions::luaSpellRuneId(lua_State* L) { // spell:runeId(id) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_RUNE, it means that this actually is no RuneSpell, so we return nil @@ -798,8 +845,8 @@ int SpellFunctions::luaSpellRuneId(lua_State* L) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getRuneItemId()); } else { - spell->setRuneItemId(getNumber(L, 2)); - pushBoolean(L, true); + spell->setRuneItemId(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -810,7 +857,7 @@ int SpellFunctions::luaSpellRuneId(lua_State* L) { // only for RuneSpells int SpellFunctions::luaSpellCharges(lua_State* L) { // spell:charges(charges) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_RUNE, it means that this actually is no RuneSpell, so we return nil @@ -822,8 +869,8 @@ int SpellFunctions::luaSpellCharges(lua_State* L) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getCharges()); } else { - spell->setCharges(getNumber(L, 2)); - pushBoolean(L, true); + spell->setCharges(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -834,7 +881,7 @@ int SpellFunctions::luaSpellCharges(lua_State* L) { // only for RuneSpells int SpellFunctions::luaSpellAllowFarUse(lua_State* L) { // spell:allowFarUse(bool) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_RUNE, it means that this actually is no RuneSpell, so we return nil @@ -844,10 +891,10 @@ int SpellFunctions::luaSpellAllowFarUse(lua_State* L) { } if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getAllowFarUse()); + Lua::pushBoolean(L, spell->getAllowFarUse()); } else { - spell->setAllowFarUse(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setAllowFarUse(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -858,7 +905,7 @@ int SpellFunctions::luaSpellAllowFarUse(lua_State* L) { // only for RuneSpells int SpellFunctions::luaSpellBlockWalls(lua_State* L) { // spell:blockWalls(bool) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_RUNE, it means that this actually is no RuneSpell, so we return nil @@ -868,10 +915,10 @@ int SpellFunctions::luaSpellBlockWalls(lua_State* L) { } if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getCheckLineOfSight()); + Lua::pushBoolean(L, spell->getCheckLineOfSight()); } else { - spell->setCheckLineOfSight(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setCheckLineOfSight(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -882,7 +929,7 @@ int SpellFunctions::luaSpellBlockWalls(lua_State* L) { // only for RuneSpells int SpellFunctions::luaSpellCheckFloor(lua_State* L) { // spell:checkFloor(bool) - const auto &spellBase = getUserdataShared(L, 1); + const auto &spellBase = Lua::getUserdataShared(L, 1); const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_RUNE, it means that this actually is no RuneSpell, so we return nil @@ -892,10 +939,10 @@ int SpellFunctions::luaSpellCheckFloor(lua_State* L) { } if (lua_gettop(L) == 1) { - pushBoolean(L, spell->getCheckFloor()); + Lua::pushBoolean(L, spell->getCheckFloor()); } else { - spell->setCheckFloor(getBoolean(L, 2)); - pushBoolean(L, true); + spell->setCheckFloor(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); diff --git a/src/lua/functions/creatures/combat/spell_functions.hpp b/src/lua/functions/creatures/combat/spell_functions.hpp index 429ebe11270..684b8af8ba2 100644 --- a/src/lua/functions/creatures/combat/spell_functions.hpp +++ b/src/lua/functions/creatures/combat/spell_functions.hpp @@ -9,63 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class SpellFunctions final : LuaScriptInterface { +class SpellFunctions { public: - explicit SpellFunctions(lua_State* L) : - LuaScriptInterface("SpellFunctions") { - init(L); - } - ~SpellFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Spell", "", SpellFunctions::luaSpellCreate); - registerMetaMethod(L, "Spell", "__eq", SpellFunctions::luaUserdataCompare); - - registerMethod(L, "Spell", "onCastSpell", SpellFunctions::luaSpellOnCastSpell); - registerMethod(L, "Spell", "register", SpellFunctions::luaSpellRegister); - registerMethod(L, "Spell", "name", SpellFunctions::luaSpellName); - registerMethod(L, "Spell", "id", SpellFunctions::luaSpellId); - registerMethod(L, "Spell", "group", SpellFunctions::luaSpellGroup); - registerMethod(L, "Spell", "cooldown", SpellFunctions::luaSpellCooldown); - registerMethod(L, "Spell", "groupCooldown", SpellFunctions::luaSpellGroupCooldown); - registerMethod(L, "Spell", "level", SpellFunctions::luaSpellLevel); - registerMethod(L, "Spell", "magicLevel", SpellFunctions::luaSpellMagicLevel); - registerMethod(L, "Spell", "mana", SpellFunctions::luaSpellMana); - registerMethod(L, "Spell", "manaPercent", SpellFunctions::luaSpellManaPercent); - registerMethod(L, "Spell", "soul", SpellFunctions::luaSpellSoul); - registerMethod(L, "Spell", "range", SpellFunctions::luaSpellRange); - registerMethod(L, "Spell", "isPremium", SpellFunctions::luaSpellPremium); - registerMethod(L, "Spell", "isEnabled", SpellFunctions::luaSpellEnabled); - registerMethod(L, "Spell", "needTarget", SpellFunctions::luaSpellNeedTarget); - registerMethod(L, "Spell", "needWeapon", SpellFunctions::luaSpellNeedWeapon); - registerMethod(L, "Spell", "needLearn", SpellFunctions::luaSpellNeedLearn); - registerMethod(L, "Spell", "allowOnSelf", SpellFunctions::luaSpellAllowOnSelf); - registerMethod(L, "Spell", "setPzLocked", SpellFunctions::luaSpellPzLocked); - registerMethod(L, "Spell", "isSelfTarget", SpellFunctions::luaSpellSelfTarget); - registerMethod(L, "Spell", "isBlocking", SpellFunctions::luaSpellBlocking); - registerMethod(L, "Spell", "isAggressive", SpellFunctions::luaSpellAggressive); - registerMethod(L, "Spell", "vocation", SpellFunctions::luaSpellVocation); - - registerMethod(L, "Spell", "castSound", SpellFunctions::luaSpellCastSound); - registerMethod(L, "Spell", "impactSound", SpellFunctions::luaSpellImpactSound); - - // Only for InstantSpell. - registerMethod(L, "Spell", "words", SpellFunctions::luaSpellWords); - registerMethod(L, "Spell", "needDirection", SpellFunctions::luaSpellNeedDirection); - registerMethod(L, "Spell", "hasParams", SpellFunctions::luaSpellHasParams); - registerMethod(L, "Spell", "hasPlayerNameParam", SpellFunctions::luaSpellHasPlayerNameParam); - registerMethod(L, "Spell", "needCasterTargetOrDirection", SpellFunctions::luaSpellNeedCasterTargetOrDirection); - registerMethod(L, "Spell", "isBlockingWalls", SpellFunctions::luaSpellIsBlockingWalls); - - // Only for RuneSpells. - registerMethod(L, "Spell", "runeId", SpellFunctions::luaSpellRuneId); - registerMethod(L, "Spell", "charges", SpellFunctions::luaSpellCharges); - registerMethod(L, "Spell", "allowFarUse", SpellFunctions::luaSpellAllowFarUse); - registerMethod(L, "Spell", "blockWalls", SpellFunctions::luaSpellBlockWalls); - registerMethod(L, "Spell", "checkFloor", SpellFunctions::luaSpellCheckFloor); - } + static void init(lua_State* L); private: static int luaSpellCreate(lua_State* L); diff --git a/src/lua/functions/creatures/combat/variant_functions.cpp b/src/lua/functions/creatures/combat/variant_functions.cpp index 19225017c07..5a386fca803 100644 --- a/src/lua/functions/creatures/combat/variant_functions.cpp +++ b/src/lua/functions/creatures/combat/variant_functions.cpp @@ -11,32 +11,41 @@ #include "items/cylinder.hpp" #include "lua/global/lua_variant.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void VariantFunctions::init(lua_State* L) { + Lua::registerClass(L, "Variant", "", VariantFunctions::luaVariantCreate); + + Lua::registerMethod(L, "Variant", "getNumber", VariantFunctions::luaVariantGetNumber); + Lua::registerMethod(L, "Variant", "getString", VariantFunctions::luaVariantGetString); + Lua::registerMethod(L, "Variant", "getPosition", VariantFunctions::luaVariantGetPosition); +} int VariantFunctions::luaVariantCreate(lua_State* L) { // Variant(number or string or position or thing) LuaVariant variant; - if (isUserdata(L, 2)) { - if (const auto &thing = getThing(L, 2)) { + if (Lua::isUserdata(L, 2)) { + if (const auto &thing = Lua::getThing(L, 2)) { variant.type = VARIANT_TARGETPOSITION; variant.pos = thing->getPosition(); } - } else if (isTable(L, 2)) { + } else if (Lua::isTable(L, 2)) { variant.type = VARIANT_POSITION; - variant.pos = getPosition(L, 2); - } else if (isNumber(L, 2)) { + variant.pos = Lua::getPosition(L, 2); + } else if (Lua::isNumber(L, 2)) { variant.type = VARIANT_NUMBER; - variant.number = getNumber(L, 2); - } else if (isString(L, 2)) { + variant.number = Lua::getNumber(L, 2); + } else if (Lua::isString(L, 2)) { variant.type = VARIANT_STRING; - variant.text = getString(L, 2); + variant.text = Lua::getString(L, 2); } - pushVariant(L, variant); + Lua::pushVariant(L, variant); return 1; } int VariantFunctions::luaVariantGetNumber(lua_State* L) { - // Variant:getNumber() - const LuaVariant &variant = getVariant(L, 1); + // Variant:Lua::getNumber() + const LuaVariant &variant = Lua::getVariant(L, 1); if (variant.type == VARIANT_NUMBER) { lua_pushnumber(L, variant.number); } else { @@ -46,23 +55,23 @@ int VariantFunctions::luaVariantGetNumber(lua_State* L) { } int VariantFunctions::luaVariantGetString(lua_State* L) { - // Variant:getString() - const LuaVariant &variant = getVariant(L, 1); + // Variant:Lua::getString() + const LuaVariant &variant = Lua::getVariant(L, 1); if (variant.type == VARIANT_STRING) { - pushString(L, variant.text); + Lua::pushString(L, variant.text); } else { - pushString(L, std::string()); + Lua::pushString(L, std::string()); } return 1; } int VariantFunctions::luaVariantGetPosition(lua_State* L) { - // Variant:getPosition() - const LuaVariant &variant = getVariant(L, 1); + // Variant:Lua::getPosition() + const LuaVariant &variant = Lua::getVariant(L, 1); if (variant.type == VARIANT_POSITION || variant.type == VARIANT_TARGETPOSITION) { - pushPosition(L, variant.pos); + Lua::pushPosition(L, variant.pos); } else { - pushPosition(L, Position()); + Lua::pushPosition(L, Position()); } return 1; } diff --git a/src/lua/functions/creatures/combat/variant_functions.hpp b/src/lua/functions/creatures/combat/variant_functions.hpp index eba239b221c..2ab5e29516a 100644 --- a/src/lua/functions/creatures/combat/variant_functions.hpp +++ b/src/lua/functions/creatures/combat/variant_functions.hpp @@ -9,23 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class VariantFunctions final : LuaScriptInterface { +class VariantFunctions { public: - explicit VariantFunctions(lua_State* L) : - LuaScriptInterface("VariantFunctions") { - init(L); - } - ~VariantFunctions() override = default; - - static void init(lua_State* L) { - registerClass(L, "Variant", "", VariantFunctions::luaVariantCreate); - - registerMethod(L, "Variant", "getNumber", VariantFunctions::luaVariantGetNumber); - registerMethod(L, "Variant", "getString", VariantFunctions::luaVariantGetString); - registerMethod(L, "Variant", "getPosition", VariantFunctions::luaVariantGetPosition); - } + static void init(lua_State* L); private: static int luaVariantCreate(lua_State* L); diff --git a/src/lua/functions/creatures/creature_functions.cpp b/src/lua/functions/creatures/creature_functions.cpp index 0da3b6cc07a..72b41d01928 100644 --- a/src/lua/functions/creatures/creature_functions.cpp +++ b/src/lua/functions/creatures/creature_functions.cpp @@ -16,28 +16,107 @@ #include "game/game.hpp" #include "lua/creature/creatureevent.hpp" #include "map/spectators.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void CreatureFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Creature", "", CreatureFunctions::luaCreatureCreate); + Lua::registerMetaMethod(L, "Creature", "__eq", Lua::luaUserdataCompare); + Lua::registerMethod(L, "Creature", "getEvents", CreatureFunctions::luaCreatureGetEvents); + Lua::registerMethod(L, "Creature", "registerEvent", CreatureFunctions::luaCreatureRegisterEvent); + Lua::registerMethod(L, "Creature", "unregisterEvent", CreatureFunctions::luaCreatureUnregisterEvent); + Lua::registerMethod(L, "Creature", "isRemoved", CreatureFunctions::luaCreatureIsRemoved); + Lua::registerMethod(L, "Creature", "isCreature", CreatureFunctions::luaCreatureIsCreature); + Lua::registerMethod(L, "Creature", "isInGhostMode", CreatureFunctions::luaCreatureIsInGhostMode); + Lua::registerMethod(L, "Creature", "isHealthHidden", CreatureFunctions::luaCreatureIsHealthHidden); + Lua::registerMethod(L, "Creature", "isImmune", CreatureFunctions::luaCreatureIsImmune); + Lua::registerMethod(L, "Creature", "canSee", CreatureFunctions::luaCreatureCanSee); + Lua::registerMethod(L, "Creature", "canSeeCreature", CreatureFunctions::luaCreatureCanSeeCreature); + Lua::registerMethod(L, "Creature", "getParent", CreatureFunctions::luaCreatureGetParent); + Lua::registerMethod(L, "Creature", "getId", CreatureFunctions::luaCreatureGetId); + Lua::registerMethod(L, "Creature", "getName", CreatureFunctions::luaCreatureGetName); + Lua::registerMethod(L, "Creature", "getTypeName", CreatureFunctions::luaCreatureGetTypeName); + Lua::registerMethod(L, "Creature", "getTarget", CreatureFunctions::luaCreatureGetTarget); + Lua::registerMethod(L, "Creature", "setTarget", CreatureFunctions::luaCreatureSetTarget); + Lua::registerMethod(L, "Creature", "getFollowCreature", CreatureFunctions::luaCreatureGetFollowCreature); + Lua::registerMethod(L, "Creature", "setFollowCreature", CreatureFunctions::luaCreatureSetFollowCreature); + Lua::registerMethod(L, "Creature", "reload", CreatureFunctions::luaCreatureReload); + Lua::registerMethod(L, "Creature", "getMaster", CreatureFunctions::luaCreatureGetMaster); + Lua::registerMethod(L, "Creature", "setMaster", CreatureFunctions::luaCreatureSetMaster); + Lua::registerMethod(L, "Creature", "getLight", CreatureFunctions::luaCreatureGetLight); + Lua::registerMethod(L, "Creature", "setLight", CreatureFunctions::luaCreatureSetLight); + Lua::registerMethod(L, "Creature", "getSpeed", CreatureFunctions::luaCreatureGetSpeed); + Lua::registerMethod(L, "Creature", "setSpeed", CreatureFunctions::luaCreatureSetSpeed); + Lua::registerMethod(L, "Creature", "getBaseSpeed", CreatureFunctions::luaCreatureGetBaseSpeed); + Lua::registerMethod(L, "Creature", "changeSpeed", CreatureFunctions::luaCreatureChangeSpeed); + Lua::registerMethod(L, "Creature", "setDropLoot", CreatureFunctions::luaCreatureSetDropLoot); + Lua::registerMethod(L, "Creature", "setSkillLoss", CreatureFunctions::luaCreatureSetSkillLoss); + Lua::registerMethod(L, "Creature", "getPosition", CreatureFunctions::luaCreatureGetPosition); + Lua::registerMethod(L, "Creature", "getTile", CreatureFunctions::luaCreatureGetTile); + Lua::registerMethod(L, "Creature", "getDirection", CreatureFunctions::luaCreatureGetDirection); + Lua::registerMethod(L, "Creature", "setDirection", CreatureFunctions::luaCreatureSetDirection); + Lua::registerMethod(L, "Creature", "getHealth", CreatureFunctions::luaCreatureGetHealth); + Lua::registerMethod(L, "Creature", "setHealth", CreatureFunctions::luaCreatureSetHealth); + Lua::registerMethod(L, "Creature", "addHealth", CreatureFunctions::luaCreatureAddHealth); + Lua::registerMethod(L, "Creature", "getMaxHealth", CreatureFunctions::luaCreatureGetMaxHealth); + Lua::registerMethod(L, "Creature", "setMaxHealth", CreatureFunctions::luaCreatureSetMaxHealth); + Lua::registerMethod(L, "Creature", "setHiddenHealth", CreatureFunctions::luaCreatureSetHiddenHealth); + Lua::registerMethod(L, "Creature", "isMoveLocked", CreatureFunctions::luaCreatureIsMoveLocked); + Lua::registerMethod(L, "Creature", "isDirectionLocked", CreatureFunctions::luaCreatureIsDirectionLocked); + Lua::registerMethod(L, "Creature", "setMoveLocked", CreatureFunctions::luaCreatureSetMoveLocked); + Lua::registerMethod(L, "Creature", "setDirectionLocked", CreatureFunctions::luaCreatureSetDirectionLocked); + Lua::registerMethod(L, "Creature", "getSkull", CreatureFunctions::luaCreatureGetSkull); + Lua::registerMethod(L, "Creature", "setSkull", CreatureFunctions::luaCreatureSetSkull); + Lua::registerMethod(L, "Creature", "getOutfit", CreatureFunctions::luaCreatureGetOutfit); + Lua::registerMethod(L, "Creature", "setOutfit", CreatureFunctions::luaCreatureSetOutfit); + Lua::registerMethod(L, "Creature", "getCondition", CreatureFunctions::luaCreatureGetCondition); + Lua::registerMethod(L, "Creature", "addCondition", CreatureFunctions::luaCreatureAddCondition); + Lua::registerMethod(L, "Creature", "removeCondition", CreatureFunctions::luaCreatureRemoveCondition); + Lua::registerMethod(L, "Creature", "hasCondition", CreatureFunctions::luaCreatureHasCondition); + Lua::registerMethod(L, "Creature", "remove", CreatureFunctions::luaCreatureRemove); + Lua::registerMethod(L, "Creature", "teleportTo", CreatureFunctions::luaCreatureTeleportTo); + Lua::registerMethod(L, "Creature", "say", CreatureFunctions::luaCreatureSay); + Lua::registerMethod(L, "Creature", "getDamageMap", CreatureFunctions::luaCreatureGetDamageMap); + Lua::registerMethod(L, "Creature", "getSummons", CreatureFunctions::luaCreatureGetSummons); + Lua::registerMethod(L, "Creature", "hasBeenSummoned", CreatureFunctions::luaCreatureHasBeenSummoned); + Lua::registerMethod(L, "Creature", "getDescription", CreatureFunctions::luaCreatureGetDescription); + Lua::registerMethod(L, "Creature", "getPathTo", CreatureFunctions::luaCreatureGetPathTo); + Lua::registerMethod(L, "Creature", "move", CreatureFunctions::luaCreatureMove); + Lua::registerMethod(L, "Creature", "getZoneType", CreatureFunctions::luaCreatureGetZoneType); + Lua::registerMethod(L, "Creature", "getZones", CreatureFunctions::luaCreatureGetZones); + Lua::registerMethod(L, "Creature", "setIcon", CreatureFunctions::luaCreatureSetIcon); + Lua::registerMethod(L, "Creature", "getIcon", CreatureFunctions::luaCreatureGetIcon); + Lua::registerMethod(L, "Creature", "getIcons", CreatureFunctions::luaCreatureGetIcons); + Lua::registerMethod(L, "Creature", "removeIcon", CreatureFunctions::luaCreatureRemoveIcon); + Lua::registerMethod(L, "Creature", "clearIcons", CreatureFunctions::luaCreatureClearIcons); + + CombatFunctions::init(L); + MonsterFunctions::init(L); + NpcFunctions::init(L); + PlayerFunctions::init(L); +} int CreatureFunctions::luaCreatureCreate(lua_State* L) { // Creature(id or name or userdata) std::shared_ptr creature; - if (isNumber(L, 2)) { - creature = g_game().getCreatureByID(getNumber(L, 2)); - } else if (isString(L, 2)) { - creature = g_game().getCreatureByName(getString(L, 2)); - } else if (isUserdata(L, 2)) { - const LuaData_t type = getUserdataType(L, 2); - if (type != LuaData_t::Player && type != LuaData_t::Monster && type != LuaData_t::Npc) { + if (Lua::isNumber(L, 2)) { + creature = g_game().getCreatureByID(Lua::getNumber(L, 2)); + } else if (Lua::isString(L, 2)) { + creature = g_game().getCreatureByName(Lua::getString(L, 2)); + } else if (Lua::isUserdata(L, 2)) { + using enum LuaData_t; + const LuaData_t type = Lua::getUserdataType(L, 2); + if (type != Player && type != Monster && type != Npc) { lua_pushnil(L); return 1; } - creature = getUserdataShared(L, 2); + creature = Lua::getUserdataShared(L, 2); } else { creature = nullptr; } if (creature) { - pushUserdata(L, creature); - setCreatureMetatable(L, -1, creature); + Lua::pushUserdata(L, creature); + Lua::setCreatureMetatable(L, -1, creature); } else { lua_pushnil(L); } @@ -46,19 +125,19 @@ int CreatureFunctions::luaCreatureCreate(lua_State* L) { int CreatureFunctions::luaCreatureGetEvents(lua_State* L) { // creature:getEvents(type) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } - const CreatureEventType_t eventType = getNumber(L, 2); + const CreatureEventType_t eventType = Lua::getNumber(L, 2); const auto eventList = creature->getCreatureEvents(eventType); lua_createtable(L, static_cast(eventList.size()), 0); int index = 0; for (const auto &eventPtr : eventList) { - pushString(L, eventPtr->getName()); + Lua::pushString(L, eventPtr->getName()); lua_rawseti(L, -2, ++index); } return 1; @@ -66,10 +145,10 @@ int CreatureFunctions::luaCreatureGetEvents(lua_State* L) { int CreatureFunctions::luaCreatureRegisterEvent(lua_State* L) { // creature:registerEvent(name) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - const std::string &name = getString(L, 2); - pushBoolean(L, creature->registerCreatureEvent(name)); + const std::string &name = Lua::getString(L, 2); + Lua::pushBoolean(L, creature->registerCreatureEvent(name)); } else { lua_pushnil(L); } @@ -78,10 +157,10 @@ int CreatureFunctions::luaCreatureRegisterEvent(lua_State* L) { int CreatureFunctions::luaCreatureUnregisterEvent(lua_State* L) { // creature:unregisterEvent(name) - const std::string &name = getString(L, 2); - const auto &creature = getUserdataShared(L, 1); + const std::string &name = Lua::getString(L, 2); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushBoolean(L, creature->unregisterCreatureEvent(name)); + Lua::pushBoolean(L, creature->unregisterCreatureEvent(name)); } else { lua_pushnil(L); } @@ -90,9 +169,9 @@ int CreatureFunctions::luaCreatureUnregisterEvent(lua_State* L) { int CreatureFunctions::luaCreatureIsRemoved(lua_State* L) { // creature:isRemoved() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushBoolean(L, creature->isRemoved()); + Lua::pushBoolean(L, creature->isRemoved()); } else { lua_pushnil(L); } @@ -101,15 +180,15 @@ int CreatureFunctions::luaCreatureIsRemoved(lua_State* L) { int CreatureFunctions::luaCreatureIsCreature(lua_State* L) { // creature:isCreature() - pushBoolean(L, getUserdataShared(L, 1) != nullptr); + Lua::pushBoolean(L, Lua::getUserdataShared(L, 1) != nullptr); return 1; } int CreatureFunctions::luaCreatureIsInGhostMode(lua_State* L) { // creature:isInGhostMode() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushBoolean(L, creature->isInGhostMode()); + Lua::pushBoolean(L, creature->isInGhostMode()); } else { lua_pushnil(L); } @@ -118,9 +197,9 @@ int CreatureFunctions::luaCreatureIsInGhostMode(lua_State* L) { int CreatureFunctions::luaCreatureIsHealthHidden(lua_State* L) { // creature:isHealthHidden() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushBoolean(L, creature->isHealthHidden()); + Lua::pushBoolean(L, creature->isHealthHidden()); } else { lua_pushnil(L); } @@ -129,10 +208,10 @@ int CreatureFunctions::luaCreatureIsHealthHidden(lua_State* L) { int CreatureFunctions::luaCreatureCanSee(lua_State* L) { // creature:canSee(position) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - const Position &position = getPosition(L, 2); - pushBoolean(L, creature->canSee(position)); + const Position &position = Lua::getPosition(L, 2); + Lua::pushBoolean(L, creature->canSee(position)); } else { lua_pushnil(L); } @@ -141,10 +220,10 @@ int CreatureFunctions::luaCreatureCanSee(lua_State* L) { int CreatureFunctions::luaCreatureCanSeeCreature(lua_State* L) { // creature:canSeeCreature(creature) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - const auto &otherCreature = getCreature(L, 2); - pushBoolean(L, creature->canSeeCreature(otherCreature)); + const auto &otherCreature = Lua::getCreature(L, 2); + Lua::pushBoolean(L, creature->canSeeCreature(otherCreature)); } else { lua_pushnil(L); } @@ -153,7 +232,7 @@ int CreatureFunctions::luaCreatureCanSeeCreature(lua_State* L) { int CreatureFunctions::luaCreatureGetParent(lua_State* L) { // creature:getParent() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; @@ -165,13 +244,13 @@ int CreatureFunctions::luaCreatureGetParent(lua_State* L) { return 1; } - pushCylinder(L, parent); + Lua::pushCylinder(L, parent); return 1; } int CreatureFunctions::luaCreatureGetId(lua_State* L) { // creature:getId() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { lua_pushnumber(L, creature->getID()); } else { @@ -182,9 +261,9 @@ int CreatureFunctions::luaCreatureGetId(lua_State* L) { int CreatureFunctions::luaCreatureGetName(lua_State* L) { // creature:getName() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushString(L, creature->getName()); + Lua::pushString(L, creature->getName()); } else { lua_pushnil(L); } @@ -193,9 +272,9 @@ int CreatureFunctions::luaCreatureGetName(lua_State* L) { int CreatureFunctions::luaCreatureGetTypeName(lua_State* L) { // creature:getTypeName() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushString(L, creature->getTypeName()); + Lua::pushString(L, creature->getTypeName()); } else { lua_pushnil(L); } @@ -204,7 +283,7 @@ int CreatureFunctions::luaCreatureGetTypeName(lua_State* L) { int CreatureFunctions::luaCreatureGetTarget(lua_State* L) { // creature:getTarget() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; @@ -212,8 +291,8 @@ int CreatureFunctions::luaCreatureGetTarget(lua_State* L) { const auto &target = creature->getAttackedCreature(); if (target) { - pushUserdata(L, target); - setCreatureMetatable(L, -1, target); + Lua::pushUserdata(L, target); + Lua::setCreatureMetatable(L, -1, target); } else { lua_pushnil(L); } @@ -222,10 +301,10 @@ int CreatureFunctions::luaCreatureGetTarget(lua_State* L) { int CreatureFunctions::luaCreatureSetTarget(lua_State* L) { // creature:setTarget(target) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - const auto &target = getCreature(L, 2); - pushBoolean(L, creature->setAttackedCreature(target)); + const auto &target = Lua::getCreature(L, 2); + Lua::pushBoolean(L, creature->setAttackedCreature(target)); } else { lua_pushnil(L); } @@ -234,7 +313,7 @@ int CreatureFunctions::luaCreatureSetTarget(lua_State* L) { int CreatureFunctions::luaCreatureGetFollowCreature(lua_State* L) { // creature:getFollowCreature() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; @@ -242,8 +321,8 @@ int CreatureFunctions::luaCreatureGetFollowCreature(lua_State* L) { const auto &followCreature = creature->getFollowCreature(); if (followCreature) { - pushUserdata(L, followCreature); - setCreatureMetatable(L, -1, followCreature); + Lua::pushUserdata(L, followCreature); + Lua::setCreatureMetatable(L, -1, followCreature); } else { lua_pushnil(L); } @@ -252,10 +331,10 @@ int CreatureFunctions::luaCreatureGetFollowCreature(lua_State* L) { int CreatureFunctions::luaCreatureSetFollowCreature(lua_State* L) { // creature:setFollowCreature(followedCreature) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - const auto &followCreature = getCreature(L, 2); - pushBoolean(L, creature->setFollowCreature(followCreature)); + const auto &followCreature = Lua::getCreature(L, 2); + Lua::pushBoolean(L, creature->setFollowCreature(followCreature)); } else { lua_pushnil(L); } @@ -264,7 +343,7 @@ int CreatureFunctions::luaCreatureSetFollowCreature(lua_State* L) { int CreatureFunctions::luaCreatureGetMaster(lua_State* L) { // creature:getMaster() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; @@ -276,33 +355,33 @@ int CreatureFunctions::luaCreatureGetMaster(lua_State* L) { return 1; } - pushUserdata(L, master); - setCreatureMetatable(L, -1, master); + Lua::pushUserdata(L, master); + Lua::setCreatureMetatable(L, -1, master); return 1; } int CreatureFunctions::luaCreatureReload(lua_State* L) { // creature:reload() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } g_game().reloadCreature(creature); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CreatureFunctions::luaCreatureSetMaster(lua_State* L) { // creature:setMaster(master) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } - pushBoolean(L, creature->setMaster(getCreature(L, 2), true)); + Lua::pushBoolean(L, creature->setMaster(Lua::getCreature(L, 2), true)); // Reloading creature icon/knownCreature CreatureFunctions::luaCreatureReload(L); return 1; @@ -310,7 +389,7 @@ int CreatureFunctions::luaCreatureSetMaster(lua_State* L) { int CreatureFunctions::luaCreatureGetLight(lua_State* L) { // creature:getLight() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; @@ -324,24 +403,24 @@ int CreatureFunctions::luaCreatureGetLight(lua_State* L) { int CreatureFunctions::luaCreatureSetLight(lua_State* L) { // creature:setLight(color, level) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } LightInfo light; - light.color = getNumber(L, 2); - light.level = getNumber(L, 3); + light.color = Lua::getNumber(L, 2); + light.level = Lua::getNumber(L, 3); creature->setCreatureLight(light); g_game().changeLight(creature); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CreatureFunctions::luaCreatureGetSpeed(lua_State* L) { // creature:getSpeed() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { lua_pushnumber(L, creature->getSpeed()); } else { @@ -352,22 +431,22 @@ int CreatureFunctions::luaCreatureGetSpeed(lua_State* L) { int CreatureFunctions::luaCreatureSetSpeed(lua_State* L) { // creature:setSpeed(speed) - const auto &creature = getCreature(L, 1); + const auto &creature = Lua::getCreature(L, 1); if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const int32_t speed = getNumber(L, 2); + const int32_t speed = Lua::getNumber(L, 2); g_game().setCreatureSpeed(creature, speed); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CreatureFunctions::luaCreatureGetBaseSpeed(lua_State* L) { // creature:getBaseSpeed() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { lua_pushnumber(L, creature->getBaseSpeed()); } else { @@ -378,25 +457,25 @@ int CreatureFunctions::luaCreatureGetBaseSpeed(lua_State* L) { int CreatureFunctions::luaCreatureChangeSpeed(lua_State* L) { // creature:changeSpeed(delta) - const auto &creature = getCreature(L, 1); + const auto &creature = Lua::getCreature(L, 1); if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const int32_t delta = getNumber(L, 2); + const int32_t delta = Lua::getNumber(L, 2); g_game().changeSpeed(creature, delta); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CreatureFunctions::luaCreatureSetDropLoot(lua_State* L) { // creature:setDropLoot(doDrop) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - creature->setDropLoot(getBoolean(L, 2)); - pushBoolean(L, true); + creature->setDropLoot(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -405,10 +484,10 @@ int CreatureFunctions::luaCreatureSetDropLoot(lua_State* L) { int CreatureFunctions::luaCreatureSetSkillLoss(lua_State* L) { // creature:setSkillLoss(skillLoss) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - creature->setSkillLoss(getBoolean(L, 2)); - pushBoolean(L, true); + creature->setSkillLoss(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -416,10 +495,10 @@ int CreatureFunctions::luaCreatureSetSkillLoss(lua_State* L) { } int CreatureFunctions::luaCreatureGetPosition(lua_State* L) { - // creature:getPosition() - const auto &creature = getUserdataShared(L, 1); + // creature:Lua::getPosition() + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushPosition(L, creature->getPosition()); + Lua::pushPosition(L, creature->getPosition()); } else { lua_pushnil(L); } @@ -428,7 +507,7 @@ int CreatureFunctions::luaCreatureGetPosition(lua_State* L) { int CreatureFunctions::luaCreatureGetTile(lua_State* L) { // creature:getTile() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; @@ -436,8 +515,8 @@ int CreatureFunctions::luaCreatureGetTile(lua_State* L) { const auto &tile = creature->getTile(); if (tile) { - pushUserdata(L, tile); - setMetatable(L, -1, "Tile"); + Lua::pushUserdata(L, tile); + Lua::setMetatable(L, -1, "Tile"); } else { lua_pushnil(L); } @@ -446,7 +525,7 @@ int CreatureFunctions::luaCreatureGetTile(lua_State* L) { int CreatureFunctions::luaCreatureGetDirection(lua_State* L) { // creature:getDirection() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { lua_pushnumber(L, creature->getDirection()); } else { @@ -457,9 +536,9 @@ int CreatureFunctions::luaCreatureGetDirection(lua_State* L) { int CreatureFunctions::luaCreatureSetDirection(lua_State* L) { // creature:setDirection(direction) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushBoolean(L, g_game().internalCreatureTurn(creature, getNumber(L, 2))); + Lua::pushBoolean(L, g_game().internalCreatureTurn(creature, Lua::getNumber(L, 2))); } else { lua_pushnil(L); } @@ -468,7 +547,7 @@ int CreatureFunctions::luaCreatureSetDirection(lua_State* L) { int CreatureFunctions::luaCreatureGetHealth(lua_State* L) { // creature:getHealth() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { lua_pushnumber(L, creature->getHealth()); } else { @@ -479,47 +558,47 @@ int CreatureFunctions::luaCreatureGetHealth(lua_State* L) { int CreatureFunctions::luaCreatureSetHealth(lua_State* L) { // creature:setHealth(health) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } - creature->health = std::min(getNumber(L, 2), creature->healthMax); + creature->health = std::min(Lua::getNumber(L, 2), creature->healthMax); g_game().addCreatureHealth(creature); const auto &player = creature->getPlayer(); if (player) { player->sendStats(); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CreatureFunctions::luaCreatureAddHealth(lua_State* L) { // creature:addHealth(healthChange, combatType) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } CombatDamage damage; - damage.primary.value = getNumber(L, 2); + damage.primary.value = Lua::getNumber(L, 2); if (damage.primary.value >= 0) { damage.primary.type = COMBAT_HEALING; } else if (damage.primary.value < 0) { - damage.primary.type = getNumber(L, 3); + damage.primary.type = Lua::getNumber(L, 3); } else { damage.primary.type = COMBAT_UNDEFINEDDAMAGE; } - pushBoolean(L, g_game().combatChangeHealth(nullptr, creature, damage)); + Lua::pushBoolean(L, g_game().combatChangeHealth(nullptr, creature, damage)); return 1; } int CreatureFunctions::luaCreatureGetMaxHealth(lua_State* L) { // creature:getMaxHealth() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { lua_pushnumber(L, creature->getMaxHealth()); } else { @@ -530,13 +609,13 @@ int CreatureFunctions::luaCreatureGetMaxHealth(lua_State* L) { int CreatureFunctions::luaCreatureSetMaxHealth(lua_State* L) { // creature:setMaxHealth(maxHealth) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } - creature->healthMax = getNumber(L, 2); + creature->healthMax = Lua::getNumber(L, 2); creature->health = std::min(creature->health, creature->healthMax); g_game().addCreatureHealth(creature); @@ -544,17 +623,17 @@ int CreatureFunctions::luaCreatureSetMaxHealth(lua_State* L) { if (player) { player->sendStats(); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CreatureFunctions::luaCreatureSetHiddenHealth(lua_State* L) { // creature:setHiddenHealth(hide) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - creature->setHiddenHealth(getBoolean(L, 2)); + creature->setHiddenHealth(Lua::getBoolean(L, 2)); g_game().addCreatureHealth(creature); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -563,9 +642,9 @@ int CreatureFunctions::luaCreatureSetHiddenHealth(lua_State* L) { int CreatureFunctions::luaCreatureIsMoveLocked(lua_State* L) { // creature:isMoveLocked() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushBoolean(L, creature->isMoveLocked()); + Lua::pushBoolean(L, creature->isMoveLocked()); } else { lua_pushnil(L); } @@ -574,10 +653,10 @@ int CreatureFunctions::luaCreatureIsMoveLocked(lua_State* L) { int CreatureFunctions::luaCreatureSetMoveLocked(lua_State* L) { // creature:setMoveLocked(moveLocked) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - creature->setMoveLocked(getBoolean(L, 2)); - pushBoolean(L, true); + creature->setMoveLocked(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -586,9 +665,9 @@ int CreatureFunctions::luaCreatureSetMoveLocked(lua_State* L) { int CreatureFunctions::luaCreatureIsDirectionLocked(lua_State* L) { // creature:isDirectionLocked() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushBoolean(L, creature->isDirectionLocked()); + Lua::pushBoolean(L, creature->isDirectionLocked()); } else { lua_pushnil(L); } @@ -597,10 +676,10 @@ int CreatureFunctions::luaCreatureIsDirectionLocked(lua_State* L) { int CreatureFunctions::luaCreatureSetDirectionLocked(lua_State* L) { // creature:setDirectionLocked(directionLocked) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - creature->setDirectionLocked(getBoolean(L, 2)); - pushBoolean(L, true); + creature->setDirectionLocked(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -609,7 +688,7 @@ int CreatureFunctions::luaCreatureSetDirectionLocked(lua_State* L) { int CreatureFunctions::luaCreatureGetSkull(lua_State* L) { // creature:getSkull() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { lua_pushnumber(L, creature->getSkull()); } else { @@ -620,10 +699,10 @@ int CreatureFunctions::luaCreatureGetSkull(lua_State* L) { int CreatureFunctions::luaCreatureSetSkull(lua_State* L) { // creature:setSkull(skull) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - creature->setSkull(getNumber(L, 2)); - pushBoolean(L, true); + creature->setSkull(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -631,10 +710,10 @@ int CreatureFunctions::luaCreatureSetSkull(lua_State* L) { } int CreatureFunctions::luaCreatureGetOutfit(lua_State* L) { - // creature:getOutfit() - const auto &creature = getUserdataShared(L, 1); + // creature:Lua::getOutfit() + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushOutfit(L, creature->getCurrentOutfit()); + Lua::pushOutfit(L, creature->getCurrentOutfit()); } else { lua_pushnil(L); } @@ -643,9 +722,9 @@ int CreatureFunctions::luaCreatureGetOutfit(lua_State* L) { int CreatureFunctions::luaCreatureSetOutfit(lua_State* L) { // creature:setOutfit(outfit) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - Outfit_t outfit = getOutfit(L, 2); + Outfit_t outfit = Lua::getOutfit(L, 2); if (g_configManager().getBoolean(WARN_UNSAFE_SCRIPTS) && outfit.lookType != 0 && !g_game().isLookTypeRegistered(outfit.lookType)) { g_logger().warn("[CreatureFunctions::luaCreatureSetOutfit] An unregistered creature looktype type with id '{}' was blocked to prevent client crash.", outfit.lookType); return 1; @@ -653,7 +732,7 @@ int CreatureFunctions::luaCreatureSetOutfit(lua_State* L) { creature->defaultOutfit = outfit; g_game().internalCreatureChangeOutfit(creature, creature->defaultOutfit); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -662,20 +741,20 @@ int CreatureFunctions::luaCreatureSetOutfit(lua_State* L) { int CreatureFunctions::luaCreatureGetCondition(lua_State* L) { // creature:getCondition(conditionType[, conditionId = CONDITIONID_COMBAT[, subId = 0]]) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } - const ConditionType_t conditionType = getNumber(L, 2); - const auto conditionId = getNumber(L, 3, CONDITIONID_COMBAT); - const auto subId = getNumber(L, 4, 0); + const ConditionType_t conditionType = Lua::getNumber(L, 2); + const auto conditionId = Lua::getNumber(L, 3, CONDITIONID_COMBAT); + const auto subId = Lua::getNumber(L, 4, 0); const auto &condition = creature->getCondition(conditionType, conditionId, subId); if (condition) { - pushUserdata(L, condition); - setWeakMetatable(L, -1, "Condition"); + Lua::pushUserdata(L, condition); + Lua::setWeakMetatable(L, -1, "Condition"); } else { lua_pushnil(L); } @@ -684,10 +763,10 @@ int CreatureFunctions::luaCreatureGetCondition(lua_State* L) { int CreatureFunctions::luaCreatureAddCondition(lua_State* L) { // creature:addCondition(condition) - const auto &creature = getUserdataShared(L, 1); - const auto &condition = getUserdataShared(L, 2); + const auto &creature = Lua::getUserdataShared(L, 1); + const auto &condition = Lua::getUserdataShared(L, 2); if (creature && condition) { - pushBoolean(L, creature->addCondition(condition->clone())); + Lua::pushBoolean(L, creature->addCondition(condition->clone())); } else { lua_pushnil(L); } @@ -696,24 +775,24 @@ int CreatureFunctions::luaCreatureAddCondition(lua_State* L) { int CreatureFunctions::luaCreatureRemoveCondition(lua_State* L) { // creature:removeCondition(conditionType[, conditionId = CONDITIONID_COMBAT[, subId = 0[, force = false]]]) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } - const ConditionType_t conditionType = getNumber(L, 2); - const auto conditionId = getNumber(L, 3, CONDITIONID_COMBAT); - const auto subId = getNumber(L, 4, 0); + const ConditionType_t conditionType = Lua::getNumber(L, 2); + const auto conditionId = Lua::getNumber(L, 3, CONDITIONID_COMBAT); + const auto subId = Lua::getNumber(L, 4, 0); const auto &condition = creature->getCondition(conditionType, conditionId, subId); if (condition) { - const bool force = getBoolean(L, 5, false); + const bool force = Lua::getBoolean(L, 5, false); if (subId == 0) { creature->removeCondition(conditionType, conditionId, force); } else { creature->removeCondition(condition); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -722,30 +801,30 @@ int CreatureFunctions::luaCreatureRemoveCondition(lua_State* L) { int CreatureFunctions::luaCreatureHasCondition(lua_State* L) { // creature:hasCondition(conditionType[, subId = 0]) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } - const ConditionType_t conditionType = getNumber(L, 2); - const auto subId = getNumber(L, 3, 0); - pushBoolean(L, creature->hasCondition(conditionType, subId)); + const ConditionType_t conditionType = Lua::getNumber(L, 2); + const auto subId = Lua::getNumber(L, 3, 0); + Lua::pushBoolean(L, creature->hasCondition(conditionType, subId)); return 1; } int CreatureFunctions::luaCreatureIsImmune(lua_State* L) { // creature:isImmune(condition or conditionType) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } - if (isNumber(L, 2)) { - pushBoolean(L, creature->isImmune(getNumber(L, 2))); - } else if (const auto condition = getUserdataShared(L, 2)) { - pushBoolean(L, creature->isImmune(condition->getType())); + if (Lua::isNumber(L, 2)) { + Lua::pushBoolean(L, creature->isImmune(Lua::getNumber(L, 2))); + } else if (const auto condition = Lua::getUserdataShared(L, 2)) { + Lua::pushBoolean(L, creature->isImmune(condition->getType())); } else { lua_pushnil(L); } @@ -754,7 +833,7 @@ int CreatureFunctions::luaCreatureIsImmune(lua_State* L) { int CreatureFunctions::luaCreatureRemove(lua_State* L) { // creature:remove([forced = true]) - auto* creaturePtr = getRawUserDataShared(L, 1); + auto* creaturePtr = Lua::getRawUserDataShared(L, 1); if (!creaturePtr) { lua_pushnil(L); return 1; @@ -766,7 +845,7 @@ int CreatureFunctions::luaCreatureRemove(lua_State* L) { return 1; } - const bool forced = getBoolean(L, 2, true); + const bool forced = Lua::getBoolean(L, 2, true); if (const auto &player = creature->getPlayer()) { if (forced) { player->removePlayer(true); @@ -778,19 +857,19 @@ int CreatureFunctions::luaCreatureRemove(lua_State* L) { } *creaturePtr = nullptr; - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CreatureFunctions::luaCreatureTeleportTo(lua_State* L) { // creature:teleportTo(position[, pushMovement = false]) - const bool pushMovement = getBoolean(L, 3, false); + const bool pushMovement = Lua::getBoolean(L, 3, false); - const Position &position = getPosition(L, 2); - const auto &creature = getUserdataShared(L, 1); + const Position &position = Lua::getPosition(L, 2); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature == nullptr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -798,7 +877,7 @@ int CreatureFunctions::luaCreatureTeleportTo(lua_State* L) { if (const auto ret = g_game().internalTeleport(creature, position, pushMovement); ret != RETURNVALUE_NOERROR) { g_logger().debug("[{}] Failed to teleport creature {}, on position {}, error code: {}", __FUNCTION__, creature->getName(), oldPosition.toString(), getReturnMessage(ret)); - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } @@ -815,7 +894,7 @@ int CreatureFunctions::luaCreatureTeleportTo(lua_State* L) { g_game().internalCreatureTurn(creature, DIRECTION_EAST); } } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -825,24 +904,24 @@ int CreatureFunctions::luaCreatureSay(lua_State* L) { Position position; if (parameters >= 6) { - position = getPosition(L, 6); + position = Lua::getPosition(L, 6); if (!position.x || !position.y) { - reportErrorFunc("Invalid position specified."); - pushBoolean(L, false); + Lua::reportErrorFunc("Invalid position specified."); + Lua::pushBoolean(L, false); return 1; } } std::shared_ptr target = nullptr; if (parameters >= 5) { - target = getCreature(L, 5); + target = Lua::getCreature(L, 5); } - const bool ghost = getBoolean(L, 4, false); + const bool ghost = Lua::getBoolean(L, 4, false); - const auto type = getNumber(L, 3, TALKTYPE_MONSTER_SAY); - const std::string &text = getString(L, 2); - const auto &creature = getUserdataShared(L, 1); + const auto type = Lua::getNumber(L, 3, TALKTYPE_MONSTER_SAY); + const std::string &text = Lua::getString(L, 2); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; @@ -854,16 +933,16 @@ int CreatureFunctions::luaCreatureSay(lua_State* L) { } if (position.x != 0) { - pushBoolean(L, g_game().internalCreatureSay(creature, type, text, ghost, &spectators, &position)); + Lua::pushBoolean(L, g_game().internalCreatureSay(creature, type, text, ghost, &spectators, &position)); } else { - pushBoolean(L, g_game().internalCreatureSay(creature, type, text, ghost, &spectators)); + Lua::pushBoolean(L, g_game().internalCreatureSay(creature, type, text, ghost, &spectators)); } return 1; } int CreatureFunctions::luaCreatureGetDamageMap(lua_State* L) { // creature:getDamageMap() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; @@ -872,8 +951,8 @@ int CreatureFunctions::luaCreatureGetDamageMap(lua_State* L) { lua_createtable(L, creature->damageMap.size(), 0); for (const auto damageEntry : creature->damageMap) { lua_createtable(L, 0, 2); - setField(L, "total", damageEntry.second.total); - setField(L, "ticks", damageEntry.second.ticks); + Lua::setField(L, "total", damageEntry.second.total); + Lua::setField(L, "ticks", damageEntry.second.ticks); lua_rawseti(L, -2, damageEntry.first); } return 1; @@ -881,7 +960,7 @@ int CreatureFunctions::luaCreatureGetDamageMap(lua_State* L) { int CreatureFunctions::luaCreatureGetSummons(lua_State* L) { // creature:getSummons() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; @@ -892,8 +971,8 @@ int CreatureFunctions::luaCreatureGetSummons(lua_State* L) { int index = 0; for (const auto &summon : creature->getSummons()) { if (summon) { - pushUserdata(L, summon); - setCreatureMetatable(L, -1, summon); + Lua::pushUserdata(L, summon); + Lua::setCreatureMetatable(L, -1, summon); lua_rawseti(L, -2, ++index); } } @@ -902,9 +981,9 @@ int CreatureFunctions::luaCreatureGetSummons(lua_State* L) { int CreatureFunctions::luaCreatureHasBeenSummoned(lua_State* L) { // creature:hasBeenSummoned() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushBoolean(L, creature->hasBeenSummoned()); + Lua::pushBoolean(L, creature->hasBeenSummoned()); } else { lua_pushnil(L); } @@ -914,10 +993,10 @@ int CreatureFunctions::luaCreatureHasBeenSummoned(lua_State* L) { int CreatureFunctions::luaCreatureGetDescription(lua_State* L) { // creature:getDescription(distance) - const int32_t distance = getNumber(L, 2); - const auto &creature = getUserdataShared(L, 1); + const int32_t distance = Lua::getNumber(L, 2); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { - pushString(L, creature->getDescription(distance)); + Lua::pushString(L, creature->getDescription(distance)); } else { lua_pushnil(L); } @@ -926,20 +1005,20 @@ int CreatureFunctions::luaCreatureGetDescription(lua_State* L) { int CreatureFunctions::luaCreatureGetPathTo(lua_State* L) { // creature:getPathTo(pos[, minTargetDist = 0[, maxTargetDist = 1[, fullPathSearch = true[, clearSight = true[, maxSearchDist = 0]]]]]) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } - const Position &position = getPosition(L, 2); + const Position &position = Lua::getPosition(L, 2); FindPathParams fpp; - fpp.minTargetDist = getNumber(L, 3, 0); - fpp.maxTargetDist = getNumber(L, 4, 1); - fpp.fullPathSearch = getBoolean(L, 5, fpp.fullPathSearch); - fpp.clearSight = getBoolean(L, 6, fpp.clearSight); - fpp.maxSearchDist = getNumber(L, 7, fpp.maxSearchDist); + fpp.minTargetDist = Lua::getNumber(L, 3, 0); + fpp.maxTargetDist = Lua::getNumber(L, 4, 1); + fpp.fullPathSearch = Lua::getBoolean(L, 5, fpp.fullPathSearch); + fpp.clearSight = Lua::getBoolean(L, 6, fpp.clearSight); + fpp.maxSearchDist = Lua::getNumber(L, 7, fpp.maxSearchDist); std::vector dirList; if (creature->getPathTo(position, dirList, fpp)) { @@ -951,7 +1030,7 @@ int CreatureFunctions::luaCreatureGetPathTo(lua_State* L) { lua_rawseti(L, -2, ++index); } } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } @@ -959,33 +1038,33 @@ int CreatureFunctions::luaCreatureGetPathTo(lua_State* L) { int CreatureFunctions::luaCreatureMove(lua_State* L) { // creature:move(direction) // creature:move(tile[, flags = 0]) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { lua_pushnil(L); return 1; } - if (isNumber(L, 2)) { - const Direction direction = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + const Direction direction = Lua::getNumber(L, 2); if (direction > DIRECTION_LAST) { lua_pushnil(L); return 1; } lua_pushnumber(L, g_game().internalMoveCreature(creature, direction, FLAG_NOLIMIT)); } else { - const auto &tile = getUserdataShared(L, 2); + const auto &tile = Lua::getUserdataShared(L, 2); if (!tile) { lua_pushnil(L); return 1; } - lua_pushnumber(L, g_game().internalMoveCreature(creature, tile, getNumber(L, 3))); + lua_pushnumber(L, g_game().internalMoveCreature(creature, tile, Lua::getNumber(L, 3))); } return 1; } int CreatureFunctions::luaCreatureGetZoneType(lua_State* L) { // creature:getZoneType() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature) { lua_pushnumber(L, creature->getZoneType()); } else { @@ -996,7 +1075,7 @@ int CreatureFunctions::luaCreatureGetZoneType(lua_State* L) { int CreatureFunctions::luaCreatureGetZones(lua_State* L) { // creature:getZones() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (creature == nullptr) { lua_pushnil(L); return 1; @@ -1007,8 +1086,8 @@ int CreatureFunctions::luaCreatureGetZones(lua_State* L) { int index = 0; for (const auto &zone : zones) { index++; - pushUserdata(L, zone); - setMetatable(L, -1, "Zone"); + Lua::pushUserdata(L, zone); + Lua::setMetatable(L, -1, "Zone"); lua_rawseti(L, -2, index); } return 1; @@ -1016,35 +1095,35 @@ int CreatureFunctions::luaCreatureGetZones(lua_State* L) { int CreatureFunctions::luaCreatureSetIcon(lua_State* L) { // creature:setIcon(key, category, icon[, number]) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto key = getString(L, 2); - const auto category = getNumber(L, 3); - const auto count = getNumber(L, 5, 0); + const auto key = Lua::getString(L, 2); + const auto category = Lua::getNumber(L, 3); + const auto count = Lua::getNumber(L, 5, 0); CreatureIcon creatureIcon; if (category == CreatureIconCategory_t::Modifications) { - const auto icon = getNumber(L, 4); + const auto icon = Lua::getNumber(L, 4); creatureIcon = CreatureIcon(icon, count); } else { - const auto icon = getNumber(L, 4); + const auto icon = Lua::getNumber(L, 4); creatureIcon = CreatureIcon(icon, count); } creature->setIcon(key, creatureIcon); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CreatureFunctions::luaCreatureGetIcons(lua_State* L) { // creature:getIcons() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -1052,9 +1131,9 @@ int CreatureFunctions::luaCreatureGetIcons(lua_State* L) { lua_createtable(L, static_cast(icons.size()), 0); for (auto &icon : icons) { lua_createtable(L, 0, 3); - setField(L, "category", static_cast(icon.category)); - setField(L, "icon", icon.serialize()); - setField(L, "count", icon.count); + Lua::setField(L, "category", static_cast(icon.category)); + Lua::setField(L, "icon", icon.serialize()); + Lua::setField(L, "count", icon.count); lua_rawseti(L, -2, static_cast(icon.category)); } return 1; @@ -1062,19 +1141,19 @@ int CreatureFunctions::luaCreatureGetIcons(lua_State* L) { int CreatureFunctions::luaCreatureGetIcon(lua_State* L) { // creature:getIcon(key) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto key = getString(L, 2); + const auto key = Lua::getString(L, 2); auto icon = creature->getIcon(key); if (icon.isSet()) { lua_createtable(L, 0, 3); - setField(L, "category", static_cast(icon.category)); - setField(L, "icon", icon.serialize()); - setField(L, "count", icon.count); + Lua::setField(L, "category", static_cast(icon.category)); + Lua::setField(L, "icon", icon.serialize()); + Lua::setField(L, "count", icon.count); } else { lua_pushnil(L); } @@ -1083,27 +1162,27 @@ int CreatureFunctions::luaCreatureGetIcon(lua_State* L) { int CreatureFunctions::luaCreatureRemoveIcon(lua_State* L) { // creature:removeIcon(key) - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto key = getString(L, 2); + const auto key = Lua::getString(L, 2); creature->removeIcon(key); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int CreatureFunctions::luaCreatureClearIcons(lua_State* L) { // creature:clearIcons() - const auto &creature = getUserdataShared(L, 1); + const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } creature->clearIcons(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } diff --git a/src/lua/functions/creatures/creature_functions.hpp b/src/lua/functions/creatures/creature_functions.hpp index f0f1d12bb77..8d1c5b8ce52 100644 --- a/src/lua/functions/creatures/creature_functions.hpp +++ b/src/lua/functions/creatures/creature_functions.hpp @@ -13,92 +13,9 @@ #include "lua/functions/creatures/monster/monster_functions.hpp" #include "lua/functions/creatures/npc/npc_functions.hpp" #include "lua/functions/creatures/player/player_functions.hpp" -#include "lua/scripts/luascript.hpp" - -class CreatureFunctions final : LuaScriptInterface { +class CreatureFunctions { public: - explicit CreatureFunctions(lua_State* L) : - LuaScriptInterface("CreatureFunctions") { - init(L); - } - ~CreatureFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Creature", "", CreatureFunctions::luaCreatureCreate); - registerMetaMethod(L, "Creature", "__eq", CreatureFunctions::luaUserdataCompare); - registerMethod(L, "Creature", "getEvents", CreatureFunctions::luaCreatureGetEvents); - registerMethod(L, "Creature", "registerEvent", CreatureFunctions::luaCreatureRegisterEvent); - registerMethod(L, "Creature", "unregisterEvent", CreatureFunctions::luaCreatureUnregisterEvent); - registerMethod(L, "Creature", "isRemoved", CreatureFunctions::luaCreatureIsRemoved); - registerMethod(L, "Creature", "isCreature", CreatureFunctions::luaCreatureIsCreature); - registerMethod(L, "Creature", "isInGhostMode", CreatureFunctions::luaCreatureIsInGhostMode); - registerMethod(L, "Creature", "isHealthHidden", CreatureFunctions::luaCreatureIsHealthHidden); - registerMethod(L, "Creature", "isImmune", CreatureFunctions::luaCreatureIsImmune); - registerMethod(L, "Creature", "canSee", CreatureFunctions::luaCreatureCanSee); - registerMethod(L, "Creature", "canSeeCreature", CreatureFunctions::luaCreatureCanSeeCreature); - registerMethod(L, "Creature", "getParent", CreatureFunctions::luaCreatureGetParent); - registerMethod(L, "Creature", "getId", CreatureFunctions::luaCreatureGetId); - registerMethod(L, "Creature", "getName", CreatureFunctions::luaCreatureGetName); - registerMethod(L, "Creature", "getTypeName", CreatureFunctions::luaCreatureGetTypeName); - registerMethod(L, "Creature", "getTarget", CreatureFunctions::luaCreatureGetTarget); - registerMethod(L, "Creature", "setTarget", CreatureFunctions::luaCreatureSetTarget); - registerMethod(L, "Creature", "getFollowCreature", CreatureFunctions::luaCreatureGetFollowCreature); - registerMethod(L, "Creature", "setFollowCreature", CreatureFunctions::luaCreatureSetFollowCreature); - registerMethod(L, "Creature", "reload", CreatureFunctions::luaCreatureReload); - registerMethod(L, "Creature", "getMaster", CreatureFunctions::luaCreatureGetMaster); - registerMethod(L, "Creature", "setMaster", CreatureFunctions::luaCreatureSetMaster); - registerMethod(L, "Creature", "getLight", CreatureFunctions::luaCreatureGetLight); - registerMethod(L, "Creature", "setLight", CreatureFunctions::luaCreatureSetLight); - registerMethod(L, "Creature", "getSpeed", CreatureFunctions::luaCreatureGetSpeed); - registerMethod(L, "Creature", "setSpeed", CreatureFunctions::luaCreatureSetSpeed); - registerMethod(L, "Creature", "getBaseSpeed", CreatureFunctions::luaCreatureGetBaseSpeed); - registerMethod(L, "Creature", "changeSpeed", CreatureFunctions::luaCreatureChangeSpeed); - registerMethod(L, "Creature", "setDropLoot", CreatureFunctions::luaCreatureSetDropLoot); - registerMethod(L, "Creature", "setSkillLoss", CreatureFunctions::luaCreatureSetSkillLoss); - registerMethod(L, "Creature", "getPosition", CreatureFunctions::luaCreatureGetPosition); - registerMethod(L, "Creature", "getTile", CreatureFunctions::luaCreatureGetTile); - registerMethod(L, "Creature", "getDirection", CreatureFunctions::luaCreatureGetDirection); - registerMethod(L, "Creature", "setDirection", CreatureFunctions::luaCreatureSetDirection); - registerMethod(L, "Creature", "getHealth", CreatureFunctions::luaCreatureGetHealth); - registerMethod(L, "Creature", "setHealth", CreatureFunctions::luaCreatureSetHealth); - registerMethod(L, "Creature", "addHealth", CreatureFunctions::luaCreatureAddHealth); - registerMethod(L, "Creature", "getMaxHealth", CreatureFunctions::luaCreatureGetMaxHealth); - registerMethod(L, "Creature", "setMaxHealth", CreatureFunctions::luaCreatureSetMaxHealth); - registerMethod(L, "Creature", "setHiddenHealth", CreatureFunctions::luaCreatureSetHiddenHealth); - registerMethod(L, "Creature", "isMoveLocked", CreatureFunctions::luaCreatureIsMoveLocked); - registerMethod(L, "Creature", "isDirectionLocked", CreatureFunctions::luaCreatureIsDirectionLocked); - registerMethod(L, "Creature", "setMoveLocked", CreatureFunctions::luaCreatureSetMoveLocked); - registerMethod(L, "Creature", "setDirectionLocked", CreatureFunctions::luaCreatureSetDirectionLocked); - registerMethod(L, "Creature", "getSkull", CreatureFunctions::luaCreatureGetSkull); - registerMethod(L, "Creature", "setSkull", CreatureFunctions::luaCreatureSetSkull); - registerMethod(L, "Creature", "getOutfit", CreatureFunctions::luaCreatureGetOutfit); - registerMethod(L, "Creature", "setOutfit", CreatureFunctions::luaCreatureSetOutfit); - registerMethod(L, "Creature", "getCondition", CreatureFunctions::luaCreatureGetCondition); - registerMethod(L, "Creature", "addCondition", CreatureFunctions::luaCreatureAddCondition); - registerMethod(L, "Creature", "removeCondition", CreatureFunctions::luaCreatureRemoveCondition); - registerMethod(L, "Creature", "hasCondition", CreatureFunctions::luaCreatureHasCondition); - registerMethod(L, "Creature", "remove", CreatureFunctions::luaCreatureRemove); - registerMethod(L, "Creature", "teleportTo", CreatureFunctions::luaCreatureTeleportTo); - registerMethod(L, "Creature", "say", CreatureFunctions::luaCreatureSay); - registerMethod(L, "Creature", "getDamageMap", CreatureFunctions::luaCreatureGetDamageMap); - registerMethod(L, "Creature", "getSummons", CreatureFunctions::luaCreatureGetSummons); - registerMethod(L, "Creature", "hasBeenSummoned", CreatureFunctions::luaCreatureHasBeenSummoned); - registerMethod(L, "Creature", "getDescription", CreatureFunctions::luaCreatureGetDescription); - registerMethod(L, "Creature", "getPathTo", CreatureFunctions::luaCreatureGetPathTo); - registerMethod(L, "Creature", "move", CreatureFunctions::luaCreatureMove); - registerMethod(L, "Creature", "getZoneType", CreatureFunctions::luaCreatureGetZoneType); - registerMethod(L, "Creature", "getZones", CreatureFunctions::luaCreatureGetZones); - registerMethod(L, "Creature", "setIcon", CreatureFunctions::luaCreatureSetIcon); - registerMethod(L, "Creature", "getIcon", CreatureFunctions::luaCreatureGetIcon); - registerMethod(L, "Creature", "getIcons", CreatureFunctions::luaCreatureGetIcons); - registerMethod(L, "Creature", "removeIcon", CreatureFunctions::luaCreatureRemoveIcon); - registerMethod(L, "Creature", "clearIcons", CreatureFunctions::luaCreatureClearIcons); - - CombatFunctions::init(L); - MonsterFunctions::init(L); - NpcFunctions::init(L); - PlayerFunctions::init(L); - } + static void init(lua_State* L); private: static int luaCreatureCreate(lua_State* L); diff --git a/src/lua/functions/creatures/monster/charm_functions.cpp b/src/lua/functions/creatures/monster/charm_functions.cpp index 920f4891afe..88d27314431 100644 --- a/src/lua/functions/creatures/monster/charm_functions.cpp +++ b/src/lua/functions/creatures/monster/charm_functions.cpp @@ -11,17 +11,36 @@ #include "game/game.hpp" #include "io/iobestiary.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void CharmFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Charm", "", CharmFunctions::luaCharmCreate); + Lua::registerMetaMethod(L, "Charm", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Charm", "name", CharmFunctions::luaCharmName); + Lua::registerMethod(L, "Charm", "description", CharmFunctions::luaCharmDescription); + Lua::registerMethod(L, "Charm", "type", CharmFunctions::luaCharmType); + Lua::registerMethod(L, "Charm", "points", CharmFunctions::luaCharmPoints); + Lua::registerMethod(L, "Charm", "damageType", CharmFunctions::luaCharmDamageType); + Lua::registerMethod(L, "Charm", "percentage", CharmFunctions::luaCharmPercentage); + Lua::registerMethod(L, "Charm", "chance", CharmFunctions::luaCharmChance); + Lua::registerMethod(L, "Charm", "messageCancel", CharmFunctions::luaCharmMessageCancel); + Lua::registerMethod(L, "Charm", "messageServerLog", CharmFunctions::luaCharmMessageServerLog); + Lua::registerMethod(L, "Charm", "effect", CharmFunctions::luaCharmEffect); + Lua::registerMethod(L, "Charm", "castSound", CharmFunctions::luaCharmCastSound); + Lua::registerMethod(L, "Charm", "impactSound", CharmFunctions::luaCharmImpactSound); +} int CharmFunctions::luaCharmCreate(lua_State* L) { // charm(id) - if (isNumber(L, 2)) { - const charmRune_t charmid = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + const charmRune_t charmid = Lua::getNumber(L, 2); const auto charmList = g_game().getCharmList(); for (const auto &charm : charmList) { if (charm->id == charmid) { - pushUserdata(L, charm); - setMetatable(L, -1, "Charm"); - pushBoolean(L, true); + Lua::pushUserdata(L, charm); + Lua::setMetatable(L, -1, "Charm"); + Lua::pushBoolean(L, true); } } } @@ -32,144 +51,144 @@ int CharmFunctions::luaCharmCreate(lua_State* L) { int CharmFunctions::luaCharmName(lua_State* L) { // get: charm:name() set: charm:name(string) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { - pushString(L, charm->name); + Lua::pushString(L, charm->name); } else { - charm->name = getString(L, 2); - pushBoolean(L, true); + charm->name = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmDescription(lua_State* L) { // get: charm:description() set: charm:description(string) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { - pushString(L, charm->description); + Lua::pushString(L, charm->description); } else { - charm->description = getString(L, 2); - pushBoolean(L, true); + charm->description = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmType(lua_State* L) { // get: charm:type() set: charm:type(charm_t) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { lua_pushnumber(L, charm->type); } else { - charm->type = getNumber(L, 2); - pushBoolean(L, true); + charm->type = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmPoints(lua_State* L) { // get: charm:points() set: charm:points(value) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { lua_pushnumber(L, charm->points); } else { - charm->points = getNumber(L, 2); - pushBoolean(L, true); + charm->points = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmDamageType(lua_State* L) { // get: charm:damageType() set: charm:damageType(type) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { lua_pushnumber(L, charm->dmgtype); } else { - charm->dmgtype = getNumber(L, 2); - pushBoolean(L, true); + charm->dmgtype = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmPercentage(lua_State* L) { // get: charm:percentage() set: charm:percentage(value) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { lua_pushnumber(L, charm->percent); } else { - charm->percent = getNumber(L, 2); - pushBoolean(L, true); + charm->percent = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmChance(lua_State* L) { // get: charm:chance() set: charm:chance(value) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { lua_pushnumber(L, charm->chance); } else { - charm->chance = getNumber(L, 2); - pushBoolean(L, true); + charm->chance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmMessageCancel(lua_State* L) { // get: charm:messageCancel() set: charm:messageCancel(string) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { - pushString(L, charm->cancelMsg); + Lua::pushString(L, charm->cancelMsg); } else { - charm->cancelMsg = getString(L, 2); - pushBoolean(L, true); + charm->cancelMsg = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmMessageServerLog(lua_State* L) { // get: charm:messageServerLog() set: charm:messageServerLog(string) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { - pushString(L, charm->logMsg); + Lua::pushString(L, charm->logMsg); } else { - charm->logMsg = getString(L, 2); - pushBoolean(L, true); + charm->logMsg = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmEffect(lua_State* L) { // get: charm:effect() set: charm:effect(value) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { lua_pushnumber(L, charm->effect); } else { - charm->effect = getNumber(L, 2); - pushBoolean(L, true); + charm->effect = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmCastSound(lua_State* L) { // get: charm:castSound() set: charm:castSound(sound) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { lua_pushnumber(L, static_cast(charm->soundCastEffect)); } else { - charm->soundCastEffect = getNumber(L, 2); - pushBoolean(L, true); + charm->soundCastEffect = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int CharmFunctions::luaCharmImpactSound(lua_State* L) { // get: charm:impactSound() set: charm:impactSound(sound) - const auto &charm = getUserdataShared(L, 1); + const auto &charm = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { lua_pushnumber(L, static_cast(charm->soundImpactEffect)); } else { - charm->soundImpactEffect = getNumber(L, 2); - pushBoolean(L, true); + charm->soundImpactEffect = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } diff --git a/src/lua/functions/creatures/monster/charm_functions.hpp b/src/lua/functions/creatures/monster/charm_functions.hpp index 837c1c14fa7..10b9f96f124 100644 --- a/src/lua/functions/creatures/monster/charm_functions.hpp +++ b/src/lua/functions/creatures/monster/charm_functions.hpp @@ -9,33 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class CharmFunctions final : LuaScriptInterface { +class CharmFunctions { public: - explicit CharmFunctions(lua_State* L) : - LuaScriptInterface("CharmFunctions") { - init(L); - } - ~CharmFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Charm", "", CharmFunctions::luaCharmCreate); - registerMetaMethod(L, "Charm", "__eq", CharmFunctions::luaUserdataCompare); - - registerMethod(L, "Charm", "name", CharmFunctions::luaCharmName); - registerMethod(L, "Charm", "description", CharmFunctions::luaCharmDescription); - registerMethod(L, "Charm", "type", CharmFunctions::luaCharmType); - registerMethod(L, "Charm", "points", CharmFunctions::luaCharmPoints); - registerMethod(L, "Charm", "damageType", CharmFunctions::luaCharmDamageType); - registerMethod(L, "Charm", "percentage", CharmFunctions::luaCharmPercentage); - registerMethod(L, "Charm", "chance", CharmFunctions::luaCharmChance); - registerMethod(L, "Charm", "messageCancel", CharmFunctions::luaCharmMessageCancel); - registerMethod(L, "Charm", "messageServerLog", CharmFunctions::luaCharmMessageServerLog); - registerMethod(L, "Charm", "effect", CharmFunctions::luaCharmEffect); - registerMethod(L, "Charm", "castSound", CharmFunctions::luaCharmCastSound); - registerMethod(L, "Charm", "impactSound", CharmFunctions::luaCharmImpactSound); - } + static void init(lua_State* L); private: static int luaCharmCreate(lua_State* L); diff --git a/src/lua/functions/creatures/monster/loot_functions.cpp b/src/lua/functions/creatures/monster/loot_functions.cpp index 79260ccc935..87e7c8c2588 100644 --- a/src/lua/functions/creatures/monster/loot_functions.cpp +++ b/src/lua/functions/creatures/monster/loot_functions.cpp @@ -12,22 +12,46 @@ #include "creatures/monsters/monsters.hpp" #include "items/item.hpp" #include "utils/tools.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void LootFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Loot", "", LootFunctions::luaCreateLoot); + + Lua::registerMethod(L, "Loot", "setId", LootFunctions::luaLootSetId); + Lua::registerMethod(L, "Loot", "setIdFromName", LootFunctions::luaLootSetIdFromName); + Lua::registerMethod(L, "Loot", "setMinCount", LootFunctions::luaLootSetMinCount); + Lua::registerMethod(L, "Loot", "setMaxCount", LootFunctions::luaLootSetMaxCount); + Lua::registerMethod(L, "Loot", "setSubType", LootFunctions::luaLootSetSubType); + Lua::registerMethod(L, "Loot", "setChance", LootFunctions::luaLootSetChance); + Lua::registerMethod(L, "Loot", "setActionId", LootFunctions::luaLootSetActionId); + Lua::registerMethod(L, "Loot", "setText", LootFunctions::luaLootSetText); + Lua::registerMethod(L, "Loot", "setNameItem", LootFunctions::luaLootSetNameItem); + Lua::registerMethod(L, "Loot", "setArticle", LootFunctions::luaLootSetArticle); + Lua::registerMethod(L, "Loot", "setAttack", LootFunctions::luaLootSetAttack); + Lua::registerMethod(L, "Loot", "setDefense", LootFunctions::luaLootSetDefense); + Lua::registerMethod(L, "Loot", "setExtraDefense", LootFunctions::luaLootSetExtraDefense); + Lua::registerMethod(L, "Loot", "setArmor", LootFunctions::luaLootSetArmor); + Lua::registerMethod(L, "Loot", "setShootRange", LootFunctions::luaLootSetShootRange); + Lua::registerMethod(L, "Loot", "setHitChance", LootFunctions::luaLootSetHitChance); + Lua::registerMethod(L, "Loot", "setUnique", LootFunctions::luaLootSetUnique); + Lua::registerMethod(L, "Loot", "addChildLoot", LootFunctions::luaLootAddChildLoot); +} int LootFunctions::luaCreateLoot(lua_State* L) { // Loot() will create a new loot item auto loot = std::make_shared(); - pushUserdata(L, loot); - setMetatable(L, -1, "Loot"); + Lua::pushUserdata(L, loot); + Lua::setMetatable(L, -1, "Loot"); return 1; } int LootFunctions::luaLootSetId(lua_State* L) { // loot:setId(id) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - if (isNumber(L, 2)) { - loot->lootBlock.id = getNumber(L, 2); - pushBoolean(L, true); + if (Lua::isNumber(L, 2)) { + loot->lootBlock.id = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { g_logger().warn("[LootFunctions::luaLootSetId] - " "Unknown loot item loot, int value expected"); @@ -41,9 +65,9 @@ int LootFunctions::luaLootSetId(lua_State* L) { int LootFunctions::luaLootSetIdFromName(lua_State* L) { // loot:setIdFromName(name) - const auto &loot = getUserdataShared(L, 1); - if (loot && isString(L, 2)) { - auto name = getString(L, 2); + const auto &loot = Lua::getUserdataShared(L, 1); + if (loot && Lua::isString(L, 2)) { + auto name = Lua::getString(L, 2); const auto ids = Item::items.nameToItems.equal_range(asLowerCaseString(name)); if (ids.first == Item::items.nameToItems.cend()) { @@ -63,7 +87,7 @@ int LootFunctions::luaLootSetIdFromName(lua_State* L) { } loot->lootBlock.id = ids.first->second; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { g_logger().warn("[LootFunctions::luaLootSetIdFromName] - " "Unknown loot item loot, string value expected"); @@ -74,10 +98,10 @@ int LootFunctions::luaLootSetIdFromName(lua_State* L) { int LootFunctions::luaLootSetSubType(lua_State* L) { // loot:setSubType(type) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.subType = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.subType = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -86,10 +110,10 @@ int LootFunctions::luaLootSetSubType(lua_State* L) { int LootFunctions::luaLootSetChance(lua_State* L) { // loot:setChance(chance) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.chance = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.chance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -98,10 +122,10 @@ int LootFunctions::luaLootSetChance(lua_State* L) { int LootFunctions::luaLootSetMinCount(lua_State* L) { // loot:setMinCount(min) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.countmin = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.countmin = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -110,10 +134,10 @@ int LootFunctions::luaLootSetMinCount(lua_State* L) { int LootFunctions::luaLootSetMaxCount(lua_State* L) { // loot:setMaxCount(max) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.countmax = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.countmax = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -122,10 +146,10 @@ int LootFunctions::luaLootSetMaxCount(lua_State* L) { int LootFunctions::luaLootSetActionId(lua_State* L) { // loot:setActionId(actionid) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.actionId = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.actionId = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -134,10 +158,10 @@ int LootFunctions::luaLootSetActionId(lua_State* L) { int LootFunctions::luaLootSetText(lua_State* L) { // loot:setText(text) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.text = getString(L, 2); - pushBoolean(L, true); + loot->lootBlock.text = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -146,10 +170,10 @@ int LootFunctions::luaLootSetText(lua_State* L) { int LootFunctions::luaLootSetNameItem(lua_State* L) { // loot:setNameItem(name) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.name = getString(L, 2); - pushBoolean(L, true); + loot->lootBlock.name = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -158,10 +182,10 @@ int LootFunctions::luaLootSetNameItem(lua_State* L) { int LootFunctions::luaLootSetArticle(lua_State* L) { // loot:setArticle(article) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.article = getString(L, 2); - pushBoolean(L, true); + loot->lootBlock.article = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -170,10 +194,10 @@ int LootFunctions::luaLootSetArticle(lua_State* L) { int LootFunctions::luaLootSetAttack(lua_State* L) { // loot:setAttack(attack) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.attack = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.attack = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -182,10 +206,10 @@ int LootFunctions::luaLootSetAttack(lua_State* L) { int LootFunctions::luaLootSetDefense(lua_State* L) { // loot:setDefense(defense) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.defense = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.defense = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -194,10 +218,10 @@ int LootFunctions::luaLootSetDefense(lua_State* L) { int LootFunctions::luaLootSetExtraDefense(lua_State* L) { // loot:setExtraDefense(defense) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.extraDefense = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.extraDefense = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -206,10 +230,10 @@ int LootFunctions::luaLootSetExtraDefense(lua_State* L) { int LootFunctions::luaLootSetArmor(lua_State* L) { // loot:setArmor(armor) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.armor = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.armor = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -218,10 +242,10 @@ int LootFunctions::luaLootSetArmor(lua_State* L) { int LootFunctions::luaLootSetShootRange(lua_State* L) { // loot:setShootRange(range) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.shootRange = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.shootRange = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -230,10 +254,10 @@ int LootFunctions::luaLootSetShootRange(lua_State* L) { int LootFunctions::luaLootSetHitChance(lua_State* L) { // loot:setHitChance(chance) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - loot->lootBlock.hitChance = getNumber(L, 2); - pushBoolean(L, true); + loot->lootBlock.hitChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -242,13 +266,13 @@ int LootFunctions::luaLootSetHitChance(lua_State* L) { int LootFunctions::luaLootSetUnique(lua_State* L) { // loot:setUnique(bool) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { if (lua_gettop(L) == 1) { - pushBoolean(L, loot->lootBlock.unique); + Lua::pushBoolean(L, loot->lootBlock.unique); } else { - loot->lootBlock.unique = getBoolean(L, 2); - pushBoolean(L, true); + loot->lootBlock.unique = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -258,14 +282,14 @@ int LootFunctions::luaLootSetUnique(lua_State* L) { int LootFunctions::luaLootAddChildLoot(lua_State* L) { // loot:addChildLoot(loot) - const auto &loot = getUserdataShared(L, 1); + const auto &loot = Lua::getUserdataShared(L, 1); if (loot) { - const auto childLoot = getUserdata(L, 2); + const auto childLoot = Lua::getUserdata(L, 2); if (childLoot) { loot->lootBlock.childLoot.push_back(childLoot->lootBlock); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } } else { lua_pushnil(L); diff --git a/src/lua/functions/creatures/monster/loot_functions.hpp b/src/lua/functions/creatures/monster/loot_functions.hpp index 321f6bd8365..faa9abc2ef1 100644 --- a/src/lua/functions/creatures/monster/loot_functions.hpp +++ b/src/lua/functions/creatures/monster/loot_functions.hpp @@ -9,38 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class LootFunctions final : LuaScriptInterface { +class LootFunctions { public: - explicit LootFunctions(lua_State* L) : - LuaScriptInterface("LootFunctions") { - init(L); - } - ~LootFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Loot", "", LootFunctions::luaCreateLoot); - - registerMethod(L, "Loot", "setId", LootFunctions::luaLootSetId); - registerMethod(L, "Loot", "setIdFromName", LootFunctions::luaLootSetIdFromName); - registerMethod(L, "Loot", "setMinCount", LootFunctions::luaLootSetMinCount); - registerMethod(L, "Loot", "setMaxCount", LootFunctions::luaLootSetMaxCount); - registerMethod(L, "Loot", "setSubType", LootFunctions::luaLootSetSubType); - registerMethod(L, "Loot", "setChance", LootFunctions::luaLootSetChance); - registerMethod(L, "Loot", "setActionId", LootFunctions::luaLootSetActionId); - registerMethod(L, "Loot", "setText", LootFunctions::luaLootSetText); - registerMethod(L, "Loot", "setNameItem", LootFunctions::luaLootSetNameItem); - registerMethod(L, "Loot", "setArticle", LootFunctions::luaLootSetArticle); - registerMethod(L, "Loot", "setAttack", LootFunctions::luaLootSetAttack); - registerMethod(L, "Loot", "setDefense", LootFunctions::luaLootSetDefense); - registerMethod(L, "Loot", "setExtraDefense", LootFunctions::luaLootSetExtraDefense); - registerMethod(L, "Loot", "setArmor", LootFunctions::luaLootSetArmor); - registerMethod(L, "Loot", "setShootRange", LootFunctions::luaLootSetShootRange); - registerMethod(L, "Loot", "setHitChance", LootFunctions::luaLootSetHitChance); - registerMethod(L, "Loot", "setUnique", LootFunctions::luaLootSetUnique); - registerMethod(L, "Loot", "addChildLoot", LootFunctions::luaLootAddChildLoot); - } + static void init(lua_State* L); private: static int luaCreateLoot(lua_State* L); diff --git a/src/lua/functions/creatures/monster/monster_functions.cpp b/src/lua/functions/creatures/monster/monster_functions.cpp index 4b85fc98c3a..3f5d0c25ea8 100644 --- a/src/lua/functions/creatures/monster/monster_functions.cpp +++ b/src/lua/functions/creatures/monster/monster_functions.cpp @@ -17,25 +17,86 @@ #include "game/game.hpp" #include "game/scheduling/events_scheduler.hpp" #include "map/spectators.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void MonsterFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Monster", "Creature", MonsterFunctions::luaMonsterCreate); + Lua::registerMetaMethod(L, "Monster", "__eq", Lua::luaUserdataCompare); + Lua::registerMethod(L, "Monster", "isMonster", MonsterFunctions::luaMonsterIsMonster); + Lua::registerMethod(L, "Monster", "getType", MonsterFunctions::luaMonsterGetType); + Lua::registerMethod(L, "Monster", "setType", MonsterFunctions::luaMonsterSetType); + Lua::registerMethod(L, "Monster", "getSpawnPosition", MonsterFunctions::luaMonsterGetSpawnPosition); + Lua::registerMethod(L, "Monster", "isInSpawnRange", MonsterFunctions::luaMonsterIsInSpawnRange); + Lua::registerMethod(L, "Monster", "isIdle", MonsterFunctions::luaMonsterIsIdle); + Lua::registerMethod(L, "Monster", "setIdle", MonsterFunctions::luaMonsterSetIdle); + Lua::registerMethod(L, "Monster", "isTarget", MonsterFunctions::luaMonsterIsTarget); + Lua::registerMethod(L, "Monster", "isOpponent", MonsterFunctions::luaMonsterIsOpponent); + Lua::registerMethod(L, "Monster", "isFriend", MonsterFunctions::luaMonsterIsFriend); + Lua::registerMethod(L, "Monster", "addFriend", MonsterFunctions::luaMonsterAddFriend); + Lua::registerMethod(L, "Monster", "removeFriend", MonsterFunctions::luaMonsterRemoveFriend); + Lua::registerMethod(L, "Monster", "getFriendList", MonsterFunctions::luaMonsterGetFriendList); + Lua::registerMethod(L, "Monster", "getFriendCount", MonsterFunctions::luaMonsterGetFriendCount); + Lua::registerMethod(L, "Monster", "addTarget", MonsterFunctions::luaMonsterAddTarget); + Lua::registerMethod(L, "Monster", "removeTarget", MonsterFunctions::luaMonsterRemoveTarget); + Lua::registerMethod(L, "Monster", "getTargetList", MonsterFunctions::luaMonsterGetTargetList); + Lua::registerMethod(L, "Monster", "getTargetCount", MonsterFunctions::luaMonsterGetTargetCount); + Lua::registerMethod(L, "Monster", "changeTargetDistance", MonsterFunctions::luaMonsterChangeTargetDistance); + Lua::registerMethod(L, "Monster", "isChallenged", MonsterFunctions::luaMonsterIsChallenged); + Lua::registerMethod(L, "Monster", "selectTarget", MonsterFunctions::luaMonsterSelectTarget); + Lua::registerMethod(L, "Monster", "searchTarget", MonsterFunctions::luaMonsterSearchTarget); + Lua::registerMethod(L, "Monster", "setSpawnPosition", MonsterFunctions::luaMonsterSetSpawnPosition); + Lua::registerMethod(L, "Monster", "getRespawnType", MonsterFunctions::luaMonsterGetRespawnType); + + Lua::registerMethod(L, "Monster", "getTimeToChangeFiendish", MonsterFunctions::luaMonsterGetTimeToChangeFiendish); + Lua::registerMethod(L, "Monster", "setTimeToChangeFiendish", MonsterFunctions::luaMonsterSetTimeToChangeFiendish); + Lua::registerMethod(L, "Monster", "getMonsterForgeClassification", MonsterFunctions::luaMonsterGetMonsterForgeClassification); + Lua::registerMethod(L, "Monster", "setMonsterForgeClassification", MonsterFunctions::luaMonsterSetMonsterForgeClassification); + Lua::registerMethod(L, "Monster", "getForgeStack", MonsterFunctions::luaMonsterGetForgeStack); + Lua::registerMethod(L, "Monster", "setForgeStack", MonsterFunctions::luaMonsterSetForgeStack); + Lua::registerMethod(L, "Monster", "configureForgeSystem", MonsterFunctions::luaMonsterConfigureForgeSystem); + Lua::registerMethod(L, "Monster", "clearFiendishStatus", MonsterFunctions::luaMonsterClearFiendishStatus); + Lua::registerMethod(L, "Monster", "isForgeable", MonsterFunctions::luaMonsterIsForgeable); + + Lua::registerMethod(L, "Monster", "getName", MonsterFunctions::luaMonsterGetName); + Lua::registerMethod(L, "Monster", "setName", MonsterFunctions::luaMonsterSetName); + + Lua::registerMethod(L, "Monster", "hazard", MonsterFunctions::luaMonsterHazard); + Lua::registerMethod(L, "Monster", "hazardCrit", MonsterFunctions::luaMonsterHazardCrit); + Lua::registerMethod(L, "Monster", "hazardDodge", MonsterFunctions::luaMonsterHazardDodge); + Lua::registerMethod(L, "Monster", "hazardDamageBoost", MonsterFunctions::luaMonsterHazardDamageBoost); + Lua::registerMethod(L, "Monster", "hazardDefenseBoost", MonsterFunctions::luaMonsterHazardDefenseBoost); + + Lua::registerMethod(L, "Monster", "addReflectElement", MonsterFunctions::luaMonsterAddReflectElement); + Lua::registerMethod(L, "Monster", "addDefense", MonsterFunctions::luaMonsterAddDefense); + Lua::registerMethod(L, "Monster", "getDefense", MonsterFunctions::luaMonsterGetDefense); + + Lua::registerMethod(L, "Monster", "isDead", MonsterFunctions::luaMonsterIsDead); + Lua::registerMethod(L, "Monster", "immune", MonsterFunctions::luaMonsterImmune); + + CharmFunctions::init(L); + LootFunctions::init(L); + MonsterSpellFunctions::init(L); + MonsterTypeFunctions::init(L); +} int MonsterFunctions::luaMonsterCreate(lua_State* L) { // Monster(id or userdata) std::shared_ptr monster; - if (isNumber(L, 2)) { - monster = g_game().getMonsterByID(getNumber(L, 2)); - } else if (isUserdata(L, 2)) { - if (getUserdataType(L, 2) != LuaData_t::Monster) { + if (Lua::isNumber(L, 2)) { + monster = g_game().getMonsterByID(Lua::getNumber(L, 2)); + } else if (Lua::isUserdata(L, 2)) { + if (Lua::getUserdataType(L, 2) != LuaData_t::Monster) { lua_pushnil(L); return 1; } - monster = getUserdataShared(L, 2); + monster = Lua::getUserdataShared(L, 2); } else { monster = nullptr; } if (monster) { - pushUserdata(L, monster); - setMetatable(L, -1, "Monster"); + Lua::pushUserdata(L, monster); + Lua::setMetatable(L, -1, "Monster"); } else { lua_pushnil(L); } @@ -44,16 +105,16 @@ int MonsterFunctions::luaMonsterCreate(lua_State* L) { int MonsterFunctions::luaMonsterIsMonster(lua_State* L) { // monster:isMonster() - pushBoolean(L, getUserdataShared(L, 1) != nullptr); + Lua::pushBoolean(L, Lua::getUserdataShared(L, 1) != nullptr); return 1; } int MonsterFunctions::luaMonsterGetType(lua_State* L) { // monster:getType() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - pushUserdata(L, monster->mType); - setMetatable(L, -1, "MonsterType"); + Lua::pushUserdata(L, monster->mType); + Lua::setMetatable(L, -1, "MonsterType"); } else { lua_pushnil(L); } @@ -62,14 +123,14 @@ int MonsterFunctions::luaMonsterGetType(lua_State* L) { int MonsterFunctions::luaMonsterSetType(lua_State* L) { // monster:setType(name or raceid, restoreHealth = false) - bool restoreHealth = getBoolean(L, 3, false); - const auto &monster = getUserdataShared(L, 1); + bool restoreHealth = Lua::getBoolean(L, 3, false); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { std::shared_ptr mType; - if (isNumber(L, 2)) { - mType = g_monsters().getMonsterTypeByRaceId(getNumber(L, 2)); + if (Lua::isNumber(L, 2)) { + mType = g_monsters().getMonsterTypeByRaceId(Lua::getNumber(L, 2)); } else { - mType = g_monsters().getMonsterType(getString(L, 2)); + mType = g_monsters().getMonsterType(Lua::getString(L, 2)); } // Unregister creature events (current MonsterType) for (const std::string &scriptName : monster->mType->info.scripts) { @@ -105,7 +166,7 @@ int MonsterFunctions::luaMonsterSetType(lua_State* L) { for (const auto &spectator : Spectators().find(monster->getPosition(), true)) { spectator->getPlayer()->sendCreatureReload(monster); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -114,9 +175,9 @@ int MonsterFunctions::luaMonsterSetType(lua_State* L) { int MonsterFunctions::luaMonsterGetSpawnPosition(lua_State* L) { // monster:getSpawnPosition() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - pushPosition(L, monster->getMasterPos()); + Lua::pushPosition(L, monster->getMasterPos()); } else { lua_pushnil(L); } @@ -125,9 +186,9 @@ int MonsterFunctions::luaMonsterGetSpawnPosition(lua_State* L) { int MonsterFunctions::luaMonsterIsInSpawnRange(lua_State* L) { // monster:isInSpawnRange([position]) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - pushBoolean(L, monster->isInSpawnRange(lua_gettop(L) >= 2 ? getPosition(L, 2) : monster->getPosition())); + Lua::pushBoolean(L, monster->isInSpawnRange(lua_gettop(L) >= 2 ? Lua::getPosition(L, 2) : monster->getPosition())); } else { lua_pushnil(L); } @@ -136,9 +197,9 @@ int MonsterFunctions::luaMonsterIsInSpawnRange(lua_State* L) { int MonsterFunctions::luaMonsterIsIdle(lua_State* L) { // monster:isIdle() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - pushBoolean(L, monster->getIdleStatus()); + Lua::pushBoolean(L, monster->getIdleStatus()); } else { lua_pushnil(L); } @@ -147,23 +208,23 @@ int MonsterFunctions::luaMonsterIsIdle(lua_State* L) { int MonsterFunctions::luaMonsterSetIdle(lua_State* L) { // monster:setIdle(idle) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { lua_pushnil(L); return 1; } - monster->setIdle(getBoolean(L, 2)); - pushBoolean(L, true); + monster->setIdle(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); return 1; } int MonsterFunctions::luaMonsterIsTarget(lua_State* L) { // monster:isTarget(creature) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - const auto &creature = getCreature(L, 2); - pushBoolean(L, monster->isTarget(creature)); + const auto &creature = Lua::getCreature(L, 2); + Lua::pushBoolean(L, monster->isTarget(creature)); } else { lua_pushnil(L); } @@ -172,10 +233,10 @@ int MonsterFunctions::luaMonsterIsTarget(lua_State* L) { int MonsterFunctions::luaMonsterIsOpponent(lua_State* L) { // monster:isOpponent(creature) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - const auto &creature = getCreature(L, 2); - pushBoolean(L, monster->isOpponent(creature)); + const auto &creature = Lua::getCreature(L, 2); + Lua::pushBoolean(L, monster->isOpponent(creature)); } else { lua_pushnil(L); } @@ -184,10 +245,10 @@ int MonsterFunctions::luaMonsterIsOpponent(lua_State* L) { int MonsterFunctions::luaMonsterIsFriend(lua_State* L) { // monster:isFriend(creature) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - const auto &creature = getCreature(L, 2); - pushBoolean(L, monster->isFriend(creature)); + const auto &creature = Lua::getCreature(L, 2); + Lua::pushBoolean(L, monster->isFriend(creature)); } else { lua_pushnil(L); } @@ -196,11 +257,11 @@ int MonsterFunctions::luaMonsterIsFriend(lua_State* L) { int MonsterFunctions::luaMonsterAddFriend(lua_State* L) { // monster:addFriend(creature) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - const auto &creature = getCreature(L, 2); + const auto &creature = Lua::getCreature(L, 2); monster->addFriend(creature); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -209,11 +270,11 @@ int MonsterFunctions::luaMonsterAddFriend(lua_State* L) { int MonsterFunctions::luaMonsterRemoveFriend(lua_State* L) { // monster:removeFriend(creature) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - const auto &creature = getCreature(L, 2); + const auto &creature = Lua::getCreature(L, 2); monster->removeFriend(creature); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -222,7 +283,7 @@ int MonsterFunctions::luaMonsterRemoveFriend(lua_State* L) { int MonsterFunctions::luaMonsterGetFriendList(lua_State* L) { // monster:getFriendList() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { lua_pushnil(L); return 1; @@ -233,8 +294,8 @@ int MonsterFunctions::luaMonsterGetFriendList(lua_State* L) { int index = 0; for (const auto &creature : friendList) { - pushUserdata(L, creature); - setCreatureMetatable(L, -1, creature); + Lua::pushUserdata(L, creature); + Lua::setCreatureMetatable(L, -1, creature); lua_rawseti(L, -2, ++index); } return 1; @@ -242,7 +303,7 @@ int MonsterFunctions::luaMonsterGetFriendList(lua_State* L) { int MonsterFunctions::luaMonsterGetFriendCount(lua_State* L) { // monster:getFriendCount() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { lua_pushnumber(L, monster->getFriendList().size()); } else { @@ -253,35 +314,35 @@ int MonsterFunctions::luaMonsterGetFriendCount(lua_State* L) { int MonsterFunctions::luaMonsterAddTarget(lua_State* L) { // monster:addTarget(creature[, pushFront = false]) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { lua_pushnil(L); return 1; } - const auto &creature = getCreature(L, 2); - const bool pushFront = getBoolean(L, 3, false); + const auto &creature = Lua::getCreature(L, 2); + const bool pushFront = Lua::getBoolean(L, 3, false); monster->addTarget(creature, pushFront); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int MonsterFunctions::luaMonsterRemoveTarget(lua_State* L) { // monster:removeTarget(creature) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { lua_pushnil(L); return 1; } - monster->removeTarget(getCreature(L, 2)); - pushBoolean(L, true); + monster->removeTarget(Lua::getCreature(L, 2)); + Lua::pushBoolean(L, true); return 1; } int MonsterFunctions::luaMonsterGetTargetList(lua_State* L) { // monster:getTargetList() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { lua_pushnil(L); return 1; @@ -292,8 +353,8 @@ int MonsterFunctions::luaMonsterGetTargetList(lua_State* L) { int index = 0; for (const auto &creature : targetList) { - pushUserdata(L, creature); - setCreatureMetatable(L, -1, creature); + Lua::pushUserdata(L, creature); + Lua::setCreatureMetatable(L, -1, creature); lua_rawseti(L, -2, ++index); } return 1; @@ -301,7 +362,7 @@ int MonsterFunctions::luaMonsterGetTargetList(lua_State* L) { int MonsterFunctions::luaMonsterGetTargetCount(lua_State* L) { // monster:getTargetCount() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { lua_pushnumber(L, monster->getTargetList().size()); } else { @@ -312,11 +373,11 @@ int MonsterFunctions::luaMonsterGetTargetCount(lua_State* L) { int MonsterFunctions::luaMonsterChangeTargetDistance(lua_State* L) { // monster:changeTargetDistance(distance[, duration = 12000]) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - const auto distance = getNumber(L, 2, 1); - const auto duration = getNumber(L, 3, 12000); - pushBoolean(L, monster->changeTargetDistance(distance, duration)); + const auto distance = Lua::getNumber(L, 2, 1); + const auto duration = Lua::getNumber(L, 3, 12000); + Lua::pushBoolean(L, monster->changeTargetDistance(distance, duration)); } else { lua_pushnil(L); } @@ -325,9 +386,9 @@ int MonsterFunctions::luaMonsterChangeTargetDistance(lua_State* L) { int MonsterFunctions::luaMonsterIsChallenged(lua_State* L) { // monster:isChallenged() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - pushBoolean(L, monster->isChallenged()); + Lua::pushBoolean(L, monster->isChallenged()); } else { lua_pushnil(L); } @@ -336,10 +397,10 @@ int MonsterFunctions::luaMonsterIsChallenged(lua_State* L) { int MonsterFunctions::luaMonsterSelectTarget(lua_State* L) { // monster:selectTarget(creature) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - const auto &creature = getCreature(L, 2); - pushBoolean(L, monster->selectTarget(creature)); + const auto &creature = Lua::getCreature(L, 2); + Lua::pushBoolean(L, monster->selectTarget(creature)); } else { lua_pushnil(L); } @@ -348,10 +409,10 @@ int MonsterFunctions::luaMonsterSelectTarget(lua_State* L) { int MonsterFunctions::luaMonsterSearchTarget(lua_State* L) { // monster:searchTarget([searchType = TARGETSEARCH_DEFAULT]) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (monster) { - const auto &searchType = getNumber(L, 2, TARGETSEARCH_DEFAULT); - pushBoolean(L, monster->searchTarget(searchType)); + const auto &searchType = Lua::getNumber(L, 2, TARGETSEARCH_DEFAULT); + Lua::pushBoolean(L, monster->searchTarget(searchType)); } else { lua_pushnil(L); } @@ -360,7 +421,7 @@ int MonsterFunctions::luaMonsterSearchTarget(lua_State* L) { int MonsterFunctions::luaMonsterSetSpawnPosition(lua_State* L) { // monster:setSpawnPosition(interval) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { lua_pushnil(L); return 1; @@ -372,17 +433,17 @@ int MonsterFunctions::luaMonsterSetSpawnPosition(lua_State* L) { monster->setMasterPos(pos); const auto &spawnMonster = g_game().map.spawnsMonster.getspawnMonsterList().emplace_back(std::make_shared(pos, 5)); - uint32_t interval = getNumber(L, 2, 90) * 1000 * 100 / std::max((uint32_t)1, (g_configManager().getNumber(RATE_SPAWN) * eventschedule)); + uint32_t interval = Lua::getNumber(L, 2, 90) * 1000 * 100 / std::max((uint32_t)1, (g_configManager().getNumber(RATE_SPAWN) * eventschedule)); spawnMonster->addMonster(monster->mType->typeName, pos, DIRECTION_NORTH, static_cast(interval)); spawnMonster->startSpawnMonsterCheck(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int MonsterFunctions::luaMonsterGetRespawnType(lua_State* L) { // monster:getRespawnType() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { lua_pushnil(L); @@ -391,17 +452,17 @@ int MonsterFunctions::luaMonsterGetRespawnType(lua_State* L) { const RespawnType respawnType = monster->getRespawnType(); lua_pushnumber(L, respawnType.period); - pushBoolean(L, respawnType.underground); + Lua::pushBoolean(L, respawnType.underground); return 2; } int MonsterFunctions::luaMonsterGetTimeToChangeFiendish(lua_State* L) { // monster:getTimeToChangeFiendish() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -411,11 +472,11 @@ int MonsterFunctions::luaMonsterGetTimeToChangeFiendish(lua_State* L) { int MonsterFunctions::luaMonsterSetTimeToChangeFiendish(lua_State* L) { // monster:setTimeToChangeFiendish(endTime) - const time_t endTime = getNumber(L, 2, 1); - const auto &monster = getUserdataShared(L, 1); + const time_t endTime = Lua::getNumber(L, 2, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -425,10 +486,10 @@ int MonsterFunctions::luaMonsterSetTimeToChangeFiendish(lua_State* L) { int MonsterFunctions::luaMonsterGetMonsterForgeClassification(lua_State* L) { // monster:getMonsterForgeClassification() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -439,11 +500,11 @@ int MonsterFunctions::luaMonsterGetMonsterForgeClassification(lua_State* L) { int MonsterFunctions::luaMonsterSetMonsterForgeClassification(lua_State* L) { // monster:setMonsterForgeClassification(classication) - const ForgeClassifications_t classification = getNumber(L, 2); - const auto &monster = getUserdataShared(L, 1); + const ForgeClassifications_t classification = Lua::getNumber(L, 2); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -453,10 +514,10 @@ int MonsterFunctions::luaMonsterSetMonsterForgeClassification(lua_State* L) { int MonsterFunctions::luaMonsterGetForgeStack(lua_State* L) { // monster:getForgeStack() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -466,11 +527,11 @@ int MonsterFunctions::luaMonsterGetForgeStack(lua_State* L) { int MonsterFunctions::luaMonsterSetForgeStack(lua_State* L) { // monster:setForgeStack(stack) - const auto stack = getNumber(L, 2, 0); - const auto &monster = getUserdataShared(L, 1); + const auto stack = Lua::getNumber(L, 2, 0); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -486,10 +547,10 @@ int MonsterFunctions::luaMonsterSetForgeStack(lua_State* L) { int MonsterFunctions::luaMonsterConfigureForgeSystem(lua_State* L) { // monster:configureForgeSystem() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -499,10 +560,10 @@ int MonsterFunctions::luaMonsterConfigureForgeSystem(lua_State* L) { int MonsterFunctions::luaMonsterClearFiendishStatus(lua_State* L) { // monster:clearFiendishStatus() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -512,58 +573,58 @@ int MonsterFunctions::luaMonsterClearFiendishStatus(lua_State* L) { int MonsterFunctions::luaMonsterIsForgeable(lua_State* L) { // monster:isForgeable() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - pushBoolean(L, monster->canBeForgeMonster()); + Lua::pushBoolean(L, monster->canBeForgeMonster()); return 1; } int MonsterFunctions::luaMonsterGetName(lua_State* L) { // monster:getName() - const auto monster = getUserdataShared(L, 1); + const auto monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - pushString(L, monster->getName()); + Lua::pushString(L, monster->getName()); return 1; } int MonsterFunctions::luaMonsterSetName(lua_State* L) { // monster:setName(name[, nameDescription]) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - monster->setName(getString(L, 2)); + monster->setName(Lua::getString(L, 2)); if (lua_gettop(L) >= 3) { - monster->setNameDescription(getString(L, 3)); + monster->setNameDescription(Lua::getString(L, 3)); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int MonsterFunctions::luaMonsterHazard(lua_State* L) { // get: monster:hazard() ; set: monster:hazard(hazard) - const auto &monster = getUserdataShared(L, 1); - const bool hazard = getBoolean(L, 2, false); + const auto &monster = Lua::getUserdataShared(L, 1); + const bool hazard = Lua::getBoolean(L, 2, false); if (monster) { if (lua_gettop(L) == 1) { - pushBoolean(L, monster->getHazard()); + Lua::pushBoolean(L, monster->getHazard()); } else { monster->setHazard(hazard); - pushBoolean(L, monster->getHazard()); + Lua::pushBoolean(L, monster->getHazard()); } } else { lua_pushnil(L); @@ -573,14 +634,14 @@ int MonsterFunctions::luaMonsterHazard(lua_State* L) { int MonsterFunctions::luaMonsterHazardCrit(lua_State* L) { // get: monster:hazardCrit() ; set: monster:hazardCrit(hazardCrit) - const auto &monster = getUserdataShared(L, 1); - const bool hazardCrit = getBoolean(L, 2, false); + const auto &monster = Lua::getUserdataShared(L, 1); + const bool hazardCrit = Lua::getBoolean(L, 2, false); if (monster) { if (lua_gettop(L) == 1) { - pushBoolean(L, monster->getHazardSystemCrit()); + Lua::pushBoolean(L, monster->getHazardSystemCrit()); } else { monster->setHazardSystemCrit(hazardCrit); - pushBoolean(L, monster->getHazardSystemCrit()); + Lua::pushBoolean(L, monster->getHazardSystemCrit()); } } else { lua_pushnil(L); @@ -590,14 +651,14 @@ int MonsterFunctions::luaMonsterHazardCrit(lua_State* L) { int MonsterFunctions::luaMonsterHazardDodge(lua_State* L) { // get: monster:hazardDodge() ; set: monster:hazardDodge(hazardDodge) - const auto &monster = getUserdataShared(L, 1); - const bool hazardDodge = getBoolean(L, 2, false); + const auto &monster = Lua::getUserdataShared(L, 1); + const bool hazardDodge = Lua::getBoolean(L, 2, false); if (monster) { if (lua_gettop(L) == 1) { - pushBoolean(L, monster->getHazardSystemDodge()); + Lua::pushBoolean(L, monster->getHazardSystemDodge()); } else { monster->setHazardSystemDodge(hazardDodge); - pushBoolean(L, monster->getHazardSystemDodge()); + Lua::pushBoolean(L, monster->getHazardSystemDodge()); } } else { lua_pushnil(L); @@ -607,14 +668,14 @@ int MonsterFunctions::luaMonsterHazardDodge(lua_State* L) { int MonsterFunctions::luaMonsterHazardDamageBoost(lua_State* L) { // get: monster:hazardDamageBoost() ; set: monster:hazardDamageBoost(hazardDamageBoost) - const auto &monster = getUserdataShared(L, 1); - const bool hazardDamageBoost = getBoolean(L, 2, false); + const auto &monster = Lua::getUserdataShared(L, 1); + const bool hazardDamageBoost = Lua::getBoolean(L, 2, false); if (monster) { if (lua_gettop(L) == 1) { - pushBoolean(L, monster->getHazardSystemDamageBoost()); + Lua::pushBoolean(L, monster->getHazardSystemDamageBoost()); } else { monster->setHazardSystemDamageBoost(hazardDamageBoost); - pushBoolean(L, monster->getHazardSystemDamageBoost()); + Lua::pushBoolean(L, monster->getHazardSystemDamageBoost()); } } else { lua_pushnil(L); @@ -624,14 +685,14 @@ int MonsterFunctions::luaMonsterHazardDamageBoost(lua_State* L) { int MonsterFunctions::luaMonsterHazardDefenseBoost(lua_State* L) { // get: monster:hazardDefenseBoost() ; set: monster:hazardDefenseBoost(hazardDefenseBoost) - const auto &monster = getUserdataShared(L, 1); - const bool hazardDefenseBoost = getBoolean(L, 2, false); + const auto &monster = Lua::getUserdataShared(L, 1); + const bool hazardDefenseBoost = Lua::getBoolean(L, 2, false); if (monster) { if (lua_gettop(L) == 1) { - pushBoolean(L, monster->getHazardSystemDefenseBoost()); + Lua::pushBoolean(L, monster->getHazardSystemDefenseBoost()); } else { monster->setHazardSystemDefenseBoost(hazardDefenseBoost); - pushBoolean(L, monster->getHazardSystemDefenseBoost()); + Lua::pushBoolean(L, monster->getHazardSystemDefenseBoost()); } } else { lua_pushnil(L); @@ -641,39 +702,39 @@ int MonsterFunctions::luaMonsterHazardDefenseBoost(lua_State* L) { int MonsterFunctions::luaMonsterAddReflectElement(lua_State* L) { // monster:addReflectElement(type, percent) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - CombatType_t element = getNumber(L, 2); - monster->addReflectElement(element, getNumber(L, 3)); - pushBoolean(L, true); + CombatType_t element = Lua::getNumber(L, 2); + monster->addReflectElement(element, Lua::getNumber(L, 3)); + Lua::pushBoolean(L, true); return 1; } int MonsterFunctions::luaMonsterAddDefense(lua_State* L) { // monster:addDefense(defense) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - monster->addDefense(getNumber(L, 2)); - pushBoolean(L, true); + monster->addDefense(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); return 1; } int MonsterFunctions::luaMonsterGetDefense(lua_State* L) { // monster:getDefense(defense) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -683,31 +744,31 @@ int MonsterFunctions::luaMonsterGetDefense(lua_State* L) { int MonsterFunctions::luaMonsterIsDead(lua_State* L) { // monster:isDead() - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - pushBoolean(L, monster->isDead()); + Lua::pushBoolean(L, monster->isDead()); return 1; } int MonsterFunctions::luaMonsterImmune(lua_State* L) { // to get: isImmune = monster:immune() // to set and get: newImmuneBool = monster:immune(newImmuneBool) - const auto &monster = getUserdataShared(L, 1); + const auto &monster = Lua::getUserdataShared(L, 1); if (!monster) { - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } if (lua_gettop(L) > 1) { - monster->setImmune(getBoolean(L, 2)); + monster->setImmune(Lua::getBoolean(L, 2)); } - pushBoolean(L, monster->isImmune()); + Lua::pushBoolean(L, monster->isImmune()); return 1; } diff --git a/src/lua/functions/creatures/monster/monster_functions.hpp b/src/lua/functions/creatures/monster/monster_functions.hpp index 0674bf64433..4ba696e941b 100644 --- a/src/lua/functions/creatures/monster/monster_functions.hpp +++ b/src/lua/functions/creatures/monster/monster_functions.hpp @@ -9,79 +9,14 @@ #pragma once -#include "lua/scripts/luascript.hpp" #include "lua/functions/creatures/monster/charm_functions.hpp" #include "lua/functions/creatures/monster/loot_functions.hpp" #include "lua/functions/creatures/monster/monster_spell_functions.hpp" #include "lua/functions/creatures/monster/monster_type_functions.hpp" -class MonsterFunctions final : LuaScriptInterface { +class MonsterFunctions { private: - explicit MonsterFunctions(lua_State* L) : - LuaScriptInterface("MonsterFunctions") { - init(L); - } - ~MonsterFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Monster", "Creature", MonsterFunctions::luaMonsterCreate); - registerMetaMethod(L, "Monster", "__eq", MonsterFunctions::luaUserdataCompare); - registerMethod(L, "Monster", "isMonster", MonsterFunctions::luaMonsterIsMonster); - registerMethod(L, "Monster", "getType", MonsterFunctions::luaMonsterGetType); - registerMethod(L, "Monster", "setType", MonsterFunctions::luaMonsterSetType); - registerMethod(L, "Monster", "getSpawnPosition", MonsterFunctions::luaMonsterGetSpawnPosition); - registerMethod(L, "Monster", "isInSpawnRange", MonsterFunctions::luaMonsterIsInSpawnRange); - registerMethod(L, "Monster", "isIdle", MonsterFunctions::luaMonsterIsIdle); - registerMethod(L, "Monster", "setIdle", MonsterFunctions::luaMonsterSetIdle); - registerMethod(L, "Monster", "isTarget", MonsterFunctions::luaMonsterIsTarget); - registerMethod(L, "Monster", "isOpponent", MonsterFunctions::luaMonsterIsOpponent); - registerMethod(L, "Monster", "isFriend", MonsterFunctions::luaMonsterIsFriend); - registerMethod(L, "Monster", "addFriend", MonsterFunctions::luaMonsterAddFriend); - registerMethod(L, "Monster", "removeFriend", MonsterFunctions::luaMonsterRemoveFriend); - registerMethod(L, "Monster", "getFriendList", MonsterFunctions::luaMonsterGetFriendList); - registerMethod(L, "Monster", "getFriendCount", MonsterFunctions::luaMonsterGetFriendCount); - registerMethod(L, "Monster", "addTarget", MonsterFunctions::luaMonsterAddTarget); - registerMethod(L, "Monster", "removeTarget", MonsterFunctions::luaMonsterRemoveTarget); - registerMethod(L, "Monster", "getTargetList", MonsterFunctions::luaMonsterGetTargetList); - registerMethod(L, "Monster", "getTargetCount", MonsterFunctions::luaMonsterGetTargetCount); - registerMethod(L, "Monster", "changeTargetDistance", MonsterFunctions::luaMonsterChangeTargetDistance); - registerMethod(L, "Monster", "isChallenged", MonsterFunctions::luaMonsterIsChallenged); - registerMethod(L, "Monster", "selectTarget", MonsterFunctions::luaMonsterSelectTarget); - registerMethod(L, "Monster", "searchTarget", MonsterFunctions::luaMonsterSearchTarget); - registerMethod(L, "Monster", "setSpawnPosition", MonsterFunctions::luaMonsterSetSpawnPosition); - registerMethod(L, "Monster", "getRespawnType", MonsterFunctions::luaMonsterGetRespawnType); - - registerMethod(L, "Monster", "getTimeToChangeFiendish", MonsterFunctions::luaMonsterGetTimeToChangeFiendish); - registerMethod(L, "Monster", "setTimeToChangeFiendish", MonsterFunctions::luaMonsterSetTimeToChangeFiendish); - registerMethod(L, "Monster", "getMonsterForgeClassification", MonsterFunctions::luaMonsterGetMonsterForgeClassification); - registerMethod(L, "Monster", "setMonsterForgeClassification", MonsterFunctions::luaMonsterSetMonsterForgeClassification); - registerMethod(L, "Monster", "getForgeStack", MonsterFunctions::luaMonsterGetForgeStack); - registerMethod(L, "Monster", "setForgeStack", MonsterFunctions::luaMonsterSetForgeStack); - registerMethod(L, "Monster", "configureForgeSystem", MonsterFunctions::luaMonsterConfigureForgeSystem); - registerMethod(L, "Monster", "clearFiendishStatus", MonsterFunctions::luaMonsterClearFiendishStatus); - registerMethod(L, "Monster", "isForgeable", MonsterFunctions::luaMonsterIsForgeable); - - registerMethod(L, "Monster", "getName", MonsterFunctions::luaMonsterGetName); - registerMethod(L, "Monster", "setName", MonsterFunctions::luaMonsterSetName); - - registerMethod(L, "Monster", "hazard", MonsterFunctions::luaMonsterHazard); - registerMethod(L, "Monster", "hazardCrit", MonsterFunctions::luaMonsterHazardCrit); - registerMethod(L, "Monster", "hazardDodge", MonsterFunctions::luaMonsterHazardDodge); - registerMethod(L, "Monster", "hazardDamageBoost", MonsterFunctions::luaMonsterHazardDamageBoost); - registerMethod(L, "Monster", "hazardDefenseBoost", MonsterFunctions::luaMonsterHazardDefenseBoost); - - registerMethod(L, "Monster", "addReflectElement", MonsterFunctions::luaMonsterAddReflectElement); - registerMethod(L, "Monster", "addDefense", MonsterFunctions::luaMonsterAddDefense); - registerMethod(L, "Monster", "getDefense", MonsterFunctions::luaMonsterGetDefense); - - registerMethod(L, "Monster", "isDead", MonsterFunctions::luaMonsterIsDead); - registerMethod(L, "Monster", "immune", MonsterFunctions::luaMonsterImmune); - - CharmFunctions::init(L); - LootFunctions::init(L); - MonsterSpellFunctions::init(L); - MonsterTypeFunctions::init(L); - } + static void init(lua_State* L); static int luaMonsterCreate(lua_State* L); diff --git a/src/lua/functions/creatures/monster/monster_spell_functions.cpp b/src/lua/functions/creatures/monster/monster_spell_functions.cpp index 693da8e6b7e..9fec549c3dd 100644 --- a/src/lua/functions/creatures/monster/monster_spell_functions.cpp +++ b/src/lua/functions/creatures/monster/monster_spell_functions.cpp @@ -10,20 +10,49 @@ #include "lua/functions/creatures/monster/monster_spell_functions.hpp" #include "creatures/monsters/monsters.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void MonsterSpellFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "MonsterSpell", "", MonsterSpellFunctions::luaCreateMonsterSpell); + + Lua::registerMethod(L, "MonsterSpell", "setType", MonsterSpellFunctions::luaMonsterSpellSetType); + Lua::registerMethod(L, "MonsterSpell", "setScriptName", MonsterSpellFunctions::luaMonsterSpellSetScriptName); + Lua::registerMethod(L, "MonsterSpell", "setChance", MonsterSpellFunctions::luaMonsterSpellSetChance); + Lua::registerMethod(L, "MonsterSpell", "setInterval", MonsterSpellFunctions::luaMonsterSpellSetInterval); + Lua::registerMethod(L, "MonsterSpell", "setRange", MonsterSpellFunctions::luaMonsterSpellSetRange); + Lua::registerMethod(L, "MonsterSpell", "setCombatValue", MonsterSpellFunctions::luaMonsterSpellSetCombatValue); + Lua::registerMethod(L, "MonsterSpell", "setCombatType", MonsterSpellFunctions::luaMonsterSpellSetCombatType); + Lua::registerMethod(L, "MonsterSpell", "setAttackValue", MonsterSpellFunctions::luaMonsterSpellSetAttackValue); + Lua::registerMethod(L, "MonsterSpell", "setNeedTarget", MonsterSpellFunctions::luaMonsterSpellSetNeedTarget); + Lua::registerMethod(L, "MonsterSpell", "setCombatLength", MonsterSpellFunctions::luaMonsterSpellSetCombatLength); + Lua::registerMethod(L, "MonsterSpell", "setCombatSpread", MonsterSpellFunctions::luaMonsterSpellSetCombatSpread); + Lua::registerMethod(L, "MonsterSpell", "setCombatRadius", MonsterSpellFunctions::luaMonsterSpellSetCombatRadius); + Lua::registerMethod(L, "MonsterSpell", "setConditionType", MonsterSpellFunctions::luaMonsterSpellSetConditionType); + Lua::registerMethod(L, "MonsterSpell", "setConditionDamage", MonsterSpellFunctions::luaMonsterSpellSetConditionDamage); + Lua::registerMethod(L, "MonsterSpell", "setConditionSpeedChange", MonsterSpellFunctions::luaMonsterSpellSetConditionSpeedChange); + Lua::registerMethod(L, "MonsterSpell", "setConditionDuration", MonsterSpellFunctions::luaMonsterSpellSetConditionDuration); + Lua::registerMethod(L, "MonsterSpell", "setConditionTickInterval", MonsterSpellFunctions::luaMonsterSpellSetConditionTickInterval); + Lua::registerMethod(L, "MonsterSpell", "setCombatShootEffect", MonsterSpellFunctions::luaMonsterSpellSetCombatShootEffect); + Lua::registerMethod(L, "MonsterSpell", "setCombatEffect", MonsterSpellFunctions::luaMonsterSpellSetCombatEffect); + Lua::registerMethod(L, "MonsterSpell", "setOutfitMonster", MonsterSpellFunctions::luaMonsterSpellSetOutfitMonster); + Lua::registerMethod(L, "MonsterSpell", "setOutfitItem", MonsterSpellFunctions::luaMonsterSpellSetOutfitItem); + Lua::registerMethod(L, "MonsterSpell", "castSound", MonsterSpellFunctions::luaMonsterSpellCastSound); + Lua::registerMethod(L, "MonsterSpell", "impactSound", MonsterSpellFunctions::luaMonsterSpellImpactSound); +} int MonsterSpellFunctions::luaCreateMonsterSpell(lua_State* L) { const auto spell = std::make_shared(); - pushUserdata(L, spell); - setMetatable(L, -1, "MonsterSpell"); + Lua::pushUserdata(L, spell); + Lua::setMetatable(L, -1, "MonsterSpell"); return 1; } int MonsterSpellFunctions::luaMonsterSpellSetType(lua_State* L) { // monsterSpell:setType(type) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->name = getString(L, 2); - pushBoolean(L, true); + spell->name = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -32,10 +61,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetType(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetScriptName(lua_State* L) { // monsterSpell:setScriptName(name) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->scriptName = getString(L, 2); - pushBoolean(L, true); + spell->scriptName = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -44,10 +73,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetScriptName(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetChance(lua_State* L) { // monsterSpell:setChance(chance) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->chance = getNumber(L, 2); - pushBoolean(L, true); + spell->chance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -56,10 +85,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetChance(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetInterval(lua_State* L) { // monsterSpell:setInterval(interval) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->interval = getNumber(L, 2); - pushBoolean(L, true); + spell->interval = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -68,10 +97,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetInterval(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetRange(lua_State* L) { // monsterSpell:setRange(range) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->range = getNumber(L, 2); - pushBoolean(L, true); + spell->range = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -80,11 +109,11 @@ int MonsterSpellFunctions::luaMonsterSpellSetRange(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetCombatValue(lua_State* L) { // monsterSpell:setCombatValue(min, max) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->minCombatValue = getNumber(L, 2); - spell->maxCombatValue = getNumber(L, 3); - pushBoolean(L, true); + spell->minCombatValue = Lua::getNumber(L, 2); + spell->maxCombatValue = Lua::getNumber(L, 3); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -93,10 +122,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetCombatValue(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetCombatType(lua_State* L) { // monsterSpell:setCombatType(combatType_t) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->combatType = getNumber(L, 2); - pushBoolean(L, true); + spell->combatType = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -105,11 +134,11 @@ int MonsterSpellFunctions::luaMonsterSpellSetCombatType(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetAttackValue(lua_State* L) { // monsterSpell:setAttackValue(attack, skill) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->attack = getNumber(L, 2); - spell->skill = getNumber(L, 3); - pushBoolean(L, true); + spell->attack = Lua::getNumber(L, 2); + spell->skill = Lua::getNumber(L, 3); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -118,10 +147,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetAttackValue(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetNeedTarget(lua_State* L) { // monsterSpell:setNeedTarget(bool) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->needTarget = getBoolean(L, 2); - pushBoolean(L, true); + spell->needTarget = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -130,10 +159,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetNeedTarget(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetCombatLength(lua_State* L) { // monsterSpell:setCombatLength(length) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->length = getNumber(L, 2); - pushBoolean(L, true); + spell->length = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -142,10 +171,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetCombatLength(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetCombatSpread(lua_State* L) { // monsterSpell:setCombatSpread(spread) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->spread = getNumber(L, 2); - pushBoolean(L, true); + spell->spread = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -154,10 +183,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetCombatSpread(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetCombatRadius(lua_State* L) { // monsterSpell:setCombatRadius(radius) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->radius = getNumber(L, 2); - pushBoolean(L, true); + spell->radius = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -166,18 +195,18 @@ int MonsterSpellFunctions::luaMonsterSpellSetCombatRadius(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetConditionType(lua_State* L) { // monsterSpell:setConditionType(type) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - auto conditionType = getNumber(L, 2); + auto conditionType = Lua::getNumber(L, 2); if (conditionType == 254) { g_logger().error("[{}] trying to register condition type none for monster: {}", __FUNCTION__, spell->name); - reportErrorFunc(fmt::format("trying to register condition type none for monster: {}", spell->name)); - pushBoolean(L, false); + Lua::reportErrorFunc(fmt::format("trying to register condition type none for monster: {}", spell->name)); + Lua::pushBoolean(L, false); return 1; } spell->conditionType = static_cast(conditionType); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -186,12 +215,12 @@ int MonsterSpellFunctions::luaMonsterSpellSetConditionType(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetConditionDamage(lua_State* L) { // monsterSpell:setConditionDamage(min, max, start) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->conditionMinDamage = getNumber(L, 2); - spell->conditionMaxDamage = getNumber(L, 3); - spell->conditionStartDamage = getNumber(L, 4); - pushBoolean(L, true); + spell->conditionMinDamage = Lua::getNumber(L, 2); + spell->conditionMaxDamage = Lua::getNumber(L, 3); + spell->conditionStartDamage = Lua::getNumber(L, 4); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -200,10 +229,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetConditionDamage(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetConditionSpeedChange(lua_State* L) { // monsterSpell:setConditionSpeedChange(speed) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->speedChange = getNumber(L, 2); - pushBoolean(L, true); + spell->speedChange = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -212,10 +241,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetConditionSpeedChange(lua_State* L) int MonsterSpellFunctions::luaMonsterSpellSetConditionDuration(lua_State* L) { // monsterSpell:setConditionDuration(duration) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->duration = getNumber(L, 2); - pushBoolean(L, true); + spell->duration = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -224,10 +253,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetConditionDuration(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetConditionTickInterval(lua_State* L) { // monsterSpell:setConditionTickInterval(interval) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->tickInterval = getNumber(L, 2); - pushBoolean(L, true); + spell->tickInterval = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -236,10 +265,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetConditionTickInterval(lua_State* L) int MonsterSpellFunctions::luaMonsterSpellSetCombatShootEffect(lua_State* L) { // monsterSpell:setCombatShootEffect(effect) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->shoot = getNumber(L, 2); - pushBoolean(L, true); + spell->shoot = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -248,10 +277,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetCombatShootEffect(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetCombatEffect(lua_State* L) { // monsterSpell:setCombatEffect(effect) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->effect = getNumber(L, 2); - pushBoolean(L, true); + spell->effect = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -260,10 +289,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetCombatEffect(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetOutfitMonster(lua_State* L) { // monsterSpell:setOutfitMonster(effect) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->outfitMonster = getString(L, 2); - pushBoolean(L, true); + spell->outfitMonster = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -272,10 +301,10 @@ int MonsterSpellFunctions::luaMonsterSpellSetOutfitMonster(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellSetOutfitItem(lua_State* L) { // monsterSpell:setOutfitItem(effect) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (spell) { - spell->outfitItem = getNumber(L, 2); - pushBoolean(L, true); + spell->outfitItem = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -284,24 +313,24 @@ int MonsterSpellFunctions::luaMonsterSpellSetOutfitItem(lua_State* L) { int MonsterSpellFunctions::luaMonsterSpellCastSound(lua_State* L) { // get: monsterSpell:castSound() set: monsterSpell:castSound(sound) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { lua_pushnumber(L, static_cast(spell->soundCastEffect)); } else { - spell->soundCastEffect = getNumber(L, 2); - pushBoolean(L, true); + spell->soundCastEffect = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int MonsterSpellFunctions::luaMonsterSpellImpactSound(lua_State* L) { // get: monsterSpell:impactSound() set: monsterSpell:impactSound(sound) - const auto &spell = getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 1); if (lua_gettop(L) == 1) { lua_pushnumber(L, static_cast(spell->soundImpactEffect)); } else { - spell->soundImpactEffect = getNumber(L, 2); - pushBoolean(L, true); + spell->soundImpactEffect = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } diff --git a/src/lua/functions/creatures/monster/monster_spell_functions.hpp b/src/lua/functions/creatures/monster/monster_spell_functions.hpp index e44c5e53470..4816e899d9a 100644 --- a/src/lua/functions/creatures/monster/monster_spell_functions.hpp +++ b/src/lua/functions/creatures/monster/monster_spell_functions.hpp @@ -9,43 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class MonsterSpellFunctions final : LuaScriptInterface { +class MonsterSpellFunctions { public: - explicit MonsterSpellFunctions(lua_State* L) : - LuaScriptInterface("MonsterSpellFunctions") { - init(L); - } - ~MonsterSpellFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "MonsterSpell", "", MonsterSpellFunctions::luaCreateMonsterSpell); - - registerMethod(L, "MonsterSpell", "setType", MonsterSpellFunctions::luaMonsterSpellSetType); - registerMethod(L, "MonsterSpell", "setScriptName", MonsterSpellFunctions::luaMonsterSpellSetScriptName); - registerMethod(L, "MonsterSpell", "setChance", MonsterSpellFunctions::luaMonsterSpellSetChance); - registerMethod(L, "MonsterSpell", "setInterval", MonsterSpellFunctions::luaMonsterSpellSetInterval); - registerMethod(L, "MonsterSpell", "setRange", MonsterSpellFunctions::luaMonsterSpellSetRange); - registerMethod(L, "MonsterSpell", "setCombatValue", MonsterSpellFunctions::luaMonsterSpellSetCombatValue); - registerMethod(L, "MonsterSpell", "setCombatType", MonsterSpellFunctions::luaMonsterSpellSetCombatType); - registerMethod(L, "MonsterSpell", "setAttackValue", MonsterSpellFunctions::luaMonsterSpellSetAttackValue); - registerMethod(L, "MonsterSpell", "setNeedTarget", MonsterSpellFunctions::luaMonsterSpellSetNeedTarget); - registerMethod(L, "MonsterSpell", "setCombatLength", MonsterSpellFunctions::luaMonsterSpellSetCombatLength); - registerMethod(L, "MonsterSpell", "setCombatSpread", MonsterSpellFunctions::luaMonsterSpellSetCombatSpread); - registerMethod(L, "MonsterSpell", "setCombatRadius", MonsterSpellFunctions::luaMonsterSpellSetCombatRadius); - registerMethod(L, "MonsterSpell", "setConditionType", MonsterSpellFunctions::luaMonsterSpellSetConditionType); - registerMethod(L, "MonsterSpell", "setConditionDamage", MonsterSpellFunctions::luaMonsterSpellSetConditionDamage); - registerMethod(L, "MonsterSpell", "setConditionSpeedChange", MonsterSpellFunctions::luaMonsterSpellSetConditionSpeedChange); - registerMethod(L, "MonsterSpell", "setConditionDuration", MonsterSpellFunctions::luaMonsterSpellSetConditionDuration); - registerMethod(L, "MonsterSpell", "setConditionTickInterval", MonsterSpellFunctions::luaMonsterSpellSetConditionTickInterval); - registerMethod(L, "MonsterSpell", "setCombatShootEffect", MonsterSpellFunctions::luaMonsterSpellSetCombatShootEffect); - registerMethod(L, "MonsterSpell", "setCombatEffect", MonsterSpellFunctions::luaMonsterSpellSetCombatEffect); - registerMethod(L, "MonsterSpell", "setOutfitMonster", MonsterSpellFunctions::luaMonsterSpellSetOutfitMonster); - registerMethod(L, "MonsterSpell", "setOutfitItem", MonsterSpellFunctions::luaMonsterSpellSetOutfitItem); - registerMethod(L, "MonsterSpell", "castSound", MonsterSpellFunctions::luaMonsterSpellCastSound); - registerMethod(L, "MonsterSpell", "impactSound", MonsterSpellFunctions::luaMonsterSpellImpactSound); - } + static void init(lua_State* L); private: static int luaCreateMonsterSpell(lua_State* L); diff --git a/src/lua/functions/creatures/monster/monster_type_functions.cpp b/src/lua/functions/creatures/monster/monster_type_functions.cpp index 3df28d12738..07ce44b626c 100644 --- a/src/lua/functions/creatures/monster/monster_type_functions.cpp +++ b/src/lua/functions/creatures/monster/monster_type_functions.cpp @@ -17,6 +17,141 @@ #include "io/io_bosstiary.hpp" #include "lua/scripts/scripts.hpp" #include "utils/tools.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void MonsterTypeFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "MonsterType", "", MonsterTypeFunctions::luaMonsterTypeCreate); + Lua::registerMetaMethod(L, "MonsterType", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "MonsterType", "isAttackable", MonsterTypeFunctions::luaMonsterTypeIsAttackable); + Lua::registerMethod(L, "MonsterType", "isConvinceable", MonsterTypeFunctions::luaMonsterTypeIsConvinceable); + Lua::registerMethod(L, "MonsterType", "isSummonable", MonsterTypeFunctions::luaMonsterTypeIsSummonable); + Lua::registerMethod(L, "MonsterType", "isPreyable", MonsterTypeFunctions::luaMonsterTypeIsPreyable); + Lua::registerMethod(L, "MonsterType", "isPreyExclusive", MonsterTypeFunctions::luaMonsterTypeIsPreyExclusive); + Lua::registerMethod(L, "MonsterType", "isIllusionable", MonsterTypeFunctions::luaMonsterTypeIsIllusionable); + Lua::registerMethod(L, "MonsterType", "isHostile", MonsterTypeFunctions::luaMonsterTypeIsHostile); + Lua::registerMethod(L, "MonsterType", "isPushable", MonsterTypeFunctions::luaMonsterTypeIsPushable); + Lua::registerMethod(L, "MonsterType", "isHealthHidden", MonsterTypeFunctions::luaMonsterTypeIsHealthHidden); + Lua::registerMethod(L, "MonsterType", "isBlockable", MonsterTypeFunctions::luaMonsterTypeIsBlockable); + Lua::registerMethod(L, "MonsterType", "isForgeCreature", MonsterTypeFunctions::luaMonsterTypeIsForgeCreature); + + Lua::registerMethod(L, "MonsterType", "familiar", MonsterTypeFunctions::luaMonsterTypeFamiliar); + Lua::registerMethod(L, "MonsterType", "isRewardBoss", MonsterTypeFunctions::luaMonsterTypeIsRewardBoss); + + Lua::registerMethod(L, "MonsterType", "canSpawn", MonsterTypeFunctions::luaMonsterTypeCanSpawn); + + Lua::registerMethod(L, "MonsterType", "canPushItems", MonsterTypeFunctions::luaMonsterTypeCanPushItems); + Lua::registerMethod(L, "MonsterType", "canPushCreatures", MonsterTypeFunctions::luaMonsterTypeCanPushCreatures); + + Lua::registerMethod(L, "MonsterType", "critChance", MonsterTypeFunctions::luaMonsterTypeCritChance); + + Lua::registerMethod(L, "MonsterType", "name", MonsterTypeFunctions::luaMonsterTypeName); + + Lua::registerMethod(L, "MonsterType", "nameDescription", MonsterTypeFunctions::luaMonsterTypeNameDescription); + + Lua::registerMethod(L, "MonsterType", "getCorpseId", MonsterTypeFunctions::luaMonsterTypegetCorpseId); + + Lua::registerMethod(L, "MonsterType", "health", MonsterTypeFunctions::luaMonsterTypeHealth); + Lua::registerMethod(L, "MonsterType", "maxHealth", MonsterTypeFunctions::luaMonsterTypeMaxHealth); + Lua::registerMethod(L, "MonsterType", "runHealth", MonsterTypeFunctions::luaMonsterTypeRunHealth); + Lua::registerMethod(L, "MonsterType", "experience", MonsterTypeFunctions::luaMonsterTypeExperience); + + Lua::registerMethod(L, "MonsterType", "faction", MonsterTypeFunctions::luaMonsterTypeFaction); + Lua::registerMethod(L, "MonsterType", "enemyFactions", MonsterTypeFunctions::luaMonsterTypeEnemyFactions); + Lua::registerMethod(L, "MonsterType", "targetPreferPlayer", MonsterTypeFunctions::luaMonsterTypeTargetPreferPlayer); + Lua::registerMethod(L, "MonsterType", "targetPreferMaster", MonsterTypeFunctions::luaMonsterTypeTargetPreferMaster); + + Lua::registerMethod(L, "MonsterType", "raceId", MonsterTypeFunctions::luaMonsterTypeRaceid); + Lua::registerMethod(L, "MonsterType", "Bestiaryclass", MonsterTypeFunctions::luaMonsterTypeBestiaryclass); + Lua::registerMethod(L, "MonsterType", "BestiaryOccurrence", MonsterTypeFunctions::luaMonsterTypeBestiaryOccurrence); + Lua::registerMethod(L, "MonsterType", "BestiaryLocations", MonsterTypeFunctions::luaMonsterTypeBestiaryLocations); + Lua::registerMethod(L, "MonsterType", "BestiaryStars", MonsterTypeFunctions::luaMonsterTypeBestiaryStars); + Lua::registerMethod(L, "MonsterType", "BestiaryCharmsPoints", MonsterTypeFunctions::luaMonsterTypeBestiaryCharmsPoints); + Lua::registerMethod(L, "MonsterType", "BestiarySecondUnlock", MonsterTypeFunctions::luaMonsterTypeBestiarySecondUnlock); + Lua::registerMethod(L, "MonsterType", "BestiaryFirstUnlock", MonsterTypeFunctions::luaMonsterTypeBestiaryFirstUnlock); + Lua::registerMethod(L, "MonsterType", "BestiarytoKill", MonsterTypeFunctions::luaMonsterTypeBestiarytoKill); + Lua::registerMethod(L, "MonsterType", "Bestiaryrace", MonsterTypeFunctions::luaMonsterTypeBestiaryrace); + + Lua::registerMethod(L, "MonsterType", "combatImmunities", MonsterTypeFunctions::luaMonsterTypeCombatImmunities); + Lua::registerMethod(L, "MonsterType", "conditionImmunities", MonsterTypeFunctions::luaMonsterTypeConditionImmunities); + + Lua::registerMethod(L, "MonsterType", "getAttackList", MonsterTypeFunctions::luaMonsterTypeGetAttackList); + Lua::registerMethod(L, "MonsterType", "addAttack", MonsterTypeFunctions::luaMonsterTypeAddAttack); + + Lua::registerMethod(L, "MonsterType", "getDefenseList", MonsterTypeFunctions::luaMonsterTypeGetDefenseList); + Lua::registerMethod(L, "MonsterType", "addDefense", MonsterTypeFunctions::luaMonsterTypeAddDefense); + + Lua::registerMethod(L, "MonsterType", "getTypeName", MonsterTypeFunctions::luaMonsterTypeGetTypeName); + + Lua::registerMethod(L, "MonsterType", "getElementList", MonsterTypeFunctions::luaMonsterTypeGetElementList); + Lua::registerMethod(L, "MonsterType", "addElement", MonsterTypeFunctions::luaMonsterTypeAddElement); + + Lua::registerMethod(L, "MonsterType", "addReflect", MonsterTypeFunctions::luaMonsterTypeAddReflect); + Lua::registerMethod(L, "MonsterType", "addHealing", MonsterTypeFunctions::luaMonsterTypeAddHealing); + + Lua::registerMethod(L, "MonsterType", "getVoices", MonsterTypeFunctions::luaMonsterTypeGetVoices); + Lua::registerMethod(L, "MonsterType", "addVoice", MonsterTypeFunctions::luaMonsterTypeAddVoice); + + Lua::registerMethod(L, "MonsterType", "getLoot", MonsterTypeFunctions::luaMonsterTypeGetLoot); + Lua::registerMethod(L, "MonsterType", "addLoot", MonsterTypeFunctions::luaMonsterTypeAddLoot); + + Lua::registerMethod(L, "MonsterType", "getCreatureEvents", MonsterTypeFunctions::luaMonsterTypeGetCreatureEvents); + Lua::registerMethod(L, "MonsterType", "registerEvent", MonsterTypeFunctions::luaMonsterTypeRegisterEvent); + + Lua::registerMethod(L, "MonsterType", "eventType", MonsterTypeFunctions::luaMonsterTypeEventType); + Lua::registerMethod(L, "MonsterType", "onThink", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); + Lua::registerMethod(L, "MonsterType", "onAppear", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); + Lua::registerMethod(L, "MonsterType", "onDisappear", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); + Lua::registerMethod(L, "MonsterType", "onMove", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); + Lua::registerMethod(L, "MonsterType", "onSay", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); + Lua::registerMethod(L, "MonsterType", "onPlayerAttack", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); + Lua::registerMethod(L, "MonsterType", "onSpawn", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); + + Lua::registerMethod(L, "MonsterType", "getSummonList", MonsterTypeFunctions::luaMonsterTypeGetSummonList); + Lua::registerMethod(L, "MonsterType", "addSummon", MonsterTypeFunctions::luaMonsterTypeAddSummon); + + Lua::registerMethod(L, "MonsterType", "maxSummons", MonsterTypeFunctions::luaMonsterTypeMaxSummons); + + Lua::registerMethod(L, "MonsterType", "armor", MonsterTypeFunctions::luaMonsterTypeArmor); + Lua::registerMethod(L, "MonsterType", "mitigation", MonsterTypeFunctions::luaMonsterTypeMitigation); + Lua::registerMethod(L, "MonsterType", "defense", MonsterTypeFunctions::luaMonsterTypeDefense); + Lua::registerMethod(L, "MonsterType", "outfit", MonsterTypeFunctions::luaMonsterTypeOutfit); + Lua::registerMethod(L, "MonsterType", "race", MonsterTypeFunctions::luaMonsterTypeRace); + Lua::registerMethod(L, "MonsterType", "corpseId", MonsterTypeFunctions::luaMonsterTypeCorpseId); + Lua::registerMethod(L, "MonsterType", "manaCost", MonsterTypeFunctions::luaMonsterTypeManaCost); + Lua::registerMethod(L, "MonsterType", "baseSpeed", MonsterTypeFunctions::luaMonsterTypeBaseSpeed); + Lua::registerMethod(L, "MonsterType", "light", MonsterTypeFunctions::luaMonsterTypeLight); + + Lua::registerMethod(L, "MonsterType", "staticAttackChance", MonsterTypeFunctions::luaMonsterTypeStaticAttackChance); + Lua::registerMethod(L, "MonsterType", "targetDistance", MonsterTypeFunctions::luaMonsterTypeTargetDistance); + Lua::registerMethod(L, "MonsterType", "yellChance", MonsterTypeFunctions::luaMonsterTypeYellChance); + Lua::registerMethod(L, "MonsterType", "yellSpeedTicks", MonsterTypeFunctions::luaMonsterTypeYellSpeedTicks); + Lua::registerMethod(L, "MonsterType", "changeTargetChance", MonsterTypeFunctions::luaMonsterTypeChangeTargetChance); + Lua::registerMethod(L, "MonsterType", "changeTargetSpeed", MonsterTypeFunctions::luaMonsterTypeChangeTargetSpeed); + + Lua::registerMethod(L, "MonsterType", "canWalkOnEnergy", MonsterTypeFunctions::luaMonsterTypeCanWalkOnEnergy); + Lua::registerMethod(L, "MonsterType", "canWalkOnFire", MonsterTypeFunctions::luaMonsterTypeCanWalkOnFire); + Lua::registerMethod(L, "MonsterType", "canWalkOnPoison", MonsterTypeFunctions::luaMonsterTypeCanWalkOnPoison); + + Lua::registerMethod(L, "MonsterType", "strategiesTargetNearest", MonsterTypeFunctions::luaMonsterTypeStrategiesTargetNearest); + Lua::registerMethod(L, "MonsterType", "strategiesTargetHealth", MonsterTypeFunctions::luaMonsterTypeStrategiesTargetHealth); + Lua::registerMethod(L, "MonsterType", "strategiesTargetDamage", MonsterTypeFunctions::luaMonsterTypeStrategiesTargetDamage); + Lua::registerMethod(L, "MonsterType", "strategiesTargetRandom", MonsterTypeFunctions::luaMonsterTypeStrategiesTargetRandom); + + Lua::registerMethod(L, "MonsterType", "respawnTypePeriod", MonsterTypeFunctions::luaMonsterTypeRespawnTypePeriod); + Lua::registerMethod(L, "MonsterType", "respawnTypeIsUnderground", MonsterTypeFunctions::luaMonsterTypeRespawnTypeIsUnderground); + + Lua::registerMethod(L, "MonsterType", "bossRace", MonsterTypeFunctions::luaMonsterTypeBossRace); + Lua::registerMethod(L, "MonsterType", "bossRaceId", MonsterTypeFunctions::luaMonsterTypeBossRaceId); + + Lua::registerMethod(L, "MonsterType", "soundChance", MonsterTypeFunctions::luaMonsterTypeSoundChance); + Lua::registerMethod(L, "MonsterType", "soundSpeedTicks", MonsterTypeFunctions::luaMonsterTypeSoundSpeedTicks); + Lua::registerMethod(L, "MonsterType", "addSound", MonsterTypeFunctions::luaMonsterTypeAddSound); + Lua::registerMethod(L, "MonsterType", "getSounds", MonsterTypeFunctions::luaMonsterTypeGetSounds); + Lua::registerMethod(L, "MonsterType", "deathSound", MonsterTypeFunctions::luaMonsterTypedeathSound); + + Lua::registerMethod(L, "MonsterType", "variant", MonsterTypeFunctions::luaMonsterTypeVariant); +} void MonsterTypeFunctions::createMonsterTypeLootLuaTable(lua_State* L, const std::vector &lootList) { lua_createtable(L, lootList.size(), 0); @@ -25,14 +160,14 @@ void MonsterTypeFunctions::createMonsterTypeLootLuaTable(lua_State* L, const std for (const auto &lootBlock : lootList) { lua_createtable(L, 0, 8); - setField(L, "itemId", lootBlock.id); - setField(L, "chance", lootBlock.chance); - setField(L, "subType", lootBlock.subType); - setField(L, "maxCount", lootBlock.countmax); - setField(L, "minCount", lootBlock.countmin); - setField(L, "actionId", lootBlock.actionId); - setField(L, "text", lootBlock.text); - pushBoolean(L, lootBlock.unique); + Lua::setField(L, "itemId", lootBlock.id); + Lua::setField(L, "chance", lootBlock.chance); + Lua::setField(L, "subType", lootBlock.subType); + Lua::setField(L, "maxCount", lootBlock.countmax); + Lua::setField(L, "minCount", lootBlock.countmin); + Lua::setField(L, "actionId", lootBlock.actionId); + Lua::setField(L, "text", lootBlock.text); + Lua::pushBoolean(L, lootBlock.unique); lua_setfield(L, -2, "unique"); createMonsterTypeLootLuaTable(L, lootBlock.childLoot); @@ -45,15 +180,15 @@ void MonsterTypeFunctions::createMonsterTypeLootLuaTable(lua_State* L, const std int MonsterTypeFunctions::luaMonsterTypeCreate(lua_State* L) { // MonsterType(name or raceid) std::shared_ptr monsterType; - if (isNumber(L, 2)) { - monsterType = g_monsters().getMonsterTypeByRaceId(getNumber(L, 2)); + if (Lua::isNumber(L, 2)) { + monsterType = g_monsters().getMonsterTypeByRaceId(Lua::getNumber(L, 2)); } else { - monsterType = g_monsters().getMonsterType(getString(L, 2)); + monsterType = g_monsters().getMonsterType(Lua::getString(L, 2)); } if (monsterType) { - pushUserdata(L, monsterType); - setMetatable(L, -1, "MonsterType"); + Lua::pushUserdata(L, monsterType); + Lua::setMetatable(L, -1, "MonsterType"); } else { lua_pushnil(L); } @@ -62,13 +197,13 @@ int MonsterTypeFunctions::luaMonsterTypeCreate(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsAttackable(lua_State* L) { // get: monsterType:isAttackable() set: monsterType:isAttackable(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isAttackable); + Lua::pushBoolean(L, monsterType->info.isAttackable); } else { - monsterType->info.isAttackable = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isAttackable = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -78,13 +213,13 @@ int MonsterTypeFunctions::luaMonsterTypeIsAttackable(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsConvinceable(lua_State* L) { // get: monsterType:isConvinceable() set: monsterType:isConvinceable(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isConvinceable); + Lua::pushBoolean(L, monsterType->info.isConvinceable); } else { - monsterType->info.isConvinceable = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isConvinceable = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -94,13 +229,13 @@ int MonsterTypeFunctions::luaMonsterTypeIsConvinceable(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsSummonable(lua_State* L) { // get: monsterType:isSummonable() set: monsterType:isSummonable(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isSummonable); + Lua::pushBoolean(L, monsterType->info.isSummonable); } else { - monsterType->info.isSummonable = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isSummonable = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -110,13 +245,13 @@ int MonsterTypeFunctions::luaMonsterTypeIsSummonable(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsPreyExclusive(lua_State* L) { // get: monsterType:isPreyExclusive() set: monsterType:isPreyExclusive(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isPreyExclusive); + Lua::pushBoolean(L, monsterType->info.isPreyExclusive); } else { - monsterType->info.isPreyExclusive = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isPreyExclusive = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -126,13 +261,13 @@ int MonsterTypeFunctions::luaMonsterTypeIsPreyExclusive(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsPreyable(lua_State* L) { // get: monsterType:isPreyable() set: monsterType:isPreyable(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isPreyable); + Lua::pushBoolean(L, monsterType->info.isPreyable); } else { - monsterType->info.isPreyable = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isPreyable = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -142,13 +277,13 @@ int MonsterTypeFunctions::luaMonsterTypeIsPreyable(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsIllusionable(lua_State* L) { // get: monsterType:isIllusionable() set: monsterType:isIllusionable(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isIllusionable); + Lua::pushBoolean(L, monsterType->info.isIllusionable); } else { - monsterType->info.isIllusionable = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isIllusionable = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -158,13 +293,13 @@ int MonsterTypeFunctions::luaMonsterTypeIsIllusionable(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsHostile(lua_State* L) { // get: monsterType:isHostile() set: monsterType:isHostile(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isHostile); + Lua::pushBoolean(L, monsterType->info.isHostile); } else { - monsterType->info.isHostile = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isHostile = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -174,13 +309,13 @@ int MonsterTypeFunctions::luaMonsterTypeIsHostile(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeFamiliar(lua_State* L) { // get: monsterType:familiar() set: monsterType:familiar(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isFamiliar); + Lua::pushBoolean(L, monsterType->info.isFamiliar); } else { - monsterType->info.isFamiliar = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isFamiliar = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -190,13 +325,13 @@ int MonsterTypeFunctions::luaMonsterTypeFamiliar(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsRewardBoss(lua_State* L) { // get: monsterType:isRewardBoss() set: monsterType:isRewardBoss(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isRewardBoss); + Lua::pushBoolean(L, monsterType->info.isRewardBoss); } else { - monsterType->info.isRewardBoss = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isRewardBoss = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -206,13 +341,13 @@ int MonsterTypeFunctions::luaMonsterTypeIsRewardBoss(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsPushable(lua_State* L) { // get: monsterType:isPushable() set: monsterType:isPushable(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.pushable); + Lua::pushBoolean(L, monsterType->info.pushable); } else { - monsterType->info.pushable = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.pushable = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -222,13 +357,13 @@ int MonsterTypeFunctions::luaMonsterTypeIsPushable(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsHealthHidden(lua_State* L) { // get: monsterType:isHealthHidden() set: monsterType:isHealthHidden(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.hiddenHealth); + Lua::pushBoolean(L, monsterType->info.hiddenHealth); } else { - monsterType->info.hiddenHealth = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.hiddenHealth = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -238,13 +373,13 @@ int MonsterTypeFunctions::luaMonsterTypeIsHealthHidden(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsBlockable(lua_State* L) { // get: monsterType:isBlockable() set: monsterType:isBlockable(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isBlockable); + Lua::pushBoolean(L, monsterType->info.isBlockable); } else { - monsterType->info.isBlockable = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isBlockable = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -254,28 +389,28 @@ int MonsterTypeFunctions::luaMonsterTypeIsBlockable(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeIsForgeCreature(lua_State* L) { // get: monsterType:isForgeCreature() set: monsterType:isForgeCreature(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - pushBoolean(L, false); - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); return 0; } if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.isForgeCreature); + Lua::pushBoolean(L, monsterType->info.isForgeCreature); } else { - monsterType->info.isForgeCreature = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.isForgeCreature = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } return 1; } int MonsterTypeFunctions::luaMonsterTypeCanSpawn(lua_State* L) { // monsterType:canSpawn(pos) - const auto &monsterType = getUserdataShared(L, 1); - const Position &position = getPosition(L, 2); + const auto &monsterType = Lua::getUserdataShared(L, 1); + const Position &position = Lua::getPosition(L, 2); if (monsterType) { - pushBoolean(L, monsterType->canSpawn(position)); + Lua::pushBoolean(L, monsterType->canSpawn(position)); } else { lua_pushnil(L); } @@ -284,13 +419,13 @@ int MonsterTypeFunctions::luaMonsterTypeCanSpawn(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeCanPushItems(lua_State* L) { // get: monsterType:canPushItems() set: monsterType:canPushItems(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.canPushItems); + Lua::pushBoolean(L, monsterType->info.canPushItems); } else { - monsterType->info.canPushItems = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.canPushItems = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -300,13 +435,13 @@ int MonsterTypeFunctions::luaMonsterTypeCanPushItems(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeCanPushCreatures(lua_State* L) { // get: monsterType:canPushCreatures() set: monsterType:canPushCreatures(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.canPushCreatures); + Lua::pushBoolean(L, monsterType->info.canPushCreatures); } else { - monsterType->info.canPushCreatures = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.canPushCreatures = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -316,10 +451,10 @@ int MonsterTypeFunctions::luaMonsterTypeCanPushCreatures(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeCritChance(lua_State* L) { // get: monsterType:critChance() set: monsterType:critChance(int) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 2) { - monsterType->info.critChance = getNumber(L, 2); + monsterType->info.critChance = Lua::getNumber(L, 2); } lua_pushnumber(L, monsterType->info.critChance); } else { @@ -330,13 +465,13 @@ int MonsterTypeFunctions::luaMonsterTypeCritChance(lua_State* L) { int32_t MonsterTypeFunctions::luaMonsterTypeName(lua_State* L) { // get: monsterType:name() set: monsterType:name(name) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushString(L, monsterType->name); + Lua::pushString(L, monsterType->name); } else { - monsterType->name = getString(L, 2); - pushBoolean(L, true); + monsterType->name = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -346,13 +481,13 @@ int32_t MonsterTypeFunctions::luaMonsterTypeName(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeNameDescription(lua_State* L) { // get: monsterType:nameDescription() set: monsterType:nameDescription(desc) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushString(L, monsterType->nameDescription); + Lua::pushString(L, monsterType->nameDescription); } else { - monsterType->nameDescription = getString(L, 2); - pushBoolean(L, true); + monsterType->nameDescription = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -362,7 +497,7 @@ int MonsterTypeFunctions::luaMonsterTypeNameDescription(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypegetCorpseId(lua_State* L) { // monsterType:getCorpseId() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { lua_pushnumber(L, monsterType->info.lookcorpse); } else { @@ -373,13 +508,13 @@ int MonsterTypeFunctions::luaMonsterTypegetCorpseId(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeHealth(lua_State* L) { // get: monsterType:health() set: monsterType:health(health) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.health); } else { - monsterType->info.health = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.health = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -389,13 +524,13 @@ int MonsterTypeFunctions::luaMonsterTypeHealth(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeMaxHealth(lua_State* L) { // get: monsterType:maxHealth() set: monsterType:maxHealth(health) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.healthMax); } else { - monsterType->info.healthMax = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.healthMax = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -405,13 +540,13 @@ int MonsterTypeFunctions::luaMonsterTypeMaxHealth(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeRunHealth(lua_State* L) { // get: monsterType:runHealth() set: monsterType:runHealth(health) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.runAwayHealth); } else { - monsterType->info.runAwayHealth = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.runAwayHealth = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -421,13 +556,13 @@ int MonsterTypeFunctions::luaMonsterTypeRunHealth(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeExperience(lua_State* L) { // get: monsterType:experience() set: monsterType:experience(exp) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.experience); } else { - monsterType->info.experience = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.experience = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -437,13 +572,13 @@ int MonsterTypeFunctions::luaMonsterTypeExperience(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeFaction(lua_State* L) { // get: monsterType:faction() set: monsterType:faction(faction) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.faction); } else { - monsterType->info.faction = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.faction = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -453,7 +588,7 @@ int MonsterTypeFunctions::luaMonsterTypeFaction(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeEnemyFactions(lua_State* L) { // get: monsterType:enemyFactions() set: monsterType:enemyFactions(enemyFaction) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_createtable(L, monsterType->info.enemyFactions.size(), 0); @@ -464,9 +599,9 @@ int MonsterTypeFunctions::luaMonsterTypeEnemyFactions(lua_State* L) { lua_rawseti(L, -2, ++index); } } else { - const Faction_t faction = getNumber(L, 2); + const Faction_t faction = Lua::getNumber(L, 2); monsterType->info.enemyFactions.insert(faction); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -476,13 +611,13 @@ int MonsterTypeFunctions::luaMonsterTypeEnemyFactions(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeTargetPreferPlayer(lua_State* L) { // get: monsterType:targetPreferPlayer() set: monsterType:targetPreferPlayer(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushboolean(L, monsterType->info.targetPreferPlayer); } else { - monsterType->info.targetPreferPlayer = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.targetPreferPlayer = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -492,13 +627,13 @@ int MonsterTypeFunctions::luaMonsterTypeTargetPreferPlayer(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeTargetPreferMaster(lua_State* L) { // get: monsterType:targetPreferMaster() set: monsterType:targetPreferMaster(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.faction); } else { - monsterType->info.targetPreferMaster = getBoolean(L, 2); - pushBoolean(L, true); + monsterType->info.targetPreferMaster = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -508,14 +643,14 @@ int MonsterTypeFunctions::luaMonsterTypeTargetPreferMaster(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeRaceid(lua_State* L) { // get: monsterType:raceId() set: monsterType:raceId(id) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.raceid); } else { - monsterType->info.raceid = getNumber(L, 2); - g_game().addBestiaryList(getNumber(L, 2), monsterType->name); - pushBoolean(L, true); + monsterType->info.raceid = Lua::getNumber(L, 2); + g_game().addBestiaryList(Lua::getNumber(L, 2), monsterType->name); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -525,13 +660,13 @@ int MonsterTypeFunctions::luaMonsterTypeRaceid(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBestiarytoKill(lua_State* L) { // get: monsterType:BestiarytoKill() set: monsterType:BestiarytoKill(value) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.bestiaryToUnlock); } else { - monsterType->info.bestiaryToUnlock = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.bestiaryToUnlock = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -541,13 +676,13 @@ int MonsterTypeFunctions::luaMonsterTypeBestiarytoKill(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBestiaryFirstUnlock(lua_State* L) { // get: monsterType:BestiaryFirstUnlock() set: monsterType:BestiaryFirstUnlock(value) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.bestiaryFirstUnlock); } else { - monsterType->info.bestiaryFirstUnlock = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.bestiaryFirstUnlock = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -557,13 +692,13 @@ int MonsterTypeFunctions::luaMonsterTypeBestiaryFirstUnlock(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBestiarySecondUnlock(lua_State* L) { // get: monsterType:BestiarySecondUnlock() set: monsterType:BestiarySecondUnlock(value) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.bestiarySecondUnlock); } else { - monsterType->info.bestiarySecondUnlock = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.bestiarySecondUnlock = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -573,13 +708,13 @@ int MonsterTypeFunctions::luaMonsterTypeBestiarySecondUnlock(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBestiaryCharmsPoints(lua_State* L) { // get: monsterType:BestiaryCharmsPoints() set: monsterType:BestiaryCharmsPoints(value) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.bestiaryCharmsPoints); } else { - monsterType->info.bestiaryCharmsPoints = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.bestiaryCharmsPoints = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -589,13 +724,13 @@ int MonsterTypeFunctions::luaMonsterTypeBestiaryCharmsPoints(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBestiaryStars(lua_State* L) { // get: monsterType:BestiaryStars() set: monsterType:BestiaryStars(value) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.bestiaryStars); } else { - monsterType->info.bestiaryStars = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.bestiaryStars = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -605,13 +740,13 @@ int MonsterTypeFunctions::luaMonsterTypeBestiaryStars(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBestiaryOccurrence(lua_State* L) { // get: monsterType:BestiaryOccurrence() set: monsterType:BestiaryOccurrence(value) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.bestiaryOccurrence); } else { - monsterType->info.bestiaryOccurrence = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.bestiaryOccurrence = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -621,13 +756,13 @@ int MonsterTypeFunctions::luaMonsterTypeBestiaryOccurrence(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBestiaryLocations(lua_State* L) { // get: monsterType:BestiaryLocations() set: monsterType:BestiaryLocations(string) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushString(L, monsterType->info.bestiaryLocations); + Lua::pushString(L, monsterType->info.bestiaryLocations); } else { - monsterType->info.bestiaryLocations = getString(L, 2); - pushBoolean(L, true); + monsterType->info.bestiaryLocations = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -637,13 +772,13 @@ int MonsterTypeFunctions::luaMonsterTypeBestiaryLocations(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBestiaryclass(lua_State* L) { // get: monsterType:Bestiaryclass() set: monsterType:Bestiaryclass(string) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushString(L, monsterType->info.bestiaryClass); + Lua::pushString(L, monsterType->info.bestiaryClass); } else { - monsterType->info.bestiaryClass = getString(L, 2); - pushBoolean(L, true); + monsterType->info.bestiaryClass = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -653,14 +788,14 @@ int MonsterTypeFunctions::luaMonsterTypeBestiaryclass(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBestiaryrace(lua_State* L) { // get: monsterType:Bestiaryrace() set: monsterType:Bestiaryrace(raceid) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.bestiaryRace); } else { - const BestiaryType_t race = getNumber(L, 2); + const BestiaryType_t race = Lua::getNumber(L, 2); monsterType->info.bestiaryRace = race; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -670,10 +805,10 @@ int MonsterTypeFunctions::luaMonsterTypeBestiaryrace(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeCombatImmunities(lua_State* L) { // get: monsterType:combatImmunities() set: monsterType:combatImmunities(immunity) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - pushBoolean(L, false); - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); return 1; } @@ -686,7 +821,7 @@ int MonsterTypeFunctions::luaMonsterTypeCombatImmunities(lua_State* L) { return 1; } - std::string immunity = getString(L, 2); + std::string immunity = Lua::getString(L, 2); CombatType_t combatType = COMBAT_NONE; if (immunity == "physical") { combatType = COMBAT_PHYSICALDAMAGE; @@ -720,16 +855,16 @@ int MonsterTypeFunctions::luaMonsterTypeCombatImmunities(lua_State* L) { } monsterType->info.m_damageImmunities.set(combatTypeToIndex(combatType), true); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int MonsterTypeFunctions::luaMonsterTypeConditionImmunities(lua_State* L) { // get: monsterType:conditionImmunities() set: monsterType:conditionImmunities(immunity) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - pushBoolean(L, false); - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); return 1; } @@ -742,7 +877,7 @@ int MonsterTypeFunctions::luaMonsterTypeConditionImmunities(lua_State* L) { return 1; } - std::string immunity = getString(L, 2); + std::string immunity = Lua::getString(L, 2); ConditionType_t conditionType = CONDITION_NONE; if (immunity == "physical") { conditionType = CONDITION_BLEEDING; @@ -778,13 +913,13 @@ int MonsterTypeFunctions::luaMonsterTypeConditionImmunities(lua_State* L) { } monsterType->info.m_conditionImmunities[static_cast(conditionType)] = true; - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int MonsterTypeFunctions::luaMonsterTypeGetAttackList(lua_State* L) { // monsterType:getAttackList() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { lua_pushnil(L); return 1; @@ -796,15 +931,15 @@ int MonsterTypeFunctions::luaMonsterTypeGetAttackList(lua_State* L) { for (const auto &spellBlock : monsterType->info.attackSpells) { lua_createtable(L, 0, 8); - setField(L, "chance", spellBlock.chance); - setField(L, "isCombatSpell", spellBlock.combatSpell ? 1 : 0); - setField(L, "isMelee", spellBlock.isMelee ? 1 : 0); - setField(L, "minCombatValue", spellBlock.minCombatValue); - setField(L, "maxCombatValue", spellBlock.maxCombatValue); - setField(L, "range", spellBlock.range); - setField(L, "speed", spellBlock.speed); + Lua::setField(L, "chance", spellBlock.chance); + Lua::setField(L, "isCombatSpell", spellBlock.combatSpell ? 1 : 0); + Lua::setField(L, "isMelee", spellBlock.isMelee ? 1 : 0); + Lua::setField(L, "minCombatValue", spellBlock.minCombatValue); + Lua::setField(L, "maxCombatValue", spellBlock.maxCombatValue); + Lua::setField(L, "range", spellBlock.range); + Lua::setField(L, "speed", spellBlock.speed); const auto combatSpell = std::static_pointer_cast(spellBlock.spell); - pushUserdata(L, combatSpell); + Lua::pushUserdata(L, combatSpell); lua_setfield(L, -2, "spell"); lua_rawseti(L, -2, ++index); @@ -814,9 +949,9 @@ int MonsterTypeFunctions::luaMonsterTypeGetAttackList(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeAddAttack(lua_State* L) { // monsterType:addAttack(monsterspell) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { - const auto &spell = getUserdataShared(L, 2); + const auto &spell = Lua::getUserdataShared(L, 2); if (spell) { spellBlock_t sb; if (g_monsters().deserializeSpell(spell, sb, monsterType->name)) { @@ -835,7 +970,7 @@ int MonsterTypeFunctions::luaMonsterTypeAddAttack(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeGetDefenseList(lua_State* L) { // monsterType:getDefenseList() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { lua_pushnil(L); return 1; @@ -847,15 +982,15 @@ int MonsterTypeFunctions::luaMonsterTypeGetDefenseList(lua_State* L) { for (const auto &spellBlock : monsterType->info.defenseSpells) { lua_createtable(L, 0, 8); - setField(L, "chance", spellBlock.chance); - setField(L, "isCombatSpell", spellBlock.combatSpell ? 1 : 0); - setField(L, "isMelee", spellBlock.isMelee ? 1 : 0); - setField(L, "minCombatValue", spellBlock.minCombatValue); - setField(L, "maxCombatValue", spellBlock.maxCombatValue); - setField(L, "range", spellBlock.range); - setField(L, "speed", spellBlock.speed); + Lua::setField(L, "chance", spellBlock.chance); + Lua::setField(L, "isCombatSpell", spellBlock.combatSpell ? 1 : 0); + Lua::setField(L, "isMelee", spellBlock.isMelee ? 1 : 0); + Lua::setField(L, "minCombatValue", spellBlock.minCombatValue); + Lua::setField(L, "maxCombatValue", spellBlock.maxCombatValue); + Lua::setField(L, "range", spellBlock.range); + Lua::setField(L, "speed", spellBlock.speed); const auto combatSpell = std::static_pointer_cast(spellBlock.spell); - pushUserdata(L, combatSpell); + Lua::pushUserdata(L, combatSpell); lua_setfield(L, -2, "spell"); lua_rawseti(L, -2, ++index); @@ -865,20 +1000,20 @@ int MonsterTypeFunctions::luaMonsterTypeGetDefenseList(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeGetTypeName(lua_State* L) { // monsterType:getTypeName() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { return 1; } - pushString(L, monsterType->typeName); + Lua::pushString(L, monsterType->typeName); return 1; } int MonsterTypeFunctions::luaMonsterTypeAddDefense(lua_State* L) { // monsterType:addDefense(monsterspell) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { - const auto &spell = getUserdataShared(L, 2); + const auto &spell = Lua::getUserdataShared(L, 2); if (spell) { spellBlock_t sb; if (g_monsters().deserializeSpell(spell, sb, monsterType->name)) { @@ -897,11 +1032,11 @@ int MonsterTypeFunctions::luaMonsterTypeAddDefense(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeAddElement(lua_State* L) { // monsterType:addElement(type, percent) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { - const CombatType_t element = getNumber(L, 2); - monsterType->info.elementMap[element] = getNumber(L, 3); - pushBoolean(L, true); + const CombatType_t element = Lua::getNumber(L, 2); + monsterType->info.elementMap[element] = Lua::getNumber(L, 3); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -910,11 +1045,11 @@ int MonsterTypeFunctions::luaMonsterTypeAddElement(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeAddReflect(lua_State* L) { // monsterType:addReflect(type, percent) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { - const CombatType_t element = getNumber(L, 2); - monsterType->info.reflectMap[element] = getNumber(L, 3); - pushBoolean(L, true); + const CombatType_t element = Lua::getNumber(L, 2); + monsterType->info.reflectMap[element] = Lua::getNumber(L, 3); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -923,11 +1058,11 @@ int MonsterTypeFunctions::luaMonsterTypeAddReflect(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeAddHealing(lua_State* L) { // monsterType:addHealing(type, percent) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { - const CombatType_t element = getNumber(L, 2); - monsterType->info.healingMap[element] = getNumber(L, 3); - pushBoolean(L, true); + const CombatType_t element = Lua::getNumber(L, 2); + monsterType->info.healingMap[element] = Lua::getNumber(L, 3); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -936,7 +1071,7 @@ int MonsterTypeFunctions::luaMonsterTypeAddHealing(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeGetElementList(lua_State* L) { // monsterType:getElementList() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { lua_pushnil(L); return 1; @@ -952,15 +1087,15 @@ int MonsterTypeFunctions::luaMonsterTypeGetElementList(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeAddVoice(lua_State* L) { // monsterType:addVoice(sentence, interval, chance, yell) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { voiceBlock_t voice; - voice.text = getString(L, 2); - monsterType->info.yellSpeedTicks = getNumber(L, 3); - monsterType->info.yellChance = getNumber(L, 4); - voice.yellText = getBoolean(L, 5); + voice.text = Lua::getString(L, 2); + monsterType->info.yellSpeedTicks = Lua::getNumber(L, 3); + monsterType->info.yellChance = Lua::getNumber(L, 4); + voice.yellText = Lua::getBoolean(L, 5); monsterType->info.voiceVector.push_back(voice); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -969,7 +1104,7 @@ int MonsterTypeFunctions::luaMonsterTypeAddVoice(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeGetVoices(lua_State* L) { // monsterType:getVoices() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { lua_pushnil(L); return 1; @@ -979,8 +1114,8 @@ int MonsterTypeFunctions::luaMonsterTypeGetVoices(lua_State* L) { lua_createtable(L, monsterType->info.voiceVector.size(), 0); for (const auto &voiceBlock : monsterType->info.voiceVector) { lua_createtable(L, 0, 2); - setField(L, "text", voiceBlock.text); - setField(L, "yellText", voiceBlock.yellText); + Lua::setField(L, "text", voiceBlock.text); + Lua::setField(L, "yellText", voiceBlock.yellText); lua_rawseti(L, -2, ++index); } return 1; @@ -988,7 +1123,7 @@ int MonsterTypeFunctions::luaMonsterTypeGetVoices(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeGetLoot(lua_State* L) { // monsterType:getLoot() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { lua_pushnil(L); return 1; @@ -1000,12 +1135,12 @@ int MonsterTypeFunctions::luaMonsterTypeGetLoot(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeAddLoot(lua_State* L) { // monsterType:addLoot(loot) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { - const auto &loot = getUserdataShared(L, 2); + const auto &loot = Lua::getUserdataShared(L, 2); if (loot) { monsterType->loadLoot(monsterType, loot->lootBlock); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1017,7 +1152,7 @@ int MonsterTypeFunctions::luaMonsterTypeAddLoot(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeGetCreatureEvents(lua_State* L) { // monsterType:getCreatureEvents() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { lua_pushnil(L); return 1; @@ -1026,7 +1161,7 @@ int MonsterTypeFunctions::luaMonsterTypeGetCreatureEvents(lua_State* L) { int index = 0; lua_createtable(L, monsterType->info.scripts.size(), 0); for (const std::string &creatureEvent : monsterType->info.scripts) { - pushString(L, creatureEvent); + Lua::pushString(L, creatureEvent); lua_rawseti(L, -2, ++index); } return 1; @@ -1034,16 +1169,16 @@ int MonsterTypeFunctions::luaMonsterTypeGetCreatureEvents(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeRegisterEvent(lua_State* L) { // monsterType:registerEvent(name) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { - const auto eventName = getString(L, 2); + const auto eventName = Lua::getString(L, 2); monsterType->info.scripts.insert(eventName); for (const auto &[_, monster] : g_game().getMonsters()) { if (monster->getMonsterType() == monsterType) { monster->registerCreatureEvent(eventName); } } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1058,13 +1193,13 @@ int MonsterTypeFunctions::luaMonsterTypeEventOnCallback(lua_State* L) { // monsterType:onSay(callback) // monsterType:onPlayerAttack(callback) // monsterType:onSpawn(callback) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (monsterType->loadCallback(&g_scripts().getScriptInterface())) { - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } - pushBoolean(L, false); + Lua::pushBoolean(L, false); } else { lua_pushnil(L); } @@ -1073,10 +1208,10 @@ int MonsterTypeFunctions::luaMonsterTypeEventOnCallback(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeEventType(lua_State* L) { // monstertype:eventType(event) - const auto &mType = getUserdataShared(L, 1); + const auto &mType = Lua::getUserdataShared(L, 1); if (mType) { - mType->info.eventType = getNumber(L, 2); - pushBoolean(L, true); + mType->info.eventType = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1085,7 +1220,7 @@ int MonsterTypeFunctions::luaMonsterTypeEventType(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeGetSummonList(lua_State* L) { // monsterType:getSummonList() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { lua_pushnil(L); return 1; @@ -1095,9 +1230,9 @@ int MonsterTypeFunctions::luaMonsterTypeGetSummonList(lua_State* L) { lua_createtable(L, monsterType->info.summons.size(), 0); for (const auto &summonBlock : monsterType->info.summons) { lua_createtable(L, 0, 3); - setField(L, "name", summonBlock.name); - setField(L, "speed", summonBlock.speed); - setField(L, "chance", summonBlock.chance); + Lua::setField(L, "name", summonBlock.name); + Lua::setField(L, "speed", summonBlock.speed); + Lua::setField(L, "chance", summonBlock.chance); lua_rawseti(L, -2, ++index); } return 1; @@ -1105,15 +1240,15 @@ int MonsterTypeFunctions::luaMonsterTypeGetSummonList(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeAddSummon(lua_State* L) { // monsterType:addSummon(name, interval, chance[, count = 1]) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { summonBlock_t summon; - summon.name = getString(L, 2); - summon.speed = getNumber(L, 3); - summon.count = getNumber(L, 5, 1); - summon.chance = getNumber(L, 4); + summon.name = Lua::getString(L, 2); + summon.speed = Lua::getNumber(L, 3); + summon.count = Lua::getNumber(L, 5, 1); + summon.chance = Lua::getNumber(L, 4); monsterType->info.summons.push_back(summon); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1122,13 +1257,13 @@ int MonsterTypeFunctions::luaMonsterTypeAddSummon(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeMaxSummons(lua_State* L) { // get: monsterType:maxSummons() set: monsterType:maxSummons(ammount) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.maxSummons); } else { - monsterType->info.maxSummons = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.maxSummons = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1138,13 +1273,13 @@ int MonsterTypeFunctions::luaMonsterTypeMaxSummons(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeArmor(lua_State* L) { // get: monsterType:armor() set: monsterType:armor(armor) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.armor); } else { - monsterType->info.armor = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.armor = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1154,31 +1289,31 @@ int MonsterTypeFunctions::luaMonsterTypeArmor(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeMitigation(lua_State* L) { // get: monsterType:mitigation() set: monsterType:mitigation(mitigation) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - pushBoolean(L, false); - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); return 1; } if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.mitigation); } else { - monsterType->info.mitigation = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.mitigation = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int MonsterTypeFunctions::luaMonsterTypeDefense(lua_State* L) { // get: monsterType:defense() set: monsterType:defense(defense) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.defense); } else { - monsterType->info.defense = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.defense = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1188,18 +1323,18 @@ int MonsterTypeFunctions::luaMonsterTypeDefense(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeOutfit(lua_State* L) { // get: monsterType:outfit() set: monsterType:outfit(outfit) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushOutfit(L, monsterType->info.outfit); + Lua::pushOutfit(L, monsterType->info.outfit); } else { - Outfit_t outfit = getOutfit(L, 2); + Outfit_t outfit = Lua::getOutfit(L, 2); if (g_configManager().getBoolean(WARN_UNSAFE_SCRIPTS) && outfit.lookType != 0 && !g_game().isLookTypeRegistered(outfit.lookType)) { g_logger().warn("[MonsterTypeFunctions::luaMonsterTypeOutfit] An unregistered creature looktype type with id '{}' was blocked to prevent client crash.", outfit.lookType); lua_pushnil(L); } else { monsterType->info.outfit = outfit; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } } } else { @@ -1210,8 +1345,8 @@ int MonsterTypeFunctions::luaMonsterTypeOutfit(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeRace(lua_State* L) { // get: monsterType:race() set: monsterType:race(race) - const auto &monsterType = getUserdataShared(L, 1); - std::string race = getString(L, 2); + const auto &monsterType = Lua::getUserdataShared(L, 1); + std::string race = Lua::getString(L, 2); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.race); @@ -1235,7 +1370,7 @@ int MonsterTypeFunctions::luaMonsterTypeRace(lua_State* L) { lua_pushnil(L); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1245,12 +1380,12 @@ int MonsterTypeFunctions::luaMonsterTypeRace(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeCorpseId(lua_State* L) { // get: monsterType:corpseId() set: monsterType:corpseId(id) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.lookcorpse); } else { - monsterType->info.lookcorpse = getNumber(L, 2); + monsterType->info.lookcorpse = Lua::getNumber(L, 2); lua_pushboolean(L, true); } } else { @@ -1261,13 +1396,13 @@ int MonsterTypeFunctions::luaMonsterTypeCorpseId(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeManaCost(lua_State* L) { // get: monsterType:manaCost() set: monsterType:manaCost(mana) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.manaCost); } else { - monsterType->info.manaCost = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.manaCost = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1277,13 +1412,13 @@ int MonsterTypeFunctions::luaMonsterTypeManaCost(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBaseSpeed(lua_State* L) { // monsterType:baseSpeed() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->getBaseSpeed()); } else { - monsterType->setBaseSpeed(getNumber(L, 2)); - pushBoolean(L, true); + monsterType->setBaseSpeed(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1292,7 +1427,7 @@ int MonsterTypeFunctions::luaMonsterTypeBaseSpeed(lua_State* L) { } int MonsterTypeFunctions::luaMonsterTypeLight(lua_State* L) { // get: monsterType:light() set: monsterType:light(color, level) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { lua_pushnil(L); return 1; @@ -1303,22 +1438,22 @@ int MonsterTypeFunctions::luaMonsterTypeLight(lua_State* L) { lua_pushnumber(L, monsterType->info.light.color); return 2; } else { - monsterType->info.light.color = getNumber(L, 2); - monsterType->info.light.level = getNumber(L, 3); - pushBoolean(L, true); + monsterType->info.light.color = Lua::getNumber(L, 2); + monsterType->info.light.level = Lua::getNumber(L, 3); + Lua::pushBoolean(L, true); } return 1; } int MonsterTypeFunctions::luaMonsterTypeStaticAttackChance(lua_State* L) { // get: monsterType:staticAttackChance() set: monsterType:staticAttackChance(chance) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.staticAttackChance); } else { - monsterType->info.staticAttackChance = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.staticAttackChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1328,13 +1463,13 @@ int MonsterTypeFunctions::luaMonsterTypeStaticAttackChance(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeTargetDistance(lua_State* L) { // get: monsterType:targetDistance() set: monsterType:targetDistance(distance) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.targetDistance); } else { - monsterType->info.targetDistance = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.targetDistance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1344,18 +1479,18 @@ int MonsterTypeFunctions::luaMonsterTypeTargetDistance(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeYellChance(lua_State* L) { // get: monsterType:yellChance() set: monsterType:yellChance(chance) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.yellChance); } else { - monsterType->info.yellChance = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.yellChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { - monsterType->info.yellChance = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.yellChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1365,13 +1500,13 @@ int MonsterTypeFunctions::luaMonsterTypeYellChance(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeYellSpeedTicks(lua_State* L) { // get: monsterType:yellSpeedTicks() set: monsterType:yellSpeedTicks(rate) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.yellSpeedTicks); } else { - monsterType->info.yellSpeedTicks = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.yellSpeedTicks = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1381,13 +1516,13 @@ int MonsterTypeFunctions::luaMonsterTypeYellSpeedTicks(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeChangeTargetChance(lua_State* L) { // monsterType:getChangeTargetChance() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.changeTargetChance); } else { - monsterType->info.changeTargetChance = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.changeTargetChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1397,13 +1532,13 @@ int MonsterTypeFunctions::luaMonsterTypeChangeTargetChance(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeChangeTargetSpeed(lua_State* L) { // get: monsterType:changeTargetSpeed() set: monsterType:changeTargetSpeed(speed) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.changeTargetSpeed); } else { - monsterType->info.changeTargetSpeed = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.changeTargetSpeed = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1413,13 +1548,13 @@ int MonsterTypeFunctions::luaMonsterTypeChangeTargetSpeed(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeCanWalkOnEnergy(lua_State* L) { // get: monsterType:canWalkOnEnergy() set: monsterType:canWalkOnEnergy(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.canWalkOnEnergy); + Lua::pushBoolean(L, monsterType->info.canWalkOnEnergy); } else { - monsterType->info.canWalkOnEnergy = getBoolean(L, 2, true); - pushBoolean(L, true); + monsterType->info.canWalkOnEnergy = Lua::getBoolean(L, 2, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1429,13 +1564,13 @@ int MonsterTypeFunctions::luaMonsterTypeCanWalkOnEnergy(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeCanWalkOnFire(lua_State* L) { // get: monsterType:canWalkOnFire() set: monsterType:canWalkOnFire(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.canWalkOnFire); + Lua::pushBoolean(L, monsterType->info.canWalkOnFire); } else { - monsterType->info.canWalkOnFire = getBoolean(L, 2, true); - pushBoolean(L, true); + monsterType->info.canWalkOnFire = Lua::getBoolean(L, 2, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1445,13 +1580,13 @@ int MonsterTypeFunctions::luaMonsterTypeCanWalkOnFire(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeCanWalkOnPoison(lua_State* L) { // get: monsterType:canWalkOnPoison() set: monsterType:canWalkOnPoison(bool) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { - pushBoolean(L, monsterType->info.canWalkOnPoison); + Lua::pushBoolean(L, monsterType->info.canWalkOnPoison); } else { - monsterType->info.canWalkOnPoison = getBoolean(L, 2, true); - pushBoolean(L, true); + monsterType->info.canWalkOnPoison = Lua::getBoolean(L, 2, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1461,13 +1596,13 @@ int MonsterTypeFunctions::luaMonsterTypeCanWalkOnPoison(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeStrategiesTargetNearest(lua_State* L) { // monsterType:strategiesTargetNearest() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.strategiesTargetNearest); } else { - monsterType->info.strategiesTargetNearest = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.strategiesTargetNearest = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1477,13 +1612,13 @@ int MonsterTypeFunctions::luaMonsterTypeStrategiesTargetNearest(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeStrategiesTargetHealth(lua_State* L) { // monsterType:strategiesTargetHealth() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.strategiesTargetHealth); } else { - monsterType->info.strategiesTargetHealth = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.strategiesTargetHealth = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1493,13 +1628,13 @@ int MonsterTypeFunctions::luaMonsterTypeStrategiesTargetHealth(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeStrategiesTargetDamage(lua_State* L) { // monsterType:strategiesTargetDamage() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.strategiesTargetDamage); } else { - monsterType->info.strategiesTargetDamage = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.strategiesTargetDamage = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1509,13 +1644,13 @@ int MonsterTypeFunctions::luaMonsterTypeStrategiesTargetDamage(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeStrategiesTargetRandom(lua_State* L) { // monsterType:strategiesTargetRandom() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.strategiesTargetRandom); } else { - monsterType->info.strategiesTargetRandom = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.strategiesTargetRandom = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1529,13 +1664,13 @@ int MonsterTypeFunctions::luaMonsterTypeStrategiesTargetRandom(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeRespawnTypePeriod(lua_State* L) { // monsterType:respawnTypePeriod() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.respawnType.period); } else { - monsterType->info.respawnType.period = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.respawnType.period = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1545,13 +1680,13 @@ int MonsterTypeFunctions::luaMonsterTypeRespawnTypePeriod(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeRespawnTypeIsUnderground(lua_State* L) { // monsterType:respawnTypeIsUnderground() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (monsterType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.respawnType.underground); } else { - monsterType->info.respawnType.underground = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.respawnType.underground = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -1562,10 +1697,10 @@ int MonsterTypeFunctions::luaMonsterTypeRespawnTypeIsUnderground(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBossRace(lua_State* L) { // set: monsterType:bosstiaryRace(raceId, class) // get: monsterType:bosstiaryRace() = this return only class name - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - pushBoolean(L, false); - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); return 0; } @@ -1574,13 +1709,13 @@ int MonsterTypeFunctions::luaMonsterTypeBossRace(lua_State* L) { lua_pushnil(L); return 1; } - pushString(L, monsterType->info.bosstiaryClass); + Lua::pushString(L, monsterType->info.bosstiaryClass); } else { - const auto bossRace = getNumber(L, 2, 0); - const auto bossClass = getString(L, 3); + const auto bossRace = Lua::getNumber(L, 2, 0); + const auto bossClass = Lua::getString(L, 3); monsterType->info.bosstiaryRace = magic_enum::enum_value(bossRace); monsterType->info.bosstiaryClass = bossClass; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } return 1; @@ -1589,10 +1724,10 @@ int MonsterTypeFunctions::luaMonsterTypeBossRace(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeBossRaceId(lua_State* L) { // set: monsterType:bossRaceId(raceId) // get: monsterType:bossRaceId() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - pushBoolean(L, false); - reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_MONSTER_TYPE_NOT_FOUND)); return 0; } @@ -1603,10 +1738,10 @@ int MonsterTypeFunctions::luaMonsterTypeBossRaceId(lua_State* L) { lua_pushnumber(L, static_cast(monsterType->info.bosstiaryRace)); } } else { - const auto raceId = getNumber(L, 2, 0); + const auto raceId = Lua::getNumber(L, 2, 0); monsterType->info.raceid = raceId; g_ioBosstiary().addBosstiaryMonster(raceId, monsterType->typeName); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } return 1; @@ -1614,57 +1749,57 @@ int MonsterTypeFunctions::luaMonsterTypeBossRaceId(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeSoundChance(lua_State* L) { // get: monsterType:soundChance() set: monsterType:soundChance(chance) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.soundChance); } else { - monsterType->info.soundChance = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.soundChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int MonsterTypeFunctions::luaMonsterTypeSoundSpeedTicks(lua_State* L) { // get: monsterType:soundSpeedTicks() set: monsterType:soundSpeedTicks(ticks) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } if (lua_gettop(L) == 1) { lua_pushnumber(L, monsterType->info.soundSpeedTicks); } else { - monsterType->info.soundSpeedTicks = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.soundSpeedTicks = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int MonsterTypeFunctions::luaMonsterTypeAddSound(lua_State* L) { // monsterType:addSound(soundId) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - monsterType->info.soundVector.push_back(getNumber(L, 2)); - pushBoolean(L, true); + monsterType->info.soundVector.push_back(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); return 1; } int MonsterTypeFunctions::luaMonsterTypeGetSounds(lua_State* L) { // monsterType:getSounds() - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { lua_pushnil(L); return 1; @@ -1683,18 +1818,18 @@ int MonsterTypeFunctions::luaMonsterTypeGetSounds(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypedeathSound(lua_State* L) { // get: monsterType:deathSound() set: monsterType:deathSound(sound) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } if (lua_gettop(L) == 1) { lua_pushnumber(L, static_cast(monsterType->info.deathSound)); } else { - monsterType->info.deathSound = getNumber(L, 2); - pushBoolean(L, true); + monsterType->info.deathSound = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; @@ -1702,18 +1837,18 @@ int MonsterTypeFunctions::luaMonsterTypedeathSound(lua_State* L) { int MonsterTypeFunctions::luaMonsterTypeVariant(lua_State* L) { // get: monsterType:variant() set: monsterType:variant(variantName) - const auto &monsterType = getUserdataShared(L, 1); + const auto &monsterType = Lua::getUserdataShared(L, 1); if (!monsterType) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } if (lua_gettop(L) == 1) { - pushString(L, monsterType->variantName); + Lua::pushString(L, monsterType->variantName); } else { - monsterType->variantName = getString(L, 2); - pushBoolean(L, true); + monsterType->variantName = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } return 1; diff --git a/src/lua/functions/creatures/monster/monster_type_functions.hpp b/src/lua/functions/creatures/monster/monster_type_functions.hpp index a10f1619d61..e272ea45cd7 100644 --- a/src/lua/functions/creatures/monster/monster_type_functions.hpp +++ b/src/lua/functions/creatures/monster/monster_type_functions.hpp @@ -9,149 +9,11 @@ #pragma once -#include "lua/scripts/luascript.hpp" +struct LootBlock; -class MonsterTypeFunctions final : LuaScriptInterface { +class MonsterTypeFunctions { public: - explicit MonsterTypeFunctions(lua_State* L) : - LuaScriptInterface("MonsterTypeFunctions") { - init(L); - } - ~MonsterTypeFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "MonsterType", "", MonsterTypeFunctions::luaMonsterTypeCreate); - registerMetaMethod(L, "MonsterType", "__eq", MonsterTypeFunctions::luaUserdataCompare); - - registerMethod(L, "MonsterType", "isAttackable", MonsterTypeFunctions::luaMonsterTypeIsAttackable); - registerMethod(L, "MonsterType", "isConvinceable", MonsterTypeFunctions::luaMonsterTypeIsConvinceable); - registerMethod(L, "MonsterType", "isSummonable", MonsterTypeFunctions::luaMonsterTypeIsSummonable); - registerMethod(L, "MonsterType", "isPreyable", MonsterTypeFunctions::luaMonsterTypeIsPreyable); - registerMethod(L, "MonsterType", "isPreyExclusive", MonsterTypeFunctions::luaMonsterTypeIsPreyExclusive); - registerMethod(L, "MonsterType", "isIllusionable", MonsterTypeFunctions::luaMonsterTypeIsIllusionable); - registerMethod(L, "MonsterType", "isHostile", MonsterTypeFunctions::luaMonsterTypeIsHostile); - registerMethod(L, "MonsterType", "isPushable", MonsterTypeFunctions::luaMonsterTypeIsPushable); - registerMethod(L, "MonsterType", "isHealthHidden", MonsterTypeFunctions::luaMonsterTypeIsHealthHidden); - registerMethod(L, "MonsterType", "isBlockable", MonsterTypeFunctions::luaMonsterTypeIsBlockable); - registerMethod(L, "MonsterType", "isForgeCreature", MonsterTypeFunctions::luaMonsterTypeIsForgeCreature); - - registerMethod(L, "MonsterType", "familiar", MonsterTypeFunctions::luaMonsterTypeFamiliar); - registerMethod(L, "MonsterType", "isRewardBoss", MonsterTypeFunctions::luaMonsterTypeIsRewardBoss); - - registerMethod(L, "MonsterType", "canSpawn", MonsterTypeFunctions::luaMonsterTypeCanSpawn); - - registerMethod(L, "MonsterType", "canPushItems", MonsterTypeFunctions::luaMonsterTypeCanPushItems); - registerMethod(L, "MonsterType", "canPushCreatures", MonsterTypeFunctions::luaMonsterTypeCanPushCreatures); - - registerMethod(L, "MonsterType", "critChance", MonsterTypeFunctions::luaMonsterTypeCritChance); - - registerMethod(L, "MonsterType", "name", MonsterTypeFunctions::luaMonsterTypeName); - - registerMethod(L, "MonsterType", "nameDescription", MonsterTypeFunctions::luaMonsterTypeNameDescription); - - registerMethod(L, "MonsterType", "getCorpseId", MonsterTypeFunctions::luaMonsterTypegetCorpseId); - - registerMethod(L, "MonsterType", "health", MonsterTypeFunctions::luaMonsterTypeHealth); - registerMethod(L, "MonsterType", "maxHealth", MonsterTypeFunctions::luaMonsterTypeMaxHealth); - registerMethod(L, "MonsterType", "runHealth", MonsterTypeFunctions::luaMonsterTypeRunHealth); - registerMethod(L, "MonsterType", "experience", MonsterTypeFunctions::luaMonsterTypeExperience); - - registerMethod(L, "MonsterType", "faction", MonsterTypeFunctions::luaMonsterTypeFaction); - registerMethod(L, "MonsterType", "enemyFactions", MonsterTypeFunctions::luaMonsterTypeEnemyFactions); - registerMethod(L, "MonsterType", "targetPreferPlayer", MonsterTypeFunctions::luaMonsterTypeTargetPreferPlayer); - registerMethod(L, "MonsterType", "targetPreferMaster", MonsterTypeFunctions::luaMonsterTypeTargetPreferMaster); - - registerMethod(L, "MonsterType", "raceId", MonsterTypeFunctions::luaMonsterTypeRaceid); - registerMethod(L, "MonsterType", "Bestiaryclass", MonsterTypeFunctions::luaMonsterTypeBestiaryclass); - registerMethod(L, "MonsterType", "BestiaryOccurrence", MonsterTypeFunctions::luaMonsterTypeBestiaryOccurrence); - registerMethod(L, "MonsterType", "BestiaryLocations", MonsterTypeFunctions::luaMonsterTypeBestiaryLocations); - registerMethod(L, "MonsterType", "BestiaryStars", MonsterTypeFunctions::luaMonsterTypeBestiaryStars); - registerMethod(L, "MonsterType", "BestiaryCharmsPoints", MonsterTypeFunctions::luaMonsterTypeBestiaryCharmsPoints); - registerMethod(L, "MonsterType", "BestiarySecondUnlock", MonsterTypeFunctions::luaMonsterTypeBestiarySecondUnlock); - registerMethod(L, "MonsterType", "BestiaryFirstUnlock", MonsterTypeFunctions::luaMonsterTypeBestiaryFirstUnlock); - registerMethod(L, "MonsterType", "BestiarytoKill", MonsterTypeFunctions::luaMonsterTypeBestiarytoKill); - registerMethod(L, "MonsterType", "Bestiaryrace", MonsterTypeFunctions::luaMonsterTypeBestiaryrace); - - registerMethod(L, "MonsterType", "combatImmunities", MonsterTypeFunctions::luaMonsterTypeCombatImmunities); - registerMethod(L, "MonsterType", "conditionImmunities", MonsterTypeFunctions::luaMonsterTypeConditionImmunities); - - registerMethod(L, "MonsterType", "getAttackList", MonsterTypeFunctions::luaMonsterTypeGetAttackList); - registerMethod(L, "MonsterType", "addAttack", MonsterTypeFunctions::luaMonsterTypeAddAttack); - - registerMethod(L, "MonsterType", "getDefenseList", MonsterTypeFunctions::luaMonsterTypeGetDefenseList); - registerMethod(L, "MonsterType", "addDefense", MonsterTypeFunctions::luaMonsterTypeAddDefense); - - registerMethod(L, "MonsterType", "getTypeName", MonsterTypeFunctions::luaMonsterTypeGetTypeName); - - registerMethod(L, "MonsterType", "getElementList", MonsterTypeFunctions::luaMonsterTypeGetElementList); - registerMethod(L, "MonsterType", "addElement", MonsterTypeFunctions::luaMonsterTypeAddElement); - - registerMethod(L, "MonsterType", "addReflect", MonsterTypeFunctions::luaMonsterTypeAddReflect); - registerMethod(L, "MonsterType", "addHealing", MonsterTypeFunctions::luaMonsterTypeAddHealing); - - registerMethod(L, "MonsterType", "getVoices", MonsterTypeFunctions::luaMonsterTypeGetVoices); - registerMethod(L, "MonsterType", "addVoice", MonsterTypeFunctions::luaMonsterTypeAddVoice); - - registerMethod(L, "MonsterType", "getLoot", MonsterTypeFunctions::luaMonsterTypeGetLoot); - registerMethod(L, "MonsterType", "addLoot", MonsterTypeFunctions::luaMonsterTypeAddLoot); - - registerMethod(L, "MonsterType", "getCreatureEvents", MonsterTypeFunctions::luaMonsterTypeGetCreatureEvents); - registerMethod(L, "MonsterType", "registerEvent", MonsterTypeFunctions::luaMonsterTypeRegisterEvent); - - registerMethod(L, "MonsterType", "eventType", MonsterTypeFunctions::luaMonsterTypeEventType); - registerMethod(L, "MonsterType", "onThink", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); - registerMethod(L, "MonsterType", "onAppear", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); - registerMethod(L, "MonsterType", "onDisappear", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); - registerMethod(L, "MonsterType", "onMove", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); - registerMethod(L, "MonsterType", "onSay", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); - registerMethod(L, "MonsterType", "onPlayerAttack", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); - registerMethod(L, "MonsterType", "onSpawn", MonsterTypeFunctions::luaMonsterTypeEventOnCallback); - - registerMethod(L, "MonsterType", "getSummonList", MonsterTypeFunctions::luaMonsterTypeGetSummonList); - registerMethod(L, "MonsterType", "addSummon", MonsterTypeFunctions::luaMonsterTypeAddSummon); - - registerMethod(L, "MonsterType", "maxSummons", MonsterTypeFunctions::luaMonsterTypeMaxSummons); - - registerMethod(L, "MonsterType", "armor", MonsterTypeFunctions::luaMonsterTypeArmor); - registerMethod(L, "MonsterType", "mitigation", MonsterTypeFunctions::luaMonsterTypeMitigation); - registerMethod(L, "MonsterType", "defense", MonsterTypeFunctions::luaMonsterTypeDefense); - registerMethod(L, "MonsterType", "outfit", MonsterTypeFunctions::luaMonsterTypeOutfit); - registerMethod(L, "MonsterType", "race", MonsterTypeFunctions::luaMonsterTypeRace); - registerMethod(L, "MonsterType", "corpseId", MonsterTypeFunctions::luaMonsterTypeCorpseId); - registerMethod(L, "MonsterType", "manaCost", MonsterTypeFunctions::luaMonsterTypeManaCost); - registerMethod(L, "MonsterType", "baseSpeed", MonsterTypeFunctions::luaMonsterTypeBaseSpeed); - registerMethod(L, "MonsterType", "light", MonsterTypeFunctions::luaMonsterTypeLight); - - registerMethod(L, "MonsterType", "staticAttackChance", MonsterTypeFunctions::luaMonsterTypeStaticAttackChance); - registerMethod(L, "MonsterType", "targetDistance", MonsterTypeFunctions::luaMonsterTypeTargetDistance); - registerMethod(L, "MonsterType", "yellChance", MonsterTypeFunctions::luaMonsterTypeYellChance); - registerMethod(L, "MonsterType", "yellSpeedTicks", MonsterTypeFunctions::luaMonsterTypeYellSpeedTicks); - registerMethod(L, "MonsterType", "changeTargetChance", MonsterTypeFunctions::luaMonsterTypeChangeTargetChance); - registerMethod(L, "MonsterType", "changeTargetSpeed", MonsterTypeFunctions::luaMonsterTypeChangeTargetSpeed); - - registerMethod(L, "MonsterType", "canWalkOnEnergy", MonsterTypeFunctions::luaMonsterTypeCanWalkOnEnergy); - registerMethod(L, "MonsterType", "canWalkOnFire", MonsterTypeFunctions::luaMonsterTypeCanWalkOnFire); - registerMethod(L, "MonsterType", "canWalkOnPoison", MonsterTypeFunctions::luaMonsterTypeCanWalkOnPoison); - - registerMethod(L, "MonsterType", "strategiesTargetNearest", MonsterTypeFunctions::luaMonsterTypeStrategiesTargetNearest); - registerMethod(L, "MonsterType", "strategiesTargetHealth", MonsterTypeFunctions::luaMonsterTypeStrategiesTargetHealth); - registerMethod(L, "MonsterType", "strategiesTargetDamage", MonsterTypeFunctions::luaMonsterTypeStrategiesTargetDamage); - registerMethod(L, "MonsterType", "strategiesTargetRandom", MonsterTypeFunctions::luaMonsterTypeStrategiesTargetRandom); - - registerMethod(L, "MonsterType", "respawnTypePeriod", MonsterTypeFunctions::luaMonsterTypeRespawnTypePeriod); - registerMethod(L, "MonsterType", "respawnTypeIsUnderground", MonsterTypeFunctions::luaMonsterTypeRespawnTypeIsUnderground); - - registerMethod(L, "MonsterType", "bossRace", MonsterTypeFunctions::luaMonsterTypeBossRace); - registerMethod(L, "MonsterType", "bossRaceId", MonsterTypeFunctions::luaMonsterTypeBossRaceId); - - registerMethod(L, "MonsterType", "soundChance", MonsterTypeFunctions::luaMonsterTypeSoundChance); - registerMethod(L, "MonsterType", "soundSpeedTicks", MonsterTypeFunctions::luaMonsterTypeSoundSpeedTicks); - registerMethod(L, "MonsterType", "addSound", MonsterTypeFunctions::luaMonsterTypeAddSound); - registerMethod(L, "MonsterType", "getSounds", MonsterTypeFunctions::luaMonsterTypeGetSounds); - registerMethod(L, "MonsterType", "deathSound", MonsterTypeFunctions::luaMonsterTypedeathSound); - - registerMethod(L, "MonsterType", "variant", MonsterTypeFunctions::luaMonsterTypeVariant); - } + static void init(lua_State* L); private: static void createMonsterTypeLootLuaTable(lua_State* L, const std::vector &lootList); diff --git a/src/lua/functions/creatures/npc/npc_functions.cpp b/src/lua/functions/creatures/npc/npc_functions.cpp index f3f5a204ba5..533182ea128 100644 --- a/src/lua/functions/creatures/npc/npc_functions.cpp +++ b/src/lua/functions/creatures/npc/npc_functions.cpp @@ -14,31 +14,69 @@ #include "creatures/players/player.hpp" #include "game/game.hpp" #include "map/spectators.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void NpcFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Npc", "Creature", NpcFunctions::luaNpcCreate); + Lua::registerMetaMethod(L, "Npc", "__eq", Lua::luaUserdataCompare); + Lua::registerMethod(L, "Npc", "isNpc", NpcFunctions::luaNpcIsNpc); + Lua::registerMethod(L, "Npc", "setMasterPos", NpcFunctions::luaNpcSetMasterPos); + Lua::registerMethod(L, "Npc", "getCurrency", NpcFunctions::luaNpcGetCurrency); + Lua::registerMethod(L, "Npc", "setCurrency", NpcFunctions::luaNpcSetCurrency); + Lua::registerMethod(L, "Npc", "getSpeechBubble", NpcFunctions::luaNpcGetSpeechBubble); + Lua::registerMethod(L, "Npc", "setSpeechBubble", NpcFunctions::luaNpcSetSpeechBubble); + Lua::registerMethod(L, "Npc", "getId", NpcFunctions::luaNpcGetId); + Lua::registerMethod(L, "Npc", "getName", NpcFunctions::luaNpcGetName); + Lua::registerMethod(L, "Npc", "setName", NpcFunctions::luaNpcSetName); + Lua::registerMethod(L, "Npc", "place", NpcFunctions::luaNpcPlace); + Lua::registerMethod(L, "Npc", "say", NpcFunctions::luaNpcSay); + Lua::registerMethod(L, "Npc", "turnToCreature", NpcFunctions::luaNpcTurnToCreature); + Lua::registerMethod(L, "Npc", "setPlayerInteraction", NpcFunctions::luaNpcSetPlayerInteraction); + Lua::registerMethod(L, "Npc", "removePlayerInteraction", NpcFunctions::luaNpcRemovePlayerInteraction); + Lua::registerMethod(L, "Npc", "isInteractingWithPlayer", NpcFunctions::luaNpcIsInteractingWithPlayer); + Lua::registerMethod(L, "Npc", "isInTalkRange", NpcFunctions::luaNpcIsInTalkRange); + Lua::registerMethod(L, "Npc", "isPlayerInteractingOnTopic", NpcFunctions::luaNpcIsPlayerInteractingOnTopic); + Lua::registerMethod(L, "Npc", "openShopWindow", NpcFunctions::luaNpcOpenShopWindow); + Lua::registerMethod(L, "Npc", "openShopWindowTable", NpcFunctions::luaNpcOpenShopWindowTable); + Lua::registerMethod(L, "Npc", "closeShopWindow", NpcFunctions::luaNpcCloseShopWindow); + Lua::registerMethod(L, "Npc", "getShopItem", NpcFunctions::luaNpcGetShopItem); + Lua::registerMethod(L, "Npc", "isMerchant", NpcFunctions::luaNpcIsMerchant); + + Lua::registerMethod(L, "Npc", "move", NpcFunctions::luaNpcMove); + Lua::registerMethod(L, "Npc", "turn", NpcFunctions::luaNpcTurn); + Lua::registerMethod(L, "Npc", "follow", NpcFunctions::luaNpcFollow); + Lua::registerMethod(L, "Npc", "sellItem", NpcFunctions::luaNpcSellItem); + + Lua::registerMethod(L, "Npc", "getDistanceTo", NpcFunctions::luaNpcGetDistanceTo); + + ShopFunctions::init(L); + NpcTypeFunctions::init(L); +} int NpcFunctions::luaNpcCreate(lua_State* L) { // Npc([id or name or userdata]) std::shared_ptr npc; if (lua_gettop(L) >= 2) { - if (isNumber(L, 2)) { - npc = g_game().getNpcByID(getNumber(L, 2)); - } else if (isString(L, 2)) { - npc = g_game().getNpcByName(getString(L, 2)); - } else if (isUserdata(L, 2)) { - if (getUserdataType(L, 2) != LuaData_t::Npc) { + if (Lua::isNumber(L, 2)) { + npc = g_game().getNpcByID(Lua::getNumber(L, 2)); + } else if (Lua::isString(L, 2)) { + npc = g_game().getNpcByName(Lua::getString(L, 2)); + } else if (Lua::isUserdata(L, 2)) { + if (Lua::getUserdataType(L, 2) != LuaData_t::Npc) { lua_pushnil(L); return 1; } - npc = getUserdataShared(L, 2); + npc = Lua::getUserdataShared(L, 2); } else { npc = nullptr; } } else { - npc = getUserdataShared(L, 1); + npc = Lua::getUserdataShared(L, 1); } if (npc) { - pushUserdata(L, npc); - setMetatable(L, -1, "Npc"); + Lua::pushUserdata(L, npc); + Lua::setMetatable(L, -1, "Npc"); } else { lua_pushnil(L); } @@ -47,30 +85,30 @@ int NpcFunctions::luaNpcCreate(lua_State* L) { int NpcFunctions::luaNpcIsNpc(lua_State* L) { // npc:isNpc() - pushBoolean(L, getUserdataShared(L, 1) != nullptr); + Lua::pushBoolean(L, Lua::getUserdataShared(L, 1) != nullptr); return 1; } int NpcFunctions::luaNpcSetMasterPos(lua_State* L) { // npc:setMasterPos(pos) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const Position &pos = getPosition(L, 2); + const Position &pos = Lua::getPosition(L, 2); npc->setMasterPos(pos); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int NpcFunctions::luaNpcGetCurrency(lua_State* L) { // npc:getCurrency() - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); } @@ -80,22 +118,22 @@ int NpcFunctions::luaNpcGetCurrency(lua_State* L) { int NpcFunctions::luaNpcSetCurrency(lua_State* L) { // npc:getCurrency() - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - npc->setCurrency(getNumber(L, 2)); + npc->setCurrency(Lua::getNumber(L, 2)); return 1; } int NpcFunctions::luaNpcGetSpeechBubble(lua_State* L) { // npc:getSpeechBubble() - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); } @@ -105,35 +143,35 @@ int NpcFunctions::luaNpcGetSpeechBubble(lua_State* L) { int NpcFunctions::luaNpcSetSpeechBubble(lua_State* L) { // npc:setSpeechBubble(speechBubble) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); } - npc->setSpeechBubble(getNumber(L, 2)); + npc->setSpeechBubble(Lua::getNumber(L, 2)); return 1; } int NpcFunctions::luaNpcGetName(lua_State* L) { // npc:getName() - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); return 1; } - pushString(L, npc->getName()); + Lua::pushString(L, npc->getName()); return 1; } int NpcFunctions::luaNpcSetName(lua_State* L) { // npc:setName(name) - const auto &npc = getUserdataShared(L, 1); - const std::string &name = getString(L, 2); + const auto &npc = Lua::getUserdataShared(L, 1); + const std::string &name = Lua::getString(L, 2); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); } @@ -143,19 +181,19 @@ int NpcFunctions::luaNpcSetName(lua_State* L) { int NpcFunctions::luaNpcPlace(lua_State* L) { // npc:place(position[, extended = false[, force = true]]) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); return 1; } - const Position &position = getPosition(L, 2); - const bool extended = getBoolean(L, 3, false); - const bool force = getBoolean(L, 4, true); + const Position &position = Lua::getPosition(L, 2); + const bool extended = Lua::getBoolean(L, 3, false); + const bool force = Lua::getBoolean(L, 4, true); if (g_game().placeCreature(npc, position, extended, force)) { - pushUserdata(L, npc); - setMetatable(L, -1, "Npc"); + Lua::pushUserdata(L, npc); + Lua::setMetatable(L, -1, "Npc"); } else { lua_pushnil(L); } @@ -168,24 +206,24 @@ int NpcFunctions::luaNpcSay(lua_State* L) { Position position; if (parameters >= 6) { - position = getPosition(L, 6); + position = Lua::getPosition(L, 6); if (!position.x || !position.y) { - reportErrorFunc("Invalid position specified."); - pushBoolean(L, false); + Lua::reportErrorFunc("Invalid position specified."); + Lua::pushBoolean(L, false); return 1; } } std::shared_ptr target = nullptr; if (parameters >= 5) { - target = getCreature(L, 5); + target = Lua::getCreature(L, 5); } - const bool ghost = getBoolean(L, 4, false); + const bool ghost = Lua::getBoolean(L, 4, false); - const auto &type = getNumber(L, 3, TALKTYPE_PRIVATE_NP); - const std::string &text = getString(L, 2); - const auto &npc = getUserdataShared(L, 1); + const auto &type = Lua::getNumber(L, 3, TALKTYPE_PRIVATE_NP); + const std::string &text = Lua::getString(L, 2); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { lua_pushnil(L); return 1; @@ -197,9 +235,9 @@ int NpcFunctions::luaNpcSay(lua_State* L) { } if (position.x != 0) { - pushBoolean(L, g_game().internalCreatureSay(npc, type, text, ghost, &spectators, &position)); + Lua::pushBoolean(L, g_game().internalCreatureSay(npc, type, text, ghost, &spectators, &position)); } else { - pushBoolean(L, g_game().internalCreatureSay(npc, type, text, ghost, &spectators)); + Lua::pushBoolean(L, g_game().internalCreatureSay(npc, type, text, ghost, &spectators)); } return 1; } @@ -210,173 +248,173 @@ int NpcFunctions::luaNpcSay(lua_State* L) { */ int NpcFunctions::luaNpcTurnToCreature(lua_State* L) { // npc:turnToCreature(creature, true) - const auto &npc = getUserdataShared(L, 1); - const auto &creature = getCreature(L, 2); + const auto &npc = Lua::getUserdataShared(L, 1); + const auto &creature = Lua::getCreature(L, 2); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); return 1; } if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); lua_pushnil(L); return 1; } - const bool stopEventWalk = getBoolean(L, 3, true); + const bool stopEventWalk = Lua::getBoolean(L, 3, true); if (stopEventWalk) { npc->stopEventWalk(); } npc->turnToCreature(creature); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int NpcFunctions::luaNpcSetPlayerInteraction(lua_State* L) { // npc:setPlayerInteraction(creature, topic = 0) - const auto &npc = getUserdataShared(L, 1); - const auto &creature = getCreature(L, 2); - const auto topicId = getNumber(L, 3, 0); + const auto &npc = Lua::getUserdataShared(L, 1); + const auto &creature = Lua::getCreature(L, 2); + const auto topicId = Lua::getNumber(L, 3, 0); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); return 1; } if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); lua_pushnil(L); return 1; } npc->setPlayerInteraction(creature->getID(), topicId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int NpcFunctions::luaNpcRemovePlayerInteraction(lua_State* L) { // npc:removePlayerInteraction() - const auto &npc = getUserdataShared(L, 1); - const auto &creature = getCreature(L, 2); + const auto &npc = Lua::getUserdataShared(L, 1); + const auto &creature = Lua::getCreature(L, 2); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); return 1; } if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); lua_pushnil(L); return 1; } npc->removePlayerInteraction(creature->getPlayer()); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int NpcFunctions::luaNpcIsInteractingWithPlayer(lua_State* L) { // npc:isInteractingWithPlayer(creature) - const auto &npc = getUserdataShared(L, 1); - const auto &creature = getCreature(L, 2); + const auto &npc = Lua::getUserdataShared(L, 1); + const auto &creature = Lua::getCreature(L, 2); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); return 1; } if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); lua_pushnil(L); return 1; } - pushBoolean(L, npc->isInteractingWithPlayer(creature->getID())); + Lua::pushBoolean(L, npc->isInteractingWithPlayer(creature->getID())); return 1; } int NpcFunctions::luaNpcIsPlayerInteractingOnTopic(lua_State* L) { // npc:isPlayerInteractingOnTopic(creature, topicId = 0) - const auto &npc = getUserdataShared(L, 1); - const auto &creature = getCreature(L, 2); - const auto topicId = getNumber(L, 3, 0); + const auto &npc = Lua::getUserdataShared(L, 1); + const auto &creature = Lua::getCreature(L, 2); + const auto topicId = Lua::getNumber(L, 3, 0); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); return 1; } if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); lua_pushnil(L); return 1; } - pushBoolean(L, npc->isPlayerInteractingOnTopic(creature->getID(), topicId)); + Lua::pushBoolean(L, npc->isPlayerInteractingOnTopic(creature->getID(), topicId)); return 1; } int NpcFunctions::luaNpcIsInTalkRange(lua_State* L) { // npc:isInTalkRange(position[, range = 4]) - const auto &npc = getUserdataShared(L, 1); - const Position &position = getPosition(L, 2); - const auto range = getNumber(L, 3, 4); + const auto &npc = Lua::getUserdataShared(L, 1); + const Position &position = Lua::getPosition(L, 2); + const auto range = Lua::getNumber(L, 3, 4); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); return 1; } - pushBoolean(L, npc && npc->canInteract(position, range)); + Lua::pushBoolean(L, npc && npc->canInteract(position, range)); return 1; } int NpcFunctions::luaNpcOpenShopWindow(lua_State* L) { // npc:openShopWindow(player) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } player->closeShopWindow(); - pushBoolean(L, player->openShopWindow(npc)); + Lua::pushBoolean(L, player->openShopWindow(npc)); return 1; } int NpcFunctions::luaNpcOpenShopWindowTable(lua_State* L) { // npc:openShopWindowTable(player, items) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &player = getUserdataShared(L, 2); + const auto &player = Lua::getUserdataShared(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } if (lua_istable(L, 3) == 0) { - reportError(__FUNCTION__, "item list is not a table."); - pushBoolean(L, false); + Lua::reportError(__FUNCTION__, "item list is not a table."); + Lua::pushBoolean(L, false); return 1; } @@ -385,18 +423,18 @@ int NpcFunctions::luaNpcOpenShopWindowTable(lua_State* L) { while (lua_next(L, 3) != 0) { const auto tableIndex = lua_gettop(L); - auto itemId = getField(L, tableIndex, "clientId"); - auto subType = getField(L, tableIndex, "subType"); + auto itemId = Lua::getField(L, tableIndex, "clientId"); + auto subType = Lua::getField(L, tableIndex, "subType"); if (subType == 0) { - subType = getField(L, tableIndex, "subtype"); + subType = Lua::getField(L, tableIndex, "subtype"); lua_pop(L, 1); } - auto buyPrice = getField(L, tableIndex, "buy"); - auto sellPrice = getField(L, tableIndex, "sell"); - auto storageKey = getField(L, tableIndex, "storageKey"); - auto storageValue = getField(L, tableIndex, "storageValue"); - auto itemName = getFieldString(L, tableIndex, "itemName"); + auto buyPrice = Lua::getField(L, tableIndex, "buy"); + auto sellPrice = Lua::getField(L, tableIndex, "sell"); + auto storageKey = Lua::getField(L, tableIndex, "storageKey"); + auto storageValue = Lua::getField(L, tableIndex, "storageValue"); + auto itemName = Lua::getFieldString(L, tableIndex, "itemName"); if (itemName.empty()) { itemName = Item::items[itemId].name; } @@ -406,23 +444,23 @@ int NpcFunctions::luaNpcOpenShopWindowTable(lua_State* L) { lua_pop(L, 3); player->closeShopWindow(); - pushBoolean(L, player->openShopWindow(npc, items)); + Lua::pushBoolean(L, player->openShopWindow(npc, items)); return 1; } int NpcFunctions::luaNpcCloseShopWindow(lua_State* L) { // npc:closeShopWindow(player) - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -430,96 +468,96 @@ int NpcFunctions::luaNpcCloseShopWindow(lua_State* L) { player->closeShopWindow(); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int NpcFunctions::luaNpcIsMerchant(lua_State* L) { // npc:isMerchant() - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto playerGUID = getNumber(L, 2, 0); + const auto playerGUID = Lua::getNumber(L, 2, 0); const auto &shopItems = npc->getShopItemVector(playerGUID); if (shopItems.empty()) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int NpcFunctions::luaNpcGetShopItem(lua_State* L) { // npc:getShopItem(itemId) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto playerGUID = getNumber(L, 2, 0); + const auto playerGUID = Lua::getNumber(L, 2, 0); const auto &shopItems = npc->getShopItemVector(playerGUID); for (const ShopBlock &shopBlock : shopItems) { - setField(L, "id", shopBlock.itemId); - setField(L, "name", shopBlock.itemName); - setField(L, "subType", shopBlock.itemSubType); - setField(L, "buyPrice", shopBlock.itemBuyPrice); - setField(L, "sellPrice", shopBlock.itemSellPrice); - setField(L, "storageKey", shopBlock.itemStorageKey); - setField(L, "storageValue", shopBlock.itemStorageValue); + Lua::setField(L, "id", shopBlock.itemId); + Lua::setField(L, "name", shopBlock.itemName); + Lua::setField(L, "subType", shopBlock.itemSubType); + Lua::setField(L, "buyPrice", shopBlock.itemBuyPrice); + Lua::setField(L, "sellPrice", shopBlock.itemSellPrice); + Lua::setField(L, "storageKey", shopBlock.itemStorageKey); + Lua::setField(L, "storageValue", shopBlock.itemStorageValue); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int NpcFunctions::luaNpcMove(lua_State* L) { // npc:move(direction) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (npc) { - g_game().internalMoveCreature(npc, getNumber(L, 2)); + g_game().internalMoveCreature(npc, Lua::getNumber(L, 2)); } return 0; } int NpcFunctions::luaNpcTurn(lua_State* L) { // npc:turn(direction) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (npc) { - g_game().internalCreatureTurn(npc, getNumber(L, 2)); + g_game().internalCreatureTurn(npc, Lua::getNumber(L, 2)); } return 0; } int NpcFunctions::luaNpcFollow(lua_State* L) { // npc:follow(player) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - pushBoolean(L, npc->setFollowCreature(player)); + Lua::pushBoolean(L, npc->setFollowCreature(player)); return 1; } int NpcFunctions::luaNpcGetId(lua_State* L) { // npc:getId() - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); lua_pushnil(L); return 1; } @@ -530,30 +568,30 @@ int NpcFunctions::luaNpcGetId(lua_State* L) { int NpcFunctions::luaNpcSellItem(lua_State* L) { // npc:sellItem(player, itemid, amount, subtype, actionid, ignoreCap, inBackpacks) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - auto itemId = getNumber(L, 3); - const double amount = getNumber(L, 4); - const auto subType = getNumber(L, 5, 1); - const auto actionId = getNumber(L, 6, 0); - const bool ignoreCap = getBoolean(L, 7, false); - const bool inBackpacks = getBoolean(L, 8, false); + auto itemId = Lua::getNumber(L, 3); + const double amount = Lua::getNumber(L, 4); + const auto subType = Lua::getNumber(L, 5, 1); + const auto actionId = Lua::getNumber(L, 6, 0); + const bool ignoreCap = Lua::getBoolean(L, 7, false); + const bool inBackpacks = Lua::getBoolean(L, 8, false); const ItemType &it = Item::items[itemId]; if (it.id == 0) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } @@ -568,7 +606,7 @@ int NpcFunctions::luaNpcSellItem(lua_State* L) { } if ((static_cast(tile->getItemList()->size()) + (slotsNedeed - player->getFreeBackpackSlots())) > 30) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); player->sendCancelMessage(RETURNVALUE_NOTENOUGHROOM); return 1; } @@ -642,23 +680,23 @@ int NpcFunctions::luaNpcSellItem(lua_State* L) { } player->sendTextMessage(MESSAGE_TRADE, ss.str()); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int NpcFunctions::luaNpcGetDistanceTo(lua_State* L) { // npc:getDistanceTo(uid) - const auto &npc = getUserdataShared(L, 1); + const auto &npc = Lua::getUserdataShared(L, 1); if (!npc) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &thing = getScriptEnv()->getThingByUID(getNumber(L, -1)); - pushBoolean(L, thing && thing->isPushable()); + const auto &thing = Lua::getScriptEnv()->getThingByUID(Lua::getNumber(L, -1)); + Lua::pushBoolean(L, thing && thing->isPushable()); if (!thing) { - reportErrorFunc(getErrorDesc(LUA_ERROR_THING_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_THING_NOT_FOUND)); lua_pushnil(L); return 1; } diff --git a/src/lua/functions/creatures/npc/npc_functions.hpp b/src/lua/functions/creatures/npc/npc_functions.hpp index e03e3f72b25..4d0adc7335a 100644 --- a/src/lua/functions/creatures/npc/npc_functions.hpp +++ b/src/lua/functions/creatures/npc/npc_functions.hpp @@ -11,52 +11,9 @@ #include "lua/functions/creatures/npc/npc_type_functions.hpp" #include "lua/functions/creatures/npc/shop_functions.hpp" -#include "lua/scripts/luascript.hpp" - -class NpcFunctions final : LuaScriptInterface { +class NpcFunctions { private: - explicit NpcFunctions(lua_State* L) : - LuaScriptInterface("NpcFunctions") { - init(L); - } - ~NpcFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Npc", "Creature", NpcFunctions::luaNpcCreate); - registerMetaMethod(L, "Npc", "__eq", NpcFunctions::luaUserdataCompare); - registerMethod(L, "Npc", "isNpc", NpcFunctions::luaNpcIsNpc); - registerMethod(L, "Npc", "setMasterPos", NpcFunctions::luaNpcSetMasterPos); - registerMethod(L, "Npc", "getCurrency", NpcFunctions::luaNpcGetCurrency); - registerMethod(L, "Npc", "setCurrency", NpcFunctions::luaNpcSetCurrency); - registerMethod(L, "Npc", "getSpeechBubble", NpcFunctions::luaNpcGetSpeechBubble); - registerMethod(L, "Npc", "setSpeechBubble", NpcFunctions::luaNpcSetSpeechBubble); - registerMethod(L, "Npc", "getId", NpcFunctions::luaNpcGetId); - registerMethod(L, "Npc", "getName", NpcFunctions::luaNpcGetName); - registerMethod(L, "Npc", "setName", NpcFunctions::luaNpcSetName); - registerMethod(L, "Npc", "place", NpcFunctions::luaNpcPlace); - registerMethod(L, "Npc", "say", NpcFunctions::luaNpcSay); - registerMethod(L, "Npc", "turnToCreature", NpcFunctions::luaNpcTurnToCreature); - registerMethod(L, "Npc", "setPlayerInteraction", NpcFunctions::luaNpcSetPlayerInteraction); - registerMethod(L, "Npc", "removePlayerInteraction", NpcFunctions::luaNpcRemovePlayerInteraction); - registerMethod(L, "Npc", "isInteractingWithPlayer", NpcFunctions::luaNpcIsInteractingWithPlayer); - registerMethod(L, "Npc", "isInTalkRange", NpcFunctions::luaNpcIsInTalkRange); - registerMethod(L, "Npc", "isPlayerInteractingOnTopic", NpcFunctions::luaNpcIsPlayerInteractingOnTopic); - registerMethod(L, "Npc", "openShopWindow", NpcFunctions::luaNpcOpenShopWindow); - registerMethod(L, "Npc", "openShopWindowTable", NpcFunctions::luaNpcOpenShopWindowTable); - registerMethod(L, "Npc", "closeShopWindow", NpcFunctions::luaNpcCloseShopWindow); - registerMethod(L, "Npc", "getShopItem", NpcFunctions::luaNpcGetShopItem); - registerMethod(L, "Npc", "isMerchant", NpcFunctions::luaNpcIsMerchant); - - registerMethod(L, "Npc", "move", NpcFunctions::luaNpcMove); - registerMethod(L, "Npc", "turn", NpcFunctions::luaNpcTurn); - registerMethod(L, "Npc", "follow", NpcFunctions::luaNpcFollow); - registerMethod(L, "Npc", "sellItem", NpcFunctions::luaNpcSellItem); - - registerMethod(L, "Npc", "getDistanceTo", NpcFunctions::luaNpcGetDistanceTo); - - ShopFunctions::init(L); - NpcTypeFunctions::init(L); - } + static void init(lua_State* L); static int luaNpcCreate(lua_State* L); diff --git a/src/lua/functions/creatures/npc/npc_type_functions.cpp b/src/lua/functions/creatures/npc/npc_type_functions.cpp index 9360591318c..84609be95b1 100644 --- a/src/lua/functions/creatures/npc/npc_type_functions.cpp +++ b/src/lua/functions/creatures/npc/npc_type_functions.cpp @@ -13,6 +13,65 @@ #include "creatures/npcs/npcs.hpp" #include "game/game.hpp" #include "lua/scripts/scripts.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void NpcTypeFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "NpcType", "", NpcTypeFunctions::luaNpcTypeCreate); + Lua::registerMetaMethod(L, "NpcType", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "NpcType", "isPushable", NpcTypeFunctions::luaNpcTypeIsPushable); + Lua::registerMethod(L, "NpcType", "floorChange", NpcTypeFunctions::luaNpcTypeFloorChange); + + Lua::registerMethod(L, "NpcType", "canSpawn", NpcTypeFunctions::luaNpcTypeCanSpawn); + + Lua::registerMethod(L, "NpcType", "canPushItems", NpcTypeFunctions::luaNpcTypeCanPushItems); + Lua::registerMethod(L, "NpcType", "canPushCreatures", NpcTypeFunctions::luaNpcTypeCanPushCreatures); + + Lua::registerMethod(L, "NpcType", "name", NpcTypeFunctions::luaNpcTypeName); + + Lua::registerMethod(L, "NpcType", "nameDescription", NpcTypeFunctions::luaNpcTypeNameDescription); + + Lua::registerMethod(L, "NpcType", "health", NpcTypeFunctions::luaNpcTypeHealth); + Lua::registerMethod(L, "NpcType", "maxHealth", NpcTypeFunctions::luaNpcTypeMaxHealth); + + Lua::registerMethod(L, "NpcType", "getVoices", NpcTypeFunctions::luaNpcTypeGetVoices); + Lua::registerMethod(L, "NpcType", "addVoice", NpcTypeFunctions::luaNpcTypeAddVoice); + + Lua::registerMethod(L, "NpcType", "getCreatureEvents", NpcTypeFunctions::luaNpcTypeGetCreatureEvents); + Lua::registerMethod(L, "NpcType", "registerEvent", NpcTypeFunctions::luaNpcTypeRegisterEvent); + + Lua::registerMethod(L, "NpcType", "eventType", NpcTypeFunctions::luaNpcTypeEventType); + Lua::registerMethod(L, "NpcType", "onThink", NpcTypeFunctions::luaNpcTypeEventOnCallback); + Lua::registerMethod(L, "NpcType", "onAppear", NpcTypeFunctions::luaNpcTypeEventOnCallback); + Lua::registerMethod(L, "NpcType", "onDisappear", NpcTypeFunctions::luaNpcTypeEventOnCallback); + Lua::registerMethod(L, "NpcType", "onMove", NpcTypeFunctions::luaNpcTypeEventOnCallback); + Lua::registerMethod(L, "NpcType", "onSay", NpcTypeFunctions::luaNpcTypeEventOnCallback); + Lua::registerMethod(L, "NpcType", "onCloseChannel", NpcTypeFunctions::luaNpcTypeEventOnCallback); + Lua::registerMethod(L, "NpcType", "onBuyItem", NpcTypeFunctions::luaNpcTypeEventOnCallback); + Lua::registerMethod(L, "NpcType", "onSellItem", NpcTypeFunctions::luaNpcTypeEventOnCallback); + Lua::registerMethod(L, "NpcType", "onCheckItem", NpcTypeFunctions::luaNpcTypeEventOnCallback); + + Lua::registerMethod(L, "NpcType", "outfit", NpcTypeFunctions::luaNpcTypeOutfit); + Lua::registerMethod(L, "NpcType", "baseSpeed", NpcTypeFunctions::luaNpcTypeBaseSpeed); + Lua::registerMethod(L, "NpcType", "walkInterval", NpcTypeFunctions::luaNpcTypeWalkInterval); + Lua::registerMethod(L, "NpcType", "walkRadius", NpcTypeFunctions::luaNpcTypeWalkRadius); + Lua::registerMethod(L, "NpcType", "light", NpcTypeFunctions::luaNpcTypeLight); + + Lua::registerMethod(L, "NpcType", "yellChance", NpcTypeFunctions::luaNpcTypeYellChance); + Lua::registerMethod(L, "NpcType", "yellSpeedTicks", NpcTypeFunctions::luaNpcTypeYellSpeedTicks); + + Lua::registerMethod(L, "NpcType", "respawnTypePeriod", NpcTypeFunctions::luaNpcTypeRespawnTypePeriod); + Lua::registerMethod(L, "NpcType", "respawnTypeIsUnderground", NpcTypeFunctions::luaNpcTypeRespawnTypeIsUnderground); + Lua::registerMethod(L, "NpcType", "speechBubble", NpcTypeFunctions::luaNpcTypeSpeechBubble); + Lua::registerMethod(L, "NpcType", "currency", NpcTypeFunctions::luaNpcTypeCurrency); + + Lua::registerMethod(L, "NpcType", "addShopItem", NpcTypeFunctions::luaNpcTypeAddShopItem); + + Lua::registerMethod(L, "NpcType", "soundChance", NpcTypeFunctions::luaNpcTypeSoundChance); + Lua::registerMethod(L, "NpcType", "soundSpeedTicks", NpcTypeFunctions::luaNpcTypeSoundSpeedTicks); + Lua::registerMethod(L, "NpcType", "addSound", NpcTypeFunctions::luaNpcTypeAddSound); + Lua::registerMethod(L, "NpcType", "getSounds", NpcTypeFunctions::luaNpcTypeGetSounds); +} void NpcTypeFunctions::createNpcTypeShopLuaTable(lua_State* L, const std::vector &shopVector) { lua_createtable(L, shopVector.size(), 0); @@ -21,12 +80,12 @@ void NpcTypeFunctions::createNpcTypeShopLuaTable(lua_State* L, const std::vector for (const auto &shopBlock : shopVector) { lua_createtable(L, 0, 5); - setField(L, "itemId", shopBlock.itemId); - setField(L, "itemName", shopBlock.itemName); - setField(L, "itemBuyPrice", shopBlock.itemBuyPrice); - setField(L, "itemSellPrice", shopBlock.itemSellPrice); - setField(L, "itemStorageKey", shopBlock.itemStorageKey); - setField(L, "itemStorageValue", shopBlock.itemStorageValue); + Lua::setField(L, "itemId", shopBlock.itemId); + Lua::setField(L, "itemName", shopBlock.itemName); + Lua::setField(L, "itemBuyPrice", shopBlock.itemBuyPrice); + Lua::setField(L, "itemSellPrice", shopBlock.itemSellPrice); + Lua::setField(L, "itemStorageKey", shopBlock.itemStorageKey); + Lua::setField(L, "itemStorageValue", shopBlock.itemStorageValue); createNpcTypeShopLuaTable(L, shopBlock.childShop); lua_setfield(L, -2, "childShop"); @@ -37,21 +96,21 @@ void NpcTypeFunctions::createNpcTypeShopLuaTable(lua_State* L, const std::vector int NpcTypeFunctions::luaNpcTypeCreate(lua_State* L) { // NpcType(name) - const auto &npcType = g_npcs().getNpcType(getString(L, 1), true); - pushUserdata(L, npcType); - setMetatable(L, -1, "NpcType"); + const auto &npcType = g_npcs().getNpcType(Lua::getString(L, 1), true); + Lua::pushUserdata(L, npcType); + Lua::setMetatable(L, -1, "NpcType"); return 1; } int NpcTypeFunctions::luaNpcTypeIsPushable(lua_State* L) { // get: npcType:isPushable() set: npcType:isPushable(bool) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { - pushBoolean(L, npcType->info.pushable); + Lua::pushBoolean(L, npcType->info.pushable); } else { - npcType->info.pushable = getBoolean(L, 2); - pushBoolean(L, true); + npcType->info.pushable = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -61,13 +120,13 @@ int NpcTypeFunctions::luaNpcTypeIsPushable(lua_State* L) { int NpcTypeFunctions::luaNpcTypeFloorChange(lua_State* L) { // get: npcType:floorChange() set: npcType:floorChange(bool) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { - pushBoolean(L, npcType->info.floorChange); + Lua::pushBoolean(L, npcType->info.floorChange); } else { - npcType->info.floorChange = getBoolean(L, 2); - pushBoolean(L, true); + npcType->info.floorChange = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -77,10 +136,10 @@ int NpcTypeFunctions::luaNpcTypeFloorChange(lua_State* L) { int NpcTypeFunctions::luaNpcTypeCanSpawn(lua_State* L) { // monsterType:canSpawn(pos) - const auto &npcType = getUserdataShared(L, 1); - const Position &position = getPosition(L, 2); + const auto &npcType = Lua::getUserdataShared(L, 1); + const Position &position = Lua::getPosition(L, 2); if (npcType) { - pushBoolean(L, npcType->canSpawn(position)); + Lua::pushBoolean(L, npcType->canSpawn(position)); } else { lua_pushnil(L); } @@ -89,13 +148,13 @@ int NpcTypeFunctions::luaNpcTypeCanSpawn(lua_State* L) { int NpcTypeFunctions::luaNpcTypeCanPushItems(lua_State* L) { // get: npcType:canPushItems() set: npcType:canPushItems(bool) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { - pushBoolean(L, npcType->info.canPushItems); + Lua::pushBoolean(L, npcType->info.canPushItems); } else { - npcType->info.canPushItems = getBoolean(L, 2); - pushBoolean(L, true); + npcType->info.canPushItems = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -105,13 +164,13 @@ int NpcTypeFunctions::luaNpcTypeCanPushItems(lua_State* L) { int NpcTypeFunctions::luaNpcTypeCanPushCreatures(lua_State* L) { // get: npcType:canPushCreatures() set: npcType:canPushCreatures(bool) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { - pushBoolean(L, npcType->info.canPushCreatures); + Lua::pushBoolean(L, npcType->info.canPushCreatures); } else { - npcType->info.canPushCreatures = getBoolean(L, 2); - pushBoolean(L, true); + npcType->info.canPushCreatures = Lua::getBoolean(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -121,13 +180,13 @@ int NpcTypeFunctions::luaNpcTypeCanPushCreatures(lua_State* L) { int32_t NpcTypeFunctions::luaNpcTypeName(lua_State* L) { // get: npcType:name() set: npcType:name(name) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { - pushString(L, npcType->name); + Lua::pushString(L, npcType->name); } else { - npcType->name = getString(L, 2); - pushBoolean(L, true); + npcType->name = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -137,13 +196,13 @@ int32_t NpcTypeFunctions::luaNpcTypeName(lua_State* L) { int NpcTypeFunctions::luaNpcTypeNameDescription(lua_State* L) { // get: npcType:nameDescription() set: npcType:nameDescription(desc) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { - pushString(L, npcType->nameDescription); + Lua::pushString(L, npcType->nameDescription); } else { - npcType->nameDescription = getString(L, 2); - pushBoolean(L, true); + npcType->nameDescription = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -153,13 +212,13 @@ int NpcTypeFunctions::luaNpcTypeNameDescription(lua_State* L) { int NpcTypeFunctions::luaNpcTypeHealth(lua_State* L) { // get: npcType:health() set: npcType:health(health) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.health); } else { - npcType->info.health = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.health = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -169,13 +228,13 @@ int NpcTypeFunctions::luaNpcTypeHealth(lua_State* L) { int NpcTypeFunctions::luaNpcTypeMaxHealth(lua_State* L) { // get: npcType:maxHealth() set: npcType:maxHealth(health) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.healthMax); } else { - npcType->info.healthMax = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.healthMax = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -185,16 +244,16 @@ int NpcTypeFunctions::luaNpcTypeMaxHealth(lua_State* L) { int NpcTypeFunctions::luaNpcTypeAddShopItem(lua_State* L) { // npcType:addShopItem(shop) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (!npcType) { lua_pushnil(L); return 1; } - const auto &shop = getUserdataShared(L, 2); + const auto &shop = Lua::getUserdataShared(L, 2); if (shop) { npcType->loadShop(npcType, shop->shopBlock); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -203,15 +262,15 @@ int NpcTypeFunctions::luaNpcTypeAddShopItem(lua_State* L) { int NpcTypeFunctions::luaNpcTypeAddVoice(lua_State* L) { // npcType:addVoice(sentence, interval, chance, yell) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { voiceBlock_t voice; - voice.text = getString(L, 2); - npcType->info.yellSpeedTicks = getNumber(L, 3); - npcType->info.yellChance = getNumber(L, 4); - voice.yellText = getBoolean(L, 5); + voice.text = Lua::getString(L, 2); + npcType->info.yellSpeedTicks = Lua::getNumber(L, 3); + npcType->info.yellChance = Lua::getNumber(L, 4); + voice.yellText = Lua::getBoolean(L, 5); npcType->info.voiceVector.push_back(voice); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -220,7 +279,7 @@ int NpcTypeFunctions::luaNpcTypeAddVoice(lua_State* L) { int NpcTypeFunctions::luaNpcTypeGetVoices(lua_State* L) { // npcType:getVoices() - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (!npcType) { lua_pushnil(L); return 1; @@ -230,8 +289,8 @@ int NpcTypeFunctions::luaNpcTypeGetVoices(lua_State* L) { lua_createtable(L, npcType->info.voiceVector.size(), 0); for (const auto &voiceBlock : npcType->info.voiceVector) { lua_createtable(L, 0, 2); - setField(L, "text", voiceBlock.text); - setField(L, "yellText", voiceBlock.yellText); + Lua::setField(L, "text", voiceBlock.text); + Lua::setField(L, "yellText", voiceBlock.yellText); lua_rawseti(L, -2, ++index); } return 1; @@ -239,7 +298,7 @@ int NpcTypeFunctions::luaNpcTypeGetVoices(lua_State* L) { int NpcTypeFunctions::luaNpcTypeGetCreatureEvents(lua_State* L) { // npcType:getCreatureEvents() - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (!npcType) { lua_pushnil(L); return 1; @@ -248,7 +307,7 @@ int NpcTypeFunctions::luaNpcTypeGetCreatureEvents(lua_State* L) { int index = 0; lua_createtable(L, npcType->info.scripts.size(), 0); for (const std::string &creatureEvent : npcType->info.scripts) { - pushString(L, creatureEvent); + Lua::pushString(L, creatureEvent); lua_rawseti(L, -2, ++index); } return 1; @@ -256,10 +315,10 @@ int NpcTypeFunctions::luaNpcTypeGetCreatureEvents(lua_State* L) { int NpcTypeFunctions::luaNpcTypeRegisterEvent(lua_State* L) { // npcType:registerEvent(name) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { - npcType->info.scripts.insert(getString(L, 2)); - pushBoolean(L, true); + npcType->info.scripts.insert(Lua::getString(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -275,13 +334,13 @@ int NpcTypeFunctions::luaNpcTypeEventOnCallback(lua_State* L) { // npcType:onBuyItem(callback) // npcType:onSellItem(callback) // npcType:onCheckItem(callback) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (npcType->loadCallback(&g_scripts().getScriptInterface())) { - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } - pushBoolean(L, false); + Lua::pushBoolean(L, false); } else { lua_pushnil(L); } @@ -290,10 +349,10 @@ int NpcTypeFunctions::luaNpcTypeEventOnCallback(lua_State* L) { int NpcTypeFunctions::luaNpcTypeEventType(lua_State* L) { // npcType:eventType(event) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { - npcType->info.eventType = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.eventType = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -302,18 +361,18 @@ int NpcTypeFunctions::luaNpcTypeEventType(lua_State* L) { int NpcTypeFunctions::luaNpcTypeOutfit(lua_State* L) { // get: npcType:outfit() set: npcType:outfit(outfit) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { - pushOutfit(L, npcType->info.outfit); + Lua::pushOutfit(L, npcType->info.outfit); } else { - Outfit_t outfit = getOutfit(L, 2); + Outfit_t outfit = Lua::getOutfit(L, 2); if (g_configManager().getBoolean(WARN_UNSAFE_SCRIPTS) && outfit.lookType != 0 && !g_game().isLookTypeRegistered(outfit.lookType)) { g_logger().warn("[NpcTypeFunctions::luaNpcTypeOutfit] An unregistered creature looktype type with id '{}' was blocked to prevent client crash.", outfit.lookType); lua_pushnil(L); } else { - npcType->info.outfit = getOutfit(L, 2); - pushBoolean(L, true); + npcType->info.outfit = Lua::getOutfit(L, 2); + Lua::pushBoolean(L, true); } } } else { @@ -324,13 +383,13 @@ int NpcTypeFunctions::luaNpcTypeOutfit(lua_State* L) { int NpcTypeFunctions::luaNpcTypeBaseSpeed(lua_State* L) { // npcType:getBaseSpeed() - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.baseSpeed); } else { - npcType->info.baseSpeed = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.baseSpeed = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -340,12 +399,12 @@ int NpcTypeFunctions::luaNpcTypeBaseSpeed(lua_State* L) { int NpcTypeFunctions::luaNpcTypeWalkInterval(lua_State* L) { // get: npcType:walkInterval() set: npcType:walkInterval(interval) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.walkInterval); } else { - npcType->info.walkInterval = getNumber(L, 2); + npcType->info.walkInterval = Lua::getNumber(L, 2); lua_pushboolean(L, true); } } else { @@ -356,12 +415,12 @@ int NpcTypeFunctions::luaNpcTypeWalkInterval(lua_State* L) { int NpcTypeFunctions::luaNpcTypeWalkRadius(lua_State* L) { // get: npcType:walkRadius() set: npcType:walkRadius(id) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.walkRadius); } else { - npcType->info.walkRadius = getNumber(L, 2); + npcType->info.walkRadius = Lua::getNumber(L, 2); lua_pushboolean(L, true); } } else { @@ -372,7 +431,7 @@ int NpcTypeFunctions::luaNpcTypeWalkRadius(lua_State* L) { int NpcTypeFunctions::luaNpcTypeLight(lua_State* L) { // get: npcType:light() set: npcType:light(color, level) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (!npcType) { lua_pushnil(L); return 1; @@ -383,27 +442,27 @@ int NpcTypeFunctions::luaNpcTypeLight(lua_State* L) { lua_pushnumber(L, npcType->info.light.color); return 2; } else { - npcType->info.light.color = getNumber(L, 2); - npcType->info.light.level = getNumber(L, 3); - pushBoolean(L, true); + npcType->info.light.color = Lua::getNumber(L, 2); + npcType->info.light.level = Lua::getNumber(L, 3); + Lua::pushBoolean(L, true); } return 1; } int NpcTypeFunctions::luaNpcTypeYellChance(lua_State* L) { // get: npcType:yellChance() set: npcType:yellChance(chance) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.yellChance); } else { - npcType->info.yellChance = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.yellChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { - npcType->info.yellChance = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.yellChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -413,13 +472,13 @@ int NpcTypeFunctions::luaNpcTypeYellChance(lua_State* L) { int NpcTypeFunctions::luaNpcTypeYellSpeedTicks(lua_State* L) { // get: npcType:yellSpeedTicks() set: npcType:yellSpeedTicks(rate) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.yellSpeedTicks); } else { - npcType->info.yellSpeedTicks = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.yellSpeedTicks = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -433,13 +492,13 @@ int NpcTypeFunctions::luaNpcTypeYellSpeedTicks(lua_State* L) { int NpcTypeFunctions::luaNpcTypeRespawnTypePeriod(lua_State* L) { // npcType:respawnTypePeriod() - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.respawnType.period); } else { - npcType->info.respawnType.period = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.respawnType.period = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -449,13 +508,13 @@ int NpcTypeFunctions::luaNpcTypeRespawnTypePeriod(lua_State* L) { int NpcTypeFunctions::luaNpcTypeRespawnTypeIsUnderground(lua_State* L) { // npcType:respawnTypeIsUnderground() - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.respawnType.underground); } else { - npcType->info.respawnType.underground = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.respawnType.underground = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -466,18 +525,18 @@ int NpcTypeFunctions::luaNpcTypeRespawnTypeIsUnderground(lua_State* L) { int NpcTypeFunctions::luaNpcTypeSpeechBubble(lua_State* L) { // get = npcType:speechBubble() // set = npcType:speechBubble(newSpeechBubble) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (!npcType) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.speechBubble); } else { - npcType->info.speechBubble = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.speechBubble = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } @@ -485,75 +544,75 @@ int NpcTypeFunctions::luaNpcTypeSpeechBubble(lua_State* L) { int NpcTypeFunctions::luaNpcTypeCurrency(lua_State* L) { // get = npcType:currency() // set = npcType:currency(newCurrency) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (!npcType) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.currencyId); } else { - npcType->info.currencyId = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.currencyId = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int NpcTypeFunctions::luaNpcTypeSoundChance(lua_State* L) { // get: npcType:soundChance() set: npcType:soundChance(chance) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (!npcType) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.soundChance); } else { - npcType->info.soundChance = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.soundChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int NpcTypeFunctions::luaNpcTypeSoundSpeedTicks(lua_State* L) { // get: npcType:soundSpeedTicks() set: npcType:soundSpeedTicks(ticks) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (!npcType) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.soundSpeedTicks); } else { - npcType->info.soundSpeedTicks = getNumber(L, 2); - pushBoolean(L, true); + npcType->info.soundSpeedTicks = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } return 1; } int NpcTypeFunctions::luaNpcTypeAddSound(lua_State* L) { // npcType:addSound(soundId) - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (!npcType) { - reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - npcType->info.soundVector.push_back(getNumber(L, 2)); - pushBoolean(L, true); + npcType->info.soundVector.push_back(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); return 1; } int NpcTypeFunctions::luaNpcTypeGetSounds(lua_State* L) { // npcType:getSounds() - const auto &npcType = getUserdataShared(L, 1); + const auto &npcType = Lua::getUserdataShared(L, 1); if (!npcType) { lua_pushnil(L); return 1; diff --git a/src/lua/functions/creatures/npc/npc_type_functions.hpp b/src/lua/functions/creatures/npc/npc_type_functions.hpp index ee54f124992..1c40cb8ccfc 100644 --- a/src/lua/functions/creatures/npc/npc_type_functions.hpp +++ b/src/lua/functions/creatures/npc/npc_type_functions.hpp @@ -9,73 +9,11 @@ #pragma once -#include "lua/scripts/luascript.hpp" +struct ShopBlock; -class NpcTypeFunctions final : LuaScriptInterface { +class NpcTypeFunctions { public: - explicit NpcTypeFunctions(lua_State* L) : - LuaScriptInterface("NpcTypeFunctions") { - init(L); - } - ~NpcTypeFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "NpcType", "", NpcTypeFunctions::luaNpcTypeCreate); - registerMetaMethod(L, "NpcType", "__eq", NpcTypeFunctions::luaUserdataCompare); - - registerMethod(L, "NpcType", "isPushable", NpcTypeFunctions::luaNpcTypeIsPushable); - registerMethod(L, "NpcType", "floorChange", NpcTypeFunctions::luaNpcTypeFloorChange); - - registerMethod(L, "NpcType", "canSpawn", NpcTypeFunctions::luaNpcTypeCanSpawn); - - registerMethod(L, "NpcType", "canPushItems", NpcTypeFunctions::luaNpcTypeCanPushItems); - registerMethod(L, "NpcType", "canPushCreatures", NpcTypeFunctions::luaNpcTypeCanPushCreatures); - - registerMethod(L, "NpcType", "name", NpcTypeFunctions::luaNpcTypeName); - - registerMethod(L, "NpcType", "nameDescription", NpcTypeFunctions::luaNpcTypeNameDescription); - - registerMethod(L, "NpcType", "health", NpcTypeFunctions::luaNpcTypeHealth); - registerMethod(L, "NpcType", "maxHealth", NpcTypeFunctions::luaNpcTypeMaxHealth); - - registerMethod(L, "NpcType", "getVoices", NpcTypeFunctions::luaNpcTypeGetVoices); - registerMethod(L, "NpcType", "addVoice", NpcTypeFunctions::luaNpcTypeAddVoice); - - registerMethod(L, "NpcType", "getCreatureEvents", NpcTypeFunctions::luaNpcTypeGetCreatureEvents); - registerMethod(L, "NpcType", "registerEvent", NpcTypeFunctions::luaNpcTypeRegisterEvent); - - registerMethod(L, "NpcType", "eventType", NpcTypeFunctions::luaNpcTypeEventType); - registerMethod(L, "NpcType", "onThink", NpcTypeFunctions::luaNpcTypeEventOnCallback); - registerMethod(L, "NpcType", "onAppear", NpcTypeFunctions::luaNpcTypeEventOnCallback); - registerMethod(L, "NpcType", "onDisappear", NpcTypeFunctions::luaNpcTypeEventOnCallback); - registerMethod(L, "NpcType", "onMove", NpcTypeFunctions::luaNpcTypeEventOnCallback); - registerMethod(L, "NpcType", "onSay", NpcTypeFunctions::luaNpcTypeEventOnCallback); - registerMethod(L, "NpcType", "onCloseChannel", NpcTypeFunctions::luaNpcTypeEventOnCallback); - registerMethod(L, "NpcType", "onBuyItem", NpcTypeFunctions::luaNpcTypeEventOnCallback); - registerMethod(L, "NpcType", "onSellItem", NpcTypeFunctions::luaNpcTypeEventOnCallback); - registerMethod(L, "NpcType", "onCheckItem", NpcTypeFunctions::luaNpcTypeEventOnCallback); - - registerMethod(L, "NpcType", "outfit", NpcTypeFunctions::luaNpcTypeOutfit); - registerMethod(L, "NpcType", "baseSpeed", NpcTypeFunctions::luaNpcTypeBaseSpeed); - registerMethod(L, "NpcType", "walkInterval", NpcTypeFunctions::luaNpcTypeWalkInterval); - registerMethod(L, "NpcType", "walkRadius", NpcTypeFunctions::luaNpcTypeWalkRadius); - registerMethod(L, "NpcType", "light", NpcTypeFunctions::luaNpcTypeLight); - - registerMethod(L, "NpcType", "yellChance", NpcTypeFunctions::luaNpcTypeYellChance); - registerMethod(L, "NpcType", "yellSpeedTicks", NpcTypeFunctions::luaNpcTypeYellSpeedTicks); - - registerMethod(L, "NpcType", "respawnTypePeriod", NpcTypeFunctions::luaNpcTypeRespawnTypePeriod); - registerMethod(L, "NpcType", "respawnTypeIsUnderground", NpcTypeFunctions::luaNpcTypeRespawnTypeIsUnderground); - registerMethod(L, "NpcType", "speechBubble", NpcTypeFunctions::luaNpcTypeSpeechBubble); - registerMethod(L, "NpcType", "currency", NpcTypeFunctions::luaNpcTypeCurrency); - - registerMethod(L, "NpcType", "addShopItem", NpcTypeFunctions::luaNpcTypeAddShopItem); - - registerMethod(L, "NpcType", "soundChance", NpcTypeFunctions::luaNpcTypeSoundChance); - registerMethod(L, "NpcType", "soundSpeedTicks", NpcTypeFunctions::luaNpcTypeSoundSpeedTicks); - registerMethod(L, "NpcType", "addSound", NpcTypeFunctions::luaNpcTypeAddSound); - registerMethod(L, "NpcType", "getSounds", NpcTypeFunctions::luaNpcTypeGetSounds); - } + static void init(lua_State* L); private: static void createNpcTypeShopLuaTable(lua_State* L, const std::vector &shopVector); @@ -83,7 +21,6 @@ class NpcTypeFunctions final : LuaScriptInterface { static int luaNpcTypeIsPushable(lua_State* L); static int luaNpcTypeFloorChange(lua_State* L); - static int luaNpcTypeRespawnType(lua_State* L); static int luaNpcTypeCanSpawn(lua_State* L); static int luaNpcTypeCanPushItems(lua_State* L); diff --git a/src/lua/functions/creatures/npc/shop_functions.cpp b/src/lua/functions/creatures/npc/shop_functions.cpp index 7ba7f89d43f..04d62ae585b 100644 --- a/src/lua/functions/creatures/npc/shop_functions.cpp +++ b/src/lua/functions/creatures/npc/shop_functions.cpp @@ -13,21 +13,35 @@ #include "items/item.hpp" #include "utils/tools.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ShopFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Shop", "", ShopFunctions::luaCreateShop); + Lua::registerMethod(L, "Shop", "setId", ShopFunctions::luaShopSetId); + Lua::registerMethod(L, "Shop", "setIdFromName", ShopFunctions::luaShopSetIdFromName); + Lua::registerMethod(L, "Shop", "setNameItem", ShopFunctions::luaShopSetNameItem); + Lua::registerMethod(L, "Shop", "setCount", ShopFunctions::luaShopSetCount); + Lua::registerMethod(L, "Shop", "setBuyPrice", ShopFunctions::luaShopSetBuyPrice); + Lua::registerMethod(L, "Shop", "setSellPrice", ShopFunctions::luaShopSetSellPrice); + Lua::registerMethod(L, "Shop", "setStorageKey", ShopFunctions::luaShopSetStorageKey); + Lua::registerMethod(L, "Shop", "setStorageValue", ShopFunctions::luaShopSetStorageValue); + Lua::registerMethod(L, "Shop", "addChildShop", ShopFunctions::luaShopAddChildShop); +} int ShopFunctions::luaCreateShop(lua_State* L) { // Shop() will create a new shop item - pushUserdata(L, std::make_shared()); - setMetatable(L, -1, "Shop"); + Lua::pushUserdata(L, std::make_shared()); + Lua::setMetatable(L, -1, "Shop"); return 1; } int ShopFunctions::luaShopSetId(lua_State* L) { // shop:setId(id) - if (const auto &shop = getUserdataShared(L, 1)) { - if (isNumber(L, 2)) { - shop->shopBlock.itemId = getNumber(L, 2); - pushBoolean(L, true); + if (const auto &shop = Lua::getUserdataShared(L, 1)) { + if (Lua::isNumber(L, 2)) { + shop->shopBlock.itemId = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { g_logger().warn("[ShopFunctions::luaShopSetId] - " "Unknown shop item shop, int value expected"); @@ -41,9 +55,9 @@ int ShopFunctions::luaShopSetId(lua_State* L) { int ShopFunctions::luaShopSetIdFromName(lua_State* L) { // shop:setIdFromName(name) - const auto &shop = getUserdataShared(L, 1); - if (shop && isString(L, 2)) { - auto name = getString(L, 2); + const auto &shop = Lua::getUserdataShared(L, 1); + if (shop && Lua::isString(L, 2)) { + auto name = Lua::getString(L, 2); const auto ids = Item::items.nameToItems.equal_range(asLowerCaseString(name)); if (ids.first == Item::items.nameToItems.cend()) { @@ -63,7 +77,7 @@ int ShopFunctions::luaShopSetIdFromName(lua_State* L) { } shop->shopBlock.itemId = ids.first->second; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { g_logger().warn("[ShopFunctions::luaShopSetIdFromName] - " "Unknown shop item shop, string value expected"); @@ -74,9 +88,9 @@ int ShopFunctions::luaShopSetIdFromName(lua_State* L) { int ShopFunctions::luaShopSetNameItem(lua_State* L) { // shop:setNameItem(name) - if (const auto &shop = getUserdataShared(L, 1)) { - shop->shopBlock.itemName = getString(L, 2); - pushBoolean(L, true); + if (const auto &shop = Lua::getUserdataShared(L, 1)) { + shop->shopBlock.itemName = Lua::getString(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -85,9 +99,9 @@ int ShopFunctions::luaShopSetNameItem(lua_State* L) { int ShopFunctions::luaShopSetCount(lua_State* L) { // shop:setCount(count) - if (const auto &shop = getUserdataShared(L, 1)) { - shop->shopBlock.itemSubType = getNumber(L, 2); - pushBoolean(L, true); + if (const auto &shop = Lua::getUserdataShared(L, 1)) { + shop->shopBlock.itemSubType = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -96,9 +110,9 @@ int ShopFunctions::luaShopSetCount(lua_State* L) { int ShopFunctions::luaShopSetBuyPrice(lua_State* L) { // shop:setBuyPrice(price) - if (const auto &shop = getUserdataShared(L, 1)) { - shop->shopBlock.itemBuyPrice = getNumber(L, 2); - pushBoolean(L, true); + if (const auto &shop = Lua::getUserdataShared(L, 1)) { + shop->shopBlock.itemBuyPrice = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -107,9 +121,9 @@ int ShopFunctions::luaShopSetBuyPrice(lua_State* L) { int ShopFunctions::luaShopSetSellPrice(lua_State* L) { // shop:setSellPrice(chance) - if (const auto &shop = getUserdataShared(L, 1)) { - shop->shopBlock.itemSellPrice = getNumber(L, 2); - pushBoolean(L, true); + if (const auto &shop = Lua::getUserdataShared(L, 1)) { + shop->shopBlock.itemSellPrice = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -118,9 +132,9 @@ int ShopFunctions::luaShopSetSellPrice(lua_State* L) { int ShopFunctions::luaShopSetStorageKey(lua_State* L) { // shop:setStorageKey(storage) - if (const auto &shop = getUserdataShared(L, 1)) { - shop->shopBlock.itemStorageKey = getNumber(L, 2); - pushBoolean(L, true); + if (const auto &shop = Lua::getUserdataShared(L, 1)) { + shop->shopBlock.itemStorageKey = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -129,9 +143,9 @@ int ShopFunctions::luaShopSetStorageKey(lua_State* L) { int ShopFunctions::luaShopSetStorageValue(lua_State* L) { // shop:setStorageValue(value) - if (const auto &shop = getUserdataShared(L, 1)) { - shop->shopBlock.itemStorageValue = getNumber(L, 2); - pushBoolean(L, true); + if (const auto &shop = Lua::getUserdataShared(L, 1)) { + shop->shopBlock.itemStorageValue = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -140,8 +154,8 @@ int ShopFunctions::luaShopSetStorageValue(lua_State* L) { int ShopFunctions::luaShopAddChildShop(lua_State* L) { // shop:addChildShop(shop) - if (const auto &shop = getUserdataShared(L, 1)) { - shop->shopBlock.childShop.push_back(getUserdataShared(L, 2)->shopBlock); + if (const auto &shop = Lua::getUserdataShared(L, 1)) { + shop->shopBlock.childShop.push_back(Lua::getUserdataShared(L, 2)->shopBlock); } else { lua_pushnil(L); } diff --git a/src/lua/functions/creatures/npc/shop_functions.hpp b/src/lua/functions/creatures/npc/shop_functions.hpp index 2d326ff73e2..7bd91568601 100644 --- a/src/lua/functions/creatures/npc/shop_functions.hpp +++ b/src/lua/functions/creatures/npc/shop_functions.hpp @@ -9,28 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class ShopFunctions final : LuaScriptInterface { +class ShopFunctions { public: - explicit ShopFunctions(lua_State* L) : - LuaScriptInterface("ShopFunctions") { - init(L); - } - ~ShopFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Shop", "", ShopFunctions::luaCreateShop); - registerMethod(L, "Shop", "setId", ShopFunctions::luaShopSetId); - registerMethod(L, "Shop", "setIdFromName", ShopFunctions::luaShopSetIdFromName); - registerMethod(L, "Shop", "setNameItem", ShopFunctions::luaShopSetNameItem); - registerMethod(L, "Shop", "setCount", ShopFunctions::luaShopSetCount); - registerMethod(L, "Shop", "setBuyPrice", ShopFunctions::luaShopSetBuyPrice); - registerMethod(L, "Shop", "setSellPrice", ShopFunctions::luaShopSetSellPrice); - registerMethod(L, "Shop", "setStorageKey", ShopFunctions::luaShopSetStorageKey); - registerMethod(L, "Shop", "setStorageValue", ShopFunctions::luaShopSetStorageValue); - registerMethod(L, "Shop", "addChildShop", ShopFunctions::luaShopAddChildShop); - } + static void init(lua_State* L); private: static int luaCreateShop(lua_State* L); diff --git a/src/lua/functions/creatures/player/group_functions.cpp b/src/lua/functions/creatures/player/group_functions.cpp index 66003a64926..0c538a10eef 100644 --- a/src/lua/functions/creatures/player/group_functions.cpp +++ b/src/lua/functions/creatures/player/group_functions.cpp @@ -11,15 +11,29 @@ #include "creatures/players/grouping/groups.hpp" #include "game/game.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void GroupFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Group", "", GroupFunctions::luaGroupCreate); + Lua::registerMetaMethod(L, "Group", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Group", "getId", GroupFunctions::luaGroupGetId); + Lua::registerMethod(L, "Group", "getName", GroupFunctions::luaGroupGetName); + Lua::registerMethod(L, "Group", "getFlags", GroupFunctions::luaGroupGetFlags); + Lua::registerMethod(L, "Group", "getAccess", GroupFunctions::luaGroupGetAccess); + Lua::registerMethod(L, "Group", "getMaxDepotItems", GroupFunctions::luaGroupGetMaxDepotItems); + Lua::registerMethod(L, "Group", "getMaxVipEntries", GroupFunctions::luaGroupGetMaxVipEntries); + Lua::registerMethod(L, "Group", "hasFlag", GroupFunctions::luaGroupHasFlag); +} int GroupFunctions::luaGroupCreate(lua_State* L) { // Group(id) - const uint32_t id = getNumber(L, 2); + const uint32_t id = Lua::getNumber(L, 2); const auto &group = g_game().groups.getGroup(id); if (group) { - pushUserdata(L, group); - setMetatable(L, -1, "Group"); + Lua::pushUserdata(L, group); + Lua::setMetatable(L, -1, "Group"); } else { lua_pushnil(L); } @@ -28,7 +42,7 @@ int GroupFunctions::luaGroupCreate(lua_State* L) { int GroupFunctions::luaGroupGetId(lua_State* L) { // group:getId() - const auto &group = getUserdataShared(L, 1); + const auto &group = Lua::getUserdataShared(L, 1); if (group) { lua_pushnumber(L, group->id); } else { @@ -39,9 +53,9 @@ int GroupFunctions::luaGroupGetId(lua_State* L) { int GroupFunctions::luaGroupGetName(lua_State* L) { // group:getName() - const auto &group = getUserdataShared(L, 1); + const auto &group = Lua::getUserdataShared(L, 1); if (group) { - pushString(L, group->name); + Lua::pushString(L, group->name); } else { lua_pushnil(L); } @@ -50,7 +64,7 @@ int GroupFunctions::luaGroupGetName(lua_State* L) { int GroupFunctions::luaGroupGetFlags(lua_State* L) { // group:getFlags() - const auto &group = getUserdataShared(L, 1); + const auto &group = Lua::getUserdataShared(L, 1); if (group) { std::bitset flags; for (uint8_t i = 0; i < magic_enum::enum_integer(PlayerFlags_t::FlagLast); ++i) { @@ -67,9 +81,9 @@ int GroupFunctions::luaGroupGetFlags(lua_State* L) { int GroupFunctions::luaGroupGetAccess(lua_State* L) { // group:getAccess() - const auto &group = getUserdataShared(L, 1); + const auto &group = Lua::getUserdataShared(L, 1); if (group) { - pushBoolean(L, group->access); + Lua::pushBoolean(L, group->access); } else { lua_pushnil(L); } @@ -78,7 +92,7 @@ int GroupFunctions::luaGroupGetAccess(lua_State* L) { int GroupFunctions::luaGroupGetMaxDepotItems(lua_State* L) { // group:getMaxDepotItems() - const auto &group = getUserdataShared(L, 1); + const auto &group = Lua::getUserdataShared(L, 1); if (group) { lua_pushnumber(L, group->maxDepotItems); } else { @@ -89,7 +103,7 @@ int GroupFunctions::luaGroupGetMaxDepotItems(lua_State* L) { int GroupFunctions::luaGroupGetMaxVipEntries(lua_State* L) { // group:getMaxVipEntries() - const auto &group = getUserdataShared(L, 1); + const auto &group = Lua::getUserdataShared(L, 1); if (group) { lua_pushnumber(L, group->maxVipEntries); } else { @@ -100,10 +114,10 @@ int GroupFunctions::luaGroupGetMaxVipEntries(lua_State* L) { int GroupFunctions::luaGroupHasFlag(lua_State* L) { // group:hasFlag(flag) - const auto &group = getUserdataShared(L, 1); + const auto &group = Lua::getUserdataShared(L, 1); if (group) { - const auto flag = static_cast(getNumber(L, 2)); - pushBoolean(L, group->flags[Groups::getFlagNumber(flag)]); + const auto flag = static_cast(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, group->flags[Groups::getFlagNumber(flag)]); } else { lua_pushnil(L); } diff --git a/src/lua/functions/creatures/player/group_functions.hpp b/src/lua/functions/creatures/player/group_functions.hpp index 7729690b771..76774df5bb2 100644 --- a/src/lua/functions/creatures/player/group_functions.hpp +++ b/src/lua/functions/creatures/player/group_functions.hpp @@ -9,28 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class GroupFunctions final : LuaScriptInterface { +class GroupFunctions { public: - explicit GroupFunctions(lua_State* L) : - LuaScriptInterface("GroupFunctions") { - init(L); - } - ~GroupFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Group", "", GroupFunctions::luaGroupCreate); - registerMetaMethod(L, "Group", "__eq", GroupFunctions::luaUserdataCompare); - - registerMethod(L, "Group", "getId", GroupFunctions::luaGroupGetId); - registerMethod(L, "Group", "getName", GroupFunctions::luaGroupGetName); - registerMethod(L, "Group", "getFlags", GroupFunctions::luaGroupGetFlags); - registerMethod(L, "Group", "getAccess", GroupFunctions::luaGroupGetAccess); - registerMethod(L, "Group", "getMaxDepotItems", GroupFunctions::luaGroupGetMaxDepotItems); - registerMethod(L, "Group", "getMaxVipEntries", GroupFunctions::luaGroupGetMaxVipEntries); - registerMethod(L, "Group", "hasFlag", GroupFunctions::luaGroupHasFlag); - } + static void init(lua_State* L); private: static int luaGroupCreate(lua_State* L); diff --git a/src/lua/functions/creatures/player/guild_functions.cpp b/src/lua/functions/creatures/player/guild_functions.cpp index 808a5725ea5..62082d4f43a 100644 --- a/src/lua/functions/creatures/player/guild_functions.cpp +++ b/src/lua/functions/creatures/player/guild_functions.cpp @@ -11,13 +11,33 @@ #include "game/game.hpp" #include "creatures/players/grouping/guild.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void GuildFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Guild", "", GuildFunctions::luaGuildCreate); + Lua::registerMetaMethod(L, "Guild", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Guild", "getId", GuildFunctions::luaGuildGetId); + Lua::registerMethod(L, "Guild", "getName", GuildFunctions::luaGuildGetName); + Lua::registerMethod(L, "Guild", "getMembersOnline", GuildFunctions::luaGuildGetMembersOnline); + + Lua::registerMethod(L, "Guild", "getBankBalance", GuildFunctions::luaGuildGetBankBalance); + Lua::registerMethod(L, "Guild", "setBankBalance", GuildFunctions::luaGuildSetBankBalance); + + Lua::registerMethod(L, "Guild", "addRank", GuildFunctions::luaGuildAddRank); + Lua::registerMethod(L, "Guild", "getRankById", GuildFunctions::luaGuildGetRankById); + Lua::registerMethod(L, "Guild", "getRankByLevel", GuildFunctions::luaGuildGetRankByLevel); + + Lua::registerMethod(L, "Guild", "getMotd", GuildFunctions::luaGuildGetMotd); + Lua::registerMethod(L, "Guild", "setMotd", GuildFunctions::luaGuildSetMotd); +} int GuildFunctions::luaGuildCreate(lua_State* L) { - const uint32_t id = getNumber(L, 2); + const uint32_t id = Lua::getNumber(L, 2); const auto &guild = g_game().getGuild(id); if (guild) { - pushUserdata(L, guild); - setMetatable(L, -1, "Guild"); + Lua::pushUserdata(L, guild); + Lua::setMetatable(L, -1, "Guild"); } else { lua_pushnil(L); } @@ -25,7 +45,7 @@ int GuildFunctions::luaGuildCreate(lua_State* L) { } int GuildFunctions::luaGuildGetId(lua_State* L) { - const auto &guild = getUserdataShared(L, 1); + const auto &guild = Lua::getUserdataShared(L, 1); if (guild) { lua_pushnumber(L, guild->getId()); } else { @@ -36,18 +56,18 @@ int GuildFunctions::luaGuildGetId(lua_State* L) { int GuildFunctions::luaGuildGetName(lua_State* L) { // guild:getName() - const auto &guild = getUserdataShared(L, 1); + const auto &guild = Lua::getUserdataShared(L, 1); if (!guild) { lua_pushnil(L); return 1; } - pushString(L, guild->getName()); + Lua::pushString(L, guild->getName()); return 1; } int GuildFunctions::luaGuildGetMembersOnline(lua_State* L) { // guild:getMembersOnline() - const auto &guild = getUserdataShared(L, 1); + const auto &guild = Lua::getUserdataShared(L, 1); if (!guild) { lua_pushnil(L); return 1; @@ -58,8 +78,8 @@ int GuildFunctions::luaGuildGetMembersOnline(lua_State* L) { int index = 0; for (const auto &player : members) { - pushUserdata(L, player); - setMetatable(L, -1, "Player"); + Lua::pushUserdata(L, player); + Lua::setMetatable(L, -1, "Player"); lua_rawseti(L, -2, ++index); } return 1; @@ -67,7 +87,7 @@ int GuildFunctions::luaGuildGetMembersOnline(lua_State* L) { int GuildFunctions::luaGuildGetBankBalance(lua_State* L) { // guild:getBankBalance() - const auto &guild = getUserdataShared(L, 1); + const auto &guild = Lua::getUserdataShared(L, 1); if (!guild) { lua_pushnil(L); return 1; @@ -78,47 +98,47 @@ int GuildFunctions::luaGuildGetBankBalance(lua_State* L) { int GuildFunctions::luaGuildSetBankBalance(lua_State* L) { // guild:setBankBalance(bankBalance) - const auto &guild = getUserdataShared(L, 1); + const auto &guild = Lua::getUserdataShared(L, 1); if (!guild) { lua_pushnil(L); return 1; } - guild->setBankBalance(getNumber(L, 2)); - pushBoolean(L, true); + guild->setBankBalance(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); return 1; } int GuildFunctions::luaGuildAddRank(lua_State* L) { // guild:addRank(id, name, level) - const auto &guild = getUserdataShared(L, 1); + const auto &guild = Lua::getUserdataShared(L, 1); if (!guild) { lua_pushnil(L); return 1; } - const uint32_t id = getNumber(L, 2); - const std::string &name = getString(L, 3); - const uint8_t level = getNumber(L, 4); + const uint32_t id = Lua::getNumber(L, 2); + const std::string &name = Lua::getString(L, 3); + const uint8_t level = Lua::getNumber(L, 4); guild->addRank(id, name, level); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int GuildFunctions::luaGuildGetRankById(lua_State* L) { // guild:getRankById(id) - const auto &guild = getUserdataShared(L, 1); + const auto &guild = Lua::getUserdataShared(L, 1); if (!guild) { lua_pushnil(L); return 1; } - const uint32_t id = getNumber(L, 2); + const uint32_t id = Lua::getNumber(L, 2); const GuildRank_ptr rank = guild->getRankById(id); if (rank) { lua_createtable(L, 0, 3); - setField(L, "id", rank->id); - setField(L, "name", rank->name); - setField(L, "level", rank->level); + Lua::setField(L, "id", rank->id); + Lua::setField(L, "name", rank->name); + Lua::setField(L, "level", rank->level); } else { lua_pushnil(L); } @@ -127,19 +147,19 @@ int GuildFunctions::luaGuildGetRankById(lua_State* L) { int GuildFunctions::luaGuildGetRankByLevel(lua_State* L) { // guild:getRankByLevel(level) - const auto &guild = getUserdataShared(L, 1); + const auto &guild = Lua::getUserdataShared(L, 1); if (!guild) { lua_pushnil(L); return 1; } - const uint8_t level = getNumber(L, 2); + const uint8_t level = Lua::getNumber(L, 2); const GuildRank_ptr rank = guild->getRankByLevel(level); if (rank) { lua_createtable(L, 0, 3); - setField(L, "id", rank->id); - setField(L, "name", rank->name); - setField(L, "level", rank->level); + Lua::setField(L, "id", rank->id); + Lua::setField(L, "name", rank->name); + Lua::setField(L, "level", rank->level); } else { lua_pushnil(L); } @@ -148,24 +168,24 @@ int GuildFunctions::luaGuildGetRankByLevel(lua_State* L) { int GuildFunctions::luaGuildGetMotd(lua_State* L) { // guild:getMotd() - const auto &guild = getUserdataShared(L, 1); + const auto &guild = Lua::getUserdataShared(L, 1); if (!guild) { lua_pushnil(L); return 1; } - pushString(L, guild->getMotd()); + Lua::pushString(L, guild->getMotd()); return 1; } int GuildFunctions::luaGuildSetMotd(lua_State* L) { // guild:setMotd(motd) - const auto &guild = getUserdataShared(L, 1); + const auto &guild = Lua::getUserdataShared(L, 1); if (!guild) { lua_pushnil(L); return 1; } - const std::string &motd = getString(L, 2); + const std::string &motd = Lua::getString(L, 2); guild->setMotd(motd); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } diff --git a/src/lua/functions/creatures/player/guild_functions.hpp b/src/lua/functions/creatures/player/guild_functions.hpp index d8f844de751..9c5ded2b63b 100644 --- a/src/lua/functions/creatures/player/guild_functions.hpp +++ b/src/lua/functions/creatures/player/guild_functions.hpp @@ -9,34 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class GuildFunctions final : LuaScriptInterface { +class GuildFunctions { public: - explicit GuildFunctions(lua_State* L) : - LuaScriptInterface("GuildFunctions") { - init(L); - } - ~GuildFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Guild", "", GuildFunctions::luaGuildCreate); - registerMetaMethod(L, "Guild", "__eq", GuildFunctions::luaUserdataCompare); - - registerMethod(L, "Guild", "getId", GuildFunctions::luaGuildGetId); - registerMethod(L, "Guild", "getName", GuildFunctions::luaGuildGetName); - registerMethod(L, "Guild", "getMembersOnline", GuildFunctions::luaGuildGetMembersOnline); - - registerMethod(L, "Guild", "getBankBalance", GuildFunctions::luaGuildGetBankBalance); - registerMethod(L, "Guild", "setBankBalance", GuildFunctions::luaGuildSetBankBalance); - - registerMethod(L, "Guild", "addRank", GuildFunctions::luaGuildAddRank); - registerMethod(L, "Guild", "getRankById", GuildFunctions::luaGuildGetRankById); - registerMethod(L, "Guild", "getRankByLevel", GuildFunctions::luaGuildGetRankByLevel); - - registerMethod(L, "Guild", "getMotd", GuildFunctions::luaGuildGetMotd); - registerMethod(L, "Guild", "setMotd", GuildFunctions::luaGuildSetMotd); - } + static void init(lua_State* L); private: static int luaGuildCreate(lua_State* L); diff --git a/src/lua/functions/creatures/player/mount_functions.cpp b/src/lua/functions/creatures/player/mount_functions.cpp index 7822e4f8ea5..6c6d929ed5c 100644 --- a/src/lua/functions/creatures/player/mount_functions.cpp +++ b/src/lua/functions/creatures/player/mount_functions.cpp @@ -11,22 +11,33 @@ #include "creatures/appearance/mounts/mounts.hpp" #include "game/game.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void MountFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Mount", "", MountFunctions::luaCreateMount); + Lua::registerMetaMethod(L, "Mount", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Mount", "getName", MountFunctions::luaMountGetName); + Lua::registerMethod(L, "Mount", "getId", MountFunctions::luaMountGetId); + Lua::registerMethod(L, "Mount", "getClientId", MountFunctions::luaMountGetClientId); + Lua::registerMethod(L, "Mount", "getSpeed", MountFunctions::luaMountGetSpeed); +} int MountFunctions::luaCreateMount(lua_State* L) { // Mount(id or name) std::shared_ptr mount; - if (isNumber(L, 2)) { - mount = g_game().mounts->getMountByID(getNumber(L, 2)); - } else if (isString(L, 2)) { - std::string mountName = getString(L, 2); + if (Lua::isNumber(L, 2)) { + mount = g_game().mounts->getMountByID(Lua::getNumber(L, 2)); + } else if (Lua::isString(L, 2)) { + std::string mountName = Lua::getString(L, 2); mount = g_game().mounts->getMountByName(mountName); } else { mount = nullptr; } if (mount) { - pushUserdata(L, mount); - setMetatable(L, -1, "Mount"); + Lua::pushUserdata(L, mount); + Lua::setMetatable(L, -1, "Mount"); } else { lua_pushnil(L); } @@ -36,9 +47,9 @@ int MountFunctions::luaCreateMount(lua_State* L) { int MountFunctions::luaMountGetName(lua_State* L) { // mount:getName() - const auto &mount = getUserdataShared(L, 1); + const auto &mount = Lua::getUserdataShared(L, 1); if (mount) { - pushString(L, mount->name); + Lua::pushString(L, mount->name); } else { lua_pushnil(L); } @@ -48,7 +59,7 @@ int MountFunctions::luaMountGetName(lua_State* L) { int MountFunctions::luaMountGetId(lua_State* L) { // mount:getId() - const auto &mount = getUserdataShared(L, 1); + const auto &mount = Lua::getUserdataShared(L, 1); if (mount) { lua_pushnumber(L, mount->id); } else { @@ -60,7 +71,7 @@ int MountFunctions::luaMountGetId(lua_State* L) { int MountFunctions::luaMountGetClientId(lua_State* L) { // mount:getClientId() - const auto &mount = getUserdataShared(L, 1); + const auto &mount = Lua::getUserdataShared(L, 1); if (mount) { lua_pushnumber(L, mount->clientId); } else { @@ -72,7 +83,7 @@ int MountFunctions::luaMountGetClientId(lua_State* L) { int MountFunctions::luaMountGetSpeed(lua_State* L) { // mount:getSpeed() - const auto &mount = getUserdataShared(L, 1); + const auto &mount = Lua::getUserdataShared(L, 1); if (mount) { lua_pushnumber(L, mount->speed); } else { diff --git a/src/lua/functions/creatures/player/mount_functions.hpp b/src/lua/functions/creatures/player/mount_functions.hpp index a6ce7003ecc..e49e67173e3 100644 --- a/src/lua/functions/creatures/player/mount_functions.hpp +++ b/src/lua/functions/creatures/player/mount_functions.hpp @@ -9,25 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class MountFunctions final : LuaScriptInterface { +class MountFunctions { public: - explicit MountFunctions(lua_State* L) : - LuaScriptInterface("MountFunctions") { - init(L); - } - ~MountFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Mount", "", MountFunctions::luaCreateMount); - registerMetaMethod(L, "Mount", "__eq", MountFunctions::luaUserdataCompare); - - registerMethod(L, "Mount", "getName", MountFunctions::luaMountGetName); - registerMethod(L, "Mount", "getId", MountFunctions::luaMountGetId); - registerMethod(L, "Mount", "getClientId", MountFunctions::luaMountGetClientId); - registerMethod(L, "Mount", "getSpeed", MountFunctions::luaMountGetSpeed); - } + static void init(lua_State* L); private: static int luaCreateMount(lua_State* L); diff --git a/src/lua/functions/creatures/player/party_functions.cpp b/src/lua/functions/creatures/player/party_functions.cpp index 2c420c3ec3f..5c43593282a 100644 --- a/src/lua/functions/creatures/player/party_functions.cpp +++ b/src/lua/functions/creatures/player/party_functions.cpp @@ -12,10 +12,31 @@ #include "creatures/players/grouping/party.hpp" #include "creatures/players/player.hpp" #include "game/game.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void PartyFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Party", "", PartyFunctions::luaPartyCreate); + Lua::registerMetaMethod(L, "Party", "__eq", Lua::luaUserdataCompare); + Lua::registerMethod(L, "Party", "disband", PartyFunctions::luaPartyDisband); + Lua::registerMethod(L, "Party", "getLeader", PartyFunctions::luaPartyGetLeader); + Lua::registerMethod(L, "Party", "setLeader", PartyFunctions::luaPartySetLeader); + Lua::registerMethod(L, "Party", "getMembers", PartyFunctions::luaPartyGetMembers); + Lua::registerMethod(L, "Party", "getMemberCount", PartyFunctions::luaPartyGetMemberCount); + Lua::registerMethod(L, "Party", "getInvitees", PartyFunctions::luaPartyGetInvitees); + Lua::registerMethod(L, "Party", "getInviteeCount", PartyFunctions::luaPartyGetInviteeCount); + Lua::registerMethod(L, "Party", "addInvite", PartyFunctions::luaPartyAddInvite); + Lua::registerMethod(L, "Party", "removeInvite", PartyFunctions::luaPartyRemoveInvite); + Lua::registerMethod(L, "Party", "addMember", PartyFunctions::luaPartyAddMember); + Lua::registerMethod(L, "Party", "removeMember", PartyFunctions::luaPartyRemoveMember); + Lua::registerMethod(L, "Party", "isSharedExperienceActive", PartyFunctions::luaPartyIsSharedExperienceActive); + Lua::registerMethod(L, "Party", "isSharedExperienceEnabled", PartyFunctions::luaPartyIsSharedExperienceEnabled); + Lua::registerMethod(L, "Party", "shareExperience", PartyFunctions::luaPartyShareExperience); + Lua::registerMethod(L, "Party", "setSharedExperience", PartyFunctions::luaPartySetSharedExperience); +} int32_t PartyFunctions::luaPartyCreate(lua_State* L) { // Party(userdata) - const auto &player = getUserdataShared(L, 2); + const auto &player = Lua::getUserdataShared(L, 2); if (!player) { lua_pushnil(L); return 1; @@ -26,8 +47,8 @@ int32_t PartyFunctions::luaPartyCreate(lua_State* L) { party = Party::create(player); g_game().updatePlayerShield(player); player->sendCreatureSkull(player); - pushUserdata(L, party); - setMetatable(L, -1, "Party"); + Lua::pushUserdata(L, party); + Lua::setMetatable(L, -1, "Party"); } else { lua_pushnil(L); } @@ -36,12 +57,12 @@ int32_t PartyFunctions::luaPartyCreate(lua_State* L) { int PartyFunctions::luaPartyDisband(lua_State* L) { // party:disband() - auto* partyPtr = getRawUserDataShared(L, 1); + auto* partyPtr = Lua::getRawUserDataShared(L, 1); if (partyPtr && *partyPtr) { std::shared_ptr &party = *partyPtr; party->disband(); party = nullptr; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -50,7 +71,7 @@ int PartyFunctions::luaPartyDisband(lua_State* L) { int PartyFunctions::luaPartyGetLeader(lua_State* L) { // party:getLeader() - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (!party) { lua_pushnil(L); return 1; @@ -58,8 +79,8 @@ int PartyFunctions::luaPartyGetLeader(lua_State* L) { const auto &leader = party->getLeader(); if (leader) { - pushUserdata(L, leader); - setMetatable(L, -1, "Player"); + Lua::pushUserdata(L, leader); + Lua::setMetatable(L, -1, "Player"); } else { lua_pushnil(L); } @@ -68,15 +89,15 @@ int PartyFunctions::luaPartyGetLeader(lua_State* L) { int PartyFunctions::luaPartySetLeader(lua_State* L) { // party:setLeader(player) - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (party) { - pushBoolean(L, party->passPartyLeadership(player)); + Lua::pushBoolean(L, party->passPartyLeadership(player)); } else { lua_pushnil(L); } @@ -85,7 +106,7 @@ int PartyFunctions::luaPartySetLeader(lua_State* L) { int PartyFunctions::luaPartyGetMembers(lua_State* L) { // party:getMembers() - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (!party) { lua_pushnil(L); return 1; @@ -94,8 +115,8 @@ int PartyFunctions::luaPartyGetMembers(lua_State* L) { int index = 0; lua_createtable(L, party->getMemberCount(), 0); for (const auto &player : party->getMembers()) { - pushUserdata(L, player); - setMetatable(L, -1, "Player"); + Lua::pushUserdata(L, player); + Lua::setMetatable(L, -1, "Player"); lua_rawseti(L, -2, ++index); } return 1; @@ -103,7 +124,7 @@ int PartyFunctions::luaPartyGetMembers(lua_State* L) { int PartyFunctions::luaPartyGetMemberCount(lua_State* L) { // party:getMemberCount() - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (party) { lua_pushnumber(L, party->getMemberCount()); } else { @@ -114,14 +135,14 @@ int PartyFunctions::luaPartyGetMemberCount(lua_State* L) { int PartyFunctions::luaPartyGetInvitees(lua_State* L) { // party:getInvitees() - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (party) { lua_createtable(L, party->getInvitationCount(), 0); int index = 0; for (const auto &player : party->getInvitees()) { - pushUserdata(L, player); - setMetatable(L, -1, "Player"); + Lua::pushUserdata(L, player); + Lua::setMetatable(L, -1, "Player"); lua_rawseti(L, -2, ++index); } } else { @@ -132,7 +153,7 @@ int PartyFunctions::luaPartyGetInvitees(lua_State* L) { int PartyFunctions::luaPartyGetInviteeCount(lua_State* L) { // party:getInviteeCount() - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (party) { lua_pushnumber(L, party->getInvitationCount()); } else { @@ -143,15 +164,15 @@ int PartyFunctions::luaPartyGetInviteeCount(lua_State* L) { int PartyFunctions::luaPartyAddInvite(lua_State* L) { // party:addInvite(player) - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (party && player) { - pushBoolean(L, party->invitePlayer(player)); + Lua::pushBoolean(L, party->invitePlayer(player)); } else { lua_pushnil(L); } @@ -160,15 +181,15 @@ int PartyFunctions::luaPartyAddInvite(lua_State* L) { int PartyFunctions::luaPartyRemoveInvite(lua_State* L) { // party:removeInvite(player) - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (party && player) { - pushBoolean(L, party->removeInvite(player)); + Lua::pushBoolean(L, party->removeInvite(player)); } else { lua_pushnil(L); } @@ -177,15 +198,15 @@ int PartyFunctions::luaPartyRemoveInvite(lua_State* L) { int PartyFunctions::luaPartyAddMember(lua_State* L) { // party:addMember(player) - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (party && player) { - pushBoolean(L, party->joinParty(player)); + Lua::pushBoolean(L, party->joinParty(player)); } else { lua_pushnil(L); } @@ -194,15 +215,15 @@ int PartyFunctions::luaPartyAddMember(lua_State* L) { int PartyFunctions::luaPartyRemoveMember(lua_State* L) { // party:removeMember(player) - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (party && player) { - pushBoolean(L, party->leaveParty(player)); + Lua::pushBoolean(L, party->leaveParty(player)); } else { lua_pushnil(L); } @@ -211,9 +232,9 @@ int PartyFunctions::luaPartyRemoveMember(lua_State* L) { int PartyFunctions::luaPartyIsSharedExperienceActive(lua_State* L) { // party:isSharedExperienceActive() - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (party) { - pushBoolean(L, party->isSharedExperienceActive()); + Lua::pushBoolean(L, party->isSharedExperienceActive()); } else { lua_pushnil(L); } @@ -222,9 +243,9 @@ int PartyFunctions::luaPartyIsSharedExperienceActive(lua_State* L) { int PartyFunctions::luaPartyIsSharedExperienceEnabled(lua_State* L) { // party:isSharedExperienceEnabled() - const auto &party = getUserdataShared(L, 1); + const auto &party = Lua::getUserdataShared(L, 1); if (party) { - pushBoolean(L, party->isSharedExperienceEnabled()); + Lua::pushBoolean(L, party->isSharedExperienceEnabled()); } else { lua_pushnil(L); } @@ -233,11 +254,11 @@ int PartyFunctions::luaPartyIsSharedExperienceEnabled(lua_State* L) { int PartyFunctions::luaPartyShareExperience(lua_State* L) { // party:shareExperience(experience) - const uint64_t experience = getNumber(L, 2); - const auto &party = getUserdataShared(L, 1); + const uint64_t experience = Lua::getNumber(L, 2); + const auto &party = Lua::getUserdataShared(L, 1); if (party) { party->shareExperience(experience); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -246,10 +267,10 @@ int PartyFunctions::luaPartyShareExperience(lua_State* L) { int PartyFunctions::luaPartySetSharedExperience(lua_State* L) { // party:setSharedExperience(active) - const bool active = getBoolean(L, 2); - const auto &party = getUserdataShared(L, 1); + const bool active = Lua::getBoolean(L, 2); + const auto &party = Lua::getUserdataShared(L, 1); if (party) { - pushBoolean(L, party->setSharedExperience(party->getLeader(), active)); + Lua::pushBoolean(L, party->setSharedExperience(party->getLeader(), active)); } else { lua_pushnil(L); } diff --git a/src/lua/functions/creatures/player/party_functions.hpp b/src/lua/functions/creatures/player/party_functions.hpp index 79e6a07de71..52801e349d6 100644 --- a/src/lua/functions/creatures/player/party_functions.hpp +++ b/src/lua/functions/creatures/player/party_functions.hpp @@ -9,35 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class PartyFunctions final : LuaScriptInterface { +class PartyFunctions { public: - explicit PartyFunctions(lua_State* L) : - LuaScriptInterface("PartyFunctions") { - init(L); - } - ~PartyFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Party", "", PartyFunctions::luaPartyCreate); - registerMetaMethod(L, "Party", "__eq", PartyFunctions::luaUserdataCompare); - registerMethod(L, "Party", "disband", PartyFunctions::luaPartyDisband); - registerMethod(L, "Party", "getLeader", PartyFunctions::luaPartyGetLeader); - registerMethod(L, "Party", "setLeader", PartyFunctions::luaPartySetLeader); - registerMethod(L, "Party", "getMembers", PartyFunctions::luaPartyGetMembers); - registerMethod(L, "Party", "getMemberCount", PartyFunctions::luaPartyGetMemberCount); - registerMethod(L, "Party", "getInvitees", PartyFunctions::luaPartyGetInvitees); - registerMethod(L, "Party", "getInviteeCount", PartyFunctions::luaPartyGetInviteeCount); - registerMethod(L, "Party", "addInvite", PartyFunctions::luaPartyAddInvite); - registerMethod(L, "Party", "removeInvite", PartyFunctions::luaPartyRemoveInvite); - registerMethod(L, "Party", "addMember", PartyFunctions::luaPartyAddMember); - registerMethod(L, "Party", "removeMember", PartyFunctions::luaPartyRemoveMember); - registerMethod(L, "Party", "isSharedExperienceActive", PartyFunctions::luaPartyIsSharedExperienceActive); - registerMethod(L, "Party", "isSharedExperienceEnabled", PartyFunctions::luaPartyIsSharedExperienceEnabled); - registerMethod(L, "Party", "shareExperience", PartyFunctions::luaPartyShareExperience); - registerMethod(L, "Party", "setSharedExperience", PartyFunctions::luaPartySetSharedExperience); - } + static void init(lua_State* L); private: static int luaPartyCreate(lua_State* L); diff --git a/src/lua/functions/creatures/player/player_functions.cpp b/src/lua/functions/creatures/player/player_functions.cpp index 7ef2be62ca4..34c665f2fe3 100644 --- a/src/lua/functions/creatures/player/player_functions.cpp +++ b/src/lua/functions/creatures/player/player_functions.cpp @@ -33,93 +33,464 @@ #include "items/containers/rewards/reward.hpp" #include "items/item.hpp" #include "map/spectators.hpp" +#include "kv/kv.hpp" #include "enums/account_coins.hpp" #include "enums/account_errors.hpp" #include "enums/player_icons.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void PlayerFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Player", "Creature", PlayerFunctions::luaPlayerCreate); + Lua::registerMetaMethod(L, "Player", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Player", "resetCharmsBestiary", PlayerFunctions::luaPlayerResetCharmsMonsters); + Lua::registerMethod(L, "Player", "unlockAllCharmRunes", PlayerFunctions::luaPlayerUnlockAllCharmRunes); + Lua::registerMethod(L, "Player", "addCharmPoints", PlayerFunctions::luaPlayeraddCharmPoints); + Lua::registerMethod(L, "Player", "isPlayer", PlayerFunctions::luaPlayerIsPlayer); + + Lua::registerMethod(L, "Player", "getGuid", PlayerFunctions::luaPlayerGetGuid); + Lua::registerMethod(L, "Player", "getIp", PlayerFunctions::luaPlayerGetIp); + Lua::registerMethod(L, "Player", "getAccountId", PlayerFunctions::luaPlayerGetAccountId); + Lua::registerMethod(L, "Player", "getLastLoginSaved", PlayerFunctions::luaPlayerGetLastLoginSaved); + Lua::registerMethod(L, "Player", "getLastLogout", PlayerFunctions::luaPlayerGetLastLogout); + + Lua::registerMethod(L, "Player", "getAccountType", PlayerFunctions::luaPlayerGetAccountType); + Lua::registerMethod(L, "Player", "setAccountType", PlayerFunctions::luaPlayerSetAccountType); + + Lua::registerMethod(L, "Player", "isMonsterBestiaryUnlocked", PlayerFunctions::luaPlayerIsMonsterBestiaryUnlocked); + Lua::registerMethod(L, "Player", "addBestiaryKill", PlayerFunctions::luaPlayerAddBestiaryKill); + Lua::registerMethod(L, "Player", "charmExpansion", PlayerFunctions::luaPlayercharmExpansion); + Lua::registerMethod(L, "Player", "getCharmMonsterType", PlayerFunctions::luaPlayergetCharmMonsterType); + + Lua::registerMethod(L, "Player", "isMonsterPrey", PlayerFunctions::luaPlayerisMonsterPrey); + Lua::registerMethod(L, "Player", "getPreyCards", PlayerFunctions::luaPlayerGetPreyCards); + Lua::registerMethod(L, "Player", "getPreyLootPercentage", PlayerFunctions::luaPlayerGetPreyLootPercentage); + Lua::registerMethod(L, "Player", "getPreyExperiencePercentage", PlayerFunctions::luaPlayerGetPreyExperiencePercentage); + Lua::registerMethod(L, "Player", "preyThirdSlot", PlayerFunctions::luaPlayerPreyThirdSlot); + Lua::registerMethod(L, "Player", "taskHuntingThirdSlot", PlayerFunctions::luaPlayerTaskThirdSlot); + Lua::registerMethod(L, "Player", "removePreyStamina", PlayerFunctions::luaPlayerRemovePreyStamina); + Lua::registerMethod(L, "Player", "addPreyCards", PlayerFunctions::luaPlayerAddPreyCards); + Lua::registerMethod(L, "Player", "removeTaskHuntingPoints", PlayerFunctions::luaPlayerRemoveTaskHuntingPoints); + Lua::registerMethod(L, "Player", "getTaskHuntingPoints", PlayerFunctions::luaPlayerGetTaskHuntingPoints); + Lua::registerMethod(L, "Player", "addTaskHuntingPoints", PlayerFunctions::luaPlayerAddTaskHuntingPoints); + + Lua::registerMethod(L, "Player", "getCapacity", PlayerFunctions::luaPlayerGetCapacity); + Lua::registerMethod(L, "Player", "setCapacity", PlayerFunctions::luaPlayerSetCapacity); + + Lua::registerMethod(L, "Player", "isTraining", PlayerFunctions::luaPlayerGetIsTraining); + Lua::registerMethod(L, "Player", "setTraining", PlayerFunctions::luaPlayerSetTraining); + + Lua::registerMethod(L, "Player", "getFreeCapacity", PlayerFunctions::luaPlayerGetFreeCapacity); + + Lua::registerMethod(L, "Player", "getKills", PlayerFunctions::luaPlayerGetKills); + Lua::registerMethod(L, "Player", "setKills", PlayerFunctions::luaPlayerSetKills); + + Lua::registerMethod(L, "Player", "getReward", PlayerFunctions::luaPlayerGetReward); + Lua::registerMethod(L, "Player", "removeReward", PlayerFunctions::luaPlayerRemoveReward); + Lua::registerMethod(L, "Player", "getRewardList", PlayerFunctions::luaPlayerGetRewardList); + + Lua::registerMethod(L, "Player", "setDailyReward", PlayerFunctions::luaPlayerSetDailyReward); + + Lua::registerMethod(L, "Player", "sendInventory", PlayerFunctions::luaPlayerSendInventory); + Lua::registerMethod(L, "Player", "sendLootStats", PlayerFunctions::luaPlayerSendLootStats); + Lua::registerMethod(L, "Player", "updateSupplyTracker", PlayerFunctions::luaPlayerUpdateSupplyTracker); + Lua::registerMethod(L, "Player", "updateKillTracker", PlayerFunctions::luaPlayerUpdateKillTracker); + + Lua::registerMethod(L, "Player", "getDepotLocker", PlayerFunctions::luaPlayerGetDepotLocker); + Lua::registerMethod(L, "Player", "getDepotChest", PlayerFunctions::luaPlayerGetDepotChest); + Lua::registerMethod(L, "Player", "getInbox", PlayerFunctions::luaPlayerGetInbox); + + Lua::registerMethod(L, "Player", "getSkullTime", PlayerFunctions::luaPlayerGetSkullTime); + Lua::registerMethod(L, "Player", "setSkullTime", PlayerFunctions::luaPlayerSetSkullTime); + Lua::registerMethod(L, "Player", "getDeathPenalty", PlayerFunctions::luaPlayerGetDeathPenalty); + + Lua::registerMethod(L, "Player", "getExperience", PlayerFunctions::luaPlayerGetExperience); + Lua::registerMethod(L, "Player", "addExperience", PlayerFunctions::luaPlayerAddExperience); + Lua::registerMethod(L, "Player", "removeExperience", PlayerFunctions::luaPlayerRemoveExperience); + Lua::registerMethod(L, "Player", "getLevel", PlayerFunctions::luaPlayerGetLevel); + + Lua::registerMethod(L, "Player", "getMagicShieldCapacityFlat", PlayerFunctions::luaPlayerGetMagicShieldCapacityFlat); + Lua::registerMethod(L, "Player", "getMagicShieldCapacityPercent", PlayerFunctions::luaPlayerGetMagicShieldCapacityPercent); + + Lua::registerMethod(L, "Player", "sendSpellCooldown", PlayerFunctions::luaPlayerSendSpellCooldown); + Lua::registerMethod(L, "Player", "sendSpellGroupCooldown", PlayerFunctions::luaPlayerSendSpellGroupCooldown); + + Lua::registerMethod(L, "Player", "getMagicLevel", PlayerFunctions::luaPlayerGetMagicLevel); + Lua::registerMethod(L, "Player", "getBaseMagicLevel", PlayerFunctions::luaPlayerGetBaseMagicLevel); + Lua::registerMethod(L, "Player", "getMana", PlayerFunctions::luaPlayerGetMana); + Lua::registerMethod(L, "Player", "addMana", PlayerFunctions::luaPlayerAddMana); + Lua::registerMethod(L, "Player", "getMaxMana", PlayerFunctions::luaPlayerGetMaxMana); + Lua::registerMethod(L, "Player", "setMaxMana", PlayerFunctions::luaPlayerSetMaxMana); + Lua::registerMethod(L, "Player", "getManaSpent", PlayerFunctions::luaPlayerGetManaSpent); + Lua::registerMethod(L, "Player", "addManaSpent", PlayerFunctions::luaPlayerAddManaSpent); + + Lua::registerMethod(L, "Player", "getBaseMaxHealth", PlayerFunctions::luaPlayerGetBaseMaxHealth); + Lua::registerMethod(L, "Player", "getBaseMaxMana", PlayerFunctions::luaPlayerGetBaseMaxMana); + + Lua::registerMethod(L, "Player", "getSkillLevel", PlayerFunctions::luaPlayerGetSkillLevel); + Lua::registerMethod(L, "Player", "getEffectiveSkillLevel", PlayerFunctions::luaPlayerGetEffectiveSkillLevel); + Lua::registerMethod(L, "Player", "getSkillPercent", PlayerFunctions::luaPlayerGetSkillPercent); + Lua::registerMethod(L, "Player", "getSkillTries", PlayerFunctions::luaPlayerGetSkillTries); + Lua::registerMethod(L, "Player", "addSkillTries", PlayerFunctions::luaPlayerAddSkillTries); + + Lua::registerMethod(L, "Player", "setLevel", PlayerFunctions::luaPlayerSetLevel); + Lua::registerMethod(L, "Player", "setMagicLevel", PlayerFunctions::luaPlayerSetMagicLevel); + Lua::registerMethod(L, "Player", "setSkillLevel", PlayerFunctions::luaPlayerSetSkillLevel); + + Lua::registerMethod(L, "Player", "addOfflineTrainingTime", PlayerFunctions::luaPlayerAddOfflineTrainingTime); + Lua::registerMethod(L, "Player", "getOfflineTrainingTime", PlayerFunctions::luaPlayerGetOfflineTrainingTime); + Lua::registerMethod(L, "Player", "removeOfflineTrainingTime", PlayerFunctions::luaPlayerRemoveOfflineTrainingTime); + + Lua::registerMethod(L, "Player", "addOfflineTrainingTries", PlayerFunctions::luaPlayerAddOfflineTrainingTries); + + Lua::registerMethod(L, "Player", "getOfflineTrainingSkill", PlayerFunctions::luaPlayerGetOfflineTrainingSkill); + Lua::registerMethod(L, "Player", "setOfflineTrainingSkill", PlayerFunctions::luaPlayerSetOfflineTrainingSkill); + + Lua::registerMethod(L, "Player", "getItemCount", PlayerFunctions::luaPlayerGetItemCount); + Lua::registerMethod(L, "Player", "getStashItemCount", PlayerFunctions::luaPlayerGetStashItemCount); + Lua::registerMethod(L, "Player", "getItemById", PlayerFunctions::luaPlayerGetItemById); + + Lua::registerMethod(L, "Player", "getVocation", PlayerFunctions::luaPlayerGetVocation); + Lua::registerMethod(L, "Player", "setVocation", PlayerFunctions::luaPlayerSetVocation); + Lua::registerMethod(L, "Player", "isPromoted", PlayerFunctions::luaPlayerIsPromoted); + + Lua::registerMethod(L, "Player", "getSex", PlayerFunctions::luaPlayerGetSex); + Lua::registerMethod(L, "Player", "setSex", PlayerFunctions::luaPlayerSetSex); + + Lua::registerMethod(L, "Player", "getPronoun", PlayerFunctions::luaPlayerGetPronoun); + Lua::registerMethod(L, "Player", "setPronoun", PlayerFunctions::luaPlayerSetPronoun); + + Lua::registerMethod(L, "Player", "getTown", PlayerFunctions::luaPlayerGetTown); + Lua::registerMethod(L, "Player", "setTown", PlayerFunctions::luaPlayerSetTown); + + Lua::registerMethod(L, "Player", "getGuild", PlayerFunctions::luaPlayerGetGuild); + Lua::registerMethod(L, "Player", "setGuild", PlayerFunctions::luaPlayerSetGuild); + + Lua::registerMethod(L, "Player", "getGuildLevel", PlayerFunctions::luaPlayerGetGuildLevel); + Lua::registerMethod(L, "Player", "setGuildLevel", PlayerFunctions::luaPlayerSetGuildLevel); + + Lua::registerMethod(L, "Player", "getGuildNick", PlayerFunctions::luaPlayerGetGuildNick); + Lua::registerMethod(L, "Player", "setGuildNick", PlayerFunctions::luaPlayerSetGuildNick); + + Lua::registerMethod(L, "Player", "getGroup", PlayerFunctions::luaPlayerGetGroup); + Lua::registerMethod(L, "Player", "setGroup", PlayerFunctions::luaPlayerSetGroup); + + Lua::registerMethod(L, "Player", "setSpecialContainersAvailable", PlayerFunctions::luaPlayerSetSpecialContainersAvailable); + Lua::registerMethod(L, "Player", "getStashCount", PlayerFunctions::luaPlayerGetStashCounter); + Lua::registerMethod(L, "Player", "openStash", PlayerFunctions::luaPlayerOpenStash); + + Lua::registerMethod(L, "Player", "getStamina", PlayerFunctions::luaPlayerGetStamina); + Lua::registerMethod(L, "Player", "setStamina", PlayerFunctions::luaPlayerSetStamina); + + Lua::registerMethod(L, "Player", "getSoul", PlayerFunctions::luaPlayerGetSoul); + Lua::registerMethod(L, "Player", "addSoul", PlayerFunctions::luaPlayerAddSoul); + Lua::registerMethod(L, "Player", "getMaxSoul", PlayerFunctions::luaPlayerGetMaxSoul); + + Lua::registerMethod(L, "Player", "getBankBalance", PlayerFunctions::luaPlayerGetBankBalance); + Lua::registerMethod(L, "Player", "setBankBalance", PlayerFunctions::luaPlayerSetBankBalance); + + Lua::registerMethod(L, "Player", "getStorageValue", PlayerFunctions::luaPlayerGetStorageValue); + Lua::registerMethod(L, "Player", "setStorageValue", PlayerFunctions::luaPlayerSetStorageValue); + + Lua::registerMethod(L, "Player", "getStorageValueByName", PlayerFunctions::luaPlayerGetStorageValueByName); + Lua::registerMethod(L, "Player", "setStorageValueByName", PlayerFunctions::luaPlayerSetStorageValueByName); + + Lua::registerMethod(L, "Player", "addItem", PlayerFunctions::luaPlayerAddItem); + Lua::registerMethod(L, "Player", "addItemEx", PlayerFunctions::luaPlayerAddItemEx); + Lua::registerMethod(L, "Player", "addItemStash", PlayerFunctions::luaPlayerAddItemStash); + Lua::registerMethod(L, "Player", "removeStashItem", PlayerFunctions::luaPlayerRemoveStashItem); + Lua::registerMethod(L, "Player", "removeItem", PlayerFunctions::luaPlayerRemoveItem); + Lua::registerMethod(L, "Player", "sendContainer", PlayerFunctions::luaPlayerSendContainer); + Lua::registerMethod(L, "Player", "sendUpdateContainer", PlayerFunctions::luaPlayerSendUpdateContainer); + + Lua::registerMethod(L, "Player", "getMoney", PlayerFunctions::luaPlayerGetMoney); + Lua::registerMethod(L, "Player", "addMoney", PlayerFunctions::luaPlayerAddMoney); + Lua::registerMethod(L, "Player", "removeMoney", PlayerFunctions::luaPlayerRemoveMoney); + + Lua::registerMethod(L, "Player", "showTextDialog", PlayerFunctions::luaPlayerShowTextDialog); + + Lua::registerMethod(L, "Player", "sendTextMessage", PlayerFunctions::luaPlayerSendTextMessage); + Lua::registerMethod(L, "Player", "sendChannelMessage", PlayerFunctions::luaPlayerSendChannelMessage); + Lua::registerMethod(L, "Player", "sendPrivateMessage", PlayerFunctions::luaPlayerSendPrivateMessage); + Lua::registerMethod(L, "Player", "channelSay", PlayerFunctions::luaPlayerChannelSay); + Lua::registerMethod(L, "Player", "openChannel", PlayerFunctions::luaPlayerOpenChannel); + + Lua::registerMethod(L, "Player", "getSlotItem", PlayerFunctions::luaPlayerGetSlotItem); + + Lua::registerMethod(L, "Player", "getParty", PlayerFunctions::luaPlayerGetParty); + + Lua::registerMethod(L, "Player", "addOutfit", PlayerFunctions::luaPlayerAddOutfit); + Lua::registerMethod(L, "Player", "addOutfitAddon", PlayerFunctions::luaPlayerAddOutfitAddon); + Lua::registerMethod(L, "Player", "removeOutfit", PlayerFunctions::luaPlayerRemoveOutfit); + Lua::registerMethod(L, "Player", "removeOutfitAddon", PlayerFunctions::luaPlayerRemoveOutfitAddon); + Lua::registerMethod(L, "Player", "hasOutfit", PlayerFunctions::luaPlayerHasOutfit); + Lua::registerMethod(L, "Player", "sendOutfitWindow", PlayerFunctions::luaPlayerSendOutfitWindow); + + Lua::registerMethod(L, "Player", "addMount", PlayerFunctions::luaPlayerAddMount); + Lua::registerMethod(L, "Player", "removeMount", PlayerFunctions::luaPlayerRemoveMount); + Lua::registerMethod(L, "Player", "hasMount", PlayerFunctions::luaPlayerHasMount); + + Lua::registerMethod(L, "Player", "addFamiliar", PlayerFunctions::luaPlayerAddFamiliar); + Lua::registerMethod(L, "Player", "removeFamiliar", PlayerFunctions::luaPlayerRemoveFamiliar); + Lua::registerMethod(L, "Player", "hasFamiliar", PlayerFunctions::luaPlayerHasFamiliar); + Lua::registerMethod(L, "Player", "setFamiliarLooktype", PlayerFunctions::luaPlayerSetFamiliarLooktype); + Lua::registerMethod(L, "Player", "getFamiliarLooktype", PlayerFunctions::luaPlayerGetFamiliarLooktype); + + Lua::registerMethod(L, "Player", "getPremiumDays", PlayerFunctions::luaPlayerGetPremiumDays); + Lua::registerMethod(L, "Player", "addPremiumDays", PlayerFunctions::luaPlayerAddPremiumDays); + Lua::registerMethod(L, "Player", "removePremiumDays", PlayerFunctions::luaPlayerRemovePremiumDays); + + Lua::registerMethod(L, "Player", "getTibiaCoins", PlayerFunctions::luaPlayerGetTibiaCoins); + Lua::registerMethod(L, "Player", "addTibiaCoins", PlayerFunctions::luaPlayerAddTibiaCoins); + Lua::registerMethod(L, "Player", "removeTibiaCoins", PlayerFunctions::luaPlayerRemoveTibiaCoins); + + Lua::registerMethod(L, "Player", "getTransferableCoins", PlayerFunctions::luaPlayerGetTransferableCoins); + Lua::registerMethod(L, "Player", "addTransferableCoins", PlayerFunctions::luaPlayerAddTransferableCoins); + Lua::registerMethod(L, "Player", "removeTransferableCoins", PlayerFunctions::luaPlayerRemoveTransferableCoins); + + Lua::registerMethod(L, "Player", "hasBlessing", PlayerFunctions::luaPlayerHasBlessing); + Lua::registerMethod(L, "Player", "addBlessing", PlayerFunctions::luaPlayerAddBlessing); + Lua::registerMethod(L, "Player", "removeBlessing", PlayerFunctions::luaPlayerRemoveBlessing); + Lua::registerMethod(L, "Player", "getBlessingCount", PlayerFunctions::luaPlayerGetBlessingCount); + + Lua::registerMethod(L, "Player", "canLearnSpell", PlayerFunctions::luaPlayerCanLearnSpell); + Lua::registerMethod(L, "Player", "learnSpell", PlayerFunctions::luaPlayerLearnSpell); + Lua::registerMethod(L, "Player", "forgetSpell", PlayerFunctions::luaPlayerForgetSpell); + Lua::registerMethod(L, "Player", "hasLearnedSpell", PlayerFunctions::luaPlayerHasLearnedSpell); + + Lua::registerMethod(L, "Player", "openImbuementWindow", PlayerFunctions::luaPlayerOpenImbuementWindow); + Lua::registerMethod(L, "Player", "closeImbuementWindow", PlayerFunctions::luaPlayerCloseImbuementWindow); + + Lua::registerMethod(L, "Player", "sendTutorial", PlayerFunctions::luaPlayerSendTutorial); + Lua::registerMethod(L, "Player", "addMapMark", PlayerFunctions::luaPlayerAddMapMark); + + Lua::registerMethod(L, "Player", "save", PlayerFunctions::luaPlayerSave); + Lua::registerMethod(L, "Player", "popupFYI", PlayerFunctions::luaPlayerPopupFYI); + + Lua::registerMethod(L, "Player", "isPzLocked", PlayerFunctions::luaPlayerIsPzLocked); + + Lua::registerMethod(L, "Player", "getClient", PlayerFunctions::luaPlayerGetClient); + + Lua::registerMethod(L, "Player", "getHouse", PlayerFunctions::luaPlayerGetHouse); + Lua::registerMethod(L, "Player", "sendHouseWindow", PlayerFunctions::luaPlayerSendHouseWindow); + Lua::registerMethod(L, "Player", "setEditHouse", PlayerFunctions::luaPlayerSetEditHouse); + + Lua::registerMethod(L, "Player", "setGhostMode", PlayerFunctions::luaPlayerSetGhostMode); + + Lua::registerMethod(L, "Player", "getContainerId", PlayerFunctions::luaPlayerGetContainerId); + Lua::registerMethod(L, "Player", "getContainerById", PlayerFunctions::luaPlayerGetContainerById); + Lua::registerMethod(L, "Player", "getContainerIndex", PlayerFunctions::luaPlayerGetContainerIndex); + + Lua::registerMethod(L, "Player", "getInstantSpells", PlayerFunctions::luaPlayerGetInstantSpells); + Lua::registerMethod(L, "Player", "canCast", PlayerFunctions::luaPlayerCanCast); + + Lua::registerMethod(L, "Player", "hasChaseMode", PlayerFunctions::luaPlayerHasChaseMode); + Lua::registerMethod(L, "Player", "hasSecureMode", PlayerFunctions::luaPlayerHasSecureMode); + Lua::registerMethod(L, "Player", "getFightMode", PlayerFunctions::luaPlayerGetFightMode); + + Lua::registerMethod(L, "Player", "getBaseXpGain", PlayerFunctions::luaPlayerGetBaseXpGain); + Lua::registerMethod(L, "Player", "setBaseXpGain", PlayerFunctions::luaPlayerSetBaseXpGain); + Lua::registerMethod(L, "Player", "getVoucherXpBoost", PlayerFunctions::luaPlayerGetVoucherXpBoost); + Lua::registerMethod(L, "Player", "setVoucherXpBoost", PlayerFunctions::luaPlayerSetVoucherXpBoost); + Lua::registerMethod(L, "Player", "getGrindingXpBoost", PlayerFunctions::luaPlayerGetGrindingXpBoost); + Lua::registerMethod(L, "Player", "setGrindingXpBoost", PlayerFunctions::luaPlayerSetGrindingXpBoost); + Lua::registerMethod(L, "Player", "getXpBoostPercent", PlayerFunctions::luaPlayerGetXpBoostPercent); + Lua::registerMethod(L, "Player", "setXpBoostPercent", PlayerFunctions::luaPlayerSetXpBoostPercent); + Lua::registerMethod(L, "Player", "getStaminaXpBoost", PlayerFunctions::luaPlayerGetStaminaXpBoost); + Lua::registerMethod(L, "Player", "setStaminaXpBoost", PlayerFunctions::luaPlayerSetStaminaXpBoost); + Lua::registerMethod(L, "Player", "getXpBoostTime", PlayerFunctions::luaPlayerGetXpBoostTime); + Lua::registerMethod(L, "Player", "setXpBoostTime", PlayerFunctions::luaPlayerSetXpBoostTime); + + Lua::registerMethod(L, "Player", "getIdleTime", PlayerFunctions::luaPlayerGetIdleTime); + Lua::registerMethod(L, "Player", "getFreeBackpackSlots", PlayerFunctions::luaPlayerGetFreeBackpackSlots); + + Lua::registerMethod(L, "Player", "isOffline", PlayerFunctions::luaPlayerIsOffline); + + Lua::registerMethod(L, "Player", "openMarket", PlayerFunctions::luaPlayerOpenMarket); + + Lua::registerMethod(L, "Player", "instantSkillWOD", PlayerFunctions::luaPlayerInstantSkillWOD); + Lua::registerMethod(L, "Player", "upgradeSpellsWOD", PlayerFunctions::luaPlayerUpgradeSpellWOD); + Lua::registerMethod(L, "Player", "revelationStageWOD", PlayerFunctions::luaPlayerRevelationStageWOD); + Lua::registerMethod(L, "Player", "reloadData", PlayerFunctions::luaPlayerReloadData); + Lua::registerMethod(L, "Player", "onThinkWheelOfDestiny", PlayerFunctions::luaPlayerOnThinkWheelOfDestiny); + Lua::registerMethod(L, "Player", "avatarTimer", PlayerFunctions::luaPlayerAvatarTimer); + Lua::registerMethod(L, "Player", "getWheelSpellAdditionalArea", PlayerFunctions::luaPlayerGetWheelSpellAdditionalArea); + Lua::registerMethod(L, "Player", "getWheelSpellAdditionalTarget", PlayerFunctions::luaPlayerGetWheelSpellAdditionalTarget); + Lua::registerMethod(L, "Player", "getWheelSpellAdditionalDuration", PlayerFunctions::luaPlayerGetWheelSpellAdditionalDuration); + + // Forge Functions + Lua::registerMethod(L, "Player", "openForge", PlayerFunctions::luaPlayerOpenForge); + Lua::registerMethod(L, "Player", "closeForge", PlayerFunctions::luaPlayerCloseForge); + + Lua::registerMethod(L, "Player", "addForgeDusts", PlayerFunctions::luaPlayerAddForgeDusts); + Lua::registerMethod(L, "Player", "removeForgeDusts", PlayerFunctions::luaPlayerRemoveForgeDusts); + Lua::registerMethod(L, "Player", "getForgeDusts", PlayerFunctions::luaPlayerGetForgeDusts); + Lua::registerMethod(L, "Player", "setForgeDusts", PlayerFunctions::luaPlayerSetForgeDusts); + + Lua::registerMethod(L, "Player", "addForgeDustLevel", PlayerFunctions::luaPlayerAddForgeDustLevel); + Lua::registerMethod(L, "Player", "removeForgeDustLevel", PlayerFunctions::luaPlayerRemoveForgeDustLevel); + Lua::registerMethod(L, "Player", "getForgeDustLevel", PlayerFunctions::luaPlayerGetForgeDustLevel); + + Lua::registerMethod(L, "Player", "getForgeSlivers", PlayerFunctions::luaPlayerGetForgeSlivers); + Lua::registerMethod(L, "Player", "getForgeCores", PlayerFunctions::luaPlayerGetForgeCores); + Lua::registerMethod(L, "Player", "isUIExhausted", PlayerFunctions::luaPlayerIsUIExhausted); + Lua::registerMethod(L, "Player", "updateUIExhausted", PlayerFunctions::luaPlayerUpdateUIExhausted); + + Lua::registerMethod(L, "Player", "setFaction", PlayerFunctions::luaPlayerSetFaction); + Lua::registerMethod(L, "Player", "getFaction", PlayerFunctions::luaPlayerGetFaction); + + // Bosstiary Functions + Lua::registerMethod(L, "Player", "getBosstiaryLevel", PlayerFunctions::luaPlayerGetBosstiaryLevel); + Lua::registerMethod(L, "Player", "getBosstiaryKills", PlayerFunctions::luaPlayerGetBosstiaryKills); + Lua::registerMethod(L, "Player", "addBosstiaryKill", PlayerFunctions::luaPlayerAddBosstiaryKill); + Lua::registerMethod(L, "Player", "setBossPoints", PlayerFunctions::luaPlayerSetBossPoints); + Lua::registerMethod(L, "Player", "setRemoveBossTime", PlayerFunctions::luaPlayerSetRemoveBossTime); + Lua::registerMethod(L, "Player", "getSlotBossId", PlayerFunctions::luaPlayerGetSlotBossId); + Lua::registerMethod(L, "Player", "getBossBonus", PlayerFunctions::luaPlayerGetBossBonus); + Lua::registerMethod(L, "Player", "sendBosstiaryCooldownTimer", PlayerFunctions::luaPlayerBosstiaryCooldownTimer); + + Lua::registerMethod(L, "Player", "sendSingleSoundEffect", PlayerFunctions::luaPlayerSendSingleSoundEffect); + Lua::registerMethod(L, "Player", "sendDoubleSoundEffect", PlayerFunctions::luaPlayerSendDoubleSoundEffect); + + Lua::registerMethod(L, "Player", "getName", PlayerFunctions::luaPlayerGetName); + Lua::registerMethod(L, "Player", "changeName", PlayerFunctions::luaPlayerChangeName); + + Lua::registerMethod(L, "Player", "hasGroupFlag", PlayerFunctions::luaPlayerHasGroupFlag); + Lua::registerMethod(L, "Player", "setGroupFlag", PlayerFunctions::luaPlayerSetGroupFlag); + Lua::registerMethod(L, "Player", "removeGroupFlag", PlayerFunctions::luaPlayerRemoveGroupFlag); + + Lua::registerMethod(L, "Player", "setHazardSystemPoints", PlayerFunctions::luaPlayerAddHazardSystemPoints); + Lua::registerMethod(L, "Player", "getHazardSystemPoints", PlayerFunctions::luaPlayerGetHazardSystemPoints); + + Lua::registerMethod(L, "Player", "setLoyaltyBonus", PlayerFunctions::luaPlayerSetLoyaltyBonus); + Lua::registerMethod(L, "Player", "getLoyaltyBonus", PlayerFunctions::luaPlayerGetLoyaltyBonus); + Lua::registerMethod(L, "Player", "getLoyaltyPoints", PlayerFunctions::luaPlayerGetLoyaltyPoints); + Lua::registerMethod(L, "Player", "getLoyaltyTitle", PlayerFunctions::luaPlayerGetLoyaltyTitle); + Lua::registerMethod(L, "Player", "setLoyaltyTitle", PlayerFunctions::luaPlayerSetLoyaltyTitle); + + Lua::registerMethod(L, "Player", "updateConcoction", PlayerFunctions::luaPlayerUpdateConcoction); + Lua::registerMethod(L, "Player", "clearSpellCooldowns", PlayerFunctions::luaPlayerClearSpellCooldowns); + + Lua::registerMethod(L, "Player", "isVip", PlayerFunctions::luaPlayerIsVip); + Lua::registerMethod(L, "Player", "getVipDays", PlayerFunctions::luaPlayerGetVipDays); + Lua::registerMethod(L, "Player", "getVipTime", PlayerFunctions::luaPlayerGetVipTime); + + Lua::registerMethod(L, "Player", "kv", PlayerFunctions::luaPlayerKV); + Lua::registerMethod(L, "Player", "getStoreInbox", PlayerFunctions::luaPlayerGetStoreInbox); + + Lua::registerMethod(L, "Player", "hasAchievement", PlayerFunctions::luaPlayerHasAchievement); + Lua::registerMethod(L, "Player", "addAchievement", PlayerFunctions::luaPlayerAddAchievement); + Lua::registerMethod(L, "Player", "removeAchievement", PlayerFunctions::luaPlayerRemoveAchievement); + Lua::registerMethod(L, "Player", "getAchievementPoints", PlayerFunctions::luaPlayerGetAchievementPoints); + Lua::registerMethod(L, "Player", "addAchievementPoints", PlayerFunctions::luaPlayerAddAchievementPoints); + Lua::registerMethod(L, "Player", "removeAchievementPoints", PlayerFunctions::luaPlayerRemoveAchievementPoints); + + // Badge Functions + Lua::registerMethod(L, "Player", "addBadge", PlayerFunctions::luaPlayerAddBadge); + + // Title Functions + Lua::registerMethod(L, "Player", "addTitle", PlayerFunctions::luaPlayerAddTitle); + Lua::registerMethod(L, "Player", "getTitles", PlayerFunctions::luaPlayerGetTitles); + Lua::registerMethod(L, "Player", "setCurrentTitle", PlayerFunctions::luaPlayerSetCurrentTitle); + + // Store Summary + Lua::registerMethod(L, "Player", "createTransactionSummary", PlayerFunctions::luaPlayerCreateTransactionSummary); + + Lua::registerMethod(L, "Player", "takeScreenshot", PlayerFunctions::luaPlayerTakeScreenshot); + Lua::registerMethod(L, "Player", "sendIconBakragore", PlayerFunctions::luaPlayerSendIconBakragore); + Lua::registerMethod(L, "Player", "removeIconBakragore", PlayerFunctions::luaPlayerRemoveIconBakragore); + Lua::registerMethod(L, "Player", "sendCreatureAppear", PlayerFunctions::luaPlayerSendCreatureAppear); + + GroupFunctions::init(L); + GuildFunctions::init(L); + MountFunctions::init(L); + PartyFunctions::init(L); + VocationFunctions::init(L); +} int PlayerFunctions::luaPlayerSendInventory(lua_State* L) { // player:sendInventory() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } player->sendInventoryIds(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSendLootStats(lua_State* L) { // player:sendLootStats(item, count) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &item = getUserdataShared(L, 2); + const auto &item = Lua::getUserdataShared(L, 2); if (!item) { lua_pushnil(L); return 1; } - const auto count = getNumber(L, 3, 0); + const auto count = Lua::getNumber(L, 3, 0); if (count == 0) { lua_pushnil(L); return 1; } player->sendLootStats(item, count); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerUpdateSupplyTracker(lua_State* L) { // player:updateSupplyTracker(item) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &item = getUserdataShared(L, 2); + const auto &item = Lua::getUserdataShared(L, 2); if (!item) { lua_pushnil(L); return 1; } player->updateSupplyTracker(item); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerUpdateKillTracker(lua_State* L) { // player:updateKillTracker(creature, corpse) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &monster = getUserdataShared(L, 2); + const auto &monster = Lua::getUserdataShared(L, 2); if (!monster) { lua_pushnil(L); return 1; } - const auto &corpse = getUserdataShared(L, 3); + const auto &corpse = Lua::getUserdataShared(L, 3); if (!corpse) { lua_pushnil(L); return 1; } player->updateKillTracker(corpse, monster->getName(), monster->getCurrentOutfit()); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -128,33 +499,33 @@ int PlayerFunctions::luaPlayerUpdateKillTracker(lua_State* L) { int PlayerFunctions::luaPlayerCreate(lua_State* L) { // Player(id or guid or name or userdata) std::shared_ptr player; - if (isNumber(L, 2)) { - const uint32_t id = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + const uint32_t id = Lua::getNumber(L, 2); if (id >= Player::getFirstID() && id <= Player::getLastID()) { player = g_game().getPlayerByID(id); } else { player = g_game().getPlayerByGUID(id); } - } else if (isString(L, 2)) { - ReturnValue ret = g_game().getPlayerByNameWildcard(getString(L, 2), player); + } else if (Lua::isString(L, 2)) { + ReturnValue ret = g_game().getPlayerByNameWildcard(Lua::getString(L, 2), player); if (ret != RETURNVALUE_NOERROR) { lua_pushnil(L); lua_pushnumber(L, ret); return 2; } - } else if (isUserdata(L, 2)) { - if (getUserdataType(L, 2) != LuaData_t::Player) { + } else if (Lua::isUserdata(L, 2)) { + if (Lua::getUserdataType(L, 2) != LuaData_t::Player) { lua_pushnil(L); return 1; } - player = getUserdataShared(L, 2); + player = Lua::getUserdataShared(L, 2); } else { player = nullptr; } if (player) { - pushUserdata(L, player); - setMetatable(L, -1, "Player"); + Lua::pushUserdata(L, player); + Lua::setMetatable(L, -1, "Player"); } else { lua_pushnil(L); } @@ -163,7 +534,7 @@ int PlayerFunctions::luaPlayerCreate(lua_State* L) { int PlayerFunctions::luaPlayerResetCharmsMonsters(lua_State* L) { // player:resetCharmsBestiary() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { player->setCharmPoints(0); player->setCharmExpansion(false); @@ -172,7 +543,7 @@ int PlayerFunctions::luaPlayerResetCharmsMonsters(lua_State* L) { for (int8_t i = CHARM_WOUND; i <= CHARM_LAST; i++) { player->parseRacebyCharm(static_cast(i), true, 0); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -181,7 +552,7 @@ int PlayerFunctions::luaPlayerResetCharmsMonsters(lua_State* L) { int PlayerFunctions::luaPlayerUnlockAllCharmRunes(lua_State* L) { // player:unlockAllCharmRunes() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { for (int8_t i = CHARM_WOUND; i <= CHARM_LAST; i++) { const auto charm = g_iobestiary().getBestiaryCharm(static_cast(i)); @@ -190,7 +561,7 @@ int PlayerFunctions::luaPlayerUnlockAllCharmRunes(lua_State* L) { player->setUnlockedRunesBit(value); } } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -199,16 +570,16 @@ int PlayerFunctions::luaPlayerUnlockAllCharmRunes(lua_State* L) { int PlayerFunctions::luaPlayeraddCharmPoints(lua_State* L) { // player:addCharmPoints() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - int16_t charms = getNumber(L, 2); + int16_t charms = Lua::getNumber(L, 2); if (charms >= 0) { g_iobestiary().addCharmPoints(player, static_cast(charms)); } else { charms = -charms; g_iobestiary().addCharmPoints(player, static_cast(charms), true); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -217,13 +588,13 @@ int PlayerFunctions::luaPlayeraddCharmPoints(lua_State* L) { int PlayerFunctions::luaPlayerIsPlayer(lua_State* L) { // player:isPlayer() - pushBoolean(L, getUserdataShared(L, 1) != nullptr); + Lua::pushBoolean(L, Lua::getUserdataShared(L, 1) != nullptr); return 1; } int PlayerFunctions::luaPlayerGetGuid(lua_State* L) { // player:getGuid() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getGUID()); } else { @@ -234,7 +605,7 @@ int PlayerFunctions::luaPlayerGetGuid(lua_State* L) { int PlayerFunctions::luaPlayerGetIp(lua_State* L) { // player:getIp() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getIP()); } else { @@ -245,7 +616,7 @@ int PlayerFunctions::luaPlayerGetIp(lua_State* L) { int PlayerFunctions::luaPlayerGetAccountId(lua_State* L) { // player:getAccountId() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || player->getAccountId() == 0) { lua_pushnil(L); return 1; @@ -258,7 +629,7 @@ int PlayerFunctions::luaPlayerGetAccountId(lua_State* L) { int PlayerFunctions::luaPlayerGetLastLoginSaved(lua_State* L) { // player:getLastLoginSaved() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getLastLoginSaved()); } else { @@ -269,7 +640,7 @@ int PlayerFunctions::luaPlayerGetLastLoginSaved(lua_State* L) { int PlayerFunctions::luaPlayerGetLastLogout(lua_State* L) { // player:getLastLogout() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getLastLogout()); } else { @@ -280,7 +651,7 @@ int PlayerFunctions::luaPlayerGetLastLogout(lua_State* L) { int PlayerFunctions::luaPlayerGetAccountType(lua_State* L) { // player:getAccountType() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getAccountType()); } else { @@ -291,13 +662,13 @@ int PlayerFunctions::luaPlayerGetAccountType(lua_State* L) { int PlayerFunctions::luaPlayerSetAccountType(lua_State* L) { // player:setAccountType(accountType) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || !player->getAccount()) { lua_pushnil(L); return 1; } - if (player->getAccount()->setAccountType(getNumber(L, 2)) != AccountErrors_t::Ok) { + if (player->getAccount()->setAccountType(Lua::getNumber(L, 2)) != AccountErrors_t::Ok) { lua_pushnil(L); return 1; } @@ -307,18 +678,18 @@ int PlayerFunctions::luaPlayerSetAccountType(lua_State* L) { return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerAddBestiaryKill(lua_State* L) { // player:addBestiaryKill(name[, amount = 1]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const auto &mtype = g_monsters().getMonsterType(getString(L, 2)); + const auto &mtype = g_monsters().getMonsterType(Lua::getString(L, 2)); if (mtype) { - g_iobestiary().addBestiaryKill(player, mtype, getNumber(L, 3, 1)); - pushBoolean(L, true); + g_iobestiary().addBestiaryKill(player, mtype, Lua::getNumber(L, 3, 1)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -330,42 +701,42 @@ int PlayerFunctions::luaPlayerAddBestiaryKill(lua_State* L) { int PlayerFunctions::luaPlayerIsMonsterBestiaryUnlocked(lua_State* L) { // player:isMonsterBestiaryUnlocked(raceId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player == nullptr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - const auto raceId = getNumber(L, 2, 0); + const auto raceId = Lua::getNumber(L, 2, 0); if (!g_monsters().getMonsterTypeByRaceId(raceId)) { - reportErrorFunc("Monster race id not exists"); - pushBoolean(L, false); + Lua::reportErrorFunc("Monster race id not exists"); + Lua::pushBoolean(L, false); return 0; } for (const uint16_t finishedRaceId : g_iobestiary().getBestiaryFinished(player)) { if (raceId == finishedRaceId) { - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } } - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 0; } int PlayerFunctions::luaPlayergetCharmMonsterType(lua_State* L) { // player:getCharmMonsterType(charmRune_t) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const charmRune_t charmid = getNumber(L, 2); + const charmRune_t charmid = Lua::getNumber(L, 2); const uint16_t raceid = player->parseRacebyCharm(charmid, false, 0); if (raceid > 0) { const auto &mtype = g_monsters().getMonsterTypeByRaceId(raceid); if (mtype) { - pushUserdata(L, mtype); - setMetatable(L, -1, "MonsterType"); + Lua::pushUserdata(L, mtype); + Lua::setMetatable(L, -1, "MonsterType"); } else { lua_pushnil(L); } @@ -380,10 +751,10 @@ int PlayerFunctions::luaPlayergetCharmMonsterType(lua_State* L) { int PlayerFunctions::luaPlayerRemovePreyStamina(lua_State* L) { // player:removePreyStamina(amount) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - g_ioprey().checkPlayerPreys(player, getNumber(L, 2, 1)); - pushBoolean(L, true); + g_ioprey().checkPlayerPreys(player, Lua::getNumber(L, 2, 1)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -392,9 +763,9 @@ int PlayerFunctions::luaPlayerRemovePreyStamina(lua_State* L) { int PlayerFunctions::luaPlayerAddPreyCards(lua_State* L) { // player:addPreyCards(amount) - if (const auto &player = getUserdataShared(L, 1)) { - player->addPreyCards(getNumber(L, 2, 0)); - pushBoolean(L, true); + if (const auto &player = Lua::getUserdataShared(L, 1)) { + player->addPreyCards(Lua::getNumber(L, 2, 0)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -403,7 +774,7 @@ int PlayerFunctions::luaPlayerAddPreyCards(lua_State* L) { int PlayerFunctions::luaPlayerGetPreyCards(lua_State* L) { // player:getPreyCards() - if (const auto &player = getUserdataShared(L, 1)) { + if (const auto &player = Lua::getUserdataShared(L, 1)) { lua_pushnumber(L, static_cast(player->getPreyCards())); } else { lua_pushnil(L); @@ -413,8 +784,8 @@ int PlayerFunctions::luaPlayerGetPreyCards(lua_State* L) { int PlayerFunctions::luaPlayerGetPreyExperiencePercentage(lua_State* L) { // player:getPreyExperiencePercentage(raceId) - if (const auto &player = getUserdataShared(L, 1)) { - if (const std::unique_ptr &slot = player->getPreyWithMonster(getNumber(L, 2, 0)); + if (const auto &player = Lua::getUserdataShared(L, 1)) { + if (const std::unique_ptr &slot = player->getPreyWithMonster(Lua::getNumber(L, 2, 0)); slot && slot->isOccupied() && slot->bonus == PreyBonus_Experience && slot->bonusTimeLeft > 0) { lua_pushnumber(L, static_cast(100 + slot->bonusPercentage)); } else { @@ -428,8 +799,8 @@ int PlayerFunctions::luaPlayerGetPreyExperiencePercentage(lua_State* L) { int PlayerFunctions::luaPlayerRemoveTaskHuntingPoints(lua_State* L) { // player:removeTaskHuntingPoints(amount) - if (const auto &player = getUserdataShared(L, 1)) { - pushBoolean(L, player->useTaskHuntingPoints(getNumber(L, 2, 0))); + if (const auto &player = Lua::getUserdataShared(L, 1)) { + Lua::pushBoolean(L, player->useTaskHuntingPoints(Lua::getNumber(L, 2, 0))); } else { lua_pushnil(L); } @@ -438,10 +809,10 @@ int PlayerFunctions::luaPlayerRemoveTaskHuntingPoints(lua_State* L) { int PlayerFunctions::luaPlayerGetTaskHuntingPoints(lua_State* L) { // player:getTaskHuntingPoints() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player == nullptr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -451,9 +822,9 @@ int PlayerFunctions::luaPlayerGetTaskHuntingPoints(lua_State* L) { int PlayerFunctions::luaPlayerAddTaskHuntingPoints(lua_State* L) { // player:addTaskHuntingPoints(amount) - if (const auto &player = getUserdataShared(L, 1)) { - const auto points = getNumber(L, 2); - player->addTaskHuntingPoints(getNumber(L, 2)); + if (const auto &player = Lua::getUserdataShared(L, 1)) { + const auto points = Lua::getNumber(L, 2); + player->addTaskHuntingPoints(Lua::getNumber(L, 2)); lua_pushnumber(L, static_cast(points)); } else { lua_pushnil(L); @@ -463,8 +834,8 @@ int PlayerFunctions::luaPlayerAddTaskHuntingPoints(lua_State* L) { int PlayerFunctions::luaPlayerGetPreyLootPercentage(lua_State* L) { // player:getPreyLootPercentage(raceid) - if (const auto &player = getUserdataShared(L, 1)) { - if (const std::unique_ptr &slot = player->getPreyWithMonster(getNumber(L, 2, 0)); + if (const auto &player = Lua::getUserdataShared(L, 1)) { + if (const std::unique_ptr &slot = player->getPreyWithMonster(Lua::getNumber(L, 2, 0)); slot && slot->isOccupied() && slot->bonus == PreyBonus_Loot) { lua_pushnumber(L, slot->bonusPercentage); } else { @@ -478,12 +849,12 @@ int PlayerFunctions::luaPlayerGetPreyLootPercentage(lua_State* L) { int PlayerFunctions::luaPlayerisMonsterPrey(lua_State* L) { // player:isMonsterPrey(raceid) - if (const auto &player = getUserdataShared(L, 1)) { - if (const std::unique_ptr &slot = player->getPreyWithMonster(getNumber(L, 2, 0)); + if (const auto &player = Lua::getUserdataShared(L, 1)) { + if (const std::unique_ptr &slot = player->getPreyWithMonster(Lua::getNumber(L, 2, 0)); slot && slot->isOccupied()) { - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } } else { lua_pushnil(L); @@ -493,14 +864,14 @@ int PlayerFunctions::luaPlayerisMonsterPrey(lua_State* L) { int PlayerFunctions::luaPlayerPreyThirdSlot(lua_State* L) { // get: player:preyThirdSlot() set: player:preyThirdSlot(bool) - if (const auto &player = getUserdataShared(L, 1); + if (const auto &player = Lua::getUserdataShared(L, 1); const auto &slot = player->getPreySlotById(PreySlot_Three)) { if (!slot) { lua_pushnil(L); } else if (lua_gettop(L) == 1) { - pushBoolean(L, slot->state != PreyDataState_Locked); + Lua::pushBoolean(L, slot->state != PreyDataState_Locked); } else { - if (getBoolean(L, 2, false)) { + if (Lua::getBoolean(L, 2, false)) { slot->eraseBonus(); slot->state = PreyDataState_Selection; slot->reloadMonsterGrid(player->getPreyBlackList(), player->getLevel()); @@ -509,7 +880,7 @@ int PlayerFunctions::luaPlayerPreyThirdSlot(lua_State* L) { slot->state = PreyDataState_Locked; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -520,12 +891,12 @@ int PlayerFunctions::luaPlayerPreyThirdSlot(lua_State* L) { int PlayerFunctions::luaPlayerTaskThirdSlot(lua_State* L) { // get: player:taskHuntingThirdSlot() set: player:taskHuntingThirdSlot(bool) - if (const auto &player = getUserdataShared(L, 1); + if (const auto &player = Lua::getUserdataShared(L, 1); const auto &slot = player->getTaskHuntingSlotById(PreySlot_Three)) { if (lua_gettop(L) == 1) { - pushBoolean(L, slot->state != PreyTaskDataState_Locked); + Lua::pushBoolean(L, slot->state != PreyTaskDataState_Locked); } else { - if (getBoolean(L, 2, false)) { + if (Lua::getBoolean(L, 2, false)) { slot->eraseTask(); slot->reloadReward(); slot->state = PreyTaskDataState_Selection; @@ -535,7 +906,7 @@ int PlayerFunctions::luaPlayerTaskThirdSlot(lua_State* L) { slot->state = PreyTaskDataState_Locked; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -545,13 +916,13 @@ int PlayerFunctions::luaPlayerTaskThirdSlot(lua_State* L) { int PlayerFunctions::luaPlayercharmExpansion(lua_State* L) { // get: player:charmExpansion() set: player:charmExpansion(bool) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { if (lua_gettop(L) == 1) { - pushBoolean(L, player->hasCharmExpansion()); + Lua::pushBoolean(L, player->hasCharmExpansion()); } else { - player->setCharmExpansion(getBoolean(L, 2, false)); - pushBoolean(L, true); + player->setCharmExpansion(Lua::getBoolean(L, 2, false)); + Lua::pushBoolean(L, true); } } else { lua_pushnil(L); @@ -561,7 +932,7 @@ int PlayerFunctions::luaPlayercharmExpansion(lua_State* L) { int PlayerFunctions::luaPlayerGetCapacity(lua_State* L) { // player:getCapacity() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getCapacity()); } else { @@ -572,11 +943,11 @@ int PlayerFunctions::luaPlayerGetCapacity(lua_State* L) { int PlayerFunctions::luaPlayerSetCapacity(lua_State* L) { // player:setCapacity(capacity) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - player->capacity = getNumber(L, 2); + player->capacity = Lua::getNumber(L, 2); player->sendStats(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -585,11 +956,11 @@ int PlayerFunctions::luaPlayerSetCapacity(lua_State* L) { int PlayerFunctions::luaPlayerSetTraining(lua_State* L) { // player:setTraining(value) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const bool value = getBoolean(L, 2, false); + const bool value = Lua::getBoolean(L, 2, false); player->setTraining(value); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -598,10 +969,10 @@ int PlayerFunctions::luaPlayerSetTraining(lua_State* L) { int PlayerFunctions::luaPlayerGetIsTraining(lua_State* L) { // player:isTraining() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - pushBoolean(L, false); - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } @@ -611,7 +982,7 @@ int PlayerFunctions::luaPlayerGetIsTraining(lua_State* L) { int PlayerFunctions::luaPlayerGetFreeCapacity(lua_State* L) { // player:getFreeCapacity() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getFreeCapacity()); } else { @@ -622,7 +993,7 @@ int PlayerFunctions::luaPlayerGetFreeCapacity(lua_State* L) { int PlayerFunctions::luaPlayerGetKills(lua_State* L) { // player:getKills() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -636,7 +1007,7 @@ int PlayerFunctions::luaPlayerGetKills(lua_State* L) { lua_rawseti(L, -2, 1); lua_pushnumber(L, kill.time); lua_rawseti(L, -2, 2); - pushBoolean(L, kill.unavenged); + Lua::pushBoolean(L, kill.unavenged); lua_rawseti(L, -2, 3); lua_rawseti(L, -2, ++idx); } @@ -646,7 +1017,7 @@ int PlayerFunctions::luaPlayerGetKills(lua_State* L) { int PlayerFunctions::luaPlayerSetKills(lua_State* L) { // player:setKills(kills) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -662,52 +1033,52 @@ int PlayerFunctions::luaPlayerSetKills(lua_State* L) { lua_rawgeti(L, -1, 1); // push target lua_rawgeti(L, -2, 2); // push time lua_rawgeti(L, -3, 3); // push unavenged - newKills.emplace_back(luaL_checknumber(L, -3), luaL_checknumber(L, -2), getBoolean(L, -1)); + newKills.emplace_back(luaL_checknumber(L, -3), luaL_checknumber(L, -2), Lua::getBoolean(L, -1)); lua_pop(L, 4); } player->unjustifiedKills = std::move(newKills); player->sendUnjustifiedPoints(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetReward(lua_State* L) { // player:getReward(rewardId[, autoCreate = false]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const uint64_t rewardId = getNumber(L, 2); - const bool autoCreate = getBoolean(L, 3, false); + const uint64_t rewardId = Lua::getNumber(L, 2); + const bool autoCreate = Lua::getBoolean(L, 3, false); if (const auto &reward = player->getReward(rewardId, autoCreate)) { - pushUserdata(L, reward); - setItemMetatable(L, -1, reward); + Lua::pushUserdata(L, reward); + Lua::setItemMetatable(L, -1, reward); } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } int PlayerFunctions::luaPlayerRemoveReward(lua_State* L) { // player:removeReward(rewardId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const uint32_t rewardId = getNumber(L, 2); + const uint32_t rewardId = Lua::getNumber(L, 2); player->removeReward(rewardId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetRewardList(lua_State* L) { // player:getRewardList() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -727,10 +1098,10 @@ int PlayerFunctions::luaPlayerGetRewardList(lua_State* L) { int PlayerFunctions::luaPlayerSetDailyReward(lua_State* L) { // player:setDailyReward(value) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - player->setDailyReward(getNumber(L, 2)); - pushBoolean(L, true); + player->setDailyReward(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -739,27 +1110,27 @@ int PlayerFunctions::luaPlayerSetDailyReward(lua_State* L) { int PlayerFunctions::luaPlayerGetDepotLocker(lua_State* L) { // player:getDepotLocker(depotId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const uint32_t depotId = getNumber(L, 2); + const uint32_t depotId = Lua::getNumber(L, 2); const auto &depotLocker = player->getDepotLocker(depotId); if (depotLocker) { depotLocker->setParent(player); - pushUserdata(L, depotLocker); - setItemMetatable(L, -1, depotLocker); + Lua::pushUserdata(L, depotLocker); + Lua::setItemMetatable(L, -1, depotLocker); } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } int PlayerFunctions::luaPlayerGetStashCounter(lua_State* L) { // player:getStashCount() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { const uint16_t sizeStash = getStashSize(player->getStashItems()); lua_pushnumber(L, sizeStash); @@ -771,28 +1142,28 @@ int PlayerFunctions::luaPlayerGetStashCounter(lua_State* L) { int PlayerFunctions::luaPlayerGetDepotChest(lua_State* L) { // player:getDepotChest(depotId[, autoCreate = false]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const uint32_t depotId = getNumber(L, 2); - const bool autoCreate = getBoolean(L, 3, false); + const uint32_t depotId = Lua::getNumber(L, 2); + const bool autoCreate = Lua::getBoolean(L, 3, false); const auto &depotChest = player->getDepotChest(depotId, autoCreate); if (depotChest) { player->setLastDepotId(depotId); - pushUserdata(L, depotChest); - setItemMetatable(L, -1, depotChest); + Lua::pushUserdata(L, depotChest); + Lua::setItemMetatable(L, -1, depotChest); } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } int PlayerFunctions::luaPlayerGetInbox(lua_State* L) { // player:getInbox() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -800,17 +1171,17 @@ int PlayerFunctions::luaPlayerGetInbox(lua_State* L) { const auto &inbox = player->getInbox(); if (inbox) { - pushUserdata(L, inbox); - setItemMetatable(L, -1, inbox); + Lua::pushUserdata(L, inbox); + Lua::setItemMetatable(L, -1, inbox); } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } int PlayerFunctions::luaPlayerGetSkullTime(lua_State* L) { // player:getSkullTime() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getSkullTicks()); } else { @@ -821,10 +1192,10 @@ int PlayerFunctions::luaPlayerGetSkullTime(lua_State* L) { int PlayerFunctions::luaPlayerSetSkullTime(lua_State* L) { // player:setSkullTime(skullTime) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - player->setSkullTicks(getNumber(L, 2)); - pushBoolean(L, true); + player->setSkullTicks(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -833,7 +1204,7 @@ int PlayerFunctions::luaPlayerSetSkullTime(lua_State* L) { int PlayerFunctions::luaPlayerGetDeathPenalty(lua_State* L) { // player:getDeathPenalty() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, static_cast(player->getLostPercent() * 100)); } else { @@ -844,7 +1215,7 @@ int PlayerFunctions::luaPlayerGetDeathPenalty(lua_State* L) { int PlayerFunctions::luaPlayerGetExperience(lua_State* L) { // player:getExperience() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getExperience()); } else { @@ -855,12 +1226,12 @@ int PlayerFunctions::luaPlayerGetExperience(lua_State* L) { int PlayerFunctions::luaPlayerAddExperience(lua_State* L) { // player:addExperience(experience[, sendText = false]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const int64_t experience = getNumber(L, 2); - const bool sendText = getBoolean(L, 3, false); + const int64_t experience = Lua::getNumber(L, 2); + const bool sendText = Lua::getBoolean(L, 3, false); player->addExperience(nullptr, experience, sendText); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -869,12 +1240,12 @@ int PlayerFunctions::luaPlayerAddExperience(lua_State* L) { int PlayerFunctions::luaPlayerRemoveExperience(lua_State* L) { // player:removeExperience(experience[, sendText = false]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const int64_t experience = getNumber(L, 2); - const bool sendText = getBoolean(L, 3, false); + const int64_t experience = Lua::getNumber(L, 2); + const bool sendText = Lua::getBoolean(L, 3, false); player->removeExperience(experience, sendText); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -883,7 +1254,7 @@ int PlayerFunctions::luaPlayerRemoveExperience(lua_State* L) { int PlayerFunctions::luaPlayerGetLevel(lua_State* L) { // player:getLevel() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getLevel()); } else { @@ -894,9 +1265,9 @@ int PlayerFunctions::luaPlayerGetLevel(lua_State* L) { int PlayerFunctions::luaPlayerGetMagicShieldCapacityFlat(lua_State* L) { // player:getMagicShieldCapacityFlat(useCharges) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - lua_pushnumber(L, player->getMagicShieldCapacityFlat(getBoolean(L, 2, false))); + lua_pushnumber(L, player->getMagicShieldCapacityFlat(Lua::getBoolean(L, 2, false))); } else { lua_pushnil(L); } @@ -905,9 +1276,9 @@ int PlayerFunctions::luaPlayerGetMagicShieldCapacityFlat(lua_State* L) { int PlayerFunctions::luaPlayerGetMagicShieldCapacityPercent(lua_State* L) { // player:getMagicShieldCapacityPercent(useCharges) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - lua_pushnumber(L, player->getMagicShieldCapacityPercent(getBoolean(L, 2, false))); + lua_pushnumber(L, player->getMagicShieldCapacityPercent(Lua::getBoolean(L, 2, false))); } else { lua_pushnil(L); } @@ -916,39 +1287,39 @@ int PlayerFunctions::luaPlayerGetMagicShieldCapacityPercent(lua_State* L) { int PlayerFunctions::luaPlayerSendSpellCooldown(lua_State* L) { // player:sendSpellCooldown(spellId, time) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const uint8_t spellId = getNumber(L, 2, 1); - const auto time = getNumber(L, 3, 0); + const uint8_t spellId = Lua::getNumber(L, 2, 1); + const auto time = Lua::getNumber(L, 3, 0); player->sendSpellCooldown(spellId, time); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSendSpellGroupCooldown(lua_State* L) { // player:sendSpellGroupCooldown(groupId, time) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto groupId = getNumber(L, 2, SPELLGROUP_ATTACK); - const auto time = getNumber(L, 3, 0); + const auto groupId = Lua::getNumber(L, 2, SPELLGROUP_ATTACK); + const auto time = Lua::getNumber(L, 3, 0); player->sendSpellGroupCooldown(groupId, time); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetMagicLevel(lua_State* L) { // player:getMagicLevel() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getMagicLevel()); } else { @@ -959,7 +1330,7 @@ int PlayerFunctions::luaPlayerGetMagicLevel(lua_State* L) { int PlayerFunctions::luaPlayerGetBaseMagicLevel(lua_State* L) { // player:getBaseMagicLevel() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getBaseMagicLevel()); } else { @@ -970,7 +1341,7 @@ int PlayerFunctions::luaPlayerGetBaseMagicLevel(lua_State* L) { int PlayerFunctions::luaPlayerGetMana(lua_State* L) { // player:getMana() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getMana()); } else { @@ -981,14 +1352,14 @@ int PlayerFunctions::luaPlayerGetMana(lua_State* L) { int PlayerFunctions::luaPlayerAddMana(lua_State* L) { // player:addMana(manaChange[, animationOnLoss = false]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const int32_t manaChange = getNumber(L, 2); - const bool animationOnLoss = getBoolean(L, 3, false); + const int32_t manaChange = Lua::getNumber(L, 2); + const bool animationOnLoss = Lua::getBoolean(L, 3, false); if (!animationOnLoss && manaChange < 0) { player->changeMana(manaChange); } else { @@ -997,13 +1368,13 @@ int PlayerFunctions::luaPlayerAddMana(lua_State* L) { damage.origin = ORIGIN_NONE; g_game().combatChangeMana(nullptr, player, damage); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetMaxMana(lua_State* L) { // player:getMaxMana() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getMaxMana()); } else { @@ -1014,23 +1385,23 @@ int PlayerFunctions::luaPlayerGetMaxMana(lua_State* L) { int PlayerFunctions::luaPlayerSetMaxMana(lua_State* L) { // player:setMaxMana(maxMana) - const auto &player = getPlayer(L, 1); + const auto &player = Lua::getPlayer(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - player->manaMax = getNumber(L, 2); + player->manaMax = Lua::getNumber(L, 2); player->mana = std::min(player->mana, player->manaMax); g_game().addPlayerMana(player); player->sendStats(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetManaSpent(lua_State* L) { // player:getManaSpent() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getSpentMana()); } else { @@ -1041,10 +1412,10 @@ int PlayerFunctions::luaPlayerGetManaSpent(lua_State* L) { int PlayerFunctions::luaPlayerAddManaSpent(lua_State* L) { // player:addManaSpent(amount) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - player->addManaSpent(getNumber(L, 2)); - pushBoolean(L, true); + player->addManaSpent(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1053,7 +1424,7 @@ int PlayerFunctions::luaPlayerAddManaSpent(lua_State* L) { int PlayerFunctions::luaPlayerGetBaseMaxHealth(lua_State* L) { // player:getBaseMaxHealth() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->healthMax); } else { @@ -1064,7 +1435,7 @@ int PlayerFunctions::luaPlayerGetBaseMaxHealth(lua_State* L) { int PlayerFunctions::luaPlayerGetBaseMaxMana(lua_State* L) { // player:getBaseMaxMana() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->manaMax); } else { @@ -1075,8 +1446,8 @@ int PlayerFunctions::luaPlayerGetBaseMaxMana(lua_State* L) { int PlayerFunctions::luaPlayerGetSkillLevel(lua_State* L) { // player:getSkillLevel(skillType) - const skills_t skillType = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const skills_t skillType = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (player && skillType <= SKILL_LAST) { lua_pushnumber(L, player->skills[skillType].level); } else { @@ -1087,8 +1458,8 @@ int PlayerFunctions::luaPlayerGetSkillLevel(lua_State* L) { int PlayerFunctions::luaPlayerGetEffectiveSkillLevel(lua_State* L) { // player:getEffectiveSkillLevel(skillType) - const skills_t skillType = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const skills_t skillType = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (player && skillType <= SKILL_LAST) { lua_pushnumber(L, player->getSkillLevel(skillType)); } else { @@ -1099,8 +1470,8 @@ int PlayerFunctions::luaPlayerGetEffectiveSkillLevel(lua_State* L) { int PlayerFunctions::luaPlayerGetSkillPercent(lua_State* L) { // player:getSkillPercent(skillType) - const skills_t skillType = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const skills_t skillType = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (player && skillType <= SKILL_LAST) { lua_pushnumber(L, player->skills[skillType].percent); } else { @@ -1111,8 +1482,8 @@ int PlayerFunctions::luaPlayerGetSkillPercent(lua_State* L) { int PlayerFunctions::luaPlayerGetSkillTries(lua_State* L) { // player:getSkillTries(skillType) - const skills_t skillType = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const skills_t skillType = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (player && skillType <= SKILL_LAST) { lua_pushnumber(L, player->skills[skillType].tries); } else { @@ -1123,12 +1494,12 @@ int PlayerFunctions::luaPlayerGetSkillTries(lua_State* L) { int PlayerFunctions::luaPlayerAddSkillTries(lua_State* L) { // player:addSkillTries(skillType, tries) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const skills_t skillType = getNumber(L, 2); - const uint64_t tries = getNumber(L, 3); + const skills_t skillType = Lua::getNumber(L, 2); + const uint64_t tries = Lua::getNumber(L, 3); player->addSkillAdvance(skillType, tries); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1137,14 +1508,14 @@ int PlayerFunctions::luaPlayerAddSkillTries(lua_State* L) { int PlayerFunctions::luaPlayerSetLevel(lua_State* L) { // player:setLevel(level) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint16_t level = getNumber(L, 2); + const uint16_t level = Lua::getNumber(L, 2); player->level = level; player->experience = Player::getExpForLevel(level); player->sendStats(); player->sendSkills(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1153,12 +1524,12 @@ int PlayerFunctions::luaPlayerSetLevel(lua_State* L) { int PlayerFunctions::luaPlayerSetMagicLevel(lua_State* L) { // player:setMagicLevel(level[, manaSpent]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint16_t level = getNumber(L, 2); + const uint16_t level = Lua::getNumber(L, 2); player->magLevel = level; - if (getNumber(L, 3, 0) > 0) { - const uint64_t manaSpent = getNumber(L, 3); + if (Lua::getNumber(L, 3, 0) > 0) { + const uint64_t manaSpent = Lua::getNumber(L, 3); const uint64_t nextReqMana = player->vocation->getReqMana(level + 1); player->manaSpent = manaSpent; player->magLevelPercent = Player::getPercentLevel(manaSpent, nextReqMana); @@ -1168,7 +1539,7 @@ int PlayerFunctions::luaPlayerSetMagicLevel(lua_State* L) { } player->sendStats(); player->sendSkills(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1177,13 +1548,13 @@ int PlayerFunctions::luaPlayerSetMagicLevel(lua_State* L) { int PlayerFunctions::luaPlayerSetSkillLevel(lua_State* L) { // player:setSkillLevel(skillType, level[, tries]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const skills_t skillType = getNumber(L, 2); - const uint16_t level = getNumber(L, 3); + const skills_t skillType = Lua::getNumber(L, 2); + const uint16_t level = Lua::getNumber(L, 3); player->skills[skillType].level = level; - if (getNumber(L, 4, 0) > 0) { - const uint64_t tries = getNumber(L, 4); + if (Lua::getNumber(L, 4, 0) > 0) { + const uint64_t tries = Lua::getNumber(L, 4); const uint64_t nextReqTries = player->vocation->getReqSkillTries(skillType, level + 1); player->skills[skillType].tries = tries; player->skills[skillType].percent = Player::getPercentLevel(tries, nextReqTries); @@ -1193,7 +1564,7 @@ int PlayerFunctions::luaPlayerSetSkillLevel(lua_State* L) { } player->sendStats(); player->sendSkills(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1202,12 +1573,12 @@ int PlayerFunctions::luaPlayerSetSkillLevel(lua_State* L) { int PlayerFunctions::luaPlayerAddOfflineTrainingTime(lua_State* L) { // player:addOfflineTrainingTime(time) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const int32_t time = getNumber(L, 2); + const int32_t time = Lua::getNumber(L, 2); player->addOfflineTrainingTime(time); player->sendStats(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1216,7 +1587,7 @@ int PlayerFunctions::luaPlayerAddOfflineTrainingTime(lua_State* L) { int PlayerFunctions::luaPlayerGetOfflineTrainingTime(lua_State* L) { // player:getOfflineTrainingTime() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getOfflineTrainingTime()); } else { @@ -1227,12 +1598,12 @@ int PlayerFunctions::luaPlayerGetOfflineTrainingTime(lua_State* L) { int PlayerFunctions::luaPlayerRemoveOfflineTrainingTime(lua_State* L) { // player:removeOfflineTrainingTime(time) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const int32_t time = getNumber(L, 2); + const int32_t time = Lua::getNumber(L, 2); player->removeOfflineTrainingTime(time); player->sendStats(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1241,11 +1612,11 @@ int PlayerFunctions::luaPlayerRemoveOfflineTrainingTime(lua_State* L) { int PlayerFunctions::luaPlayerAddOfflineTrainingTries(lua_State* L) { // player:addOfflineTrainingTries(skillType, tries) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const skills_t skillType = getNumber(L, 2); - const uint64_t tries = getNumber(L, 3); - pushBoolean(L, player->addOfflineTrainingTries(skillType, tries)); + const skills_t skillType = Lua::getNumber(L, 2); + const uint64_t tries = Lua::getNumber(L, 3); + Lua::pushBoolean(L, player->addOfflineTrainingTries(skillType, tries)); } else { lua_pushnil(L); } @@ -1254,7 +1625,7 @@ int PlayerFunctions::luaPlayerAddOfflineTrainingTries(lua_State* L) { int PlayerFunctions::luaPlayerGetOfflineTrainingSkill(lua_State* L) { // player:getOfflineTrainingSkill() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getOfflineTrainingSkill()); } else { @@ -1265,11 +1636,11 @@ int PlayerFunctions::luaPlayerGetOfflineTrainingSkill(lua_State* L) { int PlayerFunctions::luaPlayerSetOfflineTrainingSkill(lua_State* L) { // player:setOfflineTrainingSkill(skillId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const int8_t skillId = getNumber(L, 2); + const int8_t skillId = Lua::getNumber(L, 2); player->setOfflineTrainingSkill(skillId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1278,11 +1649,11 @@ int PlayerFunctions::luaPlayerSetOfflineTrainingSkill(lua_State* L) { int PlayerFunctions::luaPlayerOpenStash(lua_State* L) { // player:openStash(isNpc) - const auto &player = getUserdataShared(L, 1); - const bool isNpc = getBoolean(L, 2, false); + const auto &player = Lua::getUserdataShared(L, 1); + const bool isNpc = Lua::getBoolean(L, 2, false); if (player) { player->sendOpenStash(isNpc); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1292,41 +1663,41 @@ int PlayerFunctions::luaPlayerOpenStash(lua_State* L) { int PlayerFunctions::luaPlayerGetItemCount(lua_State* L) { // player:getItemCount(itemId[, subType = -1]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; } } - const auto subType = getNumber(L, 3, -1); + const auto subType = Lua::getNumber(L, 3, -1); lua_pushnumber(L, player->getItemTypeCount(itemId, subType)); return 1; } int PlayerFunctions::luaPlayerGetStashItemCount(lua_State* L) { // player:getStashItemCount(itemId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; @@ -1345,29 +1716,29 @@ int PlayerFunctions::luaPlayerGetStashItemCount(lua_State* L) { int PlayerFunctions::luaPlayerGetItemById(lua_State* L) { // player:getItemById(itemId, deepSearch[, subType = -1]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; } } - const bool deepSearch = getBoolean(L, 3); - const auto subType = getNumber(L, 4, -1); + const bool deepSearch = Lua::getBoolean(L, 3); + const auto subType = Lua::getNumber(L, 4, -1); const auto &item = g_game().findItemOfType(player, itemId, deepSearch, subType); if (item) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { lua_pushnil(L); } @@ -1376,10 +1747,10 @@ int PlayerFunctions::luaPlayerGetItemById(lua_State* L) { int PlayerFunctions::luaPlayerGetVocation(lua_State* L) { // player:getVocation() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - pushUserdata(L, player->getVocation()); - setMetatable(L, -1, "Vocation"); + Lua::pushUserdata(L, player->getVocation()); + Lua::setMetatable(L, -1, "Vocation"); } else { lua_pushnil(L); } @@ -1388,25 +1759,25 @@ int PlayerFunctions::luaPlayerGetVocation(lua_State* L) { int PlayerFunctions::luaPlayerSetVocation(lua_State* L) { // player:setVocation(id or name or userdata) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } std::shared_ptr vocation; - if (isNumber(L, 2)) { - vocation = g_vocations().getVocation(getNumber(L, 2)); - } else if (isString(L, 2)) { - vocation = g_vocations().getVocation(g_vocations().getVocationId(getString(L, 2))); - } else if (isUserdata(L, 2)) { - vocation = getUserdataShared(L, 2); + if (Lua::isNumber(L, 2)) { + vocation = g_vocations().getVocation(Lua::getNumber(L, 2)); + } else if (Lua::isString(L, 2)) { + vocation = g_vocations().getVocation(g_vocations().getVocationId(Lua::getString(L, 2))); + } else if (Lua::isUserdata(L, 2)) { + vocation = Lua::getUserdataShared(L, 2); } else { vocation = nullptr; } if (!vocation) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } @@ -1416,15 +1787,15 @@ int PlayerFunctions::luaPlayerSetVocation(lua_State* L) { player->sendBasicData(); player->wheel()->sendGiftOfLifeCooldown(); g_game().reloadCreature(player); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerIsPromoted(lua_State* L) { // player:isPromoted() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - pushBoolean(L, player->isPromoted()); + Lua::pushBoolean(L, player->isPromoted()); } else { lua_pushnil(L); } @@ -1433,7 +1804,7 @@ int PlayerFunctions::luaPlayerIsPromoted(lua_State* L) { int PlayerFunctions::luaPlayerGetSex(lua_State* L) { // player:getSex() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getSex()); } else { @@ -1444,11 +1815,11 @@ int PlayerFunctions::luaPlayerGetSex(lua_State* L) { int PlayerFunctions::luaPlayerSetSex(lua_State* L) { // player:setSex(newSex) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const PlayerSex_t newSex = getNumber(L, 2); + const PlayerSex_t newSex = Lua::getNumber(L, 2); player->setSex(newSex); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1457,7 +1828,7 @@ int PlayerFunctions::luaPlayerSetSex(lua_State* L) { int PlayerFunctions::luaPlayerGetPronoun(lua_State* L) { // player:getPronoun() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getPronoun()); } else { @@ -1468,11 +1839,11 @@ int PlayerFunctions::luaPlayerGetPronoun(lua_State* L) { int PlayerFunctions::luaPlayerSetPronoun(lua_State* L) { // player:setPronoun(newPronoun) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const PlayerPronoun_t newPronoun = getNumber(L, 2); + const PlayerPronoun_t newPronoun = Lua::getNumber(L, 2); player->setPronoun(newPronoun); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1481,10 +1852,10 @@ int PlayerFunctions::luaPlayerSetPronoun(lua_State* L) { int PlayerFunctions::luaPlayerGetTown(lua_State* L) { // player:getTown() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - pushUserdata(L, player->getTown()); - setMetatable(L, -1, "Town"); + Lua::pushUserdata(L, player->getTown()); + Lua::setMetatable(L, -1, "Town"); } else { lua_pushnil(L); } @@ -1493,16 +1864,16 @@ int PlayerFunctions::luaPlayerGetTown(lua_State* L) { int PlayerFunctions::luaPlayerSetTown(lua_State* L) { // player:setTown(town) - const auto &town = getUserdataShared(L, 2); + const auto &town = Lua::getUserdataShared(L, 2); if (!town) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { player->setTown(town); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1511,7 +1882,7 @@ int PlayerFunctions::luaPlayerSetTown(lua_State* L) { int PlayerFunctions::luaPlayerGetGuild(lua_State* L) { // player:getGuild() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -1523,29 +1894,29 @@ int PlayerFunctions::luaPlayerGetGuild(lua_State* L) { return 1; } - pushUserdata(L, guild); - setMetatable(L, -1, "Guild"); + Lua::pushUserdata(L, guild); + Lua::setMetatable(L, -1, "Guild"); return 1; } int PlayerFunctions::luaPlayerSetGuild(lua_State* L) { // player:setGuild(guild) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &guild = getUserdataShared(L, 2); + const auto &guild = Lua::getUserdataShared(L, 2); player->setGuild(guild); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetGuildLevel(lua_State* L) { // player:getGuildLevel() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player && player->getGuild()) { lua_pushnumber(L, player->getGuildRank()->level); } else { @@ -1556,8 +1927,8 @@ int PlayerFunctions::luaPlayerGetGuildLevel(lua_State* L) { int PlayerFunctions::luaPlayerSetGuildLevel(lua_State* L) { // player:setGuildLevel(level) - const uint8_t level = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const uint8_t level = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || !player->getGuild()) { lua_pushnil(L); return 1; @@ -1565,10 +1936,10 @@ int PlayerFunctions::luaPlayerSetGuildLevel(lua_State* L) { const auto &rank = player->getGuild()->getRankByLevel(level); if (!rank) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } else { player->setGuildRank(rank); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } return 1; @@ -1576,9 +1947,9 @@ int PlayerFunctions::luaPlayerSetGuildLevel(lua_State* L) { int PlayerFunctions::luaPlayerGetGuildNick(lua_State* L) { // player:getGuildNick() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - pushString(L, player->getGuildNick()); + Lua::pushString(L, player->getGuildNick()); } else { lua_pushnil(L); } @@ -1587,11 +1958,11 @@ int PlayerFunctions::luaPlayerGetGuildNick(lua_State* L) { int PlayerFunctions::luaPlayerSetGuildNick(lua_State* L) { // player:setGuildNick(nick) - const std::string &nick = getString(L, 2); - const auto &player = getUserdataShared(L, 1); + const std::string &nick = Lua::getString(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { player->setGuildNick(nick); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1600,10 +1971,10 @@ int PlayerFunctions::luaPlayerSetGuildNick(lua_State* L) { int PlayerFunctions::luaPlayerGetGroup(lua_State* L) { // player:getGroup() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - pushUserdata(L, player->getGroup()); - setMetatable(L, -1, "Group"); + Lua::pushUserdata(L, player->getGroup()); + Lua::setMetatable(L, -1, "Group"); } else { lua_pushnil(L); } @@ -1612,16 +1983,16 @@ int PlayerFunctions::luaPlayerGetGroup(lua_State* L) { int PlayerFunctions::luaPlayerSetGroup(lua_State* L) { // player:setGroup(group) - const auto &group = getUserdataShared(L, 2); + const auto &group = Lua::getUserdataShared(L, 2); if (!group) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { player->setGroup(group); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1630,13 +2001,13 @@ int PlayerFunctions::luaPlayerSetGroup(lua_State* L) { int PlayerFunctions::luaPlayerSetSpecialContainersAvailable(lua_State* L) { // player:setSpecialContainersAvailable(stashMenu, marketMenu, depotSearchMenu) - const bool supplyStashMenu = getBoolean(L, 2, false); - const bool marketMenu = getBoolean(L, 3, false); - const bool depotSearchMenu = getBoolean(L, 4, false); - const auto &player = getUserdataShared(L, 1); + const bool supplyStashMenu = Lua::getBoolean(L, 2, false); + const bool marketMenu = Lua::getBoolean(L, 3, false); + const bool depotSearchMenu = Lua::getBoolean(L, 4, false); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { player->setSpecialMenuAvailable(supplyStashMenu, marketMenu, depotSearchMenu); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1645,7 +2016,7 @@ int PlayerFunctions::luaPlayerSetSpecialContainersAvailable(lua_State* L) { int PlayerFunctions::luaPlayerGetStamina(lua_State* L) { // player:getStamina() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getStaminaMinutes()); } else { @@ -1656,8 +2027,8 @@ int PlayerFunctions::luaPlayerGetStamina(lua_State* L) { int PlayerFunctions::luaPlayerSetStamina(lua_State* L) { // player:setStamina(stamina) - const uint16_t stamina = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const uint16_t stamina = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { player->staminaMinutes = std::min(2520, stamina); player->sendStats(); @@ -1669,7 +2040,7 @@ int PlayerFunctions::luaPlayerSetStamina(lua_State* L) { int PlayerFunctions::luaPlayerGetSoul(lua_State* L) { // player:getSoul() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getSoul()); } else { @@ -1680,11 +2051,11 @@ int PlayerFunctions::luaPlayerGetSoul(lua_State* L) { int PlayerFunctions::luaPlayerAddSoul(lua_State* L) { // player:addSoul(soulChange) - const int32_t soulChange = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const int32_t soulChange = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { player->changeSoul(soulChange); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1693,7 +2064,7 @@ int PlayerFunctions::luaPlayerAddSoul(lua_State* L) { int PlayerFunctions::luaPlayerGetMaxSoul(lua_State* L) { // player:getMaxSoul() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player && player->vocation) { lua_pushnumber(L, player->vocation->getSoulMax()); } else { @@ -1704,7 +2075,7 @@ int PlayerFunctions::luaPlayerGetMaxSoul(lua_State* L) { int PlayerFunctions::luaPlayerGetBankBalance(lua_State* L) { // player:getBankBalance() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getBankBalance()); } else { @@ -1715,51 +2086,51 @@ int PlayerFunctions::luaPlayerGetBankBalance(lua_State* L) { int PlayerFunctions::luaPlayerSetBankBalance(lua_State* L) { // player:setBankBalance(bankBalance) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - player->setBankBalance(getNumber(L, 2)); - pushBoolean(L, true); + player->setBankBalance(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetStorageValue(lua_State* L) { // player:getStorageValue(key) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const uint32_t key = getNumber(L, 2); + const uint32_t key = Lua::getNumber(L, 2); lua_pushnumber(L, player->getStorageValue(key)); return 1; } int PlayerFunctions::luaPlayerSetStorageValue(lua_State* L) { // player:setStorageValue(key, value) - const int32_t value = getNumber(L, 3); - const uint32_t key = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const int32_t value = Lua::getNumber(L, 3); + const uint32_t key = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (IS_IN_KEYRANGE(key, RESERVED_RANGE)) { std::ostringstream ss; ss << "Accessing reserved range: " << key; - reportErrorFunc(ss.str()); - pushBoolean(L, false); + Lua::reportErrorFunc(ss.str()); + Lua::pushBoolean(L, false); return 1; } if (key == 0) { - reportErrorFunc("Storage key is nil"); + Lua::reportErrorFunc("Storage key is nil"); return 1; } if (player) { player->addStorageValue(key, value); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -1768,58 +2139,58 @@ int PlayerFunctions::luaPlayerSetStorageValue(lua_State* L) { int PlayerFunctions::luaPlayerGetStorageValueByName(lua_State* L) { // player:getStorageValueByName(name) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } g_logger().warn("The function 'player:getStorageValueByName' is deprecated and will be removed in future versions, please use KV system"); - const auto name = getString(L, 2); + const auto name = Lua::getString(L, 2); lua_pushnumber(L, player->getStorageValueByName(name)); return 1; } int PlayerFunctions::luaPlayerSetStorageValueByName(lua_State* L) { // player:setStorageValueByName(storageName, value) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } g_logger().warn("The function 'player:setStorageValueByName' is deprecated and will be removed in future versions, please use KV system"); - const auto storageName = getString(L, 2); - const int32_t value = getNumber(L, 3); + const auto storageName = Lua::getString(L, 2); + const int32_t value = Lua::getNumber(L, 3); player->addStorageValueByName(storageName, value); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerAddItem(lua_State* L) { // player:addItem(itemId, count = 1, canDropOnMap = true, subType = 1, slot = CONST_SLOT_WHEREEVER, tier = 0) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; } } - const auto count = getNumber(L, 3, 1); - auto subType = getNumber(L, 5, 1); + const auto count = Lua::getNumber(L, 3, 1); + auto subType = Lua::getNumber(L, 5, 1); const ItemType &it = Item::items[itemId]; @@ -1845,9 +2216,9 @@ int PlayerFunctions::luaPlayerAddItem(lua_State* L) { return 1; } - const bool canDropOnMap = getBoolean(L, 4, true); - const auto slot = getNumber(L, 6, CONST_SLOT_WHEREEVER); - const auto tier = getNumber(L, 7, 0); + const bool canDropOnMap = Lua::getBoolean(L, 4, true); + const auto slot = Lua::getNumber(L, 6, CONST_SLOT_WHEREEVER); + const auto tier = Lua::getNumber(L, 7, 0); for (int32_t i = 1; i <= itemCount; ++i) { int32_t stackCount = subType; if (it.stackable) { @@ -1879,12 +2250,12 @@ int PlayerFunctions::luaPlayerAddItem(lua_State* L) { if (hasTable) { lua_pushnumber(L, i); - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); lua_settable(L, -3); } else { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } } return 1; @@ -1893,33 +2264,33 @@ int PlayerFunctions::luaPlayerAddItem(lua_State* L) { int PlayerFunctions::luaPlayerAddItemEx(lua_State* L) { // player:addItemEx(item[, canDropOnMap = false[, index = INDEX_WHEREEVER[, flags = 0]]]) // player:addItemEx(item[, canDropOnMap = true[, slot = CONST_SLOT_WHEREEVER]]) - const auto &item = getUserdataShared(L, 2); + const auto &item = Lua::getUserdataShared(L, 2); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } if (item->getParent() != VirtualCylinder::virtualCylinder) { - reportErrorFunc("Item already has a parent"); - pushBoolean(L, false); + Lua::reportErrorFunc("Item already has a parent"); + Lua::pushBoolean(L, false); return 1; } - const bool canDropOnMap = getBoolean(L, 3, false); + const bool canDropOnMap = Lua::getBoolean(L, 3, false); ReturnValue returnValue; if (canDropOnMap) { - const auto slot = getNumber(L, 4, CONST_SLOT_WHEREEVER); + const auto slot = Lua::getNumber(L, 4, CONST_SLOT_WHEREEVER); returnValue = g_game().internalPlayerAddItem(player, item, true, slot); } else { - const auto index = getNumber(L, 4, INDEX_WHEREEVER); - const auto flags = getNumber(L, 5, 0); + const auto index = Lua::getNumber(L, 4, INDEX_WHEREEVER); + const auto flags = Lua::getNumber(L, 5, 0); returnValue = g_game().internalAddItem(player, item, index, flags); } @@ -1932,33 +2303,33 @@ int PlayerFunctions::luaPlayerAddItemEx(lua_State* L) { int PlayerFunctions::luaPlayerAddItemStash(lua_State* L) { // player:addItemStash(itemId, count = 1) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto itemId = getNumber(L, 2); - const auto count = getNumber(L, 3, 1); + const auto itemId = Lua::getNumber(L, 2); + const auto count = Lua::getNumber(L, 3, 1); player->addItemOnStash(itemId, count); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerRemoveStashItem(lua_State* L) { // player:removeStashItem(itemId, count) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; @@ -1971,78 +2342,78 @@ int PlayerFunctions::luaPlayerRemoveStashItem(lua_State* L) { return 1; } - const uint32_t count = getNumber(L, 3); - pushBoolean(L, player->withdrawItem(itemType.id, count)); + const uint32_t count = Lua::getNumber(L, 3); + Lua::pushBoolean(L, player->withdrawItem(itemType.id, count)); return 1; } int PlayerFunctions::luaPlayerRemoveItem(lua_State* L) { // player:removeItem(itemId, count[, subType = -1[, ignoreEquipped = false]]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; } } - const uint32_t count = getNumber(L, 3); - const auto subType = getNumber(L, 4, -1); - const bool ignoreEquipped = getBoolean(L, 5, false); - pushBoolean(L, player->removeItemOfType(itemId, count, subType, ignoreEquipped)); + const uint32_t count = Lua::getNumber(L, 3); + const auto subType = Lua::getNumber(L, 4, -1); + const bool ignoreEquipped = Lua::getBoolean(L, 5, false); + Lua::pushBoolean(L, player->removeItemOfType(itemId, count, subType, ignoreEquipped)); return 1; } int PlayerFunctions::luaPlayerSendContainer(lua_State* L) { // player:sendContainer(container) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &container = getUserdataShared(L, 2); + const auto &container = Lua::getUserdataShared(L, 2); if (!container) { lua_pushnil(L); return 1; } player->sendContainer(static_cast(container->getID()), container, container->hasParent(), static_cast(container->getFirstIndex())); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSendUpdateContainer(lua_State* L) { // player:sendUpdateContainer(container) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &container = getUserdataShared(L, 2); + const auto &container = Lua::getUserdataShared(L, 2); if (!container) { - reportErrorFunc("Container is nullptr"); + Lua::reportErrorFunc("Container is nullptr"); return 1; } player->onSendContainer(container); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetMoney(lua_State* L) { // player:getMoney() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getMoney()); } else { @@ -2053,11 +2424,11 @@ int PlayerFunctions::luaPlayerGetMoney(lua_State* L) { int PlayerFunctions::luaPlayerAddMoney(lua_State* L) { // player:addMoney(money) - const uint64_t money = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const uint64_t money = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { g_game().addMoney(player, money); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2066,12 +2437,12 @@ int PlayerFunctions::luaPlayerAddMoney(lua_State* L) { int PlayerFunctions::luaPlayerRemoveMoney(lua_State* L) { // player:removeMoney(money[, flags = 0[, useBank = true]]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint64_t money = getNumber(L, 2); - const auto flags = getNumber(L, 3, 0); - const bool useBank = getBoolean(L, 4, true); - pushBoolean(L, g_game().removeMoney(player, money, flags, useBank)); + const uint64_t money = Lua::getNumber(L, 2); + const auto flags = Lua::getNumber(L, 3, 0); + const bool useBank = Lua::getBoolean(L, 4, true); + Lua::pushBoolean(L, g_game().removeMoney(player, money, flags, useBank)); } else { lua_pushnil(L); } @@ -2080,40 +2451,40 @@ int PlayerFunctions::luaPlayerRemoveMoney(lua_State* L) { int PlayerFunctions::luaPlayerShowTextDialog(lua_State* L) { // player:showTextDialog(id or name or userdata[, text[, canWrite[, length]]]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - auto length = getNumber(L, 5, -1); - const bool canWrite = getBoolean(L, 4, false); + auto length = Lua::getNumber(L, 5, -1); + const bool canWrite = Lua::getBoolean(L, 4, false); std::string text; const int parameters = lua_gettop(L); if (parameters >= 3) { - text = getString(L, 3); + text = Lua::getString(L, 3); } std::shared_ptr item; - if (isNumber(L, 2)) { - item = Item::CreateItem(getNumber(L, 2)); - } else if (isString(L, 2)) { - item = Item::CreateItem(Item::items.getItemIdByName(getString(L, 2))); - } else if (isUserdata(L, 2)) { - if (getUserdataType(L, 2) != LuaData_t::Item) { - pushBoolean(L, false); + if (Lua::isNumber(L, 2)) { + item = Item::CreateItem(Lua::getNumber(L, 2)); + } else if (Lua::isString(L, 2)) { + item = Item::CreateItem(Item::items.getItemIdByName(Lua::getString(L, 2))); + } else if (Lua::isUserdata(L, 2)) { + if (Lua::getUserdataType(L, 2) != LuaData_t::Item) { + Lua::pushBoolean(L, false); return 1; } - item = getUserdataShared(L, 2); + item = Lua::getUserdataShared(L, 2); } else { item = nullptr; } if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -2129,7 +2500,7 @@ int PlayerFunctions::luaPlayerShowTextDialog(lua_State* L) { item->setParent(player); player->setWriteItem(item, length); player->sendTextWindow(item, length, canWrite); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -2137,7 +2508,7 @@ int PlayerFunctions::luaPlayerSendTextMessage(lua_State* L) { // player:sendTextMessage(type, text[, position, primaryValue = 0, primaryColor = TEXTCOLOR_NONE[, secondaryValue = 0, secondaryColor = TEXTCOLOR_NONE]]) // player:sendTextMessage(type, text, channelId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -2145,91 +2516,91 @@ int PlayerFunctions::luaPlayerSendTextMessage(lua_State* L) { const int parameters = lua_gettop(L); - TextMessage message(getNumber(L, 2), getString(L, 3)); + TextMessage message(Lua::getNumber(L, 2), Lua::getString(L, 3)); if (parameters == 4) { - const uint16_t channelId = getNumber(L, 4); + const uint16_t channelId = Lua::getNumber(L, 4); const auto &channel = g_chat().getChannel(player, channelId); if (!channel || !channel->hasUser(player)) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } message.channelId = channelId; } else { if (parameters >= 6) { - message.position = getPosition(L, 4); - message.primary.value = getNumber(L, 5); - message.primary.color = getNumber(L, 6); + message.position = Lua::getPosition(L, 4); + message.primary.value = Lua::getNumber(L, 5); + message.primary.color = Lua::getNumber(L, 6); } if (parameters >= 8) { - message.secondary.value = getNumber(L, 7); - message.secondary.color = getNumber(L, 8); + message.secondary.value = Lua::getNumber(L, 7); + message.secondary.color = Lua::getNumber(L, 8); } } player->sendTextMessage(message); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSendChannelMessage(lua_State* L) { // player:sendChannelMessage(author, text, type, channelId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const uint16_t channelId = getNumber(L, 5); - const SpeakClasses type = getNumber(L, 4); - const std::string &text = getString(L, 3); - const std::string &author = getString(L, 2); + const uint16_t channelId = Lua::getNumber(L, 5); + const SpeakClasses type = Lua::getNumber(L, 4); + const std::string &text = Lua::getString(L, 3); + const std::string &author = Lua::getString(L, 2); player->sendChannelMessage(author, text, type, channelId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSendPrivateMessage(lua_State* L) { // player:sendPrivateMessage(speaker, text[, type]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &speaker = getUserdataShared(L, 2); - const std::string &text = getString(L, 3); - const auto type = getNumber(L, 4, TALKTYPE_PRIVATE_FROM); + const auto &speaker = Lua::getUserdataShared(L, 2); + const std::string &text = Lua::getString(L, 3); + const auto type = Lua::getNumber(L, 4, TALKTYPE_PRIVATE_FROM); player->sendPrivateMessage(speaker, type, text); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerChannelSay(lua_State* L) { // player:channelSay(speaker, type, text, channelId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &speaker = getCreature(L, 2); - const SpeakClasses type = getNumber(L, 3); - const std::string &text = getString(L, 4); - const uint16_t channelId = getNumber(L, 5); + const auto &speaker = Lua::getCreature(L, 2); + const SpeakClasses type = Lua::getNumber(L, 3); + const std::string &text = Lua::getString(L, 4); + const uint16_t channelId = Lua::getNumber(L, 5); player->sendToChannel(speaker, type, text, channelId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerOpenChannel(lua_State* L) { // player:openChannel(channelId) - const uint16_t channelId = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const uint16_t channelId = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { g_game().playerOpenChannel(player->getID(), channelId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2238,13 +2609,13 @@ int PlayerFunctions::luaPlayerOpenChannel(lua_State* L) { int PlayerFunctions::luaPlayerGetSlotItem(lua_State* L) { // player:getSlotItem(slot) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const uint32_t slot = getNumber(L, 2); + const uint32_t slot = Lua::getNumber(L, 2); const auto &thing = player->getThing(slot); if (!thing) { lua_pushnil(L); @@ -2253,8 +2624,8 @@ int PlayerFunctions::luaPlayerGetSlotItem(lua_State* L) { const auto &item = thing->getItem(); if (item) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { lua_pushnil(L); } @@ -2263,7 +2634,7 @@ int PlayerFunctions::luaPlayerGetSlotItem(lua_State* L) { int PlayerFunctions::luaPlayerGetParty(lua_State* L) { // player:getParty() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -2271,8 +2642,8 @@ int PlayerFunctions::luaPlayerGetParty(lua_State* L) { const auto &party = player->getParty(); if (party) { - pushUserdata(L, party); - setMetatable(L, -1, "Party"); + Lua::pushUserdata(L, party); + Lua::setMetatable(L, -1, "Party"); } else { lua_pushnil(L); } @@ -2281,23 +2652,23 @@ int PlayerFunctions::luaPlayerGetParty(lua_State* L) { int PlayerFunctions::luaPlayerAddOutfit(lua_State* L) { // player:addOutfit(lookType or name, addon = 0) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - auto addon = getNumber(L, 3, 0); + auto addon = Lua::getNumber(L, 3, 0); if (lua_isnumber(L, 2)) { - player->addOutfit(getNumber(L, 2), addon); + player->addOutfit(Lua::getNumber(L, 2), addon); } else if (lua_isstring(L, 2)) { - const std::string &outfitName = getString(L, 2); + const std::string &outfitName = Lua::getString(L, 2); const auto &outfit = Outfits::getInstance().getOutfitByName(player->getSex(), outfitName); if (!outfit) { - reportErrorFunc("Outfit not found"); + Lua::reportErrorFunc("Outfit not found"); return 1; } player->addOutfit(outfit->lookType, addon); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2306,12 +2677,12 @@ int PlayerFunctions::luaPlayerAddOutfit(lua_State* L) { int PlayerFunctions::luaPlayerAddOutfitAddon(lua_State* L) { // player:addOutfitAddon(lookType, addon) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint16_t lookType = getNumber(L, 2); - const uint8_t addon = getNumber(L, 3); + const uint16_t lookType = Lua::getNumber(L, 2); + const uint8_t addon = Lua::getNumber(L, 3); player->addOutfit(lookType, addon); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2320,10 +2691,10 @@ int PlayerFunctions::luaPlayerAddOutfitAddon(lua_State* L) { int PlayerFunctions::luaPlayerRemoveOutfit(lua_State* L) { // player:removeOutfit(lookType) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint16_t lookType = getNumber(L, 2); - pushBoolean(L, player->removeOutfit(lookType)); + const uint16_t lookType = Lua::getNumber(L, 2); + Lua::pushBoolean(L, player->removeOutfit(lookType)); } else { lua_pushnil(L); } @@ -2332,11 +2703,11 @@ int PlayerFunctions::luaPlayerRemoveOutfit(lua_State* L) { int PlayerFunctions::luaPlayerRemoveOutfitAddon(lua_State* L) { // player:removeOutfitAddon(lookType, addon) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint16_t lookType = getNumber(L, 2); - const uint8_t addon = getNumber(L, 3); - pushBoolean(L, player->removeOutfitAddon(lookType, addon)); + const uint16_t lookType = Lua::getNumber(L, 2); + const uint8_t addon = Lua::getNumber(L, 3); + Lua::pushBoolean(L, player->removeOutfitAddon(lookType, addon)); } else { lua_pushnil(L); } @@ -2345,11 +2716,11 @@ int PlayerFunctions::luaPlayerRemoveOutfitAddon(lua_State* L) { int PlayerFunctions::luaPlayerHasOutfit(lua_State* L) { // player:hasOutfit(lookType[, addon = 0]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint16_t lookType = getNumber(L, 2); - const auto addon = getNumber(L, 3, 0); - pushBoolean(L, player->canWear(lookType, addon)); + const uint16_t lookType = Lua::getNumber(L, 2); + const auto addon = Lua::getNumber(L, 3, 0); + Lua::pushBoolean(L, player->canWear(lookType, addon)); } else { lua_pushnil(L); } @@ -2358,10 +2729,10 @@ int PlayerFunctions::luaPlayerHasOutfit(lua_State* L) { int PlayerFunctions::luaPlayerSendOutfitWindow(lua_State* L) { // player:sendOutfitWindow() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { player->sendOutfitWindow(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2370,67 +2741,67 @@ int PlayerFunctions::luaPlayerSendOutfitWindow(lua_State* L) { int PlayerFunctions::luaPlayerAddMount(lua_State* L) { // player:addMount(mountId or mountName) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } uint8_t mountId; - if (isNumber(L, 2)) { - mountId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + mountId = Lua::getNumber(L, 2); } else { - const auto &mount = g_game().mounts->getMountByName(getString(L, 2)); + const auto &mount = g_game().mounts->getMountByName(Lua::getString(L, 2)); if (!mount) { lua_pushnil(L); return 1; } mountId = mount->id; } - pushBoolean(L, player->tameMount(mountId)); + Lua::pushBoolean(L, player->tameMount(mountId)); return 1; } int PlayerFunctions::luaPlayerRemoveMount(lua_State* L) { // player:removeMount(mountId or mountName) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } uint8_t mountId; - if (isNumber(L, 2)) { - mountId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + mountId = Lua::getNumber(L, 2); } else { - const auto &mount = g_game().mounts->getMountByName(getString(L, 2)); + const auto &mount = g_game().mounts->getMountByName(Lua::getString(L, 2)); if (!mount) { lua_pushnil(L); return 1; } mountId = mount->id; } - pushBoolean(L, player->untameMount(mountId)); + Lua::pushBoolean(L, player->untameMount(mountId)); return 1; } int PlayerFunctions::luaPlayerHasMount(lua_State* L) { // player:hasMount(mountId or mountName) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } std::shared_ptr mount = nullptr; - if (isNumber(L, 2)) { - mount = g_game().mounts->getMountByID(getNumber(L, 2)); + if (Lua::isNumber(L, 2)) { + mount = g_game().mounts->getMountByID(Lua::getNumber(L, 2)); } else { - mount = g_game().mounts->getMountByName(getString(L, 2)); + mount = g_game().mounts->getMountByName(Lua::getString(L, 2)); } if (mount) { - pushBoolean(L, player->hasMount(mount)); + Lua::pushBoolean(L, player->hasMount(mount)); } else { lua_pushnil(L); } @@ -2439,10 +2810,10 @@ int PlayerFunctions::luaPlayerHasMount(lua_State* L) { int PlayerFunctions::luaPlayerAddFamiliar(lua_State* L) { // player:addFamiliar(lookType) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - player->addFamiliar(getNumber(L, 2)); - pushBoolean(L, true); + player->addFamiliar(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2451,10 +2822,10 @@ int PlayerFunctions::luaPlayerAddFamiliar(lua_State* L) { int PlayerFunctions::luaPlayerRemoveFamiliar(lua_State* L) { // player:removeFamiliar(lookType) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint16_t lookType = getNumber(L, 2); - pushBoolean(L, player->removeFamiliar(lookType)); + const uint16_t lookType = Lua::getNumber(L, 2); + Lua::pushBoolean(L, player->removeFamiliar(lookType)); } else { lua_pushnil(L); } @@ -2463,10 +2834,10 @@ int PlayerFunctions::luaPlayerRemoveFamiliar(lua_State* L) { int PlayerFunctions::luaPlayerHasFamiliar(lua_State* L) { // player:hasFamiliar(lookType) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint16_t lookType = getNumber(L, 2); - pushBoolean(L, player->canFamiliar(lookType)); + const uint16_t lookType = Lua::getNumber(L, 2); + Lua::pushBoolean(L, player->canFamiliar(lookType)); } else { lua_pushnil(L); } @@ -2475,10 +2846,10 @@ int PlayerFunctions::luaPlayerHasFamiliar(lua_State* L) { int PlayerFunctions::luaPlayerSetFamiliarLooktype(lua_State* L) { // player:setFamiliarLooktype(lookType) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - player->setFamiliarLooktype(getNumber(L, 2)); - pushBoolean(L, true); + player->setFamiliarLooktype(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2487,7 +2858,7 @@ int PlayerFunctions::luaPlayerSetFamiliarLooktype(lua_State* L) { int PlayerFunctions::luaPlayerGetFamiliarLooktype(lua_State* L) { // player:getFamiliarLooktype() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->defaultOutfit.lookFamiliarsType); } else { @@ -2498,7 +2869,7 @@ int PlayerFunctions::luaPlayerGetFamiliarLooktype(lua_State* L) { int PlayerFunctions::luaPlayerGetPremiumDays(lua_State* L) { // player:getPremiumDays() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player && player->getAccount()) { lua_pushnumber(L, player->getAccount()->getPremiumRemainingDays()); } else { @@ -2509,7 +2880,7 @@ int PlayerFunctions::luaPlayerGetPremiumDays(lua_State* L) { int PlayerFunctions::luaPlayerAddPremiumDays(lua_State* L) { // player:addPremiumDays(days) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || !player->getAccount()) { lua_pushnil(L); return 1; @@ -2521,7 +2892,7 @@ int PlayerFunctions::luaPlayerAddPremiumDays(lua_State* L) { return 1; } - const int32_t addDays = std::min(0xFFFE - premiumDays, getNumber(L, 2)); + const int32_t addDays = std::min(0xFFFE - premiumDays, Lua::getNumber(L, 2)); if (addDays <= 0) { return 1; } @@ -2532,13 +2903,13 @@ int PlayerFunctions::luaPlayerAddPremiumDays(lua_State* L) { return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerRemovePremiumDays(lua_State* L) { // player:removePremiumDays(days) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || !player->getAccount()) { lua_pushnil(L); return 1; @@ -2550,7 +2921,7 @@ int PlayerFunctions::luaPlayerRemovePremiumDays(lua_State* L) { return 1; } - const int32_t removeDays = std::min(0xFFFE - premiumDays, getNumber(L, 2)); + const int32_t removeDays = std::min(0xFFFE - premiumDays, Lua::getNumber(L, 2)); if (removeDays <= 0) { return 1; } @@ -2561,15 +2932,15 @@ int PlayerFunctions::luaPlayerRemovePremiumDays(lua_State* L) { return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetTibiaCoins(lua_State* L) { // player:getTibiaCoins() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || !player->getAccount()) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); lua_pushnil(L); return 1; } @@ -2585,60 +2956,60 @@ int PlayerFunctions::luaPlayerGetTibiaCoins(lua_State* L) { int PlayerFunctions::luaPlayerAddTibiaCoins(lua_State* L) { // player:addTibiaCoins(coins) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || !player->getAccount()) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); lua_pushnil(L); return 1; } - if (player->account->addCoins(CoinType::Normal, getNumber(L, 2)) != AccountErrors_t::Ok) { - reportErrorFunc("Failed to add coins"); + if (player->account->addCoins(CoinType::Normal, Lua::getNumber(L, 2)) != AccountErrors_t::Ok) { + Lua::reportErrorFunc("Failed to add coins"); lua_pushnil(L); return 1; } if (player->getAccount()->save() != AccountErrors_t::Ok) { - reportErrorFunc("Failed to save account"); + Lua::reportErrorFunc("Failed to save account"); lua_pushnil(L); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerRemoveTibiaCoins(lua_State* L) { // player:removeTibiaCoins(coins) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || !player->getAccount()) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); lua_pushnil(L); return 1; } - if (player->account->removeCoins(CoinType::Normal, getNumber(L, 2)) != AccountErrors_t::Ok) { - reportErrorFunc("Failed to remove coins"); + if (player->account->removeCoins(CoinType::Normal, Lua::getNumber(L, 2)) != AccountErrors_t::Ok) { + Lua::reportErrorFunc("Failed to remove coins"); return 1; } if (player->getAccount()->save() != AccountErrors_t::Ok) { - reportErrorFunc("Failed to save account"); + Lua::reportErrorFunc("Failed to save account"); lua_pushnil(L); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetTransferableCoins(lua_State* L) { // player:getTransferableCoins() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || !player->getAccount()) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); lua_pushnil(L); return 1; } @@ -2654,62 +3025,62 @@ int PlayerFunctions::luaPlayerGetTransferableCoins(lua_State* L) { int PlayerFunctions::luaPlayerAddTransferableCoins(lua_State* L) { // player:addTransferableCoins(coins) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || !player->getAccount()) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); lua_pushnil(L); return 1; } - if (player->account->addCoins(CoinType::Transferable, getNumber(L, 2)) != AccountErrors_t::Ok) { - reportErrorFunc("failed to add transferable coins"); + if (player->account->addCoins(CoinType::Transferable, Lua::getNumber(L, 2)) != AccountErrors_t::Ok) { + Lua::reportErrorFunc("failed to add transferable coins"); lua_pushnil(L); return 1; } if (player->getAccount()->save() != AccountErrors_t::Ok) { - reportErrorFunc("failed to save account"); + Lua::reportErrorFunc("failed to save account"); lua_pushnil(L); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerRemoveTransferableCoins(lua_State* L) { // player:removeTransferableCoins(coins) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player || !player->getAccount()) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); lua_pushnil(L); return 1; } - if (player->account->removeCoins(CoinType::Transferable, getNumber(L, 2)) != AccountErrors_t::Ok) { - reportErrorFunc("failed to remove transferable coins"); + if (player->account->removeCoins(CoinType::Transferable, Lua::getNumber(L, 2)) != AccountErrors_t::Ok) { + Lua::reportErrorFunc("failed to remove transferable coins"); lua_pushnil(L); return 1; } if (player->getAccount()->save() != AccountErrors_t::Ok) { - reportErrorFunc("failed to save account"); + Lua::reportErrorFunc("failed to save account"); lua_pushnil(L); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerHasBlessing(lua_State* L) { // player:hasBlessing(blessing) - const uint8_t blessing = getNumber(L, 2); - const auto &player = getUserdataShared(L, 1); + const uint8_t blessing = Lua::getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - pushBoolean(L, player->hasBlessing(blessing)); + Lua::pushBoolean(L, player->hasBlessing(blessing)); } else { lua_pushnil(L); } @@ -2718,52 +3089,52 @@ int PlayerFunctions::luaPlayerHasBlessing(lua_State* L) { int PlayerFunctions::luaPlayerAddBlessing(lua_State* L) { // player:addBlessing(blessing) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const uint8_t blessing = getNumber(L, 2); - const uint8_t count = getNumber(L, 3); + const uint8_t blessing = Lua::getNumber(L, 2); + const uint8_t count = Lua::getNumber(L, 3); player->addBlessing(blessing, count); player->sendBlessStatus(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerRemoveBlessing(lua_State* L) { // player:removeBlessing(blessing) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const uint8_t blessing = getNumber(L, 2); - const uint8_t count = getNumber(L, 3); + const uint8_t blessing = Lua::getNumber(L, 2); + const uint8_t count = Lua::getNumber(L, 3); if (!player->hasBlessing(blessing)) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } player->removeBlessing(blessing, count); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetBlessingCount(lua_State* L) { // player:getBlessingCount(index[, storeCount = false]) - const auto &player = getUserdataShared(L, 1); - uint8_t index = getNumber(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); + uint8_t index = Lua::getNumber(L, 2); if (index == 0) { index = 1; } if (player) { - lua_pushnumber(L, player->getBlessingCount(index, getBoolean(L, 3, false))); + lua_pushnumber(L, player->getBlessingCount(index, Lua::getBoolean(L, 3, false))); } else { lua_pushnil(L); } @@ -2772,45 +3143,45 @@ int PlayerFunctions::luaPlayerGetBlessingCount(lua_State* L) { int PlayerFunctions::luaPlayerCanLearnSpell(lua_State* L) { // player:canLearnSpell(spellName) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const std::string &spellName = getString(L, 2); + const std::string &spellName = Lua::getString(L, 2); const auto &spell = g_spells().getInstantSpellByName(spellName); if (!spell) { - reportErrorFunc("Spell \"" + spellName + "\" not found"); - pushBoolean(L, false); + Lua::reportErrorFunc("Spell \"" + spellName + "\" not found"); + Lua::pushBoolean(L, false); return 1; } if (player->hasFlag(PlayerFlags_t::IgnoreSpellCheck)) { - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } const auto vocMap = spell->getVocMap(); if (!vocMap.contains(player->getVocationId())) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } else if (player->getLevel() < spell->getLevel()) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } else if (player->getMagicLevel() < spell->getMagicLevel()) { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } else { - pushBoolean(L, true); + Lua::pushBoolean(L, true); } return 1; } int PlayerFunctions::luaPlayerLearnSpell(lua_State* L) { // player:learnSpell(spellName) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const std::string &spellName = getString(L, 2); + const std::string &spellName = Lua::getString(L, 2); player->learnInstantSpell(spellName); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2819,11 +3190,11 @@ int PlayerFunctions::luaPlayerLearnSpell(lua_State* L) { int PlayerFunctions::luaPlayerForgetSpell(lua_State* L) { // player:forgetSpell(spellName) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const std::string &spellName = getString(L, 2); + const std::string &spellName = Lua::getString(L, 2); player->forgetInstantSpell(spellName); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2832,10 +3203,10 @@ int PlayerFunctions::luaPlayerForgetSpell(lua_State* L) { int PlayerFunctions::luaPlayerHasLearnedSpell(lua_State* L) { // player:hasLearnedSpell(spellName) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const std::string &spellName = getString(L, 2); - pushBoolean(L, player->hasLearnedInstantSpell(spellName)); + const std::string &spellName = Lua::getString(L, 2); + Lua::pushBoolean(L, player->hasLearnedInstantSpell(spellName)); } else { lua_pushnil(L); } @@ -2844,11 +3215,11 @@ int PlayerFunctions::luaPlayerHasLearnedSpell(lua_State* L) { int PlayerFunctions::luaPlayerSendTutorial(lua_State* L) { // player:sendTutorial(tutorialId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint8_t tutorialId = getNumber(L, 2); + const uint8_t tutorialId = Lua::getNumber(L, 2); player->sendTutorial(tutorialId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2857,17 +3228,17 @@ int PlayerFunctions::luaPlayerSendTutorial(lua_State* L) { int PlayerFunctions::luaPlayerOpenImbuementWindow(lua_State* L) { // player:openImbuementWindow(item) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const auto &item = getUserdataShared(L, 2); + const auto &item = Lua::getUserdataShared(L, 2); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -2877,10 +3248,10 @@ int PlayerFunctions::luaPlayerOpenImbuementWindow(lua_State* L) { int PlayerFunctions::luaPlayerCloseImbuementWindow(lua_State* L) { // player:closeImbuementWindow() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -2890,13 +3261,13 @@ int PlayerFunctions::luaPlayerCloseImbuementWindow(lua_State* L) { int PlayerFunctions::luaPlayerAddMapMark(lua_State* L) { // player:addMapMark(position, type, description) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const Position &position = getPosition(L, 2); - const uint8_t type = getNumber(L, 3); - const std::string &description = getString(L, 4); + const Position &position = Lua::getPosition(L, 2); + const uint8_t type = Lua::getNumber(L, 3); + const std::string &description = Lua::getString(L, 4); player->sendAddMarker(position, type, description); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2905,12 +3276,12 @@ int PlayerFunctions::luaPlayerAddMapMark(lua_State* L) { int PlayerFunctions::luaPlayerSave(lua_State* L) { // player:save() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { if (!player->isOffline()) { player->loginPosition = player->getPosition(); } - pushBoolean(L, g_saveManager().savePlayer(player)); + Lua::pushBoolean(L, g_saveManager().savePlayer(player)); } else { lua_pushnil(L); } @@ -2919,11 +3290,11 @@ int PlayerFunctions::luaPlayerSave(lua_State* L) { int PlayerFunctions::luaPlayerPopupFYI(lua_State* L) { // player:popupFYI(message) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const std::string &message = getString(L, 2); + const std::string &message = Lua::getString(L, 2); player->sendFYIBox(message); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -2932,9 +3303,9 @@ int PlayerFunctions::luaPlayerPopupFYI(lua_State* L) { int PlayerFunctions::luaPlayerIsPzLocked(lua_State* L) { // player:isPzLocked() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - pushBoolean(L, player->isPzLocked()); + Lua::pushBoolean(L, player->isPzLocked()); } else { lua_pushnil(L); } @@ -2943,11 +3314,11 @@ int PlayerFunctions::luaPlayerIsPzLocked(lua_State* L) { int PlayerFunctions::luaPlayerGetClient(lua_State* L) { // player:getClient() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_createtable(L, 0, 2); - setField(L, "version", player->getProtocolVersion()); - setField(L, "os", player->getOperatingSystem()); + Lua::setField(L, "version", player->getProtocolVersion()); + Lua::setField(L, "os", player->getOperatingSystem()); } else { lua_pushnil(L); } @@ -2956,7 +3327,7 @@ int PlayerFunctions::luaPlayerGetClient(lua_State* L) { int PlayerFunctions::luaPlayerGetHouse(lua_State* L) { // player:getHouse() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -2964,8 +3335,8 @@ int PlayerFunctions::luaPlayerGetHouse(lua_State* L) { const auto &house = g_game().map.houses.getHouseByPlayerId(player->getGUID()); if (house) { - pushUserdata(L, house); - setMetatable(L, -1, "House"); + Lua::pushUserdata(L, house); + Lua::setMetatable(L, -1, "House"); } else { lua_pushnil(L); } @@ -2974,55 +3345,55 @@ int PlayerFunctions::luaPlayerGetHouse(lua_State* L) { int PlayerFunctions::luaPlayerSendHouseWindow(lua_State* L) { // player:sendHouseWindow(house, listId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &house = getUserdataShared(L, 2); + const auto &house = Lua::getUserdataShared(L, 2); if (!house) { lua_pushnil(L); return 1; } - const uint32_t listId = getNumber(L, 3); + const uint32_t listId = Lua::getNumber(L, 3); player->sendHouseWindow(house, listId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSetEditHouse(lua_State* L) { // player:setEditHouse(house, listId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &house = getUserdataShared(L, 2); + const auto &house = Lua::getUserdataShared(L, 2); if (!house) { lua_pushnil(L); return 1; } - const uint32_t listId = getNumber(L, 3); + const uint32_t listId = Lua::getNumber(L, 3); player->setEditHouse(house, listId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSetGhostMode(lua_State* L) { // player:setGhostMode(enabled) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const bool enabled = getBoolean(L, 2); + const bool enabled = Lua::getBoolean(L, 2); if (player->isInGhostMode() == enabled) { - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -3057,19 +3428,19 @@ int PlayerFunctions::luaPlayerSetGhostMode(lua_State* L) { } } } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetContainerId(lua_State* L) { // player:getContainerId(container) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &container = getUserdataShared(L, 2); + const auto &container = Lua::getUserdataShared(L, 2); if (container) { lua_pushnumber(L, player->getContainerID(container)); } else { @@ -3080,16 +3451,16 @@ int PlayerFunctions::luaPlayerGetContainerId(lua_State* L) { int PlayerFunctions::luaPlayerGetContainerById(lua_State* L) { // player:getContainerById(id) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto &container = player->getContainerByID(getNumber(L, 2)); + const auto &container = player->getContainerByID(Lua::getNumber(L, 2)); if (container) { - pushUserdata(L, container); - setMetatable(L, -1, "Container"); + Lua::pushUserdata(L, container); + Lua::setMetatable(L, -1, "Container"); } else { lua_pushnil(L); } @@ -3098,9 +3469,9 @@ int PlayerFunctions::luaPlayerGetContainerById(lua_State* L) { int PlayerFunctions::luaPlayerGetContainerIndex(lua_State* L) { // player:getContainerIndex(id) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - lua_pushnumber(L, player->getContainerIndex(getNumber(L, 2))); + lua_pushnumber(L, player->getContainerIndex(Lua::getNumber(L, 2))); } else { lua_pushnil(L); } @@ -3109,7 +3480,7 @@ int PlayerFunctions::luaPlayerGetContainerIndex(lua_State* L) { int PlayerFunctions::luaPlayerGetInstantSpells(lua_State* L) { // player:getInstantSpells() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -3126,7 +3497,7 @@ int PlayerFunctions::luaPlayerGetInstantSpells(lua_State* L) { int index = 0; for (const auto &spell : spells) { - pushInstantSpell(L, *spell); + Lua::pushInstantSpell(L, *spell); lua_rawseti(L, -2, ++index); } return 1; @@ -3134,10 +3505,10 @@ int PlayerFunctions::luaPlayerGetInstantSpells(lua_State* L) { int PlayerFunctions::luaPlayerCanCast(lua_State* L) { // player:canCast(spell) - const auto &player = getUserdataShared(L, 1); - const auto &spell = getUserdataShared(L, 2); + const auto &player = Lua::getUserdataShared(L, 1); + const auto &spell = Lua::getUserdataShared(L, 2); if (player && spell) { - pushBoolean(L, spell->canCast(player)); + Lua::pushBoolean(L, spell->canCast(player)); } else { lua_pushnil(L); } @@ -3146,9 +3517,9 @@ int PlayerFunctions::luaPlayerCanCast(lua_State* L) { int PlayerFunctions::luaPlayerHasChaseMode(lua_State* L) { // player:hasChaseMode() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - pushBoolean(L, player->chaseMode); + Lua::pushBoolean(L, player->chaseMode); } else { lua_pushnil(L); } @@ -3157,9 +3528,9 @@ int PlayerFunctions::luaPlayerHasChaseMode(lua_State* L) { int PlayerFunctions::luaPlayerHasSecureMode(lua_State* L) { // player:hasSecureMode() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - pushBoolean(L, player->secureMode); + Lua::pushBoolean(L, player->secureMode); } else { lua_pushnil(L); } @@ -3168,7 +3539,7 @@ int PlayerFunctions::luaPlayerHasSecureMode(lua_State* L) { int PlayerFunctions::luaPlayerGetFightMode(lua_State* L) { // player:getFightMode() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->fightMode); } else { @@ -3179,7 +3550,7 @@ int PlayerFunctions::luaPlayerGetFightMode(lua_State* L) { int PlayerFunctions::luaPlayerGetBaseXpGain(lua_State* L) { // player:getBaseXpGain() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getBaseXpGain()); } else { @@ -3190,11 +3561,11 @@ int PlayerFunctions::luaPlayerGetBaseXpGain(lua_State* L) { int PlayerFunctions::luaPlayerSetBaseXpGain(lua_State* L) { // player:setBaseXpGain(value) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - player->setBaseXpGain(getNumber(L, 2)); + player->setBaseXpGain(Lua::getNumber(L, 2)); player->sendStats(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -3203,7 +3574,7 @@ int PlayerFunctions::luaPlayerSetBaseXpGain(lua_State* L) { int PlayerFunctions::luaPlayerGetVoucherXpBoost(lua_State* L) { // player:getVoucherXpBoost() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getVoucherXpBoost()); } else { @@ -3214,11 +3585,11 @@ int PlayerFunctions::luaPlayerGetVoucherXpBoost(lua_State* L) { int PlayerFunctions::luaPlayerSetVoucherXpBoost(lua_State* L) { // player:setVoucherXpBoost(value) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - player->setVoucherXpBoost(getNumber(L, 2)); + player->setVoucherXpBoost(Lua::getNumber(L, 2)); player->sendStats(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -3227,7 +3598,7 @@ int PlayerFunctions::luaPlayerSetVoucherXpBoost(lua_State* L) { int PlayerFunctions::luaPlayerGetGrindingXpBoost(lua_State* L) { // player:getGrindingXpBoost() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getGrindingXpBoost()); } else { @@ -3238,11 +3609,11 @@ int PlayerFunctions::luaPlayerGetGrindingXpBoost(lua_State* L) { int PlayerFunctions::luaPlayerSetGrindingXpBoost(lua_State* L) { // player:setGrindingXpBoost(value) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - player->setGrindingXpBoost(getNumber(L, 2)); + player->setGrindingXpBoost(Lua::getNumber(L, 2)); player->sendStats(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -3251,7 +3622,7 @@ int PlayerFunctions::luaPlayerSetGrindingXpBoost(lua_State* L) { int PlayerFunctions::luaPlayerGetXpBoostPercent(lua_State* L) { // player:getXpBoostPercent() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getXpBoostPercent()); } else { @@ -3262,11 +3633,11 @@ int PlayerFunctions::luaPlayerGetXpBoostPercent(lua_State* L) { int PlayerFunctions::luaPlayerSetXpBoostPercent(lua_State* L) { // player:setXpBoostPercent(value) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint16_t percent = getNumber(L, 2); + const uint16_t percent = Lua::getNumber(L, 2); player->setXpBoostPercent(percent); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -3275,7 +3646,7 @@ int PlayerFunctions::luaPlayerSetXpBoostPercent(lua_State* L) { int PlayerFunctions::luaPlayerGetStaminaXpBoost(lua_State* L) { // player:getStaminaXpBoost() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getStaminaXpBoost()); } else { @@ -3286,11 +3657,11 @@ int PlayerFunctions::luaPlayerGetStaminaXpBoost(lua_State* L) { int PlayerFunctions::luaPlayerSetStaminaXpBoost(lua_State* L) { // player:setStaminaXpBoost(value) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - player->setStaminaXpBoost(getNumber(L, 2)); + player->setStaminaXpBoost(Lua::getNumber(L, 2)); player->sendStats(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -3299,12 +3670,12 @@ int PlayerFunctions::luaPlayerSetStaminaXpBoost(lua_State* L) { int PlayerFunctions::luaPlayerSetXpBoostTime(lua_State* L) { // player:setXpBoostTime(timeLeft) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - const uint16_t timeLeft = getNumber(L, 2); + const uint16_t timeLeft = Lua::getNumber(L, 2); player->setXpBoostTime(timeLeft); player->sendStats(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -3313,7 +3684,7 @@ int PlayerFunctions::luaPlayerSetXpBoostTime(lua_State* L) { int PlayerFunctions::luaPlayerGetXpBoostTime(lua_State* L) { // player:getXpBoostTime() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getXpBoostTime()); } else { @@ -3324,7 +3695,7 @@ int PlayerFunctions::luaPlayerGetXpBoostTime(lua_State* L) { int PlayerFunctions::luaPlayerGetIdleTime(lua_State* L) { // player:getIdleTime() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { lua_pushnumber(L, player->getIdleTime()); } else { @@ -3335,7 +3706,7 @@ int PlayerFunctions::luaPlayerGetIdleTime(lua_State* L) { int PlayerFunctions::luaPlayerGetFreeBackpackSlots(lua_State* L) { // player:getFreeBackpackSlots() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); } @@ -3345,11 +3716,11 @@ int PlayerFunctions::luaPlayerGetFreeBackpackSlots(lua_State* L) { } int PlayerFunctions::luaPlayerIsOffline(lua_State* L) { - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player) { - pushBoolean(L, player->isOffline()); + Lua::pushBoolean(L, player->isOffline()); } else { - pushBoolean(L, true); + Lua::pushBoolean(L, true); } return 1; @@ -3357,80 +3728,80 @@ int PlayerFunctions::luaPlayerIsOffline(lua_State* L) { int PlayerFunctions::luaPlayerOpenMarket(lua_State* L) { // player:openMarket() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } player->sendMarketEnter(player->getLastDepotId()); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } // Forge int PlayerFunctions::luaPlayerOpenForge(lua_State* L) { // player:openForge() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } player->sendOpenForge(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerCloseForge(lua_State* L) { // player:closeForge() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } player->closeForgeWindow(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerAddForgeDusts(lua_State* L) { // player:addForgeDusts(amount) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - player->addForgeDusts(getNumber(L, 2, 0)); - pushBoolean(L, true); + player->addForgeDusts(Lua::getNumber(L, 2, 0)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerRemoveForgeDusts(lua_State* L) { // player:removeForgeDusts(amount) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - player->removeForgeDusts(getNumber(L, 2, 0)); - pushBoolean(L, true); + player->removeForgeDusts(Lua::getNumber(L, 2, 0)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetForgeDusts(lua_State* L) { // player:getForgeDusts() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -3440,52 +3811,52 @@ int PlayerFunctions::luaPlayerGetForgeDusts(lua_State* L) { int PlayerFunctions::luaPlayerSetForgeDusts(lua_State* L) { // player:setForgeDusts() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - player->setForgeDusts(getNumber(L, 2, 0)); - pushBoolean(L, true); + player->setForgeDusts(Lua::getNumber(L, 2, 0)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerAddForgeDustLevel(lua_State* L) { // player:addForgeDustLevel(amount) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - player->addForgeDustLevel(getNumber(L, 2, 1)); - pushBoolean(L, true); + player->addForgeDustLevel(Lua::getNumber(L, 2, 1)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerRemoveForgeDustLevel(lua_State* L) { // player:removeForgeDustLevel(amount) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - player->removeForgeDustLevel(getNumber(L, 2, 1)); - pushBoolean(L, true); + player->removeForgeDustLevel(Lua::getNumber(L, 2, 1)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetForgeDustLevel(lua_State* L) { // player:getForgeDustLevel() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -3495,10 +3866,10 @@ int PlayerFunctions::luaPlayerGetForgeDustLevel(lua_State* L) { int PlayerFunctions::luaPlayerGetForgeSlivers(lua_State* L) { // player:getForgeSlivers() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -3509,10 +3880,10 @@ int PlayerFunctions::luaPlayerGetForgeSlivers(lua_State* L) { int PlayerFunctions::luaPlayerGetForgeCores(lua_State* L) { // player:getForgeCores() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -3523,25 +3894,25 @@ int PlayerFunctions::luaPlayerGetForgeCores(lua_State* L) { int PlayerFunctions::luaPlayerSetFaction(lua_State* L) { // player:setFaction(factionId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player == nullptr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - const Faction_t factionId = getNumber(L, 2); + const Faction_t factionId = Lua::getNumber(L, 2); player->setFaction(factionId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetFaction(lua_State* L) { // player:getFaction() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (player == nullptr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -3551,52 +3922,52 @@ int PlayerFunctions::luaPlayerGetFaction(lua_State* L) { int PlayerFunctions::luaPlayerIsUIExhausted(lua_State* L) { // player:isUIExhausted() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - const uint16_t time = getNumber(L, 2); - pushBoolean(L, player->isUIExhausted(time)); + const uint16_t time = Lua::getNumber(L, 2); + Lua::pushBoolean(L, player->isUIExhausted(time)); return 1; } int PlayerFunctions::luaPlayerUpdateUIExhausted(lua_State* L) { // player:updateUIExhausted(exhaustionTime = 250) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } player->updateUIExhausted(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } // Bosstiary Cooldown Timer int PlayerFunctions::luaPlayerBosstiaryCooldownTimer(lua_State* L) { // player:sendBosstiaryCooldownTimer() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } player->sendBosstiaryCooldownTimer(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetBosstiaryLevel(lua_State* L) { // player:getBosstiaryLevel(name) - if (const auto &player = getUserdataShared(L, 1); + if (const auto &player = Lua::getUserdataShared(L, 1); player) { - const auto &mtype = g_monsters().getMonsterType(getString(L, 2)); + const auto &mtype = g_monsters().getMonsterType(Lua::getString(L, 2)); if (mtype) { const uint32_t bossId = mtype->info.raceid; if (bossId == 0) { @@ -3616,9 +3987,9 @@ int PlayerFunctions::luaPlayerGetBosstiaryLevel(lua_State* L) { int PlayerFunctions::luaPlayerGetBosstiaryKills(lua_State* L) { // player:getBosstiaryKills(name) - if (const auto &player = getUserdataShared(L, 1); + if (const auto &player = Lua::getUserdataShared(L, 1); player) { - const auto &mtype = g_monsters().getMonsterType(getString(L, 2)); + const auto &mtype = g_monsters().getMonsterType(Lua::getString(L, 2)); if (mtype) { const uint32_t bossId = mtype->info.raceid; if (bossId == 0) { @@ -3638,12 +4009,12 @@ int PlayerFunctions::luaPlayerGetBosstiaryKills(lua_State* L) { int PlayerFunctions::luaPlayerAddBosstiaryKill(lua_State* L) { // player:addBosstiaryKill(name[, amount = 1]) - if (const auto &player = getUserdataShared(L, 1); + if (const auto &player = Lua::getUserdataShared(L, 1); player) { - const auto &mtype = g_monsters().getMonsterType(getString(L, 2)); + const auto &mtype = g_monsters().getMonsterType(Lua::getString(L, 2)); if (mtype) { - g_ioBosstiary().addBosstiaryKill(player, mtype, getNumber(L, 3, 1)); - pushBoolean(L, true); + g_ioBosstiary().addBosstiaryKill(player, mtype, Lua::getNumber(L, 3, 1)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -3655,42 +4026,42 @@ int PlayerFunctions::luaPlayerAddBosstiaryKill(lua_State* L) { int PlayerFunctions::luaPlayerSetBossPoints(lua_State* L) { // player:setBossPoints() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - player->setBossPoints(getNumber(L, 2, 0)); - pushBoolean(L, true); + player->setBossPoints(Lua::getNumber(L, 2, 0)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSetRemoveBossTime(lua_State* L) { // player:setRemoveBossTime() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - player->setRemoveBossTime(getNumber(L, 2, 0)); - pushBoolean(L, true); + player->setRemoveBossTime(Lua::getNumber(L, 2, 0)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetSlotBossId(lua_State* L) { // player:getSlotBossId(slotId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - const uint8_t slotId = getNumber(L, 2); + const uint8_t slotId = Lua::getNumber(L, 2); const auto bossId = player->getSlotBossId(slotId); lua_pushnumber(L, static_cast(bossId)); return 1; @@ -3698,14 +4069,14 @@ int PlayerFunctions::luaPlayerGetSlotBossId(lua_State* L) { int PlayerFunctions::luaPlayerGetBossBonus(lua_State* L) { // player:getBossBonus(slotId) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - const uint8_t slotId = getNumber(L, 2); + const uint8_t slotId = Lua::getNumber(L, 2); const auto bossId = player->getSlotBossId(slotId); const uint32_t playerBossPoints = player->getBossPoints(); @@ -3720,65 +4091,65 @@ int PlayerFunctions::luaPlayerGetBossBonus(lua_State* L) { int PlayerFunctions::luaPlayerSendSingleSoundEffect(lua_State* L) { // player:sendSingleSoundEffect(soundId[, actor = true]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - const SoundEffect_t soundEffect = getNumber(L, 2); - const bool actor = getBoolean(L, 3, true); + const SoundEffect_t soundEffect = Lua::getNumber(L, 2); + const bool actor = Lua::getBoolean(L, 3, true); player->sendSingleSoundEffect(player->getPosition(), soundEffect, actor ? SourceEffect_t::OWN : SourceEffect_t::GLOBAL); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSendDoubleSoundEffect(lua_State* L) { // player:sendDoubleSoundEffect(mainSoundId, secondarySoundId[, actor = true]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - const SoundEffect_t mainSoundEffect = getNumber(L, 2); - const SoundEffect_t secondarySoundEffect = getNumber(L, 3); - const bool actor = getBoolean(L, 4, true); + const SoundEffect_t mainSoundEffect = Lua::getNumber(L, 2); + const SoundEffect_t secondarySoundEffect = Lua::getNumber(L, 3); + const bool actor = Lua::getBoolean(L, 4, true); player->sendDoubleSoundEffect(player->getPosition(), mainSoundEffect, actor ? SourceEffect_t::OWN : SourceEffect_t::GLOBAL, secondarySoundEffect, actor ? SourceEffect_t::OWN : SourceEffect_t::GLOBAL); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetName(lua_State* L) { // player:getName() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - pushString(L, player->getName()); + Lua::pushString(L, player->getName()); return 1; } int PlayerFunctions::luaPlayerChangeName(lua_State* L) { // player:changeName(newName) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } if (player->isOnline()) { player->removePlayer(true, true); } player->kv()->remove("namelock"); - const auto newName = getString(L, 2); + const auto newName = Lua::getString(L, 2); player->setName(newName); g_saveManager().savePlayer(player); return 1; @@ -3786,64 +4157,64 @@ int PlayerFunctions::luaPlayerChangeName(lua_State* L) { int PlayerFunctions::luaPlayerHasGroupFlag(lua_State* L) { // player:hasGroupFlag(flag) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - pushBoolean(L, player->hasFlag(getNumber(L, 2))); + Lua::pushBoolean(L, player->hasFlag(Lua::getNumber(L, 2))); return 1; } int PlayerFunctions::luaPlayerSetGroupFlag(lua_State* L) { // player:setGroupFlag(flag) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - player->setFlag(getNumber(L, 2)); + player->setFlag(Lua::getNumber(L, 2)); return 1; } int PlayerFunctions::luaPlayerRemoveGroupFlag(lua_State* L) { // player:removeGroupFlag(flag) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - player->removeFlag(getNumber(L, 2)); + player->removeFlag(Lua::getNumber(L, 2)); return 1; } // Hazard system int PlayerFunctions::luaPlayerAddHazardSystemPoints(lua_State* L) { // player:setHazardSystemPoints(amount) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - pushBoolean(L, false); - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - player->setHazardSystemPoints(getNumber(L, 2, 0)); - pushBoolean(L, true); + player->setHazardSystemPoints(Lua::getNumber(L, 2, 0)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetHazardSystemPoints(lua_State* L) { // player:getHazardSystemPoints() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - pushBoolean(L, false); - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } @@ -3853,20 +4224,20 @@ int PlayerFunctions::luaPlayerGetHazardSystemPoints(lua_State* L) { int PlayerFunctions::luaPlayerSetLoyaltyBonus(lua_State* L) { // player:setLoyaltyBonus(amount) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - player->setLoyaltyBonus(getNumber(L, 2)); - pushBoolean(L, true); + player->setLoyaltyBonus(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetLoyaltyBonus(lua_State* L) { // player:getLoyaltyBonus() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -3878,7 +4249,7 @@ int PlayerFunctions::luaPlayerGetLoyaltyBonus(lua_State* L) { int PlayerFunctions::luaPlayerGetLoyaltyPoints(lua_State* L) { // player:getLoyaltyPoints() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -3890,51 +4261,51 @@ int PlayerFunctions::luaPlayerGetLoyaltyPoints(lua_State* L) { int PlayerFunctions::luaPlayerGetLoyaltyTitle(lua_State* L) { // player:getLoyaltyTitle() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - pushString(L, player->getLoyaltyTitle()); + Lua::pushString(L, player->getLoyaltyTitle()); return 1; } int PlayerFunctions::luaPlayerSetLoyaltyTitle(lua_State* L) { // player:setLoyaltyTitle(name) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - player->setLoyaltyTitle(getString(L, 2)); - pushBoolean(L, true); + player->setLoyaltyTitle(Lua::getString(L, 2)); + Lua::pushBoolean(L, true); return 1; } // Wheel of destiny system int PlayerFunctions::luaPlayerInstantSkillWOD(lua_State* L) { // player:instantSkillWOD(name[, value]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const std::string name = getString(L, 2); + const std::string name = Lua::getString(L, 2); if (lua_gettop(L) == 2) { - pushBoolean(L, player->wheel()->getInstant(name)); + Lua::pushBoolean(L, player->wheel()->getInstant(name)); } else { - player->wheel()->setSpellInstant(name, getBoolean(L, 3)); - pushBoolean(L, true); + player->wheel()->setSpellInstant(name, Lua::getBoolean(L, 3)); + Lua::pushBoolean(L, true); } return 1; } int PlayerFunctions::luaPlayerUpgradeSpellWOD(lua_State* L) { // player:upgradeSpellsWOD([name[, add]]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -3945,26 +4316,26 @@ int PlayerFunctions::luaPlayerUpgradeSpellWOD(lua_State* L) { return 1; } - const std::string name = getString(L, 2); + const std::string name = Lua::getString(L, 2); if (lua_gettop(L) == 2) { lua_pushnumber(L, static_cast(player->wheel()->getSpellUpgrade(name))); return 1; } - const bool add = getBoolean(L, 3); + const bool add = Lua::getBoolean(L, 3); if (add) { player->wheel()->upgradeSpell(name); } else { player->wheel()->downgradeSpell(name); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerRevelationStageWOD(lua_State* L) { // player:revelationStageWOD([name[, set]]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -3975,22 +4346,22 @@ int PlayerFunctions::luaPlayerRevelationStageWOD(lua_State* L) { return 1; } - const std::string name = getString(L, 2); + const std::string name = Lua::getString(L, 2); if (lua_gettop(L) == 2) { lua_pushnumber(L, static_cast(player->wheel()->getStage(name))); return 1; } - const bool value = getNumber(L, 3); + const bool value = Lua::getNumber(L, 3); player->wheel()->setSpellInstant(name, value); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerReloadData(lua_State* L) { // player:reloadData() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -4001,26 +4372,26 @@ int PlayerFunctions::luaPlayerReloadData(lua_State* L) { player->sendBasicData(); player->wheel()->sendGiftOfLifeCooldown(); g_game().reloadCreature(player); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerOnThinkWheelOfDestiny(lua_State* L) { // player:onThinkWheelOfDestiny([force = false]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - player->wheel()->onThink(getBoolean(L, 2, false)); - pushBoolean(L, true); + player->wheel()->onThink(Lua::getBoolean(L, 2, false)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerAvatarTimer(lua_State* L) { // player:avatarTimer([value]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; @@ -4029,59 +4400,59 @@ int PlayerFunctions::luaPlayerAvatarTimer(lua_State* L) { if (lua_gettop(L) == 1) { lua_pushnumber(L, static_cast(player->wheel()->getOnThinkTimer(WheelOnThink_t::AVATAR_SPELL))); } else { - player->wheel()->setOnThinkTimer(WheelOnThink_t::AVATAR_SPELL, getNumber(L, 2)); - pushBoolean(L, true); + player->wheel()->setOnThinkTimer(WheelOnThink_t::AVATAR_SPELL, Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } return 1; } int PlayerFunctions::luaPlayerGetWheelSpellAdditionalArea(lua_State* L) { // player:getWheelSpellAdditionalArea(spellname) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - const auto spellName = getString(L, 2); + const auto spellName = Lua::getString(L, 2); if (spellName.empty()) { - reportErrorFunc("Spell name is empty"); - pushBoolean(L, false); + Lua::reportErrorFunc("Spell name is empty"); + Lua::pushBoolean(L, false); return 0; } const auto &spell = g_spells().getInstantSpellByName(spellName); if (!spell) { - reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - pushBoolean(L, player->wheel()->getSpellAdditionalArea(spellName)); + Lua::pushBoolean(L, player->wheel()->getSpellAdditionalArea(spellName)); return 1; } int PlayerFunctions::luaPlayerGetWheelSpellAdditionalTarget(lua_State* L) { // player:getWheelSpellAdditionalTarget(spellname) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - const auto spellName = getString(L, 2); + const auto spellName = Lua::getString(L, 2); if (spellName.empty()) { - reportErrorFunc("Spell name is empty"); - pushBoolean(L, false); + Lua::reportErrorFunc("Spell name is empty"); + Lua::pushBoolean(L, false); return 0; } const auto &spell = g_spells().getInstantSpellByName(spellName); if (!spell) { - reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -4091,24 +4462,24 @@ int PlayerFunctions::luaPlayerGetWheelSpellAdditionalTarget(lua_State* L) { int PlayerFunctions::luaPlayerGetWheelSpellAdditionalDuration(lua_State* L) { // player:getWheelSpellAdditionalDuration(spellname) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } - const auto spellName = getString(L, 2); + const auto spellName = Lua::getString(L, 2); if (spellName.empty()) { - reportErrorFunc("Spell name is empty"); - pushBoolean(L, false); + Lua::reportErrorFunc("Spell name is empty"); + Lua::pushBoolean(L, false); return 0; } const auto &spell = g_spells().getInstantSpellByName(spellName); if (!spell) { - reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); + Lua::pushBoolean(L, false); return 0; } @@ -4118,46 +4489,46 @@ int PlayerFunctions::luaPlayerGetWheelSpellAdditionalDuration(lua_State* L) { int PlayerFunctions::luaPlayerUpdateConcoction(lua_State* L) { // player:updateConcoction(itemid, timeLeft) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - player->updateConcoction(getNumber(L, 2), getNumber(L, 3)); - pushBoolean(L, true); + player->updateConcoction(Lua::getNumber(L, 2), Lua::getNumber(L, 3)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerClearSpellCooldowns(lua_State* L) { // player:clearSpellCooldowns() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } player->clearCooldowns(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerIsVip(lua_State* L) { // player:isVip() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, player->isVip()); + Lua::pushBoolean(L, player->isVip()); return 1; } int PlayerFunctions::luaPlayerGetVipDays(lua_State* L) { // player:getVipDays() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -4167,10 +4538,10 @@ int PlayerFunctions::luaPlayerGetVipDays(lua_State* L) { int PlayerFunctions::luaPlayerGetVipTime(lua_State* L) { // player:getVipTime() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -4180,102 +4551,102 @@ int PlayerFunctions::luaPlayerGetVipTime(lua_State* L) { int PlayerFunctions::luaPlayerKV(lua_State* L) { // player:kv() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - pushUserdata(L, player->kv()); - setMetatable(L, -1, "KV"); + Lua::pushUserdata(L, player->kv()); + Lua::setMetatable(L, -1, "KV"); return 1; } int PlayerFunctions::luaPlayerGetStoreInbox(lua_State* L) { // player:getStoreInbox() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } if (const auto &item = player->getStoreInbox()) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } int PlayerFunctions::luaPlayerHasAchievement(lua_State* L) { // player:hasAchievement(id or name) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } uint16_t achievementId = 0; - if (isNumber(L, 2)) { - achievementId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + achievementId = Lua::getNumber(L, 2); } else { - achievementId = g_game().getAchievementByName(getString(L, 2)).id; + achievementId = g_game().getAchievementByName(Lua::getString(L, 2)).id; } - pushBoolean(L, player->achiev()->isUnlocked(achievementId)); + Lua::pushBoolean(L, player->achiev()->isUnlocked(achievementId)); return 1; } int PlayerFunctions::luaPlayerAddAchievement(lua_State* L) { // player:addAchievement(id or name[, sendMessage = true]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } uint16_t achievementId = 0; - if (isNumber(L, 2)) { - achievementId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + achievementId = Lua::getNumber(L, 2); } else { - achievementId = g_game().getAchievementByName(getString(L, 2)).id; + achievementId = g_game().getAchievementByName(Lua::getString(L, 2)).id; } - const bool success = player->achiev()->add(achievementId, getBoolean(L, 3, true)); + const bool success = player->achiev()->add(achievementId, Lua::getBoolean(L, 3, true)); if (success) { player->sendTakeScreenshot(SCREENSHOT_TYPE_ACHIEVEMENT); } - pushBoolean(L, success); + Lua::pushBoolean(L, success); return 1; } int PlayerFunctions::luaPlayerRemoveAchievement(lua_State* L) { // player:removeAchievement(id or name) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } uint16_t achievementId = 0; - if (isNumber(L, 2)) { - achievementId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + achievementId = Lua::getNumber(L, 2); } else { - achievementId = g_game().getAchievementByName(getString(L, 2)).id; + achievementId = g_game().getAchievementByName(Lua::getString(L, 2)).id; } - pushBoolean(L, player->achiev()->remove(achievementId)); + Lua::pushBoolean(L, player->achiev()->remove(achievementId)); return 1; } int PlayerFunctions::luaPlayerGetAchievementPoints(lua_State* L) { // player:getAchievementPoints() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } @@ -4285,67 +4656,67 @@ int PlayerFunctions::luaPlayerGetAchievementPoints(lua_State* L) { int PlayerFunctions::luaPlayerAddAchievementPoints(lua_State* L) { // player:addAchievementPoints(amount) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const auto points = getNumber(L, 2); + const auto points = Lua::getNumber(L, 2); if (points > 0) { player->achiev()->addPoints(points); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerRemoveAchievementPoints(lua_State* L) { // player:removeAchievementPoints(amount) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const auto points = getNumber(L, 2); + const auto points = Lua::getNumber(L, 2); if (points > 0) { player->achiev()->removePoints(points); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerAddBadge(lua_State* L) { // player:addBadge(id) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - player->badge()->add(getNumber(L, 2, 0)); - pushBoolean(L, true); + player->badge()->add(Lua::getNumber(L, 2, 0)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerAddTitle(lua_State* L) { // player:addTitle(id) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - player->title()->manage(true, getNumber(L, 2, 0)); - pushBoolean(L, true); + player->title()->manage(true, Lua::getNumber(L, 2, 0)); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerGetTitles(lua_State* L) { // player:getTitles() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } @@ -4355,9 +4726,9 @@ int PlayerFunctions::luaPlayerGetTitles(lua_State* L) { int index = 0; for (const auto &title : playerTitles) { lua_createtable(L, 0, 3); - setField(L, "id", title.first.m_id); - setField(L, "name", player->title()->getNameBySex(player->getSex(), title.first.m_maleName, title.first.m_femaleName)); - setField(L, "description", title.first.m_description); + Lua::setField(L, "id", title.first.m_id); + Lua::setField(L, "name", player->title()->getNameBySex(player->getSex(), title.first.m_maleName, title.first.m_femaleName)); + Lua::setField(L, "description", title.first.m_description); lua_rawseti(L, -2, ++index); } return 1; @@ -4365,101 +4736,101 @@ int PlayerFunctions::luaPlayerGetTitles(lua_State* L) { int PlayerFunctions::luaPlayerSetCurrentTitle(lua_State* L) { // player:setCurrentTitle(id) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const auto &title = g_game().getTitleById(getNumber(L, 2, 0)); + const auto &title = g_game().getTitleById(Lua::getNumber(L, 2, 0)); if (title.m_id == 0) { - reportErrorFunc(getErrorDesc(LUA_ERROR_VARIANT_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_VARIANT_NOT_FOUND)); return 1; } player->title()->setCurrentTitle(title.m_id); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerCreateTransactionSummary(lua_State* L) { // player:createTransactionSummary(type, amount[, id = 0]) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - const auto type = getNumber(L, 2, 0); + const auto type = Lua::getNumber(L, 2, 0); if (type == 0) { - reportErrorFunc(getErrorDesc(LUA_ERROR_VARIANT_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_VARIANT_NOT_FOUND)); return 1; } - const auto amount = getNumber(L, 3, 1); - const auto id = getString(L, 4, ""); + const auto amount = Lua::getNumber(L, 3, 1); + const auto id = Lua::getString(L, 4, ""); player->cyclopedia()->updateStoreSummary(type, amount, id); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerTakeScreenshot(lua_State* L) { // player:takeScreenshot(screenshotType) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto screenshotType = getNumber(L, 2); + const auto screenshotType = Lua::getNumber(L, 2); player->sendTakeScreenshot(screenshotType); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSendIconBakragore(lua_State* L) { // player:sendIconBakragore() - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto iconType = getNumber(L, 2); + const auto iconType = Lua::getNumber(L, 2); player->sendIconBakragore(iconType); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerRemoveIconBakragore(lua_State* L) { // player:removeIconBakragore(iconType or nil for remove all bakragore icons) - const auto &player = getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 1); if (!player) { lua_pushnil(L); return 1; } - const auto iconType = getNumber(L, 2, IconBakragore::None); + const auto iconType = Lua::getNumber(L, 2, IconBakragore::None); if (iconType == IconBakragore::None) { player->removeBakragoreIcons(); } else { player->removeBakragoreIcon(iconType); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PlayerFunctions::luaPlayerSendCreatureAppear(lua_State* L) { - auto player = getUserdataShared(L, 1); + auto player = Lua::getUserdataShared(L, 1); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - bool isLogin = getBoolean(L, 2, false); + bool isLogin = Lua::getBoolean(L, 2, false); player->sendCreatureAppear(player, player->getPosition(), isLogin); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } diff --git a/src/lua/functions/creatures/player/player_functions.hpp b/src/lua/functions/creatures/player/player_functions.hpp index 090e4371abe..8ecb42b83aa 100644 --- a/src/lua/functions/creatures/player/player_functions.hpp +++ b/src/lua/functions/creatures/player/player_functions.hpp @@ -9,7 +9,6 @@ #pragma once -#include "lua/scripts/luascript.hpp" #include "lua/functions/creatures/player/group_functions.hpp" #include "lua/functions/creatures/player/guild_functions.hpp" #include "lua/functions/creatures/player/mount_functions.hpp" @@ -19,381 +18,8 @@ enum class PlayerIcon : uint8_t; enum class IconBakragore : uint8_t; -class PlayerFunctions final : LuaScriptInterface { - explicit PlayerFunctions(lua_State* L) : - LuaScriptInterface("PlayerFunctions") { - init(L); - } - ~PlayerFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Player", "Creature", PlayerFunctions::luaPlayerCreate); - registerMetaMethod(L, "Player", "__eq", PlayerFunctions::luaUserdataCompare); - - registerMethod(L, "Player", "resetCharmsBestiary", PlayerFunctions::luaPlayerResetCharmsMonsters); - registerMethod(L, "Player", "unlockAllCharmRunes", PlayerFunctions::luaPlayerUnlockAllCharmRunes); - registerMethod(L, "Player", "addCharmPoints", PlayerFunctions::luaPlayeraddCharmPoints); - registerMethod(L, "Player", "isPlayer", PlayerFunctions::luaPlayerIsPlayer); - - registerMethod(L, "Player", "getGuid", PlayerFunctions::luaPlayerGetGuid); - registerMethod(L, "Player", "getIp", PlayerFunctions::luaPlayerGetIp); - registerMethod(L, "Player", "getAccountId", PlayerFunctions::luaPlayerGetAccountId); - registerMethod(L, "Player", "getLastLoginSaved", PlayerFunctions::luaPlayerGetLastLoginSaved); - registerMethod(L, "Player", "getLastLogout", PlayerFunctions::luaPlayerGetLastLogout); - - registerMethod(L, "Player", "getAccountType", PlayerFunctions::luaPlayerGetAccountType); - registerMethod(L, "Player", "setAccountType", PlayerFunctions::luaPlayerSetAccountType); - - registerMethod(L, "Player", "isMonsterBestiaryUnlocked", PlayerFunctions::luaPlayerIsMonsterBestiaryUnlocked); - registerMethod(L, "Player", "addBestiaryKill", PlayerFunctions::luaPlayerAddBestiaryKill); - registerMethod(L, "Player", "charmExpansion", PlayerFunctions::luaPlayercharmExpansion); - registerMethod(L, "Player", "getCharmMonsterType", PlayerFunctions::luaPlayergetCharmMonsterType); - - registerMethod(L, "Player", "isMonsterPrey", PlayerFunctions::luaPlayerisMonsterPrey); - registerMethod(L, "Player", "getPreyCards", PlayerFunctions::luaPlayerGetPreyCards); - registerMethod(L, "Player", "getPreyLootPercentage", PlayerFunctions::luaPlayerGetPreyLootPercentage); - registerMethod(L, "Player", "getPreyExperiencePercentage", PlayerFunctions::luaPlayerGetPreyExperiencePercentage); - registerMethod(L, "Player", "preyThirdSlot", PlayerFunctions::luaPlayerPreyThirdSlot); - registerMethod(L, "Player", "taskHuntingThirdSlot", PlayerFunctions::luaPlayerTaskThirdSlot); - registerMethod(L, "Player", "removePreyStamina", PlayerFunctions::luaPlayerRemovePreyStamina); - registerMethod(L, "Player", "addPreyCards", PlayerFunctions::luaPlayerAddPreyCards); - registerMethod(L, "Player", "removeTaskHuntingPoints", PlayerFunctions::luaPlayerRemoveTaskHuntingPoints); - registerMethod(L, "Player", "getTaskHuntingPoints", PlayerFunctions::luaPlayerGetTaskHuntingPoints); - registerMethod(L, "Player", "addTaskHuntingPoints", PlayerFunctions::luaPlayerAddTaskHuntingPoints); - - registerMethod(L, "Player", "getCapacity", PlayerFunctions::luaPlayerGetCapacity); - registerMethod(L, "Player", "setCapacity", PlayerFunctions::luaPlayerSetCapacity); - - registerMethod(L, "Player", "isTraining", PlayerFunctions::luaPlayerGetIsTraining); - registerMethod(L, "Player", "setTraining", PlayerFunctions::luaPlayerSetTraining); - - registerMethod(L, "Player", "getFreeCapacity", PlayerFunctions::luaPlayerGetFreeCapacity); - - registerMethod(L, "Player", "getKills", PlayerFunctions::luaPlayerGetKills); - registerMethod(L, "Player", "setKills", PlayerFunctions::luaPlayerSetKills); - - registerMethod(L, "Player", "getReward", PlayerFunctions::luaPlayerGetReward); - registerMethod(L, "Player", "removeReward", PlayerFunctions::luaPlayerRemoveReward); - registerMethod(L, "Player", "getRewardList", PlayerFunctions::luaPlayerGetRewardList); - - registerMethod(L, "Player", "setDailyReward", PlayerFunctions::luaPlayerSetDailyReward); - - registerMethod(L, "Player", "sendInventory", PlayerFunctions::luaPlayerSendInventory); - registerMethod(L, "Player", "sendLootStats", PlayerFunctions::luaPlayerSendLootStats); - registerMethod(L, "Player", "updateSupplyTracker", PlayerFunctions::luaPlayerUpdateSupplyTracker); - registerMethod(L, "Player", "updateKillTracker", PlayerFunctions::luaPlayerUpdateKillTracker); - - registerMethod(L, "Player", "getDepotLocker", PlayerFunctions::luaPlayerGetDepotLocker); - registerMethod(L, "Player", "getDepotChest", PlayerFunctions::luaPlayerGetDepotChest); - registerMethod(L, "Player", "getInbox", PlayerFunctions::luaPlayerGetInbox); - - registerMethod(L, "Player", "getSkullTime", PlayerFunctions::luaPlayerGetSkullTime); - registerMethod(L, "Player", "setSkullTime", PlayerFunctions::luaPlayerSetSkullTime); - registerMethod(L, "Player", "getDeathPenalty", PlayerFunctions::luaPlayerGetDeathPenalty); - - registerMethod(L, "Player", "getExperience", PlayerFunctions::luaPlayerGetExperience); - registerMethod(L, "Player", "addExperience", PlayerFunctions::luaPlayerAddExperience); - registerMethod(L, "Player", "removeExperience", PlayerFunctions::luaPlayerRemoveExperience); - registerMethod(L, "Player", "getLevel", PlayerFunctions::luaPlayerGetLevel); - - registerMethod(L, "Player", "getMagicShieldCapacityFlat", PlayerFunctions::luaPlayerGetMagicShieldCapacityFlat); - registerMethod(L, "Player", "getMagicShieldCapacityPercent", PlayerFunctions::luaPlayerGetMagicShieldCapacityPercent); - - registerMethod(L, "Player", "sendSpellCooldown", PlayerFunctions::luaPlayerSendSpellCooldown); - registerMethod(L, "Player", "sendSpellGroupCooldown", PlayerFunctions::luaPlayerSendSpellGroupCooldown); - - registerMethod(L, "Player", "getMagicLevel", PlayerFunctions::luaPlayerGetMagicLevel); - registerMethod(L, "Player", "getBaseMagicLevel", PlayerFunctions::luaPlayerGetBaseMagicLevel); - registerMethod(L, "Player", "getMana", PlayerFunctions::luaPlayerGetMana); - registerMethod(L, "Player", "addMana", PlayerFunctions::luaPlayerAddMana); - registerMethod(L, "Player", "getMaxMana", PlayerFunctions::luaPlayerGetMaxMana); - registerMethod(L, "Player", "setMaxMana", PlayerFunctions::luaPlayerSetMaxMana); - registerMethod(L, "Player", "getManaSpent", PlayerFunctions::luaPlayerGetManaSpent); - registerMethod(L, "Player", "addManaSpent", PlayerFunctions::luaPlayerAddManaSpent); - - registerMethod(L, "Player", "getBaseMaxHealth", PlayerFunctions::luaPlayerGetBaseMaxHealth); - registerMethod(L, "Player", "getBaseMaxMana", PlayerFunctions::luaPlayerGetBaseMaxMana); - - registerMethod(L, "Player", "getSkillLevel", PlayerFunctions::luaPlayerGetSkillLevel); - registerMethod(L, "Player", "getEffectiveSkillLevel", PlayerFunctions::luaPlayerGetEffectiveSkillLevel); - registerMethod(L, "Player", "getSkillPercent", PlayerFunctions::luaPlayerGetSkillPercent); - registerMethod(L, "Player", "getSkillTries", PlayerFunctions::luaPlayerGetSkillTries); - registerMethod(L, "Player", "addSkillTries", PlayerFunctions::luaPlayerAddSkillTries); - - registerMethod(L, "Player", "setLevel", PlayerFunctions::luaPlayerSetLevel); - registerMethod(L, "Player", "setMagicLevel", PlayerFunctions::luaPlayerSetMagicLevel); - registerMethod(L, "Player", "setSkillLevel", PlayerFunctions::luaPlayerSetSkillLevel); - - registerMethod(L, "Player", "addOfflineTrainingTime", PlayerFunctions::luaPlayerAddOfflineTrainingTime); - registerMethod(L, "Player", "getOfflineTrainingTime", PlayerFunctions::luaPlayerGetOfflineTrainingTime); - registerMethod(L, "Player", "removeOfflineTrainingTime", PlayerFunctions::luaPlayerRemoveOfflineTrainingTime); - - registerMethod(L, "Player", "addOfflineTrainingTries", PlayerFunctions::luaPlayerAddOfflineTrainingTries); - - registerMethod(L, "Player", "getOfflineTrainingSkill", PlayerFunctions::luaPlayerGetOfflineTrainingSkill); - registerMethod(L, "Player", "setOfflineTrainingSkill", PlayerFunctions::luaPlayerSetOfflineTrainingSkill); - - registerMethod(L, "Player", "getItemCount", PlayerFunctions::luaPlayerGetItemCount); - registerMethod(L, "Player", "getStashItemCount", PlayerFunctions::luaPlayerGetStashItemCount); - registerMethod(L, "Player", "getItemById", PlayerFunctions::luaPlayerGetItemById); - - registerMethod(L, "Player", "getVocation", PlayerFunctions::luaPlayerGetVocation); - registerMethod(L, "Player", "setVocation", PlayerFunctions::luaPlayerSetVocation); - registerMethod(L, "Player", "isPromoted", PlayerFunctions::luaPlayerIsPromoted); - - registerMethod(L, "Player", "getSex", PlayerFunctions::luaPlayerGetSex); - registerMethod(L, "Player", "setSex", PlayerFunctions::luaPlayerSetSex); - - registerMethod(L, "Player", "getPronoun", PlayerFunctions::luaPlayerGetPronoun); - registerMethod(L, "Player", "setPronoun", PlayerFunctions::luaPlayerSetPronoun); - - registerMethod(L, "Player", "getTown", PlayerFunctions::luaPlayerGetTown); - registerMethod(L, "Player", "setTown", PlayerFunctions::luaPlayerSetTown); - - registerMethod(L, "Player", "getGuild", PlayerFunctions::luaPlayerGetGuild); - registerMethod(L, "Player", "setGuild", PlayerFunctions::luaPlayerSetGuild); - - registerMethod(L, "Player", "getGuildLevel", PlayerFunctions::luaPlayerGetGuildLevel); - registerMethod(L, "Player", "setGuildLevel", PlayerFunctions::luaPlayerSetGuildLevel); - - registerMethod(L, "Player", "getGuildNick", PlayerFunctions::luaPlayerGetGuildNick); - registerMethod(L, "Player", "setGuildNick", PlayerFunctions::luaPlayerSetGuildNick); - - registerMethod(L, "Player", "getGroup", PlayerFunctions::luaPlayerGetGroup); - registerMethod(L, "Player", "setGroup", PlayerFunctions::luaPlayerSetGroup); - - registerMethod(L, "Player", "setSpecialContainersAvailable", PlayerFunctions::luaPlayerSetSpecialContainersAvailable); - registerMethod(L, "Player", "getStashCount", PlayerFunctions::luaPlayerGetStashCounter); - registerMethod(L, "Player", "openStash", PlayerFunctions::luaPlayerOpenStash); - - registerMethod(L, "Player", "getStamina", PlayerFunctions::luaPlayerGetStamina); - registerMethod(L, "Player", "setStamina", PlayerFunctions::luaPlayerSetStamina); - - registerMethod(L, "Player", "getSoul", PlayerFunctions::luaPlayerGetSoul); - registerMethod(L, "Player", "addSoul", PlayerFunctions::luaPlayerAddSoul); - registerMethod(L, "Player", "getMaxSoul", PlayerFunctions::luaPlayerGetMaxSoul); - - registerMethod(L, "Player", "getBankBalance", PlayerFunctions::luaPlayerGetBankBalance); - registerMethod(L, "Player", "setBankBalance", PlayerFunctions::luaPlayerSetBankBalance); - - registerMethod(L, "Player", "getStorageValue", PlayerFunctions::luaPlayerGetStorageValue); - registerMethod(L, "Player", "setStorageValue", PlayerFunctions::luaPlayerSetStorageValue); - - registerMethod(L, "Player", "getStorageValueByName", PlayerFunctions::luaPlayerGetStorageValueByName); - registerMethod(L, "Player", "setStorageValueByName", PlayerFunctions::luaPlayerSetStorageValueByName); - - registerMethod(L, "Player", "addItem", PlayerFunctions::luaPlayerAddItem); - registerMethod(L, "Player", "addItemEx", PlayerFunctions::luaPlayerAddItemEx); - registerMethod(L, "Player", "addItemStash", PlayerFunctions::luaPlayerAddItemStash); - registerMethod(L, "Player", "removeStashItem", PlayerFunctions::luaPlayerRemoveStashItem); - registerMethod(L, "Player", "removeItem", PlayerFunctions::luaPlayerRemoveItem); - registerMethod(L, "Player", "sendContainer", PlayerFunctions::luaPlayerSendContainer); - registerMethod(L, "Player", "sendUpdateContainer", PlayerFunctions::luaPlayerSendUpdateContainer); - - registerMethod(L, "Player", "getMoney", PlayerFunctions::luaPlayerGetMoney); - registerMethod(L, "Player", "addMoney", PlayerFunctions::luaPlayerAddMoney); - registerMethod(L, "Player", "removeMoney", PlayerFunctions::luaPlayerRemoveMoney); - - registerMethod(L, "Player", "showTextDialog", PlayerFunctions::luaPlayerShowTextDialog); - - registerMethod(L, "Player", "sendTextMessage", PlayerFunctions::luaPlayerSendTextMessage); - registerMethod(L, "Player", "sendChannelMessage", PlayerFunctions::luaPlayerSendChannelMessage); - registerMethod(L, "Player", "sendPrivateMessage", PlayerFunctions::luaPlayerSendPrivateMessage); - registerMethod(L, "Player", "channelSay", PlayerFunctions::luaPlayerChannelSay); - registerMethod(L, "Player", "openChannel", PlayerFunctions::luaPlayerOpenChannel); - - registerMethod(L, "Player", "getSlotItem", PlayerFunctions::luaPlayerGetSlotItem); - - registerMethod(L, "Player", "getParty", PlayerFunctions::luaPlayerGetParty); - - registerMethod(L, "Player", "addOutfit", PlayerFunctions::luaPlayerAddOutfit); - registerMethod(L, "Player", "addOutfitAddon", PlayerFunctions::luaPlayerAddOutfitAddon); - registerMethod(L, "Player", "removeOutfit", PlayerFunctions::luaPlayerRemoveOutfit); - registerMethod(L, "Player", "removeOutfitAddon", PlayerFunctions::luaPlayerRemoveOutfitAddon); - registerMethod(L, "Player", "hasOutfit", PlayerFunctions::luaPlayerHasOutfit); - registerMethod(L, "Player", "sendOutfitWindow", PlayerFunctions::luaPlayerSendOutfitWindow); - - registerMethod(L, "Player", "addMount", PlayerFunctions::luaPlayerAddMount); - registerMethod(L, "Player", "removeMount", PlayerFunctions::luaPlayerRemoveMount); - registerMethod(L, "Player", "hasMount", PlayerFunctions::luaPlayerHasMount); - - registerMethod(L, "Player", "addFamiliar", PlayerFunctions::luaPlayerAddFamiliar); - registerMethod(L, "Player", "removeFamiliar", PlayerFunctions::luaPlayerRemoveFamiliar); - registerMethod(L, "Player", "hasFamiliar", PlayerFunctions::luaPlayerHasFamiliar); - registerMethod(L, "Player", "setFamiliarLooktype", PlayerFunctions::luaPlayerSetFamiliarLooktype); - registerMethod(L, "Player", "getFamiliarLooktype", PlayerFunctions::luaPlayerGetFamiliarLooktype); - - registerMethod(L, "Player", "getPremiumDays", PlayerFunctions::luaPlayerGetPremiumDays); - registerMethod(L, "Player", "addPremiumDays", PlayerFunctions::luaPlayerAddPremiumDays); - registerMethod(L, "Player", "removePremiumDays", PlayerFunctions::luaPlayerRemovePremiumDays); - - registerMethod(L, "Player", "getTibiaCoins", PlayerFunctions::luaPlayerGetTibiaCoins); - registerMethod(L, "Player", "addTibiaCoins", PlayerFunctions::luaPlayerAddTibiaCoins); - registerMethod(L, "Player", "removeTibiaCoins", PlayerFunctions::luaPlayerRemoveTibiaCoins); - - registerMethod(L, "Player", "getTransferableCoins", PlayerFunctions::luaPlayerGetTransferableCoins); - registerMethod(L, "Player", "addTransferableCoins", PlayerFunctions::luaPlayerAddTransferableCoins); - registerMethod(L, "Player", "removeTransferableCoins", PlayerFunctions::luaPlayerRemoveTransferableCoins); - - registerMethod(L, "Player", "hasBlessing", PlayerFunctions::luaPlayerHasBlessing); - registerMethod(L, "Player", "addBlessing", PlayerFunctions::luaPlayerAddBlessing); - registerMethod(L, "Player", "removeBlessing", PlayerFunctions::luaPlayerRemoveBlessing); - registerMethod(L, "Player", "getBlessingCount", PlayerFunctions::luaPlayerGetBlessingCount); - - registerMethod(L, "Player", "canLearnSpell", PlayerFunctions::luaPlayerCanLearnSpell); - registerMethod(L, "Player", "learnSpell", PlayerFunctions::luaPlayerLearnSpell); - registerMethod(L, "Player", "forgetSpell", PlayerFunctions::luaPlayerForgetSpell); - registerMethod(L, "Player", "hasLearnedSpell", PlayerFunctions::luaPlayerHasLearnedSpell); - - registerMethod(L, "Player", "openImbuementWindow", PlayerFunctions::luaPlayerOpenImbuementWindow); - registerMethod(L, "Player", "closeImbuementWindow", PlayerFunctions::luaPlayerCloseImbuementWindow); - - registerMethod(L, "Player", "sendTutorial", PlayerFunctions::luaPlayerSendTutorial); - registerMethod(L, "Player", "addMapMark", PlayerFunctions::luaPlayerAddMapMark); - - registerMethod(L, "Player", "save", PlayerFunctions::luaPlayerSave); - registerMethod(L, "Player", "popupFYI", PlayerFunctions::luaPlayerPopupFYI); - - registerMethod(L, "Player", "isPzLocked", PlayerFunctions::luaPlayerIsPzLocked); - - registerMethod(L, "Player", "getClient", PlayerFunctions::luaPlayerGetClient); - - registerMethod(L, "Player", "getHouse", PlayerFunctions::luaPlayerGetHouse); - registerMethod(L, "Player", "sendHouseWindow", PlayerFunctions::luaPlayerSendHouseWindow); - registerMethod(L, "Player", "setEditHouse", PlayerFunctions::luaPlayerSetEditHouse); - - registerMethod(L, "Player", "setGhostMode", PlayerFunctions::luaPlayerSetGhostMode); - - registerMethod(L, "Player", "getContainerId", PlayerFunctions::luaPlayerGetContainerId); - registerMethod(L, "Player", "getContainerById", PlayerFunctions::luaPlayerGetContainerById); - registerMethod(L, "Player", "getContainerIndex", PlayerFunctions::luaPlayerGetContainerIndex); - - registerMethod(L, "Player", "getInstantSpells", PlayerFunctions::luaPlayerGetInstantSpells); - registerMethod(L, "Player", "canCast", PlayerFunctions::luaPlayerCanCast); - - registerMethod(L, "Player", "hasChaseMode", PlayerFunctions::luaPlayerHasChaseMode); - registerMethod(L, "Player", "hasSecureMode", PlayerFunctions::luaPlayerHasSecureMode); - registerMethod(L, "Player", "getFightMode", PlayerFunctions::luaPlayerGetFightMode); - - registerMethod(L, "Player", "getBaseXpGain", PlayerFunctions::luaPlayerGetBaseXpGain); - registerMethod(L, "Player", "setBaseXpGain", PlayerFunctions::luaPlayerSetBaseXpGain); - registerMethod(L, "Player", "getVoucherXpBoost", PlayerFunctions::luaPlayerGetVoucherXpBoost); - registerMethod(L, "Player", "setVoucherXpBoost", PlayerFunctions::luaPlayerSetVoucherXpBoost); - registerMethod(L, "Player", "getGrindingXpBoost", PlayerFunctions::luaPlayerGetGrindingXpBoost); - registerMethod(L, "Player", "setGrindingXpBoost", PlayerFunctions::luaPlayerSetGrindingXpBoost); - registerMethod(L, "Player", "getXpBoostPercent", PlayerFunctions::luaPlayerGetXpBoostPercent); - registerMethod(L, "Player", "setXpBoostPercent", PlayerFunctions::luaPlayerSetXpBoostPercent); - registerMethod(L, "Player", "getStaminaXpBoost", PlayerFunctions::luaPlayerGetStaminaXpBoost); - registerMethod(L, "Player", "setStaminaXpBoost", PlayerFunctions::luaPlayerSetStaminaXpBoost); - registerMethod(L, "Player", "getXpBoostTime", PlayerFunctions::luaPlayerGetXpBoostTime); - registerMethod(L, "Player", "setXpBoostTime", PlayerFunctions::luaPlayerSetXpBoostTime); - - registerMethod(L, "Player", "getIdleTime", PlayerFunctions::luaPlayerGetIdleTime); - registerMethod(L, "Player", "getFreeBackpackSlots", PlayerFunctions::luaPlayerGetFreeBackpackSlots); - - registerMethod(L, "Player", "isOffline", PlayerFunctions::luaPlayerIsOffline); - - registerMethod(L, "Player", "openMarket", PlayerFunctions::luaPlayerOpenMarket); - - registerMethod(L, "Player", "instantSkillWOD", PlayerFunctions::luaPlayerInstantSkillWOD); - registerMethod(L, "Player", "upgradeSpellsWOD", PlayerFunctions::luaPlayerUpgradeSpellWOD); - registerMethod(L, "Player", "revelationStageWOD", PlayerFunctions::luaPlayerRevelationStageWOD); - registerMethod(L, "Player", "reloadData", PlayerFunctions::luaPlayerReloadData); - registerMethod(L, "Player", "onThinkWheelOfDestiny", PlayerFunctions::luaPlayerOnThinkWheelOfDestiny); - registerMethod(L, "Player", "avatarTimer", PlayerFunctions::luaPlayerAvatarTimer); - registerMethod(L, "Player", "getWheelSpellAdditionalArea", PlayerFunctions::luaPlayerGetWheelSpellAdditionalArea); - registerMethod(L, "Player", "getWheelSpellAdditionalTarget", PlayerFunctions::luaPlayerGetWheelSpellAdditionalTarget); - registerMethod(L, "Player", "getWheelSpellAdditionalDuration", PlayerFunctions::luaPlayerGetWheelSpellAdditionalDuration); - - // Forge Functions - registerMethod(L, "Player", "openForge", PlayerFunctions::luaPlayerOpenForge); - registerMethod(L, "Player", "closeForge", PlayerFunctions::luaPlayerCloseForge); - - registerMethod(L, "Player", "addForgeDusts", PlayerFunctions::luaPlayerAddForgeDusts); - registerMethod(L, "Player", "removeForgeDusts", PlayerFunctions::luaPlayerRemoveForgeDusts); - registerMethod(L, "Player", "getForgeDusts", PlayerFunctions::luaPlayerGetForgeDusts); - registerMethod(L, "Player", "setForgeDusts", PlayerFunctions::luaPlayerSetForgeDusts); - - registerMethod(L, "Player", "addForgeDustLevel", PlayerFunctions::luaPlayerAddForgeDustLevel); - registerMethod(L, "Player", "removeForgeDustLevel", PlayerFunctions::luaPlayerRemoveForgeDustLevel); - registerMethod(L, "Player", "getForgeDustLevel", PlayerFunctions::luaPlayerGetForgeDustLevel); - - registerMethod(L, "Player", "getForgeSlivers", PlayerFunctions::luaPlayerGetForgeSlivers); - registerMethod(L, "Player", "getForgeCores", PlayerFunctions::luaPlayerGetForgeCores); - registerMethod(L, "Player", "isUIExhausted", PlayerFunctions::luaPlayerIsUIExhausted); - registerMethod(L, "Player", "updateUIExhausted", PlayerFunctions::luaPlayerUpdateUIExhausted); - - registerMethod(L, "Player", "setFaction", PlayerFunctions::luaPlayerSetFaction); - registerMethod(L, "Player", "getFaction", PlayerFunctions::luaPlayerGetFaction); - - // Bosstiary Functions - registerMethod(L, "Player", "getBosstiaryLevel", PlayerFunctions::luaPlayerGetBosstiaryLevel); - registerMethod(L, "Player", "getBosstiaryKills", PlayerFunctions::luaPlayerGetBosstiaryKills); - registerMethod(L, "Player", "addBosstiaryKill", PlayerFunctions::luaPlayerAddBosstiaryKill); - registerMethod(L, "Player", "setBossPoints", PlayerFunctions::luaPlayerSetBossPoints); - registerMethod(L, "Player", "setRemoveBossTime", PlayerFunctions::luaPlayerSetRemoveBossTime); - registerMethod(L, "Player", "getSlotBossId", PlayerFunctions::luaPlayerGetSlotBossId); - registerMethod(L, "Player", "getBossBonus", PlayerFunctions::luaPlayerGetBossBonus); - registerMethod(L, "Player", "sendBosstiaryCooldownTimer", PlayerFunctions::luaPlayerBosstiaryCooldownTimer); - - registerMethod(L, "Player", "sendSingleSoundEffect", PlayerFunctions::luaPlayerSendSingleSoundEffect); - registerMethod(L, "Player", "sendDoubleSoundEffect", PlayerFunctions::luaPlayerSendDoubleSoundEffect); - - registerMethod(L, "Player", "getName", PlayerFunctions::luaPlayerGetName); - registerMethod(L, "Player", "changeName", PlayerFunctions::luaPlayerChangeName); - - registerMethod(L, "Player", "hasGroupFlag", PlayerFunctions::luaPlayerHasGroupFlag); - registerMethod(L, "Player", "setGroupFlag", PlayerFunctions::luaPlayerSetGroupFlag); - registerMethod(L, "Player", "removeGroupFlag", PlayerFunctions::luaPlayerRemoveGroupFlag); - - registerMethod(L, "Player", "setHazardSystemPoints", PlayerFunctions::luaPlayerAddHazardSystemPoints); - registerMethod(L, "Player", "getHazardSystemPoints", PlayerFunctions::luaPlayerGetHazardSystemPoints); - - registerMethod(L, "Player", "setLoyaltyBonus", PlayerFunctions::luaPlayerSetLoyaltyBonus); - registerMethod(L, "Player", "getLoyaltyBonus", PlayerFunctions::luaPlayerGetLoyaltyBonus); - registerMethod(L, "Player", "getLoyaltyPoints", PlayerFunctions::luaPlayerGetLoyaltyPoints); - registerMethod(L, "Player", "getLoyaltyTitle", PlayerFunctions::luaPlayerGetLoyaltyTitle); - registerMethod(L, "Player", "setLoyaltyTitle", PlayerFunctions::luaPlayerSetLoyaltyTitle); - - registerMethod(L, "Player", "updateConcoction", PlayerFunctions::luaPlayerUpdateConcoction); - registerMethod(L, "Player", "clearSpellCooldowns", PlayerFunctions::luaPlayerClearSpellCooldowns); - - registerMethod(L, "Player", "isVip", PlayerFunctions::luaPlayerIsVip); - registerMethod(L, "Player", "getVipDays", PlayerFunctions::luaPlayerGetVipDays); - registerMethod(L, "Player", "getVipTime", PlayerFunctions::luaPlayerGetVipTime); - - registerMethod(L, "Player", "kv", PlayerFunctions::luaPlayerKV); - registerMethod(L, "Player", "getStoreInbox", PlayerFunctions::luaPlayerGetStoreInbox); - - registerMethod(L, "Player", "hasAchievement", PlayerFunctions::luaPlayerHasAchievement); - registerMethod(L, "Player", "addAchievement", PlayerFunctions::luaPlayerAddAchievement); - registerMethod(L, "Player", "removeAchievement", PlayerFunctions::luaPlayerRemoveAchievement); - registerMethod(L, "Player", "getAchievementPoints", PlayerFunctions::luaPlayerGetAchievementPoints); - registerMethod(L, "Player", "addAchievementPoints", PlayerFunctions::luaPlayerAddAchievementPoints); - registerMethod(L, "Player", "removeAchievementPoints", PlayerFunctions::luaPlayerRemoveAchievementPoints); - - // Badge Functions - registerMethod(L, "Player", "addBadge", PlayerFunctions::luaPlayerAddBadge); - - // Title Functions - registerMethod(L, "Player", "addTitle", PlayerFunctions::luaPlayerAddTitle); - registerMethod(L, "Player", "getTitles", PlayerFunctions::luaPlayerGetTitles); - registerMethod(L, "Player", "setCurrentTitle", PlayerFunctions::luaPlayerSetCurrentTitle); - - // Store Summary - registerMethod(L, "Player", "createTransactionSummary", PlayerFunctions::luaPlayerCreateTransactionSummary); - - registerMethod(L, "Player", "takeScreenshot", PlayerFunctions::luaPlayerTakeScreenshot); - registerMethod(L, "Player", "sendIconBakragore", PlayerFunctions::luaPlayerSendIconBakragore); - registerMethod(L, "Player", "removeIconBakragore", PlayerFunctions::luaPlayerRemoveIconBakragore); - registerMethod(L, "Player", "sendCreatureAppear", PlayerFunctions::luaPlayerSendCreatureAppear); - - GroupFunctions::init(L); - GuildFunctions::init(L); - MountFunctions::init(L); - PartyFunctions::init(L); - VocationFunctions::init(L); - } +class PlayerFunctions { + static void init(lua_State* L); static int luaPlayerCreate(lua_State* L); diff --git a/src/lua/functions/creatures/player/vocation_functions.cpp b/src/lua/functions/creatures/player/vocation_functions.cpp index 59b2a0d87dc..166e000f433 100644 --- a/src/lua/functions/creatures/player/vocation_functions.cpp +++ b/src/lua/functions/creatures/player/vocation_functions.cpp @@ -10,20 +10,55 @@ #include "lua/functions/creatures/player/vocation_functions.hpp" #include "creatures/players/vocations/vocation.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void VocationFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Vocation", "", VocationFunctions::luaVocationCreate); + Lua::registerMetaMethod(L, "Vocation", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Vocation", "getId", VocationFunctions::luaVocationGetId); + Lua::registerMethod(L, "Vocation", "getClientId", VocationFunctions::luaVocationGetClientId); + Lua::registerMethod(L, "Vocation", "getBaseId", VocationFunctions::luaVocationGetBaseId); + Lua::registerMethod(L, "Vocation", "getName", VocationFunctions::luaVocationGetName); + Lua::registerMethod(L, "Vocation", "getDescription", VocationFunctions::luaVocationGetDescription); + + Lua::registerMethod(L, "Vocation", "getRequiredSkillTries", VocationFunctions::luaVocationGetRequiredSkillTries); + Lua::registerMethod(L, "Vocation", "getRequiredManaSpent", VocationFunctions::luaVocationGetRequiredManaSpent); + + Lua::registerMethod(L, "Vocation", "getCapacityGain", VocationFunctions::luaVocationGetCapacityGain); + + Lua::registerMethod(L, "Vocation", "getHealthGain", VocationFunctions::luaVocationGetHealthGain); + Lua::registerMethod(L, "Vocation", "getHealthGainTicks", VocationFunctions::luaVocationGetHealthGainTicks); + Lua::registerMethod(L, "Vocation", "getHealthGainAmount", VocationFunctions::luaVocationGetHealthGainAmount); + + Lua::registerMethod(L, "Vocation", "getManaGain", VocationFunctions::luaVocationGetManaGain); + Lua::registerMethod(L, "Vocation", "getManaGainTicks", VocationFunctions::luaVocationGetManaGainTicks); + Lua::registerMethod(L, "Vocation", "getManaGainAmount", VocationFunctions::luaVocationGetManaGainAmount); + + Lua::registerMethod(L, "Vocation", "getMaxSoul", VocationFunctions::luaVocationGetMaxSoul); + Lua::registerMethod(L, "Vocation", "getSoulGainTicks", VocationFunctions::luaVocationGetSoulGainTicks); + + Lua::registerMethod(L, "Vocation", "getBaseAttackSpeed", VocationFunctions::luaVocationGetBaseAttackSpeed); + Lua::registerMethod(L, "Vocation", "getAttackSpeed", VocationFunctions::luaVocationGetAttackSpeed); + Lua::registerMethod(L, "Vocation", "getBaseSpeed", VocationFunctions::luaVocationGetBaseSpeed); + + Lua::registerMethod(L, "Vocation", "getDemotion", VocationFunctions::luaVocationGetDemotion); + Lua::registerMethod(L, "Vocation", "getPromotion", VocationFunctions::luaVocationGetPromotion); +} int VocationFunctions::luaVocationCreate(lua_State* L) { // Vocation(id or name) uint16_t vocationId; - if (isNumber(L, 2)) { - vocationId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + vocationId = Lua::getNumber(L, 2); } else { - vocationId = g_vocations().getVocationId(getString(L, 2)); + vocationId = g_vocations().getVocationId(Lua::getString(L, 2)); } const auto &vocation = g_vocations().getVocation(vocationId); if (vocation) { - pushUserdata(L, vocation); - setMetatable(L, -1, "Vocation"); + Lua::pushUserdata(L, vocation); + Lua::setMetatable(L, -1, "Vocation"); } else { lua_pushnil(L); } @@ -32,7 +67,7 @@ int VocationFunctions::luaVocationCreate(lua_State* L) { int VocationFunctions::luaVocationGetId(lua_State* L) { // vocation:getId() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getId()); } else { @@ -43,7 +78,7 @@ int VocationFunctions::luaVocationGetId(lua_State* L) { int VocationFunctions::luaVocationGetClientId(lua_State* L) { // vocation:getClientId() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getClientId()); } else { @@ -54,7 +89,7 @@ int VocationFunctions::luaVocationGetClientId(lua_State* L) { int VocationFunctions::luaVocationGetBaseId(lua_State* L) { // vocation:getBaseId() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getBaseId()); } else { @@ -65,9 +100,9 @@ int VocationFunctions::luaVocationGetBaseId(lua_State* L) { int VocationFunctions::luaVocationGetName(lua_State* L) { // vocation:getName() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { - pushString(L, vocation->getVocName()); + Lua::pushString(L, vocation->getVocName()); } else { lua_pushnil(L); } @@ -76,9 +111,9 @@ int VocationFunctions::luaVocationGetName(lua_State* L) { int VocationFunctions::luaVocationGetDescription(lua_State* L) { // vocation:getDescription() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { - pushString(L, vocation->getVocDescription()); + Lua::pushString(L, vocation->getVocDescription()); } else { lua_pushnil(L); } @@ -87,10 +122,10 @@ int VocationFunctions::luaVocationGetDescription(lua_State* L) { int VocationFunctions::luaVocationGetRequiredSkillTries(lua_State* L) { // vocation:getRequiredSkillTries(skillType, skillLevel) - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { - const skills_t skillType = getNumber(L, 2); - const uint16_t skillLevel = getNumber(L, 3); + const skills_t skillType = Lua::getNumber(L, 2); + const uint16_t skillLevel = Lua::getNumber(L, 3); lua_pushnumber(L, vocation->getReqSkillTries(skillType, skillLevel)); } else { lua_pushnil(L); @@ -100,9 +135,9 @@ int VocationFunctions::luaVocationGetRequiredSkillTries(lua_State* L) { int VocationFunctions::luaVocationGetRequiredManaSpent(lua_State* L) { // vocation:getRequiredManaSpent(magicLevel) - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { - const uint32_t magicLevel = getNumber(L, 2); + const uint32_t magicLevel = Lua::getNumber(L, 2); lua_pushnumber(L, vocation->getReqMana(magicLevel)); } else { lua_pushnil(L); @@ -112,7 +147,7 @@ int VocationFunctions::luaVocationGetRequiredManaSpent(lua_State* L) { int VocationFunctions::luaVocationGetCapacityGain(lua_State* L) { // vocation:getCapacityGain() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getCapGain()); } else { @@ -123,7 +158,7 @@ int VocationFunctions::luaVocationGetCapacityGain(lua_State* L) { int VocationFunctions::luaVocationGetHealthGain(lua_State* L) { // vocation:getHealthGain() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getHPGain()); } else { @@ -134,7 +169,7 @@ int VocationFunctions::luaVocationGetHealthGain(lua_State* L) { int VocationFunctions::luaVocationGetHealthGainTicks(lua_State* L) { // vocation:getHealthGainTicks() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getHealthGainTicks()); } else { @@ -145,7 +180,7 @@ int VocationFunctions::luaVocationGetHealthGainTicks(lua_State* L) { int VocationFunctions::luaVocationGetHealthGainAmount(lua_State* L) { // vocation:getHealthGainAmount() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getHealthGainAmount()); } else { @@ -156,7 +191,7 @@ int VocationFunctions::luaVocationGetHealthGainAmount(lua_State* L) { int VocationFunctions::luaVocationGetManaGain(lua_State* L) { // vocation:getManaGain() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getManaGain()); } else { @@ -167,7 +202,7 @@ int VocationFunctions::luaVocationGetManaGain(lua_State* L) { int VocationFunctions::luaVocationGetManaGainTicks(lua_State* L) { // vocation:getManaGainTicks() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getManaGainTicks()); } else { @@ -178,7 +213,7 @@ int VocationFunctions::luaVocationGetManaGainTicks(lua_State* L) { int VocationFunctions::luaVocationGetManaGainAmount(lua_State* L) { // vocation:getManaGainAmount() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getManaGainAmount()); } else { @@ -189,7 +224,7 @@ int VocationFunctions::luaVocationGetManaGainAmount(lua_State* L) { int VocationFunctions::luaVocationGetMaxSoul(lua_State* L) { // vocation:getMaxSoul() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getSoulMax()); } else { @@ -200,7 +235,7 @@ int VocationFunctions::luaVocationGetMaxSoul(lua_State* L) { int VocationFunctions::luaVocationGetSoulGainTicks(lua_State* L) { // vocation:getSoulGainTicks() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getSoulGainTicks()); } else { @@ -211,7 +246,7 @@ int VocationFunctions::luaVocationGetSoulGainTicks(lua_State* L) { int VocationFunctions::luaVocationGetBaseAttackSpeed(lua_State* L) { // vocation:getBaseAttackSpeed() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getBaseAttackSpeed()); } else { @@ -222,7 +257,7 @@ int VocationFunctions::luaVocationGetBaseAttackSpeed(lua_State* L) { int VocationFunctions::luaVocationGetAttackSpeed(lua_State* L) { // vocation:getAttackSpeed() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getAttackSpeed()); } else { @@ -233,7 +268,7 @@ int VocationFunctions::luaVocationGetAttackSpeed(lua_State* L) { int VocationFunctions::luaVocationGetBaseSpeed(lua_State* L) { // vocation:getBaseSpeed() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (vocation) { lua_pushnumber(L, vocation->getBaseSpeed()); } else { @@ -244,7 +279,7 @@ int VocationFunctions::luaVocationGetBaseSpeed(lua_State* L) { int VocationFunctions::luaVocationGetDemotion(lua_State* L) { // vocation:getDemotion() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (!vocation) { lua_pushnil(L); return 1; @@ -258,8 +293,8 @@ int VocationFunctions::luaVocationGetDemotion(lua_State* L) { const auto &demotedVocation = g_vocations().getVocation(fromId); if (demotedVocation && demotedVocation != vocation) { - pushUserdata(L, demotedVocation); - setMetatable(L, -1, "Vocation"); + Lua::pushUserdata(L, demotedVocation); + Lua::setMetatable(L, -1, "Vocation"); } else { lua_pushnil(L); } @@ -268,7 +303,7 @@ int VocationFunctions::luaVocationGetDemotion(lua_State* L) { int VocationFunctions::luaVocationGetPromotion(lua_State* L) { // vocation:getPromotion() - const auto &vocation = getUserdataShared(L, 1); + const auto &vocation = Lua::getUserdataShared(L, 1); if (!vocation) { lua_pushnil(L); return 1; @@ -282,8 +317,8 @@ int VocationFunctions::luaVocationGetPromotion(lua_State* L) { const auto &promotedVocation = g_vocations().getVocation(promotedId); if (promotedVocation && promotedVocation != vocation) { - pushUserdata(L, promotedVocation); - setMetatable(L, -1, "Vocation"); + Lua::pushUserdata(L, promotedVocation); + Lua::setMetatable(L, -1, "Vocation"); } else { lua_pushnil(L); } diff --git a/src/lua/functions/creatures/player/vocation_functions.hpp b/src/lua/functions/creatures/player/vocation_functions.hpp index 7d98131ecb3..10c9364680d 100644 --- a/src/lua/functions/creatures/player/vocation_functions.hpp +++ b/src/lua/functions/creatures/player/vocation_functions.hpp @@ -9,49 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class VocationFunctions final : LuaScriptInterface { +class VocationFunctions { public: - explicit VocationFunctions(lua_State* L) : - LuaScriptInterface("VocationFunctions") { - init(L); - } - ~VocationFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Vocation", "", VocationFunctions::luaVocationCreate); - registerMetaMethod(L, "Vocation", "__eq", VocationFunctions::luaUserdataCompare); - - registerMethod(L, "Vocation", "getId", VocationFunctions::luaVocationGetId); - registerMethod(L, "Vocation", "getClientId", VocationFunctions::luaVocationGetClientId); - registerMethod(L, "Vocation", "getBaseId", VocationFunctions::luaVocationGetBaseId); - registerMethod(L, "Vocation", "getName", VocationFunctions::luaVocationGetName); - registerMethod(L, "Vocation", "getDescription", VocationFunctions::luaVocationGetDescription); - - registerMethod(L, "Vocation", "getRequiredSkillTries", VocationFunctions::luaVocationGetRequiredSkillTries); - registerMethod(L, "Vocation", "getRequiredManaSpent", VocationFunctions::luaVocationGetRequiredManaSpent); - - registerMethod(L, "Vocation", "getCapacityGain", VocationFunctions::luaVocationGetCapacityGain); - - registerMethod(L, "Vocation", "getHealthGain", VocationFunctions::luaVocationGetHealthGain); - registerMethod(L, "Vocation", "getHealthGainTicks", VocationFunctions::luaVocationGetHealthGainTicks); - registerMethod(L, "Vocation", "getHealthGainAmount", VocationFunctions::luaVocationGetHealthGainAmount); - - registerMethod(L, "Vocation", "getManaGain", VocationFunctions::luaVocationGetManaGain); - registerMethod(L, "Vocation", "getManaGainTicks", VocationFunctions::luaVocationGetManaGainTicks); - registerMethod(L, "Vocation", "getManaGainAmount", VocationFunctions::luaVocationGetManaGainAmount); - - registerMethod(L, "Vocation", "getMaxSoul", VocationFunctions::luaVocationGetMaxSoul); - registerMethod(L, "Vocation", "getSoulGainTicks", VocationFunctions::luaVocationGetSoulGainTicks); - - registerMethod(L, "Vocation", "getBaseAttackSpeed", VocationFunctions::luaVocationGetBaseAttackSpeed); - registerMethod(L, "Vocation", "getAttackSpeed", VocationFunctions::luaVocationGetAttackSpeed); - registerMethod(L, "Vocation", "getBaseSpeed", VocationFunctions::luaVocationGetBaseSpeed); - - registerMethod(L, "Vocation", "getDemotion", VocationFunctions::luaVocationGetDemotion); - registerMethod(L, "Vocation", "getPromotion", VocationFunctions::luaVocationGetPromotion); - } + static void init(lua_State* L); private: static int luaVocationCreate(lua_State* L); diff --git a/src/lua/functions/events/action_functions.cpp b/src/lua/functions/events/action_functions.cpp index a21ab37ebd9..39ca0939150 100644 --- a/src/lua/functions/events/action_functions.cpp +++ b/src/lua/functions/events/action_functions.cpp @@ -12,105 +12,119 @@ #include "lua/creature/actions.hpp" #include "game/game.hpp" #include "items/item.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ActionFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Action", "", ActionFunctions::luaCreateAction); + Lua::registerMethod(L, "Action", "onUse", ActionFunctions::luaActionOnUse); + Lua::registerMethod(L, "Action", "register", ActionFunctions::luaActionRegister); + Lua::registerMethod(L, "Action", "id", ActionFunctions::luaActionItemId); + Lua::registerMethod(L, "Action", "aid", ActionFunctions::luaActionActionId); + Lua::registerMethod(L, "Action", "uid", ActionFunctions::luaActionUniqueId); + Lua::registerMethod(L, "Action", "position", ActionFunctions::luaActionPosition); + Lua::registerMethod(L, "Action", "allowFarUse", ActionFunctions::luaActionAllowFarUse); + Lua::registerMethod(L, "Action", "blockWalls", ActionFunctions::luaActionBlockWalls); + Lua::registerMethod(L, "Action", "checkFloor", ActionFunctions::luaActionCheckFloor); + Lua::registerMethod(L, "Action", "position", ActionFunctions::luaActionPosition); +} int ActionFunctions::luaCreateAction(lua_State* L) { // Action() - const auto action = std::make_shared(getScriptEnv()->getScriptInterface()); - pushUserdata(L, action); - setMetatable(L, -1, "Action"); + const auto action = std::make_shared(); + Lua::pushUserdata(L, action); + Lua::setMetatable(L, -1, "Action"); return 1; } int ActionFunctions::luaActionOnUse(lua_State* L) { // action:onUse(callback) - const auto &action = getUserdataShared(L, 1); + const auto &action = Lua::getUserdataShared(L, 1); if (action) { - if (!action->loadCallback()) { - pushBoolean(L, false); + if (!action->loadScriptId()) { + Lua::pushBoolean(L, false); return 1; } - action->setLoadedCallback(true); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } int ActionFunctions::luaActionRegister(lua_State* L) { // action:register() - const auto &action = getUserdataShared(L, 1); + const auto &action = Lua::getUserdataShared(L, 1); if (action) { - if (!action->isLoadedCallback()) { - pushBoolean(L, false); + if (!action->isLoadedScriptId()) { + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, g_actions().registerLuaEvent(action)); - pushBoolean(L, true); + Lua::pushBoolean(L, g_actions().registerLuaEvent(action)); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } int ActionFunctions::luaActionItemId(lua_State* L) { // action:id(ids) - const auto &action = getUserdataShared(L, 1); + const auto &action = Lua::getUserdataShared(L, 1); if (action) { const int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc if (parameters > 1) { for (int i = 0; i < parameters; ++i) { - action->setItemIdsVector(getNumber(L, 2 + i)); + action->setItemIdsVector(Lua::getNumber(L, 2 + i)); } } else { - action->setItemIdsVector(getNumber(L, 2)); + action->setItemIdsVector(Lua::getNumber(L, 2)); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } int ActionFunctions::luaActionActionId(lua_State* L) { // action:aid(aids) - const auto &action = getUserdataShared(L, 1); + const auto &action = Lua::getUserdataShared(L, 1); if (action) { const int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc if (parameters > 1) { for (int i = 0; i < parameters; ++i) { - action->setActionIdsVector(getNumber(L, 2 + i)); + action->setActionIdsVector(Lua::getNumber(L, 2 + i)); } } else { - action->setActionIdsVector(getNumber(L, 2)); + action->setActionIdsVector(Lua::getNumber(L, 2)); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } int ActionFunctions::luaActionUniqueId(lua_State* L) { // action:uid(uids) - const auto &action = getUserdataShared(L, 1); + const auto &action = Lua::getUserdataShared(L, 1); if (action) { const int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc if (parameters > 1) { for (int i = 0; i < parameters; ++i) { - action->setUniqueIdsVector(getNumber(L, 2 + i)); + action->setUniqueIdsVector(Lua::getNumber(L, 2 + i)); } } else { - action->setUniqueIdsVector(getNumber(L, 2)); + action->setUniqueIdsVector(Lua::getNumber(L, 2)); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } @@ -121,20 +135,20 @@ int ActionFunctions::luaActionPosition(lua_State* L) { * @param itemId or @param itemName = if item id or string name is set, the item is created on position (if not exists), this variable is nil by default * action:position(positions, itemId or name) */ - const auto &action = getUserdataShared(L, 1); + const auto &action = Lua::getUserdataShared(L, 1); if (!action) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const Position position = getPosition(L, 2); + const Position position = Lua::getPosition(L, 2); // The parameter "- 1" because self is a parameter aswell, which we want to skip L 1 (UserData) - // isNumber(L, 2) is for skip the itemId + // Lua::isNumber(L, 2) is for skip the itemId if (const int parameters = lua_gettop(L) - 1; - parameters > 1 && isNumber(L, 2)) { + parameters > 1 && Lua::isNumber(L, 2)) { for (int i = 0; i < parameters; ++i) { - action->setPositionsVector(getPosition(L, 2 + i)); + action->setPositionsVector(Lua::getPosition(L, 2 + i)); } } else { action->setPositionsVector(position); @@ -142,14 +156,14 @@ int ActionFunctions::luaActionPosition(lua_State* L) { uint16_t itemId; bool createItem = false; - if (isNumber(L, 3)) { - itemId = getNumber(L, 3); + if (Lua::isNumber(L, 3)) { + itemId = Lua::getNumber(L, 3); createItem = true; - } else if (isString(L, 3)) { - itemId = Item::items.getItemIdByName(getString(L, 3)); + } else if (Lua::isString(L, 3)) { + itemId = Item::items.getItemIdByName(Lua::getString(L, 3)); if (itemId == 0) { - reportErrorFunc("Not found item with name: " + getString(L, 3)); - pushBoolean(L, false); + Lua::reportErrorFunc("Not found item with name: " + Lua::getString(L, 3)); + Lua::pushBoolean(L, false); return 1; } @@ -158,8 +172,8 @@ int ActionFunctions::luaActionPosition(lua_State* L) { if (createItem) { if (!Item::items.hasItemType(itemId)) { - reportErrorFunc("Not found item with id: " + itemId); - pushBoolean(L, false); + Lua::reportErrorFunc("Not found item with id: " + itemId); + Lua::pushBoolean(L, false); return 1; } @@ -172,45 +186,45 @@ int ActionFunctions::luaActionPosition(lua_State* L) { g_game().setCreateLuaItems(position, itemId); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int ActionFunctions::luaActionAllowFarUse(lua_State* L) { // action:allowFarUse(bool) - const auto &action = getUserdataShared(L, 1); + const auto &action = Lua::getUserdataShared(L, 1); if (action) { - action->setAllowFarUse(getBoolean(L, 2)); - pushBoolean(L, true); + action->setAllowFarUse(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } int ActionFunctions::luaActionBlockWalls(lua_State* L) { // action:blockWalls(bool) - const auto &action = getUserdataShared(L, 1); + const auto &action = Lua::getUserdataShared(L, 1); if (action) { - action->setCheckLineOfSight(getBoolean(L, 2)); - pushBoolean(L, true); + action->setCheckLineOfSight(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } int ActionFunctions::luaActionCheckFloor(lua_State* L) { // action:checkFloor(bool) - const auto &action = getUserdataShared(L, 1); + const auto &action = Lua::getUserdataShared(L, 1); if (action) { - action->setCheckFloor(getBoolean(L, 2)); - pushBoolean(L, true); + action->setCheckFloor(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } else { - reportErrorFunc(getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); } return 1; } diff --git a/src/lua/functions/events/action_functions.hpp b/src/lua/functions/events/action_functions.hpp index 0816ca9d334..e72b65bf726 100644 --- a/src/lua/functions/events/action_functions.hpp +++ b/src/lua/functions/events/action_functions.hpp @@ -9,29 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class ActionFunctions final : LuaScriptInterface { +class ActionFunctions { public: - explicit ActionFunctions(lua_State* L) : - LuaScriptInterface("ActionFunctions") { - init(L); - } - ~ActionFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Action", "", ActionFunctions::luaCreateAction); - registerMethod(L, "Action", "onUse", ActionFunctions::luaActionOnUse); - registerMethod(L, "Action", "register", ActionFunctions::luaActionRegister); - registerMethod(L, "Action", "id", ActionFunctions::luaActionItemId); - registerMethod(L, "Action", "aid", ActionFunctions::luaActionActionId); - registerMethod(L, "Action", "uid", ActionFunctions::luaActionUniqueId); - registerMethod(L, "Action", "position", ActionFunctions::luaActionPosition); - registerMethod(L, "Action", "allowFarUse", ActionFunctions::luaActionAllowFarUse); - registerMethod(L, "Action", "blockWalls", ActionFunctions::luaActionBlockWalls); - registerMethod(L, "Action", "checkFloor", ActionFunctions::luaActionCheckFloor); - registerMethod(L, "Action", "position", ActionFunctions::luaActionPosition); - } + static void init(lua_State* L); private: static int luaCreateAction(lua_State* L); diff --git a/src/lua/functions/events/creature_event_functions.cpp b/src/lua/functions/events/creature_event_functions.cpp index ea4c05758d5..6f67375d3eb 100644 --- a/src/lua/functions/events/creature_event_functions.cpp +++ b/src/lua/functions/events/creature_event_functions.cpp @@ -11,21 +11,40 @@ #include "lua/creature/creatureevent.hpp" #include "utils/tools.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void CreatureEventFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "CreatureEvent", "", CreatureEventFunctions::luaCreateCreatureEvent); + Lua::registerMethod(L, "CreatureEvent", "type", CreatureEventFunctions::luaCreatureEventType); + Lua::registerMethod(L, "CreatureEvent", "register", CreatureEventFunctions::luaCreatureEventRegister); + Lua::registerMethod(L, "CreatureEvent", "onLogin", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onLogout", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onThink", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onPrepareDeath", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onDeath", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onKill", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onAdvance", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onModalWindow", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onTextEdit", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onHealthChange", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onManaChange", CreatureEventFunctions::luaCreatureEventOnCallback); + Lua::registerMethod(L, "CreatureEvent", "onExtendedOpcode", CreatureEventFunctions::luaCreatureEventOnCallback); +} int CreatureEventFunctions::luaCreateCreatureEvent(lua_State* L) { // CreatureEvent(eventName) - const auto creatureEvent = std::make_shared(getScriptEnv()->getScriptInterface()); - creatureEvent->setName(getString(L, 2)); - pushUserdata(L, creatureEvent); - setMetatable(L, -1, "CreatureEvent"); + const auto creatureEvent = std::make_shared(); + creatureEvent->setName(Lua::getString(L, 2)); + Lua::pushUserdata(L, creatureEvent); + Lua::setMetatable(L, -1, "CreatureEvent"); return 1; } int CreatureEventFunctions::luaCreatureEventType(lua_State* L) { // creatureevent:type(callback) - const auto &creatureEvent = getUserdataShared(L, 1); + const auto &creatureEvent = Lua::getUserdataShared(L, 1); if (creatureEvent) { - std::string typeName = getString(L, 2); + std::string typeName = Lua::getString(L, 2); const std::string tmpStr = asLowerCaseString(typeName); if (tmpStr == "login") { creatureEvent->setEventType(CREATURE_EVENT_LOGIN); @@ -55,10 +74,10 @@ int CreatureEventFunctions::luaCreatureEventType(lua_State* L) { g_logger().error("[CreatureEventFunctions::luaCreatureEventType] - " "Invalid type for creature event: {}", typeName); - pushBoolean(L, false); + Lua::pushBoolean(L, false); } creatureEvent->setLoaded(true); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -67,13 +86,13 @@ int CreatureEventFunctions::luaCreatureEventType(lua_State* L) { int CreatureEventFunctions::luaCreatureEventRegister(lua_State* L) { // creatureevent:register() - const auto &creatureEvent = getUserdataShared(L, 1); + const auto &creatureEvent = Lua::getUserdataShared(L, 1); if (creatureEvent) { - if (!creatureEvent->isLoadedCallback()) { - pushBoolean(L, false); + if (!creatureEvent->isLoadedScriptId()) { + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, g_creatureEvents().registerLuaEvent(creatureEvent)); + Lua::pushBoolean(L, g_creatureEvents().registerLuaEvent(creatureEvent)); } else { lua_pushnil(L); } @@ -82,13 +101,13 @@ int CreatureEventFunctions::luaCreatureEventRegister(lua_State* L) { int CreatureEventFunctions::luaCreatureEventOnCallback(lua_State* L) { // creatureevent:onLogin / logout / etc. (callback) - const auto &creatureEvent = getUserdataShared(L, 1); + const auto &creatureEvent = Lua::getUserdataShared(L, 1); if (creatureEvent) { - if (!creatureEvent->loadCallback()) { - pushBoolean(L, false); + if (!creatureEvent->loadScriptId()) { + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } diff --git a/src/lua/functions/events/creature_event_functions.hpp b/src/lua/functions/events/creature_event_functions.hpp index f605d517fbf..be896a495ec 100644 --- a/src/lua/functions/events/creature_event_functions.hpp +++ b/src/lua/functions/events/creature_event_functions.hpp @@ -9,33 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class CreatureEventFunctions final : LuaScriptInterface { +class CreatureEventFunctions { public: - explicit CreatureEventFunctions(lua_State* L) : - LuaScriptInterface("CreatureEventFunctions") { - init(L); - } - ~CreatureEventFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "CreatureEvent", "", CreatureEventFunctions::luaCreateCreatureEvent); - registerMethod(L, "CreatureEvent", "type", CreatureEventFunctions::luaCreatureEventType); - registerMethod(L, "CreatureEvent", "register", CreatureEventFunctions::luaCreatureEventRegister); - registerMethod(L, "CreatureEvent", "onLogin", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onLogout", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onThink", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onPrepareDeath", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onDeath", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onKill", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onAdvance", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onModalWindow", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onTextEdit", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onHealthChange", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onManaChange", CreatureEventFunctions::luaCreatureEventOnCallback); - registerMethod(L, "CreatureEvent", "onExtendedOpcode", CreatureEventFunctions::luaCreatureEventOnCallback); - } + static void init(lua_State* L); private: static int luaCreateCreatureEvent(lua_State* L); diff --git a/src/lua/functions/events/event_callback_functions.cpp b/src/lua/functions/events/event_callback_functions.cpp index db59092f3af..bec82177256 100644 --- a/src/lua/functions/events/event_callback_functions.cpp +++ b/src/lua/functions/events/event_callback_functions.cpp @@ -14,6 +14,7 @@ #include "utils/tools.hpp" #include "items/item.hpp" #include "creatures/players/player.hpp" +#include "lua/functions/lua_functions_loader.hpp" /** * @class EventCallbackFunctions @@ -26,33 +27,33 @@ */ void EventCallbackFunctions::init(lua_State* luaState) { - registerSharedClass(luaState, "EventCallback", "", EventCallbackFunctions::luaEventCallbackCreate); - registerMethod(luaState, "EventCallback", "type", EventCallbackFunctions::luaEventCallbackType); - registerMethod(luaState, "EventCallback", "register", EventCallbackFunctions::luaEventCallbackRegister); + Lua::registerSharedClass(luaState, "EventCallback", "", EventCallbackFunctions::luaEventCallbackCreate); + Lua::registerMethod(luaState, "EventCallback", "type", EventCallbackFunctions::luaEventCallbackType); + Lua::registerMethod(luaState, "EventCallback", "register", EventCallbackFunctions::luaEventCallbackRegister); } int EventCallbackFunctions::luaEventCallbackCreate(lua_State* luaState) { - const auto &callbackName = getString(luaState, 2); + const auto &callbackName = Lua::getString(luaState, 2); if (callbackName.empty()) { - reportErrorFunc("Invalid callback name"); + Lua::reportErrorFunc("Invalid callback name"); return 1; } - bool skipDuplicationCheck = getBoolean(luaState, 3, false); - const auto eventCallback = std::make_shared(getScriptEnv()->getScriptInterface(), callbackName, skipDuplicationCheck); - pushUserdata(luaState, eventCallback); - setMetatable(luaState, -1, "EventCallback"); + bool skipDuplicationCheck = Lua::getBoolean(luaState, 3, false); + const auto eventCallback = std::make_shared(callbackName, skipDuplicationCheck); + Lua::pushUserdata(luaState, eventCallback); + Lua::setMetatable(luaState, -1, "EventCallback"); return 1; } int EventCallbackFunctions::luaEventCallbackType(lua_State* luaState) { - const auto &callback = getUserdataShared(luaState, 1); + const auto &callback = Lua::getUserdataShared(luaState, 1); if (!callback) { - reportErrorFunc("EventCallback is nil"); + Lua::reportErrorFunc("EventCallback is nil"); return 0; } - auto typeName = getString(luaState, 2); + auto typeName = Lua::getString(luaState, 2); auto lowerTypeName = asLowerCaseString(typeName); bool found = false; for (auto enumValue : magic_enum::enum_values()) { @@ -69,46 +70,45 @@ int EventCallbackFunctions::luaEventCallbackType(lua_State* luaState) { if (!found) { g_logger().error("[{}] No valid event name: {}", __func__, typeName); - pushBoolean(luaState, false); + Lua::pushBoolean(luaState, false); } - pushBoolean(luaState, true); + Lua::pushBoolean(luaState, true); return 1; } int EventCallbackFunctions::luaEventCallbackRegister(lua_State* luaState) { - const auto &callback = getUserdataShared(luaState, 1); + const auto &callback = Lua::getUserdataShared(luaState, 1); if (!callback) { return 0; } - if (!callback->isLoadedCallback()) { + if (!callback->isLoadedScriptId()) { return 0; } if (g_callbacks().isCallbackRegistered(callback)) { - reportErrorFunc(fmt::format("EventCallback is duplicated for event with name: {}", callback->getName())); + Lua::reportErrorFunc(fmt::format("EventCallback is duplicated for event with name: {}", callback->getName())); return 0; } g_callbacks().addCallback(callback); - pushBoolean(luaState, true); + Lua::pushBoolean(luaState, true); return 1; } // Callback functions int EventCallbackFunctions::luaEventCallbackLoad(lua_State* luaState) { - const auto &callback = getUserdataShared(luaState, 1); + const auto &callback = Lua::getUserdataShared(luaState, 1); if (!callback) { return 1; } - if (!callback->loadCallback()) { - reportErrorFunc("Cannot load callback"); + if (!callback->loadScriptId()) { + Lua::reportErrorFunc("Cannot load callback"); return 1; } - callback->setLoadedCallback(true); - pushBoolean(luaState, true); + Lua::pushBoolean(luaState, true); return 1; } diff --git a/src/lua/functions/events/event_callback_functions.hpp b/src/lua/functions/events/event_callback_functions.hpp index 9623a52169b..34a08995b2b 100644 --- a/src/lua/functions/events/event_callback_functions.hpp +++ b/src/lua/functions/events/event_callback_functions.hpp @@ -9,8 +9,6 @@ #pragma once -#include "lua/scripts/luascript.hpp" - /** * @class EventCallbackFunctions * @brief Provides a set of static functions for working with Event Callbacks in Lua. @@ -18,14 +16,8 @@ * @details This class encapsulates the Lua binding functions related to event callbacks, * allowing for interaction between the C++ codebase and Lua scripts. */ -class EventCallbackFunctions final : public LuaScriptInterface { +class EventCallbackFunctions { public: - explicit EventCallbackFunctions(lua_State* L) : - LuaScriptInterface("EventCallbackFunctions") { - init(L); - } - ~EventCallbackFunctions() override = default; - /** * @brief Initializes the Lua state with the event callback functions. * diff --git a/src/lua/functions/events/events_scheduler_functions.cpp b/src/lua/functions/events/events_scheduler_functions.cpp index 7b9e7709d9e..0c83a862ff4 100644 --- a/src/lua/functions/events/events_scheduler_functions.cpp +++ b/src/lua/functions/events/events_scheduler_functions.cpp @@ -10,6 +10,17 @@ #include "lua/functions/events/events_scheduler_functions.hpp" #include "game/scheduling/events_scheduler.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void EventsSchedulerFunctions::init(lua_State* L) { + Lua::registerTable(L, "EventsScheduler"); + + Lua::registerMethod(L, "EventsScheduler", "getEventSLoot", EventsSchedulerFunctions::luaEventsSchedulergetEventSLoot); + Lua::registerMethod(L, "EventsScheduler", "getEventSBossLoot", EventsSchedulerFunctions::luaEventsSchedulergetEventSBossLoot); + Lua::registerMethod(L, "EventsScheduler", "getEventSSkill", EventsSchedulerFunctions::luaEventsSchedulergetEventSSkill); + Lua::registerMethod(L, "EventsScheduler", "getEventSExp", EventsSchedulerFunctions::luaEventsSchedulergetEventSExp); + Lua::registerMethod(L, "EventsScheduler", "getSpawnMonsterSchedule", EventsSchedulerFunctions::luaEventsSchedulergetSpawnMonsterSchedule); +} int EventsSchedulerFunctions::luaEventsSchedulergetEventSLoot(lua_State* L) { // EventsScheduler.getEventSLoot diff --git a/src/lua/functions/events/events_scheduler_functions.hpp b/src/lua/functions/events/events_scheduler_functions.hpp index 9059bf1fdfb..58919ebc9c5 100644 --- a/src/lua/functions/events/events_scheduler_functions.hpp +++ b/src/lua/functions/events/events_scheduler_functions.hpp @@ -9,25 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class EventsSchedulerFunctions final : private LuaScriptInterface { +class EventsSchedulerFunctions { public: - explicit EventsSchedulerFunctions(lua_State* L) : - LuaScriptInterface("EventsSchedulerFunctions") { - init(L); - } - ~EventsSchedulerFunctions() override = default; - - static void init(lua_State* L) { - registerTable(L, "EventsScheduler"); - - registerMethod(L, "EventsScheduler", "getEventSLoot", EventsSchedulerFunctions::luaEventsSchedulergetEventSLoot); - registerMethod(L, "EventsScheduler", "getEventSBossLoot", EventsSchedulerFunctions::luaEventsSchedulergetEventSBossLoot); - registerMethod(L, "EventsScheduler", "getEventSSkill", EventsSchedulerFunctions::luaEventsSchedulergetEventSSkill); - registerMethod(L, "EventsScheduler", "getEventSExp", EventsSchedulerFunctions::luaEventsSchedulergetEventSExp); - registerMethod(L, "EventsScheduler", "getSpawnMonsterSchedule", EventsSchedulerFunctions::luaEventsSchedulergetSpawnMonsterSchedule); - } + static void init(lua_State* L); private: static int luaEventsSchedulergetEventSLoot(lua_State* L); diff --git a/src/lua/functions/events/global_event_functions.cpp b/src/lua/functions/events/global_event_functions.cpp index 32c9eccf2e5..b7dddb9bb02 100644 --- a/src/lua/functions/events/global_event_functions.cpp +++ b/src/lua/functions/events/global_event_functions.cpp @@ -12,21 +12,37 @@ #include "game/game.hpp" #include "lua/global/globalevent.hpp" #include "utils/tools.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void GlobalEventFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "GlobalEvent", "", GlobalEventFunctions::luaCreateGlobalEvent); + Lua::registerMethod(L, "GlobalEvent", "type", GlobalEventFunctions::luaGlobalEventType); + Lua::registerMethod(L, "GlobalEvent", "register", GlobalEventFunctions::luaGlobalEventRegister); + Lua::registerMethod(L, "GlobalEvent", "time", GlobalEventFunctions::luaGlobalEventTime); + Lua::registerMethod(L, "GlobalEvent", "interval", GlobalEventFunctions::luaGlobalEventInterval); + Lua::registerMethod(L, "GlobalEvent", "onThink", GlobalEventFunctions::luaGlobalEventOnCallback); + Lua::registerMethod(L, "GlobalEvent", "onTime", GlobalEventFunctions::luaGlobalEventOnCallback); + Lua::registerMethod(L, "GlobalEvent", "onStartup", GlobalEventFunctions::luaGlobalEventOnCallback); + Lua::registerMethod(L, "GlobalEvent", "onShutdown", GlobalEventFunctions::luaGlobalEventOnCallback); + Lua::registerMethod(L, "GlobalEvent", "onRecord", GlobalEventFunctions::luaGlobalEventOnCallback); + Lua::registerMethod(L, "GlobalEvent", "onPeriodChange", GlobalEventFunctions::luaGlobalEventOnCallback); + Lua::registerMethod(L, "GlobalEvent", "onSave", GlobalEventFunctions::luaGlobalEventOnCallback); +} int GlobalEventFunctions::luaCreateGlobalEvent(lua_State* L) { - const auto global = std::make_shared(getScriptEnv()->getScriptInterface()); - global->setName(getString(L, 2)); + const auto global = std::make_shared(); + global->setName(Lua::getString(L, 2)); global->setEventType(GLOBALEVENT_NONE); - pushUserdata(L, global); - setMetatable(L, -1, "GlobalEvent"); + Lua::pushUserdata(L, global); + Lua::setMetatable(L, -1, "GlobalEvent"); return 1; } int GlobalEventFunctions::luaGlobalEventType(lua_State* L) { // globalevent:type(callback) - const auto &global = getUserdataShared(L, 1); + const auto &global = Lua::getUserdataShared(L, 1); if (global) { - const std::string typeName = getString(L, 2); + const std::string typeName = Lua::getString(L, 2); const std::string tmpStr = asLowerCaseString(typeName); if (tmpStr == "startup") { global->setEventType(GLOBALEVENT_STARTUP); @@ -43,9 +59,9 @@ int GlobalEventFunctions::luaGlobalEventType(lua_State* L) { } else { g_logger().error("[GlobalEventFunctions::luaGlobalEventType] - " "Invalid type for global event: {}"); - pushBoolean(L, false); + Lua::pushBoolean(L, false); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -54,18 +70,18 @@ int GlobalEventFunctions::luaGlobalEventType(lua_State* L) { int GlobalEventFunctions::luaGlobalEventRegister(lua_State* L) { // globalevent:register() - const auto &globalevent = getUserdataShared(L, 1); + const auto &globalevent = Lua::getUserdataShared(L, 1); if (globalevent) { - if (!globalevent->isLoadedCallback()) { - pushBoolean(L, false); + if (!globalevent->isLoadedScriptId()) { + Lua::pushBoolean(L, false); return 1; } if (globalevent->getEventType() == GLOBALEVENT_NONE && globalevent->getInterval() == 0) { g_logger().error("{} - No interval for globalevent with name {}", __FUNCTION__, globalevent->getName()); - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, g_globalEvents().registerLuaEvent(globalevent)); + Lua::pushBoolean(L, g_globalEvents().registerLuaEvent(globalevent)); } else { lua_pushnil(L); } @@ -74,13 +90,13 @@ int GlobalEventFunctions::luaGlobalEventRegister(lua_State* L) { int GlobalEventFunctions::luaGlobalEventOnCallback(lua_State* L) { // globalevent:onThink / record / etc. (callback) - const auto &globalevent = getUserdataShared(L, 1); + const auto &globalevent = Lua::getUserdataShared(L, 1); if (globalevent) { - if (!globalevent->loadCallback()) { - pushBoolean(L, false); + if (!globalevent->loadScriptId()) { + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -89,9 +105,9 @@ int GlobalEventFunctions::luaGlobalEventOnCallback(lua_State* L) { int GlobalEventFunctions::luaGlobalEventTime(lua_State* L) { // globalevent:time(time) - const auto &globalevent = getUserdataShared(L, 1); + const auto &globalevent = Lua::getUserdataShared(L, 1); if (globalevent) { - std::string timer = getString(L, 2); + std::string timer = Lua::getString(L, 2); const std::vector params = vectorAtoi(explodeString(timer, ":")); const int32_t hour = params.front(); @@ -99,7 +115,7 @@ int GlobalEventFunctions::luaGlobalEventTime(lua_State* L) { g_logger().error("[GlobalEventFunctions::luaGlobalEventTime] - " "Invalid hour {} for globalevent with name: {}", timer, globalevent->getName()); - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } @@ -113,7 +129,7 @@ int GlobalEventFunctions::luaGlobalEventTime(lua_State* L) { g_logger().error("[GlobalEventFunctions::luaGlobalEventTime] - " "Invalid minute: {} for globalevent with name: {}", timer, globalevent->getName()); - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } @@ -123,7 +139,7 @@ int GlobalEventFunctions::luaGlobalEventTime(lua_State* L) { g_logger().error("[GlobalEventFunctions::luaGlobalEventTime] - " "Invalid minute: {} for globalevent with name: {}", timer, globalevent->getName()); - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } } @@ -143,7 +159,7 @@ int GlobalEventFunctions::luaGlobalEventTime(lua_State* L) { globalevent->setNextExecution(current_time + difference); globalevent->setEventType(GLOBALEVENT_TIMER); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -152,11 +168,11 @@ int GlobalEventFunctions::luaGlobalEventTime(lua_State* L) { int GlobalEventFunctions::luaGlobalEventInterval(lua_State* L) { // globalevent:interval(interval) - const auto &globalevent = getUserdataShared(L, 1); + const auto &globalevent = Lua::getUserdataShared(L, 1); if (globalevent) { - globalevent->setInterval(getNumber(L, 2)); - globalevent->setNextExecution(OTSYS_TIME() + getNumber(L, 2)); - pushBoolean(L, true); + globalevent->setInterval(Lua::getNumber(L, 2)); + globalevent->setNextExecution(OTSYS_TIME() + Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } diff --git a/src/lua/functions/events/global_event_functions.hpp b/src/lua/functions/events/global_event_functions.hpp index 41fc57fdfea..1860a7a8d39 100644 --- a/src/lua/functions/events/global_event_functions.hpp +++ b/src/lua/functions/events/global_event_functions.hpp @@ -9,30 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class GlobalEventFunctions final : LuaScriptInterface { +class GlobalEventFunctions { public: - explicit GlobalEventFunctions(lua_State* L) : - LuaScriptInterface("GlobalEventFunctions") { - init(L); - } - ~GlobalEventFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "GlobalEvent", "", GlobalEventFunctions::luaCreateGlobalEvent); - registerMethod(L, "GlobalEvent", "type", GlobalEventFunctions::luaGlobalEventType); - registerMethod(L, "GlobalEvent", "register", GlobalEventFunctions::luaGlobalEventRegister); - registerMethod(L, "GlobalEvent", "time", GlobalEventFunctions::luaGlobalEventTime); - registerMethod(L, "GlobalEvent", "interval", GlobalEventFunctions::luaGlobalEventInterval); - registerMethod(L, "GlobalEvent", "onThink", GlobalEventFunctions::luaGlobalEventOnCallback); - registerMethod(L, "GlobalEvent", "onTime", GlobalEventFunctions::luaGlobalEventOnCallback); - registerMethod(L, "GlobalEvent", "onStartup", GlobalEventFunctions::luaGlobalEventOnCallback); - registerMethod(L, "GlobalEvent", "onShutdown", GlobalEventFunctions::luaGlobalEventOnCallback); - registerMethod(L, "GlobalEvent", "onRecord", GlobalEventFunctions::luaGlobalEventOnCallback); - registerMethod(L, "GlobalEvent", "onPeriodChange", GlobalEventFunctions::luaGlobalEventOnCallback); - registerMethod(L, "GlobalEvent", "onSave", GlobalEventFunctions::luaGlobalEventOnCallback); - } + static void init(lua_State* L); private: static int luaCreateGlobalEvent(lua_State* L); diff --git a/src/lua/functions/events/move_event_functions.cpp b/src/lua/functions/events/move_event_functions.cpp index ac7727fb2a9..c5ea87b5a45 100644 --- a/src/lua/functions/events/move_event_functions.cpp +++ b/src/lua/functions/events/move_event_functions.cpp @@ -12,20 +12,42 @@ #include "creatures/creature.hpp" #include "lua/creature/movement.hpp" #include "utils/tools.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void MoveEventFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "MoveEvent", "", MoveEventFunctions::luaCreateMoveEvent); + Lua::registerMethod(L, "MoveEvent", "type", MoveEventFunctions::luaMoveEventType); + Lua::registerMethod(L, "MoveEvent", "register", MoveEventFunctions::luaMoveEventRegister); + Lua::registerMethod(L, "MoveEvent", "level", MoveEventFunctions::luaMoveEventLevel); + Lua::registerMethod(L, "MoveEvent", "magicLevel", MoveEventFunctions::luaMoveEventMagLevel); + Lua::registerMethod(L, "MoveEvent", "slot", MoveEventFunctions::luaMoveEventSlot); + Lua::registerMethod(L, "MoveEvent", "id", MoveEventFunctions::luaMoveEventItemId); + Lua::registerMethod(L, "MoveEvent", "aid", MoveEventFunctions::luaMoveEventActionId); + Lua::registerMethod(L, "MoveEvent", "uid", MoveEventFunctions::luaMoveEventUniqueId); + Lua::registerMethod(L, "MoveEvent", "position", MoveEventFunctions::luaMoveEventPosition); + Lua::registerMethod(L, "MoveEvent", "premium", MoveEventFunctions::luaMoveEventPremium); + Lua::registerMethod(L, "MoveEvent", "vocation", MoveEventFunctions::luaMoveEventVocation); + Lua::registerMethod(L, "MoveEvent", "onEquip", MoveEventFunctions::luaMoveEventOnCallback); + Lua::registerMethod(L, "MoveEvent", "onDeEquip", MoveEventFunctions::luaMoveEventOnCallback); + Lua::registerMethod(L, "MoveEvent", "onStepIn", MoveEventFunctions::luaMoveEventOnCallback); + Lua::registerMethod(L, "MoveEvent", "onStepOut", MoveEventFunctions::luaMoveEventOnCallback); + Lua::registerMethod(L, "MoveEvent", "onAddItem", MoveEventFunctions::luaMoveEventOnCallback); + Lua::registerMethod(L, "MoveEvent", "onRemoveItem", MoveEventFunctions::luaMoveEventOnCallback); +} int MoveEventFunctions::luaCreateMoveEvent(lua_State* L) { // MoveEvent() - const auto moveevent = std::make_shared(getScriptEnv()->getScriptInterface()); - pushUserdata(L, moveevent); - setMetatable(L, -1, "MoveEvent"); + const auto moveevent = std::make_shared(); + Lua::pushUserdata(L, moveevent); + Lua::setMetatable(L, -1, "MoveEvent"); return 1; } int MoveEventFunctions::luaMoveEventType(lua_State* L) { // moveevent:type(callback) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { - std::string typeName = getString(L, 2); + std::string typeName = Lua::getString(L, 2); const std::string tmpStr = asLowerCaseString(typeName); if (tmpStr == "stepin") { moveevent->setEventType(MOVE_EVENT_STEP_IN); @@ -49,9 +71,9 @@ int MoveEventFunctions::luaMoveEventType(lua_State* L) { g_logger().error("[MoveEventFunctions::luaMoveEventType] - " "No valid event name: {}", typeName); - pushBoolean(L, false); + Lua::pushBoolean(L, false); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -60,16 +82,16 @@ int MoveEventFunctions::luaMoveEventType(lua_State* L) { int MoveEventFunctions::luaMoveEventRegister(lua_State* L) { // moveevent:register() - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { // If not scripted, register item event // Example: unscripted_equipments.lua - if (!moveevent->isLoadedCallback()) { - pushBoolean(L, g_moveEvents().registerLuaItemEvent(moveevent)); + if (!moveevent->isLoadedScriptId()) { + Lua::pushBoolean(L, g_moveEvents().registerLuaItemEvent(moveevent)); return 1; } - pushBoolean(L, g_moveEvents().registerLuaEvent(moveevent)); + Lua::pushBoolean(L, g_moveEvents().registerLuaEvent(moveevent)); } else { lua_pushnil(L); } @@ -78,14 +100,14 @@ int MoveEventFunctions::luaMoveEventRegister(lua_State* L) { int MoveEventFunctions::luaMoveEventOnCallback(lua_State* L) { // moveevent:onEquip / deEquip / etc. (callback) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { - if (!moveevent->loadCallback()) { - pushBoolean(L, false); + if (!moveevent->loadScriptId()) { + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -94,14 +116,14 @@ int MoveEventFunctions::luaMoveEventOnCallback(lua_State* L) { int MoveEventFunctions::luaMoveEventSlot(lua_State* L) { // moveevent:slot(slot) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (!moveevent) { lua_pushnil(L); return 1; } if (moveevent->getEventType() == MOVE_EVENT_EQUIP || moveevent->getEventType() == MOVE_EVENT_DEEQUIP) { - std::string slotName = asLowerCaseString(getString(L, 2)); + std::string slotName = asLowerCaseString(Lua::getString(L, 2)); if (slotName == "head") { moveevent->setSlot(SLOTP_HEAD); } else if (slotName == "necklace") { @@ -128,22 +150,22 @@ int MoveEventFunctions::luaMoveEventSlot(lua_State* L) { g_logger().warn("[MoveEventFunctions::luaMoveEventSlot] - " "Unknown slot type: {}", slotName); - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int MoveEventFunctions::luaMoveEventLevel(lua_State* L) { // moveevent:level(lvl) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { - moveevent->setRequiredLevel(getNumber(L, 2)); + moveevent->setRequiredLevel(Lua::getNumber(L, 2)); moveevent->setWieldInfo(WIELDINFO_LEVEL); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -152,11 +174,11 @@ int MoveEventFunctions::luaMoveEventLevel(lua_State* L) { int MoveEventFunctions::luaMoveEventMagLevel(lua_State* L) { // moveevent:magicLevel(lvl) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { - moveevent->setRequiredMagLevel(getNumber(L, 2)); + moveevent->setRequiredMagLevel(Lua::getNumber(L, 2)); moveevent->setWieldInfo(WIELDINFO_MAGLV); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -165,11 +187,11 @@ int MoveEventFunctions::luaMoveEventMagLevel(lua_State* L) { int MoveEventFunctions::luaMoveEventPremium(lua_State* L) { // moveevent:premium(bool) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { - moveevent->setNeedPremium(getBoolean(L, 2)); + moveevent->setNeedPremium(Lua::getBoolean(L, 2)); moveevent->setWieldInfo(WIELDINFO_PREMIUM); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -178,22 +200,22 @@ int MoveEventFunctions::luaMoveEventPremium(lua_State* L) { int MoveEventFunctions::luaMoveEventVocation(lua_State* L) { // moveevent:vocation(vocName[, showInDescription = false, lastVoc = false]) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { - moveevent->addVocEquipMap(getString(L, 2)); + moveevent->addVocEquipMap(Lua::getString(L, 2)); moveevent->setWieldInfo(WIELDINFO_VOCREQ); std::string tmp; bool showInDescription = false; bool lastVoc = false; - if (getBoolean(L, 3)) { - showInDescription = getBoolean(L, 3); + if (Lua::getBoolean(L, 3)) { + showInDescription = Lua::getBoolean(L, 3); } - if (getBoolean(L, 4)) { - lastVoc = getBoolean(L, 4); + if (Lua::getBoolean(L, 4)) { + lastVoc = Lua::getBoolean(L, 4); } if (showInDescription) { if (moveevent->getVocationString().empty()) { - tmp = asLowerCaseString(getString(L, 2)); + tmp = asLowerCaseString(Lua::getString(L, 2)); tmp += "s"; moveevent->setVocationString(tmp); } else { @@ -203,12 +225,12 @@ int MoveEventFunctions::luaMoveEventVocation(lua_State* L) { } else { tmp += ", "; } - tmp += asLowerCaseString(getString(L, 2)); + tmp += asLowerCaseString(Lua::getString(L, 2)); tmp += "s"; moveevent->setVocationString(tmp); } } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -217,17 +239,17 @@ int MoveEventFunctions::luaMoveEventVocation(lua_State* L) { int MoveEventFunctions::luaMoveEventItemId(lua_State* L) { // moveevent:id(ids) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { const int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc if (parameters > 1) { for (int i = 0; i < parameters; ++i) { - moveevent->setItemId(getNumber(L, 2 + i)); + moveevent->setItemId(Lua::getNumber(L, 2 + i)); } } else { - moveevent->setItemId(getNumber(L, 2)); + moveevent->setItemId(Lua::getNumber(L, 2)); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -236,17 +258,17 @@ int MoveEventFunctions::luaMoveEventItemId(lua_State* L) { int MoveEventFunctions::luaMoveEventActionId(lua_State* L) { // moveevent:aid(ids) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { const int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc if (parameters > 1) { for (int i = 0; i < parameters; ++i) { - moveevent->setActionId(getNumber(L, 2 + i)); + moveevent->setActionId(Lua::getNumber(L, 2 + i)); } } else { - moveevent->setActionId(getNumber(L, 2)); + moveevent->setActionId(Lua::getNumber(L, 2)); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -255,17 +277,17 @@ int MoveEventFunctions::luaMoveEventActionId(lua_State* L) { int MoveEventFunctions::luaMoveEventUniqueId(lua_State* L) { // moveevent:uid(ids) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { const int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc if (parameters > 1) { for (int i = 0; i < parameters; ++i) { - moveevent->setUniqueId(getNumber(L, 2 + i)); + moveevent->setUniqueId(Lua::getNumber(L, 2 + i)); } } else { - moveevent->setUniqueId(getNumber(L, 2)); + moveevent->setUniqueId(Lua::getNumber(L, 2)); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -274,17 +296,17 @@ int MoveEventFunctions::luaMoveEventUniqueId(lua_State* L) { int MoveEventFunctions::luaMoveEventPosition(lua_State* L) { // moveevent:position(positions) - const auto &moveevent = getUserdataShared(L, 1); + const auto &moveevent = Lua::getUserdataShared(L, 1); if (moveevent) { const int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc if (parameters > 1) { for (int i = 0; i < parameters; ++i) { - moveevent->setPosition(getPosition(L, 2 + i)); + moveevent->setPosition(Lua::getPosition(L, 2 + i)); } } else { - moveevent->setPosition(getPosition(L, 2)); + moveevent->setPosition(Lua::getPosition(L, 2)); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } diff --git a/src/lua/functions/events/move_event_functions.hpp b/src/lua/functions/events/move_event_functions.hpp index 202006257e6..cfc30c3ecda 100644 --- a/src/lua/functions/events/move_event_functions.hpp +++ b/src/lua/functions/events/move_event_functions.hpp @@ -9,36 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class MoveEventFunctions final : LuaScriptInterface { +class MoveEventFunctions { public: - explicit MoveEventFunctions(lua_State* L) : - LuaScriptInterface("MoveEventFunctions") { - init(L); - } - ~MoveEventFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "MoveEvent", "", MoveEventFunctions::luaCreateMoveEvent); - registerMethod(L, "MoveEvent", "type", MoveEventFunctions::luaMoveEventType); - registerMethod(L, "MoveEvent", "register", MoveEventFunctions::luaMoveEventRegister); - registerMethod(L, "MoveEvent", "level", MoveEventFunctions::luaMoveEventLevel); - registerMethod(L, "MoveEvent", "magicLevel", MoveEventFunctions::luaMoveEventMagLevel); - registerMethod(L, "MoveEvent", "slot", MoveEventFunctions::luaMoveEventSlot); - registerMethod(L, "MoveEvent", "id", MoveEventFunctions::luaMoveEventItemId); - registerMethod(L, "MoveEvent", "aid", MoveEventFunctions::luaMoveEventActionId); - registerMethod(L, "MoveEvent", "uid", MoveEventFunctions::luaMoveEventUniqueId); - registerMethod(L, "MoveEvent", "position", MoveEventFunctions::luaMoveEventPosition); - registerMethod(L, "MoveEvent", "premium", MoveEventFunctions::luaMoveEventPremium); - registerMethod(L, "MoveEvent", "vocation", MoveEventFunctions::luaMoveEventVocation); - registerMethod(L, "MoveEvent", "onEquip", MoveEventFunctions::luaMoveEventOnCallback); - registerMethod(L, "MoveEvent", "onDeEquip", MoveEventFunctions::luaMoveEventOnCallback); - registerMethod(L, "MoveEvent", "onStepIn", MoveEventFunctions::luaMoveEventOnCallback); - registerMethod(L, "MoveEvent", "onStepOut", MoveEventFunctions::luaMoveEventOnCallback); - registerMethod(L, "MoveEvent", "onAddItem", MoveEventFunctions::luaMoveEventOnCallback); - registerMethod(L, "MoveEvent", "onRemoveItem", MoveEventFunctions::luaMoveEventOnCallback); - } + static void init(lua_State* L); private: static int luaCreateMoveEvent(lua_State* L); diff --git a/src/lua/functions/events/talk_action_functions.cpp b/src/lua/functions/events/talk_action_functions.cpp index 1b87fec4b7d..39328ea7de4 100644 --- a/src/lua/functions/events/talk_action_functions.cpp +++ b/src/lua/functions/events/talk_action_functions.cpp @@ -14,53 +14,67 @@ #include "utils/tools.hpp" #include "enums/account_group_type.hpp" +#include "lua/functions/lua_functions_loader.hpp" +#include "lua/scripts/luascript.hpp" + +void TalkActionFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "TalkAction", "", TalkActionFunctions::luaCreateTalkAction); + Lua::registerMethod(L, "TalkAction", "onSay", TalkActionFunctions::luaTalkActionOnSay); + Lua::registerMethod(L, "TalkAction", "groupType", TalkActionFunctions::luaTalkActionGroupType); + Lua::registerMethod(L, "TalkAction", "register", TalkActionFunctions::luaTalkActionRegister); + Lua::registerMethod(L, "TalkAction", "separator", TalkActionFunctions::luaTalkActionSeparator); + Lua::registerMethod(L, "TalkAction", "getName", TalkActionFunctions::luaTalkActionGetName); + Lua::registerMethod(L, "TalkAction", "getDescription", TalkActionFunctions::luaTalkActionGetDescription); + Lua::registerMethod(L, "TalkAction", "setDescription", TalkActionFunctions::luaTalkActionSetDescription); + Lua::registerMethod(L, "TalkAction", "getGroupType", TalkActionFunctions::luaTalkActionGetGroupType); +} int TalkActionFunctions::luaCreateTalkAction(lua_State* L) { // TalkAction(words) or TalkAction(word1, word2, word3) std::vector wordsVector; for (int i = 2; i <= lua_gettop(L); i++) { - wordsVector.push_back(getString(L, i)); + wordsVector.push_back(Lua::getString(L, i)); } - const auto talkactionSharedPtr = std::make_shared(getScriptEnv()->getScriptInterface()); + const auto talkactionSharedPtr = std::make_shared(); talkactionSharedPtr->setWords(wordsVector); - pushUserdata(L, talkactionSharedPtr); - setMetatable(L, -1, "TalkAction"); + Lua::pushUserdata(L, talkactionSharedPtr); + Lua::setMetatable(L, -1, "TalkAction"); return 1; } int TalkActionFunctions::luaTalkActionOnSay(lua_State* L) { // talkAction:onSay(callback) - const auto &talkactionSharedPtr = getUserdataShared(L, 1); + const auto &talkactionSharedPtr = Lua::getUserdataShared(L, 1); if (!talkactionSharedPtr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - if (!talkactionSharedPtr->loadCallback()) { - pushBoolean(L, false); + if (!talkactionSharedPtr->loadScriptId()) { + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int TalkActionFunctions::luaTalkActionGroupType(lua_State* L) { // talkAction:groupType(GroupType = GROUP_TYPE_NORMAL) - const auto &talkactionSharedPtr = getUserdataShared(L, 1); + const auto &talkactionSharedPtr = Lua::getUserdataShared(L, 1); if (!talkactionSharedPtr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } GroupType groupType; const int type = lua_type(L, 2); if (type == LUA_TNUMBER) { - groupType = enumFromValue(getNumber(L, 2)); + groupType = enumFromValue(Lua::getNumber(L, 2)); } else if (type == LUA_TSTRING) { - std::string strValue = getString(L, 2); + std::string strValue = Lua::getString(L, 2); if (strValue == "normal") { groupType = GROUP_TYPE_NORMAL; } else if (strValue == "tutor") { @@ -74,108 +88,108 @@ int TalkActionFunctions::luaTalkActionGroupType(lua_State* L) { } else if (strValue == "god") { groupType = GROUP_TYPE_GOD; } else { - const auto string = fmt::format("Invalid group type string value {} for group type for script: {}", strValue, getScriptEnv()->getScriptInterface()->getLoadingScriptName()); - reportErrorFunc(string); - pushBoolean(L, false); + const auto string = fmt::format("Invalid group type string value {} for group type for script: {}", strValue, Lua::getScriptEnv()->getScriptInterface()->getLoadingScriptName()); + Lua::reportErrorFunc(string); + Lua::pushBoolean(L, false); return 1; } } else { - const auto string = fmt::format("Expected number or string value for group type for script: {}", getScriptEnv()->getScriptInterface()->getLoadingScriptName()); - reportErrorFunc(string); - pushBoolean(L, false); + const auto string = fmt::format("Expected number or string value for group type for script: {}", Lua::getScriptEnv()->getScriptInterface()->getLoadingScriptName()); + Lua::reportErrorFunc(string); + Lua::pushBoolean(L, false); return 1; } talkactionSharedPtr->setGroupType(groupType); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int TalkActionFunctions::luaTalkActionRegister(lua_State* L) { // talkAction:register() - const auto &talkactionSharedPtr = getUserdataShared(L, 1); + const auto &talkactionSharedPtr = Lua::getUserdataShared(L, 1); if (!talkactionSharedPtr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - if (!talkactionSharedPtr->isLoadedCallback()) { - pushBoolean(L, false); + if (!talkactionSharedPtr->isLoadedScriptId()) { + Lua::pushBoolean(L, false); return 1; } if (talkactionSharedPtr->getGroupType() == GROUP_TYPE_NONE) { const auto string = fmt::format("TalkAction with name {} does't have groupType", talkactionSharedPtr->getWords()); - reportErrorFunc(string); - pushBoolean(L, false); + Lua::reportErrorFunc(string); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, g_talkActions().registerLuaEvent(talkactionSharedPtr)); + Lua::pushBoolean(L, g_talkActions().registerLuaEvent(talkactionSharedPtr)); return 1; } int TalkActionFunctions::luaTalkActionSeparator(lua_State* L) { // talkAction:separator(sep) - const auto &talkactionSharedPtr = getUserdataShared(L, 1); + const auto &talkactionSharedPtr = Lua::getUserdataShared(L, 1); if (!talkactionSharedPtr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - talkactionSharedPtr->setSeparator(getString(L, 2)); - pushBoolean(L, true); + talkactionSharedPtr->setSeparator(Lua::getString(L, 2)); + Lua::pushBoolean(L, true); return 1; } int TalkActionFunctions::luaTalkActionGetName(lua_State* L) { // local name = talkAction:getName() - const auto &talkactionSharedPtr = getUserdataShared(L, 1); + const auto &talkactionSharedPtr = Lua::getUserdataShared(L, 1); if (!talkactionSharedPtr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - pushString(L, talkactionSharedPtr->getWords()); + Lua::pushString(L, talkactionSharedPtr->getWords()); return 1; } int TalkActionFunctions::luaTalkActionGetDescription(lua_State* L) { // local description = talkAction:getDescription() - const auto &talkactionSharedPtr = getUserdataShared(L, 1); + const auto &talkactionSharedPtr = Lua::getUserdataShared(L, 1); if (!talkactionSharedPtr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - pushString(L, talkactionSharedPtr->getDescription()); + Lua::pushString(L, talkactionSharedPtr->getDescription()); return 1; } int TalkActionFunctions::luaTalkActionSetDescription(lua_State* L) { // local description = talkAction:setDescription() - const auto &talkactionSharedPtr = getUserdataShared(L, 1); + const auto &talkactionSharedPtr = Lua::getUserdataShared(L, 1); if (!talkactionSharedPtr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - talkactionSharedPtr->setDescription(getString(L, 2)); - pushBoolean(L, true); + talkactionSharedPtr->setDescription(Lua::getString(L, 2)); + Lua::pushBoolean(L, true); return 1; } int TalkActionFunctions::luaTalkActionGetGroupType(lua_State* L) { // local groupType = talkAction:getGroupType() - const auto &talkactionSharedPtr = getUserdataShared(L, 1); + const auto &talkactionSharedPtr = Lua::getUserdataShared(L, 1); if (!talkactionSharedPtr) { - reportErrorFunc(getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_TALK_ACTION_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } diff --git a/src/lua/functions/events/talk_action_functions.hpp b/src/lua/functions/events/talk_action_functions.hpp index 487f2046d2f..5877293bb9e 100644 --- a/src/lua/functions/events/talk_action_functions.hpp +++ b/src/lua/functions/events/talk_action_functions.hpp @@ -9,27 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class TalkActionFunctions final : LuaScriptInterface { +class TalkActionFunctions { public: - explicit TalkActionFunctions(lua_State* L) : - LuaScriptInterface("TalkActionFunctions") { - init(L); - } - ~TalkActionFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "TalkAction", "", TalkActionFunctions::luaCreateTalkAction); - registerMethod(L, "TalkAction", "onSay", TalkActionFunctions::luaTalkActionOnSay); - registerMethod(L, "TalkAction", "groupType", TalkActionFunctions::luaTalkActionGroupType); - registerMethod(L, "TalkAction", "register", TalkActionFunctions::luaTalkActionRegister); - registerMethod(L, "TalkAction", "separator", TalkActionFunctions::luaTalkActionSeparator); - registerMethod(L, "TalkAction", "getName", TalkActionFunctions::luaTalkActionGetName); - registerMethod(L, "TalkAction", "getDescription", TalkActionFunctions::luaTalkActionGetDescription); - registerMethod(L, "TalkAction", "setDescription", TalkActionFunctions::luaTalkActionSetDescription); - registerMethod(L, "TalkAction", "getGroupType", TalkActionFunctions::luaTalkActionGetGroupType); - } + static void init(lua_State* L); private: static int luaCreateTalkAction(lua_State* L); diff --git a/src/lua/functions/items/container_functions.cpp b/src/lua/functions/items/container_functions.cpp index 8a0a84e3199..934c8ce3ff8 100644 --- a/src/lua/functions/items/container_functions.cpp +++ b/src/lua/functions/items/container_functions.cpp @@ -12,15 +12,37 @@ #include "game/game.hpp" #include "items/item.hpp" #include "utils/tools.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ContainerFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Container", "Item", ContainerFunctions::luaContainerCreate); + Lua::registerMetaMethod(L, "Container", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Container", "getSize", ContainerFunctions::luaContainerGetSize); + Lua::registerMethod(L, "Container", "getMaxCapacity", ContainerFunctions::luaContainerGetMaxCapacity); + Lua::registerMethod(L, "Container", "getCapacity", ContainerFunctions::luaContainerGetCapacity); + Lua::registerMethod(L, "Container", "getEmptySlots", ContainerFunctions::luaContainerGetEmptySlots); + Lua::registerMethod(L, "Container", "getContentDescription", ContainerFunctions::luaContainerGetContentDescription); + Lua::registerMethod(L, "Container", "getItems", ContainerFunctions::luaContainerGetItems); + Lua::registerMethod(L, "Container", "getItemHoldingCount", ContainerFunctions::luaContainerGetItemHoldingCount); + Lua::registerMethod(L, "Container", "getItemCountById", ContainerFunctions::luaContainerGetItemCountById); + + Lua::registerMethod(L, "Container", "getItem", ContainerFunctions::luaContainerGetItem); + Lua::registerMethod(L, "Container", "hasItem", ContainerFunctions::luaContainerHasItem); + Lua::registerMethod(L, "Container", "addItem", ContainerFunctions::luaContainerAddItem); + Lua::registerMethod(L, "Container", "addItemEx", ContainerFunctions::luaContainerAddItemEx); + Lua::registerMethod(L, "Container", "getCorpseOwner", ContainerFunctions::luaContainerGetCorpseOwner); + Lua::registerMethod(L, "Container", "registerReward", ContainerFunctions::luaContainerRegisterReward); +} int ContainerFunctions::luaContainerCreate(lua_State* L) { // Container(uid) - const uint32_t id = getNumber(L, 2); + const uint32_t id = Lua::getNumber(L, 2); - const auto &container = getScriptEnv()->getContainerByUID(id); + const auto &container = Lua::getScriptEnv()->getContainerByUID(id); if (container) { - pushUserdata(L, container); - setMetatable(L, -1, "Container"); + Lua::pushUserdata(L, container); + Lua::setMetatable(L, -1, "Container"); } else { lua_pushnil(L); } @@ -29,7 +51,7 @@ int ContainerFunctions::luaContainerCreate(lua_State* L) { int ContainerFunctions::luaContainerGetSize(lua_State* L) { // container:getSize() - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (container) { lua_pushnumber(L, container->size()); } else { @@ -40,7 +62,7 @@ int ContainerFunctions::luaContainerGetSize(lua_State* L) { int ContainerFunctions::luaContainerGetMaxCapacity(lua_State* L) { // container:getMaxCapacity() - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (container) { lua_pushnumber(L, container->getMaxCapacity()); } else { @@ -51,7 +73,7 @@ int ContainerFunctions::luaContainerGetMaxCapacity(lua_State* L) { int ContainerFunctions::luaContainerGetCapacity(lua_State* L) { // container:getCapacity() - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (container) { lua_pushnumber(L, container->capacity()); } else { @@ -62,14 +84,14 @@ int ContainerFunctions::luaContainerGetCapacity(lua_State* L) { int ContainerFunctions::luaContainerGetEmptySlots(lua_State* L) { // container:getEmptySlots([recursive = false]) - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (!container) { lua_pushnil(L); return 1; } uint32_t slots = container->capacity() - container->size(); - const bool recursive = getBoolean(L, 2, false); + const bool recursive = Lua::getBoolean(L, 2, false); if (recursive) { for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) { if (const auto &tmpContainer = (*it)->getContainer()) { @@ -83,7 +105,7 @@ int ContainerFunctions::luaContainerGetEmptySlots(lua_State* L) { int ContainerFunctions::luaContainerGetItemHoldingCount(lua_State* L) { // container:getItemHoldingCount() - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (container) { lua_pushnumber(L, container->getItemHoldingCount()); } else { @@ -94,17 +116,17 @@ int ContainerFunctions::luaContainerGetItemHoldingCount(lua_State* L) { int ContainerFunctions::luaContainerGetItem(lua_State* L) { // container:getItem(index) - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (!container) { lua_pushnil(L); return 1; } - const uint32_t index = getNumber(L, 2); + const uint32_t index = Lua::getNumber(L, 2); const auto &item = container->getItemByIndex(index); if (item) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { lua_pushnil(L); } @@ -113,10 +135,10 @@ int ContainerFunctions::luaContainerGetItem(lua_State* L) { int ContainerFunctions::luaContainerHasItem(lua_State* L) { // container:hasItem(item) - const auto &item = getUserdataShared(L, 2); - const auto &container = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 2); + const auto &container = Lua::getUserdataShared(L, 1); if (container) { - pushBoolean(L, container->isHoldingItem(item)); + Lua::pushBoolean(L, container->isHoldingItem(item)); } else { lua_pushnil(L); } @@ -125,26 +147,26 @@ int ContainerFunctions::luaContainerHasItem(lua_State* L) { int ContainerFunctions::luaContainerAddItem(lua_State* L) { // container:addItem(itemId[, count/subType = 1[, index = INDEX_WHEREEVER[, flags = 0]]]) - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (!container) { lua_pushnil(L); - reportErrorFunc("Container is nullptr"); + Lua::reportErrorFunc("Container is nullptr"); return 1; } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); - reportErrorFunc("Item id is wrong"); + Lua::reportErrorFunc("Item id is wrong"); return 1; } } - auto count = getNumber(L, 3, 1); + auto count = Lua::getNumber(L, 3, 1); const ItemType &it = Item::items[itemId]; if (it.stackable) { count = std::min(count, it.stackSize); @@ -153,46 +175,46 @@ int ContainerFunctions::luaContainerAddItem(lua_State* L) { const auto &item = Item::CreateItem(itemId, count); if (!item) { lua_pushnil(L); - reportErrorFunc("Item is nullptr"); + Lua::reportErrorFunc("Item is nullptr"); return 1; } - const auto index = getNumber(L, 4, INDEX_WHEREEVER); - const auto flags = getNumber(L, 5, 0); + const auto index = Lua::getNumber(L, 4, INDEX_WHEREEVER); + const auto flags = Lua::getNumber(L, 5, 0); ReturnValue ret = g_game().internalAddItem(container, item, index, flags); if (ret == RETURNVALUE_NOERROR) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { - reportErrorFunc(fmt::format("Cannot add item to container, error code: '{}'", getReturnMessage(ret))); - pushBoolean(L, false); + Lua::reportErrorFunc(fmt::format("Cannot add item to container, error code: '{}'", getReturnMessage(ret))); + Lua::pushBoolean(L, false); } return 1; } int ContainerFunctions::luaContainerAddItemEx(lua_State* L) { // container:addItemEx(item[, index = INDEX_WHEREEVER[, flags = 0]]) - const auto &item = getUserdataShared(L, 2); + const auto &item = Lua::getUserdataShared(L, 2); if (!item) { lua_pushnil(L); return 1; } - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (!container) { lua_pushnil(L); return 1; } if (item->getParent() != VirtualCylinder::virtualCylinder) { - reportErrorFunc("Item already has a parent"); + Lua::reportErrorFunc("Item already has a parent"); lua_pushnil(L); return 1; } - const auto index = getNumber(L, 3, INDEX_WHEREEVER); - const auto flags = getNumber(L, 4, 0); + const auto index = Lua::getNumber(L, 3, INDEX_WHEREEVER); + const auto flags = Lua::getNumber(L, 4, 0); ReturnValue ret = g_game().internalAddItem(container, item, index, flags); if (ret == RETURNVALUE_NOERROR) { ScriptEnvironment::removeTempItem(item); @@ -203,7 +225,7 @@ int ContainerFunctions::luaContainerAddItemEx(lua_State* L) { int ContainerFunctions::luaContainerGetCorpseOwner(lua_State* L) { // container:getCorpseOwner() - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (container) { lua_pushnumber(L, container->getCorpseOwner()); } else { @@ -214,33 +236,33 @@ int ContainerFunctions::luaContainerGetCorpseOwner(lua_State* L) { int ContainerFunctions::luaContainerGetItemCountById(lua_State* L) { // container:getItemCountById(itemId[, subType = -1]) - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (!container) { lua_pushnil(L); return 1; } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; } } - const auto subType = getNumber(L, 3, -1); + const auto subType = Lua::getNumber(L, 3, -1); lua_pushnumber(L, container->getItemTypeCount(itemId, subType)); return 1; } int ContainerFunctions::luaContainerGetContentDescription(lua_State* L) { // container:getContentDescription([oldProtocol]) - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (container) { - pushString(L, container->getContentDescription(getBoolean(L, 2, false))); + Lua::pushString(L, container->getContentDescription(Lua::getBoolean(L, 2, false))); } else { lua_pushnil(L); } @@ -249,13 +271,13 @@ int ContainerFunctions::luaContainerGetContentDescription(lua_State* L) { int ContainerFunctions::luaContainerGetItems(lua_State* L) { // container:getItems([recursive = false]) - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (!container) { lua_pushnil(L); return 1; } - const bool recursive = getBoolean(L, 2, false); + const bool recursive = Lua::getBoolean(L, 2, false); const std::vector> items = container->getItems(recursive); lua_createtable(L, static_cast(items.size()), 0); @@ -263,8 +285,8 @@ int ContainerFunctions::luaContainerGetItems(lua_State* L) { int index = 0; for (const auto &item : items) { index++; - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); lua_rawseti(L, -2, index); } return 1; @@ -272,7 +294,7 @@ int ContainerFunctions::luaContainerGetItems(lua_State* L) { int ContainerFunctions::luaContainerRegisterReward(lua_State* L) { // container:registerReward() - const auto &container = getUserdataShared(L, 1); + const auto &container = Lua::getUserdataShared(L, 1); if (!container) { lua_pushnil(L); return 1; @@ -285,6 +307,6 @@ int ContainerFunctions::luaContainerRegisterReward(lua_State* L) { container->internalAddThing(rewardContainer); container->setRewardCorpse(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } diff --git a/src/lua/functions/items/container_functions.hpp b/src/lua/functions/items/container_functions.hpp index 52bb33e6b52..8510a717b48 100644 --- a/src/lua/functions/items/container_functions.hpp +++ b/src/lua/functions/items/container_functions.hpp @@ -9,36 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class ContainerFunctions final : LuaScriptInterface { +class ContainerFunctions { private: - explicit ContainerFunctions(lua_State* L) : - LuaScriptInterface("ContainerFunctions") { - init(L); - } - ~ContainerFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Container", "Item", ContainerFunctions::luaContainerCreate); - registerMetaMethod(L, "Container", "__eq", ContainerFunctions::luaUserdataCompare); - - registerMethod(L, "Container", "getSize", ContainerFunctions::luaContainerGetSize); - registerMethod(L, "Container", "getMaxCapacity", ContainerFunctions::luaContainerGetMaxCapacity); - registerMethod(L, "Container", "getCapacity", ContainerFunctions::luaContainerGetCapacity); - registerMethod(L, "Container", "getEmptySlots", ContainerFunctions::luaContainerGetEmptySlots); - registerMethod(L, "Container", "getContentDescription", ContainerFunctions::luaContainerGetContentDescription); - registerMethod(L, "Container", "getItems", ContainerFunctions::luaContainerGetItems); - registerMethod(L, "Container", "getItemHoldingCount", ContainerFunctions::luaContainerGetItemHoldingCount); - registerMethod(L, "Container", "getItemCountById", ContainerFunctions::luaContainerGetItemCountById); - - registerMethod(L, "Container", "getItem", ContainerFunctions::luaContainerGetItem); - registerMethod(L, "Container", "hasItem", ContainerFunctions::luaContainerHasItem); - registerMethod(L, "Container", "addItem", ContainerFunctions::luaContainerAddItem); - registerMethod(L, "Container", "addItemEx", ContainerFunctions::luaContainerAddItemEx); - registerMethod(L, "Container", "getCorpseOwner", ContainerFunctions::luaContainerGetCorpseOwner); - registerMethod(L, "Container", "registerReward", ContainerFunctions::luaContainerRegisterReward); - } + static void init(lua_State* L); static int luaContainerCreate(lua_State* L); diff --git a/src/lua/functions/items/imbuement_functions.cpp b/src/lua/functions/items/imbuement_functions.cpp index cf01295ed96..b28cc771a78 100644 --- a/src/lua/functions/items/imbuement_functions.cpp +++ b/src/lua/functions/items/imbuement_functions.cpp @@ -11,15 +11,30 @@ #include "items/weapons/weapons.hpp" #include "creatures/players/imbuements/imbuements.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ImbuementFunctions::init(lua_State* L) { + Lua::registerClass(L, "Imbuement", "", ImbuementFunctions::luaCreateImbuement); + Lua::registerMetaMethod(L, "Imbuement", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Imbuement", "getName", ImbuementFunctions::luaImbuementGetName); + Lua::registerMethod(L, "Imbuement", "getId", ImbuementFunctions::luaImbuementGetId); + Lua::registerMethod(L, "Imbuement", "getItems", ImbuementFunctions::luaImbuementGetItems); + Lua::registerMethod(L, "Imbuement", "getBase", ImbuementFunctions::luaImbuementGetBase); + Lua::registerMethod(L, "Imbuement", "getCategory", ImbuementFunctions::luaImbuementGetCategory); + Lua::registerMethod(L, "Imbuement", "isPremium", ImbuementFunctions::luaImbuementIsPremium); + Lua::registerMethod(L, "Imbuement", "getElementDamage", ImbuementFunctions::luaImbuementGetElementDamage); + Lua::registerMethod(L, "Imbuement", "getCombatType", ImbuementFunctions::luaImbuementGetCombatType); +} int ImbuementFunctions::luaCreateImbuement(lua_State* L) { // Imbuement(id) - const uint16_t imbuementId = getNumber(L, 2); + const uint16_t imbuementId = Lua::getNumber(L, 2); Imbuement* imbuement = g_imbuements().getImbuement(imbuementId); if (imbuement) { - pushUserdata(L, imbuement); - setMetatable(L, -1, "Imbuement"); + Lua::pushUserdata(L, imbuement); + Lua::setMetatable(L, -1, "Imbuement"); } else { lua_pushnil(L); } @@ -28,9 +43,9 @@ int ImbuementFunctions::luaCreateImbuement(lua_State* L) { int ImbuementFunctions::luaImbuementGetName(lua_State* L) { // imbuement:getName() - const auto* imbuement = getUserdata(L, 1); + const auto* imbuement = Lua::getUserdata(L, 1); if (imbuement) { - pushString(L, imbuement->getName()); + Lua::pushString(L, imbuement->getName()); } else { lua_pushnil(L); } @@ -39,7 +54,7 @@ int ImbuementFunctions::luaImbuementGetName(lua_State* L) { int ImbuementFunctions::luaImbuementGetId(lua_State* L) { // imbuement:getId() - const auto* imbuement = getUserdata(L, 1); + const auto* imbuement = Lua::getUserdata(L, 1); if (imbuement) { lua_pushnumber(L, imbuement->getID()); } else { @@ -50,7 +65,7 @@ int ImbuementFunctions::luaImbuementGetId(lua_State* L) { int ImbuementFunctions::luaImbuementGetItems(lua_State* L) { // imbuement:getItems() - const auto* imbuement = getUserdata(L, 1); + const auto* imbuement = Lua::getUserdata(L, 1); if (!imbuement) { lua_pushnil(L); return 1; @@ -61,8 +76,8 @@ int ImbuementFunctions::luaImbuementGetItems(lua_State* L) { lua_createtable(L, items.size(), 0); for (const auto &itm : items) { lua_createtable(L, 0, 2); - setField(L, "itemid", itm.first); - setField(L, "count", itm.second); + Lua::setField(L, "itemid", itm.first); + Lua::setField(L, "count", itm.second); lua_rawseti(L, -2, itm.first); } @@ -71,7 +86,7 @@ int ImbuementFunctions::luaImbuementGetItems(lua_State* L) { int ImbuementFunctions::luaImbuementGetBase(lua_State* L) { // imbuement:getBase() - const auto* imbuement = getUserdata(L, 1); + const auto* imbuement = Lua::getUserdata(L, 1); if (!imbuement) { lua_pushnil(L); return 1; @@ -84,19 +99,19 @@ int ImbuementFunctions::luaImbuementGetBase(lua_State* L) { } lua_createtable(L, 0, 7); - setField(L, "id", baseImbuement->id); - setField(L, "name", baseImbuement->name); - setField(L, "price", baseImbuement->price); - setField(L, "protection", baseImbuement->protectionPrice); - setField(L, "percent", baseImbuement->percent); - setField(L, "removeCost", baseImbuement->removeCost); - setField(L, "duration", baseImbuement->duration); + Lua::setField(L, "id", baseImbuement->id); + Lua::setField(L, "name", baseImbuement->name); + Lua::setField(L, "price", baseImbuement->price); + Lua::setField(L, "protection", baseImbuement->protectionPrice); + Lua::setField(L, "percent", baseImbuement->percent); + Lua::setField(L, "removeCost", baseImbuement->removeCost); + Lua::setField(L, "duration", baseImbuement->duration); return 1; } int ImbuementFunctions::luaImbuementGetCategory(lua_State* L) { // imbuement:getCategory() - const auto* imbuement = getUserdata(L, 1); + const auto* imbuement = Lua::getUserdata(L, 1); if (!imbuement) { lua_pushnil(L); return 1; @@ -106,8 +121,8 @@ int ImbuementFunctions::luaImbuementGetCategory(lua_State* L) { if (categoryImbuement) { lua_createtable(L, 0, 2); - setField(L, "id", categoryImbuement->id); - setField(L, "name", categoryImbuement->name); + Lua::setField(L, "id", categoryImbuement->id); + Lua::setField(L, "name", categoryImbuement->name); } else { lua_pushnil(L); } @@ -117,19 +132,19 @@ int ImbuementFunctions::luaImbuementGetCategory(lua_State* L) { int ImbuementFunctions::luaImbuementIsPremium(lua_State* L) { // imbuement:isPremium() - const auto* imbuement = getUserdata(L, 1); + const auto* imbuement = Lua::getUserdata(L, 1); if (!imbuement) { lua_pushnil(L); return 1; } - pushBoolean(L, imbuement->isPremium()); + Lua::pushBoolean(L, imbuement->isPremium()); return 1; } int ImbuementFunctions::luaImbuementGetElementDamage(lua_State* L) { // imbuement:getElementDamage() - const auto* imbuement = getUserdata(L, 1); + const auto* imbuement = Lua::getUserdata(L, 1); if (imbuement) { lua_pushnumber(L, imbuement->elementDamage); } else { @@ -140,7 +155,7 @@ int ImbuementFunctions::luaImbuementGetElementDamage(lua_State* L) { int ImbuementFunctions::luaImbuementGetCombatType(lua_State* L) { // imbuement:getCombatType() - const auto* imbuement = getUserdata(L, 1); + const auto* imbuement = Lua::getUserdata(L, 1); if (imbuement) { lua_pushnumber(L, imbuement->combatType); } else { diff --git a/src/lua/functions/items/imbuement_functions.hpp b/src/lua/functions/items/imbuement_functions.hpp index f86882b1cd2..7631cbb1b2f 100644 --- a/src/lua/functions/items/imbuement_functions.hpp +++ b/src/lua/functions/items/imbuement_functions.hpp @@ -9,29 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class ImbuementFunctions final : LuaScriptInterface { +class ImbuementFunctions { public: - explicit ImbuementFunctions(lua_State* L) : - LuaScriptInterface("ImbuementFunctions") { - init(L); - } - ~ImbuementFunctions() override = default; - - static void init(lua_State* L) { - registerClass(L, "Imbuement", "", ImbuementFunctions::luaCreateImbuement); - registerMetaMethod(L, "Imbuement", "__eq", ImbuementFunctions::luaUserdataCompare); - - registerMethod(L, "Imbuement", "getName", ImbuementFunctions::luaImbuementGetName); - registerMethod(L, "Imbuement", "getId", ImbuementFunctions::luaImbuementGetId); - registerMethod(L, "Imbuement", "getItems", ImbuementFunctions::luaImbuementGetItems); - registerMethod(L, "Imbuement", "getBase", ImbuementFunctions::luaImbuementGetBase); - registerMethod(L, "Imbuement", "getCategory", ImbuementFunctions::luaImbuementGetCategory); - registerMethod(L, "Imbuement", "isPremium", ImbuementFunctions::luaImbuementIsPremium); - registerMethod(L, "Imbuement", "getElementDamage", ImbuementFunctions::luaImbuementGetElementDamage); - registerMethod(L, "Imbuement", "getCombatType", ImbuementFunctions::luaImbuementGetCombatType); - } + static void init(lua_State* L); private: static int luaCreateImbuement(lua_State* L); diff --git a/src/lua/functions/items/item_classification_functions.cpp b/src/lua/functions/items/item_classification_functions.cpp index 0b68458f2a3..5d1ccea8aff 100644 --- a/src/lua/functions/items/item_classification_functions.cpp +++ b/src/lua/functions/items/item_classification_functions.cpp @@ -11,15 +11,23 @@ #include "game/game.hpp" #include "items/items_classification.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ItemClassificationFunctions::init(lua_State* L) { + Lua::registerClass(L, "ItemClassification", "", ItemClassificationFunctions::luaItemClassificationCreate); + Lua::registerMetaMethod(L, "ItemClassification", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "ItemClassification", "addTier", ItemClassificationFunctions::luaItemClassificationAddTier); +} int ItemClassificationFunctions::luaItemClassificationCreate(lua_State* L) { // ItemClassification(id) - if (isNumber(L, 2)) { - const ItemClassification* itemClassification = g_game().getItemsClassification(getNumber(L, 2), false); + if (Lua::isNumber(L, 2)) { + const ItemClassification* itemClassification = g_game().getItemsClassification(Lua::getNumber(L, 2), false); if (itemClassification) { - pushUserdata(L, itemClassification); - setMetatable(L, -1, "ItemClassification"); - pushBoolean(L, true); + Lua::pushUserdata(L, itemClassification); + Lua::setMetatable(L, -1, "ItemClassification"); + Lua::pushBoolean(L, true); } } @@ -29,16 +37,16 @@ int ItemClassificationFunctions::luaItemClassificationCreate(lua_State* L) { int ItemClassificationFunctions::luaItemClassificationAddTier(lua_State* L) { // itemClassification:addTier(id, core, regularPrice, convergenceFusionPrice, convergenceTransferPrice) - auto* itemClassification = getUserdata(L, 1); + auto* itemClassification = Lua::getUserdata(L, 1); if (itemClassification) { itemClassification->addTier( - getNumber(L, 2), - getNumber(L, 3), - getNumber(L, 4), - getNumber(L, 5), - getNumber(L, 6) + Lua::getNumber(L, 2), + Lua::getNumber(L, 3), + Lua::getNumber(L, 4), + Lua::getNumber(L, 5), + Lua::getNumber(L, 6) ); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } diff --git a/src/lua/functions/items/item_classification_functions.hpp b/src/lua/functions/items/item_classification_functions.hpp index 29b04268282..413e549a601 100644 --- a/src/lua/functions/items/item_classification_functions.hpp +++ b/src/lua/functions/items/item_classification_functions.hpp @@ -9,22 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class ItemClassificationFunctions final : LuaScriptInterface { +class ItemClassificationFunctions { public: - explicit ItemClassificationFunctions(lua_State* L) : - LuaScriptInterface("ItemClassificationFunctions") { - init(L); - } - ~ItemClassificationFunctions() override = default; - - static void init(lua_State* L) { - registerClass(L, "ItemClassification", "", ItemClassificationFunctions::luaItemClassificationCreate); - registerMetaMethod(L, "ItemClassification", "__eq", ItemClassificationFunctions::luaUserdataCompare); - - registerMethod(L, "ItemClassification", "addTier", ItemClassificationFunctions::luaItemClassificationAddTier); - } + static void init(lua_State* L); private: static int luaItemClassificationCreate(lua_State* L); diff --git a/src/lua/functions/items/item_functions.cpp b/src/lua/functions/items/item_functions.cpp index c54d43bc94b..11d719f44f7 100644 --- a/src/lua/functions/items/item_functions.cpp +++ b/src/lua/functions/items/item_functions.cpp @@ -16,16 +16,97 @@ #include "items/decay/decay.hpp" #include "items/item.hpp" #include "utils/tools.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ItemFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Item", "", ItemFunctions::luaItemCreate); + Lua::registerMetaMethod(L, "Item", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Item", "isItem", ItemFunctions::luaItemIsItem); + + Lua::registerMethod(L, "Item", "getContainer", ItemFunctions::luaItemGetContainer); + Lua::registerMethod(L, "Item", "getParent", ItemFunctions::luaItemGetParent); + Lua::registerMethod(L, "Item", "getTopParent", ItemFunctions::luaItemGetTopParent); + + Lua::registerMethod(L, "Item", "getId", ItemFunctions::luaItemGetId); + + Lua::registerMethod(L, "Item", "clone", ItemFunctions::luaItemClone); + Lua::registerMethod(L, "Item", "split", ItemFunctions::luaItemSplit); + Lua::registerMethod(L, "Item", "remove", ItemFunctions::luaItemRemove); + + Lua::registerMethod(L, "Item", "getUniqueId", ItemFunctions::luaItemGetUniqueId); + Lua::registerMethod(L, "Item", "getActionId", ItemFunctions::luaItemGetActionId); + Lua::registerMethod(L, "Item", "setActionId", ItemFunctions::luaItemSetActionId); + + Lua::registerMethod(L, "Item", "getCount", ItemFunctions::luaItemGetCount); + Lua::registerMethod(L, "Item", "getCharges", ItemFunctions::luaItemGetCharges); + Lua::registerMethod(L, "Item", "getFluidType", ItemFunctions::luaItemGetFluidType); + Lua::registerMethod(L, "Item", "getWeight", ItemFunctions::luaItemGetWeight); + + Lua::registerMethod(L, "Item", "getSubType", ItemFunctions::luaItemGetSubType); + + Lua::registerMethod(L, "Item", "getName", ItemFunctions::luaItemGetName); + Lua::registerMethod(L, "Item", "getPluralName", ItemFunctions::luaItemGetPluralName); + Lua::registerMethod(L, "Item", "getArticle", ItemFunctions::luaItemGetArticle); + + Lua::registerMethod(L, "Item", "getPosition", ItemFunctions::luaItemGetPosition); + Lua::registerMethod(L, "Item", "getTile", ItemFunctions::luaItemGetTile); + + Lua::registerMethod(L, "Item", "hasAttribute", ItemFunctions::luaItemHasAttribute); + Lua::registerMethod(L, "Item", "getAttribute", ItemFunctions::luaItemGetAttribute); + Lua::registerMethod(L, "Item", "setAttribute", ItemFunctions::luaItemSetAttribute); + Lua::registerMethod(L, "Item", "removeAttribute", ItemFunctions::luaItemRemoveAttribute); + Lua::registerMethod(L, "Item", "getCustomAttribute", ItemFunctions::luaItemGetCustomAttribute); + Lua::registerMethod(L, "Item", "setCustomAttribute", ItemFunctions::luaItemSetCustomAttribute); + Lua::registerMethod(L, "Item", "removeCustomAttribute", ItemFunctions::luaItemRemoveCustomAttribute); + Lua::registerMethod(L, "Item", "canBeMoved", ItemFunctions::luaItemCanBeMoved); + + Lua::registerMethod(L, "Item", "setOwner", ItemFunctions::luaItemSetOwner); + Lua::registerMethod(L, "Item", "getOwnerId", ItemFunctions::luaItemGetOwnerId); + Lua::registerMethod(L, "Item", "isOwner", ItemFunctions::luaItemIsOwner); + Lua::registerMethod(L, "Item", "getOwnerName", ItemFunctions::luaItemGetOwnerName); + Lua::registerMethod(L, "Item", "hasOwner", ItemFunctions::luaItemHasOwner); + + Lua::registerMethod(L, "Item", "moveTo", ItemFunctions::luaItemMoveTo); + Lua::registerMethod(L, "Item", "transform", ItemFunctions::luaItemTransform); + Lua::registerMethod(L, "Item", "decay", ItemFunctions::luaItemDecay); + + Lua::registerMethod(L, "Item", "serializeAttributes", ItemFunctions::luaItemSerializeAttributes); + Lua::registerMethod(L, "Item", "moveToSlot", ItemFunctions::luaItemMoveToSlot); + + Lua::registerMethod(L, "Item", "getDescription", ItemFunctions::luaItemGetDescription); + + Lua::registerMethod(L, "Item", "hasProperty", ItemFunctions::luaItemHasProperty); + + Lua::registerMethod(L, "Item", "getImbuementSlot", ItemFunctions::luaItemGetImbuementSlot); + Lua::registerMethod(L, "Item", "getImbuement", ItemFunctions::luaItemGetImbuement); + + Lua::registerMethod(L, "Item", "setDuration", ItemFunctions::luaItemSetDuration); + + Lua::registerMethod(L, "Item", "isInsideDepot", ItemFunctions::luaItemIsInsideDepot); + Lua::registerMethod(L, "Item", "isContainer", ItemFunctions::luaItemIsContainer); + + Lua::registerMethod(L, "Item", "getTier", ItemFunctions::luaItemGetTier); + Lua::registerMethod(L, "Item", "setTier", ItemFunctions::luaItemSetTier); + Lua::registerMethod(L, "Item", "getClassification", ItemFunctions::luaItemGetClassification); + + Lua::registerMethod(L, "Item", "canReceiveAutoCarpet", ItemFunctions::luaItemCanReceiveAutoCarpet); + + ContainerFunctions::init(L); + ImbuementFunctions::init(L); + ItemTypeFunctions::init(L); + WeaponFunctions::init(L); +} // Item int ItemFunctions::luaItemCreate(lua_State* L) { // Item(uid) - const uint32_t id = getNumber(L, 2); + const uint32_t id = Lua::getNumber(L, 2); - const auto &item = getScriptEnv()->getItemByUID(id); + const auto &item = Lua::getScriptEnv()->getItemByUID(id); if (item) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { lua_pushnil(L); } @@ -34,13 +115,13 @@ int ItemFunctions::luaItemCreate(lua_State* L) { int ItemFunctions::luaItemIsItem(lua_State* L) { // item:isItem() - pushBoolean(L, getUserdataShared(L, 1) != nullptr); + Lua::pushBoolean(L, Lua::getUserdataShared(L, 1) != nullptr); return 1; } int ItemFunctions::luaItemGetContainer(lua_State* L) { // item:getContainer() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; @@ -49,17 +130,17 @@ int ItemFunctions::luaItemGetContainer(lua_State* L) { const auto &container = item->getContainer(); if (!container) { g_logger().trace("Item {} is not a container", item->getName()); - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - pushUserdata(L, container); + Lua::pushUserdata(L, container); return 1; } int ItemFunctions::luaItemGetParent(lua_State* L) { // item:getParent() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; @@ -71,13 +152,13 @@ int ItemFunctions::luaItemGetParent(lua_State* L) { return 1; } - pushCylinder(L, parent); + Lua::pushCylinder(L, parent); return 1; } int ItemFunctions::luaItemGetTopParent(lua_State* L) { // item:getTopParent() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; @@ -89,13 +170,13 @@ int ItemFunctions::luaItemGetTopParent(lua_State* L) { return 1; } - pushCylinder(L, topParent); + Lua::pushCylinder(L, topParent); return 1; } int ItemFunctions::luaItemGetId(lua_State* L) { // item:getId() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { lua_pushnumber(L, item->getID()); } else { @@ -106,7 +187,7 @@ int ItemFunctions::luaItemGetId(lua_State* L) { int ItemFunctions::luaItemClone(lua_State* L) { // item:clone() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; @@ -118,17 +199,17 @@ int ItemFunctions::luaItemClone(lua_State* L) { return 1; } - getScriptEnv()->addTempItem(clone); + Lua::getScriptEnv()->addTempItem(clone); clone->setParent(VirtualCylinder::virtualCylinder); - pushUserdata(L, clone); - setItemMetatable(L, -1, clone); + Lua::pushUserdata(L, clone); + Lua::setItemMetatable(L, -1, clone); return 1; } int ItemFunctions::luaItemSplit(lua_State* L) { // item:split([count = 1]) - const auto &itemPtr = getRawUserDataShared(L, 1); + const auto &itemPtr = Lua::getRawUserDataShared(L, 1); if (!itemPtr) { lua_pushnil(L); return 1; @@ -140,7 +221,7 @@ int ItemFunctions::luaItemSplit(lua_State* L) { return 1; } - const uint16_t count = std::min(getNumber(L, 2, 1), item->getItemCount()); + const uint16_t count = std::min(Lua::getNumber(L, 2, 1), item->getItemCount()); const uint16_t diff = item->getItemCount() - count; const auto &splitItem = item->clone(); @@ -151,7 +232,7 @@ int ItemFunctions::luaItemSplit(lua_State* L) { splitItem->setItemCount(count); - ScriptEnvironment* env = getScriptEnv(); + ScriptEnvironment* env = Lua::getScriptEnv(); const uint32_t uid = env->addThing(item); const auto &newItem = g_game().transformItem(item, item->getID(), diff); @@ -168,17 +249,17 @@ int ItemFunctions::luaItemSplit(lua_State* L) { splitItem->setParent(VirtualCylinder::virtualCylinder); env->addTempItem(splitItem); - pushUserdata(L, splitItem); - setItemMetatable(L, -1, splitItem); + Lua::pushUserdata(L, splitItem); + Lua::setItemMetatable(L, -1, splitItem); return 1; } int ItemFunctions::luaItemRemove(lua_State* L) { // item:remove([count = -1]) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { - const auto count = getNumber(L, 2, -1); - pushBoolean(L, g_game().internalRemoveItem(item, count) == RETURNVALUE_NOERROR); + const auto count = Lua::getNumber(L, 2, -1); + Lua::pushBoolean(L, g_game().internalRemoveItem(item, count) == RETURNVALUE_NOERROR); } else { lua_pushnil(L); } @@ -187,11 +268,11 @@ int ItemFunctions::luaItemRemove(lua_State* L) { int ItemFunctions::luaItemGetUniqueId(lua_State* L) { // item:getUniqueId() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { uint32_t uniqueId = item->getAttribute(ItemAttribute_t::UNIQUEID); if (uniqueId == 0) { - uniqueId = getScriptEnv()->addThing(item); + uniqueId = Lua::getScriptEnv()->addThing(item); } lua_pushnumber(L, static_cast(uniqueId)); } else { @@ -202,7 +283,7 @@ int ItemFunctions::luaItemGetUniqueId(lua_State* L) { int ItemFunctions::luaItemGetActionId(lua_State* L) { // item:getActionId() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { const auto actionId = item->getAttribute(ItemAttribute_t::ACTIONID); lua_pushnumber(L, actionId); @@ -214,11 +295,11 @@ int ItemFunctions::luaItemGetActionId(lua_State* L) { int ItemFunctions::luaItemSetActionId(lua_State* L) { // item:setActionId(actionId) - const uint16_t actionId = getNumber(L, 2); - const auto &item = getUserdataShared(L, 1); + const uint16_t actionId = Lua::getNumber(L, 2); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { item->setAttribute(ItemAttribute_t::ACTIONID, actionId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -227,7 +308,7 @@ int ItemFunctions::luaItemSetActionId(lua_State* L) { int ItemFunctions::luaItemGetCount(lua_State* L) { // item:getCount() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { lua_pushnumber(L, item->getItemCount()); } else { @@ -238,7 +319,7 @@ int ItemFunctions::luaItemGetCount(lua_State* L) { int ItemFunctions::luaItemGetCharges(lua_State* L) { // item:getCharges() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { lua_pushnumber(L, item->getCharges()); } else { @@ -249,7 +330,7 @@ int ItemFunctions::luaItemGetCharges(lua_State* L) { int ItemFunctions::luaItemGetFluidType(lua_State* L) { // item:getFluidType() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { lua_pushnumber(L, static_cast(item->getAttribute(ItemAttribute_t::FLUIDTYPE))); } else { @@ -260,7 +341,7 @@ int ItemFunctions::luaItemGetFluidType(lua_State* L) { int ItemFunctions::luaItemGetWeight(lua_State* L) { // item:getWeight() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { lua_pushnumber(L, item->getWeight()); } else { @@ -271,7 +352,7 @@ int ItemFunctions::luaItemGetWeight(lua_State* L) { int ItemFunctions::luaItemGetSubType(lua_State* L) { // item:getSubType() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { lua_pushnumber(L, item->getSubType()); } else { @@ -282,9 +363,9 @@ int ItemFunctions::luaItemGetSubType(lua_State* L) { int ItemFunctions::luaItemGetName(lua_State* L) { // item:getName() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { - pushString(L, item->getName()); + Lua::pushString(L, item->getName()); } else { lua_pushnil(L); } @@ -293,9 +374,9 @@ int ItemFunctions::luaItemGetName(lua_State* L) { int ItemFunctions::luaItemGetPluralName(lua_State* L) { // item:getPluralName() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { - pushString(L, item->getPluralName()); + Lua::pushString(L, item->getPluralName()); } else { lua_pushnil(L); } @@ -304,9 +385,9 @@ int ItemFunctions::luaItemGetPluralName(lua_State* L) { int ItemFunctions::luaItemGetArticle(lua_State* L) { // item:getArticle() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { - pushString(L, item->getArticle()); + Lua::pushString(L, item->getArticle()); } else { lua_pushnil(L); } @@ -314,10 +395,10 @@ int ItemFunctions::luaItemGetArticle(lua_State* L) { } int ItemFunctions::luaItemGetPosition(lua_State* L) { - // item:getPosition() - const auto &item = getUserdataShared(L, 1); + // item:Lua::getPosition() + const auto &item = Lua::getUserdataShared(L, 1); if (item) { - pushPosition(L, item->getPosition()); + Lua::pushPosition(L, item->getPosition()); } else { lua_pushnil(L); } @@ -326,7 +407,7 @@ int ItemFunctions::luaItemGetPosition(lua_State* L) { int ItemFunctions::luaItemGetTile(lua_State* L) { // item:getTile() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; @@ -334,8 +415,8 @@ int ItemFunctions::luaItemGetTile(lua_State* L) { const auto &tile = item->getTile(); if (tile) { - pushUserdata(L, tile); - setMetatable(L, -1, "Tile"); + Lua::pushUserdata(L, tile); + Lua::setMetatable(L, -1, "Tile"); } else { lua_pushnil(L); } @@ -344,38 +425,38 @@ int ItemFunctions::luaItemGetTile(lua_State* L) { int ItemFunctions::luaItemHasAttribute(lua_State* L) { // item:hasAttribute(key) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; } ItemAttribute_t attribute; - if (isNumber(L, 2)) { - attribute = getNumber(L, 2); - } else if (isString(L, 2)) { - attribute = stringToItemAttribute(getString(L, 2)); + if (Lua::isNumber(L, 2)) { + attribute = Lua::getNumber(L, 2); + } else if (Lua::isString(L, 2)) { + attribute = stringToItemAttribute(Lua::getString(L, 2)); } else { attribute = ItemAttribute_t::NONE; } - pushBoolean(L, item->hasAttribute(attribute)); + Lua::pushBoolean(L, item->hasAttribute(attribute)); return 1; } int ItemFunctions::luaItemGetAttribute(lua_State* L) { // item:getAttribute(key) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; } ItemAttribute_t attribute; - if (isNumber(L, 2)) { - attribute = getNumber(L, 2); - } else if (isString(L, 2)) { - attribute = stringToItemAttribute(getString(L, 2)); + if (Lua::isNumber(L, 2)) { + attribute = Lua::getNumber(L, 2); + } else if (Lua::isString(L, 2)) { + attribute = stringToItemAttribute(Lua::getString(L, 2)); } else { attribute = ItemAttribute_t::NONE; } @@ -388,7 +469,7 @@ int ItemFunctions::luaItemGetAttribute(lua_State* L) { lua_pushnumber(L, static_cast(item->getAttribute(attribute))); } else if (item->isAttributeString(attribute)) { - pushString(L, item->getAttribute(attribute)); + Lua::pushString(L, item->getAttribute(attribute)); } else { lua_pushnil(L); } @@ -397,17 +478,17 @@ int ItemFunctions::luaItemGetAttribute(lua_State* L) { int ItemFunctions::luaItemSetAttribute(lua_State* L) { // item:setAttribute(key, value) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; } ItemAttribute_t attribute; - if (isNumber(L, 2)) { - attribute = getNumber(L, 2); - } else if (isString(L, 2)) { - attribute = stringToItemAttribute(getString(L, 2)); + if (Lua::isNumber(L, 2)) { + attribute = Lua::getNumber(L, 2); + } else if (Lua::isString(L, 2)) { + attribute = stringToItemAttribute(Lua::getString(L, 2)); } else { attribute = ItemAttribute_t::NONE; } @@ -415,39 +496,39 @@ int ItemFunctions::luaItemSetAttribute(lua_State* L) { if (item->isAttributeInteger(attribute)) { switch (attribute) { case ItemAttribute_t::DECAYSTATE: { - if (const auto decayState = getNumber(L, 3); + if (const auto decayState = Lua::getNumber(L, 3); decayState == DECAYING_FALSE || decayState == DECAYING_STOPPING) { g_decay().stopDecay(item); } else { g_decay().startDecay(item); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } case ItemAttribute_t::DURATION: { item->setDecaying(DECAYING_PENDING); - item->setDuration(getNumber(L, 3)); + item->setDuration(Lua::getNumber(L, 3)); g_decay().startDecay(item); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } case ItemAttribute_t::DURATION_TIMESTAMP: { - reportErrorFunc("Attempt to set protected key \"duration timestamp\""); - pushBoolean(L, false); + Lua::reportErrorFunc("Attempt to set protected key \"duration timestamp\""); + Lua::pushBoolean(L, false); return 1; } default: break; } - item->setAttribute(attribute, getNumber(L, 3)); + item->setAttribute(attribute, Lua::getNumber(L, 3)); item->updateTileFlags(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else if (item->isAttributeString(attribute)) { - const auto newAttributeString = getString(L, 3); + const auto newAttributeString = Lua::getString(L, 3); item->setAttribute(attribute, newAttributeString); item->updateTileFlags(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -456,17 +537,17 @@ int ItemFunctions::luaItemSetAttribute(lua_State* L) { int ItemFunctions::luaItemRemoveAttribute(lua_State* L) { // item:removeAttribute(key) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; } ItemAttribute_t attribute; - if (isNumber(L, 2)) { - attribute = getNumber(L, 2); - } else if (isString(L, 2)) { - attribute = stringToItemAttribute(getString(L, 2)); + if (Lua::isNumber(L, 2)) { + attribute = Lua::getNumber(L, 2); + } else if (Lua::isString(L, 2)) { + attribute = stringToItemAttribute(Lua::getString(L, 2)); } else { attribute = ItemAttribute_t::NONE; } @@ -477,28 +558,28 @@ int ItemFunctions::luaItemRemoveAttribute(lua_State* L) { if (ret) { item->removeAttribute(attribute); } else { - reportErrorFunc("Attempt to erase protected key \"duration timestamp\""); + Lua::reportErrorFunc("Attempt to erase protected key \"duration timestamp\""); } } else { - reportErrorFunc("Attempt to erase protected key \"uid\""); + Lua::reportErrorFunc("Attempt to erase protected key \"uid\""); } - pushBoolean(L, ret); + Lua::pushBoolean(L, ret); return 1; } int ItemFunctions::luaItemGetCustomAttribute(lua_State* L) { // item:getCustomAttribute(key) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; } const CustomAttribute* customAttribute; - if (isNumber(L, 2)) { - customAttribute = item->getCustomAttribute(std::to_string(getNumber(L, 2))); - } else if (isString(L, 2)) { - customAttribute = item->getCustomAttribute(getString(L, 2)); + if (Lua::isNumber(L, 2)) { + customAttribute = item->getCustomAttribute(std::to_string(Lua::getNumber(L, 2))); + } else if (Lua::isString(L, 2)) { + customAttribute = item->getCustomAttribute(Lua::getString(L, 2)); } else { lua_pushnil(L); return 1; @@ -514,57 +595,57 @@ int ItemFunctions::luaItemGetCustomAttribute(lua_State* L) { int ItemFunctions::luaItemSetCustomAttribute(lua_State* L) { // item:setCustomAttribute(key, value) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; } std::string key; - if (isNumber(L, 2)) { - key = std::to_string(getNumber(L, 2)); - } else if (isString(L, 2)) { - key = getString(L, 2); + if (Lua::isNumber(L, 2)) { + key = std::to_string(Lua::getNumber(L, 2)); + } else if (Lua::isString(L, 2)) { + key = Lua::getString(L, 2); } else { lua_pushnil(L); return 1; } - if (isNumber(L, 3)) { - const double doubleValue = getNumber(L, 3); + if (Lua::isNumber(L, 3)) { + const double doubleValue = Lua::getNumber(L, 3); if (std::floor(doubleValue) < doubleValue) { item->setCustomAttribute(key, doubleValue); } else { - const int64_t int64 = getNumber(L, 3); + const int64_t int64 = Lua::getNumber(L, 3); item->setCustomAttribute(key, int64); } - } else if (isString(L, 3)) { - const std::string stringValue = getString(L, 3); + } else if (Lua::isString(L, 3)) { + const std::string stringValue = Lua::getString(L, 3); item->setCustomAttribute(key, stringValue); - } else if (isBoolean(L, 3)) { - const bool boolValue = getBoolean(L, 3); + } else if (Lua::isBoolean(L, 3)) { + const bool boolValue = Lua::getBoolean(L, 3); item->setCustomAttribute(key, boolValue); } else { lua_pushnil(L); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int ItemFunctions::luaItemRemoveCustomAttribute(lua_State* L) { // item:removeCustomAttribute(key) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; } - if (isNumber(L, 2)) { - pushBoolean(L, item->removeCustomAttribute(std::to_string(getNumber(L, 2)))); - } else if (isString(L, 2)) { - pushBoolean(L, item->removeCustomAttribute(getString(L, 2))); + if (Lua::isNumber(L, 2)) { + Lua::pushBoolean(L, item->removeCustomAttribute(std::to_string(Lua::getNumber(L, 2)))); + } else if (Lua::isString(L, 2)) { + Lua::pushBoolean(L, item->removeCustomAttribute(Lua::getString(L, 2))); } else { lua_pushnil(L); } @@ -573,9 +654,9 @@ int ItemFunctions::luaItemRemoveCustomAttribute(lua_State* L) { int ItemFunctions::luaItemCanBeMoved(lua_State* L) { // item:canBeMoved() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { - pushBoolean(L, item->canBeMoved()); + Lua::pushBoolean(L, item->canBeMoved()); } else { lua_pushnil(L); } @@ -584,7 +665,7 @@ int ItemFunctions::luaItemCanBeMoved(lua_State* L) { int ItemFunctions::luaItemSerializeAttributes(lua_State* L) { // item:serializeAttributes() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { lua_pushnil(L); return 1; @@ -601,7 +682,7 @@ int ItemFunctions::luaItemSerializeAttributes(lua_State* L) { int ItemFunctions::luaItemMoveTo(lua_State* L) { // item:moveTo(position or cylinder[, flags]) - const auto &itemPtr = getRawUserDataShared(L, 1); + const auto &itemPtr = Lua::getRawUserDataShared(L, 1); if (!itemPtr) { lua_pushnil(L); return 1; @@ -614,24 +695,24 @@ int ItemFunctions::luaItemMoveTo(lua_State* L) { } std::shared_ptr toCylinder; - if (isUserdata(L, 2)) { - const LuaData_t type = getUserdataType(L, 2); + if (Lua::isUserdata(L, 2)) { + const LuaData_t type = Lua::getUserdataType(L, 2); switch (type) { case LuaData_t::Container: - toCylinder = getUserdataShared(L, 2); + toCylinder = Lua::getUserdataShared(L, 2); break; case LuaData_t::Player: - toCylinder = getUserdataShared(L, 2); + toCylinder = Lua::getUserdataShared(L, 2); break; case LuaData_t::Tile: - toCylinder = getUserdataShared(L, 2); + toCylinder = Lua::getUserdataShared(L, 2); break; default: toCylinder = nullptr; break; } } else { - toCylinder = g_game().map.getTile(getPosition(L, 2)); + toCylinder = g_game().map.getTile(Lua::getPosition(L, 2)); } if (!toCylinder) { @@ -640,28 +721,28 @@ int ItemFunctions::luaItemMoveTo(lua_State* L) { } if (item->getParent() == toCylinder) { - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } - const auto flags = getNumber(L, 3, FLAG_NOLIMIT | FLAG_IGNOREBLOCKITEM | FLAG_IGNOREBLOCKCREATURE | FLAG_IGNORENOTMOVABLE); + const auto flags = Lua::getNumber(L, 3, FLAG_NOLIMIT | FLAG_IGNOREBLOCKITEM | FLAG_IGNOREBLOCKCREATURE | FLAG_IGNORENOTMOVABLE); if (item->getParent() == VirtualCylinder::virtualCylinder) { - pushBoolean(L, g_game().internalAddItem(toCylinder, item, INDEX_WHEREEVER, flags) == RETURNVALUE_NOERROR); + Lua::pushBoolean(L, g_game().internalAddItem(toCylinder, item, INDEX_WHEREEVER, flags) == RETURNVALUE_NOERROR); } else { std::shared_ptr moveItem = nullptr; ReturnValue ret = g_game().internalMoveItem(item->getParent(), toCylinder, INDEX_WHEREEVER, item, item->getItemCount(), &moveItem, flags); if (moveItem) { *itemPtr = moveItem; } - pushBoolean(L, ret == RETURNVALUE_NOERROR); + Lua::pushBoolean(L, ret == RETURNVALUE_NOERROR); } return 1; } int ItemFunctions::luaItemTransform(lua_State* L) { // item:transform(itemId[, count/subType = -1]) - const auto &itemPtr = getRawUserDataShared(L, 1); + const auto &itemPtr = Lua::getRawUserDataShared(L, 1); if (!itemPtr) { lua_pushnil(L); return 1; @@ -674,19 +755,19 @@ int ItemFunctions::luaItemTransform(lua_State* L) { } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; } } - auto subType = getNumber(L, 3, -1); + auto subType = Lua::getNumber(L, 3, -1); if (item->getID() == itemId && (subType == -1 || subType == item->getSubType())) { - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -695,7 +776,7 @@ int ItemFunctions::luaItemTransform(lua_State* L) { subType = std::min(subType, it.stackSize); } - ScriptEnvironment* env = getScriptEnv(); + ScriptEnvironment* env = Lua::getScriptEnv(); const uint32_t uid = env->addThing(item); const auto &newItem = g_game().transformItem(item, itemId, subType); @@ -708,21 +789,21 @@ int ItemFunctions::luaItemTransform(lua_State* L) { } item = newItem; - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int ItemFunctions::luaItemDecay(lua_State* L) { // item:decay(decayId) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { - if (isNumber(L, 2)) { + if (Lua::isNumber(L, 2)) { ItemType &it = Item::items.getItemType(item->getID()); - it.decayTo = getNumber(L, 2); + it.decayTo = Lua::getNumber(L, 2); } item->startDecaying(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -731,32 +812,32 @@ int ItemFunctions::luaItemDecay(lua_State* L) { int ItemFunctions::luaItemMoveToSlot(lua_State* L) { // item:moveToSlot(player, slot) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item || item->isRemoved()) { lua_pushnil(L); return 1; } - const auto &player = getUserdataShared(L, 2); + const auto &player = Lua::getUserdataShared(L, 2); if (!player) { lua_pushnil(L); return 1; } - const auto slot = getNumber(L, 3, CONST_SLOT_WHEREEVER); + const auto slot = Lua::getNumber(L, 3, CONST_SLOT_WHEREEVER); ReturnValue ret = g_game().internalMoveItem(item->getParent(), player, slot, item, item->getItemCount(), nullptr); - pushBoolean(L, ret == RETURNVALUE_NOERROR); + Lua::pushBoolean(L, ret == RETURNVALUE_NOERROR); return 1; } int ItemFunctions::luaItemGetDescription(lua_State* L) { // item:getDescription(distance) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { - const int32_t distance = getNumber(L, 2); - pushString(L, item->getDescription(distance)); + const int32_t distance = Lua::getNumber(L, 2); + Lua::pushString(L, item->getDescription(distance)); } else { lua_pushnil(L); } @@ -765,10 +846,10 @@ int ItemFunctions::luaItemGetDescription(lua_State* L) { int ItemFunctions::luaItemHasProperty(lua_State* L) { // item:hasProperty(property) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (item) { - const ItemProperty property = getNumber(L, 2); - pushBoolean(L, item->hasProperty(property)); + const ItemProperty property = Lua::getNumber(L, 2); + Lua::pushBoolean(L, item->hasProperty(property)); } else { lua_pushnil(L); } @@ -777,10 +858,10 @@ int ItemFunctions::luaItemHasProperty(lua_State* L) { int ItemFunctions::luaItemGetImbuement(lua_State* L) { // item:getImbuement() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -795,23 +876,23 @@ int ItemFunctions::luaItemGetImbuement(lua_State* L) { continue; } - pushUserdata(L, imbuement); - setMetatable(L, -1, "Imbuement"); + Lua::pushUserdata(L, imbuement); + Lua::setMetatable(L, -1, "Imbuement"); lua_createtable(L, 0, 3); - setField(L, "id", imbuement->getID()); - setField(L, "name", imbuement->getName()); - setField(L, "duration", static_cast(imbuementInfo.duration)); + Lua::setField(L, "id", imbuement->getID()); + Lua::setField(L, "name", imbuement->getName()); + Lua::setField(L, "duration", static_cast(imbuementInfo.duration)); } return 1; } int ItemFunctions::luaItemGetImbuementSlot(lua_State* L) { // item:getImbuementSlot() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -822,26 +903,26 @@ int ItemFunctions::luaItemGetImbuementSlot(lua_State* L) { int ItemFunctions::luaItemSetDuration(lua_State* L) { // item:setDuration(minDuration, maxDuration = 0, decayTo = 0, showDuration = true) // Example: item:setDuration(10000, 20000, 2129, false) = random duration from range 10000/20000 - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - const uint32_t minDuration = getNumber(L, 2); + const uint32_t minDuration = Lua::getNumber(L, 2); uint32_t maxDuration = 0; if (lua_gettop(L) > 2) { - maxDuration = uniform_random(minDuration, getNumber(L, 3)); + maxDuration = uniform_random(minDuration, Lua::getNumber(L, 3)); } uint16_t itemid = 0; if (lua_gettop(L) > 3) { - itemid = getNumber(L, 4); + itemid = Lua::getNumber(L, 4); } bool showDuration = true; if (lua_gettop(L) > 4) { - showDuration = getBoolean(L, 5); + showDuration = Lua::getBoolean(L, 5); } ItemType &it = Item::items.getItemType(item->getID()); @@ -853,43 +934,43 @@ int ItemFunctions::luaItemSetDuration(lua_State* L) { it.showDuration = showDuration; it.decayTo = itemid; item->startDecaying(); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int ItemFunctions::luaItemIsInsideDepot(lua_State* L) { // item:isInsideDepot([includeInbox = false]) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, item->isInsideDepot(getBoolean(L, 2, false))); + Lua::pushBoolean(L, item->isInsideDepot(Lua::getBoolean(L, 2, false))); return 1; } int ItemFunctions::luaItemIsContainer(lua_State* L) { // item:isContainer() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } const auto &it = Item::items[item->getID()]; - pushBoolean(L, it.isContainer()); + Lua::pushBoolean(L, it.isContainer()); return 1; } int ItemFunctions::luaItemGetTier(lua_State* L) { // item:getTier() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -899,24 +980,24 @@ int ItemFunctions::luaItemGetTier(lua_State* L) { int ItemFunctions::luaItemSetTier(lua_State* L) { // item:setTier(tier) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - item->setTier(getNumber(L, 2)); - pushBoolean(L, true); + item->setTier(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); return 1; } int ItemFunctions::luaItemGetClassification(lua_State* L) { // item:getClassification() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } @@ -926,52 +1007,52 @@ int ItemFunctions::luaItemGetClassification(lua_State* L) { int ItemFunctions::luaItemCanReceiveAutoCarpet(lua_State* L) { // item:canReceiveAutoCarpet() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); - pushBoolean(L, false); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, item->canReceiveAutoCarpet()); + Lua::pushBoolean(L, item->canReceiveAutoCarpet()); return 1; } int ItemFunctions::luaItemSetOwner(lua_State* L) { // item:setOwner(creature|creatureId) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); return 0; } - if (isUserdata(L, 2)) { - const auto &creature = getUserdataShared(L, 2); + if (Lua::isUserdata(L, 2)) { + const auto &creature = Lua::getUserdataShared(L, 2); if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 0; } item->setOwner(creature); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } - const auto creatureId = getNumber(L, 2); + const auto creatureId = Lua::getNumber(L, 2); if (creatureId != 0) { item->setOwner(creatureId); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } int ItemFunctions::luaItemGetOwnerId(lua_State* L) { // item:getOwner() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); return 0; } @@ -986,42 +1067,42 @@ int ItemFunctions::luaItemGetOwnerId(lua_State* L) { int ItemFunctions::luaItemIsOwner(lua_State* L) { // item:isOwner(creature|creatureId) - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); return 0; } - if (isUserdata(L, 2)) { - const auto &creature = getUserdataShared(L, 2); + if (Lua::isUserdata(L, 2)) { + const auto &creature = Lua::getUserdataShared(L, 2); if (!creature) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 0; } - pushBoolean(L, item->isOwner(creature)); + Lua::pushBoolean(L, item->isOwner(creature)); return 1; } - const auto creatureId = getNumber(L, 2); + const auto creatureId = Lua::getNumber(L, 2); if (creatureId != 0) { - pushBoolean(L, item->isOwner(creatureId)); + Lua::pushBoolean(L, item->isOwner(creatureId)); return 1; } - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } int ItemFunctions::luaItemGetOwnerName(lua_State* L) { // item:getOwnerName() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); return 0; } if (const auto ownerName = item->getOwnerName(); !ownerName.empty()) { - pushString(L, ownerName); + Lua::pushString(L, ownerName); return 1; } @@ -1031,12 +1112,12 @@ int ItemFunctions::luaItemGetOwnerName(lua_State* L) { int ItemFunctions::luaItemHasOwner(lua_State* L) { // item:hasOwner() - const auto &item = getUserdataShared(L, 1); + const auto &item = Lua::getUserdataShared(L, 1); if (!item) { - reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); return 1; } - pushBoolean(L, item->hasOwner()); + Lua::pushBoolean(L, item->hasOwner()); return 1; } diff --git a/src/lua/functions/items/item_functions.hpp b/src/lua/functions/items/item_functions.hpp index 1cf3764eac8..44b97c0e113 100644 --- a/src/lua/functions/items/item_functions.hpp +++ b/src/lua/functions/items/item_functions.hpp @@ -13,95 +13,9 @@ #include "lua/functions/items/imbuement_functions.hpp" #include "lua/functions/items/item_type_functions.hpp" #include "lua/functions/items/weapon_functions.hpp" -#include "lua/scripts/luascript.hpp" - -class ItemFunctions final : LuaScriptInterface { +class ItemFunctions { public: - explicit ItemFunctions(lua_State* L) : - LuaScriptInterface("ItemFunctions") { - init(L); - } - ~ItemFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Item", "", ItemFunctions::luaItemCreate); - registerMetaMethod(L, "Item", "__eq", ItemFunctions::luaUserdataCompare); - - registerMethod(L, "Item", "isItem", ItemFunctions::luaItemIsItem); - - registerMethod(L, "Item", "getContainer", ItemFunctions::luaItemGetContainer); - registerMethod(L, "Item", "getParent", ItemFunctions::luaItemGetParent); - registerMethod(L, "Item", "getTopParent", ItemFunctions::luaItemGetTopParent); - - registerMethod(L, "Item", "getId", ItemFunctions::luaItemGetId); - - registerMethod(L, "Item", "clone", ItemFunctions::luaItemClone); - registerMethod(L, "Item", "split", ItemFunctions::luaItemSplit); - registerMethod(L, "Item", "remove", ItemFunctions::luaItemRemove); - - registerMethod(L, "Item", "getUniqueId", ItemFunctions::luaItemGetUniqueId); - registerMethod(L, "Item", "getActionId", ItemFunctions::luaItemGetActionId); - registerMethod(L, "Item", "setActionId", ItemFunctions::luaItemSetActionId); - - registerMethod(L, "Item", "getCount", ItemFunctions::luaItemGetCount); - registerMethod(L, "Item", "getCharges", ItemFunctions::luaItemGetCharges); - registerMethod(L, "Item", "getFluidType", ItemFunctions::luaItemGetFluidType); - registerMethod(L, "Item", "getWeight", ItemFunctions::luaItemGetWeight); - - registerMethod(L, "Item", "getSubType", ItemFunctions::luaItemGetSubType); - - registerMethod(L, "Item", "getName", ItemFunctions::luaItemGetName); - registerMethod(L, "Item", "getPluralName", ItemFunctions::luaItemGetPluralName); - registerMethod(L, "Item", "getArticle", ItemFunctions::luaItemGetArticle); - - registerMethod(L, "Item", "getPosition", ItemFunctions::luaItemGetPosition); - registerMethod(L, "Item", "getTile", ItemFunctions::luaItemGetTile); - - registerMethod(L, "Item", "hasAttribute", ItemFunctions::luaItemHasAttribute); - registerMethod(L, "Item", "getAttribute", ItemFunctions::luaItemGetAttribute); - registerMethod(L, "Item", "setAttribute", ItemFunctions::luaItemSetAttribute); - registerMethod(L, "Item", "removeAttribute", ItemFunctions::luaItemRemoveAttribute); - registerMethod(L, "Item", "getCustomAttribute", ItemFunctions::luaItemGetCustomAttribute); - registerMethod(L, "Item", "setCustomAttribute", ItemFunctions::luaItemSetCustomAttribute); - registerMethod(L, "Item", "removeCustomAttribute", ItemFunctions::luaItemRemoveCustomAttribute); - registerMethod(L, "Item", "canBeMoved", ItemFunctions::luaItemCanBeMoved); - - registerMethod(L, "Item", "setOwner", ItemFunctions::luaItemSetOwner); - registerMethod(L, "Item", "getOwnerId", ItemFunctions::luaItemGetOwnerId); - registerMethod(L, "Item", "isOwner", ItemFunctions::luaItemIsOwner); - registerMethod(L, "Item", "getOwnerName", ItemFunctions::luaItemGetOwnerName); - registerMethod(L, "Item", "hasOwner", ItemFunctions::luaItemHasOwner); - - registerMethod(L, "Item", "moveTo", ItemFunctions::luaItemMoveTo); - registerMethod(L, "Item", "transform", ItemFunctions::luaItemTransform); - registerMethod(L, "Item", "decay", ItemFunctions::luaItemDecay); - - registerMethod(L, "Item", "serializeAttributes", ItemFunctions::luaItemSerializeAttributes); - registerMethod(L, "Item", "moveToSlot", ItemFunctions::luaItemMoveToSlot); - - registerMethod(L, "Item", "getDescription", ItemFunctions::luaItemGetDescription); - - registerMethod(L, "Item", "hasProperty", ItemFunctions::luaItemHasProperty); - - registerMethod(L, "Item", "getImbuementSlot", ItemFunctions::luaItemGetImbuementSlot); - registerMethod(L, "Item", "getImbuement", ItemFunctions::luaItemGetImbuement); - - registerMethod(L, "Item", "setDuration", ItemFunctions::luaItemSetDuration); - - registerMethod(L, "Item", "isInsideDepot", ItemFunctions::luaItemIsInsideDepot); - registerMethod(L, "Item", "isContainer", ItemFunctions::luaItemIsContainer); - - registerMethod(L, "Item", "getTier", ItemFunctions::luaItemGetTier); - registerMethod(L, "Item", "setTier", ItemFunctions::luaItemSetTier); - registerMethod(L, "Item", "getClassification", ItemFunctions::luaItemGetClassification); - - registerMethod(L, "Item", "canReceiveAutoCarpet", ItemFunctions::luaItemCanReceiveAutoCarpet); - - ContainerFunctions::init(L); - ImbuementFunctions::init(L); - ItemTypeFunctions::init(L); - WeaponFunctions::init(L); - } + static void init(lua_State* L); private: static int luaItemCreate(lua_State* L); diff --git a/src/lua/functions/items/item_type_functions.cpp b/src/lua/functions/items/item_type_functions.cpp index 57c5cc5b999..1669e6b795e 100644 --- a/src/lua/functions/items/item_type_functions.cpp +++ b/src/lua/functions/items/item_type_functions.cpp @@ -11,27 +11,96 @@ #include "items/item.hpp" #include "items/items.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void ItemTypeFunctions::init(lua_State* L) { + Lua::registerClass(L, "ItemType", "", ItemTypeFunctions::luaItemTypeCreate); + Lua::registerMetaMethod(L, "ItemType", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "ItemType", "isCorpse", ItemTypeFunctions::luaItemTypeIsCorpse); + Lua::registerMethod(L, "ItemType", "isDoor", ItemTypeFunctions::luaItemTypeIsDoor); + Lua::registerMethod(L, "ItemType", "isContainer", ItemTypeFunctions::luaItemTypeIsContainer); + Lua::registerMethod(L, "ItemType", "isFluidContainer", ItemTypeFunctions::luaItemTypeIsFluidContainer); + Lua::registerMethod(L, "ItemType", "isMovable", ItemTypeFunctions::luaItemTypeIsMovable); + Lua::registerMethod(L, "ItemType", "isRune", ItemTypeFunctions::luaItemTypeIsRune); + Lua::registerMethod(L, "ItemType", "isStackable", ItemTypeFunctions::luaItemTypeIsStackable); + Lua::registerMethod(L, "ItemType", "isStowable", ItemTypeFunctions::luaItemTypeIsStowable); + Lua::registerMethod(L, "ItemType", "isReadable", ItemTypeFunctions::luaItemTypeIsReadable); + Lua::registerMethod(L, "ItemType", "isWritable", ItemTypeFunctions::luaItemTypeIsWritable); + Lua::registerMethod(L, "ItemType", "isBlocking", ItemTypeFunctions::luaItemTypeIsBlocking); + Lua::registerMethod(L, "ItemType", "isGroundTile", ItemTypeFunctions::luaItemTypeIsGroundTile); + Lua::registerMethod(L, "ItemType", "isMagicField", ItemTypeFunctions::luaItemTypeIsMagicField); + Lua::registerMethod(L, "ItemType", "isMultiUse", ItemTypeFunctions::luaItemTypeIsMultiUse); + Lua::registerMethod(L, "ItemType", "isPickupable", ItemTypeFunctions::luaItemTypeIsPickupable); + Lua::registerMethod(L, "ItemType", "isKey", ItemTypeFunctions::luaItemTypeIsKey); + Lua::registerMethod(L, "ItemType", "isQuiver", ItemTypeFunctions::luaItemTypeIsQuiver); + + Lua::registerMethod(L, "ItemType", "getType", ItemTypeFunctions::luaItemTypeGetType); + Lua::registerMethod(L, "ItemType", "getId", ItemTypeFunctions::luaItemTypeGetId); + Lua::registerMethod(L, "ItemType", "getName", ItemTypeFunctions::luaItemTypeGetName); + Lua::registerMethod(L, "ItemType", "getPluralName", ItemTypeFunctions::luaItemTypeGetPluralName); + Lua::registerMethod(L, "ItemType", "getArticle", ItemTypeFunctions::luaItemTypeGetArticle); + Lua::registerMethod(L, "ItemType", "getDescription", ItemTypeFunctions::luaItemTypeGetDescription); + Lua::registerMethod(L, "ItemType", "getSlotPosition", ItemTypeFunctions::luaItemTypeGetSlotPosition); + + Lua::registerMethod(L, "ItemType", "getCharges", ItemTypeFunctions::luaItemTypeGetCharges); + Lua::registerMethod(L, "ItemType", "getFluidSource", ItemTypeFunctions::luaItemTypeGetFluidSource); + Lua::registerMethod(L, "ItemType", "getCapacity", ItemTypeFunctions::luaItemTypeGetCapacity); + Lua::registerMethod(L, "ItemType", "getWeight", ItemTypeFunctions::luaItemTypeGetWeight); + Lua::registerMethod(L, "ItemType", "getStackSize", ItemTypeFunctions::luaItemTypeGetStackSize); + + Lua::registerMethod(L, "ItemType", "getHitChance", ItemTypeFunctions::luaItemTypeGetHitChance); + Lua::registerMethod(L, "ItemType", "getShootRange", ItemTypeFunctions::luaItemTypeGetShootRange); + + Lua::registerMethod(L, "ItemType", "getAttack", ItemTypeFunctions::luaItemTypeGetAttack); + Lua::registerMethod(L, "ItemType", "getDefense", ItemTypeFunctions::luaItemTypeGetDefense); + Lua::registerMethod(L, "ItemType", "getExtraDefense", ItemTypeFunctions::luaItemTypeGetExtraDefense); + Lua::registerMethod(L, "ItemType", "getImbuementSlot", ItemTypeFunctions::luaItemTypeGetImbuementSlot); + Lua::registerMethod(L, "ItemType", "getArmor", ItemTypeFunctions::luaItemTypeGetArmor); + Lua::registerMethod(L, "ItemType", "getWeaponType", ItemTypeFunctions::luaItemTypeGetWeaponType); + + Lua::registerMethod(L, "ItemType", "getElementType", ItemTypeFunctions::luaItemTypeGetElementType); + Lua::registerMethod(L, "ItemType", "getElementDamage", ItemTypeFunctions::luaItemTypeGetElementDamage); + + Lua::registerMethod(L, "ItemType", "getTransformEquipId", ItemTypeFunctions::luaItemTypeGetTransformEquipId); + Lua::registerMethod(L, "ItemType", "getTransformDeEquipId", ItemTypeFunctions::luaItemTypeGetTransformDeEquipId); + Lua::registerMethod(L, "ItemType", "getDestroyId", ItemTypeFunctions::luaItemTypeGetDestroyId); + Lua::registerMethod(L, "ItemType", "getDecayId", ItemTypeFunctions::luaItemTypeGetDecayId); + Lua::registerMethod(L, "ItemType", "getRequiredLevel", ItemTypeFunctions::luaItemTypeGetRequiredLevel); + Lua::registerMethod(L, "ItemType", "getAmmoType", ItemTypeFunctions::luaItemTypeGetAmmoType); + + Lua::registerMethod(L, "ItemType", "getDecayTime", ItemTypeFunctions::luaItemTypeGetDecayTime); + Lua::registerMethod(L, "ItemType", "getShowDuration", ItemTypeFunctions::luaItemTypeGetShowDuration); + Lua::registerMethod(L, "ItemType", "getWrapableTo", ItemTypeFunctions::luaItemTypeGetWrapableTo); + Lua::registerMethod(L, "ItemType", "getSpeed", ItemTypeFunctions::luaItemTypeGetSpeed); + Lua::registerMethod(L, "ItemType", "getBaseSpeed", ItemTypeFunctions::luaItemTypeGetBaseSpeed); + Lua::registerMethod(L, "ItemType", "getVocationString", ItemTypeFunctions::luaItemTypeGetVocationString); + + Lua::registerMethod(L, "ItemType", "hasSubType", ItemTypeFunctions::luaItemTypeHasSubType); + + ItemClassificationFunctions::init(L); +} int ItemTypeFunctions::luaItemTypeCreate(lua_State* L) { // ItemType(id or name) uint32_t id; - if (isNumber(L, 2)) { - id = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + id = Lua::getNumber(L, 2); } else { - id = Item::items.getItemIdByName(getString(L, 2)); + id = Item::items.getItemIdByName(Lua::getString(L, 2)); } const ItemType &itemType = Item::items[id]; - pushUserdata(L, &itemType); - setMetatable(L, -1, "ItemType"); + Lua::pushUserdata(L, &itemType); + Lua::setMetatable(L, -1, "ItemType"); return 1; } int ItemTypeFunctions::luaItemTypeIsCorpse(lua_State* L) { // itemType:isCorpse() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isCorpse); + Lua::pushBoolean(L, itemType->isCorpse); } else { lua_pushnil(L); } @@ -40,9 +109,9 @@ int ItemTypeFunctions::luaItemTypeIsCorpse(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsDoor(lua_State* L) { // itemType:isDoor() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isDoor()); + Lua::pushBoolean(L, itemType->isDoor()); } else { lua_pushnil(L); } @@ -51,9 +120,9 @@ int ItemTypeFunctions::luaItemTypeIsDoor(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsContainer(lua_State* L) { // itemType:isContainer() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isContainer()); + Lua::pushBoolean(L, itemType->isContainer()); } else { lua_pushnil(L); } @@ -62,9 +131,9 @@ int ItemTypeFunctions::luaItemTypeIsContainer(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsFluidContainer(lua_State* L) { // itemType:isFluidContainer() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isFluidContainer()); + Lua::pushBoolean(L, itemType->isFluidContainer()); } else { lua_pushnil(L); } @@ -73,9 +142,9 @@ int ItemTypeFunctions::luaItemTypeIsFluidContainer(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsMovable(lua_State* L) { // itemType:isMovable() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->movable); + Lua::pushBoolean(L, itemType->movable); } else { lua_pushnil(L); } @@ -84,9 +153,9 @@ int ItemTypeFunctions::luaItemTypeIsMovable(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsRune(lua_State* L) { // itemType:isRune() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isRune()); + Lua::pushBoolean(L, itemType->isRune()); } else { lua_pushnil(L); } @@ -95,9 +164,9 @@ int ItemTypeFunctions::luaItemTypeIsRune(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsStackable(lua_State* L) { // itemType:isStackable() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->stackable); + Lua::pushBoolean(L, itemType->stackable); } else { lua_pushnil(L); } @@ -106,9 +175,9 @@ int ItemTypeFunctions::luaItemTypeIsStackable(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsStowable(lua_State* L) { // itemType:isStowable() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->stackable && itemType->wareId > 0); + Lua::pushBoolean(L, itemType->stackable && itemType->wareId > 0); } else { lua_pushnil(L); } @@ -117,9 +186,9 @@ int ItemTypeFunctions::luaItemTypeIsStowable(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsReadable(lua_State* L) { // itemType:isReadable() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->canReadText); + Lua::pushBoolean(L, itemType->canReadText); } else { lua_pushnil(L); } @@ -128,9 +197,9 @@ int ItemTypeFunctions::luaItemTypeIsReadable(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsWritable(lua_State* L) { // itemType:isWritable() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->canWriteText); + Lua::pushBoolean(L, itemType->canWriteText); } else { lua_pushnil(L); } @@ -139,9 +208,9 @@ int ItemTypeFunctions::luaItemTypeIsWritable(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsBlocking(lua_State* L) { // itemType:isBlocking() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->blockProjectile || itemType->blockSolid); + Lua::pushBoolean(L, itemType->blockProjectile || itemType->blockSolid); } else { lua_pushnil(L); } @@ -150,9 +219,9 @@ int ItemTypeFunctions::luaItemTypeIsBlocking(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsGroundTile(lua_State* L) { // itemType:isGroundTile() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isGroundTile()); + Lua::pushBoolean(L, itemType->isGroundTile()); } else { lua_pushnil(L); } @@ -161,9 +230,9 @@ int ItemTypeFunctions::luaItemTypeIsGroundTile(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsMagicField(lua_State* L) { // itemType:isMagicField() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isMagicField()); + Lua::pushBoolean(L, itemType->isMagicField()); } else { lua_pushnil(L); } @@ -172,9 +241,9 @@ int ItemTypeFunctions::luaItemTypeIsMagicField(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsMultiUse(lua_State* L) { // itemType:isMultiUse() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isMultiUse()); + Lua::pushBoolean(L, itemType->isMultiUse()); } else { lua_pushnil(L); } @@ -183,9 +252,9 @@ int ItemTypeFunctions::luaItemTypeIsMultiUse(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsPickupable(lua_State* L) { // itemType:isPickupable() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isPickupable()); + Lua::pushBoolean(L, itemType->isPickupable()); } else { lua_pushnil(L); } @@ -194,9 +263,9 @@ int ItemTypeFunctions::luaItemTypeIsPickupable(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsKey(lua_State* L) { // itemType:isKey() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isKey()); + Lua::pushBoolean(L, itemType->isKey()); } else { lua_pushnil(L); } @@ -205,9 +274,9 @@ int ItemTypeFunctions::luaItemTypeIsKey(lua_State* L) { int ItemTypeFunctions::luaItemTypeIsQuiver(lua_State* L) { // itemType:isQuiver() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->isQuiver()); + Lua::pushBoolean(L, itemType->isQuiver()); } else { lua_pushnil(L); } @@ -216,7 +285,7 @@ int ItemTypeFunctions::luaItemTypeIsQuiver(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetType(lua_State* L) { // itemType:getType() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->type); } else { @@ -227,7 +296,7 @@ int ItemTypeFunctions::luaItemTypeGetType(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetId(lua_State* L) { // itemType:getId() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->id); } else { @@ -238,9 +307,9 @@ int ItemTypeFunctions::luaItemTypeGetId(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetName(lua_State* L) { // itemType:getName() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushString(L, itemType->name); + Lua::pushString(L, itemType->name); } else { lua_pushnil(L); } @@ -249,9 +318,9 @@ int ItemTypeFunctions::luaItemTypeGetName(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetPluralName(lua_State* L) { // itemType:getPluralName() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushString(L, itemType->getPluralName()); + Lua::pushString(L, itemType->getPluralName()); } else { lua_pushnil(L); } @@ -260,9 +329,9 @@ int ItemTypeFunctions::luaItemTypeGetPluralName(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetArticle(lua_State* L) { // itemType:getArticle() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushString(L, itemType->article); + Lua::pushString(L, itemType->article); } else { lua_pushnil(L); } @@ -271,11 +340,11 @@ int ItemTypeFunctions::luaItemTypeGetArticle(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetDescription(lua_State* L) { // itemType:getDescription([count]) - const auto &itemType = getUserdata(L, 1); + const auto &itemType = Lua::getUserdata(L, 1); if (itemType) { - const auto count = getNumber(L, 2, -1); + const auto count = Lua::getNumber(L, 2, -1); const auto description = Item::getDescription(*itemType, 1, nullptr, count); - pushString(L, description); + Lua::pushString(L, description); } else { lua_pushnil(L); } @@ -284,7 +353,7 @@ int ItemTypeFunctions::luaItemTypeGetDescription(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetSlotPosition(lua_State* L) { // itemType:getSlotPosition() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->slotPosition); } else { @@ -295,7 +364,7 @@ int ItemTypeFunctions::luaItemTypeGetSlotPosition(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetCharges(lua_State* L) { // itemType:getCharges() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->charges); } else { @@ -306,7 +375,7 @@ int ItemTypeFunctions::luaItemTypeGetCharges(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetFluidSource(lua_State* L) { // itemType:getFluidSource() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->fluidSource); } else { @@ -317,7 +386,7 @@ int ItemTypeFunctions::luaItemTypeGetFluidSource(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetCapacity(lua_State* L) { // itemType:getCapacity() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->maxItems); } else { @@ -328,9 +397,9 @@ int ItemTypeFunctions::luaItemTypeGetCapacity(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetWeight(lua_State* L) { // itemType:getWeight([count = 1]) - const auto count = getNumber(L, 2, 1); + const auto count = Lua::getNumber(L, 2, 1); - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (!itemType) { lua_pushnil(L); return 1; @@ -343,7 +412,7 @@ int ItemTypeFunctions::luaItemTypeGetWeight(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetStackSize(lua_State* L) { // itemType:getStackSize() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (!itemType) { lua_pushnil(L); return 1; @@ -356,7 +425,7 @@ int ItemTypeFunctions::luaItemTypeGetStackSize(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetHitChance(lua_State* L) { // itemType:getHitChance() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->hitChance); } else { @@ -367,7 +436,7 @@ int ItemTypeFunctions::luaItemTypeGetHitChance(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetShootRange(lua_State* L) { // itemType:getShootRange() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->shootRange); } else { @@ -378,7 +447,7 @@ int ItemTypeFunctions::luaItemTypeGetShootRange(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetAttack(lua_State* L) { // itemType:getAttack() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->attack); } else { @@ -389,7 +458,7 @@ int ItemTypeFunctions::luaItemTypeGetAttack(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetDefense(lua_State* L) { // itemType:getDefense() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->defense); } else { @@ -400,7 +469,7 @@ int ItemTypeFunctions::luaItemTypeGetDefense(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetExtraDefense(lua_State* L) { // itemType:getExtraDefense() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->extraDefense); } else { @@ -411,7 +480,7 @@ int ItemTypeFunctions::luaItemTypeGetExtraDefense(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetImbuementSlot(lua_State* L) { // itemType:getImbuementSlot() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->imbuementSlot); } else { @@ -422,7 +491,7 @@ int ItemTypeFunctions::luaItemTypeGetImbuementSlot(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetArmor(lua_State* L) { // itemType:getArmor() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->armor); } else { @@ -433,7 +502,7 @@ int ItemTypeFunctions::luaItemTypeGetArmor(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetWeaponType(lua_State* L) { // itemType:getWeaponType() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->weaponType); } else { @@ -444,7 +513,7 @@ int ItemTypeFunctions::luaItemTypeGetWeaponType(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetAmmoType(lua_State* L) { // itemType:getAmmoType() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->ammoType); } else { @@ -455,7 +524,7 @@ int ItemTypeFunctions::luaItemTypeGetAmmoType(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetElementType(lua_State* L) { // itemType:getElementType() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (!itemType) { lua_pushnil(L); return 1; @@ -472,7 +541,7 @@ int ItemTypeFunctions::luaItemTypeGetElementType(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetElementDamage(lua_State* L) { // itemType:getElementDamage() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (!itemType) { lua_pushnil(L); return 1; @@ -489,7 +558,7 @@ int ItemTypeFunctions::luaItemTypeGetElementDamage(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetTransformEquipId(lua_State* L) { // itemType:getTransformEquipId() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->transformEquipTo); } else { @@ -500,7 +569,7 @@ int ItemTypeFunctions::luaItemTypeGetTransformEquipId(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetTransformDeEquipId(lua_State* L) { // itemType:getTransformDeEquipId() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->transformDeEquipTo); } else { @@ -511,7 +580,7 @@ int ItemTypeFunctions::luaItemTypeGetTransformDeEquipId(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetDestroyId(lua_State* L) { // itemType:getDestroyId() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->destroyTo); } else { @@ -522,7 +591,7 @@ int ItemTypeFunctions::luaItemTypeGetDestroyId(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetDecayId(lua_State* L) { // itemType:getDecayId() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->decayTo); } else { @@ -533,7 +602,7 @@ int ItemTypeFunctions::luaItemTypeGetDecayId(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetRequiredLevel(lua_State* L) { // itemType:getRequiredLevel() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->minReqLevel); } else { @@ -544,7 +613,7 @@ int ItemTypeFunctions::luaItemTypeGetRequiredLevel(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetSpeed(lua_State* L) { // itemType:getSpeed() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (!itemType) { lua_pushnil(L); return 1; @@ -561,7 +630,7 @@ int ItemTypeFunctions::luaItemTypeGetSpeed(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetBaseSpeed(lua_State* L) { // itemType:getBaseSpeed() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->speed); } else { @@ -572,7 +641,7 @@ int ItemTypeFunctions::luaItemTypeGetBaseSpeed(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetDecayTime(lua_State* L) { // itemType:getDecayTime() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->decayTime); } else { @@ -583,7 +652,7 @@ int ItemTypeFunctions::luaItemTypeGetDecayTime(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetShowDuration(lua_State* L) { // itemType:getShowDuration() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushboolean(L, itemType->showDuration); } else { @@ -593,7 +662,7 @@ int ItemTypeFunctions::luaItemTypeGetShowDuration(lua_State* L) { } int ItemTypeFunctions::luaItemTypeGetWrapableTo(lua_State* L) { // itemType:getWrapableTo() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { lua_pushnumber(L, itemType->wrapableTo); } else { @@ -604,9 +673,9 @@ int ItemTypeFunctions::luaItemTypeGetWrapableTo(lua_State* L) { int ItemTypeFunctions::luaItemTypeHasSubType(lua_State* L) { // itemType:hasSubType() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushBoolean(L, itemType->hasSubType()); + Lua::pushBoolean(L, itemType->hasSubType()); } else { lua_pushnil(L); } @@ -615,9 +684,9 @@ int ItemTypeFunctions::luaItemTypeHasSubType(lua_State* L) { int ItemTypeFunctions::luaItemTypeGetVocationString(lua_State* L) { // itemType:getVocationString() - const auto* itemType = getUserdata(L, 1); + const auto* itemType = Lua::getUserdata(L, 1); if (itemType) { - pushString(L, itemType->vocationString); + Lua::pushString(L, itemType->vocationString); } else { lua_pushnil(L); } diff --git a/src/lua/functions/items/item_type_functions.hpp b/src/lua/functions/items/item_type_functions.hpp index 254831fba56..19a401bc235 100644 --- a/src/lua/functions/items/item_type_functions.hpp +++ b/src/lua/functions/items/item_type_functions.hpp @@ -9,84 +9,11 @@ #pragma once -#include "lua/scripts/luascript.hpp" #include "lua/functions/items/item_classification_functions.hpp" -class ItemTypeFunctions final : LuaScriptInterface { +class ItemTypeFunctions { public: - explicit ItemTypeFunctions(lua_State* L) : - LuaScriptInterface("ItemTypeFunctions") { - init(L); - } - ~ItemTypeFunctions() override = default; - - static void init(lua_State* L) { - registerClass(L, "ItemType", "", ItemTypeFunctions::luaItemTypeCreate); - registerMetaMethod(L, "ItemType", "__eq", ItemTypeFunctions::luaUserdataCompare); - - registerMethod(L, "ItemType", "isCorpse", ItemTypeFunctions::luaItemTypeIsCorpse); - registerMethod(L, "ItemType", "isDoor", ItemTypeFunctions::luaItemTypeIsDoor); - registerMethod(L, "ItemType", "isContainer", ItemTypeFunctions::luaItemTypeIsContainer); - registerMethod(L, "ItemType", "isFluidContainer", ItemTypeFunctions::luaItemTypeIsFluidContainer); - registerMethod(L, "ItemType", "isMovable", ItemTypeFunctions::luaItemTypeIsMovable); - registerMethod(L, "ItemType", "isRune", ItemTypeFunctions::luaItemTypeIsRune); - registerMethod(L, "ItemType", "isStackable", ItemTypeFunctions::luaItemTypeIsStackable); - registerMethod(L, "ItemType", "isStowable", ItemTypeFunctions::luaItemTypeIsStowable); - registerMethod(L, "ItemType", "isReadable", ItemTypeFunctions::luaItemTypeIsReadable); - registerMethod(L, "ItemType", "isWritable", ItemTypeFunctions::luaItemTypeIsWritable); - registerMethod(L, "ItemType", "isBlocking", ItemTypeFunctions::luaItemTypeIsBlocking); - registerMethod(L, "ItemType", "isGroundTile", ItemTypeFunctions::luaItemTypeIsGroundTile); - registerMethod(L, "ItemType", "isMagicField", ItemTypeFunctions::luaItemTypeIsMagicField); - registerMethod(L, "ItemType", "isMultiUse", ItemTypeFunctions::luaItemTypeIsMultiUse); - registerMethod(L, "ItemType", "isPickupable", ItemTypeFunctions::luaItemTypeIsPickupable); - registerMethod(L, "ItemType", "isKey", ItemTypeFunctions::luaItemTypeIsKey); - registerMethod(L, "ItemType", "isQuiver", ItemTypeFunctions::luaItemTypeIsQuiver); - - registerMethod(L, "ItemType", "getType", ItemTypeFunctions::luaItemTypeGetType); - registerMethod(L, "ItemType", "getId", ItemTypeFunctions::luaItemTypeGetId); - registerMethod(L, "ItemType", "getName", ItemTypeFunctions::luaItemTypeGetName); - registerMethod(L, "ItemType", "getPluralName", ItemTypeFunctions::luaItemTypeGetPluralName); - registerMethod(L, "ItemType", "getArticle", ItemTypeFunctions::luaItemTypeGetArticle); - registerMethod(L, "ItemType", "getDescription", ItemTypeFunctions::luaItemTypeGetDescription); - registerMethod(L, "ItemType", "getSlotPosition", ItemTypeFunctions::luaItemTypeGetSlotPosition); - - registerMethod(L, "ItemType", "getCharges", ItemTypeFunctions::luaItemTypeGetCharges); - registerMethod(L, "ItemType", "getFluidSource", ItemTypeFunctions::luaItemTypeGetFluidSource); - registerMethod(L, "ItemType", "getCapacity", ItemTypeFunctions::luaItemTypeGetCapacity); - registerMethod(L, "ItemType", "getWeight", ItemTypeFunctions::luaItemTypeGetWeight); - registerMethod(L, "ItemType", "getStackSize", ItemTypeFunctions::luaItemTypeGetStackSize); - - registerMethod(L, "ItemType", "getHitChance", ItemTypeFunctions::luaItemTypeGetHitChance); - registerMethod(L, "ItemType", "getShootRange", ItemTypeFunctions::luaItemTypeGetShootRange); - - registerMethod(L, "ItemType", "getAttack", ItemTypeFunctions::luaItemTypeGetAttack); - registerMethod(L, "ItemType", "getDefense", ItemTypeFunctions::luaItemTypeGetDefense); - registerMethod(L, "ItemType", "getExtraDefense", ItemTypeFunctions::luaItemTypeGetExtraDefense); - registerMethod(L, "ItemType", "getImbuementSlot", ItemTypeFunctions::luaItemTypeGetImbuementSlot); - registerMethod(L, "ItemType", "getArmor", ItemTypeFunctions::luaItemTypeGetArmor); - registerMethod(L, "ItemType", "getWeaponType", ItemTypeFunctions::luaItemTypeGetWeaponType); - - registerMethod(L, "ItemType", "getElementType", ItemTypeFunctions::luaItemTypeGetElementType); - registerMethod(L, "ItemType", "getElementDamage", ItemTypeFunctions::luaItemTypeGetElementDamage); - - registerMethod(L, "ItemType", "getTransformEquipId", ItemTypeFunctions::luaItemTypeGetTransformEquipId); - registerMethod(L, "ItemType", "getTransformDeEquipId", ItemTypeFunctions::luaItemTypeGetTransformDeEquipId); - registerMethod(L, "ItemType", "getDestroyId", ItemTypeFunctions::luaItemTypeGetDestroyId); - registerMethod(L, "ItemType", "getDecayId", ItemTypeFunctions::luaItemTypeGetDecayId); - registerMethod(L, "ItemType", "getRequiredLevel", ItemTypeFunctions::luaItemTypeGetRequiredLevel); - registerMethod(L, "ItemType", "getAmmoType", ItemTypeFunctions::luaItemTypeGetAmmoType); - - registerMethod(L, "ItemType", "getDecayTime", ItemTypeFunctions::luaItemTypeGetDecayTime); - registerMethod(L, "ItemType", "getShowDuration", ItemTypeFunctions::luaItemTypeGetShowDuration); - registerMethod(L, "ItemType", "getWrapableTo", ItemTypeFunctions::luaItemTypeGetWrapableTo); - registerMethod(L, "ItemType", "getSpeed", ItemTypeFunctions::luaItemTypeGetSpeed); - registerMethod(L, "ItemType", "getBaseSpeed", ItemTypeFunctions::luaItemTypeGetBaseSpeed); - registerMethod(L, "ItemType", "getVocationString", ItemTypeFunctions::luaItemTypeGetVocationString); - - registerMethod(L, "ItemType", "hasSubType", ItemTypeFunctions::luaItemTypeHasSubType); - - ItemClassificationFunctions::init(L); - } + static void init(lua_State* L); private: static int luaItemTypeCreate(lua_State* L); diff --git a/src/lua/functions/items/weapon_functions.cpp b/src/lua/functions/items/weapon_functions.cpp index 4a02b1c4b29..e87d843116a 100644 --- a/src/lua/functions/items/weapon_functions.cpp +++ b/src/lua/functions/items/weapon_functions.cpp @@ -13,43 +13,76 @@ #include "items/item.hpp" #include "lua/scripts/lua_environment.hpp" #include "utils/tools.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void WeaponFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Weapon", "", WeaponFunctions::luaCreateWeapon); + Lua::registerMethod(L, "Weapon", "action", WeaponFunctions::luaWeaponAction); + Lua::registerMethod(L, "Weapon", "register", WeaponFunctions::luaWeaponRegister); + Lua::registerMethod(L, "Weapon", "id", WeaponFunctions::luaWeaponId); + Lua::registerMethod(L, "Weapon", "level", WeaponFunctions::luaWeaponLevel); + Lua::registerMethod(L, "Weapon", "magicLevel", WeaponFunctions::luaWeaponMagicLevel); + Lua::registerMethod(L, "Weapon", "mana", WeaponFunctions::luaWeaponMana); + Lua::registerMethod(L, "Weapon", "manaPercent", WeaponFunctions::luaWeaponManaPercent); + Lua::registerMethod(L, "Weapon", "health", WeaponFunctions::luaWeaponHealth); + Lua::registerMethod(L, "Weapon", "healthPercent", WeaponFunctions::luaWeaponHealthPercent); + Lua::registerMethod(L, "Weapon", "soul", WeaponFunctions::luaWeaponSoul); + Lua::registerMethod(L, "Weapon", "breakChance", WeaponFunctions::luaWeaponBreakChance); + Lua::registerMethod(L, "Weapon", "premium", WeaponFunctions::luaWeaponPremium); + Lua::registerMethod(L, "Weapon", "wieldUnproperly", WeaponFunctions::luaWeaponUnproperly); + Lua::registerMethod(L, "Weapon", "vocation", WeaponFunctions::luaWeaponVocation); + Lua::registerMethod(L, "Weapon", "onUseWeapon", WeaponFunctions::luaWeaponOnUseWeapon); + Lua::registerMethod(L, "Weapon", "element", WeaponFunctions::luaWeaponElement); + Lua::registerMethod(L, "Weapon", "attack", WeaponFunctions::luaWeaponAttack); + Lua::registerMethod(L, "Weapon", "defense", WeaponFunctions::luaWeaponDefense); + Lua::registerMethod(L, "Weapon", "range", WeaponFunctions::luaWeaponRange); + Lua::registerMethod(L, "Weapon", "charges", WeaponFunctions::luaWeaponCharges); + Lua::registerMethod(L, "Weapon", "duration", WeaponFunctions::luaWeaponDuration); + Lua::registerMethod(L, "Weapon", "decayTo", WeaponFunctions::luaWeaponDecayTo); + Lua::registerMethod(L, "Weapon", "transformEquipTo", WeaponFunctions::luaWeaponTransformEquipTo); + Lua::registerMethod(L, "Weapon", "transformDeEquipTo", WeaponFunctions::luaWeaponTransformDeEquipTo); + Lua::registerMethod(L, "Weapon", "slotType", WeaponFunctions::luaWeaponSlotType); + Lua::registerMethod(L, "Weapon", "hitChance", WeaponFunctions::luaWeaponHitChance); + Lua::registerMethod(L, "Weapon", "extraElement", WeaponFunctions::luaWeaponExtraElement); + + // exclusively for distance weapons + Lua::registerMethod(L, "Weapon", "ammoType", WeaponFunctions::luaWeaponAmmoType); + Lua::registerMethod(L, "Weapon", "maxHitChance", WeaponFunctions::luaWeaponMaxHitChance); + + // exclusively for wands + Lua::registerMethod(L, "Weapon", "damage", WeaponFunctions::luaWeaponWandDamage); + + // exclusively for wands & distance weapons + Lua::registerMethod(L, "Weapon", "shootType", WeaponFunctions::luaWeaponShootType); +} int WeaponFunctions::luaCreateWeapon(lua_State* L) { // Weapon(type) - const WeaponType_t type = getNumber(L, 2); + const WeaponType_t type = Lua::getNumber(L, 2); switch (type) { case WEAPON_SWORD: case WEAPON_AXE: case WEAPON_CLUB: { - if (const auto &weaponPtr = g_luaEnvironment().createWeaponObject(getScriptEnv()->getScriptInterface())) { - pushUserdata(L, weaponPtr); - setMetatable(L, -1, "Weapon"); - weaponPtr->weaponType = type; - } else { - lua_pushnil(L); - } + auto weaponPtr = std::make_shared(); + Lua::pushUserdata(L, weaponPtr); + Lua::setMetatable(L, -1, "Weapon"); + weaponPtr->weaponType = type; break; } case WEAPON_MISSILE: case WEAPON_DISTANCE: case WEAPON_AMMO: { - if (const auto &weaponPtr = g_luaEnvironment().createWeaponObject(getScriptEnv()->getScriptInterface())) { - pushUserdata(L, weaponPtr); - setMetatable(L, -1, "Weapon"); - weaponPtr->weaponType = type; - } else { - lua_pushnil(L); - } + auto weaponPtr = std::make_shared(); + Lua::pushUserdata(L, weaponPtr); + Lua::setMetatable(L, -1, "Weapon"); + weaponPtr->weaponType = type; break; } case WEAPON_WAND: { - if (const auto &weaponPtr = g_luaEnvironment().createWeaponObject(getScriptEnv()->getScriptInterface())) { - pushUserdata(L, weaponPtr); - setMetatable(L, -1, "Weapon"); - weaponPtr->weaponType = type; - } else { - lua_pushnil(L); - } + auto weaponPtr = std::make_shared(); + Lua::pushUserdata(L, weaponPtr); + Lua::setMetatable(L, -1, "Weapon"); + weaponPtr->weaponType = type; break; } default: { @@ -62,9 +95,9 @@ int WeaponFunctions::luaCreateWeapon(lua_State* L) { int WeaponFunctions::luaWeaponAction(lua_State* L) { // weapon:action(callback) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - std::string typeName = getString(L, 2); + std::string typeName = Lua::getString(L, 2); const std::string tmpStr = asLowerCaseString(typeName); if (tmpStr == "removecount") { weapon->action = WEAPONACTION_REMOVECOUNT; @@ -76,9 +109,9 @@ int WeaponFunctions::luaWeaponAction(lua_State* L) { g_logger().error("[WeaponFunctions::luaWeaponAction] - " "No valid action {}", typeName); - pushBoolean(L, false); + Lua::pushBoolean(L, false); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -87,15 +120,15 @@ int WeaponFunctions::luaWeaponAction(lua_State* L) { int WeaponFunctions::luaWeaponRegister(lua_State* L) { // weapon:register() - const WeaponShared_ptr* weaponPtr = getRawUserDataShared(L, 1); + const WeaponShared_ptr* weaponPtr = Lua::getRawUserDataShared(L, 1); if (weaponPtr && *weaponPtr) { WeaponShared_ptr weapon = *weaponPtr; if (weapon->weaponType == WEAPON_DISTANCE || weapon->weaponType == WEAPON_AMMO || weapon->weaponType == WEAPON_MISSILE) { - weapon = getUserdataShared(L, 1); + weapon = Lua::getUserdataShared(L, 1); } else if (weapon->weaponType == WEAPON_WAND) { - weapon = getUserdataShared(L, 1); + weapon = Lua::getUserdataShared(L, 1); } else { - weapon = getUserdataShared(L, 1); + weapon = Lua::getUserdataShared(L, 1); } const uint16_t id = weapon->getID(); @@ -110,7 +143,7 @@ int WeaponFunctions::luaWeaponRegister(lua_State* L) { } weapon->configureWeapon(it); - pushBoolean(L, g_weapons().registerLuaEvent(weapon)); + Lua::pushBoolean(L, g_weapons().registerLuaEvent(weapon)); weapon = nullptr; // Releases weapon, removing the luascript reference } else { lua_pushnil(L); @@ -120,14 +153,14 @@ int WeaponFunctions::luaWeaponRegister(lua_State* L) { int WeaponFunctions::luaWeaponOnUseWeapon(lua_State* L) { // weapon:onUseWeapon(callback) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - if (!weapon->loadCallback()) { - pushBoolean(L, false); + if (!weapon->loadScriptId()) { + Lua::pushBoolean(L, false); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -136,10 +169,10 @@ int WeaponFunctions::luaWeaponOnUseWeapon(lua_State* L) { int WeaponFunctions::luaWeaponUnproperly(lua_State* L) { // weapon:wieldedUnproperly(bool) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setWieldUnproperly(getBoolean(L, 2)); - pushBoolean(L, true); + weapon->setWieldUnproperly(Lua::getBoolean(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -148,11 +181,11 @@ int WeaponFunctions::luaWeaponUnproperly(lua_State* L) { int WeaponFunctions::luaWeaponLevel(lua_State* L) { // weapon:level(lvl) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setRequiredLevel(getNumber(L, 2)); + weapon->setRequiredLevel(Lua::getNumber(L, 2)); weapon->setWieldInfo(WIELDINFO_LEVEL); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -161,11 +194,11 @@ int WeaponFunctions::luaWeaponLevel(lua_State* L) { int WeaponFunctions::luaWeaponMagicLevel(lua_State* L) { // weapon:magicLevel(lvl) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setRequiredMagLevel(getNumber(L, 2)); + weapon->setRequiredMagLevel(Lua::getNumber(L, 2)); weapon->setWieldInfo(WIELDINFO_MAGLV); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -174,10 +207,10 @@ int WeaponFunctions::luaWeaponMagicLevel(lua_State* L) { int WeaponFunctions::luaWeaponMana(lua_State* L) { // weapon:mana(mana) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setMana(getNumber(L, 2)); - pushBoolean(L, true); + weapon->setMana(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -186,10 +219,10 @@ int WeaponFunctions::luaWeaponMana(lua_State* L) { int WeaponFunctions::luaWeaponManaPercent(lua_State* L) { // weapon:manaPercent(percent) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setManaPercent(getNumber(L, 2)); - pushBoolean(L, true); + weapon->setManaPercent(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -198,10 +231,10 @@ int WeaponFunctions::luaWeaponManaPercent(lua_State* L) { int WeaponFunctions::luaWeaponHealth(lua_State* L) { // weapon:health(health) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setHealth(getNumber(L, 2)); - pushBoolean(L, true); + weapon->setHealth(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -210,10 +243,10 @@ int WeaponFunctions::luaWeaponHealth(lua_State* L) { int WeaponFunctions::luaWeaponHealthPercent(lua_State* L) { // weapon:healthPercent(percent) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setHealthPercent(getNumber(L, 2)); - pushBoolean(L, true); + weapon->setHealthPercent(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -222,10 +255,10 @@ int WeaponFunctions::luaWeaponHealthPercent(lua_State* L) { int WeaponFunctions::luaWeaponSoul(lua_State* L) { // weapon:soul(soul) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setSoul(getNumber(L, 2)); - pushBoolean(L, true); + weapon->setSoul(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -234,10 +267,10 @@ int WeaponFunctions::luaWeaponSoul(lua_State* L) { int WeaponFunctions::luaWeaponBreakChance(lua_State* L) { // weapon:breakChance(percent) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setBreakChance(getNumber(L, 2)); - pushBoolean(L, true); + weapon->setBreakChance(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -246,15 +279,15 @@ int WeaponFunctions::luaWeaponBreakChance(lua_State* L) { int WeaponFunctions::luaWeaponWandDamage(lua_State* L) { // weapon:damage(damage[min, max]) only use this if the weapon is a wand! - const auto &weapon = getUserdataShared(L, 1); + const auto &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setMinChange(getNumber(L, 2)); + weapon->setMinChange(Lua::getNumber(L, 2)); if (lua_gettop(L) > 2) { - weapon->setMaxChange(getNumber(L, 3)); + weapon->setMaxChange(Lua::getNumber(L, 3)); } else { - weapon->setMaxChange(getNumber(L, 2)); + weapon->setMaxChange(Lua::getNumber(L, 2)); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -263,10 +296,10 @@ int WeaponFunctions::luaWeaponWandDamage(lua_State* L) { int WeaponFunctions::luaWeaponElement(lua_State* L) { // weapon:element(combatType) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - if (!getNumber(L, 2)) { - std::string element = getString(L, 2); + if (!Lua::getNumber(L, 2)) { + std::string element = Lua::getString(L, 2); const std::string tmpStrValue = asLowerCaseString(element); if (tmpStrValue == "earth") { weapon->params.combatType = COMBAT_EARTHDAMAGE; @@ -286,9 +319,9 @@ int WeaponFunctions::luaWeaponElement(lua_State* L) { element); } } else { - weapon->params.combatType = getNumber(L, 2); + weapon->params.combatType = Lua::getNumber(L, 2); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -297,11 +330,11 @@ int WeaponFunctions::luaWeaponElement(lua_State* L) { int WeaponFunctions::luaWeaponPremium(lua_State* L) { // weapon:premium(bool) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setNeedPremium(getBoolean(L, 2)); + weapon->setNeedPremium(Lua::getBoolean(L, 2)); weapon->setWieldInfo(WIELDINFO_PREMIUM); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -310,22 +343,22 @@ int WeaponFunctions::luaWeaponPremium(lua_State* L) { int WeaponFunctions::luaWeaponVocation(lua_State* L) { // weapon:vocation(vocName[, showInDescription = false, lastVoc = false]) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->addVocWeaponMap(getString(L, 2)); + weapon->addVocWeaponMap(Lua::getString(L, 2)); weapon->setWieldInfo(WIELDINFO_VOCREQ); std::string tmp; bool showInDescription = false; bool lastVoc = false; - if (getBoolean(L, 3)) { - showInDescription = getBoolean(L, 3); + if (Lua::getBoolean(L, 3)) { + showInDescription = Lua::getBoolean(L, 3); } - if (getBoolean(L, 4)) { - lastVoc = getBoolean(L, 4); + if (Lua::getBoolean(L, 4)) { + lastVoc = Lua::getBoolean(L, 4); } if (showInDescription) { if (weapon->getVocationString().empty()) { - tmp = asLowerCaseString(getString(L, 2)); + tmp = asLowerCaseString(Lua::getString(L, 2)); tmp += "s"; weapon->setVocationString(tmp); } else { @@ -335,12 +368,12 @@ int WeaponFunctions::luaWeaponVocation(lua_State* L) { } else { tmp += ", "; } - tmp += asLowerCaseString(getString(L, 2)); + tmp += asLowerCaseString(Lua::getString(L, 2)); tmp += "s"; weapon->setVocationString(tmp); } } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -349,10 +382,10 @@ int WeaponFunctions::luaWeaponVocation(lua_State* L) { int WeaponFunctions::luaWeaponId(lua_State* L) { // weapon:id(id) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { - weapon->setID(getNumber(L, 2)); - pushBoolean(L, true); + weapon->setID(Lua::getNumber(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -361,12 +394,12 @@ int WeaponFunctions::luaWeaponId(lua_State* L) { int WeaponFunctions::luaWeaponAttack(lua_State* L) { // weapon:attack(atk) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.attack = getNumber(L, 2); - pushBoolean(L, true); + it.attack = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -375,15 +408,15 @@ int WeaponFunctions::luaWeaponAttack(lua_State* L) { int WeaponFunctions::luaWeaponDefense(lua_State* L) { // weapon:defense(defense[, extraDefense]) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.defense = getNumber(L, 2); + it.defense = Lua::getNumber(L, 2); if (lua_gettop(L) > 2) { - it.extraDefense = getNumber(L, 3); + it.extraDefense = Lua::getNumber(L, 3); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -392,12 +425,12 @@ int WeaponFunctions::luaWeaponDefense(lua_State* L) { int WeaponFunctions::luaWeaponRange(lua_State* L) { // weapon:range(range) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.shootRange = getNumber(L, 2); - pushBoolean(L, true); + it.shootRange = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -406,17 +439,17 @@ int WeaponFunctions::luaWeaponRange(lua_State* L) { int WeaponFunctions::luaWeaponCharges(lua_State* L) { // weapon:charges(charges[, showCharges = true]) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { bool showCharges = true; if (lua_gettop(L) > 2) { - showCharges = getBoolean(L, 3); + showCharges = Lua::getBoolean(L, 3); } const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.charges = getNumber(L, 2); + it.charges = Lua::getNumber(L, 2); it.showCharges = showCharges; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -425,17 +458,17 @@ int WeaponFunctions::luaWeaponCharges(lua_State* L) { int WeaponFunctions::luaWeaponDuration(lua_State* L) { // weapon:duration(duration[, showDuration = true]) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { bool showDuration = true; if (lua_gettop(L) > 2) { - showDuration = getBoolean(L, 3); + showDuration = Lua::getBoolean(L, 3); } const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.decayTime = getNumber(L, 2); + it.decayTime = Lua::getNumber(L, 2); it.showDuration = showDuration; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -444,16 +477,16 @@ int WeaponFunctions::luaWeaponDuration(lua_State* L) { int WeaponFunctions::luaWeaponDecayTo(lua_State* L) { // weapon:decayTo([itemid = 0] - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { uint16_t itemid = 0; if (lua_gettop(L) > 1) { - itemid = getNumber(L, 2); + itemid = Lua::getNumber(L, 2); } const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); it.decayTo = itemid; - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -462,12 +495,12 @@ int WeaponFunctions::luaWeaponDecayTo(lua_State* L) { int WeaponFunctions::luaWeaponTransformEquipTo(lua_State* L) { // weapon:transformEquipTo(itemid) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.transformEquipTo = getNumber(L, 2); - pushBoolean(L, true); + it.transformEquipTo = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -476,12 +509,12 @@ int WeaponFunctions::luaWeaponTransformEquipTo(lua_State* L) { int WeaponFunctions::luaWeaponTransformDeEquipTo(lua_State* L) { // weapon:transformDeEquipTo(itemid) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.transformDeEquipTo = getNumber(L, 2); - pushBoolean(L, true); + it.transformDeEquipTo = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -490,12 +523,12 @@ int WeaponFunctions::luaWeaponTransformDeEquipTo(lua_State* L) { int WeaponFunctions::luaWeaponShootType(lua_State* L) { // weapon:shootType(type) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.shootType = getNumber(L, 2); - pushBoolean(L, true); + it.shootType = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -504,18 +537,18 @@ int WeaponFunctions::luaWeaponShootType(lua_State* L) { int WeaponFunctions::luaWeaponSlotType(lua_State* L) { // weapon:slotType(slot) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - const std::string slot = getString(L, 2); + const std::string slot = Lua::getString(L, 2); if (slot == "two-handed") { it.slotPosition = SLOTP_TWO_HAND; } else { it.slotPosition = SLOTP_HAND; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -524,11 +557,11 @@ int WeaponFunctions::luaWeaponSlotType(lua_State* L) { int WeaponFunctions::luaWeaponAmmoType(lua_State* L) { // weapon:ammoType(type) - const auto &weapon = getUserdataShared(L, 1); + const auto &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - std::string type = getString(L, 2); + std::string type = Lua::getString(L, 2); if (type == "arrow") { it.ammoType = AMMO_ARROW; @@ -541,7 +574,7 @@ int WeaponFunctions::luaWeaponAmmoType(lua_State* L) { lua_pushnil(L); return 1; } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -550,12 +583,12 @@ int WeaponFunctions::luaWeaponAmmoType(lua_State* L) { int WeaponFunctions::luaWeaponHitChance(lua_State* L) { // weapon:hitChance(chance) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.hitChance = getNumber(L, 2); - pushBoolean(L, true); + it.hitChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -564,12 +597,12 @@ int WeaponFunctions::luaWeaponHitChance(lua_State* L) { int WeaponFunctions::luaWeaponMaxHitChance(lua_State* L) { // weapon:maxHitChance(max) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.maxHitChance = getNumber(L, 2); - pushBoolean(L, true); + it.maxHitChance = Lua::getNumber(L, 2); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -578,14 +611,14 @@ int WeaponFunctions::luaWeaponMaxHitChance(lua_State* L) { int WeaponFunctions::luaWeaponExtraElement(lua_State* L) { // weapon:extraElement(atk, combatType) - const WeaponShared_ptr &weapon = getUserdataShared(L, 1); + const WeaponShared_ptr &weapon = Lua::getUserdataShared(L, 1); if (weapon) { const uint16_t id = weapon->getID(); const ItemType &it = Item::items.getItemType(id); - it.abilities->elementDamage = getNumber(L, 2); + it.abilities->elementDamage = Lua::getNumber(L, 2); - if (!getNumber(L, 3)) { - std::string element = getString(L, 3); + if (!Lua::getNumber(L, 3)) { + std::string element = Lua::getString(L, 3); const std::string tmpStrValue = asLowerCaseString(element); if (tmpStrValue == "earth") { it.abilities->elementType = COMBAT_EARTHDAMAGE; @@ -605,9 +638,9 @@ int WeaponFunctions::luaWeaponExtraElement(lua_State* L) { element); } } else { - it.abilities->elementType = getNumber(L, 3); + it.abilities->elementType = Lua::getNumber(L, 3); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } diff --git a/src/lua/functions/items/weapon_functions.hpp b/src/lua/functions/items/weapon_functions.hpp index d68146f82bc..971ff286036 100644 --- a/src/lua/functions/items/weapon_functions.hpp +++ b/src/lua/functions/items/weapon_functions.hpp @@ -9,56 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class WeaponFunctions final : LuaScriptInterface { +class WeaponFunctions { public: - explicit WeaponFunctions(lua_State* L) : - LuaScriptInterface("WeaponFunctions") { - init(L); - } - ~WeaponFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Weapon", "", WeaponFunctions::luaCreateWeapon); - registerMethod(L, "Weapon", "action", WeaponFunctions::luaWeaponAction); - registerMethod(L, "Weapon", "register", WeaponFunctions::luaWeaponRegister); - registerMethod(L, "Weapon", "id", WeaponFunctions::luaWeaponId); - registerMethod(L, "Weapon", "level", WeaponFunctions::luaWeaponLevel); - registerMethod(L, "Weapon", "magicLevel", WeaponFunctions::luaWeaponMagicLevel); - registerMethod(L, "Weapon", "mana", WeaponFunctions::luaWeaponMana); - registerMethod(L, "Weapon", "manaPercent", WeaponFunctions::luaWeaponManaPercent); - registerMethod(L, "Weapon", "health", WeaponFunctions::luaWeaponHealth); - registerMethod(L, "Weapon", "healthPercent", WeaponFunctions::luaWeaponHealthPercent); - registerMethod(L, "Weapon", "soul", WeaponFunctions::luaWeaponSoul); - registerMethod(L, "Weapon", "breakChance", WeaponFunctions::luaWeaponBreakChance); - registerMethod(L, "Weapon", "premium", WeaponFunctions::luaWeaponPremium); - registerMethod(L, "Weapon", "wieldUnproperly", WeaponFunctions::luaWeaponUnproperly); - registerMethod(L, "Weapon", "vocation", WeaponFunctions::luaWeaponVocation); - registerMethod(L, "Weapon", "onUseWeapon", WeaponFunctions::luaWeaponOnUseWeapon); - registerMethod(L, "Weapon", "element", WeaponFunctions::luaWeaponElement); - registerMethod(L, "Weapon", "attack", WeaponFunctions::luaWeaponAttack); - registerMethod(L, "Weapon", "defense", WeaponFunctions::luaWeaponDefense); - registerMethod(L, "Weapon", "range", WeaponFunctions::luaWeaponRange); - registerMethod(L, "Weapon", "charges", WeaponFunctions::luaWeaponCharges); - registerMethod(L, "Weapon", "duration", WeaponFunctions::luaWeaponDuration); - registerMethod(L, "Weapon", "decayTo", WeaponFunctions::luaWeaponDecayTo); - registerMethod(L, "Weapon", "transformEquipTo", WeaponFunctions::luaWeaponTransformEquipTo); - registerMethod(L, "Weapon", "transformDeEquipTo", WeaponFunctions::luaWeaponTransformDeEquipTo); - registerMethod(L, "Weapon", "slotType", WeaponFunctions::luaWeaponSlotType); - registerMethod(L, "Weapon", "hitChance", WeaponFunctions::luaWeaponHitChance); - registerMethod(L, "Weapon", "extraElement", WeaponFunctions::luaWeaponExtraElement); - - // exclusively for distance weapons - registerMethod(L, "Weapon", "ammoType", WeaponFunctions::luaWeaponAmmoType); - registerMethod(L, "Weapon", "maxHitChance", WeaponFunctions::luaWeaponMaxHitChance); - - // exclusively for wands - registerMethod(L, "Weapon", "damage", WeaponFunctions::luaWeaponWandDamage); - - // exclusively for wands & distance weapons - registerMethod(L, "Weapon", "shootType", WeaponFunctions::luaWeaponShootType); - } + static void init(lua_State* L); private: static int luaCreateWeapon(lua_State* L); diff --git a/src/lua/functions/lua_functions_loader.cpp b/src/lua/functions/lua_functions_loader.cpp index 894cf252c80..d4aeac2a7f1 100644 --- a/src/lua/functions/lua_functions_loader.cpp +++ b/src/lua/functions/lua_functions_loader.cpp @@ -30,7 +30,7 @@ class LuaScriptInterface; -void LuaFunctionsLoader::load(lua_State* L) { +void Lua::load(lua_State* L) { if (!L) { g_game().dieSafely("Invalid lua state, cannot load lua functions."); } @@ -45,7 +45,7 @@ void LuaFunctionsLoader::load(lua_State* L) { ZoneFunctions::init(L); } -std::string LuaFunctionsLoader::getErrorDesc(ErrorCode_t code) { +std::string Lua::getErrorDesc(ErrorCode_t code) { switch (code) { case LUA_ERROR_PLAYER_NOT_FOUND: return "Player not found"; @@ -92,7 +92,7 @@ std::string LuaFunctionsLoader::getErrorDesc(ErrorCode_t code) { } } -int LuaFunctionsLoader::protectedCall(lua_State* L, int nargs, int nresults) { +int Lua::protectedCall(lua_State* L, int nargs, int nresults) { if (const int ret = validateDispatcherContext(__FUNCTION__); ret != 0) { return ret; } @@ -106,7 +106,7 @@ int LuaFunctionsLoader::protectedCall(lua_State* L, int nargs, int nresults) { return ret; } -void LuaFunctionsLoader::reportError(const char* function, const std::string &error_desc, bool stack_trace /* = false*/) { +void Lua::reportError(const char* function, const std::string &error_desc, bool stack_trace /* = false*/) { int32_t scriptId; int32_t callbackId; bool timerEvent; @@ -144,7 +144,7 @@ void LuaFunctionsLoader::reportError(const char* function, const std::string &er g_logger().error(logMsg.str()); } -int LuaFunctionsLoader::luaErrorHandler(lua_State* L) { +int Lua::luaErrorHandler(lua_State* L) { const std::string &errorMessage = popString(L); const auto interface = getScriptEnv()->getScriptInterface(); if (!interface) { @@ -157,7 +157,7 @@ int LuaFunctionsLoader::luaErrorHandler(lua_State* L) { return 1; } -void LuaFunctionsLoader::pushVariant(lua_State* L, const LuaVariant &var) { +void Lua::pushVariant(lua_State* L, const LuaVariant &var) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -185,7 +185,7 @@ void LuaFunctionsLoader::pushVariant(lua_State* L, const LuaVariant &var) { setMetatable(L, -1, "Variant"); } -void LuaFunctionsLoader::pushThing(lua_State* L, const std::shared_ptr &thing) { +void Lua::pushThing(lua_State* L, const std::shared_ptr &thing) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -210,7 +210,7 @@ void LuaFunctionsLoader::pushThing(lua_State* L, const std::shared_ptr &t } } -void LuaFunctionsLoader::pushCylinder(lua_State* L, const std::shared_ptr &cylinder) { +void Lua::pushCylinder(lua_State* L, const std::shared_ptr &cylinder) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -231,7 +231,7 @@ void LuaFunctionsLoader::pushCylinder(lua_State* L, const std::shared_ptr weakObjectTypes; if (validateDispatcherContext(__FUNCTION__)) { return; @@ -309,7 +317,7 @@ void LuaFunctionsLoader::setWeakMetatable(lua_State* L, int32_t index, const std lua_setmetatable(L, index - 1); } -void LuaFunctionsLoader::setItemMetatable(lua_State* L, int32_t index, const std::shared_ptr &item) { +void Lua::setItemMetatable(lua_State* L, int32_t index, const std::shared_ptr &item) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -324,7 +332,7 @@ void LuaFunctionsLoader::setItemMetatable(lua_State* L, int32_t index, const std lua_setmetatable(L, index - 1); } -void LuaFunctionsLoader::setCreatureMetatable(lua_State* L, int32_t index, const std::shared_ptr &creature) { +void Lua::setCreatureMetatable(lua_State* L, int32_t index, const std::shared_ptr &creature) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -339,7 +347,7 @@ void LuaFunctionsLoader::setCreatureMetatable(lua_State* L, int32_t index, const lua_setmetatable(L, index - 1); } -CombatDamage LuaFunctionsLoader::getCombatDamage(lua_State* L) { +CombatDamage Lua::getCombatDamage(lua_State* L) { CombatDamage damage; damage.primary.value = getNumber(L, -4); damage.primary.type = getNumber(L, -3); @@ -351,7 +359,7 @@ CombatDamage LuaFunctionsLoader::getCombatDamage(lua_State* L) { } // Get -std::string LuaFunctionsLoader::getFormatedLoggerMessage(lua_State* L) { +std::string Lua::getFormatedLoggerMessage(lua_State* L) { const std::string format = getString(L, 1); const int n = lua_gettop(L); fmt::dynamic_format_arg_store args; @@ -387,7 +395,7 @@ std::string LuaFunctionsLoader::getFormatedLoggerMessage(lua_State* L) { return {}; } -std::string LuaFunctionsLoader::getString(lua_State* L, int32_t arg) { +std::string Lua::getString(lua_State* L, int32_t arg) { size_t len; const char* c_str = lua_tolstring(L, arg, &len); if (!c_str || len == 0) { @@ -396,7 +404,7 @@ std::string LuaFunctionsLoader::getString(lua_State* L, int32_t arg) { return std::string(c_str, len); } -Position LuaFunctionsLoader::getPosition(lua_State* L, int32_t arg, int32_t &stackpos) { +Position Lua::getPosition(lua_State* L, int32_t arg, int32_t &stackpos) { Position position; position.x = getField(L, arg, "x"); position.y = getField(L, arg, "y"); @@ -413,7 +421,7 @@ Position LuaFunctionsLoader::getPosition(lua_State* L, int32_t arg, int32_t &sta return position; } -Position LuaFunctionsLoader::getPosition(lua_State* L, int32_t arg) { +Position Lua::getPosition(lua_State* L, int32_t arg) { Position position; position.x = getField(L, arg, "x"); position.y = getField(L, arg, "y"); @@ -423,7 +431,7 @@ Position LuaFunctionsLoader::getPosition(lua_State* L, int32_t arg) { return position; } -Outfit_t LuaFunctionsLoader::getOutfit(lua_State* L, int32_t arg) { +Outfit_t Lua::getOutfit(lua_State* L, int32_t arg) { Outfit_t outfit; outfit.lookMountFeet = getField(L, arg, "lookMountFeet"); outfit.lookMountLegs = getField(L, arg, "lookMountLegs"); @@ -445,7 +453,7 @@ Outfit_t LuaFunctionsLoader::getOutfit(lua_State* L, int32_t arg) { return outfit; } -LuaVariant LuaFunctionsLoader::getVariant(lua_State* L, int32_t arg) { +LuaVariant Lua::getVariant(lua_State* L, int32_t arg) { LuaVariant var; var.instantName = getFieldString(L, arg, "instantName"); var.runeName = getFieldString(L, arg, "runeName"); @@ -479,7 +487,7 @@ LuaVariant LuaFunctionsLoader::getVariant(lua_State* L, int32_t arg) { return var; } -std::shared_ptr LuaFunctionsLoader::getThing(lua_State* L, int32_t arg) { +std::shared_ptr Lua::getThing(lua_State* L, int32_t arg) { std::shared_ptr thing; if (lua_getmetatable(L, arg) != 0) { lua_rawgeti(L, -1, 't'); @@ -513,14 +521,14 @@ std::shared_ptr LuaFunctionsLoader::getThing(lua_State* L, int32_t arg) { return thing; } -std::shared_ptr LuaFunctionsLoader::getCreature(lua_State* L, int32_t arg) { +std::shared_ptr Lua::getCreature(lua_State* L, int32_t arg) { if (isUserdata(L, arg)) { return getUserdataShared(L, arg); } return g_game().getCreatureByID(getNumber(L, arg)); } -std::shared_ptr LuaFunctionsLoader::getPlayer(lua_State* L, int32_t arg, bool allowOffline /* = false */) { +std::shared_ptr Lua::getPlayer(lua_State* L, int32_t arg, bool allowOffline /* = false */) { if (isUserdata(L, arg)) { return getUserdataShared(L, arg); } else if (isNumber(L, arg)) { @@ -528,11 +536,11 @@ std::shared_ptr LuaFunctionsLoader::getPlayer(lua_State* L, int32_t arg, } else if (isString(L, arg)) { return g_game().getPlayerByName(getString(L, arg), allowOffline); } - g_logger().warn("LuaFunctionsLoader::getPlayer: Invalid argument."); + g_logger().warn("Lua::getPlayer: Invalid argument."); return nullptr; } -std::shared_ptr LuaFunctionsLoader::getGuild(lua_State* L, int32_t arg, bool allowOffline /* = false */) { +std::shared_ptr Lua::getGuild(lua_State* L, int32_t arg, bool allowOffline /* = false */) { if (isUserdata(L, arg)) { return getUserdataShared(L, arg); } else if (isNumber(L, arg)) { @@ -540,16 +548,16 @@ std::shared_ptr LuaFunctionsLoader::getGuild(lua_State* L, int32_t arg, b } else if (isString(L, arg)) { return g_game().getGuildByName(getString(L, arg), allowOffline); } - g_logger().warn("LuaFunctionsLoader::getGuild: Invalid argument."); + g_logger().warn("Lua::getGuild: Invalid argument."); return nullptr; } -std::string LuaFunctionsLoader::getFieldString(lua_State* L, int32_t arg, const std::string &key) { +std::string Lua::getFieldString(lua_State* L, int32_t arg, const std::string &key) { lua_getfield(L, arg, key.c_str()); return getString(L, -1); } -LuaData_t LuaFunctionsLoader::getUserdataType(lua_State* L, int32_t arg) { +LuaData_t Lua::getUserdataType(lua_State* L, int32_t arg) { if (lua_getmetatable(L, arg) == 0) { return LuaData_t::Unknown; } @@ -561,12 +569,12 @@ LuaData_t LuaFunctionsLoader::getUserdataType(lua_State* L, int32_t arg) { return type; } -std::string LuaFunctionsLoader::getUserdataTypeName(LuaData_t userType) { +std::string Lua::getUserdataTypeName(LuaData_t userType) { return magic_enum::enum_name(userType).data(); } // Push -void LuaFunctionsLoader::pushBoolean(lua_State* L, bool value) { +void Lua::pushBoolean(lua_State* L, bool value) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -574,7 +582,7 @@ void LuaFunctionsLoader::pushBoolean(lua_State* L, bool value) { lua_pushboolean(L, value ? 1 : 0); } -void LuaFunctionsLoader::pushCombatDamage(lua_State* L, const CombatDamage &damage) { +void Lua::pushCombatDamage(lua_State* L, const CombatDamage &damage) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -586,7 +594,7 @@ void LuaFunctionsLoader::pushCombatDamage(lua_State* L, const CombatDamage &dama lua_pushnumber(L, damage.origin); } -void LuaFunctionsLoader::pushInstantSpell(lua_State* L, const InstantSpell &spell) { +void Lua::pushInstantSpell(lua_State* L, const InstantSpell &spell) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -603,7 +611,7 @@ void LuaFunctionsLoader::pushInstantSpell(lua_State* L, const InstantSpell &spel setMetatable(L, -1, "Spell"); } -void LuaFunctionsLoader::pushPosition(lua_State* L, const Position &position, int32_t stackpos /* = 0*/) { +void Lua::pushPosition(lua_State* L, const Position &position, int32_t stackpos /* = 0*/) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -618,7 +626,7 @@ void LuaFunctionsLoader::pushPosition(lua_State* L, const Position &position, in setMetatable(L, -1, "Position"); } -void LuaFunctionsLoader::pushOutfit(lua_State* L, const Outfit_t &outfit) { +void Lua::pushOutfit(lua_State* L, const Outfit_t &outfit) { if (validateDispatcherContext(__FUNCTION__)) { return; } @@ -639,7 +647,7 @@ void LuaFunctionsLoader::pushOutfit(lua_State* L, const Outfit_t &outfit) { setField(L, "lookFamiliarsType", outfit.lookFamiliarsType); } -void LuaFunctionsLoader::registerClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction /* = nullptr*/) { +void Lua::registerClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction /* = nullptr*/) { // className = {} lua_newtable(L); lua_pushvalue(L, -1); @@ -701,7 +709,7 @@ void LuaFunctionsLoader::registerClass(lua_State* L, const std::string &classNam lua_pop(L, 2); } -void LuaFunctionsLoader::registerMethod(lua_State* L, const std::string &globalName, const std::string &methodName, lua_CFunction func) { +void Lua::registerMethod(lua_State* L, const std::string &globalName, const std::string &methodName, lua_CFunction func) { // globalName.methodName = func lua_getglobal(L, globalName.c_str()); lua_pushcfunction(L, func); @@ -711,13 +719,13 @@ void LuaFunctionsLoader::registerMethod(lua_State* L, const std::string &globalN lua_pop(L, 1); } -void LuaFunctionsLoader::registerTable(lua_State* L, const std::string &tableName) { +void Lua::registerTable(lua_State* L, const std::string &tableName) { // _G[tableName] = {} lua_newtable(L); lua_setglobal(L, tableName.c_str()); } -void LuaFunctionsLoader::registerMetaMethod(lua_State* L, const std::string &className, const std::string &methodName, lua_CFunction func) { +void Lua::registerMetaMethod(lua_State* L, const std::string &className, const std::string &methodName, lua_CFunction func) { // className.metatable.methodName = func luaL_getmetatable(L, className.c_str()); lua_pushcfunction(L, func); @@ -727,7 +735,7 @@ void LuaFunctionsLoader::registerMetaMethod(lua_State* L, const std::string &cla lua_pop(L, 1); } -void LuaFunctionsLoader::registerVariable(lua_State* L, const std::string &tableName, const std::string &name, lua_Number value) { +void Lua::registerVariable(lua_State* L, const std::string &tableName, const std::string &name, lua_Number value) { // tableName.name = value lua_getglobal(L, tableName.c_str()); setField(L, name.c_str(), value); @@ -736,31 +744,31 @@ void LuaFunctionsLoader::registerVariable(lua_State* L, const std::string &table lua_pop(L, 1); } -void LuaFunctionsLoader::registerGlobalBoolean(lua_State* L, const std::string &name, bool value) { +void Lua::registerGlobalBoolean(lua_State* L, const std::string &name, bool value) { // _G[name] = value pushBoolean(L, value); lua_setglobal(L, name.c_str()); } -void LuaFunctionsLoader::registerGlobalMethod(lua_State* L, const std::string &functionName, lua_CFunction func) { +void Lua::registerGlobalMethod(lua_State* L, const std::string &functionName, lua_CFunction func) { // _G[functionName] = func lua_pushcfunction(L, func); lua_setglobal(L, functionName.c_str()); } -void LuaFunctionsLoader::registerGlobalVariable(lua_State* L, const std::string &name, lua_Number value) { +void Lua::registerGlobalVariable(lua_State* L, const std::string &name, lua_Number value) { // _G[name] = value lua_pushnumber(L, value); lua_setglobal(L, name.c_str()); } -void LuaFunctionsLoader::registerGlobalString(lua_State* L, const std::string &variable, const std::string &name) { +void Lua::registerGlobalString(lua_State* L, const std::string &variable, const std::string &name) { // Example: registerGlobalString(L, "VARIABLE_NAME", "variable string"); pushString(L, name); lua_setglobal(L, variable.c_str()); } -std::string LuaFunctionsLoader::escapeString(const std::string &string) { +std::string Lua::escapeString(const std::string &string) { std::string s = string; replaceString(s, "\\", "\\\\"); replaceString(s, "\"", "\\\""); @@ -769,17 +777,17 @@ std::string LuaFunctionsLoader::escapeString(const std::string &string) { return s; } -int LuaFunctionsLoader::luaUserdataCompare(lua_State* L) { +int Lua::luaUserdataCompare(lua_State* L) { pushBoolean(L, getUserdata(L, 1) == getUserdata(L, 2)); return 1; } -void LuaFunctionsLoader::registerSharedClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction) { +void Lua::registerSharedClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction) { registerClass(L, className, baseClass, newFunction); registerMetaMethod(L, className, "__gc", luaGarbageCollection); } -int LuaFunctionsLoader::luaGarbageCollection(lua_State* L) { +int Lua::luaGarbageCollection(lua_State* L) { const auto objPtr = static_cast*>(lua_touserdata(L, 1)); if (objPtr) { objPtr->reset(); @@ -787,7 +795,7 @@ int LuaFunctionsLoader::luaGarbageCollection(lua_State* L) { return 0; } -int LuaFunctionsLoader::validateDispatcherContext(std::string_view fncName) { +int Lua::validateDispatcherContext(std::string_view fncName) { if (DispatcherContext::isOn() && g_dispatcher().context().isAsync()) { g_logger().warn("[{}] The call to lua was ignored because the '{}' task is trying to communicate while in async mode.", fncName, g_dispatcher().context().getName()); return LUA_ERRRUN; diff --git a/src/lua/functions/lua_functions_loader.hpp b/src/lua/functions/lua_functions_loader.hpp index 9c5b65c14bb..fffc7e5b355 100644 --- a/src/lua/functions/lua_functions_loader.hpp +++ b/src/lua/functions/lua_functions_loader.hpp @@ -26,11 +26,13 @@ class Guild; class Zone; class KV; +using lua_Number = double; + struct LuaVariant; #define reportErrorFunc(a) reportError(__FUNCTION__, a, true) -class LuaFunctionsLoader { +class Lua { public: static void load(lua_State* L); @@ -42,6 +44,7 @@ class LuaFunctionsLoader { static void pushThing(lua_State* L, const std::shared_ptr &thing); static void pushVariant(lua_State* L, const LuaVariant &var); static void pushString(lua_State* L, const std::string &value); + static void pushNumber(lua_State* L, lua_Number value); static void pushCallback(lua_State* L, int32_t callback); static void pushCylinder(lua_State* L, const std::shared_ptr &cylinder); @@ -60,23 +63,29 @@ class LuaFunctionsLoader { static void setCreatureMetatable(lua_State* L, int32_t index, const std::shared_ptr &creature); template - static std::enable_if_t, T> - getNumber(lua_State* L, int32_t arg) { - return static_cast(static_cast(lua_tonumber(L, arg))); - } - template - static std::enable_if_t || std::is_floating_point_v, T> getNumber(lua_State* L, int32_t arg) { + static T getNumber(lua_State* L, int32_t arg) { auto number = lua_tonumber(L, arg); - // If there is overflow, we return the value 0 - if constexpr (std::is_integral_v && std::is_unsigned_v) { - if (number < 0) { - g_logger().debug("[{}] overflow, setting to default signed value (0)", __FUNCTION__); - number = T(0); + + if constexpr (std::is_enum_v) { + return static_cast(static_cast(number)); + } + + if constexpr (std::is_integral_v) { + if constexpr (std::is_unsigned_v) { + if (number < 0) { + g_logger().debug("[{}] overflow, setting to default unsigned value (0)", __FUNCTION__); + return T(0); + } } + return static_cast(number); + } + if constexpr (std::is_floating_point_v) { + return static_cast(number); } - return static_cast(number); + return T {}; } + template static T getNumber(lua_State* L, int32_t arg, T defaultValue) { const auto parameters = lua_gettop(L); @@ -228,7 +237,6 @@ class LuaFunctionsLoader { new (userData) std::shared_ptr(value); } -protected: static void registerClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction = nullptr); static void registerSharedClass(lua_State* L, const std::string &className, const std::string &baseClass, lua_CFunction newFunction = nullptr); static void registerMethod(lua_State* L, const std::string &globalName, const std::string &methodName, lua_CFunction func); diff --git a/src/lua/functions/map/house_functions.cpp b/src/lua/functions/map/house_functions.cpp index 52ca752587b..10ebbc2cf0c 100644 --- a/src/lua/functions/map/house_functions.cpp +++ b/src/lua/functions/map/house_functions.cpp @@ -16,12 +16,50 @@ #include "io/iologindata.hpp" #include "map/house/house.hpp" #include "creatures/players/player.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void HouseFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "House", "", HouseFunctions::luaHouseCreate); + Lua::registerMetaMethod(L, "House", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "House", "getId", HouseFunctions::luaHouseGetId); + Lua::registerMethod(L, "House", "getName", HouseFunctions::luaHouseGetName); + Lua::registerMethod(L, "House", "getTown", HouseFunctions::luaHouseGetTown); + Lua::registerMethod(L, "House", "getExitPosition", HouseFunctions::luaHouseGetExitPosition); + Lua::registerMethod(L, "House", "getRent", HouseFunctions::luaHouseGetRent); + Lua::registerMethod(L, "House", "getPrice", HouseFunctions::luaHouseGetPrice); + + Lua::registerMethod(L, "House", "getOwnerGuid", HouseFunctions::luaHouseGetOwnerGuid); + Lua::registerMethod(L, "House", "setHouseOwner", HouseFunctions::luaHouseSetHouseOwner); + Lua::registerMethod(L, "House", "setNewOwnerGuid", HouseFunctions::luaHouseSetNewOwnerGuid); + Lua::registerMethod(L, "House", "hasItemOnTile", HouseFunctions::luaHouseHasItemOnTile); + Lua::registerMethod(L, "House", "hasNewOwnership", HouseFunctions::luaHouseHasNewOwnership); + Lua::registerMethod(L, "House", "startTrade", HouseFunctions::luaHouseStartTrade); + + Lua::registerMethod(L, "House", "getBeds", HouseFunctions::luaHouseGetBeds); + Lua::registerMethod(L, "House", "getBedCount", HouseFunctions::luaHouseGetBedCount); + + Lua::registerMethod(L, "House", "getDoors", HouseFunctions::luaHouseGetDoors); + Lua::registerMethod(L, "House", "getDoorCount", HouseFunctions::luaHouseGetDoorCount); + Lua::registerMethod(L, "House", "getDoorIdByPosition", HouseFunctions::luaHouseGetDoorIdByPosition); + + Lua::registerMethod(L, "House", "getTiles", HouseFunctions::luaHouseGetTiles); + Lua::registerMethod(L, "House", "getItems", HouseFunctions::luaHouseGetItems); + Lua::registerMethod(L, "House", "getTileCount", HouseFunctions::luaHouseGetTileCount); + + Lua::registerMethod(L, "House", "canEditAccessList", HouseFunctions::luaHouseCanEditAccessList); + Lua::registerMethod(L, "House", "getAccessList", HouseFunctions::luaHouseGetAccessList); + Lua::registerMethod(L, "House", "setAccessList", HouseFunctions::luaHouseSetAccessList); + + Lua::registerMethod(L, "House", "kickPlayer", HouseFunctions::luaHouseKickPlayer); + Lua::registerMethod(L, "House", "isInvited", HouseFunctions::luaHouseIsInvited); +} int HouseFunctions::luaHouseCreate(lua_State* L) { // House(id) - if (const auto &house = g_game().map.houses.getHouse(getNumber(L, 2))) { - pushUserdata(L, house); - setMetatable(L, -1, "House"); + if (const auto &house = g_game().map.houses.getHouse(Lua::getNumber(L, 2))) { + Lua::pushUserdata(L, house); + Lua::setMetatable(L, -1, "House"); } else { lua_pushnil(L); } @@ -30,7 +68,7 @@ int HouseFunctions::luaHouseCreate(lua_State* L) { int HouseFunctions::luaHouseGetId(lua_State* L) { // house:getId() - if (const auto &house = getUserdataShared(L, 1)) { + if (const auto &house = Lua::getUserdataShared(L, 1)) { lua_pushnumber(L, house->getId()); } else { lua_pushnil(L); @@ -40,8 +78,8 @@ int HouseFunctions::luaHouseGetId(lua_State* L) { int HouseFunctions::luaHouseGetName(lua_State* L) { // house:getName() - if (const auto &house = getUserdataShared(L, 1)) { - pushString(L, house->getName()); + if (const auto &house = Lua::getUserdataShared(L, 1)) { + Lua::pushString(L, house->getName()); } else { lua_pushnil(L); } @@ -50,15 +88,15 @@ int HouseFunctions::luaHouseGetName(lua_State* L) { int HouseFunctions::luaHouseGetTown(lua_State* L) { // house:getTown() - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; } if (const auto &town = g_game().map.towns.getTown(house->getTownId())) { - pushUserdata(L, town); - setMetatable(L, -1, "Town"); + Lua::pushUserdata(L, town); + Lua::setMetatable(L, -1, "Town"); } else { lua_pushnil(L); } @@ -67,8 +105,8 @@ int HouseFunctions::luaHouseGetTown(lua_State* L) { int HouseFunctions::luaHouseGetExitPosition(lua_State* L) { // house:getExitPosition() - if (const auto &house = getUserdataShared(L, 1)) { - pushPosition(L, house->getEntryPosition()); + if (const auto &house = Lua::getUserdataShared(L, 1)) { + Lua::pushPosition(L, house->getEntryPosition()); } else { lua_pushnil(L); } @@ -77,7 +115,7 @@ int HouseFunctions::luaHouseGetExitPosition(lua_State* L) { int HouseFunctions::luaHouseGetRent(lua_State* L) { // house:getRent() - if (const auto &house = getUserdataShared(L, 1)) { + if (const auto &house = Lua::getUserdataShared(L, 1)) { lua_pushnumber(L, house->getRent()); } else { lua_pushnil(L); @@ -87,9 +125,9 @@ int HouseFunctions::luaHouseGetRent(lua_State* L) { int HouseFunctions::luaHouseGetPrice(lua_State* L) { // house:getPrice() - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { - reportErrorFunc("House not found"); + Lua::reportErrorFunc("House not found"); lua_pushnumber(L, 0); return 1; } @@ -100,7 +138,7 @@ int HouseFunctions::luaHouseGetPrice(lua_State* L) { int HouseFunctions::luaHouseGetOwnerGuid(lua_State* L) { // house:getOwnerGuid() - if (const auto &house = getUserdataShared(L, 1)) { + if (const auto &house = Lua::getUserdataShared(L, 1)) { lua_pushnumber(L, house->getOwner()); } else { lua_pushnil(L); @@ -110,23 +148,23 @@ int HouseFunctions::luaHouseGetOwnerGuid(lua_State* L) { int HouseFunctions::luaHouseSetHouseOwner(lua_State* L) { // house:setHouseOwner(guid[, updateDatabase = true]) - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { - reportErrorFunc("House not found"); + Lua::reportErrorFunc("House not found"); lua_pushnil(L); return 1; } - const uint32_t guid = getNumber(L, 2); - const bool updateDatabase = getBoolean(L, 3, true); + const uint32_t guid = Lua::getNumber(L, 2); + const bool updateDatabase = Lua::getBoolean(L, 3, true); house->setOwner(guid, updateDatabase); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int HouseFunctions::luaHouseSetNewOwnerGuid(lua_State* L) { // house:setNewOwnerGuid(guid[, updateDatabase = true]) - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (house) { auto isTransferOnRestart = g_configManager().getBoolean(TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART); if (isTransferOnRestart && house->hasNewOwnership()) { @@ -138,9 +176,9 @@ int HouseFunctions::luaHouseSetNewOwnerGuid(lua_State* L) { return 1; } - const auto guid = getNumber(L, 2, 0); + const auto guid = Lua::getNumber(L, 2, 0); house->setNewOwnerGuid(guid, false); - pushBoolean(L, true); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } @@ -149,36 +187,36 @@ int HouseFunctions::luaHouseSetNewOwnerGuid(lua_State* L) { int HouseFunctions::luaHouseHasItemOnTile(lua_State* L) { // house:hasItemOnTile() - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { - reportErrorFunc("House not found"); + Lua::reportErrorFunc("House not found"); lua_pushnil(L); return 1; } - pushBoolean(L, house->hasItemOnTile()); + Lua::pushBoolean(L, house->hasItemOnTile()); return 1; } int HouseFunctions::luaHouseHasNewOwnership(lua_State* L) { // house:hasNewOwnership(guid) - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { - reportErrorFunc("House not found"); + Lua::reportErrorFunc("House not found"); lua_pushnil(L); return 1; } auto isTransferOnRestart = g_configManager().getBoolean(TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART); - pushBoolean(L, isTransferOnRestart && house->hasNewOwnership()); + Lua::pushBoolean(L, isTransferOnRestart && house->hasNewOwnership()); return 1; } int HouseFunctions::luaHouseStartTrade(lua_State* L) { // house:startTrade(player, tradePartner) - const auto &house = getUserdataShared(L, 1); - const auto &player = getUserdataShared(L, 2); - const auto &tradePartner = getUserdataShared(L, 3); + const auto &house = Lua::getUserdataShared(L, 1); + const auto &player = Lua::getUserdataShared(L, 2); + const auto &tradePartner = Lua::getUserdataShared(L, 3); if (!player || !tradePartner || !house) { lua_pushnil(L); @@ -230,7 +268,7 @@ int HouseFunctions::luaHouseStartTrade(lua_State* L) { int HouseFunctions::luaHouseGetBeds(lua_State* L) { // house:getBeds() - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; @@ -241,8 +279,8 @@ int HouseFunctions::luaHouseGetBeds(lua_State* L) { int index = 0; for (const auto &bedItem : beds) { - pushUserdata(L, bedItem); - setItemMetatable(L, -1, bedItem); + Lua::pushUserdata(L, bedItem); + Lua::setItemMetatable(L, -1, bedItem); lua_rawseti(L, -2, ++index); } return 1; @@ -250,7 +288,7 @@ int HouseFunctions::luaHouseGetBeds(lua_State* L) { int HouseFunctions::luaHouseGetBedCount(lua_State* L) { // house:getBedCount() - if (const auto &house = getUserdataShared(L, 1)) { + if (const auto &house = Lua::getUserdataShared(L, 1)) { lua_pushnumber(L, house->getBedCount()); } else { lua_pushnil(L); @@ -260,7 +298,7 @@ int HouseFunctions::luaHouseGetBedCount(lua_State* L) { int HouseFunctions::luaHouseGetDoors(lua_State* L) { // house:getDoors() - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; @@ -271,8 +309,8 @@ int HouseFunctions::luaHouseGetDoors(lua_State* L) { int index = 0; for (const auto &door : doors) { - pushUserdata(L, door); - setItemMetatable(L, -1, door); + Lua::pushUserdata(L, door); + Lua::setItemMetatable(L, -1, door); lua_rawseti(L, -2, ++index); } return 1; @@ -280,7 +318,7 @@ int HouseFunctions::luaHouseGetDoors(lua_State* L) { int HouseFunctions::luaHouseGetDoorCount(lua_State* L) { // house:getDoorCount() - if (const auto &house = getUserdataShared(L, 1)) { + if (const auto &house = Lua::getUserdataShared(L, 1)) { lua_pushnumber(L, house->getDoors().size()); } else { lua_pushnil(L); @@ -290,13 +328,13 @@ int HouseFunctions::luaHouseGetDoorCount(lua_State* L) { int HouseFunctions::luaHouseGetDoorIdByPosition(lua_State* L) { // house:getDoorIdByPosition(position) - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; } - const auto &door = house->getDoorByPosition(getPosition(L, 2)); + const auto &door = house->getDoorByPosition(Lua::getPosition(L, 2)); if (door) { lua_pushnumber(L, door->getDoorId()); } else { @@ -307,7 +345,7 @@ int HouseFunctions::luaHouseGetDoorIdByPosition(lua_State* L) { int HouseFunctions::luaHouseGetTiles(lua_State* L) { // house:getTiles() - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; @@ -318,8 +356,8 @@ int HouseFunctions::luaHouseGetTiles(lua_State* L) { int index = 0; for (const auto &tile : tiles) { - pushUserdata(L, tile); - setMetatable(L, -1, "Tile"); + Lua::pushUserdata(L, tile); + Lua::setMetatable(L, -1, "Tile"); lua_rawseti(L, -2, ++index); } return 1; @@ -327,7 +365,7 @@ int HouseFunctions::luaHouseGetTiles(lua_State* L) { int HouseFunctions::luaHouseGetItems(lua_State* L) { // house:getItems() - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; @@ -341,8 +379,8 @@ int HouseFunctions::luaHouseGetItems(lua_State* L) { const TileItemVector* itemVector = tile->getItemList(); if (itemVector) { for (const auto &item : *itemVector) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); lua_rawseti(L, -2, ++index); } } @@ -352,7 +390,7 @@ int HouseFunctions::luaHouseGetItems(lua_State* L) { int HouseFunctions::luaHouseGetTileCount(lua_State* L) { // house:getTileCount() - if (const auto &house = getUserdataShared(L, 1)) { + if (const auto &house = Lua::getUserdataShared(L, 1)) { lua_pushnumber(L, house->getTiles().size()); } else { lua_pushnil(L); @@ -362,95 +400,95 @@ int HouseFunctions::luaHouseGetTileCount(lua_State* L) { int HouseFunctions::luaHouseCanEditAccessList(lua_State* L) { // house:canEditAccessList(listId, player) - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; } - const uint32_t listId = getNumber(L, 2); + const uint32_t listId = Lua::getNumber(L, 2); - const auto &player = getPlayer(L, 3); + const auto &player = Lua::getPlayer(L, 3); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - pushBoolean(L, house->canEditAccessList(listId, player)); + Lua::pushBoolean(L, house->canEditAccessList(listId, player)); return 1; } int HouseFunctions::luaHouseGetAccessList(lua_State* L) { // house:getAccessList(listId) - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; } std::string list; - const uint32_t listId = getNumber(L, 2); + const uint32_t listId = Lua::getNumber(L, 2); if (house->getAccessList(listId, list)) { - pushString(L, list); + Lua::pushString(L, list); } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } int HouseFunctions::luaHouseSetAccessList(lua_State* L) { // house:setAccessList(listId, list) - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; } - const uint32_t listId = getNumber(L, 2); - const std::string &list = getString(L, 3); + const uint32_t listId = Lua::getNumber(L, 2); + const std::string &list = Lua::getString(L, 3); house->setAccessList(listId, list); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int HouseFunctions::luaHouseKickPlayer(lua_State* L) { // house:kickPlayer(player, targetPlayer) - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; } - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc("Player is nullptr"); + Lua::reportErrorFunc("Player is nullptr"); return 1; } - const auto &targetPlayer = getPlayer(L, 3); + const auto &targetPlayer = Lua::getPlayer(L, 3); if (!targetPlayer) { - reportErrorFunc("Target player is nullptr"); + Lua::reportErrorFunc("Target player is nullptr"); return 1; } - pushBoolean(L, house->kickPlayer(player, targetPlayer)); + Lua::pushBoolean(L, house->kickPlayer(player, targetPlayer)); return 1; } int HouseFunctions::luaHouseIsInvited(lua_State* L) { // house:isInvited(player) - const auto &house = getUserdataShared(L, 1); + const auto &house = Lua::getUserdataShared(L, 1); if (!house) { lua_pushnil(L); return 1; } - const auto &player = getPlayer(L, 2); + const auto &player = Lua::getPlayer(L, 2); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } - pushBoolean(L, house->isInvited(player)); + Lua::pushBoolean(L, house->isInvited(player)); return 1; } diff --git a/src/lua/functions/map/house_functions.hpp b/src/lua/functions/map/house_functions.hpp index 8d527bf6354..bb7200a5b3f 100644 --- a/src/lua/functions/map/house_functions.hpp +++ b/src/lua/functions/map/house_functions.hpp @@ -9,52 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class HouseFunctions final : LuaScriptInterface { +class HouseFunctions { public: - explicit HouseFunctions(lua_State* L) : - LuaScriptInterface("HouseFunctions") { - init(L); - } - ~HouseFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "House", "", HouseFunctions::luaHouseCreate); - registerMetaMethod(L, "House", "__eq", HouseFunctions::luaUserdataCompare); - - registerMethod(L, "House", "getId", HouseFunctions::luaHouseGetId); - registerMethod(L, "House", "getName", HouseFunctions::luaHouseGetName); - registerMethod(L, "House", "getTown", HouseFunctions::luaHouseGetTown); - registerMethod(L, "House", "getExitPosition", HouseFunctions::luaHouseGetExitPosition); - registerMethod(L, "House", "getRent", HouseFunctions::luaHouseGetRent); - registerMethod(L, "House", "getPrice", HouseFunctions::luaHouseGetPrice); - - registerMethod(L, "House", "getOwnerGuid", HouseFunctions::luaHouseGetOwnerGuid); - registerMethod(L, "House", "setHouseOwner", HouseFunctions::luaHouseSetHouseOwner); - registerMethod(L, "House", "setNewOwnerGuid", HouseFunctions::luaHouseSetNewOwnerGuid); - registerMethod(L, "House", "hasItemOnTile", HouseFunctions::luaHouseHasItemOnTile); - registerMethod(L, "House", "hasNewOwnership", HouseFunctions::luaHouseHasNewOwnership); - registerMethod(L, "House", "startTrade", HouseFunctions::luaHouseStartTrade); - - registerMethod(L, "House", "getBeds", HouseFunctions::luaHouseGetBeds); - registerMethod(L, "House", "getBedCount", HouseFunctions::luaHouseGetBedCount); - - registerMethod(L, "House", "getDoors", HouseFunctions::luaHouseGetDoors); - registerMethod(L, "House", "getDoorCount", HouseFunctions::luaHouseGetDoorCount); - registerMethod(L, "House", "getDoorIdByPosition", HouseFunctions::luaHouseGetDoorIdByPosition); - - registerMethod(L, "House", "getTiles", HouseFunctions::luaHouseGetTiles); - registerMethod(L, "House", "getItems", HouseFunctions::luaHouseGetItems); - registerMethod(L, "House", "getTileCount", HouseFunctions::luaHouseGetTileCount); - - registerMethod(L, "House", "canEditAccessList", HouseFunctions::luaHouseCanEditAccessList); - registerMethod(L, "House", "getAccessList", HouseFunctions::luaHouseGetAccessList); - registerMethod(L, "House", "setAccessList", HouseFunctions::luaHouseSetAccessList); - - registerMethod(L, "House", "kickPlayer", HouseFunctions::luaHouseKickPlayer); - registerMethod(L, "House", "isInvited", HouseFunctions::luaHouseIsInvited); - } + static void init(lua_State* L); private: static int luaHouseCreate(lua_State* L); diff --git a/src/lua/functions/map/position_functions.cpp b/src/lua/functions/map/position_functions.cpp index b182ee5e447..a9de71fde2b 100644 --- a/src/lua/functions/map/position_functions.cpp +++ b/src/lua/functions/map/position_functions.cpp @@ -14,26 +14,50 @@ #include "creatures/players/player.hpp" #include "game/game.hpp" #include "game/movement/position.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void PositionFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Position", "", PositionFunctions::luaPositionCreate); + Lua::registerMetaMethod(L, "Position", "__add", PositionFunctions::luaPositionAdd); + Lua::registerMetaMethod(L, "Position", "__sub", PositionFunctions::luaPositionSub); + Lua::registerMetaMethod(L, "Position", "__eq", PositionFunctions::luaPositionCompare); + + Lua::registerMethod(L, "Position", "getDistance", PositionFunctions::luaPositionGetDistance); + Lua::registerMethod(L, "Position", "getPathTo", PositionFunctions::luaPositionGetPathTo); + Lua::registerMethod(L, "Position", "isSightClear", PositionFunctions::luaPositionIsSightClear); + + Lua::registerMethod(L, "Position", "getTile", PositionFunctions::luaPositionGetTile); + Lua::registerMethod(L, "Position", "getZones", PositionFunctions::luaPositionGetZones); + + Lua::registerMethod(L, "Position", "sendMagicEffect", PositionFunctions::luaPositionSendMagicEffect); + Lua::registerMethod(L, "Position", "removeMagicEffect", PositionFunctions::luaPositionRemoveMagicEffect); + Lua::registerMethod(L, "Position", "sendDistanceEffect", PositionFunctions::luaPositionSendDistanceEffect); + + Lua::registerMethod(L, "Position", "sendSingleSoundEffect", PositionFunctions::luaPositionSendSingleSoundEffect); + Lua::registerMethod(L, "Position", "sendDoubleSoundEffect", PositionFunctions::luaPositionSendDoubleSoundEffect); + + Lua::registerMethod(L, "Position", "toString", PositionFunctions::luaPositionToString); +} int PositionFunctions::luaPositionCreate(lua_State* L) { // Position([x = 0[, y = 0[, z = 0[, stackpos = 0]]]]) // Position([position]) if (lua_gettop(L) <= 1) { - pushPosition(L, Position()); + Lua::pushPosition(L, Position()); return 1; } int32_t stackpos; - if (isTable(L, 2)) { - const Position &position = getPosition(L, 2, stackpos); - pushPosition(L, position, stackpos); + if (Lua::isTable(L, 2)) { + const Position &position = Lua::getPosition(L, 2, stackpos); + Lua::pushPosition(L, position, stackpos); } else { - const auto x = getNumber(L, 2, 0); - const auto y = getNumber(L, 3, 0); - const auto z = getNumber(L, 4, 0); - stackpos = getNumber(L, 5, 0); + const auto x = Lua::getNumber(L, 2, 0); + const auto y = Lua::getNumber(L, 3, 0); + const auto z = Lua::getNumber(L, 4, 0); + stackpos = Lua::getNumber(L, 5, 0); - pushPosition(L, Position(x, y, z), stackpos); + Lua::pushPosition(L, Position(x, y, z), stackpos); } return 1; } @@ -41,62 +65,62 @@ int PositionFunctions::luaPositionCreate(lua_State* L) { int PositionFunctions::luaPositionAdd(lua_State* L) { // positionValue = position + positionEx int32_t stackpos; - const Position &position = getPosition(L, 1, stackpos); + const Position &position = Lua::getPosition(L, 1, stackpos); Position positionEx; if (stackpos == 0) { - positionEx = getPosition(L, 2, stackpos); + positionEx = Lua::getPosition(L, 2, stackpos); } else { - positionEx = getPosition(L, 2); + positionEx = Lua::getPosition(L, 2); } - pushPosition(L, position + positionEx, stackpos); + Lua::pushPosition(L, position + positionEx, stackpos); return 1; } int PositionFunctions::luaPositionSub(lua_State* L) { // positionValue = position - positionEx int32_t stackpos; - const Position &position = getPosition(L, 1, stackpos); + const Position &position = Lua::getPosition(L, 1, stackpos); Position positionEx; if (stackpos == 0) { - positionEx = getPosition(L, 2, stackpos); + positionEx = Lua::getPosition(L, 2, stackpos); } else { - positionEx = getPosition(L, 2); + positionEx = Lua::getPosition(L, 2); } - pushPosition(L, position - positionEx, stackpos); + Lua::pushPosition(L, position - positionEx, stackpos); return 1; } int PositionFunctions::luaPositionCompare(lua_State* L) { // position == positionEx - const Position &positionEx = getPosition(L, 2); - const Position &position = getPosition(L, 1); - pushBoolean(L, position == positionEx); + const Position &positionEx = Lua::getPosition(L, 2); + const Position &position = Lua::getPosition(L, 1); + Lua::pushBoolean(L, position == positionEx); return 1; } int PositionFunctions::luaPositionGetDistance(lua_State* L) { // position:getDistance(positionEx) - const Position &positionEx = getPosition(L, 2); - const Position &position = getPosition(L, 1); + const Position &positionEx = Lua::getPosition(L, 2); + const Position &position = Lua::getPosition(L, 1); lua_pushnumber(L, std::max(std::max(std::abs(Position::getDistanceX(position, positionEx)), std::abs(Position::getDistanceY(position, positionEx))), std::abs(Position::getDistanceZ(position, positionEx)))); return 1; } int PositionFunctions::luaPositionGetPathTo(lua_State* L) { // position:getPathTo(pos[, minTargetDist = 0[, maxTargetDist = 1[, fullPathSearch = true[, clearSight = true[, maxSearchDist = 0]]]]]) - const Position &pos = getPosition(L, 1); - const Position &position = getPosition(L, 2); + const Position &pos = Lua::getPosition(L, 1); + const Position &position = Lua::getPosition(L, 2); FindPathParams fpp; - fpp.minTargetDist = getNumber(L, 3, 0); - fpp.maxTargetDist = getNumber(L, 4, 1); - fpp.fullPathSearch = getBoolean(L, 5, fpp.fullPathSearch); - fpp.clearSight = getBoolean(L, 6, fpp.clearSight); - fpp.maxSearchDist = getNumber(L, 7, fpp.maxSearchDist); + fpp.minTargetDist = Lua::getNumber(L, 3, 0); + fpp.maxTargetDist = Lua::getNumber(L, 4, 1); + fpp.fullPathSearch = Lua::getBoolean(L, 5, fpp.fullPathSearch); + fpp.clearSight = Lua::getBoolean(L, 6, fpp.clearSight); + fpp.maxSearchDist = Lua::getNumber(L, 7, fpp.maxSearchDist); std::vector dirList; if (g_game().map.getPathMatching(pos, dirList, FrozenPathingConditionCall(position), fpp)) { @@ -108,30 +132,30 @@ int PositionFunctions::luaPositionGetPathTo(lua_State* L) { lua_rawseti(L, -2, ++index); } } else { - pushBoolean(L, false); + Lua::pushBoolean(L, false); } return 1; } int PositionFunctions::luaPositionIsSightClear(lua_State* L) { // position:isSightClear(positionEx[, sameFloor = true]) - const bool sameFloor = getBoolean(L, 3, true); - const Position &positionEx = getPosition(L, 2); - const Position &position = getPosition(L, 1); - pushBoolean(L, g_game().isSightClear(position, positionEx, sameFloor)); + const bool sameFloor = Lua::getBoolean(L, 3, true); + const Position &positionEx = Lua::getPosition(L, 2); + const Position &position = Lua::getPosition(L, 1); + Lua::pushBoolean(L, g_game().isSightClear(position, positionEx, sameFloor)); return 1; } int PositionFunctions::luaPositionGetTile(lua_State* L) { // position:getTile() - const Position &position = getPosition(L, 1); - pushUserdata(L, g_game().map.getTile(position)); + const Position &position = Lua::getPosition(L, 1); + Lua::pushUserdata(L, g_game().map.getTile(position)); return 1; } int PositionFunctions::luaPositionGetZones(lua_State* L) { // position:getZones() - const Position &position = getPosition(L, 1); + const Position &position = Lua::getPosition(L, 1); const auto &tile = g_game().map.getTile(position); if (tile == nullptr) { lua_pushnil(L); @@ -140,8 +164,8 @@ int PositionFunctions::luaPositionGetZones(lua_State* L) { int index = 0; for (const auto &zone : tile->getZones()) { index++; - pushUserdata(L, zone); - setMetatable(L, -1, "Zone"); + Lua::pushUserdata(L, zone); + Lua::setMetatable(L, -1, "Zone"); lua_rawseti(L, -2, index); } return 1; @@ -151,30 +175,30 @@ int PositionFunctions::luaPositionSendMagicEffect(lua_State* L) { // position:sendMagicEffect(magicEffect[, player = nullptr]) CreatureVector spectators; if (lua_gettop(L) >= 3) { - const auto &player = getPlayer(L, 3); + const auto &player = Lua::getPlayer(L, 3); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } spectators.emplace_back(player); } - MagicEffectClasses magicEffect = getNumber(L, 2); + MagicEffectClasses magicEffect = Lua::getNumber(L, 2); if (g_configManager().getBoolean(WARN_UNSAFE_SCRIPTS) && !g_game().isMagicEffectRegistered(magicEffect)) { g_logger().warn("[PositionFunctions::luaPositionSendMagicEffect] An unregistered magic effect type with id '{}' was blocked to prevent client crash.", fmt::underlying(magicEffect)); - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - const Position &position = getPosition(L, 1); + const Position &position = Lua::getPosition(L, 1); if (!spectators.empty()) { Game::addMagicEffect(spectators, position, magicEffect); } else { g_game().addMagicEffect(position, magicEffect); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -182,30 +206,30 @@ int PositionFunctions::luaPositionRemoveMagicEffect(lua_State* L) { // position:removeMagicEffect(magicEffect[, player = nullptr]) CreatureVector spectators; if (lua_gettop(L) >= 3) { - const auto &player = getPlayer(L, 3); + const auto &player = Lua::getPlayer(L, 3); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } spectators.emplace_back(player); } - MagicEffectClasses magicEffect = getNumber(L, 2); + MagicEffectClasses magicEffect = Lua::getNumber(L, 2); if (g_configManager().getBoolean(WARN_UNSAFE_SCRIPTS) && !g_game().isMagicEffectRegistered(magicEffect)) { g_logger().warn("[PositionFunctions::luaPositionRemoveMagicEffect] An unregistered magic effect type with id '{}' was blocked to prevent client crash.", fmt::underlying(magicEffect)); - pushBoolean(L, false); + Lua::pushBoolean(L, false); return 1; } - const Position &position = getPosition(L, 1); + const Position &position = Lua::getPosition(L, 1); if (!spectators.empty()) { Game::removeMagicEffect(spectators, position, magicEffect); } else { g_game().removeMagicEffect(position, magicEffect); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } @@ -213,18 +237,18 @@ int PositionFunctions::luaPositionSendDistanceEffect(lua_State* L) { // position:sendDistanceEffect(positionEx, distanceEffect[, player = nullptr]) CreatureVector spectators; if (lua_gettop(L) >= 4) { - const auto &player = getPlayer(L, 4); + const auto &player = Lua::getPlayer(L, 4); if (!player) { - reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); + Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); return 1; } spectators.emplace_back(player); } - const ShootType_t distanceEffect = getNumber(L, 3); - const Position &positionEx = getPosition(L, 2); - const Position &position = getPosition(L, 1); + const ShootType_t distanceEffect = Lua::getNumber(L, 3); + const Position &positionEx = Lua::getPosition(L, 2); + const Position &position = Lua::getPosition(L, 1); if (g_configManager().getBoolean(WARN_UNSAFE_SCRIPTS) && !g_game().isDistanceEffectRegistered(distanceEffect)) { g_logger().warn("[PositionFunctions::luaPositionSendDistanceEffect] An unregistered distance effect type with id '{}' was blocked to prevent client crash.", fmt::underlying(distanceEffect)); return 1; @@ -236,36 +260,36 @@ int PositionFunctions::luaPositionSendDistanceEffect(lua_State* L) { g_game().addDistanceEffect(position, positionEx, distanceEffect); } - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PositionFunctions::luaPositionSendSingleSoundEffect(lua_State* L) { // position:sendSingleSoundEffect(soundId[, actor = nullptr]) - const Position &position = getPosition(L, 1); - const SoundEffect_t soundEffect = getNumber(L, 2); - const auto actor = getCreature(L, 3); + const Position &position = Lua::getPosition(L, 1); + const SoundEffect_t soundEffect = Lua::getNumber(L, 2); + const auto actor = Lua::getCreature(L, 3); g_game().sendSingleSoundEffect(position, soundEffect, actor); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PositionFunctions::luaPositionSendDoubleSoundEffect(lua_State* L) { // position:sendDoubleSoundEffect(mainSoundId, secondarySoundId[, actor = nullptr]) - const Position &position = getPosition(L, 1); - const SoundEffect_t mainSoundEffect = getNumber(L, 2); - const SoundEffect_t secondarySoundEffect = getNumber(L, 3); - const auto &actor = getCreature(L, 4); + const Position &position = Lua::getPosition(L, 1); + const SoundEffect_t mainSoundEffect = Lua::getNumber(L, 2); + const SoundEffect_t secondarySoundEffect = Lua::getNumber(L, 3); + const auto &actor = Lua::getCreature(L, 4); g_game().sendDoubleSoundEffect(position, mainSoundEffect, secondarySoundEffect, actor); - pushBoolean(L, true); + Lua::pushBoolean(L, true); return 1; } int PositionFunctions::luaPositionToString(lua_State* L) { // position:toString() - const Position &position = getPosition(L, 1); - pushString(L, position.toString()); + const Position &position = Lua::getPosition(L, 1); + Lua::pushString(L, position.toString()); return 1; } diff --git a/src/lua/functions/map/position_functions.hpp b/src/lua/functions/map/position_functions.hpp index 4bb6917aecc..eb864a5417f 100644 --- a/src/lua/functions/map/position_functions.hpp +++ b/src/lua/functions/map/position_functions.hpp @@ -9,38 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class PositionFunctions final : LuaScriptInterface { +class PositionFunctions { public: - explicit PositionFunctions(lua_State* L) : - LuaScriptInterface("PositionFunctions") { - init(L); - } - ~PositionFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Position", "", PositionFunctions::luaPositionCreate); - registerMetaMethod(L, "Position", "__add", PositionFunctions::luaPositionAdd); - registerMetaMethod(L, "Position", "__sub", PositionFunctions::luaPositionSub); - registerMetaMethod(L, "Position", "__eq", PositionFunctions::luaPositionCompare); - - registerMethod(L, "Position", "getDistance", PositionFunctions::luaPositionGetDistance); - registerMethod(L, "Position", "getPathTo", PositionFunctions::luaPositionGetPathTo); - registerMethod(L, "Position", "isSightClear", PositionFunctions::luaPositionIsSightClear); - - registerMethod(L, "Position", "getTile", PositionFunctions::luaPositionGetTile); - registerMethod(L, "Position", "getZones", PositionFunctions::luaPositionGetZones); - - registerMethod(L, "Position", "sendMagicEffect", PositionFunctions::luaPositionSendMagicEffect); - registerMethod(L, "Position", "removeMagicEffect", PositionFunctions::luaPositionRemoveMagicEffect); - registerMethod(L, "Position", "sendDistanceEffect", PositionFunctions::luaPositionSendDistanceEffect); - - registerMethod(L, "Position", "sendSingleSoundEffect", PositionFunctions::luaPositionSendSingleSoundEffect); - registerMethod(L, "Position", "sendDoubleSoundEffect", PositionFunctions::luaPositionSendDoubleSoundEffect); - - registerMethod(L, "Position", "toString", PositionFunctions::luaPositionToString); - } + static void init(lua_State* L); private: static int luaPositionCreate(lua_State* L); diff --git a/src/lua/functions/map/teleport_functions.cpp b/src/lua/functions/map/teleport_functions.cpp index 84eafe21ba4..c3e6f60effc 100644 --- a/src/lua/functions/map/teleport_functions.cpp +++ b/src/lua/functions/map/teleport_functions.cpp @@ -11,16 +11,25 @@ #include "game/movement/teleport.hpp" #include "items/item.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void TeleportFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Teleport", "Item", TeleportFunctions::luaTeleportCreate); + Lua::registerMetaMethod(L, "Teleport", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Teleport", "getDestination", TeleportFunctions::luaTeleportGetDestination); + Lua::registerMethod(L, "Teleport", "setDestination", TeleportFunctions::luaTeleportSetDestination); +} // Teleport int TeleportFunctions::luaTeleportCreate(lua_State* L) { // Teleport(uid) - const uint32_t id = getNumber(L, 2); + const uint32_t id = Lua::getNumber(L, 2); - const auto &item = getScriptEnv()->getItemByUID(id); + const auto &item = Lua::getScriptEnv()->getItemByUID(id); if (item && item->getTeleport()) { - pushUserdata(L, item); - setMetatable(L, -1, "Teleport"); + Lua::pushUserdata(L, item); + Lua::setMetatable(L, -1, "Teleport"); } else { lua_pushnil(L); } @@ -29,9 +38,9 @@ int TeleportFunctions::luaTeleportCreate(lua_State* L) { int TeleportFunctions::luaTeleportGetDestination(lua_State* L) { // teleport:getDestination() - const auto &teleport = getUserdataShared(L, 1); + const auto &teleport = Lua::getUserdataShared(L, 1); if (teleport) { - pushPosition(L, teleport->getDestPos()); + Lua::pushPosition(L, teleport->getDestPos()); } else { lua_pushnil(L); } @@ -40,10 +49,10 @@ int TeleportFunctions::luaTeleportGetDestination(lua_State* L) { int TeleportFunctions::luaTeleportSetDestination(lua_State* L) { // teleport:setDestination(position) - const auto &teleport = getUserdataShared(L, 1); + const auto &teleport = Lua::getUserdataShared(L, 1); if (teleport) { - teleport->setDestPos(getPosition(L, 2)); - pushBoolean(L, true); + teleport->setDestPos(Lua::getPosition(L, 2)); + Lua::pushBoolean(L, true); } else { lua_pushnil(L); } diff --git a/src/lua/functions/map/teleport_functions.hpp b/src/lua/functions/map/teleport_functions.hpp index 57f59a70315..636590c1491 100644 --- a/src/lua/functions/map/teleport_functions.hpp +++ b/src/lua/functions/map/teleport_functions.hpp @@ -9,23 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class TeleportFunctions final : LuaScriptInterface { +class TeleportFunctions { public: - explicit TeleportFunctions(lua_State* L) : - LuaScriptInterface("TeleportFunctions") { - init(L); - } - ~TeleportFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Teleport", "Item", TeleportFunctions::luaTeleportCreate); - registerMetaMethod(L, "Teleport", "__eq", TeleportFunctions::luaUserdataCompare); - - registerMethod(L, "Teleport", "getDestination", TeleportFunctions::luaTeleportGetDestination); - registerMethod(L, "Teleport", "setDestination", TeleportFunctions::luaTeleportSetDestination); - } + static void init(lua_State* L); private: static int luaTeleportCreate(lua_State* L); diff --git a/src/lua/functions/map/tile_functions.cpp b/src/lua/functions/map/tile_functions.cpp index d43ce68e6be..c39d96348bd 100644 --- a/src/lua/functions/map/tile_functions.cpp +++ b/src/lua/functions/map/tile_functions.cpp @@ -11,23 +11,69 @@ #include "creatures/combat/combat.hpp" #include "game/game.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void TileFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Tile", "", TileFunctions::luaTileCreate); + Lua::registerMetaMethod(L, "Tile", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Tile", "getPosition", TileFunctions::luaTileGetPosition); + Lua::registerMethod(L, "Tile", "getGround", TileFunctions::luaTileGetGround); + Lua::registerMethod(L, "Tile", "getThing", TileFunctions::luaTileGetThing); + Lua::registerMethod(L, "Tile", "getThingCount", TileFunctions::luaTileGetThingCount); + Lua::registerMethod(L, "Tile", "getTopVisibleThing", TileFunctions::luaTileGetTopVisibleThing); + + Lua::registerMethod(L, "Tile", "getTopTopItem", TileFunctions::luaTileGetTopTopItem); + Lua::registerMethod(L, "Tile", "getTopDownItem", TileFunctions::luaTileGetTopDownItem); + Lua::registerMethod(L, "Tile", "getFieldItem", TileFunctions::luaTileGetFieldItem); + + Lua::registerMethod(L, "Tile", "getItemById", TileFunctions::luaTileGetItemById); + Lua::registerMethod(L, "Tile", "getItemByType", TileFunctions::luaTileGetItemByType); + Lua::registerMethod(L, "Tile", "getItemByTopOrder", TileFunctions::luaTileGetItemByTopOrder); + Lua::registerMethod(L, "Tile", "getItemCountById", TileFunctions::luaTileGetItemCountById); + + Lua::registerMethod(L, "Tile", "getBottomCreature", TileFunctions::luaTileGetBottomCreature); + Lua::registerMethod(L, "Tile", "getTopCreature", TileFunctions::luaTileGetTopCreature); + Lua::registerMethod(L, "Tile", "getBottomVisibleCreature", TileFunctions::luaTileGetBottomVisibleCreature); + Lua::registerMethod(L, "Tile", "getTopVisibleCreature", TileFunctions::luaTileGetTopVisibleCreature); + + Lua::registerMethod(L, "Tile", "getItems", TileFunctions::luaTileGetItems); + Lua::registerMethod(L, "Tile", "getItemCount", TileFunctions::luaTileGetItemCount); + Lua::registerMethod(L, "Tile", "getDownItemCount", TileFunctions::luaTileGetDownItemCount); + Lua::registerMethod(L, "Tile", "getTopItemCount", TileFunctions::luaTileGetTopItemCount); + + Lua::registerMethod(L, "Tile", "getCreatures", TileFunctions::luaTileGetCreatures); + Lua::registerMethod(L, "Tile", "getCreatureCount", TileFunctions::luaTileGetCreatureCount); + + Lua::registerMethod(L, "Tile", "getThingIndex", TileFunctions::luaTileGetThingIndex); + + Lua::registerMethod(L, "Tile", "hasProperty", TileFunctions::luaTileHasProperty); + Lua::registerMethod(L, "Tile", "hasFlag", TileFunctions::luaTileHasFlag); + + Lua::registerMethod(L, "Tile", "queryAdd", TileFunctions::luaTileQueryAdd); + Lua::registerMethod(L, "Tile", "addItem", TileFunctions::luaTileAddItem); + Lua::registerMethod(L, "Tile", "addItemEx", TileFunctions::luaTileAddItemEx); + + Lua::registerMethod(L, "Tile", "getHouse", TileFunctions::luaTileGetHouse); + Lua::registerMethod(L, "Tile", "sweep", TileFunctions::luaTileSweep); +} int TileFunctions::luaTileCreate(lua_State* L) { // Tile(x, y, z) // Tile(position) std::shared_ptr tile; - if (isTable(L, 2)) { - tile = g_game().map.getTile(getPosition(L, 2)); + if (Lua::isTable(L, 2)) { + tile = g_game().map.getTile(Lua::getPosition(L, 2)); } else { - const uint8_t z = getNumber(L, 4); - const uint16_t y = getNumber(L, 3); - const uint16_t x = getNumber(L, 2); + const uint8_t z = Lua::getNumber(L, 4); + const uint16_t y = Lua::getNumber(L, 3); + const uint16_t x = Lua::getNumber(L, 2); tile = g_game().map.getTile(x, y, z); } if (tile) { - pushUserdata(L, tile); - setMetatable(L, -1, "Tile"); + Lua::pushUserdata(L, tile); + Lua::setMetatable(L, -1, "Tile"); } else { lua_pushnil(L); } @@ -35,10 +81,10 @@ int TileFunctions::luaTileCreate(lua_State* L) { } int TileFunctions::luaTileGetPosition(lua_State* L) { - // tile:getPosition() - const auto &tile = getUserdataShared(L, 1); + // tile:Lua::getPosition() + const auto &tile = Lua::getUserdataShared(L, 1); if (tile) { - pushPosition(L, tile->getPosition()); + Lua::pushPosition(L, tile->getPosition()); } else { lua_pushnil(L); } @@ -47,10 +93,10 @@ int TileFunctions::luaTileGetPosition(lua_State* L) { int TileFunctions::luaTileGetGround(lua_State* L) { // tile:getGround() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (tile && tile->getGround()) { - pushUserdata(L, tile->getGround()); - setItemMetatable(L, -1, tile->getGround()); + Lua::pushUserdata(L, tile->getGround()); + Lua::setItemMetatable(L, -1, tile->getGround()); } else { lua_pushnil(L); } @@ -59,8 +105,8 @@ int TileFunctions::luaTileGetGround(lua_State* L) { int TileFunctions::luaTileGetThing(lua_State* L) { // tile:getThing(index) - const int32_t index = getNumber(L, 2); - const auto &tile = getUserdataShared(L, 1); + const int32_t index = Lua::getNumber(L, 2); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -73,11 +119,11 @@ int TileFunctions::luaTileGetThing(lua_State* L) { } if (const auto &creature = thing->getCreature()) { - pushUserdata(L, creature); - setCreatureMetatable(L, -1, creature); + Lua::pushUserdata(L, creature); + Lua::setCreatureMetatable(L, -1, creature); } else if (const auto &item = thing->getItem()) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { lua_pushnil(L); } @@ -86,7 +132,7 @@ int TileFunctions::luaTileGetThing(lua_State* L) { int TileFunctions::luaTileGetThingCount(lua_State* L) { // tile:getThingCount() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (tile) { lua_pushnumber(L, tile->getThingCount()); } else { @@ -97,8 +143,8 @@ int TileFunctions::luaTileGetThingCount(lua_State* L) { int TileFunctions::luaTileGetTopVisibleThing(lua_State* L) { // tile:getTopVisibleThing(creature) - const auto &creature = getCreature(L, 2); - const auto &tile = getUserdataShared(L, 1); + const auto &creature = Lua::getCreature(L, 2); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -111,11 +157,11 @@ int TileFunctions::luaTileGetTopVisibleThing(lua_State* L) { } if (const auto &visibleCreature = thing->getCreature()) { - pushUserdata(L, visibleCreature); - setCreatureMetatable(L, -1, visibleCreature); + Lua::pushUserdata(L, visibleCreature); + Lua::setCreatureMetatable(L, -1, visibleCreature); } else if (const auto &visibleItem = thing->getItem()) { - pushUserdata(L, visibleItem); - setItemMetatable(L, -1, visibleItem); + Lua::pushUserdata(L, visibleItem); + Lua::setItemMetatable(L, -1, visibleItem); } else { lua_pushnil(L); } @@ -124,7 +170,7 @@ int TileFunctions::luaTileGetTopVisibleThing(lua_State* L) { int TileFunctions::luaTileGetTopTopItem(lua_State* L) { // tile:getTopTopItem() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -132,8 +178,8 @@ int TileFunctions::luaTileGetTopTopItem(lua_State* L) { const auto &item = tile->getTopTopItem(); if (item) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { lua_pushnil(L); } @@ -142,7 +188,7 @@ int TileFunctions::luaTileGetTopTopItem(lua_State* L) { int TileFunctions::luaTileGetTopDownItem(lua_State* L) { // tile:getTopDownItem() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -150,8 +196,8 @@ int TileFunctions::luaTileGetTopDownItem(lua_State* L) { const auto &item = tile->getTopDownItem(); if (item) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { lua_pushnil(L); } @@ -160,7 +206,7 @@ int TileFunctions::luaTileGetTopDownItem(lua_State* L) { int TileFunctions::luaTileGetFieldItem(lua_State* L) { // tile:getFieldItem() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -168,8 +214,8 @@ int TileFunctions::luaTileGetFieldItem(lua_State* L) { const auto &item = tile->getFieldItem(); if (item) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { lua_pushnil(L); } @@ -178,28 +224,28 @@ int TileFunctions::luaTileGetFieldItem(lua_State* L) { int TileFunctions::luaTileGetItemById(lua_State* L) { // tile:getItemById(itemId[, subType = -1]) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; } } - const auto subType = getNumber(L, 3, -1); + const auto subType = Lua::getNumber(L, 3, -1); const auto &item = g_game().findItemOfType(tile, itemId, false, subType); if (item) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { lua_pushnil(L); } @@ -208,7 +254,7 @@ int TileFunctions::luaTileGetItemById(lua_State* L) { int TileFunctions::luaTileGetItemByType(lua_State* L) { // tile:getItemByType(itemType) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -216,7 +262,7 @@ int TileFunctions::luaTileGetItemByType(lua_State* L) { bool found; - const ItemTypes_t itemType = getNumber(L, 2); + const ItemTypes_t itemType = Lua::getNumber(L, 2); switch (itemType) { case ITEM_TYPE_TELEPORT: found = tile->hasFlag(TILESTATE_TELEPORT); @@ -249,8 +295,8 @@ int TileFunctions::luaTileGetItemByType(lua_State* L) { if (const auto &item = tile->getGround()) { const ItemType &it = Item::items[item->getID()]; if (it.type == itemType) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); return 1; } } @@ -259,8 +305,8 @@ int TileFunctions::luaTileGetItemByType(lua_State* L) { for (auto &item : *items) { const ItemType &it = Item::items[item->getID()]; if (it.type == itemType) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); return 1; } } @@ -272,13 +318,13 @@ int TileFunctions::luaTileGetItemByType(lua_State* L) { int TileFunctions::luaTileGetItemByTopOrder(lua_State* L) { // tile:getItemByTopOrder(topOrder) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } - const int32_t topOrder = getNumber(L, 2); + const int32_t topOrder = Lua::getNumber(L, 2); const auto &item = tile->getItemByTopOrder(topOrder); if (!item) { @@ -286,26 +332,26 @@ int TileFunctions::luaTileGetItemByTopOrder(lua_State* L) { return 1; } - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); return 1; } int TileFunctions::luaTileGetItemCountById(lua_State* L) { // tile:getItemCountById(itemId[, subType = -1]) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } - const auto subType = getNumber(L, 3, -1); + const auto subType = Lua::getNumber(L, 3, -1); uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; @@ -318,7 +364,7 @@ int TileFunctions::luaTileGetItemCountById(lua_State* L) { int TileFunctions::luaTileGetBottomCreature(lua_State* L) { // tile:getBottomCreature() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -330,14 +376,14 @@ int TileFunctions::luaTileGetBottomCreature(lua_State* L) { return 1; } - pushUserdata(L, creature); - setCreatureMetatable(L, -1, creature); + Lua::pushUserdata(L, creature); + Lua::setCreatureMetatable(L, -1, creature); return 1; } int TileFunctions::luaTileGetTopCreature(lua_State* L) { // tile:getTopCreature() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -349,20 +395,20 @@ int TileFunctions::luaTileGetTopCreature(lua_State* L) { return 1; } - pushUserdata(L, creature); - setCreatureMetatable(L, -1, creature); + Lua::pushUserdata(L, creature); + Lua::setCreatureMetatable(L, -1, creature); return 1; } int TileFunctions::luaTileGetBottomVisibleCreature(lua_State* L) { // tile:getBottomVisibleCreature(creature) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } - const auto &creature = getCreature(L, 2); + const auto &creature = Lua::getCreature(L, 2); if (!creature) { lua_pushnil(L); return 1; @@ -370,8 +416,8 @@ int TileFunctions::luaTileGetBottomVisibleCreature(lua_State* L) { const auto &visibleCreature = tile->getBottomVisibleCreature(creature); if (visibleCreature) { - pushUserdata(L, visibleCreature); - setCreatureMetatable(L, -1, visibleCreature); + Lua::pushUserdata(L, visibleCreature); + Lua::setCreatureMetatable(L, -1, visibleCreature); } else { lua_pushnil(L); } @@ -380,13 +426,13 @@ int TileFunctions::luaTileGetBottomVisibleCreature(lua_State* L) { int TileFunctions::luaTileGetTopVisibleCreature(lua_State* L) { // tile:getTopVisibleCreature(creature) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } - const auto &creature = getCreature(L, 2); + const auto &creature = Lua::getCreature(L, 2); if (!creature) { lua_pushnil(L); return 1; @@ -394,8 +440,8 @@ int TileFunctions::luaTileGetTopVisibleCreature(lua_State* L) { const auto &visibleCreature = tile->getTopVisibleCreature(creature); if (visibleCreature) { - pushUserdata(L, visibleCreature); - setCreatureMetatable(L, -1, visibleCreature); + Lua::pushUserdata(L, visibleCreature); + Lua::setCreatureMetatable(L, -1, visibleCreature); } else { lua_pushnil(L); } @@ -404,7 +450,7 @@ int TileFunctions::luaTileGetTopVisibleCreature(lua_State* L) { int TileFunctions::luaTileGetItems(lua_State* L) { // tile:getItems() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -420,8 +466,8 @@ int TileFunctions::luaTileGetItems(lua_State* L) { int index = 0; for (auto &item : *itemVector) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); lua_rawseti(L, -2, ++index); } return 1; @@ -429,7 +475,7 @@ int TileFunctions::luaTileGetItems(lua_State* L) { int TileFunctions::luaTileGetItemCount(lua_State* L) { // tile:getItemCount() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -441,7 +487,7 @@ int TileFunctions::luaTileGetItemCount(lua_State* L) { int TileFunctions::luaTileGetDownItemCount(lua_State* L) { // tile:getDownItemCount() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (tile) { lua_pushnumber(L, tile->getDownItemCount()); } else { @@ -452,7 +498,7 @@ int TileFunctions::luaTileGetDownItemCount(lua_State* L) { int TileFunctions::luaTileGetTopItemCount(lua_State* L) { // tile:getTopItemCount() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -464,7 +510,7 @@ int TileFunctions::luaTileGetTopItemCount(lua_State* L) { int TileFunctions::luaTileGetCreatures(lua_State* L) { // tile:getCreatures() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -480,8 +526,8 @@ int TileFunctions::luaTileGetCreatures(lua_State* L) { int index = 0; for (auto &creature : *creatureVector) { - pushUserdata(L, creature); - setCreatureMetatable(L, -1, creature); + Lua::pushUserdata(L, creature); + Lua::setCreatureMetatable(L, -1, creature); lua_rawseti(L, -2, ++index); } return 1; @@ -489,7 +535,7 @@ int TileFunctions::luaTileGetCreatures(lua_State* L) { int TileFunctions::luaTileGetCreatureCount(lua_State* L) { // tile:getCreatureCount() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -501,7 +547,7 @@ int TileFunctions::luaTileGetCreatureCount(lua_State* L) { int TileFunctions::luaTileHasProperty(lua_State* L) { // tile:hasProperty(property[, item]) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; @@ -509,29 +555,29 @@ int TileFunctions::luaTileHasProperty(lua_State* L) { std::shared_ptr item; if (lua_gettop(L) >= 3) { - item = getUserdataShared(L, 3); + item = Lua::getUserdataShared(L, 3); } else { item = nullptr; } - ItemProperty property = getNumber(L, 2); + ItemProperty property = Lua::getNumber(L, 2); if (item) { - pushBoolean(L, tile->hasProperty(item, property)); + Lua::pushBoolean(L, tile->hasProperty(item, property)); } else { - pushBoolean(L, tile->hasProperty(property)); + Lua::pushBoolean(L, tile->hasProperty(property)); } return 1; } int TileFunctions::luaTileGetThingIndex(lua_State* L) { // tile:getThingIndex(thing) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } - const auto &thing = getThing(L, 2); + const auto &thing = Lua::getThing(L, 2); if (thing) { lua_pushnumber(L, tile->getThingIndex(thing)); } else { @@ -542,10 +588,10 @@ int TileFunctions::luaTileGetThingIndex(lua_State* L) { int TileFunctions::luaTileHasFlag(lua_State* L) { // tile:hasFlag(flag) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (tile) { - TileFlags_t flag = getNumber(L, 2); - pushBoolean(L, tile->hasFlag(flag)); + TileFlags_t flag = Lua::getNumber(L, 2); + Lua::pushBoolean(L, tile->hasFlag(flag)); } else { lua_pushnil(L); } @@ -554,15 +600,15 @@ int TileFunctions::luaTileHasFlag(lua_State* L) { int TileFunctions::luaTileQueryAdd(lua_State* L) { // tile:queryAdd(thing[, flags]) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } - const auto &thing = getThing(L, 2); + const auto &thing = Lua::getThing(L, 2); if (thing) { - const auto flags = getNumber(L, 3, 0); + const auto flags = Lua::getNumber(L, 3, 0); lua_pushnumber(L, tile->queryAdd(0, thing, 1, flags)); } else { lua_pushnil(L); @@ -572,24 +618,24 @@ int TileFunctions::luaTileQueryAdd(lua_State* L) { int TileFunctions::luaTileAddItem(lua_State* L) { // tile:addItem(itemId[, count/subType = 1[, flags = 0]]) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } uint16_t itemId; - if (isNumber(L, 2)) { - itemId = getNumber(L, 2); + if (Lua::isNumber(L, 2)) { + itemId = Lua::getNumber(L, 2); } else { - itemId = Item::items.getItemIdByName(getString(L, 2)); + itemId = Item::items.getItemIdByName(Lua::getString(L, 2)); if (itemId == 0) { lua_pushnil(L); return 1; } } - const auto subType = getNumber(L, 3, 1); + const auto subType = Lua::getNumber(L, 3, 1); const auto &item = Item::CreateItem(itemId, std::min(subType, Item::items[itemId].stackSize)); if (!item) { @@ -597,12 +643,12 @@ int TileFunctions::luaTileAddItem(lua_State* L) { return 1; } - const auto flags = getNumber(L, 4, 0); + const auto flags = Lua::getNumber(L, 4, 0); ReturnValue ret = g_game().internalAddItem(tile, item, INDEX_WHEREEVER, flags); if (ret == RETURNVALUE_NOERROR) { - pushUserdata(L, item); - setItemMetatable(L, -1, item); + Lua::pushUserdata(L, item); + Lua::setItemMetatable(L, -1, item); } else { lua_pushnil(L); @@ -612,25 +658,25 @@ int TileFunctions::luaTileAddItem(lua_State* L) { int TileFunctions::luaTileAddItemEx(lua_State* L) { // tile:addItemEx(item[, flags = 0]) - const auto &item = getUserdataShared(L, 2); + const auto &item = Lua::getUserdataShared(L, 2); if (!item) { lua_pushnil(L); return 1; } - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } if (item->getParent() != VirtualCylinder::virtualCylinder) { - reportErrorFunc("Item already has a parent"); + Lua::reportErrorFunc("Item already has a parent"); lua_pushnil(L); return 1; } - const auto flags = getNumber(L, 3, 0); + const auto flags = Lua::getNumber(L, 3, 0); ReturnValue ret = g_game().internalAddItem(tile, item, INDEX_WHEREEVER, flags); if (ret == RETURNVALUE_NOERROR) { ScriptEnvironment::removeTempItem(item); @@ -641,15 +687,15 @@ int TileFunctions::luaTileAddItemEx(lua_State* L) { int TileFunctions::luaTileGetHouse(lua_State* L) { // tile:getHouse() - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } if (const auto &houseTile = std::dynamic_pointer_cast(tile)) { - pushUserdata(L, houseTile->getHouse()); - setMetatable(L, -1, "House"); + Lua::pushUserdata(L, houseTile->getHouse()); + Lua::setMetatable(L, -1, "House"); } else { lua_pushnil(L); } @@ -658,12 +704,12 @@ int TileFunctions::luaTileGetHouse(lua_State* L) { int TileFunctions::luaTileSweep(lua_State* L) { // tile:sweep(actor) - const auto &tile = getUserdataShared(L, 1); + const auto &tile = Lua::getUserdataShared(L, 1); if (!tile) { lua_pushnil(L); return 1; } - const auto &actor = getPlayer(L, 2); + const auto &actor = Lua::getPlayer(L, 2); if (!actor) { lua_pushnil(L); return 1; @@ -689,6 +735,6 @@ int TileFunctions::luaTileSweep(lua_State* L) { return 1; } - pushBoolean(L, house->transferToDepot(actor, houseTile)); + Lua::pushBoolean(L, house->transferToDepot(actor, houseTile)); return 1; } diff --git a/src/lua/functions/map/tile_functions.hpp b/src/lua/functions/map/tile_functions.hpp index a1183508b45..e88b39acef8 100644 --- a/src/lua/functions/map/tile_functions.hpp +++ b/src/lua/functions/map/tile_functions.hpp @@ -9,60 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class TileFunctions final : LuaScriptInterface { +class TileFunctions { public: - explicit TileFunctions(lua_State* L) : - LuaScriptInterface("TileFunctions") { - init(L); - } - ~TileFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Tile", "", TileFunctions::luaTileCreate); - registerMetaMethod(L, "Tile", "__eq", TileFunctions::luaUserdataCompare); - - registerMethod(L, "Tile", "getPosition", TileFunctions::luaTileGetPosition); - registerMethod(L, "Tile", "getGround", TileFunctions::luaTileGetGround); - registerMethod(L, "Tile", "getThing", TileFunctions::luaTileGetThing); - registerMethod(L, "Tile", "getThingCount", TileFunctions::luaTileGetThingCount); - registerMethod(L, "Tile", "getTopVisibleThing", TileFunctions::luaTileGetTopVisibleThing); - - registerMethod(L, "Tile", "getTopTopItem", TileFunctions::luaTileGetTopTopItem); - registerMethod(L, "Tile", "getTopDownItem", TileFunctions::luaTileGetTopDownItem); - registerMethod(L, "Tile", "getFieldItem", TileFunctions::luaTileGetFieldItem); - - registerMethod(L, "Tile", "getItemById", TileFunctions::luaTileGetItemById); - registerMethod(L, "Tile", "getItemByType", TileFunctions::luaTileGetItemByType); - registerMethod(L, "Tile", "getItemByTopOrder", TileFunctions::luaTileGetItemByTopOrder); - registerMethod(L, "Tile", "getItemCountById", TileFunctions::luaTileGetItemCountById); - - registerMethod(L, "Tile", "getBottomCreature", TileFunctions::luaTileGetBottomCreature); - registerMethod(L, "Tile", "getTopCreature", TileFunctions::luaTileGetTopCreature); - registerMethod(L, "Tile", "getBottomVisibleCreature", TileFunctions::luaTileGetBottomVisibleCreature); - registerMethod(L, "Tile", "getTopVisibleCreature", TileFunctions::luaTileGetTopVisibleCreature); - - registerMethod(L, "Tile", "getItems", TileFunctions::luaTileGetItems); - registerMethod(L, "Tile", "getItemCount", TileFunctions::luaTileGetItemCount); - registerMethod(L, "Tile", "getDownItemCount", TileFunctions::luaTileGetDownItemCount); - registerMethod(L, "Tile", "getTopItemCount", TileFunctions::luaTileGetTopItemCount); - - registerMethod(L, "Tile", "getCreatures", TileFunctions::luaTileGetCreatures); - registerMethod(L, "Tile", "getCreatureCount", TileFunctions::luaTileGetCreatureCount); - - registerMethod(L, "Tile", "getThingIndex", TileFunctions::luaTileGetThingIndex); - - registerMethod(L, "Tile", "hasProperty", TileFunctions::luaTileHasProperty); - registerMethod(L, "Tile", "hasFlag", TileFunctions::luaTileHasFlag); - - registerMethod(L, "Tile", "queryAdd", TileFunctions::luaTileQueryAdd); - registerMethod(L, "Tile", "addItem", TileFunctions::luaTileAddItem); - registerMethod(L, "Tile", "addItemEx", TileFunctions::luaTileAddItemEx); - - registerMethod(L, "Tile", "getHouse", TileFunctions::luaTileGetHouse); - registerMethod(L, "Tile", "sweep", TileFunctions::luaTileSweep); - } + static void init(lua_State* L); private: static int luaTileCreate(lua_State* L); diff --git a/src/lua/functions/map/town_functions.cpp b/src/lua/functions/map/town_functions.cpp index eba9213d0de..fa77003fe77 100644 --- a/src/lua/functions/map/town_functions.cpp +++ b/src/lua/functions/map/town_functions.cpp @@ -11,21 +11,31 @@ #include "game/game.hpp" #include "map/town.hpp" +#include "lua/functions/lua_functions_loader.hpp" + +void TownFunctions::init(lua_State* L) { + Lua::registerSharedClass(L, "Town", "", TownFunctions::luaTownCreate); + Lua::registerMetaMethod(L, "Town", "__eq", Lua::luaUserdataCompare); + + Lua::registerMethod(L, "Town", "getId", TownFunctions::luaTownGetId); + Lua::registerMethod(L, "Town", "getName", TownFunctions::luaTownGetName); + Lua::registerMethod(L, "Town", "getTemplePosition", TownFunctions::luaTownGetTemplePosition); +} int TownFunctions::luaTownCreate(lua_State* L) { // Town(id or name) std::shared_ptr town; - if (isNumber(L, 2)) { - town = g_game().map.towns.getTown(getNumber(L, 2)); - } else if (isString(L, 2)) { - town = g_game().map.towns.getTown(getString(L, 2)); + if (Lua::isNumber(L, 2)) { + town = g_game().map.towns.getTown(Lua::getNumber(L, 2)); + } else if (Lua::isString(L, 2)) { + town = g_game().map.towns.getTown(Lua::getString(L, 2)); } else { town = nullptr; } if (town) { - pushUserdata(L, town); - setMetatable(L, -1, "Town"); + Lua::pushUserdata(L, town); + Lua::setMetatable(L, -1, "Town"); } else { lua_pushnil(L); } @@ -34,7 +44,7 @@ int TownFunctions::luaTownCreate(lua_State* L) { int TownFunctions::luaTownGetId(lua_State* L) { // town:getId() - if (const auto &town = getUserdataShared(L, 1)) { + if (const auto &town = Lua::getUserdataShared(L, 1)) { lua_pushnumber(L, town->getID()); } else { lua_pushnil(L); @@ -44,8 +54,8 @@ int TownFunctions::luaTownGetId(lua_State* L) { int TownFunctions::luaTownGetName(lua_State* L) { // town:getName() - if (const auto &town = getUserdataShared(L, 1)) { - pushString(L, town->getName()); + if (const auto &town = Lua::getUserdataShared(L, 1)) { + Lua::pushString(L, town->getName()); } else { lua_pushnil(L); } @@ -54,8 +64,8 @@ int TownFunctions::luaTownGetName(lua_State* L) { int TownFunctions::luaTownGetTemplePosition(lua_State* L) { // town:getTemplePosition() - if (const auto &town = getUserdataShared(L, 1)) { - pushPosition(L, town->getTemplePosition()); + if (const auto &town = Lua::getUserdataShared(L, 1)) { + Lua::pushPosition(L, town->getTemplePosition()); } else { lua_pushnil(L); } diff --git a/src/lua/functions/map/town_functions.hpp b/src/lua/functions/map/town_functions.hpp index a3e41ec67ae..81430daf2c4 100644 --- a/src/lua/functions/map/town_functions.hpp +++ b/src/lua/functions/map/town_functions.hpp @@ -9,24 +9,9 @@ #pragma once -#include "lua/scripts/luascript.hpp" - -class TownFunctions final : LuaScriptInterface { +class TownFunctions { public: - explicit TownFunctions(lua_State* L) : - LuaScriptInterface("TownFunctions") { - init(L); - } - ~TownFunctions() override = default; - - static void init(lua_State* L) { - registerSharedClass(L, "Town", "", TownFunctions::luaTownCreate); - registerMetaMethod(L, "Town", "__eq", TownFunctions::luaUserdataCompare); - - registerMethod(L, "Town", "getId", TownFunctions::luaTownGetId); - registerMethod(L, "Town", "getName", TownFunctions::luaTownGetName); - registerMethod(L, "Town", "getTemplePosition", TownFunctions::luaTownGetTemplePosition); - } + static void init(lua_State* L); private: static int luaTownCreate(lua_State* L); diff --git a/src/lua/global/globalevent.cpp b/src/lua/global/globalevent.cpp index f7de024bda1..ae988a814de 100644 --- a/src/lua/global/globalevent.cpp +++ b/src/lua/global/globalevent.cpp @@ -12,10 +12,16 @@ #include "utils/tools.hpp" #include "game/game.hpp" #include "game/scheduling/dispatcher.hpp" +#include "lua/scripts/scripts.hpp" +#include "lib/di/container.hpp" GlobalEvents::GlobalEvents() = default; GlobalEvents::~GlobalEvents() = default; +GlobalEvents &GlobalEvents::getInstance() { + return inject(); +} + void GlobalEvents::clear() { // Stop events g_dispatcher().stopEvent(thinkEventId); @@ -184,8 +190,34 @@ GlobalEventMap GlobalEvents::getEventMap(GlobalEvent_t type) { } } -GlobalEvent::GlobalEvent(LuaScriptInterface* interface) : - Script(interface) { } +GlobalEvent::GlobalEvent() = default; + +LuaScriptInterface* GlobalEvent::getScriptInterface() const { + return &g_scripts().getScriptInterface(); +} + +bool GlobalEvent::loadScriptId() { + LuaScriptInterface &luaInterface = g_scripts().getScriptInterface(); + m_scriptId = luaInterface.getEvent(); + if (m_scriptId == -1) { + g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName()); + return false; + } + + return true; +} + +int32_t GlobalEvent::getScriptId() const { + return m_scriptId; +} + +void GlobalEvent::setScriptId(int32_t newScriptId) { + m_scriptId = newScriptId; +} + +bool GlobalEvent::isLoadedScriptId() const { + return m_scriptId != 0; +} std::string GlobalEvent::getScriptTypeName() const { switch (eventType) { diff --git a/src/lua/global/globalevent.hpp b/src/lua/global/globalevent.hpp index cc08bb9c771..f8337cd773f 100644 --- a/src/lua/global/globalevent.hpp +++ b/src/lua/global/globalevent.hpp @@ -10,12 +10,17 @@ #pragma once #include "utils/utils_definitions.hpp" -#include "lua/scripts/scripts.hpp" +#include "lua/lua_definitions.hpp" +class LuaScriptInterface; class GlobalEvent; using GlobalEventMap = std::map>; -class GlobalEvents final : public Scripts { +enum LightState_t : uint8_t; + +struct LightInfo; + +class GlobalEvents { public: GlobalEvents(); ~GlobalEvents(); @@ -24,9 +29,7 @@ class GlobalEvents final : public Scripts { GlobalEvents(const GlobalEvents &) = delete; GlobalEvents &operator=(const GlobalEvents &) = delete; - static GlobalEvents &getInstance() { - return inject(); - } + static GlobalEvents &getInstance(); void startup() const; void shutdown() const; @@ -43,14 +46,15 @@ class GlobalEvents final : public Scripts { private: GlobalEventMap thinkMap, serverMap, timerMap; - uint64_t thinkEventId = 0, timerEventId = 0; + uint64_t thinkEventId = 0; + uint64_t timerEventId = 0; }; constexpr auto g_globalEvents = GlobalEvents::getInstance; -class GlobalEvent final : public Script { +class GlobalEvent { public: - explicit GlobalEvent(LuaScriptInterface* interface); + explicit GlobalEvent(); bool executePeriodChange(LightState_t lightState, LightInfo lightInfo) const; bool executeRecord(uint32_t current, uint32_t old) const; @@ -83,10 +87,17 @@ class GlobalEvent final : public Script { nextExecution = time; } + std::string getScriptTypeName() const; + LuaScriptInterface* getScriptInterface() const; + bool loadScriptId(); + int32_t getScriptId() const; + void setScriptId(int32_t newScriptId); + bool isLoadedScriptId() const; + private: - GlobalEvent_t eventType = GLOBALEVENT_NONE; + int32_t m_scriptId {}; - std::string getScriptTypeName() const override; + GlobalEvent_t eventType = GLOBALEVENT_NONE; std::string name; int64_t nextExecution = 0; diff --git a/src/lua/lua_definitions.hpp b/src/lua/lua_definitions.hpp index ace5ee32164..fa1bdcec01a 100644 --- a/src/lua/lua_definitions.hpp +++ b/src/lua/lua_definitions.hpp @@ -99,7 +99,7 @@ enum TalkActionResult_t { TALKACTION_FAILED, }; -enum GlobalEvent_t { +enum GlobalEvent_t : uint8_t { GLOBALEVENT_NONE, GLOBALEVENT_TIMER, diff --git a/src/lua/scripts/lua_environment.cpp b/src/lua/scripts/lua_environment.cpp index b4cc620b1ce..9e95beaf8da 100644 --- a/src/lua/scripts/lua_environment.cpp +++ b/src/lua/scripts/lua_environment.cpp @@ -13,9 +13,14 @@ #include "lua/functions/lua_functions_loader.hpp" #include "lua/scripts/script_environment.hpp" #include "lua/global/lua_timer_event_descr.hpp" +#include "lib/di/container.hpp" bool LuaEnvironment::shuttingDown = false; +LuaEnvironment &LuaEnvironment::getInstance() { + return inject(); +} + static const std::unique_ptr &AreaCombatNull {}; LuaEnvironment::LuaEnvironment() : @@ -43,7 +48,7 @@ lua_State* LuaEnvironment::getLuaState() { bool LuaEnvironment::initState() { luaState = luaL_newstate(); - LuaFunctionsLoader::load(luaState); + Lua::load(luaState); runningEventId = EVENT_ID_USER; return true; @@ -60,10 +65,6 @@ bool LuaEnvironment::closeState() { return false; } - for (const auto &combatEntry : combatIdMap) { - clearCombatObjects(combatEntry.first); - } - for (const auto &areaEntry : areaIdMap) { clearAreaObjects(areaEntry.first); } @@ -76,7 +77,6 @@ bool LuaEnvironment::closeState() { luaL_unref(luaState, LUA_REGISTRYINDEX, timerEventDesc.function); } - combatIdMap.clear(); areaIdMap.clear(); timerEvents.clear(); cacheFiles.clear(); @@ -94,31 +94,6 @@ LuaScriptInterface* LuaEnvironment::getTestInterface() { return testInterface; } -std::shared_ptr LuaEnvironment::getCombatObject(uint32_t id) const { - const auto it = combatMap.find(id); - if (it == combatMap.end()) { - return nullptr; - } - return it->second; -} - -std::shared_ptr LuaEnvironment::createCombatObject(LuaScriptInterface* interface) { - auto combat = std::make_shared(); - combatMap[++lastCombatId] = combat; - combatIdMap[interface].push_back(lastCombatId); - return combat; -} - -void LuaEnvironment::clearCombatObjects(LuaScriptInterface* interface) { - const auto it = combatIdMap.find(interface); - if (it == combatIdMap.end()) { - return; - } - - it->second.clear(); - combatMap.clear(); -} - const std::unique_ptr &LuaEnvironment::getAreaObject(uint32_t id) const { const auto it = areaMap.find(id); if (it == areaMap.end()) { diff --git a/src/lua/scripts/lua_environment.hpp b/src/lua/scripts/lua_environment.hpp index e7751bb4969..63efd2d7033 100644 --- a/src/lua/scripts/lua_environment.hpp +++ b/src/lua/scripts/lua_environment.hpp @@ -35,9 +35,7 @@ class LuaEnvironment final : public LuaScriptInterface { LuaEnvironment(const LuaEnvironment &) = delete; LuaEnvironment &operator=(const LuaEnvironment &) = delete; - static LuaEnvironment &getInstance() { - return inject(); - } + static LuaEnvironment &getInstance(); bool initState() override; bool reInitState() override; @@ -45,38 +43,6 @@ class LuaEnvironment final : public LuaScriptInterface { LuaScriptInterface* getTestInterface(); - std::shared_ptr getCombatObject(uint32_t id) const; - std::shared_ptr createCombatObject(LuaScriptInterface* interface); - void clearCombatObjects(LuaScriptInterface* interface); - - template - std::shared_ptr createWeaponObject(LuaScriptInterface* interface) { - auto weapon = std::make_shared(interface); - const auto weaponId = ++lastWeaponId; - weaponMap[weaponId] = weapon; - weaponIdMap[interface].push_back(weaponId); - return weapon; - } - - template - std::shared_ptr getWeaponObject(uint32_t id) const { - const auto it = weaponMap.find(id); - if (it == weaponMap.end()) { - return nullptr; - } - return it->second; - } - - void clearWeaponObjects(LuaScriptInterface* interface) { - const auto it = weaponIdMap.find(interface); - if (it == weaponIdMap.end()) { - return; - } - - it->second.clear(); - weaponMap.clear(); - } - const std::unique_ptr &getAreaObject(uint32_t id) const; uint32_t createAreaObject(LuaScriptInterface* interface); void clearAreaObjects(LuaScriptInterface* interface); @@ -96,14 +62,6 @@ class LuaEnvironment final : public LuaScriptInterface { phmap::flat_hash_map> areaIdMap; uint32_t lastAreaId = 0; - phmap::flat_hash_map> combatMap; - phmap::flat_hash_map> combatIdMap; - uint32_t lastCombatId = 0; - - std::unordered_map> weaponMap; - std::unordered_map> weaponIdMap; - uint32_t lastWeaponId = 0; - LuaScriptInterface* testInterface = nullptr; friend class LuaScriptInterface; diff --git a/src/lua/scripts/luascript.cpp b/src/lua/scripts/luascript.cpp index 44c2c92b477..7c0ce2c096c 100644 --- a/src/lua/scripts/luascript.cpp +++ b/src/lua/scripts/luascript.cpp @@ -16,8 +16,8 @@ ScriptEnvironment::DBResultMap ScriptEnvironment::tempResults; uint32_t ScriptEnvironment::lastResultId = 0; std::multimap> ScriptEnvironment::tempItems; -ScriptEnvironment LuaFunctionsLoader::scriptEnv[16]; -int32_t LuaFunctionsLoader::scriptEnvIndex = -1; +ScriptEnvironment Lua::scriptEnv[16]; +int32_t Lua::scriptEnvIndex = -1; LuaScriptInterface::LuaScriptInterface(std::string initInterfaceName) : interfaceName(std::move(initInterfaceName)) { @@ -28,7 +28,6 @@ LuaScriptInterface::~LuaScriptInterface() { } bool LuaScriptInterface::reInitState() { - g_luaEnvironment().clearCombatObjects(this); g_luaEnvironment().clearAreaObjects(this); closeState(); diff --git a/src/lua/scripts/luascript.hpp b/src/lua/scripts/luascript.hpp index 82ed9fa69af..c4d916a593c 100644 --- a/src/lua/scripts/luascript.hpp +++ b/src/lua/scripts/luascript.hpp @@ -12,7 +12,7 @@ #include "lua/functions/lua_functions_loader.hpp" #include "lua/scripts/script_environment.hpp" -class LuaScriptInterface : public LuaFunctionsLoader { +class LuaScriptInterface : public Lua { public: explicit LuaScriptInterface(std::string interfaceName); virtual ~LuaScriptInterface(); diff --git a/src/lua/scripts/scripts.cpp b/src/lua/scripts/scripts.cpp index 556ed1be50b..f4a141253c4 100644 --- a/src/lua/scripts/scripts.cpp +++ b/src/lua/scripts/scripts.cpp @@ -9,6 +9,7 @@ #include "lua/scripts/scripts.hpp" +#include "lib/di/container.hpp" #include "config/configmanager.hpp" #include "creatures/combat/spells.hpp" #include "creatures/monsters/monsters.hpp" @@ -24,6 +25,11 @@ Scripts::Scripts() : scriptInterface.initState(); } +Scripts &Scripts::getInstance() { + static Scripts instance; + return instance; +} + void Scripts::clearAllScripts() const { g_actions().clear(); g_creatureEvents().clear(); @@ -61,16 +67,18 @@ bool Scripts::loadEventSchedulerScripts(const std::string &fileName) { return false; } -bool Scripts::loadScripts(std::string loadPath, bool isLib, bool reload) { - const auto dir = std::filesystem::current_path() / loadPath; +bool Scripts::loadScripts(std::string_view folderName, bool isLib, bool reload) { + const auto dir = std::filesystem::current_path() / folderName; + // Checks if the folder exists and is really a folder if (!std::filesystem::exists(dir) || !std::filesystem::is_directory(dir)) { - g_logger().error("Can not load folder {}", loadPath); + g_logger().error("Can not load folder {}", folderName); return false; } // Declare a string variable to store the last directory std::string lastDirectory; + // Recursive iterate through all entries in the directory for (const auto &entry : std::filesystem::recursive_directory_iterator(dir)) { // Get the filename of the entry as a string @@ -78,11 +86,14 @@ bool Scripts::loadScripts(std::string loadPath, bool isLib, bool reload) { std::string fileFolder = realPath.parent_path().filename().string(); // Script folder, example: "actions" std::string scriptFolder = realPath.parent_path().string(); + // Create a string_view for the fileFolder and scriptFolder strings const std::string_view fileFolderView(fileFolder); const std::string_view scriptFolderView(scriptFolder); + // Filename, example: "demon.lua" std::string file(realPath.filename().string()); + if (!std::filesystem::is_regular_file(entry) || realPath.extension() != ".lua") { // Skip this entry if it is not a regular file or does not have a .lua extension continue; diff --git a/src/lua/scripts/scripts.hpp b/src/lua/scripts/scripts.hpp index 10ed18ed4f4..2efedb04232 100644 --- a/src/lua/scripts/scripts.hpp +++ b/src/lua/scripts/scripts.hpp @@ -9,118 +9,27 @@ #pragma once -#include "lib/di/container.hpp" #include "lua/scripts/luascript.hpp" class Scripts { public: - Scripts(); - // non-copyable Scripts(const Scripts &) = delete; Scripts &operator=(const Scripts &) = delete; - static Scripts &getInstance() { - return inject(); - } + static Scripts &getInstance(); void clearAllScripts() const; bool loadEventSchedulerScripts(const std::string &fileName); - bool loadScripts(std::string folderName, bool isLib, bool reload); + bool loadScripts(std::string_view folderName, bool isLib, bool reload); LuaScriptInterface &getScriptInterface() { return scriptInterface; } - /** - * @brief Get the Script Id object - * - * @return int32_t - */ - int32_t getScriptId() const { - return scriptId; - } private: - int32_t scriptId = 0; LuaScriptInterface scriptInterface; + Scripts(); }; constexpr auto g_scripts = Scripts::getInstance; - -class Script { -public: - /** - * @brief Explicit construtor - * explicit, that is, it cannot be used for implicit conversions and - * copy-initialization. - * - * @param interface Lua Script Interface - */ - explicit Script(LuaScriptInterface* interface) : - scriptInterface(interface) { } - virtual ~Script() = default; - - /** - * @brief Check if script is loaded - * - * @return true - * @return false - */ - bool isLoadedCallback() const { - return loadedCallback; - } - void setLoadedCallback(bool loaded) { - loadedCallback = loaded; - } - - // Load revscriptsys callback - bool loadCallback() { - if (!scriptInterface) { - g_logger().error("[Script::loadCallback] scriptInterface is nullptr, scriptid = {}", scriptId); - return false; - } - - if (scriptId != 0) { - g_logger().error("[Script::loadCallback] scriptid is not zero, scriptid = {}, scriptName {}", scriptId, scriptInterface->getLoadingScriptName()); - return false; - } - - const int32_t id = scriptInterface->getEvent(); - if (id == -1) { - g_logger().error("[Script::loadCallback] Event {} not found for script with name {}", getScriptTypeName(), scriptInterface->getLoadingScriptName()); - return false; - } - - setLoadedCallback(true); - scriptId = id; - return true; - } - - // NOTE: Pure virtual method ( = 0) that must be implemented in derived classes - // Script type (Action, CreatureEvent, GlobalEvent, MoveEvent, Spell, Weapon) - virtual std::string getScriptTypeName() const = 0; - - // Method to access the scriptInterface in derived classes - virtual LuaScriptInterface* getScriptInterface() const { - return scriptInterface; - } - - virtual void setScriptInterface(LuaScriptInterface* newInterface) { - scriptInterface = newInterface; - } - - // Method to access the scriptId in derived classes - virtual int32_t getScriptId() const { - return scriptId; - } - virtual void setScriptId(int32_t newScriptId) { - scriptId = newScriptId; - } - -private: - // If script is loaded callback - bool loadedCallback = false; - - int32_t scriptId = 0; - LuaScriptInterface* scriptInterface = nullptr; -}; diff --git a/src/server/network/protocol/protocolgame.cpp b/src/server/network/protocol/protocolgame.cpp index 15b435087bd..466a2413353 100644 --- a/src/server/network/protocol/protocolgame.cpp +++ b/src/server/network/protocol/protocolgame.cpp @@ -48,6 +48,7 @@ #include "lua/modules/modules.hpp" #include "server/network/message/outputmessage.hpp" #include "utils/tools.hpp" +#include "creatures/players/vocations/vocation.hpp" #include "enums/account_coins.hpp" #include "enums/account_group_type.hpp" diff --git a/src/server/signals.cpp b/src/server/signals.cpp index 29382c55809..1e6e0e4ece7 100644 --- a/src/server/signals.cpp +++ b/src/server/signals.cpp @@ -19,6 +19,7 @@ #include "lua/creature/events.hpp" #include "lua/global/globalevent.hpp" #include "lua/scripts/lua_environment.hpp" +#include "lib/di/container.hpp" Signals::Signals(asio::io_service &service) : set(service) {