From 8ea87de83364ad55ad3bf4576ead8a944f0a1799 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Mon, 25 Jul 2022 22:39:17 -0300 Subject: [PATCH 1/4] Add exhaustion to npc say and sell/buy item --- src/creatures/players/player.cpp | 11 +++++++++++ src/creatures/players/player.h | 6 +++++- src/game/game.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index 2f4942e20f7..bab676b4224 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -5613,6 +5613,17 @@ bool Player::isMarketExhausted() const { return (OTSYS_TIME() - lastMarketInteraction < exhaust_time); } +// Player talk with npc exhausted +bool Player::isNpcExhausted() const { + // One second = 1000 + uint32_t exhaustionTime = 500; + return (OTSYS_TIME() - lastNpcInteraction < exhaustionTime); +} + +void Player::updateNpcExhausted() { + lastNpcInteraction = OTSYS_TIME(); +} + uint64_t Player::getItemCustomPrice(uint16_t itemId, bool buyPrice/* = false*/) const { auto it = itemPriceMap.find(itemId); diff --git a/src/creatures/players/player.h b/src/creatures/players/player.h index a806c446830..ec207677cc7 100644 --- a/src/creatures/players/player.h +++ b/src/creatures/players/player.h @@ -1692,6 +1692,9 @@ class Player final : public Creature, public Cylinder lastMarketInteraction = OTSYS_TIME(); } + bool isNpcExhausted() const; + void updateNpcExhausted(); + bool isQuickLootListedItem(const Item* item) const { if (!item) { return false; @@ -2211,7 +2214,8 @@ class Player final : public Creature, public Cylinder int64_t skullTicks = 0; int64_t lastWalkthroughAttempt = 0; int64_t lastToggleMount = 0; - int64_t lastMarketInteraction = 0; // Market exhaust. + int64_t lastMarketInteraction = 0; + int64_t lastNpcInteraction = 0; int64_t lastStashInteraction = 0; int64_t lastDepotSearchInteraction = 0; int64_t lastPing; diff --git a/src/game/game.cpp b/src/game/game.cpp index dd1e73a7945..ad77128d6ae 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -4359,7 +4359,14 @@ void Game::playerBuyItem(uint32_t playerId, uint16_t itemId, uint8_t count, uint return; } + // Check npc say exhausted + if (player->isNpcExhausted()) { + player->sendCancelMessage(RETURNVALUE_YOUAREEXHAUSTED); + return; + } + merchant->onPlayerBuyItem(player, it.id, count, amount, ignoreCap, inBackpacks); + player->updateNpcExhausted(); } void Game::playerSellItem(uint32_t playerId, uint16_t itemId, uint8_t count, uint8_t amount, bool ignoreEquipped) @@ -4383,7 +4390,14 @@ void Game::playerSellItem(uint32_t playerId, uint16_t itemId, uint8_t count, uin return; } + // Check npc say exhausted + if (player->isNpcExhausted()) { + player->sendCancelMessage(RETURNVALUE_YOUAREEXHAUSTED); + return; + } + merchant->onPlayerSellItem(player, it.id, count, amount, ignoreEquipped); + player->updateNpcExhausted(); } void Game::playerCloseShop(uint32_t playerId) @@ -5309,6 +5323,16 @@ bool Game::playerSpeakTo(Player* player, SpeakClasses type, const std::string& r void Game::playerSpeakToNpc(Player* player, const std::string& text) { + if (player == nullptr) { + SPDLOG_ERROR("[Game::playerSpeakToNpc] - Player is nullptr"); + } + + // Check npc say exhausted + if (player->isNpcExhausted()) { + player->sendCancelMessage(RETURNVALUE_YOUAREEXHAUSTED); + return; + } + SpectatorHashSet spectators; map.getSpectators(spectators, player->getPosition()); for (Creature* spectator : spectators) { @@ -5316,6 +5340,8 @@ void Game::playerSpeakToNpc(Player* player, const std::string& text) spectator->onCreatureSay(player, TALKTYPE_PRIVATE_PN, text); } } + + player->updateNpcExhausted(); } //-- From b13db4ce58fc748a6a8be5d16e0b04312064d4aa Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Mon, 25 Jul 2022 22:49:20 -0300 Subject: [PATCH 2/4] Add return --- src/game/game.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/game/game.cpp b/src/game/game.cpp index ad77128d6ae..5b965c5a826 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -5325,6 +5325,7 @@ void Game::playerSpeakToNpc(Player* player, const std::string& text) { if (player == nullptr) { SPDLOG_ERROR("[Game::playerSpeakToNpc] - Player is nullptr"); + return; } // Check npc say exhausted From f43f1c2e5437f6d542ccad0b6435caaa533b9127 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Wed, 27 Jul 2022 13:56:38 -0300 Subject: [PATCH 3/4] Add default exhaustion to 150 ms --- src/creatures/players/player.cpp | 4 +--- src/creatures/players/player.h | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index bab676b4224..6c2545af9bd 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -5614,9 +5614,7 @@ bool Player::isMarketExhausted() const { } // Player talk with npc exhausted -bool Player::isNpcExhausted() const { - // One second = 1000 - uint32_t exhaustionTime = 500; +bool Player::isNpcExhausted(uint32_t exhaustionTime) const { return (OTSYS_TIME() - lastNpcInteraction < exhaustionTime); } diff --git a/src/creatures/players/player.h b/src/creatures/players/player.h index ec207677cc7..1b542caef8f 100644 --- a/src/creatures/players/player.h +++ b/src/creatures/players/player.h @@ -1692,7 +1692,7 @@ class Player final : public Creature, public Cylinder lastMarketInteraction = OTSYS_TIME(); } - bool isNpcExhausted() const; + bool isNpcExhausted(uint32_t exhaustionTime = 150) const; void updateNpcExhausted(); bool isQuickLootListedItem(const Item* item) const { From 4ab3de2bcd86b40840cdf3c6ccc19a9171fc8904 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Wed, 27 Jul 2022 13:57:09 -0300 Subject: [PATCH 4/4] Add comment --- src/creatures/players/player.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index 6c2545af9bd..c968c57ccdf 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -5614,7 +5614,7 @@ bool Player::isMarketExhausted() const { } // Player talk with npc exhausted -bool Player::isNpcExhausted(uint32_t exhaustionTime) const { +bool Player::isNpcExhausted(uint32_t exhaustionTime /*= 250*/) const { return (OTSYS_TIME() - lastNpcInteraction < exhaustionTime); }