ghast-physics 0.5.6

Vanilla-exact, pure and deterministic player movement for the ghast Minecraft Java Edition client library (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3), verified bit for bit against the real game
Documentation
//! Large replay of the projectile oracles: every `*.jsonl.gz` in
//! `oracle/cache/<v>/fuzz-projectiles/` for 1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3 (made by
//! `MC=<v> oracle/run.sh projectiles-fuzz`), and in `oracle/cache/<v>/fuzz-fishing/` for
//! 1.12.2, 1.16.5 and 26.x (`fishing-fuzz`: client bobbers hooked and biting), compared bit for
//! bit, one case in memory at a time. Skips a version whose directory is absent.
//!
//! Needs the git-ignored oracle cache, so it is behind the `oracle-cache` feature:
//!
//! ```sh
//! MC=26.1 oracle/run.sh projectiles-fuzz --seed 2 --count 2000 --ticks 100
//! cargo test --release -p ghast-physics --features oracle-cache --test projectiles_cache -- --nocapture
//! ```
//!
//! `PROJ_CACHE_FILTER=s3` replays only the files whose name contains `s3` (to split a long run).
#![cfg(feature = "oracle-cache")]
#![recursion_limit = "256"]

mod common;

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use common::{Mismatch, repo_root};

fn files(version: &str, sub: &str) -> Vec<PathBuf> {
    let dir = repo_root().join("oracle/cache").join(version).join(sub);
    let Ok(rd) = std::fs::read_dir(&dir) else {
        return Vec::new();
    };
    let filter = std::env::var("PROJ_CACHE_FILTER").unwrap_or_default();
    let mut v: Vec<PathBuf> = rd
        .filter_map(|e| e.ok().map(|e| e.path()))
        .filter(|p| {
            let s = p.to_string_lossy();
            s.ends_with(".jsonl.gz") && s.contains(filter.as_str())
        })
        .collect();
    v.sort();
    v
}

/// One file's totals: cases, ticks, per kind and side (cases, ticks), failures.
type FileSummary = (
    usize,
    usize,
    BTreeMap<String, (usize, usize)>,
    Vec<Mismatch>,
);

/// Replays every file of a version's `fuzz-projectiles` with `replay` and asserts no case
/// differs.
fn check(version: &str, replay: impl Fn(&Path) -> FileSummary) {
    check_dir(version, "fuzz-projectiles", replay);
}

/// Replays every file of `oracle/cache/<version>/<sub>` with `replay` and asserts no case
/// differs.
fn check_dir(version: &str, sub: &str, replay: impl Fn(&Path) -> FileSummary) {
    let files = files(version, sub);
    if files.is_empty() {
        eprintln!("skipped: no oracle/cache/{version}/{sub}");
        return;
    }
    let (mut cases, mut ticks) = (0, 0);
    let mut by_kind: BTreeMap<String, (usize, usize)> = BTreeMap::new();
    let mut failures = Vec::new();
    for f in &files {
        let (c, t, k, fl) = replay(f);
        eprintln!("{}: {c} cases, {t} ticks, {} differ", f.display(), fl.len());
        cases += c;
        ticks += t;
        for (kind, (c, t)) in k {
            let e = by_kind.entry(kind).or_default();
            e.0 += c;
            e.1 += t;
        }
        failures.extend(fl);
    }
    for (k, (c, t)) in &by_kind {
        eprintln!("  {k}: {c} cases, {t} ticks");
    }
    let (known, failures): (Vec<_>, Vec<_>) = failures
        .into_iter()
        .partition(|f| KNOWN.contains(&(version, f.case.as_str())));
    for f in &known {
        eprintln!("known, not modelled: {f}");
    }
    for f in failures.iter().take(20) {
        eprintln!("MISMATCH {f}");
    }
    eprintln!(
        "{version} total: {cases} cases, {ticks} entity ticks matched, {} cases differ \
         ({} known)",
        failures.len(),
        known.len()
    );
    assert!(failures.is_empty(), "{} cases differ", failures.len());
}

/// Cases whose difference is a world change the entity physics does not model (listed, so a
/// new difference still fails):
///
/// * 1.8.9 `proj_s5_1687_falling_block` (server, tick 5): a falling anvil clears its start
///   block; the server's neighbour update makes the trapdoors under it, which hang from nothing,
///   drop one after the other, and the anvil falls through where they were.
const KNOWN: &[(&str, &str)] = &[("1.8.9", "proj_s5_1687_falling_block")];

#[cfg(feature = "v26_1")]
fn modern<R: common::v261::Row>(path: &Path) -> FileSummary {
    let s = common::projectiles26::replay_file::<R>(path);
    (s.cases, s.ticks, s.by_kind, s.failures)
}

#[cfg(feature = "v26_1")]
#[test]
fn projectiles_cache_26_1() {
    check("26.1", modern::<ghast_physics::modern::V26_1>);
}

#[cfg(all(feature = "v26_1", feature = "v26_2"))]
#[test]
fn projectiles_cache_26_2() {
    check("26.2", modern::<ghast_physics::modern::V26_2>);
}

#[cfg(all(feature = "v26_1", feature = "v26_3"))]
#[test]
fn projectiles_cache_26_3() {
    check("26.3", modern::<ghast_physics::modern::V26_3>);
}

#[cfg(feature = "v1_8")]
#[test]
fn projectiles_cache_1_8_9() {
    check("1.8.9", |p| {
        let s = common::projectiles_legacy::v1_8::replay_file(p);
        (s.cases, s.ticks, s.by_kind, s.failures)
    });
}

#[cfg(feature = "v1_12")]
#[test]
fn projectiles_cache_1_12_2() {
    check("1.12.2", |p| {
        let s = common::projectiles_legacy::v1_12::replay_file(p);
        (s.cases, s.ticks, s.by_kind, s.failures)
    });
}

#[cfg(feature = "v1_16_5")]
#[test]
fn projectiles_cache_1_16_5() {
    check("1.16.5", |p| {
        let s = common::projectiles_legacy::v1_16_5::replay_file(p);
        (s.cases, s.ticks, s.by_kind, s.failures)
    });
}

// Client bobbers with a hooked entity and the bite (`oracle/cache/<v>/fuzz-fishing/`, made by
// `MC=<v> oracle/run.sh fishing-fuzz`).

#[cfg(feature = "v26_1")]
#[test]
fn fishing_cache_26_1() {
    check_dir(
        "26.1",
        "fuzz-fishing",
        modern::<ghast_physics::modern::V26_1>,
    );
}

#[cfg(all(feature = "v26_1", feature = "v26_2"))]
#[test]
fn fishing_cache_26_2() {
    check_dir(
        "26.2",
        "fuzz-fishing",
        modern::<ghast_physics::modern::V26_2>,
    );
}

#[cfg(all(feature = "v26_1", feature = "v26_3"))]
#[test]
fn fishing_cache_26_3() {
    check_dir(
        "26.3",
        "fuzz-fishing",
        modern::<ghast_physics::modern::V26_3>,
    );
}

#[cfg(feature = "v1_12")]
#[test]
fn fishing_cache_1_12_2() {
    check_dir("1.12.2", "fuzz-fishing", |p| {
        let s = common::projectiles_legacy::v1_12::replay_file(p);
        (s.cases, s.ticks, s.by_kind, s.failures)
    });
}

#[cfg(feature = "v1_16_5")]
#[test]
fn fishing_cache_1_16_5() {
    check_dir("1.16.5", "fuzz-fishing", |p| {
        let s = common::projectiles_legacy::v1_16_5::replay_file(p);
        (s.cases, s.ticks, s.by_kind, s.failures)
    });
}