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"
|
2012-03-12 15:39:41 -04:00
|
|
|
#include "WGFlat.h"
|
2011-12-25 07:55:20 -05:00
|
|
|
|
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
|
2012-02-28 05:01:42 -05:00
|
|
|
const int QUEUE_SKIP_LIMIT = 500;
|
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;
|
|
|
|
|
2012-03-12 15:39:41 -04:00
|
|
|
if (NoCaseCompare(a_WorldGeneratorName, "Test") == 0 )
|
2012-02-13 16:47:03 -05:00
|
|
|
{
|
2012-02-16 12:45:26 -05:00
|
|
|
m_pWorldGenerator = new cWorldGenerator_Test(a_World);
|
2012-02-13 16:47:03 -05:00
|
|
|
}
|
2012-03-12 15:39:41 -04:00
|
|
|
else if (NoCaseCompare(a_WorldGeneratorName, "flat") == 0)
|
|
|
|
{
|
|
|
|
m_pWorldGenerator = new cWGFlat(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-03-10 12:37:00 -05:00
|
|
|
m_ShouldTerminate = true;
|
2012-02-13 16:47:03 -05:00
|
|
|
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-28 11:59:59 -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)
|
|
|
|
{
|
|
|
|
if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkY == a_ChunkY) && (itr->m_ChunkZ == a_ChunkZ))
|
|
|
|
{
|
|
|
|
// Already in the queue, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} // 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-28 11:59:59 -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-28 11:59:59 -05:00
|
|
|
m_Queue.push_back(cChunkCoords(a_ChunkX, a_ChunkY, a_ChunkZ));
|
2011-12-25 07:55:20 -05:00
|
|
|
}
|
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);
|
2012-03-10 12:37:00 -05:00
|
|
|
while (!m_ShouldTerminate && !m_Queue.empty())
|
2012-02-18 12:53:22 -05:00
|
|
|
{
|
|
|
|
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-03-10 12:37:00 -05:00
|
|
|
while (!m_ShouldTerminate)
|
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();
|
2012-03-10 12:37:00 -05:00
|
|
|
if (m_ShouldTerminate)
|
2012-02-13 16:47:03 -05:00
|
|
|
{
|
|
|
|
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-28 05:01:42 -05:00
|
|
|
if (m_World->IsChunkValid(coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ))
|
2012-02-13 16:47:03 -05:00
|
|
|
{
|
2012-02-28 05:01:42 -05:00
|
|
|
// Already generated, ignore request
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SkipEnabled && !m_World->HasChunkAnyClients(coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ))
|
|
|
|
{
|
|
|
|
LOGWARNING("Chunk generator overloaded, skipping chunk [%d, %d, %d] (HasClients: %d)", coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ);
|
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-03-14 16:56:09 -04:00
|
|
|
char BlockData[cChunkDef::BlockDataSize];
|
2012-02-18 12:53:22 -05:00
|
|
|
cEntityList Entities;
|
|
|
|
cBlockEntityList BlockEntities;
|
|
|
|
m_pWorldGenerator->GenerateChunk(a_ChunkX, a_ChunkY, a_ChunkZ, BlockData, Entities, BlockEntities);
|
2012-03-14 16:56:09 -04:00
|
|
|
|
|
|
|
m_World->ChunkDataGenerated(
|
|
|
|
a_ChunkX, a_ChunkY, a_ChunkZ,
|
|
|
|
BlockData,
|
|
|
|
BlockData + cChunkDef::MetaOffset,
|
|
|
|
BlockData + cChunkDef::LightOffset,
|
|
|
|
BlockData + cChunkDef::SkyLightOffset,
|
|
|
|
NULL,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|