Skip to content

Commit

Permalink
fix: hireling say only inside a house and other adjusts (#2360)
Browse files Browse the repository at this point in the history
Resolves #2357

Set some logs to trace
Change from "ipairs" to "pairs" from achievements.
  • Loading branch information
dudantas authored Mar 2, 2024
1 parent 34fe193 commit bdc3387
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion data/libs/functions/revscriptsys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions data/scripts/lib/register_achievements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChainCallback>(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) {
Expand Down
10 changes: 10 additions & 0 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Player>()) == HOUSE_NOT_INVITED)) {
return false;
}

return true;
}
2 changes: 2 additions & 0 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2595,6 +2595,8 @@ class Player final : public Creature, public Cylinder, public Bankable {

std::shared_ptr<Container> getStoreInbox() const;

bool canSpeakWithHireling(uint8_t speechbubble);

private:
friend class PlayerLock;
std::mutex mutex;
Expand Down
16 changes: 16 additions & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5908,6 +5908,10 @@ void Game::playerSpeakToNpc(std::shared_ptr<Player> player, const std::string &t
}

for (const auto &spectator : Spectators().find<Creature>(player->getPosition()).filter<Npc>()) {
if (!player->canSpeakWithHireling(spectator->getNpc()->getSpeechBubble())) {
continue;
}

spectator->getNpc()->onCreatureSay(player, TALKTYPE_PRIVATE_PN, text);
}

Expand Down Expand Up @@ -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>(player->getPosition(), true);
spectators.insert(npc);
internalCreatureSay(player, TALKTYPE_SAY, "hi", false, &spectators);
Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/lua/functions/core/libs/logger_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/lua/functions/core/libs/logger_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit bdc3387

Please sign in to comment.