From bdc3387ce384524060b2318c02f7fc75eafdf540 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Sat, 2 Mar 2024 17:10:47 -0300 Subject: [PATCH] fix: hireling say only inside a house and other adjusts (#2360) Resolves #2357 Set some logs to trace Change from "ipairs" to "pairs" from achievements. --- data/libs/functions/revscriptsys.lua | 2 +- data/scripts/lib/register_achievements.lua | 4 ++-- src/creatures/combat/combat.cpp | 2 +- src/creatures/players/player.cpp | 10 ++++++++++ src/creatures/players/player.hpp | 2 ++ src/game/game.cpp | 16 ++++++++++++++++ src/lua/functions/core/libs/logger_functions.cpp | 11 +++++++++++ src/lua/functions/core/libs/logger_functions.hpp | 1 + 8 files changed, 44 insertions(+), 4 deletions(-) diff --git a/data/libs/functions/revscriptsys.lua b/data/libs/functions/revscriptsys.lua index 5a31ec086f3..84b8275e3ce 100644 --- a/data/libs/functions/revscriptsys.lua +++ b/data/libs/functions/revscriptsys.lua @@ -108,7 +108,7 @@ do local function EventCallbackNewIndex(self, key, value) local func = eventCallbacks[key] if func and type(func) == "function" then - logger.debug("[Registering EventCallback: {}", key) + logger.trace("[Registering EventCallback: {}", key) func(self, value) self:type(key) else diff --git a/data/scripts/lib/register_achievements.lua b/data/scripts/lib/register_achievements.lua index b906afd287a..11d238d8bcf 100644 --- a/data/scripts/lib/register_achievements.lua +++ b/data/scripts/lib/register_achievements.lua @@ -551,7 +551,7 @@ Functions: ACHIEVEMENT_FIRST = 1 ACHIEVEMENT_LAST = #ACHIEVEMENTS -for id, achievTable in ipairs(ACHIEVEMENTS) do +for id, achievTable in pairs(ACHIEVEMENTS) do if achievTable.name == nil then logger.error(string.format("[Achievements registration] - Invalid achievement with no name, id: '%s'", id)) goto continue -- Skips to the next iteration using the 'continue' label @@ -567,7 +567,7 @@ for id, achievTable in ipairs(ACHIEVEMENTS) do local grade = achievTable.grade or 0 local points = achievTable.points or 0 - logger.debug("[Achievements registration] - Registering achievement '{}' with id '{}'", achievTable.name, id) + logger.trace("[Achievements registration] - Registering achievement '{}' with id '{}'", achievTable.name, id) Game.registerAchievement(id, achievTable.name, achievTable.description, secret, grade, points) ::continue:: -- Label used by 'goto' to continue the loop diff --git a/src/creatures/combat/combat.cpp b/src/creatures/combat/combat.cpp index 5b5ba5f037b..62c9e1638a8 100644 --- a/src/creatures/combat/combat.cpp +++ b/src/creatures/combat/combat.cpp @@ -524,7 +524,7 @@ bool Combat::setCallback(CallBackParam_t key) { void Combat::setChainCallback(uint8_t chainTargets, uint8_t chainDistance, bool backtracking) { params.chainCallback = std::make_unique(chainTargets, chainDistance, backtracking); - g_logger().debug("ChainCallback created: {}, with targets: {}, distance: {}, backtracking: {}", params.chainCallback != nullptr, chainTargets, chainDistance, backtracking); + g_logger().trace("ChainCallback created: {}, with targets: {}, distance: {}, backtracking: {}", params.chainCallback != nullptr, chainTargets, chainDistance, backtracking); } CallBack* Combat::getCallback(CallBackParam_t key) { diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index 28986292029..6dd304e90ba 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -8074,3 +8074,13 @@ void Player::checkAndShowBlessingMessage() { sendTextMessage(MESSAGE_EVENT_ADVANCE, blessOutput.str()); } } + +bool Player::canSpeakWithHireling(uint8_t speechbubble) { + const auto &playerTile = getTile(); + const auto &house = playerTile ? playerTile->getHouse() : nullptr; + if (speechbubble == SPEECHBUBBLE_HIRELING && (!house || house->getHouseAccessLevel(static_self_cast()) == HOUSE_NOT_INVITED)) { + return false; + } + + return true; +} diff --git a/src/creatures/players/player.hpp b/src/creatures/players/player.hpp index d37214911f3..592db179071 100644 --- a/src/creatures/players/player.hpp +++ b/src/creatures/players/player.hpp @@ -2595,6 +2595,8 @@ class Player final : public Creature, public Cylinder, public Bankable { std::shared_ptr getStoreInbox() const; + bool canSpeakWithHireling(uint8_t speechbubble); + private: friend class PlayerLock; std::mutex mutex; diff --git a/src/game/game.cpp b/src/game/game.cpp index c17f16b1a17..26e8cb84281 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -5908,6 +5908,10 @@ void Game::playerSpeakToNpc(std::shared_ptr player, const std::string &t } for (const auto &spectator : Spectators().find(player->getPosition()).filter()) { + if (!player->canSpeakWithHireling(spectator->getNpc()->getSpeechBubble())) { + continue; + } + spectator->getNpc()->onCreatureSay(player, TALKTYPE_PRIVATE_PN, text); } @@ -8452,6 +8456,16 @@ void Game::playerNpcGreet(uint32_t playerId, uint32_t npcId) { return; } + // Check npc say exhausted + if (player->isUIExhausted()) { + player->sendCancelMessage(RETURNVALUE_YOUAREEXHAUSTED); + return; + } + + if (!player->canSpeakWithHireling(npc->getSpeechBubble())) { + return; + } + auto spectators = Spectators().find(player->getPosition(), true); spectators.insert(npc); internalCreatureSay(player, TALKTYPE_SAY, "hi", false, &spectators); @@ -8463,6 +8477,8 @@ void Game::playerNpcGreet(uint32_t playerId, uint32_t npcId) { } else { internalCreatureSay(player, TALKTYPE_PRIVATE_PN, "sail", false, &npcsSpectators); } + + player->updateUIExhausted(); } void Game::playerLeaveMarket(uint32_t playerId) { diff --git a/src/lua/functions/core/libs/logger_functions.cpp b/src/lua/functions/core/libs/logger_functions.cpp index dc039a9ed0d..1b2605f6421 100644 --- a/src/lua/functions/core/libs/logger_functions.cpp +++ b/src/lua/functions/core/libs/logger_functions.cpp @@ -24,6 +24,7 @@ void LoggerFunctions::init(lua_State* L) { registerMethod(L, "logger", "warn", LoggerFunctions::luaLoggerWarn); registerMethod(L, "logger", "error", LoggerFunctions::luaLoggerError); registerMethod(L, "logger", "debug", LoggerFunctions::luaLoggerDebug); + registerMethod(L, "logger", "trace", LoggerFunctions::luaLoggerTrace); } int LoggerFunctions::luaSpdlogInfo(lua_State* L) { @@ -107,3 +108,13 @@ int LoggerFunctions::luaLoggerDebug(lua_State* L) { } return 1; } + +int LoggerFunctions::luaLoggerTrace(lua_State* L) { + // logger.trace(text) + if (isString(L, 1)) { + g_logger().trace(getFormatedLoggerMessage(L)); + } else { + 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 5e83f1461c9..2c4dba46cc1 100644 --- a/src/lua/functions/core/libs/logger_functions.hpp +++ b/src/lua/functions/core/libs/logger_functions.hpp @@ -25,4 +25,5 @@ class LoggerFunctions final : public LuaScriptInterface { static int luaLoggerError(lua_State* L); static int luaLoggerInfo(lua_State* L); static int luaLoggerWarn(lua_State* L); + static int luaLoggerTrace(lua_State* L); };