From 2bc1292fc3ec50168dedfae94f01e2fcf3adc8e9 Mon Sep 17 00:00:00 2001 From: IntegratedQuantum Date: Wed, 3 Jul 2024 21:09:23 +0200 Subject: [PATCH] Normalize structure chances if their sum is larger than one. fixes #544 --- src/server/terrain/biomes.zig | 9 ++++++++- src/server/terrain/chunkgen/StructureGenerator.zig | 5 ++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/server/terrain/biomes.zig b/src/server/terrain/biomes.zig index 94a2f7b3..1b1e8f0d 100644 --- a/src/server/terrain/biomes.zig +++ b/src/server/terrain/biomes.zig @@ -30,7 +30,7 @@ const StructureModel = struct { return StructureModel { .vtable = vtable, .data = vtable.loadModel(arena.allocator(), parameters), - .chance = parameters.get(f32, "chance", 0.5), + .chance = 16*parameters.get(f32, "chance", 0.01), // TODO: Should this use the sample point chance directly, instead of the per block chance? }; } @@ -293,10 +293,17 @@ pub const Biome = struct { const structures = json.getChild("structures"); var vegetation = main.ListUnmanaged(StructureModel){}; + var totalChance: f32 = 0; defer vegetation.deinit(main.stackAllocator); for(structures.toSlice()) |elem| { if(StructureModel.initModel(elem)) |model| { vegetation.append(main.stackAllocator, model); + totalChance += model.chance; + } + } + if(totalChance > 1) { + for(vegetation.items) |*model| { + model.chance /= totalChance; } } self.vegetationModels = main.globalAllocator.dupe(StructureModel, vegetation.items); diff --git a/src/server/terrain/chunkgen/StructureGenerator.zig b/src/server/terrain/chunkgen/StructureGenerator.zig index 6dd43401..84a5d49c 100644 --- a/src/server/terrain/chunkgen/StructureGenerator.zig +++ b/src/server/terrain/chunkgen/StructureGenerator.zig @@ -52,13 +52,12 @@ pub fn generate(worldSeed: u64, chunk: *main.chunk.ServerChunk, caveMap: CaveMap const biome = biomeMap.getBiome(px, py, relZ); var randomValue = random.nextFloat(&seed); for(biome.vegetationModels) |model| { // TODO: Could probably use an alias table here. - const adaptedChance = model.chance*16; - if(randomValue < adaptedChance) { + if(randomValue < model.chance) { model.generate(px, py, relZ, chunk, caveMap, &seed); break; } else { // Make sure that after the first one was considered all others get the correct chances. - randomValue = (randomValue - adaptedChance)/(1 - adaptedChance); + randomValue -= model.chance; } } }