From 52d5760be17235a1811e9a26ce849c4a33afad72 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Mon, 30 Mar 2020 18:23:25 +0100 Subject: Add helper functions to get combined light from cChunk These are now used for plant growth --- src/Blocks/BlockGrass.h | 6 ++---- src/Blocks/BlockPlant.h | 17 +++++------------ src/Chunk.h | 6 ++++++ src/Mobs/Spider.cpp | 2 +- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/Blocks/BlockGrass.h b/src/Blocks/BlockGrass.h index 0e54cb092..341a44b07 100644 --- a/src/Blocks/BlockGrass.h +++ b/src/Blocks/BlockGrass.h @@ -60,9 +60,8 @@ public: return; } - NIBBLETYPE light = std::max(a_Chunk.GetBlockLight(a_RelX, a_RelY + 1, a_RelZ), a_Chunk.GetTimeAlteredLight(a_Chunk.GetSkyLight(a_RelX, a_RelY + 1, a_RelZ))); // Source block is not bright enough to spread - if (light < 9) + if (a_Chunk.GetLightAltered(Vector3i(a_RelX, a_RelY + 1, a_RelZ)) < 9) { return; } @@ -99,8 +98,7 @@ public: } auto abovePos = pos.addedY(1); BLOCKTYPE above = chunk->GetBlock(abovePos); - NIBBLETYPE light = std::max(chunk->GetBlockLight(abovePos), chunk->GetTimeAlteredLight(chunk->GetSkyLight(abovePos))); - if ((light > 4) && + if ((a_Chunk.GetLightAltered(abovePos) > 4) && cBlockInfo::IsTransparent(above) && (!IsBlockLava(above)) && (!IsBlockWaterOrIce(above)) diff --git a/src/Blocks/BlockPlant.h b/src/Blocks/BlockPlant.h index 0e249bbde..d71614cdb 100644 --- a/src/Blocks/BlockPlant.h +++ b/src/Blocks/BlockPlant.h @@ -77,26 +77,19 @@ protected: { return paGrowth; } - NIBBLETYPE Blocklight = a_Chunk.GetBlockLight(a_RelPos); - NIBBLETYPE SkyLight = a_Chunk.GetSkyLight (a_RelPos); - NIBBLETYPE Light = a_Chunk.GetTimeAlteredLight(SkyLight); - - // If the amount of light provided by blocks is greater than the sky light, use it instead - if (Blocklight > Light) - { - Light = Blocklight; - } // Based on light levels, decide between growth, stay and death: - if (Light > 8) + // Grow if the combined adjusted light above is bright enough. + if (a_Chunk.GetLightAltered(a_RelPos + Vector3i(0, 1, 0)) > 8) { return paGrowth; } - else if ((Blocklight < 9) && (SkyLight < 9)) + // Die if the combined non-adjusted light inside is dark enough. + if (a_Chunk.GetLight(a_RelPos) < 8) { return paDeath; } - + // Otherwise stay the same. return paStay; } diff --git a/src/Chunk.h b/src/Chunk.h index dd21d0e8f..35c4acf8d 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -415,6 +415,12 @@ public: /** Light alterations based on time */ NIBBLETYPE GetTimeAlteredLight(NIBBLETYPE a_Skylight) const; + /** Get the maximum of natural and artificial light illuminating the block (0 - 15). */ + inline NIBBLETYPE GetLight(Vector3i a_RelPos) const { return std::max(GetBlockLight(a_RelPos), GetSkyLight(a_RelPos)); } + + /** Get the maximum of natural and artificial light illuminating the block (0 - 15), taking daytime into account. */ + inline NIBBLETYPE GetLightAltered(Vector3i a_RelPos) const { return std::max(GetBlockLight(a_RelPos), GetSkyLightAltered(a_RelPos)); } + /** Get the level of artificial light illuminating the block (0 - 15) */ inline NIBBLETYPE GetBlockLight(Vector3i a_RelPos) const { return m_ChunkData.GetBlockLight(a_RelPos); } inline NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const { return m_ChunkData.GetBlockLight({ a_RelX, a_RelY, a_RelZ }); } diff --git a/src/Mobs/Spider.cpp b/src/Mobs/Spider.cpp index 329d89e93..319610fa4 100644 --- a/src/Mobs/Spider.cpp +++ b/src/Mobs/Spider.cpp @@ -50,7 +50,7 @@ void cSpider::EventSeePlayer(cPlayer * a_Player, cChunk & a_Chunk) if ( a_Player->CanMobsTarget() && - !((Chunk->GetSkyLightAltered(Rel.x, Rel.y, Rel.z) > 11) || (Chunk->GetBlockLight(Rel.x, Rel.y, Rel.z) > 11)) + !(Chunk->GetLightAltered(Rel) > 11) ) { super::EventSeePlayer(a_Player, a_Chunk); -- cgit v1.2.3