From ea71bfa9b645cda80b7d4364c675ebaee8db8353 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 8 Feb 2014 21:55:21 +0100 Subject: Initial ChunkStay code. --- src/ChunkStay.h | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/ChunkStay.h (limited to 'src/ChunkStay.h') diff --git a/src/ChunkStay.h b/src/ChunkStay.h new file mode 100644 index 000000000..6eb8e1669 --- /dev/null +++ b/src/ChunkStay.h @@ -0,0 +1,92 @@ + +// ChunkStay.h + +/* Declares the cChunkStay class representing a base for classes that want to wait for certain chunks to load, +then do some action on them. While the object is enabled, the chunks contained within are locked and will +not unload +*/ + + + + + +#pragma once + + + + + +// fwd +class cChunkMap; + + + + + +/** Makes chunks stay loaded until this object is cleared or destroyed +Works by setting internal flags in the cChunk that it should not be unloaded. +To optimize for speed, cChunkStay has an Enabled flag, it will "stay" the chunks only when enabled +and it will refuse chunk-list manipulations when enabled. +The object itself is not made thread-safe, it's supposed to be used from a single thread only. +This class is abstract, the descendants are expected to provide the OnChunkAvailable() and +the OnAllChunksAvailable() callback implementations. Note that those are called from the contexts of +different threads' - the caller, the Loader or the Generator thread. +*/ +class cChunkStay +{ +public: + cChunkStay(void); + ~cChunkStay(); + + void Clear(void); + + /** Adds a chunk to be locked from unloading. + To be used only while the ChunkStay object is not enabled. */ + void Add (int a_ChunkX, int a_ChunkZ); + + /** Releases the chunk so that it's no longer locked from unloading. + To be used only while the ChunkStay object is not enabled. */ + void Remove(int a_ChunkX, int a_ChunkZ); + + /** Enables the ChunkStay on the specified chunkmap, causing it to load and generate chunks. + All the contained chunks are queued for loading / generating. */ + void Enable (cChunkMap & a_ChunkMap); + + /** Disables the ChunkStay, the chunks are released and the ChunkStay + object can be edited with Add() and Remove() again*/ + void Disable(void); + + /** Returns all the chunks that should be kept */ + const cChunkCoordsVector & GetChunks(void) const { return m_Chunks; } + + /** Called when a specific chunk become available. */ + virtual void OnChunkAvailable(int a_ChunkX, int a_ChunkZ) = 0; + + /** Caled once all of the contained chunks are available. */ + virtual void OnAllChunksAvailable(void) = 0; + +protected: + + friend class cChunkMap; + + + /** The chunkmap where the object is enabled. + Valid only after call to Enable() and before Disable(). */ + cChunkMap * m_ChunkMap; + + /** The list of chunks to lock from unloading. */ + cChunkCoordsVector m_Chunks; + + /** The chunks that still need loading */ + cChunkCoordsVector m_OutstandingChunks; + + + /** Called by cChunkMap when a chunk is available, checks m_NumLoaded and triggers the appropriate callbacks. + May be called for chunks outside this ChunkStay. */ + void ChunkAvailable(int a_ChunkX, int a_ChunkZ); +} ; + + + + + -- cgit v1.2.3 From 9455f59b1100f3db2c5b1e88bb9d1f6a01664721 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 9 Feb 2014 18:56:16 +0100 Subject: Initial Lua cChunkStay export. --- src/ChunkStay.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/ChunkStay.h') diff --git a/src/ChunkStay.h b/src/ChunkStay.h index 6eb8e1669..a6274812e 100644 --- a/src/ChunkStay.h +++ b/src/ChunkStay.h @@ -32,12 +32,16 @@ This class is abstract, the descendants are expected to provide the OnChunkAvail the OnAllChunksAvailable() callback implementations. Note that those are called from the contexts of different threads' - the caller, the Loader or the Generator thread. */ +// tolua_begin class cChunkStay { public: + // tolua_end cChunkStay(void); ~cChunkStay(); + // tolua_begin + void Clear(void); /** Adds a chunk to be locked from unloading. @@ -48,14 +52,20 @@ public: To be used only while the ChunkStay object is not enabled. */ void Remove(int a_ChunkX, int a_ChunkZ); + // tolua_end + /** Enables the ChunkStay on the specified chunkmap, causing it to load and generate chunks. All the contained chunks are queued for loading / generating. */ void Enable (cChunkMap & a_ChunkMap); + // tolua_begin + /** Disables the ChunkStay, the chunks are released and the ChunkStay object can be edited with Add() and Remove() again*/ void Disable(void); + // tolua_end + /** Returns all the chunks that should be kept */ const cChunkCoordsVector & GetChunks(void) const { return m_Chunks; } @@ -84,7 +94,7 @@ protected: /** Called by cChunkMap when a chunk is available, checks m_NumLoaded and triggers the appropriate callbacks. May be called for chunks outside this ChunkStay. */ void ChunkAvailable(int a_ChunkX, int a_ChunkZ); -} ; +} ; // tolua_export -- cgit v1.2.3 From 47a497fa895fa5f353ba593d4bb232ea514e66c9 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 9 Feb 2014 20:39:45 +0100 Subject: First working version of cLuaChunkStay. It works, but has random failures, probably due to threading issues. --- src/ChunkStay.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ChunkStay.h') diff --git a/src/ChunkStay.h b/src/ChunkStay.h index a6274812e..1e9cd69e8 100644 --- a/src/ChunkStay.h +++ b/src/ChunkStay.h @@ -62,7 +62,7 @@ public: /** Disables the ChunkStay, the chunks are released and the ChunkStay object can be edited with Add() and Remove() again*/ - void Disable(void); + virtual void Disable(void); // tolua_end -- cgit v1.2.3 From 9cebc9157cf43ba639227b9d79b980b3613dda1e Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Mon, 10 Feb 2014 22:47:10 +0100 Subject: Rewritten Lua ChunkStay API into a single function, cWorld:ChunkStay(). This fixes problems with indeterminate class object lifespan (Lua-GC) and forgetting to disable it or keep it until ready. --- src/ChunkStay.h | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'src/ChunkStay.h') diff --git a/src/ChunkStay.h b/src/ChunkStay.h index 1e9cd69e8..80b084aee 100644 --- a/src/ChunkStay.h +++ b/src/ChunkStay.h @@ -32,48 +32,42 @@ This class is abstract, the descendants are expected to provide the OnChunkAvail the OnAllChunksAvailable() callback implementations. Note that those are called from the contexts of different threads' - the caller, the Loader or the Generator thread. */ -// tolua_begin class cChunkStay { public: - // tolua_end cChunkStay(void); ~cChunkStay(); - // tolua_begin - void Clear(void); /** Adds a chunk to be locked from unloading. To be used only while the ChunkStay object is not enabled. */ - void Add (int a_ChunkX, int a_ChunkZ); + void Add(int a_ChunkX, int a_ChunkZ); /** Releases the chunk so that it's no longer locked from unloading. To be used only while the ChunkStay object is not enabled. */ void Remove(int a_ChunkX, int a_ChunkZ); - // tolua_end - /** Enables the ChunkStay on the specified chunkmap, causing it to load and generate chunks. All the contained chunks are queued for loading / generating. */ void Enable (cChunkMap & a_ChunkMap); - // tolua_begin - /** Disables the ChunkStay, the chunks are released and the ChunkStay object can be edited with Add() and Remove() again*/ virtual void Disable(void); - // tolua_end - /** Returns all the chunks that should be kept */ const cChunkCoordsVector & GetChunks(void) const { return m_Chunks; } /** Called when a specific chunk become available. */ virtual void OnChunkAvailable(int a_ChunkX, int a_ChunkZ) = 0; - /** Caled once all of the contained chunks are available. */ - virtual void OnAllChunksAvailable(void) = 0; + /** Caled once all of the contained chunks are available. + If returns true, the ChunkStay is automatically disabled by the ChunkMap; if it returns false, the ChunkStay is kept. */ + virtual bool OnAllChunksAvailable(void) = 0; + + /** Called by the ChunkMap when the ChunkStay is disabled. The object may choose to delete itself. */ + virtual void OnDisabled(void) = 0; protected: @@ -92,9 +86,10 @@ protected: /** Called by cChunkMap when a chunk is available, checks m_NumLoaded and triggers the appropriate callbacks. - May be called for chunks outside this ChunkStay. */ - void ChunkAvailable(int a_ChunkX, int a_ChunkZ); -} ; // tolua_export + May be called for chunks outside this ChunkStay. + Returns true if the ChunkStay is to be automatically disabled by the ChunkMap; returns false to keep the ChunkStay. */ + bool ChunkAvailable(int a_ChunkX, int a_ChunkZ); +} ; -- cgit v1.2.3