From 26b1b4074df6407e7e281ccb1a390830821d4ffc Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Thu, 28 Jul 2022 01:15:51 -0300 Subject: [PATCH] Fixed random client debug (rework on checkAsCreatureKnown function) (#463) --- src/server/network/protocol/protocolgame.cpp | 69 +++++++++++--------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/src/server/network/protocol/protocolgame.cpp b/src/server/network/protocol/protocolgame.cpp index 8a61486f978..a96e6859164 100644 --- a/src/server/network/protocol/protocolgame.cpp +++ b/src/server/network/protocol/protocolgame.cpp @@ -947,46 +947,51 @@ void ProtocolGame::GetFloorDescription(NetworkMessage &msg, int32_t x, int32_t y void ProtocolGame::checkCreatureAsKnown(uint32_t id, bool &known, uint32_t &removedKnown) { - known = !knownCreatureSet.insert(id).second; - if (known) { - return; - } - - if (knownCreatureSet.size() < 1300) { - removedKnown = 0; + if (auto [creatureKnown, creatureInserted] = knownCreatureSet.insert(id); + !creatureInserted) + { + known = true; return; } - - // Look for a creature to remove - for (auto it = knownCreatureSet.begin(), end = knownCreatureSet.end(); it != end; ++it) { - if (*it == id) { - continue; - } - - Creature* creature = g_game().getCreatureByID(*it); - if (!creature || canSee(creature)) { - continue; - } - - const Player* checkPlayer = creature->getPlayer(); - if (!checkPlayer) { - removedKnown = *it; - knownCreatureSet.erase(it); - return; + known = false; + if (knownCreatureSet.size() > 1300) + { + // Look for a creature to remove + for (auto it = knownCreatureSet.begin(), end = knownCreatureSet.end(); it != end; ++it) { + if (*it == id) { + continue; + } + // We need to protect party players from removing + Creature* creature = g_game().getCreatureByID(*it); + if (const Player* checkPlayer; + creature && (checkPlayer = creature->getPlayer()) != nullptr) + { + if (player->getParty() != checkPlayer->getParty() && !canSee(creature)) { + removedKnown = *it; + knownCreatureSet.erase(it); + return; + } + } else if (!canSee(creature)) { + removedKnown = *it; + knownCreatureSet.erase(it); + return; + } } - // We need to protect party players from removing - if (checkPlayer && player->getParty() != checkPlayer->getParty()) { - removedKnown = *it; - knownCreatureSet.erase(it); - return; + // Bad situation. Let's just remove anyone. + auto it = knownCreatureSet.begin(); + if (*it == id) + { + ++it; } removedKnown = *it; + knownCreatureSet.erase(it); + } + else + { + removedKnown = 0; } - - /* Bad situation. Let's just remove the last valid one. */ - knownCreatureSet.erase(removedKnown); } bool ProtocolGame::canSee(const Creature *c) const