From 5b1936d5d528ffb78bc56098d6af8ba534462873 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 8 Oct 2026 15:43:25 +0200 Subject: [PATCH] Form snow and ice and fill cauldrons in rain from biome temperatures Answer ServerLevel.tickPrecipitation (Biome.shouldFreeze, shouldSnow, getPrecipitationAt) and Level.isRainingAt from the worldgen bundle's biome temperatures, so snow accumulates, water freezes, cauldrons fill, farmland stays moist and fire is extinguished in rain. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 6 + .../horizon-world/src/level/growth_tests.rs | 30 +++- crates/horizon-world/src/level/mod.rs | 1 + crates/horizon-world/src/level/weather.rs | 132 ++++++++++++++++++ crates/horizon-world/src/worldgen/live.rs | 17 +++ 5 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 crates/horizon-world/src/level/weather.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e88f91635..1b9052ae0 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -208,6 +208,12 @@ struct GameLevel<'w> { registry: &'static horizon_world::registry::BlockRegistry, } impl LevelAccess for GameLevel<'_> { + fn precipitation(&self, pos: BlockPos) -> Option { + self.behaviours.precipitation_info(self, pos) + } + fn is_raining_at(&self, pos: BlockPos) -> bool { + self.behaviours.is_raining_at(self, pos) + } fn block_entity_analog_signal(&self, pos: BlockPos, _state: BlockStateId, _direction: Direction) -> i32 { machines::container_signal(self.world, self.behaviours, self.items, self.registry, pos) } diff --git a/crates/horizon-world/src/level/growth_tests.rs b/crates/horizon-world/src/level/growth_tests.rs index 2d120335c..de46103b8 100644 --- a/crates/horizon-world/src/level/growth_tests.rs +++ b/crates/horizon-world/src/level/growth_tests.rs @@ -70,9 +70,6 @@ impl LevelAccess for Level { fn game_time(&self) -> i64 { 0 } - fn block_light(&self, _pos: BlockPos) -> i32 { - 15 - } fn emit(&mut self, event: LevelEvent) { self.events.push(event); } @@ -340,3 +337,30 @@ fn tall_flowers_and_seagrass() { } ))); } + +#[test] +fn snowy_biomes_freeze_and_snow() { + let Some(f) = fixture() else { return }; + let mut w = World::new(f, 1); + w.level.biome = "minecraft:snowy_plains"; + w.floor("grass_block", 2); + let water = w.id("water", &[]); + w.put(BlockPos::new(1, 63, 0), water); + let b = &f.behaviours; + let grass = b + .precipitation_info(&w.level, BlockPos::new(0, 70, 0)) + .unwrap(); + assert_eq!(grass.top, BlockPos::new(0, 64, 0)); + assert!(grass.snows_at_top && !grass.freezes_below); + assert_eq!(grass.kind, PrecipitationKind::Snow); + let pond = b + .precipitation_info(&w.level, BlockPos::new(1, 70, 0)) + .unwrap(); + assert!(pond.freezes_below, "a water source at the edge freezes"); + w.level.biome = "minecraft:plains"; + let rain = b + .precipitation_info(&w.level, BlockPos::new(0, 70, 0)) + .unwrap(); + assert!(!rain.snows_at_top && !rain.freezes_below); + assert_eq!(rain.kind, PrecipitationKind::Rain); +} diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index ee7e1aa61..b111b392d 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -118,6 +118,7 @@ mod speleothem; mod survive; mod tick; mod update; +mod weather; #[cfg(test)] mod growth_tests; diff --git a/crates/horizon-world/src/level/weather.rs b/crates/horizon-world/src/level/weather.rs new file mode 100644 index 000000000..845548ce1 --- /dev/null +++ b/crates/horizon-world/src/level/weather.rs @@ -0,0 +1,132 @@ +//! Biome weather queries for a live level: `ServerLevel.tickPrecipitation`'s +//! inputs (`Biome.shouldFreeze`, `shouldSnow`, `getPrecipitationAt`) and +//! `Level.isRainingAt`, from the biome temperatures of the worldgen bundle. +use super::{Behaviours, Dimension, LevelAccess, Precipitation, PrecipitationKind}; +use crate::{ + fluid::FluidKind, + worldgen::biome_temperature::{BiomeTemperatureSettings, Precipitation as BiomePrecipitation}, +}; +use horizon_core::{geometry::Direction, position::BlockPos}; + +fn sea_level(level: &L) -> i32 { + match level.dimension() { + Dimension::Nether => 32, + Dimension::End => 0, + Dimension::Overworld => 63, + } +} + +impl Behaviours { + fn biome_settings( + &self, + level: &L, + pos: BlockPos, + ) -> Option { + let features = self.live_features()?; + let biome = features.biome_id(level.biome_name(pos)?)?; + features.biome_temperature(biome) + } + /// `Biome.getPrecipitationAt(pos, seaLevel)`. + fn precipitation_kind( + settings: BiomeTemperatureSettings, + pos: BlockPos, + sea: i32, + ) -> BiomePrecipitation { + if !settings.has_precipitation { + BiomePrecipitation::None + } else if settings.height_adjusted(pos, sea) < 0.15 { + BiomePrecipitation::Snow + } else { + BiomePrecipitation::Rain + } + } + /// `getHeightmapPos(MOTION_BLOCKING, pos)`. + fn motion_blocking_top(&self, level: &L, pos: BlockPos) -> BlockPos { + let blocks = self.tag("minecraft:blocks_motion_in_heightmap"); + for y in (level.min_y()..level.max_y()).rev() { + let state = level.block_state(BlockPos::new(pos.x, y, pos.z)); + let fluid = self.fluid(state).kind() != FluidKind::Empty; + if fluid || blocks.is_some_and(|t| t.contains(self.block(state))) { + return BlockPos::new(pos.x, y + 1, pos.z); + } + } + BlockPos::new(pos.x, level.min_y(), pos.z) + } + /// Answers for one `ServerLevel.tickPrecipitation` at the column of `pos`; + /// None without live features or a biome. + pub fn precipitation_info( + &self, + level: &L, + pos: BlockPos, + ) -> Option { + let top = self.motion_blocking_top(level, pos); + let below = BlockPos::new(top.x, top.y - 1, top.z); + let settings = self.biome_settings(level, top)?; + let sea = sea_level(level); + let inside = |p: BlockPos| p.y >= level.min_y() && p.y < level.max_y(); + // Biome.shouldFreeze(level, below, true) + let freezes_below = settings.height_adjusted(below, sea) < 0.15 + && inside(below) + && level.block_light(below) < 10 + && { + let state = level.block_state(below); + self.block(state) == self.named.water && self.fluid(state).is_source() + } + && [ + Direction::West, + Direction::East, + Direction::North, + Direction::South, + ] + .into_iter() + .any(|d| { + let n = super::update::rel(below, d); + !self.is_water_fluid(self.fluid(level.block_state(n)).kind()) + }); + // Biome.shouldSnow(level, top) + let snows_at_top = Self::precipitation_kind(settings, top, sea) == BiomePrecipitation::Snow + && inside(top) + && level.block_light(top) < 10 + && { + let state = level.block_state(top); + (self.is_air(state) || self.block(state) == self.named.snow) + && self.snow_survives(level, top) + }; + let kind = match Self::precipitation_kind(settings, below, sea) { + BiomePrecipitation::None => PrecipitationKind::None, + BiomePrecipitation::Rain => PrecipitationKind::Rain, + BiomePrecipitation::Snow => PrecipitationKind::Snow, + }; + Some(Precipitation { + top, + freezes_below, + snows_at_top, + kind, + }) + } + /// `SnowLayerBlock.canSurvive` for a default snow layer at `pos`. + fn snow_survives(&self, level: &L, pos: BlockPos) -> bool { + let below = level.block_state(BlockPos::new(pos.x, pos.y - 1, pos.z)); + let block = self.block(below); + if self.tags.cannot_support_snow_layer.contains(block) { + return false; + } + if self.tags.support_override_snow_layer.contains(block) { + return true; + } + self.collision_face_full(below, Direction::Up) + || (block == self.named.snow && self.table.int(below, self.props.layers) == 8) + } + /// `Level.isRainingAt(pos)`. + pub fn is_raining_at(&self, level: &L, pos: BlockPos) -> bool { + if !level.is_raining() || level.sky_light(pos) < 15 { + return false; + } + if self.motion_blocking_top(level, pos).y > pos.y { + return false; + } + self.biome_settings(level, pos).is_some_and(|s| { + Self::precipitation_kind(s, pos, sea_level(level)) == BiomePrecipitation::Rain + }) + } +} diff --git a/crates/horizon-world/src/worldgen/live.rs b/crates/horizon-world/src/worldgen/live.rs index 7460d1450..c8cbcca0c 100644 --- a/crates/horizon-world/src/worldgen/live.rs +++ b/crates/horizon-world/src/worldgen/live.rs @@ -34,6 +34,8 @@ pub struct LiveFeatures { placed: Cache, /// `BiomeGenerationSettings.getBoneMealFeatures` per biome, in vanilla order. bone_meal: Vec>, + /// Temperature settings per biome (`Biome.getTemperature`). + temperatures: Vec, } fn resource(message: impl Into) -> GeneratorError { @@ -193,7 +195,15 @@ impl LiveFeatures { } bone_meal.push(list); } + let temperatures = (0..data.biome_names().len()) + .map(|i| { + super::biome_temperature::BiomeTemperatureSettings::from_json( + data.biome_json(BiomeId(i as u32)), + ) + }) + .collect::, _>>()?; Ok(Some(Self { + temperatures, data: data.clone(), catalog, bindings, @@ -296,6 +306,13 @@ impl LiveFeatures { } } } + /// Temperature settings of a biome. + pub fn biome_temperature( + &self, + biome: BiomeId, + ) -> Option { + self.temperatures.get(biome.0 as usize).copied() + } /// Whether the named biome is in a biome tag (`minecraft:...`). pub fn biome_in_tag(&self, biome: &str, tag: &str) -> bool { self.data