Fly thrown tridents on by themselves between server updates

This commit is contained in:
Kacpherek
2026-10-26 19:14:04 +01:00
parent cfc55bb6c6
commit 886a84dea0
3 changed files with 115 additions and 20 deletions
+13
View File
@@ -1406,6 +1406,17 @@ impl Game<'_> {
{
return;
}
// A thrown trident is an arrow: placed at once, flown on by its tick.
if let Some(t) = self.tridents.iter_mut().find(|t| t.id == id as u32) {
if path.is_some() {
t.position = Vec3::new(position[0], position[1], position[2]);
}
if let Some((yaw, pitch)) = rotation {
t.yaw = yaw;
t.pitch = pitch;
}
return;
}
// A dragon fireball keeps Entity's NO_OP InterpolationHandler too:
// the server's position replaces its own at once.
if let Some(b) = self.dragon_fireballs.iter_mut().find(|b| b.id == id as u32) {
@@ -3016,6 +3027,8 @@ impl Game<'_> {
p.velocity = v;
} else if let Some(b) = self.dragon_fireballs.iter_mut().find(|b| b.id == id as u32) {
b.velocity = v;
} else if let Some(t) = self.tridents.iter_mut().find(|t| t.id == id as u32) {
t.velocity = v;
} else if let Some(p) = self
.remote
.as_mut()
@@ -122,6 +122,8 @@ impl Game<'_> {
"minecraft:trident" => {
let mut t = super::trident::ThrownTrident::remote(id, at, yaw, pitch);
t.velocity = velocity;
// Projectile.getAddEntityPacket: the owner's id is the data.
t.owner = data as u32;
self.tridents.push(t);
}
"minecraft:firework_rocket" => {
@@ -261,6 +263,15 @@ impl Game<'_> {
(9, Value::Bool(show)) => c.show_bottom = *show,
_ => {}
}
} else if let Some(t) = self.tridents.iter_mut().find(|t| t.id == id) {
// AbstractArrow.ID_FLAGS (no physics) and IN_GROUND, and
// ThrownTrident.ID_LOYALTY.
match (index, value) {
(8, Value::Byte(flags)) => t.no_physics = flags & 2 != 0,
(10, Value::Bool(in_ground)) => t.in_ground = *in_ground,
(11, Value::Byte(loyalty)) => t.loyalty = u32::from(*loyalty as u8),
_ => {}
}
} else if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| p.id == id) {
match (&mut p.kind, index, value) {
// AbstractArrow.ID_FLAGS (critical) and IN_GROUND.
@@ -294,6 +305,25 @@ impl Game<'_> {
}
}
/// `Entity.getEyePosition` of a projectile's owner: this game's player or
/// another player the server sent.
fn remote_eye(&self, id: u32) -> Option<Vec3> {
let me = self
.remote
.as_ref()
.and_then(|r| u32::try_from(r.entity_id).ok());
if me == Some(id) {
let eye = self.eye_position()?;
return Some(Vec3::new(eye[0], eye[1], eye[2]));
}
let m = self.mobs.iter().find(|m| m.id == id)?;
Some(Vec3::new(
m.position.x,
m.position.y + m.eye_height(),
m.position.z,
))
}
/// One tick of interpolation, as `tick_remote_objects`.
pub(super) fn tick_remote_projectiles(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) {
// PrimedTnt.tick runs on a client too: it falls, bounces and burns its
@@ -326,6 +356,7 @@ impl Game<'_> {
// updates (10 to 20 ticks apart); a block stops them there, and only
// the server decides what they hit.
let mut projectiles = std::mem::take(&mut self.creatures.projectiles);
let mut tridents = std::mem::take(&mut self.tridents);
{
let w = self.mob_world_base(&[]);
let world = super::creatures::ProjWorld {
@@ -348,12 +379,12 @@ impl Game<'_> {
}
}
}
for t in &mut tridents {
t.remote_tick(&world, self.remote_eye(t.owner));
}
}
self.creatures.projectiles = projectiles;
for t in &mut self.tridents {
let id = t.id;
t.remote_step(|at| step(id, at));
}
self.tridents = tridents;
// FireworkRocketEntity.tick: a rocket attached to the gliding local
// player boosts it client-side too (attachedToEntity.isFallFlying).
let me = self
+67 -16
View File
@@ -8,6 +8,7 @@ use horizon_client::{
items::ItemStack,
mesher::Vertex,
mobs::{Damage, DamageKind, Who, anim::Poses},
projectiles::ProjectileWorld,
};
use horizon_core::{
geometry::{Aabb, Vec3},
@@ -30,6 +31,8 @@ pub(super) struct ThrownTrident {
pub(super) no_physics: bool,
returning: u32,
creative_only: bool,
/// The entity id a server sent as its owner (the one Loyalty returns it to).
pub(super) owner: u32,
}
impl ThrownTrident {
/// A trident a server sent (`handleAddEntity`): placed and moved by it.
@@ -49,12 +52,71 @@ impl ThrownTrident {
no_physics: false,
returning: 0,
creative_only: false,
owner: 0,
}
}
/// One tick of a server's client: the previous position, then `step`.
pub(super) fn remote_step(&mut self, step: impl FnOnce(Vec3) -> Vec3) {
/// `ThrownTrident.tick`'s Loyalty return: pulled up and towards the
/// owner's eye.
fn loyalty_pull(&mut self, owner_eye: Vec3) {
let v = Vec3::new(
owner_eye.x - self.position.x,
owner_eye.y - self.position.y,
owner_eye.z - self.position.z,
);
self.position.y += v.y * 0.015 * f64::from(self.loyalty);
let len = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt().max(1e-9);
let accel = 0.05 * f64::from(self.loyalty);
self.velocity = Vec3::new(
self.velocity.x * 0.95 + v.x / len * accel,
self.velocity.y * 0.95 + v.y / len * accel,
self.velocity.z * 0.95 + v.z / len * accel,
);
}
/// `ThrownTrident.tick` on a server's client: it flies on by itself
/// between the server's updates (`AbstractArrow.tick`: gravity and drag,
/// a block holds it), and Loyalty pulls it back to its owner; only the
/// server decides what it hits.
pub(super) fn remote_tick(&mut self, w: &impl ProjectileWorld, owner_eye: Option<Vec3>) {
self.previous = self.position;
self.position = step(self.position);
if self.in_ground_time > 4 {
self.dealt_damage = true;
}
if self.loyalty > 0
&& (self.dealt_damage || self.no_physics)
&& let Some(eye) = owner_eye
{
self.no_physics = true;
self.in_ground = false;
self.loyalty_pull(eye);
}
if self.in_ground && !self.no_physics {
self.in_ground_time += 1;
return;
}
self.in_ground_time = 0;
let m = self.velocity;
let (x, z) = if self.no_physics {
(-m.x, -m.z)
} else {
(m.x, m.z)
};
self.yaw = x.atan2(z).to_degrees() as f32;
self.pitch = m.y.atan2(m.x.hypot(m.z)).to_degrees() as f32;
let to = self.position.add_vector(m);
if !self.no_physics
&& let Some((at, _, _)) = w.clip(self.position, to)
{
self.position = at;
self.velocity = Vec3::ZERO;
self.in_ground = true;
return;
}
self.position = to;
// A trident's water inertia (0.99) equals the air drag.
self.velocity = self.velocity.scale(0.99);
if !self.no_physics {
self.velocity.y -= 0.05;
}
}
}
const HALF: f64 = 0.25;
@@ -240,6 +302,7 @@ impl Game<'_> {
no_physics: false,
returning: 0,
creative_only: self.creative,
owner: 0,
});
self.player_projectile_shot();
self.audio
@@ -261,19 +324,7 @@ impl Game<'_> {
if t.loyalty > 0 && (t.dealt_damage || t.no_physics) {
t.no_physics = true;
t.in_ground = false;
let v = Vec3::new(
owner_eye.x - t.position.x,
owner_eye.y - t.position.y,
owner_eye.z - t.position.z,
);
t.position.y += v.y * 0.015 * f64::from(t.loyalty);
let len = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt().max(1e-9);
let accel = 0.05 * f64::from(t.loyalty);
t.velocity = Vec3::new(
t.velocity.x * 0.95 + v.x / len * accel,
t.velocity.y * 0.95 + v.y / len * accel,
t.velocity.z * 0.95 + v.z / len * accel,
);
t.loyalty_pull(owner_eye);
if t.returning == 0 {
self.audio.play(
"minecraft:item.trident.return",