From 770d73f080e33ba7db71deca2a0826a84e54318c Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Fri, 9 Aug 2024 14:29:51 +0200 Subject: [PATCH] minior position/rotation improvements --- .../src/client/play/c_player_chat_message.rs | 2 +- .../src/client/play/c_remove_entities.rs | 5 ++- .../src/client/play/c_spawn_player.rs | 24 +++++------ pumpkin-text/src/click.rs | 2 +- pumpkin-text/src/color.rs | 2 +- pumpkin-text/src/lib.rs | 8 ++-- pumpkin/src/client/player_packet.rs | 40 +++++++++++++------ pumpkin/src/commands/pumpkin.rs | 6 +-- pumpkin/src/server.rs | 27 +++++++------ pumpkin/src/util/math.rs | 12 ++++++ pumpkin/src/util/mod.rs | 2 +- 11 files changed, 80 insertions(+), 50 deletions(-) create mode 100644 pumpkin/src/util/math.rs diff --git a/pumpkin-protocol/src/client/play/c_player_chat_message.rs b/pumpkin-protocol/src/client/play/c_player_chat_message.rs index 79b403dab..a08aaf99b 100644 --- a/pumpkin-protocol/src/client/play/c_player_chat_message.rs +++ b/pumpkin-protocol/src/client/play/c_player_chat_message.rs @@ -3,7 +3,7 @@ use pumpkin_macros::packet; use pumpkin_text::TextComponent; use serde::Serialize; -use crate::{ VarInt}; +use crate::VarInt; #[derive(Serialize, Clone)] #[packet(0x39)] diff --git a/pumpkin-protocol/src/client/play/c_remove_entities.rs b/pumpkin-protocol/src/client/play/c_remove_entities.rs index 22477d744..bf711da8b 100644 --- a/pumpkin-protocol/src/client/play/c_remove_entities.rs +++ b/pumpkin-protocol/src/client/play/c_remove_entities.rs @@ -12,6 +12,9 @@ pub struct CRemoveEntities { impl CRemoveEntities { pub fn new(entitiy_ids: Vec) -> Self { - Self { count: VarInt(entitiy_ids.len() as i32), entitiy_ids } + Self { + count: VarInt(entitiy_ids.len() as i32), + entitiy_ids, + } } } diff --git a/pumpkin-protocol/src/client/play/c_spawn_player.rs b/pumpkin-protocol/src/client/play/c_spawn_player.rs index 9bfa4a694..1efec8c5a 100644 --- a/pumpkin-protocol/src/client/play/c_spawn_player.rs +++ b/pumpkin-protocol/src/client/play/c_spawn_player.rs @@ -29,13 +29,13 @@ impl CSpawnEntity { x: f64, y: f64, z: f64, - pitch: u8, // angle - yaw: u8, // angle - head_yaw: u8, // angle + pitch: f32, // angle + yaw: f32, // angle + head_yaw: f32, // angle data: VarInt, - velocity_x: i16, - velocity_y: i16, - velocity_z: i16, + velocity_x: f32, + velocity_y: f32, + velocity_z: f32, ) -> Self { Self { entity_id, @@ -44,13 +44,13 @@ impl CSpawnEntity { x, y, z, - pitch, - yaw, - head_yaw, + pitch: (pitch * 256.0 / 360.0).floor() as u8, + yaw: (yaw * 256.0 / 360.0).floor() as u8, + head_yaw: (head_yaw * 256.0 / 360.0).floor() as u8, data, - velocity_x, - velocity_y, - velocity_z, + velocity_x: (velocity_x.clamp(-3.9, 3.9) * 8000.0) as i16, + velocity_y: (velocity_y.clamp(-3.9, 3.9) * 8000.0) as i16, + velocity_z: (velocity_z.clamp(-3.9, 3.9) * 8000.0) as i16, } } } diff --git a/pumpkin-text/src/click.rs b/pumpkin-text/src/click.rs index 5bdc91694..c1656e28b 100644 --- a/pumpkin-text/src/click.rs +++ b/pumpkin-text/src/click.rs @@ -16,4 +16,4 @@ pub enum ClickEvent { ChangePage(i32), /// Copies the given text to system clipboard CopyToClipboard(String), -} \ No newline at end of file +} diff --git a/pumpkin-text/src/color.rs b/pumpkin-text/src/color.rs index f4afea427..6fa25219b 100644 --- a/pumpkin-text/src/color.rs +++ b/pumpkin-text/src/color.rs @@ -33,4 +33,4 @@ pub enum NamedColor { LightPurple, Yellow, White, -} \ No newline at end of file +} diff --git a/pumpkin-text/src/lib.rs b/pumpkin-text/src/lib.rs index 4913a8f4f..f18c04188 100644 --- a/pumpkin-text/src/lib.rs +++ b/pumpkin-text/src/lib.rs @@ -6,8 +6,8 @@ use fastnbt::SerOpts; use hover::HoverEvent; use serde::{Deserialize, Serialize}; -pub mod color; pub mod click; +pub mod color; pub mod hover; #[derive(Clone, Default, Debug, Serialize, Deserialize)] @@ -163,9 +163,9 @@ impl TextComponent { click_event: &self.click_event, hover_event: &self.hover_event, }; - // dbg!(&serde_json::to_string(&astruct)); - let nbt = fastnbt::to_bytes_with_opts(&astruct, SerOpts::network_nbt()).unwrap(); - nbt + // dbg!(&serde_json::to_string(&astruct)); + + fastnbt::to_bytes_with_opts(&astruct, SerOpts::network_nbt()).unwrap() } } diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index ea6625cf1..9163561a4 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -15,6 +15,7 @@ use crate::{ commands::{handle_command, CommandSender}, entity::player::Hand, server::Server, + util::math::wrap_degrees, }; use super::Client; @@ -38,18 +39,27 @@ impl Client { } } + fn clamp_horizontal(pos: f64) -> f64 { + pos.clamp(-3.0E7, 3.0E7) + } + + fn clamp_vertical(pos: f64) -> f64 { + pos.clamp(-2.0E7, 2.0E7) + } + pub fn handle_position(&mut self, server: &mut Server, position: SPlayerPosition) { if position.x.is_nan() || position.feet_y.is_nan() || position.z.is_nan() { self.kick("Invalid movement"); + return; } let player = self.player.as_mut().unwrap(); let entity = &mut player.entity; entity.lastx = entity.x; entity.lasty = entity.y; entity.lastz = entity.z; - entity.x = position.x; - entity.y = position.feet_y; - entity.z = position.z; + entity.x = Self::clamp_horizontal(position.x); + entity.y = Self::clamp_vertical(position.feet_y); + entity.z = Self::clamp_horizontal(position.z); // TODO: teleport when moving > 8 block // send new position to all other players @@ -81,18 +91,20 @@ impl Client { || position_rotation.z.is_nan() { self.kick("Invalid movement"); + return; } if !position_rotation.yaw.is_finite() || !position_rotation.pitch.is_finite() { self.kick("Invalid rotation"); + return; } let player = self.player.as_mut().unwrap(); let entity = &mut player.entity; - entity.x = position_rotation.x; - entity.y = position_rotation.feet_y; - entity.z = position_rotation.z; - entity.yaw = position_rotation.yaw % 360.0; - entity.pitch = position_rotation.pitch.clamp(-90.0, 90.0) % 360.0; + entity.x = Self::clamp_horizontal(position_rotation.x); + entity.y = Self::clamp_vertical(position_rotation.feet_y); + entity.z = Self::clamp_horizontal(position_rotation.z); + entity.yaw = wrap_degrees(position_rotation.yaw); + entity.pitch = wrap_degrees(position_rotation.pitch); // send new position to all other players let on_ground = player.on_ground; @@ -122,22 +134,24 @@ impl Client { pub fn handle_rotation(&mut self, server: &mut Server, rotation: SPlayerRotation) { if !rotation.yaw.is_finite() || !rotation.pitch.is_finite() { self.kick("Invalid rotation"); + return; } let player = self.player.as_mut().unwrap(); let entity = &mut player.entity; - entity.yaw = rotation.yaw % 360.0; - entity.pitch = rotation.pitch.clamp(-90.0, 90.0) % 360.0; + entity.yaw = wrap_degrees(rotation.yaw); + entity.pitch = wrap_degrees(rotation.pitch); // send new position to all other players let on_ground = player.on_ground; let entity_id = entity.entity_id; - let yaw = entity.yaw; - let pitch = entity.pitch; + let yaw = (entity.yaw * 256.0 / 360.0).floor(); + let pitch = (entity.pitch * 256.0 / 360.0).floor(); + let head_yaw = (entity.head_yaw * 256.0 / 360.0).floor(); server.broadcast_packet( self, CUpdateEntityRot::new(entity_id.into(), yaw as u8, pitch as u8, on_ground), ); - server.broadcast_packet(self, CHeadRot::new(entity_id.into(), yaw as u8)); + server.broadcast_packet(self, CHeadRot::new(entity_id.into(), head_yaw as u8)); } pub fn handle_chat_command(&mut self, _server: &mut Server, command: SChatCommand) { diff --git a/pumpkin/src/commands/pumpkin.rs b/pumpkin/src/commands/pumpkin.rs index 3cdbeb83d..0b6eabc76 100644 --- a/pumpkin/src/commands/pumpkin.rs +++ b/pumpkin/src/commands/pumpkin.rs @@ -1,7 +1,5 @@ -use pumpkin_protocol::{ - CURRENT_MC_PROTOCOL, -}; -use pumpkin_text::{click::ClickEvent, color::NamedColor, hover::HoverEvent, TextComponent}; +use pumpkin_protocol::CURRENT_MC_PROTOCOL; +use pumpkin_text::{color::NamedColor, TextComponent}; use crate::server::CURRENT_MC_VERSION; diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index 3cc11a6cd..afcf829a2 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -165,6 +165,8 @@ impl Server { let x = 10.0; let y = 500.0; let z = 10.0; + let yaw = 10.0; + let pitch = 10.0; client.teleport(x, y, z, 10.0, 10.0); let gameprofile = client.gameprofile.as_ref().unwrap(); // first send info update to our new player, So he can see his Skin @@ -208,6 +210,7 @@ impl Server { // spawn player for every client self.broadcast_packet_expect( client, + // TODO: add velo CSpawnEntity::new( entity_id.into(), gameprofile.id, @@ -215,13 +218,13 @@ impl Server { x, y, z, - 0, - 0, - 0, + pitch, + yaw, + yaw, 0.into(), - 0, - 0, - 0, + 0.0, + 0.0, + 0.0, ), ); // spawn players for our client @@ -238,13 +241,13 @@ impl Server { entity.x, entity.y, entity.z, - entity.yaw as u8, - entity.pitch as u8, - 0, + entity.yaw, + entity.pitch, + entity.pitch, 0.into(), - 0, - 0, - 0, + 0.0, + 0.0, + 0.0, )) } } diff --git a/pumpkin/src/util/math.rs b/pumpkin/src/util/math.rs new file mode 100644 index 000000000..2d90277d4 --- /dev/null +++ b/pumpkin/src/util/math.rs @@ -0,0 +1,12 @@ +pub fn wrap_degrees(var: f32) -> f32 { + let mut var1 = var % 360.0; + if var1 >= 180.0 { + var1 -= 360.0; + } + + if var1 < -180.0 { + var1 += 360.0; + } + + var1 +} diff --git a/pumpkin/src/util/mod.rs b/pumpkin/src/util/mod.rs index 8b1378917..b8135c3c3 100644 --- a/pumpkin/src/util/mod.rs +++ b/pumpkin/src/util/mod.rs @@ -1 +1 @@ - +pub mod math;