From ed7615d362490f57bca5ac239c64eb9360913a4b Mon Sep 17 00:00:00 2001 From: chexit Date: Tue, 3 Nov 2026 10:34:23 +0300 Subject: [PATCH] Add flat render world to mod test scripts Co-authored-by: Codex --- crates/horizon-mods/src/testing.rs | 117 +++++++++++++++++- crates/horizon-mods/tests/sandbox.rs | 47 +++++++ crates/horizon-mods/tests/ui_guest/src/lib.rs | 18 +++ 3 files changed, 181 insertions(+), 1 deletion(-) diff --git a/crates/horizon-mods/src/testing.rs b/crates/horizon-mods/src/testing.rs index 0fe821074..137b38d0c 100644 --- a/crates/horizon-mods/src/testing.rs +++ b/crates/horizon-mods/src/testing.rs @@ -29,6 +29,8 @@ pub struct TestGame { /// Every name that joined, for their id. names: Vec, tick: u64, + /// Optional synthetic render-query surface; blocks below it are solid. + pub render_ground_y: Option, /// Capability `world`'s Overworld: the blocks a script set or the mod /// changed; every other block, and the other dimensions, are air. Every /// biome is the dimension's plainest: plains, nether wastes, the end. @@ -79,6 +81,108 @@ impl crate::WorldView for ScriptWorld { } } +/// Answers the previous tick's render requests without a renderer or loaded chunks. +fn answer_flat_world(mods: &mut Mods, ground_y: i32) { + use crate::world_query::{self, BlockProbe, Cell, Trace}; + use crate::{SectionFill, WorldChanges, WorldRayAnswer, WorldSection, WorldVerdict}; + struct Flat(i32); + impl BlockProbe for Flat { + fn cell(&mut self, _: i32, y: i32, _: i32) -> Cell<'_> { + if y < self.0 { + Cell::Solid(None) + } else { + Cell::Empty + } + } + } + for (i, columns) in mods.take_ground_queries() { + mods.answer_ground( + i, + columns.into_iter().map(|(x, z)| (x, z, ground_y)).collect(), + ); + } + for (i, queries) in mods.take_world_queries() { + let unknown = |id, known_to| WorldRayAnswer { + id, + verdict: WorldVerdict::Unknown, + fraction: 0.0, + normal: (0, 0, 0), + block: (0, 0, 0), + known_to, + }; + let mut rays: Vec<_> = queries + .rays + .into_iter() + .map(|r| { + match world_query::trace( + &mut Flat(ground_y), + [r.start.0, r.start.1, r.start.2], + [r.end.0, r.end.1, r.end.2], + ) { + Trace::Clear => WorldRayAnswer { + id: r.id, + verdict: WorldVerdict::Clear, + fraction: 1.0, + normal: (0, 0, 0), + block: (0, 0, 0), + known_to: 1.0, + }, + Trace::Hit { + fraction, + normal, + block, + } => WorldRayAnswer { + id: r.id, + verdict: WorldVerdict::Hit, + fraction, + normal: (normal[0], normal[1], normal[2]), + block: (block[0], block[1], block[2]), + known_to: 1.0, + }, + Trace::Unknown { known_to } => unknown(r.id, known_to), + } + }) + .collect(); + rays.extend(queries.over.into_iter().map(|id| unknown(id, 0.0))); + let sections = queries + .sections + .into_iter() + .flat_map(|(_, sections)| sections) + .map(|(x, y, z)| { + let base = i64::from(y) * 16; + let solid = (i64::from(ground_y) - base).clamp(0, 16) as usize; + let (fill, bits) = match solid { + 0 => (SectionFill::Empty, Vec::new()), + 16 => (SectionFill::Full, Vec::new()), + _ => { + let mut bits = vec![0; 512]; + bits[..solid * 32].fill(255); + (SectionFill::Mixed, bits) + } + }; + WorldSection { + x, + y, + z, + fill, + bits, + } + }) + .collect(); + mods.answer_world( + i, + rays, + sections, + WorldChanges { + epoch: 1, + revision: 0, + overflow: false, + sections: Vec::new(), + }, + ); + } +} + /// Where a script starts: standing at 0.5 64 0.5 in the Overworld, facing /// south, full health and food, at the first tick of a new world's first morning. pub fn default_player() -> Player { @@ -119,6 +223,7 @@ impl TestGame { players: Vec::new(), names: Vec::new(), tick: 0, + render_ground_y: None, world: Default::default(), }; copy_mod(hmod, ©)?; @@ -160,6 +265,9 @@ impl TestGame { /// `n` game ticks; each moves the clock one tick on, as the game does. pub fn tick(&mut self, n: u64) { for _ in 0..n { + if let Some(y) = self.render_ground_y { + answer_flat_world(&mut self.mods, y); + } self.tick += 1; if let Some(clock) = &mut self.clock { clock.game_time += 1; @@ -375,6 +483,8 @@ pub struct Step { /// Leave the world: the mod reads no player and no clock until a step sets them. #[serde(default)] title_screen: bool, + /// Answer render world queries using a flat surface at this height. + render_ground_y: Option, player: Option, clock: Option, #[serde(default)] @@ -520,6 +630,9 @@ impl Step { if self.reload { game.reload()?; } + if let Some(y) = self.render_ground_y { + game.render_ground_y = Some(y); + } if self.title_screen { game.player = None; game.clock = None; @@ -615,7 +728,7 @@ impl Step { (Some(reason), Some(want)) if !reason.contains(want.as_str()) => { return Err(format!("the mod stopped ({reason}), expected {want:?}")); } - (Some(_), Some(_)) => return Ok(()), + (Some(_), Some(_)) => {} (Some(reason), None) => return Err(format!("the mod stopped: {reason}")), (None, Some(want)) => { return Err(format!( @@ -681,6 +794,8 @@ impl Step { } if let Some(want) = &e.error { same("error", &Some(want.clone()), &feedback(true))?; + } else if let Some(error) = feedback(true) { + return Err(format!("unexpected command error: {error}")); } if let Some(want) = &e.panel { let got: Vec = game diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index 7f1ea3110..6920db79a 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -1542,3 +1542,50 @@ fn browser_lists_items_and_hears_the_player() { } assert_eq!(said(&mut mods, "browser_poll")[0], "256 events"); } + +#[test] +fn test_game_answers_render_queries_only_in_flat_mode() { + let path = mods_dir().join("horizon_mods_test_ui_guest.hmod"); + let mut plain = TestGame::load(&path).unwrap(); + assert!(plain.command("flat_query")); + plain.tick(1); + assert!(plain.command("flat_read")); + let plain_out = format!("{:?}", plain.drain()); + assert!( + plain_out.contains("ground []") + && plain_out.contains("sections []") + && plain_out.contains("rays []"), + "{plain_out}" + ); + + let mut flat = TestGame::load(&path).unwrap(); + Script::parse("[[step]]\nrender-ground-y = 64\ncommand = 'flat_query'\n") + .unwrap() + .run(&mut flat) + .unwrap(); + flat.tick(1); + assert!(flat.command("flat_read")); + let out = format!("{:?}", flat.drain()); + assert!(out.contains("(2, 3, 64)"), "{out}"); + assert!(out.contains("fill: SectionFill::Full"), "{out}"); + assert!(out.contains("fill: SectionFill::Empty"), "{out}"); + assert!( + out.contains("verdict: Verdict::Hit, fraction: 0.5"), + "{out}" + ); + assert!(out.contains("epoch: 1, revision: 0"), "{out}"); +} + +#[test] +fn test_script_rejects_unexpected_command_error() { + let path = mods_dir().join("horizon_mods_test_guest.hmod"); + let mut game = TestGame::load(&path).unwrap(); + let error = Script::parse("[[step]]\ncommand = 'save_key'\n") + .unwrap() + .run(&mut game) + .unwrap_err(); + assert!( + error.contains("unexpected command error: Usage: /save_key"), + "{error}" + ); +} diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs index 5a1aad6e5..8ddb8e0e5 100644 --- a/crates/horizon-mods/tests/ui_guest/src/lib.rs +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -103,6 +103,8 @@ impl Mod for Guest { "ui_screen", "ui_settings", "ui_close", + "flat_query", + "flat_read", "render_hud", "render_clear", "cap_status", @@ -157,6 +159,22 @@ impl Mod for Guest { }), "ui_settings" => ui::set_settings(&widgets()), "ui_close" => ui::close_screen(), + "flat_query" => { + render::query_ground(&[(2, 3)]); + render::query_sections(render::Policy::Collision, &[(0, 3, 0), (0, 4, 0)]); + render::query_rays(&[render::Ray { + id: 7, + start: (0.5, 66.0, 0.5), + end: (0.5, 62.0, 0.5), + policy: render::Policy::Collision, + }]); + } + "flat_read" => { + chat::show(&format!("ground {:?}", render::ground())); + chat::show(&format!("sections {:?}", render::sections())); + chat::show(&format!("rays {:?}", render::rays())); + chat::show(&format!("changes {:?}", render::world_changes())); + } "render_hud" => render::set_hud(&shapes()), "render_clear" => render::set_hud(&[]), "cap_status" => {