diff options
| -rwxr-xr-x | Cargo.lock | 1 | ||||
| -rw-r--r-- | azalea-core/src/position.rs | 2 | ||||
| -rw-r--r-- | azalea-physics/Cargo.toml | 11 | ||||
| -rw-r--r-- | azalea-physics/src/collision/mod.rs | 8 | ||||
| -rw-r--r-- | azalea-physics/src/lib.rs | 68 | ||||
| -rwxr-xr-x | azalea-protocol/src/connect.rs | 4 |
6 files changed, 73 insertions, 21 deletions
@@ -220,6 +220,7 @@ dependencies = [ "azalea-block", "azalea-core", "azalea-world", + "uuid", ] [[package]] diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index d0931a39..7451bc14 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -181,7 +181,7 @@ pub struct GlobalPos { } /// An exact point in the world. -#[derive(Debug, Clone, Copy, Default)] +#[derive(Debug, Clone, Copy, Default, PartialEq)] pub struct Vec3 { pub x: f64, pub y: f64, diff --git a/azalea-physics/Cargo.toml b/azalea-physics/Cargo.toml index 856c8ba6..688da161 100644 --- a/azalea-physics/Cargo.toml +++ b/azalea-physics/Cargo.toml @@ -2,10 +2,15 @@ edition = "2021" name = "azalea-physics" version = "0.1.0" +description = "Physics for Minecraft entities." +license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -azalea-block = {path = "../azalea-block"} -azalea-core = {path = "../azalea-core"} -azalea-world = {path = "../azalea-world"} +azalea-block = { path = "../azalea-block" } +azalea-core = { path = "../azalea-core" } +azalea-world = { path = "../azalea-world" } + +[dev-dependencies] +uuid = "^1.1.2" diff --git a/azalea-physics/src/collision/mod.rs b/azalea-physics/src/collision/mod.rs index 465698b2..fe060b90 100644 --- a/azalea-physics/src/collision/mod.rs +++ b/azalea-physics/src/collision/mod.rs @@ -147,10 +147,10 @@ impl MovableEntity for EntityMut<'_> { // TODO: minecraft checks for a "minor" horizontal collision here let block_pos_below = { self.on_pos_legacy() }; - let _block_state_below = self - .dimension - .get_block_state(&block_pos_below) - .expect("Couldn't get block state below"); + // let _block_state_below = self + // .dimension + // .get_block_state(&block_pos_below) + // .expect("Couldn't get block state below"); println!("move_entity 4"); // self.check_fall_damage(collide_result.y, on_ground, block_state_below, block_pos_below); diff --git a/azalea-physics/src/lib.rs b/azalea-physics/src/lib.rs index 642fa89e..e1c585e7 100644 --- a/azalea-physics/src/lib.rs +++ b/azalea-physics/src/lib.rs @@ -1,6 +1,6 @@ pub mod collision; -use azalea_block::Block; +use azalea_block::{Block, BlockState}; use azalea_core::{BlockPos, Vec3}; use azalea_world::entity::{EntityData, EntityMut}; use collision::{MovableEntity, MoverType}; @@ -29,22 +29,21 @@ impl HasPhysics for EntityMut<'_> { // TODO: elytra let block_pos_below = get_block_pos_below_that_affects_movement(self); - let block_friction = - if let Some(block_state_below) = self.dimension.get_block_state(&block_pos_below) { - let block_below: Box<dyn Block> = block_state_below.into(); - block_below.behavior().friction - } else { - unreachable!( - "Block below at {:?} should be a real block.", - block_pos_below - ) - }; + + let block_state_below = self + .dimension + .get_block_state(&block_pos_below) + .unwrap_or(BlockState::Air); + let block_below: Box<dyn Block> = block_state_below.into(); + let block_friction = block_below.behavior().friction; let inertia = if self.on_ground { block_friction * 0.91 } else { 0.91 }; + + // this applies the current delta let mut movement = handle_relative_friction_and_calculate_movement(self, acceleration, block_friction); @@ -73,6 +72,15 @@ impl HasPhysics for EntityMut<'_> { fn ai_step(&mut self) { // vanilla does movement interpolation here, doesn't really matter much for a bot though + if self.delta.x.abs() < 0.003 { + self.delta.x = 0.; + } + if self.delta.y.abs() < 0.003 { + self.delta.y = 0.; + } + if self.delta.z.abs() < 0.003 { + self.delta.z = 0.; + } self.xxa *= 0.98; self.zza *= 0.98; @@ -128,3 +136,41 @@ fn get_speed(entity: &EntityData, friction: f32) -> f32 { 0.02 } } + +#[cfg(test)] +mod tests { + use super::*; + use azalea_world::Dimension; + use uuid::Uuid; + + #[test] + fn test_gravity() { + let mut dim = Dimension::default(); + + dim.add_entity( + 0, + EntityData::new( + Uuid::from_u128(0), + Vec3 { + x: 0., + y: 70., + z: 0., + }, + ), + ); + let mut entity = dim.entity_mut(0).unwrap(); + // y should start at 70 + assert_eq!(entity.pos().y, 70.); + entity.ai_step(); + // delta is applied before gravity, so the first tick only sets the delta + assert_eq!(entity.pos().y, 70.); + assert!(entity.delta.y < 0.); + entity.ai_step(); + // the second tick applies the delta to the position, so now it should go down + assert!( + entity.pos().y < 70., + "Entity y ({}) didn't go down after physics steps", + entity.pos().y + ); + } +} diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index e29ba1df..dbca4214 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -87,8 +87,8 @@ impl Connection<ClientboundHandshakePacket, ServerboundHandshakePacket> { impl Connection<ClientboundLoginPacket, ServerboundLoginPacket> { pub fn set_compression_threshold(&mut self, threshold: i32) { - // if you pass a threshold of 0 or less, compression is disabled - if threshold > 0 { + // if you pass a threshold of less than 0, compression is disabled + if threshold >= 0 { self.compression_threshold = Some(threshold as u32); } else { self.compression_threshold = None; |
