Fix height check for cave biomes.

fixes #693
This commit is contained in:
IntegratedQuantum 2024-09-06 22:45:02 +02:00
parent b162199138
commit 1764de2912

View File

@ -31,8 +31,10 @@ pub fn generate(map: *CaveBiomeMapFragment, worldSeed: u64) void {
var validBiomes = main.ListUnmanaged(*const Biome).initCapacity(main.stackAllocator, caveBiomes.len);
defer validBiomes.deinit(main.stackAllocator);
const worldPos = CaveBiomeMapFragment.rotateInverse(.{map.pos.wx, map.pos.wy, map.pos.wz});
const marginDiv = 1024;
const marginMul: comptime_int = @reduce(.Max, @abs(comptime CaveBiomeMapFragment.rotate(.{marginDiv, marginDiv, marginDiv})));
for(caveBiomes) |*biome| {
if(biome.minHeight < worldPos[2] +% CaveBiomeMapFragment.caveBiomeMapSize and biome.maxHeight > worldPos[2]) {
if(biome.minHeight < worldPos[2] +% CaveBiomeMapFragment.caveBiomeMapSize*marginMul/marginDiv and biome.maxHeight > worldPos[2]) {
validBiomes.appendAssumeCapacity(biome);
}
}
@ -46,18 +48,8 @@ pub fn generate(map: *CaveBiomeMapFragment, worldSeed: u64) void {
while(z < CaveBiomeMapFragment.caveBiomeMapSize) : (z += CaveBiomeMapFragment.caveBiomeSize) {
// Sort all biomes to the start that fit into the height region of the given z plane:
var totalChance: f64 = 0;
var insertionIndex: usize = 0;
var i: usize = 0;
while(i < validBiomes.items.len) : (i += 1) {
if(validBiomes.items[i].minHeight < worldPos[2] + z + (CaveBiomeMapFragment.caveBiomeSize - 1) and validBiomes.items[i].maxHeight > worldPos[2] + z) {
if(insertionIndex != i) {
const swap = validBiomes.items[i];
validBiomes.items[i] = validBiomes.items[insertionIndex];
validBiomes.items[insertionIndex] = swap;
}
totalChance += validBiomes.items[insertionIndex].chance;
insertionIndex += 1;
}
for(validBiomes.items) |b| {
totalChance += b.chance;
}
if(totalChance == 0) {
totalChance = 1;
@ -66,18 +58,24 @@ pub fn generate(map: *CaveBiomeMapFragment, worldSeed: u64) void {
while(x < CaveBiomeMapFragment.caveBiomeMapSize) : (x += CaveBiomeMapFragment.caveBiomeSize) {
var y: u31 = 0;
while(y < CaveBiomeMapFragment.caveBiomeMapSize) : (y += CaveBiomeMapFragment.caveBiomeSize) {
const biomeWorldPos = CaveBiomeMapFragment.rotateInverse(.{map.pos.wx + x, map.pos.wy + y, map.pos.wz + z});
for(0..2) |_map| {
var randomValue = random.nextDouble(&seed)*totalChance;
var biome: *const Biome = undefined;
i = 0;
while(true) {
biome = validBiomes.items[i];
i += 1;
randomValue -= biome.chance;
if(randomValue < 0) break;
var randomValue = random.nextDouble(&seed)*totalChance;
var biome: *const Biome = undefined;
var i: usize = 0;
while(true) {
biome = validBiomes.items[i];
i += 1;
randomValue -= biome.chance;
if(randomValue < 0) break;
}
const index = CaveBiomeMapFragment.getIndex(x, y, z);
map.biomeMap[index][_map] = biome;
if(biome.minHeight < biomeWorldPos[2] + CaveBiomeMapFragment.caveBiomeSize*marginMul/marginDiv and biome.maxHeight > biomeWorldPos[2]) {
break;
}
}
const index = CaveBiomeMapFragment.getIndex(x, y, z);
map.biomeMap[index][_map] = biome;
}
}
}