add placements priority
This commit is contained in:
parent
fb53b3941c
commit
b5c1050f43
@ -1,3 +1,3 @@
|
||||
[
|
||||
{"struct": "coal_ore0", "rarity": 22000}
|
||||
{"struct": "coal_ore0", "rarity": 4400}
|
||||
]
|
||||
|
||||
@ -17,7 +17,7 @@ local function place_ores(placements, x, z, w, d, seed, hmap, chunk_height)
|
||||
local sz = math.random() * d
|
||||
local sy = math.random() * (chunk_height * 0.5)
|
||||
if sy < hmap:at(sx, sz) * chunk_height - 6 then
|
||||
table.insert(placements, {ore.struct, {sx, sy, sz}, math.random()*4})
|
||||
table.insert(placements, {ore.struct, {sx, sy, sz}, math.random()*4, -1})
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -33,7 +33,7 @@ function place_structures_wide(x, z, w, d, seed, chunk_height)
|
||||
local placements = {}
|
||||
if math.random() < 0.05 then -- generate caves
|
||||
local sx = x + math.random() * 10 - 5
|
||||
local sy = math.random() * (chunk_height / 4) + 27
|
||||
local sy = math.random() * (chunk_height / 4) + 10
|
||||
local sz = z + math.random() * 10 - 5
|
||||
|
||||
local dir = math.random() * math.pi * 2
|
||||
|
||||
@ -84,7 +84,7 @@ public:
|
||||
return maps;
|
||||
}
|
||||
|
||||
void perform_line(lua::State* L, PrototypePlacements& placements) {
|
||||
void perform_line(lua::State* L, std::vector<Placement>& placements) {
|
||||
rawgeti(L, 2);
|
||||
blockid_t block = touinteger(L, -1);
|
||||
pop(L);
|
||||
@ -101,10 +101,17 @@ public:
|
||||
int radius = touinteger(L, -1);
|
||||
pop(L);
|
||||
|
||||
placements.lines.emplace_back(block, a, b, radius);
|
||||
int priority = 0;
|
||||
if (objlen(L, -1) >= 6) {
|
||||
rawgeti(L, 6);
|
||||
priority = tointeger(L, -1);
|
||||
pop(L);
|
||||
}
|
||||
|
||||
void perform_placement(lua::State* L, PrototypePlacements& placements) {
|
||||
placements.emplace_back(priority, LinePlacement {block, a, b, radius});
|
||||
}
|
||||
|
||||
void perform_placement(lua::State* L, std::vector<Placement>& placements) {
|
||||
rawgeti(L, 1);
|
||||
int structIndex = 0;
|
||||
if (isstring(L, -1)) {
|
||||
@ -129,19 +136,28 @@ public:
|
||||
pop(L);
|
||||
|
||||
rawgeti(L, 3);
|
||||
int rotation = tointeger(L, -1) & 0b11;
|
||||
uint8_t rotation = tointeger(L, -1) & 0b11;
|
||||
pop(L);
|
||||
|
||||
placements.structs.emplace_back(structIndex, pos, rotation);
|
||||
int priority = 1;
|
||||
if (objlen(L, -1) >= 4) {
|
||||
rawgeti(L, 4);
|
||||
priority = tointeger(L, -1);
|
||||
pop(L);
|
||||
}
|
||||
|
||||
PrototypePlacements placeStructuresWide(
|
||||
placements.emplace_back(
|
||||
priority, StructurePlacement {structIndex, pos, rotation}
|
||||
);
|
||||
}
|
||||
|
||||
std::vector<Placement> placeStructuresWide(
|
||||
const glm::ivec2& offset,
|
||||
const glm::ivec2& size,
|
||||
uint64_t seed,
|
||||
uint chunkHeight
|
||||
) override {
|
||||
PrototypePlacements placements {};
|
||||
std::vector<Placement> placements {};
|
||||
|
||||
stackguard _(L);
|
||||
pushenv(L, *env);
|
||||
@ -165,11 +181,11 @@ public:
|
||||
return placements;
|
||||
}
|
||||
|
||||
PrototypePlacements placeStructures(
|
||||
std::vector<Placement> placeStructures(
|
||||
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed,
|
||||
const std::shared_ptr<Heightmap>& heightmap, uint chunkHeight
|
||||
) override {
|
||||
PrototypePlacements placements {};
|
||||
std::vector<Placement> placements {};
|
||||
|
||||
stackguard _(L);
|
||||
pushenv(L, *env);
|
||||
|
||||
@ -118,11 +118,6 @@ struct Biome {
|
||||
BlocksLayers seaLayers;
|
||||
};
|
||||
|
||||
struct PrototypePlacements {
|
||||
std::vector<StructurePlacement> structs {};
|
||||
std::vector<LinePlacement> lines {};
|
||||
};
|
||||
|
||||
/// @brief Generator behaviour and settings interface
|
||||
class GeneratorScript {
|
||||
public:
|
||||
@ -154,7 +149,7 @@ public:
|
||||
uint bpd
|
||||
) = 0;
|
||||
|
||||
virtual PrototypePlacements placeStructuresWide(
|
||||
virtual std::vector<Placement> placeStructuresWide(
|
||||
const glm::ivec2& offset,
|
||||
const glm::ivec2& size,
|
||||
uint64_t seed,
|
||||
@ -169,7 +164,7 @@ public:
|
||||
/// @param heightmap area heightmap
|
||||
/// @param chunkHeight chunk height to use as heights multiplier
|
||||
/// @return structure & line placements
|
||||
virtual PrototypePlacements placeStructures(
|
||||
virtual std::vector<Placement> placeStructures(
|
||||
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed,
|
||||
const std::shared_ptr<Heightmap>& heightmap, uint chunkHeight) = 0;
|
||||
};
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <variant>
|
||||
#include <stdint.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
@ -25,3 +26,13 @@ struct LinePlacement {
|
||||
: block(block), a(std::move(a)), b(std::move(b)), radius(radius) {
|
||||
}
|
||||
};
|
||||
|
||||
struct Placement {
|
||||
int priority;
|
||||
std::variant<StructurePlacement, LinePlacement> placement;
|
||||
|
||||
Placement(
|
||||
int priority,
|
||||
std::variant<StructurePlacement, LinePlacement> placement
|
||||
) : priority(priority), placement(std::move(placement)) {}
|
||||
};
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include "WorldGenerator.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
|
||||
#include "maths/util.hpp"
|
||||
#include "content/Content.hpp"
|
||||
@ -147,34 +148,36 @@ inline AABB gen_chunk_aabb(int chunkX, int chunkZ) {
|
||||
}
|
||||
|
||||
void WorldGenerator::placeStructure(
|
||||
const glm::ivec3& offset, size_t structureId, uint8_t rotation,
|
||||
const StructurePlacement& placement, int priority,
|
||||
int chunkX, int chunkZ
|
||||
) {
|
||||
auto& structure = *def.structures[structureId]->fragments[rotation];
|
||||
auto position = glm::ivec3(chunkX * CHUNK_W, 0, chunkZ * CHUNK_D)+offset;
|
||||
auto& structure =
|
||||
*def.structures[placement.structure]->fragments[placement.rotation];
|
||||
auto position =
|
||||
glm::ivec3(chunkX * CHUNK_W, 0, chunkZ * CHUNK_D) + placement.position;
|
||||
auto size = structure.getSize() + glm::ivec3(0, CHUNK_H, 0);
|
||||
AABB aabb(position, position + size);
|
||||
for (int lcz = -1; lcz <= 1; lcz++) {
|
||||
for (int lcx = -1; lcx <= 1; lcx++) {
|
||||
if (lcx == 0 && lcz == 0) {
|
||||
continue;
|
||||
}
|
||||
auto& otherPrototype = requirePrototype(
|
||||
chunkX + lcx, chunkZ + lcz
|
||||
);
|
||||
auto chunkAABB = gen_chunk_aabb(chunkX + lcx, chunkZ + lcz);
|
||||
if (chunkAABB.intersect(aabb)) {
|
||||
otherPrototype.structures.emplace_back(
|
||||
structureId,
|
||||
offset - glm::ivec3(lcx * CHUNK_W, 0, lcz * CHUNK_D),
|
||||
rotation
|
||||
otherPrototype.placements.emplace_back(
|
||||
priority,
|
||||
StructurePlacement {
|
||||
placement.structure,
|
||||
placement.position -
|
||||
glm::ivec3(lcx * CHUNK_W, 0, lcz * CHUNK_D),
|
||||
placement.rotation}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WorldGenerator::placeLine(const LinePlacement& line) {
|
||||
void WorldGenerator::placeLine(const LinePlacement& line, int priority) {
|
||||
AABB aabb(line.a, line.b);
|
||||
aabb.fix();
|
||||
aabb.a -= line.radius;
|
||||
@ -187,30 +190,29 @@ void WorldGenerator::placeLine(const LinePlacement& line) {
|
||||
for (int cx = cxa; cx <= cxb; cx++) {
|
||||
const auto& found = prototypes.find({cx, cz});
|
||||
if (found != prototypes.end()) {
|
||||
found->second->lines.push_back(line);
|
||||
found->second->placements.emplace_back(priority, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WorldGenerator::placeStructures(
|
||||
const PrototypePlacements& placements,
|
||||
const std::vector<Placement>& placements,
|
||||
ChunkPrototype& prototype,
|
||||
int chunkX,
|
||||
int chunkZ
|
||||
) {
|
||||
util::concat(prototype.structures, placements.structs);
|
||||
for (const auto& placement : prototype.structures) {
|
||||
const auto& offset = placement.position;
|
||||
if (placement.structure < 0 || placement.structure >= def.structures.size()) {
|
||||
logger.error() << "invalid structure index " << placement.structure;
|
||||
for (const auto& placement : placements) {
|
||||
if (auto sp = std::get_if<StructurePlacement>(&placement.placement)) {
|
||||
if (sp->structure < 0 || sp->structure >= def.structures.size()) {
|
||||
logger.error() << "invalid structure index " << sp->structure;
|
||||
continue;
|
||||
}
|
||||
placeStructure(
|
||||
offset, placement.structure, placement.rotation, chunkX, chunkZ);
|
||||
placeStructure(*sp, placement.priority, chunkX, chunkZ);
|
||||
} else {
|
||||
const auto& line = std::get<LinePlacement>(placement.placement);
|
||||
placeLine(line, placement.priority);
|
||||
}
|
||||
for (const auto& line : placements.lines) {
|
||||
placeLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
@ -251,7 +253,7 @@ void WorldGenerator::generateStructures(
|
||||
for (uint x = 0; x < CHUNK_W; x++) {
|
||||
float rand = structsRand.randFloat();
|
||||
const Biome* biome = biomes[z * CHUNK_W + x];
|
||||
size_t structureId = biome->structures.choose(rand, -1);
|
||||
int structureId = biome->structures.choose(rand, -1);
|
||||
if (structureId == -1) {
|
||||
continue;
|
||||
}
|
||||
@ -264,12 +266,16 @@ void WorldGenerator::generateStructures(
|
||||
glm::ivec3 position {x, height, z};
|
||||
position.x -= structure.getSize().x / 2;
|
||||
position.z -= structure.getSize().z / 2;
|
||||
prototype.structures.push_back({
|
||||
static_cast<int>(structureId), position, rotation});
|
||||
prototype.placements.emplace_back(
|
||||
1, StructurePlacement {structureId, position, rotation}
|
||||
);
|
||||
placeStructure(
|
||||
position,
|
||||
StructurePlacement {
|
||||
structureId,
|
||||
rotation,
|
||||
position,
|
||||
rotation
|
||||
},
|
||||
1,
|
||||
chunkX,
|
||||
chunkZ
|
||||
);
|
||||
@ -361,6 +367,9 @@ void WorldGenerator::generatePlants(
|
||||
blockid_t plant = biome->plants.choose(rand);
|
||||
if (plant) {
|
||||
auto& voxel = voxels[vox_index(x, height+1, z)];
|
||||
if (voxel.id) {
|
||||
continue;
|
||||
}
|
||||
auto& groundVoxel = voxels[vox_index(x, height, z)];
|
||||
if (indices.get(groundVoxel.id)->rt.solid) {
|
||||
voxel = {plant, {}};
|
||||
@ -422,9 +431,8 @@ void WorldGenerator::generate(voxel* voxels, int chunkX, int chunkZ) {
|
||||
generate_pole(groundLayers, height, 0, seaLevel, voxels, x, z);
|
||||
}
|
||||
}
|
||||
generateLines(prototype, voxels, chunkX, chunkZ);
|
||||
generatePlacements(prototype, voxels, chunkX, chunkZ);
|
||||
generatePlants(prototype, values, voxels, chunkX, chunkZ, biomes);
|
||||
generateStructures(prototype, voxels, chunkX, chunkZ);
|
||||
|
||||
#ifndef NDEBUG
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
@ -436,13 +444,36 @@ void WorldGenerator::generate(voxel* voxels, int chunkX, int chunkZ) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void WorldGenerator::generateStructures(
|
||||
void WorldGenerator::generatePlacements(
|
||||
const ChunkPrototype& prototype, voxel* voxels, int chunkX, int chunkZ
|
||||
) {
|
||||
for (const auto& placement : prototype.structures) {
|
||||
auto placements = prototype.placements;
|
||||
std::stable_sort(
|
||||
placements.begin(),
|
||||
placements.end(),
|
||||
[](const auto& a, const auto& b) {
|
||||
return a.priority < b.priority;
|
||||
}
|
||||
);
|
||||
for (const auto& placement : placements) {
|
||||
if (auto structure = std::get_if<StructurePlacement>(&placement.placement)) {
|
||||
generateStructure(prototype, *structure, voxels, chunkX, chunkZ);
|
||||
} else {
|
||||
const auto& line = std::get<LinePlacement>(placement.placement);
|
||||
generateLine(prototype, line, voxels, chunkX, chunkZ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WorldGenerator::generateStructure(
|
||||
const ChunkPrototype& prototype,
|
||||
const StructurePlacement& placement,
|
||||
voxel* voxels,
|
||||
int chunkX, int chunkZ
|
||||
) {
|
||||
if (placement.structure < 0 || placement.structure >= def.structures.size()) {
|
||||
logger.error() << "invalid structure index " << placement.structure;
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
auto& generatingStructure = def.structures[placement.structure];
|
||||
auto& structure = *generatingStructure->fragments[placement.rotation];
|
||||
@ -477,13 +508,15 @@ void WorldGenerator::generateStructures(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WorldGenerator::generateLines(
|
||||
const ChunkPrototype& prototype, voxel* voxels, int chunkX, int chunkZ
|
||||
void WorldGenerator::generateLine(
|
||||
const ChunkPrototype& prototype,
|
||||
const LinePlacement& line,
|
||||
voxel* voxels,
|
||||
int chunkX, int chunkZ
|
||||
) {
|
||||
const auto& indices = content->getIndices()->blocks;
|
||||
for (const auto& line : prototype.lines) {
|
||||
|
||||
int cgx = chunkX * CHUNK_W;
|
||||
int cgz = chunkZ * CHUNK_D;
|
||||
|
||||
@ -510,7 +543,10 @@ void WorldGenerator::generateLines(
|
||||
glm::ivec3 closest = util::closest_point_on_segment(
|
||||
a, b, point
|
||||
);
|
||||
if (y > 0 && util::distance2(closest, point) <= radius*radius && line.block == BLOCK_AIR) {
|
||||
if (y > 0 &&
|
||||
util::distance2(closest, point) <= radius * radius &&
|
||||
line.block == BLOCK_AIR
|
||||
) {
|
||||
auto& voxel = voxels[vox_index(x, y, z)];
|
||||
if (!indices.require(voxel.id).replaceable) {
|
||||
voxel = {line.block, {}};
|
||||
@ -530,7 +566,6 @@ void WorldGenerator::generateLines(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WorldGenDebugInfo WorldGenerator::createDebugInfo() const {
|
||||
const auto& area = surroundMap.getArea();
|
||||
|
||||
@ -17,7 +17,6 @@ struct GeneratorDef;
|
||||
class Heightmap;
|
||||
struct Biome;
|
||||
class VoxelFragment;
|
||||
struct PrototypePlacements;
|
||||
|
||||
enum class ChunkPrototypeLevel {
|
||||
VOID=0, WIDE_STRUCTS, BIOMES, HEIGHTMAP, STRUCTURES
|
||||
@ -32,9 +31,7 @@ struct ChunkPrototype {
|
||||
/// @brief chunk heightmap
|
||||
std::shared_ptr<Heightmap> heightmap;
|
||||
|
||||
std::vector<StructurePlacement> structures;
|
||||
|
||||
std::vector<LinePlacement> lines;
|
||||
std::vector<Placement> placements;
|
||||
};
|
||||
|
||||
struct WorldGenDebugInfo {
|
||||
@ -69,22 +66,32 @@ class WorldGenerator {
|
||||
|
||||
void generateStructures(ChunkPrototype& prototype, int x, int z);
|
||||
|
||||
void generatePlacements(
|
||||
const ChunkPrototype& prototype, voxel* voxels, int x, int z
|
||||
);
|
||||
|
||||
void generateBiomes(ChunkPrototype& prototype, int x, int z);
|
||||
|
||||
void generateHeightmap(ChunkPrototype& prototype, int x, int z);
|
||||
|
||||
void placeStructure(
|
||||
const glm::ivec3& offset, size_t structure, uint8_t rotation,
|
||||
const StructurePlacement& placement, int priority,
|
||||
int chunkX, int chunkZ
|
||||
);
|
||||
|
||||
void placeLine(const LinePlacement& line);
|
||||
void placeLine(const LinePlacement& line, int priority);
|
||||
|
||||
void generateLines(
|
||||
const ChunkPrototype& prototype, voxel* voxels, int x, int z
|
||||
void generateLine(
|
||||
const ChunkPrototype& prototype,
|
||||
const LinePlacement& placement,
|
||||
voxel* voxels,
|
||||
int x, int z
|
||||
);
|
||||
void generateStructures(
|
||||
const ChunkPrototype& prototype, voxel* voxels, int x, int z
|
||||
void generateStructure(
|
||||
const ChunkPrototype& prototype,
|
||||
const StructurePlacement& placement,
|
||||
voxel* voxels,
|
||||
int x, int z
|
||||
);
|
||||
void generatePlants(
|
||||
const ChunkPrototype& prototype,
|
||||
@ -104,7 +111,7 @@ class WorldGenerator {
|
||||
);
|
||||
|
||||
void placeStructures(
|
||||
const PrototypePlacements& placements,
|
||||
const std::vector<Placement>& placements,
|
||||
ChunkPrototype& prototype,
|
||||
int x, int z);
|
||||
public:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user