#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Bobber {
pub id: i32,
pub position: [f64; 3],
pub state: BobberState,
pub hooked: Option<i32>,
pub biting: bool,
pub age: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BobberState {
Flying,
Bobbing,
Hooked,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FishingEvent {
Cast {
id: i32,
},
Bite {
id: i32,
},
BiteEnded {
id: i32,
},
Hooked {
id: i32,
entity: i32,
},
Unhooked {
id: i32,
},
Gone {
id: i32,
},
}
impl FishingEvent {
#[must_use]
pub fn bobber(&self) -> i32 {
match *self {
FishingEvent::Cast { id }
| FishingEvent::Bite { id }
| FishingEvent::BiteEnded { id }
| FishingEvent::Hooked { id, .. }
| FishingEvent::Unhooked { id }
| FishingEvent::Gone { id } => id,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BiteSignal {
EntityData,
SplashSound,
}
#[must_use]
pub fn bite_signal(version: crate::GameVersion) -> BiteSignal {
if version.protocol() < 735 {
BiteSignal::SplashSound
} else {
BiteSignal::EntityData
}
}
const MAX_EVENTS: usize = 64;
#[cfg_attr(not(any(feature = "v1_8", feature = "v1_12_2")), allow(dead_code))]
const SOUND_RANGE_SQ: f64 = 4.0;
#[derive(Debug, Default)]
pub(crate) struct Fishing {
bobber: Option<Bobber>,
hooked_raw: i32,
bite_left: u8,
events: Vec<FishingEvent>,
}
impl Fishing {
#[inline(always)]
pub(crate) fn active(&self) -> bool {
self.bobber.is_some()
}
#[inline]
pub(crate) fn id(&self) -> Option<i32> {
self.bobber.map(|b| b.id)
}
#[inline]
pub(crate) fn bobber(&self) -> Option<&Bobber> {
self.bobber.as_ref()
}
#[inline(always)]
pub(crate) fn has_events(&self) -> bool {
!self.events.is_empty()
}
pub(crate) fn drain(&mut self, out: &mut Vec<FishingEvent>) {
out.append(&mut self.events);
}
fn push(&mut self, e: FishingEvent) {
if self.events.len() < MAX_EVENTS {
self.events.push(e);
}
}
pub(crate) fn spawned(&mut self, id: i32, owner: i32, own_id: i32, pos: [f64; 3]) -> bool {
if let Some(old) = self.bobber
&& old.id == id
{
self.push(FishingEvent::Gone { id });
self.bobber = None;
}
if owner != own_id {
return false;
}
let was = self.bobber.is_some();
if let Some(old) = self.bobber.take() {
self.push(FishingEvent::Gone { id: old.id });
}
self.hooked_raw = 0;
self.bite_left = 0;
self.bobber = Some(Bobber {
id,
position: pos,
state: BobberState::Flying,
hooked: None,
biting: false,
age: 0,
});
self.push(FishingEvent::Cast { id });
!was
}
pub(crate) fn gone(&mut self) -> bool {
match self.bobber.take() {
Some(b) => {
self.push(FishingEvent::Gone { id: b.id });
true
}
None => false,
}
}
#[cfg_attr(
not(any(feature = "v1_12_2", feature = "v1_16_5", ghast_modern)),
allow(dead_code)
)]
pub(crate) fn hooked(&mut self, raw: i32) {
let Some(b) = &mut self.bobber else {
return;
};
if raw == self.hooked_raw {
return;
}
self.hooked_raw = raw;
let id = b.id;
if raw > 0 {
b.hooked = Some(raw - 1);
b.state = BobberState::Hooked;
self.push(FishingEvent::Hooked {
id,
entity: raw - 1,
});
} else {
b.hooked = None;
b.state = BobberState::Flying;
self.push(FishingEvent::Unhooked { id });
}
}
#[cfg_attr(not(any(feature = "v1_16_5", ghast_modern)), allow(dead_code))]
pub(crate) fn biting(&mut self, on: bool) {
let Some(b) = &mut self.bobber else {
return;
};
if b.biting == on {
return;
}
b.biting = on;
let id = b.id;
self.push(if on {
FishingEvent::Bite { id }
} else {
FishingEvent::BiteEnded { id }
});
}
#[cfg_attr(not(any(feature = "v1_8", feature = "v1_12_2")), allow(dead_code))]
pub(crate) fn splash(
&mut self,
pos: [f64; 3],
others: impl Iterator<Item = [f64; 3]>,
window: u8,
) {
let Some(b) = &mut self.bobber else {
return;
};
let d = dist_sq(pos, b.position);
if d > SOUND_RANGE_SQ {
return;
}
let mut others = others;
if others.any(|o| dist_sq(pos, o) < d) {
return;
}
let id = b.id;
let new = !b.biting;
b.biting = true;
self.bite_left = window;
if new {
self.push(FishingEvent::Bite { id });
}
}
pub(crate) fn tick(
&mut self,
pos: Option<[f64; 3]>,
in_water: impl FnOnce([f64; 3]) -> bool,
) -> bool {
let Some(b) = &mut self.bobber else {
return false;
};
let Some(pos) = pos else {
return self.gone();
};
b.position = pos;
b.age = b.age.saturating_add(1);
if b.state == BobberState::Flying && b.hooked.is_none() && in_water(pos) {
b.state = BobberState::Bobbing;
}
if self.bite_left > 0 {
self.bite_left -= 1;
if self.bite_left == 0 {
b.biting = false;
}
}
false
}
}
#[cfg_attr(not(any(feature = "v1_8", feature = "v1_12_2")), allow(dead_code))]
fn dist_sq(a: [f64; 3], b: [f64; 3]) -> f64 {
let (x, y, z) = (a[0] - b[0], a[1] - b[1], a[2] - b[2]);
x * x + y * y + z * z
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn own_bobber_lifecycle() {
let mut f = Fishing::default();
assert!(!f.spawned(10, 7, 5, [0.0; 3]), "another player's bobber");
assert!(!f.active());
assert!(f.spawned(11, 5, 5, [1.0, 64.0, 1.0]));
assert_eq!(f.id(), Some(11));
assert!(!f.tick(Some([1.0, 63.5, 1.0]), |_| true));
assert_eq!(f.bobber().unwrap().state, BobberState::Bobbing);
f.biting(true);
f.biting(true);
f.biting(false);
f.hooked(43);
assert_eq!(f.bobber().unwrap().hooked, Some(42));
f.hooked(0);
assert!(f.tick(None, |_| false));
assert!(!f.active());
let mut ev = Vec::new();
f.drain(&mut ev);
assert_eq!(
ev,
[
FishingEvent::Cast { id: 11 },
FishingEvent::Bite { id: 11 },
FishingEvent::BiteEnded { id: 11 },
FishingEvent::Hooked { id: 11, entity: 42 },
FishingEvent::Unhooked { id: 11 },
FishingEvent::Gone { id: 11 },
]
);
}
#[test]
fn splash_window() {
let mut f = Fishing::default();
f.spawned(3, 1, 1, [0.5, 62.9, 0.5]);
f.splash([4.0, 63.0, 0.5], std::iter::empty(), 10);
assert!(!f.bobber().unwrap().biting);
f.splash([0.5, 63.0, 1.5], [[0.5, 63.0, 1.6]].into_iter(), 10);
assert!(!f.bobber().unwrap().biting);
f.splash([0.5, 63.0, 0.5], [[3.0, 63.0, 3.0]].into_iter(), 10);
assert!(f.bobber().unwrap().biting);
for _ in 0..9 {
f.tick(Some([0.5, 62.9, 0.5]), |_| true);
}
assert!(f.bobber().unwrap().biting);
f.tick(Some([0.5, 62.9, 0.5]), |_| true);
assert!(!f.bobber().unwrap().biting);
let mut ev = Vec::new();
f.drain(&mut ev);
assert_eq!(
ev,
[FishingEvent::Cast { id: 3 }, FishingEvent::Bite { id: 3 }]
);
}
#[test]
fn signals() {
use crate::GameVersion as V;
assert_eq!(bite_signal(V::V1_8_9), BiteSignal::SplashSound);
assert_eq!(bite_signal(V::V1_12_2), BiteSignal::SplashSound);
assert_eq!(bite_signal(V::V1_16_5), BiteSignal::EntityData);
assert_eq!(bite_signal(V::V26_1), BiteSignal::EntityData);
}
}