]> git.localhorst.tv Git - blank.git/blobdiff - src/world.cpp
some cleanup
[blank.git] / src / world.cpp
index fb1023f8c5aad75ab50d5fa630f1ca139f05d8c8..b67bef31e9a71aa17c7c5bbcedf8f7aed5786cd7 100644 (file)
@@ -16,7 +16,8 @@ World::World()
 , player()
 , player_chunk(0, 0, 0)
 , loaded()
-, to_generate() {
+, to_generate()
+, to_free() {
        blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block
        blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &stairShape }); // white stair
        blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &slabShape }); // white slab
@@ -37,16 +38,22 @@ World::World()
 namespace {
 
 bool ChunkLess(const Chunk &a, const Chunk &b) {
-       return dot(a.Position(), a.Position()) < dot(b.Position(), b.Position());
+       return
+               a.Position().x * a.Position().x +
+               a.Position().y * a.Position().y +
+               a.Position().z * a.Position().z <
+               b.Position().x * b.Position().x +
+               b.Position().y * b.Position().y +
+               b.Position().z * b.Position().z;
 }
 
 }
 
-void World::Generate(const glm::tvec3<int> &from, const glm::tvec3<int> &to) {
+void World::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
        for (int z = from.z; z < to.z; ++z) {
                for (int y = from.y; y < to.y; ++y) {
                        for (int x = from.x; x < to.x; ++x) {
-                               glm::vec3 pos{float(x), float(y), float(z)};
+                               Block::Pos pos{float(x), float(y), float(z)};
                                if (ChunkAvailable(pos)) {
                                        continue;
                                } else if (x == 0 && y == 0 && z == 0) {
@@ -64,7 +71,9 @@ void World::Generate(const glm::tvec3<int> &from, const glm::tvec3<int> &to) {
 }
 
 void World::Generate(Chunk &chunk) {
-       glm::vec3 pos(chunk.Position());
+       chunk.Allocate();
+       Chunk::Pos pos(chunk.Position());
+       glm::vec3 coords(pos * Chunk::Extent());
        if (pos.x == 0 && pos.y == 0 && pos.z == 0) {
                for (size_t i = 1; i < blockType.Size(); ++i) {
                        chunk.BlockAt(i) = Block(blockType[i]);
@@ -75,8 +84,8 @@ void World::Generate(Chunk &chunk) {
                for (int z = 0; z < Chunk::Depth(); ++z) {
                        for (int y = 0; y < Chunk::Height(); ++y) {
                                for (int x = 0; x < Chunk::Width(); ++x) {
-                                       glm::vec3 block_pos{float(x), float(y), float(z)};
-                                       glm::vec3 gen_pos = (pos * Chunk::Extent() + block_pos) / 64.0f;
+                                       Block::Pos block_pos{float(x), float(y), float(z)};
+                                       glm::vec3 gen_pos = (coords + block_pos) / 64.0f;
                                        float val = blockNoise(gen_pos);
                                        if (val > 0.8f) {
                                                int col_val = int((colorNoise(gen_pos) + 1.0f) * 2.0f) % 4;
@@ -131,33 +140,33 @@ bool World::Intersection(
 }
 
 
-Chunk *World::ChunkLoaded(const glm::tvec3<int> &pos) {
+Chunk *World::ChunkLoaded(const Chunk::Pos &pos) {
        for (Chunk &chunk : loaded) {
-               if (glm::tvec3<int>(chunk.Position()) == pos) {
+               if (chunk.Position() == pos) {
                        return &chunk;
                }
        }
        return nullptr;
 }
 
-Chunk *World::ChunkQueued(const glm::tvec3<int> &pos) {
+Chunk *World::ChunkQueued(const Chunk::Pos &pos) {
        for (Chunk &chunk : to_generate) {
-               if (glm::tvec3<int>(chunk.Position()) == pos) {
+               if (chunk.Position() == pos) {
                        return &chunk;
                }
        }
        return nullptr;
 }
 
-Chunk *World::ChunkAvailable(const glm::tvec3<int> &pos) {
+Chunk *World::ChunkAvailable(const Chunk::Pos &pos) {
        Chunk *chunk = ChunkLoaded(pos);
        if (chunk) return chunk;
 
        return ChunkQueued(pos);
 }
 
-Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
-       const glm::vec3 tgt_pos = to.Position() + dir;
+Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
+       const Chunk::Pos tgt_pos = to.Position() + dir;
 
        Chunk *chunk = ChunkLoaded(tgt_pos);
        if (chunk) {
@@ -193,7 +202,9 @@ void World::CheckChunkGeneration() {
                        if (std::abs(player_chunk.x - iter->Position().x) > max_dist
                                        || std::abs(player_chunk.y - iter->Position().y) > max_dist
                                        || std::abs(player_chunk.z - iter->Position().z) > max_dist) {
-                               iter = loaded.erase(iter);
+                               auto saved = iter;
+                               ++iter;
+                               to_free.splice(to_free.end(), loaded, saved);
                        } else {
                                ++iter;
                        }
@@ -209,7 +220,7 @@ void World::CheckChunkGeneration() {
                        }
                }
                // add missing new chunks
-               glm::tvec3<int> offset(max_dist, max_dist, max_dist);
+               const Chunk::Pos offset(max_dist, max_dist, max_dist);
                Generate(player_chunk - offset, player_chunk + offset);
        }
 
@@ -217,6 +228,10 @@ void World::CheckChunkGeneration() {
                Generate(to_generate.front());
                loaded.splice(loaded.end(), to_generate, to_generate.begin());
        }
+
+       if (!to_free.empty()) {
+               to_free.pop_front();
+       }
 }