ghast-client 0.5.6

Headless Minecraft Java Edition client for the ghast library: connection, login, fixed 50 ms tick loop and a version-agnostic bot API (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3)
Documentation
//! Unit tests of the shared movement glue on 1.8.9: teleport rules, the Grim compatibility
//! position resend and the test floor.

use super::*;
use crate::version::{ClientVersion, V1_8};

/// The 1.8 physics' teleport matches the version's documented rules.
#[test]
fn teleport_rules_agree() {
    let cases = [
        (1.5, 64.0, -2.5, 450.0_f32, 100.0_f32, 0_i8),
        (0.25, -1.0, 0.5, -30.0, -95.0, 0x1f),
        (3.0, 70.0, 3.0, 720.5, 10.0, 0x08),
    ];
    for (x, y, z, yaw, pitch, flags) in cases {
        let t = Teleport {
            x,
            y,
            z,
            yaw,
            pitch,
            flags,
            teleport_id: None,
            ..Default::default()
        };
        let start = Pose {
            pos: Vec3::new(10.0, 65.0, 10.0),
            yaw: 33.0,
            pitch: 5.0,
            motion: Vec3::new(0.1, -0.0784, 0.2),
            ..Pose::default()
        };
        let mut a = start;
        <V1_8 as ClientVersion>::apply_teleport(&mut a, &t);
        let mut sim = Sim18::new();
        let mut b = start;
        sim.player
            .set_position(start.pos.x, start.pos.y, start.pos.z);
        sim.player.vel = [start.motion.x, start.motion.y, start.motion.z];
        sim.teleport(&mut b, &t);
        assert_eq!(
            (a.pos, a.yaw, a.pitch, a.motion),
            (b.pos, b.yaw, b.pitch, b.motion),
            "{t:?}"
        );
    }
}

#[test]
fn force_position_only_on_height_change() {
    use ghast_physics::Packet as P;
    let mut pk = ghast_physics::Packets::new();
    pk.push(P::StopSprinting);
    pk.push(P::Look {
        yaw: 1.0,
        pitch: 2.0,
        on_ground: true,
    });
    let mut sent = [1.0, 3.979, 8.3];
    let mut ticks = 3;
    // Same height: untouched.
    force_position(
        &mut pk,
        [1.01, 3.979, 8.3],
        &mut sent,
        &mut ticks,
        ForceStepPosition::Always,
    );
    assert!(matches!(pk.as_slice()[1], P::Look { .. }));
    // Stepped up: the look becomes a position_look, the bookkeeping follows.
    force_position(
        &mut pk,
        [1.01, 4.0, 8.28],
        &mut sent,
        &mut ticks,
        ForceStepPosition::Always,
    );
    assert_eq!(pk.as_slice()[0], P::StopSprinting);
    assert!(matches!(
        pk.as_slice()[1],
        P::PositionLook {
            y: 4.0,
            on_ground: true,
            ..
        }
    ));
    assert_eq!((sent, ticks), ([1.01, 4.0, 8.28], 0));
}

#[test]
fn force_position_step_up_only() {
    use ghast_physics::Packet as P;
    let look = |on_ground| P::Look {
        yaw: 1.0,
        pitch: 2.0,
        on_ground,
    };
    let mode = ForceStepPosition::StepUpOnly;
    // Feet went down (a landing under the threshold): stays vanilla.
    let mut pk = ghast_physics::Packets::new();
    pk.push(look(true));
    let (mut sent, mut ticks) = ([1.0, 4.0, 8.3], 3);
    force_position(&mut pk, [1.01, 3.979, 8.3], &mut sent, &mut ticks, mode);
    assert!(matches!(pk.as_slice()[0], P::Look { .. }));
    // Went up but airborne: stays vanilla.
    force_position(&mut pk, [1.01, 4.01, 8.3], &mut sent, &mut ticks, mode);
    let mut air = ghast_physics::Packets::new();
    air.push(look(false));
    force_position(&mut air, [1.01, 4.01, 8.3], &mut sent, &mut ticks, mode);
    assert!(matches!(air.as_slice()[0], P::Look { .. }));
    // A step-up (up and on ground): forced.
    let mut up = ghast_physics::Packets::new();
    up.push(look(true));
    let (mut sent, mut ticks) = ([1.0, 3.979, 8.3], 3);
    force_position(&mut up, [1.01, 4.0, 8.28], &mut sent, &mut ticks, mode);
    assert!(matches!(up.as_slice()[0], P::PositionLook { y: 4.0, .. }));
    assert_eq!((sent, ticks), ([1.01, 4.0, 8.28], 0));
}

#[test]
fn physics_stands_on_the_test_floor() {
    let mut sim = Sim18::new();
    sim.world_packet(&test_chunks::v1_8(0, 0)).unwrap();
    let mut pose = Pose::default();
    sim.teleport(
        &mut pose,
        &Teleport {
            x: 8.5,
            y: 64.0,
            z: 8.5,
            yaw: 0.0,
            pitch: 0.0,
            flags: 0,
            teleport_id: None,
            ..Default::default()
        },
    );
    let mut out = Vec::new();
    for _ in 0..30 {
        sim.step(&mut pose, Controls::NONE, false, 1, &mut out);
    }
    assert_eq!(pose.pos, Vec3::new(8.5, 64.0, 8.5));
    assert!(pose.on_ground);
    // Walking forward moves the player.
    for _ in 0..10 {
        sim.step(
            &mut pose,
            Controls {
                forward: true,
                ..Controls::NONE
            },
            false,
            1,
            &mut out,
        );
    }
    assert!(pose.pos.z > 9.0, "{pose:?}");
}