From 9a6b88d6ad3524af4f57c5bbf0d862c9dbc69fde Mon Sep 17 00:00:00 2001 From: Luan Santos Date: Sat, 9 Dec 2023 17:55:21 -0800 Subject: [PATCH] improve: more reliable quickloot/autoloot --- src/creatures/creature.cpp | 14 +++++++------- src/game/game.cpp | 8 ++++---- src/game/game.hpp | 8 +------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/creatures/creature.cpp b/src/creatures/creature.cpp index 4720b903233..bc77050268d 100644 --- a/src/creatures/creature.cpp +++ b/src/creatures/creature.cpp @@ -800,23 +800,23 @@ bool Creature::dropCorpse(std::shared_ptr lastHitCreature, std::shared corpse->startDecaying(); bool corpses = corpse->isRewardCorpse() || (corpse->getID() == ITEM_MALE_CORPSE || corpse->getID() == ITEM_FEMALE_CORPSE); const auto player = mostDamageCreature ? mostDamageCreature->getPlayer() : nullptr; - if (corpse->getContainer() && player && !corpses) { + auto corpseContainer = corpse->getContainer(); + if (corpseContainer && player && !corpses) { auto monster = getMonster(); if (monster && !monster->isRewardBoss()) { std::ostringstream lootMessage; - lootMessage << "Loot of " << getNameDescription() << ": " << corpse->getContainer()->getContentDescription(player->getProtocolVersion() < 1200) << "."; - auto suffix = corpse->getContainer()->getAttribute(ItemAttribute_t::LOOTMESSAGE_SUFFIX); + lootMessage << "Loot of " << getNameDescription() << ": " << corpseContainer->getContentDescription(player->getProtocolVersion() < 1200) << "."; + auto suffix = corpseContainer->getAttribute(ItemAttribute_t::LOOTMESSAGE_SUFFIX); if (!suffix.empty()) { lootMessage << suffix; } player->sendLootMessage(lootMessage.str()); } - if (player->checkAutoLoot()) { - int32_t pos = tile->getStackposOfItem(player, corpse); + if (player->checkAutoLoot() && corpseContainer && mostDamageCreature->getPlayer()) { g_dispatcher().addEvent( - std::bind(&Game::playerQuickLoot, &g_game(), mostDamageCreature->getID(), this->getPosition(), corpse->getID(), pos - 1, nullptr, false, true), - "Game::playerQuickLoot" + std::bind(&Game::playerQuickLootCorpse, &g_game(), player, corpseContainer), + "Game::playerQuickLootCorpse" ); } } diff --git a/src/game/game.cpp b/src/game/game.cpp index 5b973f3776a..c00eb4e1f1a 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -2539,7 +2539,7 @@ ReturnValue Game::internalTeleport(const std::shared_ptr &thing, const Po return RETURNVALUE_NOTPOSSIBLE; } -void Game::internalQuickLootCorpse(std::shared_ptr player, std::shared_ptr corpse) { +void Game::playerQuickLootCorpse(std::shared_ptr player, std::shared_ptr corpse) { if (!player || !corpse) { return; } @@ -4994,11 +4994,11 @@ void Game::playerQuickLoot(uint32_t playerId, const Position &pos, uint16_t item auto rewardId = corpse->getAttribute(ItemAttribute_t::DATE); auto reward = player->getReward(rewardId, false); if (reward) { - internalQuickLootCorpse(player, reward->getContainer()); + playerQuickLootCorpse(player, reward->getContainer()); } } else { if (!lootAllCorpses) { - internalQuickLootCorpse(player, corpse); + playerQuickLootCorpse(player, corpse); } else { playerLootAllCorpses(player, pos, lootAllCorpses); } @@ -5035,7 +5035,7 @@ void Game::playerLootAllCorpses(std::shared_ptr player, const Position & } corpses++; - internalQuickLootCorpse(player, tileCorpse); + playerQuickLootCorpse(player, tileCorpse); if (corpses >= 30) { break; } diff --git a/src/game/game.hpp b/src/game/game.hpp index bbf2e66088c..4a026ed0265 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -350,6 +350,7 @@ class Game { void playerSetFightModes(uint32_t playerId, FightMode_t fightMode, bool chaseMode, bool secureMode); void playerLookAt(uint32_t playerId, uint16_t itemId, const Position &pos, uint8_t stackPos); void playerLookInBattleList(uint32_t playerId, uint32_t creatureId); + void playerQuickLootCorpse(std::shared_ptr player, std::shared_ptr corpse); void playerQuickLoot(uint32_t playerId, const Position &pos, uint16_t itemId, uint8_t stackPos, std::shared_ptr defaultItem = nullptr, bool lootAllCorpses = false, bool autoLoot = false); void playerLootAllCorpses(std::shared_ptr player, const Position &pos, bool lootAllCorpses); void playerSetLootContainer(uint32_t playerId, ObjectCategory_t category, const Position &pos, uint16_t itemId, uint8_t stackPos); @@ -697,13 +698,6 @@ class Game { void playerSpeakToNpc(std::shared_ptr player, const std::string &text); std::shared_ptr createPlayerTask(uint32_t delay, std::function f, std::string context) const; - /** - * Player wants to loot a corpse - * \param player Player pointer - * \param corpse Container pointer to be looted - */ - void internalQuickLootCorpse(std::shared_ptr player, std::shared_ptr corpse); - /** * @brief Finds the container for loot based on the given parameters. *