mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-22 18:39:46 +00:00
cleanup WorldGenerator
This commit is contained in:
parent
cfdc8672ec
commit
35e20c7dd4
2 changed files with 53 additions and 104 deletions
|
|
@ -1,8 +1,5 @@
|
||||||
#include "WorldGenerator.hpp"
|
#include "WorldGenerator.hpp"
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include "maths/util.hpp"
|
#include "maths/util.hpp"
|
||||||
#include "content/Content.hpp"
|
#include "content/Content.hpp"
|
||||||
#include "voxels/Block.hpp"
|
#include "voxels/Block.hpp"
|
||||||
|
|
@ -15,6 +12,9 @@
|
||||||
#include "maths/util.hpp"
|
#include "maths/util.hpp"
|
||||||
#include "debug/Logger.hpp"
|
#include "debug/Logger.hpp"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
static debug::Logger logger("world-generator");
|
static debug::Logger logger("world-generator");
|
||||||
|
|
||||||
/// @brief Max number of biome parameters
|
/// @brief Max number of biome parameters
|
||||||
|
|
@ -49,7 +49,7 @@ WorldGenerator::WorldGenerator(
|
||||||
if (prototypes.find({x, z}) != prototypes.end()) {
|
if (prototypes.find({x, z}) != prototypes.end()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
prototypes[{x, z}] = generatePrototype(x, z);
|
prototypes[{x, z}] = generatePrototype();
|
||||||
});
|
});
|
||||||
surroundMap.setLevelCallback(def.wideStructsChunksRadius + 1,
|
surroundMap.setLevelCallback(def.wideStructsChunksRadius + 1,
|
||||||
[this](int const x, int const z) {
|
[this](int const x, int const z) {
|
||||||
|
|
@ -144,9 +144,7 @@ static inline const Biome* choose_biome(
|
||||||
return chosenBiome;
|
return chosenBiome;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<ChunkPrototype> WorldGenerator::generatePrototype(
|
std::unique_ptr<ChunkPrototype> WorldGenerator::generatePrototype() {
|
||||||
int chunkX, int chunkZ
|
|
||||||
) {
|
|
||||||
return std::make_unique<ChunkPrototype>();
|
return std::make_unique<ChunkPrototype>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -237,21 +235,24 @@ void WorldGenerator::placeBlock(const BlockPlacement& block, int priority) {
|
||||||
for (int cz = cza; cz <= czb; cz++) {
|
for (int cz = cza; cz <= czb; cz++) {
|
||||||
for (int cx = cxa; cx <= cxb; cx++) {
|
for (int cx = cxa; cx <= cxb; cx++) {
|
||||||
const auto& found = prototypes.find({cx, cz});
|
const auto& found = prototypes.find({cx, cz});
|
||||||
if (found != prototypes.end()) {
|
if (found == prototypes.end()) {
|
||||||
// position becomes relative to prototype chunk
|
continue;
|
||||||
glm::ivec3 rel = block.position - glm::ivec3(cx * CHUNK_W, 0, cz * CHUNK_D);
|
|
||||||
bool owner = (cx == floordiv<CHUNK_W>(block.position.x)) && (cz == floordiv<CHUNK_D>(block.position.z));
|
|
||||||
found->second->placements.emplace_back(priority, BlockPlacement{block.block, rel, block.rotation, !owner});
|
|
||||||
}
|
}
|
||||||
|
// position becomes relative to prototype chunk
|
||||||
|
glm::ivec3 rel =
|
||||||
|
block.position - glm::ivec3(cx * CHUNK_W, 0, cz * CHUNK_D);
|
||||||
|
bool owner = (cx == floordiv<CHUNK_W>(block.position.x)) &&
|
||||||
|
(cz == floordiv<CHUNK_D>(block.position.z));
|
||||||
|
found->second->placements.emplace_back(
|
||||||
|
priority,
|
||||||
|
BlockPlacement {block.block, rel, block.rotation, !owner}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldGenerator::placeStructures(
|
void WorldGenerator::placeStructures(
|
||||||
const std::vector<Placement>& placements,
|
const std::vector<Placement>& placements, int chunkX, int chunkZ
|
||||||
ChunkPrototype& prototype,
|
|
||||||
int chunkX,
|
|
||||||
int chunkZ
|
|
||||||
) {
|
) {
|
||||||
for (const auto& placement : placements) {
|
for (const auto& placement : placements) {
|
||||||
if (auto sp = std::get_if<StructurePlacement>(&placement.placement)) {
|
if (auto sp = std::get_if<StructurePlacement>(&placement.placement)) {
|
||||||
|
|
@ -278,7 +279,7 @@ void WorldGenerator::generateStructuresWide(
|
||||||
auto placements = def.script->placeStructuresWide(
|
auto placements = def.script->placeStructuresWide(
|
||||||
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, CHUNK_H
|
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, CHUNK_H
|
||||||
);
|
);
|
||||||
placeStructures(placements, prototype, chunkX, chunkZ);
|
placeStructures(placements, chunkX, chunkZ);
|
||||||
|
|
||||||
prototype.level = ChunkPrototypeLevel::WIDE_STRUCTS;
|
prototype.level = ChunkPrototypeLevel::WIDE_STRUCTS;
|
||||||
}
|
}
|
||||||
|
|
@ -296,7 +297,7 @@ void WorldGenerator::generateStructures(
|
||||||
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D},
|
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D},
|
||||||
heightmap, CHUNK_H
|
heightmap, CHUNK_H
|
||||||
);
|
);
|
||||||
placeStructures(placements, prototype, chunkX, chunkZ);
|
placeStructures(placements, chunkX, chunkZ);
|
||||||
|
|
||||||
util::PseudoRandom structsRand;
|
util::PseudoRandom structsRand;
|
||||||
structsRand.setSeed(chunkX, chunkZ);
|
structsRand.setSeed(chunkX, chunkZ);
|
||||||
|
|
@ -405,12 +406,7 @@ void WorldGenerator::update(int centerX, int centerY, int loadDistance) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldGenerator::generatePlants(
|
void WorldGenerator::generatePlants(
|
||||||
const ChunkPrototype& prototype,
|
float* heights, voxel* voxels, int chunkX, int chunkZ, const Biome** biomes
|
||||||
float* heights,
|
|
||||||
voxel* voxels,
|
|
||||||
int chunkX,
|
|
||||||
int chunkZ,
|
|
||||||
const Biome** biomes
|
|
||||||
) {
|
) {
|
||||||
const auto& indices = content.getIndices()->blocks;
|
const auto& indices = content.getIndices()->blocks;
|
||||||
util::PseudoRandom plantsRand;
|
util::PseudoRandom plantsRand;
|
||||||
|
|
@ -447,12 +443,7 @@ void WorldGenerator::generatePlants(
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldGenerator::generateLand(
|
void WorldGenerator::generateLand(
|
||||||
const ChunkPrototype& prototype,
|
float* values, voxel* voxels, const Biome** biomes
|
||||||
float* values,
|
|
||||||
voxel* voxels,
|
|
||||||
int chunkX,
|
|
||||||
int chunkZ,
|
|
||||||
const Biome** biomes
|
|
||||||
) {
|
) {
|
||||||
uint seaLevel = def.seaLevel;
|
uint seaLevel = def.seaLevel;
|
||||||
for (uint z = 0; z < CHUNK_D; z++) {
|
for (uint z = 0; z < CHUNK_D; z++) {
|
||||||
|
|
@ -477,27 +468,13 @@ void WorldGenerator::generate(voxel* voxels, int chunkX, int chunkZ) {
|
||||||
const auto& prototype = requirePrototype(chunkX, chunkZ);
|
const auto& prototype = requirePrototype(chunkX, chunkZ);
|
||||||
const auto values = prototype.heightmap->getValues();
|
const auto values = prototype.heightmap->getValues();
|
||||||
|
|
||||||
uint seaLevel = def.seaLevel;
|
|
||||||
|
|
||||||
std::memset(voxels, 0, sizeof(voxel) * CHUNK_VOL);
|
std::memset(voxels, 0, sizeof(voxel) * CHUNK_VOL);
|
||||||
|
|
||||||
const auto& biomes = prototype.biomes.get();
|
const auto& biomes = prototype.biomes.get();
|
||||||
for (uint z = 0; z < CHUNK_D; z++) {
|
|
||||||
for (uint x = 0; x < CHUNK_W; x++) {
|
|
||||||
const Biome* biome = biomes[z * CHUNK_W + x];
|
|
||||||
|
|
||||||
int height = values[z * CHUNK_W + x] * CHUNK_H;
|
generateLand(values, voxels, biomes);
|
||||||
height = std::max(0, height);
|
|
||||||
|
|
||||||
const auto& groundLayers = biome->groundLayers;
|
|
||||||
const auto& seaLayers = biome->seaLayers;
|
|
||||||
|
|
||||||
generate_pole(seaLayers, seaLevel, height, seaLevel, voxels, x, z);
|
|
||||||
generate_pole(groundLayers, height, 0, seaLevel, voxels, x, z);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
generatePlacements(prototype, voxels, chunkX, chunkZ);
|
generatePlacements(prototype, voxels, chunkX, chunkZ);
|
||||||
generatePlants(prototype, values, voxels, chunkX, chunkZ, biomes);
|
generatePlants(values, voxels, chunkX, chunkZ, biomes);
|
||||||
|
|
||||||
[[maybe_unused]] const auto& indices = content.getIndices()->blocks;
|
[[maybe_unused]] const auto& indices = content.getIndices()->blocks;
|
||||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||||
|
|
@ -526,20 +503,17 @@ void WorldGenerator::generatePlacements(
|
||||||
);
|
);
|
||||||
for (const auto& placement : placements) {
|
for (const auto& placement : placements) {
|
||||||
if (auto structure = std::get_if<StructurePlacement>(&placement.placement)) {
|
if (auto structure = std::get_if<StructurePlacement>(&placement.placement)) {
|
||||||
generateStructure(prototype, *structure, voxels, chunkX, chunkZ);
|
generateStructure(*structure, voxels);
|
||||||
} else if (auto line = std::get_if<LinePlacement>(&placement.placement)) {
|
} else if (auto line = std::get_if<LinePlacement>(&placement.placement)) {
|
||||||
generateLine(prototype, *line, voxels, chunkX, chunkZ);
|
generateLine(*line, voxels, chunkX, chunkZ);
|
||||||
} else if (auto block = std::get_if<BlockPlacement>(&placement.placement)) {
|
} else if (auto block = std::get_if<BlockPlacement>(&placement.placement)) {
|
||||||
generateBlock(prototype, *block, voxels, chunkX, chunkZ);
|
generateBlock(*block, voxels);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldGenerator::generateStructure(
|
void WorldGenerator::generateStructure(
|
||||||
const ChunkPrototype& prototype,
|
const StructurePlacement& placement, voxel* voxels
|
||||||
const StructurePlacement& placement,
|
|
||||||
voxel* voxels,
|
|
||||||
int chunkX, int chunkZ
|
|
||||||
) {
|
) {
|
||||||
if (placement.structure < 0 || placement.structure >= def.structures.size()) {
|
if (placement.structure < 0 || placement.structure >= def.structures.size()) {
|
||||||
logger.error() << "invalid structure index " << placement.structure;
|
logger.error() << "invalid structure index " << placement.structure;
|
||||||
|
|
@ -577,10 +551,7 @@ void WorldGenerator::generateStructure(
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldGenerator::generateLine(
|
void WorldGenerator::generateLine(
|
||||||
const ChunkPrototype& prototype,
|
const LinePlacement& line, voxel* voxels, int chunkX, int chunkZ
|
||||||
const LinePlacement& line,
|
|
||||||
voxel* voxels,
|
|
||||||
int chunkX, int chunkZ
|
|
||||||
) {
|
) {
|
||||||
const auto& indices = content.getIndices()->blocks;
|
const auto& indices = content.getIndices()->blocks;
|
||||||
|
|
||||||
|
|
@ -610,26 +581,26 @@ void WorldGenerator::generateLine(
|
||||||
glm::ivec3 closest = util::closest_point_on_segment(
|
glm::ivec3 closest = util::closest_point_on_segment(
|
||||||
a, b, point
|
a, b, point
|
||||||
);
|
);
|
||||||
if (y > 0 &&
|
if (y <= 0 || util::distance2(closest, point) > radius * radius) {
|
||||||
util::distance2(closest, point) <= radius * radius
|
continue;
|
||||||
) {
|
}
|
||||||
auto& voxel = voxels[vox_index(x, y, z)];
|
auto& voxel = voxels[vox_index(x, y, z)];
|
||||||
if (line.block != BLOCK_AIR) {
|
if (line.block != BLOCK_AIR) {
|
||||||
voxel = {line.block, {}};
|
voxel = {line.block, {}};
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!indices.require(voxel.id).replaceable) {
|
if (!indices.require(voxel.id).replaceable) {
|
||||||
voxel = {line.block, {}};
|
voxel = {line.block, {}};
|
||||||
}
|
}
|
||||||
auto& below = voxels[vox_index(x, y-1, z)];
|
auto& below = voxels[vox_index(x, y-1, z)];
|
||||||
glm::ivec3 closest2 = util::closest_point_on_segment(
|
glm::ivec3 closest2 = util::closest_point_on_segment(
|
||||||
a, b, {gx, y-1, gz}
|
a, b, {gx, y-1, gz}
|
||||||
);
|
);
|
||||||
if (util::distance2(closest2, {gx, y-1, gz}) > radius*radius) {
|
if (util::distance2(closest2, {gx, y - 1, gz}) >
|
||||||
const auto& def = indices.require(below.id);
|
radius * radius) {
|
||||||
if (def.rt.surfaceReplacement != below.id) {
|
const auto& def = indices.require(below.id);
|
||||||
below = {def.rt.surfaceReplacement, {}};
|
if (def.rt.surfaceReplacement != below.id) {
|
||||||
}
|
below = {def.rt.surfaceReplacement, {}};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -638,10 +609,7 @@ void WorldGenerator::generateLine(
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldGenerator::generateBlock(
|
void WorldGenerator::generateBlock(
|
||||||
const ChunkPrototype& prototype,
|
const BlockPlacement& placement, voxel* voxels
|
||||||
const BlockPlacement& placement,
|
|
||||||
voxel* voxels,
|
|
||||||
int chunkX, int chunkZ
|
|
||||||
) {
|
) {
|
||||||
const auto& indices = content.getIndices()->blocks;
|
const auto& indices = content.getIndices()->blocks;
|
||||||
const auto& def = indices.require(placement.block);
|
const auto& def = indices.require(placement.block);
|
||||||
|
|
|
||||||
|
|
@ -59,9 +59,7 @@ class WorldGenerator {
|
||||||
SurroundMap surroundMap;
|
SurroundMap surroundMap;
|
||||||
|
|
||||||
/// @brief Generate chunk prototype (see ChunkPrototype)
|
/// @brief Generate chunk prototype (see ChunkPrototype)
|
||||||
/// @param x chunk position X divided by CHUNK_W
|
std::unique_ptr<ChunkPrototype> generatePrototype();
|
||||||
/// @param z chunk position Y divided by CHUNK_D
|
|
||||||
std::unique_ptr<ChunkPrototype> generatePrototype(int x, int z);
|
|
||||||
|
|
||||||
ChunkPrototype& requirePrototype(int x, int z);
|
ChunkPrototype& requirePrototype(int x, int z);
|
||||||
|
|
||||||
|
|
@ -85,25 +83,13 @@ class WorldGenerator {
|
||||||
const ChunkPrototype& prototype, voxel* voxels, int x, int z
|
const ChunkPrototype& prototype, voxel* voxels, int x, int z
|
||||||
);
|
);
|
||||||
void generateLine(
|
void generateLine(
|
||||||
const ChunkPrototype& prototype,
|
|
||||||
const LinePlacement& placement,
|
const LinePlacement& placement,
|
||||||
voxel* voxels,
|
voxel* voxels,
|
||||||
int x, int z
|
int x, int z
|
||||||
);
|
);
|
||||||
void generateBlock(
|
void generateBlock(const BlockPlacement& placement, voxel* voxels);
|
||||||
const ChunkPrototype& prototype,
|
void generateStructure(const StructurePlacement& placement, voxel* voxels);
|
||||||
const BlockPlacement& placement,
|
|
||||||
voxel* voxels,
|
|
||||||
int x, int z
|
|
||||||
);
|
|
||||||
void generateStructure(
|
|
||||||
const ChunkPrototype& prototype,
|
|
||||||
const StructurePlacement& placement,
|
|
||||||
voxel* voxels,
|
|
||||||
int x, int z
|
|
||||||
);
|
|
||||||
void generatePlants(
|
void generatePlants(
|
||||||
const ChunkPrototype& prototype,
|
|
||||||
float* values,
|
float* values,
|
||||||
voxel* voxels,
|
voxel* voxels,
|
||||||
int x,
|
int x,
|
||||||
|
|
@ -111,18 +97,13 @@ class WorldGenerator {
|
||||||
const Biome** biomes
|
const Biome** biomes
|
||||||
);
|
);
|
||||||
void generateLand(
|
void generateLand(
|
||||||
const ChunkPrototype& prototype,
|
|
||||||
float* values,
|
float* values,
|
||||||
voxel* voxels,
|
voxel* voxels,
|
||||||
int x,
|
|
||||||
int z,
|
|
||||||
const Biome** biomes
|
const Biome** biomes
|
||||||
);
|
);
|
||||||
|
|
||||||
void placeStructures(
|
void placeStructures(
|
||||||
const std::vector<Placement>& placements,
|
const std::vector<Placement>& placements, int x, int z
|
||||||
ChunkPrototype& prototype,
|
|
||||||
int x, int z
|
|
||||||
);
|
);
|
||||||
public:
|
public:
|
||||||
WorldGenerator(
|
WorldGenerator(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue