Skip to content

Commit

Permalink
minior position/rotation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 9, 2024
1 parent e96f072 commit 770d73f
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/client/play/c_player_chat_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 4 additions & 1 deletion pumpkin-protocol/src/client/play/c_remove_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub struct CRemoveEntities {

impl CRemoveEntities {
pub fn new(entitiy_ids: Vec<VarInt>) -> Self {
Self { count: VarInt(entitiy_ids.len() as i32), entitiy_ids }
Self {
count: VarInt(entitiy_ids.len() as i32),
entitiy_ids,
}
}
}
24 changes: 12 additions & 12 deletions pumpkin-protocol/src/client/play/c_spawn_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-text/src/click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ pub enum ClickEvent {
ChangePage(i32),
/// Copies the given text to system clipboard
CopyToClipboard(String),
}
}
2 changes: 1 addition & 1 deletion pumpkin-text/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ pub enum NamedColor {
LightPurple,
Yellow,
White,
}
}
8 changes: 4 additions & 4 deletions pumpkin-text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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()
}
}

Expand Down
40 changes: 27 additions & 13 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
commands::{handle_command, CommandSender},
entity::player::Hand,
server::Server,
util::math::wrap_degrees,
};

use super::Client;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions pumpkin/src/commands/pumpkin.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
27 changes: 15 additions & 12 deletions pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -208,20 +210,21 @@ impl Server {
// spawn player for every client
self.broadcast_packet_expect(
client,
// TODO: add velo
CSpawnEntity::new(
entity_id.into(),
gameprofile.id,
EntityType::Player.to_i32().unwrap().into(),
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
Expand All @@ -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,
))
}
}
Expand Down
12 changes: 12 additions & 0 deletions pumpkin/src/util/math.rs
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion pumpkin/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@

pub mod math;

0 comments on commit 770d73f

Please sign in to comment.