Skip to content

Commit

Permalink
Added iron ore generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
LordDeatHunter authored and Unarelith committed Feb 29, 2020
1 parent 03732e7 commit 0263387
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mods/default/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions server/include/world/TerrainGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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_
35 changes: 34 additions & 1 deletion server/source/world/TerrainGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void TerrainGenerator::setBlocksFromLuaTable(const sol::table &table) {
m_waterBlockID = Registry::getInstance().getBlockFromStringID(table["water"].get<std::string>()).id();
m_sandBlockID = Registry::getInstance().getBlockFromStringID(table["sand"].get<std::string>()).id();
m_tallgrassBlockID = Registry::getInstance().getBlockFromStringID(table["tallgrass"].get<std::string>()).id();
m_ironOreBlockID = Registry::getInstance().getBlockFromStringID(table["iron_ore"].get<std::string>()).id();
}

void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const {
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -172,4 +206,3 @@ float TerrainGenerator::noise3d_abs(double x, double y, double z, int octaves, f

return sum;
}

0 comments on commit 0263387

Please sign in to comment.