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 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-08 15:43:25 +02:00
parent af6d8610a2
commit 5b1936d5d5
5 changed files with 183 additions and 3 deletions
+6
View File
@@ -208,6 +208,12 @@ struct GameLevel<'w> {
registry: &'static horizon_world::registry::BlockRegistry,
}
impl LevelAccess for GameLevel<'_> {
fn precipitation(&self, pos: BlockPos) -> Option<horizon_world::level::Precipitation> {
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)
}
+27 -3
View File
@@ -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);
}
+1
View File
@@ -118,6 +118,7 @@ mod speleothem;
mod survive;
mod tick;
mod update;
mod weather;
#[cfg(test)]
mod growth_tests;
+132
View File
@@ -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<L: LevelAccess + ?Sized>(level: &L) -> i32 {
match level.dimension() {
Dimension::Nether => 32,
Dimension::End => 0,
Dimension::Overworld => 63,
}
}
impl Behaviours {
fn biome_settings<L: LevelAccess + ?Sized>(
&self,
level: &L,
pos: BlockPos,
) -> Option<BiomeTemperatureSettings> {
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<L: LevelAccess + ?Sized>(&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<L: LevelAccess + ?Sized>(
&self,
level: &L,
pos: BlockPos,
) -> Option<Precipitation> {
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<L: LevelAccess + ?Sized>(&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<L: LevelAccess + ?Sized>(&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
})
}
}
+17
View File
@@ -34,6 +34,8 @@ pub struct LiveFeatures {
placed: Cache<PlacedFeatureDefinition>,
/// `BiomeGenerationSettings.getBoneMealFeatures` per biome, in vanilla order.
bone_meal: Vec<Vec<String>>,
/// Temperature settings per biome (`Biome.getTemperature`).
temperatures: Vec<super::biome_temperature::BiomeTemperatureSettings>,
}
fn resource(message: impl Into<String>) -> 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::<Result<Vec<_>, _>>()?;
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<super::biome_temperature::BiomeTemperatureSettings> {
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