Normalize structure chances if their sum is larger than one.

fixes #544
This commit is contained in:
IntegratedQuantum 2024-07-03 21:09:23 +02:00
parent 1f61ea0c37
commit 2bc1292fc3
2 changed files with 10 additions and 4 deletions

View File

@ -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);

View File

@ -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;
}
}
}