From 054a89dd9e5d6819adede9d7ba781b69f98ff2f4 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Wed, 6 Jan 2021 00:35:42 +0000 Subject: Clarify cClientHandle, cPlayer ownership semantics + A cPlayer, once created, has a strong pointer to the cClientHandle. The player ticks the clienthandle. If he finds the handle destroyed, he destroys himself in turn. Nothing else can kill the player. * The client handle has a pointer to the player. Once a player is created, the client handle never outlasts the player, nor does it manage the player's lifetime. The pointer is always safe to use after FinishAuthenticate, which is also the point where cProtocol is put into the Game state that allows player manipulation. + Entities are once again never lost by constructing a chunk when they try to move into one that doesn't exist. * Fixed a forgotten Super invocation in cPlayer::OnRemoveFromWorld. * Fix SaveToDisk usage in destructor by only saving things cPlayer owns, instead of accessing cWorld. --- src/ChunkMap.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'src/ChunkMap.cpp') diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index c5798260b..c1d9b78ab 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -940,19 +940,18 @@ void cChunkMap::RemoveClientFromChunks(cClientHandle * a_Client) void cChunkMap::AddEntity(OwnedEntity a_Entity) { cCSLock Lock(m_CSChunks); - const auto Chunk = FindChunk(a_Entity->GetChunkX(), a_Entity->GetChunkZ()); - if (Chunk == nullptr) + if (FindChunk(a_Entity->GetChunkX(), a_Entity->GetChunkZ()) == nullptr) { - LOGWARNING("Entity at %p (%s, ID %d) spawning in a non-existent chunk, the entity is lost.", - static_cast(a_Entity.get()), a_Entity->GetClass(), a_Entity->GetUniqueID() + LOGWARNING("%s: Entity at %p (%s, ID %d) spawning in a non-existent chunk.", + __FUNCTION__, static_cast(a_Entity.get()), a_Entity->GetClass(), a_Entity->GetUniqueID() ); - return; } const auto EntityPtr = a_Entity.get(); ASSERT(EntityPtr->GetWorld() == m_World); - Chunk->AddEntity(std::move(a_Entity)); + auto & Chunk = ConstructChunk(a_Entity->GetChunkX(), a_Entity->GetChunkZ()); + Chunk.AddEntity(std::move(a_Entity)); EntityPtr->OnAddToWorld(*m_World); ASSERT(!EntityPtr->IsTicking()); @@ -1738,11 +1737,7 @@ void cChunkMap::Tick(std::chrono::milliseconds a_Dt) cCSLock Lock(m_CSChunks); for (auto & Chunk : m_Chunks) { - // Only tick chunks that are valid and should be ticked: - if (Chunk.second.IsValid() && Chunk.second.ShouldBeTicked()) - { - Chunk.second.Tick(a_Dt); - } + Chunk.second.Tick(a_Dt); } } -- cgit v1.2.3