2011-12-25 07:55:20 -05:00
|
|
|
#include "MemoryLeak.h"
|
|
|
|
#include "cChunkGenerator.h"
|
|
|
|
#include "cChunkMap.h"
|
|
|
|
#include "cChunk.h"
|
|
|
|
#include "cWorld.h"
|
|
|
|
|
|
|
|
#include "cThread.h"
|
|
|
|
#include "cCriticalSection.h"
|
|
|
|
#include "cSemaphore.h"
|
|
|
|
|
|
|
|
#include "cMCLogger.h"
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
typedef std::pair<int, int> ChunkCoord;
|
|
|
|
typedef std::list< ChunkCoord > ChunkCoordList;
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// If the generation queue size exceeds this number, a warning will be output
|
|
|
|
#define QUEUE_WARNING_LIMIT 1000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-25 07:55:20 -05:00
|
|
|
|
|
|
|
struct cChunkGenerator::sChunkGeneratorState
|
|
|
|
{
|
2012-01-29 09:29:26 -05:00
|
|
|
cCriticalSection m_CriticalSection; // For protecting the variables in this struct
|
|
|
|
|
|
|
|
ChunkCoordList GenerateQueue;
|
|
|
|
ChunkCoord CurrentlyGeneratingCoords;
|
|
|
|
cChunk* pCurrentlyGenerating;
|
|
|
|
bool bCurrentlyGenerating;
|
|
|
|
|
|
|
|
cSemaphore m_Semaphore;
|
|
|
|
cThread * pThread;
|
|
|
|
|
|
|
|
bool bStop;
|
|
|
|
|
|
|
|
sChunkGeneratorState(void)
|
|
|
|
: m_Semaphore(1, 0)
|
2011-12-25 07:55:20 -05:00
|
|
|
, pThread( 0 )
|
|
|
|
, bStop( false )
|
|
|
|
, bCurrentlyGenerating( false )
|
|
|
|
, pCurrentlyGenerating( false )
|
|
|
|
{}
|
2012-01-29 09:29:26 -05:00
|
|
|
};
|
|
|
|
|
2011-12-25 07:55:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cChunkGenerator::cChunkGenerator( cChunkMap* a_pChunkMap )
|
|
|
|
: m_pState( new sChunkGeneratorState )
|
|
|
|
, m_pChunkMap( a_pChunkMap )
|
|
|
|
{
|
|
|
|
m_pState->pThread = new cThread( GenerateThread, this, "cChunkGenerator::GenerateThread" );
|
|
|
|
m_pState->pThread->Start( true );
|
|
|
|
}
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-25 07:55:20 -05:00
|
|
|
cChunkGenerator::~cChunkGenerator()
|
|
|
|
{
|
|
|
|
m_pState->bStop = true;
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
m_pState->m_Semaphore.Signal(); // Signal so thread can continue and exit
|
2011-12-25 07:55:20 -05:00
|
|
|
delete m_pState->pThread;
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
delete m_pState;
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-25 07:55:20 -05:00
|
|
|
void cChunkGenerator::GenerateChunk( int a_X, int a_Z )
|
|
|
|
{
|
2012-01-29 09:29:26 -05:00
|
|
|
cCSLock Lock(&m_pState->m_CriticalSection);
|
2011-12-25 07:55:20 -05:00
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
if (m_pState->bCurrentlyGenerating)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-01-29 09:29:26 -05:00
|
|
|
if ((m_pState->CurrentlyGeneratingCoords.first == a_X) && (m_pState->CurrentlyGeneratingCoords.second == a_Z))
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
|
|
|
return; // Already generating this chunk, so ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pState->GenerateQueue.remove( ChunkCoord(a_X, a_Z) );
|
2012-01-29 09:29:26 -05:00
|
|
|
if (m_pState->GenerateQueue.size() >= QUEUE_WARNING_LIMIT)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-01-29 09:29:26 -05:00
|
|
|
LOGWARN("WARNING: Adding chunk (%i, %i) to generation queue; Queue is too big! (%i)", a_X, a_Z, m_pState->GenerateQueue.size() );
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
|
|
|
m_pState->GenerateQueue.push_back( ChunkCoord(a_X, a_Z) );
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
Lock.Unlock();
|
|
|
|
|
|
|
|
m_pState->m_Semaphore.Signal();
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-25 07:55:20 -05:00
|
|
|
void cChunkGenerator::GenerateThread( void* a_Params )
|
|
|
|
{
|
|
|
|
// Cache some values for easy access (they are all references/pointers)
|
2012-01-29 09:29:26 -05:00
|
|
|
cChunkGenerator * self = (cChunkGenerator*)a_Params;
|
|
|
|
sChunkGeneratorState * m_pState = self->m_pState;
|
|
|
|
ChunkCoordList & GenerateQueue = m_pState->GenerateQueue;
|
|
|
|
cChunkMap & ChunkMap = *self->m_pChunkMap;
|
|
|
|
cCriticalSection * CriticalSection = &m_pState->m_CriticalSection;
|
|
|
|
cSemaphore & Semaphore = m_pState->m_Semaphore;
|
|
|
|
|
|
|
|
while (!m_pState->bStop)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-01-29 09:29:26 -05:00
|
|
|
cCSLock Lock(CriticalSection);
|
|
|
|
if (GenerateQueue.size() == 0)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-01-29 09:29:26 -05:00
|
|
|
cCSUnlock Unlock(Lock);
|
|
|
|
Semaphore.Wait();
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
2012-01-29 09:29:26 -05:00
|
|
|
if (m_pState->bStop) break;
|
|
|
|
|
2011-12-25 07:55:20 -05:00
|
|
|
ChunkCoord coord = *GenerateQueue.begin(); // Get next coord from queue
|
|
|
|
GenerateQueue.erase( GenerateQueue.begin() ); // Remove coordinate from queue
|
|
|
|
m_pState->bCurrentlyGenerating = true;
|
|
|
|
m_pState->CurrentlyGeneratingCoords = coord;
|
2012-01-29 09:29:26 -05:00
|
|
|
Lock.Unlock(); // Unlock ASAP
|
2011-12-25 07:55:20 -05:00
|
|
|
|
|
|
|
ChunkMap.GetWorld()->LockChunks();
|
|
|
|
if( ChunkMap.GetChunk( coord.first, 0, coord.second ) ) // Make sure it has not been loaded in the meantime. Don't want to generate the same chunk twice
|
|
|
|
{ // This is possible when forcing the server to generate a chunk in the main thread
|
|
|
|
ChunkMap.GetWorld()->UnlockChunks();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ChunkMap.GetWorld()->UnlockChunks();
|
|
|
|
|
|
|
|
LOGINFO("cChunkGenerator generating chunk %i %i", coord.first, coord.second );
|
|
|
|
cChunk* Chunk = new cChunk( coord.first, 0, coord.second, ChunkMap.GetWorld() );
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
Lock.Lock();
|
2011-12-25 07:55:20 -05:00
|
|
|
m_pState->pCurrentlyGenerating = Chunk;
|
2012-01-29 09:29:26 -05:00
|
|
|
Lock.Unlock();
|
2011-12-25 07:55:20 -05:00
|
|
|
|
|
|
|
Chunk->Initialize(); // Generate the chunk
|
|
|
|
|
|
|
|
ChunkMap.GetWorld()->LockChunks();
|
|
|
|
ChunkMap.AddChunk( Chunk );
|
|
|
|
ChunkMap.GetWorld()->UnlockChunks();
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
Lock.Lock();
|
2011-12-25 07:55:20 -05:00
|
|
|
m_pState->bCurrentlyGenerating = false;
|
|
|
|
m_pState->pCurrentlyGenerating = 0;
|
2012-01-29 09:29:26 -05:00
|
|
|
Lock.Unlock();
|
|
|
|
} // while (!bStop)
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|