2012-06-14 06:06:06 -07:00
2013-01-25 02:12:29 -08:00
// ChunkGenerator.h
2012-06-14 06:06:06 -07:00
// Interfaces to the cChunkGenerator class representing the thread that generates chunks
/*
The object takes requests for generating chunks and processes them in a separate thread one by one .
The requests are not added to the queue if there is already a request with the same coords
Before generating , the thread checks if the chunk hasn ' t been already generated .
It is theoretically possible to have multiple generator threads by having multiple instances of this object ,
but then it MAY happen that the chunk is generated twice .
If the generator queue is overloaded , the generator skips chunks with no clients in them
*/
# pragma once
2012-09-23 14:23:33 -07:00
# include "../OSSupport/IsThread.h"
2012-09-23 13:14:04 -07:00
# include "../ChunkDef.h"
2012-06-14 06:06:06 -07:00
// fwd:
class cWorld ;
class cIniFile ;
2013-01-25 02:12:29 -08:00
class cChunkDesc ;
2012-06-14 06:06:06 -07:00
class cChunkGenerator :
cIsThread
{
typedef cIsThread super ;
public :
2013-01-25 02:12:29 -08:00
/// The interface that a class has to implement to become a generator
class cGenerator
{
public :
cGenerator ( cChunkGenerator & a_ChunkGenerator ) ;
virtual ~ cGenerator ( ) { } ; // Force a virtual destructor
/// Called to initialize the generator on server startup.
virtual void Initialize ( cWorld * a_World , cIniFile & a_IniFile ) ;
/// Generates the biomes for the specified chunk (directly, not in a separate thread). Used by the world loader if biomes failed loading.
virtual void GenerateBiomes ( int a_ChunkX , int a_ChunkZ , cChunkDef : : BiomeMap & a_BiomeMap ) = 0 ;
/// Returns the biome at the specified coords. Used by ChunkMap if an invalid chunk is queried for biome. Default implementation uses GenerateBiomes().
virtual EMCSBiome GetBiomeAt ( int a_BlockX , int a_BlockZ ) ;
2013-02-08 08:01:44 -08:00
/// Called in a separate thread to do the actual chunk generation. Generator should generate into a_ChunkDesc.
virtual void DoGenerate ( int a_ChunkX , int a_ChunkZ , cChunkDesc & a_ChunkDesc ) = 0 ;
2013-01-25 02:12:29 -08:00
protected :
cChunkGenerator & m_ChunkGenerator ;
cWorld * m_World ;
} ;
2012-06-14 06:06:06 -07:00
cChunkGenerator ( void ) ;
~ cChunkGenerator ( ) ;
bool Start ( cWorld * a_World , cIniFile & a_IniFile ) ;
void Stop ( void ) ;
2013-01-25 02:12:29 -08:00
/// Queues the chunk for generation; removes duplicate requests
void QueueGenerateChunk ( int a_ChunkX , int a_ChunkY , int a_ChunkZ ) ;
2012-06-14 06:06:06 -07:00
2013-01-25 02:12:29 -08:00
/// Generates the biomes for the specified chunk (directly, not in a separate thread). Used by the world loader if biomes failed loading.
2012-06-14 06:06:06 -07:00
void GenerateBiomes ( int a_ChunkX , int a_ChunkZ , cChunkDef : : BiomeMap & a_BiomeMap ) ;
void WaitForQueueEmpty ( void ) ;
int GetQueueLength ( void ) ;
int GetSeed ( void ) const { return m_Seed ; }
2013-01-25 02:12:29 -08:00
/// Returns the biome at the specified coords. Used by ChunkMap if an invalid chunk is queried for biome
2012-06-14 06:06:06 -07:00
EMCSBiome GetBiomeAt ( int a_BlockX , int a_BlockZ ) ;
2013-01-25 02:12:29 -08:00
/// Reads a block type from the ini file; returns the blocktype on success, emits a warning and returns a_Default's representation on failure.
static BLOCKTYPE GetIniBlock ( cIniFile & a_IniFile , const AString & a_SectionName , const AString & a_ValueName , const AString & a_Default ) ;
2012-06-14 06:06:06 -07:00
private :
cWorld * m_World ;
int m_Seed ;
cCriticalSection m_CS ;
cChunkCoordsList m_Queue ;
2013-01-25 02:12:29 -08:00
cEvent m_Event ; ///< Set when an item is added to the queue or the thread should terminate
cEvent m_evtRemoved ; ///< Set when an item is removed from the queue
2012-06-14 06:06:06 -07:00
2013-01-25 02:12:29 -08:00
cGenerator * m_Generator ; ///< The actual generator engine used to generate chunks
2012-06-14 06:06:06 -07:00
// cIsThread override:
virtual void Execute ( void ) override ;
void DoGenerate ( int a_ChunkX , int a_ChunkY , int a_ChunkZ ) ;
} ;