Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Jukitsu/Jukcraft
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukitsu committed Jun 28, 2024
2 parents 85e094c + 52730bd commit 0304457
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 81 deletions.
102 changes: 21 additions & 81 deletions Jukcraft/src/world/LightEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ namespace Jukcraft {
void LightEngine::initSkyLight(std::shared_ptr<Chunk>& chunk) {
glm::ivec2 chunkPos = chunk->getChunkPos();

uint32_t height = 0;
int32_t height = 0;
for (uint32_t lx = 0; lx < CHUNK_DIM; lx++)
for (uint32_t lz = 0; lz < CHUNK_DIM; lz++) {
int ly = CHUNK_HEIGHT - 1;
int32_t ly = CHUNK_HEIGHT - 1;
for (; ly >= 0; ly--) {
if (chunk->getBlock(BlockPos(lx, ly, lz)))
break;

}

if (ly > (int)height && ly >= 0)
height = (uint32_t)ly;
if (ly > height && ly >= 0)
height = ly;
}

for (uint32_t ly = CHUNK_HEIGHT - 1; ly >= height; ly--)
for (int32_t ly = CHUNK_HEIGHT - 1; ly >= height; ly--)
for (uint32_t lx = 0; lx < CHUNK_DIM; lx++)
for (uint32_t lz = 0; lz < CHUNK_DIM; lz++) {
chunk->setSkyLight(BlockPos(lx, ly, lz), 15);
Expand All @@ -31,38 +31,23 @@ namespace Jukcraft {
for (uint32_t lz = 0; lz < CHUNK_DIM; lz++) {
glm::ivec3 localPos(lx, height, lz);
BlockPos lightEnginePos(chunkPos, localPos);
skylightIncreaseQueue.push(
SkyLightIncreaseNode{
lightEnginePos,
15
}
);
skylightIncreaseQueue.emplace(lightEnginePos, 15);
propagateSkyLightIncrease();
}
}

void LightEngine::increaseLight(const BlockPos& pos, std::shared_ptr<Chunk>& chunk, uint8_t light) {
chunk->setBlockLight(pos.getLocalPos(), light);

lightIncreaseQueue.push(
BlockLightIncreaseNode{
pos,
light
}
);
lightIncreaseQueue.emplace(pos, light);
propagateLightIncrease();
}

void LightEngine::decreaseLight(const BlockPos& pos, std::shared_ptr<Chunk>& chunk) {
uint8_t oldlight = chunk->getBlockLight(pos.getLocalPos());
chunk->setBlockLight(pos.getLocalPos(), 0);

lightDecreaseQueue.push(
BlockLightDecreaseNode{
pos,
oldlight
}
);
lightDecreaseQueue.emplace(pos, oldlight);
propagateLightDecrease();
propagateLightIncrease();
}
Expand All @@ -71,12 +56,7 @@ namespace Jukcraft {
uint8_t oldlight = chunk->getSkyLight(pos.getLocalPos());
chunk->setSkyLight(pos.getLocalPos(), 0);

skylightDecreaseQueue.push(
SkyLightDecreaseNode{
pos,
oldlight
}
);
skylightDecreaseQueue.emplace(pos, oldlight);
propagateSkyLightDecrease();
propagateSkyLightIncrease();
}
Expand All @@ -103,13 +83,8 @@ namespace Jukcraft {
if (chunk.value()->getBlockLight(localPos) + 2 <= light
&& blocks[chunk.value()->getBlock(localPos)].isTransparent()) {
chunk.value()->setBlockLight(localPos, light - 1);
lightIncreaseQueue.push(
BlockLightIncreaseNode{
newPos,
(uint8_t)(light - 1)
}
);
chunkManager.updateChunkAtPosition(chunk.value(), localPos);
lightIncreaseQueue.emplace(newPos, light - 1);
markPositionForUpdate(chunk.value(), localPos);
}
}
}
Expand Down Expand Up @@ -137,24 +112,14 @@ namespace Jukcraft {
&& blocks[chunk.value()->getBlock(localPos)].isTransparent()) {
if (direction.y == -1) {
chunk.value()->setSkyLight(localPos, skylight);
skylightIncreaseQueue.push(
SkyLightIncreaseNode{
newPos,
skylight
}
);
skylightIncreaseQueue.emplace(newPos, skylight);
}
else if (chunk.value()->getSkyLight(localPos) + 2 <= skylight) {
chunk.value()->setSkyLight(localPos, skylight - 1);
skylightIncreaseQueue.push(
SkyLightIncreaseNode{
newPos,
(uint8_t)(skylight - 1)
}
);
skylightIncreaseQueue.emplace(newPos, skylight - 1);
}
else continue;
chunkManager.updateChunkAtPosition(chunk.value(), localPos);
markPositionForUpdate(chunk.value(), localPos);
}
}
}
Expand All @@ -181,12 +146,7 @@ namespace Jukcraft {
Block block = blocks[chunk.value()->getBlock(localPos)];

if (block.getLight()) {
lightIncreaseQueue.push(
BlockLightIncreaseNode{
newPos,
block.getLight()
}
);
lightIncreaseQueue.emplace(newPos, block.getLight());
continue;
}

Expand All @@ -197,21 +157,11 @@ namespace Jukcraft {

if (neighbourLight < oldlight) {
chunk.value()->setBlockLight(localPos, 0);
lightDecreaseQueue.push(
BlockLightDecreaseNode{
newPos,
neighbourLight
}
);
chunkManager.updateChunkAtPosition(chunk.value(), localPos);
lightDecreaseQueue.emplace(newPos, neighbourLight);
markPositionForUpdate(chunk.value(), localPos);
}
else if (neighbourLight >= oldlight) {
lightIncreaseQueue.push(
BlockLightIncreaseNode{
newPos,
neighbourLight
}
);
lightIncreaseQueue.emplace(newPos, neighbourLight);
}
}

Expand Down Expand Up @@ -242,21 +192,11 @@ namespace Jukcraft {
uint8_t neighbourSkylight = chunk.value()->getSkyLight(localPos);
if (direction.y == -1 || neighbourSkylight < oldlight) {
chunk.value()->setSkyLight(localPos, 0);
skylightDecreaseQueue.push(
SkyLightDecreaseNode{
newPos,
neighbourSkylight
}
);
chunkManager.updateChunkAtPosition(chunk.value(), localPos);
skylightDecreaseQueue.emplace(newPos, neighbourSkylight);
markPositionForUpdate(chunk.value(), localPos);
}
else if (neighbourSkylight >= oldlight) {
skylightIncreaseQueue.push(
SkyLightIncreaseNode{
newPos,
neighbourSkylight
}
);
skylightIncreaseQueue.emplace(newPos, neighbourSkylight);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions Jukcraft/src/world/LightEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,32 @@ namespace Jukcraft {
void toggleLightUpdates(bool value) noexcept { doLightUpdates = value; }
private:
void markPositionForUpdate(std::shared_ptr<Chunk>&, const glm::ivec3& localPos);

struct BlockLightIncreaseNode {
BlockPos pos;
uint8_t light;

BlockLightIncreaseNode(const BlockPos& pos, uint8_t light) :pos(pos), light(light) {}
};
struct BlockLightDecreaseNode {
BlockPos pos;
uint8_t oldlight;

BlockLightDecreaseNode(const BlockPos& pos, uint8_t oldlight) :pos(pos), oldlight(oldlight) {}
};
struct SkyLightIncreaseNode {
BlockPos pos;
uint8_t light;

SkyLightIncreaseNode(const BlockPos& pos, uint8_t light) :pos(pos), light(light) {}
};
struct SkyLightDecreaseNode {
BlockPos pos;
uint8_t oldlight;

SkyLightDecreaseNode(const BlockPos& pos, uint8_t oldlight) :pos(pos), oldlight(oldlight) {}
};

std::queue<BlockLightIncreaseNode> lightIncreaseQueue;
std::queue<SkyLightIncreaseNode> skylightIncreaseQueue;
std::queue<BlockLightDecreaseNode> lightDecreaseQueue;
Expand Down
2 changes: 2 additions & 0 deletions Jukcraft/src/world/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Jukcraft {
}
}
);

lightEngine.toggleLightUpdates(true);
}

void World::tick() {
Expand Down

0 comments on commit 0304457

Please sign in to comment.