Skip to content

Commit

Permalink
up all commits done so far
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryntet committed Oct 11, 2024
1 parent ba2903a commit 935359f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 39 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ derive_more = { version = "1.0.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }

itertools = "0.13.0"

[profile.dev]
opt-level = 2
5 changes: 3 additions & 2 deletions pumpkin-world/src/coordinates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::ops::Deref;

use crate::{WORLD_LOWEST_Y, WORLD_MAX_Y};
use derive_more::derive::{AsMut, AsRef, Display, Into};
use num_traits::real::Real;
use num_traits::{PrimInt, Signed, Unsigned};
use pumpkin_core::math::position::WorldPosition;
use pumpkin_core::math::vector2::Vector2;
Expand Down Expand Up @@ -122,9 +123,9 @@ impl From<WorldPosition> for ChunkRelativeBlockCoordinates {
impl From<Vector3<f64>> for ChunkRelativeBlockCoordinates {
fn from(pos: Vector3<f64>) -> Self {
Self {
x: ChunkRelativeOffset((pos.x / 16.) as u8),
x: ChunkRelativeOffset((pos.x / 16.).round() as u8),
y: Height(pos.y.ceil() as i16),
z: ChunkRelativeOffset((pos.z / 16.) as u8),
z: ChunkRelativeOffset((pos.z / 16.).round() as u8),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl Player {
None => {
self.kick(TextComponent::text("Invalid hand"));
}
};
}
}

pub async fn handle_chat_message(&self, _server: &Arc<Server>, chat_message: SChatMessage) {
Expand Down
9 changes: 6 additions & 3 deletions pumpkin/src/entity/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,17 @@ impl ItemEntity {
dbg!(self.entity.pos.load().y);
}*/
//self.entity.bounds_check().await;
self.entity.collision_check(&server).await;
self.entity.collision_check(false).await;
let on_ground = self.entity.on_ground.load(Ordering::Relaxed);
if !on_ground
|| self.entity.velocity.load().horizontal_length_squared() > 1.0e-5
|| ticks % 4 == 0
{
self.entity.advance_position().await;
let slipperiness = 0.98;
self.entity.collision_check(true).await;
let on_ground = self.entity.on_ground.load(Ordering::Relaxed);
let slipperiness = 0.98 * if on_ground { 0.6 } else { 1. };

let mut velocity =
self.entity
.velocity
Expand All @@ -224,7 +227,7 @@ impl ItemEntity {
velocity.y = 0.;
velocity.x = 0.;
}
if self.entity.on_ground.load(Ordering::Relaxed) {
if on_ground {
if velocity.y < 0. {
velocity = velocity.multiply(1., -0.5, 1.);
}
Expand Down
55 changes: 22 additions & 33 deletions pumpkin/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use itertools::Itertools;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::float::FloatCore;
use num_traits::real::Real;
use num_traits::{Float, Signed, ToPrimitive};
use num_traits::{Float, ToPrimitive};
use pumpkin_core::math::{
get_section_cord, position::WorldPosition, vector2::Vector2, vector3::Vector3,
};
Expand Down Expand Up @@ -159,16 +159,15 @@ impl Entity {

pub async fn advance_position(&self) {
let mut velocity = self.velocity.load();
if self.on_ground.load(Ordering::Relaxed)
&& (self.pos.load().y + velocity.y).ceil() < self.pos.load().y.ceil()
{
velocity.y = self.pos.load().y.floor() - (self.pos.load().y + velocity.y)
let on_ground = self.on_ground.load(Ordering::Relaxed);
if on_ground && velocity.y.is_sign_negative() {
velocity = velocity.multiply(1., 0., 1.);
}
let pos = self.pos.load().add(&velocity);
self.set_pos(pos.x, pos.y, pos.z);
}

async fn collision_check(&self, server: &Arc<Server>) {
async fn collision_check(&self, snap: bool) {
// TODO: Collision check with other entities.

let pos = self.pos.load();
Expand All @@ -177,8 +176,8 @@ impl Entity {
let mut chunks = vec![];
for future_position in &future_positions {
// TODO Change rounding based on velocity direction
let x_section = get_section_cord(future_position.x.round() as i32);
let z_section = get_section_cord(future_position.z.round() as i32);
let x_section = get_section_cord(future_position.x.floor() as i32);
let z_section = get_section_cord(future_position.z.floor() as i32);
let chunk_pos = Vector2::new(x_section, z_section);
if !chunks.contains(&chunk_pos) {
chunks.push(chunk_pos)
Expand Down Expand Up @@ -207,34 +206,24 @@ impl Entity {
.expect("This should be received")
.blocks
.get_block(ChunkRelativeBlockCoordinates::from(Vector3 {
x: future_position.x,
z: future_position.z,
y: future_position.y.ceil(),
x: future_position.x.floor(),
z: future_position.z.floor(),
y: future_position.y.floor(),
}));

if !block_id.is_air() {
let on_ground = self.on_ground.load(Ordering::Relaxed);
let mut new_pos = pos;
let mut velocity = self.velocity.load();
if (pos.x.floor() - future_position.x.floor()).abs() > 1. && !on_ground {
velocity = velocity.multiply(0., 1., 1.);
}
if (pos.z.floor() - future_position.z.floor()).abs() > 1. && !on_ground {
velocity = velocity.multiply(1., 1., 0.);
}
if pos.y.abs().floor() > future_position.y.abs().floor() {
velocity = velocity.multiply(1., 0., 1.);
if !on_ground {
new_pos = pos.multiply(1., 0., 1.).add(&Vector3::new(
0.,
future_position.y + 1.,
0.,
));
if pos.y > future_position.y && !self.on_ground.load(Ordering::Relaxed) {
let mut new_pos = pos;
new_pos.y = pos.y.floor();
if self.on_ground.load(Ordering::Relaxed) {
let velocity = self.velocity.load();
self.velocity.store(velocity.multiply(1., 0., 1.));
}
self.on_ground.store(true, Ordering::Relaxed);
if snap {
self.set_pos(new_pos.x, new_pos.y, new_pos.z);
}
}
self.pos.store(new_pos);
self.velocity.store(velocity);
} else {
self.on_ground.store(false, Ordering::Relaxed);
}
Expand Down Expand Up @@ -387,8 +376,8 @@ impl Entity {
let block = chunks
.iter()
.find_map(|chunk| {
if chunk.position.x == get_section_cord(pos.x.round() as i32)
&& chunk.position.z == get_section_cord(pos.z.round() as i32)
if chunk.position.x == get_section_cord(pos.x.ceil() as i32)
&& chunk.position.z == get_section_cord(pos.z.ceil() as i32)
{
Some(chunk.blocks.get_block(pos.into()))
} else {
Expand Down Expand Up @@ -527,7 +516,7 @@ impl Entity {
self.world.broadcast_packet_all(&packet);
}

pub async fn set_pose(&self, pose: EntityPose) {
pub fn set_pose(&self, pose: EntityPose) {
self.pose.store(pose);
let pose = pose as i32;
let packet = CSetEntityMetadata::<VarInt>::new(
Expand Down

0 comments on commit 935359f

Please sign in to comment.