17 public cChunkDataCallback
26 if (Section ==
nullptr)
37 auto InPtr = Section->m_BlockTypes + Z * cChunkDef::Width + OffsetY * cChunkDef::Width *
cChunkDef::Width;
38 std::copy_n(InPtr, cChunkDef::Width, OutputRows + OutputIdx * cChunkDef::Width);
44 OutputIdx += cChunkDef::Width * 6;
54 const ROW * InputRows =
reinterpret_cast<const ROW *
>(a_Heightmap);
55 ROW * OutputRows =
reinterpret_cast<ROW *
>(
m_HeightMap);
60 OutputRows[OutputIdx] = InputRows[InputIdx++];
66 for (
size_t i = 0; i <
ARRAYCOUNT(*a_Heightmap); i++)
68 if ((*a_Heightmap)[i] > MaxHeight)
70 MaxHeight = (*a_Heightmap)[i];
87 m_BlockTypes(a_BlockTypes),
88 m_HeightMap(a_HeightMap)
102 super(
"cLightingThread"),
132 for (cChunkStays::iterator itr =
m_Queue.begin(), end =
m_Queue.end(); itr != end; ++itr)
333 for (
int z = 0; z < 3; z++)
336 for (
int x = 0; x < 3; x++)
367 int BaseZ = z * cChunkDef::Width * 3;
368 for (
int x = 1; x < cChunkDef::Width * 3 - 1; x++)
386 int Neighbor3 =
m_HeightMap[idx + cChunkDef::Width * 3] + 1;
387 int Neighbor4 =
m_HeightMap[idx - cChunkDef::Width * 3] + 1;
388 int MaxNeighbor = std::max(std::max(Neighbor1, Neighbor2), std::max(Neighbor3, Neighbor4));
405 for (
int y = Current + 1, Index = idx + y * BlocksPerYLayer; y < MaxNeighbor; y++, Index +=
BlocksPerYLayer)
449 size_t NumSeeds2 = 0;
474 size_t a_NumSeedsIn,
unsigned char * a_IsSeedIn,
unsigned int * a_SeedIdxIn,
475 size_t & a_NumSeedsOut,
unsigned char * a_IsSeedOut,
unsigned int * a_SeedIdxOut
479 size_t NumSeedsOut = 0;
480 for (
size_t i = 0; i < a_NumSeedsIn; i++)
489 PropagateLight(a_Light, SeedIdx, SeedIdx + 1, NumSeedsOut, a_IsSeedOut, a_SeedIdxOut);
493 PropagateLight(a_Light, SeedIdx, SeedIdx - 1, NumSeedsOut, a_IsSeedOut, a_SeedIdxOut);
512 a_NumSeedsOut = NumSeedsOut;
529 a_ChunkLight[OutIdx++] =
static_cast<NIBBLETYPE>(a_LightArray[InIdx + 1] << 4) | a_LightArray[InIdx];
532 InIdx += cChunkDef::Width * 2;
536 InIdx += cChunkDef::Width * cChunkDef::Width * 6;
550 m_Queue.push_back(&a_ChunkStay);
563 m_LightingThread(a_LightingThread),
566 m_CallbackAfter(
std::move(a_CallbackAfter))
568 Add(a_ChunkX + 1, a_ChunkZ + 1);
569 Add(a_ChunkX + 1, a_ChunkZ);
570 Add(a_ChunkX + 1, a_ChunkZ - 1);
571 Add(a_ChunkX, a_ChunkZ + 1);
572 Add(a_ChunkX, a_ChunkZ);
573 Add(a_ChunkX, a_ChunkZ - 1);
574 Add(a_ChunkX - 1, a_ChunkZ + 1);
575 Add(a_ChunkX - 1, a_ChunkZ);
576 Add(a_ChunkX - 1, a_ChunkZ - 1);
void Set(void)
Sets the event - releases one thread that has been waiting in Wait().
std::unique_ptr< cChunkCoordCallback > m_CallbackAfter
std::atomic< bool > m_ShouldTerminate
The overriden Execute() method should check this value periodically and terminate if this is true...
Makes chunks stay loaded until this object is cleared or destroyed Works by setting internal flags in...
void QueueChunk(int a_ChunkX, int a_ChunkZ, std::unique_ptr< cChunkCoordCallback > a_CallbackAfter)
Queues the entire chunk for lighting.
virtual void OnDisabled(void) override
Called by the ChunkMap when the ChunkStay is disabled.
unsigned char BLOCKTYPE
The datatype used by blockdata.
const sChunkSection * GetSection(size_t a_SectionNum) const
Return a pointer to the chunk section or nullptr if all air.
virtual void HeightMap(const cChunkDef::HeightMap *a_Heightmap) override
void ReadChunks(int a_ChunkX, int a_ChunkZ)
Prepares m_BlockTypes and m_HeightMap data; zeroes out the light arrays.
static const int NumBlocks
static bool IsSkylightDispersant(BLOCKTYPE a_Type)
void ChunkLighted(int a_ChunkX, int a_ChunkZ, const cChunkDef::BlockNibbles &a_BlockLight, const cChunkDef::BlockNibbles &a_SkyLight)
void PrepareBlockLight(void)
Uses m_BlockTypes to initialize the m_BlockLight[] data; fills in seeds for the blocklight.
cChunkStays m_PendingQueue
The ChunkStays that are waiting for load.
virtual void Execute(void) override
This is the main thread entrypoint.
HEIGHTTYPE m_MaxHeight
The highest block in the current 3x3 chunk data.
unsigned char m_IsSeed2[BlocksPerYLayer *cChunkDef::Height]
void Enable(cChunkMap &a_ChunkMap)
Enables the ChunkStay on the specified chunkmap, causing it to load and generate chunks.
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
static const int BlocksPerYLayer
unsigned char HEIGHTTYPE
The type used by the heightmap.
static NIBBLETYPE GetLightValue(BLOCKTYPE a_Type)
void LightChunk(cLightingChunkStay &a_Item)
Lights the entire chunk.
void CalcLight(NIBBLETYPE *a_Light)
Calculates light in the light array specified, using stored seeds.
void CompressLight(NIBBLETYPE *a_LightArray, NIBBLETYPE *a_ChunkLight)
Compresses from 1-block-per-byte (faster calc) into 2-blocks-per-byte (MC storage): ...
void Wait(void)
Waits until the event has been set.
virtual void Disable(void)
Disables the ChunkStay, the chunks are released and the ChunkStay object can be edited with Add() and...
void PrepareSkyLight(void)
Uses m_HeightMap to initialize the m_SkyLight[] data; fills in seeds for the skylight.
size_t GetQueueLength(void)
Temporary RAII unlock for a cCSLock.
bool IsChunkLighted(int a_ChunkX, int a_ChunkZ)
cLightingChunkStay(cLightingThread &a_LightingThread, int a_ChunkX, int a_ChunkZ, std::unique_ptr< cChunkCoordCallback > a_CallbackAfter)
Chunk data callback that takes the chunk data and puts them into cLightingThread's m_BlockTypes[] / m...
virtual bool OnAllChunksAvailable(void) override
Caled once all of the contained chunks are available.
virtual ~cLightingThread() override
NIBBLETYPE m_SkyLight[BlocksPerYLayer *cChunkDef::Height]
cLightingThread & m_LightingThread
void WaitForQueueEmpty(void)
Blocks until the queue is empty or the thread is terminated.
cLightingThread(cWorld &a_World)
void Stop(void)
Signals the thread to terminate and waits until it's finished.
bool GetChunkData(cChunkCoords a_Coords, cChunkDataCallback &a_Callback) const
Calls the callback with the chunk's data, if available (with ChunkCS locked).
unsigned int m_SeedIdx2[BlocksPerYLayer *cChunkDef::Height]
cCriticalSection m_CS
The mutex to protect m_Queue and m_PendingQueue.
cChunkMap * GetChunkMap(void)
NIBBLETYPE m_BlockLight[BlocksPerYLayer *cChunkDef::Height]
static bool IsTransparent(BLOCKTYPE a_Type)
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
cChunkStays m_Queue
The ChunkStays that are loaded and are waiting to be lit.
unsigned char m_IsSeed1[BlocksPerYLayer *cChunkDef::Height]
static const size_t NumSections
BLOCKTYPE m_BlockTypes[BlocksPerYLayer *cChunkDef::Height]
NIBBLETYPE BlockNibbles[NumBlocks/2]
The type used for block data in nibble format, AXIS_ORDER ordering.
void Add(int a_ChunkX, int a_ChunkZ)
Adds a chunk to be locked from unloading.
unsigned int m_SeedIdx1[BlocksPerYLayer *cChunkDef::Height]
void CalcLightStep(NIBBLETYPE *a_Light, size_t a_NumSeedsIn, unsigned char *a_IsSeedIn, unsigned int *a_SeedIdxIn, size_t &a_NumSeedsOut, unsigned char *a_IsSeedOut, unsigned int *a_SeedIdxOut)
Does one step in the light calculation - one seed propagation and seed recalculation.
cReader(BLOCKTYPE *a_BlockTypes, HEIGHTTYPE *a_HeightMap)
void PropagateLight(NIBBLETYPE *a_Light, unsigned int a_SrcIdx, unsigned int a_DstIdx, size_t &a_NumSeedsOut, unsigned char *a_IsSeedOut, unsigned int *a_SeedIdxOut)
HEIGHTTYPE HeightMap[Width *Width]
The type used for any heightmap operations and storage; idx = x + Width * z; Height points to the hig...
void QueueChunkStay(cLightingChunkStay &a_ChunkStay)
Queues a chunkstay that has all of its chunks loaded.
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
virtual void ChunkData(const cChunkData &a_ChunkBuffer) override
static const int SectionHeight
HEIGHTTYPE m_HeightMap[BlocksPerYLayer]