Refactor UpdateEntities to not lose items if we add them while processing (#664)

* if we are deleting entities, and we add an entity
to delete, dont throw it out

* made it all uniform

* change update back to how it was
since it's an unordere map and
wouldn't be guaranteed to update even in this secnario
This commit is contained in:
Aaron Kimbrell 2022-07-21 21:09:25 -05:00 committed by GitHub
parent 40a9aefb5b
commit 5523b6aafc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -162,99 +162,70 @@ void EntityManager::DestroyEntity(Entity* entity) {
void EntityManager::UpdateEntities(const float deltaTime) { void EntityManager::UpdateEntities(const float deltaTime) {
for (const auto& e : m_Entities) { for (const auto& e : m_Entities) {
e.second->Update(deltaTime); e.second->Update(deltaTime);
} }
for (const auto entityId : m_EntitiesToSerialize) for (auto entry = m_EntitiesToSerialize.begin(); entry != m_EntitiesToSerialize.end(); entry++) {
{ auto* entity = GetEntity(*entry);
auto* entity = GetEntity(entityId);
if (entity == nullptr) continue; if (entity == nullptr) continue;
m_SerializationCounter++; m_SerializationCounter++;
RakNet::BitStream stream; RakNet::BitStream stream;
stream.Write(static_cast<char>(ID_REPLICA_MANAGER_SERIALIZE)); stream.Write(static_cast<char>(ID_REPLICA_MANAGER_SERIALIZE));
stream.Write(static_cast<unsigned short>(entity->GetNetworkId())); stream.Write(static_cast<unsigned short>(entity->GetNetworkId()));
entity->WriteBaseReplicaData(&stream, PACKET_TYPE_SERIALIZATION); entity->WriteBaseReplicaData(&stream, PACKET_TYPE_SERIALIZATION);
entity->WriteComponents(&stream, PACKET_TYPE_SERIALIZATION); entity->WriteComponents(&stream, PACKET_TYPE_SERIALIZATION);
if (entity->GetIsGhostingCandidate()) if (entity->GetIsGhostingCandidate()) {
{ for (auto* player : Player::GetAllPlayers()) {
for (auto* player : Player::GetAllPlayers()) if (player->IsObserved(*entry)) {
{
if (player->IsObserved(entityId))
{
Game::server->Send(&stream, player->GetSystemAddress(), false); Game::server->Send(&stream, player->GetSystemAddress(), false);
} }
} }
} } else {
else
{
Game::server->Send(&stream, UNASSIGNED_SYSTEM_ADDRESS, true); Game::server->Send(&stream, UNASSIGNED_SYSTEM_ADDRESS, true);
} }
} }
m_EntitiesToSerialize.clear(); m_EntitiesToSerialize.clear();
for (const auto& entry : m_EntitiesToKill) for (auto entry = m_EntitiesToKill.begin(); entry != m_EntitiesToKill.end(); entry++) {
{ auto* entity = GetEntity(*entry);
auto* entity = GetEntity(entry);
if (!entity) continue; if (!entity) continue;
if (entity->GetScheduledKiller()) if (entity->GetScheduledKiller()) {
{
entity->Smash(entity->GetScheduledKiller()->GetObjectID(), SILENT); entity->Smash(entity->GetScheduledKiller()->GetObjectID(), SILENT);
} } else {
else
{
entity->Smash(LWOOBJID_EMPTY, SILENT); entity->Smash(LWOOBJID_EMPTY, SILENT);
} }
} }
m_EntitiesToKill.clear(); m_EntitiesToKill.clear();
for (const auto entry : m_EntitiesToDelete) for (auto entry = m_EntitiesToDelete.begin(); entry != m_EntitiesToDelete.end(); entry++) {
{
// Get all this info first before we delete the player. // Get all this info first before we delete the player.
auto entityToDelete = GetEntity(entry); auto entityToDelete = GetEntity(*entry);
auto networkIdToErase = entityToDelete->GetNetworkId(); auto networkIdToErase = entityToDelete->GetNetworkId();
const auto& ghostingToDelete = std::find(m_EntitiesToGhost.begin(), m_EntitiesToGhost.end(), entityToDelete); const auto& ghostingToDelete = std::find(m_EntitiesToGhost.begin(), m_EntitiesToGhost.end(), entityToDelete);
if (entityToDelete) if (entityToDelete) {
{
// If we are a player run through the player destructor. // If we are a player run through the player destructor.
if (entityToDelete->IsPlayer()) if (entityToDelete->IsPlayer()) {
{
delete dynamic_cast<Player*>(entityToDelete); delete dynamic_cast<Player*>(entityToDelete);
} } else {
else
{
delete entityToDelete; delete entityToDelete;
} }
entityToDelete = nullptr; entityToDelete = nullptr;
if (networkIdToErase != 0) m_LostNetworkIds.push(networkIdToErase);
if (networkIdToErase != 0)
{
m_LostNetworkIds.push(networkIdToErase);
}
} }
if (ghostingToDelete != m_EntitiesToGhost.end()) if (ghostingToDelete != m_EntitiesToGhost.end()) m_EntitiesToGhost.erase(ghostingToDelete);
{
m_EntitiesToGhost.erase(ghostingToDelete);
}
m_Entities.erase(entry);
m_Entities.erase(*entry);
} }
m_EntitiesToDelete.clear(); m_EntitiesToDelete.clear();
} }