From 15352db7bc03a176ea7a184f40a7f4e72b54b5fe Mon Sep 17 00:00:00 2001 From: Mel Date: Sat, 29 Jul 2023 17:31:10 +0200 Subject: Add default light to just-generated chunks --- src/World/Chunk.hpp | 4 ++-- src/World/Generation/Decoration.cpp | 14 +++++++++++++- src/World/Generation/Decoration.hpp | 5 +++++ src/World/Generation/Generator.cpp | 1 + src/World/Generation/Generator.hpp | 1 + 5 files changed, 22 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp index 1aa2dd1..0b00c97 100644 --- a/src/World/Chunk.hpp +++ b/src/World/Chunk.hpp @@ -21,8 +21,8 @@ public: m_position{(Real)x * Width, 0.0f, (Real)y * Width} {} struct BlockData { - BlockData() : type(BlockType::Air), light{200} {} - BlockData(BlockType t) : type(t), light{200} {} + BlockData() : type(BlockType::Air), light{0} {} + BlockData(BlockType t) : type(t), light{0} {} BlockType type; U8 light; diff --git a/src/World/Generation/Decoration.cpp b/src/World/Generation/Decoration.cpp index bcabffa..556b5e7 100644 --- a/src/World/Generation/Decoration.cpp +++ b/src/World/Generation/Decoration.cpp @@ -45,7 +45,7 @@ void TreeDecorator::decorate_chunk(Chunk& chunk) { Pos last_tree = Pos::max(); for (UInt x = 0; x < Chunk::Width; x++) { for (UInt z = 0; z < Chunk::Width; z++) { - for (UInt y = Chunk::Height; y > 1; y--) { + for (UInt y = Chunk::Height - 1; y != 0; y--) { Pos pos{x, y, z}; if (!is_valid_position(pos)) continue; @@ -96,4 +96,16 @@ Bool TreeDecorator::is_valid_position(Vector<3, UInt> pos) { && pos.z() + tree_radius <= Chunk::Width; } +void DefaultLightDecorator::decorate_chunk(Chunk& chunk) { + for (UInt x = 0; x < Chunk::Width; x++) { + for (UInt z = 0; z < Chunk::Width; z++) { + for (UInt y = Chunk::Height - 1; y != 0; y--) { + auto& block = chunk.at(x, y, z); + if (!block.type.is_translucent()) break; + chunk.at(x, y, z).light = 200; + } + } + } +} + } diff --git a/src/World/Generation/Decoration.hpp b/src/World/Generation/Decoration.hpp index b7a839b..2f119ff 100644 --- a/src/World/Generation/Decoration.hpp +++ b/src/World/Generation/Decoration.hpp @@ -31,4 +31,9 @@ private: Math::Random::Noise<2> m_tree_noise; }; +class DefaultLightDecorator final : public Decorator { +public: + void decorate_chunk(Chunk& chunk) override; +}; + } diff --git a/src/World/Generation/Generator.cpp b/src/World/Generation/Generator.cpp index 9d63fc1..79c10b6 100644 --- a/src/World/Generation/Generator.cpp +++ b/src/World/Generation/Generator.cpp @@ -126,6 +126,7 @@ Generator::Map3D Generator::generate_terrain(Map2D& height_map, I64 } void Generator::decorate_soil(Chunk& chunk, Map2D& biome_map, Map3D& terrain_map) { + // TODO: Put all of this into decorators. constexpr UInt dirt_depth = 4; constexpr UInt water_height = 40; diff --git a/src/World/Generation/Generator.hpp b/src/World/Generation/Generator.hpp index ee78dc5..85f2c38 100644 --- a/src/World/Generation/Generator.hpp +++ b/src/World/Generation/Generator.hpp @@ -45,6 +45,7 @@ private: static inline std::vector s_decorators = { new TreeDecorator(), + new DefaultLightDecorator(), }; Math::Perlin::Noise<2> m_landmass_noise{.scale=800.0f, .octaves=4, .persistence=0.3f, .lacunarity=3.5f}; -- cgit 1.4.1