diff options
author | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2016-08-20 14:34:29 +0200 |
---|---|---|
committer | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2016-08-20 14:34:29 +0200 |
commit | 7175b9e435a54cff33914d21384625a445cc5cf0 (patch) | |
tree | 7feb44f1e8e4ed7ea4d5bf0ba1d173abd3417f34 /src/Chunk.h | |
parent | Added cWorld:SetSpawn() API and Lua binding (#3316) (diff) | |
download | cuberite-7175b9e435a54cff33914d21384625a445cc5cf0.tar cuberite-7175b9e435a54cff33914d21384625a445cc5cf0.tar.gz cuberite-7175b9e435a54cff33914d21384625a445cc5cf0.tar.bz2 cuberite-7175b9e435a54cff33914d21384625a445cc5cf0.tar.lz cuberite-7175b9e435a54cff33914d21384625a445cc5cf0.tar.xz cuberite-7175b9e435a54cff33914d21384625a445cc5cf0.tar.zst cuberite-7175b9e435a54cff33914d21384625a445cc5cf0.zip |
Diffstat (limited to 'src/Chunk.h')
-rw-r--r-- | src/Chunk.h | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/src/Chunk.h b/src/Chunk.h index 925680fdd..a75054d30 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -88,7 +88,7 @@ public: cChunk * a_NeighborXM, cChunk * a_NeighborXP, cChunk * a_NeighborZM, cChunk * a_NeighborZP, // Neighbor chunks cAllocationPool<cChunkData::sChunkSection> & a_Pool ); - cChunk(cChunk & other); + ~cChunk(); /** Returns true iff the chunk block data is valid (loaded / generated) */ @@ -244,22 +244,21 @@ public: int GetHeight( int a_X, int a_Z); - void SendBlockTo(int a_RelX, int a_RelY, int a_RelZ, cClientHandle * a_Client); + void SendBlockTo(int a_RelX, int a_RelY, int a_RelZ, const std::weak_ptr<cClientHandle> & a_Client); /** Adds a client to the chunk; returns true if added, false if already there */ - bool AddClient(cClientHandle * a_Client); + bool AddClient(const std::shared_ptr<cClientHandle> & a_Client); /** Removes the specified client from the chunk; ignored if client not in chunk. */ - void RemoveClient(cClientHandle * a_Client); - - /** Returns true if the specified client is present in this chunk. */ - bool HasClient(cClientHandle * a_Client); + void RemoveClient(const std::shared_ptr<cClientHandle> & a_Client); /** Returns true if theres any client in the chunk; false otherwise */ bool HasAnyClients(void) const; - void AddEntity(cEntity * a_Entity); - void RemoveEntity(cEntity * a_Entity); + void AddEntity(std::unique_ptr<cEntity> a_Entity); + + void RemoveEntity(cEntity & a_Entity); + bool HasEntity(UInt32 a_EntityID); /** Calls the callback for each entity; returns true if all entities processed, false if the callback aborted by returning true */ @@ -477,10 +476,43 @@ public: as at least one requests is active the chunk will be ticked). */ void SetAlwaysTicked(bool a_AlwaysTicked); - // Makes a copy of the list - cClientHandleList GetAllClients(void) const + /** Collects and returns a list of all clients in the world who currently have this chunk. */ + std::vector<std::shared_ptr<cClientHandle>> GetAllStrongClientPtrs(void); + + /** Returns the internal data structure holding weak pointers to all clients in this chunk. */ + auto & GetAllWeakClientPtrs(void) { - return cClientHandleList(m_LoadedByClient.begin(), m_LoadedByClient.end()); + return m_LoadedByClient; + } + + std::unique_ptr<cEntity> AcquireAssociatedEntityPtr(const cEntity & a_Entity) + { + auto Iterator = std::find_if( + m_Entities.begin(), + m_Entities.end(), + [&a_Entity](const decltype(m_Entities)::value_type & a_Value) + { + return (a_Value.get() == &a_Entity); + } + ); + + if (Iterator == m_Entities.cend()) + { + return nullptr; + } + else + { + auto Entity = std::move(*Iterator); + Entity->SetParentChunk(nullptr); + + if (!Entity->IsPlayer()) + { + MarkDirty(); + } + + m_Entities.erase(Iterator); + return Entity; + } } private: @@ -517,9 +549,8 @@ private: std::vector<Vector3i> m_ToTickBlocks; sSetBlockVector m_PendingSendBlocks; ///< Blocks that have changed and need to be sent to all clients - // A critical section is not needed, because all chunk access is protected by its parent ChunkMap's csLayers - std::vector<cClientHandle *> m_LoadedByClient; - cEntityList m_Entities; + std::vector<std::weak_ptr<cClientHandle>> m_LoadedByClient; + std::vector<std::unique_ptr<cEntity>> m_Entities; cBlockEntityList m_BlockEntities; /** Number of times the chunk has been requested to stay (by various cChunkStay objects); if zero, the chunk can be unloaded */ @@ -593,7 +624,7 @@ private: bool GrowMelonPumpkin(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, MTRand & a_Random); /** Called by Tick() when an entity moves out of this chunk into a neighbor; moves the entity and sends spawn / despawn packet to clients */ - void MoveEntityToNewChunk(cEntity * a_Entity); + void MoveEntityToNewChunk(std::unique_ptr<cEntity> a_Entity); }; typedef cChunk * cChunkPtr; |