Skip to content

Commit

Permalink
Implement EntityPositionSync (#196)
Browse files Browse the repository at this point in the history
* implement EntityPositionSync

* fix EntityPositionSync setting the wrong vec_delta_codec and also move into a RelativeEntityUpdate
  • Loading branch information
mat-1 authored Dec 12, 2024
1 parent 2393200 commit e9136c9
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 106 deletions.
13 changes: 7 additions & 6 deletions azalea-client/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub fn send_position(
pos: **position,
x_rot: direction.x_rot,
y_rot: direction.y_rot,
on_ground: physics.on_ground,
on_ground: physics.on_ground(),
}
.into_variant(),
)
Expand All @@ -205,7 +205,7 @@ pub fn send_position(
x: position.x,
y: position.y,
z: position.z,
on_ground: physics.on_ground,
on_ground: physics.on_ground(),
}
.into_variant(),
)
Expand All @@ -214,14 +214,14 @@ pub fn send_position(
ServerboundMovePlayerRot {
x_rot: direction.x_rot,
y_rot: direction.y_rot,
on_ground: physics.on_ground,
on_ground: physics.on_ground(),
}
.into_variant(),
)
} else if physics.last_on_ground != physics.on_ground {
} else if physics.last_on_ground() != physics.on_ground() {
Some(
ServerboundMovePlayerStatusOnly {
on_ground: physics.on_ground,
on_ground: physics.on_ground(),
}
.into_variant(),
)
Expand All @@ -238,7 +238,8 @@ pub fn send_position(
last_direction.x_rot = direction.x_rot;
}

physics.last_on_ground = physics.on_ground;
let on_ground = physics.on_ground();
physics.set_last_on_ground(on_ground);
// minecraft checks for autojump here, but also autojump is bad so

packet
Expand Down
142 changes: 111 additions & 31 deletions azalea-client/src/packet_handling/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,16 @@ pub fn process_packet_events(ecs: &mut World) {
let new_y = apply_change(position.y, p.relative.y, p.change.pos.y);
let new_z = apply_change(position.z, p.relative.z, p.change.pos.z);

let new_y_rot = apply_change(direction.y_rot, p.relative.y_rot, p.change.y_rot);
let new_x_rot = apply_change(direction.x_rot, p.relative.x_rot, p.change.x_rot);
let new_y_rot = apply_change(
direction.y_rot,
p.relative.y_rot,
p.change.look_direction.y_rot,
);
let new_x_rot = apply_change(
direction.x_rot,
p.relative.x_rot,
p.change.look_direction.x_rot,
);

let mut new_delta_from_rotations = physics.velocity;
if p.relative.rotate_delta {
Expand Down Expand Up @@ -829,30 +837,29 @@ pub fn process_packet_events(ecs: &mut World) {
let (mut commands, mut query) = system_state.get_mut(ecs);
let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap();

let entity = entity_id_index.get(&MinecraftEntityId(p.id));

if let Some(entity) = entity {
let new_pos = p.position;
let new_look_direction = LookDirection {
x_rot: (p.x_rot as i32 * 360) as f32 / 256.,
y_rot: (p.y_rot as i32 * 360) as f32 / 256.,
};
commands.entity(entity).queue(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity| {
let mut position = entity.get_mut::<Position>().unwrap();
if new_pos != **position {
**position = new_pos;
}
let mut look_direction = entity.get_mut::<LookDirection>().unwrap();
if new_look_direction != *look_direction {
*look_direction = new_look_direction;
}
}),
});
} else {
let Some(entity) = entity_id_index.get(&MinecraftEntityId(p.id)) else {
warn!("Got teleport entity packet for unknown entity id {}", p.id);
}
continue;
};

let new_pos = p.position;
let new_look_direction = LookDirection {
x_rot: (p.x_rot as i32 * 360) as f32 / 256.,
y_rot: (p.y_rot as i32 * 360) as f32 / 256.,
};
commands.entity(entity).queue(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity| {
let mut position = entity.get_mut::<Position>().unwrap();
if new_pos != **position {
**position = new_pos;
}
let mut look_direction = entity.get_mut::<LookDirection>().unwrap();
if new_look_direction != *look_direction {
*look_direction = new_look_direction;
}
}),
});

system_state.apply(ecs);
}
Expand All @@ -870,15 +877,26 @@ pub fn process_packet_events(ecs: &mut World) {
let (mut commands, mut query) = system_state.get_mut(ecs);
let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap();

debug!("Got move entity pos packet {p:?}");

let entity = entity_id_index.get(&MinecraftEntityId(p.entity_id));

if let Some(entity) = entity {
let delta = p.delta.clone();
let new_delta = p.delta.clone();
let new_on_ground = p.on_ground;
commands.entity(entity).queue(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity_mut| {
let mut physics = entity_mut.get_mut::<Physics>().unwrap();
let new_pos = physics.vec_delta_codec.decode(
new_delta.xa as i64,
new_delta.ya as i64,
new_delta.za as i64,
);
physics.vec_delta_codec.set_base(new_pos);
physics.set_on_ground(new_on_ground);

let mut position = entity_mut.get_mut::<Position>().unwrap();
let new_pos = position.with_delta(&delta);
if new_pos != **position {
**position = new_pos;
}
Expand All @@ -901,23 +919,36 @@ pub fn process_packet_events(ecs: &mut World) {
let (mut commands, mut query) = system_state.get_mut(ecs);
let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap();

debug!("Got move entity pos rot packet {p:?}");

let entity = entity_id_index.get(&MinecraftEntityId(p.entity_id));

if let Some(entity) = entity {
let delta = p.delta.clone();
let new_delta = p.delta.clone();
let new_look_direction = LookDirection {
x_rot: (p.x_rot as i32 * 360) as f32 / 256.,
y_rot: (p.y_rot as i32 * 360) as f32 / 256.,
};

let new_on_ground = p.on_ground;

commands.entity(entity).queue(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity_mut| {
let mut physics = entity_mut.get_mut::<Physics>().unwrap();
let new_pos = physics.vec_delta_codec.decode(
new_delta.xa as i64,
new_delta.ya as i64,
new_delta.za as i64,
);
physics.vec_delta_codec.set_base(new_pos);
physics.set_on_ground(new_on_ground);

let mut position = entity_mut.get_mut::<Position>().unwrap();
let new_pos = position.with_delta(&delta);
if new_pos != **position {
**position = new_pos;
}

let mut look_direction = entity_mut.get_mut::<LookDirection>().unwrap();
if new_look_direction != *look_direction {
*look_direction = new_look_direction;
Expand Down Expand Up @@ -949,10 +980,14 @@ pub fn process_packet_events(ecs: &mut World) {
x_rot: (p.x_rot as i32 * 360) as f32 / 256.,
y_rot: (p.y_rot as i32 * 360) as f32 / 256.,
};
let new_on_ground = p.on_ground;

commands.entity(entity).queue(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity_mut| {
let mut physics = entity_mut.get_mut::<Physics>().unwrap();
physics.set_on_ground(new_on_ground);

let mut look_direction = entity_mut.get_mut::<LookDirection>().unwrap();
if new_look_direction != *look_direction {
*look_direction = new_look_direction;
Expand Down Expand Up @@ -1416,7 +1451,7 @@ pub fn process_packet_events(ecs: &mut World) {
system_state.apply(ecs);
}

ClientboundGamePacket::StartConfiguration(_) => {
ClientboundGamePacket::StartConfiguration(_p) => {
let mut system_state: SystemState<(Commands, EventWriter<SendPacketEvent>)> =
SystemState::new(ecs);
let (mut commands, mut packet_events) = system_state.get_mut(ecs);
Expand All @@ -1434,6 +1469,52 @@ pub fn process_packet_events(ecs: &mut World) {
system_state.apply(ecs);
}

ClientboundGamePacket::EntityPositionSync(p) => {
let mut system_state: SystemState<(
Commands,
Query<(&EntityIdIndex, &InstanceHolder)>,
)> = SystemState::new(ecs);
let (mut commands, mut query) = system_state.get_mut(ecs);
let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap();

let Some(entity) = entity_id_index.get(&MinecraftEntityId(p.id)) else {
warn!("Got teleport entity packet for unknown entity id {}", p.id);
continue;
};

let new_position = p.values.pos;
let new_on_ground = p.on_ground;
let new_look_direction = p.values.look_direction;

commands.entity(entity).queue(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity_mut| {
let is_local_entity = entity_mut.get::<LocalEntity>().is_some();
let mut physics = entity_mut.get_mut::<Physics>().unwrap();

physics.vec_delta_codec.set_base(new_position);

if is_local_entity {
debug!("Ignoring entity position sync packet for local player");
return;
}

physics.set_on_ground(new_on_ground);

let mut last_sent_position =
entity_mut.get_mut::<LastSentPosition>().unwrap();
**last_sent_position = new_position;
let mut position = entity_mut.get_mut::<Position>().unwrap();
**position = new_position;

let mut look_direction = entity_mut.get_mut::<LookDirection>().unwrap();
*look_direction = new_look_direction;
}),
});

system_state.apply(ecs);
}

ClientboundGamePacket::SelectAdvancementsTab(_) => {}
ClientboundGamePacket::SetActionBarText(_) => {}
ClientboundGamePacket::SetBorderCenter(_) => {}
Expand Down Expand Up @@ -1476,7 +1557,6 @@ pub fn process_packet_events(ecs: &mut World) {
ClientboundGamePacket::ProjectilePower(_) => {}
ClientboundGamePacket::CustomReportDetails(_) => {}
ClientboundGamePacket::ServerLinks(_) => {}
ClientboundGamePacket::EntityPositionSync(_) => {}
ClientboundGamePacket::PlayerRotation(_) => {}
ClientboundGamePacket::RecipeBookAdd(_) => {}
ClientboundGamePacket::RecipeBookRemove(_) => {}
Expand Down
4 changes: 3 additions & 1 deletion azalea-core/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro_rules! vec3_impl {
($name:ident, $type:ty) => {
impl $name {
#[inline]
pub fn new(x: $type, y: $type, z: $type) -> Self {
pub const fn new(x: $type, y: $type, z: $type) -> Self {
Self { x, y, z }
}

Expand Down Expand Up @@ -223,6 +223,8 @@ pub struct Vec3 {
vec3_impl!(Vec3, f64);

impl Vec3 {
pub const ZERO: Vec3 = Vec3::new(0.0, 0.0, 0.0);

/// Get the distance of this vector to the origin by doing
/// `sqrt(x^2 + y^2 + z^2)`.
pub fn length(&self) -> f64 {
Expand Down
2 changes: 1 addition & 1 deletion azalea-entity/src/dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct EntityDimensions {
}

impl EntityDimensions {
pub fn make_bounding_box(&self, pos: &Vec3) -> AABB {
pub fn make_bounding_box(&self, pos: Vec3) -> AABB {
let radius = (self.width / 2.0) as f64;
let height = self.height as f64;
AABB {
Expand Down
Loading

0 comments on commit e9136c9

Please sign in to comment.