biomes: arbres depuis le MNH, exemples de validation d'altitude

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Cette révision appartient à :
TheoetClaude Sonnet 5.5 a révisé 2026-10-30 00:23:56 +01:00
1 Parent e2f92d0f15
révision a5083a696d
3 fichiers modifiés
+187 -1

Aucun fichier ne correspond à votre recherche

+37 -1
Voir le fichier
@@ -184,6 +184,33 @@ pub fn arbre_en(c: &Colonne, x: i32, z: i32, seed: u64) -> Option<Arbre> {
Some(Arbre { essence: es, hauteur: (h0 + dh * r) as u8 })
}
/// Arbres reels depuis un MNH (hauteur de canopee, m, ligne 0 = nord, `w` x `h`).
/// Un arbre par cellule 4x4 : le pixel le plus haut s'il depasse 3 m et n'est ni bati, ni route, ni eau.
/// `colonne(x, z)` fournit la colonne (x = colonne, z = ligne). Essence selon l'etage/le sol, hauteur = MNH (3..=40).
pub fn arbres_depuis_mnh(mnh: &[f32], w: usize, h: usize, colonne: &dyn Fn(i32, i32) -> Colonne, seed: u64) -> Vec<(i32, i32, Arbre)> {
const CELL: usize = 4;
let mut out = Vec::new();
for cz in 0..h.div_ceil(CELL) {
for cx in 0..w.div_ceil(CELL) {
let (mut best, mut bx, mut bz) = (0.0f32, 0usize, 0usize);
for z in cz * CELL..((cz + 1) * CELL).min(h) {
for x in cx * CELL..((cx + 1) * CELL).min(w) {
let v = mnh[z * w + x];
if v.is_finite() && v > best { best = v; bx = x; bz = z; }
}
}
if best < 3.0 { continue; }
let c = colonne(bx as i32, bz as i32);
if c.bati > 0 || c.route > 0 || c.eau > 0 { continue; }
let e = etage(&c, bx as i32, bz as i32, seed);
if e == Etage::Nival { continue; }
let es = essence(&c, e, bx as i32, bz as i32, seed);
out.push((bx as i32, bz as i32, Arbre { essence: es, hauteur: best.round().clamp(3.0, 40.0) as u8 }));
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
@@ -208,7 +235,7 @@ mod tests {
assert_eq!(biome(&col(1200.0, 5.0, 1), 3, 3, 1), Biome::Meadow);
assert_eq!(biome(&col(1200.0, 5.0, 3), 3, 3, 1), Biome::Taiga);
assert_eq!(biome(&col(1200.0, 5.0, 2), 3, 3, 1), Biome::Forest);
assert_eq!(biome(&col(2800.0, 5.0, 1), 3, 3, 1), Biome::StonyPeaks);
assert_eq!(biome(&col(3000.0, 5.0, 1), 3, 3, 1), Biome::StonyPeaks);
}
#[test] fn pente_donne_roche() {
assert_eq!(palette(&col(1200.0, 80.0, 1), 1, 1, 1).haut, "minecraft:stone");
@@ -244,6 +271,15 @@ mod tests {
let a = (0..400).flat_map(|x| (0..400).map(move |z| (x, z))).find_map(|(x, z)| arbre_en(&c, x, z, 2)).unwrap();
assert!(!a.essence.coniferes());
}
#[test] fn arbres_mnh() {
let mut mnh = vec![0.0f32; 16 * 8];
mnh[3 * 16 + 5] = 18.4; mnh[1 * 16 + 9] = 2.0; mnh[6 * 16 + 14] = 12.0;
let a = arbres_depuis_mnh(&mnh, 16, 8, &|_, _| col(1000.0, 5.0, 3), 1);
assert_eq!(a.len(), 2);
assert!(a.iter().any(|&(x, z, t)| (x, z, t.hauteur) == (5, 3, 18)));
let b = arbres_depuis_mnh(&mnh, 16, 8, &|_, _| { let mut c = col(1000.0, 5.0, 3); c.bati = 1; c }, 1);
assert!(b.is_empty());
}
#[test] fn pas_d_arbre_nival() {
assert_eq!(densite(&col(3000.0, 5.0, 3), Etage::Nival), 0.0);
}