mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-09-13 14:27:14 -04:00
Normalize structure chances if their sum is larger than one.
fixes #544
This commit is contained in:
parent
1f61ea0c37
commit
2bc1292fc3
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user