From 8231b352f2a99ad14b7e3b22b5162069db794807 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Sat, 7 Mar 2020 01:08:46 +0100 Subject: [PATCH] Style and small C++ mistakes fixed. --- CONTRIBUTORS.md | 2 +- common/source/core/Registry.cpp | 5 ++++- common/source/core/Registry.hpp | 5 +++-- common/source/world/Biome.cpp | 9 ++++++-- common/source/world/Biome.hpp | 25 ++++++--------------- common/source/world/PlacementEntry.hpp | 3 ++- common/source/world/Tree.cpp | 5 ++--- common/source/world/Tree.hpp | 5 ++--- mods/default/biomes.lua | 16 +++++-------- mods/default/trees.lua | 1 + server/source/lua/LuaMod.cpp | 7 ++---- server/source/lua/ScriptEngine.cpp | 3 +-- server/source/world/TerrainBiomeSampler.cpp | 20 +++++++---------- server/source/world/TerrainBiomeSampler.hpp | 1 + server/source/world/TerrainGenerator.cpp | 19 +++++++++++----- server/source/world/TerrainGenerator.hpp | 1 + 16 files changed, 60 insertions(+), 67 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 19ba1a17..1b3c11dc 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,4 +1,4 @@ The following people have contributed code to this project and hold the copyright on some portions (see the commit history for details): * Pedro Gimeno Fortea (pgimeno) \ * Nikola Schordinger (DeatHunter) \ -* Kurt Spencer (K.jpg) \ \ No newline at end of file +* Kurt Spencer (K.jpg) \ diff --git a/common/source/core/Registry.cpp b/common/source/core/Registry.cpp index d9488133..15311d06 100644 --- a/common/source/core/Registry.cpp +++ b/common/source/core/Registry.cpp @@ -49,7 +49,6 @@ Item &Registry::registerSerializedItem(sf::Packet &packet) { return m_items.back(); } - Tree &Registry::registerTree(const std::string &stringID, const std::string &label) { size_t id = m_trees.size(); m_treesID.emplace(stringID, id); @@ -103,18 +102,22 @@ const Item &Registry::getItemFromStringID(const std::string &stringID) { const Tree &Registry::getTreeFromStringID(const std::string &stringID) { if (stringID.empty()) throw EXCEPTION("Trying to get tree from empty string ID."); + auto it = m_treesID.find(stringID); if (it == m_treesID.end()) throw EXCEPTION("Unknown tree:", stringID); + return getTree(it->second); } const Biome &Registry::getBiomeFromStringID(const std::string &stringID) { if (stringID.empty()) throw EXCEPTION("Trying to get tree from empty string ID."); + auto it = m_biomesID.find(stringID); if (it == m_biomesID.end()) throw EXCEPTION("Unknown tree:", stringID); + return getBiome(it->second); } diff --git a/common/source/core/Registry.hpp b/common/source/core/Registry.hpp index 47aeeec1..e28fe349 100644 --- a/common/source/core/Registry.hpp +++ b/common/source/core/Registry.hpp @@ -70,6 +70,7 @@ class Registry : public ISerializable { Tree ®isterTree(const std::string &stringID, const std::string &label); Tree ®isterSerializedTree(sf::Packet &packet); + Biome ®isterBiome(const std::string &stringID, const std::string &label); Biome ®isterSerializedBiome(sf::Packet &packet); @@ -108,8 +109,8 @@ class Registry : public ISerializable { std::unordered_map m_blocksID; std::unordered_map m_itemsID; - std::unordered_map m_treesID; - std::unordered_map m_biomesID; + std::unordered_map m_treesID; + std::unordered_map m_biomesID; enum class DataType { Block, diff --git a/common/source/world/Biome.cpp b/common/source/world/Biome.cpp index fccd8a80..fc8b83e8 100644 --- a/common/source/world/Biome.cpp +++ b/common/source/world/Biome.cpp @@ -36,9 +36,14 @@ Biome::Biome(u16 id, const std::string &stringID, const std::string &label) { } void Biome::serialize(sf::Packet &packet) const { - packet << m_id << m_stringID << m_label << m_params << m_topBlockID << m_groundBlockID << m_deepBlockID << m_beachBlockID << m_liquidBlockID << m_flora << m_ores << m_trees; + packet << m_id << m_stringID << m_label << m_params + << m_topBlockID << m_groundBlockID << m_deepBlockID << m_beachBlockID << m_liquidBlockID + << m_flora << m_ores << m_trees; } void Biome::deserialize(sf::Packet &packet) { - packet >> m_id >> m_stringID >> m_label >> m_params >> m_topBlockID >> m_groundBlockID >> m_deepBlockID >> m_beachBlockID >> m_liquidBlockID >> m_flora >> m_ores >> m_trees; + packet >> m_id >> m_stringID >> m_label >> m_params + >> m_topBlockID >> m_groundBlockID >> m_deepBlockID >> m_beachBlockID >> m_liquidBlockID + >> m_flora >> m_ores >> m_trees; } + diff --git a/common/source/world/Biome.hpp b/common/source/world/Biome.hpp index 145f5aa5..5246e123 100644 --- a/common/source/world/Biome.hpp +++ b/common/source/world/Biome.hpp @@ -55,35 +55,22 @@ class Biome : public ISerializable { u16 getDeepBlockID() const { return m_deepBlockID; } u16 getBeachBlockID() const { return m_beachBlockID; } u16 getLiquidBlockID() const { return m_liquidBlockID; } + const std::vector &getFlora() const { return m_flora; } const std::vector &getOres() const { return m_ores; } const std::vector &getTrees() const { return m_trees; } - void setParams(std::vector &value) { m_params = value; } + void addParameter(double parameter) { m_params.emplace_back(parameter); } void setTopBlockID(u16 value) { m_topBlockID = value; } void setGroundBlockID(u16 value) { m_groundBlockID = value; } void setDeepBlockID(u16 value) { m_deepBlockID = value; } void setBeachBlockID(u16 value) { m_beachBlockID = value; } void setLiquidBlockID(u16 value) { m_liquidBlockID = value; } - void setFlora(std::vector &value) { m_flora = value; } - void setOres(std::vector &value) { m_ores = value; } - void setTrees(std::vector &value) { m_trees = value; } - PlacementEntry::Flora &addFlora() { - m_flora.emplace_back(); - return m_flora.back(); - } - - PlacementEntry::Ore &addOre() { - m_ores.emplace_back(); - return m_ores.back(); - } - - PlacementEntry::Tree &addTree() { - m_trees.emplace_back(); - return m_trees.back(); - } + PlacementEntry::Flora &addFlora() { m_flora.emplace_back(); return m_flora.back(); } + PlacementEntry::Ore &addOre() { m_ores.emplace_back(); return m_ores.back(); } + PlacementEntry::Tree &addTree() { m_trees.emplace_back(); return m_trees.back(); } private: u16 m_id; @@ -92,11 +79,13 @@ class Biome : public ISerializable { // TODO something to distinguish the worldtype of biome std::vector m_params; + u16 m_topBlockID; u16 m_groundBlockID; u16 m_deepBlockID; u16 m_beachBlockID; u16 m_liquidBlockID; + std::vector m_flora; std::vector m_ores; std::vector m_trees; diff --git a/common/source/world/PlacementEntry.hpp b/common/source/world/PlacementEntry.hpp index bdafc955..c975f76d 100644 --- a/common/source/world/PlacementEntry.hpp +++ b/common/source/world/PlacementEntry.hpp @@ -27,9 +27,10 @@ #ifndef PLACEMENTENTRY_HPP_ #define PLACEMENTENTRY_HPP_ -#include #include +#include + #include "ISerializable.hpp" namespace PlacementEntry { diff --git a/common/source/world/Tree.cpp b/common/source/world/Tree.cpp index fe75fbe3..e8a02609 100644 --- a/common/source/world/Tree.cpp +++ b/common/source/world/Tree.cpp @@ -24,10 +24,8 @@ * * ===================================================================================== */ -#include - -#include "Tree.hpp" #include "NetworkUtils.hpp" +#include "Tree.hpp" Tree::Tree(u16 id, const std::string &stringID, const std::string &label) { m_id = id; @@ -42,3 +40,4 @@ void Tree::serialize(sf::Packet &packet) const { void Tree::deserialize(sf::Packet &packet) { packet >> m_id >> m_stringID >> m_label >> m_logBlockID >> m_leavesBlockID; } + diff --git a/common/source/world/Tree.hpp b/common/source/world/Tree.hpp index 023afb36..d6b80016 100644 --- a/common/source/world/Tree.hpp +++ b/common/source/world/Tree.hpp @@ -33,8 +33,7 @@ #include "ISerializable.hpp" -class Tree : public ISerializable -{ +class Tree : public ISerializable { public: Tree() = default; Tree(u16 id, const std::string &stringID, const std::string &label); @@ -62,4 +61,4 @@ class Tree : public ISerializable u16 m_leavesBlockID; }; -#endif // TREE_HPP_ \ No newline at end of file +#endif // TREE_HPP_ diff --git a/mods/default/biomes.lua b/mods/default/biomes.lua index 15277d03..6ae3bd84 100644 --- a/mods/default/biomes.lua +++ b/mods/default/biomes.lua @@ -39,7 +39,7 @@ mod:biome { deep_block = "default:stone", beach_block = "default:sand", liquid_block = "default:water", - + trees = { { type = "default:oak", @@ -59,7 +59,7 @@ mod:biome { size = 8 } }, - + flora = { { block = "default:tallgrass", @@ -69,7 +69,7 @@ mod:biome { { block = "default:dandelion", spawns_on = "default:grass", - probability = 0.1 + probability = 0.025 } } } @@ -86,15 +86,8 @@ mod:biome { top_block = "default:stone", ground_block = "default:stone", deep_block = "default:stone", - beach_block = "default:stone", + beach_block = "default:sand", liquid_block = "default:water", - - trees = { - { - type = "default:oak", - probability = 0.00390625 - } - } } mod:biome { @@ -112,3 +105,4 @@ mod:biome { beach_block = "default:sand", liquid_block = "default:water" } + diff --git a/mods/default/trees.lua b/mods/default/trees.lua index 08334099..b48a71d5 100644 --- a/mods/default/trees.lua +++ b/mods/default/trees.lua @@ -31,3 +31,4 @@ mod:tree { log_block = "default:oak_wood", leaves_block = "default:oak_leaves" } + diff --git a/server/source/lua/LuaMod.cpp b/server/source/lua/LuaMod.cpp index c23cdfad..67bbc2c2 100644 --- a/server/source/lua/LuaMod.cpp +++ b/server/source/lua/LuaMod.cpp @@ -163,11 +163,8 @@ void LuaMod::registerBiome(const sol::table &table) { // TODO eventually a WorldType could have a list of biome parameter names in order, // and we could use those as the ordered keys. // Currently hardcoding "temperature" and "precipitation" to get something functional. - size_t nBiomeParams = 2; - std::vector params(nBiomeParams); - params[0] = table["params"]["temperature"]; - params[1] = table["params"]["precipitation"]; - biome.setParams(params); + biome.addParameter(table["params"]["temperature"]); + biome.addParameter(table["params"]["precipitation"]); biome.setTopBlockID(Registry::getInstance().getBlockFromStringID(table["top_block"]).id()); biome.setGroundBlockID(Registry::getInstance().getBlockFromStringID(table["ground_block"]).id()); diff --git a/server/source/lua/ScriptEngine.cpp b/server/source/lua/ScriptEngine.cpp index 13f2c62f..1386c4b5 100644 --- a/server/source/lua/ScriptEngine.cpp +++ b/server/source/lua/ScriptEngine.cpp @@ -63,8 +63,7 @@ void ScriptEngine::initUsertypes() { ); m_lua.new_usertype("ServerWorld", - sol::base_classes, sol::bases(), - "terrain_generator", &ServerWorld::terrainGenerator + sol::base_classes, sol::bases() ); m_lua.new_usertype("Chunk", diff --git a/server/source/world/TerrainBiomeSampler.cpp b/server/source/world/TerrainBiomeSampler.cpp index 2cef9d3f..dadbe471 100644 --- a/server/source/world/TerrainBiomeSampler.cpp +++ b/server/source/world/TerrainBiomeSampler.cpp @@ -29,22 +29,17 @@ #include "TerrainBiomeSampler.hpp" TerrainBiomeSampler::TerrainBiomeSampler() { - m_paramNoises = std::vector(biomeParamCount); for (u8 i = 0; i < biomeParamCount; i++) { - m_paramNoises[i].SetNoiseType(FastNoise::NoiseType::SimplexFractal); - m_paramNoises[i].SetFrequency(1 / 800.0f); - m_paramNoises[i].SetFractalOctaves(5); - m_paramNoises[i].SetSeed(i); + m_paramNoises.emplace_back(); + + m_paramNoises.back().SetNoiseType(FastNoise::NoiseType::SimplexFractal); + m_paramNoises.back().SetFrequency(1 / 800.0f); + m_paramNoises.back().SetFractalOctaves(5); + m_paramNoises.back().SetSeed(i); } } u16 TerrainBiomeSampler::getBiomeIndexAt(s32 x, s32 y) const { - // Compute noise instances - std::vector biomeParams(biomeParamCount); - for (u8 i = 0; i < biomeParamCount; i++) { - biomeParams[i] = m_paramNoises[i].GetNoise(x, y); - } - // TODO with a lot of biomes, perhaps we want an R-Tree or similar, instead of a long loop. // Should also finish solving for analytic blending, or find completely separate solution such as isotropically-modified genlayer // If we continue with temp/precip/etc params, need to write a weighted lloyd smoother so biomes becone fairly represented. @@ -56,7 +51,7 @@ u16 TerrainBiomeSampler::getBiomeIndexAt(s32 x, s32 y) const { for (auto &biome : Registry::getInstance().biomes()) { double deviation = 0; for (int i = 0; i < biomeParamCount; i++) { - double dp = biomeParams[i] - biome.getParams()[i]; + double dp = m_paramNoises[i].GetNoise(x, y) - biome.getParams()[i]; deviation += dp * dp; } if (deviation < decidedBiomeDeviation) { @@ -68,3 +63,4 @@ u16 TerrainBiomeSampler::getBiomeIndexAt(s32 x, s32 y) const { return decidedBiomeIndex; } + diff --git a/server/source/world/TerrainBiomeSampler.hpp b/server/source/world/TerrainBiomeSampler.hpp index 9ff92805..9deb52e1 100644 --- a/server/source/world/TerrainBiomeSampler.hpp +++ b/server/source/world/TerrainBiomeSampler.hpp @@ -43,6 +43,7 @@ class TerrainBiomeSampler { private: static const u8 biomeParamCount = 2; // TODO if kept, should be defined in the worldtype, dynamically. + std::vector m_paramNoises; }; diff --git a/server/source/world/TerrainGenerator.cpp b/server/source/world/TerrainGenerator.cpp index 3c3c94b1..81ede444 100644 --- a/server/source/world/TerrainGenerator.cpp +++ b/server/source/world/TerrainGenerator.cpp @@ -50,7 +50,6 @@ void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const { Chunk *topChunk = chunk.getSurroundingChunk(Chunk::Top); for(int y = 0 ; y < CHUNK_DEPTH ; y++) { for(int x = 0 ; x < CHUNK_WIDTH ; x++) { - u16 biomeIndex = biomeSampler.getBiomeIndexAt(x + chunk.x() * CHUNK_WIDTH, y + chunk.y() * CHUNK_DEPTH); const Biome &biome = Registry::getInstance().getBiome(biomeIndex); @@ -65,7 +64,6 @@ void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const { for(int z = 0 ; z < CHUNK_HEIGHT ; z++) { // Are we above "ground" level? if(z + chunk.z() * CHUNK_HEIGHT > h) { - // If we are not yet up to sea level, fill with water blocks if (z + chunk.z() * CHUNK_HEIGHT < SEALEVEL) { chunk.setBlockRaw(x, y, z, biome.getLiquidBlockID()); @@ -73,9 +71,12 @@ void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const { // Otherwise we are in the air else { bool placedTree = false; + // Try to place a tree if (chunk.getBlock(x, y, z - 1) == biome.getTopBlockID()) { for (const PlacementEntry::Tree &treePlacement : biome.getTrees()) { - if (rand() > RAND_MAX * treePlacement.probability) continue; + if (rand() > RAND_MAX * treePlacement.probability) + continue; + const Tree &tree = Registry::getInstance().getTree(treePlacement.treeID); // Trunk @@ -107,8 +108,12 @@ void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const { if (!placedTree) { bool placedFlora = false; for (const PlacementEntry::Flora &flora : biome.getFlora()) { - if (chunk.getBlock(x, y, z - 1) != flora.spawnsOnBlockID) continue; - if (rand() > RAND_MAX * flora.probability) continue; + if (chunk.getBlock(x, y, z - 1) != flora.spawnsOnBlockID) + continue; + + if (rand() > RAND_MAX * flora.probability) + continue; + chunk.setBlockRaw(x, y, z, flora.blockID); placedFlora = true; break; @@ -136,7 +141,9 @@ void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const { // This could be achieved either by setting up a generation pipeline with stages, // processing neighboring chunks' ores every time, or generating them with noise. for (const PlacementEntry::Ore &ore : biome.getOres()) { - if (rand() > RAND_MAX * ore.probability) continue; + if (rand() > RAND_MAX * ore.probability) + continue; + oreFloodFill(chunk, x, y, z, biome.getDeepBlockID(), ore.blockID, 2); break; } diff --git a/server/source/world/TerrainGenerator.hpp b/server/source/world/TerrainGenerator.hpp index 4175cb87..5c5282aa 100644 --- a/server/source/world/TerrainGenerator.hpp +++ b/server/source/world/TerrainGenerator.hpp @@ -28,6 +28,7 @@ #define TERRAINGENERATOR_HPP_ #include + #include #include "TerrainBiomeSampler.hpp"