Skip to content

Commit

Permalink
improve: migrate Weapon and derivatives to shared_ptr (opentibiabr#1659)
Browse files Browse the repository at this point in the history
  • Loading branch information
beats-dh authored Oct 5, 2023
1 parent 04ba21a commit 72adc6e
Show file tree
Hide file tree
Showing 29 changed files with 62 additions and 91 deletions.
1 change: 0 additions & 1 deletion src/config/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ CombatDamage Combat::getCombatDamage(std::shared_ptr<Creature> creature, std::sh
);
} else if (formulaType == COMBAT_FORMULA_SKILL) {
std::shared_ptr<Item> 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<int32_t>(minb),
Expand Down Expand Up @@ -1514,7 +1514,7 @@ void ValueCallback::getMinMaxValues(std::shared_ptr<Player> player, CombatDamage
case COMBAT_FORMULA_SKILL: {
// onGetPlayerMinMaxValues(player, attackSkill, attackValue, attackFactor)
std::shared_ptr<Item> tool = player->getWeapon();
const Weapon* weapon = g_weapons().getWeapon(tool);
const WeaponShared_ptr weapon = g_weapons().getWeapon(tool);
std::shared_ptr<Item> item = nullptr;

if (weapon) {
Expand Down
1 change: 0 additions & 1 deletion src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion src/creatures/npcs/npcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4222,7 +4222,7 @@ void Player::doAttacking(uint32_t) {
bool result = false;

std::shared_ptr<Item> 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);

Expand Down
6 changes: 0 additions & 6 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
1 change: 0 additions & 1 deletion src/game/scheduling/events_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/game/zones/zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::shared_ptr<Zone>> Zone::zones = {};
const static std::shared_ptr<Zone> nullZone = nullptr;
Expand Down
1 change: 0 additions & 1 deletion src/io/iologindata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/io/ioprey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "pch.hpp"

#include "declarations.hpp"
#include "creatures/monsters/monster.hpp"
#include "creatures/players/player.hpp"
#include "config/configmanager.hpp"
Expand Down
1 change: 0 additions & 1 deletion src/items/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion src/items/items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 0 additions & 1 deletion src/items/tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 3 additions & 3 deletions src/items/weapons/weapons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Weapons::Weapons() = default;
Weapons::~Weapons() = default;

const Weapon* Weapons::getWeapon(std::shared_ptr<Item> item) const {
const WeaponShared_ptr Weapons::getWeapon(std::shared_ptr<Item> item) const {
if (!item) {
return nullptr;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -524,7 +524,7 @@ bool WeaponDistance::useWeapon(std::shared_ptr<Player> player, std::shared_ptr<I
const ItemType &it = Item::items[id];
if (it.weaponType == WEAPON_AMMO) {
std::shared_ptr<Item> 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 {
Expand Down
9 changes: 5 additions & 4 deletions src/items/weapons/weapons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class WeaponMelee;
class WeaponDistance;
class WeaponWand;

using Weapon_ptr = std::unique_ptr<Weapon>;
using WeaponUnique_ptr = std::unique_ptr<Weapon>;
using WeaponShared_ptr = std::shared_ptr<Weapon>;

class Weapons final : public Scripts {
public:
Expand All @@ -36,16 +37,16 @@ class Weapons final : public Scripts {
return inject<Weapons>();
}

const Weapon* getWeapon(std::shared_ptr<Item> item) const;
const WeaponShared_ptr getWeapon(std::shared_ptr<Item> 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<uint32_t, Weapon*> weapons;
std::map<uint32_t, WeaponShared_ptr> weapons;
};

constexpr auto g_weapons = Weapons::getInstance;
Expand Down
1 change: 0 additions & 1 deletion src/kv/kv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "kv/kv.hpp"
#include "lib/di/container.hpp"
#include "utils/tools.hpp"

KVStore &KVStore::getInstance() {
return inject<KVStore>();
Expand Down
2 changes: 0 additions & 2 deletions src/lua/callbacks/event_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion src/lua/functions/core/game/game_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions src/lua/functions/core/game/lua_enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
{ \
Expand Down
1 change: 0 additions & 1 deletion src/lua/functions/items/imbuement_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 72adc6e

Please sign in to comment.