From 774253e509ce13881229efda8849bf6f3c47b665 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 18 Mar 2015 19:48:58 +0100 Subject: [PATCH] add some light blocks to generated surfaces --- src/chunk.cpp | 15 +++++++++++++++ src/chunk.hpp | 4 ++++ src/generator.cpp | 10 ++++++++++ src/generator.hpp | 4 +++- src/world.cpp | 1 + 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/chunk.cpp b/src/chunk.cpp index 81c8698..5169422 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -263,6 +263,21 @@ int Chunk::GetLight(int index) const { } +bool Chunk::IsSurface(const Pos &pos) const { + const Block &block = BlockAt(pos); + if (!Type(block).visible) { + return false; + } + for (int face = 0; face < Block::FACE_COUNT; ++face) { + const Block *next = FindNext(pos, Block::Face(face)); + if (!next || !Type(*next).visible) { + return true; + } + } + return false; +} + + void Chunk::Allocate() { blocks.resize(Size(), Block(0)); light.resize(Size(), 0); diff --git a/src/chunk.hpp b/src/chunk.hpp index 7e720bd..c7bcd2f 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -76,6 +76,10 @@ public: (idx / Width()) % Height() == Height() - 1; // high Y plane } + bool IsSurface(int index) const { return IsSurface(ToPos(index)); } + bool IsSurface(const Block::Pos &pos) const { return IsSurface(Pos(pos)); } + bool IsSurface(const Pos &pos) const; + void SetNeighbor(Chunk &); bool HasNeighbor(Block::Face f) const { return neighbor[f]; } Chunk &GetNeighbor(Block::Face f) { return *neighbor[f]; } diff --git a/src/generator.cpp b/src/generator.cpp index f701230..c1a7a08 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -11,6 +11,7 @@ Generator::Generator(unsigned int seed) , stretch(64.0f) , solid_threshold(0.8f) , space(0) +, light(0) , solids() { } @@ -35,6 +36,15 @@ void Generator::operator ()(Chunk &chunk) const { } } } + unsigned int random = 263167 * pos.x + 2097593 * pos.y + 426389 * pos.z; + for (int index = 0; index < Chunk::Size(); ++index) { + if (chunk.IsSurface(index)) { + random = random * 666649 + 7778777; + if ((random % 32) == 0) { + chunk.SetBlock(index, Block(light)); + } + } + } chunk.Invalidate(); chunk.CheckUpdate(); } diff --git a/src/generator.hpp b/src/generator.hpp index 9dc39cb..b3e8ffb 100644 --- a/src/generator.hpp +++ b/src/generator.hpp @@ -18,16 +18,18 @@ public: void operator ()(Chunk &) const; void Space(Block::Type t) { space = t; } + void Light(Block::Type t) { light = t; } void Solids(const std::vector &s) { solids = s; } private: SimplexNoise solidNoise; - SimplexNoise typeNoise; + WorleyNoise typeNoise; float stretch; float solid_threshold; Block::Type space; + Block::Type light; std::vector solids; }; diff --git a/src/world.cpp b/src/world.cpp index f0d96f8..e4e3826 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -103,6 +103,7 @@ World::World() } generate.Space(0); + generate.Light(13); generate.Solids({ 1, 4, 7, 10 }); player = &AddEntity(); -- 2.39.2