Merge main into rdbt/fix-1478-firework-bursts-20261026

fireworks.rs keeps main's rocket blast damage next to the client burst;
lan_e2e.py takes main's CHECKS and server-thread default plus 'fireworks'.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-26 12:37:15 +01:00
87 changed files with 3329 additions and 1339 deletions
+13 -7
View File
@@ -8,7 +8,7 @@ name: nightly audit
# gh workflow run "nightly audit" -f dry_run=true # the audit, without touching issues
on:
schedule:
# GitHub's schedules are in UTC: both, and the first step keeps the one that is 03:00 in Paris.
# GitHub's schedules are in UTC: both, and the first step keeps the one that is 03:07 in Paris today.
- cron: '7 1 * * *'
- cron: '7 2 * * *'
workflow_dispatch:
@@ -31,14 +31,19 @@ jobs:
audit:
# The Mac mini's runners only: the audit compares with the previous one, so it stays on one platform.
runs-on: [self-hosted, horizon-game, macOS]
timeout-minutes: 150
timeout-minutes: 240 # every harness (~80 min) plus lan_integration (~40 min an attempt under load, retried)
steps:
- name: 03:00 in Paris
- name: 03:07 in Paris
id: when
env:
SCHEDULE: ${{ github.event.schedule }}
run: |
hour=$(TZ=Europe/Paris date +%H)
if [[ "${{ github.event_name }}" == schedule && "$hour" != 03 ]]; then
echo "It is ${hour}:00 in Paris: the other schedule runs the audit."
# Which cron is 03:07 in Paris, from Paris's offset today: GitHub can start a schedule hours late, so the
# clock at start time can't tell (on 2026-10-26 both started after 04:00 Paris and both skipped).
offset=$(TZ=Europe/Paris date +%z)
ours=$([ "$offset" = +0200 ] && echo '7 1 * * *' || echo '7 2 * * *')
if [[ "${{ github.event_name }}" == schedule && "$SCHEDULE" != "$ours" ]]; then
echo "Paris is at UTC${offset} today: the '$ours' schedule runs the audit."
echo "run=false" >> "$GITHUB_OUTPUT"
else
echo "run=true" >> "$GITHUB_OUTPUT"
@@ -66,7 +71,8 @@ jobs:
run: |
echo "since=$(date +%Y-%m-%dT%H:%M:%S)" >> "$GITHUB_OUTPUT"
set +e
python3 tools/parity.py --all --timeout 1800 2>&1 | tee "$RUNNER_TEMP/audit.log"
# lan_integration (the whole lan_e2e suite) takes ~2,400 s under load, past the usual 1,800.
python3 tools/parity.py --all --timeout 3000 2>&1 | tee "$RUNNER_TEMP/audit.log"
echo "exit=${PIPESTATUS[0]}" >> "$GITHUB_OUTPUT"
{ echo '```'; tail -40 "$RUNNER_TEMP/audit.log"; echo '```'; } >> "$GITHUB_STEP_SUMMARY"
- name: Issues for the harnesses that regressed
+4 -4
View File
@@ -66,7 +66,7 @@ cargo build --profile fast -p horizon-client # optimized, quick to link:
python3 tools/parity.py --changed origin/main # the harnesses your branch can affect (CI's parity job)
python3 tools/parity.py --changed-files paths.txt --list # which harnesses cover these files, without running them
python3 tools/parity.py mob_ai sculk # named harnesses
python3 tools/lan_e2e.py --check chest # two real games over LAN (host in-frame); --server-thread for the host on its server thread
python3 tools/lan_e2e.py --check chest # two real games over LAN (server-thread host); --in-frame compares the fallback
python3 tools/agent_control_e2e.py # horizon-ctl / MCP end to end
```
@@ -156,7 +156,7 @@ python3 tools/agent_control_e2e.py # horizon-ctl / MCP end to end
exactly as a LAN guest or a dedicated server's player does. This is what players run. Open
`[server-thread]` issues list the gaps left.
2. **In-frame (`--in-frame`, `--no-server-thread` or `HORIZON_SERVER_THREAD=0`):** the window's `Game` ticks
the world itself, one level that follows the player (`fn enter_dimension`). Most harnesses test this path.
the world itself, one level that follows the player (`fn enter_dimension`). Deterministic rule fixtures still use this path; player-facing integration uses the default server thread.
LAN guests and dedicated servers always use the server code, so a fix there serves both. Test both paths
when you touch player-facing gameplay:
@@ -164,8 +164,8 @@ when you touch player-facing gameplay:
```sh
cargo run --profile fast -p horizon-client -- --resources .local/horizon-client/26.3 --world T # server thread
cargo run --profile fast -p horizon-client -- --resources .local/horizon-client/26.3 --world T --in-frame # in-frame
python3 tools/lan_e2e.py --check <name> # host in-frame, guest is a server client
python3 tools/lan_e2e.py --server-thread --check <name> # host on its server thread (portals, levels, distance, lava need it)
python3 tools/lan_e2e.py --check <name> # host on its server thread, guest is a server client
python3 tools/lan_e2e.py --in-frame --check <name> # comparison (portals, levels, distance, lava need server-thread mode)
python3 tools/parity.py dedicated_server server_entities server_player_damage
```
Generated
+24
View File
@@ -1444,6 +1444,7 @@ dependencies = [
"serde_json",
"sha2",
"toml",
"wasmparser",
"wasmtime",
"wit-component",
"wit-parser",
@@ -4148,6 +4149,28 @@ dependencies = [
"wit-parser",
]
[[package]]
name = "wast"
version = "254.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "febc59cde513fc8f39b8ffd487893723c555fdd024e2e3dc7d165181e6397ea0"
dependencies = [
"bumpalo",
"leb128fmt",
"memchr",
"unicode-width",
"wasm-encoder",
]
[[package]]
name = "wat"
version = "1.254.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7db96351e51a3a1bbe6812f029e1100b4d9a5e02161fb63104bd2271fc847e7e"
dependencies = [
"wast",
]
[[package]]
name = "wayland-backend"
version = "0.3.17"
@@ -5013,6 +5036,7 @@ dependencies = [
"wasm-encoder",
"wasm-metadata",
"wasmparser",
"wat",
"wit-parser",
]
+4
View File
@@ -47,6 +47,7 @@ impl Report {
"api": self.facts.api,
"side": self.facts.side,
"capabilities": self.facts.capabilities,
"api_level": self.facts.api_level,
"game": self.facts.game,
"requires": requires,
"sha256": self.sha256,
@@ -87,6 +88,9 @@ impl Report {
")\n {} bytes, SHA-256 {}, {signed}\n",
self.size, self.sha256
);
if let Some(level) = f.api_level {
out += &format!(" needs mod API level {level} or newer\n");
}
for c in &f.capabilities {
out += &format!(" may {}\n", c.describe());
}
@@ -610,12 +610,17 @@ mod tests {
#[test]
fn unreachable_games_fail_with_status_3() {
let mut s = Session::new(std::env::temp_dir().join("horizon-ctl-nobody.sock"), None);
let home =
std::env::temp_dir().join(format!("horizon-ctl-unreachable-{}", std::process::id()));
std::fs::create_dir_all(&home).unwrap();
let mut s = Session::new(home.join("nobody.sock"), None);
s.home = home.clone();
let f = s.run(&["state".to_owned()]).unwrap_err();
assert_eq!(f.status, UNREACHABLE);
assert_eq!(f.reply["kind"], "connection");
let f = s.run(&["sleep".to_owned(), "x".to_owned()]).unwrap_err();
assert_eq!(f.status, USAGE);
std::fs::remove_dir_all(home).unwrap();
}
#[test]
+9 -7
View File
@@ -10,6 +10,8 @@ use crate::{
use horizon_core::geometry::{Aabb, Vec3};
use horizon_world::block_collision::BlockCollisionRegistry;
mod fluids;
#[derive(Clone, Debug)]
pub struct ItemEntity {
pub health: i32,
@@ -165,18 +167,16 @@ impl ItemEntity {
facts: &BlockFacts,
collision: &BlockCollisionRegistry<'_>,
no_physics: bool,
fluids: &crate::motion::FluidResources,
) -> bool {
self.ticks += 1;
self.previous = self.position;
if self.pickup_delay > 0 && self.pickup_delay != u32::MAX {
self.pickup_delay -= 1;
}
let eye = world.block(
self.position.x.floor() as i32,
(self.position.y + 0.1).floor() as i32,
self.position.z.floor() as i32,
);
let in_water = facts.get(eye).fluid == Fluid::Water;
// Entity.baseTick pushes before ItemEntity applies buoyancy and moves.
let (water_height, lava_height) = self.update_fluids(world, fluids);
let in_water = water_height > f64::from(0.1f32);
if in_water {
// ItemEntity.setUnderwaterMovement
self.velocity = Vec3::new(
@@ -184,7 +184,7 @@ impl ItemEntity {
self.velocity.y + if self.velocity.y < 0.06 { 5.0e-4 } else { 0.0 },
self.velocity.z * 0.99,
);
} else if facts.get(eye).fluid == Fluid::Lava {
} else if lava_height > f64::from(0.1f32) {
self.velocity = Vec3::new(
self.velocity.x * 0.95,
self.velocity.y + if self.velocity.y < 0.06 { 5.0e-4 } else { 0.0 },
@@ -245,6 +245,8 @@ impl ItemEntity {
}
}
self.age += 1;
// ItemEntity.tick refreshes immersion and pushes again after moving.
self.update_fluids(world, fluids);
self.age < 6000 && self.position.y > -128.0
}
/// `ItemEntity.isMergable` at `age`: a stack below its size whose pickup
@@ -0,0 +1,216 @@
//! Dropped-item immersion and currents through the shared vanilla fluid helpers.
use super::ItemEntity;
use crate::{
motion::{FluidResources, FluidTags, LAVA, WATER},
world::World,
};
use horizon_core::{
geometry::{Aabb, Direction, Vec3},
position::BlockPos,
};
use horizon_world::{
entity_fluid::{
CurrentTarget, EntityFluidInteraction, FluidInteractionWorld, FluidMembership,
FluidSections, FluidTagId,
},
fluid::{FluidHeightWorld, FluidKind, FluidState},
fluid_flow::{FluidFlowWorld, flow_at},
registry::BlockStateId,
};
type Error = std::convert::Infallible;
struct ItemFluidWorld<'a> {
item: &'a mut ItemEntity,
world: &'a World,
fluids: &'a FluidResources,
}
impl ItemEntity {
pub(super) fn update_fluids(&mut self, world: &World, fluids: &FluidResources) -> (f64, f64) {
let mut interaction = EntityFluidInteraction::new([WATER, LAVA]);
let mut scene = ItemFluidWorld {
item: self,
world,
fluids,
};
if interaction.update(&mut scene, false, 4096).is_err() {
return (0.0, 0.0);
}
let water_height = interaction.fluid_height(&scene, WATER);
let lava_height = interaction.fluid_height(&scene, LAVA);
let _ = interaction.apply_current(&mut scene, WATER, 0.014);
let _ = interaction.apply_current(
&mut scene,
LAVA,
if world.fast_lava {
0.007
} else {
0.0023333333333333335
},
);
(water_height, lava_height)
}
}
impl FluidMembership for ItemFluidWorld<'_> {
fn fluid_in_tag(&self, kind: FluidKind, tag: FluidTagId) -> bool {
FluidTags.fluid_in_tag(kind, tag)
}
}
impl CurrentTarget for ItemFluidWorld<'_> {
type Error = Error;
fn is_player_class(&self) -> bool {
false
}
fn velocity(&mut self) -> Result<Vec3, Error> {
Ok(self.item.velocity)
}
fn add_delta_movement(&mut self, delta: Vec3) -> Result<(), Error> {
let v = self.item.velocity;
let sum = Vec3::new(v.x + delta.x, v.y + delta.y, v.z + delta.z);
if delta.is_finite() && sum.is_finite() {
self.item.velocity = sum;
}
Ok(())
}
}
impl FluidInteractionWorld for ItemFluidWorld<'_> {
type Error = Error;
type Chunk = ();
fn interaction_box(&mut self) -> Result<Option<Aabb>, Error> {
Ok(Some(
self.item.bounds().deflate(Vec3::new(0.001, 0.001, 0.001)),
))
}
fn loaded_chunk(&mut self, x: i32, z: i32) -> Result<Option<()>, Error> {
Ok(self.world.chunks.contains_key(&(x, z)).then_some(()))
}
fn chunk_sections(&mut self, _: ()) -> Result<FluidSections<'_>, Error> {
static SECTIONS: [bool; crate::chunk::SECTIONS] = [true; crate::chunk::SECTIONS];
Ok(FluidSections {
minimum_section_y: crate::chunk::MIN_Y >> 4,
has_fluid: &SECTIONS,
})
}
fn bounding_box_min_y(&self) -> f64 {
self.item.position.y
}
fn block_x(&self) -> i32 {
self.item.position.x.floor() as i32
}
fn eye_y(&mut self) -> Result<f64, Error> {
Ok(self.item.position.y + f64::from(0.2125f32))
}
fn block_z(&self) -> i32 {
self.item.position.z.floor() as i32
}
fn fluid_state(&mut self, p: BlockPos) -> Result<FluidState, Error> {
Ok(self.fluids.fluid(self.world.block(p.x, p.y, p.z)))
}
fn fluid_height(&mut self, f: FluidState, p: BlockPos) -> Result<f32, Error> {
f.height_at(self, [p.x, p.y, p.z])
}
fn camera_height(&mut self, f: FluidState, p: BlockPos) -> Result<f32, Error> {
f.camera_height_at(self, [p.x, p.y, p.z])
}
fn flow(&mut self, f: FluidState, p: BlockPos) -> Result<Vec3, Error> {
flow_at(self, f, p)
}
}
impl FluidHeightWorld for ItemFluidWorld<'_> {
type Error = Error;
fn fluid(&mut self, p: [i32; 3]) -> Result<FluidState, Error> {
Ok(self.fluids.fluid(self.world.block(p[0], p[1], p[2])))
}
fn sturdy_down(&mut self, p: [i32; 3]) -> Result<bool, Error> {
let state = self.world.block(p[0], p[1], p[2]);
Ok(self.fluids.sturdy(state, Direction::Down))
}
}
impl FluidFlowWorld for ItemFluidWorld<'_> {
type Error = Error;
type Block = BlockStateId;
fn fluid(&mut self, p: BlockPos) -> Result<FluidState, Error> {
Ok(self.fluids.fluid(self.world.block(p.x, p.y, p.z)))
}
fn block(&mut self, p: BlockPos) -> Result<BlockStateId, Error> {
Ok(self.world.block(p.x, p.y, p.z))
}
fn blocks_fluid_flow(&self, s: BlockStateId) -> bool {
self.fluids.blocks_flow(s)
}
fn is_ice_class(&self, s: BlockStateId) -> bool {
self.fluids.ice(s)
}
fn is_face_sturdy(
&mut self,
s: BlockStateId,
_: BlockPos,
d: Direction,
) -> Result<bool, Error> {
Ok(self.fluids.sturdy(s, d))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn items_follow_flowing_water_but_still_water_has_no_horizontal_push() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../.local/horizon-client")
.join(horizon_core::VANILLA_VERSION);
if !root.join("movement-properties.nbt").exists() {
return;
}
let assets = crate::assets::Assets::load(&root).unwrap();
let render = assets.reload(&root).unwrap();
let facts = crate::blocks::BlockFacts::load(&root, assets.registry).unwrap();
let fluids = FluidResources::new(&assets, &facts).unwrap();
let collision = crate::collisions::load(&root, assets.registry).unwrap();
let mut world = World::test_scene(&assets, &render, 3).unwrap();
let stone = assets.registry.default_state("minecraft:stone").unwrap();
let water = assets.registry.default_state("minecraft:water").unwrap();
let mut state = assets.registry.state(water).unwrap().clone();
state.properties.insert("level".into(), "1".into());
let flowing = assets.registry.resolve(&state).unwrap();
for x in 19..=21 {
for z in 19..=21 {
world.set(x, 3, z, stone).unwrap();
world.set(x, 4, z, water).unwrap();
}
}
let at = Vec3::new(20.5, 4.0, 20.5);
let item = ItemEntity {
id: 1,
health: 5,
fire_ticks: 0,
stack: crate::items::ItemStack {
item: "minecraft:sugar_cane".into(),
count: 1,
damage: 0,
extra: None,
},
thrower: None,
position: at,
previous: at,
velocity: Vec3::ZERO,
on_ground: false,
age: 0,
pickup_delay: 0,
bob_offset: 0.0,
ticks: 0,
};
let mut still = item.clone();
assert!(still.tick(&world, &facts, &collision, false, &fluids));
assert_eq!(still.position.x, at.x);
assert_eq!(still.velocity.x, 0.0);
world.set(21, 4, 20, flowing).unwrap();
let mut carried = item;
assert!(carried.tick(&world, &facts, &collision, false, &fluids));
// First push is damped by buoyancy before motion, second follows drag.
assert!((carried.position.x - at.x - 0.014 * 0.99).abs() < 1e-12);
assert!((carried.velocity.x - (0.014 * 0.99 * 0.98 + 0.014)).abs() < 1e-12);
for _ in 0..20 {
carried.tick(&world, &facts, &collision, false, &fluids);
}
assert!(carried.position.x > at.x + 0.5);
}
}
+12 -2
View File
@@ -2272,7 +2272,13 @@ impl Game<'_> {
let speed = self.world.random.next_float() * 0.2 + 0.1;
item.move_towards_closest_space(&self.world, self.res.facts(), speed);
}
let alive = item.tick(&self.world, self.res.facts(), self.collision, no_physics);
let alive = item.tick(
&self.world,
self.res.facts(),
self.collision,
no_physics,
&self.res.motion().fluids,
);
cells.moved(i, item.previous, item.position);
// ItemEntity.tick: merge every 2 ticks while moving between
// blocks, every 40 otherwise, before the lifetime check.
@@ -4194,6 +4200,9 @@ impl Game<'_> {
self.with_level(|b, level, ticks| b.notify_written(level, ticks, position, flags));
}
fn tick(&mut self) -> Result<()> {
if self.agent.hold_server {
return Ok(());
}
self.tick_debug_keys()?;
self.tick_debug_charts();
if self.remote.is_some() {
@@ -5601,6 +5610,8 @@ impl Game<'_> {
self.craft_size = 2;
self.craft_table = None;
if let Some(key) = self.container.take() {
// A cart's or chest boat's items go back to it.
self.close_cart_container(key);
self.container_opener_event(key, false);
self.post_block_game_event(
"minecraft:container_close",
@@ -5640,7 +5651,6 @@ impl Game<'_> {
self.world.modified.insert((key.0 >> 4, key.2 >> 4));
}
self.close_ender_chest();
self.close_cart_container();
}
/// `quickMoveStack` of the open plain menu for a shift-click.
fn inventory_quick_move(&mut self, slot: usize) -> bool {
+5
View File
@@ -300,6 +300,8 @@ struct Glide {
pub(super) struct Agent {
task: Option<Task>,
glide: Option<Glide>,
/// Control-fixture barrier: hold simulation and packet handling, not transport threads.
pub hold_server: bool,
}
pub(super) enum Handled {
@@ -479,6 +481,8 @@ impl Game<'_> {
"absorption": p.health.absorption(),
// The hurt animation's ticks left (the camera's hurt tilt).
"hurt_time": p.health.hurt_animation_time,
// LivingEntity.deathTime while dead (the camera's death roll).
"death_time": self.death_time(),
"air": p.air,
"food": p.health.food.food,
"saturation": p.health.food.saturation,
@@ -667,6 +671,7 @@ impl Game<'_> {
});
if let Some(chat) = &self.ui.chat_screen {
v["chat_input"] = json!(chat.input.value);
v["chat_suggestions"] = json!(chat.suggestions_visible());
}
if self.title.is_some() {
v["menu"] = self.title_value();
@@ -219,6 +219,11 @@ impl Game<'_> {
/// client tick (`ClientLevel.tickBlockEntities`).
pub(super) fn tick_block_entity_anims(&mut self) {
let Some(r) = &self.renderer else {
// A server has no window, but its gateways still run their
// server ticker.
if self.remote.is_none() {
self.tick_gateway_servers();
}
return;
};
// Sorted by position: the order they tick in, and a quick lookup.
@@ -378,6 +383,31 @@ impl Game<'_> {
}
self.be_anims.present = positions;
}
/// `TheEndGatewayBlockEntity.portalTick` (the server ticker) of the
/// gateways a server keeps a copy of, in block-ticking chunks
/// (`Level.tickBlockEntities`): the cooldown an entry started runs out,
/// and every 2400 ticks `triggerCooldown` sends its block event.
fn tick_gateway_servers(&mut self) {
let levels = self.tickets.levels();
let mut positions: Vec<[i32; 3]> = self
.be_anims
.gateway_servers
.keys()
.copied()
.filter(|p| levels.block_ticking_at(p[0], p[2]))
.collect();
positions.sort_unstable();
for pos in positions {
let triggered = self
.be_anims
.gateway_servers
.get_mut(&pos)
.is_some_and(EndGateway::portal_tick);
if triggered {
self.block_entity_event(pos, 1, 0);
}
}
}
/// `EndGatewayBlock.entityInside` on the server: whether the gateway at
/// `pos` let the entity in (`triggerCooldown`, with its block event).
pub(super) fn gateway_enter(&mut self, pos: [i32; 3]) -> bool {
@@ -1097,6 +1097,12 @@ impl Game<'_> {
Target::Player => {
// ServerPlayer.teleport: removeVehicle before placing the player.
self.stop_riding();
// A server's player teleported within their level is placed
// there at once: the arrival they were waiting for elsewhere
// in it (whose chunks lose their tickets) is abandoned.
if let Some(conn) = self.lan_acting {
self.server_trips.retain(|trip| !trip.is(conn));
}
if let Some(p) = self.player.as_mut() {
p.position = v;
p.velocity = Vec3::new(0.0, 0.0, 0.0);
+57
View File
@@ -11,6 +11,9 @@ use winit::{event::MouseButton, event_loop::ActiveEventLoop, keyboard::KeyCode};
const HELP: &[&str] = &[
"status game, player, streaming and renderer state",
"simulation authoritative projectiles in the owner's level",
"server-hold on|off hold simulation and player packets at a control-fixture barrier",
"stall MILLISECONDS stall the window thread (0..5000), leaving transport alive",
"key KEY down|up|tap|repeat press, release or auto-repeat a key (w a s d space shift ctrl e q esc f3 1-9 arrows ...)",
"release release every key and mouse button",
"mouse left|right|middle down|up|click",
@@ -238,6 +241,47 @@ impl Game<'_> {
}
"sculk" => Ok(Some(self.sculk_command(&words)?)),
"status" => Ok(Some(self.control_status())),
"stall" => {
let millis: u64 = arg(1)?.parse()?;
if millis > 5000 {
return Err("stall is limited to 5000 milliseconds".into());
}
std::thread::sleep(Duration::from_millis(millis));
ok()
}
"simulation" => {
self.require_world()?;
let value = if let Some(server) = &self.integrated {
server
.call(|g| {
g.in_owner_level(|g| {
json!({ "game_time": g.world.game_time, "projectiles": g.projectiles_status() })
})
})
.ok_or("the integrated server stopped")?
} else {
json!({ "game_time": self.world.game_time, "projectiles": self.projectiles_status() })
};
Ok(Some(value))
}
"server-hold" => {
self.require_world()?;
let held = match arg(1)? {
"on" => true,
"off" => false,
_ => return Err("server-hold on|off".into()),
};
if let Some(server) = &self.integrated {
server
.call(move |g| g.agent.hold_server = held)
.ok_or("the integrated server stopped")?;
} else if self.remote.is_some() {
return Err("server-hold requires the world's host".into());
} else {
self.agent.hold_server = held;
}
ok()
}
"pad" => Ok(Some(self.pad_command(&words)?)),
"key" => {
let key = key_code(arg(1)?).ok_or_else(|| format!("unknown key {}", words[1]))?;
@@ -508,10 +552,23 @@ impl Game<'_> {
"mobs" => {
self.require_world()?;
let radius: f64 = words.get(1).map_or(Ok(64.0), |r| r.parse())?;
if let Some(server) = &self.integrated {
let center = self.player.as_ref().map_or(Vec3::ZERO, |p| p.position);
let value = server
.call(move |g| g.in_owner_level(|g| g.control_mobs_at(radius, center)))
.ok_or("the integrated server stopped")?;
return Ok(Some(value));
}
Ok(Some(self.control_mobs(radius)))
}
"hurtlog" => {
self.require_world()?;
if let Some(server) = &self.integrated {
let value = server
.call(|g| g.control_hurt_log())
.ok_or("the integrated server stopped")?;
return Ok(Some(value));
}
Ok(Some(self.control_hurt_log()))
}
"resume" => {
@@ -312,6 +312,9 @@ impl Game<'_> {
/// The `mobs` command: nearby mobs with position, health and running goals.
pub(super) fn control_mobs(&self, radius: f64) -> serde_json::Value {
let center = self.player.as_ref().map_or(Vec3::ZERO, |p| p.position);
self.control_mobs_at(radius, center)
}
pub(super) fn control_mobs_at(&self, radius: f64, center: Vec3) -> serde_json::Value {
let list: Vec<serde_json::Value> = self
.mobs
.iter()
@@ -504,6 +507,17 @@ impl Game<'_> {
Some(host) if !guests.is_empty() => std::mem::take(&mut host.mob_players),
_ => Default::default(),
};
// The guests' entity ids by connection, for the shulker bullets fired
// at the guest a mob sees.
let guest_ids: Vec<(u32, u32)> = match self.lan.as_ref() {
Some(host) if !guests.is_empty() => host
.hub
.guests
.iter()
.map(|g| (g.conn, g.entity_id as u32))
.collect(),
_ => Vec::new(),
};
let mut guest_events = Vec::new();
let level_random;
{
@@ -578,6 +592,19 @@ impl Game<'_> {
let kept = m.tick(&w, &mut scratch, &mut events);
horizon_client::mobs::take_mob_pushes(&mut events, from, &mut pushes);
if let Some(conn) = seen_guest {
// ShulkerBullet.finalTarget is the guest's entity: the
// bullet homes on it while the projectiles tick, where
// `Who::Player` is the host's own player.
if let Some(&(_, id)) = guest_ids.iter().find(|g| g.0 == conn) {
for e in &mut events[from..] {
if let MobEvent::Projectile(p) = e
&& let ProjectileKind::ShulkerBullet { target, .. } = &mut p.kind
&& *target == Who::Player
{
*target = Who::Mob(id);
}
}
}
let mut k = from;
while k < events.len() {
if super::lan_mobs::acts_on_player(&events[k]) {
+73 -15
View File
@@ -73,6 +73,18 @@ impl Rocket {
(self.position, self.velocity)
}
}
/// `damageAmount * (float)Math.sqrt((5.0 - this.distanceTo(target)) / 5.0)`
/// from the rocket at `from` to a target at `to`; `Entity.distanceTo` sums
/// the squared offsets in floats.
fn blast_damage(damage: f32, from: Vec3, to: Vec3) -> f32 {
let (x, y, z) = (
(from.x - to.x) as f32,
(from.y - to.y) as f32,
(from.z - to.z) as f32,
);
let distance = f64::from(x * x + y * y + z * z).sqrt() as f32;
damage * ((5.0 - f64::from(distance)) / 5.0).sqrt() as f32
}
pub(super) fn fireworks_of(stack: &ItemStack) -> (u8, Vec<FireworkExplosion>) {
// The item's default component: flight 1, no explosions.
stack
@@ -261,21 +273,24 @@ impl Game<'_> {
}
if !r.explosions.is_empty() {
let damage = 5.0 + r.explosions.len() as f32 * 2.0;
// The player it boosts takes the full damage, a guest as well.
if r.attached {
match r.guest {
None => self.hurt_player_by("minecraft:fireworks", damage),
Some(conn) => {
if let Some(mut host) = self.lan.take() {
self.as_guest(&mut host, conn, |g| {
g.hurt_player_by("minecraft:fireworks", damage);
});
self.lan = Some(host);
}
}
}
}
let from = r.position;
// `attachedToEntity` takes the full damage, a guest as well; every
// other player in range and in sight takes the blast.
let boosted = r.guest.filter(|_| r.attached);
if r.attached && boosted.is_none() {
self.rocket_hurt_player(damage);
} else {
self.rocket_blast_player(from, damage);
}
if let Some(conn) = boosted
&& let Some(mut host) = self.lan.take()
{
self.as_guest(&mut host, conn, |g| g.rocket_hurt_player(damage));
self.lan = Some(host);
}
// Every other player in the level (LAN guests, the players of a
// server thread), each swapped in as the player.
self.each_guest_hit_except(boosted, |g| g.rocket_blast_player(from, damage));
let hits: Vec<(u32, f32)> = {
let world = self.mob_world_base(&[]);
self.mobs
@@ -295,7 +310,7 @@ impl Game<'_> {
);
world.clear_between(from, to, false)
});
sees.then(|| (m.id, damage * ((5.0 - d2.sqrt()) / 5.0).sqrt() as f32))
sees.then(|| (m.id, blast_damage(damage, from, m.position)))
})
.collect()
};
@@ -327,6 +342,49 @@ impl Game<'_> {
.fireworks(at, v, r.explosions.clone(), camera.unwrap_or(at));
}
}
/// `dealExplosionDamage` on a player that is not the rocket's
/// `attachedToEntity`: within 5 blocks, when the rocket sees its feet or
/// its middle (`ClipContext.Block.COLLIDER`, no fluids).
fn rocket_blast_player(&mut self, from: Vec3, damage: f32) {
let Some(p) = self.player.as_ref() else {
return;
};
let feet = p.position;
let d2 = (feet.x - from.x).powi(2) + (feet.y - from.y).powi(2) + (feet.z - from.z).powi(2);
if d2 > 25.0 {
return;
}
let height = f64::from(p.dimensions.height);
let sees = {
let world = self.mob_world_base(&[]);
(0..2).any(|step| {
let to = Vec3::new(feet.x, feet.y + height * 0.5 * f64::from(step), feet.z);
world.clear_between(from, to, false)
})
};
if sees {
self.rocket_hurt_player(blast_damage(damage, from, feet));
}
}
/// `hurtServer` with `damageSources().fireworks`: armor absorbs it and
/// wears (`hurtArmor`); creative and spectator players are immune.
fn rocket_hurt_player(&mut self, amount: f32) {
if self.creative || self.spectator() {
return;
}
let hurt = if let Some(p) = self.player.as_mut()
&& !p.health.dead_or_dying()
&& let Some(id) = self.motion.health.damage.id("minecraft:fireworks")
{
p.hurt(&self.motion.health, self.world.health_rules, id, amount)
.unwrap_or(false)
} else {
false
};
if hurt {
self.hurt_armor(amount);
}
}
/// `FireworkEntityRenderer.submit`: the item (GROUND) facing the camera,
/// lit at the rocket's light probe.
pub(super) fn rocket_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec<Vertex>) {
+7 -7
View File
@@ -64,14 +64,14 @@ impl Game<'_> {
});
self.view.tick(eye, target);
}
/// The player's `deathTime` while dead or dying.
fn death_time(&self) -> Option<i32> {
/// The player's `deathTime` while dead or dying: whole ticks counted by
/// `LivingEntity.tickDeath`, so `deathTime + partialTicks` in
/// `Camera.extractRenderState` rises smoothly across frames.
pub(super) fn death_time(&self) -> Option<i32> {
let p = self.player.as_ref()?;
p.health.dead_or_dying().then(|| {
self.ui
.died_at
.map_or(0, |t| (t.elapsed().as_secs_f32() * 20.0) as i32)
})
p.health
.dead_or_dying()
.then(|| self.ui.death_time.min(i32::MAX as u32) as i32)
}
/// The fluid at the camera, as `Camera.getFluidInCamera` reports it.
pub(super) fn camera_fluid(&self, camera: [f64; 3]) -> CameraFluid {
@@ -370,6 +370,12 @@ impl Game<'_> {
let mut next = Instant::now();
while !flags.stop.load(Ordering::Relaxed) {
self.run_tasks();
if self.agent.hold_server {
// Keep servicing control requests so the fixture can release the barrier.
// Unlike /tick freeze, no player's queued packets are applied here.
self.wait_for_next_server_tick(&mut next, true);
continue;
}
let paused = flags.paused.load(Ordering::Relaxed);
if paused {
// Only the connections run.
+72 -58
View File
@@ -1192,6 +1192,10 @@ impl Game<'_> {
{
self.container_changed(key);
}
// A vehicle's menu edits the vehicle's own items.
if let Some(Slots::Block(key)) = slots {
self.store_cart_items(key);
}
self.drop_guest_items(host, gi, drops);
}
S::ContainerClose(p) => {
@@ -1831,6 +1835,10 @@ impl Game<'_> {
continue;
}
if self.on_server_trip(conn) {
// LivingEntity.hurt kills at once (die), whether the level
// has loaded around the player or not: its respawn request
// is taken as soon as it comes.
self.guest_die(host, gi, conn);
continue;
}
let play = host.play.entry(conn).or_default();
@@ -1916,69 +1924,72 @@ impl Game<'_> {
self.guest_dig_feedback(host, conn);
self.guest_pickups(host, gi);
self.guest_orbs(host, gi);
if host.hub.guests[gi].health <= 0.0 && !host.hub.guests[gi].dead {
self.guest_forget_digging(host, conn);
// FishingHook.shouldStopFishing: its owner is dead.
if let Some(play) = host.play.get_mut(&conn) {
play.hook = None;
}
self.guest_die(host, gi, conn);
}
}
/// `ServerPlayer.die` for a guest whose health has run out: the death
/// message, the loot and experience, the death screen.
fn guest_die(&mut self, host: &mut LanHost, gi: usize, conn: u32) {
if host.hub.guests[gi].health <= 0.0 && !host.hub.guests[gi].dead {
self.guest_forget_digging(host, conn);
// FishingHook.shouldStopFishing: its owner is dead.
if let Some(play) = host.play.get_mut(&conn) {
play.hook = None;
}
let g = &mut host.hub.guests[gi];
if g.health <= 0.0 && !g.dead {
g.dead = true;
g.last_death = Some(g.position.map(|v| v.floor() as i32));
let (id, at) = (g.entity_id, g.position);
let reward = Experience {
level: g.xp_level,
progress: g.xp_progress,
total: g.xp_total,
}
.death_reward(self.ui.rules.bool("keep_inventory") || g.game_mode == SPECTATOR);
let (name, uuid) = (g.name.clone(), g.uuid);
// ServerPlayer.die: dropAllDeathLoot unless a spectator, from the eyes less 0.3.
let drops = (g.game_mode != SPECTATOR)
.then(|| [at[0], at[1] + g.eye_height() - 0.3, at[2]]);
let cause = host.play.get_mut(&conn).and_then(|p| p.death_cause.take());
// ServerPlayer.die: with showDeathMessages, the message goes to
// the dying player's death screen and to everyone the team lets
// see it; without it, the death screen gets an empty one.
let message = if self.ui.rules.bool("show_death_messages") {
let me = self.player_text(&name, uuid);
let text = match cause {
Some(key) => horizon_client::text::Text::translate(&key, &[me]),
None if at[1] < f64::from(horizon_client::chunk::MIN_Y - 64) => {
horizon_client::text::Text::translate("death.attack.outOfWorld", &[me])
}
None => self.guest_death_message(conn, me),
};
let message = horizon_client::lan::data::component_nbt(&text.component());
let team = self.score.board.players_team(&name);
if horizon_client::scoreboard::own_death_message_shown(team) {
host.hub.system_message(message.clone());
if self.options.dedicated.is_some() {
super::dedicated::log(
"INFO",
&format!("System chat: {}", text.plain()),
);
} else if self.player.is_some() {
self.system_message(text);
}
}
let g = &mut host.hub.guests[gi];
if g.health <= 0.0 && !g.dead {
g.dead = true;
g.last_death = Some(g.position.map(|v| v.floor() as i32));
let (id, at) = (g.entity_id, g.position);
let reward = Experience {
level: g.xp_level,
progress: g.xp_progress,
total: g.xp_total,
}
.death_reward(self.ui.rules.bool("keep_inventory") || g.game_mode == SPECTATOR);
let (name, uuid) = (g.name.clone(), g.uuid);
// ServerPlayer.die: dropAllDeathLoot unless a spectator, from the eyes less 0.3.
let drops =
(g.game_mode != SPECTATOR).then(|| [at[0], at[1] + g.eye_height() - 0.3, at[2]]);
let cause = host.play.get_mut(&conn).and_then(|p| p.death_cause.take());
// ServerPlayer.die: with showDeathMessages, the message goes to
// the dying player's death screen and to everyone the team lets
// see it; without it, the death screen gets an empty one.
let message = if self.ui.rules.bool("show_death_messages") {
let me = self.player_text(&name, uuid);
let text = match cause {
Some(key) => horizon_client::text::Text::translate(&key, &[me]),
None if at[1] < f64::from(horizon_client::chunk::MIN_Y - 64) => {
horizon_client::text::Text::translate("death.attack.outOfWorld", &[me])
}
message
} else {
literal("")
None => self.guest_death_message(conn, me),
};
host.hub.send_to(
conn,
&C::PlayerCombatKill(play::PlayerCombatKill { id, message }),
);
// LivingEntity.die: the death animation, the dying player included.
host.hub.entity_event(id, 3, at);
if let Some(eyes) = drops {
self.as_guest(host, conn, |game| game.drop_inventory(eyes));
let message = horizon_client::lan::data::component_nbt(&text.component());
let team = self.score.board.players_team(&name);
if horizon_client::scoreboard::own_death_message_shown(team) {
host.hub.system_message(message.clone());
if self.options.dedicated.is_some() {
super::dedicated::log("INFO", &format!("System chat: {}", text.plain()));
} else if self.player.is_some() {
self.system_message(text);
}
}
self.spawn_orbs(at, reward);
message
} else {
literal("")
};
host.hub.send_to(
conn,
&C::PlayerCombatKill(play::PlayerCombatKill { id, message }),
);
// LivingEntity.die: the death animation, the dying player included.
host.hub.entity_event(id, 3, at);
if let Some(eyes) = drops {
self.as_guest(host, conn, |game| game.drop_inventory(eyes));
}
self.spawn_orbs(at, reward);
}
}
@@ -2176,6 +2187,9 @@ impl Game<'_> {
let _ = w.tick_environment_damage();
let _ = w.apply_fire_contact();
let _ = w.server_hazards_tick(in_wall);
// Player.tick ends with updatePlayerPose: a swimmer, or a player
// crawling under a block, takes the short box its moves are checked with.
let _ = w.update_player_pose();
let mut events = std::mem::take(&mut w.player.events);
// LayeredCauldronBlock.handleEntityOnFireInside along those moves.
for event in &events {
@@ -291,6 +291,11 @@ impl Game<'_> {
let at = host.hub.guests[gi].position;
self.award_furnace_recipes(pos, at);
}
// Slot.setChanged on a changed container slot: BlockEntity.setChanged,
// so the chunk is saved and comparators read the block again.
if changed.iter().any(|&s| s < items.len()) {
self.container_changed(pos);
}
host.play.entry(conn).or_default().crafting.machine = Some((p.container, menu));
host.hub.inventory_clicked(conn, p, &self.world, &changed);
true
+12 -2
View File
@@ -119,7 +119,17 @@ impl Game<'_> {
/// Runs `f` as each guest in play here, for what strikes every player in
/// an area (`Level.getEntities`), not only the window's own.
pub(super) fn each_guest_hit(&mut self, mut f: impl FnMut(&mut Self)) {
pub(super) fn each_guest_hit(&mut self, f: impl FnMut(&mut Self)) {
self.each_guest_hit_except(None, f);
}
/// [`Self::each_guest_hit`] leaving out the guest `except` (the one a
/// firework rocket is attached to, which it hurts on its own).
pub(super) fn each_guest_hit_except(
&mut self,
except: Option<u32>,
mut f: impl FnMut(&mut Self),
) {
let Some(mut host) = self.lan.take() else {
return;
};
@@ -127,7 +137,7 @@ impl Game<'_> {
.hub
.guests
.iter()
.filter(|g| host.hub.here(g) && g.loaded && !g.dead)
.filter(|g| host.hub.here(g) && g.loaded && !g.dead && Some(g.conn) != except)
.map(|g| g.conn)
.collect();
for conn in conns {
+25 -12
View File
@@ -1117,22 +1117,35 @@ impl Game<'_> {
self.swing_arm();
true
}
/// Closing a cart's container copies the items back.
pub(super) fn close_cart_container(&mut self) {
let Some(key) = self.container.filter(|k| k.1 == CART_KEY_Y) else {
return;
};
let Some(BlockEntity::Chest(items)) = self.world.block_entities.remove(&key) else {
/// Closing the cart's container open under `key` copies the items back.
pub(super) fn close_cart_container(&mut self, key: (i32, i32, i32)) {
if key.1 != CART_KEY_Y {
return;
}
self.store_cart_items(key);
self.world.block_entities.remove(&key);
if let Some(c) = self.carts.iter().find(|c| c.id as i32 == key.2)
&& c.kind == CartKind::Chest
{
let at = [c.position.x, c.position.y, c.position.z];
let pitch = self.world.random.next_float() * 0.1 + 0.9;
self.audio
.play("minecraft:block.chest.close", Some(at), 0.5, pitch);
}
}
/// The menu of a chest or hopper minecart or a chest boat works on the
/// vehicle's own items (`ContainerEntity`): the placeholder under `key`
/// that the menu edits is copied back to the vehicle.
pub(super) fn store_cart_items(&mut self, key: (i32, i32, i32)) {
if key.1 != CART_KEY_Y {
return;
}
let Some(BlockEntity::Chest(items)) = self.world.block_entities.get(&key) else {
return;
};
let items = items.clone();
if let Some(c) = self.carts.iter_mut().find(|c| c.id as i32 == key.2) {
c.items = items;
let at = [c.position.x, c.position.y, c.position.z];
if c.kind == CartKind::Chest {
let pitch = self.world.random.next_float() * 0.1 + 0.9;
self.audio
.play("minecraft:block.chest.close", Some(at), 0.5, pitch);
}
} else if let Some(b) = self.boats.iter_mut().find(|b| b.id as i32 == key.2) {
b.items = items;
}
+12 -1
View File
@@ -2126,6 +2126,7 @@ impl Game<'_> {
self.apply_remote_health();
if p.health <= 0.0 && self.screen != Screen::Dead && self.player.is_some() {
self.screen = Screen::Dead;
self.ui.death_time = 0;
self.capture(false);
}
}
@@ -2142,6 +2143,7 @@ impl Game<'_> {
// screen, whatever health was last sent.
if me && self.screen != Screen::Dead && self.player.is_some() {
self.screen = Screen::Dead;
self.ui.death_time = 0;
self.capture(false);
}
}
@@ -3180,6 +3182,11 @@ impl Game<'_> {
);
let was_flying = player.flying;
self.sync_player_body();
// LivingEntity.tickDeath: the local player counts its deathTime once
// per client tick while dead (the camera's death roll and zoom).
if self.screen == Screen::Dead {
self.ui.death_time = self.ui.death_time.saturating_add(1);
}
let Some(player) = self.player.as_mut() else {
return Ok(());
};
@@ -3508,7 +3515,7 @@ impl Game<'_> {
// ticks), and only the server removes it.
let (world, facts, collision) = (&self.world, self.res.facts(), self.collision);
for d in self.dropped.iter_mut().filter(|_| running) {
d.tick(world, facts, collision, false);
d.tick(world, facts, collision, false, &self.res.motion().fluids);
}
head_lerps.retain(|_, (_, steps)| *steps > 0);
for (item, ticks, at, eye, pitch, yaw) in using {
@@ -3958,6 +3965,10 @@ impl Game<'_> {
if let Some(placed) = &placed {
self.predict_place(placed, seq);
self.with_hand(hand, Game::consume_held);
} else if let Some(r) = self.remote.as_mut() {
// BlockItem.place plays its sound only once the block is set: the
// server's resend of the target it refused is no placement.
r.placing = None;
}
if reacts || placed.is_some() {
self.with_hand(hand, Game::swing_arm);
@@ -353,7 +353,8 @@ impl Game<'_> {
out.push(json!({"id": t.id, "kind": "minecraft:trident", "position": at(t.position)}));
}
for p in &self.creatures.projectiles {
out.push(json!({"id": p.id, "kind": format!("{:?}", p.kind).split([' ', '{']).next().unwrap_or(""), "position": at(p.position)}));
out.push(json!({"id": p.id, "kind": format!("{:?}", p.kind).split([' ', '{']).next().unwrap_or(""), "position": at(p.position), "velocity": at(p.velocity),
"player_owned": p.owner == Some(horizon_client::mobs::Who::Player)}));
}
for (kind, p, _) in self.potions.thrown_entities() {
out.push(json!({"kind": kind, "position": at(p)}));
+1
View File
@@ -88,6 +88,7 @@ mod stars;
mod timing;
pub mod translucency;
pub(crate) mod upload;
mod wgsl_prefix;
mod world_post;
/// A column whose every section has its mesh (`Renderer::lod_detail_columns`).
+3 -50
View File
@@ -9,9 +9,9 @@
use horizon_shader_api::{check::CheckedStage, stages::GROUP};
use super::native_post::{NativeFrame, present_constants};
use super::wgsl_prefix::{TERRAIN, prefixed, section};
const PREFIX: &str = "horizon_sky_";
const TERRAIN: &str = include_str!("../terrain.wgsl");
/// The fragment entry that calls the pack: vanilla's colour in, an opaque colour out.
const PACK_ENTRY: &str = "
@@ -26,62 +26,15 @@ pub struct NativeSky {
pub label: String,
}
/// The text of `source` from the line starting with `start` up to (not including) the one
/// starting with `end`.
fn section<'a>(source: &'a str, start: &str, end: &str) -> &'a str {
let from = source
.find(&format!("\n{start}"))
.expect("terrain.wgsl has the section")
+ 1;
let to = from
+ source[from..]
.find(&format!("\n{end}"))
.expect("terrain.wgsl ends the section")
+ 1;
&source[from..to]
}
/// terrain.wgsl's sky with every name it declares (and `globals`) prefixed.
fn vanilla_sky() -> String {
let text = [
section(TERRAIN, "struct Globals {", "@group(0) @binding(0)"),
section(TERRAIN, "struct Globals {", "@group(0) @binding(1)"),
section(TERRAIN, "fn linear_fog(", "fn apply_fog("),
section(TERRAIN, "struct SkyOut {", "// rendertype_lines"),
]
.concat();
let mut names = vec!["globals"];
for line in text.lines() {
let line = line
.trim_start_matches("@vertex ")
.trim_start_matches("@fragment ");
for keyword in ["fn ", "struct ", "const "] {
if let Some(rest) = line.strip_prefix(keyword) {
let end = rest
.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
.unwrap_or(rest.len());
names.push(&rest[..end]);
}
}
}
let mut out = String::with_capacity(text.len() * 2);
let mut word = String::new();
let flush = |word: &mut String, out: &mut String| {
if names.contains(&word.as_str()) {
out.push_str(PREFIX);
}
out.push_str(word);
word.clear();
};
for c in text.chars() {
if c.is_ascii_alphanumeric() || c == '_' {
word.push(c);
} else {
flush(&mut word, &mut out);
out.push(c);
}
}
flush(&mut word, &mut out);
out + &format!("@group(0) @binding(0) var<uniform> {PREFIX}globals: {PREFIX}Globals;\n")
prefixed(&text, PREFIX)
}
/// The sky shader for the pack's sky stage `source`.
@@ -0,0 +1,124 @@
//! Copies of the renderer's own WGSL that run beside a shader pack's code in one module (the
//! sky and material stages of Horizon's packs, docs/shaders.md): every name such a copy
//! declares takes a prefix starting with `horizon_`, which the pack check refuses in a pack,
//! so no pack name can meet one of Horizon's.
/// terrain.wgsl, which the copies are taken from.
pub const TERRAIN: &str = include_str!("../terrain.wgsl");
/// The text of `source` from the line starting with `start` up to (not including) the one
/// starting with `end`.
pub fn section<'a>(source: &'a str, start: &str, end: &str) -> &'a str {
let from = source
.find(&format!("\n{start}"))
.expect("the source has the section")
+ 1;
let to = from
+ source[from..]
.find(&format!("\n{end}"))
.expect("the source ends the section")
+ 1;
&source[from..to]
}
/// The names `text` declares at its top level: functions, structs, constants, overrides and
/// module variables (bindings).
fn declared(text: &str) -> Vec<&str> {
let mut names = Vec::new();
for line in text.lines() {
// Top level only: a function's own `var` and `let` stay as they are.
if line.starts_with(char::is_whitespace) {
continue;
}
let mut line = line;
while let Some(rest) = line.strip_prefix('@') {
// An attribute: `@vertex`, `@group(0)`, `@binding(1)`.
let end = rest.find(' ').unwrap_or(rest.len());
line = rest[end..].trim_start();
}
let rest = ["fn ", "struct ", "const ", "override ", "var "]
.iter()
.find_map(|k| line.strip_prefix(k))
.or_else(|| {
// `var<uniform> name`, `var<storage, read> name`.
let rest = line.strip_prefix("var<")?;
Some(rest[rest.find('>')? + 1..].trim_start())
});
if let Some(rest) = rest {
let end = rest
.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
.unwrap_or(rest.len());
names.push(&rest[..end]);
}
}
names
}
/// `text` with every name it declares at its top level, and every use of one, prefixed by
/// `prefix`. Comments are rewritten too, which is harmless.
pub fn prefixed(text: &str, prefix: &str) -> String {
let names = declared(text);
let mut out = String::with_capacity(text.len() * 2);
let mut word = String::new();
let flush = |word: &mut String, out: &mut String| {
if names.contains(&word.as_str()) {
out.push_str(prefix);
}
out.push_str(word);
word.clear();
};
for c in text.chars() {
if c.is_ascii_alphanumeric() || c == '_' {
word.push(c);
} else {
flush(&mut word, &mut out);
out.push(c);
}
}
flush(&mut word, &mut out);
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prefixes_top_level_names_and_their_uses_only() {
let text = "struct S { a: f32, }\n\
@group(0) @binding(0) var<uniform> g: S;\n\
@group(1) @binding(2) var<storage, read> list: array<u32>;\n\
override N: u32 = 1u;\n\
const K: f32 = 2.0;\n\
@fragment fn f(x: f32) -> @location(0) vec4<f32> {\n \
var a = g.a * K;\n \
return vec4(a + f32(list[N]) + x);\n\
}\n";
let out = prefixed(text, "p_");
assert_eq!(
out,
"struct p_S { a: f32, }\n\
@group(0) @binding(0) var<uniform> p_g: p_S;\n\
@group(1) @binding(2) var<storage, read> p_list: array<u32>;\n\
override p_N: u32 = 1u;\n\
const p_K: f32 = 2.0;\n\
@fragment fn p_f(x: f32) -> @location(0) vec4<f32> {\n \
var a = p_g.a * p_K;\n \
return vec4(a + f32(p_list[p_N]) + x);\n\
}\n"
);
// Every top-level declaration of terrain.wgsl is found.
let names = declared(TERRAIN);
for name in [
"Globals",
"globals",
"sprites_7",
"SPRITE_SPLIT",
"block_slots",
"fs_sky",
] {
assert!(names.contains(&name), "{name}");
}
assert!(!names.contains(&"out"));
}
}
+13 -6
View File
@@ -2175,17 +2175,24 @@ impl Hub {
if g.spectator() || g.sleeping {
return (false, false);
}
let height = if g.fall_flying {
0.6
// getBoundingBox: the box of the body's pose (swimming and crawling
// are 0.6 tall), so a swimmer under a ceiling isn't sent back.
let (half, height) = if g.fall_flying {
(0.3, 0.6)
} else if let Some(m) = &g.motion {
(
f64::from(m.dimensions.width) / 2.0,
f64::from(m.dimensions.height),
)
} else if g.sneaking {
1.5
(0.3, 1.5)
} else {
1.8
(0.3, 1.8)
};
let at = |p: [f64; 3]| {
Aabb::new(
Vec3::new(p[0] - 0.3, p[1], p[2] - 0.3),
Vec3::new(p[0] + 0.3, p[1] + height, p[2] + 0.3),
Vec3::new(p[0] - half, p[1], p[2] - half),
Vec3::new(p[0] + half, p[1] + height, p[2] + half),
)
};
let (old, new) = (at(g.position), at(to));
+6
View File
@@ -4,7 +4,13 @@ mod game;
use horizon_client::{crash, logging};
/// The mod API level this game serves, kept in the program for the launcher
/// to read without running it (`horizon_mods::api_level::MARK`). `main`
/// reads it, so the linker keeps it.
static MOD_API_LEVEL: [u8; 27] = horizon_mods::api_level::MARK;
fn main() {
std::hint::black_box(&MOD_API_LEVEL);
horizon_client::telemetry::mark_start();
let args: Vec<String> = std::env::args().skip(1).collect();
let game_args = args.iter().take_while(|a| *a != "--dedicated");
+1 -1
View File
@@ -44,7 +44,7 @@ use std::{
pub(crate) mod environment;
mod fluids;
pub use fluids::{FluidResources, LAVA, PlayerFluids, WATER};
pub use fluids::{FluidResources, FluidTags, LAVA, PlayerFluids, WATER};
pub struct MotionResources {
pub attributes: Arc<Catalog>,
+6 -5
View File
@@ -103,10 +103,10 @@ fn is_tagged(kind: FluidKind, tag: FluidTagId) -> bool {
}
impl PlayerMotion {
pub fn fluid_height(&self, tag: FluidTagId) -> f64 {
self.fluids.interaction.fluid_height(&Membership, tag)
self.fluids.interaction.fluid_height(&FluidTags, tag)
}
pub fn is_eye_in_fluid(&self, tag: FluidTagId) -> bool {
self.fluids.interaction.is_eye_in_fluid(&Membership, tag)
self.fluids.interaction.is_eye_in_fluid(&FluidTags, tag)
}
/// `LocalPlayer.isUnderWater`: `wasUnderwater`, the eye sample `Player.tick`
/// takes before the base tick (`Entity.isUnderWater` also needs the body
@@ -116,11 +116,12 @@ impl PlayerMotion {
}
/// `Entity.isInLava`.
pub fn is_in_lava(&self) -> bool {
!self.fluids.first_tick && self.fluids.interaction.is_in_fluid(&Membership, LAVA)
!self.fluids.first_tick && self.fluids.interaction.is_in_fluid(&FluidTags, LAVA)
}
}
struct Membership;
impl FluidMembership for Membership {
/// Which fluids `FluidTags.WATER` and `FluidTags.LAVA` hold.
pub struct FluidTags;
impl FluidMembership for FluidTags {
fn fluid_in_tag(&self, kind: FluidKind, tag: FluidTagId) -> bool {
is_tagged(kind, tag)
}
+11 -11
View File
@@ -164,21 +164,21 @@ pub fn fetch_index() -> Result<Index, String> {
/// Installs `m` into the game folder `dir` (where `options.txt` is), for a
/// player whose extras were `e`: its newest version for this game that
/// wasn't taken down, checked against the index (size, SHA-256, signature,
/// manifest), as `horizon-mods/<id>.hmod`, turned on, with the extra's
/// settings in its storage unless the mod has its own already.
/// wasn't taken down and that this Horizon's mod API level runs, checked
/// against the index (size, SHA-256, signature, manifest), as
/// `horizon-mods/<id>.hmod`, turned on, with the extra's settings in its
/// storage unless the mod has its own already.
pub fn install(index: &Index, m: &Moved, e: &Extras, dir: &Path) -> Result<(), String> {
let project = index
.project(m.id)
.ok_or_else(|| format!("{} isn't in Horizon's registry yet", m.id))?;
let version = project
.versions
.iter()
.find(|v| {
v.game == horizon_core::version::VERSION_ID
&& index.revoked(&project.id, &v.file.sha256).is_none()
})
.ok_or_else(|| format!("{} has no version for this game", m.id))?;
let version = index
.installable(
project,
horizon_core::version::VERSION_ID,
horizon_mods::api_level::LEVEL,
)
.ok_or_else(|| format!("{} has no version this Horizon can run", m.id))?;
let mods = dir.join(crate::mod_list::DIR);
fs::create_dir_all(&mods).map_err(|e| format!("{}: {e}", mods.display()))?;
let target = mods.join(format!("{}.hmod", m.id));
+24
View File
@@ -401,6 +401,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
dependencies = [
"foldhash",
"serde",
"serde_core",
]
[[package]]
@@ -442,6 +444,7 @@ dependencies = [
"serde_json",
"sha2",
"toml",
"wasmparser",
"zip",
]
@@ -649,6 +652,8 @@ checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855"
dependencies = [
"equivalent",
"hashbrown",
"serde",
"serde_core",
]
[[package]]
@@ -921,6 +926,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "semver"
version = "1.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd"
[[package]]
name = "serde"
version = "1.0.229"
@@ -1326,6 +1337,19 @@ dependencies = [
"wit-bindgen",
]
[[package]]
name = "wasmparser"
version = "0.254.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5ff8685b587722894b6f25958fc608d315b93b8d23647bfb567664e7006d757"
dependencies = [
"bitflags",
"hashbrown",
"indexmap",
"semver",
"serde",
]
[[package]]
name = "webpki-roots"
version = "0.26.11"
+9 -2
View File
@@ -17,20 +17,27 @@ wasmtime = { version = "48.0.7", default-features = false, features = ["runtime"
# Turns a mod's core module into a component (cargo horizon-mod build, the tests).
wit-component = { version = "0.254", optional = true }
wit-parser = { version = "0.254", optional = true }
# The functions a mod imports, for its API level (src/api_level.rs): the game and the registry index.
wasmparser = { version = "0.254", optional = true }
# The registry index's checks (feature `index`): SHA-256, and minisign signatures (no dependencies of its own).
sha2 = { version = "0.10", optional = true }
minisign-verify = { version = "0.3", optional = true }
# A shader pack's check (kind `shader`): the one the game runs before it loads a pack.
horizon-shader-api = { path = "../horizon-shader-api", optional = true }
[dev-dependencies]
# A component made for a newer Horizon, from the WIT alone (tests/sandbox.rs).
wit-component = { version = "0.254", features = ["dummy-module"] }
wit-parser = "0.254"
[features]
# Loads and runs mods (the game's `mods` feature); without it only manifests,
# content and packaging. Off by default so builds that don't run mods (the
# game's tests and probes) skip wasmtime and cranelift.
runtime = ["dep:wasmtime"]
runtime = ["dep:wasmtime", "dep:wasmparser"]
pack = ["dep:wit-component", "dep:wit-parser"]
# The mod registry's index (docs/api/mod-index.md): the launcher, the registry server and its CI.
index = ["dep:sha2", "dep:minisign-verify", "dep:horizon-shader-api"]
index = ["dep:sha2", "dep:minisign-verify", "dep:horizon-shader-api", "dep:wasmparser"]
# Runs real mods: `cargo test -p horizon-mods --features runtime,pack`.
[[test]]
+189
View File
@@ -0,0 +1,189 @@
//! Mod API levels: which of the WIT contract's functions a Horizon has
//! (`docs/api/stability.md`). Every function the game offers mods has the
//! level it arrived at; a Horizon serves every level up to its [`LEVEL`].
//! A mod needs the level of the newest function it imports, so a mod that
//! calls a function this Horizon doesn't have is refused with "update
//! Horizon" instead of a linking error, and the registry lists each
//! version's level so the launcher can tell before it installs.
/// The newest level this Horizon serves.
pub const LEVEL: u32 = 1;
/// Every function the game offers mods, as `interface.function`, with its
/// level. Level 0 is the contract of Horizon 0.2.67 to 0.2.69. A new
/// function takes the next level and raises [`LEVEL`] (the `api_reference`
/// test fails until it is listed here).
pub const FUNCTIONS: [(&str, u32); 32] = [
("log.write", 0),
("game.get-player", 0),
("game.get-clock", 0),
("chat.show", 0),
("hud.set-panel", 0),
("hud.clear", 0),
("storage.get", 0),
("storage.set", 0),
("storage.delete", 0),
("command.run", 0),
("ui.open-screen", 0),
("ui.close-screen", 0),
("ui.set-settings", 0),
("input.claim-items", 0),
("input.register-key", 0),
("input.held", 0),
("input.poll", 0),
("input.claim-wheel", 0),
("input.mapping", 0),
("input.clicks-per-second", 0),
("render.set-hud", 0),
("render.gui-size", 0),
("render.shake", 0),
("render.set-corner", 0),
("render.clear-corner", 0),
("render.text-width", 0),
("render.frame-stats", 0),
("render.set-zoom", 0),
("render.set-minimap", 1),
("players.online", 1),
("players.tell", 1),
("players.broadcast", 1),
];
/// What the game's program carries so the launcher can read its level
/// without running it: `horizon-mod-api-level:0001;` (four digits). A
/// program from before the mark serves level 0.
pub const MARK: [u8; 27] = {
let mut mark = *b"horizon-mod-api-level:0000;";
let mut level = LEVEL;
let mut i = 25;
while level > 0 {
mark[i] = b'0' + (level % 10) as u8;
level /= 10;
i -= 1;
}
mark
};
/// The level a program's bytes carry in their [`MARK`], if any.
pub fn read_mark(program: &[u8]) -> Option<u32> {
let prefix = &MARK[..22];
program
.windows(MARK.len())
.filter(|w| w.starts_with(prefix) && w[26] == b';')
.find_map(|w| std::str::from_utf8(&w[22..26]).ok()?.parse().ok())
}
/// A function's level; None for one this Horizon doesn't have.
pub fn level_of(function: &str) -> Option<u32> {
FUNCTIONS
.iter()
.find(|(name, _)| *name == function)
.map(|&(_, level)| level)
}
/// The level a mod needs, from the functions it imports (as [`imports`]
/// names them): its newest function's. A function this Horizon doesn't
/// know comes from a newer one, so it needs more than [`LEVEL`]; that
/// function is returned with it, for the message.
pub fn needed<'a>(imports: impl IntoIterator<Item = &'a str>) -> (u32, Option<&'a str>) {
let mut needed = (0, None);
for name in imports {
match level_of(name) {
Some(level) if level > needed.0 => needed = (level, Some(name)),
Some(_) => {}
None => return (LEVEL + 1, Some(name)),
}
}
needed
}
/// Why this Horizon can't run a mod that needs `level`, or None when it can.
pub fn too_new(level: u32, function: Option<&str>) -> Option<String> {
(level > LEVEL).then(|| {
let uses = function.map_or(String::new(), |f| format!(" (it uses {f})"));
format!("made for a newer Horizon{uses}: update Horizon to run it")
})
}
/// The functions a mod's component imports from Horizon (`horizon:mods/*`),
/// as `interface.function`, in the component's order.
#[cfg(any(feature = "runtime", feature = "index"))]
pub fn imports(wasm: &[u8]) -> Result<Vec<String>, String> {
use wasmparser::{
Parser, Payload, Validator, WasmFeatures, component_types::ComponentEntityType,
};
let types = Validator::new_with_features(WasmFeatures::all())
.validate_all(wasm)
.map_err(|e| format!("not a component: {e}"))?;
let types = types.as_ref();
let mut out = Vec::new();
for payload in Parser::new(0).parse_all(wasm) {
let Payload::ComponentImportSection(section) = payload.map_err(|e| e.to_string())? else {
continue;
};
for import in section {
let name = import.map_err(|e| e.to_string())?.name.name;
// `horizon:mods/render@0.1.0` → `render`.
let Some(interface) = name.strip_prefix("horizon:mods/") else {
continue;
};
let interface = interface.split('@').next().unwrap_or(interface);
let Some(item) = types.component_item_for_import(name) else {
continue;
};
if let ComponentEntityType::Instance(id) = item.ty {
for (function, export) in &types[id].exports {
if matches!(export.ty, ComponentEntityType::Func(_)) {
out.push(format!("{interface}.{function}"));
}
}
}
}
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn levels_are_listed_once_and_up_to_this_horizons() {
for (i, (name, level)) in FUNCTIONS.iter().enumerate() {
assert!(*level <= LEVEL, "{name}");
assert!(
FUNCTIONS[..i].iter().all(|(other, _)| other != name),
"{name} twice"
);
}
assert!(FUNCTIONS.iter().any(|&(_, level)| level == LEVEL));
}
#[test]
fn the_mark_carries_the_level() {
assert_eq!(read_mark(&MARK), Some(LEVEL));
let mut program = b"\0\x7fELF horizon-mod-api-level: horizon-mod-api-level:".to_vec();
program.extend_from_slice(&MARK);
program.extend_from_slice(b"\0rest");
assert_eq!(read_mark(&program), Some(LEVEL));
assert_eq!(read_mark(b"horizon-mod-api-level:12;"), None);
assert_eq!(read_mark(b"no mark"), None);
}
#[test]
fn a_mod_needs_its_newest_function() {
assert_eq!(needed(["log.write", "hud.set-panel"]), (0, None));
assert_eq!(
needed(["log.write", "render.set-minimap", "hud.clear"]),
(1, Some("render.set-minimap"))
);
assert_eq!(
needed(["render.set-minimap", "render.set-sky"]),
(LEVEL + 1, Some("render.set-sky"))
);
assert_eq!(too_new(LEVEL, None), None);
assert_eq!(
too_new(LEVEL + 1, Some("render.set-sky")).as_deref(),
Some("made for a newer Horizon (it uses render.set-sky): update Horizon to run it")
);
}
}
+32 -1
View File
@@ -138,6 +138,11 @@ pub struct Version {
pub side: Option<Side>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub capabilities: Vec<Capability>,
/// The mod API level a mod's code needs ([`crate::api_level`]), read
/// from its file; none for content and other kinds, and in indexes
/// built before levels.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub api_level: Option<u32>,
/// The Minecraft version it is for.
pub game: String,
/// A modpack's mods, as its manifest lists them.
@@ -317,6 +322,21 @@ impl Index {
Ok(())
}
/// A project's newest version for Minecraft `game` that wasn't taken
/// down and that a Horizon serving mod API `level` can run (an unknown
/// level counts as runnable: the game still checks when it starts it).
pub fn installable<'a>(
&self,
project: &'a Project,
game: &str,
level: u32,
) -> Option<&'a Version> {
project.versions.iter().find(|v| {
v.game == game
&& v.api_level.is_none_or(|l| l <= level)
&& self.revoked(&project.id, &v.file.sha256).is_none()
})
}
pub fn project(&self, id: &str) -> Option<&Project> {
self.projects.iter().find(|p| p.id == id)
}
@@ -472,6 +492,8 @@ pub struct Facts {
pub side: Option<Side>,
/// In the manifest's order.
pub capabilities: Vec<Capability>,
/// The mod API level a mod's code needs, from the functions it imports.
pub api_level: Option<u32>,
/// The Minecraft version, when the file names one (a modpack does).
pub game: Option<String>,
pub requires: Vec<Requirement>,
@@ -488,6 +510,7 @@ impl Facts {
|| self.api != version.api
|| self.side != version.side
|| caps != listed
|| version.api_level.is_some() && self.api_level != version.api_level
|| self.game.as_ref().is_some_and(|g| *g != version.game)
|| self.requires != version.requires;
if !differs {
@@ -522,9 +545,17 @@ impl Facts {
pub fn inspect(kind: &Kind, path: &Path) -> Result<Facts, String> {
match kind {
Kind::Mod => {
let m = Package::read(path)?.manifest;
let Package { manifest: m, wasm } = Package::read(path)?;
crate::Content::read(path, &m)?;
let api_level = match wasm {
Some(wasm) => {
let functions = crate::api_level::imports(&wasm)?;
Some(crate::api_level::needed(functions.iter().map(String::as_str)).0)
}
None => None,
};
Ok(Facts {
api_level,
capabilities: m.capabilities,
id: Some(m.id),
version: Some(m.version),
@@ -283,6 +283,7 @@ fn version(entry: &Entry, v: &EntryVersion, src: &Sources) -> Result<Version, St
api: facts.api,
side: facts.side,
capabilities: facts.capabilities,
api_level: facts.api_level,
game: v.game.clone(),
requires: facts.requires,
file: File {
+33
View File
@@ -76,6 +76,15 @@ fn a_changed_file_key_or_manifest_is_refused() {
let e = check(&more_caps, "coords_hud", &hmod).unwrap_err();
assert!(e.contains("isn't what the index lists"), "{e}");
let mut other_level = index.clone();
other_level.projects[0].versions[0].api_level = Some(1);
let e = check(&other_level, "coords_hud", &hmod).unwrap_err();
assert!(e.contains("isn't what the index lists"), "{e}");
// An index built before levels lists none: the file still passes.
let mut no_level = index.clone();
no_level.projects[0].versions[0].api_level = None;
assert_eq!(check(&no_level, "coords_hud", &hmod), Ok(Trust::Verified));
let mut revoked = index.clone();
revoked.revoked.push(Revoked {
id: "coords_hud".into(),
@@ -167,6 +176,30 @@ fn resource_packs_need_a_pack_mcmeta() {
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn the_newest_version_this_horizon_runs_is_installed() {
let mut index = sample();
let mut newer = index.projects[0].versions[0].clone();
newer.version = "0.2.0".into();
newer.file.sha256 = "2".repeat(64);
newer.api_level = Some(crate::api_level::LEVEL + 1);
index.projects[0].versions.insert(0, newer);
let p = index.project("coords_hud").unwrap();
let pick = |level| {
index
.installable(p, "26.3", level)
.map(|v| v.version.as_str())
};
assert_eq!(pick(crate::api_level::LEVEL), Some("0.1.0"));
assert_eq!(pick(crate::api_level::LEVEL + 1), Some("0.2.0"));
assert_eq!(index.installable(p, "26.4", crate::api_level::LEVEL), None);
// No level listed: runnable as far as the index knows.
index.projects[0].versions[0].api_level = None;
let p = index.project("coords_hud").unwrap();
let v = index.installable(p, "26.3", 0).unwrap();
assert_eq!(v.version, "0.2.0");
}
#[test]
fn shader_packs_pass_the_game_s_own_check() {
let manifest = "schema = 1\nid = \"glow\"\nname = \"Glow\"\nversion = \"1.0.0\"\napi = \"0.1\"\n\
+1
View File
@@ -5,6 +5,7 @@
//!
//! Nothing here runs unless a mod is installed and turned on: an empty
//! [`Mods`] makes no engine and every hook returns at once.
pub mod api_level;
pub mod content;
#[cfg(feature = "index")]
pub mod index;
+6
View File
@@ -424,6 +424,12 @@ impl Mods {
.map(|(name, _)| name.to_string())
.collect();
m.check_imports(imports.iter().map(String::as_str))?;
// A function this Horizon doesn't have: say so, not wasmtime's linking error.
let functions = crate::api_level::imports(wasm)?;
let (level, function) = crate::api_level::needed(functions.iter().map(String::as_str));
if let Some(why) = crate::api_level::too_new(level, function) {
return Err(why);
}
let data_dir = path
.parent()
.unwrap_or(Path::new("."))
@@ -302,3 +302,38 @@ fn every_capability_with_an_interface_is_in_the_contract() {
}
}
}
/// Every function the game offers (an interface some world imports and none
/// exports) has an API level, and the levels list nothing else.
#[test]
fn every_function_has_an_api_level() {
let mut resolve = Resolve::default();
let package = resolve.push_file(root().join(WIT)).unwrap();
let package = &resolve.packages[package];
let exported: Vec<_> = package
.worlds
.values()
.flat_map(|&w| resolve.worlds[w].exports.values())
.filter_map(|item| match item {
WorldItem::Interface { id, .. } => Some(*id),
_ => None,
})
.collect();
let mut offered = Vec::new();
for (name, &id) in &package.interfaces {
if !exported.contains(&id) {
for function in resolve.interfaces[id].functions.keys() {
offered.push(format!("{name}.{function}"));
}
}
}
for f in &offered {
assert!(
horizon_mods::api_level::level_of(f).is_some(),
"{f} has no level in crates/horizon-mods/src/api_level.rs: add it at the next level"
);
}
for (f, _) in horizon_mods::api_level::FUNCTIONS {
assert!(offered.iter().any(|o| o == f), "{f} isn't in the WIT");
}
}
@@ -29,6 +29,7 @@
"hud",
"chat"
],
"api_level": 0,
"game": "26.3",
"file": {
"name": "coords_hud-0.1.0.hmod",
+59
View File
@@ -197,6 +197,65 @@ fn undeclared_capabilities_are_refused() {
assert_eq!(mods.entries()[0].status, refused);
}
/// A mod calling a function this Horizon doesn't have is refused with
/// "update Horizon", not a linking error; the test mods' levels are read
/// from the functions they import.
#[test]
fn a_mod_for_a_newer_horizon_is_refused() {
use horizon_mods::api_level::{LEVEL, imports, needed};
use wit_parser::{ManglingAndAbi, Resolve};
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
let wit = std::fs::read_to_string(root.join("crates/horizon-mod-sdk/wit/horizon.wit"))
.unwrap()
.replace(
" set-minimap: func(minimap: option<minimap>);",
" set-minimap: func(minimap: option<minimap>);\n set-sky: func(on: bool);",
);
let mut resolve = Resolve::default();
let package = resolve.push_str("horizon.wit", &wit).unwrap();
let world = resolve
.select_world(&[package], Some("horizon-ui-mod"))
.unwrap();
let mut module = wit_component::dummy_module(&resolve, world, ManglingAndAbi::Standard32);
wit_component::embed_component_metadata(
&mut module,
&resolve,
world,
wit_component::StringEncoding::UTF8,
)
.unwrap();
let wasm = wit_component::ComponentEncoder::default()
.module(&module)
.unwrap()
.encode()
.unwrap();
let functions = imports(&wasm).unwrap();
assert!(functions.iter().any(|f| f == "render.set-sky"));
let dir = mods_dir().join("too_new");
std::fs::create_dir_all(&dir).unwrap();
let manifest = r#"
id = "too_new"
name = "Too New"
version = "0.1.0"
api = "0.1"
capabilities = ["game-read", "chat", "hud", "storage", "command", "ui", "render", "input"]
experimental = ["ui", "render", "input"]
"#;
std::fs::write(dir.join("horizon-mod.toml"), manifest).unwrap();
std::fs::write(dir.join("mod.wasm"), wasm).unwrap();
let mods = Mods::load(&[dir], |_| true, |_| false);
let refused = "made for a newer Horizon (it uses render.set-sky): update Horizon to run it";
assert_eq!(mods.entries()[0].status, Status::Failed(refused.into()));
let level = |file: &str| {
let wasm = Package::read(&mods_dir().join(file)).unwrap().wasm.unwrap();
needed(imports(&wasm).unwrap().iter().map(String::as_str)).0
};
assert_eq!(level("coords_hud.hmod"), 0);
// The UI guest shows a minimap (`render.set-minimap`).
assert_eq!(level("horizon_mods_test_ui_guest.hmod"), LEVEL);
}
#[test]
fn vanilla_commands_win() {
let mods = Mods::load(
+3 -3
View File
@@ -828,9 +828,9 @@ pub fn carry(
// Additions to other sets and pools, as files the game merges.
let modifier = serde_json::from_slice::<Value>(&bytes).ok();
let types = &mut report.worldgen_types;
if let Some((kind, mut added)) = modifier.and_then(|v| {
crate::legacy_structure_mods::lithostitched::modifier(&v, loaded, types)
}) {
if let Some((kind, mut added)) =
modifier.and_then(|v| crate::lithostitched::modifier(&v, loaded, types))
{
crate::processors::carry(
kind,
&mut added,
@@ -157,6 +157,7 @@ mod tests {
}
#[test]
#[ignore = "modular compat is crate::lithostitched's now (its tests)"]
fn modular_compat_runs_its_list_only_with_its_mod() {
let mut data = ProcessorData::default();
let swap = json!({"processor_type": "minecraft:rule", "rules": []});
@@ -1,503 +0,0 @@
//! Lithostitched: its jigsaw structure, its worldgen modifiers and its
//! processors.
use super::ctov;
use crate::processors::{ProcessorData, carry_all, processors, referenced};
use crate::worldgen::{ENHANCED_TERRAIN, Upgrade, start_checks};
use serde_json::{Value, json};
use std::collections::BTreeMap;
/// A `structure` file of Lithostitched's jigsaw type as vanilla's, with the
/// start rules it shares with the other libraries' jigsaws (`worldgen`'s
/// `start_checks`). `None` for any other file.
pub(crate) fn upgrade(
kind: &str,
v: &mut Value,
lost: &mut BTreeMap<String, usize>,
) -> Option<bool> {
if kind != "structure" || v["type"] != "lithostitched:jigsaw" {
return None;
}
start_checks(v, "lowest", lost, false);
if v.as_object_mut()
.and_then(|o| o.remove(ENHANCED_TERRAIN))
.is_some()
{
*lost.entry(ENHANCED_TERRAIN.into()).or_default() += 1;
}
v["type"] = "minecraft:jigsaw".into();
Some(true)
}
/// A lithostitched worldgen modifier
/// (`data/<ns>/lithostitched/worldgen_modifier/`) that adds to structure
/// sets or template pools, as a file the game merges into them (the key
/// `horizon:add_to`, `horizon_client::mod_content`): (kind, file). The
/// modifier's conditions are checked against `loaded`, the mods ported
/// along: a compat addition for a mod that is not would place that mod's
/// blocks, which the game does not have. Other modifier types are left out.
/// Both are counted in `report`.
pub(crate) fn modifier(
v: &Value,
loaded: &dyn Fn(&str) -> bool,
report: &mut Upgrade,
) -> Option<(&'static str, Value)> {
let t = v["type"].as_str().unwrap_or_default();
let mut left = |why: &str| *report.modifiers_left.entry(why.to_owned()).or_default() += 1;
let (kind, targets, from, to) = match t {
"lithostitched:add_structure_set_entries" => {
("structure_set", "structure_sets", "entries", "structures")
}
"lithostitched:add_template_pool_elements" => {
("template_pool", "template_pools", "elements", "elements")
}
_ => {
left(t);
return None;
}
};
if !conditions_hold(v, loaded) {
left("conditions");
return None;
}
let targets: Option<Vec<&str>> = match &v[targets] {
Value::String(s) => Some(vec![s.as_str()]),
Value::Array(a) => a.iter().map(Value::as_str).collect(),
_ => None,
};
let Some(targets) = targets.filter(|t| !t.is_empty() && t.iter().all(|t| !t.starts_with('#')))
else {
left("target");
return None;
};
let entries = v[from].as_array().cloned().unwrap_or_default();
for target in &targets {
*report.added_to.entry((*target).to_owned()).or_default() += entries.len();
}
Some((
kind,
serde_json::json!({"horizon:add_to": targets, to: entries}),
))
}
/// Whether a modifier's load conditions hold: lithostitched's `predicate`,
/// and the loader's conditions (NeoForge's, else Fabric's). A condition
/// type not known here does not hold.
fn conditions_hold(v: &Value, loaded: &dyn Fn(&str) -> bool) -> bool {
let all = |list: &Value, test: &dyn Fn(&Value) -> bool| {
list.as_array().is_some_and(|a| a.iter().all(test))
};
fn predicate(p: &Value, loaded: &dyn Fn(&str) -> bool) -> bool {
let test = |q: &Value| predicate(q, loaded);
let list = &p["predicates"];
match p["type"]
.as_str()
.map(|t| t.trim_start_matches("lithostitched:"))
{
Some("true") => true,
Some("mod_loaded") => p["mod_id"].as_str().is_some_and(loaded),
Some("not") => !predicate(&p["predicate"], loaded),
Some("all_of") => list.as_array().is_some_and(|a| a.iter().all(test)),
Some("any_of") => list.as_array().is_some_and(|a| a.iter().any(test)),
_ => false,
}
}
fn neoforge(c: &Value, loaded: &dyn Fn(&str) -> bool) -> bool {
let test = |q: &Value| neoforge(q, loaded);
match c["type"].as_str() {
Some("neoforge:true") => true,
Some("neoforge:mod_loaded") => c["modid"].as_str().is_some_and(loaded),
Some("neoforge:not") => !neoforge(&c["value"], loaded),
Some("neoforge:and") => c["values"].as_array().is_some_and(|a| a.iter().all(test)),
Some("neoforge:or") => c["values"].as_array().is_some_and(|a| a.iter().any(test)),
_ => false,
}
}
fn fabric(c: &Value, loaded: &dyn Fn(&str) -> bool) -> bool {
let test = |q: &Value| fabric(q, loaded);
let mods = || {
c["values"]
.as_array()
.into_iter()
.flatten()
.map(Value::as_str)
};
match c["condition"].as_str() {
Some("fabric:true") => true,
Some("fabric:all_mods_loaded") => mods().all(|m| m.is_some_and(loaded)),
Some("fabric:any_mods_loaded") => mods().any(|m| m.is_some_and(loaded)),
Some("fabric:not") => !fabric(&c["value"], loaded),
Some("fabric:and") => c["values"].as_array().is_some_and(|a| a.iter().all(test)),
Some("fabric:or") => c["values"].as_array().is_some_and(|a| a.iter().any(test)),
_ => false,
}
}
let litho = v.get("predicate").is_none_or(|p| predicate(p, loaded));
let loader = match (
v.get("neoforge:conditions"),
v.get("fabric:load_conditions"),
) {
(Some(c), _) => all(c, &|c| neoforge(c, loaded)),
// One condition or a list of them.
(None, Some(c)) if c.is_array() => all(c, &|c| fabric(c, loaded)),
(None, Some(c)) => fabric(c, loaded),
(None, None) => true,
};
litho && loader
}
/// The data form of one of Lithostitched's processors: the processors to put
/// in its place, or `None` when it has none (or is not Lithostitched's).
pub(crate) fn processor(
kind: &str,
p: &Value,
data: &ProcessorData,
carried: &mut BTreeMap<String, usize>,
) -> Option<Vec<Value>> {
match kind {
"lithostitched:apply_random" => apply_random(p, data, carried).map(|p| vec![p]),
"lithostitched:condition" => condition(p, data, carried).map(|p| vec![p]),
"lithostitched:block_swap" => {
// The same map; the game's swap keeps the properties both have.
p["blocks"].as_object()?;
Some(vec![
json!({"processor_type": "horizon:block_swap", "blocks": p["blocks"]}),
])
}
"lithostitched:set_block" => {
// A simple or weighted state provider, the block entity left as
// it is.
let provider = p["state_provider"]["type"].as_str()?;
let modifier = p["block_entity_modifier"]["type"].as_str();
let plain =
modifier.is_none_or(|m| m.trim_start_matches("minecraft:") == "passthrough");
if !plain
|| !matches!(
provider,
"minecraft:simple_state_provider" | "minecraft:weighted_state_provider"
)
{
return None;
}
let mut out = p.clone();
out["processor_type"] = "horizon:set_block".into();
if let Some(o) = out.as_object_mut() {
o.remove("block_entity_modifier");
}
Some(vec![out])
}
_ => None,
}
}
/// The references (Lithostitched's, and CTOV's compat processors) in a
/// processor list written in; whether any was.
pub(crate) fn expand_references(
list: Vec<Value>,
data: &ProcessorData,
carried: &mut BTreeMap<String, usize>,
) -> (Vec<Value>, bool) {
references(list, data, carried, 0)
}
/// A processor list inside a carried processor: an id stays (the game reads
/// that file, carried on its own); an inline one is carried here.
fn nested(list: &Value, data: &ProcessorData, carried: &mut BTreeMap<String, usize>) -> Value {
let mut list = list.clone();
if let Some(ps) = processors(&mut list) {
*ps = carry_all(std::mem::take(ps), data, carried).0;
}
list
}
/// Lithostitched's `apply_random` as `horizon:apply_random`: its weighted
/// lists, or its set of lists at weight 1. A tag is not carried.
fn apply_random(
p: &Value,
data: &ProcessorData,
carried: &mut BTreeMap<String, usize>,
) -> Option<Value> {
let lists = &p["processor_lists"];
let weighted = lists.as_array().is_some_and(|a| {
a.iter()
.all(|e| e.get("weight").is_some() && e.get("data").is_some())
});
let entries: Vec<(Value, Value)> = if weighted {
lists
.as_array()?
.iter()
.map(|e| (e["weight"].clone(), e["data"].clone()))
.collect()
} else {
match lists {
Value::String(id) if !id.starts_with('#') => vec![(1.into(), lists.clone())],
Value::Array(a) if a.iter().any(|x| x.get("processor_type").is_some()) => {
vec![(1.into(), lists.clone())]
}
Value::Array(a)
if a.iter()
.all(|x| x.as_str().is_some_and(|s| !s.starts_with('#')) || x.is_object()) =>
{
a.iter().map(|x| (1.into(), x.clone())).collect()
}
Value::Object(_) => vec![(1.into(), lists.clone())],
_ => return None,
}
};
let lists: Vec<Value> = entries
.into_iter()
.map(|(weight, list)| json!({"weight": weight, "data": nested(&list, data, carried)}))
.collect();
let mut out = json!({"processor_type": "horizon:apply_random", "processor_lists": lists});
if let Some(mode) = p.get("mode") {
out["mode"] = mode.clone();
}
Some(out)
}
/// Lithostitched's `condition` as `horizon:condition`, when the game runs
/// every condition in it (true, all_of, any_of, not, random_chance,
/// matching_blocks with plain property values); else it stays Java.
fn condition(
p: &Value,
data: &ProcessorData,
carried: &mut BTreeMap<String, usize>,
) -> Option<Value> {
fn runs(c: &Value) -> bool {
if let Some(all) = c.as_array() {
return all.iter().all(runs);
}
let kind = c["type"].as_str().unwrap_or_default();
match kind.strip_prefix("lithostitched:").unwrap_or(kind) {
"true" | "random_chance" => true,
"all_of" | "any_of" => c["conditions"]
.as_array()
.is_some_and(|a| a.iter().all(runs)),
"not" => runs(&c["condition"]),
"matching_blocks" => c.get("properties").is_none_or(|p| {
p.as_object()
.is_some_and(|o| o.values().all(Value::is_string))
}),
_ => false,
}
}
if !runs(&p["if_true"]) {
return None;
}
// One processor or a list of them.
let mut branch = |key: &str| -> Value {
let list = match p.get(key) {
None => Vec::new(),
Some(Value::Array(a)) => a.clone(),
Some(one) => vec![one.clone()],
};
carry_all(list, data, carried).0.into()
};
let (then, otherwise) = (branch("then"), branch("else"));
let mut out = json!({"processor_type": "horizon:condition", "if_true": p["if_true"],
"then": then, "else": otherwise});
if let Some(mode) = p.get("random_mode") {
out["random_mode"] = mode.clone();
}
Some(out)
}
/// Lithostitched's references in a list, each replaced by the processors of
/// the lists it names, in order (references in them too, to a depth of 8).
/// One naming a tag or a list the mod does not have stays, to be dropped.
/// Whether any was replaced.
fn references(
list: Vec<Value>,
data: &ProcessorData,
carried: &mut BTreeMap<String, usize>,
depth: usize,
) -> (Vec<Value>, bool) {
let mut out = Vec::with_capacity(list.len());
let mut changed = false;
for p in list {
if let Some(inner) = ctov::modular_compat(&p, data, carried, depth) {
changed = true;
out.extend(references(inner, data, carried, depth + 1).0);
continue;
}
let named = (p["processor_type"] == "lithostitched:reference" && depth < 8)
.then(|| referenced(&p["processor_lists"], data))
.flatten();
match named {
Some(inner) => {
*carried.entry("lithostitched:reference".into()).or_default() += 1;
out.extend(references(inner, data, carried, depth + 1).0);
changed = true;
}
None => out.push(p),
}
}
(out, changed)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::legacy_structure_mods::data;
use crate::processors::carry;
#[test]
fn modifiers_add_to_sets_and_pools() {
let mut report = Upgrade::default();
let only_fd = |m: &str| m == "farmersdelight";
let set = json!({"type": "lithostitched:add_structure_set_entries",
"structure_sets": "minecraft:villages",
"entries": [{"structure": "x:village", "weight": 4}]});
let (kind, added) = modifier(&set, &only_fd, &mut report).unwrap();
assert_eq!(kind, "structure_set");
assert_eq!(
added,
json!({"horizon:add_to": ["minecraft:villages"],
"structures": [{"structure": "x:village", "weight": 4}]})
);
// A compat addition holds only when its mod is ported along.
let pool = |modid: &str| {
json!({"type": "lithostitched:add_template_pool_elements",
"neoforge:conditions": [{"type": "neoforge:mod_loaded", "modid": modid}],
"fabric:load_conditions": {"condition": "fabric:all_mods_loaded", "values": ["other"]},
"template_pools": ["x:houses", "y:houses"],
"elements": [{"weight": 5, "element": {"element_type": "minecraft:single_pool_element",
"location": "x:fd_farm", "projection": "rigid", "processors": "minecraft:empty"}}]})
};
let (kind, added) = modifier(&pool("farmersdelight"), &only_fd, &mut report).unwrap();
assert_eq!(kind, "template_pool");
assert_eq!(added["horizon:add_to"], json!(["x:houses", "y:houses"]));
assert_eq!(added["elements"][0]["weight"], 5);
assert!(modifier(&pool("create"), &only_fd, &mut report).is_none());
// Fabric's conditions when there are no NeoForge ones; lithostitched's own.
let fabric = json!({"type": "lithostitched:add_structure_set_entries",
"fabric:load_conditions": [{"condition": "fabric:not",
"value": {"condition": "fabric:any_mods_loaded", "values": ["create"]}}],
"predicate": {"type": "lithostitched:all_of", "predicates": [
{"type": "lithostitched:mod_loaded", "mod_id": "farmersdelight"},
{"type": "lithostitched:true"}]},
"structure_sets": "minecraft:villages", "entries": []});
assert!(modifier(&fabric, &only_fd, &mut report).is_some());
let tagged = json!({"type": "lithostitched:add_structure_set_entries",
"structure_sets": "#x:sets", "entries": []});
assert!(modifier(&tagged, &only_fd, &mut report).is_none());
let features = json!({"type": "lithostitched:add_features"});
assert!(modifier(&features, &only_fd, &mut report).is_none());
assert_eq!(
report.added_to,
BTreeMap::from([
("minecraft:villages".into(), 1),
("x:houses".into(), 1),
("y:houses".into(), 1),
])
);
assert_eq!(
report.modifiers_left,
BTreeMap::from([
("conditions".into(), 1),
("lithostitched:add_features".into(), 1),
("target".into(), 1),
])
);
}
#[test]
fn lithostitched_block_swaps_become_the_games() {
let mut list = json!({"processors": [
{"processor_type": "lithostitched:block_swap",
"blocks": {"minecraft:cobblestone": "minecraft:mossy_cobblestone"}},
{"processor_type": "lithostitched:block_swap", "blocks": []}]});
let mut carried = BTreeMap::new();
assert!(carry("processor_list", &mut list, &data(), &mut carried));
let p = list["processors"].as_array().unwrap();
assert_eq!(
p[0],
json!({"processor_type": "horizon:block_swap",
"blocks": {"minecraft:cobblestone": "minecraft:mossy_cobblestone"}})
);
// A map that is not one stays Java (dropped later).
assert_eq!(p[1]["processor_type"], "lithostitched:block_swap");
}
#[test]
fn lithostitched_references_are_written_in() {
let mut data = data();
let gravity = json!({"processor_type": "minecraft:gravity", "heightmap": "WORLD_SURFACE_WG", "offset": 0});
data.lists
.insert("v:road".into(), json!({"processors": [gravity.clone()]}));
data.lists.insert(
"v:house".into(),
json!({"processors": [
{"processor_type": "lithostitched:reference", "processor_lists": "v:road"},
{"processor_type": "lithostitched:block_swap", "blocks": {"minecraft:dirt": "minecraft:grass_block"}}]}),
);
let mut list = json!({"processors": [
{"processor_type": "lithostitched:reference", "processor_lists": ["v:house", "v:road"]},
{"processor_type": "lithostitched:reference", "processor_lists": "#v:roads"},
{"processor_type": "lithostitched:reference", "processor_lists": ["v:nowhere"]}]});
let mut carried = BTreeMap::new();
assert!(carry("processor_list", &mut list, &data, &mut carried));
let p = list["processors"].as_array().unwrap();
// House (its own reference to road first, then its swap), then road;
// the swap inside is carried too.
assert_eq!(p[0], gravity);
assert_eq!(p[1]["processor_type"], "horizon:block_swap");
assert_eq!(p[2], gravity);
// A tag or a list the mod lacks stays, to be dropped.
assert_eq!(p[3]["processor_lists"], "#v:roads");
assert_eq!(p[4]["processor_lists"], json!(["v:nowhere"]));
assert_eq!(carried["lithostitched:reference"], 2);
}
#[test]
fn lithostitched_random_and_condition_become_the_games() {
let mut data = data();
let swap = json!({"processor_type": "lithostitched:block_swap",
"blocks": {"minecraft:dirt": "minecraft:grass_block"}});
data.lists
.insert("v:green".into(), json!({"processors": [swap.clone()]}));
let mut list = json!({"processors": [
{"processor_type": "lithostitched:apply_random", "mode": "per_piece", "processor_lists": [
{"weight": 3, "data": "minecraft:empty"},
{"weight": 1, "data": {"processors": [
{"processor_type": "lithostitched:reference", "processor_lists": "v:green"},
{"processor_type": "v:java"}]}}]},
{"processor_type": "lithostitched:apply_random", "mode": "per_block",
"processor_lists": ["v:green", "v:red"]},
{"processor_type": "lithostitched:apply_random", "mode": "per_block",
"processor_lists": "#v:colours"},
{"processor_type": "lithostitched:condition",
"if_true": [{"type": "lithostitched:matching_blocks", "blocks": "minecraft:cobblestone"}],
"then": swap, "else": {"processor_type": "lithostitched:set_block", "state_provider":
{"type": "minecraft:simple_state_provider", "state": {"Name": "minecraft:stone"}}}},
{"processor_type": "lithostitched:condition",
"if_true": {"type": "lithostitched:matching_biomes", "biomes": "#c:is_cold"},
"then": []}]});
let mut carried = BTreeMap::new();
assert!(carry("processor_list", &mut list, &data, &mut carried));
let p = list["processors"].as_array().unwrap();
let green = json!({"processor_type": "horizon:block_swap",
"blocks": {"minecraft:dirt": "minecraft:grass_block"}});
// Inline lists are carried; Java left in them is dropped later.
assert_eq!(
p[0],
json!({"processor_type": "horizon:apply_random", "mode": "per_piece", "processor_lists": [
{"weight": 3, "data": "minecraft:empty"},
{"weight": 1, "data": {"processors": [green.clone(), {"processor_type": "v:java"}]}}]})
);
assert_eq!(
p[1]["processor_lists"],
json!([{"weight": 1, "data": "v:green"}, {"weight": 1, "data": "v:red"}])
);
assert_eq!(p[2]["processor_type"], "lithostitched:apply_random");
assert_eq!(
p[3],
json!({"processor_type": "horizon:condition",
"if_true": [{"type": "lithostitched:matching_blocks", "blocks": "minecraft:cobblestone"}],
"then": [green], "else": [{"processor_type": "horizon:set_block", "state_provider":
{"type": "minecraft:simple_state_provider", "state": {"Name": "minecraft:stone"}}}]})
);
assert_eq!(p[4]["processor_type"], "lithostitched:condition");
assert_eq!(carried["lithostitched:apply_random"], 2);
assert_eq!(carried["lithostitched:condition"], 1);
}
}
@@ -13,10 +13,11 @@
//! The general code reaches them only through the `pub(crate)` entry points
//! of these three modules. Nothing in them is used by another mod's
//! conversion.
#[allow(dead_code)] // its modular compat is `crate::lithostitched`'s now; the rest goes with C1
pub(crate) mod ctov;
pub(crate) mod lithostitched;
#[cfg(test)]
#[allow(dead_code)] // the old Lithostitched tests used it
fn data() -> crate::processors::ProcessorData {
let mut d = crate::processors::ProcessorData::default();
d.spawners.insert(
+1
View File
@@ -14,6 +14,7 @@ pub mod import;
pub mod inventory;
pub mod jar;
pub(crate) mod legacy_structure_mods;
mod lithostitched;
pub mod modrinth;
pub mod oracle;
pub mod pipeline;
@@ -0,0 +1,434 @@
//! Lithostitched's worldgen, carried as the game reads it (clean room:
//! `cleanroom/structures/spec-lithostitched.md`; CTOV's processor:
//! `spec-ctov.md` §4).
//!
//! - `lithostitched:jigsaw` structures become vanilla's jigsaw. Lithostitched
//! lays jigsaws out its own way (OPEN-L5): Horizon keeps vanilla's layouts.
//! - Its processors (`block_swap`, `reference`, `apply_random`, `condition`,
//! `set_block`, `discard_input`) are the game's own types
//! (`horizon_world`'s `structure::processor`): they are carried as written,
//! the processors nested in them carried in turn. CTOV's
//! `ctov:modular_compat` (a reference that runs only when its mod is loaded)
//! becomes a `reference` when the mod is ported along, else nothing.
//! - `add_template_pool_elements` modifiers become `horizon:add_to` pool files
//! (`docs/mods.md`), when their load conditions pass. Lithostitched's own
//! pool elements (`limited`, `guaranteed`, `delegating`) place their
//! `delegate`; what they add to it is counted as lost.
use crate::{processors::ProcessorData, worldgen::Upgrade};
use serde_json::{Value, json};
use std::collections::BTreeMap;
const JIGSAW: &str = "lithostitched:jigsaw";
/// Vanilla's JigsawStructure.MAX_DEPTH.
const MAX_SIZE: i64 = 20;
const MODULAR_COMPAT: &str = "ctov:modular_compat";
/// The processor types the game reads.
const PROCESSORS: [&str; 6] = [
"lithostitched:block_swap",
"lithostitched:reference",
"lithostitched:apply_random",
"lithostitched:condition",
"lithostitched:set_block",
"lithostitched:discard_input",
];
fn count(map: &mut BTreeMap<String, usize>, name: &str) {
*map.entry(name.to_owned()).or_default() += 1;
}
/// A `lithostitched:jigsaw` structure as vanilla's jigsaw: `Some(true)`, else
/// `None`.
pub(crate) fn upgrade(
kind: &str,
v: &mut Value,
lost: &mut BTreeMap<String, usize>,
) -> Option<bool> {
if kind != "structure" || v["type"] != JIGSAW {
return None;
}
v["type"] = "minecraft:jigsaw".into();
// `size` may be an int provider (sampled per start): its largest value.
let size = match &v["size"] {
Value::Null => None,
Value::Number(n) => n.as_i64(),
provider => {
count(lost, "lithostitched:jigsaw size provider");
["max_inclusive", "value"]
.iter()
.find_map(|k| provider[k].as_i64())
}
};
if let Some(size) = size {
if size > MAX_SIZE {
count(lost, "lithostitched:jigsaw size over 20");
}
v["size"] = size.clamp(0, MAX_SIZE).into();
}
let o = v.as_object_mut()?;
match o.remove("start_projection") {
Some(Value::String(heightmap)) if heightmap != "floor" && heightmap != "ceiling" => {
o.entry("project_start_to_heightmap")
.or_insert(heightmap.into());
}
Some(_) => count(lost, "lithostitched:jigsaw start_projection"),
None => {}
}
if o.remove("fixed_rotation").is_some_and(|f| f == true) {
count(lost, "lithostitched:jigsaw fixed_rotation");
}
Some(true)
}
/// Lithostitched's pool elements as the element they wrap; whether `element`
/// was one.
pub(crate) fn upgrade_pool_element(
element: &mut Value,
lost: &mut BTreeMap<String, usize>,
) -> bool {
let Some(kind) = element["element_type"].as_str() else {
return false;
};
match kind {
"lithostitched:limited" | "lithostitched:guaranteed" | "lithostitched:delegating" => {
count(lost, kind);
let delegate = element["delegate"].take();
*element = delegate;
// A delegate wrapping another (only `delegating` allows it).
upgrade_pool_element(element, lost);
true
}
_ => false,
}
}
/// The ids a modifier targets: an id or a list of ids (`None` for tags and
/// Lithostitched's object forms).
fn ids(v: &Value) -> Option<Vec<String>> {
let id = |v: &Value| {
v.as_str()
.filter(|s| !s.starts_with('#'))
.map(str::to_owned)
};
match v {
Value::Array(a) => a.iter().map(id).collect(),
other => Some(vec![id(other)?]),
}
}
/// NeoForge's load conditions (`neoforge:conditions`): all must pass.
fn conditions_pass(v: &Value, loaded: &dyn Fn(&str) -> bool) -> bool {
fn pass(c: &Value, loaded: &dyn Fn(&str) -> bool) -> bool {
let all = |key: &str| {
c[key]
.as_array()
.into_iter()
.flatten()
.all(|c| pass(c, loaded))
};
match c["type"].as_str() {
Some("neoforge:mod_loaded") => c["modid"].as_str().is_some_and(loaded),
Some("neoforge:true") => true,
Some("neoforge:not") => !pass(&c["value"], loaded),
Some("neoforge:and") => all("values"),
Some("neoforge:or") => {
(c["values"].as_array().into_iter().flatten()).any(|c| pass(c, loaded))
}
_ => false,
}
}
(v["neoforge:conditions"].as_array().into_iter().flatten()).all(|c| pass(c, loaded))
}
/// A worldgen modifier as a file the game merges: `("template_pool", file)`
/// for `add_template_pool_elements`; the others are counted in
/// `report.modifiers_left`.
pub(crate) fn modifier(
v: &Value,
loaded: &dyn Fn(&str) -> bool,
report: &mut Upgrade,
) -> Option<(&'static str, Value)> {
let kind = v["type"].as_str().unwrap_or("no type");
if !conditions_pass(v, loaded) {
count(&mut report.modifiers_left, "conditions");
return None;
}
if kind != "lithostitched:add_template_pool_elements" {
count(&mut report.modifiers_left, kind);
return None;
}
let (Some(targets), Some(elements)) = (ids(&v["template_pools"]), v["elements"].as_array())
else {
count(&mut report.modifiers_left, "target");
return None;
};
for target in &targets {
*report.added_to.entry(target.clone()).or_default() += elements.len();
}
Some((
"template_pool",
json!({"horizon:add_to": targets, "elements": elements}),
))
}
/// Whether the game reads a Lithostitched processor as it is: one of its
/// types, with no processor list named by tag (the game has no processor
/// list tags) and a state provider it runs.
pub(crate) fn game_reads(p: &Value) -> bool {
let Some(kind) = p["processor_type"]
.as_str()
.filter(|t| PROCESSORS.contains(t))
else {
return false;
};
let tag = |v: &Value| v.as_str().is_some_and(|s| s.starts_with('#'));
match kind {
"lithostitched:reference" => match &p["processor_lists"] {
Value::Array(a) => !a.iter().any(tag),
other => other.is_string() && !tag(other),
},
"lithostitched:apply_random" => p["processor_lists"]
.as_array()
.is_some_and(|a| a.iter().all(|e| !tag(e) && !tag(&e["data"]))),
"lithostitched:set_block" => matches!(
p["state_provider"]["type"].as_str(),
Some("minecraft:simple_state_provider" | "minecraft:weighted_state_provider")
),
"lithostitched:block_swap" => p["blocks"].is_object(),
"lithostitched:condition" => !p["if_true"].is_null(),
_ => true,
}
}
/// The processor lists inside a Lithostitched processor, for the general
/// code that walks lists (`then`, `else`, `apply_random`'s inline data).
pub(crate) fn inner_lists(p: &mut Value) -> Vec<&mut Value> {
match p["processor_type"].as_str() {
Some("lithostitched:apply_random") => (p["processor_lists"].as_array_mut().into_iter())
.flatten()
.filter_map(|e| e.get_mut("data").filter(|d| !d.is_string()))
.collect(),
Some("lithostitched:condition") => (p.as_object_mut().into_iter())
.flat_map(|o| o.iter_mut())
.filter(|(k, _)| *k == "then" || *k == "else")
.map(|(_, v)| v)
.collect(),
_ => Vec::new(),
}
}
/// A Lithostitched processor with the processors nested in it carried
/// (`Some`, when one of them changed); `None` for any other processor.
pub(crate) fn processor(
kind: &str,
p: &Value,
data: &ProcessorData,
carried: &mut BTreeMap<String, usize>,
) -> Option<Vec<Value>> {
if !PROCESSORS.contains(&kind) {
return None;
}
let mut out = p.clone();
let mut changed = false;
for list in inner_lists(&mut out) {
// One processor alone is a list of one.
if list.get("processor_type").is_some() {
*list = Value::Array(vec![list.take()]);
changed = true;
}
if let Some(processors) = crate::processors::processors(list) {
let (inner, inner_changed) =
crate::processors::carry_all(std::mem::take(processors), data, carried);
*processors = inner;
changed |= inner_changed;
}
}
changed.then(|| vec![out])
}
/// The list with CTOV's modular compat processors written as the game's
/// `reference` (its mod is ported along) or taken out; whether any was.
pub(crate) fn expand_references(
list: Vec<Value>,
data: &ProcessorData,
carried: &mut BTreeMap<String, usize>,
) -> (Vec<Value>, bool) {
let mut changed = false;
let mut out = Vec::with_capacity(list.len());
for p in list {
if p["processor_type"] != MODULAR_COMPAT {
out.push(p);
continue;
}
changed = true;
count(carried, MODULAR_COMPAT);
let loaded = p["modid"]
.as_str()
.is_some_and(|m| data.compat_loaded.contains(m));
if loaded {
out.push(json!({"processor_type": "lithostitched:reference",
"processor_lists": p["reference"].clone()}));
}
}
(out, changed)
}
/// Notes which mods CTOV's modular compat processors name are `loaded`.
pub(crate) fn mark_loaded(data: &mut ProcessorData, loaded: &dyn Fn(&str) -> bool) {
fn visit(v: &Value, out: &mut Vec<String>) {
match v {
Value::Object(o) => {
if o.get("processor_type").and_then(Value::as_str) == Some(MODULAR_COMPAT)
&& let Some(m) = o.get("modid").and_then(Value::as_str)
{
out.push(m.to_owned());
}
o.values().for_each(|v| visit(v, out));
}
Value::Array(a) => a.iter().for_each(|v| visit(v, out)),
_ => {}
}
}
let mut named = Vec::new();
data.lists.values().for_each(|l| visit(l, &mut named));
data.compat_loaded
.extend(named.into_iter().filter(|m| loaded(m)));
}
#[cfg(test)]
mod tests {
use super::*;
use crate::worldgen::upgrade as upgrade_file;
#[test]
fn jigsaws_become_vanillas() {
let mut lost = BTreeMap::new();
let mut s = json!({"type": JIGSAW, "size": {"type": "uniform", "min_inclusive": 3,
"max_inclusive": 40}, "start_projection": "WORLD_SURFACE_WG", "fixed_rotation": true});
assert_eq!(upgrade("structure", &mut s, &mut lost), Some(true));
assert_eq!(
s,
json!({"type": "minecraft:jigsaw", "size": 20,
"project_start_to_heightmap": "WORLD_SURFACE_WG"})
);
assert_eq!(lost.len(), 3);
assert_eq!(
upgrade(
"structure",
&mut json!({"type": "minecraft:jigsaw"}),
&mut lost
),
None
);
}
#[test]
fn wrapped_elements_place_their_delegate() {
let mut lost = BTreeMap::new();
let delegate = json!({"element_type": "minecraft:single_pool_element", "location": "a:b",
"projection": "rigid", "processors": "minecraft:empty"});
let mut e =
json!({"element_type": "lithostitched:limited", "limit": 1, "delegate": delegate});
assert!(upgrade_pool_element(&mut e, &mut lost));
assert_eq!(e, delegate);
assert_eq!(lost["lithostitched:limited"], 1);
assert!(!upgrade_pool_element(&mut e, &mut lost));
}
#[test]
fn modifiers_add_to_pools_when_their_mods_are_loaded() {
let mut report = Upgrade::default();
let loaded = |m: &str| m == "waystones";
let add = |modid: &str, pools: Value| {
json!({"type": "lithostitched:add_template_pool_elements", "template_pools": pools,
"elements": [{"weight": 2, "element": {"element_type": "minecraft:empty_pool_element"}}],
"neoforge:conditions": [{"type": "neoforge:mod_loaded", "modid": modid}]})
};
let (kind, file) = modifier(
&add("waystones", json!("ctov:village/plains/house")),
&loaded,
&mut report,
)
.unwrap();
assert_eq!(kind, "template_pool");
assert_eq!(file["horizon:add_to"], json!(["ctov:village/plains/house"]));
assert_eq!(file["elements"][0]["weight"], 2);
assert_eq!(report.added_to["ctov:village/plains/house"], 1);
assert!(modifier(&add("sawmill", json!("a:b")), &loaded, &mut report).is_none());
assert!(modifier(&add("waystones", json!("#a:pools")), &loaded, &mut report).is_none());
let aliases = json!({"type": "lithostitched:set_pool_aliases"});
assert!(modifier(&aliases, &loaded, &mut report).is_none());
assert_eq!(
report.modifiers_left,
BTreeMap::from([
("conditions".into(), 1),
("lithostitched:set_pool_aliases".into(), 1),
("target".into(), 1),
])
);
}
/// A CTOV list (spec-ctov §4): references, a condition with a single
/// processor and a nested condition, a random list holding the compat
/// hook, and processors the game cannot read.
#[test]
fn processors_stay_lithostitcheds_with_their_insides_carried() {
let hook = json!({"processor_type": MODULAR_COMPAT, "modid": "natures_spirit",
"reference": "ctov:village/mesa/house_wisteria"});
let list = json!({"processors": [
{"processor_type": "lithostitched:reference", "processor_lists": ["ctov:village/mesa/house"]},
{"processor_type": "lithostitched:condition", "random_mode": "per_block",
"if_true": {"type": "lithostitched:matching_blocks", "blocks": "minecraft:water",
"match_type": "location"},
"then": {"processor_type": "lithostitched:set_block", "state_provider": {
"type": "minecraft:simple_state_provider", "state": {"Name": "minecraft:spruce_planks"}}},
"else": [{"processor_type": "somemod:java_processor"}]},
{"processor_type": "lithostitched:apply_random", "mode": "per_piece", "processor_lists": [
{"weight": 1, "data": "minecraft:empty"},
{"weight": 1, "data": {"processors": [hook]}}]},
{"processor_type": "lithostitched:reference", "processor_lists": "#ctov:palettes"},
{"processor_type": "lithostitched:set_block", "state_provider": {
"type": "minecraft:noise_provider"}}]});
for (loaded, wisteria) in [
(false, json!([])),
(
true,
json!([{"processor_type":
"lithostitched:reference", "processor_lists": "ctov:village/mesa/house_wisteria"}]),
),
] {
let mut data = ProcessorData::default();
data.lists
.insert("ctov:village/mesa/house".into(), list.clone());
mark_loaded(&mut data, &|m| loaded && m == "natures_spirit");
let mut v = list.clone();
let mut report = Upgrade::default();
crate::processors::carry(
"processor_list",
&mut v,
&data,
&mut report.processors_carried,
);
upgrade_file("processor_list", &mut v, &mut report, &BTreeMap::new());
let p = &v["processors"];
assert_eq!(p.as_array().unwrap().len(), 3, "{p}");
assert_eq!(p[0], list["processors"][0]);
// One processor is a list of one; the Java one is taken out.
assert_eq!(
p[1]["then"][0]["state_provider"]["state"]["id"],
"minecraft:spruce_planks"
);
assert_eq!(p[1]["else"], json!([]));
assert_eq!(p[2]["processor_lists"][0]["data"], "minecraft:empty");
assert_eq!(p[2]["processor_lists"][1]["data"]["processors"], wisteria);
assert_eq!(report.processors_carried[MODULAR_COMPAT], 1);
assert_eq!(
report.processors_dropped,
BTreeMap::from([
("lithostitched:reference".into(), 1),
("lithostitched:set_block".into(), 1),
("somemod:java_processor".into(), 1),
])
);
}
}
}
+5 -6
View File
@@ -10,13 +10,13 @@
//! - Repurposed Structures' mineshaft sky view becomes the game's
//! `horizon:mineshaft_sky_view`.
//!
//! The processors of YUNG's mods, Lithostitched and Choice Theorem's Overhauled
//! Village are carried by `legacy_structure_mods`, called from [`carry`].
//! Lithostitched's processors and Choice Theorem's Overhauled Village's
//! compat hook are carried by `crate::lithostitched`, called from [`carry`].
//!
//! What is not here is taken out of its list later and counted
//! (`worldgen::upgrade`). The entity NBT is written in 26.3's shape (an
//! `equipment` map, not 1.21.1's `HandItems` and `ArmorItems`).
use crate::legacy_structure_mods::{ctov, lithostitched};
use crate::lithostitched;
use serde_json::{Value, json};
use std::{
collections::{BTreeMap, BTreeSet},
@@ -42,7 +42,7 @@ pub struct ProcessorData {
impl ProcessorData {
/// Notes which mods its compat processors name are `loaded`.
pub fn mark_loaded(&mut self, loaded: &dyn Fn(&str) -> bool) {
ctov::mark_loaded(self, loaded);
lithostitched::mark_loaded(self, loaded);
}
pub fn read<R: Read + Seek>(zip: &mut zip::ZipArchive<R>) -> Self {
@@ -177,8 +177,7 @@ fn carry_at(
let mut out = Vec::with_capacity(expanded.len());
for p in expanded {
let kind = p["processor_type"].as_str().unwrap_or_default().to_owned();
// Lithostitched's processors are carried apart from this code
// (`legacy_structure_mods`).
// Lithostitched's processors stay theirs, with what they hold carried.
let replacement = lithostitched::processor(&kind, &p, data, carried).or_else(|| {
match pillar_type(&kind) {
Some(to) => Some(vec![pillar(&p, to, data, carried, depth)]),
+12 -19
View File
@@ -15,10 +15,10 @@
//! closely.
//!
//! YUNG's API's files are carried as written ([`crate::yung`]).
//! Lithostitched's and CTOV's worldgen files are converted by
//! `legacy_structure_mods`, called from [`upgrade`], [`crate::carry`] and the
//! processors.
use crate::{legacy_structure_mods::lithostitched, yung};
//! Lithostitched's jigsaws, elements, modifiers and processors are carried by
//! [`crate::lithostitched`]; the entries CTOV adds to vanilla's structure sets
//! by `legacy_structure_mods`, called from [`crate::carry`].
use crate::{lithostitched, yung};
use serde_json::Value;
use std::{
collections::{BTreeMap, BTreeSet},
@@ -191,28 +191,19 @@ fn drop_java_processors(list: &mut Value, dropped: &mut BTreeMap<String, usize>)
let before = processors.len();
processors.retain(|p| {
let t = p["processor_type"].as_str().unwrap_or_default();
let java = !t.starts_with("minecraft:") && !t.starts_with("horizon:");
let java = !t.starts_with("minecraft:")
&& !t.starts_with("horizon:")
&& !lithostitched::game_reads(p);
if java {
*dropped.entry(t.to_owned()).or_default() += 1;
}
!java
});
let mut changed = processors.len() != before;
// The inline lists inside the game's apply_random and condition.
// The inline lists inside Lithostitched's apply_random and condition.
for p in processors.iter_mut() {
let inner: Vec<&mut Value> = match p["processor_type"].as_str() {
Some("horizon:apply_random") => p["processor_lists"]
.as_array_mut()
.into_iter()
.flatten()
.map(|e| &mut e["data"])
.collect(),
Some("horizon:condition") => {
let o = p.as_object_mut().into_iter().flat_map(|o| o.iter_mut());
o.filter(|(k, _)| *k == "then" || *k == "else")
.map(|(_, v)| v)
.collect()
}
Some(t) if t.starts_with("lithostitched:") => lithostitched::inner_lists(p),
Some("horizon:pillar" | "horizon:bottom_pillar") => {
p.get_mut("pillar_processor_list").into_iter().collect()
}
@@ -398,7 +389,9 @@ fn upgrade_types(
}
}
for element in elements {
if yung::upgrade_pool_element(element, lost) {
if yung::upgrade_pool_element(element, lost)
|| lithostitched::upgrade_pool_element(element, lost)
{
changed = true;
continue;
}
@@ -21,7 +21,7 @@ use horizon_core::{
use serde_json::Value;
use std::{collections::BTreeSet, sync::Arc};
mod lithostitched;
mod litho;
mod pillars;
mod sea_level;
@@ -238,6 +238,23 @@ impl Modifier {
)
}
}
impl Modifier {
/// RuleBlockEntityModifier.apply: the block entity the output gets.
fn apply(&self, nbt: Option<&Compound>, random: &mut impl ProcessorRandom) -> Option<Compound> {
match self {
Self::Passthrough => nbt.cloned(),
Self::Clear => Some(Compound::new()),
Self::AppendLoot(table) => {
let mut nbt = nbt.cloned().unwrap_or_default();
nbt.insert("LootTable".into(), Tag::String(table.as_str().into()));
nbt.insert("LootTableSeed".into(), Tag::Long(random.long()));
Some(nbt)
}
Self::AppendStatic(data) => Some(edited(nbt, &[], data)),
Self::Edit { remove, data } => Some(edited(nbt, remove, data)),
}
}
}
/// A block entity with `remove`'s keys taken out and `data` merged in
/// (append_static is the one with nothing to remove).
fn edited(nbt: Option<&Compound>, remove: &[String], data: &Compound) -> Compound {
@@ -292,17 +309,21 @@ pub enum Processor {
SpawnerMobs(Box<SpawnerMobs>),
Pick(Box<Pick>),
CountCap(Box<CountCap>),
/// `horizon:block_swap` (Lithostitched's `block_swap`): blocks by name
/// become others, keeping the properties both have.
/// `lithostitched:block_swap`: blocks by name become others, keeping
/// the properties both have (a block the game lacks is left alone).
BlockSwap(std::collections::BTreeMap<String, String>),
/// `horizon:apply_random` (Lithostitched's `apply_random`): a processor
/// list drawn by weight runs on the block.
ApplyRandom(Box<lithostitched::ApplyRandom>),
/// `horizon:condition` (Lithostitched's `condition`): `then` or `else`
/// `lithostitched:apply_random`: a processor list drawn by weight runs
/// on the block.
ApplyRandom(Box<litho::ApplyRandom>),
/// `lithostitched:reference`: the named lists run on the block in turn.
Reference(Vec<ProcessorList>),
/// `lithostitched:discard_input`: the world's block stays.
DiscardInput,
/// `lithostitched:condition`: `then` or `else`
/// runs on the block, by a test of it.
Condition(Box<lithostitched::Conditional>),
/// `horizon:set_block` (Lithostitched's `set_block`).
SetBlock(Box<lithostitched::SetBlock>),
Condition(Box<litho::Conditional>),
/// `lithostitched:set_block`: the block from a state provider.
SetBlock(Box<litho::SetBlock>),
/// `horizon:pillar` (Repurposed Structures' and Moog's pillars): a
/// trigger block starts a column down to the ground.
Pillar(Box<pillars::Pillar>),
@@ -786,7 +807,7 @@ impl Processor {
})?))
}
"horizon:pick" => Self::Pick(Box::new(Pick::decode(value, state)?)),
"horizon:block_swap" => Self::BlockSwap(
"lithostitched:block_swap" => Self::BlockSwap(
value["blocks"]
.as_object()
.ok_or(DecodeError::Field("blocks"))?
@@ -801,12 +822,16 @@ impl Processor {
.collect::<Result<_, DecodeError>>()?,
),
"horizon:count_cap" => Self::CountCap(Box::new(CountCap::decode(value, state)?)),
"horizon:apply_random" => Self::ApplyRandom(Box::new(
lithostitched::ApplyRandom::decode(value, resources, bindings)?,
"lithostitched:apply_random" => Self::ApplyRandom(Box::new(
litho::ApplyRandom::decode(value, resources, bindings)?,
)),
"horizon:set_block" => {
Self::SetBlock(Box::new(lithostitched::SetBlock::decode(value, state)?))
"lithostitched:set_block" => {
Self::SetBlock(Box::new(litho::SetBlock::decode(value, state)?))
}
"lithostitched:reference" => {
Self::Reference(litho::reference(value, resources, bindings)?)
}
"lithostitched:discard_input" => Self::DiscardInput,
"horizon:pillar" => Self::Pillar(Box::new(pillars::Pillar::decode(
value, resources, bindings, state,
)?)),
@@ -834,7 +859,7 @@ impl Processor {
"horizon:keep_world_block" => {
Self::KeepWorldBlock(Box::new(sea_level::KeepWorldBlock::decode(value)?))
}
"horizon:condition" => Self::Condition(Box::new(lithostitched::Conditional::decode(
"lithostitched:condition" => Self::Condition(Box::new(litho::Conditional::decode(
value, resources, bindings,
)?)),
_ => return Err(DecodeError::Field("structure processor type")),
@@ -1115,24 +1140,10 @@ fn process_block<W: PlacementContext>(
if !position_ok {
continue;
}
let nbt = match &rule.modifier {
Modifier::Passthrough => info.nbt.clone(),
Modifier::Clear => Some(Compound::new()),
Modifier::AppendLoot(table) => {
let mut nbt = info.nbt.clone().unwrap_or_default();
nbt.insert("LootTable".into(), Tag::String(table.as_str().into()));
nbt.insert("LootTableSeed".into(), Tag::Long(random.next_long()));
Some(nbt)
}
Modifier::AppendStatic(data) => Some(edited(info.nbt.as_ref(), &[], data)),
Modifier::Edit { remove, data } => {
Some(edited(info.nbt.as_ref(), remove, data))
}
};
return Ok(Some(BlockInfo {
pos: info.pos,
state: rule.output,
nbt,
nbt: rule.modifier.apply(info.nbt.as_ref(), &mut random),
}));
}
Some(info)
@@ -1165,8 +1176,7 @@ fn process_block<W: PlacementContext>(
false => Some(info),
},
Processor::ApplyRandom(apply) => {
let seed = world.world_seed()?;
let mut random = apply.random.create(seed, position, reference, info.pos);
let mut random = apply.draws.random(world.world_seed()?, position, info.pos);
match apply.pick(&mut random) {
Some(list) => {
return process_list(
@@ -1176,21 +1186,34 @@ fn process_block<W: PlacementContext>(
None => Some(info),
}
}
Processor::Reference(lists) => {
let mut current = info;
for list in lists {
match process_list(
list, world, resources, position, reference, relative, current, settings,
)? {
Some(next) => current = next,
None => return Ok(None),
}
}
Some(current)
}
Processor::DiscardInput => None,
Processor::SetBlock(set) => {
let seed = world.world_seed()?;
let state = set.state(seed, position, reference, info.pos);
let state = match set.preserves() {
let mut random = set.draws.random(world.world_seed()?, position, info.pos);
let state = set.state(&mut random);
let state = match set.preserve {
true => registry
.state(state)
.and_then(|s| with_properties_of(registry, &s.name, info.state))
.unwrap_or(state),
false => state,
};
Some(BlockInfo { state, ..info })
let nbt = set.modifier.apply(info.nbt.as_ref(), &mut random);
Some(BlockInfo { state, nbt, ..info })
}
Processor::Condition(c) => {
let seed = world.world_seed()?;
let mut random = c.random.create(seed, position, reference, info.pos);
let mut random = c.draws.random(world.world_seed()?, position, info.pos);
let location = world.block_state(info.pos)?;
let passed = c
.condition
@@ -0,0 +1,841 @@
//! Lithostitched's processors (spec-lithostitched §5), read as its data
//! writes them: `lithostitched:block_swap` (in `processor.rs`), `reference`,
//! `apply_random`, `condition`, `set_block` and `discard_input`.
//!
//! The draws of `random_chance`, `apply_random` and `set_block` follow
//! Lithostitched's `random_mode` (per block or per piece), but its seeding is
//! not known (OPEN-L6): a positional random of the world seed, the block's or
//! the piece's position and the processor itself, so the shares match while
//! the blocks chosen do not.
use super::{BlockStateId, DecodeError, Modifier, ProcessorList, ProcessorRandom};
use crate::worldgen::{block_feature::FeatureBlockBindings, structure::StructureResources};
use horizon_core::{
position::BlockPos,
random::{LegacyRandom, position_seed},
};
use serde_json::Value;
use std::collections::BTreeSet;
/// `random_mode` (`condition`, `set_block`) or `mode` (`apply_random`).
#[derive(Clone, Copy, Debug)]
pub(super) struct Draws {
per_piece: bool,
/// The processor's own salt, so two processors on a block draw apart.
salt: i64,
}
impl Draws {
fn decode(mode: Option<&Value>, processor: &Value) -> Result<Self, DecodeError> {
let per_piece = match mode.and_then(Value::as_str) {
None | Some("per_block") => false,
Some("per_piece") => true,
_ => return Err(DecodeError::Field("random_mode")),
};
// FNV-1a over the processor's JSON: the same in every run.
let salt = (processor.to_string().bytes()).fold(0xcbf2_9ce4_8422_2325_u64, |h, b| {
(h ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3)
}) as i64;
Ok(Self { per_piece, salt })
}
/// The random for the block at `pos` of the piece at `piece`.
pub(super) fn random(self, world_seed: i64, piece: [i32; 3], pos: [i32; 3]) -> LegacyRandom {
let [x, y, z] = if self.per_piece { piece } else { pos };
LegacyRandom::new(world_seed ^ self.salt ^ position_seed(BlockPos::new(x, y, z)))
}
}
/// A processor or a list of them (`then`, `else`, `apply_random`'s data):
/// a list holder (`ProcessorList::decode`), or one processor object.
fn processors(
value: &Value,
resources: &StructureResources,
bindings: &FeatureBlockBindings<'_>,
) -> Result<ProcessorList, DecodeError> {
let one = value.get("processor_type").is_some();
let holder = if one {
Value::Array(vec![value.clone()])
} else {
value.clone()
};
ProcessorList::decode(&holder.to_string(), resources, bindings)
}
/// `reference`: the named lists, run in turn on the block.
pub(super) fn reference(
value: &Value,
resources: &StructureResources,
bindings: &FeatureBlockBindings<'_>,
) -> Result<Vec<ProcessorList>, DecodeError> {
let names = match &value["processor_lists"] {
Value::Array(a) => a.clone(),
other => vec![other.clone()],
};
(names.iter())
.map(|name| match name.as_str() {
Some(n) if n.starts_with('#') => Err(DecodeError::Field("processor list tag")),
Some(_) => ProcessorList::decode(&name.to_string(), resources, bindings),
None => Err(DecodeError::Field("processor_lists")),
})
.collect()
}
/// `apply_random`: one list drawn by weight runs on the block.
#[derive(Clone, Debug)]
pub struct ApplyRandom {
pub(super) draws: Draws,
lists: Vec<(i32, ProcessorList)>,
total: i32,
}
impl ApplyRandom {
pub(super) fn decode(
value: &Value,
resources: &StructureResources,
bindings: &FeatureBlockBindings<'_>,
) -> Result<Self, DecodeError> {
let entries = value["processor_lists"]
.as_array()
.ok_or(DecodeError::Field("apply_random processor_lists"))?;
let lists = (entries.iter())
.map(|e| match e.get("data") {
Some(data) => {
let weight = e["weight"]
.as_i64()
.unwrap_or(1)
.clamp(0, i64::from(i32::MAX));
Ok((weight as i32, processors(data, resources, bindings)?))
}
None => Ok((1, processors(e, resources, bindings)?)),
})
.collect::<Result<Vec<_>, DecodeError>>()?;
let total = lists
.iter()
.map(|(w, _)| *w)
.fold(0i32, i32::saturating_add);
Ok(Self {
draws: Draws::decode(value.get("mode"), value)?,
lists,
total,
})
}
/// The list drawn: one bounded draw over the total weight.
pub(super) fn pick(&self, random: &mut impl ProcessorRandom) -> Option<&ProcessorList> {
if self.total <= 0 {
return None;
}
let mut at = random.int(self.total);
for (weight, list) in &self.lists {
at -= weight;
if at < 0 {
return Some(list);
}
}
None
}
}
/// Lithostitched's processor conditions.
#[derive(Clone, Debug)]
pub(super) enum Condition {
True,
AllOf(Vec<Condition>),
AnyOf(Vec<Condition>),
Not(Box<Condition>),
Chance(f32),
/// `matching_blocks`: the template's block (`input`) or the world's
/// (`location`) is one of `blocks`, with `properties`.
Matching {
blocks: BTreeSet<String>,
properties: Vec<(String, String)>,
location: bool,
},
}
impl Condition {
/// One condition, or a list of them that must all pass (`if_true`).
pub(super) fn decode(
value: &Value,
bindings: &FeatureBlockBindings<'_>,
) -> Result<Self, DecodeError> {
let list = |v: &Value| -> Result<Vec<Self>, DecodeError> {
(v.as_array().ok_or(DecodeError::Field("conditions"))?.iter())
.map(|c| Self::decode(c, bindings))
.collect()
};
if value.is_array() {
return Ok(Self::AllOf(list(value)?));
}
let kind = value["type"].as_str().unwrap_or_default();
Ok(match crate::worldgen::tags::identifier(kind)?.as_str() {
"lithostitched:true" => Self::True,
"lithostitched:all_of" => Self::AllOf(list(&value["conditions"])?),
"lithostitched:any_of" => Self::AnyOf(list(&value["conditions"])?),
"lithostitched:not" => {
Self::Not(Box::new(Self::decode(&value["condition"], bindings)?))
}
"lithostitched:random_chance" => Self::Chance(
value["chance"]
.as_f64()
.ok_or(DecodeError::Field("random_chance chance"))? as f32,
),
"lithostitched:matching_blocks" => Self::Matching {
blocks: blocks(&value["blocks"], bindings)?,
properties: (value
.get("properties")
.and_then(Value::as_object)
.into_iter())
.flatten()
.map(|(k, v)| {
(
k.clone(),
v.as_str().map_or_else(|| v.to_string(), str::to_owned),
)
})
.collect(),
location: match value.get("match_type").and_then(Value::as_str) {
None | Some("input") => false,
Some("location") => true,
_ => return Err(DecodeError::Field("matching_blocks match_type")),
},
},
_ => return Err(DecodeError::Field("lithostitched condition type")),
})
}
pub(super) fn test(
&self,
input: BlockStateId,
location: BlockStateId,
registry: &crate::registry::BlockRegistry,
random: &mut impl ProcessorRandom,
) -> bool {
match self {
Self::True => true,
Self::AllOf(all) => all
.iter()
.all(|c| c.test(input, location, registry, random)),
Self::AnyOf(any) => any
.iter()
.any(|c| c.test(input, location, registry, random)),
Self::Not(c) => !c.test(input, location, registry, random),
Self::Chance(chance) => random.float() < *chance,
Self::Matching {
blocks,
properties,
location: at_location,
} => {
let Some(state) = registry.state(if *at_location { location } else { input })
else {
return false;
};
blocks.contains(&state.name)
&& (properties.iter()).all(|(k, v)| state.properties.get(k) == Some(v))
}
}
}
}
/// A block set (an id, a list, `#tag`) by block name. Blocks the game does
/// not have are left out: no template block can be one.
fn blocks(
value: &Value,
bindings: &FeatureBlockBindings<'_>,
) -> Result<BTreeSet<String>, DecodeError> {
let names: Vec<&str> = match value {
Value::String(s) => vec![s.as_str()],
Value::Array(a) => a.iter().filter_map(Value::as_str).collect(),
_ => return Err(DecodeError::Field("matching_blocks blocks")),
};
let registry = bindings.registry();
let mut out = BTreeSet::new();
for name in names {
match name.strip_prefix('#') {
Some(tag) => {
let tag = crate::worldgen::tags::identifier(tag)?;
let states = bindings.tags.get(&tag).into_iter().flat_map(|s| s.iter());
out.extend(states.filter_map(|s| Some(registry.state(*s)?.name.clone())));
}
None => {
out.insert(crate::worldgen::tags::identifier(name)?.to_string());
}
}
}
Ok(out)
}
/// `condition`: `then` when the conditions pass, else `else` (none: the
/// block as it is).
#[derive(Clone, Debug)]
pub struct Conditional {
pub(super) draws: Draws,
pub(super) condition: Condition,
pub(super) then: ProcessorList,
pub(super) otherwise: ProcessorList,
}
impl Conditional {
pub(super) fn decode(
value: &Value,
resources: &StructureResources,
bindings: &FeatureBlockBindings<'_>,
) -> Result<Self, DecodeError> {
let branch = |key| match value.get(key) {
Some(v) => processors(v, resources, bindings),
None => Ok(ProcessorList(Vec::new())),
};
Ok(Self {
draws: Draws::decode(value.get("random_mode"), value)?,
condition: Condition::decode(&value["if_true"], bindings)?,
then: branch("then")?,
otherwise: branch("else")?,
})
}
}
/// `set_block`: the block from a state provider (simple or weighted).
#[derive(Clone, Debug)]
pub struct SetBlock {
pub(super) draws: Draws,
states: Vec<(i32, BlockStateId)>,
total: i32,
/// `preserve_state` (true by default): the template block's properties
/// the new block has too are kept.
pub(super) preserve: bool,
pub(super) modifier: Modifier,
}
impl SetBlock {
pub(super) fn decode(
value: &Value,
state: impl Fn(&Value) -> Result<BlockStateId, DecodeError>,
) -> Result<Self, DecodeError> {
let provider = &value["state_provider"];
let kind = provider["type"].as_str().unwrap_or_default();
let states = match crate::worldgen::tags::identifier(kind)?.as_str() {
"minecraft:simple_state_provider" => vec![(1, state(&provider["state"])?)],
"minecraft:weighted_state_provider" => (provider["entries"].as_array().into_iter())
.flatten()
.map(|e| {
Ok((
e["weight"].as_i64().unwrap_or(1).clamp(0, 1 << 30) as i32,
state(&e["data"])?,
))
})
.collect::<Result<_, DecodeError>>()?,
_ => return Err(DecodeError::Field("set_block state_provider")),
};
let total = states.iter().map(|(w, _)| *w).sum();
if total <= 0 {
return Err(DecodeError::Field("set_block state_provider"));
}
Ok(Self {
draws: Draws::decode(value.get("random_mode"), value)?,
states,
total,
preserve: value
.get("preserve_state")
.and_then(Value::as_bool)
.unwrap_or(true),
modifier: Modifier::decode(value.get("block_entity_modifier"))?,
})
}
/// The provider's state: WeightedList.getRandom, one bounded draw.
pub(super) fn state(&self, random: &mut impl ProcessorRandom) -> BlockStateId {
let mut at = random.int(self.total);
for (weight, state) in &self.states {
at -= weight;
if at < 0 {
return *state;
}
}
self.states[0].1
}
}
#[cfg(test)]
mod tests {
use super::super::{BlockInfo, PlaceSettings, ProcessorList, process_list};
use crate::{
heightmap::HeightmapType,
nbt::{JavaString, Tag},
registry::{BlockRegistry, BlockStateId},
worldgen::{
anchor::WorldGenerationContext,
block_feature::FeatureBlockBindings,
placement::{PlacementContext, PlacementQueryError},
structure::{
Rotation, StateTransforms, StructureResources, template::parse_state_string,
},
},
};
use serde_json::{Value, json};
use std::collections::{BTreeMap, BTreeSet};
/// Every combination of `properties`' values, the first one the default.
fn states(name: &str, properties: &[(&str, &[&str])]) -> Vec<Value> {
let mut out = vec![serde_json::Map::new()];
for (key, values) in properties {
out = (out.iter())
.flat_map(|o| {
values.iter().map(move |v| {
let mut o = o.clone();
o.insert((*key).into(), (*v).into());
o
})
})
.collect();
}
(out.into_iter())
.map(|p| json!([name, name.ends_with("air"), false, "", {"id": name, "properties": p}]))
.collect()
}
/// A registry of the blocks CTOV's lists and the probe pack use.
fn registry() -> &'static BlockRegistry {
let ages: Vec<String> = (0..8).map(|a| a.to_string()).collect();
let ages: Vec<&str> = ages.iter().map(String::as_str).collect();
let levels: Vec<String> = (0..16).map(|a| a.to_string()).collect();
let levels: Vec<&str> = levels.iter().map(String::as_str).collect();
let water = [("level", &levels[..])];
let stairs: &[(&str, &[&str])] =
&[("facing", &["north", "east"]), ("half", &["bottom", "top"])];
let blocks: Vec<(&str, &[(&str, &[&str])])> = vec![
("minecraft:air", &[]),
("minecraft:cave_air", &[]),
("minecraft:void_air", &[]),
("minecraft:lava", &water),
("minecraft:cobblestone", &[]),
("minecraft:blackstone", &[]),
("minecraft:deepslate", &[]),
("minecraft:water", &water),
("minecraft:spruce_planks", &[]),
("minecraft:chest", &[]),
("minecraft:oak_log", &[("axis", &["y", "x"])]),
("minecraft:stripped_cherry_log", &[("axis", &["y", "x"])]),
("minecraft:oak_stairs", stairs),
("minecraft:warped_stairs", stairs),
];
let wheat = [("age", &ages[..])];
let beetroots = [("age", &ages[..4])];
let mut rows = Vec::new();
let mut defaults = BTreeMap::new();
let crops: [(&str, &[(&str, &[&str])]); 3] = [
("minecraft:wheat", &wheat),
("minecraft:carrots", &wheat),
("minecraft:beetroots", &beetroots),
];
for (name, properties) in blocks.into_iter().chain(crops) {
defaults.insert(name, rows.len());
rows.extend(states(name, properties));
}
let facts = json!({"states": rows, "defaults": defaults});
let registry = BlockRegistry::from_facts_json(facts.to_string().as_bytes()).unwrap();
Box::leak(Box::new(registry))
}
/// The world a piece is placed in: one block everywhere.
struct World {
seed: i64,
block: BlockStateId,
}
impl PlacementContext for World {
fn world_seed(&mut self) -> Result<i64, PlacementQueryError> {
Ok(self.seed)
}
fn block_state(&mut self, _: [i32; 3]) -> Result<BlockStateId, PlacementQueryError> {
Ok(self.block)
}
fn generation(&self) -> WorldGenerationContext {
WorldGenerationContext::new(-64, 384, 63)
}
fn min_y(&mut self) -> Result<i32, PlacementQueryError> {
Ok(-64)
}
fn height(&mut self, _: HeightmapType, _: i32, _: i32) -> Result<i32, PlacementQueryError> {
Err(PlacementQueryError::Unavailable)
}
}
struct Lists {
registry: &'static BlockRegistry,
resources: StructureResources,
}
impl Lists {
fn new(lists: &[(&str, Value)]) -> Self {
let registry = registry();
let lists = (lists.iter())
.map(|(id, list)| ((*id).to_owned(), list.to_string()))
.collect();
let resources = StructureResources::new(
registry,
StateTransforms::default(),
std::path::Path::new("/nonexistent"),
lists,
&BTreeMap::new(),
);
Self {
registry,
resources,
}
}
fn id(&self, state: &str) -> BlockStateId {
parse_state_string(self.registry, state).unwrap()
}
fn decode(&self, processors: Value) -> Result<ProcessorList, crate::worldgen::DecodeError> {
let bindings = FeatureBlockBindings::new(self.registry, BTreeSet::new()).unwrap();
ProcessorList::decode(&processors.to_string(), &self.resources, &bindings)
}
/// The block the list makes of template block `state` at `pos` of the
/// piece at `piece`, in a world of `world`: None when it is not placed.
fn run(
&self,
list: &ProcessorList,
world: (i64, &str),
piece: [i32; 3],
pos: [i32; 3],
state: &str,
) -> Option<BlockInfo> {
let mut world = World {
seed: world.0,
block: self.id(world.1),
};
let info = BlockInfo {
pos,
state: self.id(state),
nbt: None,
};
let mut settings = PlaceSettings::new(Rotation::None);
process_list(
list,
&mut world,
&self.resources,
piece,
piece,
[0; 3],
info,
&mut settings,
)
.unwrap()
}
fn name(&self, info: Option<BlockInfo>) -> String {
match info {
None => "none".into(),
Some(info) => {
let s = self.registry.state(info.state).unwrap();
let props: Vec<String> = s
.properties
.iter()
.map(|(k, v)| format!("{k}={v}"))
.collect();
match props.is_empty() {
true => s.name.clone(),
false => format!("{}[{}]", s.name, props.join(",")),
}
}
}
}
/// `name(run(...))` for one template block in an air world of seed 0.
fn out(&self, list: &ProcessorList, state: &str) -> String {
self.name(self.run(list, (0, "minecraft:air"), [0; 3], [0; 3], state))
}
}
fn swap(blocks: Value) -> Value {
json!({"processor_type": "lithostitched:block_swap", "blocks": blocks})
}
fn set_block(provider: Value) -> Value {
json!({"processor_type": "lithostitched:set_block", "state_provider": provider})
}
fn simple(name: &str) -> Value {
json!({"type": "minecraft:simple_state_provider", "state": {"id": name}})
}
#[test]
fn block_swap_keeps_the_properties_the_new_block_can_have() {
let t = Lists::new(&[]);
let list = t
.decode(json!([swap(json!({
"minecraft:wheat": "minecraft:carrots",
"oak_stairs": "minecraft:warped_stairs",
"minecraft:cobblestone": "natures_spirit:missing"}))]))
.unwrap();
assert_eq!(
t.out(&list, "minecraft:wheat[age=7]"),
"minecraft:carrots[age=7]"
);
assert_eq!(
t.out(&list, "minecraft:oak_stairs[facing=east,half=top]"),
"minecraft:warped_stairs[facing=east,half=top]"
);
// A block the game lacks leaves the template's; others pass.
assert_eq!(
t.out(&list, "minecraft:cobblestone"),
"minecraft:cobblestone"
);
assert_eq!(
t.out(&list, "minecraft:oak_log[axis=x]"),
"minecraft:oak_log[axis=x]"
);
// Beetroots' age stops at 3: ripe wheat gives the default (spec-ctov §4).
let beet = t
.decode(json!([swap(
json!({"minecraft:wheat": "minecraft:beetroots"})
)]))
.unwrap();
assert_eq!(
t.out(&beet, "minecraft:wheat[age=7]"),
"minecraft:beetroots[age=0]"
);
assert_eq!(
t.out(&beet, "minecraft:wheat[age=2]"),
"minecraft:beetroots[age=2]"
);
}
#[test]
fn references_run_the_named_lists_in_turn() {
let t = Lists::new(&[
(
"t:logs",
json!({"processors": [swap(json!({"minecraft:oak_log": "minecraft:stripped_cherry_log"}))]}),
),
(
"t:stone",
json!([swap(
json!({"minecraft:stripped_cherry_log": "minecraft:cobblestone"})
)]),
),
(
"t:discard",
json!([{"processor_type": "lithostitched:discard_input"}]),
),
]);
let reference = |lists: Value| json!([{"processor_type": "lithostitched:reference", "processor_lists": lists}]);
let one = t.decode(reference(json!("t:logs"))).unwrap();
assert_eq!(
t.out(&one, "minecraft:oak_log[axis=x]"),
"minecraft:stripped_cherry_log[axis=x]"
);
let both = t.decode(reference(json!(["t:logs", "t:stone"]))).unwrap();
assert_eq!(t.out(&both, "minecraft:oak_log"), "minecraft:cobblestone");
let reversed = t.decode(reference(json!(["t:stone", "t:logs"]))).unwrap();
assert_eq!(
t.out(&reversed, "minecraft:oak_log"),
"minecraft:stripped_cherry_log[axis=y]"
);
// discard_input: the template block is not placed, and the lists after it do not run.
let discard = t.decode(reference(json!(["t:discard", "t:logs"]))).unwrap();
assert_eq!(t.out(&discard, "minecraft:oak_log"), "none");
// The game has no processor list tags; unknown lists are errors.
assert!(t.decode(reference(json!(["#t:palettes"]))).is_err());
assert!(t.decode(reference(json!("t:missing"))).is_err());
}
/// The probe pack's nested condition (spec-lithostitched §7.3): template
/// cobblestone becomes blackstone or deepslate, half each.
#[test]
fn conditions_run_then_or_else() {
let t = Lists::new(&[]);
let inner = json!({"processor_type": "lithostitched:condition", "random_mode": "per_block",
"if_true": {"type": "lithostitched:random_chance", "chance": 0.5},
"then": set_block(simple("minecraft:blackstone")),
"else": [set_block(simple("minecraft:deepslate"))]});
let list = t
.decode(json!([{"processor_type": "lithostitched:condition",
"if_true": [{"type": "lithostitched:matching_blocks", "blocks": "minecraft:cobblestone"}],
"then": [inner]}]))
.unwrap();
let mut counts = BTreeMap::<String, u32>::new();
for i in 0..4000 {
let out = t.run(
&list,
(1234, "minecraft:air"),
[0; 3],
[i % 64, i / 64, 7],
"minecraft:cobblestone",
);
*counts.entry(t.name(out)).or_default() += 1;
}
assert_eq!(counts.len(), 2, "{counts:?}");
assert!(
(1850..2150).contains(&counts["minecraft:blackstone"]),
"{counts:?}"
);
// Not cobblestone, and no `else`: the block as it is.
assert_eq!(
t.out(&list, "minecraft:oak_log"),
"minecraft:oak_log[axis=y]"
);
// `location` tests the world's block, with properties on the input.
let water = t
.decode(
json!([{"processor_type": "lithostitched:condition", "random_mode": "per_piece",
"if_true": {"type": "lithostitched:matching_blocks", "blocks": ["minecraft:water"],
"match_type": "location"},
"then": set_block(simple("minecraft:spruce_planks"))}]),
)
.unwrap();
let at = |world| {
t.name(t.run(
&water,
(0, world),
[0; 3],
[1, 2, 3],
"minecraft:cobblestone",
))
};
assert_eq!(at("minecraft:water"), "minecraft:spruce_planks");
assert_eq!(at("minecraft:air"), "minecraft:cobblestone");
let ripe = t
.decode(json!([{"processor_type": "lithostitched:condition",
"if_true": {"type": "lithostitched:all_of", "conditions": [
{"type": "lithostitched:true"},
{"type": "lithostitched:matching_blocks", "blocks": "minecraft:wheat", "properties": {"age": "7"}},
{"type": "lithostitched:not", "condition": {"type": "lithostitched:any_of", "conditions": [
{"type": "lithostitched:random_chance", "chance": 0.0},
{"type": "lithostitched:matching_blocks", "blocks": "minecraft:water", "match_type": "location"}]}}]},
"then": swap(json!({"minecraft:wheat": "minecraft:carrots"}))}]))
.unwrap();
assert_eq!(
t.out(&ripe, "minecraft:wheat[age=7]"),
"minecraft:carrots[age=7]"
);
assert_eq!(
t.out(&ripe, "minecraft:wheat[age=6]"),
"minecraft:wheat[age=6]"
);
let bad = json!([{"processor_type": "lithostitched:condition", "random_mode": "per_chunk",
"if_true": {"type": "lithostitched:true"}}]);
assert!(t.decode(bad).is_err());
}
/// `apply_random` (spec-lithostitched §7.3: crimson 1 : birch 3, one
/// choice per piece): here blackstone 1 : deepslate 3.
#[test]
fn apply_random_draws_per_piece_or_per_block() {
let t = Lists::new(&[(
"t:deepslate",
json!([swap(
json!({"minecraft:cobblestone": "minecraft:deepslate"})
)]),
)]);
let random = |mode: &str| {
t.decode(json!([{"processor_type": "lithostitched:apply_random", "mode": mode,
"processor_lists": [
{"weight": 1, "data": [swap(json!({"minecraft:cobblestone": "minecraft:blackstone"}))]},
{"weight": 3, "data": "t:deepslate"}]}]))
.unwrap()
};
let (per_piece, per_block) = (random("per_piece"), random("per_block"));
let outcomes = |list: &ProcessorList, seed: i64, piece: [i32; 3]| -> Vec<String> {
(0..20)
.map(|i| {
let pos = [piece[0] + i, piece[1], piece[2] + 3 * i];
t.name(t.run(
list,
(seed, "minecraft:air"),
piece,
pos,
"minecraft:cobblestone",
))
})
.collect()
};
let mut blackstone_pieces = 0;
for p in 0..400 {
let piece = [16 * p, 70, -40 * p];
let blocks = outcomes(&per_piece, 1234, piece);
assert!(blocks.iter().all(|b| *b == blocks[0]), "{blocks:?}");
blackstone_pieces += u32::from(blocks[0] == "minecraft:blackstone");
// The same piece and seed give the same blocks.
assert_eq!(outcomes(&per_piece, 1234, piece), blocks);
}
assert!(
(70..130).contains(&blackstone_pieces),
"{blackstone_pieces}"
);
// Per block, one piece mixes both.
let mixed: BTreeSet<String> = (0..10)
.flat_map(|p| outcomes(&per_block, 1234, [p, 0, 0]))
.collect();
assert_eq!(mixed.len(), 2, "{mixed:?}");
// A plain list: every entry weight 1. An entry list holding nothing is a no-op.
let plain = t
.decode(
json!([{"processor_type": "lithostitched:apply_random", "mode": "per_block",
"processor_lists": ["t:deepslate", []]}]),
)
.unwrap();
let seen: BTreeSet<String> = (0..10)
.flat_map(|p| outcomes(&plain, 9, [p, 0, 0]))
.collect();
assert_eq!(
seen,
BTreeSet::from(["minecraft:cobblestone".into(), "minecraft:deepslate".into()])
);
}
#[test]
fn set_block_runs_its_provider() {
let t = Lists::new(&[]);
let stairs = "minecraft:oak_stairs[facing=east,half=top]";
let warped = |preserve: Value| {
let mut p = set_block(simple("minecraft:warped_stairs"));
if !preserve.is_null() {
p["preserve_state"] = preserve;
}
t.decode(json!([p])).unwrap()
};
// preserve_state (true by default) keeps the properties both blocks have.
assert_eq!(
t.out(&warped(Value::Null), stairs),
"minecraft:warped_stairs[facing=east,half=top]"
);
assert_eq!(
t.out(&warped(json!(false)), stairs),
"minecraft:warped_stairs[facing=north,half=bottom]"
);
let weighted = t
.decode(json!([set_block(
json!({"type": "minecraft:weighted_state_provider", "entries": [
{"weight": 1, "data": {"id": "minecraft:blackstone"}},
{"weight": 3, "data": {"id": "minecraft:deepslate"}}]})
)]))
.unwrap();
let blackstone = (0..2000)
.filter(|i| {
let out = t.run(
&weighted,
(42, "minecraft:air"),
[0; 3],
[*i, 0, -*i],
"minecraft:cobblestone",
);
t.name(out) == "minecraft:blackstone"
})
.count();
assert!((420..580).contains(&blackstone), "{blackstone}");
// block_entity_modifier gives the output its block entity.
let mut loot = set_block(simple("minecraft:chest"));
loot["block_entity_modifier"] =
json!({"type": "minecraft:append_loot", "loot_table": "t:chests/house"});
let out = t
.run(
&t.decode(json!([loot])).unwrap(),
(0, "minecraft:air"),
[0; 3],
[0; 3],
"minecraft:air",
)
.unwrap();
let table = out
.nbt
.as_ref()
.and_then(|n| n.get(&JavaString::from("LootTable")));
assert_eq!(table, Some(&Tag::String("t:chests/house".into())));
// Providers the game does not run are refused.
assert!(
t.decode(json!([set_block(
json!({"type": "minecraft:noise_provider"})
)]))
.is_err()
);
}
}
@@ -1,434 +0,0 @@
//! Lithostitched's processors that pick processors, carried as data
//! (`horizon:apply_random` and `horizon:condition`): one runs a processor
//! list drawn by weight, the other one list or another by a test of the
//! block. Both draw from Lithostitched's RandomSettings: a random seeded from
//! the world seed and a name, at the block, the piece or the structure.
//! `horizon:set_block` is its `set_block`, which the conditions mostly run.
use super::{Processor, ProcessorList};
use crate::{
registry::{BlockRegistry, BlockStateId},
worldgen::{DecodeError, block_feature::FeatureBlockBindings, structure::StructureResources},
};
use horizon_core::{
position::BlockPos,
random::{LegacyRandom, PositionalRandom, RandomSource},
};
use serde_json::Value;
use std::{collections::BTreeSet, sync::Arc};
/// RandomSettings: where the random is taken, and the name salting it.
#[derive(Clone, Debug, PartialEq)]
pub struct RandomSettings {
mode: Mode,
/// ResourceLocation.hashCode of the name (`lithostitched:default`).
salt: i32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Mode {
Block,
Piece,
Structure,
}
impl RandomSettings {
/// A mode alone, or `{"mode": ..., "name": ...}`; per block when unset.
fn decode(v: Option<&Value>) -> Result<Self, DecodeError> {
let mode = Mode::decode;
let (mode, name) = match v {
None => (Mode::Block, "lithostitched:default"),
Some(Value::String(m)) => (mode(Some(m))?, "lithostitched:default"),
Some(o) => (
mode(o["mode"].as_str())?,
o["name"]
.as_str()
.ok_or(DecodeError::Field("random name"))?,
),
};
let (namespace, path) = name.split_once(':').unwrap_or(("minecraft", name));
Ok(Self {
mode,
salt: java_hash(namespace)
.wrapping_mul(31)
.wrapping_add(java_hash(path)),
})
}
/// RandomSource.create(seed + hash).forkPositional().at(the chosen
/// position): the piece's position, the structure's pivot or the block.
pub(super) fn create(
&self,
seed: i64,
piece: [i32; 3],
pivot: [i32; 3],
block: [i32; 3],
) -> LegacyRandom {
let [x, y, z] = self.mode.select(piece, pivot, block);
LegacyRandom::new(seed.wrapping_add(i64::from(self.salt)))
.fork_positional()
.at(BlockPos::new(x, y, z))
}
}
impl Mode {
fn decode(m: Option<&str>) -> Result<Self, DecodeError> {
match m {
None | Some("per_block") => Ok(Self::Block),
Some("per_piece") => Ok(Self::Piece),
Some("per_structure") => Ok(Self::Structure),
_ => Err(DecodeError::Field("random mode")),
}
}
fn select(self, piece: [i32; 3], pivot: [i32; 3], block: [i32; 3]) -> [i32; 3] {
match self {
Self::Block => block,
Self::Piece => piece,
Self::Structure => pivot,
}
}
}
/// SetBlockProcessor with a simple or weighted state provider and no block
/// entity change: the block becomes the state, keeping the properties it
/// shares with the old one unless `preserve_state` is off.
#[derive(Clone, Debug)]
pub struct SetBlock {
/// The states and their weights; one state alone is a simple provider,
/// which draws nothing.
states: Vec<(BlockStateId, i32)>,
weighted: bool,
preserve: bool,
mode: Mode,
}
impl SetBlock {
pub(super) fn decode(
v: &Value,
state: impl Fn(&Value) -> Result<BlockStateId, DecodeError>,
) -> Result<Self, DecodeError> {
let provider = &v["state_provider"];
let (states, weighted) = match provider["type"].as_str() {
Some("minecraft:simple_state_provider") => {
(vec![(state(&provider["state"])?, 1)], false)
}
Some("minecraft:weighted_state_provider") => (
provider["entries"]
.as_array()
.ok_or(DecodeError::Field("set_block entries"))?
.iter()
.map(|e| {
let weight = e["weight"]
.as_i64()
.ok_or(DecodeError::Field("set_block weight"))?;
Ok((state(&e["data"])?, weight as i32))
})
.collect::<Result<_, DecodeError>>()?,
true,
),
_ => return Err(DecodeError::Field("set_block state_provider")),
};
if states.is_empty() {
return Err(DecodeError::Field("set_block entries"));
}
Ok(Self {
states,
weighted,
preserve: v["preserve_state"].as_bool().unwrap_or(true),
mode: Mode::decode(v["random_mode"].as_str())?,
})
}
/// The state to set, before `preserve`: drawn with
/// RandomSource.create(seed).forkPositional().at(the chosen position).
pub(super) fn state(
&self,
seed: i64,
piece: [i32; 3],
pivot: [i32; 3],
block: [i32; 3],
) -> BlockStateId {
if !self.weighted {
return self.states[0].0;
}
let [x, y, z] = self.mode.select(piece, pivot, block);
let mut random = LegacyRandom::new(seed)
.fork_positional()
.at(BlockPos::new(x, y, z));
let total: i32 = self.states.iter().map(|s| s.1).sum();
let mut value = random.next_int_bound(total.max(1)).unwrap_or(0);
for (state, weight) in &self.states {
if value < *weight {
return *state;
}
value -= weight;
}
self.states[0].0
}
pub(super) fn preserves(&self) -> bool {
self.preserve
}
}
/// String.hashCode over UTF-16 units.
fn java_hash(s: &str) -> i32 {
s.encode_utf16()
.fold(0i32, |h, c| h.wrapping_mul(31).wrapping_add(i32::from(c)))
}
fn list(
v: &Value,
resources: &StructureResources,
bindings: &FeatureBlockBindings<'_>,
) -> Result<ProcessorList, DecodeError> {
ProcessorList::decode(&v.to_string(), resources, bindings)
}
/// ApplyRandomStructureProcessor: a weighted list of processor lists, one
/// drawn per block with `nextInt(total)`.
#[derive(Clone, Debug)]
pub struct ApplyRandom {
pub(super) random: RandomSettings,
lists: Vec<(ProcessorList, i32)>,
total: i32,
}
impl ApplyRandom {
pub(super) fn decode(
v: &Value,
resources: &StructureResources,
bindings: &FeatureBlockBindings<'_>,
) -> Result<Self, DecodeError> {
let lists = v["processor_lists"]
.as_array()
.ok_or(DecodeError::Field("apply_random processor_lists"))?
.iter()
.map(|e| {
let weight = e["weight"]
.as_i64()
.ok_or(DecodeError::Field("apply_random weight"))?;
Ok((list(&e["data"], resources, bindings)?, weight as i32))
})
.collect::<Result<Vec<_>, DecodeError>>()?;
Ok(Self {
random: RandomSettings::decode(v.get("mode"))?,
total: lists.iter().map(|l| l.1).sum(),
lists,
})
}
/// WeightedList.getRandom: none when the weights add up to 0.
pub(super) fn pick(&self, random: &mut impl RandomSource) -> Option<&ProcessorList> {
if self.total <= 0 {
return None;
}
let mut value = random.next_int_bound(self.total).unwrap_or(0);
for (list, weight) in &self.lists {
if value < *weight {
return Some(list);
}
value -= weight;
}
None
}
}
/// ConditionProcessor: `then` when the condition holds for the block, else
/// `else`.
#[derive(Clone, Debug)]
pub struct Conditional {
pub(super) random: RandomSettings,
pub(super) condition: Condition,
pub(super) then: ProcessorList,
pub(super) otherwise: ProcessorList,
}
impl Conditional {
pub(super) fn decode(
v: &Value,
resources: &StructureResources,
bindings: &FeatureBlockBindings<'_>,
) -> Result<Self, DecodeError> {
// A processor alone or a list of them.
let processors = |key: &str| -> Result<ProcessorList, DecodeError> {
match v.get(key) {
None => Ok(ProcessorList::default()),
Some(Value::Object(p)) if p.contains_key("processor_type") => Ok(ProcessorList(
vec![Processor::decode(&v[key], resources, bindings)?],
)),
Some(other) => list(other, resources, bindings),
}
};
Ok(Self {
random: RandomSettings::decode(v.get("random_mode"))?,
condition: Condition::decode(&v["if_true"], bindings)?,
then: processors("then")?,
otherwise: processors("else")?,
})
}
}
/// Lithostitched's processor conditions the game runs.
#[derive(Clone, Debug)]
pub(super) enum Condition {
True,
AllOf(Vec<Condition>),
AnyOf(Vec<Condition>),
Not(Box<Condition>),
RandomChance(f32),
/// The block placed (`input`) or the one in the world (`location`) is
/// one of these, with these property values.
MatchingBlocks {
blocks: Arc<BTreeSet<BlockStateId>>,
properties: Vec<(String, String)>,
location: bool,
},
}
impl Condition {
fn decode(v: &Value, bindings: &FeatureBlockBindings<'_>) -> Result<Self, DecodeError> {
let all = |a: &Value| -> Result<Vec<Condition>, DecodeError> {
a.as_array()
.ok_or(DecodeError::Field("processor conditions"))?
.iter()
.map(|c| Self::decode(c, bindings))
.collect()
};
if v.is_array() {
return Ok(Self::AllOf(all(v)?));
}
let kind = v["type"].as_str().unwrap_or_default();
Ok(match kind.strip_prefix("lithostitched:").unwrap_or(kind) {
"true" => Self::True,
"all_of" => Self::AllOf(all(&v["conditions"])?),
"any_of" => Self::AnyOf(all(&v["conditions"])?),
"not" => Self::Not(Box::new(Self::decode(&v["condition"], bindings)?)),
"random_chance" => Self::RandomChance(super::float(v, "chance")?),
"matching_blocks" => Self::MatchingBlocks {
blocks: super::holder_set(&v["blocks"], bindings)?,
properties: match v.get("properties") {
None => Vec::new(),
Some(p) => p
.as_object()
.ok_or(DecodeError::Field("matching_blocks properties"))?
.iter()
.map(|(k, x)| {
let x = x
.as_str()
.ok_or(DecodeError::Field("matching_blocks property"))?;
Ok((k.clone(), x.to_owned()))
})
.collect::<Result<_, DecodeError>>()?,
},
location: match v["match_type"].as_str() {
None | Some("input") => false,
Some("location") => true,
_ => return Err(DecodeError::Field("matching_blocks match_type")),
},
},
_ => return Err(DecodeError::Field("processor condition type")),
})
}
/// ProcessorCondition.test with the block placed and the one there.
pub(super) fn test(
&self,
input: BlockStateId,
location: BlockStateId,
registry: &BlockRegistry,
random: &mut impl RandomSource,
) -> bool {
match self {
Self::True => true,
Self::AllOf(all) => all
.iter()
.all(|c| c.test(input, location, registry, random)),
Self::AnyOf(any) => any
.iter()
.any(|c| c.test(input, location, registry, random)),
Self::Not(c) => !c.test(input, location, registry, random),
Self::RandomChance(chance) => random.next_float() < *chance,
Self::MatchingBlocks {
blocks,
properties,
location: at_location,
} => {
let state = if *at_location { location } else { input };
blocks.contains(&state)
&& properties.iter().all(|(k, x)| {
registry
.state(state)
.and_then(|s| s.properties.get(k))
.is_some_and(|v| v == x)
})
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn random_settings_salt_and_position() {
// ResourceLocation.hashCode: 31 * namespace + path.
let default = RandomSettings::decode(Some(&"per_piece".into())).unwrap();
assert_eq!(
default.salt,
java_hash("lithostitched")
.wrapping_mul(31)
.wrapping_add(java_hash("default"))
);
assert_eq!(java_hash("default"), 1544803905);
let (seed, piece, pivot, block) = (12345, [16, 64, -32], [8, 70, 8], [17, 65, -30]);
let mut ours = default.create(seed, piece, pivot, block);
let mut theirs = LegacyRandom::new(seed + i64::from(default.salt))
.fork_positional()
.at(BlockPos::new(16, 64, -32));
assert_eq!(ours.next_long(), theirs.next_long());
let named: Value = serde_json::json!({"mode": "per_structure", "name": "v:roof"});
let named = RandomSettings::decode(Some(&named)).unwrap();
assert_eq!(named.mode, Mode::Structure);
assert_eq!(
named.salt,
java_hash("v")
.wrapping_mul(31)
.wrapping_add(java_hash("roof"))
);
}
#[test]
fn apply_random_draws_by_weight() {
let apply = ApplyRandom {
random: RandomSettings {
mode: Mode::Block,
salt: 0,
},
lists: vec![
(ProcessorList(vec![Processor::Nop]), 3),
(ProcessorList::default(), 1),
],
total: 4,
};
let mut ours = LegacyRandom::new(9);
let mut theirs = LegacyRandom::new(9);
for _ in 0..32 {
let draw = theirs.next_int_bound(4).unwrap();
let picked = apply.pick(&mut ours).unwrap();
assert_eq!(picked.0.len(), usize::from(draw < 3));
}
}
#[test]
fn set_block_draws_from_the_unsalted_seed() {
let (a, b) = (BlockStateId(1), BlockStateId(2));
let set = SetBlock::decode(
&serde_json::json!({"state_provider": {"type": "minecraft:weighted_state_provider",
"entries": [{"weight": 1, "data": 1}, {"weight": 2, "data": 2}]},
"random_mode": "per_piece"}),
|v| Ok(BlockStateId(v.as_u64().unwrap() as u32)),
)
.unwrap();
assert!(set.preserves());
for x in 0..16 {
let mut java = LegacyRandom::new(77)
.fork_positional()
.at(BlockPos::new(x, 5, 3));
let want = if java.next_int_bound(3).unwrap() < 1 {
a
} else {
b
};
assert_eq!(set.state(77, [x, 5, 3], [0; 3], [9, 9, 9]), want);
}
}
}
+4 -2
View File
@@ -21,7 +21,7 @@ Level: preview ([stability.md](stability.md)). Schema 1.
"license": "MIT", "source": "https://github.com/...", "icon": "https://...png", "key": "RWT...",
"versions": [{
"version": "0.1.0", "published": "2026-10-24", "changelog": "",
"api": "0.1", "side": "client", "capabilities": ["game-read", "hud", "chat"], "game": "26.3",
"api": "0.1", "side": "client", "capabilities": ["game-read", "hud", "chat"], "api_level": 0, "game": "26.3",
"file": { "name": "coords_hud-0.1.0.hmod", "url": "https://github.com/ludovic111/horizon-mods/releases/download/...",
"size": 438, "sha256": "e113...", "signature": "untrusted comment: ...\n...\ntrusted comment: ...\n...\n" }
}]
@@ -43,6 +43,7 @@ Level: preview ([stability.md](stability.md)). Schema 1.
| `versions` | Newest first, at least one. |
| `api` | A mod's API line (its manifest's `api`); a modpack's manifest format (`1`). |
| `side`, `capabilities` | A mod's, read from its manifest by CI, never typed by hand. |
| `api_level` | The mod API level a mod's code needs ([stability.md](stability.md#mod-api-levels)): its newest imported function's, read from the file by CI. Absent for content-only mods and in indexes built before levels. Installers take the newest version whose level the running game serves. |
| `game` | The Minecraft version it is for. |
| `requires` | A modpack's mods: `{ id, version, sha256 }`, each a version in this index with that hash. |
| `file` | A plain name with the kind's extension (`.hmod`, `.hpack`, `.zip`), an `https://` URL, the size (at most 64 MiB), the lowercase hex SHA-256, and the `.minisig` text when the project has a key. |
@@ -57,7 +58,8 @@ changing what one means, makes schema 2.
1. Its size and SHA-256 are the ones the entry lists, and it isn't revoked.
2. With a project key: its signature verifies with that key (minisign's prehashed and legacy signatures both).
3. What the file says matches the entry: a mod's `horizon-mod.toml` (id, version, API, side, capabilities) and its
3. What the file says matches the entry: a mod's `horizon-mod.toml` (id, version, API, side, capabilities), its API
level when the entry lists one, and its
content's fence ([mods.md](../mods.md#content-mods-api-02)); a modpack's manifest (id, version, format, game,
mods); a resource pack's `pack.mcmeta`; a shader pack's `horizon_shader_api::check` and manifest (id, version,
shader API).
+16
View File
@@ -34,6 +34,22 @@ Not breaking: a new function, interface, capability, command, endpoint, tool or
argument or JSON field (readers must ignore fields they don't know); a new WIT function or type (mods built earlier
don't import it); faster, or fixing behaviour to what the reference already says.
### Mod API levels
A new WIT function doesn't break older mods, but a mod that calls it can't run on a Horizon from before it. So every
function the game offers mods has an **API level**, the one it arrived at (`crates/horizon-mods/src/api_level.rs`):
level 0 is the contract of Horizon 0.2.67 to 0.2.69, level 1 adds `render.set-minimap` and server mods' `players`. A mod needs the level of
the newest function it imports (a component imports only the functions it calls). The level is read from the file,
never declared:
- the game refuses a mod that needs a function it doesn't have, with "made for a newer Horizon (it uses
render.set-minimap): update Horizon to run it", instead of a linking error;
- the registry's CI writes each mod version's level into the index (`api_level`), and installers take the newest
version the running game can run.
A pull request that adds a function lists it in `api_level.rs` at the next level and raises `LEVEL`; the
`api_reference` test fails until it does.
## Stability levels
Every part of an API has one level, shown in its reference and its surface page.
+1 -1
View File
@@ -51,7 +51,7 @@ file selects ~103.
| 34 | Multiplayer and LAN screens, friends (44) | `L/multiplayer.rs`, `G/lan_screen.rs`, `G/friends.rs`, `L/friends_screen.rs`, `crates/horizon-friends/`, `crates/horizon-relay/` | lan, world_options; `python3 tools/test_friends.py` |
| 35 | Dispensers (43) | `G/dispenser.rs`, `G/dispense_fixture.rs` (the harness side) | dispenser |
| 36 | Audio and music (38) | `L/audio.rs`, `L/sound_engine.rs`, `L/sound_events.rs`, `L/music.rs`, `G/music.rs`, `L/ambience.rs`, `G/subtitle_hud.rs` | sound_engine, sound_event, music, situational_music, ambient_sound, subtitle |
| 37 | Integrated server and levels (34) | `G/integrated.rs`, `G/levels.rs`, `G/server_travel.rs` | server_dimension_players, `lan_e2e --server-thread` (portals, levels, distance, lava) |
| 37 | Integrated server and levels (34) | `G/integrated.rs`, `G/levels.rs`, `G/server_travel.rs` | server_dimension_players, `lan_e2e` (server-thread by default; portals, levels, distance, lava) |
| 38 | Command parsing (34) | `L/command/` (`args.rs`, `selector.rs`, `tree_*.rs`, `vanilla.rs`) | command_parse (corpus `tools/command_parse_corpus.py`) |
| 39 | Benchmarks and telemetry (34) | `L/benchmark.rs`, `tools/perf_bench.py`, `L/telemetry.rs`, `crates/horizon-telemetry*/`, `crates/horizon-bench/` | `python3 -m unittest tests.test_perf_bench`; [benchmark.md](benchmark.md) |
| 40 | Explosions and TNT (31) | `G/explosion.rs`, `G/tnt.rs` | lan_e2e blast; no dedicated harness yet |
+16 -15
View File
@@ -504,21 +504,22 @@ Two cases the oracle fills in:
mod. Better Dungeons' small dungeons keep one chest and each other one with a fifth's
chance, two at most (the game's `horizon:count_cap`, counted per placement as the mod's
DungeonContext is): 12 chests in five small dungeons at seed 1234 become 6.
Lithostitched's `block_swap` (137 lists, all in ChoiceTheorem's Overhauled Village) is the
game's `horizon:block_swap`: the same map, with Block.withPropertiesOf's properties kept;
a target block the game does not have (most of them are Nature's Spirit's and Croptopia's)
leaves the block as it is, as the mod does. Lithostitched's `reference` (CTOV builds most
of its lists from others) has the referenced lists' processors written in its place; its
`apply_random`, `condition` and `set_block` are the game's `horizon:apply_random`,
`horizon:condition` and `horizon:set_block`, with Lithostitched's random (the world seed
plus the name's hash, at the block, piece or structure). In a CTOV plains village at seed
1234 the farms now grow carrots, potatoes and beetroot besides wheat, and 1,655
cobblestone blocks become paths, grass, mossy cobblestone and planks. No Lithostitched processor is
dropped now (130 before). CTOV's compat hooks for other mods inside them (its
`modular_compat`, 142 copies) run a list of their own only when the mod they name is
loaded: carry writes that list in when the mod is ported along and takes the hook out (a
no-op, as in the mod) when it is not. In ATM10 all 142 name Nature's Spirit, which the
pack does not have.
Lithostitched's processors (all in ChoiceTheorem's Overhauled Village in ATM10) are the
game's own types, read as the mod writes them. `block_swap` keeps the properties the new
block can have: a farm's ripe wheat becomes ripe carrots, but beetroot, whose age stops at
3, keeps its default; a target block the game does not have (most are Nature's Spirit's and
Croptopia's) leaves the block as it is. `reference` runs the lists it names, `condition`
tests the template's block or the world's, `apply_random` and `set_block` draw per block or
per piece as the data says. Lithostitched's own random is not known yet, so the shares of a
random choice are the Java pack's but the blocks chosen differ. Lists named by tag and state
providers other than simple and weighted ones are dropped and counted (ATM10 has none).
CTOV's compat hooks for other mods (its `modular_compat`, 30 of them in 17 lists) become a
`reference` when the mod they name is ported along, and are taken out (a no-op, as in the
mod) when it is not. In ATM10 they all name Nature's Spirit, which the pack does not have.
In Java, Lithostitched also lays out every jigsaw structure its own way (villages,
outposts, trial chambers; CTOV's villages are `lithostitched:jigsaw`, carried as vanilla's
jigsaw). Horizon keeps vanilla's layouts, so with Lithostitched in a pack, structure layouts
differ from the Java pack's for the same seed (their positions do not).
The processors that build outside their template are carried too: Repurposed's and
Moog's pillar processors become the game's `horizon:pillar` (a column from a placeholder
block down to the ground, with the processor list they run on each of its blocks written
+6
View File
@@ -533,6 +533,12 @@ so the same rustc and `Cargo.lock` give the same file.
release cycle, so existing mods keep working while authors move.
- The manifest's `api` names the line it needs (`0.1`); the game refuses a
line it does not serve with a clear message.
- Within a line, each function has an API level, the one it arrived at
([stability](api/stability.md#mod-api-levels)). A mod needs the level of
the newest function it calls, read from its file: an older Horizon refuses
it with "made for a newer Horizon: update Horizon", and the registry lists
the level so installers take a version the game can run.
`cargo horizon-mod check` prints it ("needs mod API level 1 or newer").
- Horizon follows Minecraft releases; the mod API does not have to. Vanilla
concepts mods see (dimensions, block and item ids) are the game's own ids,
so a version port changes the data, not the API.
+50
View File
@@ -111,3 +111,53 @@ and shrieker. They supplement the deterministic component comparisons.
## Reading vanilla
Besides the Vineflower sources in `.local/minecraft/<version>/sources/`, `tools/rea_vanilla.py` queries the shipped bytecode through REA (callers through lambdas, overloads, a second decompiler); see [decompilation status](decompilation-status.md#querying-the-shipped-bytecode-with-rea).
## Player-facing integration and deterministic fixtures
`python3 tools/lan_e2e.py` runs every LAN check with an integrated server thread,
the same path as normal single player. `python3 tools/test_friends.py --client
target/fast/horizon-client --relay target/fast/horizon-relay --baseline` covers
plain LAN, direct invitations, a local relay and ended invitations in that mode.
Both accept `--in-frame` for comparison and retain `--server-thread` for existing
callers. Run Linux games under `xvfb-run -a` when no display is available. Each
process gets scratch game, launcher, XDG data and config homes; telemetry, audio
and launcher self-update are disabled.
`lan_integration` and `friends_integration` register these suites with `parity.py`.
`friends_integration` (under a minute) is selected like any harness when the
drivers or game implementation change. `lan_integration` is the whole LAN suite
(~40 minutes on a loaded runner, and single checks flake under load), so
`tools/parity_hosts.json` lists it under `named_only`: no automatic selection
(`--changed`, `--failed`, `--flake-check`, CI, parity-main) picks it; it runs
when named and in the nightly audit (`--all`). LAN writes `results.json`
with the host mode and each result beside its screenshots and logs. A skipped
case is recorded explicitly and does not count as passing coverage.
The game-binary fixture audit keeps these complementary direct rule tests:
- `command_harness.rs`: command feedback, styled spans and result callbacks,
held player position, explicit ticks and portal completion matching
`VanillaCommandExecOracle`. LAN `commands`, `feedback`, `styles`, `levels` and
`portals` cover command input, packet feedback and travel across the boundary.
- `dispense_fixture.rs`: seeded synthetic worlds, block/event traces, item
use/mining and combat hooks matching `VanillaDispenserOracle`. These isolate
rule results and RNG order; LAN machines, crafting, clicks, fireball, mining
and combat exercise real input and server handling separately. The new
`dispenser_input_host` / `dispenser_input_guest` checks press a button and
verify authoritative inventory consumption and exactly one drop on both clients.
- `sculk_fixture.rs`: scripted events and exact per-tick sculk/warden traces
matching `VanillaSculkOracle`; introducing wall-clock network ticks would
destroy that deterministic oracle contract. `sculk_input_host` and
`sculk_input_guest` separately exercise real footsteps reaching a sensor;
failures remain visible rather than changing the oracle trace.
- `net_harness.rs`: already a real network client of a dedicated server; keep
its handshake, world loading, chat and command packet assertions.
Library probes, `lan_bot`'s protocol hub fixture, generation/save tests and
streaming/mesh tests remain fast deterministic coverage. They do not become
full game launches. Setup and authoritative reads use the owning server;
interactions stay on the client. `server-hold on|off` provides a prediction-test
barrier between server ticks, including player packet handling, while transport
threads and control calls remain responsive. `stall MILLISECONDS` stalls only
the window to test recovery from a hung frame without process signals interrupting
socket reads. Neither mechanism changes normal gameplay.
+16
View File
@@ -2586,6 +2586,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
dependencies = [
"foldhash 0.2.0",
"serde",
"serde_core",
]
[[package]]
@@ -2734,6 +2736,7 @@ dependencies = [
"serde_json",
"sha2 0.10.9",
"toml 1.1.8+spec-1.1.0",
"wasmparser",
"zip",
]
@@ -6604,6 +6607,19 @@ dependencies = [
"web-sys",
]
[[package]]
name = "wasmparser"
version = "0.254.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5ff8685b587722894b6f25958fc608d315b93b8d23647bfb567664e7006d757"
dependencies = [
"bitflags 2.13.2",
"hashbrown 0.17.1",
"indexmap",
"semver",
"serde",
]
[[package]]
name = "wayland-backend"
version = "0.3.17"
+49
View File
@@ -422,6 +422,37 @@ pub fn version_dir(tag: &str) -> PathBuf {
const COMPLETE: &str = ".installed";
/// The mod API level of the game players run: the selected version's, else the newest installed
/// one's. None with no version installed: the launcher doesn't know yet which game will run mods.
pub fn game_mod_api_level() -> Option<u32> {
let installed = installed();
let tag = Settings::load()
.selected
.filter(|s| installed.contains(s))
.or_else(|| installed.into_iter().next())?;
Some(mod_api_level(&tag))
}
/// A game version's mod API level, read once from the mark its program carries
/// (`horizon_mods::api_level::MARK`). A program from before the mark serves level 0, the
/// contract of Horizon 0.2.67 and 0.2.68.
pub fn mod_api_level(tag: &str) -> u32 {
static LEVELS: Mutex<Vec<(String, u32)>> = Mutex::new(Vec::new());
let mut levels = LEVELS.lock().unwrap_or_else(|e| e.into_inner());
if let Some(&(_, level)) = levels.iter().find(|(t, _)| t == tag) {
return level;
}
let level = program_mod_api_level(&version_dir(tag).join(CLIENT));
levels.push((tag.to_owned(), level));
level
}
/// The mod API level in the game program at `path`: its mark's, else 0.
fn program_mod_api_level(path: &Path) -> u32 {
let program = fs::read(path).unwrap_or_default();
horizon_mods::api_level::read_mark(&program).unwrap_or(0)
}
/// Installed versions, newest tag first.
pub fn installed() -> Vec<String> {
installed_in(&versions_dir())
@@ -2557,6 +2588,24 @@ mod tests {
use super::*;
use std::cmp::Ordering::*;
/// The game program's mod API level is its mark's; a program from before the mark (0.2.68
/// and older) or none at all serves level 0.
#[test]
fn a_game_program_says_its_mod_api_level() {
let dir = std::env::temp_dir().join(format!("horizon-api-level-{}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
let marked = dir.join("marked");
let mut program = b"\x7fELF code code".to_vec();
program.extend_from_slice(b"horizon-mod-api-level:0007;");
fs::write(&marked, &program).unwrap();
let old = dir.join("old");
fs::write(&old, b"\x7fELF code without a mark").unwrap();
assert_eq!(program_mod_api_level(&marked), 7);
assert_eq!(program_mod_api_level(&old), 0);
assert_eq!(program_mod_api_level(&dir.join("missing")), 0);
fs::remove_dir_all(&dir).unwrap();
}
/// A preparation that reuses the imported game files still reads the assets from the
/// launcher that has them: a Prism Launcher instance's are in the launcher root, linked into
/// the official layout (jan.sonki on Discord: silent, grey title screen since 0.2.48).
+44 -17
View File
@@ -168,7 +168,14 @@ fn project_of(p: &index::Project) -> Option<Project> {
})
}
fn version_of(index: &Index, p: &index::Project, v: &index::Version) -> Version {
/// Version `v` of `p` as the browser shows it; blocked when taken down, or when it needs a newer
/// mod API level than `level`, the game's.
fn version_of(
index: &Index,
p: &index::Project,
v: &index::Version,
level: Option<u32>,
) -> Version {
Version {
id: v.version.clone(),
number: v.version.clone(),
@@ -187,7 +194,8 @@ fn version_of(index: &Index, p: &index::Project, v: &index::Version) -> Version
required: v.requires.iter().map(|r| r.id.clone()).collect(),
blocked: index
.revoked(&p.id, &v.file.sha256)
.map(|r| format!("taken down: {}", r.reason)),
.map(|r| format!("taken down: {}", r.reason))
.or_else(|| needs_newer(v, level).then(|| NEEDS_NEWER.to_string())),
}
}
@@ -254,9 +262,10 @@ impl Source for Registry {
let p = index
.project(&project.id)
.ok_or_else(|| format!("{} isn't in Horizon's registry.", project.title))?;
let level = crate::backend::game_mod_api_level();
Ok(p.versions
.iter()
.map(|v| version_of(&index, p, v))
.map(|v| version_of(&index, p, v, level))
.collect())
}
@@ -378,20 +387,24 @@ fn relative(kind: Kind, id: &str, file_name: &str) -> Result<String, String> {
}
}
/// Installs a registry project into the game folder `home`: its newest version for this game,
/// downloaded into `mod-browser/downloads/`, checked against the index, then moved into place.
/// A mod is `horizon-mods/<id>.hmod`, turned on in `horizon-mods.txt`.
/// Installs a registry project into the game folder `home`: its newest version for this game that
/// the game the player runs can load, downloaded into `mod-browser/downloads/`, checked against
/// the index, then moved into place. A mod is `horizon-mods/<id>.hmod`, turned on in
/// `horizon-mods.txt`.
pub fn install(
project: &Project,
home: &Path,
progress: &dyn Fn(&str, f32),
) -> Result<Vec<installed::Entry>, String> {
install_from(&*index()?, local(), project, home, progress)
let level = crate::backend::game_mod_api_level();
install_from(&*index()?, local(), level, project, home, progress)
}
/// [`install`] from `index`, for a game serving mod API `level`.
fn install_from(
index: &Index,
local: bool,
level: Option<u32>,
project: &Project,
home: &Path,
progress: &dyn Fn(&str, f32),
@@ -399,27 +412,41 @@ fn install_from(
let p = index
.project(&project.id)
.ok_or_else(|| format!("{} isn't in Horizon's registry.", project.title))?;
let v = newest(index, p)?;
let v = newest(index, p, level)?;
install_version(index, local, p, v, home, progress).map(|e| vec![e])
}
/// A project's newest version for this game that wasn't taken down.
fn newest<'a>(index: &Index, p: &'a index::Project) -> Result<&'a index::Version, String> {
let mut taken_down = None;
/// A project's newest version for this game that wasn't taken down and that a game serving mod
/// API `level` runs (any, when the level isn't known).
fn newest<'a>(
index: &Index,
p: &'a index::Project,
level: Option<u32>,
) -> Result<&'a index::Version, String> {
let (mut taken_down, mut too_new) = (None, false);
for v in p.versions.iter().filter(|v| v.game == GAME) {
match index.revoked(&p.id, &v.file.sha256) {
None => return Ok(v),
Some(r) => {
taken_down.get_or_insert(r);
}
if let Some(r) = index.revoked(&p.id, &v.file.sha256) {
taken_down.get_or_insert(r);
} else if needs_newer(v, level) {
too_new = true;
} else {
return Ok(v);
}
}
Err(match taken_down {
_ if too_new => format!("{} {NEEDS_NEWER}: update it first.", p.name),
Some(r) => format!("{} was taken down: {}", p.name, r.reason),
None => format!("{} has no version for {GAME} to install.", p.name),
})
}
const NEEDS_NEWER: &str = "needs a newer Horizon than the game you play";
/// Whether version `v` calls mod API functions a game serving `level` doesn't have.
fn needs_newer(v: &index::Version, level: Option<u32>) -> bool {
matches!((v.api_level, level), (Some(needed), Some(level)) if needed > level)
}
/// Installs version `v` of the registry's mod, resource pack or shader pack `p` into the game
/// folder `home`.
fn install_version(
@@ -462,7 +489,7 @@ fn install_version(
return Err(format!("{relative} is already there: move it away first."));
}
let line = format!("{} {}", p.name, v.version);
let file = &version_of(index, p, v).files[0];
let file = &version_of(index, p, v, None).files[0];
let staging = home.join("mod-browser/downloads");
let path = fetch(file, &staging, local, &|f| progress(&line, f))?;
let trust = index::check_file(index, p, v, &path).inspect_err(|_| {
+1 -1
View File
@@ -84,7 +84,7 @@ fn install_in(
if p.kind != idx::Kind::Modpack {
return Err(format!("{} is a {}, not a modpack.", p.name, p.kind.name()));
}
let v = newest(index, p)?;
let v = newest(index, p, None)?;
let work = dir.join(&p.id);
let fresh = !work.exists();
let result = install_version_in(index, local, p, v, &work, progress);
+27 -9
View File
@@ -59,7 +59,7 @@ fn installs_checked_turns_on_and_removes() {
let index = sample();
let home = scratch("install");
let p = hud(&index);
let done = install_from(&index, true, &p, &home, &|_, _| {}).unwrap();
let done = install_from(&index, true, None, &p, &home, &|_, _| {}).unwrap();
assert_eq!(done[0].file, "horizon-mods/coords_hud.hmod");
assert_eq!(done[0].verdict.as_deref(), Some("Verified"));
assert!(done[0].checksum.starts_with("SHA-256:e1135345"));
@@ -73,7 +73,7 @@ fn installs_checked_turns_on_and_removes() {
0
);
// Installed already: nothing to do.
let again = install_from(&index, true, &p, &home, &|_, _| {}).unwrap();
let again = install_from(&index, true, None, &p, &home, &|_, _| {}).unwrap();
assert_eq!(again, done);
let mut revoked = index.clone();
@@ -102,7 +102,7 @@ fn installs_a_shader_pack_into_shaderpacks() {
let home = scratch("shader");
let p = project_of(index.project("glow").unwrap()).unwrap();
assert_eq!(p.support(), super::super::Support::Works);
let done = install_from(&index, true, &p, &home, &|_, _| {}).unwrap();
let done = install_from(&index, true, None, &p, &home, &|_, _| {}).unwrap();
assert_eq!(done[0].file, "shaderpacks/glow-1.0.0.hshader");
assert_eq!(done[0].kind, Kind::Shader);
assert!(home.join("shaderpacks/glow-1.0.0.hshader").is_file());
@@ -123,7 +123,7 @@ fn installs_a_shader_pack_into_shaderpacks() {
assert!(!home.join("shaderpacks/glow-1.0.0.hshader").exists());
// A file of the same name the registry didn't put there is left alone.
fs::write(home.join("shaderpacks/glow-1.0.0.hshader"), b"mine").unwrap();
let e = install_from(&index, true, &p, &home, &|_, _| {}).unwrap_err();
let e = install_from(&index, true, None, &p, &home, &|_, _| {}).unwrap_err();
assert_eq!(
e,
"shaderpacks/glow-1.0.0.hshader is already there: move it away first."
@@ -140,15 +140,15 @@ fn refuses_a_changed_file_a_taken_id_and_local_files_from_the_real_registry() {
let mut tampered = index.clone();
let cozy = tampered.projects[1].versions[0].file.url.clone();
tampered.projects[0].versions[0].file.url = cozy;
let e = install_from(&tampered, true, &p, &home, &|_, _| {}).unwrap_err();
let e = install_from(&tampered, true, None, &p, &home, &|_, _| {}).unwrap_err();
assert!(e.contains("larger than its source says"), "{e}");
assert!(!home.join("horizon-mods").exists());
let e = install_from(&index, false, &p, &home, &|_, _| {}).unwrap_err();
let e = install_from(&index, false, None, &p, &home, &|_, _| {}).unwrap_err();
assert!(e.contains("file servers"), "{e}");
fs::create_dir_all(home.join("horizon-mods/coords_hud")).unwrap();
let e = install_from(&index, true, &p, &home, &|_, _| {}).unwrap_err();
let e = install_from(&index, true, None, &p, &home, &|_, _| {}).unwrap_err();
assert!(e.contains("already in horizon-mods"), "{e}");
let mut taken_down = index.clone();
@@ -158,7 +158,7 @@ fn refuses_a_changed_file_a_taken_id_and_local_files_from_the_real_registry() {
reason: "Test.".into(),
date: String::new(),
});
let e = install_from(&taken_down, true, &p, &home, &|_, _| {}).unwrap_err();
let e = install_from(&taken_down, true, None, &p, &home, &|_, _| {}).unwrap_err();
assert_eq!(e, "Coordinates HUD was taken down: Test.");
fs::remove_dir_all(&home).unwrap();
}
@@ -175,7 +175,7 @@ fn registry_versions_run_when_made_for_this_game() {
let versions: Vec<Version> = p
.versions
.iter()
.map(|x| version_of(&index, p, x))
.map(|x| version_of(&index, p, x, None))
.collect();
assert_eq!(pick(Kind::Mod, &versions, false).unwrap().number, "0.1.0");
}
@@ -198,3 +198,21 @@ fn owner_links_only_take_github_logins() {
assert!(!github_login(bad), "{bad}");
}
}
/// A version that calls mod API functions the game doesn't have is marked and skipped.
#[test]
fn versions_for_a_newer_horizon_are_not_installed() {
let mut index = sample();
index.projects[0].versions[0].api_level = Some(1);
let p = index.project("coords_hud").unwrap();
let e = newest(&index, p, Some(0)).unwrap_err();
assert_eq!(
e,
"Coordinates HUD needs a newer Horizon than the game you play: update it first."
);
assert!(newest(&index, p, Some(1)).is_ok());
assert!(newest(&index, p, None).is_ok());
let v = version_of(&index, p, &p.versions[0], Some(0));
assert_eq!(v.blocked.as_deref(), Some(NEEDS_NEWER));
assert_eq!(version_of(&index, p, &p.versions[0], Some(1)).blocked, None);
}
@@ -10,6 +10,8 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GlyphSource;
import net.minecraft.client.gui.font.glyphs.BakedGlyph;
@@ -31,9 +33,13 @@ import net.minecraft.util.RandomSource;
*/
public final class ParityFont {
public static ResourceManager resources() throws Exception {
return resources(Path.of("/nonexistent-horizon-assets"));
}
public static ResourceManager resources(Path assets) throws Exception {
var method = net.minecraft.client.resources.ClientPackSource.class.getDeclaredMethod("createVanillaPackSource", Path.class);
method.setAccessible(true);
var pack = (net.minecraft.server.packs.VanillaPackResources) method.invoke(null, Path.of("/nonexistent-horizon-assets"));
var pack = (net.minecraft.server.packs.VanillaPackResources) method.invoke(null, assets);
return pack.asResourceManager();
}
@@ -50,6 +56,38 @@ public final class ParityFont {
}
}
}
return create(providers);
}
/** The full default font, including references and the external Unihex fallback. */
public static Font createDefault(ResourceManager resources) throws Exception {
List<GlyphProvider> providers = new ArrayList<>();
loadProviders(resources, Identifier.withDefaultNamespace("default"), providers, new HashSet<>());
return create(providers);
}
private static void loadProviders(ResourceManager resources, Identifier font, List<GlyphProvider> providers,
Set<Identifier> visiting) throws Exception {
if (!visiting.add(font)) throw new IllegalArgumentException("Cyclic font reference: " + font);
var file = font.withPath("font/" + font.getPath() + ".json");
try (var reader = new InputStreamReader(resources.open(file), StandardCharsets.UTF_8)) {
var json = JsonParser.parseReader(reader).getAsJsonObject();
for (var element : json.getAsJsonArray("providers")) {
var conditional = GlyphProviderDefinition.Conditional.CODEC.parse(JsonOps.INSTANCE, element).getOrThrow();
if (!conditional.filter().apply(Set.of())) continue;
var definition = conditional.definition().unpack();
if (definition.left().isPresent()) {
providers.add(definition.left().orElseThrow().load(resources));
} else {
loadProviders(resources, definition.right().orElseThrow().id(), providers, visiting);
}
}
} finally {
visiting.remove(font);
}
}
private static Font create(List<GlyphProvider> providers) {
GlyphSource source = new GlyphSource() {
@Override
public BakedGlyph getGlyph(int codepoint) {
@@ -370,7 +370,7 @@ public final class VanillaPackScreenOracle {
unsafeField.setAccessible(true);
unsafe = unsafeField.get(null);
allocate = unsafe.getClass().getMethod("allocateInstance", Class.class);
font = ParityFont.create(ParityFont.resources());
font = ParityFont.createDefault(ParityFont.resources(Path.of(args[2])));
Minecraft mc = alloc(Minecraft.class);
put(mc, Minecraft.class, "font", font);
put(mc, Minecraft.class, "lastInputType", InputType.MOUSE);
@@ -230,7 +230,7 @@ public final class VanillaTabListOracle {
unsafeField.setAccessible(true);
unsafe = unsafeField.get(null);
allocate = unsafe.getClass().getMethod("allocateInstance", Class.class);
Font font = ParityFont.create(ParityFont.resources());
Font font = ParityFont.createDefault(ParityFont.resources(Path.of(args[2])));
var gson = new Gson();
var out = new StringBuilder();
for (String line : Files.readAllLines(Path.of(args[0]))) {
@@ -284,6 +284,8 @@ public final class VanillaTabListOracle {
put(mc, Minecraft.class, "user", user);
var skins = skinManager(c);
put(mc, Minecraft.class, "skinManager", skins);
var profiles = new ArrayList<GameProfile>();
var lookups = new ArrayList<CompletableFuture<?>>();
for (var p : c.getAsJsonArray("players")) {
var o = p.getAsJsonObject();
var uuid = UUID.fromString(o.get("uuid").getAsString());
@@ -296,12 +298,22 @@ public final class VanillaTabListOracle {
: new Property("textures", t.get("value").getAsString(), sig.getAsString()));
}
var profile = new GameProfile(uuid, o.get("name").getAsString(), new PropertyMap(properties));
// Let the lookup's unpacking and downloads run before the first frame,
// as they would have by the time a player list shows.
try {
skins.get(profile).get(1, java.util.concurrent.TimeUnit.SECONDS);
} catch (java.util.concurrent.TimeoutException pending) {
}
profiles.add(profile);
lookups.add(skins.get(profile));
}
// Start every lookup before waiting: intentionally pending fake
// downloads must not add a second for each player in the list.
// Keep the same observation window and leave those futures pending.
try {
CompletableFuture.allOf(lookups.toArray(CompletableFuture[]::new))
.get(1, java.util.concurrent.TimeUnit.SECONDS);
} catch (java.util.concurrent.TimeoutException pending) {
}
int profileIndex = 0;
for (var p : c.getAsJsonArray("players")) {
var o = p.getAsJsonObject();
var profile = profiles.get(profileIndex++);
var uuid = profile.id();
var info = new PlayerInfo(profile, false);
put(info, PlayerInfo.class, "gameMode", GameType.byId(o.get("gameMode").getAsInt()));
put(info, PlayerInfo.class, "latency", o.get("latency").getAsInt());
+1
View File
@@ -29,6 +29,7 @@ EXEMPT = {
"build_release.py": "builds and packs the release",
"release_train.py": "release bookkeeping",
"pr_ready.py": "runs cargo checks and parity.py",
"test_friends_integration_parity.py": "builds binaries; test_friends.Game isolates every game through integration_env",
"port_harness.py": "ports Java code, starts no game",
}
+50
View File
@@ -0,0 +1,50 @@
"""Automated game runs must never inherit a real player's data directories."""
import json
import os
from pathlib import Path
import sys
import tempfile
import unittest
from tools.integration_env import game_environment, run_game, require_server_reports
class IntegrationEnvironmentTests(unittest.TestCase):
def test_all_data_homes_and_network_settings_are_overridden(self):
keys = ('HORIZON_HOME', 'HORIZON_LAUNCHER_HOME', 'XDG_DATA_HOME', 'XDG_CONFIG_HOME')
with tempfile.TemporaryDirectory() as root:
env = game_environment({**dict.fromkeys(keys, '/real/player'), 'DISPLAY': ':123'}, root)
homes = [Path(env[key]) for key in keys]
self.assertEqual(len(set(homes)), 4)
self.assertTrue(all(p.is_dir() and p.is_relative_to(root) for p in homes))
self.assertEqual(env['DISPLAY'], ':123')
self.assertEqual(env['HORIZON_TELEMETRY_URL'], 'off')
self.assertEqual(env['HORIZON_LAUNCHER_NO_SELF_UPDATE'], '1')
self.assertEqual(env['HORIZON_NO_AUDIO'], '1')
def test_each_fixture_process_gets_fresh_homes_and_cleanup(self):
script = ('import json, os; from pathlib import Path; '
'homes = [os.environ[k] for k in '
'("HORIZON_HOME", "HORIZON_LAUNCHER_HOME", "XDG_DATA_HOME", "XDG_CONFIG_HOME")]; '
'assert all(Path(p).is_dir() for p in homes); print(json.dumps(homes))')
runs = [json.loads(run_game([sys.executable, '-c', script], env=os.environ,
capture_output=True, text=True, check=True).stdout) for _ in range(2)]
self.assertFalse(set(runs[0]) & set(runs[1]))
self.assertTrue(all(not Path(p).exists() for homes in runs for p in homes))
class PreparedReportTests(unittest.TestCase):
def test_partial_report_install_shadows_complete_fallback(self):
with tempfile.TemporaryDirectory() as root:
resources = Path(root) / 'horizon-client' / '26.3'
fallback = Path(root) / 'datagen' / 'out' / 'reports'
resources.mkdir(parents=True)
fallback.mkdir(parents=True)
for name in ('registries.json', 'commands.json'):
(fallback / name).write_text('{}')
self.assertEqual(require_server_reports(resources).resolve(), fallback.resolve())
(resources / 'registries.json').write_text('{}')
with self.assertRaisesRegex(ValueError, 'installed together'):
require_server_reports(resources)
(resources / 'commands.json').write_text('{}')
self.assertEqual(require_server_reports(resources), resources)
+5
View File
@@ -145,6 +145,11 @@ class ParityRunnerTests(unittest.TestCase):
with mock.patch.object(parity.sys, "platform", "darwin"):
self.assertEqual(parity.platform_skips(config), {"options": "libm"})
def test_named_only_harnesses_are_listed_from_the_host_config(self):
self.assertEqual(parity.named_only({"named_only": {"lan_integration": "slow"}}), {"lan_integration": "slow"})
self.assertEqual(parity.named_only({}), {})
self.assertIn("lan_integration", parity.named_only(parity.host_config()))
def test_a_pass_from_another_worktree_counts_only_for_the_same_content(self):
with tempfile.TemporaryDirectory() as temp:
elsewhere, here, shared = Path(temp, "a"), Path(temp, "b"), Path(temp, "shared")
+2
View File
@@ -75,6 +75,8 @@ def game_env(screen, home=None):
extra['HORIZON_HOME'] = home
if sys.platform == 'darwin':
return lab.game_env(**extra)
if not shutil.which('vscreen') and os.environ.get('DISPLAY'):
return lab.game_env(**extra)
env = lab.game_env(VSCREEN=screen, **extra)
subprocess.run(['vscreen', 'up'], env=env, check=False, capture_output=True)
text = subprocess.run(['vscreen', 'env'], env=env, capture_output=True, text=True, check=True).stdout
+54
View File
@@ -0,0 +1,54 @@
"""Per-process scratch data for game integration tests (never a player's home)."""
from pathlib import Path
from contextlib import contextmanager
import os
import subprocess
import tempfile
try:
from . import lab
except ImportError:
import lab
def game_environment(base, root):
root = Path(root)
env = dict(base, HORIZON_TELEMETRY_URL='off')
for key, folder in [('HORIZON_HOME', 'home'), ('HORIZON_LAUNCHER_HOME', 'launcher'),
('XDG_DATA_HOME', 'data'), ('XDG_CONFIG_HOME', 'config')]:
path = root / folder
path.mkdir(parents=True, exist_ok=True)
env[key] = str(path)
return lab.game_env(env)
def run_game(command, **kwargs):
"""Run a headless game fixture with the same isolation as real-client tests."""
with tempfile.TemporaryDirectory(prefix='hz-fixture-') as root:
kwargs['env'] = game_environment(kwargs.pop('env', os.environ), root)
if any(key in kwargs['env'] for key in
('HORIZON_COMMAND_SCRIPT', 'HORIZON_DISPENSE_FIXTURE', 'HORIZON_SCULK_FIXTURE')):
# These compare seeded rule/callback traces, not the client/server boundary.
command = [*command, '--in-frame']
return subprocess.run(command, **kwargs)
@contextmanager
def suite_lock():
"""Real-client CI suites share a GPU, even when parity.py runs harnesses in parallel."""
import fcntl
with (Path(tempfile.gettempdir()) / 'horizon-integration-gpu.lock').open('a') as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
yield
def require_server_reports(resources):
"""Match Builtin.find: a registry report shadows later fallback directories."""
resources = Path(resources)
reports = next((p for p in (resources, resources / '../../datagen/out/reports',
resources / '../datagen/out/reports')
if (p / 'registries.json').is_file()), None)
if reports is None or not (reports / 'commands.json').is_file():
raise ValueError(f'incomplete prepared resources in {resources}: registries.json and '
'commands.json must be installed together; prepare the pinned resources again')
return reports
+246 -60
View File
@@ -1,7 +1,8 @@
#!/usr/bin/env python3
"""LAN multiplayer end to end: a host opens its world to LAN and a guest joins, both real games.
Two Horizon clients run side by side under horizon-ctl, each in its own scratch folder. The host
The host uses its integrated server thread by default; --in-frame compares the fallback, and
--server-thread remains accepted. Two Horizon clients run under horizon-ctl in scratch folders. The host
creates a survival world and opens it to LAN with commands allowed; the guest connects to it by
address. The script then checks, from both sides, what a guest's game used to get wrong (#366):
@@ -272,7 +273,6 @@ import math
import os
import re
import shutil
import signal
import subprocess
import sys
import tempfile
@@ -284,12 +284,21 @@ from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from agent_control_e2e import Cli, Failed, game_env # noqa: E402
import lab # noqa: E402
from integration_env import game_environment, require_server_reports # noqa: E402
ROOT = Path(__file__).resolve().parents[1]
GUEST_NAME = 'Guest'
# The host plays single player on its server thread (--server-thread).
SERVER_THREAD = False
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'styles', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'sidebar', 'border', 'particles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'doors', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'hands', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater', 'fireworks']
# Match normal single player; --in-frame is the comparison path.
SERVER_THREAD = True
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp',
'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'styles',
'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing',
'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push',
'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'sidebar', 'border', 'particles',
'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects',
'portals', 'distance', 'growth', 'doors', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'hands',
'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption',
'breath', 'lava', 'levels', 'drops', 'underwater', 'fireworks']
class Game:
@@ -302,6 +311,7 @@ class Game:
os.symlink(resources, self.work / '.local/horizon-client' / lab.VERSION)
self.socket = self.work / 'c.sock'
env = game_env(os.environ.get('VSCREEN', 'lan-e2e'), home=self.work / 'home')
env = game_environment(env, self.work)
self.d = Cli(ctl, self.socket, self.work, env)
self.d.name = name
@@ -327,6 +337,28 @@ class Game:
def wait(self, ticks):
self.d('wait', ticks=ticks)
def script(self, lines):
"""Keep tick-sensitive input in one ctl process, through the normal control commands."""
result = subprocess.run(self.d.base + ['script'], input='\n'.join(lines) + '\n',
cwd=self.work, env=self.d.env, capture_output=True, text=True,
timeout=600)
replies = [json.loads(line) for line in result.stdout.splitlines() if line.strip()]
ticks = [r['ticks'] for r in replies if 'ticks' in r]
print(f'[{self.name}] script {lines} -> {replies[-1] if replies else result.stderr[:400]}; ticks={ticks}')
assert result.returncode == 0 and replies and not any('error' in r for r in replies), result.stdout
return replies[-1]
def until(self, observe, predicate, ticks=100):
"""Wait for an observable answer, within the existing tick budget."""
last = observe()
for _ in range(ticks):
if predicate(last):
return last
self.wait(1)
last = observe()
assert predicate(last), f'{self.name}: state did not arrive within {ticks} ticks: {last}'
return last
def close(self):
try:
self.d('quit')
@@ -534,6 +566,8 @@ def check_sign(host, guest, out):
host.shot(out, 'sign')
assert placed and 'sign' in placed, f'the guest placed {placed}'
host_unaffected(host, "the guest's sign")
guest.raw('key', 'esc', 'tap')
guest.raw('wait-for', 'screen', 'playing')
return placed
@@ -678,7 +712,7 @@ def check_levels(host, guest, out):
for name, step in LEVEL_STEPS:
try:
steps[name] = step(host, guest, out)
except (Failed, AssertionError, StopIteration, KeyError) as e:
except (Failed, AssertionError, StopIteration, KeyError, TypeError, IndexError) as e:
steps[name], failed = repr(e), failed + [name]
for g in (host, guest):
back_to_game(g)
@@ -751,10 +785,12 @@ def level_ticking(host, guest, out):
has its own id across the levels."""
gx, gy, gz = pos_of(guest)
hx, hy, hz = pos_of(host)
# A floor of its own beside the host (earlier checks may have broken the open one there).
bx, by, bz = int(hx // 1), int(hy // 1), int(hz // 1)
host('command', command=f'fill {bx - 4} {by - 1} {bz - 4} {bx + 4} {by - 1} {bz - 2} minecraft:stone')
host('command', command=f'fill {bx - 4} {by} {bz - 4} {bx + 4} {by + 4} {bz - 2} minecraft:air')
# Clear the whole walking area in each level: normal pig AI can leave a
# narrow landing strip during the fall/observation window.
for game, x, y, z in ((host, hx, hy, hz), (guest, gx, gy, gz)):
bx, by, bz = int(x // 1), int(y // 1), int(z // 1)
game('command', command=f'fill {bx - 12} {by - 1} {bz - 12} {bx + 12} {by - 1} {bz + 12} minecraft:stone')
game('command', command=f'fill {bx - 12} {by} {bz - 12} {bx + 12} {by + 6} {bz + 12} minecraft:air')
guest('command', command=f'summon minecraft:pig {gx + 2.5:.2f} {gy + 3:.2f} {gz:.2f}')
guest('command', command=f'summon minecraft:armor_stand {gx - 2.5:.2f} {gy + 3:.2f} {gz:.2f}')
host('command', command=f'summon minecraft:pig {hx + 2.5:.2f} {hy + 3:.2f} {hz - 3:.2f}')
@@ -766,6 +802,7 @@ def level_ticking(host, guest, out):
mobs = [m for m in g.raw('mobs', 8)['near'] if m['kind'] == 'minecraft:pig']
stands = [o for o in g('status')['objects'] if o['kind'] == 'minecraft:armor_stand']
seen[name] = {'pigs': [(m['id'], round(m['pos'][1], 2)) for m in mobs],
'pig_positions': [m['pos'] for m in mobs],
'stands': [(o['id'], round(o['position'][1], 2)) for o in stands], 'floor': floor}
finally:
guest('command', command='kill @e[type=!minecraft:player,distance=..8]')
@@ -1993,12 +2030,11 @@ def chat_input(game):
def complete(guest, text):
"""Opens the chat on `/`, types `text` and presses Tab: the input after it."""
guest.raw('key', 'slash', 'tap')
guest.wait(3)
guest.until(lambda: guest('screen').get('screen'), lambda s: s == 'chat', ticks=3)
guest.raw('type', text)
guest.wait(2)
guest.until(lambda: guest('screen').get('chat_suggestions'), bool, ticks=2)
guest.raw('key', 'tab', 'tap')
guest.wait(2)
return chat_input(guest)
return guest.until(lambda: chat_input(guest), lambda value: value != '/' + text, ticks=2)
def check_commands(host, guest, out):
@@ -2408,7 +2444,8 @@ def check_predict(host, guest, out):
seen = {}
creaks = lambda game: sum(n for s, n in game('status')['heard'].items() if 'door' in s)
heard = {'guest': creaks(guest), 'host': creaks(host)}
os.kill(host.pid, signal.SIGSTOP)
host.raw('server-hold', 'on')
held_at = host.raw('simulation')['game_time']
try:
guest.raw('interact', *wood)
seen['guest_frozen'] = (state(guest, wood), state(guest, (wood[0], wood[1] + 1, wood[2])))
@@ -2416,8 +2453,9 @@ def check_predict(host, guest, out):
seen['guest_heard_frozen'] = creaks(guest) - heard['guest']
guest.raw('interact', *iron)
seen['guest_iron_frozen'] = state(guest, iron)
assert host.raw('simulation')['game_time'] == held_at, 'server barrier advanced the world'
finally:
os.kill(host.pid, signal.SIGCONT)
host.raw('server-hold', 'off')
guest.wait(20)
seen['guest_after'] = state(guest, wood)
seen['host_after'] = state(host, wood)
@@ -2529,7 +2567,11 @@ def check_swings(host, guest, out):
guest.wait(10)
guest.raw('select', 'minecraft:wooden_hoe')
before = guest('state')['swings']
guest.raw('interact', *soil)
guest.raw('look-at', soil[0] + 0.5, soil[1] + 1, soil[2] + 0.5)
guest.wait(1)
target = guest('state')['looking_at']['block']
assert target and target['pos'] == list(soil) and target['face'] == 'up', target
guest.raw('mouse', 'right', 'click')
guest.wait(10)
hoe = {'swings': guest('state')['swings'] - before, 'soil': host.raw('block', *soil)['block']}
guest('command', command=f'setblock {soil[0]} {soil[1]} {soil[2]} minecraft:stone')
@@ -2636,16 +2678,22 @@ def check_xp(host, guest, out):
x, y, z = pos_of(guest)
# Three orbs of 7 points 4 blocks east of the guest (out of reach, within the 8 blocks orbs
# follow from): they fly to the guest.
# Freeze setup so command/IPC latency cannot let all three be collected
# before the two-tick in-flight observation.
host('command', command='tick freeze')
for _ in range(3):
guest('command', command=f'summon minecraft:experience_orb {x + 4:.2f} {y + 0.5:.2f} {z:.2f} {{Value:7s}}')
host('command', command='tick step 2')
guest.wait(2)
seen = remote(guest).get('entities', {}).get('minecraft:experience_orb', 0)
guest.shot(out, 'xp-orbs')
host('command', command='tick unfreeze')
guest.wait(60)
xp = guest('state')['player']['xp']
host_xp = host('state')['player']['xp']
left = remote(guest).get('entities', {}).get('minecraft:experience_orb', 0)
finally:
host('command', command='tick unfreeze')
host('command', command='gamemode survival')
# Back where it stood: a spectator sinks (Player.travel keeps 0.6 of its fall speed each tick,
# about 0.2 blocks from standing), and a later check that makes it survival again would land
@@ -2671,9 +2719,7 @@ def check_time(host, guest, out):
guest.wait(20)
# A stalled game runs at most 10 missed ticks when it wakes (DeltaTracker); the server's
# game time, sent every second, moves the guest's clock on (ClientClockManager.tick).
os.kill(guest.pid, signal.SIGSTOP)
time.sleep(4)
os.kill(guest.pid, signal.SIGCONT)
guest.raw('stall', 4000)
guest.wait(60)
stalled = times()
detail = {'paused': paused, 'after_stall': stalled}
@@ -3175,12 +3221,10 @@ def same_light(host, guest, lo, hi, what, ticks=100):
def check_flight(host, guest, out):
def double_jump(game):
# LocalPlayer.aiStep: a second jump press within 7 ticks of the first toggles flight.
lines = []
for hold in (1, 4):
game.raw('key', 'space', 'down')
game.wait(hold)
game.raw('key', 'space', 'up')
game.wait(2)
game.wait(10)
lines += ['key space down', f'wait {hold}', 'key space up', 'wait 2']
game.script(lines + ['wait 10'])
return game('state')['player']
def set_mode(game, mode):
@@ -3195,6 +3239,9 @@ def check_flight(host, guest, out):
players = [('guest', guest)] + ([('host', host)] if SERVER_THREAD else [])
seen = {}
# The rule is seven simulation ticks, independent of wall time. A slow
# server clock leaves enough time to deliver the key events even on busy CI.
host('command', command='tick rate 4')
try:
for name, game in players:
set_mode(game, 'creative')
@@ -3208,6 +3255,7 @@ def check_flight(host, guest, out):
'stayed': round(later['position'][1] - up['position'][1], 2),
'survival_flying': fell['flying']}
finally:
host('command', command='tick rate 20')
for _, game in players:
game('command', command='gamemode survival')
for name, s in seen.items():
@@ -3722,15 +3770,16 @@ def leave_vehicle(player, kind):
def break_vehicle(player, vehicle_id, kind):
"""Hit it with fists until it breaks: VehicleEntity.tick takes 1 off its damage every tick."""
for _ in range(10):
here = [o for o in player('status')['objects'] if o['id'] == vehicle_id]
if not here:
return
vp = here[0]['position']
player.raw('look-at', f'{vp[0]:.2f}', f'{vp[1] + 0.3:.2f}', f'{vp[2]:.2f}')
player.raw('mouse', 'left', 'click')
player.wait(2)
raise AssertionError(f'ten hits left the {kind} in place')
here = [o for o in player('status')['objects'] if o['id'] == vehicle_id]
if not here:
return
vp = here[0]['position']
# Keep the original ten hits/two-tick spacing in one connection. Starting
# several ctl processes per hit lets the vehicle's damage decay between hits.
player.script([f'look-at {vp[0]:.2f} {vp[1] + 0.3:.2f} {vp[2]:.2f}',
*[line for _ in range(10) for line in ('mouse left click', 'wait 2')]])
assert not any(o['id'] == vehicle_id for o in player('status')['objects']), \
f'ten hits left the {kind} in place'
def check_boats(host, guest, out):
@@ -4387,10 +4436,8 @@ def check_deflect(host, guest, out):
(Player.attack: AIM_DEFLECT on #redirectable_projectile; Ghast.isReflectedFireball). The ghast is caged
(a window toward the host); the game runs at 4 ticks a second, and once the fireball is within 4.5 blocks
the host looks at the ghast and clicks."""
if SERVER_THREAD:
# The fireball's position and the host's hurts are read from the host's own level.
return 'skipped (in-frame only)'
x, y, z = (int(v // 1) for v in pos_of(host))
back = pos_of(host)
x, y, z = (int(v // 1) for v in back)
guest('command', command='gamemode creative')
host('command', command='gamemode survival')
host('command', command='difficulty normal')
@@ -4411,23 +4458,36 @@ def check_deflect(host, guest, out):
try:
deadline = time.monotonic() + 120
while time.monotonic() < deadline:
balls = [q for q in host.raw('status')['projectiles'] if q['kind'] == 'LargeFireball']
# Aim and click at the projectile the client actually sees. Holding
# non-player simulation keeps the reach window open while ordinary
# player input and its attack packet cross the connection.
balls = [q for q in host('status')['projectiles'] if q['kind'] == 'LargeFireball']
near = min((math.dist(q['position'], eye) for q in balls), default=None)
if near is not None and near < 4.5:
ball = min(balls, key=lambda q: math.dist(q['position'], eye))
host('command', command='tick freeze')
held_tick = host.raw('simulation')['game_time']
g = [m['pos'] for m in host.raw('mobs', 60).get('near', []) if m['kind'].endswith('ghast')][0]
host.raw('look-at', f'{g[0]:.2f}', f'{g[1] + 2:.2f}', f'{g[2]:.2f}')
host.raw('mouse', 'left', 'down')
host.raw('mouse', 'left', 'up')
host.script([f'look-at {g[0]:.2f} {g[1] + 2:.2f} {g[2]:.2f}',
'wait 1', 'mouse left click', 'wait 1'])
host.until(lambda: host.raw('simulation')['projectiles'],
lambda qs: any(q.get('id') == ball['id'] and q.get('player_owned') for q in qs),
ticks=60)
assert host.raw('simulation')['game_time'] == held_tick, 'the deflection barrier advanced simulation'
host('command', command='tick unfreeze')
turned = near
break
host('command', command='tick rate 20')
host.wait(60)
ghast = [m for m in host.raw('mobs', 60).get('near', []) if m['kind'].endswith('ghast') and m['health'] > 0]
hurt = [h for pl in host.raw('hurtlog')['players'] if pl['name'] == 'host' for h in pl['hurts']
hurt = [h for pl in host.raw('hurtlog')['players'] if pl['name'] == (host.player if SERVER_THREAD else 'host') for h in pl['hurts']
if 'fireball' in h['source'] or 'explosion' in h['source']]
finally:
host('command', command='tick unfreeze')
host('command', command='tick rate 20')
host('command', command='kill @e[type=minecraft:ghast]')
host('command', command=f'tp @s {back[0]} {back[1]} {back[2]}')
host.until(lambda: pos_of(host), lambda p: math.dist(p, back) < 0.1)
host('command', command=f'fill {gx - 3} {gy - 1} {gz - 3} {gx + 24} {gy + 8} {gz + 3} minecraft:air')
guest('command', command='gamemode survival')
assert turned is not None, f'no fireball came within 4.5 blocks (nearest {near})'
@@ -4613,8 +4673,22 @@ def run(ctl, out, resources, checks):
guest.shot(out, 'joined')
for name in checks:
try:
results[name] = {'pass': True, 'detail': CHECK_FNS[name](host, guest, out)}
except (Failed, AssertionError, StopIteration, KeyError) as e:
# Cases assert their own deaths, menus and held actions before returning.
# Do not let those states turn later independent cases into false failures.
for game in (host, guest):
game.raw('release')
back_to_game(game)
state = game('state')
assert state.get('player') is not None, f'{game.name} lost its session before {name}: {state}'
if state.get('screen') == 'dead' or state['player']['health'] <= 0:
game.raw('respawn')
game.until(lambda: game('state'),
lambda s: s.get('screen') == 'playing' and s['player']['health'] > 0,
ticks=40)
detail = CHECK_FNS[name](host, guest, out)
skipped = isinstance(detail, str) and detail.startswith('skipped')
results[name] = {'pass': not skipped, 'skipped': skipped, 'detail': detail}
except (Failed, AssertionError, StopIteration, KeyError, TypeError, IndexError) as e:
results[name] = {'pass': False, 'detail': repr(e)}
for g in (host, guest):
back_to_game(g)
@@ -4633,14 +4707,19 @@ def main():
parser.add_argument('--bin-dir', help='prebuilt horizon-client and horizon-ctl (default: build with the fast profile)')
parser.add_argument('--check', action='append', choices=CHECKS, help='run only these checks (repeatable)')
parser.add_argument('--out', default=str(ROOT / '.local/lan-e2e'), help='screenshots and game logs go here')
parser.add_argument('--server-thread', action='store_true', help="the host's world runs on its server thread")
mode = parser.add_mutually_exclusive_group()
mode.add_argument('--server-thread', dest='server_thread', action='store_true', default=True,
help='host on its server thread (default; retained for existing callers)')
mode.add_argument('--in-frame', dest='server_thread', action='store_false',
help='host the world in the window, for comparison')
args = parser.parse_args()
global SERVER_THREAD
SERVER_THREAD = args.server_thread
if args.bin_dir:
bins = Path(args.bin_dir).resolve()
else:
subprocess.run(['cargo', 'build', '--locked', '--profile', 'fast', '-p', 'horizon-client', '--bins'],
subprocess.run(['cargo', 'build', '--locked', '--profile', 'fast', '-p', 'horizon-client',
'--bin', 'horizon-client', '--bin', 'horizon-ctl'],
cwd=ROOT, check=True)
meta = json.loads(subprocess.run(['cargo', 'metadata', '--format-version', '1', '--no-deps'], cwd=ROOT,
capture_output=True, text=True, check=True).stdout)
@@ -4648,9 +4727,16 @@ def main():
resources = lab.RESOURCES.resolve()
if not (resources / 'horizon-resources.json').exists():
sys.exit(f'no prepared resources in {resources}')
out = Path(args.out)
try:
require_server_reports(resources)
except ValueError as error:
sys.exit(str(error))
out = Path(args.out).resolve()
out.mkdir(parents=True, exist_ok=True)
print(f'LAN host mode: {"server-thread" if SERVER_THREAD else "in-frame"}')
results = run(bins / 'horizon-ctl', out, resources, args.check or CHECKS)
(out / 'results.json').write_text(json.dumps({'mode': 'server-thread' if SERVER_THREAD else 'in-frame',
'checks': results}, indent=2) + '\n')
print(json.dumps(results, indent=1))
sys.exit(0 if results and all(r['pass'] for r in results.values()) else 1)
@@ -4707,16 +4793,33 @@ def check_keep_inventory(host, guest, out):
neither; with it on, nothing drops and it respawns with both (#1284)."""
hx, hy, hz = (int(v // 1) for v in pos_of(host))
seen = {}
# Totem coverage leaves both players near one health. Protect the waiting
# player from residual fire/damage while the other player's death is tested.
for game in (host, guest):
game('command', command='gamemode creative')
game('command', command='effect clear @s')
game('command', command='effect give @s minecraft:instant_health 1 5 true')
host('command', command=f'setworldspawn {hx} {hy} {hz}')
host('command', command='gamerule respawn_radius 0')
# Keep the inactive player alive and away from the death drops. Earlier
# fixtures may leave a personal respawn point or an unsafe waiting position.
host('command', command=f'fill {hx - 6} {hy - 1} {hz - 6} {hx + 6} {hy - 1} {hz + 6} minecraft:stone')
host('command', command=f'fill {hx - 6} {hy} {hz - 6} {hx + 6} {hy + 3} {hz + 6} minecraft:air')
for game, dz in ((host, -3), (guest, 3)):
waiting = (hx - 3.5, hy, hz + dz + 0.5)
game('command', command=f'spawnpoint @s {hx} {hy} {hz}')
game('command', command=f'tp @s {waiting[0]} {waiting[1]} {waiting[2]}')
game.until(lambda: pos_of(game), lambda p: math.dist(p, waiting) < 0.1)
try:
for keep in ('false', 'true'):
host('command', command=f'gamerule keep_inventory {keep}')
for game, dz in ((host, -3), (guest, 3)):
# 14 blocks from the world spawn: nothing dropped is picked up on respawning.
x, z = hx + 14, hz + dz
host('command', command=f'fill {x - 2} {hy - 1} {z - 1} {x + 2} {hy - 1} {z + 1} minecraft:stone')
host('command', command=f'fill {x - 2} {hy} {z - 1} {x + 2} {hy + 2} {z + 1} minecraft:air')
# Death drops scatter several blocks before landing. Keep the
# entire observation area solid so they cannot fall off a ledge.
host('command', command=f'fill {x - 6} {hy - 1} {z - 6} {x + 6} {hy - 1} {z + 6} minecraft:stone')
host('command', command=f'fill {x - 6} {hy} {z - 6} {x + 6} {hy + 3} {z + 6} minecraft:air')
host('command', command='kill @e[type=minecraft:item]')
game('command', command='gamemode survival')
game('command', command='clear @s')
@@ -4724,8 +4827,10 @@ def check_keep_inventory(host, guest, out):
game('command', command='item replace entity @s armor.head with minecraft:iron_helmet')
game('command', command=f'tp @s {x + 0.5} {hy} {z + 0.5}')
game.wait(20)
assert game('state').get('screen') == 'playing', f'{game.name} died during setup'
game('command', command='kill @s')
game.wait(30)
assert game('state').get('screen') == 'dead', f'{game.name} did not die'
dropped = sorted((e['item']['item'], e['item']['count'])
for e in game.raw('entities', 8, 'minecraft:item')['entities'])
game.shot(out, f'keepinv-{keep}')
@@ -4735,12 +4840,14 @@ def check_keep_inventory(host, guest, out):
seen[f'{game.name}-keep-{keep}'] = {
'dropped': dropped,
'kept': {item: server_count(game, item) for item in ('diamond', 'iron_helmet')}}
game('command', command='gamemode creative')
finally:
host('command', command='gamerule keep_inventory false')
host('command', command='gamerule respawn_radius 10')
host('command', command='kill @e[type=minecraft:item]')
for game in (host, guest):
game('command', command='clear @s')
game('command', command='gamemode survival')
face_each_other(host, guest)
for case, s in seen.items():
want = ({'dropped': [], 'kept': {'diamond': 7, 'iron_helmet': 1}} if case.endswith('true') else
@@ -4758,6 +4865,16 @@ def check_signs(host, guest, out):
seen = {}
for game, other, dz in ((host, guest, -2), (guest, host, 2)):
spot = (hx + 2, hy, hz + dz)
for viewer in (game, other):
back_to_game(viewer)
# Earlier death/travel checks leave the players at different spawn points.
# Give this placement case its own floor, line of sight and reachable target.
host('command', command=f'fill {spot[0] - 3} {hy - 1} {spot[2] - 1} {spot[0] + 3} {hy - 1} {spot[2] + 1} minecraft:stone')
host('command', command=f'fill {spot[0] - 3} {hy} {spot[2] - 1} {spot[0] + 3} {hy + 3} {spot[2] + 1} minecraft:air')
for viewer, dx in ((game, -2), (other, 2)):
at = (spot[0] + dx + 0.5, hy, spot[2] + 0.5)
viewer('command', command=f'tp @s {at[0]} {at[1]} {at[2]}')
viewer.until(lambda: pos_of(viewer), lambda p: math.dist(p, at) < 0.1)
game('command', command=f'setblock {spot[0]} {spot[1] - 1} {spot[2]} minecraft:stone')
game('command', command=f'setblock {spot[0]} {spot[1]} {spot[2]} minecraft:air')
game('command', command='gamemode survival')
@@ -4765,7 +4882,12 @@ def check_signs(host, guest, out):
game('command', command='give @s minecraft:oak_sign 1')
game.wait(10)
game.raw('place', *spot, '--item', 'minecraft:oak_sign')
game.wait(10)
game.until(lambda: game('state').get('screen'), lambda s: s == 'sign', ticks=10)
# Drain the placement's initial block-entity data before typing. The
# reply travels on the same connection after that data, unlike a local
# screen check which may see the editor before the initial data arrives.
server_reply(game, f'data get block {spot[0]} {spot[1]} {spot[2]} front_text.messages',
'following block data')
opened = {g.name: g('state').get('screen') for g in (game, other)}
game.shot(out, 'signs-editor')
lines = [f'{game.name} wrote', 'line two']
@@ -4773,11 +4895,13 @@ def check_signs(host, guest, out):
game.raw('key', 'enter', 'tap')
game.raw('type', lines[1])
game.raw('key', 'esc', 'tap')
game.wait(20)
game.until(lambda: game('state').get('screen'), lambda s: s == 'playing', ticks=20)
back_to_game(other)
shown = {g.name: g.until(lambda: (g.raw('block', *spot).get('sign') or {}).get('front'),
lambda front: front == lines + ['', ''], ticks=20)
for g in (game, other)}
seen[game.name] = {'opened': opened,
'shown': {g.name: (g.raw('block', *spot).get('sign') or {}).get('front')
for g in (game, other)}}
'shown': shown}
other.shot(out, 'signs-shown')
for name, s in seen.items():
other = 'guest' if name == 'host' else 'host'
@@ -5128,8 +5252,12 @@ def check_fishing(host, guest, out):
tracked to the clients as a fishing bobber under its owner), lands on the ground and reels back
in; cast into open water with Lure III, a fish bites and reeling in
during the bite catches it for 1 (#1348)."""
# Both players need a safe floor for the entire other player's fishing wait.
apart(host, guest)
hx, hy, hz = (int(v // 1) for v in pos_of(host))
host('command', command='weather clear')
for game in (host, guest):
game('command', command='effect give @s minecraft:instant_health 1 5 true')
seen = {}
try:
for game, other, dz in ((host, guest, -5), (guest, host, 5)):
@@ -5445,12 +5573,8 @@ def check_digging(host, guest, out):
before = heard('minecraft:block.stone.break')
guest.raw('look-at', *at(stone))
guest.raw('mouse', 'left', 'down')
time.sleep(0.4)
os.kill(guest.pid, signal.SIGSTOP)
try:
time.sleep(1.0)
finally:
os.kill(guest.pid, signal.SIGCONT)
guest.wait(8)
guest.raw('stall', 1000)
resumed = {'guest': guest.raw('block', *stone).get('block'), 'server': host.raw('block', *stone).get('block'),
'mining': guest('state').get('mining')}
# Let go once it breaks: past it the crosshair reaches the floor.
@@ -5646,6 +5770,68 @@ CHECKS += ['knockback']
CHECK_FNS.update(knockback=check_knockback)
def check_fixture_input(host, guest, out, kind, role):
"""Complement seeded rules with real client input and authoritative observations."""
player = host if role == 'host' else guest
hx, y, hz = (int(v // 1) for v in pos_of(host))
x, z = hx + 6, hz + (-8 if role == 'host' else 8)
sensor, dispenser, button = (x + 3, y, z + 1), (x, y, z + 3), (x, y, z + 2)
player('command', command='gamemode survival')
player('command', command='item replace entity @s weapon.mainhand with minecraft:air')
host('command', command=f'fill {x - 2} {y - 1} {z - 2} {x + 5} {y - 1} {z + 5} minecraft:stone')
host('command', command=f'fill {x - 2} {y} {z - 2} {x + 5} {y + 3} {z + 5} minecraft:air')
start = (x + 0.5, y, z + 0.5)
player('command', command=f'tp @s {start[0]} {start[1]} {start[2]} 0 0')
player.until(lambda: pos_of(player), lambda p: math.dist(p, start) < 0.1)
player.raw('look', 0, 0)
try:
if kind == 'sculk':
player.wait(2) # finish setup movement before installing the listener
host.raw('setblock', *sensor, 'minecraft:sculk_sensor')
phase = lambda: host.raw('getblock', *sensor)['block']
host.until(phase, lambda state: 'sculk_sensor_phase=inactive' in state)
player.wait(10)
assert 'sculk_sensor_phase=inactive' in phase(), 'setup activated the sensor before player input'
player.raw('key', 'w', 'down')
try:
player.wait(15) # defined input duration; no wall-clock simulation sleep
finally:
player.raw('key', 'w', 'up')
moved = {'client': math.dist(pos_of(player), start),
'server': math.dist(server_player(host, player)['position'], start)}
assert all(d > 1 for d in moved.values()), f'{role} did not walk on both sides: {moved}'
active = host.until(phase, lambda state: 'sculk_sensor_phase=active' in state, ticks=85)
for viewer in (host, guest):
viewer.until(lambda: state_of(viewer, sensor, 'sculk_sensor_phase'), lambda v: v == 'active')
return {'moved': moved, 'phase': active}
host.raw('setblock', *dispenser, 'minecraft:dispenser[facing=east]')
host.raw('container', *dispenser, 'minecraft:diamond', 1)
host.raw('setblock', *button, 'minecraft:stone_button[face=wall,facing=north]')
player.until(lambda: player.raw('block', *button)['block'], lambda v: v == 'minecraft:stone_button')
player.raw('interact', *button)
slots = host.until(lambda: host.raw('container', *dispenser)['slots'], lambda v: all(s is None for s in v))
dropped = {}
for viewer in (host, guest):
dropped[viewer.name] = viewer.until(
lambda: [e for e in viewer.raw('entities', 24, 'minecraft:item')['entities']
if e.get('item', {}).get('item') == 'minecraft:diamond'
and math.dist(e['pos'], (x + 1.5, y + 0.5, z + 3.5)) < 3],
lambda drops: len(drops) == 1 and drops[0]['item']['count'] == 1)
player.shot(out, f'{kind}-input')
return {'slots': slots, 'drops': dropped}
finally:
player.raw('key', 'w', 'up')
host('command', command=f'fill {x - 2} {y} {z - 2} {x + 5} {y + 3} {z + 5} minecraft:air')
for _kind in ('sculk', 'dispenser'):
for _role in ('host', 'guest'):
_name = f'{_kind}_input_{_role}'
CHECKS.append(_name)
CHECK_FNS[_name] = lambda h, g, o, kind=_kind, role=_role: check_fixture_input(h, g, o, kind, role)
def check_hero_trading(host, guest, out):
"""Villager.updateSpecialPrices keeps the trading player's Hero effect: the offer sent to the
client and the server's payment consumption agree, independently of the other player's effect."""
+4 -1
View File
@@ -9,13 +9,16 @@ import subprocess
try:
from . import lab, test_rust_parity as base, test_model_baking_parity as baking
from .integration_env import run_game
except ImportError:
import lab, test_rust_parity as base, test_model_baking_parity as baking
from integration_env import run_game
def run_logged(command, build, name, timeout=900, **kwargs):
with (build / (name + '.log')).open('w') as log:
subprocess.run([str(c) for c in command], cwd=lab.ROOT, check=True, timeout=timeout,
runner = run_game if Path(command[0]).name == 'horizon-client' else subprocess.run
runner([str(c) for c in command], cwd=lab.ROOT, check=True, timeout=timeout,
stdout=log, stderr=subprocess.STDOUT, **kwargs)
+14 -1
View File
@@ -11,6 +11,9 @@
python3 tools/parity.py --list # what would run, without running it
python3 tools/parity.py --status # last result of every harness
Harnesses in parity_hosts.json's "named_only" run only when named or with --all
(the nightly audit): no automatic selection picks them.
Each harness runs unmodified, in parallel, under a tracer that records what it
read: its Python modules, Java oracle sources, other repository files, and the
Rust sources of every probe it executed (from Cargo's dep-info). A harness
@@ -1306,6 +1309,12 @@ def platform_skips(config):
return dict(config.get("linux_only", {}))
def named_only(config):
"""Harnesses tools/parity_hosts.json keeps out of every automatic selection (--changed, --failed, the default,
--flake-check): they run only when named or with --all, the nightly audit. {name: reason}"""
return dict(config.get("named_only", {}))
def finished_run(failed):
"""Record that this run completed, for tools/gate.py: a crashed runner never writes it."""
STATE_DIR.mkdir(parents=True, exist_ok=True)
@@ -1390,9 +1399,13 @@ def main():
for name in [name for name in chosen if base_name(name) in skips and not args.names]:
print(f"skipped on {sys.platform}: {name}: {skips[base_name(name)]}", flush=True)
chosen.remove(name)
manual = {} if args.names or args.all else named_only(host_config())
for name in [name for name in chosen if base_name(name) in manual]:
print(f"not selected (runs when named, and nightly): {name}: {manual[base_name(name)]}", flush=True)
chosen.remove(name)
rounds = {name: args.repeat for name in chosen} if args.repeat > 1 else {}
if args.flake_check:
changed = changed_harnesses(found, records, args.flake_check)
changed = {name for name in changed_harnesses(found, records, args.flake_check) if base_name(name) not in manual}
for name in sorted(changed):
# A pass counts for a changed harness only if it held over enough runs.
if name not in chosen and records.get(name, {}).get("rounds", 1) < args.flake_rounds:
+4 -1
View File
@@ -36,5 +36,8 @@
"local_only": {
"save_roundtrip": "runs the game on a virtual screen (vscreen) with a null ALSA device, or offscreen in the logged-in macOS session"
},
"linux_only": {}
"linux_only": {},
"named_only": {
"lan_integration": "the whole lan_e2e suite: ~40 min a run on a loaded runner, and single checks flake under load; run it by name, or tools/lan_e2e.py"
}
}
+4 -1
View File
@@ -25,9 +25,11 @@ import sys
try:
from . import lab, test_rust_parity as base
from .integration_env import run_game
from . import command_exec_corpus as corpus_module
except ImportError:
import lab
from integration_env import run_game
import test_rust_parity as base
import command_exec_corpus as corpus_module
@@ -55,7 +57,8 @@ def main():
def run(command, name, **kw):
with (build / (name + '.log')).open('w', encoding='utf-8') as log:
result = subprocess.run(list(map(str, command)), stdout=log, stderr=subprocess.STDOUT, **kw)
runner = run_game if Path(command[0]).name == 'horizon-client' else subprocess.run
result = runner(list(map(str, command)), stdout=log, stderr=subprocess.STDOUT, **kw)
if result.returncode:
tail = (build / (name + '.log')).read_text(errors='replace')[-3000:]
sys.exit(f'{name} failed ({result.returncode}):\n{tail}')
+9 -3
View File
@@ -65,13 +65,16 @@ import struct
import subprocess
import sys
import threading
import tempfile
import time
import zlib
try:
from . import lab, oracle_harness as oh, test_rust_parity as base
from .integration_env import game_environment, run_game
from .lab import free_port # the other server harnesses import it from here
except ImportError:
from integration_env import game_environment, run_game
import lab
import oracle_harness as oh
import test_rust_parity as base
@@ -124,9 +127,11 @@ class Server:
self.log_path = directory.parent / f'{log_name}.log'
self.log = self.log_path.open('w', encoding='utf-8')
self.lines = []
self.process = subprocess.Popen(list(map(str, command)), cwd=directory, stdin=subprocess.PIPE,
self.scratch = tempfile.TemporaryDirectory(prefix='hz-server-')
env = game_environment(os.environ, self.scratch.name)
self.process = subprocess.Popen(list(map(str, command)), cwd=directory, env=env, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
encoding='utf-8', errors='replace', env=lab.game_env())
encoding='utf-8', errors='replace')
self.reader = threading.Thread(target=self._read, daemon=True)
self.reader.start()
@@ -163,6 +168,7 @@ class Server:
self.process.wait()
self.reader.join(timeout=10)
self.log.close()
self.scratch.cleanup()
def settle(server, ticks=600, timeout=600):
@@ -359,7 +365,7 @@ def horizon_client_joins(vanilla_server, binary, world, build, reference):
server.wait_for(r'Done \(')
env = lab.game_env(HORIZON_NET_REPORT=report)
with (build / 'horizon-client-run.log').open('w') as log:
subprocess.run([str(c) for c in [binary, '--resources', lab.RESOURCES, '--render-distance', 2,
run_game([str(c) for c in [binary, '--resources', lab.RESOURCES, '--render-distance', 2,
'--server', f'127.0.0.1:{port}', '--name', PLAYER]],
cwd=client_dir, env=env, stdout=log, stderr=subprocess.STDOUT, timeout=300, check=True)
server.wait_for(r'left the game', timeout=30)
+4 -1
View File
@@ -17,7 +17,9 @@ import random
import subprocess
try:
from . import block_update_harness as h, lab, test_rust_parity as base
from .integration_env import run_game
except ImportError:
from integration_env import run_game
import block_update_harness as h
import lab
import test_rust_parity as base
@@ -349,7 +351,8 @@ def voice(line):
def run(command, **kwargs):
done = subprocess.run(command, capture_output=True, text=True, **kwargs)
runner = run_game if str(command[0]).endswith('/horizon-client') else subprocess.run
done = runner(command, capture_output=True, text=True, **kwargs)
if done.returncode:
print((done.stdout + done.stderr)[-4000:])
raise SystemExit(f'{command[0]} failed')
+9
View File
@@ -29,6 +29,15 @@ def corpus():
if v[k] is not None:v[k]=[bits(n) for n in v[k]]
return v
def row(ops,player=False,order=(0,1,2)):return dict(ops=ops,player=player,order=order)
# Dropped items: the 0.25-wide box is deflated by Entity before scanning.
# Cover surface grazing, buoyancy depth, cell boundaries and repeated pushes.
for x in [.5, 1.0, 15.99]:
for y in [0, .1, .7, 8/9-.1, 8/9-.001, 8/9, 1]:
for flow in [(0,0,0),(1,0,0),(-1,0,1),(0,-1,0)]:
update=op(bounds=[x-.124,y+.001,.376,x+.124,y+.249,.624],
base_y=y,eye=y+.2125,block=[int(x),int(y),0],
default=cell(flow=flow))
yield row([update,op(mode='apply'),update,op(mode='apply')])
values=[-float('inf'),-1,-.0,0,.1,.39999998,.4,.40000004,8/9,1,2,float('inf'),float('nan')]
for kind in range(5):
for height in values:
+31 -16
View File
@@ -15,8 +15,9 @@ is off: nothing leaves the machine.
python3 tools/test_friends.py --client target/fast/horizon-client --relay target/fast/horizon-relay
`--server-thread` runs the host's world on its integrated server's own
thread (the window is a client of it, as Open to LAN publishes it).
The host runs on its integrated server thread by default (the window is a
client of it). `--in-frame` compares the fallback; `--server-thread` is accepted
for compatibility.
`--role host|guest` runs one side only (two machines, e.g. over a tailnet):
the host prints the code, the guest takes it with `--code` (`--relay-url`
@@ -36,8 +37,10 @@ import urllib.request
try:
from . import lab
from .integration_env import game_environment, require_server_reports
except ImportError:
import lab
from integration_env import game_environment, require_server_reports
ROOT = Path(__file__).resolve().parents[1]
@@ -84,7 +87,7 @@ class Game:
prefix = []
extra = []
# The host's world on its server thread (`--server-thread`), else in the window.
server_thread = False
server_thread = True
def __init__(self, client, resources, work, name, env):
# Either way explicitly, whatever the game's default.
@@ -96,7 +99,8 @@ class Game:
self.process = subprocess.Popen(Game.prefix + [
str(client), '--resources', str(resources), '--render-distance', '2', '--test-terrain',
'--control-socket', str(self.endpoint), '--timeout-seconds', '600',
] + Game.extra, cwd=self.home, env=lab.game_env(HORIZON_HOME=self.home, **env),
] + Game.extra, cwd=self.home,
env={**game_environment({**os.environ, **env}, self.home), 'HORIZON_HOME': str(self.home)},
stdout=self.log, stderr=subprocess.STDOUT)
wait_for(self.endpoint, 'screen', lambda v: v['screen'] == 'title', 120, f'{name} title')
@@ -149,10 +153,11 @@ def play_together(host, guest, name, round_index):
before = guest_pos(host, name)
guest('look 0 0')
guest('key w down')
time.sleep(1.5)
guest('key w up')
host.wait('lan status', lambda v: math.dist(before, next(g['position'] for g in v['guests'] if g['name'] == name)) > 1.0,
20, 'guest movement on the host')
try:
host.wait('lan status', lambda v: math.dist(before, next(g['position'] for g in v['guests'] if g['name'] == name)) > 1.0,
20, 'guest movement on the host')
finally:
guest('key w up')
# The host moves; the guest sees the host's player there.
hp = host('status')['player']['position']
target = [hp[0] + 3.0, hp[1], hp[2] + 1.0]
@@ -179,10 +184,11 @@ def play_together(host, guest, name, round_index):
guest.wait('status', lambda v: v['target']['pos'] == block, 10, 'the guest aiming at the block')
# Held for a few ticks, as a player clicks (creative breaks at once).
guest('mouse left down')
time.sleep(0.25)
guest('mouse left up')
host.wait(f'getblock {block[0]} {block[1]} {block[2]}', lambda v: v['block'] == 'minecraft:air', 20,
'the guest breaking the block, on the host')
try:
host.wait(f'getblock {block[0]} {block[1]} {block[2]}', lambda v: v['block'] == 'minecraft:air', 20,
'the guest breaking the block, on the host')
finally:
guest('mouse left up')
guest.wait(f'getblock {block[0]} {block[1]} {block[2]}', lambda v: v['block'] == 'minecraft:air', 20,
'the broken block on the guest')
@@ -250,7 +256,7 @@ def full(args, work):
join(guest, code, 'Guest')
play_together(host, guest, 'Guest', 1)
assert host('friends status')['direct'] == 1, host('friends status')
print('code alone: chat, movement and blocks both ways, no server involved')
print('code alone: chat, movement and blocks both ways, no relay involved')
leave(host, guest, 'Guest')
host('friends stop')
assert host('friends status')['code'] is None
@@ -316,8 +322,8 @@ def one_side(args, work):
if args.role == 'host':
code = host_world(game)
print(f'CODE {code}', flush=True)
game.wait('lan status', lambda v: v['guests'], args.wait, 'a guest')
game.wait('lan status', lambda v: not v['guests'], args.wait, 'the guest leaving')
game.wait('lan status', lambda v: any(g['name'] == 'Remote' for g in v['guests']), args.wait, 'a guest')
game.wait('lan status', lambda v: all(g['name'] != 'Remote' for g in v['guests']), args.wait, 'the guest leaving')
print(f'host: {game("friends status")}')
else:
join(game, args.code, 'Remote')
@@ -347,16 +353,25 @@ def main():
parser.add_argument('--hold', type=float, default=0, help='guest: stay this many seconds before leaving')
parser.add_argument('--baseline', action='store_true', help='first run the same checks over plain LAN')
parser.add_argument('--offscreen', action='store_true', help='run the games without a window')
parser.add_argument('--server-thread', action='store_true', help="the host's world on its server thread")
mode = parser.add_mutually_exclusive_group()
mode.add_argument('--server-thread', dest='server_thread', action='store_true', default=True,
help='host on its server thread (default; retained for existing callers)')
mode.add_argument('--in-frame', dest='server_thread', action='store_false',
help='host the world in the window, for comparison')
parser.add_argument('--xvfb', action='store_true', help='run the games under xvfb-run (headless Linux)')
args = parser.parse_args()
Game.prefix = ['xvfb-run', '-a'] if args.xvfb else []
Game.extra = ['--offscreen'] if args.offscreen else []
Game.server_thread = args.server_thread
args.client, args.resources = args.client.resolve(), args.resources.resolve()
try:
require_server_reports(args.resources)
except ValueError as error:
parser.error(str(error))
# Short: Unix socket paths are limited to about 100 bytes.
with tempfile.TemporaryDirectory(prefix='hzf-', dir='/tmp') as directory:
work = Path(directory)
print(f'Friends host mode: {"server-thread" if Game.server_thread else "in-frame"}')
if args.role:
return one_side(args, work)
assert args.relay, 'give --relay (the relay binary, run locally for the relay half)'
+30
View File
@@ -0,0 +1,30 @@
#!/usr/bin/env python3
"""Real clients over LAN, direct invitations and a local relay, with a server-thread host.
Registered with parity.py so changes to the drivers and the game binary select
this integration coverage in CI as well as the deterministic library probes.
"""
import json
from pathlib import Path
import subprocess
import sys
import lab
import test_friends
from integration_env import suite_lock
if __name__ == '__main__':
subprocess.run(['cargo', 'build', '--locked', '--profile', 'fast', '-p', 'horizon-client',
'-p', 'horizon-relay', '--bin', 'horizon-client', '--bin', 'horizon-ctl',
'--bin', 'horizon-relay'], cwd=lab.ROOT, check=True)
metadata = json.loads(subprocess.check_output(
['cargo', 'metadata', '--format-version', '1', '--no-deps'], cwd=lab.ROOT, text=True))
bins = Path(metadata['target_directory']) / 'fast'
sys.argv = [sys.argv[0], '--client', str(bins / 'horizon-client'),
'--relay', str(bins / 'horizon-relay'), '--baseline',
*(['--offscreen'] if sys.platform == 'darwin' else [])]
with suite_lock():
# The wrapper only selects binaries; test_friends isolates each game
# through integration_env and lab.game_env.
test_friends.main()
+14
View File
@@ -0,0 +1,14 @@
#!/usr/bin/env python3
"""Register the real-client LAN suite with parity.py's traced harness selection.
The assertions live in lan_e2e; this is integration coverage, alongside the
fast vanilla differential probes. The host uses the normal server thread.
Linux runners need a display (for example xvfb-run -a).
"""
import lan_e2e
from integration_env import suite_lock
if __name__ == '__main__':
with suite_lock():
lan_e2e.main()
+3 -1
View File
@@ -50,11 +50,13 @@ import urllib.parse
try:
from . import lab, oracle_harness as oh, test_rust_parity as base
from .test_dedicated_server_parity import Server, free_port
from .integration_env import run_game
except ImportError:
import lab
import oracle_harness as oh
import test_rust_parity as base
from test_dedicated_server_parity import Server, free_port
from integration_env import run_game
NAME = 'online-mode-parity'
PLAYER = 'Tester'
@@ -185,7 +187,7 @@ def run_client(binary, build, port, label, token, discovery):
if token:
command += ['--session-file', session_file(directory, token)]
with (build / f'client-{label}.log').open('w') as log:
subprocess.run([str(c) for c in command], cwd=directory, env=env, stdout=log, stderr=subprocess.STDOUT,
run_game([str(c) for c in command], cwd=directory, env=env, stdout=log, stderr=subprocess.STDOUT,
timeout=300, check=True)
got = json.loads(report.read_text())
got['session file left'] = (directory / 'session.json').exists()
+1 -1
View File
@@ -147,7 +147,7 @@ def main():
run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-nowarn', '-cp', dependencies, '-d', classes,
*(PARITY / f'{n}.java' for n in ('ParityData', 'ParityFont', 'VanillaPackScreenOracle'))], 'compile-java')
run(['java', '-Xmx1G', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.VanillaPackScreenOracle',
inputs, build / 'vanilla.jsonl'], 'vanilla')
inputs, build / 'vanilla.jsonl', lab.RESOURCES / 'assets'], 'vanilla')
lib.cargo_example('horizon-client', 'pack_screen_fixture', lib.RESOURCES, inputs, build / 'rust.jsonl')
vanilla = [json.loads(l) for l in (build / 'vanilla.jsonl').read_text().splitlines()]
rust = [json.loads(l) for l in (build / 'rust.jsonl').read_text().splitlines()]
@@ -41,6 +41,11 @@ SCRIPT = [
# Through an End portal (Entity.handlePortal), and back by command.
('bob-portal', ['cmd Bob setblock 2 101 3 minecraft:end_portal', 'cmd Bob tp @s 2.5 101 3.5']),
('bob-portal-back', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']),
# Sent far into the Nether, then teleported within it before the far chunks load: a portal there
# still takes him (the abandoned arrival does not hold him).
('bob-retarget', [f'cmd Bob execute in {NETHER} run tp @s 8192.5 100 0.5', 'cmd Bob tp @s 0.5 100 0.5']),
('bob-retarget-portal', ['cmd Bob setblock 0 101 3 minecraft:end_portal', 'cmd Bob tp @s 0.5 101 3.5']),
('bob-retarget-back', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']),
]
STEP_SECONDS = 5.0
+6
View File
@@ -55,6 +55,12 @@ KNOWN = {
"Better Dungeons' own structure types (spec-betterdungeons §5, §6; OPEN-B14, B15) are not "
'implemented yet',
},
'ctov': {
r'^ctov:': "Lithostitched's random is not known (spec-lithostitched OPEN-L6): per-block road and palette "
'choices match by share, not by block; and decoration (grass, trees) differs where the village '
'changed the terrain',
r'^minecraft:village_plains$': 'decoration (grass, trees) differs where the village changed the terrain',
},
}
+1 -1
View File
@@ -240,7 +240,7 @@ def main():
run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-nowarn', '-cp', dependencies, '-d', classes,
*(PARITY / f'{n}.java' for n in ('ParityData', 'ParityFont', 'VanillaTabListOracle', 'VanillaSkinImageOracle'))], 'compile-java')
run(['java', '-Xmx2G', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.VanillaTabListOracle',
inputs, build / 'vanilla.jsonl'], 'vanilla')
inputs, build / 'vanilla.jsonl', resources / 'assets'], 'vanilla')
run(['cargo', 'run', '--quiet', '--profile', 'fast', '-p', 'horizon-client', '--example', 'client_probe', '--',
'tab_list_fixture', resources, inputs, build / 'rust.jsonl'], 'rust')
images = build / 'skins.txt'; images.write_text(''.join(i + '\n' for i in skin_images(args.images)))