From 0263387e3563b90e5127f9fd896e17b9eb12aec7 Mon Sep 17 00:00:00 2001 From: Nikola Schrodinger Date: Sat, 29 Feb 2020 01:36:10 +0100 Subject: [PATCH] Added iron ore generation. --- mods/default/init.lua | 3 +- server/include/world/TerrainGenerator.hpp | 2 ++ server/source/world/TerrainGenerator.cpp | 35 ++++++++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/mods/default/init.lua b/mods/default/init.lua index fea74a8df..3b3e4b194 100644 --- a/mods/default/init.lua +++ b/mods/default/init.lua @@ -39,7 +39,8 @@ openminer:world():terrain_generator():set_blocks({ flower = "default:flower", water = "default:water", sand = "default:sand", - tallgrass = "default:tallgrass" + tallgrass = "default:tallgrass", + iron_ore = "default:iron_ore" }) function init(player) diff --git a/server/include/world/TerrainGenerator.hpp b/server/include/world/TerrainGenerator.hpp index 5ed950192..a3e7d1bb8 100644 --- a/server/include/world/TerrainGenerator.hpp +++ b/server/include/world/TerrainGenerator.hpp @@ -44,6 +44,7 @@ class TerrainGenerator { private: void fastNoiseGeneration(ServerChunk &chunk) const; + void oreFloodFill(ServerChunk &chunk, double x, double y, double z, u16 toReplace, u16 replaceWith, int depth) const; static float noise2d(double x, double y, int octaves, float persistence); static float noise3d_abs(double x, double y, double z, int octaves, float persistence); @@ -56,6 +57,7 @@ class TerrainGenerator { u16 m_waterBlockID = 0; u16 m_sandBlockID = 0; u16 m_tallgrassBlockID = 0; + u16 m_ironOreBlockID = 0; }; #endif // TERRAINGENERATOR_HPP_ diff --git a/server/source/world/TerrainGenerator.cpp b/server/source/world/TerrainGenerator.cpp index b3a085280..7ff4049c5 100644 --- a/server/source/world/TerrainGenerator.cpp +++ b/server/source/world/TerrainGenerator.cpp @@ -50,6 +50,7 @@ void TerrainGenerator::setBlocksFromLuaTable(const sol::table &table) { m_waterBlockID = Registry::getInstance().getBlockFromStringID(table["water"].get()).id(); m_sandBlockID = Registry::getInstance().getBlockFromStringID(table["sand"].get()).id(); m_tallgrassBlockID = Registry::getInstance().getBlockFromStringID(table["tallgrass"].get()).id(); + m_ironOreBlockID = Registry::getInstance().getBlockFromStringID(table["iron_ore"].get()).id(); } void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const { @@ -119,6 +120,8 @@ void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const { chunk.setBlockRaw(x, y, z, m_sandBlockID); else if (z + chunk.z() * CHUNK_HEIGHT > h - 3) chunk.setBlockRaw(x, y, z, m_dirtBlockID); + else if ((rand() % 4096) == 0) + oreFloodFill(chunk, x, y, z, m_stoneBlockID, m_ironOreBlockID, 2); else chunk.setBlockRaw(x, y, z, m_stoneBlockID); @@ -145,6 +148,37 @@ void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const { } } +void TerrainGenerator::oreFloodFill(ServerChunk &chunk, double x, double y, double z, u16 toReplace, u16 replaceWith, int depth) const { + if (depth < 0) return; + if (chunk.getBlock(x, y, z) == replaceWith) return; + if (chunk.getBlock(x, y, z) == toReplace) + chunk.setBlockRaw(x, y, z, replaceWith); + + oreFloodFill(chunk, x + 1, y, z, toReplace, replaceWith, depth - 1); + oreFloodFill(chunk, x - 1, y, z, toReplace, replaceWith, depth - 1); + oreFloodFill(chunk, x, y + 1, z, toReplace, replaceWith, depth - 1); + oreFloodFill(chunk, x, y - 1, z, toReplace, replaceWith, depth - 1); + oreFloodFill(chunk, x, y, z + 1, toReplace, replaceWith, depth - 1); + oreFloodFill(chunk, x, y, z - 1, toReplace, replaceWith, depth - 1); + + if (rand() % 15 == 0) + oreFloodFill(chunk, x + 1, y + 1, z + 1, toReplace, replaceWith, depth - 1); + if (rand() % 15 == 0) + oreFloodFill(chunk, x + 1, y + 1, z - 1, toReplace, replaceWith, depth - 1); + if (rand() % 15 == 0) + oreFloodFill(chunk, x + 1, y - 1, z + 1, toReplace, replaceWith, depth - 1); + if (rand() % 15 == 0) + oreFloodFill(chunk, x + 1, y - 1, z - 1, toReplace, replaceWith, depth - 1); + if (rand() % 15 == 0) + oreFloodFill(chunk, x - 1, y + 1, z + 1, toReplace, replaceWith, depth - 1); + if (rand() % 15 == 0) + oreFloodFill(chunk, x - 1, y + 1, z - 1, toReplace, replaceWith, depth - 1); + if (rand() % 15 == 0) + oreFloodFill(chunk, x - 1, y - 1, z + 1, toReplace, replaceWith, depth - 1); + if (rand() % 15 == 0) + oreFloodFill(chunk, x - 1, y - 1, z - 1, toReplace, replaceWith, depth - 1); +} + float TerrainGenerator::noise2d(double x, double y, int octaves, float persistence) { float sum = 0; float strength = 1.0; @@ -172,4 +206,3 @@ float TerrainGenerator::noise3d_abs(double x, double y, double z, int octaves, f return sum; } -