Style and small C++ mistakes fixed.

master
Quentin Bazin 2020-03-07 01:08:46 +01:00
parent 474cdd16c4
commit 8231b352f2
16 changed files with 60 additions and 67 deletions

View File

@ -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) \<pgimeno@users.noreply.notabug.org\>
* Nikola Schordinger (DeatHunter) \<Discord: DH#9367\>
* Kurt Spencer (K.jpg) \<https://github.com/KdotJPG\>
* Kurt Spencer (K.jpg) \<Discord: K.jpg#4154\>

View File

@ -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);
}

View File

@ -70,6 +70,7 @@ class Registry : public ISerializable {
Tree &registerTree(const std::string &stringID, const std::string &label);
Tree &registerSerializedTree(sf::Packet &packet);
Biome &registerBiome(const std::string &stringID, const std::string &label);
Biome &registerSerializedBiome(sf::Packet &packet);
@ -108,8 +109,8 @@ class Registry : public ISerializable {
std::unordered_map<std::string, u32> m_blocksID;
std::unordered_map<std::string, u32> m_itemsID;
std::unordered_map<std::string, u32> m_treesID;
std::unordered_map<std::string, u32> m_biomesID;
std::unordered_map<std::string, u16> m_treesID;
std::unordered_map<std::string, u16> m_biomesID;
enum class DataType {
Block,

View File

@ -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;
}

View File

@ -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<PlacementEntry::Flora> &getFlora() const { return m_flora; }
const std::vector<PlacementEntry::Ore> &getOres() const { return m_ores; }
const std::vector<PlacementEntry::Tree> &getTrees() const { return m_trees; }
void setParams(std::vector<double> &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<PlacementEntry::Flora> &value) { m_flora = value; }
void setOres(std::vector<PlacementEntry::Ore> &value) { m_ores = value; }
void setTrees(std::vector<PlacementEntry::Tree> &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<double> m_params;
u16 m_topBlockID;
u16 m_groundBlockID;
u16 m_deepBlockID;
u16 m_beachBlockID;
u16 m_liquidBlockID;
std::vector<PlacementEntry::Flora> m_flora;
std::vector<PlacementEntry::Ore> m_ores;
std::vector<PlacementEntry::Tree> m_trees;

View File

@ -27,9 +27,10 @@
#ifndef PLACEMENTENTRY_HPP_
#define PLACEMENTENTRY_HPP_
#include <gk/core/IntTypes.hpp>
#include <SFML/Network/Packet.hpp>
#include <gk/core/IntTypes.hpp>
#include "ISerializable.hpp"
namespace PlacementEntry {

View File

@ -24,10 +24,8 @@
*
* =====================================================================================
*/
#include <SFML/Network/Packet.hpp>
#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;
}

View File

@ -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_
#endif // TREE_HPP_

View File

@ -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"
}

View File

@ -31,3 +31,4 @@ mod:tree {
log_block = "default:oak_wood",
leaves_block = "default:oak_leaves"
}

View File

@ -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<double> 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());

View File

@ -63,8 +63,7 @@ void ScriptEngine::initUsertypes() {
);
m_lua.new_usertype<ServerWorld>("ServerWorld",
sol::base_classes, sol::bases<World>(),
"terrain_generator", &ServerWorld::terrainGenerator
sol::base_classes, sol::bases<World>()
);
m_lua.new_usertype<Chunk>("Chunk",

View File

@ -29,22 +29,17 @@
#include "TerrainBiomeSampler.hpp"
TerrainBiomeSampler::TerrainBiomeSampler() {
m_paramNoises = std::vector<FastNoise>(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<double> 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;
}

View File

@ -43,6 +43,7 @@ class TerrainBiomeSampler {
private:
static const u8 biomeParamCount = 2; // TODO if kept, should be defined in the worldtype, dynamically.
std::vector<FastNoise> m_paramNoises;
};

View File

@ -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;
}

View File

@ -28,6 +28,7 @@
#define TERRAINGENERATOR_HPP_
#include <gk/core/IntTypes.hpp>
#include <sol.hpp>
#include "TerrainBiomeSampler.hpp"