2015-05-29 19:18:52 -04:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
|
|
|
|
#include "SpawnPrepare.h"
|
|
|
|
#include "World.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-30 06:11:17 -04:00
|
|
|
class cSpawnPrepareCallback :
|
|
|
|
public cChunkCoordCallback
|
|
|
|
{
|
|
|
|
public:
|
2017-08-22 08:34:43 -04:00
|
|
|
cSpawnPrepareCallback(std::shared_ptr<cSpawnPrepare> a_SpawnPrepare) :
|
|
|
|
m_SpawnPrepare(std::move(a_SpawnPrepare))
|
2015-05-30 06:11:17 -04:00
|
|
|
{
|
2017-08-22 08:34:43 -04:00
|
|
|
ASSERT(m_SpawnPrepare != nullptr);
|
2015-05-30 06:11:17 -04:00
|
|
|
}
|
|
|
|
protected:
|
|
|
|
|
2017-08-22 08:34:43 -04:00
|
|
|
std::shared_ptr<cSpawnPrepare> m_SpawnPrepare;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2019-09-01 03:30:00 -04:00
|
|
|
virtual void Call(cChunkCoords a_Coords, bool a_IsSuccess) override
|
2015-05-30 06:11:17 -04:00
|
|
|
{
|
2019-09-01 03:30:00 -04:00
|
|
|
m_SpawnPrepare->PreparedChunkCallback(a_Coords.m_ChunkX, a_Coords.m_ChunkZ);
|
2015-05-30 06:11:17 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-29 19:18:52 -04:00
|
|
|
|
|
|
|
|
2017-08-22 08:34:43 -04:00
|
|
|
cSpawnPrepare::cSpawnPrepare(cWorld & a_World, int a_SpawnChunkX, int a_SpawnChunkZ, int a_PrepareDistance, int a_FirstIdx, sMakeSharedTag):
|
2015-05-29 19:18:52 -04:00
|
|
|
m_World(a_World),
|
|
|
|
m_SpawnChunkX(a_SpawnChunkX),
|
|
|
|
m_SpawnChunkZ(a_SpawnChunkZ),
|
|
|
|
m_PrepareDistance(a_PrepareDistance),
|
|
|
|
m_NextIdx(a_FirstIdx),
|
|
|
|
m_MaxIdx(a_PrepareDistance * a_PrepareDistance),
|
|
|
|
m_NumPrepared(0),
|
|
|
|
m_LastReportTime(std::chrono::steady_clock::now()),
|
|
|
|
m_LastReportChunkCount(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cSpawnPrepare::PrepareChunks(cWorld & a_World, int a_SpawnChunkX, int a_SpawnChunkZ, int a_PrepareDistance)
|
|
|
|
{
|
|
|
|
|
|
|
|
// Queue the initial chunks:
|
|
|
|
int MaxIdx = a_PrepareDistance * a_PrepareDistance;
|
|
|
|
int maxQueue = std::min(MaxIdx - 1, 100); // Number of chunks to queue at once
|
2017-08-22 08:34:43 -04:00
|
|
|
auto prep = std::make_shared<cSpawnPrepare>(a_World, a_SpawnChunkX, a_SpawnChunkZ, a_PrepareDistance, maxQueue, sMakeSharedTag{});
|
2015-05-29 19:18:52 -04:00
|
|
|
for (int i = 0; i < maxQueue; i++)
|
|
|
|
{
|
|
|
|
int chunkX, chunkZ;
|
2017-08-22 08:34:43 -04:00
|
|
|
prep->DecodeChunkCoords(i, chunkX, chunkZ);
|
2020-08-01 14:18:03 -04:00
|
|
|
a_World.PrepareChunk(chunkX, chunkZ, std::make_unique<cSpawnPrepareCallback>(prep));
|
2015-05-29 19:18:52 -04:00
|
|
|
} // for i
|
|
|
|
|
|
|
|
// Wait for the lighting thread to prepare everything. Event is set in the Call() callback:
|
2015-11-02 13:21:35 -05:00
|
|
|
if (MaxIdx > 0)
|
|
|
|
{
|
2017-08-22 08:34:43 -04:00
|
|
|
prep->m_EvtFinished.Wait();
|
2015-11-02 13:21:35 -05:00
|
|
|
}
|
2015-05-29 19:18:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cSpawnPrepare::DecodeChunkCoords(int a_Idx, int & a_ChunkX, int & a_ChunkZ)
|
|
|
|
{
|
|
|
|
// A zigzag pattern from the top to bottom, each row alternating between forward-x and backward-x:
|
|
|
|
int z = a_Idx / m_PrepareDistance;
|
|
|
|
int x = a_Idx % m_PrepareDistance;
|
|
|
|
if ((z & 1) == 0)
|
|
|
|
{
|
|
|
|
// Reverse every second row:
|
|
|
|
x = m_PrepareDistance - 1 - x;
|
|
|
|
}
|
|
|
|
a_ChunkZ = m_SpawnChunkZ + z - m_PrepareDistance / 2;
|
|
|
|
a_ChunkX = m_SpawnChunkX + x - m_PrepareDistance / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-30 06:11:17 -04:00
|
|
|
void cSpawnPrepare::PreparedChunkCallback(int a_ChunkX, int a_ChunkZ)
|
2015-05-29 19:18:52 -04:00
|
|
|
{
|
|
|
|
// Check if this was the last chunk:
|
|
|
|
m_NumPrepared += 1;
|
|
|
|
if (m_NumPrepared >= m_MaxIdx)
|
|
|
|
{
|
|
|
|
m_EvtFinished.Set();
|
|
|
|
// Must return here, because "this" may have gotten deleted by the previous line
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue another chunk, if appropriate:
|
|
|
|
if (m_NextIdx < m_MaxIdx)
|
|
|
|
{
|
|
|
|
int chunkX, chunkZ;
|
|
|
|
DecodeChunkCoords(m_NextIdx, chunkX, chunkZ);
|
2020-08-01 14:18:03 -04:00
|
|
|
m_World.GetLightingThread().QueueChunk(chunkX, chunkZ, std::make_unique<cSpawnPrepareCallback>(shared_from_this()));
|
2015-05-29 19:18:52 -04:00
|
|
|
m_NextIdx += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report progress every 1 second:
|
|
|
|
auto Now = std::chrono::steady_clock::now();
|
|
|
|
if (Now - m_LastReportTime > std::chrono::seconds(1))
|
|
|
|
{
|
|
|
|
float PercentDone = static_cast<float>(m_NumPrepared * 100) / m_MaxIdx;
|
|
|
|
float ChunkSpeed = static_cast<float>((m_NumPrepared - m_LastReportChunkCount) * 1000) / std::chrono::duration_cast<std::chrono::milliseconds>(Now - m_LastReportTime).count();
|
|
|
|
LOG("Preparing spawn (%s): %.02f%% (%d/%d; %.02f chunks / sec)",
|
2015-12-19 16:48:19 -05:00
|
|
|
m_World.GetName().c_str(), PercentDone, m_NumPrepared.load(std::memory_order_seq_cst), m_MaxIdx, ChunkSpeed
|
2015-05-29 19:18:52 -04:00
|
|
|
);
|
|
|
|
m_LastReportTime = Now;
|
|
|
|
m_LastReportChunkCount = m_NumPrepared;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|