From f5842062d3f9594d8f22f8938915194d20ebdf5f Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sat, 13 Apr 2013 21:28:55 +0000 Subject: Proper entity removal functions git-svn-id: http://mc-server.googlecode.com/svn/trunk@1386 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/ChunkMap.cpp | 48 ++++++++++++++---------------------------------- source/ChunkMap.h | 9 +++------ source/Player.cpp | 2 +- source/World.cpp | 20 ++++++++++---------- source/World.h | 7 +++---- 5 files changed, 31 insertions(+), 55 deletions(-) (limited to 'source') diff --git a/source/ChunkMap.cpp b/source/ChunkMap.cpp index 76835ee74..6be49e399 100644 --- a/source/ChunkMap.cpp +++ b/source/ChunkMap.cpp @@ -1370,42 +1370,39 @@ void cChunkMap::RemoveClientFromChunks(cClientHandle * a_Client) -// TODO: This function should not be needed, remove? -void cChunkMap::MoveEntityToChunk(cEntity * a_Entity, int a_ChunkX, int a_ChunkZ) +void cChunkMap::AddEntity(cEntity * a_Entity) { cCSLock Lock(m_CSLayers); - cChunkPtr OldChunk = GetChunkNoGen(a_Entity->GetChunkX(), ZERO_CHUNK_Y, a_Entity->GetChunkZ()); - if (OldChunk != NULL) - { - OldChunk->RemoveEntity(a_Entity); - } - cChunkPtr NewChunk = GetChunkNoGen(a_ChunkX, ZERO_CHUNK_Y, a_ChunkZ); - if (NewChunk != NULL) + cChunkPtr Chunk = GetChunkNoGen(a_Entity->GetChunkX(), ZERO_CHUNK_Y, a_Entity->GetChunkZ()); + if ((Chunk == NULL) && !Chunk->IsValid()) { - NewChunk->AddEntity(a_Entity); + return; } + Chunk->AddEntity(a_Entity); } -void cChunkMap::RemoveEntityFromChunk(cEntity * a_Entity, int a_ChunkX, int a_ChunkZ) +bool cChunkMap::HasEntity(int a_UniqueID) { cCSLock Lock(m_CSLayers); - cChunkPtr Chunk = GetChunkNoGen(a_ChunkX, ZERO_CHUNK_Y, a_ChunkZ); - if ((Chunk == NULL) && !Chunk->IsValid()) + for (cChunkLayerList::const_iterator itr = m_Layers.begin(); itr != m_Layers.end(); ++itr) { - return; + if ((*itr)->HasEntity(a_UniqueID)) + { + return true; + } } - Chunk->RemoveEntity(a_Entity); + return false; } -void cChunkMap::AddEntity(cEntity * a_Entity) +void cChunkMap::RemoveEntity(cEntity * a_Entity) { cCSLock Lock(m_CSLayers); cChunkPtr Chunk = GetChunkNoGen(a_Entity->GetChunkX(), ZERO_CHUNK_Y, a_Entity->GetChunkZ()); @@ -1413,24 +1410,7 @@ void cChunkMap::AddEntity(cEntity * a_Entity) { return; } - Chunk->AddEntity(a_Entity); -} - - - - - -bool cChunkMap::HasEntity(int a_UniqueID) -{ - cCSLock Lock(m_CSLayers); - for (cChunkLayerList::const_iterator itr = m_Layers.begin(); itr != m_Layers.end(); ++itr) - { - if ((*itr)->HasEntity(a_UniqueID)) - { - return true; - } - } - return false; + Chunk->RemoveEntity(a_Entity); } diff --git a/source/ChunkMap.h b/source/ChunkMap.h index d52b2f263..e3dfdc88f 100644 --- a/source/ChunkMap.h +++ b/source/ChunkMap.h @@ -183,18 +183,15 @@ public: /// Removes the client from all chunks it is present in void RemoveClientFromChunks(cClientHandle * a_Client); - /// Moves the entity from its current chunk to the new chunk specified - void MoveEntityToChunk(cEntity * a_Entity, int a_ChunkX, int a_ChunkZ); - - /// Removes the entity from the chunk specified - void RemoveEntityFromChunk(cEntity * a_Entity, int a_ChunkX, int a_ChunkZ); - /// Adds the entity to its appropriate chunk, takes ownership of the entity pointer void AddEntity(cEntity * a_Entity); /// Returns true if the entity with specified ID is present in the chunks bool HasEntity(int a_EntityID); + /// Removes the entity from its appropriate chunk + void RemoveEntity(cEntity * a_Entity); + /// Calls the callback for each entity in the entire world; returns true if all entities processed, false if the callback aborted by returning true bool ForEachEntity(cEntityCallback & a_Callback); // Lua-accessible diff --git a/source/Player.cpp b/source/Player.cpp index cfbf5cdff..2cb96ca9c 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -853,7 +853,7 @@ bool cPlayer::MoveToWorld( const char* a_WorldName ) /* Remove all links to the old world */ m_World->RemovePlayer( this ); m_ClientHandle->RemoveFromAllChunks(); - m_World->RemoveEntityFromChunk(this, GetChunkX(), GetChunkZ()); + m_World->RemoveEntity(this); /* Add player to all the necessary parts of the new world */ SetWorld( World ); diff --git a/source/World.cpp b/source/World.cpp index 6e321d63a..74a7ea399 100644 --- a/source/World.cpp +++ b/source/World.cpp @@ -1722,7 +1722,7 @@ void cWorld::AddPlayer(cPlayer * a_Player) void cWorld::RemovePlayer(cPlayer * a_Player) { - m_ChunkMap->RemoveEntityFromChunk(a_Player, a_Player->GetChunkX(), a_Player->GetChunkZ()); + m_ChunkMap->RemoveEntity(a_Player); cCSLock Lock(m_CSPlayers); m_Players.remove(a_Player); } @@ -1853,15 +1853,6 @@ void cWorld::SendPlayerList(cPlayer * a_DestPlayer) -void cWorld::RemoveEntityFromChunk(cEntity * a_Entity, int a_ChunkX, int a_ChunkZ) -{ - m_ChunkMap->RemoveEntityFromChunk(a_Entity, a_ChunkX, a_ChunkZ); -} - - - - - bool cWorld::ForEachEntity(cEntityCallback & a_Callback) { return m_ChunkMap->ForEachEntity(a_Callback); @@ -2084,6 +2075,15 @@ bool cWorld::HasEntity(int a_UniqueID) +void cWorld::RemoveEntity(cEntity * a_Entity) +{ + m_ChunkMap->RemoveEntity(a_Entity); +} + + + + + unsigned int cWorld::GetNumPlayers(void) { cCSLock Lock(m_CSPlayers); diff --git a/source/World.h b/source/World.h index 38506d1a0..70e203bc3 100644 --- a/source/World.h +++ b/source/World.h @@ -207,12 +207,13 @@ public: void SendPlayerList(cPlayer * a_DestPlayer); // Sends playerlist to the player + /// Adds the entity into its appropriate chunk; takes ownership of the entity ptr void AddEntity(cEntity * a_Entity); bool HasEntity(int a_UniqueID); - /// Removes the entity from the chunk specified - void RemoveEntityFromChunk(cEntity * a_Entity, int a_ChunkX, int a_ChunkZ); + /// Removes the entity, the entity ptr ownership is assumed taken by the caller + void RemoveEntity(cEntity * a_Entity); /// Calls the callback for each entity in the entire world; returns true if all entities processed, false if the callback aborted by returning true bool ForEachEntity(cEntityCallback & a_Callback); // Exported in ManualBindings.cpp @@ -558,8 +559,6 @@ private: void TickWeather(float a_Dt); // Handles weather each tick void TickSpawnMobs(float a_Dt); // Handles mob spawning each tick - void RemoveEntity( cEntity * a_Entity ); - /// Creates a new fluid simulator, loads its settings from the inifile (a_FluidName section) cFluidSimulator * InitializeFluidSimulator(cIniFile & a_IniFile, const char * a_FluidName, BLOCKTYPE a_SimulateBlock, BLOCKTYPE a_StationaryBlock); }; // tolua_export -- cgit v1.2.3