]> git.localhorst.tv Git - blank.git/commitdiff
block type prototypability and new types
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 25 Nov 2015 15:59:19 +0000 (16:59 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 25 Nov 2015 16:18:06 +0000 (17:18 +0100)
this will mess up your world saves

and by "your" I mean "my"

assets
doc/todo
src/app/app.cpp
src/world/BlockType.hpp
src/world/BlockTypeRegistry.hpp
src/world/Generator.cpp
src/world/block.cpp
tst/world/ChunkTest.cpp

diff --git a/assets b/assets
index 8fed55072b5b2d5baa06e2ab045df3161f86fdb2..38f6d1d15a6d1469a080d77e9fcdafe5c545abf0 160000 (submodule)
--- a/assets
+++ b/assets
@@ -1 +1 @@
-Subproject commit 8fed55072b5b2d5baa06e2ab045df3161f86fdb2
+Subproject commit 38f6d1d15a6d1469a080d77e9fcdafe5c545abf0
index 9df0ec47c2a0f193161f0b910ec79c923dc64d07..2de96bd057cf19454ddcd24c1e798a5976a84de9 100644 (file)
--- a/doc/todo
+++ b/doc/todo
@@ -103,6 +103,8 @@ world generator that is not boring
        exploration
        biomes seem too small, maybe that will become easier to tune when
        there's a little more diversity between them
+       chunk generation takes too long, it's incredibly annoying
+       should look into speeding it up and executing on a different thread
 
 spawning
 
index 4cc742ffede2ad5db34617b69b28570d307209b7..6c89bdbeebeef3710b43b37881a0af1042e8ccf6 100644 (file)
@@ -321,11 +321,16 @@ void AssetLoader::LoadBlockTypes(
                throw std::runtime_error("failed to open block type file " + full);
        }
        TokenStreamReader in(file);
-       string name;
+       string proto;
        while (in.HasMore()) {
-               in.ReadIdentifier(name);
-               in.Skip(Token::EQUALS);
                BlockType type;
+               in.ReadIdentifier(type.name);
+               in.Skip(Token::EQUALS);
+               if (in.Peek().type == Token::IDENTIFIER) {
+                       // prototype
+                       in.ReadIdentifier(proto);
+                       type.Copy(reg.Get(proto));
+               }
                type.Read(in, snd_index, tex_index, shapes);
                in.Skip(Token::SEMICOLON);
                reg.Add(std::move(type));
index a63507aec28ce568f991866a2abece597d2ac845..ef328d4d926feb353cf76df65810274c99939a73 100644 (file)
@@ -31,6 +31,9 @@ struct BlockType {
        /// gravity configuration or null if not emitting gravity
        std::unique_ptr<BlockGravity> gravity;
 
+       /// a string identifying in contexts where numbers just won't do
+       /// must be unique within any given set
+       std::string name;
        /// a string to display to the user
        std::string label;
 
@@ -74,6 +77,10 @@ struct BlockType {
 
        BlockType() noexcept;
 
+       /// clone values of given type
+       /// this copies everything except for ID, name, label, and gravity
+       void Copy(const BlockType &) noexcept;
+
        void Read(
                TokenStreamReader &in,
                ResourceIndex &snd_index,
index 6612d1516e72c893298f21ed193830a15fdd5671..23b183a1b1cf765a390d920bc9b41278f7500fd5 100644 (file)
@@ -3,6 +3,8 @@
 
 #include "BlockType.hpp"
 
+#include <map>
+#include <string>
 #include <vector>
 
 
@@ -20,6 +22,8 @@ public:
 public:
        BlockTypeRegistry();
 
+       /// register a new block type
+       /// this may throw if the name is already taken (names must be unique)
        Block::Type Add(BlockType &&);
 
        size_t size() const noexcept { return types.size(); }
@@ -29,14 +33,20 @@ public:
        iterator end() noexcept { return types.end(); }
        const_iterator end() const noexcept { return types.end(); }
 
-       BlockType &operator [](Block::Type id) { return types[id]; }
-       const BlockType &operator [](Block::Type id) const { return types[id]; }
+       /// lookup by ID
+       BlockType &operator [](Block::Type id) noexcept { return types[id]; }
+       const BlockType &operator [](Block::Type id) const noexcept { return types[id]; }
 
-       BlockType &Get(Block::Type id) { return types[id]; }
-       const BlockType &Get(Block::Type id) const { return types[id]; }
+       BlockType &Get(Block::Type id) noexcept { return types[id]; }
+       const BlockType &Get(Block::Type id) const noexcept { return types[id]; }
+
+       /// lookup by name
+       BlockType &Get(const std::string &name);
+       const BlockType &Get(const std::string &name) const;
 
 private:
        std::vector<BlockType> types;
+       std::map<std::string, Block::Type> names;
 
 };
 
index 466139b8943a4fadc1187736e6b29509d462642b..edb7e9caa51bc879ec7181c66153710dc5e24a34 100644 (file)
@@ -91,6 +91,8 @@ Block Generator::Generate(const glm::vec3 &pos) const noexcept {
                return Block(0);
        }
        float random = GetValue(random_noise, pos, config.randomness);
+       // as weird as it sounds, but this is faster tham glm::fract and generates a
+       // better distribution than (transformed variants of) erf, erfc, atan, and smoothstep
        if (random < 0.0f) random += 1.0f;
        float value = random * total;
        // TODO: change to binary search
index 61e2ab4e9801fc920b4885353a7bc03858620808..937291810c5f0557a8d52d1b862316a601ce28ba 100644 (file)
@@ -8,6 +8,7 @@
 #include "../shared/ResourceIndex.hpp"
 
 #include <iostream>
+#include <stdexcept>
 #include <glm/gtx/euler_angles.hpp>
 #include <glm/gtx/norm.hpp>
 #include <glm/gtx/transform.hpp>
@@ -77,6 +78,7 @@ BlockType::BlockType() noexcept
 , rgb_mod(1.0f, 1.0f, 1.0f)
 , outline_color(-1, -1, -1)
 , gravity()
+, name("anonymous")
 , label("some block")
 , place_sound(-1)
 , remove_sound(-1)
@@ -103,6 +105,35 @@ BlockType::BlockType() noexcept
 
 }
 
+void BlockType::Copy(const BlockType &other) noexcept {
+       shape = other.shape;
+       textures = other.textures;
+       hsl_mod = other.hsl_mod;
+       rgb_mod = other.rgb_mod;
+       outline_color = other.outline_color;
+       place_sound = other.place_sound;
+       remove_sound = other.remove_sound;
+       luminosity = other.luminosity;
+       visible = other.visible;
+       block_light = other.block_light;
+       collision = other.collision;
+       collide_block = collide_block;
+       generate = other.generate;
+       min_solidity = other.min_solidity;
+       mid_solidity = other.mid_solidity;
+       max_solidity = other.max_solidity;
+       min_humidity = other.min_humidity;
+       mid_humidity = other.mid_humidity;
+       max_humidity = other.max_humidity;
+       min_temperature = other.min_temperature;
+       mid_temperature = other.mid_temperature;
+       max_temperature = other.max_temperature;
+       min_richness = other.min_richness;
+       mid_richness = other.mid_richness;
+       max_richness = other.max_richness;
+       commonness = other.commonness;
+}
+
 void BlockType::Read(
        TokenStreamReader &in,
        ResourceIndex &snd_index,
@@ -225,6 +256,8 @@ void BlockType::OutlinePrimitiveMesh(PrimitiveMesh::Buffer &buf) const noexcept
 
 BlockTypeRegistry::BlockTypeRegistry() {
        BlockType air;
+       air.name = "air";
+       air.label = "Air";
        air.visible = false;
        air.block_light = false;
        air.collision = false;
@@ -234,11 +267,32 @@ BlockTypeRegistry::BlockTypeRegistry() {
 
 Block::Type BlockTypeRegistry::Add(BlockType &&t) {
        int id = types.size();
+       if (!names.emplace(t.name, id).second) {
+               throw std::runtime_error("duplicate block type name " + t.name);
+       }
        types.push_back(std::move(t));
        types.back().id = id;
        return id;
 }
 
+BlockType &BlockTypeRegistry::Get(const std::string &name) {
+       auto entry = names.find(name);
+       if (entry != names.end()) {
+               return Get(entry->second);
+       } else {
+               throw std::runtime_error("unknown block type " + name);
+       }
+}
+
+const BlockType &BlockTypeRegistry::Get(const std::string &name) const {
+       auto entry = names.find(name);
+       if (entry != names.end()) {
+               return Get(entry->second);
+       } else {
+               throw std::runtime_error("unknown block type " + name);
+       }
+}
+
 
 namespace {
 
index e2c02fd4862ee36d4a2112ea33fc799a6be778e8..26f2014cfc3fbec2b061959ce893e2d51ce249e0 100644 (file)
@@ -17,11 +17,13 @@ void ChunkTest::setUp() {
        types = BlockTypeRegistry();
 
        BlockType obstacle;
+       obstacle.name = "obstacle";
        obstacle.visible = true;
        obstacle.block_light = true;
        types.Add(std::move(obstacle));
 
        BlockType source;
+       source.name = "source";
        source.visible = true;
        source.luminosity = 5;
        source.block_light = true;