From ea59cc10c0725d983d698f49e1621c011f0bf2a1 Mon Sep 17 00:00:00 2001 From: wincent Date: Sat, 14 May 2022 09:12:35 +0200 Subject: [PATCH] Utilize `set` instead of vector in player observed list. --- dGame/Player.cpp | 72 ++++++------------------------------------------ dGame/Player.h | 11 ++++---- 2 files changed, 14 insertions(+), 69 deletions(-) diff --git a/dGame/Player.cpp b/dGame/Player.cpp index 634d4a68..518e4a74 100644 --- a/dGame/Player.cpp +++ b/dGame/Player.cpp @@ -29,9 +29,7 @@ Player::Player(const LWOOBJID& objectID, const EntityInfo info, User* user, Enti m_GhostReferencePoint = NiPoint3::ZERO; m_GhostOverridePoint = NiPoint3::ZERO; m_GhostOverride = false; - m_ObservedEntitiesLength = 256; - m_ObservedEntitiesUsed = 0; - m_ObservedEntities.resize(m_ObservedEntitiesLength); + m_ObservedEntities.reserve(256); m_Character->SetEntity(this); @@ -122,26 +120,12 @@ void Player::SendToZone(LWOMAPID zoneId, LWOCLONEID cloneId) void Player::AddLimboConstruction(LWOOBJID objectId) { - const auto& iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId); - - if (iter != m_LimboConstructions.end()) - { - return; - } - - m_LimboConstructions.push_back(objectId); + m_LimboConstructions.insert(objectId); } void Player::RemoveLimboConstruction(LWOOBJID objectId) { - const auto& iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId); - - if (iter == m_LimboConstructions.end()) - { - return; - } - - m_LimboConstructions.erase(iter); + m_LimboConstructions.erase(objectId); } void Player::ConstructLimboEntities() @@ -203,50 +187,17 @@ bool Player::GetGhostOverride() const void Player::ObserveEntity(int32_t id) { - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) - { - if (m_ObservedEntities[i] == 0 || m_ObservedEntities[i] == id) - { - m_ObservedEntities[i] = id; - - return; - } - } - - const auto index = m_ObservedEntitiesUsed++; - - if (m_ObservedEntitiesUsed > m_ObservedEntitiesLength) - { - m_ObservedEntities.resize(m_ObservedEntitiesLength + m_ObservedEntitiesLength); - - m_ObservedEntitiesLength = m_ObservedEntitiesLength + m_ObservedEntitiesLength; - } - - m_ObservedEntities[index] = id; + m_ObservedEntities.insert(id); } bool Player::IsObserved(int32_t id) { - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) - { - if (m_ObservedEntities[i] == id) - { - return true; - } - } - - return false; + return m_ObservedEntities.find(id) != m_ObservedEntities.end(); } void Player::GhostEntity(int32_t id) { - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) - { - if (m_ObservedEntities[i] == id) - { - m_ObservedEntities[i] = 0; - } - } + m_ObservedEntities.erase(id); } Player* Player::GetPlayer(const SystemAddress& sysAddr) @@ -303,15 +254,8 @@ Player::~Player() { Game::logger->Log("Player", "Deleted player\n"); - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) + for (const auto id : m_ObservedEntities) { - const auto id = m_ObservedEntities[i]; - - if (id == 0) - { - continue; - } - auto* entity = EntityManager::Instance()->GetGhostCandidate(id); if (entity != nullptr) @@ -319,6 +263,8 @@ Player::~Player() entity->SetObservers(entity->GetObservers() - 1); } } + + m_ObservedEntities.clear(); m_LimboConstructions.clear(); diff --git a/dGame/Player.h b/dGame/Player.h index bba01363..1210323f 100644 --- a/dGame/Player.h +++ b/dGame/Player.h @@ -2,6 +2,9 @@ #include "Entity.h" +#include +#include + /** * Extended Entity for player data and behavior. * @@ -120,13 +123,9 @@ private: bool m_GhostOverride; - std::vector m_ObservedEntities; - - int32_t m_ObservedEntitiesLength; + std::unordered_set m_ObservedEntities; - int32_t m_ObservedEntitiesUsed; - - std::vector m_LimboConstructions; + std::set m_LimboConstructions; std::map m_DroppedLoot;