2012-01-29 11:28:19 -08:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
|
2011-12-25 04:55:20 -08:00
|
|
|
#include "cChunkGenerator.h"
|
|
|
|
#include "cWorld.h"
|
2012-02-13 13:47:03 -08:00
|
|
|
#include "cWorldGenerator.h"
|
|
|
|
#include "cWorldGenerator_Test.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-25 04:55:20 -08:00
|
|
|
|
|
|
|
|
|
|
|
typedef std::pair<int, int> ChunkCoord;
|
|
|
|
typedef std::list< ChunkCoord > ChunkCoordList;
|
|
|
|
|
2012-01-29 06:29:26 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// If the generation queue size exceeds this number, a warning will be output
|
2012-02-13 13:47:03 -08: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 02:01:42 -08:00
|
|
|
const int QUEUE_SKIP_LIMIT = 500;
|
2012-01-29 06:29:26 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-25 04:55:20 -08:00
|
|
|
|
2012-02-13 13:47:03 -08:00
|
|
|
cChunkGenerator::cChunkGenerator(void)
|
|
|
|
: super("cChunkGenerator")
|
|
|
|
, m_World(NULL)
|
|
|
|
, m_pWorldGenerator(NULL)
|
2011-12-25 04:55:20 -08:00
|
|
|
{
|
2012-02-13 13:47:03 -08:00
|
|
|
}
|
|
|
|
|
2012-01-29 06:29:26 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-13 13:47:03 -08:00
|
|
|
cChunkGenerator::~cChunkGenerator()
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
}
|
2012-01-29 06:29:26 -08:00
|
|
|
|
2011-12-25 04:55:20 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-13 13:47:03 -08:00
|
|
|
bool cChunkGenerator::Start(cWorld * a_World, const AString & a_WorldGeneratorName)
|
2011-12-25 04:55:20 -08:00
|
|
|
{
|
2012-02-13 13:47:03 -08:00
|
|
|
m_World = a_World;
|
|
|
|
|
|
|
|
if (a_WorldGeneratorName.compare("Test") == 0 )
|
|
|
|
{
|
2012-02-16 09:45:26 -08:00
|
|
|
m_pWorldGenerator = new cWorldGenerator_Test(a_World);
|
2012-02-13 13:47:03 -08:00
|
|
|
}
|
|
|
|
else // Default
|
|
|
|
{
|
2012-02-16 09:45:26 -08:00
|
|
|
m_pWorldGenerator = new cWorldGenerator(a_World);
|
2012-02-13 13:47:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return super::Start();
|
2011-12-25 04:55:20 -08:00
|
|
|
}
|
|
|
|
|
2012-01-29 06:29:26 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-13 13:47:03 -08:00
|
|
|
void cChunkGenerator::Stop(void)
|
2011-12-25 04:55:20 -08:00
|
|
|
{
|
2012-02-13 13:47:03 -08:00
|
|
|
mShouldTerminate = true;
|
|
|
|
m_Event.Set();
|
2012-02-18 09:53:22 -08:00
|
|
|
m_evtRemoved.Set(); // Wake up anybody waiting for empty queue
|
2012-02-13 13:47:03 -08:00
|
|
|
Wait();
|
|
|
|
|
|
|
|
delete m_pWorldGenerator;
|
|
|
|
m_pWorldGenerator = NULL;
|
2011-12-25 04:55:20 -08:00
|
|
|
}
|
|
|
|
|
2012-01-29 06:29:26 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-17 09:56:25 -08:00
|
|
|
void cChunkGenerator::GenerateChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
|
2011-12-25 04:55:20 -08:00
|
|
|
{
|
|
|
|
{
|
2012-02-28 08:59:59 -08: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 04:55:20 -08:00
|
|
|
{
|
2012-02-28 08:59:59 -08: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 04:55:20 -08:00
|
|
|
}
|
2012-02-28 08:59:59 -08:00
|
|
|
m_Queue.push_back(cChunkCoords(a_ChunkX, a_ChunkY, a_ChunkZ));
|
2011-12-25 04:55:20 -08:00
|
|
|
}
|
2012-01-29 06:29:26 -08:00
|
|
|
|
2012-02-13 13:47:03 -08:00
|
|
|
m_Event.Set();
|
2011-12-25 04:55:20 -08:00
|
|
|
}
|
|
|
|
|
2012-01-29 06:29:26 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-18 09:53:22 -08: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 13:47:03 -08:00
|
|
|
void cChunkGenerator::Execute(void)
|
2011-12-25 04:55:20 -08:00
|
|
|
{
|
2012-02-13 13:47:03 -08:00
|
|
|
while (!mShouldTerminate)
|
2011-12-25 04:55:20 -08:00
|
|
|
{
|
2012-02-13 13:47:03 -08:00
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
while (m_Queue.size() == 0)
|
2011-12-25 04:55:20 -08:00
|
|
|
{
|
2012-01-29 06:29:26 -08:00
|
|
|
cCSUnlock Unlock(Lock);
|
2012-02-13 13:47:03 -08:00
|
|
|
m_Event.Wait();
|
|
|
|
if (mShouldTerminate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-12-25 04:55:20 -08:00
|
|
|
}
|
2012-01-29 06:29:26 -08:00
|
|
|
|
2012-02-13 13:47:03 -08: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 06:29:26 -08:00
|
|
|
Lock.Unlock(); // Unlock ASAP
|
2012-02-18 09:53:22 -08:00
|
|
|
m_evtRemoved.Set();
|
2011-12-25 04:55:20 -08:00
|
|
|
|
2012-02-28 02:01:42 -08:00
|
|
|
if (m_World->IsChunkValid(coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ))
|
2012-02-13 13:47:03 -08:00
|
|
|
{
|
2012-02-28 02:01:42 -08: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 04:55:20 -08:00
|
|
|
continue;
|
|
|
|
}
|
2012-02-16 09:45:26 -08:00
|
|
|
|
2012-02-18 09:53:22 -08:00
|
|
|
LOGD("Generating chunk [%d, %d, %d]", coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ);
|
2012-02-17 09:56:25 -08:00
|
|
|
DoGenerate(coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ);
|
2012-02-13 13:47:03 -08:00
|
|
|
|
2012-02-16 09:45:26 -08:00
|
|
|
// Save the chunk right after generating, so that we don't have to generate it again on next run
|
2012-02-17 09:56:25 -08:00
|
|
|
m_World->GetStorage().QueueSaveChunk(coords.m_ChunkX, coords.m_ChunkY, coords.m_ChunkZ);
|
2012-01-29 06:29:26 -08:00
|
|
|
} // while (!bStop)
|
2011-12-25 04:55:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-29 06:29:26 -08:00
|
|
|
|
2012-02-17 09:56:25 -08:00
|
|
|
void cChunkGenerator::DoGenerate(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
|
|
|
|
{
|
2012-02-18 09:53:22 -08: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 11:18:16 -08:00
|
|
|
m_pWorldGenerator->PostGenerateChunk(a_ChunkX, a_ChunkY, a_ChunkZ);
|
2012-02-17 09:56:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|