1
0

Add cWorld::RemoveEntity and use in cEntity (#4003)

* Add cWorld::RemoveEntity and use in cEntity

* cEntity: Remove uneeded asserts from Destroy and DoMoveToWorld
This commit is contained in:
peterbell10
2017-09-05 15:11:35 +01:00
committed by Alexander Harkness
parent 028a35ef0d
commit ef1f371dab
3 changed files with 44 additions and 11 deletions

View File

@@ -223,8 +223,6 @@ cChunk * cEntity::GetParentChunk()
void cEntity::Destroy(bool a_ShouldBroadcast)
{
ASSERT(IsTicking());
ASSERT(GetParentChunk() != nullptr);
SetIsTicking(false);
// Unleash leashed mobs
@@ -238,17 +236,17 @@ void cEntity::Destroy(bool a_ShouldBroadcast)
m_World->BroadcastDestroyEntity(*this);
}
cChunk * ParentChunk = GetParentChunk();
m_World->QueueTask([this, ParentChunk](cWorld & a_World)
auto ParentChunkCoords = cChunkDef::BlockToChunk(GetPosition());
m_World->QueueTask([this, ParentChunkCoords](cWorld & a_World)
{
LOGD("Destroying entity #%i (%s) from chunk (%d, %d)",
this->GetUniqueID(), this->GetClass(),
ParentChunk->GetPosX(), ParentChunk->GetPosZ()
ParentChunkCoords.m_ChunkX, ParentChunkCoords.m_ChunkZ
);
// Make sure that RemoveEntity returned a valid smart pointer
// Also, not storing the returned pointer means automatic destruction
VERIFY(ParentChunk->RemoveEntity(*this));
VERIFY(a_World.RemoveEntity(*this));
});
Destroyed();
}
@@ -1565,7 +1563,6 @@ bool cEntity::DoMoveToWorld(cWorld * a_World, bool a_ShouldSendRespawn, Vector3d
{
UNUSED(a_ShouldSendRespawn);
ASSERT(a_World != nullptr);
ASSERT(IsTicking());
if (GetWorld() == a_World)
{
@@ -1586,6 +1583,9 @@ bool cEntity::DoMoveToWorld(cWorld * a_World, bool a_ShouldSendRespawn, Vector3d
// Tell others we are gone
GetWorld()->BroadcastDestroyEntity(*this);
// Take note of old chunk coords
auto OldChunkCoords = cChunkDef::BlockToChunk(GetPosition());
// Set position to the new position
SetPosition(a_NewPosition);
@@ -1600,16 +1600,15 @@ bool cEntity::DoMoveToWorld(cWorld * a_World, bool a_ShouldSendRespawn, Vector3d
// Queue add to new world and removal from the old one
cWorld * OldWorld = GetWorld();
cChunk * ParentChunk = GetParentChunk();
SetWorld(a_World); // Chunks may be streamed before cWorld::AddPlayer() sets the world to the new value
OldWorld->QueueTask([this, ParentChunk, a_World](cWorld & a_OldWorld)
OldWorld->QueueTask([this, OldChunkCoords, a_World](cWorld & a_OldWorld)
{
LOGD("Warping entity #%i (%s) from world \"%s\" to \"%s\". Source chunk: (%d, %d) ",
this->GetUniqueID(), this->GetClass(),
a_OldWorld.GetName().c_str(), a_World->GetName().c_str(),
ParentChunk->GetPosX(), ParentChunk->GetPosZ()
OldChunkCoords.m_ChunkX, OldChunkCoords.m_ChunkZ
);
a_World->AddEntity(ParentChunk->RemoveEntity(*this));
a_World->AddEntity(a_OldWorld.RemoveEntity(*this));
cRoot::Get()->GetPluginManager()->CallHookEntityChangedWorld(*this, a_OldWorld);
});
return true;