use super::tests::{Script, engine, join, names};
use super::*;
use crate::common::EntityAction;
use bytes::BufMut;
use ghast_protocol::RawPacket;
use ghast_protocol::codec::McWrite;
#[cfg(not(feature = "v26_3"))]
const VERSIONS: [GameVersion; 5] = [
GameVersion::V1_8_9,
GameVersion::V1_12_2,
GameVersion::V1_16_5,
GameVersion::V26_1,
GameVersion::V26_2,
];
#[cfg(feature = "v26_3")]
const VERSIONS: [GameVersion; 6] = [
GameVersion::V1_8_9,
GameVersion::V1_12_2,
GameVersion::V1_16_5,
GameVersion::V26_1,
GameVersion::V26_2,
GameVersion::V26_3,
];
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(super) enum Kind {
Player,
Zombie,
}
fn uuid(id: i32) -> uuid::Uuid {
uuid::Uuid::from_u128(0x7465_7374_0000 + id as u128)
}
pub(super) fn put(e: &mut Engine<Box<dyn Sim>>, id: i32, kind: Kind, pos: Vec3) {
let v = e.core.opts.caps.version;
if kind == Kind::Player && v.caps().configuration_state {
let mut b = bytes::BytesMut::new();
b.put_u8(1);
b.put_varint(1);
b.put_u128(uuid(id).as_u128());
b.put_varint(6);
b.put_slice(b"Target");
b.put_varint(0);
let pid = modern_cb_id!(v, PLAYER_INFO);
e.sim
.world_packet(&RawPacket::new(pid, b.freeze()))
.unwrap();
}
e.sim.world_packet(&spawn(v, id, kind, pos)).unwrap();
}
fn floor(e: &mut Engine<Box<dyn Sim>>) {
if e.core.opts.caps.version != GameVersion::V1_16_5 {
return;
}
let id = ghast_protocol::v1_16_5::play::clientbound::id::BLOCK_CHANGE;
for x in -15i64..=15 {
for z in 7i64..=9 {
let pos = ((x & 0x3ff_ffff) << 38) | ((z & 0x3ff_ffff) << 12) | 63;
let mut b = bytes::BytesMut::new();
b.put_i64(pos);
b.put_varint(1);
e.sim.world_packet(&RawPacket::new(id, b.freeze())).unwrap();
}
}
}
fn spawn(v: GameVersion, id: i32, kind: Kind, pos: Vec3) -> RawPacket {
let fixed = |d: f64| (d * 32.0).floor() as i32;
let vel = ghast_protocol::defs::p47::types::Vec3i16 { x: 0, y: 0, z: 0 };
match v.instance() {
GameVersion::V1_8_9 => {
use ghast_protocol::v1_8::play::clientbound as cb;
match kind {
Kind::Player => RawPacket::from_packet(&cb::NamedEntitySpawn {
entity_id: id,
player_uuid: uuid(id),
x: fixed(pos.x),
y: fixed(pos.y),
z: fixed(pos.z),
yaw: 0,
pitch: 0,
current_item: 0,
metadata: Vec::new(),
}),
Kind::Zombie => RawPacket::from_packet(&cb::SpawnEntityLiving {
entity_id: id,
r#type: 54,
x: fixed(pos.x),
y: fixed(pos.y),
z: fixed(pos.z),
yaw: 0,
pitch: 0,
head_pitch: 0,
velocity: vel,
metadata: Vec::new(),
}),
}
}
GameVersion::V1_12_2 => {
use ghast_protocol::v1_12_2::play::clientbound as cb;
match kind {
Kind::Player => RawPacket::from_packet(&cb::NamedEntitySpawn {
entity_id: id,
player_uuid: uuid(id),
x: pos.x,
y: pos.y,
z: pos.z,
yaw: 0,
pitch: 0,
metadata: Vec::new(),
}),
Kind::Zombie => RawPacket::from_packet(&cb::SpawnEntityLiving {
entity_id: id,
entity_uuid: uuid(id),
r#type: 54,
x: pos.x,
y: pos.y,
z: pos.z,
yaw: 0,
pitch: 0,
head_pitch: 0,
velocity: vel,
metadata: Vec::new(),
}),
}
}
GameVersion::V1_16_5 => {
use ghast_protocol::v1_16_5::play::clientbound as cb;
match kind {
Kind::Player => RawPacket::from_packet(&cb::NamedEntitySpawn {
entity_id: id,
player_uuid: uuid(id),
x: pos.x,
y: pos.y,
z: pos.z,
yaw: 0,
pitch: 0,
}),
Kind::Zombie => RawPacket::from_packet(&cb::SpawnEntityLiving {
entity_id: id,
entity_uuid: uuid(id),
r#type: ghast_data::v1_16_5::entities()
.iter()
.find(|e| e.name == "zombie")
.map(|e| i32::from(e.id))
.unwrap(),
x: pos.x,
y: pos.y,
z: pos.z,
yaw: 0,
pitch: 0,
head_pitch: 0,
velocity: vel,
}),
}
}
modern => {
let name = match kind {
Kind::Player => "player",
Kind::Zombie => "zombie",
};
let (ty, id_spawn) = (
modern_entity_type!(modern, name),
modern_cb_id!(modern, SPAWN_ENTITY),
);
let mut b = bytes::BytesMut::new();
b.put_varint(id);
b.put_u128(uuid(id).as_u128());
b.put_varint(i32::from(ty));
for c in [pos.x, pos.y, pos.z] {
b.put_f64(c);
}
b.put_u8(0);
b.put_i8(0);
b.put_i8(0);
b.put_i8(0);
b.put_varint(0);
return RawPacket::new(id_spawn, b.freeze());
}
}
.unwrap()
}
fn own_flags(v: GameVersion, flags: u8) -> RawPacket {
let mut b = bytes::BytesMut::new();
b.put_varint(7);
let id = match v.instance() {
GameVersion::V1_8_9 => {
b.put_u8(0);
b.put_u8(flags);
b.put_u8(0x7f);
return RawPacket::new(
ghast_protocol::v1_8::play::clientbound::id::ENTITY_METADATA,
b.freeze(),
);
}
GameVersion::V1_12_2 => ghast_protocol::v1_12_2::play::clientbound::id::ENTITY_METADATA,
GameVersion::V1_16_5 => ghast_protocol::v1_16_5::play::clientbound::id::ENTITY_METADATA,
v => modern_cb_id!(v, ENTITY_METADATA),
};
b.put_u8(0);
b.put_u8(0);
b.put_u8(flags);
b.put_u8(0xff);
RawPacket::new(id, b.freeze())
}
pub(super) fn setup(v: GameVersion) -> Engine<Box<dyn Sim>> {
let mut e = engine(v);
join(&mut e);
floor(&mut e);
e.sim.test_set_own_id(7);
e.sim.set_entities(Some(ghast_entity::Config::default()));
e.handle(Inbound::Health(Health {
health: 20.0,
food: 20,
saturation: 5.0,
..Health::default()
}));
e
}
fn tick(
e: &mut Engine<Box<dyn Sim>>,
t0: Instant,
f: impl FnMut(&mut TickContext<'_>),
) -> Vec<&'static str> {
e.note_inbound(t0);
let mut b = Script(f);
e.tick(&mut b, t0);
names(e.outbound())
}
fn sprint_actions(out: &[Outbound]) -> Vec<EntityAction> {
out.iter()
.filter_map(|o| match o {
Outbound::EntityAction { action, .. }
if matches!(
action,
EntityAction::StartSprinting | EntityAction::StopSprinting
) =>
{
Some(*action)
}
_ => None,
})
.collect()
}
const RUN: Controls = Controls {
forward: true,
sprint: true,
..Controls::NONE
};
const WALK: Controls = Controls {
forward: true,
..Controls::NONE
};
fn sprinting_at(v: GameVersion, kind: Kind, t0: Instant) -> Engine<Box<dyn Sim>> {
let mut e = setup(v);
for _ in 0..8 {
tick(&mut e, t0, |ctx| ctx.set_controls(RUN));
}
tick(&mut e, t0, |ctx| ctx.set_controls(WALK));
assert!(e.core.pose.sprinting, "{v}: sprinting");
let s = e.core.sent.pos;
put(&mut e, 50, kind, Vec3::new(s.x - 1.5, s.y, s.z));
e
}
fn attack_tick(
e: &mut Engine<Box<dyn Sim>>,
t0: Instant,
controls: Controls,
attack: bool,
) -> (Vec<EntityAction>, Vec3, bool) {
let mut hit = false;
e.note_inbound(t0);
let mut b = Script(|ctx: &mut TickContext<'_>| {
ctx.set_controls(controls);
if attack {
hit = ctx.attack(50);
}
});
e.tick(&mut b, t0);
let actions = sprint_actions(e.outbound());
e.outbound().clear();
(actions, e.core.pose.motion, hit)
}
#[test]
fn sprint_hit_slows_and_unsprints_on_players_only() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = sprinting_at(v, Kind::Player, t0);
let (actions, _, hit) = attack_tick(&mut e, t0, WALK, true);
assert!(hit, "{v}: the attack went out");
assert_eq!(actions, [EntityAction::StopSprinting], "{v}");
assert!(!e.core.pose.sprinting, "{v}");
let mut e = sprinting_at(v, Kind::Zombie, t0);
let (actions, _, hit) = attack_tick(&mut e, t0, WALK, true);
assert!(hit, "{v}: the attack went out");
assert!(actions.is_empty(), "{v}: {actions:?}");
assert!(e.core.pose.sprinting, "{v}");
}
}
#[test]
fn sprint_hit_keeps_the_held_sprint_and_slows_by_0_6() {
let t0 = Instant::now();
for v in VERSIONS {
let mut a = sprinting_at(v, Kind::Player, t0);
let mut b = sprinting_at(v, Kind::Player, t0);
let v0 = a.core.pose.motion;
assert_eq!(v0, b.core.pose.motion, "{v}");
assert!(v0.x < -0.1, "{v}: running west {v0:?}");
let (_, plain, _) = attack_tick(&mut a, t0, RUN, false);
let (actions, slowed, hit) = attack_tick(&mut b, t0, RUN, true);
assert!(hit, "{v}");
assert!(actions.is_empty(), "{v}: {actions:?}");
assert!(b.core.pose.sprinting, "{v}");
let f = f64::from(0.6f32 * 0.91);
for (p, s, v0) in [(plain.x, slowed.x, v0.x), (plain.z, slowed.z, v0.z)] {
let want = p - 0.4 * v0 * f;
assert!(
(s - want).abs() < 1e-9,
"{v}: {s} vs {want} (plain {p}, v0 {v0})"
);
}
assert_eq!(plain.y, slowed.y, "{v}");
}
}
#[test]
fn weak_hit_keeps_the_sprint_on_1_9_plus() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = sprinting_at(v, Kind::Zombie, t0);
let (actions, _, hit) = attack_tick(&mut e, t0, WALK, true);
assert!(hit && actions.is_empty(), "{v}: {actions:?}");
let s = e.core.sent.pos;
let destroy = destroy(v, 50);
e.sim.world_packet(&destroy).unwrap();
put(&mut e, 50, Kind::Player, Vec3::new(s.x - 1.5, s.y, s.z));
let (actions, _, hit) = attack_tick(&mut e, t0, WALK, true);
assert!(hit, "{v}");
if v.caps().combat_1_9 {
assert!(actions.is_empty(), "{v}: {actions:?}");
} else {
assert_eq!(actions, [EntityAction::StopSprinting], "{v}");
}
}
}
fn destroy(v: GameVersion, id: i32) -> RawPacket {
let mut b = bytes::BytesMut::new();
b.put_varint(1);
b.put_varint(id);
let pid = match v.instance() {
GameVersion::V1_8_9 => ghast_protocol::v1_8::play::clientbound::id::ENTITY_DESTROY,
GameVersion::V1_12_2 => ghast_protocol::v1_12_2::play::clientbound::id::ENTITY_DESTROY,
GameVersion::V1_16_5 => ghast_protocol::v1_16_5::play::clientbound::id::ENTITY_DESTROY,
v => modern_cb_id!(v, ENTITY_DESTROY),
};
RawPacket::new(pid, b.freeze())
}
#[test]
fn knockback_enchantment_slows_without_sprint() {
let t0 = Instant::now();
for v in [
GameVersion::V1_8_9,
GameVersion::V1_12_2,
GameVersion::V1_16_5,
] {
let run = |kb: Option<u8>, attack: bool| {
let mut e = setup(v);
if let Some(level) = kb {
e.inventory_mut()
.on_set_slot(0, 36, Some(knockback_sword(v, level)));
}
for _ in 0..8 {
tick(&mut e, t0, |ctx| ctx.set_controls(WALK));
}
assert!(!e.core.pose.sprinting, "{v}");
let s = e.core.sent.pos;
put(&mut e, 50, Kind::Player, Vec3::new(s.x - 1.5, s.y, s.z));
let v0 = e.core.pose.motion;
let (actions, after, hit) = attack_tick(&mut e, t0, WALK, attack);
assert!(hit == attack && actions.is_empty(), "{v}");
(v0, after)
};
let (v0, plain) = run(Some(1), false);
let (_, slowed) = run(Some(1), true);
let f = f64::from(0.6f32 * 0.91);
let want = plain.x - 0.4 * v0.x * f;
assert!(
(slowed.x - want).abs() < 1e-9,
"{v}: {} vs {want}",
slowed.x
);
let (_, no_kb) = run(None, true);
assert_eq!(no_kb, plain, "{v}: no knockback, no slow-down");
}
}
fn knockback_sword(v: GameVersion, level: u8) -> ghast_inventory::Stack {
let mut b = vec![10, 0, 0, 9];
if v == GameVersion::V1_16_5 {
let id = b"minecraft:knockback";
b.extend_from_slice(&[0, 12]);
b.extend_from_slice(b"Enchantments");
b.push(10);
b.extend_from_slice(&1i32.to_be_bytes());
b.extend_from_slice(&[8, 0, 2]);
b.extend_from_slice(b"id");
b.extend_from_slice(&(id.len() as u16).to_be_bytes());
b.extend_from_slice(id);
} else {
b.extend_from_slice(&[0, 4]);
b.extend_from_slice(b"ench");
b.push(10);
b.extend_from_slice(&1i32.to_be_bytes());
b.extend_from_slice(&[2, 0, 2]);
b.extend_from_slice(b"id");
b.extend_from_slice(&19i16.to_be_bytes());
}
b.extend_from_slice(&[2, 0, 3]);
b.extend_from_slice(b"lvl");
b.extend_from_slice(&i16::from(level).to_be_bytes());
b.extend_from_slice(&[0, 0]);
let id = match v {
GameVersion::V1_16_5 => i32::from(
ghast_data::v1_16_5::Item::from_name("diamond_sword")
.unwrap()
.id(),
),
_ => 276,
};
ghast_inventory::Stack::new(id, 1, 0).with_nbt(Some(
ghast_protocol::nbt::RawNbt::from_bytes(bytes::Bytes::from(b)).unwrap(),
))
}
#[test]
fn own_flags_byte_sets_the_sprint_flag() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = setup(v);
let mut seen = None;
tick(&mut e, t0, |ctx| seen = Some(ctx.server_flags()));
assert_eq!(seen, Some(None), "{v}");
for _ in 0..8 {
tick(&mut e, t0, |ctx| ctx.set_controls(RUN));
}
e.sim.world_packet(&own_flags(v, 0x08)).unwrap();
let mut seen = (None, false);
tick(&mut e, t0, |ctx| {
ctx.set_controls(WALK);
seen = (ctx.server_flags(), ctx.server_sprinting());
});
assert_eq!(seen, (Some(0x08), true), "{v}");
assert!(e.core.pose.sprinting, "{v}");
e.sim.world_packet(&own_flags(v, 0)).unwrap();
e.note_inbound(t0);
let mut b = Script(|ctx: &mut TickContext<'_>| {
ctx.set_controls(WALK);
seen = (ctx.server_flags(), ctx.server_sprinting());
});
e.tick(&mut b, t0);
let actions = sprint_actions(e.outbound());
e.outbound().clear();
assert_eq!(seen, (Some(0), false), "{v}");
assert_eq!(actions, [EntityAction::StopSprinting], "{v}");
assert!(!e.core.pose.sprinting, "{v}");
let mut other = bytes::BytesMut::from(&own_flags(v, 0x08).body[..]);
other[0] = 8;
let id = own_flags(v, 0).id;
e.sim
.world_packet(&RawPacket::new(id, other.freeze()))
.unwrap();
tick(&mut e, t0, |ctx| {
seen = (ctx.server_flags(), ctx.server_sprinting())
});
assert_eq!(seen, (Some(0), false), "{v}");
e.sim.reset_player();
tick(&mut e, t0, |ctx| {
seen = (ctx.server_flags(), ctx.server_sprinting())
});
assert_eq!(seen, (None, false), "{v}");
}
}
#[test]
fn dead_player_does_not_walk() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = setup(v);
tick(&mut e, t0, |ctx| ctx.set_controls(WALK));
e.handle(Inbound::Health(Health {
health: 0.0,
food: 20,
saturation: 0.0,
..Health::default()
}));
let x0 = e.core.pose.pos.x;
for _ in 0..20 {
tick(&mut e, t0, |ctx| ctx.set_controls(WALK));
}
let moved = x0 - e.core.pose.pos.x;
assert!(moved < 0.25, "{v}: moved {moved}");
assert!(e.core.pose.motion.x.abs() < 1e-3, "{v}");
}
}
#[test]
fn open_inventory_releases_the_keys() {
let t0 = Instant::now();
for v in VERSIONS {
for release in [true, false] {
let mut e = setup(v);
e.core.opts.gui_releases_keys = release;
for _ in 0..8 {
tick(&mut e, t0, |ctx| ctx.set_controls(RUN));
}
assert!(e.core.pose.sprinting, "{v}");
e.note_inbound(t0);
let mut b = Script(|ctx: &mut TickContext<'_>| {
ctx.set_controls(RUN);
ctx.open_inventory().unwrap();
});
e.tick(&mut b, t0);
let actions = sprint_actions(e.outbound());
e.outbound().clear();
if release {
assert_eq!(actions, [EntityAction::StopSprinting], "{v}");
} else {
assert!(actions.is_empty(), "{v}: {actions:?}");
}
let x0 = e.core.pose.pos.x;
for _ in 0..20 {
tick(&mut e, t0, |ctx| ctx.set_controls(RUN));
}
let moved = x0 - e.core.pose.pos.x;
assert_eq!(moved < 0.5, release, "{v}: moved {moved}");
tick(&mut e, t0, |ctx| {
ctx.set_controls(RUN);
ctx.close_window().unwrap();
});
let x1 = e.core.pose.pos.x;
for _ in 0..5 {
tick(&mut e, t0, |ctx| ctx.set_controls(RUN));
}
assert!(x1 - e.core.pose.pos.x > 0.5, "{v}");
}
}
}
#[test]
fn own_inventory_passes_new_presses_on_1_16_5() {
let t0 = Instant::now();
for v in VERSIONS {
let passes = v.caps().own_inventory_passes_keys;
assert_eq!(passes, v == GameVersion::V1_16_5, "{v}");
let mut e = setup(v);
for _ in 0..8 {
tick(&mut e, t0, |ctx| ctx.set_controls(RUN));
}
tick(&mut e, t0, |ctx| {
ctx.set_controls(RUN);
ctx.open_inventory().unwrap();
});
let x0 = e.core.pose.pos.x;
for _ in 0..20 {
tick(&mut e, t0, |ctx| ctx.set_controls(RUN));
}
let moved = x0 - e.core.pose.pos.x;
assert!(moved < 0.5, "{v}: moved {moved}");
assert!(!e.core.pose.sprinting, "{v}");
tick(&mut e, t0, |ctx| ctx.set_controls(Controls::NONE));
e.outbound().clear();
let x1 = e.core.pose.pos.x;
for _ in 0..10 {
tick(&mut e, t0, |ctx| ctx.set_controls(RUN));
}
let moved = x1 - e.core.pose.pos.x;
assert_eq!(moved > 1.0, passes, "{v}: moved {moved}");
assert_eq!(e.core.pose.sprinting, passes, "{v}");
assert!(e.core.inv.inventory_open(), "{v}");
tick(&mut e, t0, |ctx| {
ctx.set_controls(Controls::NONE);
ctx.close_window().unwrap();
});
for _ in 0..8 {
tick(&mut e, t0, |ctx| ctx.set_controls(WALK));
}
tick(&mut e, t0, |ctx| {
ctx.set_controls(WALK);
ctx.open_inventory().unwrap();
});
let strafe = Controls { left: true, ..WALK };
let z0 = e.core.pose.pos.z;
let x2 = e.core.pose.pos.x;
for _ in 0..20 {
tick(&mut e, t0, |ctx| ctx.set_controls(strafe));
}
let (dx, dz) = (x2 - e.core.pose.pos.x, z0 - e.core.pose.pos.z);
assert!(dx.abs() < 0.5, "{v}: forward stays released, moved {dx}");
assert_eq!(dz.abs() > 1.0, passes, "{v}: strafe {dz}");
}
}
#[test]
fn velocity_and_collisions() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = setup(v);
for _ in 0..3 {
tick(&mut e, t0, |_| {});
}
let mut seen = (Vec3::default(), false, false);
tick(&mut e, t0, |ctx| {
seen = (
ctx.velocity(),
ctx.horizontal_collision(),
ctx.vertical_collision(),
);
});
assert!(!seen.1 && seen.2, "{v}: standing on the floor {seen:?}");
assert_eq!(seen.0, e.core.pose.motion, "{v}");
let push = Vec3::new(0.25, 0.375, -0.125);
e.handle(Inbound::Velocity {
entity_id: 7,
motion: push,
});
tick(&mut e, t0, |ctx| seen.0 = ctx.velocity());
assert_eq!(seen.0, push, "{v}");
}
}
#[test]
fn an_attack_tick_holds_the_turn() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = sprinting_at(v, Kind::Player, t0);
let before = (e.core.pose.yaw, e.core.pose.pitch);
let aim = (before.0 + 20.0, 30.0);
let mut hit = false;
e.note_inbound(t0);
let mut b = Script(|ctx: &mut TickContext<'_>| {
ctx.set_controls(WALK);
ctx.set_aim(aim.0, aim.1);
hit = ctx.attack(50);
});
e.tick(&mut b, t0);
assert!(hit, "{v}: the attack went out");
assert_eq!(
(e.core.pose.yaw, e.core.pose.pitch),
before,
"{v}: the turn was held"
);
let looks: Vec<(f32, f32)> = e
.outbound()
.iter()
.filter_map(|o| match o {
Outbound::Move(
MovePacket::Look { yaw, pitch, .. }
| MovePacket::PositionLook { yaw, pitch, .. },
) => Some((*yaw, *pitch)),
_ => None,
})
.collect();
assert!(
looks.is_empty(),
"{v}: no new look on the attack tick: {looks:?}"
);
e.outbound().clear();
let mut b = Script(|ctx: &mut TickContext<'_>| {
ctx.set_controls(WALK);
ctx.set_aim(aim.0, aim.1);
});
e.tick(&mut b, t0);
assert_ne!(
(e.core.pose.yaw, e.core.pose.pitch),
before,
"{v}: turned a tick later"
);
}
}
#[test]
fn sprint_queries_follow_the_server_flags() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = sprinting_at(v, Kind::Player, t0);
let mut seen = Vec::new();
tick(&mut e, t0, |ctx| {
ctx.set_controls(WALK);
seen.push((ctx.sprinting(), ctx.attack_slows(50)));
assert!(ctx.attack(50));
seen.push((ctx.sprinting(), ctx.attack_slows(50)));
seen.push((ctx.pose().sprinting, ctx.attack_slows(999)));
});
assert_eq!(seen, [(true, true), (true, false), (true, false)], "{v}");
assert!(!e.core.pose.sprinting, "{v}: the hit ended the sprint");
let run = |attack: bool| {
let mut e = sprinting_at(v, Kind::Player, t0);
e.sim.world_packet(&own_flags(v, 0x08)).unwrap();
tick(&mut e, t0, |ctx| ctx.set_controls(WALK));
e.sim.world_packet(&own_flags(v, 0)).unwrap();
let mut seen = None;
tick(&mut e, t0, |ctx| {
ctx.set_controls(WALK);
seen = Some((ctx.pose().sprinting, ctx.sprinting(), ctx.attack_slows(50)));
if attack {
assert!(ctx.attack(50));
}
});
(seen, e.core.pose.motion)
};
let (plain_seen, plain) = run(false);
let (seen, hit) = run(true);
assert_eq!(seen, Some((true, false, false)), "{v}");
assert_eq!(plain_seen, seen, "{v}");
assert_eq!(hit, plain, "{v}: no slow-down");
}
}
#[test]
fn turn_rate_changes_mid_session() {
let t0 = Instant::now();
let v = VERSIONS[0];
let mut e = setup(v);
tick(&mut e, t0, |_| {});
let y0 = e.core.pose.yaw;
let mut rate = (0.0, 0.0);
tick(&mut e, t0, |ctx| {
rate = ctx.turn_rate();
ctx.set_aim(y0 + 90.0, 0.0);
});
assert_eq!(rate, (14.0, 10.5));
let d1 = e.core.pose.yaw - y0;
assert!(d1 > 13.0 && d1 < 14.5, "{d1}");
tick(&mut e, t0, |ctx| {
ctx.set_turn_rate(40.0, f64::NAN);
rate = ctx.turn_rate();
ctx.set_aim(y0 + 90.0, 0.0);
});
assert_eq!(rate, (40.0, 10.5));
let d2 = e.core.pose.yaw - y0 - d1;
assert!(d2 > 39.0 && d2 < 41.0, "{d2}");
tick(&mut e, t0, |ctx| {
rate = ctx.turn_rate();
ctx.set_aim(y0 + 90.0, 0.0);
});
assert_eq!(rate, (40.0, 10.5));
assert!((e.core.pose.yaw - y0 - 90.0).abs() < 0.2);
}
fn facing(v: GameVersion, look: (f32, f32), t0: Instant) -> Engine<Box<dyn Sim>> {
let mut e = setup(v);
tick(&mut e, t0, |_| {});
let s = e.core.pose.pos;
put(&mut e, 50, Kind::Player, Vec3::new(s.x - 2.5, s.y, s.z));
e.core.forced_look = Some(look);
for _ in 0..4 {
tick(&mut e, t0, |_| {});
}
assert_eq!((e.core.pose.yaw, e.core.pose.pitch), look, "{v}");
e
}
fn hand_order(out: &[Outbound]) -> Vec<&'static str> {
let last_hand = out.iter().rposition(|o| {
matches!(
o,
Outbound::Swing | Outbound::Punch | Outbound::UseEntity { .. }
)
});
let first_move = out.iter().position(|o| matches!(o, Outbound::Move(_)));
if let (Some(h), Some(m)) = (last_hand, first_move) {
assert!(h < m, "hand packets after the movement: {out:?}");
}
out.iter()
.filter_map(|o| match o {
Outbound::Swing | Outbound::Punch => Some("swing"),
Outbound::UseEntity {
target: 50,
action: crate::common::EntityUse::Attack,
..
} => Some("attack"),
Outbound::UseEntity { .. } => Some("other use_entity"),
_ => None,
})
.collect()
}
fn press_tick(
e: &mut Engine<Box<dyn Sim>>,
t0: Instant,
presses: usize,
mut f: impl FnMut(&mut TickContext<'_>),
) -> Vec<Outbound> {
e.note_inbound(t0);
let mut b = Script(|ctx: &mut TickContext<'_>| {
f(ctx);
for _ in 0..presses {
assert!(ctx.press(hands::Button::Left));
}
});
e.tick(&mut b, t0);
std::mem::take(e.outbound())
}
#[test]
fn queued_presses_attack_in_the_version_order() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = facing(v, (90.0, 0.0), t0);
let mut right = true;
let out = press_tick(&mut e, t0, 2, |ctx| {
right = ctx.press(hands::Button::Right);
});
assert!(!right, "{v}: the right button is not queued");
let want: &[&str] = if v.caps().combat_1_9 {
&["attack", "swing", "attack", "swing"]
} else {
&["swing", "attack", "swing", "attack"]
};
assert_eq!(hand_order(&out), want, "{v}");
if v.caps().combat_1_9 {
assert_eq!(e.core.hands.ticks_since_swing, 0, "{v}: cooldown reset");
}
let out = press_tick(&mut e, t0, 0, |_| {});
assert_eq!(hand_order(&out), [] as [&str; 0], "{v}");
}
}
#[test]
fn queued_sprint_hits_slow_once() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = sprinting_at(v, Kind::Player, t0);
let out = press_tick(&mut e, t0, 2, |ctx| ctx.set_controls(WALK));
assert_eq!(
hand_order(&out).iter().filter(|&&n| n == "attack").count(),
2,
"{v}: {out:?}"
);
assert_eq!(sprint_actions(&out), [EntityAction::StopSprinting], "{v}");
assert!(!e.core.pose.sprinting, "{v}");
}
}
#[test]
fn queued_presses_pick_after_the_turn() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = facing(v, (90.0, -10.0), t0);
let out = press_tick(&mut e, t0, 1, |ctx| ctx.set_aim(90.0, 0.0));
let want: &[&str] = if v.caps().combat_1_9 {
&["attack", "swing"]
} else {
&["swing", "attack"]
};
assert_eq!(hand_order(&out), want, "{v}");
let now = (e.core.pose.yaw, e.core.pose.pitch);
assert!(now.0 == 90.0 && now.1.abs() < 0.2, "{v}: turned {now:?}");
let looks: Vec<(f32, f32)> = out
.iter()
.filter_map(|o| match o {
Outbound::Move(
MovePacket::Look { yaw, pitch, .. }
| MovePacket::PositionLook { yaw, pitch, .. },
) => Some((*yaw, *pitch)),
_ => None,
})
.collect();
assert_eq!(looks, [now], "{v}: the pick's look goes out");
let mut e = facing(v, (90.0, -10.0), t0);
let out = press_tick(&mut e, t0, 0, |ctx| {
ctx.set_aim(90.0, 0.0);
ctx.set_buttons(Buttons {
left: true,
right: false,
});
});
assert_eq!(hand_order(&out), ["swing"], "{v}");
}
}
#[test]
fn queued_presses_share_one_pick_and_the_miss_delay() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = facing(v, (90.0, 0.0), t0);
let out = press_tick(&mut e, t0, 3, |ctx| ctx.set_aim(102.0, 0.0));
assert!((e.core.pose.yaw - 102.0).abs() < 0.2, "{v}: turned");
let order = hand_order(&out);
if v.caps().combat_1_9 {
assert_eq!(order, ["swing"], "{v}: one miss");
assert_eq!(e.core.hands.ticks_since_swing, 0, "{v}: a miss resets");
} else {
assert_eq!(
order,
["swing", "attack", "swing", "attack", "swing", "attack"],
"{v}: one pick (head yaw)"
);
}
e.core.forced_look = Some((90.0, -60.0));
tick(&mut e, t0, |_| {});
for _ in 0..2 {
let out = press_tick(&mut e, t0, 3, |_| {});
assert_eq!(hand_order(&out), ["swing"], "{v}");
}
let held = Buttons {
left: true,
right: false,
};
let out = press_tick(&mut e, t0, 2, |ctx| ctx.set_buttons(held));
assert_eq!(hand_order(&out), ["swing"], "{v}");
let out = press_tick(&mut e, t0, 2, |ctx| ctx.set_buttons(held));
assert_eq!(hand_order(&out), [] as [&str; 0], "{v}");
tick(&mut e, t0, |ctx| ctx.set_buttons(Buttons::default()));
e.core.game_mode = GameMode::Creative;
let out = press_tick(&mut e, t0, 3, |_| {});
assert_eq!(hand_order(&out), ["swing", "swing", "swing"], "{v}");
}
}
#[test]
fn queued_presses_dropped_behind_a_screen() {
let t0 = Instant::now();
for v in VERSIONS {
let mut e = facing(v, (90.0, 0.0), t0);
let out = press_tick(&mut e, t0, 2, |ctx| {
ctx.open_inventory().unwrap();
});
assert_eq!(hand_order(&out), [] as [&str; 0], "{v}");
let out = press_tick(&mut e, t0, 0, |ctx| {
ctx.close_window().unwrap();
});
assert_eq!(hand_order(&out), [] as [&str; 0], "{v}");
}
}