From 72adc6e4bd02db4e8bef76af09367419ad9f7d8d Mon Sep 17 00:00:00 2001 From: Beats Date: Thu, 5 Oct 2023 01:51:37 -0300 Subject: [PATCH] improve: migrate Weapon and derivatives to shared_ptr (#1659) --- src/config/configmanager.cpp | 1 - src/creatures/combat/combat.cpp | 4 +- src/creatures/monsters/monster.cpp | 1 - src/creatures/npcs/npcs.cpp | 1 - src/creatures/players/player.cpp | 2 +- src/game/game.cpp | 6 -- src/game/scheduling/events_scheduler.cpp | 1 - src/game/zones/zone.cpp | 1 - src/io/iologindata.cpp | 1 - src/io/ioprey.cpp | 1 - src/items/item.cpp | 1 - src/items/items.cpp | 1 - src/items/tile.cpp | 1 - src/items/weapons/weapons.cpp | 6 +- src/items/weapons/weapons.hpp | 9 +- src/kv/kv.cpp | 1 - src/lua/callbacks/event_callback.cpp | 2 - .../functions/core/game/game_functions.cpp | 1 - src/lua/functions/core/game/lua_enums.cpp | 3 - .../functions/items/imbuement_functions.cpp | 1 - src/lua/functions/items/weapon_functions.cpp | 92 +++++++++---------- src/lua/functions/items/weapon_functions.hpp | 2 +- src/lua/functions/lua_functions_loader.cpp | 2 - src/lua/scripts/lua_environment.hpp | 6 +- src/lua/scripts/luascript.hpp | 2 +- src/lua/scripts/script_environment.cpp | 1 - src/map/utils/qtreenode.cpp | 1 - src/server/network/connection/connection.cpp | 1 - src/server/network/message/networkmessage.cpp | 1 - 29 files changed, 62 insertions(+), 91 deletions(-) diff --git a/src/config/configmanager.cpp b/src/config/configmanager.cpp index 940ea29eb51..3eacd9e9ade 100644 --- a/src/config/configmanager.cpp +++ b/src/config/configmanager.cpp @@ -12,7 +12,6 @@ #include "config/configmanager.hpp" #include "declarations.hpp" #include "game/game.hpp" -#include "lua/scripts/luajit_sync.hpp" #include "server/network/webhook/webhook.hpp" #if LUA_VERSION_NUM >= 502 diff --git a/src/creatures/combat/combat.cpp b/src/creatures/combat/combat.cpp index 606a1c132fb..8b650253567 100644 --- a/src/creatures/combat/combat.cpp +++ b/src/creatures/combat/combat.cpp @@ -73,7 +73,7 @@ CombatDamage Combat::getCombatDamage(std::shared_ptr creature, std::sh ); } else if (formulaType == COMBAT_FORMULA_SKILL) { std::shared_ptr tool = player->getWeapon(); - const Weapon* weapon = g_weapons().getWeapon(tool); + const WeaponShared_ptr weapon = g_weapons().getWeapon(tool); if (weapon) { damage.primary.value = normal_random( static_cast(minb), @@ -1514,7 +1514,7 @@ void ValueCallback::getMinMaxValues(std::shared_ptr player, CombatDamage case COMBAT_FORMULA_SKILL: { // onGetPlayerMinMaxValues(player, attackSkill, attackValue, attackFactor) std::shared_ptr tool = player->getWeapon(); - const Weapon* weapon = g_weapons().getWeapon(tool); + const WeaponShared_ptr weapon = g_weapons().getWeapon(tool); std::shared_ptr item = nullptr; if (weapon) { diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 4ef06634933..eef9aaa3830 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -14,7 +14,6 @@ #include "creatures/players/wheel/player_wheel.hpp" #include "game/game.hpp" #include "game/scheduling/dispatcher.hpp" -#include "lua/creature/events.hpp" #include "lua/callbacks/event_callback.hpp" #include "lua/callbacks/events_callbacks.hpp" #include "map/spectators.hpp" diff --git a/src/creatures/npcs/npcs.cpp b/src/creatures/npcs/npcs.cpp index 432b0adbe15..c18897d1c4c 100644 --- a/src/creatures/npcs/npcs.cpp +++ b/src/creatures/npcs/npcs.cpp @@ -11,7 +11,6 @@ #include "declarations.hpp" #include "creatures/combat/combat.hpp" -#include "creatures/creature.hpp" #include "lua/scripts/lua_environment.hpp" #include "creatures/combat/spells.hpp" #include "creatures/npcs/npcs.hpp" diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index 59b9292662d..0ec4bedf8a4 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -4222,7 +4222,7 @@ void Player::doAttacking(uint32_t) { bool result = false; std::shared_ptr tool = getWeapon(); - const Weapon* weapon = g_weapons().getWeapon(tool); + const WeaponShared_ptr weapon = g_weapons().getWeapon(tool); uint32_t delay = getAttackSpeed(); bool classicSpeed = g_configManager().getBoolean(CLASSIC_ATTACK_SPEED); diff --git a/src/game/game.cpp b/src/game/game.cpp index 88264725b41..9f853ab6b22 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -12,13 +12,11 @@ #include "lua/creature/actions.hpp" #include "items/bed.hpp" #include "creatures/creature.hpp" -#include "lua/creature/creatureevent.hpp" #include "database/databasetasks.hpp" #include "lua/creature/events.hpp" #include "lua/callbacks/event_callback.hpp" #include "lua/callbacks/events_callbacks.hpp" #include "game/game.hpp" -#include "game/functions/game_reload.hpp" #include "game/zones/zone.hpp" #include "lua/global/globalevent.hpp" #include "io/iologindata.hpp" @@ -34,13 +32,9 @@ #include "creatures/combat/spells.hpp" #include "lua/creature/talkaction.hpp" #include "items/weapons/weapons.hpp" -#include "lua/scripts/scripts.hpp" -#include "lua/modules/modules.hpp" #include "creatures/players/imbuements/imbuements.hpp" -#include "account/account.hpp" #include "creatures/players/wheel/player_wheel.hpp" #include "creatures/npcs/npc.hpp" -#include "creatures/npcs/npcs.hpp" #include "server/network/webhook/webhook.hpp" #include "protobuf/appearances.pb.h" #include "server/network/protocol/protocollogin.hpp" diff --git a/src/game/scheduling/events_scheduler.cpp b/src/game/scheduling/events_scheduler.cpp index f7bd197979e..d5c71378f4a 100644 --- a/src/game/scheduling/events_scheduler.cpp +++ b/src/game/scheduling/events_scheduler.cpp @@ -12,7 +12,6 @@ #include "config/configmanager.hpp" #include "game/scheduling/events_scheduler.hpp" #include "lua/scripts/scripts.hpp" -#include "utils/pugicast.hpp" bool EventsScheduler::loadScheduleEventFromXml() { pugi::xml_document doc; diff --git a/src/game/zones/zone.cpp b/src/game/zones/zone.cpp index e06975ff7a6..f1aa300efee 100644 --- a/src/game/zones/zone.cpp +++ b/src/game/zones/zone.cpp @@ -14,7 +14,6 @@ #include "creatures/monsters/monster.hpp" #include "creatures/npcs/npc.hpp" #include "creatures/players/player.hpp" -#include "game/scheduling/dispatcher.hpp" phmap::parallel_flat_hash_map> Zone::zones = {}; const static std::shared_ptr nullZone = nullptr; diff --git a/src/io/iologindata.cpp b/src/io/iologindata.cpp index 307d41a9cb3..f32b90568a4 100644 --- a/src/io/iologindata.cpp +++ b/src/io/iologindata.cpp @@ -15,7 +15,6 @@ #include "game/game.hpp" #include "creatures/monsters/monster.hpp" #include "creatures/players/wheel/player_wheel.hpp" -#include "io/ioprey.hpp" bool IOLoginData::gameWorldAuthentication(const std::string &accountDescriptor, const std::string &password, std::string &characterName, uint32_t &accountId, bool oldProtocol) { account::Account account(accountDescriptor); diff --git a/src/io/ioprey.cpp b/src/io/ioprey.cpp index b81dad1fdf7..50c3070b4a0 100644 --- a/src/io/ioprey.cpp +++ b/src/io/ioprey.cpp @@ -9,7 +9,6 @@ #include "pch.hpp" -#include "declarations.hpp" #include "creatures/monsters/monster.hpp" #include "creatures/players/player.hpp" #include "config/configmanager.hpp" diff --git a/src/items/item.cpp b/src/items/item.cpp index ea04be89385..b6902575bb2 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -10,7 +10,6 @@ #include "pch.hpp" #include "items/item.hpp" -#include "items/functions/item/item_parse.hpp" #include "items/containers/container.hpp" #include "items/decay/decay.hpp" #include "game/movement/teleport.hpp" diff --git a/src/items/items.cpp b/src/items/items.cpp index ca8e20e5b30..690e01a5b92 100644 --- a/src/items/items.cpp +++ b/src/items/items.cpp @@ -11,7 +11,6 @@ #include "items/functions/item/item_parse.hpp" #include "items/items.hpp" -#include "items/weapons/weapons.hpp" #include "game/game.hpp" #include "utils/pugicast.hpp" diff --git a/src/items/tile.cpp b/src/items/tile.cpp index 25f7f865688..0d9a5db0ead 100644 --- a/src/items/tile.cpp +++ b/src/items/tile.cpp @@ -19,7 +19,6 @@ #include "lua/creature/movement.hpp" #include "game/movement/teleport.hpp" #include "items/trashholder.hpp" -#include "map/house/housetile.hpp" #include "io/iomap.hpp" #include "map/spectators.hpp" diff --git a/src/items/weapons/weapons.cpp b/src/items/weapons/weapons.cpp index 995b71518c0..08f1d75c032 100644 --- a/src/items/weapons/weapons.cpp +++ b/src/items/weapons/weapons.cpp @@ -17,7 +17,7 @@ Weapons::Weapons() = default; Weapons::~Weapons() = default; -const Weapon* Weapons::getWeapon(std::shared_ptr item) const { +const WeaponShared_ptr Weapons::getWeapon(std::shared_ptr item) const { if (!item) { return nullptr; } @@ -33,7 +33,7 @@ void Weapons::clear() { weapons.clear(); } -bool Weapons::registerLuaEvent(Weapon* event) { +bool Weapons::registerLuaEvent(WeaponShared_ptr event) { weapons[event->getID()] = event; return true; } @@ -524,7 +524,7 @@ bool WeaponDistance::useWeapon(std::shared_ptr player, std::shared_ptr mainWeaponItem = player->getWeapon(true); - const Weapon* mainWeapon = g_weapons().getWeapon(mainWeaponItem); + const WeaponShared_ptr mainWeapon = g_weapons().getWeapon(mainWeaponItem); if (mainWeapon) { damageModifier = mainWeapon->playerWeaponCheck(player, target, mainWeaponItem->getShootRange()); } else { diff --git a/src/items/weapons/weapons.hpp b/src/items/weapons/weapons.hpp index 90785983e17..78a7195fb3c 100644 --- a/src/items/weapons/weapons.hpp +++ b/src/items/weapons/weapons.hpp @@ -21,7 +21,8 @@ class WeaponMelee; class WeaponDistance; class WeaponWand; -using Weapon_ptr = std::unique_ptr; +using WeaponUnique_ptr = std::unique_ptr; +using WeaponShared_ptr = std::shared_ptr; class Weapons final : public Scripts { public: @@ -36,16 +37,16 @@ class Weapons final : public Scripts { return inject(); } - const Weapon* getWeapon(std::shared_ptr item) const; + const WeaponShared_ptr getWeapon(std::shared_ptr item) const; static int32_t getMaxMeleeDamage(int32_t attackSkill, int32_t attackValue); static int32_t getMaxWeaponDamage(uint32_t level, int32_t attackSkill, int32_t attackValue, float attackFactor, bool isMelee); - bool registerLuaEvent(Weapon* event); + bool registerLuaEvent(WeaponShared_ptr event); void clear(); private: - std::map weapons; + std::map weapons; }; constexpr auto g_weapons = Weapons::getInstance; diff --git a/src/kv/kv.cpp b/src/kv/kv.cpp index cd2a8b974b5..e5dbc974b76 100644 --- a/src/kv/kv.cpp +++ b/src/kv/kv.cpp @@ -11,7 +11,6 @@ #include "kv/kv.hpp" #include "lib/di/container.hpp" -#include "utils/tools.hpp" KVStore &KVStore::getInstance() { return inject(); diff --git a/src/lua/callbacks/event_callback.cpp b/src/lua/callbacks/event_callback.cpp index 6444446bde9..8510502b226 100644 --- a/src/lua/callbacks/event_callback.cpp +++ b/src/lua/callbacks/event_callback.cpp @@ -11,8 +11,6 @@ #include "lua/callbacks/event_callback.hpp" -#include "lua/callbacks/event_callback.hpp" -#include "lua/callbacks/events_callbacks.hpp" #include "utils/tools.hpp" #include "items/item.hpp" #include "creatures/players/player.hpp" diff --git a/src/lua/functions/core/game/game_functions.cpp b/src/lua/functions/core/game/game_functions.cpp index 86358f1daf7..ec59b8b1c34 100644 --- a/src/lua/functions/core/game/game_functions.cpp +++ b/src/lua/functions/core/game/game_functions.cpp @@ -23,7 +23,6 @@ #include "lua/creature/talkaction.hpp" #include "lua/functions/creatures/npc/npc_type_functions.hpp" #include "lua/scripts/lua_environment.hpp" -#include "lua/scripts/scripts.hpp" #include "lua/creature/events.hpp" #include "lua/callbacks/event_callback.hpp" #include "lua/callbacks/events_callbacks.hpp" diff --git a/src/lua/functions/core/game/lua_enums.cpp b/src/lua/functions/core/game/lua_enums.cpp index 8d86185290f..fc1586f3363 100644 --- a/src/lua/functions/core/game/lua_enums.cpp +++ b/src/lua/functions/core/game/lua_enums.cpp @@ -11,15 +11,12 @@ #include "lua/functions/core/game/lua_enums.hpp" -#include "account/account.hpp" #include "creatures/players/wheel/wheel_definitions.hpp" #include "io/io_bosstiary.hpp" #include "config/configmanager.hpp" #include "creatures/creature.hpp" -#include "lua/creature/creatureevent.hpp" #include "declarations.hpp" #include "game/functions/game_reload.hpp" -#include "game/game.hpp" #define registerEnumClass(luaState, enumClassType) \ { \ diff --git a/src/lua/functions/items/imbuement_functions.cpp b/src/lua/functions/items/imbuement_functions.cpp index f512e56a62b..2a4e85d7d88 100644 --- a/src/lua/functions/items/imbuement_functions.cpp +++ b/src/lua/functions/items/imbuement_functions.cpp @@ -9,7 +9,6 @@ #include "pch.hpp" -#include "items/item.hpp" #include "items/weapons/weapons.hpp" #include "creatures/players/imbuements/imbuements.hpp" #include "lua/functions/items/imbuement_functions.hpp" diff --git a/src/lua/functions/items/weapon_functions.cpp b/src/lua/functions/items/weapon_functions.cpp index 2e5fde7ff15..3a41d0ac75d 100644 --- a/src/lua/functions/items/weapon_functions.cpp +++ b/src/lua/functions/items/weapon_functions.cpp @@ -25,7 +25,7 @@ int WeaponFunctions::luaCreateWeapon(lua_State* L) { case WEAPON_AXE: case WEAPON_CLUB: { if (auto weaponPtr = g_luaEnvironment().createWeaponObject(getScriptEnv()->getScriptInterface())) { - pushUserdata(L, weaponPtr.get()); + pushUserdata(L, weaponPtr); setMetatable(L, -1, "Weapon"); weaponPtr->weaponType = type; } else { @@ -37,7 +37,7 @@ int WeaponFunctions::luaCreateWeapon(lua_State* L) { case WEAPON_DISTANCE: case WEAPON_AMMO: { if (auto weaponPtr = g_luaEnvironment().createWeaponObject(getScriptEnv()->getScriptInterface())) { - pushUserdata(L, weaponPtr.get()); + pushUserdata(L, weaponPtr); setMetatable(L, -1, "Weapon"); weaponPtr->weaponType = type; } else { @@ -47,7 +47,7 @@ int WeaponFunctions::luaCreateWeapon(lua_State* L) { } case WEAPON_WAND: { if (auto weaponPtr = g_luaEnvironment().createWeaponObject(getScriptEnv()->getScriptInterface())) { - pushUserdata(L, weaponPtr.get()); + pushUserdata(L, weaponPtr); setMetatable(L, -1, "Weapon"); weaponPtr->weaponType = type; } else { @@ -65,7 +65,7 @@ int WeaponFunctions::luaCreateWeapon(lua_State* L) { int WeaponFunctions::luaWeaponAction(lua_State* L) { // weapon:action(callback) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { std::string typeName = getString(L, 2); std::string tmpStr = asLowerCaseString(typeName); @@ -90,15 +90,15 @@ int WeaponFunctions::luaWeaponAction(lua_State* L) { int WeaponFunctions::luaWeaponRegister(lua_State* L) { // weapon:register() - Weapon** weaponPtr = getRawUserdata(L, 1); + WeaponShared_ptr* weaponPtr = getRawUserDataShared(L, 1); if (weaponPtr && *weaponPtr) { - Weapon* weapon = *weaponPtr; + WeaponShared_ptr weapon = *weaponPtr; if (weapon->weaponType == WEAPON_DISTANCE || weapon->weaponType == WEAPON_AMMO || weapon->weaponType == WEAPON_MISSILE) { - weapon = getUserdata(L, 1); + weapon = getUserdataShared(L, 1); } else if (weapon->weaponType == WEAPON_WAND) { - weapon = getUserdata(L, 1); + weapon = getUserdataShared(L, 1); } else { - weapon = getUserdata(L, 1); + weapon = getUserdataShared(L, 1); } uint16_t id = weapon->getID(); @@ -123,7 +123,7 @@ int WeaponFunctions::luaWeaponRegister(lua_State* L) { int WeaponFunctions::luaWeaponOnUseWeapon(lua_State* L) { // weapon:onUseWeapon(callback) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { if (!weapon->loadCallback()) { pushBoolean(L, false); @@ -139,7 +139,7 @@ int WeaponFunctions::luaWeaponOnUseWeapon(lua_State* L) { int WeaponFunctions::luaWeaponUnproperly(lua_State* L) { // weapon:wieldedUnproperly(bool) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setWieldUnproperly(getBoolean(L, 2)); pushBoolean(L, true); @@ -151,7 +151,7 @@ int WeaponFunctions::luaWeaponUnproperly(lua_State* L) { int WeaponFunctions::luaWeaponLevel(lua_State* L) { // weapon:level(lvl) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setRequiredLevel(getNumber(L, 2)); weapon->setWieldInfo(WIELDINFO_LEVEL); @@ -164,7 +164,7 @@ int WeaponFunctions::luaWeaponLevel(lua_State* L) { int WeaponFunctions::luaWeaponMagicLevel(lua_State* L) { // weapon:magicLevel(lvl) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setRequiredMagLevel(getNumber(L, 2)); weapon->setWieldInfo(WIELDINFO_MAGLV); @@ -177,7 +177,7 @@ int WeaponFunctions::luaWeaponMagicLevel(lua_State* L) { int WeaponFunctions::luaWeaponMana(lua_State* L) { // weapon:mana(mana) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setMana(getNumber(L, 2)); pushBoolean(L, true); @@ -189,7 +189,7 @@ int WeaponFunctions::luaWeaponMana(lua_State* L) { int WeaponFunctions::luaWeaponManaPercent(lua_State* L) { // weapon:manaPercent(percent) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setManaPercent(getNumber(L, 2)); pushBoolean(L, true); @@ -201,7 +201,7 @@ int WeaponFunctions::luaWeaponManaPercent(lua_State* L) { int WeaponFunctions::luaWeaponHealth(lua_State* L) { // weapon:health(health) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setHealth(getNumber(L, 2)); pushBoolean(L, true); @@ -213,7 +213,7 @@ int WeaponFunctions::luaWeaponHealth(lua_State* L) { int WeaponFunctions::luaWeaponHealthPercent(lua_State* L) { // weapon:healthPercent(percent) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setHealthPercent(getNumber(L, 2)); pushBoolean(L, true); @@ -225,7 +225,7 @@ int WeaponFunctions::luaWeaponHealthPercent(lua_State* L) { int WeaponFunctions::luaWeaponSoul(lua_State* L) { // weapon:soul(soul) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setSoul(getNumber(L, 2)); pushBoolean(L, true); @@ -237,7 +237,7 @@ int WeaponFunctions::luaWeaponSoul(lua_State* L) { int WeaponFunctions::luaWeaponBreakChance(lua_State* L) { // weapon:breakChance(percent) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setBreakChance(getNumber(L, 2)); pushBoolean(L, true); @@ -249,7 +249,7 @@ 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! - WeaponWand* weapon = getUserdata(L, 1); + const auto &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setMinChange(getNumber(L, 2)); if (lua_gettop(L) > 2) { @@ -266,7 +266,7 @@ int WeaponFunctions::luaWeaponWandDamage(lua_State* L) { int WeaponFunctions::luaWeaponElement(lua_State* L) { // weapon:element(combatType) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { if (!getNumber(L, 2)) { std::string element = getString(L, 2); @@ -300,7 +300,7 @@ int WeaponFunctions::luaWeaponElement(lua_State* L) { int WeaponFunctions::luaWeaponPremium(lua_State* L) { // weapon:premium(bool) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setNeedPremium(getBoolean(L, 2)); weapon->setWieldInfo(WIELDINFO_PREMIUM); @@ -313,7 +313,7 @@ int WeaponFunctions::luaWeaponPremium(lua_State* L) { int WeaponFunctions::luaWeaponVocation(lua_State* L) { // weapon:vocation(vocName[, showInDescription = false, lastVoc = false]) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->addVocWeaponMap(getString(L, 2)); weapon->setWieldInfo(WIELDINFO_VOCREQ); @@ -352,7 +352,7 @@ int WeaponFunctions::luaWeaponVocation(lua_State* L) { int WeaponFunctions::luaWeaponId(lua_State* L) { // weapon:id(id) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { weapon->setID(getNumber(L, 2)); pushBoolean(L, true); @@ -364,7 +364,7 @@ int WeaponFunctions::luaWeaponId(lua_State* L) { int WeaponFunctions::luaWeaponAttack(lua_State* L) { // weapon:attack(atk) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); @@ -378,7 +378,7 @@ int WeaponFunctions::luaWeaponAttack(lua_State* L) { int WeaponFunctions::luaWeaponDefense(lua_State* L) { // weapon:defense(defense[, extraDefense]) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); @@ -395,7 +395,7 @@ int WeaponFunctions::luaWeaponDefense(lua_State* L) { int WeaponFunctions::luaWeaponRange(lua_State* L) { // weapon:range(range) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); @@ -409,7 +409,7 @@ int WeaponFunctions::luaWeaponRange(lua_State* L) { int WeaponFunctions::luaWeaponCharges(lua_State* L) { // weapon:charges(charges[, showCharges = true]) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { bool showCharges = true; if (lua_gettop(L) > 2) { @@ -428,7 +428,7 @@ int WeaponFunctions::luaWeaponCharges(lua_State* L) { int WeaponFunctions::luaWeaponDuration(lua_State* L) { // weapon:duration(duration[, showDuration = true]) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { bool showDuration = true; if (lua_gettop(L) > 2) { @@ -447,7 +447,7 @@ int WeaponFunctions::luaWeaponDuration(lua_State* L) { int WeaponFunctions::luaWeaponDecayTo(lua_State* L) { // weapon:decayTo([itemid = 0] - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t itemid = 0; if (lua_gettop(L) > 1) { @@ -465,7 +465,7 @@ int WeaponFunctions::luaWeaponDecayTo(lua_State* L) { int WeaponFunctions::luaWeaponTransformEquipTo(lua_State* L) { // weapon:transformEquipTo(itemid) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); @@ -479,7 +479,7 @@ int WeaponFunctions::luaWeaponTransformEquipTo(lua_State* L) { int WeaponFunctions::luaWeaponTransformDeEquipTo(lua_State* L) { // weapon:transformDeEquipTo(itemid) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); @@ -493,7 +493,7 @@ int WeaponFunctions::luaWeaponTransformDeEquipTo(lua_State* L) { int WeaponFunctions::luaWeaponShootType(lua_State* L) { // weapon:shootType(type) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); @@ -507,7 +507,7 @@ int WeaponFunctions::luaWeaponShootType(lua_State* L) { int WeaponFunctions::luaWeaponSlotType(lua_State* L) { // weapon:slotType(slot) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); @@ -527,7 +527,7 @@ int WeaponFunctions::luaWeaponSlotType(lua_State* L) { int WeaponFunctions::luaWeaponAmmoType(lua_State* L) { // weapon:ammoType(type) - WeaponDistance* weapon = getUserdata(L, 1); + const auto &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); @@ -553,7 +553,7 @@ int WeaponFunctions::luaWeaponAmmoType(lua_State* L) { int WeaponFunctions::luaWeaponHitChance(lua_State* L) { // weapon:hitChance(chance) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); @@ -567,7 +567,7 @@ int WeaponFunctions::luaWeaponHitChance(lua_State* L) { int WeaponFunctions::luaWeaponMaxHitChance(lua_State* L) { // weapon:maxHitChance(max) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); @@ -581,34 +581,34 @@ int WeaponFunctions::luaWeaponMaxHitChance(lua_State* L) { int WeaponFunctions::luaWeaponExtraElement(lua_State* L) { // weapon:extraElement(atk, combatType) - Weapon* weapon = getUserdata(L, 1); + const WeaponShared_ptr &weapon = getUserdataShared(L, 1); if (weapon) { uint16_t id = weapon->getID(); ItemType &it = Item::items.getItemType(id); - it.abilities.get()->elementDamage = getNumber(L, 2); + it.abilities->elementDamage = getNumber(L, 2); if (!getNumber(L, 3)) { std::string element = getString(L, 3); std::string tmpStrValue = asLowerCaseString(element); if (tmpStrValue == "earth") { - it.abilities.get()->elementType = COMBAT_EARTHDAMAGE; + it.abilities->elementType = COMBAT_EARTHDAMAGE; } else if (tmpStrValue == "ice") { - it.abilities.get()->elementType = COMBAT_ICEDAMAGE; + it.abilities->elementType = COMBAT_ICEDAMAGE; } else if (tmpStrValue == "energy") { - it.abilities.get()->elementType = COMBAT_ENERGYDAMAGE; + it.abilities->elementType = COMBAT_ENERGYDAMAGE; } else if (tmpStrValue == "fire") { - it.abilities.get()->elementType = COMBAT_FIREDAMAGE; + it.abilities->elementType = COMBAT_FIREDAMAGE; } else if (tmpStrValue == "death") { - it.abilities.get()->elementType = COMBAT_DEATHDAMAGE; + it.abilities->elementType = COMBAT_DEATHDAMAGE; } else if (tmpStrValue == "holy") { - it.abilities.get()->elementType = COMBAT_HOLYDAMAGE; + it.abilities->elementType = COMBAT_HOLYDAMAGE; } else { g_logger().warn("[WeaponFunctions:luaWeaponExtraElement] - " "Type {} does not exist", element); } } else { - it.abilities.get()->elementType = getNumber(L, 3); + it.abilities->elementType = getNumber(L, 3); } pushBoolean(L, true); } else { diff --git a/src/lua/functions/items/weapon_functions.hpp b/src/lua/functions/items/weapon_functions.hpp index 2f0d2e8ccb6..01ba6966317 100644 --- a/src/lua/functions/items/weapon_functions.hpp +++ b/src/lua/functions/items/weapon_functions.hpp @@ -14,7 +14,7 @@ class WeaponFunctions final : LuaScriptInterface { public: static void init(lua_State* L) { - registerClass(L, "Weapon", "", WeaponFunctions::luaCreateWeapon); + registerSharedClass(L, "Weapon", "", WeaponFunctions::luaCreateWeapon); registerMethod(L, "Weapon", "action", WeaponFunctions::luaWeaponAction); registerMethod(L, "Weapon", "register", WeaponFunctions::luaWeaponRegister); registerMethod(L, "Weapon", "id", WeaponFunctions::luaWeaponId); diff --git a/src/lua/functions/lua_functions_loader.cpp b/src/lua/functions/lua_functions_loader.cpp index 944849b1e6f..cf377592988 100644 --- a/src/lua/functions/lua_functions_loader.cpp +++ b/src/lua/functions/lua_functions_loader.cpp @@ -12,13 +12,11 @@ #include "creatures/combat/spells.hpp" #include "creatures/monsters/monster.hpp" #include "creatures/npcs/npc.hpp" -#include "creatures/players/imbuements/imbuements.hpp" #include "creatures/players/player.hpp" #include "creatures/players/grouping/guild.hpp" #include "game/zones/zone.hpp" #include "game/game.hpp" #include "game/movement/teleport.hpp" -#include "items/weapons/weapons.hpp" #include "lua/functions/core/core_functions.hpp" #include "lua/functions/creatures/creature_functions.hpp" #include "lua/functions/events/events_functions.hpp" diff --git a/src/lua/scripts/lua_environment.hpp b/src/lua/scripts/lua_environment.hpp index 216abb8ec78..042a21fe1d8 100644 --- a/src/lua/scripts/lua_environment.hpp +++ b/src/lua/scripts/lua_environment.hpp @@ -25,7 +25,7 @@ class LuaEnvironment : public LuaScriptInterface { static bool shuttingDown; LuaEnvironment(); - ~LuaEnvironment(); + ~LuaEnvironment() override; lua_State* getLuaState() override; @@ -38,7 +38,7 @@ class LuaEnvironment : public LuaScriptInterface { } bool initState() override; - bool reInitState(); + bool reInitState() override; bool closeState() override; LuaScriptInterface* getTestInterface(); @@ -50,7 +50,7 @@ class LuaEnvironment : public LuaScriptInterface { template std::shared_ptr createWeaponObject(LuaScriptInterface* interface) { auto weapon = std::make_shared(interface); - int weaponId = ++lastWeaponId; + auto weaponId = ++lastWeaponId; weaponMap[weaponId] = weapon; weaponIdMap[interface].push_back(weaponId); return weapon; diff --git a/src/lua/scripts/luascript.hpp b/src/lua/scripts/luascript.hpp index 80541e86d18..8f0b3b36c19 100644 --- a/src/lua/scripts/luascript.hpp +++ b/src/lua/scripts/luascript.hpp @@ -23,7 +23,7 @@ class LuaScriptInterface : public LuaFunctionsLoader { LuaScriptInterface &operator=(const LuaScriptInterface &) = delete; virtual bool initState(); - bool reInitState(); + virtual bool reInitState(); int32_t loadFile(const std::string &file, const std::string &scriptName); diff --git a/src/lua/scripts/script_environment.cpp b/src/lua/scripts/script_environment.cpp index 8899a954f69..ccebdb1a8dd 100644 --- a/src/lua/scripts/script_environment.cpp +++ b/src/lua/scripts/script_environment.cpp @@ -9,7 +9,6 @@ #include "pch.hpp" -#include "declarations.hpp" #include "game/game.hpp" #include "lua/scripts/luascript.hpp" #include "lua/scripts/script_environment.hpp" diff --git a/src/map/utils/qtreenode.cpp b/src/map/utils/qtreenode.cpp index 92d0a0423ee..ace469e14aa 100644 --- a/src/map/utils/qtreenode.cpp +++ b/src/map/utils/qtreenode.cpp @@ -10,7 +10,6 @@ #include "pch.hpp" #include "creatures/creature.hpp" -#include "map/mapcache.hpp" #include "qtreenode.hpp" bool QTreeLeafNode::newLeaf = false; diff --git a/src/server/network/connection/connection.cpp b/src/server/network/connection/connection.cpp index 04e7a82a64f..c2df08b2824 100644 --- a/src/server/network/connection/connection.cpp +++ b/src/server/network/connection/connection.cpp @@ -12,7 +12,6 @@ #include "server/network/connection/connection.hpp" #include "server/network/message/outputmessage.hpp" #include "server/network/protocol/protocol.hpp" -#include "server/network/protocol/protocolgame.hpp" #include "game/scheduling/scheduler.hpp" #include "game/scheduling/dispatcher.hpp" #include "server/server.hpp" diff --git a/src/server/network/message/networkmessage.cpp b/src/server/network/message/networkmessage.cpp index 9c7b5a333cb..457e3b32be0 100644 --- a/src/server/network/message/networkmessage.cpp +++ b/src/server/network/message/networkmessage.cpp @@ -11,7 +11,6 @@ #include "server/network/message/networkmessage.hpp" #include "items/containers/container.hpp" -#include "creatures/creature.hpp" int32_t NetworkMessage::decodeHeader() { int32_t newSize = buffer[0] | buffer[1] << 8;