From 7092f16a06f73d825cc73c6cdf132674a418fc70 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:23:44 +0200 Subject: [PATCH] horizon-world probes: one copy of the helpers they shared Now that the probes are modules of one binary, the helpers copied between them live once in examples/probe/support: the collision probes' shape printer (emit, values, box_values, bounds, 8 copies each), the canonical NaN bits of f64 and f32 (50 copies), the registry JSON-to-NBT reader (15) and the JSON block position (4). -1,097 lines of copies. f64::NAN.to_bits() is 0x7ff8000000000000, the literal some copies used, so every probe prints the same bytes. tools/parity.py follows include!: three probes include another probe's source, and stay dependent on it. Co-Authored-By: Claude Opus 5.5 --- .../examples/probe/attribute_fixture.rs | 22 ++--- .../probe/attribute_health_fixture.rs | 22 ++--- .../probe/attribute_map_queries_fixture.rs | 10 +-- .../probe/block_collision_scan_fixture.rs | 67 +-------------- .../examples/probe/block_landing_fixture.rs | 30 ++----- .../examples/probe/block_push_fixture.rs | 28 ++---- .../probe/block_state_codec_fixture.rs | 12 +-- .../examples/probe/blocking_rules_fixture.rs | 10 +-- .../examples/probe/bulk_section_fixture.rs | 12 +-- .../probe/carving_geometry_fixture.rs | 14 +-- .../examples/probe/chunk_dump.rs | 12 +-- .../examples/probe/chunk_stage_fixture.rs | 12 +-- .../probe/collision_context_fixture.rs | 67 +-------------- .../examples/probe/combat_rules_fixture.rs | 10 +-- .../probe/consumable_emission_fixture.rs | 20 ++--- .../examples/probe/consumable_fixture.rs | 26 +++--- .../probe/damage_reduction_fixture.rs | 12 +-- .../examples/probe/damage_source_fixture.rs | 14 +-- .../examples/probe/dynamic_support_fixture.rs | 67 +-------------- .../examples/probe/edge_backoff_fixture.rs | 26 +++--- .../effect_attribute_modifiers_fixture.rs | 29 +++---- .../probe/entity_attribute_fixture.rs | 14 +-- .../probe/entity_collision_fixture.rs | 67 +-------------- .../examples/probe/entity_fluid_fixture.rs | 30 +++---- .../examples/probe/entity_move_fixture.rs | 32 ++----- .../examples/probe/face_support_fixture.rs | 67 +-------------- .../examples/probe/fall_damage_fixture.rs | 24 ++---- .../examples/probe/feature_catalog_fixture.rs | 12 +-- .../examples/probe/feature_region_fixture.rs | 12 +-- .../examples/probe/fill_layer_fixture.rs | 12 +-- .../examples/probe/fluid_fixture.rs | 12 +-- .../examples/probe/food_fixture.rs | 20 ++--- .../examples/probe/food_storage_fixture.rs | 18 ++-- .../examples/probe/ground_state_fixture.rs | 20 ++--- .../probe/interaction_shape_fixture.rs | 26 +++--- .../examples/probe/item_blocking_fixture.rs | 12 +-- .../examples/probe/item_particles_fixture.rs | 20 ++--- .../examples/probe/item_use_fixture.rs | 10 +-- .../probe/live_attribute_updates_fixture.rs | 8 +- .../examples/probe/live_attributes_fixture.rs | 23 ++--- .../examples/probe/living_air_fixture.rs | 22 +++-- .../examples/probe/living_damage_fixture.rs | 28 +++--- .../examples/probe/living_fall_fixture.rs | 16 ++-- .../examples/probe/living_fluid_fixture.rs | 22 +++-- .../examples/probe/living_health_fixture.rs | 18 ++-- .../examples/probe/living_hurt_fixture.rs | 22 ++--- .../examples/probe/living_landing_fixture.rs | 32 +++---- .../examples/probe/living_motion_fixture.rs | 22 ++--- crates/horizon-world/examples/probe/main.rs | 2 + .../examples/probe/material_stage_fixture.rs | 12 +-- .../probe/movement_emission_fixture.rs | 21 +---- .../probe/movement_registry_fixture.rs | 8 +- .../probe/movement_support_fixture.rs | 8 +- .../examples/probe/piston_movement_fixture.rs | 20 ++--- .../examples/probe/piston_shape_fixture.rs | 67 +-------------- .../examples/probe/player_food_fixture.rs | 12 +-- .../examples/probe/player_health_fixture.rs | 18 ++-- .../examples/probe/player_hurt_fixture.rs | 22 ++--- .../examples/probe/player_travel_fixture.rs | 15 ++-- .../probe/registered_player_combat_fixture.rs | 34 +++----- .../probe/registry_collision_scan_fixture.rs | 65 +------------- .../probe/registry_fall_ray_fixture.rs | 14 +-- .../examples/probe/replace_block_fixture.rs | 12 +-- .../examples/probe/restitution_fixture.rs | 16 ++-- .../examples/probe/rule_test_fixture.rs | 12 +-- .../examples/probe/scattered_ore_fixture.rs | 12 +-- .../probe/server_player_combat_fixture.rs | 34 +++----- .../probe/server_player_damage_fixture.rs | 24 ++---- .../examples/probe/spawn_origin.rs | 12 +-- .../examples/probe/state_provider_fixture.rs | 12 +-- .../probe/stored_effect_attributes_fixture.rs | 19 ++--- .../stored_effect_notifications_fixture.rs | 2 +- .../examples/probe/support/common.rs | 49 +++++++++++ .../examples/probe/support/shapes.rs | 85 +++++++++++++++++++ .../examples/probe/synced_floats_fixture.rs | 16 ++-- .../probe/teleport_consume_fixture.rs | 34 +++----- .../examples/probe/terrain_fixture.rs | 12 +-- .../examples/probe/view_direction_fixture.rs | 20 ++--- .../examples/probe/water_splash_fixture.rs | 15 ++-- .../examples/probe/world_border_fixture.rs | 67 +-------------- .../examples/probe/world_ray_fixture.rs | 14 +-- tests/test_parity_runner.py | 9 +- tools/parity.py | 24 +++--- 83 files changed, 561 insertions(+), 1400 deletions(-) create mode 100644 crates/horizon-world/examples/probe/support/common.rs create mode 100644 crates/horizon-world/examples/probe/support/shapes.rs diff --git a/crates/horizon-world/examples/probe/attribute_fixture.rs b/crates/horizon-world/examples/probe/attribute_fixture.rs index e5432a6a3..79cf4e236 100644 --- a/crates/horizon-world/examples/probe/attribute_fixture.rs +++ b/crates/horizon-world/examples/probe/attribute_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_world::attributes::{ AttributeError, Definition, Instance, Modifier, Observer, Operation, Packed, }; @@ -10,13 +11,6 @@ use std::{ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - f64::NAN.to_bits() - } else { - v.to_bits() - } -} fn refs<'a>(mods: impl IntoIterator, pool: &[Modifier]) -> String { let s = mods .into_iter() @@ -43,9 +37,9 @@ impl Observer for Callback<'_> { self.out, "dirty {} {:016x} {} {:016x} {} {}", self.target, - bits(a.base_value()), + f64_bits(a.base_value()), a.is_dirty(), - bits(a.cached_value()), + f64_bits(a.cached_value()), a.modifiers().len(), a.permanent_modifiers().len() )?; @@ -55,7 +49,7 @@ impl Observer for Callback<'_> { return Err(io::Error::other("injected attribute observer failure")); } if self.op["read"].as_bool().unwrap() { - writeln!(self.out, "observed {:016x}", bits(a.value()))?; + writeln!(self.out, "observed {:016x}", f64_bits(a.value()))?; } Ok(()) } @@ -64,9 +58,9 @@ fn state(out: &mut dyn Write, id: usize, a: &Instance, pool: &[Modifier]) -> io: writeln!( out, "state {id} {:016x} {} {:016x}", - bits(a.base_value()), + f64_bits(a.base_value()), a.is_dirty(), - bits(a.cached_value()) + f64_bits(a.cached_value()) )?; writeln!(out, "all {}", refs(a.modifiers(), pool))?; writeln!(out, "permanent {}", refs(a.permanent_modifiers(), pool))?; @@ -165,7 +159,7 @@ pub fn main() -> Result<(), Box> { writeln!( cb.out, "packed {:016x} {}", - bits(p.base), + f64_bits(p.base), refs(&p.modifiers, &pool) )?; packed = Some(p); @@ -181,7 +175,7 @@ pub fn main() -> Result<(), Box> { ), "many" => instances[target].add_permanent_many(mods(), &mut cb), "value" => { - writeln!(cb.out, "value {:016x}", bits(instances[target].value()))?; + writeln!(cb.out, "value {:016x}", f64_bits(instances[target].value()))?; Ok(()) } _ => panic!("fixture mode"), diff --git a/crates/horizon-world/examples/probe/attribute_health_fixture.rs b/crates/horizon-world/examples/probe/attribute_health_fixture.rs index 5efe0cb96..05e779aac 100644 --- a/crates/horizon-world/examples/probe/attribute_health_fixture.rs +++ b/crates/horizon-world/examples/probe/attribute_health_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::{ attribute_health::{AttributeHealth, HealthAttributes, HealthError, HealthObserver}, attributes::{ @@ -21,13 +22,6 @@ fn single(v: &Value) -> f32 { fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn double_bits(v: f64) -> u64 { if v.is_nan() { f64::NAN.to_bits() @@ -55,7 +49,7 @@ impl Observer<'_> { impl HealthObserver for Observer<'_> { type Error = io::Error; fn updated(&mut self, h: &mut SyncedFloat) -> io::Result<()> { - self.effect(&format!("updated {:08x}", bits(h.value()))) + self.effect(&format!("updated {:08x}", f32_bits(h.value()))) } } // This fixture observes health/food values, not cross-attribute hash order. @@ -168,7 +162,7 @@ impl FoodWorld for World<'_, A> { } fn hurt_starvation(&mut self, _: (), _: &mut FoodData, v: f32) -> Result<(), Self::Error> { self.observer - .effect(&format!("starve {:08x}", bits(v))) + .effect(&format!("starve {:08x}", f32_bits(v))) .map_err(HealthError::Observer) } } @@ -261,7 +255,7 @@ fn run(dir: &Path, output: &str) -> Result<(), Box(dir: &Path, output: &str) -> Result<(), Box { let id = world.templates[op["template"].as_u64().unwrap() as usize] .id() .to_owned(); map.modifier_value(world.key(op), &id, &mut world) - .map(bits) + .map(f64_hex) .map_err(|e| match e { ModifierValueError::World(e) => e, ModifierValueError::MissingInstanceModifier => Stop::Null, @@ -475,7 +475,7 @@ mod integration { "value" => world .handle(op["instance"].as_i64().unwrap()) .and_then(|h| h.ok_or(Stop::Null)) - .map(|h| bits(world.instances[h.id].value.as_mut().unwrap().value())), + .map(|h| f64_hex(world.instances[h.id].value.as_mut().unwrap().value())), _ => world.raw(&mut map, op).map(|()| "ok".into()), }; world.logs.push(format!( diff --git a/crates/horizon-world/examples/probe/block_collision_scan_fixture.rs b/crates/horizon-world/examples/probe/block_collision_scan_fixture.rs index c27c554b1..e39fe2b8d 100644 --- a/crates/horizon-world/examples/probe/block_collision_scan_fixture.rs +++ b/crates/horizon-world/examples/probe/block_collision_scan_fixture.rs @@ -1,5 +1,6 @@ +use crate::support::shapes::emit; use horizon_core::{ - geometry::{Aabb, Axis, ShapeOp, Vec3, VoxelShape}, + geometry::{Aabb, ShapeOp, Vec3, VoxelShape}, position::{BlockPos, ChunkPos}, }; use horizon_world::{ @@ -20,70 +21,6 @@ use std::{ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn values( - out: &mut impl Write, - label: &str, - values: impl IntoIterator, -) -> io::Result<()> { - write!(out, "{label}")?; - for v in values { - let bits = if v.is_nan() { - 0x7ff8_0000_0000_0000 - } else { - v.to_bits() - }; - write!(out, " {bits:016x}")?; - } - writeln!(out) -} -fn box_values(out: &mut impl Write, label: &str, b: Aabb) -> io::Result<()> { - values( - out, - label, - [ - b.min(Axis::X), - b.min(Axis::Y), - b.min(Axis::Z), - b.max(Axis::X), - b.max(Axis::Y), - b.max(Axis::Z), - ], - ) -} -fn emit(out: &mut impl Write, shape: &VoxelShape) -> io::Result<()> { - writeln!( - out, - "shape {} {} {}", - shape.is_empty(), - shape.boxes().len(), - shape.is_cube() - )?; - for (axis, name) in Axis::ALL.into_iter().zip(["x", "y", "z"]) { - values(out, name, shape.coordinates(axis).iter().copied())?; - } - let axes = shape.cube_coordinate_axes(); - writeln!( - out, - "axes {}", - u8::from(axes[0]) + 2 * u8::from(axes[1]) + 4 * u8::from(axes[2]) - )?; - values( - out, - "extrema", - Axis::ALL - .map(|a| shape.min(a)) - .into_iter() - .chain(Axis::ALL.map(|a| shape.max(a))), - )?; - match shape.bounds() { - Some(b) => box_values(out, "bounds", b)?, - None => writeln!(out, "bounds none")?, - } - for b in shape.boxes() { - box_values(out, "box", *b)?; - } - Ok(()) -} fn shape(index: u64) -> VoxelShape { let cuboid = |min, max| VoxelShape::from_box(Aabb::new(min, max)).unwrap(); diff --git a/crates/horizon-world/examples/probe/block_landing_fixture.rs b/crates/horizon-world/examples/probe/block_landing_fixture.rs index 77b42f418..a6ab6ac33 100644 --- a/crates/horizon-world/examples/probe/block_landing_fixture.rs +++ b/crates/horizon-world/examples/probe/block_landing_fixture.rs @@ -1,5 +1,7 @@ +use crate::support::common::f64_bits; +use crate::support::shapes::bounds; use horizon_core::{ - geometry::{Aabb, Axis, Vec3, VoxelShape}, + geometry::{Aabb, Vec3, VoxelShape}, position::BlockPos, }; use horizon_world::{ @@ -24,13 +26,6 @@ fn number(v: &Value) -> f64 { fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn fbits(v: f32) -> u32 { if v.is_nan() { 0x7fc00000 } else { v.to_bits() } } @@ -43,17 +38,6 @@ fn bbox(v: &Value) -> Aabb { Vec3::new(number(&v[3]), number(&v[4]), number(&v[5])), ) } -fn bounds(label: &str, b: Aabb) -> String { - format!( - "{label} {:016x} {:016x} {:016x} {:016x} {:016x} {:016x}", - bits(b.min(Axis::X)), - bits(b.min(Axis::Y)), - bits(b.min(Axis::Z)), - bits(b.max(Axis::X)), - bits(b.max(Axis::Y)), - bits(b.max(Axis::Z)) - ) -} struct World<'a, 'r> { f: &'a Value, out: &'a mut dyn Write, @@ -100,9 +84,9 @@ impl BlockPushWorld for World<'_, '_> { self.query(format!( "teleport {} {:016x} {:016x} {:016x}", e.0, - bits(v.x), - bits(v.y), - bits(v.z) + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) ))?; let i = e.0 as usize; self.entities[i] = self.entities[i].move_by(v); @@ -169,7 +153,7 @@ impl BlockLandingWorld for World<'_, '_> { Ok(k) } fn cause_fall_damage(&mut self, d: f64, m: f32, s: Self::DamageSource) -> io::Result { - self.query(format!("damage {:016x} {:08x} {s}", bits(d), fbits(m)))?; + self.query(format!("damage {:016x} {:08x} {s}", f64_bits(d), fbits(m)))?; Ok(self.flag("damage")) } fn known_sound(&self, s: LandingSound) -> Self::Sound { diff --git a/crates/horizon-world/examples/probe/block_push_fixture.rs b/crates/horizon-world/examples/probe/block_push_fixture.rs index 2c77a8059..678e0fa24 100644 --- a/crates/horizon-world/examples/probe/block_push_fixture.rs +++ b/crates/horizon-world/examples/probe/block_push_fixture.rs @@ -1,5 +1,7 @@ +use crate::support::common::f64_bits; +use crate::support::shapes::bounds; use horizon_core::{ - geometry::{Aabb, Axis, ShapeOp, Vec3, VoxelShape}, + geometry::{Aabb, ShapeOp, Vec3, VoxelShape}, position::BlockPos, }; use horizon_world::{ @@ -16,30 +18,12 @@ use std::{ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn bbox(v: &Value) -> Aabb { Aabb::new( Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])), Vec3::new(number(&v[3]), number(&v[4]), number(&v[5])), ) } -fn bounds(label: &str, b: Aabb) -> String { - format!( - "{label} {:016x} {:016x} {:016x} {:016x} {:016x} {:016x}", - bits(b.min(Axis::X)), - bits(b.min(Axis::Y)), - bits(b.min(Axis::Z)), - bits(b.max(Axis::X)), - bits(b.max(Axis::Y)), - bits(b.max(Axis::Z)) - ) -} fn shape(v: &Value) -> Result> { let mut s = VoxelShape::empty(); for b in v.as_array().unwrap() { @@ -85,9 +69,9 @@ impl BlockPushWorld for World<'_> { self.query(format!( "teleport {} {:016x} {:016x} {:016x}", e.0, - bits(v.x), - bits(v.y), - bits(v.z) + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) ))?; let index = e.0 as usize; self.entities[index] = self.entities[index].move_by(v); diff --git a/crates/horizon-world/examples/probe/block_state_codec_fixture.rs b/crates/horizon-world/examples/probe/block_state_codec_fixture.rs index c0372d4fd..40d51d4c2 100644 --- a/crates/horizon-world/examples/probe/block_state_codec_fixture.rs +++ b/crates/horizon-world/examples/probe/block_state_codec_fixture.rs @@ -1,4 +1,5 @@ //! Original JSON block-state codec comparison transport. +use crate::support::common::tag; use horizon_world::{ DATA_VERSION, nbt::Tag, @@ -11,17 +12,6 @@ use std::{ io::{self, Write}, path::Path, }; -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); diff --git a/crates/horizon-world/examples/probe/blocking_rules_fixture.rs b/crates/horizon-world/examples/probe/blocking_rules_fixture.rs index c8613725c..19d3bb8d5 100644 --- a/crates/horizon-world/examples/probe/blocking_rules_fixture.rs +++ b/crates/horizon-world/examples/probe/blocking_rules_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::blocking_rules::{ BlockingRules, DamageReduction, ItemDamageFunction, TypeMatchWorld, }; @@ -13,13 +14,6 @@ fn single(v: &Value) -> f32 { fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} struct World<'a> { f: &'a Value, out: &'a mut dyn Write, @@ -96,7 +90,7 @@ pub fn main() -> Result<(), Box> { )?; writeln!(w.out, "item {}", rules.item_damage.apply(damage))?; match rules.resolve(&mut w, damage, number(&f["angle"])) { - Ok(v) => writeln!(w.out, "blocked {:08x}\nresult ok", bits(v))?, + Ok(v) => writeln!(w.out, "blocked {:08x}\nresult ok", f32_bits(v))?, Err(_) => writeln!(w.out, "result error")?, }; writeln!(w.out, "queries {} {}", w.calls, w.changed)?; diff --git a/crates/horizon-world/examples/probe/bulk_section_fixture.rs b/crates/horizon-world/examples/probe/bulk_section_fixture.rs index eefdd9e52..a7bcc834a 100644 --- a/crates/horizon-world/examples/probe/bulk_section_fixture.rs +++ b/crates/horizon-world/examples/probe/bulk_section_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::tag; use horizon_world::worldgen::{ anchor::WorldGenerationContext, bulk_section::TerrainBulkAccess, feature_region::TerrainRegion, }; @@ -55,17 +56,6 @@ impl TerrainBlockStates for Properties<'_> { fn n(v: &Value) -> i32 { v.as_i64().unwrap() as i32 } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(i) => Tag::Int(i.as_i64().unwrap() as i32), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - _ => panic!("unsupported registry fixture value"), - } -} fn fixture( f: &Value, registry: &BlockRegistry, diff --git a/crates/horizon-world/examples/probe/carving_geometry_fixture.rs b/crates/horizon-world/examples/probe/carving_geometry_fixture.rs index c45ae4cda..52582b45b 100644 --- a/crates/horizon-world/examples/probe/carving_geometry_fixture.rs +++ b/crates/horizon-world/examples/probe/carving_geometry_fixture.rs @@ -1,4 +1,5 @@ //! Transport for shared cave/canyon geometry and ordered output failures. +use crate::support::common::f64_bits; use horizon_core::{ carving::{CarverOutput, CarvingMask, can_reach, carve_ellipsoid}, position::ChunkPos, @@ -15,13 +16,6 @@ fn n(v: &Value) -> i32 { fn d(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} struct Output<'a> { mask: &'a mut CarvingMask, trace: &'a std::cell::RefCell>, @@ -78,9 +72,9 @@ pub fn main() -> Result<(), Box> { }; trace.borrow_mut().push(format!( "skip {:x} {:x} {:x} {by} {skip}", - bits(x), - bits(y), - bits(z) + f64_bits(x), + f64_bits(y), + f64_bits(z) )); skip }, diff --git a/crates/horizon-world/examples/probe/chunk_dump.rs b/crates/horizon-world/examples/probe/chunk_dump.rs index f6a93e660..d36944989 100644 --- a/crates/horizon-world/examples/probe/chunk_dump.rs +++ b/crates/horizon-world/examples/probe/chunk_dump.rs @@ -11,6 +11,7 @@ //! quart layer (16 columns, z-major), one `y Y ID...` line per block row and //! one `be X Y Z CANONICAL_NBT` line per block entity and one `t block|fluid //! NAME X Y Z PRIORITY` line per scheduled tick. +use crate::support::common::tag; use horizon_core::position::ChunkPos; use horizon_world::{ DATA_VERSION, @@ -23,17 +24,6 @@ use std::{collections::HashMap, fmt::Write as _, fs, io::Write as _, path::Path} type Error = Box; -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(o) => { - Tag::Compound(o.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} fn registry(resources: &Path) -> Result<&'static BlockRegistry, Error> { let facts: Value = serde_json::from_str(&fs::read_to_string(resources.join("facts.json"))?)?; let states = facts["states"].as_array().ok_or("facts states")?; diff --git a/crates/horizon-world/examples/probe/chunk_stage_fixture.rs b/crates/horizon-world/examples/probe/chunk_stage_fixture.rs index 0ae093dcd..2929de2f1 100644 --- a/crates/horizon-world/examples/probe/chunk_stage_fixture.rs +++ b/crates/horizon-world/examples/probe/chunk_stage_fixture.rs @@ -1,4 +1,5 @@ //! Test transport for pre-light ProtoChunk writes and stage heightmap transitions. +use crate::support::common::tag; use horizon_core::position::ChunkPos; use horizon_world::{ DATA_VERSION, @@ -54,17 +55,6 @@ impl TerrainBlockStates for Properties<'_> { fn n(v: &Value) -> i32 { v.as_i64().unwrap() as i32 } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(i) => Tag::Int(i.as_i64().unwrap() as i32), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - _ => panic!("unsupported registry fixture value"), - } -} fn fixture( f: &Value, registry: &BlockRegistry, diff --git a/crates/horizon-world/examples/probe/collision_context_fixture.rs b/crates/horizon-world/examples/probe/collision_context_fixture.rs index 86620ab99..f26137fef 100644 --- a/crates/horizon-world/examples/probe/collision_context_fixture.rs +++ b/crates/horizon-world/examples/probe/collision_context_fixture.rs @@ -1,5 +1,6 @@ +use crate::support::shapes::emit; use horizon_core::{ - geometry::{Aabb, Axis, ShapeOp, Vec3, VoxelShape}, + geometry::{Aabb, ShapeOp, Vec3, VoxelShape}, position::BlockPos, }; use horizon_world::{ @@ -31,70 +32,6 @@ const ITEMS: [&str; 5] = [ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn values( - out: &mut impl Write, - label: &str, - values: impl IntoIterator, -) -> io::Result<()> { - write!(out, "{label}")?; - for v in values { - let bits = if v.is_nan() { - 0x7ff8_0000_0000_0000 - } else { - v.to_bits() - }; - write!(out, " {bits:016x}")?; - } - writeln!(out) -} -fn box_values(out: &mut impl Write, label: &str, b: Aabb) -> io::Result<()> { - values( - out, - label, - [ - b.min(Axis::X), - b.min(Axis::Y), - b.min(Axis::Z), - b.max(Axis::X), - b.max(Axis::Y), - b.max(Axis::Z), - ], - ) -} -fn emit(out: &mut impl Write, shape: &VoxelShape) -> io::Result<()> { - writeln!( - out, - "shape {} {} {}", - shape.is_empty(), - shape.boxes().len(), - shape.is_cube() - )?; - for (axis, name) in Axis::ALL.into_iter().zip(["x", "y", "z"]) { - values(out, name, shape.coordinates(axis).iter().copied())?; - } - let axes = shape.cube_coordinate_axes(); - writeln!( - out, - "axes {}", - u8::from(axes[0]) + 2 * u8::from(axes[1]) + 4 * u8::from(axes[2]) - )?; - values( - out, - "extrema", - Axis::ALL - .map(|a| shape.min(a)) - .into_iter() - .chain(Axis::ALL.map(|a| shape.max(a))), - )?; - match shape.bounds() { - Some(b) => box_values(out, "bounds", b)?, - None => writeln!(out, "bounds none")?, - } - for b in shape.boxes() { - box_values(out, "box", *b)?; - } - Ok(()) -} struct LiquidWorld<'a, W> { out: &'a mut W, calls: i32, diff --git a/crates/horizon-world/examples/probe/combat_rules_fixture.rs b/crates/horizon-world/examples/probe/combat_rules_fixture.rs index 973596f6c..9e523a30d 100644 --- a/crates/horizon-world/examples/probe/combat_rules_fixture.rs +++ b/crates/horizon-world/examples/probe/combat_rules_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::combat_rules::{self, ArmorWorld}; use serde_json::Value; use std::{ @@ -8,13 +9,6 @@ use std::{ fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} struct World<'a> { f: &'a Value, out: &'a mut dyn Write, @@ -77,7 +71,7 @@ pub fn main() -> Result<(), Box> { }; match result { Ok(v) => { - writeln!(w.out, "value {:08x}\nresult ok", bits(v))?; + writeln!(w.out, "value {:08x}\nresult ok", f32_bits(v))?; } Err(_) => writeln!(w.out, "result error")?, } diff --git a/crates/horizon-world/examples/probe/consumable_emission_fixture.rs b/crates/horizon-world/examples/probe/consumable_emission_fixture.rs index 55dcf0100..6bff122e1 100644 --- a/crates/horizon-world/examples/probe/consumable_emission_fixture.rs +++ b/crates/horizon-world/examples/probe/consumable_emission_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{ geometry::Vec3, random::{LegacyRandom, RandomSource, XoroshiroRandom}, @@ -23,13 +24,6 @@ fn n(v: &Value, k: &str) -> i32 { fn fl(v: &Value, k: &str) -> f32 { f32::from_bits(v[k].as_u64().unwrap() as u32) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn opt(v: Option<&i32>) -> String { v.map_or("none".into(), i32::to_string) } @@ -142,12 +136,12 @@ impl ItemParticleWorld for World<'_, R> { "particle {level} {} {} {damage} {same} {} {} {} {} {} {}", t.item(), t.count(), - bits(p.x), - bits(p.y), - bits(p.z), - bits(v.x), - bits(v.y), - bits(v.z) + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z), + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) )) } } diff --git a/crates/horizon-world/examples/probe/consumable_fixture.rs b/crates/horizon-world/examples/probe/consumable_fixture.rs index b5eec06dc..fccf46129 100644 --- a/crates/horizon-world/examples/probe/consumable_fixture.rs +++ b/crates/horizon-world/examples/probe/consumable_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{ geometry::Vec3, random::{LegacyRandom, RandomSource, XoroshiroRandom}, @@ -33,13 +34,6 @@ fn n(v: &Value, k: &str) -> i32 { fn fl(v: &Value, k: &str) -> f32 { f32::from_bits(v[k].as_u64().unwrap() as u32) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn opt(v: Option<&Component>) -> String { match v { None => "none".into(), @@ -189,12 +183,12 @@ impl ItemParticleWorld for World<'_, R> { "particle {level} {} {} {damage} {same} {} {} {} {} {} {}", t.item(), t.count(), - bits(p.x), - bits(p.y), - bits(p.z), - bits(v.x), - bits(v.y), - bits(v.z) + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z), + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) )) } } @@ -263,9 +257,9 @@ impl FoodConsumptionWorld for World<'_, R> { "food-sound {} {} {category} {} {} {} {} {}", s.level, s.sound, - bits(s.position.x), - bits(s.position.y), - bits(s.position.z), + f64_bits(s.position.x), + f64_bits(s.position.y), + f64_bits(s.position.z), fbits(s.volume), fbits(s.pitch) )) diff --git a/crates/horizon-world/examples/probe/damage_reduction_fixture.rs b/crates/horizon-world/examples/probe/damage_reduction_fixture.rs index 766a53439..7310ca43e 100644 --- a/crates/horizon-world/examples/probe/damage_reduction_fixture.rs +++ b/crates/horizon-world/examples/probe/damage_reduction_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::{ combat_rules::ArmorWorld, damage_reduction::{self, Bypass, DamageReductionWorld}, @@ -14,13 +15,6 @@ fn single(v: &Value) -> f32 { fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} struct World<'a> { f: &'a Value, out: &'a mut dyn Write, @@ -73,7 +67,7 @@ impl DamageReductionWorld for World<'_> { Ok(self.state()[format!("bypass_{n}")].as_bool().unwrap()) } fn hurt_armor(&mut self, v: f32) -> io::Result<()> { - self.query(&format!("hurt-armor {:08x}", bits(v))) + self.query(&format!("hurt-armor {:08x}", f32_bits(v))) } fn armor_value(&mut self) -> io::Result { self.query("armor")?; @@ -139,7 +133,7 @@ pub fn main() -> Result<(), Box> { } else { damage_reduction::after_magic(&mut w, d) }; - r.and_then(|v| writeln!(w.out, "float {:08x}", bits(v))) + r.and_then(|v| writeln!(w.out, "float {:08x}", f32_bits(v))) }; writeln!( w.out, diff --git a/crates/horizon-world/examples/probe/damage_source_fixture.rs b/crates/horizon-world/examples/probe/damage_source_fixture.rs index f50f11d22..8599e3cac 100644 --- a/crates/horizon-world/examples/probe/damage_source_fixture.rs +++ b/crates/horizon-world/examples/probe/damage_source_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_world::damage_source::{Catalog, EntityKind, SourceWorld}; use serde_json::Value; use std::{ @@ -11,21 +12,14 @@ fn number(v: &Value) -> f64 { fn position(v: &Value) -> [f64; 3] { std::array::from_fn(|i| number(&v[i])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - f64::NAN.to_bits() - } else { - v.to_bits() - } -} fn vec(v: Option<[f64; 3]>) -> String { match v { None => "none".into(), Some(v) => format!( "{:016x} {:016x} {:016x}", - bits(v[0]), - bits(v[1]), - bits(v[2]) + f64_bits(v[0]), + f64_bits(v[1]), + f64_bits(v[2]) ), } } diff --git a/crates/horizon-world/examples/probe/dynamic_support_fixture.rs b/crates/horizon-world/examples/probe/dynamic_support_fixture.rs index 92f8c159a..84a176ea4 100644 --- a/crates/horizon-world/examples/probe/dynamic_support_fixture.rs +++ b/crates/horizon-world/examples/probe/dynamic_support_fixture.rs @@ -1,4 +1,5 @@ -use horizon_core::geometry::{Aabb, Axis, Direction, VoxelShape}; +use crate::support::shapes::{emit, values}; +use horizon_core::geometry::{Direction, VoxelShape}; use horizon_world::{ block_collision::{DynamicCollisionWorld, ShulkerCollisionState}, block_support::SupportType, @@ -15,70 +16,6 @@ use std::{ io::{self, Write}, path::Path, }; -fn values( - out: &mut impl Write, - label: &str, - values: impl IntoIterator, -) -> io::Result<()> { - write!(out, "{label}")?; - for v in values { - let bits = if v.is_nan() { - 0x7ff8_0000_0000_0000 - } else { - v.to_bits() - }; - write!(out, " {bits:016x}")?; - } - writeln!(out) -} -fn box_values(out: &mut impl Write, label: &str, b: Aabb) -> io::Result<()> { - values( - out, - label, - [ - b.min(Axis::X), - b.min(Axis::Y), - b.min(Axis::Z), - b.max(Axis::X), - b.max(Axis::Y), - b.max(Axis::Z), - ], - ) -} -fn emit(out: &mut impl Write, shape: &VoxelShape) -> io::Result<()> { - writeln!( - out, - "shape {} {} {}", - shape.is_empty(), - shape.boxes().len(), - shape.is_cube() - )?; - for (axis, name) in Axis::ALL.into_iter().zip(["x", "y", "z"]) { - values(out, name, shape.coordinates(axis).iter().copied())?; - } - let axes = shape.cube_coordinate_axes(); - writeln!( - out, - "axes {}", - u8::from(axes[0]) + 2 * u8::from(axes[1]) + 4 * u8::from(axes[2]) - )?; - values( - out, - "extrema", - Axis::ALL - .map(|a| shape.min(a)) - .into_iter() - .chain(Axis::ALL.map(|a| shape.max(a))), - )?; - match shape.bounds() { - Some(b) => box_values(out, "bounds", b)?, - None => writeln!(out, "bounds none")?, - } - for b in shape.boxes() { - box_values(out, "box", *b)?; - } - Ok(()) -} struct World<'a, W> { collisions: &'a CachedCollisionRegistry<'a>, pistons: &'a PistonBlockBindings<'a>, diff --git a/crates/horizon-world/examples/probe/edge_backoff_fixture.rs b/crates/horizon-world/examples/probe/edge_backoff_fixture.rs index ad9eb235a..a805cbb1e 100644 --- a/crates/horizon-world/examples/probe/edge_backoff_fixture.rs +++ b/crates/horizon-world/examples/probe/edge_backoff_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::geometry::{Aabb, Axis, Vec3}; use horizon_world::edge_backoff::{EdgeBackoffInput, EdgeBackoffWorld, back_off_from_edge}; use serde_json::Value; @@ -12,13 +13,6 @@ fn number(v: &Value) -> f64 { fn vector(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} struct World<'a> { input: &'a Value, out: &'a mut dyn Write, @@ -56,12 +50,12 @@ impl EdgeBackoffWorld for World<'_> { fn no_collision(&mut self, b: Aabb) -> io::Result { self.query(format!( "clear {:016x} {:016x} {:016x} {:016x} {:016x} {:016x}", - bits(b.min(Axis::X)), - bits(b.min(Axis::Y)), - bits(b.min(Axis::Z)), - bits(b.max(Axis::X)), - bits(b.max(Axis::Y)), - bits(b.max(Axis::Z)) + f64_bits(b.min(Axis::X)), + f64_bits(b.min(Axis::Y)), + f64_bits(b.min(Axis::Z)), + f64_bits(b.max(Axis::X)), + f64_bits(b.max(Axis::Y)), + f64_bits(b.max(Axis::Z)) ))?; let p = self.probes; self.probes += 1; @@ -105,9 +99,9 @@ pub fn main() -> Result<(), Box> { Ok(v) => writeln!( world.out, "movement {:016x} {:016x} {:016x}", - bits(v.x), - bits(v.y), - bits(v.z) + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) )?, Err(_) => writeln!(world.out, "error")?, }; diff --git a/crates/horizon-world/examples/probe/effect_attribute_modifiers_fixture.rs b/crates/horizon-world/examples/probe/effect_attribute_modifiers_fixture.rs index 1ff3e79b6..160dcdc28 100644 --- a/crates/horizon-world/examples/probe/effect_attribute_modifiers_fixture.rs +++ b/crates/horizon-world/examples/probe/effect_attribute_modifiers_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_hex; use horizon_world::{ attributes::{AttributeError, Definition, Instance, Modifier, Observer, Operation}, effect_attribute_modifiers::{self as effects, Definitions, Error, ModifierWorld, Template}, @@ -24,21 +25,11 @@ enum Stop { Callback, Duplicate, } -fn bits(v: f64) -> String { - format!( - "{:016x}", - if v.is_nan() { - f64::NAN.to_bits() - } else { - v.to_bits() - } - ) -} fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } fn desc(m: &Modifier) -> String { - format!("{}:{}:{}", m.id(), bits(m.amount()), m.operation() as u8) + format!("{}:{}:{}", m.id(), f64_hex(m.amount()), m.operation() as u8) } fn key_desc(k: Option<&Key>) -> String { k.map_or("null".into(), |k| k.id.to_string()) @@ -66,7 +57,7 @@ impl Shared { .iter() .map(|(k, v)| { let v = v.as_ref().map_or("null".into(), |t| { - format!("{}:{}:{}", t.id(), bits(t.amount()), t.operation() as u8) + format!("{}:{}:{}", t.id(), f64_hex(t.amount()), t.operation() as u8) }); format!("{}={v}", key_desc(k.as_ref())) }) @@ -126,9 +117,9 @@ impl Shared { fn state(&mut self, id: usize, a: &Instance) { self.logs.push(format!( "instance {id} {} {} {} | {} | {}", - bits(a.base_value()), + f64_hex(a.base_value()), a.is_dirty(), - bits(a.cached_value()), + f64_hex(a.cached_value()), self.refs(a.modifiers()), self.refs(a.permanent_modifiers()) )); @@ -153,7 +144,7 @@ impl Observer for Dirty<'_> { if self.shared.op["read"].as_bool().unwrap() { self.shared .logs - .push(format!("observed {}", bits(a.value()))); + .push(format!("observed {}", f64_hex(a.value()))); } Ok(()) } @@ -316,10 +307,10 @@ pub fn main() -> Result<(), Box> { } "value" => { let id = op["target"].as_u64().unwrap() as usize; - world - .shared - .logs - .push(format!("value {id} {}", bits(world.instances[id].value()))); + world.shared.logs.push(format!( + "value {id} {}", + f64_hex(world.instances[id].value()) + )); Ok(()) } _ => { diff --git a/crates/horizon-world/examples/probe/entity_attribute_fixture.rs b/crates/horizon-world/examples/probe/entity_attribute_fixture.rs index 2e94b8b1c..e5fe13b98 100644 --- a/crates/horizon-world/examples/probe/entity_attribute_fixture.rs +++ b/crates/horizon-world/examples/probe/entity_attribute_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_world::attributes::{ AttributeError, Modifier, Operation, entity::{Catalog, LookupError}, @@ -12,17 +13,10 @@ use std::{ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - f64::NAN.to_bits() - } else { - v.to_bits() - } -} fn lookup(out: &mut dyn Write, label: &str, result: Result) -> io::Result<()> { match result { Ok(v) => { - writeln!(out, "{label} {:016x}", bits(v))?; + writeln!(out, "{label} {:016x}", f64_bits(v))?; writeln!(out, "ok") } Err(e) => writeln!( @@ -114,9 +108,9 @@ pub fn main() -> Result<(), Box> { writeln!( out, "state {i} {:016x} {} {:016x} {} {} {} {}", - bits(a.base_value()), + f64_bits(a.base_value()), a.is_dirty(), - bits(a.cached_value()), + f64_bits(a.cached_value()), map.needs_update(id), map.needs_sync(id), a.modifiers().len(), diff --git a/crates/horizon-world/examples/probe/entity_collision_fixture.rs b/crates/horizon-world/examples/probe/entity_collision_fixture.rs index 815d9c249..d801e7d34 100644 --- a/crates/horizon-world/examples/probe/entity_collision_fixture.rs +++ b/crates/horizon-world/examples/probe/entity_collision_fixture.rs @@ -1,4 +1,5 @@ -use horizon_core::geometry::{Aabb, Axis, Vec3, VoxelShape}; +use crate::support::shapes::{box_values, emit}; +use horizon_core::geometry::{Aabb, Vec3}; use horizon_world::{ entity_collision::{CollisionCandidate, EntityCollisionWorld, entity_collision_shapes}, entity_obstruction::EntityIdentity, @@ -12,70 +13,6 @@ use std::{ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn values( - out: &mut impl Write, - label: &str, - values: impl IntoIterator, -) -> io::Result<()> { - write!(out, "{label}")?; - for v in values { - let bits = if v.is_nan() { - 0x7ff8_0000_0000_0000 - } else { - v.to_bits() - }; - write!(out, " {bits:016x}")?; - } - writeln!(out) -} -fn box_values(out: &mut impl Write, label: &str, b: Aabb) -> io::Result<()> { - values( - out, - label, - [ - b.min(Axis::X), - b.min(Axis::Y), - b.min(Axis::Z), - b.max(Axis::X), - b.max(Axis::Y), - b.max(Axis::Z), - ], - ) -} -fn emit(out: &mut impl Write, shape: &VoxelShape) -> io::Result<()> { - writeln!( - out, - "shape {} {} {}", - shape.is_empty(), - shape.boxes().len(), - shape.is_cube() - )?; - for (axis, name) in Axis::ALL.into_iter().zip(["x", "y", "z"]) { - values(out, name, shape.coordinates(axis).iter().copied())?; - } - let axes = shape.cube_coordinate_axes(); - writeln!( - out, - "axes {}", - u8::from(axes[0]) + 2 * u8::from(axes[1]) + 4 * u8::from(axes[2]) - )?; - values( - out, - "extrema", - Axis::ALL - .map(|a| shape.min(a)) - .into_iter() - .chain(Axis::ALL.map(|a| shape.max(a))), - )?; - match shape.bounds() { - Some(b) => box_values(out, "bounds", b)?, - None => writeln!(out, "bounds none")?, - } - for b in shape.boxes() { - box_values(out, "box", *b)?; - } - Ok(()) -} fn bounds(v: &Value) -> Aabb { Aabb::new( Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])), diff --git a/crates/horizon-world/examples/probe/entity_fluid_fixture.rs b/crates/horizon-world/examples/probe/entity_fluid_fixture.rs index ad9b6ba2a..d897eea1c 100644 --- a/crates/horizon-world/examples/probe/entity_fluid_fixture.rs +++ b/crates/horizon-world/examples/probe/entity_fluid_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::{f64_bits, position}; use horizon_core::{ geometry::{Aabb, Vec3}, position::BlockPos, @@ -28,26 +29,17 @@ fn single(v: &Value) -> f32 { fn vector(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - f64::NAN.to_bits() - } else { - v.to_bits() - } -} fn show(v: Vec3) -> String { - format!("{:016x} {:016x} {:016x}", bits(v.x), bits(v.y), bits(v.z)) + format!( + "{:016x} {:016x} {:016x}", + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) + ) } fn pos(p: BlockPos) -> String { format!("{} {} {}", p.x, p.y, p.z) } -fn position(v: &Value) -> BlockPos { - BlockPos::new( - v[0].as_i64().unwrap() as i32, - v[1].as_i64().unwrap() as i32, - v[2].as_i64().unwrap() as i32, - ) -} #[derive(Clone, Copy, Default)] struct Tick { touch: bool, @@ -446,7 +438,7 @@ pub fn main() -> Result<(), Box> { w.out, "tracker {} {:016x} {}", t.fluid as u8, - bits(t.height), + f64_bits(t.height), t.eyes_inside )?; } @@ -455,7 +447,7 @@ pub fn main() -> Result<(), Box> { w.out, "current {} {:016x} {} {}", tag.0, - bits(c.height), + f64_bits(c.height), show(c.accumulated), c.count )?; @@ -465,7 +457,7 @@ pub fn main() -> Result<(), Box> { let height = interaction.fluid_height(&w, tag); let inside = interaction.is_in_fluid(&w, tag); let eyes = interaction.is_eye_in_fluid(&w, tag); - writeln!(w.out, "tag {t} {:016x} {inside} {eyes}", bits(height))?; + writeln!(w.out, "tag {t} {:016x} {inside} {eyes}", f64_bits(height))?; } writeln!(w.out, "final {}", show(w.velocity))?; writeln!(w.out, "queries {}", w.calls)?; @@ -476,7 +468,7 @@ pub fn main() -> Result<(), Box> { "tick {} {} {:016x}", tick.touch, tick.first, - bits(tick.fall) + f64_bits(tick.fall) )?; } } diff --git a/crates/horizon-world/examples/probe/entity_move_fixture.rs b/crates/horizon-world/examples/probe/entity_move_fixture.rs index fb31c8f79..c31e485a9 100644 --- a/crates/horizon-world/examples/probe/entity_move_fixture.rs +++ b/crates/horizon-world/examples/probe/entity_move_fixture.rs @@ -1,5 +1,7 @@ +use crate::support::common::f64_bits; +use crate::support::shapes::bounds; use horizon_core::{ - geometry::{Aabb, Axis, Vec3, VoxelShape, resolve_shape_movement}, + geometry::{Aabb, Vec3, VoxelShape, resolve_shape_movement}, position::BlockPos, }; use horizon_world::{ @@ -23,30 +25,12 @@ fn single(v: &Value) -> f32 { fn vector(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn vec(label: &str, v: Vec3) -> String { format!( "{label} {:016x} {:016x} {:016x}", - bits(v.x), - bits(v.y), - bits(v.z) - ) -} -fn bounds(label: &str, b: Aabb) -> String { - format!( - "{label} {:016x} {:016x} {:016x} {:016x} {:016x} {:016x}", - bits(b.min(Axis::X)), - bits(b.min(Axis::Y)), - bits(b.min(Axis::Z)), - bits(b.max(Axis::X)), - bits(b.max(Axis::Y)), - bits(b.max(Axis::Z)) + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) ) } fn pos(p: BlockPos) -> String { @@ -213,7 +197,7 @@ impl EntityMoveWorld for World<'_> { _: BlockStateId, p: BlockPos, ) -> io::Result<()> { - self.query(format!("fall {:016x} {ground} {}", bits(dy), pos(p)))?; + self.query(format!("fall {:016x} {ground} {}", f64_bits(dy), pos(p)))?; if self.flag("fall_mutates") { self.fall = 3.0; self.velocity = Vec3::new(17.0, 18.0, 19.0); @@ -399,7 +383,7 @@ pub fn main() -> Result<(), Box> { w.flags.minor, w.grounded, w.removed, - bits(w.fall) + f64_bits(w.fall) )?; writeln!(w.out, "history {}", w.history.records().len())?; for r in w.history.records() { diff --git a/crates/horizon-world/examples/probe/face_support_fixture.rs b/crates/horizon-world/examples/probe/face_support_fixture.rs index 60c228136..28e6fa81d 100644 --- a/crates/horizon-world/examples/probe/face_support_fixture.rs +++ b/crates/horizon-world/examples/probe/face_support_fixture.rs @@ -1,4 +1,5 @@ -use horizon_core::geometry::{Aabb, Axis, Direction, ShapeOp, Vec3, VoxelShape}; +use crate::support::shapes::emit; +use horizon_core::geometry::{Aabb, Direction, ShapeOp, Vec3, VoxelShape}; use horizon_world::block_support::SupportType; use serde_json::Value; use std::{ @@ -13,70 +14,6 @@ fn numbers(v: &Value) -> Vec { .map(|v| f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap())) .collect() } -fn values( - out: &mut impl Write, - label: &str, - values: impl IntoIterator, -) -> io::Result<()> { - write!(out, "{label}")?; - for v in values { - let bits = if v.is_nan() { - 0x7ff8_0000_0000_0000 - } else { - v.to_bits() - }; - write!(out, " {bits:016x}")?; - } - writeln!(out) -} -fn box_values(out: &mut impl Write, label: &str, b: Aabb) -> io::Result<()> { - values( - out, - label, - [ - b.min(Axis::X), - b.min(Axis::Y), - b.min(Axis::Z), - b.max(Axis::X), - b.max(Axis::Y), - b.max(Axis::Z), - ], - ) -} -fn emit(out: &mut impl Write, shape: &VoxelShape) -> io::Result<()> { - writeln!( - out, - "shape {} {} {}", - shape.is_empty(), - shape.boxes().len(), - shape.is_cube() - )?; - for (axis, name) in Axis::ALL.into_iter().zip(["x", "y", "z"]) { - values(out, name, shape.coordinates(axis).iter().copied())?; - } - let axes = shape.cube_coordinate_axes(); - writeln!( - out, - "axes {}", - u8::from(axes[0]) + 2 * u8::from(axes[1]) + 4 * u8::from(axes[2]) - )?; - values( - out, - "extrema", - Axis::ALL - .map(|a| shape.min(a)) - .into_iter() - .chain(Axis::ALL.map(|a| shape.max(a))), - )?; - match shape.bounds() { - Some(b) => box_values(out, "bounds", b)?, - None => writeln!(out, "bounds none")?, - } - for b in shape.boxes() { - box_values(out, "box", *b)?; - } - Ok(()) -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); diff --git a/crates/horizon-world/examples/probe/fall_damage_fixture.rs b/crates/horizon-world/examples/probe/fall_damage_fixture.rs index af77d212f..573b9c1e9 100644 --- a/crates/horizon-world/examples/probe/fall_damage_fixture.rs +++ b/crates/horizon-world/examples/probe/fall_damage_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_world::fall_damage::{FallDamageWorld, check_fall_damage}; use serde_json::Value; @@ -9,13 +10,6 @@ use std::{ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn support(v: &Value) -> Option { if v.is_null() { None @@ -66,7 +60,7 @@ impl FallDamageWorld for World<'_> { p.x, p.y, p.z, - bits(distance) + f64_bits(distance) ))?; if self.input["mutate"].as_bool().unwrap() { self.fall = 17.25; @@ -88,9 +82,9 @@ impl FallDamageWorld for World<'_> { fn hit_ground(&mut self, p: Vec3, s: u8) -> io::Result<()> { self.query(format!( "hit-ground {:016x} {:016x} {:016x} {}", - bits(p.x), - bits(p.y), - bits(p.z), + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z), if s == 1 { "queried" } else { "input" } )) } @@ -130,10 +124,10 @@ pub fn main() -> Result<(), Box> { writeln!( world.out, "final {:016x} {:016x} {:016x} {:016x}", - bits(world.fall), - bits(p.x), - bits(p.y), - bits(p.z) + f64_bits(world.fall), + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z) )?; if let Some(p) = world.support { writeln!(world.out, "support {} {} {}", p.x, p.y, p.z)?; diff --git a/crates/horizon-world/examples/probe/feature_catalog_fixture.rs b/crates/horizon-world/examples/probe/feature_catalog_fixture.rs index d140a64e9..bbc69ffd3 100644 --- a/crates/horizon-world/examples/probe/feature_catalog_fixture.rs +++ b/crates/horizon-world/examples/probe/feature_catalog_fixture.rs @@ -1,4 +1,5 @@ //! Feature world transport retaining ordered mutations and partial failures. +use crate::support::common::tag; use horizon_core::random::{LegacyRandom, RandomSource, WorldgenRandom, XoroshiroRandom}; use horizon_world::{ DATA_VERSION, @@ -178,17 +179,6 @@ fn fixture( writeln!(out, "tail {}", random.next_long())?; Ok(()) } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); diff --git a/crates/horizon-world/examples/probe/feature_region_fixture.rs b/crates/horizon-world/examples/probe/feature_region_fixture.rs index 755c5306d..6a244998d 100644 --- a/crates/horizon-world/examples/probe/feature_region_fixture.rs +++ b/crates/horizon-world/examples/probe/feature_region_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::tag; use horizon_core::random::{LegacyRandom, RandomSource, WorldgenRandom, XoroshiroRandom}; use horizon_world::worldgen::{ anchor::WorldGenerationContext, @@ -66,17 +67,6 @@ impl TerrainBlockStates for Properties<'_> { fn n(v: &Value) -> i32 { v.as_i64().unwrap() as i32 } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(i) => Tag::Int(i.as_i64().unwrap() as i32), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - _ => panic!("unsupported registry fixture value"), - } -} fn fixture( f: &Value, registry: &BlockRegistry, diff --git a/crates/horizon-world/examples/probe/fill_layer_fixture.rs b/crates/horizon-world/examples/probe/fill_layer_fixture.rs index a0a35dd93..e107a6383 100644 --- a/crates/horizon-world/examples/probe/fill_layer_fixture.rs +++ b/crates/horizon-world/examples/probe/fill_layer_fixture.rs @@ -1,4 +1,5 @@ //! Feature world transport retaining ordered mutations and partial failures. +use crate::support::common::tag; use horizon_core::random::{LegacyRandom, RandomSource, XoroshiroRandom}; use horizon_world::{ DATA_VERSION, @@ -134,17 +135,6 @@ fn fixture( writeln!(out, "tail {}", random.next_long())?; Ok(()) } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); diff --git a/crates/horizon-world/examples/probe/fluid_fixture.rs b/crates/horizon-world/examples/probe/fluid_fixture.rs index 1e16480a9..1c5ed4a85 100644 --- a/crates/horizon-world/examples/probe/fluid_fixture.rs +++ b/crates/horizon-world/examples/probe/fluid_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::tag; use horizon_world::{ DATA_VERSION, fluid::{FluidBlockBindings, FluidHeightWorld, FluidKind, FluidState, FluidStateTable}, @@ -40,17 +41,6 @@ impl FluidHeightWorld for World<'_, W> { Ok(self.sturdy) } } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); diff --git a/crates/horizon-world/examples/probe/food_fixture.rs b/crates/horizon-world/examples/probe/food_fixture.rs index 2852eaf3e..c12106dfb 100644 --- a/crates/horizon-world/examples/probe/food_fixture.rs +++ b/crates/horizon-world/examples/probe/food_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::food::{Difficulty, FoodData, FoodWorld}; use serde_json::Value; use std::{ @@ -8,13 +9,6 @@ use std::{ fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn state(v: &Value) -> FoodData { FoodData { food: v["food"].as_i64().unwrap() as i32, @@ -72,7 +66,7 @@ impl FoodWorld for World<'_> { Ok(self.world()["hurt"].as_bool().unwrap()) } fn heal(&mut self, f: &mut FoodData, v: f32) -> io::Result<()> { - self.query(f, &format!("heal {:08x}", bits(v))) + self.query(f, &format!("heal {:08x}", f32_bits(v))) } fn health(&mut self, f: &mut FoodData) -> io::Result { self.query(f, "health")?; @@ -81,7 +75,7 @@ impl FoodWorld for World<'_> { fn hurt_starvation(&mut self, _: (), f: &mut FoodData, v: f32) -> io::Result<()> { self.query(f, "damage-sources")?; self.query(f, "starve-source")?; - self.query(f, &format!("hurt {:08x}", bits(v))) + self.query(f, &format!("hurt {:08x}", f32_bits(v))) } } pub fn main() -> Result<(), Box> { @@ -123,8 +117,8 @@ pub fn main() -> Result<(), Box> { w.out, "state {} {:08x} {:08x} {} {} {}", food.food, - bits(food.saturation), - bits(food.exhaustion), + f32_bits(food.saturation), + f32_bits(food.exhaustion), food.timer, food.has_enough_food(), food.needs_food() @@ -135,8 +129,8 @@ pub fn main() -> Result<(), Box> { w.out, "final {} {:08x} {:08x} {}", food.food, - bits(food.saturation), - bits(food.exhaustion), + f32_bits(food.saturation), + f32_bits(food.exhaustion), food.timer )?; writeln!(w.out, "queries {}", w.calls)?; diff --git a/crates/horizon-world/examples/probe/food_storage_fixture.rs b/crates/horizon-world/examples/probe/food_storage_fixture.rs index bf7759a93..13e9a52dc 100644 --- a/crates/horizon-world/examples/probe/food_storage_fixture.rs +++ b/crates/horizon-world/examples/probe/food_storage_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::{ food::FoodData, nbt::{Compound, Tag}, @@ -17,13 +18,6 @@ const KEYS: [&str; 4] = [ fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn tag(v: &Value) -> Option { let value = &v["value"]; Some(match v["type"].as_i64().unwrap() { @@ -82,8 +76,8 @@ pub fn main() -> Result<(), Box> { "state {} {} {:08x} {:08x}", food.food, food.timer, - bits(food.saturation), - bits(food.exhaustion) + f32_bits(food.saturation), + f32_bits(food.exhaustion) )?; let mut saved = Compound::new(); saved.insert("unrelated".into(), Tag::Int(42)); @@ -96,7 +90,7 @@ pub fn main() -> Result<(), Box> { for key in KEYS { match saved[&key.into()] { Tag::Int(v) => writeln!(out, "tag 3 {v}")?, - Tag::Float(v) => writeln!(out, "tag 5 {:08x}", bits(f32::from_bits(v)))?, + Tag::Float(v) => writeln!(out, "tag 5 {:08x}", f32_bits(f32::from_bits(v)))?, _ => panic!("saved type"), } } @@ -106,8 +100,8 @@ pub fn main() -> Result<(), Box> { "roundtrip {} {} {:08x} {:08x}", food.food, food.timer, - bits(food.saturation), - bits(food.exhaustion) + f32_bits(food.saturation), + f32_bits(food.exhaustion) )?; } Ok(()) diff --git a/crates/horizon-world/examples/probe/ground_state_fixture.rs b/crates/horizon-world/examples/probe/ground_state_fixture.rs index 398757854..47611587f 100644 --- a/crates/horizon-world/examples/probe/ground_state_fixture.rs +++ b/crates/horizon-world/examples/probe/ground_state_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{ geometry::{Aabb, Axis, Vec3}, position::BlockPos, @@ -15,13 +16,6 @@ fn number(v: &Value) -> f64 { fn vector(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn position(v: &Value) -> Option { if v.is_null() { None @@ -62,12 +56,12 @@ pub fn main() -> Result<(), Box> { writeln!( out, "find {:016x} {:016x} {:016x} {:016x} {:016x} {:016x}", - bits(b.min(Axis::X)), - bits(b.min(Axis::Y)), - bits(b.min(Axis::Z)), - bits(b.max(Axis::X)), - bits(b.max(Axis::Y)), - bits(b.max(Axis::Z)) + f64_bits(b.min(Axis::X)), + f64_bits(b.min(Axis::Y)), + f64_bits(b.min(Axis::Z)), + f64_bits(b.max(Axis::X)), + f64_bits(b.max(Axis::Y)), + f64_bits(b.max(Axis::Z)) )?; let call = calls; calls += 1; diff --git a/crates/horizon-world/examples/probe/interaction_shape_fixture.rs b/crates/horizon-world/examples/probe/interaction_shape_fixture.rs index 6e10326f0..c1ca5700b 100644 --- a/crates/horizon-world/examples/probe/interaction_shape_fixture.rs +++ b/crates/horizon-world/examples/probe/interaction_shape_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{ geometry::{Aabb, Axis, ShapeOp, Vec3, VoxelShape}, position::BlockPos, @@ -20,13 +21,6 @@ fn number(v: &Value) -> f64 { fn vector(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn shape(v: &Value) -> Result> { let mut result = VoxelShape::empty(); for b in v.as_array().unwrap() { @@ -131,12 +125,12 @@ pub fn main() -> Result<(), Box> { writeln!( out, "box {:016x} {:016x} {:016x} {:016x} {:016x} {:016x}", - bits(b.min(Axis::X)), - bits(b.min(Axis::Y)), - bits(b.min(Axis::Z)), - bits(b.max(Axis::X)), - bits(b.max(Axis::Y)), - bits(b.max(Axis::Z)) + f64_bits(b.min(Axis::X)), + f64_bits(b.min(Axis::Y)), + f64_bits(b.min(Axis::Z)), + f64_bits(b.max(Axis::X)), + f64_bits(b.max(Axis::Y)), + f64_bits(b.max(Axis::Z)) )?; } } @@ -160,9 +154,9 @@ pub fn main() -> Result<(), Box> { Some(h) => writeln!( out, "hit {:016x} {:016x} {:016x} {} {} {} {} {}", - bits(h.location.x), - bits(h.location.y), - bits(h.location.z), + f64_bits(h.location.x), + f64_bits(h.location.y), + f64_bits(h.location.z), format!("{:?}", h.direction).to_lowercase(), h.block.x, h.block.y, diff --git a/crates/horizon-world/examples/probe/item_blocking_fixture.rs b/crates/horizon-world/examples/probe/item_blocking_fixture.rs index 7bfdcdc99..e3f622a6d 100644 --- a/crates/horizon-world/examples/probe/item_blocking_fixture.rs +++ b/crates/horizon-world/examples/probe/item_blocking_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_core::geometry::Vec3; use horizon_world::{ blocking_rules::{BlockingRules, DamageReduction, ItemDamageFunction, TypeMatchWorld}, @@ -19,13 +20,6 @@ fn number(v: &Value) -> f64 { fn vec(v: &Value) -> Option { (!v.is_null()).then(|| Vec3::new(number(&v[0]), number(&v[1]), number(&v[2]))) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} struct Component { present: bool, bypass: Option, @@ -154,7 +148,7 @@ impl ItemBlockingWorld for World<'_> { e == 1 } fn block_using_item(&mut self, _: u64, damage: f32, fully: bool) -> io::Result<()> { - self.event(&format!("block-using 0 {:08x} {fully}", bits(damage))) + self.event(&format!("block-using 0 {:08x} {fully}", f32_bits(damage))) } } pub fn main() -> Result<(), Box> { @@ -207,7 +201,7 @@ pub fn main() -> Result<(), Box> { changed: false, }; match item_blocking::apply(&mut w, single(&f["damage"])) { - Ok(v) => writeln!(w.out, "blocked {:08x}\nresult ok", bits(v))?, + Ok(v) => writeln!(w.out, "blocked {:08x}\nresult ok", f32_bits(v))?, Err(_) => writeln!(w.out, "result error")?, }; writeln!(w.out, "queries {} {}", w.calls, w.changed)?; diff --git a/crates/horizon-world/examples/probe/item_particles_fixture.rs b/crates/horizon-world/examples/probe/item_particles_fixture.rs index 6fe11ea7d..3b7375e17 100644 --- a/crates/horizon-world/examples/probe/item_particles_fixture.rs +++ b/crates/horizon-world/examples/probe/item_particles_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{ geometry::Vec3, random::{LegacyRandom, RandomSource, XoroshiroRandom}, @@ -22,13 +23,6 @@ fn n(v: &Value, k: &str) -> i32 { fn fl(v: &Value, k: &str) -> f32 { f32::from_bits(v[k].as_u64().unwrap() as u32) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn opt(v: Option<&i32>) -> String { v.map_or("none".into(), i32::to_string) } @@ -141,12 +135,12 @@ impl ItemParticleWorld for World<'_, R> { "particle {level} {} {} {damage} {same} {} {} {} {} {} {}", t.item(), t.count(), - bits(p.x), - bits(p.y), - bits(p.z), - bits(v.x), - bits(v.y), - bits(v.z) + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z), + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) )) } } diff --git a/crates/horizon-world/examples/probe/item_use_fixture.rs b/crates/horizon-world/examples/probe/item_use_fixture.rs index 28bf66743..8d88aae7a 100644 --- a/crates/horizon-world/examples/probe/item_use_fixture.rs +++ b/crates/horizon-world/examples/probe/item_use_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::{ blocking_rules::{BlockingRules, ItemDamageFunction}, item_use::{self, ItemUseWorld}, @@ -11,13 +12,6 @@ use std::{ fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} struct World<'a> { f: &'a Value, out: &'a mut dyn Write, @@ -127,7 +121,7 @@ pub fn main() -> Result<(), Box> { item_use::ticks_using_item(&mut w).and_then(|v| writeln!(w.out, "ticks {v}")) } "partial" => item_use::ticks_using_item_partial(&mut w, single(&f["partial"])) - .and_then(|v| writeln!(w.out, "float {:08x}", bits(v))), + .and_then(|v| writeln!(w.out, "float {:08x}", f32_bits(v))), _ => panic!("mode"), }; writeln!( diff --git a/crates/horizon-world/examples/probe/live_attribute_updates_fixture.rs b/crates/horizon-world/examples/probe/live_attribute_updates_fixture.rs index 03b271ef1..b053c2c82 100644 --- a/crates/horizon-world/examples/probe/live_attribute_updates_fixture.rs +++ b/crates/horizon-world/examples/probe/live_attribute_updates_fixture.rs @@ -252,9 +252,9 @@ mod integration { "instance {} {} {} {} {} | {} | {}", h.index(), key.name(), - bits(a.base_value()), + f64_hex(a.base_value()), a.is_dirty(), - bits(a.cached_value()), + f64_hex(a.cached_value()), refs(a.modifiers(), &mut self.pools[i]), refs(a.permanent_modifiers(), &mut self.pools[i]) ) @@ -541,8 +541,8 @@ mod integration { None, ); let result = match op["mode"].as_str().unwrap() { - "value" => w.attribute_value(key).map(bits), - "base" => live_updates::value(&mut w, key, ValueKind::Base).map(bits), + "value" => w.attribute_value(key).map(f64_hex), + "base" => live_updates::value(&mut w, key, ValueKind::Base).map(f64_hex), "set-health" => w .dispatch_set_health(single(&op["value"])) .map(|()| "ok".into()), diff --git a/crates/horizon-world/examples/probe/live_attributes_fixture.rs b/crates/horizon-world/examples/probe/live_attributes_fixture.rs index 2bbff5bdd..1c4a868f5 100644 --- a/crates/horizon-world/examples/probe/live_attributes_fixture.rs +++ b/crates/horizon-world/examples/probe/live_attributes_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_hex; use horizon_world::{ attribute_map::ValueKind, attributes::{ @@ -17,16 +18,6 @@ use std::{ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f64) -> String { - format!( - "{:016x}", - if v.is_nan() { - f64::NAN.to_bits() - } else { - v.to_bits() - } - ) -} fn error(e: Error) -> &'static str { match e { Error::NullAttribute | Error::MissingInstanceModifier => "null", @@ -52,7 +43,7 @@ fn refs(mods: &[Modifier], pool: &mut Vec) -> String { format!( "{id}:{}:{}:{}", m.id(), - bits(m.amount()), + f64_hex(m.amount()), m.operation() as u8 ) }) @@ -104,11 +95,11 @@ pub fn main() -> Result<(), Box> { )?; let mode = op["mode"].as_str().unwrap(); let result: Result = match mode { - "value" => attrs.value(key, ValueKind::Computed).map(bits), - "base" => attrs.value(key, ValueKind::Base).map(bits), + "value" => attrs.value(key, ValueKind::Computed).map(f64_hex), + "base" => attrs.value(key, ValueKind::Base).map(f64_hex), "has" => attrs.has_attribute(key).map(|v| v.to_string()), "has_modifier" => attrs.has_modifier(key, m.id()).map(|v| v.to_string()), - "modifier" => attrs.modifier_value(key, m.id()).map(bits), + "modifier" => attrs.modifier_value(key, m.id()).map(f64_hex), "ensure" => attrs.get_instance(key).map(|h| handle(h.as_ref())), "reset" => attrs.reset_base(key).map(|v| v.to_string()), "ack_update" => { @@ -208,9 +199,9 @@ pub fn main() -> Result<(), Box> { "instance {} {} {} {} {} | {} | {}", h.index(), key.name(), - bits(a.base_value()), + f64_hex(a.base_value()), a.is_dirty(), - bits(a.cached_value()), + f64_hex(a.cached_value()), refs(a.modifiers(), &mut pool), refs(a.permanent_modifiers(), &mut pool) )?; diff --git a/crates/horizon-world/examples/probe/living_air_fixture.rs b/crates/horizon-world/examples/probe/living_air_fixture.rs index bf77ce86f..882c5a8f3 100644 --- a/crates/horizon-world/examples/probe/living_air_fixture.rs +++ b/crates/horizon-world/examples/probe/living_air_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_world::living_air::*; use serde_json::Value; @@ -15,18 +16,16 @@ fn single(v: &Value) -> f32 { fn vec(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn fbits(v: f32) -> u32 { if v.is_nan() { 0x7fc00000 } else { v.to_bits() } } fn vector(v: Vec3) -> String { - format!("{:016x} {:016x} {:016x}", bits(v.x), bits(v.y), bits(v.z)) + format!( + "{:016x} {:016x} {:016x}", + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) + ) } struct World<'a> { f: &'a Value, @@ -239,7 +238,12 @@ pub fn main() -> Result<(), Box> { "result {}", if result.is_ok() { "ok" } else { "error" } )?; - writeln!(w.out, "final {} {:016x}", vector(w.velocity), bits(w.fall))?; + writeln!( + w.out, + "final {} {:016x}", + vector(w.velocity), + f64_bits(w.fall) + )?; writeln!(w.out, "queries {}", w.calls)?; } Ok(()) diff --git a/crates/horizon-world/examples/probe/living_damage_fixture.rs b/crates/horizon-world/examples/probe/living_damage_fixture.rs index 4e3c06887..d8097bde9 100644 --- a/crates/horizon-world/examples/probe/living_damage_fixture.rs +++ b/crates/horizon-world/examples/probe/living_damage_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::{ living_damage::{self, DamageStat, DamageWorld}, living_health::{self, HealthWorld}, @@ -14,13 +15,6 @@ fn single(v: &Value) -> f32 { fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn stat(s: DamageStat) -> &'static str { match s { DamageStat::Absorbed => "absorbed", @@ -69,11 +63,11 @@ impl DamageWorld for World<'_> { Ok(self.state()["invulnerable"].as_bool().unwrap()) } fn after_armor(&mut self, v: f32) -> io::Result { - self.query(&format!("armor {:08x}", bits(v)))?; + self.query(&format!("armor {:08x}", f32_bits(v)))?; Ok(single(&self.state()["armor"])) } fn after_magic(&mut self, v: f32) -> io::Result { - self.query(&format!("magic {:08x}", bits(v)))?; + self.query(&format!("magic {:08x}", f32_bits(v)))?; Ok(single(&self.state()["magic"])) } fn absorption(&mut self) -> io::Result { @@ -98,13 +92,13 @@ impl DamageWorld for World<'_> { Ok(single(&self.state()["food_exhaustion"])) } fn cause_food_exhaustion(&mut self, v: f32) -> io::Result<()> { - self.query(&format!("exhaust {:08x}", bits(v)))?; + self.query(&format!("exhaust {:08x}", f32_bits(v)))?; self.exhaustion += v; Ok(()) } fn record_damage(&mut self, v: f32) -> io::Result<()> { self.query("tracker")?; - self.query(&format!("record {:08x}", bits(v)))?; + self.query(&format!("record {:08x}", f32_bits(v)))?; self.recorded = v; Ok(()) } @@ -113,7 +107,7 @@ impl DamageWorld for World<'_> { Ok(self.health) } fn set_health(&mut self, v: f32) -> io::Result<()> { - self.query(&format!("set-health {:08x}", bits(v)))?; + self.query(&format!("set-health {:08x}", f32_bits(v)))?; self.health = v; Ok(()) } @@ -140,7 +134,7 @@ impl HealthWorld for World<'_> { DamageWorld::set_health(self, v) } fn store_absorption(&mut self, v: f32) -> io::Result<()> { - self.query(&format!("set-absorption {:08x}", bits(v)))?; + self.query(&format!("set-absorption {:08x}", f32_bits(v)))?; self.absorption = v; Ok(()) } @@ -181,10 +175,10 @@ pub fn main() -> Result<(), Box> { writeln!( w.out, "final {:08x} {:08x} {:08x} {:08x}", - bits(w.health), - bits(w.absorption), - bits(w.exhaustion), - bits(w.recorded) + f32_bits(w.health), + f32_bits(w.absorption), + f32_bits(w.exhaustion), + f32_bits(w.recorded) )?; writeln!(w.out, "queries {}", w.calls)?; } diff --git a/crates/horizon-world/examples/probe/living_fall_fixture.rs b/crates/horizon-world/examples/probe/living_fall_fixture.rs index 84d7fb9a3..2d65929ff 100644 --- a/crates/horizon-world/examples/probe/living_fall_fixture.rs +++ b/crates/horizon-world/examples/probe/living_fall_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::geometry::Vec3; use horizon_world::living_fall::{ ImpulseContext, LivingFallWorld, calculate_fall_damage, cause_living_fall_damage, @@ -14,13 +15,6 @@ fn number(v: &Value) -> f64 { fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn vector(v: &Value) -> Option { if v.is_null() { None @@ -60,9 +54,9 @@ impl World<'_> { writeln!( self.out, "impact {:016x} {:016x} {:016x}", - bits(p.x), - bits(p.y), - bits(p.z) + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z) ) } else { writeln!(self.out, "impact none") @@ -100,7 +94,7 @@ impl LivingFallWorld for World<'_> { fn propagate_fall_to_passengers(&mut self, d: f64, m: f32) -> io::Result<()> { self.query(format!( "passengers {:016x} {:08x}", - bits(d), + f64_bits(d), if m.is_nan() { 0x7fc00000 } else { m.to_bits() } ))?; if self.f["mutate"].as_bool().unwrap() { diff --git a/crates/horizon-world/examples/probe/living_fluid_fixture.rs b/crates/horizon-world/examples/probe/living_fluid_fixture.rs index 0bada6334..bd3704965 100644 --- a/crates/horizon-world/examples/probe/living_fluid_fixture.rs +++ b/crates/horizon-world/examples/probe/living_fluid_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_world::living_air::*; use horizon_world::living_fluid::*; @@ -16,18 +17,16 @@ fn single(v: &Value) -> f32 { fn vec(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn fbits(v: f32) -> u32 { if v.is_nan() { 0x7fc00000 } else { v.to_bits() } } fn vector(v: Vec3) -> String { - format!("{:016x} {:016x} {:016x}", bits(v.x), bits(v.y), bits(v.z)) + format!( + "{:016x} {:016x} {:016x}", + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) + ) } struct World<'a> { f: &'a Value, @@ -320,7 +319,12 @@ pub fn main() -> Result<(), Box> { "result {}", if result.is_ok() { "ok" } else { "error" } )?; - writeln!(w.out, "final {} {:016x}", vector(w.velocity), bits(w.fall))?; + writeln!( + w.out, + "final {} {:016x}", + vector(w.velocity), + f64_bits(w.fall) + )?; writeln!(w.out, "queries {}", w.calls)?; } Ok(()) diff --git a/crates/horizon-world/examples/probe/living_health_fixture.rs b/crates/horizon-world/examples/probe/living_health_fixture.rs index 2b2f98129..53d00b076 100644 --- a/crates/horizon-world/examples/probe/living_health_fixture.rs +++ b/crates/horizon-world/examples/probe/living_health_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::living_health::{self, HealthWorld, SyncedFloat}; use serde_json::Value; use std::{ @@ -11,13 +12,6 @@ fn single(v: &Value) -> f32 { fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} struct Context<'a> { f: &'a Value, out: &'a mut dyn Write, @@ -70,7 +64,7 @@ impl HealthWorld for World<'_> { self.stores[data].set(v, false, |_| self.ctx.query("updated")) } fn store_absorption(&mut self, v: f32) -> io::Result<()> { - self.ctx.query(&format!("absorption {:08x}", bits(v)))?; + self.ctx.query(&format!("absorption {:08x}", f32_bits(v)))?; self.absorption = v; Ok(()) } @@ -78,7 +72,7 @@ impl HealthWorld for World<'_> { self.ctx.world()["dead"].as_bool().unwrap() } fn dispatch_set_health(&mut self, v: f32) -> io::Result<()> { - self.ctx.query(&format!("set {:08x}", bits(v)))?; + self.ctx.query(&format!("set {:08x}", f32_bits(v)))?; living_health::set_health(self, v) } } @@ -123,7 +117,7 @@ pub fn main() -> Result<(), Box> { "packed {}", w.stores[w.ctx.active] .take_dirty() - .map(|v| format!("{:08x}", bits(v))) + .map(|v| format!("{:08x}", f32_bits(v))) .unwrap_or_else(|| "none".into()) ) } @@ -138,7 +132,7 @@ pub fn main() -> Result<(), Box> { writeln!( w.ctx.out, "data {i} {:08x} {} {} {}", - bits(s.value()), + f32_bits(s.value()), s.is_dirty(), s.item_dirty(), s.is_default() @@ -149,7 +143,7 @@ pub fn main() -> Result<(), Box> { w.ctx.out, "state {} {:08x} {dead} {}", w.ctx.active, - bits(w.absorption), + f32_bits(w.absorption), w.ctx.calls )?; } diff --git a/crates/horizon-world/examples/probe/living_hurt_fixture.rs b/crates/horizon-world/examples/probe/living_hurt_fixture.rs index 4c9e46230..b5ab3e890 100644 --- a/crates/horizon-world/examples/probe/living_hurt_fixture.rs +++ b/crates/horizon-world/examples/probe/living_hurt_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::living_hurt::{HurtState, HurtTag, HurtWorld, hurt_server}; use serde_json::Value; use std::{ @@ -8,19 +9,12 @@ use std::{ fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn state(s: &HurtState<()>) -> String { format!( "{} {} {:08x} {} {} {} {}", s.no_action_time, s.cooldown, - bits(s.last_hurt), + f32_bits(s.last_hurt), s.hurt_duration, s.hurt_time, s.last_source.is_some(), @@ -126,7 +120,7 @@ impl HurtWorld for Env<'_> { Ok(self.flag("component")) } fn apply_blocking(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result { - self.event(&format!("blocking {:08x}", bits(v)))?; + self.event(&format!("blocking {:08x}", f32_bits(v)))?; Ok(single(&self.op["blocked"])) } fn helmet_empty(&mut self, _: &mut HurtState<()>) -> io::Result { @@ -134,10 +128,10 @@ impl HurtWorld for Env<'_> { Ok(!self.flag("helmet")) } fn hurt_helmet(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<()> { - self.event(&format!("hurt-helmet {:08x}", bits(v))) + self.event(&format!("hurt-helmet {:08x}", f32_bits(v))) } fn actually_hurt(&mut self, s: &mut HurtState<()>, v: f32) -> io::Result<()> { - self.event(&format!("hurt {:08x} {}", bits(v), state(s)))?; + self.event(&format!("hurt {:08x} {}", f32_bits(v), state(s)))?; self.hit = true; self.recorded = v; if self.flag("mutate_hit") { @@ -155,7 +149,7 @@ impl HurtWorld for Env<'_> { Ok(()) } fn knockback(&mut self, _: &mut HurtState<()>, v: f32, b: bool) -> io::Result<()> { - self.event(&format!("knockback {:08x} {b}", bits(v))) + self.event(&format!("knockback {:08x} {b}", f32_bits(v))) } fn death_protection(&mut self, _: &mut HurtState<()>) -> io::Result { self.event("tag bypasses_invulnerability")?; @@ -179,7 +173,7 @@ impl HurtWorld for Env<'_> { fn effects_on_hurt(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<()> { self.event("effects")?; for i in 0..self.op["effects"].as_u64().unwrap() { - self.event(&format!("effect {i} {:08x}", bits(v)))?; + self.event(&format!("effect {i} {:08x}", f32_bits(v)))?; } Ok(()) } @@ -239,7 +233,7 @@ pub fn main() -> Result<(), Box> { e.out, "state {} {:08x} {} {}", state(&s), - bits(recorded), + f32_bits(recorded), e.hit, e.calls )?; diff --git a/crates/horizon-world/examples/probe/living_landing_fixture.rs b/crates/horizon-world/examples/probe/living_landing_fixture.rs index 87b438978..3c3e333d7 100644 --- a/crates/horizon-world/examples/probe/living_landing_fixture.rs +++ b/crates/horizon-world/examples/probe/living_landing_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_world::fall_damage::FallDamageWorld; use horizon_world::living_landing::{LivingLandingWorld, check_living_fall_damage}; @@ -10,13 +11,6 @@ use std::{ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn support(v: &Value) -> Option { if v.is_null() { None @@ -69,7 +63,7 @@ impl FallDamageWorld for World<'_> { p.x, p.y, p.z, - bits(distance) + f64_bits(distance) ))?; if self.input["mutate"].as_bool().unwrap() { self.fall = 17.25; @@ -91,9 +85,9 @@ impl FallDamageWorld for World<'_> { fn hit_ground(&mut self, p: Vec3, s: u8) -> io::Result<()> { self.query(format!( "hit-ground {:016x} {:016x} {:016x} {}", - bits(p.x), - bits(p.y), - bits(p.z), + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z), if s == 1 { "queried" } else { "input" } )) } @@ -142,11 +136,11 @@ impl LivingLandingWorld for World<'_> { assert_eq!(s, 0); self.query(format!( "particles {:016x} {:016x} {:016x} {} {:016x}", - bits(p.x), - bits(p.y), - bits(p.z), + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z), count, - bits(speed) + f64_bits(speed) ))?; if self.input["particle_mutate"].as_bool().unwrap() { self.fall = number(&self.input["after_fall"]); @@ -194,10 +188,10 @@ pub fn main() -> Result<(), Box> { writeln!( world.out, "final {:016x} {:016x} {:016x} {:016x}", - bits(world.fall), - bits(p.x), - bits(p.y), - bits(p.z) + f64_bits(world.fall), + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z) )?; if let Some(p) = world.support { writeln!(world.out, "support {} {} {}", p.x, p.y, p.z)?; diff --git a/crates/horizon-world/examples/probe/living_motion_fixture.rs b/crates/horizon-world/examples/probe/living_motion_fixture.rs index 174527053..96c0792ac 100644 --- a/crates/horizon-world/examples/probe/living_motion_fixture.rs +++ b/crates/horizon-world/examples/probe/living_motion_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::geometry::Vec3; use horizon_world::living_motion::*; use serde_json::Value; @@ -15,18 +16,16 @@ fn single(v: &Value) -> f32 { fn vec(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn fbits(v: f32) -> u32 { if v.is_nan() { 0x7fc00000 } else { v.to_bits() } } fn vector(v: Vec3) -> String { - format!("{:016x} {:016x} {:016x}", bits(v.x), bits(v.y), bits(v.z)) + format!( + "{:016x} {:016x} {:016x}", + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) + ) } struct World<'a> { f: &'a Value, @@ -138,10 +137,11 @@ pub fn main() -> Result<(), Box> { "power" => jump_power(&mut w, single(&f["scale"])) .and_then(|v| writeln!(w.out, "float {:08x}", fbits(v))), "jump" => jump_from_ground(&mut w), - "gravity" => gravity(&mut w).and_then(|v| writeln!(w.out, "double {:016x}", bits(v))), - "effective" => { - effective_gravity(&mut w).and_then(|v| writeln!(w.out, "double {:016x}", bits(v))) + "gravity" => { + gravity(&mut w).and_then(|v| writeln!(w.out, "double {:016x}", f64_bits(v))) } + "effective" => effective_gravity(&mut w) + .and_then(|v| writeln!(w.out, "double {:016x}", f64_bits(v))), "apply" => apply_gravity(&mut w), "up" => swim_vertical(&mut w, true), "down" => swim_vertical(&mut w, false), diff --git a/crates/horizon-world/examples/probe/main.rs b/crates/horizon-world/examples/probe/main.rs index 470fabd2b..d9a0289b8 100644 --- a/crates/horizon-world/examples/probe/main.rs +++ b/crates/horizon-world/examples/probe/main.rs @@ -5,8 +5,10 @@ use std::process::{ExitCode, Termination}; /// Code shared by several probes. mod support { + pub mod common; pub mod decoration_storage; pub mod placement_queries; + pub mod shapes; } mod active_effect_tick_fixture; diff --git a/crates/horizon-world/examples/probe/material_stage_fixture.rs b/crates/horizon-world/examples/probe/material_stage_fixture.rs index 9ae27db9d..31a563f7c 100644 --- a/crates/horizon-world/examples/probe/material_stage_fixture.rs +++ b/crates/horizon-world/examples/probe/material_stage_fixture.rs @@ -1,4 +1,5 @@ //! Original transport for complete surface-stage chunk comparison. +use crate::support::common::tag; use horizon_core::{ biome::{BiomeZoom, obfuscate_seed}, carving::CarvingMask, @@ -78,17 +79,6 @@ impl TerrainBlockStates for Properties<'_> { self.0[id.0 as usize] } } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(o) => { - Tag::Compound(o.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} fn n(v: &Value) -> i32 { v.as_i64().unwrap() as i32 } diff --git a/crates/horizon-world/examples/probe/movement_emission_fixture.rs b/crates/horizon-world/examples/probe/movement_emission_fixture.rs index b49ac7b33..70ef1b32b 100644 --- a/crates/horizon-world/examples/probe/movement_emission_fixture.rs +++ b/crates/horizon-world/examples/probe/movement_emission_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::{f64_bits, position}; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_world::movement_emission::*; use serde_json::Value; @@ -15,23 +16,9 @@ fn single(v: &Value) -> f32 { fn vector(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn position(v: &Value) -> BlockPos { - BlockPos::new( - v[0].as_i64().unwrap() as i32, - v[1].as_i64().unwrap() as i32, - v[2].as_i64().unwrap() as i32, - ) -} fn pos(p: BlockPos) -> String { format!("{} {} {}", p.x, p.y, p.z) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn fbits(v: f32) -> u32 { if v.is_nan() { 0x7fc00000 } else { v.to_bits() } } @@ -137,9 +124,9 @@ impl MovementEmissionWorld for World<'_> { let p = vector(&self.f["position"]); self.query(format!( "step {s} {:016x} {:016x} {:016x}", - bits(p.x), - bits(p.y), - bits(p.z) + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z) ))?; self.mutate(); Ok(()) diff --git a/crates/horizon-world/examples/probe/movement_registry_fixture.rs b/crates/horizon-world/examples/probe/movement_registry_fixture.rs index 4ebe76a39..28a934ee5 100644 --- a/crates/horizon-world/examples/probe/movement_registry_fixture.rs +++ b/crates/horizon-world/examples/probe/movement_registry_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::position; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_world::movement_support::{self as support, MovementLocation}; use horizon_world::{ @@ -21,13 +22,6 @@ fn number(v: &Value) -> f64 { fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn position(v: &Value) -> BlockPos { - BlockPos::new( - v[0].as_i64().unwrap() as i32, - v[1].as_i64().unwrap() as i32, - v[2].as_i64().unwrap() as i32, - ) -} struct World<'a> { input: &'a Value, out: &'a mut dyn Write, diff --git a/crates/horizon-world/examples/probe/movement_support_fixture.rs b/crates/horizon-world/examples/probe/movement_support_fixture.rs index df6ffb046..1206aa0dc 100644 --- a/crates/horizon-world/examples/probe/movement_support_fixture.rs +++ b/crates/horizon-world/examples/probe/movement_support_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::position; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_world::movement_support::{self as support, MovementBlockWorld, MovementLocation}; use serde_json::Value; @@ -12,13 +13,6 @@ fn number(v: &Value) -> f64 { fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn position(v: &Value) -> BlockPos { - BlockPos::new( - v[0].as_i64().unwrap() as i32, - v[1].as_i64().unwrap() as i32, - v[2].as_i64().unwrap() as i32, - ) -} struct World<'a> { input: &'a Value, out: &'a mut dyn Write, diff --git a/crates/horizon-world/examples/probe/piston_movement_fixture.rs b/crates/horizon-world/examples/probe/piston_movement_fixture.rs index 68af8348e..804ccec2e 100644 --- a/crates/horizon-world/examples/probe/piston_movement_fixture.rs +++ b/crates/horizon-world/examples/probe/piston_movement_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::geometry::Vec3; use horizon_world::piston_movement::PistonMovement; use serde_json::Value; @@ -12,13 +13,6 @@ fn number(v: &Value) -> f64 { fn vector(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); @@ -45,18 +39,18 @@ pub fn main() -> Result<(), Box> { Ok(v) => writeln!( out, "movement {:016x} {:016x} {:016x}", - bits(v.x), - bits(v.y), - bits(v.z) + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) )?, Err(_) => writeln!(out, "error")?, } writeln!( out, "deltas {:016x} {:016x} {:016x}", - bits(state.deltas[0]), - bits(state.deltas[1]), - bits(state.deltas[2]) + f64_bits(state.deltas[0]), + f64_bits(state.deltas[1]), + f64_bits(state.deltas[2]) )?; writeln!(out, "tick {}", state.game_time)?; writeln!(out, "queries {calls}")?; diff --git a/crates/horizon-world/examples/probe/piston_shape_fixture.rs b/crates/horizon-world/examples/probe/piston_shape_fixture.rs index 4eb07b4d8..ec75a9502 100644 --- a/crates/horizon-world/examples/probe/piston_shape_fixture.rs +++ b/crates/horizon-world/examples/probe/piston_shape_fixture.rs @@ -1,4 +1,5 @@ -use horizon_core::geometry::{Aabb, Axis, Direction, VoxelShape}; +use crate::support::shapes::emit; +use horizon_core::geometry::{Direction, VoxelShape}; use horizon_world::{ collision::CachedCollisionRegistry, dynamic_support::{DynamicSupport, DynamicSupportWorld}, @@ -12,70 +13,6 @@ use std::{ io::{self, Write}, path::Path, }; -fn values( - out: &mut impl Write, - label: &str, - values: impl IntoIterator, -) -> io::Result<()> { - write!(out, "{label}")?; - for v in values { - let bits = if v.is_nan() { - 0x7ff8_0000_0000_0000 - } else { - v.to_bits() - }; - write!(out, " {bits:016x}")?; - } - writeln!(out) -} -fn box_values(out: &mut impl Write, label: &str, b: Aabb) -> io::Result<()> { - values( - out, - label, - [ - b.min(Axis::X), - b.min(Axis::Y), - b.min(Axis::Z), - b.max(Axis::X), - b.max(Axis::Y), - b.max(Axis::Z), - ], - ) -} -fn emit(out: &mut impl Write, shape: &VoxelShape) -> io::Result<()> { - writeln!( - out, - "shape {} {} {}", - shape.is_empty(), - shape.boxes().len(), - shape.is_cube() - )?; - for (axis, name) in Axis::ALL.into_iter().zip(["x", "y", "z"]) { - values(out, name, shape.coordinates(axis).iter().copied())?; - } - let axes = shape.cube_coordinate_axes(); - writeln!( - out, - "axes {}", - u8::from(axes[0]) + 2 * u8::from(axes[1]) + 4 * u8::from(axes[2]) - )?; - values( - out, - "extrema", - Axis::ALL - .map(|a| shape.min(a)) - .into_iter() - .chain(Axis::ALL.map(|a| shape.max(a))), - )?; - match shape.bounds() { - Some(b) => box_values(out, "bounds", b)?, - None => writeln!(out, "bounds none")?, - } - for b in shape.boxes() { - box_values(out, "box", *b)?; - } - Ok(()) -} struct World<'a, 'r, W> { collisions: &'a CachedCollisionRegistry<'r>, dynamic: &'a [Option], diff --git a/crates/horizon-world/examples/probe/player_food_fixture.rs b/crates/horizon-world/examples/probe/player_food_fixture.rs index e2ef7d450..186ec6332 100644 --- a/crates/horizon-world/examples/probe/player_food_fixture.rs +++ b/crates/horizon-world/examples/probe/player_food_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::{ food::FoodData, player_food::{self, PlayerFoodWorld, StoredPlayerFood}, @@ -11,13 +12,6 @@ use std::{ fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} struct World<'a> { f: &'a Value, out: &'a mut dyn Write, @@ -68,7 +62,7 @@ impl PlayerFoodWorld for World<'_> { } fn add_exhaustion_direct(&mut self, v: f32) -> io::Result<()> { let i = self.index("direct"); - self.query(&format!("exhaust {i} {:08x}", bits(v)))?; + self.query(&format!("exhaust {i} {:08x}", f32_bits(v)))?; self.foods[i].add_exhaustion(v); Ok(()) } @@ -136,7 +130,7 @@ pub fn main() -> Result<(), Box> { w.out, "food {i} {} {:08x}", food.food, - bits(food.exhaustion) + f32_bits(food.exhaustion) )?; } writeln!(w.out, "queries {}", w.calls)?; diff --git a/crates/horizon-world/examples/probe/player_health_fixture.rs b/crates/horizon-world/examples/probe/player_health_fixture.rs index 4a4987de3..24163bb75 100644 --- a/crates/horizon-world/examples/probe/player_health_fixture.rs +++ b/crates/horizon-world/examples/probe/player_health_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::{ attributes::{ Modifier, Operation, @@ -19,20 +20,13 @@ fn single(v: &Value) -> f32 { fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn values(v: Option<&[FloatValue]>) -> String { match v { None => "none".into(), Some([]) => "empty".into(), Some(v) => v .iter() - .map(|v| format!("{}:{:08x}", v.id, bits(v.value))) + .map(|v| format!("{}:{:08x}", v.id, f32_bits(v.value))) .collect::>() .join(","), } @@ -59,7 +53,7 @@ impl Observer for Callback<'_> { fn updated(&mut self, data: &mut SyncedFloats, id: u8) -> io::Result<()> { self.event(&format!( "updated {id} {:08x} {}", - bits(data.get(id).unwrap()), + f32_bits(data.get(id).unwrap()), data.is_dirty() ))?; if self.op["pack_callback"].as_bool().unwrap() { @@ -145,7 +139,7 @@ pub fn main() -> Result<(), Box> { .bind(&mut cb) .absorption() .map_err(|_| ()) - .and_then(|v| writeln!(cb.out, "float {:08x}", bits(v)).map_err(|_| ())), + .and_then(|v| writeln!(cb.out, "float {:08x}", f32_bits(v)).map_err(|_| ())), "hurt" => state .bind(&mut cb) .is_hurt() @@ -227,8 +221,8 @@ pub fn main() -> Result<(), Box> { writeln!( cb.out, "state {:08x} {:08x} {} {} {} {} {} {}", - bits(state.data.get(ids.health).unwrap()), - bits(state.data.get(ids.absorption).unwrap()), + f32_bits(state.data.get(ids.health).unwrap()), + f32_bits(state.data.get(ids.absorption).unwrap()), state.data.is_dirty(), state.data.item_dirty(ids.health).unwrap(), state.data.item_dirty(ids.absorption).unwrap(), diff --git a/crates/horizon-world/examples/probe/player_hurt_fixture.rs b/crates/horizon-world/examples/probe/player_hurt_fixture.rs index 765f7b04b..6e77abf71 100644 --- a/crates/horizon-world/examples/probe/player_hurt_fixture.rs +++ b/crates/horizon-world/examples/probe/player_hurt_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::living_hurt::{HurtState, HurtTag, HurtWorld}; use horizon_world::{ food::Difficulty, @@ -15,19 +16,12 @@ use std::{ fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn state(s: &HurtState<()>) -> String { format!( "{} {} {:08x} {} {} {} {}", s.no_action_time, s.cooldown, - bits(s.last_hurt), + f32_bits(s.last_hurt), s.hurt_duration, s.hurt_time, s.last_source.is_some(), @@ -134,7 +128,7 @@ impl HurtWorld for Env<'_> { Ok(self.flag("component")) } fn apply_blocking(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result { - self.event(&format!("blocking {:08x}", bits(v)))?; + self.event(&format!("blocking {:08x}", f32_bits(v)))?; Ok(single(&self.op["blocked"])) } fn helmet_empty(&mut self, _: &mut HurtState<()>) -> io::Result { @@ -142,10 +136,10 @@ impl HurtWorld for Env<'_> { Ok(!self.flag("helmet")) } fn hurt_helmet(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<()> { - self.event(&format!("hurt-helmet {:08x}", bits(v))) + self.event(&format!("hurt-helmet {:08x}", f32_bits(v))) } fn actually_hurt(&mut self, s: &mut HurtState<()>, v: f32) -> io::Result<()> { - self.event(&format!("hurt {:08x} {}", bits(v), state(s)))?; + self.event(&format!("hurt {:08x} {}", f32_bits(v), state(s)))?; self.hit = true; self.recorded = v; if self.flag("mutate_hit") { @@ -163,7 +157,7 @@ impl HurtWorld for Env<'_> { Ok(()) } fn knockback(&mut self, _: &mut HurtState<()>, v: f32, b: bool) -> io::Result<()> { - self.event(&format!("knockback {:08x} {b}", bits(v))) + self.event(&format!("knockback {:08x} {b}", f32_bits(v))) } fn death_protection(&mut self, _: &mut HurtState<()>) -> io::Result { self.event("tag bypasses_invulnerability")?; @@ -187,7 +181,7 @@ impl HurtWorld for Env<'_> { fn effects_on_hurt(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<()> { self.event("effects")?; for i in 0..self.op["effects"].as_u64().unwrap() { - self.event(&format!("effect {i} {:08x}", bits(v)))?; + self.event(&format!("effect {i} {:08x}", f32_bits(v)))?; } Ok(()) } @@ -303,7 +297,7 @@ pub fn main() -> Result<(), Box> { e.out, "state {} {:08x} {} {}", state(&s), - bits(recorded), + f32_bits(recorded), e.hit, e.calls )?; diff --git a/crates/horizon-world/examples/probe/player_travel_fixture.rs b/crates/horizon-world/examples/probe/player_travel_fixture.rs index 5627ef6a2..e4d493662 100644 --- a/crates/horizon-world/examples/probe/player_travel_fixture.rs +++ b/crates/horizon-world/examples/probe/player_travel_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_world::player_travel::*; use serde_json::Value; @@ -15,18 +16,16 @@ fn single(v: &Value) -> f32 { fn vec(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn fbits(v: f32) -> u32 { if v.is_nan() { 0x7fc00000 } else { v.to_bits() } } fn vector(v: Vec3) -> String { - format!("{:016x} {:016x} {:016x}", bits(v.x), bits(v.y), bits(v.z)) + format!( + "{:016x} {:016x} {:016x}", + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) + ) } struct World<'a> { f: &'a Value, diff --git a/crates/horizon-world/examples/probe/registered_player_combat_fixture.rs b/crates/horizon-world/examples/probe/registered_player_combat_fixture.rs index 4029b4918..c70881165 100644 --- a/crates/horizon-world/examples/probe/registered_player_combat_fixture.rs +++ b/crates/horizon-world/examples/probe/registered_player_combat_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::damage_immunity::{DamageRule, ImmunityTag, ImmunityWorld}; use horizon_world::damage_source::{ Catalog as DamageCatalog, DamageSource, EntityKind, SourceWorld, @@ -31,19 +32,12 @@ use std::{ fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn state(s: &HurtState<()>) -> String { format!( "{} {} {:08x} {} {} {} {}", s.no_action_time, s.cooldown, - bits(s.last_hurt), + f32_bits(s.last_hurt), s.hurt_duration, s.hurt_time, s.last_source.is_some(), @@ -151,7 +145,7 @@ impl HurtWorld for Env<'_> { Ok(self.flag("component")) } fn apply_blocking(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result { - self.event(&format!("blocking {:08x}", bits(v)))?; + self.event(&format!("blocking {:08x}", f32_bits(v)))?; Ok(single(&self.op["blocked"])) } fn helmet_empty(&mut self, _: &mut HurtState<()>) -> io::Result { @@ -159,7 +153,7 @@ impl HurtWorld for Env<'_> { Ok(!self.flag("helmet")) } fn hurt_helmet(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<()> { - self.event(&format!("hurt-helmet {:08x}", bits(v))) + self.event(&format!("hurt-helmet {:08x}", f32_bits(v))) } fn actually_hurt(&mut self, _s: &mut HurtState<()>, _v: f32) -> io::Result<()> { panic!("combat binding supplies actual inner damage") @@ -171,7 +165,7 @@ impl HurtWorld for Env<'_> { Ok(()) } fn knockback(&mut self, _: &mut HurtState<()>, v: f32, b: bool) -> io::Result<()> { - self.event(&format!("knockback {:08x} {b}", bits(v))) + self.event(&format!("knockback {:08x} {b}", f32_bits(v))) } fn death_protection(&mut self, _: &mut HurtState<()>) -> io::Result { if !self @@ -197,7 +191,7 @@ impl HurtWorld for Env<'_> { fn effects_on_hurt(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<()> { self.event("effects")?; for i in 0..self.op["effects"].as_u64().unwrap() { - self.event(&format!("effect {i} {:08x}", bits(v)))?; + self.event(&format!("effect {i} {:08x}", f32_bits(v)))?; } Ok(()) } @@ -280,7 +274,7 @@ fn values(v: Option<&[FloatValue]>) -> String { Some([]) => "empty".into(), Some(v) => v .iter() - .map(|v| format!("{}:{:08x}", v.id, bits(v.value))) + .map(|v| format!("{}:{:08x}", v.id, f32_bits(v.value))) .collect::>() .join(","), } @@ -290,7 +284,7 @@ impl Observer for Env<'_> { fn updated(&mut self, data: &mut SyncedFloats, id: u8) -> io::Result<()> { self.event(&format!( "updated {id} {:08x} {}", - bits(data.get(id).unwrap()), + f32_bits(data.get(id).unwrap()), data.is_dirty() ))?; if self.flag("pack_callback") { @@ -329,7 +323,7 @@ impl ServerDamageEnvironment for Env<'_> { Ok(self.source.bypasses(b)) } fn hurt_armor(&mut self, a: &mut EntityAttributes, v: f32) -> io::Result<()> { - self.event(&format!("hurt-armor {:08x}", bits(v)))?; + self.event(&format!("hurt-armor {:08x}", f32_bits(v)))?; if self.flag("break_armor") { let id = a.catalog().id("minecraft:armor").unwrap(); a.edit(id, |a, o| { @@ -387,7 +381,7 @@ impl ServerDamageEnvironment for Env<'_> { } fn record_damage(&mut self, v: f32) -> io::Result<()> { self.event("tracker")?; - self.event(&format!("record {:08x}", bits(v)))?; + self.event(&format!("record {:08x}", f32_bits(v)))?; self.recorded = v; self.hit = true; Ok(()) @@ -513,16 +507,16 @@ pub fn main() -> Result<(), Box> { e.out, "state {} {:08x} {} {}", state(&s), - bits(recorded), + f32_bits(recorded), e.hit, e.calls )?; writeln!( e.out, "storage {:08x} {:08x} {:08x} {} {} {} {}", - bits(data.get(ids.health).unwrap()), - bits(data.get(ids.absorption).unwrap()), - bits(food.exhaustion), + f32_bits(data.get(ids.health).unwrap()), + f32_bits(data.get(ids.absorption).unwrap()), + f32_bits(food.exhaustion), data.is_dirty(), data.item_dirty(ids.health).unwrap(), data.item_dirty(ids.absorption).unwrap(), diff --git a/crates/horizon-world/examples/probe/registry_collision_scan_fixture.rs b/crates/horizon-world/examples/probe/registry_collision_scan_fixture.rs index f2504eab4..b66812142 100644 --- a/crates/horizon-world/examples/probe/registry_collision_scan_fixture.rs +++ b/crates/horizon-world/examples/probe/registry_collision_scan_fixture.rs @@ -1,4 +1,5 @@ //! Integrated registry/cursor differential transport using resident TerrainChunk storage. +use crate::support::shapes::{emit, values}; use horizon_core::{ geometry::{Aabb, Axis, Vec3, VoxelShape}, position::{BlockPos, ChunkPos}, @@ -43,70 +44,6 @@ use std::{ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn values( - out: &mut impl Write, - label: &str, - values: impl IntoIterator, -) -> io::Result<()> { - write!(out, "{label}")?; - for v in values { - let bits = if v.is_nan() { - 0x7ff8_0000_0000_0000 - } else { - v.to_bits() - }; - write!(out, " {bits:016x}")?; - } - writeln!(out) -} -fn box_values(out: &mut impl Write, label: &str, b: Aabb) -> io::Result<()> { - values( - out, - label, - [ - b.min(Axis::X), - b.min(Axis::Y), - b.min(Axis::Z), - b.max(Axis::X), - b.max(Axis::Y), - b.max(Axis::Z), - ], - ) -} -fn emit(out: &mut impl Write, shape: &VoxelShape) -> io::Result<()> { - writeln!( - out, - "shape {} {} {}", - shape.is_empty(), - shape.boxes().len(), - shape.is_cube() - )?; - for (axis, name) in Axis::ALL.into_iter().zip(["x", "y", "z"]) { - values(out, name, shape.coordinates(axis).iter().copied())?; - } - let axes = shape.cube_coordinate_axes(); - writeln!( - out, - "axes {}", - u8::from(axes[0]) + 2 * u8::from(axes[1]) + 4 * u8::from(axes[2]) - )?; - values( - out, - "extrema", - Axis::ALL - .map(|a| shape.min(a)) - .into_iter() - .chain(Axis::ALL.map(|a| shape.max(a))), - )?; - match shape.bounds() { - Some(b) => box_values(out, "bounds", b)?, - None => writeln!(out, "bounds none")?, - } - for b in shape.boxes() { - box_values(out, "box", *b)?; - } - Ok(()) -} struct Trace { out: io::BufWriter, calls: i32, diff --git a/crates/horizon-world/examples/probe/registry_fall_ray_fixture.rs b/crates/horizon-world/examples/probe/registry_fall_ray_fixture.rs index c3861308e..c4f61644c 100644 --- a/crates/horizon-world/examples/probe/registry_fall_ray_fixture.rs +++ b/crates/horizon-world/examples/probe/registry_fall_ray_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_world::{ entity_obstruction::EntityIdentity, @@ -21,13 +22,6 @@ fn number(v: &Value) -> f64 { fn vector(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn pos(p: BlockPos) -> String { format!("{} {} {}", p.x, p.y, p.z) } @@ -181,9 +175,9 @@ pub fn main() -> Result<(), Box> { w.out, "hit {} {:016x} {:016x} {:016x} {} {} {}", r.miss, - bits(h.location.x), - bits(h.location.y), - bits(h.location.z), + f64_bits(h.location.x), + f64_bits(h.location.y), + f64_bits(h.location.z), format!("{:?}", h.direction).to_lowercase(), pos(h.block), h.inside diff --git a/crates/horizon-world/examples/probe/replace_block_fixture.rs b/crates/horizon-world/examples/probe/replace_block_fixture.rs index 7d4f05d7c..d0a742f99 100644 --- a/crates/horizon-world/examples/probe/replace_block_fixture.rs +++ b/crates/horizon-world/examples/probe/replace_block_fixture.rs @@ -1,4 +1,5 @@ //! Feature world transport retaining ordered mutations and partial failures. +use crate::support::common::tag; use horizon_core::random::{LegacyRandom, RandomSource, XoroshiroRandom}; use horizon_world::{ DATA_VERSION, @@ -135,17 +136,6 @@ fn fixture( writeln!(out, "tail {}", random.next_long())?; Ok(()) } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); diff --git a/crates/horizon-world/examples/probe/restitution_fixture.rs b/crates/horizon-world/examples/probe/restitution_fixture.rs index 1432d92a8..0d630274e 100644 --- a/crates/horizon-world/examples/probe/restitution_fixture.rs +++ b/crates/horizon-world/examples/probe/restitution_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{geometry::Vec3, position::BlockPos}; use horizon_world::{ movement_restitution::{RestitutionInput, RestitutionWorld, restitute_movement}, @@ -18,19 +19,12 @@ fn single(v: &Value) -> f32 { fn vector(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn vector_text(prefix: &str, v: Vec3) -> String { format!( "{prefix} {:016x} {:016x} {:016x}", - bits(v.x), - bits(v.y), - bits(v.z) + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) ) } struct World<'a, W> { @@ -102,7 +96,7 @@ impl RestitutionWorld for World<'_, W> { p.x, p.y, p.z, - bits(fall) + f64_bits(fall) ))?; if self.f["mutate"].as_bool().unwrap() { self.velocity = Vec3::new(17.0, 18.0, 19.0); diff --git a/crates/horizon-world/examples/probe/rule_test_fixture.rs b/crates/horizon-world/examples/probe/rule_test_fixture.rs index 8974eed1a..42a25532c 100644 --- a/crates/horizon-world/examples/probe/rule_test_fixture.rs +++ b/crates/horizon-world/examples/probe/rule_test_fixture.rs @@ -1,4 +1,5 @@ //! Differential transport for replacement rules over pinned block identities. +use crate::support::common::tag; use horizon_core::random::{LegacyRandom, RandomSource, XoroshiroRandom}; use horizon_world::{ DATA_VERSION, @@ -41,17 +42,6 @@ fn fixture( writeln!(out, "tail {}", random.next_long())?; Ok(()) } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); diff --git a/crates/horizon-world/examples/probe/scattered_ore_fixture.rs b/crates/horizon-world/examples/probe/scattered_ore_fixture.rs index 2c43f2b83..91b6821b7 100644 --- a/crates/horizon-world/examples/probe/scattered_ore_fixture.rs +++ b/crates/horizon-world/examples/probe/scattered_ore_fixture.rs @@ -1,4 +1,5 @@ //! Feature world transport retaining ordered mutations and partial failures. +use crate::support::common::tag; use horizon_core::random::{LegacyRandom, RandomSource, XoroshiroRandom}; use horizon_world::{ DATA_VERSION, @@ -159,17 +160,6 @@ fn fixture( writeln!(out, "tail {}", random.next_long())?; Ok(()) } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); diff --git a/crates/horizon-world/examples/probe/server_player_combat_fixture.rs b/crates/horizon-world/examples/probe/server_player_combat_fixture.rs index cceb1f001..9607dcec2 100644 --- a/crates/horizon-world/examples/probe/server_player_combat_fixture.rs +++ b/crates/horizon-world/examples/probe/server_player_combat_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::damage_immunity::{DamageRule, ImmunityTag, ImmunityWorld}; use horizon_world::living_hurt::{HurtState, HurtTag, HurtWorld}; use horizon_world::{ @@ -84,19 +85,12 @@ impl FixtureAttributes for Live { fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn state(s: &HurtState<()>) -> String { format!( "{} {} {:08x} {} {} {} {}", s.no_action_time, s.cooldown, - bits(s.last_hurt), + f32_bits(s.last_hurt), s.hurt_duration, s.hurt_time, s.last_source.is_some(), @@ -208,7 +202,7 @@ impl HurtWorld for Env<'_> { Ok(self.flag("component")) } fn apply_blocking(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result { - self.event(&format!("blocking {:08x}", bits(v)))?; + self.event(&format!("blocking {:08x}", f32_bits(v)))?; Ok(single(&self.op["blocked"])) } fn helmet_empty(&mut self, _: &mut HurtState<()>) -> io::Result { @@ -216,7 +210,7 @@ impl HurtWorld for Env<'_> { Ok(!self.flag("helmet")) } fn hurt_helmet(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<()> { - self.event(&format!("hurt-helmet {:08x}", bits(v))) + self.event(&format!("hurt-helmet {:08x}", f32_bits(v))) } fn actually_hurt(&mut self, _s: &mut HurtState<()>, _v: f32) -> io::Result<()> { panic!("combat binding supplies actual inner damage") @@ -228,7 +222,7 @@ impl HurtWorld for Env<'_> { Ok(()) } fn knockback(&mut self, _: &mut HurtState<()>, v: f32, b: bool) -> io::Result<()> { - self.event(&format!("knockback {:08x} {b}", bits(v))) + self.event(&format!("knockback {:08x} {b}", f32_bits(v))) } fn death_protection(&mut self, _: &mut HurtState<()>) -> io::Result { self.event("tag bypasses_invulnerability")?; @@ -252,7 +246,7 @@ impl HurtWorld for Env<'_> { fn effects_on_hurt(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<()> { self.event("effects")?; for i in 0..self.op["effects"].as_u64().unwrap() { - self.event(&format!("effect {i} {:08x}", bits(v)))?; + self.event(&format!("effect {i} {:08x}", f32_bits(v)))?; } Ok(()) } @@ -337,7 +331,7 @@ fn values(v: Option<&[FloatValue]>) -> String { Some([]) => "empty".into(), Some(v) => v .iter() - .map(|v| format!("{}:{:08x}", v.id, bits(v.value))) + .map(|v| format!("{}:{:08x}", v.id, f32_bits(v.value))) .collect::>() .join(","), } @@ -347,7 +341,7 @@ impl Observer for Env<'_> { fn updated(&mut self, data: &mut SyncedFloats, id: u8) -> io::Result<()> { self.event(&format!( "updated {id} {:08x} {}", - bits(data.get(id).unwrap()), + f32_bits(data.get(id).unwrap()), data.is_dirty() ))?; if self.flag("pack_callback") { @@ -393,7 +387,7 @@ impl ServerDamageEnvironment for Env<'_> { Ok(self.flag(&format!("bypass_{n}"))) } fn hurt_armor(&mut self, a: &mut A, v: f32) -> io::Result<()> { - self.event(&format!("hurt-armor {:08x}", bits(v)))?; + self.event(&format!("hurt-armor {:08x}", f32_bits(v)))?; if self.flag("break_armor") { a.configure("minecraft:armor", number(&self.op["armor_after"]), None); } @@ -447,7 +441,7 @@ impl ServerDamageEnvironment for Env<'_> { } fn record_damage(&mut self, v: f32) -> io::Result<()> { self.event("tracker")?; - self.event(&format!("record {:08x}", bits(v)))?; + self.event(&format!("record {:08x}", f32_bits(v)))?; self.recorded = v; self.hit = true; Ok(()) @@ -554,16 +548,16 @@ fn run(dir: &Path, output: &str) -> Result<(), Box f32 { fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn values(v: Option<&[FloatValue]>) -> String { match v { None => "none".into(), Some([]) => "empty".into(), Some(v) => v .iter() - .map(|v| format!("{}:{:08x}", v.id, bits(v.value))) + .map(|v| format!("{}:{:08x}", v.id, f32_bits(v.value))) .collect::>() .join(","), } @@ -69,7 +63,7 @@ impl Observer for Env<'_> { fn updated(&mut self, data: &mut SyncedFloats, id: u8) -> io::Result<()> { self.event(&format!( "updated {id} {:08x} {}", - bits(data.get(id).unwrap()), + f32_bits(data.get(id).unwrap()), data.is_dirty() ))?; if self.flag("pack_callback") { @@ -115,7 +109,7 @@ impl ServerDamageEnvironment for Env<'_> { Ok(self.flag(&format!("bypass_{n}"))) } fn hurt_armor(&mut self, a: &mut EntityAttributes, v: f32) -> io::Result<()> { - self.event(&format!("hurt-armor {:08x}", bits(v)))?; + self.event(&format!("hurt-armor {:08x}", f32_bits(v)))?; if self.flag("break_armor") { let id = a.catalog().id("minecraft:armor").unwrap(); a.edit(id, |a, o| { @@ -174,7 +168,7 @@ impl ServerDamageEnvironment for Env<'_> { } fn record_damage(&mut self, v: f32) -> io::Result<()> { self.event("tracker")?; - self.event(&format!("record {:08x}", bits(v)))?; + self.event(&format!("record {:08x}", f32_bits(v)))?; self.recorded = v; Ok(()) } @@ -255,10 +249,10 @@ pub fn main() -> Result<(), Box> { writeln!( env.out, "state {:08x} {:08x} {:08x} {:08x} {} {} {} {}", - bits(data.get(ids.health).unwrap()), - bits(data.get(ids.absorption).unwrap()), - bits(food.exhaustion), - bits(recorded), + f32_bits(data.get(ids.health).unwrap()), + f32_bits(data.get(ids.absorption).unwrap()), + f32_bits(food.exhaustion), + f32_bits(recorded), data.is_dirty(), data.item_dirty(ids.health).unwrap(), data.item_dirty(ids.absorption).unwrap(), diff --git a/crates/horizon-world/examples/probe/spawn_origin.rs b/crates/horizon-world/examples/probe/spawn_origin.rs index bf62726af..7162a00a8 100644 --- a/crates/horizon-world/examples/probe/spawn_origin.rs +++ b/crates/horizon-world/examples/probe/spawn_origin.rs @@ -3,6 +3,7 @@ //! (NoiseBasedChunkGenerator.getOrigin), as `seed x z chunk_x chunk_z` lines. //! //! usage: spawn_origin ... +use crate::support::common::tag; use horizon_world::{ DATA_VERSION, nbt::Tag, @@ -14,17 +15,6 @@ use std::{fs, path::Path}; type Error = Box; -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(o) => { - Tag::Compound(o.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} fn registry(resources: &Path) -> Result<&'static BlockRegistry, Error> { let facts: Value = serde_json::from_str(&fs::read_to_string(resources.join("facts.json"))?)?; let states = facts["states"].as_array().ok_or("facts states")?; diff --git a/crates/horizon-world/examples/probe/state_provider_fixture.rs b/crates/horizon-world/examples/probe/state_provider_fixture.rs index 68d91a1cf..6164e5d6c 100644 --- a/crates/horizon-world/examples/probe/state_provider_fixture.rs +++ b/crates/horizon-world/examples/probe/state_provider_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::tag; use horizon_core::random::{LegacyRandom, RandomSource, WorldgenRandom, XoroshiroRandom}; use horizon_world::{ DATA_VERSION, @@ -288,17 +289,6 @@ fn fixture( writeln!(out, "tail {}", random.next_long())?; Ok(()) } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - _ => panic!("invalid registry metadata"), - } -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); diff --git a/crates/horizon-world/examples/probe/stored_effect_attributes_fixture.rs b/crates/horizon-world/examples/probe/stored_effect_attributes_fixture.rs index b904a0520..10ae7bf63 100644 --- a/crates/horizon-world/examples/probe/stored_effect_attributes_fixture.rs +++ b/crates/horizon-world/examples/probe/stored_effect_attributes_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_hex; use horizon_world::{ attribute_map::AttributeMap, attributes::{AttributeError, Definition, Instance, Modifier, Observer, Operation}, @@ -38,16 +39,6 @@ enum Stop { Null, Bounds, } -fn bits(v: f64) -> String { - format!( - "{:016x}", - if v.is_nan() { - f64::NAN.to_bits() - } else { - v.to_bits() - } - ) -} fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } @@ -118,7 +109,7 @@ impl World { format!( "{id}:{}:{}:{}", m.id(), - bits(m.amount()), + f64_hex(m.amount()), m.operation() as u8 ) }) @@ -162,9 +153,9 @@ impl World { let prefix = format!( "instance {id} {} {} {} {}", n.key.id, - bits(a.base_value()), + f64_hex(a.base_value()), a.is_dirty(), - bits(a.cached_value()) + f64_hex(a.cached_value()) ); let all = a.modifiers().to_vec(); let permanent = a.permanent_modifiers().to_vec(); @@ -469,7 +460,7 @@ pub fn main() -> Result<(), Box> { "value" => world .handle(op["instance"].as_i64().unwrap()) .and_then(|h| h.ok_or(Stop::Null)) - .map(|h| bits(world.instances[h.id].value.as_mut().unwrap().value())), + .map(|h| f64_hex(world.instances[h.id].value.as_mut().unwrap().value())), _ => world.raw(&mut map, op).map(|()| "ok".into()), }; world.logs.push(format!( diff --git a/crates/horizon-world/examples/probe/stored_effect_notifications_fixture.rs b/crates/horizon-world/examples/probe/stored_effect_notifications_fixture.rs index 31b4b047b..9dccc7cf8 100644 --- a/crates/horizon-world/examples/probe/stored_effect_notifications_fixture.rs +++ b/crates/horizon-world/examples/probe/stored_effect_notifications_fixture.rs @@ -329,7 +329,7 @@ mod integration { "value" => world .handle(op["instance"].as_i64().unwrap()) .and_then(|h| h.ok_or(Stop::Null)) - .map(|h| bits(world.instances[h.id].value.as_mut().unwrap().value())), + .map(|h| f64_hex(world.instances[h.id].value.as_mut().unwrap().value())), _ => world.raw(&mut map, op).map(|()| "ok".into()), }; world.logs.push(format!( diff --git a/crates/horizon-world/examples/probe/support/common.rs b/crates/horizon-world/examples/probe/support/common.rs new file mode 100644 index 000000000..17c453363 --- /dev/null +++ b/crates/horizon-world/examples/probe/support/common.rs @@ -0,0 +1,49 @@ +//! Number formats and JSON readers several probes share. +use horizon_core::position::BlockPos; +use horizon_world::nbt::Tag; +use serde_json::Value; + +/// An f64's bits, every NaN as Java's canonical one (Double.doubleToLongBits). +pub fn f64_bits(v: f64) -> u64 { + if v.is_nan() { + f64::NAN.to_bits() + } else { + v.to_bits() + } +} + +/// An f32's bits, every NaN as Java's canonical one (Float.floatToIntBits). +pub fn f32_bits(v: f32) -> u32 { + if v.is_nan() { + f32::NAN.to_bits() + } else { + v.to_bits() + } +} + +/// f64_bits as 16 hex digits. +pub fn f64_hex(v: f64) -> String { + format!("{:016x}", f64_bits(v)) +} + +/// Registry metadata from the oracle's JSON as NBT: strings, ints, lists and compounds. +pub fn tag(v: &Value) -> Tag { + match v { + Value::String(s) => Tag::String(s.as_str().into()), + Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), + Value::Array(a) => Tag::List(a.iter().map(tag).collect()), + Value::Object(m) => { + Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) + } + _ => panic!("invalid registry metadata"), + } +} + +/// A block position from a JSON `[x, y, z]`. +pub fn position(v: &Value) -> BlockPos { + BlockPos::new( + v[0].as_i64().unwrap() as i32, + v[1].as_i64().unwrap() as i32, + v[2].as_i64().unwrap() as i32, + ) +} diff --git a/crates/horizon-world/examples/probe/support/shapes.rs b/crates/horizon-world/examples/probe/support/shapes.rs new file mode 100644 index 000000000..eed5f436d --- /dev/null +++ b/crates/horizon-world/examples/probe/support/shapes.rs @@ -0,0 +1,85 @@ +//! Shapes and boxes printed the same way by the collision probes. +use super::common::f64_bits; +use horizon_core::geometry::{Aabb, Axis, VoxelShape}; +use std::io::{self, Write}; + +pub fn emit(out: &mut impl Write, shape: &VoxelShape) -> io::Result<()> { + writeln!( + out, + "shape {} {} {}", + shape.is_empty(), + shape.boxes().len(), + shape.is_cube() + )?; + for (axis, name) in Axis::ALL.into_iter().zip(["x", "y", "z"]) { + values(out, name, shape.coordinates(axis).iter().copied())?; + } + let axes = shape.cube_coordinate_axes(); + writeln!( + out, + "axes {}", + u8::from(axes[0]) + 2 * u8::from(axes[1]) + 4 * u8::from(axes[2]) + )?; + values( + out, + "extrema", + Axis::ALL + .map(|a| shape.min(a)) + .into_iter() + .chain(Axis::ALL.map(|a| shape.max(a))), + )?; + match shape.bounds() { + Some(b) => box_values(out, "bounds", b)?, + None => writeln!(out, "bounds none")?, + } + for b in shape.boxes() { + box_values(out, "box", *b)?; + } + Ok(()) +} + +/// `label` and each value's bits in hex, NaN as Java's canonical one. +pub fn values( + out: &mut impl Write, + label: &str, + values: impl IntoIterator, +) -> io::Result<()> { + write!(out, "{label}")?; + for v in values { + let bits = if v.is_nan() { + 0x7ff8_0000_0000_0000 + } else { + v.to_bits() + }; + write!(out, " {bits:016x}")?; + } + writeln!(out) +} + +pub fn box_values(out: &mut impl Write, label: &str, b: Aabb) -> io::Result<()> { + values( + out, + label, + [ + b.min(Axis::X), + b.min(Axis::Y), + b.min(Axis::Z), + b.max(Axis::X), + b.max(Axis::Y), + b.max(Axis::Z), + ], + ) +} + +/// `label` and the box's six bounds' bits in hex. +pub fn bounds(label: &str, b: Aabb) -> String { + format!( + "{label} {:016x} {:016x} {:016x} {:016x} {:016x} {:016x}", + f64_bits(b.min(Axis::X)), + f64_bits(b.min(Axis::Y)), + f64_bits(b.min(Axis::Z)), + f64_bits(b.max(Axis::X)), + f64_bits(b.max(Axis::Y)), + f64_bits(b.max(Axis::Z)) + ) +} diff --git a/crates/horizon-world/examples/probe/synced_floats_fixture.rs b/crates/horizon-world/examples/probe/synced_floats_fixture.rs index 8209c4aa5..7f649c18b 100644 --- a/crates/horizon-world/examples/probe/synced_floats_fixture.rs +++ b/crates/horizon-world/examples/probe/synced_floats_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f32_bits; use horizon_world::synced_floats::{DataError, FloatValue, Observer, SyncedFloats}; use serde_json::Value; use std::{ @@ -8,13 +9,6 @@ use std::{ fn single(v: &Value) -> f32 { f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn bits(v: f32) -> u32 { - if v.is_nan() { - f32::NAN.to_bits() - } else { - v.to_bits() - } -} fn parse(v: &Value) -> Vec { v.as_array() .unwrap() @@ -31,7 +25,7 @@ fn values(v: Option<&[FloatValue]>) -> String { Some([]) => "empty".into(), Some(v) => v .iter() - .map(|v| format!("{}:{:08x}", v.id, bits(v.value))) + .map(|v| format!("{}:{:08x}", v.id, f32_bits(v.value))) .collect::>() .join(","), } @@ -59,7 +53,7 @@ impl Observer for Callback<'_> { fn updated(&mut self, data: &mut SyncedFloats, id: u8) -> io::Result<()> { self.event(&format!( "updated {id} {:08x} {} {}", - bits(data.get(id).unwrap()), + f32_bits(data.get(id).unwrap()), data.is_dirty(), data.item_dirty(id).unwrap() ))?; @@ -129,7 +123,7 @@ pub fn main() -> Result<(), Box> { ), "assign" => data.assign(&parse(&op["values"]), &mut cb), "get" => data.get(id).map_err(DataError::UnknownId).and_then(|v| { - writeln!(cb.out, "get {:08x}", bits(v)).map_err(DataError::Observer) + writeln!(cb.out, "get {:08x}", f32_bits(v)).map_err(DataError::Observer) }), "pack" => writeln!(cb.out, "packed {}", values(data.take_dirty().as_deref())) .map_err(DataError::Observer), @@ -149,7 +143,7 @@ pub fn main() -> Result<(), Box> { writeln!( cb.out, "state {id} {:08x} {} {}", - bits(data.get(*id).unwrap()), + f32_bits(data.get(*id).unwrap()), data.item_dirty(*id).unwrap(), data.is_default(*id).unwrap() )?; diff --git a/crates/horizon-world/examples/probe/teleport_consume_fixture.rs b/crates/horizon-world/examples/probe/teleport_consume_fixture.rs index cf82d3b67..595fa3855 100644 --- a/crates/horizon-world/examples/probe/teleport_consume_fixture.rs +++ b/crates/horizon-world/examples/probe/teleport_consume_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{ geometry::Vec3, position::BlockPos, @@ -13,13 +14,6 @@ use std::{ fn n(v: &Value, k: &str) -> i32 { v[k].as_i64().unwrap() as i32 } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} struct World<'a, R> { f: Value, out: &'a mut dyn Write, @@ -63,10 +57,10 @@ impl World<'_, R> { self.attempts, self.changed, self.riding, - bits(self.pos.x), - bits(self.pos.y), - bits(self.pos.z), - bits(self.fall), + f64_bits(self.pos.x), + f64_bits(self.pos.y), + f64_bits(self.pos.z), + f64_bits(self.fall), self.grace, self.impact ) @@ -128,9 +122,9 @@ impl TeleportConsumeWorld for World<'_, R> { self.attempts += 1; self.event(format!( "teleport {} {} {}", - bits(p.x), - bits(p.y), - bits(p.z) + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z) ))?; let at = n(self.config(), "success_at"); let success = at >= 0 && i >= at; @@ -142,9 +136,9 @@ impl TeleportConsumeWorld for World<'_, R> { fn teleport_event(&mut self, l: i32, p: Vec3) -> Result<(), Self::Error> { self.event(format!( "game-event {l} {} {} {}", - bits(p.x), - bits(p.y), - bits(p.z) + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z) )) } fn is_fox(&self) -> bool { @@ -153,9 +147,9 @@ impl TeleportConsumeWorld for World<'_, R> { fn teleport_sound(&mut self, l: i32, p: Vec3, fox: bool) -> Result<(), Self::Error> { self.event(format!( "sound {l} {fox} {} {} {}", - bits(p.x), - bits(p.y), - bits(p.z) + f64_bits(p.x), + f64_bits(p.y), + f64_bits(p.z) )) } fn block_position(&mut self) -> Result { diff --git a/crates/horizon-world/examples/probe/terrain_fixture.rs b/crates/horizon-world/examples/probe/terrain_fixture.rs index 7d23227d9..506273a10 100644 --- a/crates/horizon-world/examples/probe/terrain_fixture.rs +++ b/crates/horizon-world/examples/probe/terrain_fixture.rs @@ -1,4 +1,5 @@ //! Test transport for registry-backed noise-stage filling against vanilla doFill. +use crate::support::common::tag; use horizon_core::{ density::{ Beardifier, JigsawJunction, RigidPiece, SamplerContext, StructureBounds, TerrainAdjustment, @@ -67,17 +68,6 @@ impl TerrainBlockStates for Properties<'_> { fn n(v: &Value) -> i32 { v.as_i64().unwrap() as i32 } -fn tag(v: &Value) -> Tag { - match v { - Value::String(s) => Tag::String(s.as_str().into()), - Value::Number(i) => Tag::Int(i.as_i64().unwrap() as i32), - Value::Object(m) => { - Tag::Compound(m.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) - } - Value::Array(a) => Tag::List(a.iter().map(tag).collect()), - _ => panic!("unsupported registry fixture value"), - } -} fn structure(v: &Value) -> Beardifier { let bounds = |v: &Value| { StructureBounds::new( diff --git a/crates/horizon-world/examples/probe/view_direction_fixture.rs b/crates/horizon-world/examples/probe/view_direction_fixture.rs index 0121de934..0c5f483df 100644 --- a/crates/horizon-world/examples/probe/view_direction_fixture.rs +++ b/crates/horizon-world/examples/probe/view_direction_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::geometry::Vec3; use horizon_world::view_direction; use serde_json::Value; @@ -15,13 +16,6 @@ fn number(v: &Value) -> f64 { fn vec(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - f64::NAN.to_bits() - } else { - v.to_bits() - } -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).unwrap(); let dir = Path::new(&arg); @@ -32,22 +26,22 @@ pub fn main() -> Result<(), Box> { "acos" => writeln!( out, "angle {:016x}", - bits(view_direction::acos(number(&f["value"]))) + f64_bits(view_direction::acos(number(&f["value"]))) )?, "view" => { let v = view_direction::view_vector(single(&f["pitch"]), single(&f["yaw"])); writeln!( out, "view {:016x} {:016x} {:016x}", - bits(v.x), - bits(v.y), - bits(v.z) + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) )?; } "blocking" => writeln!( out, "angle {:016x}", - bits(view_direction::blocking_angle( + f64_bits(view_direction::blocking_angle( vec(&f["source"]), vec(&f["position"]), single(&f["yaw"]) @@ -56,7 +50,7 @@ pub fn main() -> Result<(), Box> { "fallback" => writeln!( out, "angle {:016x}", - bits(view_direction::BLOCKING_FALLBACK_ANGLE) + f64_bits(view_direction::BLOCKING_FALLBACK_ANGLE) )?, _ => panic!("mode"), } diff --git a/crates/horizon-world/examples/probe/water_splash_fixture.rs b/crates/horizon-world/examples/probe/water_splash_fixture.rs index 08f5bd7b9..8d6eabc79 100644 --- a/crates/horizon-world/examples/probe/water_splash_fixture.rs +++ b/crates/horizon-world/examples/probe/water_splash_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{ geometry::Vec3, random::{LegacyRandom, RandomSource, XoroshiroRandom}, @@ -18,13 +19,6 @@ fn single(v: &Value) -> f32 { fn vec(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - f64::NAN.to_bits() - } else { - v.to_bits() - } -} fn float_bits(v: f32) -> u32 { if v.is_nan() { f32::NAN.to_bits() @@ -33,7 +27,12 @@ fn float_bits(v: f32) -> u32 { } } fn vector(v: Vec3) -> String { - format!("{:016x} {:016x} {:016x}", bits(v.x), bits(v.y), bits(v.z)) + format!( + "{:016x} {:016x} {:016x}", + f64_bits(v.x), + f64_bits(v.y), + f64_bits(v.z) + ) } struct World<'a, R> { f: &'a Value, diff --git a/crates/horizon-world/examples/probe/world_border_fixture.rs b/crates/horizon-world/examples/probe/world_border_fixture.rs index ba00b2ca0..ab0b8795b 100644 --- a/crates/horizon-world/examples/probe/world_border_fixture.rs +++ b/crates/horizon-world/examples/probe/world_border_fixture.rs @@ -1,5 +1,6 @@ +use crate::support::shapes::{emit, values}; use horizon_core::geometry::{ - Aabb, Axis, Vec3, VoxelShape, + Aabb, Vec3, border::{BorderGeometry, BorderStatus}, }; use serde_json::Value; @@ -11,70 +12,6 @@ use std::{ fn number(v: &Value) -> f64 { f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap()) } -fn values( - out: &mut impl Write, - label: &str, - values: impl IntoIterator, -) -> io::Result<()> { - write!(out, "{label}")?; - for v in values { - let bits = if v.is_nan() { - 0x7ff8_0000_0000_0000 - } else { - v.to_bits() - }; - write!(out, " {bits:016x}")?; - } - writeln!(out) -} -fn box_values(out: &mut impl Write, label: &str, b: Aabb) -> io::Result<()> { - values( - out, - label, - [ - b.min(Axis::X), - b.min(Axis::Y), - b.min(Axis::Z), - b.max(Axis::X), - b.max(Axis::Y), - b.max(Axis::Z), - ], - ) -} -fn emit(out: &mut impl Write, shape: &VoxelShape) -> io::Result<()> { - writeln!( - out, - "shape {} {} {}", - shape.is_empty(), - shape.boxes().len(), - shape.is_cube() - )?; - for (axis, name) in Axis::ALL.into_iter().zip(["x", "y", "z"]) { - values(out, name, shape.coordinates(axis).iter().copied())?; - } - let axes = shape.cube_coordinate_axes(); - writeln!( - out, - "axes {}", - u8::from(axes[0]) + 2 * u8::from(axes[1]) + 4 * u8::from(axes[2]) - )?; - values( - out, - "extrema", - Axis::ALL - .map(|a| shape.min(a)) - .into_iter() - .chain(Axis::ALL.map(|a| shape.max(a))), - )?; - match shape.bounds() { - Some(b) => box_values(out, "bounds", b)?, - None => writeln!(out, "bounds none")?, - } - for b in shape.boxes() { - box_values(out, "box", *b)?; - } - Ok(()) -} pub fn main() -> Result<(), Box> { let arg = crate::args().nth(1).expect("private fixture directory"); let dir = Path::new(&arg); diff --git a/crates/horizon-world/examples/probe/world_ray_fixture.rs b/crates/horizon-world/examples/probe/world_ray_fixture.rs index 5b8a3b9fb..ded8645c7 100644 --- a/crates/horizon-world/examples/probe/world_ray_fixture.rs +++ b/crates/horizon-world/examples/probe/world_ray_fixture.rs @@ -1,3 +1,4 @@ +use crate::support::common::f64_bits; use horizon_core::{ geometry::{Aabb, ShapeOp, Vec3, VoxelShape}, position::BlockPos, @@ -15,13 +16,6 @@ fn number(v: &Value) -> f64 { fn vector(v: &Value) -> Vec3 { Vec3::new(number(&v[0]), number(&v[1]), number(&v[2])) } -fn bits(v: f64) -> u64 { - if v.is_nan() { - 0x7ff8000000000000 - } else { - v.to_bits() - } -} fn pos(p: BlockPos) -> String { format!("{} {} {}", p.x, p.y, p.z) } @@ -185,9 +179,9 @@ pub fn main() -> Result<(), Box> { w.out, "hit {} {:016x} {:016x} {:016x} {} {} {}", r.miss, - bits(h.location.x), - bits(h.location.y), - bits(h.location.z), + f64_bits(h.location.x), + f64_bits(h.location.y), + f64_bits(h.location.z), format!("{:?}", h.direction).to_lowercase(), pos(h.block), h.inside diff --git a/tests/test_parity_runner.py b/tests/test_parity_runner.py index 9e0f2a2ec..1ecd790f0 100644 --- a/tests/test_parity_runner.py +++ b/tests/test_parity_runner.py @@ -55,13 +55,14 @@ class ParityRunnerTests(unittest.TestCase): with tempfile.TemporaryDirectory() as temp, mock.patch.object(parity, "ROOT", Path(temp)): probe = Path(temp, "crates/w/examples/probe") (probe / "support").mkdir(parents=True) - (probe / "a_fixture.rs").write_text("use crate::support::shared as s;") + (probe / "a_fixture.rs").write_text('mod integration { include!("d_fixture.rs"); }') + (probe / "d_fixture.rs").write_text("use crate::support::shared as s;") for name in ("b_fixture.rs", "c_fixture.rs", "main.rs", "support/shared.rs", "support/other.rs"): (probe / name).write_text("") - paths = [f"crates/w/examples/probe/{name}" for name in - ("a_fixture.rs", "b_fixture.rs", "c_fixture.rs", "main.rs", "support/other.rs", "support/shared.rs")] + paths = [f"crates/w/examples/probe/{name}" for name in ("a_fixture.rs", "b_fixture.rs", "c_fixture.rs", + "d_fixture.rs", "main.rs", "support/other.rs", "support/shared.rs")] self.assertEqual(parity.only_probes_run(["crates/w/src/lib.rs", *paths], {"a_fixture", "b_fixture"}), - ["crates/w/src/lib.rs", *paths[:2], paths[3], paths[5]]) + ["crates/w/src/lib.rs", *paths[:2], *paths[3:5], paths[6]]) self.assertEqual(parity.only_probes_run(paths, set()), paths) def test_java_sources_include_classes_pulled_from_the_sourcepath(self): diff --git a/tools/parity.py b/tools/parity.py index 271baee6f..8d12963e1 100644 --- a/tools/parity.py +++ b/tools/parity.py @@ -482,19 +482,21 @@ def probes_run(commands): def only_probes_run(paths, names): - """A probe binary's dep-info lists every probe in it. Keep those the harness ran and the shared support - modules they name (support::), so that editing one probe reruns only its own harnesses.""" + """A probe binary's dep-info lists every probe in it. Keep those the harness ran, the probes they include! + and the shared support modules they name (support::), so that editing one probe reruns only its own + harnesses.""" if not names: return paths - ran = [path for path in paths if (match := PROBE_SOURCE.search(path)) and not match.group(1) and match.group(2) in names] - used = "".join((ROOT / path).read_text(encoding="utf-8", errors="replace") for path in ran) - kept = [] - for path in paths: - match = PROBE_SOURCE.search(path) - if not match or (match.group(1) and f"support::{match.group(2)}" in used) \ - or (not match.group(1) and match.group(2) in (*names, "main")): - kept.append(path) - return kept + names, used = {*names, "main"}, "" + while True: + ran = [path for path in paths if (match := PROBE_SOURCE.search(path)) and not match.group(1) and match.group(2) in names] + used = "".join((ROOT / path).read_text(encoding="utf-8", errors="replace") for path in ran) + included = set(re.findall(r'include!\("(\w+)\.rs"\)', used)) - names + if not included: + break + names |= included + return [path for path in paths if not (match := PROBE_SOURCE.search(path)) + or (f"support::{match.group(2)}" in used if match.group(1) else match.group(2) in names)] def dependencies(trace, scope=None):