use super::tests::{engine, load};
use super::*;
use crate::fishing::{BobberState, FishingEvent};
use ghast_protocol::RawPacket;
const ME: i32 = 5;
const OTHER: i32 = 6;
const POS: [f64; 3] = [8.5, 70.0, 8.5];
#[derive(Default)]
struct W(Vec<u8>);
impl W {
fn varint(mut self, v: i32) -> W {
let mut v = v as u32;
loop {
let b = (v & 0x7f) as u8;
v >>= 7;
if v == 0 {
self.0.push(b);
return self;
}
self.0.push(b | 0x80);
}
}
fn bytes(mut self, b: &[u8]) -> W {
self.0.extend_from_slice(b);
self
}
fn raw(self, id: i32) -> RawPacket {
RawPacket {
id,
body: self.0.into(),
}
}
}
struct V {
game: GameVersion,
spawn: fn(i32, i32) -> RawPacket,
bite: fn(i32, [f64; 3]) -> RawPacket,
hooked: Option<fn(i32, i32) -> RawPacket>,
destroy: fn(i32) -> RawPacket,
bite_packet: i32,
login: Option<fn() -> RawPacket>,
}
fn uuid(e: i32) -> [u8; 16] {
(u128::from(e as u32) | 7 << 64).to_be_bytes()
}
fn fixed(v: f64) -> [u8; 4] {
((v * 32.0).floor() as i32).to_be_bytes()
}
fn eighth(v: f64) -> [u8; 4] {
((v * 8.0) as i32).to_be_bytes()
}
fn f64s(w: W, p: [f64; 3]) -> W {
w.bytes(&p[0].to_be_bytes())
.bytes(&p[1].to_be_bytes())
.bytes(&p[2].to_be_bytes())
}
fn v1_8() -> V {
use ghast_protocol::v1_8::play::clientbound::id;
V {
game: GameVersion::V1_8_9,
spawn: |e, owner| {
W::default()
.varint(e)
.bytes(&[90])
.bytes(&fixed(POS[0]))
.bytes(&fixed(POS[1]))
.bytes(&fixed(POS[2]))
.bytes(&[0, 0])
.bytes(&owner.to_be_bytes())
.bytes(&[0; 6])
.raw(id::SPAWN_ENTITY)
},
bite: |_, p| {
W::default()
.varint(13)
.bytes(b"random.splash")
.bytes(&eighth(p[0]))
.bytes(&eighth(p[1]))
.bytes(&eighth(p[2]))
.bytes(&0.25f32.to_be_bytes())
.bytes(&[63])
.raw(id::NAMED_SOUND_EFFECT)
},
hooked: None,
destroy: |e| W::default().varint(1).varint(e).raw(id::ENTITY_DESTROY),
bite_packet: id::NAMED_SOUND_EFFECT,
login: Some(|| {
W::default()
.bytes(&ME.to_be_bytes())
.bytes(&[0, 0, 1, 20, 4])
.bytes(b"flat")
.bytes(&[0])
.raw(id::LOGIN)
}),
}
}
fn v1_12() -> V {
use ghast_protocol::v1_12_2::play::clientbound::id;
V {
game: GameVersion::V1_12_2,
spawn: |e, owner| {
f64s(W::default().varint(e).bytes(&uuid(e)).bytes(&[90]), POS)
.bytes(&[0, 0])
.bytes(&owner.to_be_bytes())
.bytes(&[0; 6])
.raw(id::SPAWN_ENTITY)
},
bite: |_, p| {
W::default()
.varint(153)
.varint(6)
.bytes(&eighth(p[0]))
.bytes(&eighth(p[1]))
.bytes(&eighth(p[2]))
.bytes(&0.25f32.to_be_bytes())
.bytes(&1.0f32.to_be_bytes())
.raw(id::SOUND_EFFECT)
},
hooked: Some(|e, h| {
W::default()
.varint(e)
.bytes(&[6])
.varint(1)
.varint(h)
.bytes(&[0xff])
.raw(id::ENTITY_METADATA)
}),
destroy: |e| W::default().varint(1).varint(e).raw(id::ENTITY_DESTROY),
bite_packet: id::SOUND_EFFECT,
login: Some(|| {
W::default()
.bytes(&ME.to_be_bytes())
.bytes(&[0])
.bytes(&0i32.to_be_bytes())
.bytes(&[1, 20, 4])
.bytes(b"flat")
.bytes(&[0])
.raw(id::LOGIN)
}),
}
}
fn v1_16_5() -> V {
use ghast_protocol::v1_16_5::play::clientbound::id;
V {
game: GameVersion::V1_16_5,
spawn: |e, owner| {
let ty = i32::from(
ghast_data::v1_16_5::entity_by_name("fishing_bobber")
.unwrap()
.id,
);
f64s(W::default().varint(e).bytes(&uuid(e)).varint(ty), POS)
.bytes(&[0, 0])
.bytes(&owner.to_be_bytes())
.bytes(&[0; 6])
.raw(id::SPAWN_ENTITY)
},
bite: |e, _| {
W::default()
.varint(e)
.bytes(&[8])
.varint(7)
.bytes(&[1, 0xff])
.raw(id::ENTITY_METADATA)
},
hooked: Some(|e, h| {
W::default()
.varint(e)
.bytes(&[7])
.varint(1)
.varint(h)
.bytes(&[0xff])
.raw(id::ENTITY_METADATA)
}),
destroy: |e| W::default().varint(1).varint(e).raw(id::ENTITY_DESTROY),
bite_packet: id::ENTITY_METADATA,
login: None,
}
}
fn v26_1() -> V {
use ghast_protocol::v26_1::play::clientbound as cb;
V {
game: GameVersion::V26_1,
spawn: |e, owner| {
let ty = ghast_data::v26_1::entity_by_name("fishing_bobber")
.unwrap()
.id;
let p = cb::SpawnEntity {
entity_id: e,
object_uuid: uuid::Uuid::from_u64_pair(1, e as u64),
r#type: i32::from(ty),
x: POS[0],
y: POS[1],
z: POS[2],
velocity: ghast_protocol::types::LpVec3::ZERO,
pitch: 0,
yaw: 0,
head_pitch: 0,
object_data: owner,
};
let mut enc = ghast_protocol::FrameEncoder::new();
enc.write_packet(&p).unwrap();
let out = enc.out_mut().split().freeze();
let mut r = &out[..];
for _ in 0..2 {
while r[0] & 0x80 != 0 {
r = &r[1..];
}
r = &r[1..];
}
RawPacket {
id: cb::id::SPAWN_ENTITY,
body: bytes::Bytes::copy_from_slice(r),
}
},
bite: |e, _| {
W::default()
.varint(e)
.bytes(&[9])
.varint(8)
.bytes(&[1, 0xff])
.raw(cb::id::ENTITY_METADATA)
},
hooked: Some(|e, h| {
W::default()
.varint(e)
.bytes(&[8])
.varint(1)
.varint(h)
.bytes(&[0xff])
.raw(cb::id::ENTITY_METADATA)
}),
destroy: |e| W::default().varint(1).varint(e).raw(cb::id::ENTITY_DESTROY),
bite_packet: cb::id::ENTITY_METADATA,
login: None,
}
}
fn events(e: &mut Engine<Box<dyn Sim>>) -> Vec<FishingEvent> {
let mut v = Vec::new();
if e.sim.fishing_news() {
e.sim.drain_fishing(&mut v);
}
v
}
fn check(v: &V) {
let mut e = engine(v.game);
e.sim.set_entities(crate::client::entity_config(
crate::Interests::ENTITY_TRACKING,
v.game.caps(),
));
e.sim.test_set_own_id(ME);
if let Some(login) = v.login {
e.sim.world_packet(&login()).unwrap();
}
load(&mut e, 0, 0);
let mut pose = e.core.pose;
let g = v.game;
let sound = matches!(g, GameVersion::V1_8_9 | GameVersion::V1_12_2);
let packet = |e: &mut Engine<Box<dyn Sim>>, raw: &RawPacket| {
e.sim.world_packet(raw).unwrap();
};
packet(&mut e, &(v.spawn)(40, OTHER));
assert!(e.sim.fishing().is_none(), "{g:?}: another player's bobber");
assert!(events(&mut e).is_empty());
if sound {
assert!(!e.sim.entity_packet_ids().contains(&v.bite_packet));
}
packet(&mut e, &(v.spawn)(41, ME));
let t = (
e.sim
.entities()
.unwrap()
.get(41)
.map(|x| (x.entity_type(), x.data())),
e.sim
.entities()
.unwrap()
.iter()
.map(|x| x.id())
.collect::<Vec<_>>(),
);
let b = *e
.sim
.fishing()
.unwrap_or_else(|| panic!("{g:?}: our bobber (tracked: {t:?})"));
assert_eq!((b.id, b.state, b.biting), (41, BobberState::Flying, false));
assert!(e.sim.take_filter_dirty(), "{g:?}: the filter grows");
assert!(e.sim.entity_packet_ids().contains(&v.bite_packet));
e.sim.tick_entities(&mut pose);
assert_eq!(e.sim.fishing().unwrap().age, 1);
if sound {
packet(&mut e, &(v.bite)(41, [POS[0] + 5.0, POS[1], POS[2]]));
assert!(!e.sim.fishing().unwrap().biting, "{g:?}: a far splash");
}
packet(&mut e, &(v.bite)(41, POS));
assert!(e.sim.fishing().unwrap().biting, "{g:?}: the bite");
if let Some(h) = v.hooked {
packet(&mut e, &h(41, 41));
let b = *e.sim.fishing().unwrap();
assert_eq!((b.state, b.hooked), (BobberState::Hooked, Some(40)));
}
packet(&mut e, &(v.destroy)(41));
e.sim.tick_entities(&mut pose);
assert!(e.sim.fishing().is_none(), "{g:?}: gone");
assert!(e.sim.take_filter_dirty(), "{g:?}: the filter shrinks");
if sound {
assert!(!e.sim.entity_packet_ids().contains(&v.bite_packet));
}
let mut want = vec![FishingEvent::Cast { id: 41 }, FishingEvent::Bite { id: 41 }];
if v.hooked.is_some() {
want.push(FishingEvent::Hooked { id: 41, entity: 40 });
}
want.push(FishingEvent::Gone { id: 41 });
assert_eq!(events(&mut e), want, "{g:?}");
}
#[test]
fn own_bobber_1_8_9() {
check(&v1_8());
}
#[test]
fn own_bobber_1_12_2() {
check(&v1_12());
}
#[test]
fn own_bobber_1_16_5() {
check(&v1_16_5());
}
#[test]
fn own_bobber_26_1() {
check(&v26_1());
}
#[test]
fn behaviour_sees_the_bobber() {
struct Watch(Vec<FishingEvent>, Option<i32>);
impl Behavior for Watch {
fn name(&self) -> &str {
"fishing-watch"
}
fn tick(&mut self, ctx: &mut TickContext<'_>) {
self.0.extend_from_slice(ctx.events().fishing());
self.1 = ctx.fishing().map(|b| b.id);
}
}
let v = v1_16_5();
let mut e = engine(v.game);
super::tests::join(&mut e);
e.sim.set_entities(crate::client::entity_config(
crate::Interests::ENTITY_TRACKING,
v.game.caps(),
));
e.sim.test_set_own_id(ME);
let mut b = Watch(Vec::new(), None);
e.tick(&mut b, Instant::now());
e.sim.world_packet(&(v.spawn)(41, ME)).unwrap();
e.sim.world_packet(&(v.bite)(41, POS)).unwrap();
e.tick(&mut b, Instant::now());
assert_eq!(
b.0,
[FishingEvent::Cast { id: 41 }, FishingEvent::Bite { id: 41 }]
);
assert_eq!(b.1, Some(41));
}