Utilize set instead of vector in player observed list.

This commit is contained in:
wincent 2022-05-14 09:12:35 +02:00
parent e6227562b0
commit ea59cc10c0
2 changed files with 14 additions and 69 deletions

View File

@ -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)
@ -320,6 +264,8 @@ Player::~Player()
}
}
m_ObservedEntities.clear();
m_LimboConstructions.clear();
const auto& iter = std::find(m_Players.begin(), m_Players.end(), this);

View File

@ -2,6 +2,9 @@
#include "Entity.h"
#include <set>
#include <unordered_set>
/**
* Extended Entity for player data and behavior.
*
@ -120,13 +123,9 @@ private:
bool m_GhostOverride;
std::vector<int32_t> m_ObservedEntities;
std::unordered_set<int32_t> m_ObservedEntities;
int32_t m_ObservedEntitiesLength;
int32_t m_ObservedEntitiesUsed;
std::vector<LWOOBJID> m_LimboConstructions;
std::set<LWOOBJID> m_LimboConstructions;
std::map<LWOOBJID, Loot::Info> m_DroppedLoot;