2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
|
2011-12-25 07:55:20 -05:00
|
|
|
#include "cChunkGenerator.h"
|
|
|
|
#include "cWorld.h"
|
2012-02-13 16:47:03 -05:00
|
|
|
#include "cWorldGenerator.h"
|
|
|
|
#include "cWorldGenerator_Test.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-25 07:55:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2012-02-13 16:47:03 -05:00
|
|
|
const int QUEUE_WARNING_LIMIT = 1000;
|
|
|
|
|
|
|
|
/// If the generation queue size exceeds this number, chunks with no clients will be skipped
|
|
|
|
const int QUEUE_SKIP_LIMIT = 50;
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-25 07:55:20 -05:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
cChunkGenerator::cChunkGenerator(void)
|
|
|
|
: super("cChunkGenerator")
|
|
|
|
, m_World(NULL)
|
|
|
|
, m_pWorldGenerator(NULL)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
}
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
cChunkGenerator::~cChunkGenerator()
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
}
|
2012-01-29 09:29:26 -05:00
|
|
|
|
2011-12-25 07:55:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
bool cChunkGenerator::Start(cWorld * a_World, const AString & a_WorldGeneratorName)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
m_World = a_World;
|
|
|
|
|
|
|
|
if (a_WorldGeneratorName.compare("Test") == 0 )
|
|
|
|
{
|
2012-02-16 12:45:26 -05:00
|
|
|
m_pWorldGenerator = new cWorldGenerator_Test(a_World);
|
2012-02-13 16:47:03 -05:00
|
|
|
}
|
|
|
|
else // Default
|
|
|
|
{
|
2012-02-16 12:45:26 -05:00
|
|
|
m_pWorldGenerator = new cWorldGenerator(a_World);
|
2012-02-13 16:47:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return super::Start();
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
void cChunkGenerator::Stop(void)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
mShouldTerminate = true;
|
|
|
|
m_Event.Set();
|
2012-02-18 12:53:22 -05:00
|
|
|
m_evtRemoved.Set(); // Wake up anybody waiting for empty queue
|
2012-02-13 16:47:03 -05:00
|
|
|
Wait();
|
|
|
|
|
|
|
|
delete m_pWorldGenerator;
|
|
|
|
m_pWorldGenerator = NULL;
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-17 12:56:25 -05:00
|
|
|
void cChunkGenerator::GenerateChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
|
|
|
|
// Check if it is already in the queue:
|
|
|
|
for (cChunkCoordsList::iterator itr = m_Queue.begin(); itr != m_Queue.end(); ++itr)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-02-17 12:56:25 -05:00
|
|
|
if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkY == a_ChunkY) && (itr->m_ChunkZ == a_ChunkZ))
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
// Already in the queue, bail out
|
|
|
|
return;
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
2012-02-13 16:47:03 -05:00
|
|
|
} // for itr - m_Queue[]
|
|
|
|
|
|
|
|
// Add to queue, issue a warning if too many:
|
|
|
|
if (m_Queue.size() >= QUEUE_WARNING_LIMIT)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
LOGWARN("WARNING: Adding chunk [%i, %i] to generation queue; Queue is too big! (%i)", a_ChunkX, a_ChunkZ, m_Queue.size());
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
2012-02-17 12:56:25 -05:00
|
|
|
m_Queue.push_back(cChunkCoords(a_ChunkX, a_ChunkY, a_ChunkZ));
|
2012-01-29 09:29:26 -05:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
m_Event.Set();
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-18 12:53:22 -05:00
|
|
|
void cChunkGenerator::WaitForQueueEmpty(void)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
while (!mShouldTerminate && !m_Queue.empty())
|
|
|
|
{
|
|
|
|
cCSUnlock Unlock(Lock);
|
|
|
|
m_evtRemoved.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cChunkGenerator::GetQueueLength(void)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
return (int)m_Queue.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
void cChunkGenerator::Execute(void)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
while (!mShouldTerminate)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
while (m_Queue.size() == 0)
|
2011-12-25 07:55:20 -05:00
|
|
|
{
|
2012-01-29 09:29:26 -05:00
|
|
|
cCSUnlock Unlock(Lock);
|
2012-02-13 16:47:03 -05:00
|
|
|
m_Event.Wait();
|
|
|
|
if (mShouldTerminate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
2012-01-29 09:29:26 -05:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
cChunkCoords coords = m_Queue.front(); // Get next coord from queue
|
|
|
|
m_Queue.erase( m_Queue.begin() ); // Remove coordinate from queue
|
|
|
|
bool SkipEnabled = (m_Queue.size() > QUEUE_SKIP_LIMIT);
|
2012-01-29 09:29:26 -05:00
|
|
|
Lock.Unlock(); // Unlock ASAP
|
2012-02-18 12:53:22 -05:00
|
|
|
m_evtRemoved.Set();
|
2011-12-25 07:55:20 -05:00
|
|
|
|
2012-02-16 12:45:26 -05:00
|
|
|
if (
|
2012-02-17 12:56:25 -05:00
|
|
|
m_World->IsChunkValid(coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ) ||
|
|
|
|
(SkipEnabled && m_World->HasChunkAnyClients(coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ))
|
2012-02-16 12:45:26 -05:00
|
|
|
)
|
2012-02-13 16:47:03 -05:00
|
|
|
{
|
|
|
|
// Already generated / overload-skip, ignore request
|
2011-12-25 07:55:20 -05:00
|
|
|
continue;
|
|
|
|
}
|
2012-02-16 12:45:26 -05:00
|
|
|
|
2012-02-18 12:53:22 -05:00
|
|
|
LOGD("Generating chunk [%d, %d, %d]", coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ);
|
2012-02-17 12:56:25 -05:00
|
|
|
DoGenerate(coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ);
|
2012-02-13 16:47:03 -05:00
|
|
|
|
2012-02-16 12:45:26 -05:00
|
|
|
// Save the chunk right after generating, so that we don't have to generate it again on next run
|
2012-02-17 12:56:25 -05:00
|
|
|
m_World->GetStorage().QueueSaveChunk(coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ);
|
2012-01-29 09:29:26 -05:00
|
|
|
} // while (!bStop)
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-29 09:29:26 -05:00
|
|
|
|
2012-02-17 12:56:25 -05:00
|
|
|
void cChunkGenerator::DoGenerate(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
|
|
|
|
{
|
2012-02-18 12:53:22 -05:00
|
|
|
char BlockData[cChunk::c_BlockDataSize];
|
|
|
|
cEntityList Entities;
|
|
|
|
cBlockEntityList BlockEntities;
|
|
|
|
m_pWorldGenerator->GenerateChunk(a_ChunkX, a_ChunkY, a_ChunkZ, BlockData, Entities, BlockEntities);
|
|
|
|
m_World->ChunkDataGenerated(a_ChunkX, a_ChunkY, a_ChunkZ, BlockData, Entities, BlockEntities);
|
2012-02-18 14:18:16 -05:00
|
|
|
m_pWorldGenerator->PostGenerateChunk(a_ChunkX, a_ChunkY, a_ChunkZ);
|
2012-02-17 12:56:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|