1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

use owned instead of borrowed Vec3 more

This commit is contained in:
mat 2025-06-11 22:58:41 -06:30
parent 89ddd5e85f
commit a2606569bb
21 changed files with 230 additions and 301 deletions

View file

@ -20,7 +20,7 @@ pub struct FluidState {
/// set (see FlowingFluid.getFlowing) /// set (see FlowingFluid.getFlowing)
pub falling: bool, pub falling: bool,
} }
#[derive(Default, Clone, Debug, PartialEq, Eq)] #[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
pub enum FluidKind { pub enum FluidKind {
#[default] #[default]
Empty, Empty,

View file

@ -162,7 +162,7 @@ where
entities.sort_by_cached_key(|(_, position)| { entities.sort_by_cached_key(|(_, position)| {
// to_bits is fine here as long as the number is positive // to_bits is fine here as long as the number is positive
position.distance_squared_to(&nearest_to).to_bits() position.distance_squared_to(nearest_to).to_bits()
}); });
entities entities

View file

@ -363,7 +363,7 @@ pub fn update_hit_result_component(
}; };
let instance = instance_lock.read(); let instance = instance_lock.read();
let hit_result = pick(look_direction, &eye_position, &instance.chunks, pick_range); let hit_result = pick(*look_direction, eye_position, &instance.chunks, pick_range);
if let Some(mut hit_result_ref) = hit_result_ref { if let Some(mut hit_result_ref) = hit_result_ref {
**hit_result_ref = hit_result; **hit_result_ref = hit_result;
} else { } else {
@ -384,8 +384,8 @@ pub fn update_hit_result_component(
/// ///
/// TODO: does not currently check for entities /// TODO: does not currently check for entities
pub fn pick( pub fn pick(
look_direction: &LookDirection, look_direction: LookDirection,
eye_position: &Vec3, eye_position: Vec3,
chunks: &azalea_world::ChunkStorage, chunks: &azalea_world::ChunkStorage,
pick_range: f64, pick_range: f64,
) -> HitResult { ) -> HitResult {
@ -400,18 +400,18 @@ pub fn pick(
/// ///
/// Also see [`pick`]. /// Also see [`pick`].
pub fn pick_block( pub fn pick_block(
look_direction: &LookDirection, look_direction: LookDirection,
eye_position: &Vec3, eye_position: Vec3,
chunks: &azalea_world::ChunkStorage, chunks: &azalea_world::ChunkStorage,
pick_range: f64, pick_range: f64,
) -> BlockHitResult { ) -> BlockHitResult {
let view_vector = view_vector(look_direction); let view_vector = view_vector(look_direction);
let end_position = eye_position + &(view_vector * pick_range); let end_position = eye_position + (view_vector * pick_range);
azalea_physics::clip::clip( azalea_physics::clip::clip(
chunks, chunks,
ClipContext { ClipContext {
from: *eye_position, from: eye_position,
to: end_position, to: end_position,
block_shape_type: BlockShapeType::Outline, block_shape_type: BlockShapeType::Outline,
fluid_pick_type: FluidPickType::None, fluid_pick_type: FluidPickType::None,

View file

@ -428,7 +428,7 @@ impl GamePacketHandler<'_> {
p.relative p.relative
.apply(&p.change, &mut position, &mut direction, &mut physics); .apply(&p.change, &mut position, &mut direction, &mut physics);
// old_pos is set to the current position when we're teleported // old_pos is set to the current position when we're teleported
physics.set_old_pos(&position); physics.set_old_pos(*position);
// send the relevant packets // send the relevant packets
commands.trigger(SendPacketEvent::new( commands.trigger(SendPacketEvent::new(
@ -828,7 +828,7 @@ impl GamePacketHandler<'_> {
&mut physics, &mut physics,
); );
// old_pos is set to the current position when we're teleported // old_pos is set to the current position when we're teleported
physics.set_old_pos(&old_position); physics.set_old_pos(old_position);
}); });
}, },
)); ));

View file

@ -15,14 +15,16 @@ pub struct AABB {
pub struct ClipPointOpts<'a> { pub struct ClipPointOpts<'a> {
pub t: &'a mut f64, pub t: &'a mut f64,
pub approach_dir: Option<Direction>, pub approach_dir: Option<Direction>,
pub delta: &'a Vec3, pub delta: Vec3,
pub begin: f64, pub begin: f64,
pub min_x: f64, pub min_x: f64,
pub min_z: f64, pub min_z: f64,
pub max_x: f64, pub max_x: f64,
pub max_z: f64, pub max_z: f64,
pub result_dir: Direction, pub result_dir: Direction,
pub start: &'a Vec3, pub start: Vec3,
} }
impl AABB { impl AABB {
@ -51,85 +53,51 @@ impl AABB {
AABB { min, max } AABB { min, max }
} }
pub fn expand_towards(&self, other: &Vec3) -> AABB { pub fn expand_towards(&self, other: Vec3) -> AABB {
let mut min_x = self.min.x; let mut min = self.min;
let mut min_y = self.min.y; let mut max = self.max;
let mut min_z = self.min.z;
let mut max_x = self.max.x;
let mut max_y = self.max.y;
let mut max_z = self.max.z;
if other.x < 0.0 { if other.x < 0.0 {
min_x += other.x; min.x += other.x;
} else if other.x > 0.0 { } else if other.x > 0.0 {
max_x += other.x; max.x += other.x;
} }
if other.y < 0.0 { if other.y < 0.0 {
min_y += other.y; min.y += other.y;
} else if other.y > 0.0 { } else if other.y > 0.0 {
max_y += other.y; max.y += other.y;
} }
if other.z < 0.0 { if other.z < 0.0 {
min_z += other.z; min.z += other.z;
} else if other.z > 0.0 { } else if other.z > 0.0 {
max_z += other.z; max.z += other.z;
} }
AABB { AABB { min, max }
min: Vec3::new(min_x, min_y, min_z),
max: Vec3::new(max_x, max_y, max_z),
}
} }
pub fn inflate(&self, amount: Vec3) -> AABB { pub fn inflate(&self, amount: Vec3) -> AABB {
let min_x = self.min.x - amount.x; let min = self.min - amount;
let min_y = self.min.y - amount.y; let max = self.max + amount;
let min_z = self.min.z - amount.z;
let max_x = self.max.x + amount.x; AABB { min, max }
let max_y = self.max.y + amount.y;
let max_z = self.max.z + amount.z;
AABB {
min: Vec3::new(min_x, min_y, min_z),
max: Vec3::new(max_x, max_y, max_z),
}
} }
pub fn inflate_all(&self, amount: f64) -> AABB { pub fn inflate_all(&self, amount: f64) -> AABB {
self.inflate(Vec3::new(amount, amount, amount)) self.inflate(Vec3::new(amount, amount, amount))
} }
pub fn intersect(&self, other: &AABB) -> AABB { pub fn intersect(&self, other: &AABB) -> AABB {
let min_x = self.min.x.max(other.min.x); let min = self.min.max(other.min);
let min_y = self.min.y.max(other.min.y); let max = self.max.min(other.max);
let min_z = self.min.z.max(other.min.z); AABB { min, max }
let max_x = self.max.x.min(other.max.x);
let max_y = self.max.y.min(other.max.y);
let max_z = self.max.z.min(other.max.z);
AABB {
min: Vec3::new(min_x, min_y, min_z),
max: Vec3::new(max_x, max_y, max_z),
}
} }
pub fn minmax(&self, other: &AABB) -> AABB { pub fn minmax(&self, other: &AABB) -> AABB {
let min_x = self.min.x.min(other.min.x); let min = self.min.min(other.min);
let min_y = self.min.y.min(other.min.y); let max = self.max.max(other.max);
let min_z = self.min.z.min(other.min.z); AABB { min, max }
let max_x = self.max.x.max(other.max.x);
let max_y = self.max.y.max(other.max.y);
let max_z = self.max.z.max(other.max.z);
AABB {
min: Vec3::new(min_x, min_y, min_z),
max: Vec3::new(max_x, max_y, max_z),
}
} }
pub fn move_relative(&self, delta: Vec3) -> AABB { pub fn move_relative(&self, delta: Vec3) -> AABB {
@ -147,22 +115,13 @@ impl AABB {
&& self.min.z < other.max.z && self.min.z < other.max.z
&& self.max.z > other.min.z && self.max.z > other.min.z
} }
pub fn intersects_vec3(&self, corner1: &Vec3, corner2: &Vec3) -> bool { pub fn intersects_vec3(&self, corner1: Vec3, corner2: Vec3) -> bool {
self.intersects_aabb(&AABB { let min = corner1.min(corner2);
min: Vec3::new( let max = corner1.max(corner2);
corner1.x.min(corner2.x), self.intersects_aabb(&AABB { min, max })
corner1.y.min(corner2.y),
corner1.z.min(corner2.z),
),
max: Vec3::new(
corner1.x.max(corner2.x),
corner1.y.max(corner2.y),
corner1.z.max(corner2.z),
),
})
} }
pub fn contains(&self, point: &Vec3) -> bool { pub fn contains(&self, point: Vec3) -> bool {
point.x >= self.min.x point.x >= self.min.x
&& point.x < self.max.x && point.x < self.max.x
&& point.y >= self.min.y && point.y >= self.min.y
@ -178,6 +137,7 @@ impl AABB {
(x + y + z) / 3.0 (x + y + z) / 3.0
} }
#[inline]
pub fn get_size(&self, axis: Axis) -> f64 { pub fn get_size(&self, axis: Axis) -> f64 {
axis.choose( axis.choose(
self.max.x - self.min.x, self.max.x - self.min.x,
@ -193,24 +153,24 @@ impl AABB {
self.deflate(Vec3::new(amount, amount, amount)) self.deflate(Vec3::new(amount, amount, amount))
} }
pub fn clip(&self, min: &Vec3, max: &Vec3) -> Option<Vec3> { pub fn clip(&self, min: Vec3, max: Vec3) -> Option<Vec3> {
let mut t = 1.0; let mut t = 1.0;
let delta = max - min; let delta = max - min;
let _dir = Self::get_direction_aabb(self, min, &mut t, None, &delta)?; let _dir = Self::get_direction_aabb(self, min, &mut t, None, delta)?;
Some(min + &(delta * t)) Some(min + (delta * t))
} }
pub fn clip_with_from_and_to(min: &Vec3, max: &Vec3, from: &Vec3, to: &Vec3) -> Option<Vec3> { pub fn clip_with_from_and_to(min: Vec3, max: Vec3, from: Vec3, to: Vec3) -> Option<Vec3> {
let mut t = 1.0; let mut t = 1.0;
let delta = to - from; let delta = to - from;
let _dir = Self::get_direction(min, max, from, &mut t, None, &delta)?; let _dir = Self::get_direction(min, max, from, &mut t, None, delta)?;
Some(from + &(delta * t)) Some(from + (delta * t))
} }
pub fn clip_iterable( pub fn clip_iterable(
boxes: &Vec<AABB>, boxes: &[AABB],
from: &Vec3, from: Vec3,
to: &Vec3, to: Vec3,
pos: BlockPos, pos: BlockPos,
) -> Option<BlockHitResult> { ) -> Option<BlockHitResult> {
let mut t = 1.0; let mut t = 1.0;
@ -223,12 +183,12 @@ impl AABB {
from, from,
&mut t, &mut t,
dir, dir,
&delta, delta,
); );
} }
let dir = dir?; let dir = dir?;
Some(BlockHitResult { Some(BlockHitResult {
location: from + &(delta * t), location: from + (delta * t),
direction: dir, direction: dir,
block_pos: pos, block_pos: pos,
inside: false, inside: false,
@ -239,32 +199,34 @@ impl AABB {
fn get_direction_aabb( fn get_direction_aabb(
&self, &self,
from: &Vec3, from: Vec3,
t: &mut f64, t: &mut f64,
dir: Option<Direction>, dir: Option<Direction>,
delta: &Vec3, delta: Vec3,
) -> Option<Direction> { ) -> Option<Direction> {
AABB::get_direction(&self.min, &self.max, from, t, dir, delta) AABB::get_direction(self.min, self.max, from, t, dir, delta)
} }
fn get_direction( fn get_direction(
min: &Vec3, min: Vec3,
max: &Vec3, max: Vec3,
from: &Vec3, from: Vec3,
t: &mut f64, t: &mut f64,
mut dir: Option<Direction>, mut dir: Option<Direction>,
delta: &Vec3, delta: Vec3,
) -> Option<Direction> { ) -> Option<Direction> {
if delta.x > EPSILON { if delta.x > EPSILON {
dir = Self::clip_point(ClipPointOpts { dir = Self::clip_point(ClipPointOpts {
t, t,
approach_dir: dir, approach_dir: dir,
delta, delta,
begin: min.x, begin: min.x,
min_x: min.y, min_x: min.y,
max_x: max.y, max_x: max.y,
min_z: min.z, min_z: min.z,
max_z: max.z, max_z: max.z,
result_dir: Direction::West, result_dir: Direction::West,
start: from, start: from,
}); });
@ -273,11 +235,13 @@ impl AABB {
t, t,
approach_dir: dir, approach_dir: dir,
delta, delta,
begin: max.x, begin: max.x,
min_x: min.y, min_x: min.y,
max_x: max.y, max_x: max.y,
min_z: min.z, min_z: min.z,
max_z: max.z, max_z: max.z,
result_dir: Direction::East, result_dir: Direction::East,
start: from, start: from,
}); });
@ -287,7 +251,7 @@ impl AABB {
dir = Self::clip_point(ClipPointOpts { dir = Self::clip_point(ClipPointOpts {
t, t,
approach_dir: dir, approach_dir: dir,
delta: &Vec3 { delta: Vec3 {
x: delta.y, x: delta.y,
y: delta.z, y: delta.z,
z: delta.x, z: delta.x,
@ -298,7 +262,7 @@ impl AABB {
min_z: min.x, min_z: min.x,
max_z: max.x, max_z: max.x,
result_dir: Direction::Down, result_dir: Direction::Down,
start: &Vec3 { start: Vec3 {
x: from.y, x: from.y,
y: from.z, y: from.z,
z: from.x, z: from.x,
@ -308,7 +272,7 @@ impl AABB {
dir = Self::clip_point(ClipPointOpts { dir = Self::clip_point(ClipPointOpts {
t, t,
approach_dir: dir, approach_dir: dir,
delta: &Vec3 { delta: Vec3 {
x: delta.y, x: delta.y,
y: delta.z, y: delta.z,
z: delta.x, z: delta.x,
@ -319,7 +283,7 @@ impl AABB {
min_z: min.x, min_z: min.x,
max_z: max.x, max_z: max.x,
result_dir: Direction::Up, result_dir: Direction::Up,
start: &Vec3 { start: Vec3 {
x: from.y, x: from.y,
y: from.z, y: from.z,
z: from.x, z: from.x,
@ -331,7 +295,7 @@ impl AABB {
dir = Self::clip_point(ClipPointOpts { dir = Self::clip_point(ClipPointOpts {
t, t,
approach_dir: dir, approach_dir: dir,
delta: &Vec3 { delta: Vec3 {
x: delta.z, x: delta.z,
y: delta.x, y: delta.x,
z: delta.y, z: delta.y,
@ -342,7 +306,7 @@ impl AABB {
min_z: min.y, min_z: min.y,
max_z: max.y, max_z: max.y,
result_dir: Direction::North, result_dir: Direction::North,
start: &Vec3 { start: Vec3 {
x: from.z, x: from.z,
y: from.x, y: from.x,
z: from.y, z: from.y,
@ -352,7 +316,7 @@ impl AABB {
dir = Self::clip_point(ClipPointOpts { dir = Self::clip_point(ClipPointOpts {
t, t,
approach_dir: dir, approach_dir: dir,
delta: &Vec3 { delta: Vec3 {
x: delta.z, x: delta.z,
y: delta.x, y: delta.x,
z: delta.y, z: delta.y,
@ -363,7 +327,7 @@ impl AABB {
min_z: min.y, min_z: min.y,
max_z: max.y, max_z: max.y,
result_dir: Direction::South, result_dir: Direction::South,
start: &Vec3 { start: Vec3 {
x: from.z, x: from.z,
y: from.x, y: from.x,
z: from.y, z: from.y,
@ -431,7 +395,7 @@ impl AABB {
axis.choose(self.min.x, self.min.y, self.min.z) axis.choose(self.min.x, self.min.y, self.min.z)
} }
pub fn collided_along_vector(&self, vector: Vec3, boxes: &Vec<AABB>) -> bool { pub fn collided_along_vector(&self, vector: Vec3, boxes: &[AABB]) -> bool {
let center = self.get_center(); let center = self.get_center();
let new_center = center + vector; let new_center = center + vector;
@ -441,11 +405,11 @@ impl AABB {
self.get_size(Axis::Y) * 0.5, self.get_size(Axis::Y) * 0.5,
self.get_size(Axis::Z) * 0.5, self.get_size(Axis::Z) * 0.5,
)); ));
if inflated.contains(&new_center) || inflated.contains(&center) { if inflated.contains(new_center) || inflated.contains(center) {
return true; return true;
} }
if inflated.clip(&center, &new_center).is_some() { if inflated.clip(center, new_center).is_some() {
return true; return true;
} }
} }
@ -494,12 +458,12 @@ mod tests {
fn test_aabb_clip_iterable() { fn test_aabb_clip_iterable() {
assert_ne!( assert_ne!(
AABB::clip_iterable( AABB::clip_iterable(
&vec![AABB { &[AABB {
min: Vec3::new(0., 0., 0.), min: Vec3::new(0., 0., 0.),
max: Vec3::new(1., 1., 1.), max: Vec3::new(1., 1., 1.),
}], }],
&Vec3::new(-1., -1., -1.), Vec3::new(-1., -1., -1.),
&Vec3::new(1., 1., 1.), Vec3::new(1., 1., 1.),
BlockPos::new(0, 0, 0), BlockPos::new(0, 0, 0),
), ),
None None

View file

@ -34,7 +34,7 @@ macro_rules! vec3_impl {
/// Get the squared distance from this position to another position. /// Get the squared distance from this position to another position.
/// Equivalent to `(self - other).length_squared()`. /// Equivalent to `(self - other).length_squared()`.
#[inline] #[inline]
pub fn distance_squared_to(&self, other: &Self) -> $type { pub fn distance_squared_to(self, other: Self) -> $type {
(self - other).length_squared() (self - other).length_squared()
} }
@ -44,7 +44,7 @@ macro_rules! vec3_impl {
} }
#[inline] #[inline]
pub fn horizontal_distance_squared_to(&self, other: &Self) -> $type { pub fn horizontal_distance_squared_to(self, other: Self) -> $type {
(self - other).horizontal_distance_squared() (self - other).horizontal_distance_squared()
} }
@ -115,6 +115,23 @@ macro_rules! vec3_impl {
self.x * other.x + self.y * other.y + self.z * other.z self.x * other.x + self.y * other.y + self.z * other.z
} }
/// Make a new position with the lower coordinates for each axis.
pub fn min(&self, other: Self) -> Self {
Self {
x: self.x.min(other.x),
y: self.x.min(other.y),
z: self.x.min(other.z),
}
}
/// Make a new position with the higher coordinates for each axis.
pub fn max(&self, other: Self) -> Self {
Self {
x: self.x.max(other.x),
y: self.x.max(other.y),
z: self.x.max(other.z),
}
}
/// Replace the Y with 0. /// Replace the Y with 0.
#[inline] #[inline]
pub fn xz(&self) -> Self { pub fn xz(&self) -> Self {
@ -298,7 +315,7 @@ impl Vec3 {
/// Get the distance from this position to another position. /// Get the distance from this position to another position.
/// Equivalent to `(self - other).length()`. /// Equivalent to `(self - other).length()`.
pub fn distance_to(&self, other: &Self) -> f64 { pub fn distance_to(self, other: Self) -> f64 {
(self - other).length() (self - other).length()
} }
@ -382,40 +399,6 @@ impl BlockPos {
(self.x.abs() + self.y.abs() + self.z.abs()) as u32 (self.x.abs() + self.y.abs() + self.z.abs()) as u32
} }
/// Make a new BlockPos with the lower coordinates for each axis.
///
/// ```
/// # use azalea_core::position::BlockPos;
/// assert_eq!(
/// BlockPos::min(&BlockPos::new(1, 20, 300), &BlockPos::new(50, 40, 30),),
/// BlockPos::new(1, 20, 30),
/// );
/// ```
pub fn min(&self, other: &Self) -> Self {
Self {
x: self.x.min(other.x),
y: self.y.min(other.y),
z: self.z.min(other.z),
}
}
/// Make a new BlockPos with the higher coordinates for each axis.
///
/// ```
/// # use azalea_core::position::BlockPos;
/// assert_eq!(
/// BlockPos::max(&BlockPos::new(1, 20, 300), &BlockPos::new(50, 40, 30),),
/// BlockPos::new(50, 40, 300),
/// );
/// ```
pub fn max(&self, other: &Self) -> Self {
Self {
x: self.x.max(other.x),
y: self.y.max(other.y),
z: self.z.max(other.z),
}
}
pub fn offset_with_direction(self, direction: Direction) -> Self { pub fn offset_with_direction(self, direction: Direction) -> Self {
self + direction.normal() self + direction.normal()
} }

View file

@ -12,7 +12,7 @@ impl EntityDimensions {
Self { width, height } Self { width, height }
} }
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 radius = (self.width / 2.0) as f64;
let height = self.height as f64; let height = self.height as f64;
AABB { AABB {

View file

@ -39,15 +39,15 @@ pub use crate::plugin::*;
pub fn move_relative( pub fn move_relative(
physics: &mut Physics, physics: &mut Physics,
direction: &LookDirection, direction: LookDirection,
speed: f32, speed: f32,
acceleration: &Vec3, acceleration: Vec3,
) { ) {
let input_vector = input_vector(direction, speed, acceleration); let input_vector = input_vector(direction, speed, acceleration);
physics.velocity += input_vector; physics.velocity += input_vector;
} }
pub fn input_vector(direction: &LookDirection, speed: f32, acceleration: &Vec3) -> Vec3 { pub fn input_vector(direction: LookDirection, speed: f32, acceleration: Vec3) -> Vec3 {
let distance = acceleration.length_squared(); let distance = acceleration.length_squared();
if distance < 1.0E-7 { if distance < 1.0E-7 {
return Vec3::ZERO; return Vec3::ZERO;
@ -55,7 +55,7 @@ pub fn input_vector(direction: &LookDirection, speed: f32, acceleration: &Vec3)
let acceleration = if distance > 1.0 { let acceleration = if distance > 1.0 {
acceleration.normalize() acceleration.normalize()
} else { } else {
*acceleration acceleration
} }
.scale(speed as f64); .scale(speed as f64);
let y_rot = math::sin(direction.y_rot * 0.017453292f32); let y_rot = math::sin(direction.y_rot * 0.017453292f32);
@ -67,7 +67,7 @@ pub fn input_vector(direction: &LookDirection, speed: f32, acceleration: &Vec3)
} }
} }
pub fn view_vector(look_direction: &LookDirection) -> Vec3 { pub fn view_vector(look_direction: LookDirection) -> Vec3 {
let x_rot = look_direction.x_rot * 0.017453292; let x_rot = look_direction.x_rot * 0.017453292;
let y_rot = -look_direction.y_rot * 0.017453292; let y_rot = -look_direction.y_rot * 0.017453292;
let y_rot_cos = math::cos(y_rot); let y_rot_cos = math::cos(y_rot);
@ -82,7 +82,7 @@ pub fn view_vector(look_direction: &LookDirection) -> Vec3 {
} }
/// Get the position of the block below the entity, but a little lower. /// Get the position of the block below the entity, but a little lower.
pub fn on_pos_legacy(chunk_storage: &ChunkStorage, position: &Position) -> BlockPos { pub fn on_pos_legacy(chunk_storage: &ChunkStorage, position: Position) -> BlockPos {
on_pos(0.2, chunk_storage, position) on_pos(0.2, chunk_storage, position)
} }
@ -98,7 +98,7 @@ pub fn on_pos_legacy(chunk_storage: &ChunkStorage, position: &Position) -> Block
// } // }
// } // }
// return var5; // return var5;
pub fn on_pos(offset: f32, chunk_storage: &ChunkStorage, pos: &Position) -> BlockPos { pub fn on_pos(offset: f32, chunk_storage: &ChunkStorage, pos: Position) -> BlockPos {
let x = pos.x.floor() as i32; let x = pos.x.floor() as i32;
let y = (pos.y - offset as f64).floor() as i32; let y = (pos.y - offset as f64).floor() as i32;
let z = pos.z.floor() as i32; let z = pos.z.floor() as i32;
@ -323,7 +323,7 @@ impl Physics {
no_jump_delay: 0, no_jump_delay: 0,
bounding_box: dimensions.make_bounding_box(&pos), bounding_box: dimensions.make_bounding_box(pos),
dimensions, dimensions,
has_impulse: false, has_impulse: false,
@ -375,8 +375,8 @@ impl Physics {
self.lava_fluid_height > 0. self.lava_fluid_height > 0.
} }
pub fn set_old_pos(&mut self, pos: &Position) { pub fn set_old_pos(&mut self, pos: Position) {
self.old_position = **pos; self.old_position = *pos;
} }
} }
@ -496,10 +496,10 @@ impl EntityBundle {
/// If this is for a client then all of our clients will have this. /// If this is for a client then all of our clients will have this.
/// ///
/// This component is not removed from clients when they disconnect. /// This component is not removed from clients when they disconnect.
#[derive(Component, Clone, Debug, Default)] #[derive(Component, Clone, Copy, Debug, Default)]
pub struct LocalEntity; pub struct LocalEntity;
#[derive(Component, Clone, Debug, PartialEq, Deref, DerefMut)] #[derive(Component, Clone, Copy, Debug, PartialEq, Deref, DerefMut)]
pub struct FluidOnEyes(FluidKind); pub struct FluidOnEyes(FluidKind);
impl FluidOnEyes { impl FluidOnEyes {
@ -508,5 +508,5 @@ impl FluidOnEyes {
} }
} }
#[derive(Component, Clone, Debug, PartialEq, Deref, DerefMut)] #[derive(Component, Clone, Copy, Debug, PartialEq, Deref, DerefMut)]
pub struct OnClimbable(bool); pub struct OnClimbable(bool);

View file

@ -199,7 +199,7 @@ pub fn clamp_look_direction(mut query: Query<&mut LookDirection>) {
/// Cached position in the world must be updated. /// Cached position in the world must be updated.
pub fn update_bounding_box(mut query: Query<(&Position, &mut Physics), Changed<Position>>) { pub fn update_bounding_box(mut query: Query<(&Position, &mut Physics), Changed<Position>>) {
for (position, mut physics) in query.iter_mut() { for (position, mut physics) in query.iter_mut() {
let bounding_box = physics.dimensions.make_bounding_box(position); let bounding_box = physics.dimensions.make_bounding_box(**position);
physics.bounding_box = bounding_box; physics.bounding_box = bounding_box;
} }
} }

View file

@ -101,22 +101,22 @@ pub fn clip(chunk_storage: &ChunkStorage, context: ClipContext) -> BlockHitResul
let block_shape = ctx.block_shape(block_state); let block_shape = ctx.block_shape(block_state);
let interaction_clip = clip_with_interaction_override( let interaction_clip = clip_with_interaction_override(
&ctx.from, ctx.from,
&ctx.to, ctx.to,
block_pos, block_pos,
block_shape, block_shape,
&block_state, block_state,
); );
let fluid_shape = ctx.fluid_shape(fluid_state, chunk_storage, block_pos); let fluid_shape = ctx.fluid_shape(fluid_state, chunk_storage, block_pos);
let fluid_clip = fluid_shape.clip(&ctx.from, &ctx.to, block_pos); let fluid_clip = fluid_shape.clip(ctx.from, ctx.to, block_pos);
let distance_to_interaction = interaction_clip let distance_to_interaction = interaction_clip
.as_ref() .as_ref()
.map(|hit| ctx.from.distance_squared_to(&hit.location)) .map(|hit| ctx.from.distance_squared_to(hit.location))
.unwrap_or(f64::MAX); .unwrap_or(f64::MAX);
let distance_to_fluid = fluid_clip let distance_to_fluid = fluid_clip
.as_ref() .as_ref()
.map(|hit| ctx.from.distance_squared_to(&hit.location)) .map(|hit| ctx.from.distance_squared_to(hit.location))
.unwrap_or(f64::MAX); .unwrap_or(f64::MAX);
if distance_to_interaction <= distance_to_fluid { if distance_to_interaction <= distance_to_fluid {
@ -137,11 +137,11 @@ pub fn clip(chunk_storage: &ChunkStorage, context: ClipContext) -> BlockHitResul
} }
fn clip_with_interaction_override( fn clip_with_interaction_override(
from: &Vec3, from: Vec3,
to: &Vec3, to: Vec3,
block_pos: BlockPos, block_pos: BlockPos,
block_shape: &VoxelShape, block_shape: &VoxelShape,
_block_state: &BlockState, _block_state: BlockState,
) -> Option<BlockHitResult> { ) -> Option<BlockHitResult> {
let block_hit_result = block_shape.clip(from, to, block_pos); let block_hit_result = block_shape.clip(from, to, block_pos);
@ -255,7 +255,7 @@ pub fn traverse_blocks<C, T>(
} }
} }
pub fn box_traverse_blocks(from: &Vec3, to: &Vec3, aabb: &AABB) -> HashSet<BlockPos> { pub fn box_traverse_blocks(from: Vec3, to: Vec3, aabb: &AABB) -> HashSet<BlockPos> {
let delta = to - from; let delta = to - from;
let traversed_blocks = BlockPos::between_closed_aabb(aabb); let traversed_blocks = BlockPos::between_closed_aabb(aabb);
if delta.length_squared() < (0.99999_f32 * 0.99999) as f64 { if delta.length_squared() < (0.99999_f32 * 0.99999) as f64 {
@ -346,10 +346,10 @@ pub fn add_collisions_along_travel(
step_count += 1; step_count += 1;
let Some(clip_location) = AABB::clip_with_from_and_to( let Some(clip_location) = AABB::clip_with_from_and_to(
&Vec3::new(min_x as f64, min_y as f64, min_z as f64), Vec3::new(min_x as f64, min_y as f64, min_z as f64),
&Vec3::new((min_x + 1) as f64, (min_y + 1) as f64, (min_z + 1) as f64), Vec3::new((min_x + 1) as f64, (min_y + 1) as f64, (min_z + 1) as f64),
&from, from,
&to, to,
) else { ) else {
continue; continue;
}; };

View file

@ -35,7 +35,7 @@ pub enum MoverType {
// Entity.collide // Entity.collide
fn collide( fn collide(
movement: &Vec3, movement: Vec3,
world: &Instance, world: &Instance,
physics: &azalea_entity::Physics, physics: &azalea_entity::Physics,
source_entity: Option<Entity>, source_entity: Option<Entity>,
@ -51,7 +51,7 @@ fn collide(
collidable_entity_query, collidable_entity_query,
); );
let collided_delta = if movement.length_squared() == 0.0 { let collided_delta = if movement.length_squared() == 0.0 {
*movement movement
} else { } else {
collide_bounding_box(movement, &entity_bounding_box, world, &entity_collisions) collide_bounding_box(movement, &entity_bounding_box, world, &entity_collisions)
}; };
@ -65,20 +65,20 @@ fn collide(
let max_up_step = 0.6; let max_up_step = 0.6;
if max_up_step > 0. && on_ground && (x_collision || z_collision) { if max_up_step > 0. && on_ground && (x_collision || z_collision) {
let mut step_to_delta = collide_bounding_box( let mut step_to_delta = collide_bounding_box(
&movement.with_y(max_up_step), movement.with_y(max_up_step),
&entity_bounding_box, &entity_bounding_box,
world, world,
&entity_collisions, &entity_collisions,
); );
let directly_up_delta = collide_bounding_box( let directly_up_delta = collide_bounding_box(
&Vec3::ZERO.with_y(max_up_step), Vec3::ZERO.with_y(max_up_step),
&entity_bounding_box.expand_towards(&Vec3::new(movement.x, 0., movement.z)), &entity_bounding_box.expand_towards(Vec3::new(movement.x, 0., movement.z)),
world, world,
&entity_collisions, &entity_collisions,
); );
if directly_up_delta.y < max_up_step { if directly_up_delta.y < max_up_step {
let target_movement = collide_bounding_box( let target_movement = collide_bounding_box(
&movement.with_y(0.), movement.with_y(0.),
&entity_bounding_box.move_relative(directly_up_delta), &entity_bounding_box.move_relative(directly_up_delta),
world, world,
&entity_collisions, &entity_collisions,
@ -95,7 +95,7 @@ fn collide(
> collided_delta.horizontal_distance_squared() > collided_delta.horizontal_distance_squared()
{ {
return step_to_delta.add(collide_bounding_box( return step_to_delta.add(collide_bounding_box(
&Vec3::ZERO.with_y(-step_to_delta.y + movement.y), Vec3::ZERO.with_y(-step_to_delta.y + movement.y),
&entity_bounding_box.move_relative(step_to_delta), &entity_bounding_box.move_relative(step_to_delta),
world, world,
&entity_collisions, &entity_collisions,
@ -112,7 +112,7 @@ fn collide(
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn move_colliding( pub fn move_colliding(
_mover_type: MoverType, _mover_type: MoverType,
movement: &Vec3, movement: Vec3,
world: &Instance, world: &Instance,
position: &mut Mut<azalea_entity::Position>, position: &mut Mut<azalea_entity::Position>,
physics: &mut azalea_entity::Physics, physics: &mut azalea_entity::Physics,
@ -180,7 +180,7 @@ pub fn move_colliding(
// TODO: minecraft checks for a "minor" horizontal collision here // TODO: minecraft checks for a "minor" horizontal collision here
let _block_pos_below = azalea_entity::on_pos_legacy(&world.chunks, position); let _block_pos_below = azalea_entity::on_pos_legacy(&world.chunks, **position);
// let _block_state_below = self // let _block_state_below = self
// .world // .world
// .get_block_state(&block_pos_below) // .get_block_state(&block_pos_below)
@ -239,7 +239,7 @@ pub fn move_colliding(
} }
fn collide_bounding_box( fn collide_bounding_box(
movement: &Vec3, movement: Vec3,
entity_bounding_box: &AABB, entity_bounding_box: &AABB,
world: &Instance, world: &Instance,
entity_collisions: &[VoxelShape], entity_collisions: &[VoxelShape],
@ -259,63 +259,44 @@ fn collide_bounding_box(
} }
fn collide_with_shapes( fn collide_with_shapes(
movement: &Vec3, mut movement: Vec3,
mut entity_box: AABB, mut entity_box: AABB,
collision_boxes: &Vec<VoxelShape>, collision_boxes: &[VoxelShape],
) -> Vec3 { ) -> Vec3 {
if collision_boxes.is_empty() { if collision_boxes.is_empty() {
return *movement; return movement;
} }
let mut x_movement = movement.x; if movement.y != 0. {
let mut y_movement = movement.y; movement.y = Shapes::collide(Axis::Y, &entity_box, collision_boxes, movement.y);
let mut z_movement = movement.z; if movement.y != 0. {
if y_movement != 0. { entity_box = entity_box.move_relative(Vec3::new(0., movement.y, 0.));
y_movement = Shapes::collide(&Axis::Y, &entity_box, collision_boxes, y_movement);
if y_movement != 0. {
entity_box = entity_box.move_relative(Vec3 {
x: 0.,
y: y_movement,
z: 0.,
});
} }
} }
// whether the player is moving more in the z axis than x // whether the player is moving more in the z axis than x
// this is done to fix a movement bug, minecraft does this too // this is done to fix a movement bug, minecraft does this too
let more_z_movement = x_movement.abs() < z_movement.abs(); let more_z_movement = movement.x.abs() < movement.z.abs();
if more_z_movement && z_movement != 0. { if more_z_movement && movement.z != 0. {
z_movement = Shapes::collide(&Axis::Z, &entity_box, collision_boxes, z_movement); movement.z = Shapes::collide(Axis::Z, &entity_box, collision_boxes, movement.z);
if z_movement != 0. { if movement.z != 0. {
entity_box = entity_box.move_relative(Vec3 { entity_box = entity_box.move_relative(Vec3::new(0., 0., movement.z));
x: 0.,
y: 0.,
z: z_movement,
});
} }
} }
if x_movement != 0. { if movement.x != 0. {
x_movement = Shapes::collide(&Axis::X, &entity_box, collision_boxes, x_movement); movement.x = Shapes::collide(Axis::X, &entity_box, collision_boxes, movement.x);
if x_movement != 0. { if movement.x != 0. {
entity_box = entity_box.move_relative(Vec3 { entity_box = entity_box.move_relative(Vec3::new(movement.x, 0., 0.));
x: x_movement,
y: 0.,
z: 0.,
});
} }
} }
if !more_z_movement && z_movement != 0. { if !more_z_movement && movement.z != 0. {
z_movement = Shapes::collide(&Axis::Z, &entity_box, collision_boxes, z_movement); movement.z = Shapes::collide(Axis::Z, &entity_box, collision_boxes, movement.z);
} }
Vec3 { movement
x: x_movement,
y: y_movement,
z: z_movement,
}
} }
/// Get the [`VoxelShape`] for the given fluid state. /// Get the [`VoxelShape`] for the given fluid state.

View file

@ -98,9 +98,9 @@ impl Shapes {
} }
pub fn collide( pub fn collide(
axis: &Axis, axis: Axis,
entity_box: &AABB, entity_box: &AABB,
collision_boxes: &Vec<VoxelShape>, collision_boxes: &[VoxelShape],
mut movement: f64, mut movement: f64,
) -> f64 { ) -> f64 {
for shape in collision_boxes { for shape in collision_boxes {
@ -408,7 +408,7 @@ impl VoxelShape {
} }
} }
pub fn clip(&self, from: &Vec3, to: &Vec3, block_pos: BlockPos) -> Option<BlockHitResult> { pub fn clip(&self, from: Vec3, to: Vec3, block_pos: BlockPos) -> Option<BlockHitResult> {
if self.is_empty() { if self.is_empty() {
return None; return None;
} }
@ -416,7 +416,7 @@ impl VoxelShape {
if vector.length_squared() < EPSILON { if vector.length_squared() < EPSILON {
return None; return None;
} }
let right_after_start = from + &(vector * 0.001); let right_after_start = from + (vector * 0.001);
if self.shape().is_full_wide( if self.shape().is_full_wide(
self.find_index(Axis::X, right_after_start.x - block_pos.x as f64), self.find_index(Axis::X, right_after_start.x - block_pos.x as f64),
@ -436,8 +436,8 @@ impl VoxelShape {
} }
} }
pub fn collide(&self, axis: &Axis, entity_box: &AABB, movement: f64) -> f64 { pub fn collide(&self, axis: Axis, entity_box: &AABB, movement: f64) -> f64 {
self.collide_x(AxisCycle::between(*axis, Axis::X), entity_box, movement) self.collide_x(AxisCycle::between(axis, Axis::X), entity_box, movement)
} }
pub fn collide_x(&self, axis_cycle: AxisCycle, entity_box: &AABB, mut movement: f64) -> f64 { pub fn collide_x(&self, axis_cycle: AxisCycle, entity_box: &AABB, mut movement: f64) -> f64 {
if self.shape().is_empty() { if self.shape().is_empty() {
@ -753,8 +753,8 @@ mod tests {
let block_shape = &*BLOCK_SHAPE; let block_shape = &*BLOCK_SHAPE;
let block_hit_result = block_shape let block_hit_result = block_shape
.clip( .clip(
&Vec3::new(-0.3, 0.5, 0.), Vec3::new(-0.3, 0.5, 0.),
&Vec3::new(5.3, 0.5, 0.), Vec3::new(5.3, 0.5, 0.),
BlockPos::new(0, 0, 0), BlockPos::new(0, 0, 0),
) )
.unwrap(); .unwrap();

View file

@ -32,7 +32,7 @@ pub fn update_in_water_state_and_do_fluid_pushing(
physics.water_fluid_height = 0.; physics.water_fluid_height = 0.;
physics.lava_fluid_height = 0.; physics.lava_fluid_height = 0.;
update_in_water_state_and_do_water_current_pushing(&mut physics, &world, position); update_in_water_state_and_do_water_current_pushing(&mut physics, &world, *position);
// right now doing registries.dimension_type() clones the entire registry which // right now doing registries.dimension_type() clones the entire registry which
// is very inefficient, so for now we're doing this instead // is very inefficient, so for now we're doing this instead
@ -63,7 +63,7 @@ pub fn update_in_water_state_and_do_fluid_pushing(
fn update_in_water_state_and_do_water_current_pushing( fn update_in_water_state_and_do_water_current_pushing(
physics: &mut Physics, physics: &mut Physics,
world: &Instance, world: &Instance,
_position: &Position, _position: Position,
) { ) {
// TODO: implement vehicles and boats // TODO: implement vehicles and boats
// if vehicle == AbstractBoat { // if vehicle == AbstractBoat {

View file

@ -18,6 +18,7 @@ use azalea_entity::{
Attributes, InLoadedChunk, Jumping, LocalEntity, LookDirection, OnClimbable, Physics, Pose, Attributes, InLoadedChunk, Jumping, LocalEntity, LookDirection, OnClimbable, Physics, Pose,
Position, metadata::Sprinting, move_relative, Position, metadata::Sprinting, move_relative,
}; };
use azalea_registry::Block;
use azalea_world::{Instance, InstanceContainer, InstanceName}; use azalea_world::{Instance, InstanceContainer, InstanceName};
use bevy_app::{App, Plugin}; use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*; use bevy_ecs::prelude::*;
@ -110,9 +111,9 @@ pub fn ai_step(
{ {
jump_from_ground( jump_from_ground(
&mut physics, &mut physics,
position, *position,
look_direction, *look_direction,
sprinting, *sprinting,
instance_name, instance_name,
&instance_container, &instance_container,
); );
@ -187,11 +188,11 @@ fn check_inside_blocks(
for movement in movements { for movement in movements {
let bounding_box_at_target = physics let bounding_box_at_target = physics
.dimensions .dimensions
.make_bounding_box(&movement.to) .make_bounding_box(movement.to)
.deflate_all(1.0E-5); .deflate_all(1.0E-5);
for traversed_block in for traversed_block in
box_traverse_blocks(&movement.from, &movement.to, &bounding_box_at_target) box_traverse_blocks(movement.from, movement.to, &bounding_box_at_target)
{ {
// if (!this.isAlive()) { // if (!this.isAlive()) {
// return; // return;
@ -221,8 +222,8 @@ fn check_inside_blocks(
if entity_inside_collision_shape != &*BLOCK_SHAPE if entity_inside_collision_shape != &*BLOCK_SHAPE
&& !collided_with_shape_moving_from( && !collided_with_shape_moving_from(
&movement.from, movement.from,
&movement.to, movement.to,
traversed_block, traversed_block,
entity_inside_collision_shape, entity_inside_collision_shape,
physics, physics,
@ -241,8 +242,8 @@ fn check_inside_blocks(
} }
fn collided_with_shape_moving_from( fn collided_with_shape_moving_from(
from: &Vec3, from: Vec3,
to: &Vec3, to: Vec3,
traversed_block: BlockPos, traversed_block: BlockPos,
entity_inside_collision_shape: &VoxelShape, entity_inside_collision_shape: &VoxelShape,
physics: &Physics, physics: &Physics,
@ -304,9 +305,9 @@ pub struct EntityMovement {
pub fn jump_from_ground( pub fn jump_from_ground(
physics: &mut Physics, physics: &mut Physics,
position: &Position, position: Position,
look_direction: &LookDirection, look_direction: LookDirection,
sprinting: &Sprinting, sprinting: Sprinting,
instance_name: &InstanceName, instance_name: &InstanceName,
instance_container: &InstanceContainer, instance_container: &InstanceContainer,
) { ) {
@ -322,7 +323,7 @@ pub fn jump_from_ground(
y: jump_power, y: jump_power,
z: old_delta_movement.z, z: old_delta_movement.z,
}; };
if **sprinting { if *sprinting {
// sprint jumping gives some extra velocity // sprint jumping gives some extra velocity
let y_rot = look_direction.y_rot * 0.017453292; let y_rot = look_direction.y_rot * 0.017453292;
physics.velocity += Vec3 { physics.velocity += Vec3 {
@ -337,11 +338,11 @@ pub fn jump_from_ground(
pub fn update_old_position(mut query: Query<(&mut Physics, &Position)>) { pub fn update_old_position(mut query: Query<(&mut Physics, &Position)>) {
for (mut physics, position) in &mut query { for (mut physics, position) in &mut query {
physics.set_old_pos(position); physics.set_old_pos(*position);
} }
} }
fn get_block_pos_below_that_affects_movement(position: &Position) -> BlockPos { fn get_block_pos_below_that_affects_movement(position: Position) -> BlockPos {
BlockPos::new( BlockPos::new(
position.x.floor() as i32, position.x.floor() as i32,
// TODO: this uses bounding_box.min_y instead of position.y // TODO: this uses bounding_box.min_y instead of position.y
@ -355,13 +356,13 @@ struct HandleRelativeFrictionAndCalculateMovementOpts<'a, 'b, 'world, 'state> {
block_friction: f32, block_friction: f32,
world: &'a Instance, world: &'a Instance,
physics: &'a mut Physics, physics: &'a mut Physics,
direction: &'a LookDirection, direction: LookDirection,
position: Mut<'a, Position>, position: Mut<'a, Position>,
attributes: &'a Attributes, attributes: &'a Attributes,
is_sprinting: bool, is_sprinting: bool,
on_climbable: &'a OnClimbable, on_climbable: OnClimbable,
pose: Option<&'a Pose>, pose: Option<Pose>,
jumping: &'a Jumping, jumping: Jumping,
entity: Entity, entity: Entity,
physics_query: &'a PhysicsQuery<'world, 'state, 'b>, physics_query: &'a PhysicsQuery<'world, 'state, 'b>,
collidable_entity_query: &'a CollidableEntityQuery<'world, 'state>, collidable_entity_query: &'a CollidableEntityQuery<'world, 'state>,
@ -387,18 +388,18 @@ fn handle_relative_friction_and_calculate_movement(
physics, physics,
direction, direction,
get_friction_influenced_speed(physics, attributes, block_friction, is_sprinting), get_friction_influenced_speed(physics, attributes, block_friction, is_sprinting),
&Vec3 { Vec3::new(
x: physics.x_acceleration as f64, physics.x_acceleration as f64,
y: physics.y_acceleration as f64, physics.y_acceleration as f64,
z: physics.z_acceleration as f64, physics.z_acceleration as f64,
}, ),
); );
physics.velocity = handle_on_climbable(physics.velocity, on_climbable, &position, world, pose); physics.velocity = handle_on_climbable(physics.velocity, on_climbable, *position, world, pose);
move_colliding( move_colliding(
MoverType::Own, MoverType::Own,
&physics.velocity.clone(), physics.velocity,
world, world,
&mut position, &mut position,
physics, physics,
@ -414,14 +415,14 @@ fn handle_relative_friction_and_calculate_movement(
// PowderSnowBlock.canEntityWalkOnPowderSnow(entity))) { var3 = new // PowderSnowBlock.canEntityWalkOnPowderSnow(entity))) { var3 = new
// Vec3(var3.x, 0.2D, var3.z); } // Vec3(var3.x, 0.2D, var3.z); }
if physics.horizontal_collision || **jumping { if physics.horizontal_collision || *jumping {
let block_at_feet: azalea_registry::Block = world let block_at_feet: Block = world
.chunks .chunks
.get_block_state((*position).into()) .get_block_state((*position).into())
.unwrap_or_default() .unwrap_or_default()
.into(); .into();
if **on_climbable || block_at_feet == azalea_registry::Block::PowderSnow { if *on_climbable || block_at_feet == Block::PowderSnow {
physics.velocity.y = 0.2; physics.velocity.y = 0.2;
} }
} }
@ -431,12 +432,12 @@ fn handle_relative_friction_and_calculate_movement(
fn handle_on_climbable( fn handle_on_climbable(
velocity: Vec3, velocity: Vec3,
on_climbable: &OnClimbable, on_climbable: OnClimbable,
position: &Position, position: Position,
world: &Instance, world: &Instance,
pose: Option<&Pose>, pose: Option<Pose>,
) -> Vec3 { ) -> Vec3 {
if !**on_climbable { if !*on_climbable {
return velocity; return velocity;
} }
@ -450,7 +451,7 @@ fn handle_on_climbable(
// sneaking on ladders/vines // sneaking on ladders/vines
if y < 0.0 if y < 0.0
&& pose.copied() == Some(Pose::Sneaking) && pose == Some(Pose::Sneaking)
&& azalea_registry::Block::from( && azalea_registry::Block::from(
world world
.chunks .chunks
@ -485,7 +486,7 @@ fn get_friction_influenced_speed(
/// Returns the what the entity's jump should be multiplied by based on the /// Returns the what the entity's jump should be multiplied by based on the
/// block they're standing on. /// block they're standing on.
fn block_jump_factor(world: &Instance, position: &Position) -> f32 { fn block_jump_factor(world: &Instance, position: Position) -> f32 {
let block_at_pos = world.chunks.get_block_state(position.into()); let block_at_pos = world.chunks.get_block_state(position.into());
let block_below = world let block_below = world
.chunks .chunks
@ -513,7 +514,7 @@ fn block_jump_factor(world: &Instance, position: &Position) -> f32 {
// public double getJumpBoostPower() { // public double getJumpBoostPower() {
// return this.hasEffect(MobEffects.JUMP) ? (double)(0.1F * // return this.hasEffect(MobEffects.JUMP) ? (double)(0.1F *
// (float)(this.getEffect(MobEffects.JUMP).getAmplifier() + 1)) : 0.0D; } // (float)(this.getEffect(MobEffects.JUMP).getAmplifier() + 1)) : 0.0D; }
fn jump_power(world: &Instance, position: &Position) -> f32 { fn jump_power(world: &Instance, position: Position) -> f32 {
0.42 * block_jump_factor(world, position) 0.42 * block_jump_factor(world, position)
} }

View file

@ -73,7 +73,7 @@ pub fn travel(
&world, &world,
entity, entity,
&mut physics, &mut physics,
&direction, *direction,
position, position,
attributes, attributes,
sprinting, sprinting,
@ -86,13 +86,13 @@ pub fn travel(
&world, &world,
entity, entity,
&mut physics, &mut physics,
&direction, *direction,
position, position,
attributes, attributes,
sprinting, sprinting,
on_climbable, *on_climbable,
pose, pose,
jumping, *jumping,
&physics_query, &physics_query,
&collidable_entity_query, &collidable_entity_query,
); );
@ -106,19 +106,19 @@ fn travel_in_air(
world: &Instance, world: &Instance,
entity: Entity, entity: Entity,
physics: &mut Physics, physics: &mut Physics,
direction: &LookDirection, direction: LookDirection,
position: Mut<Position>, position: Mut<Position>,
attributes: &Attributes, attributes: &Attributes,
sprinting: Sprinting, sprinting: Sprinting,
on_climbable: &OnClimbable, on_climbable: OnClimbable,
pose: Option<&Pose>, pose: Option<&Pose>,
jumping: &Jumping, jumping: Jumping,
physics_query: &PhysicsQuery, physics_query: &PhysicsQuery,
collidable_entity_query: &CollidableEntityQuery, collidable_entity_query: &CollidableEntityQuery,
) { ) {
let gravity = get_effective_gravity(); let gravity = get_effective_gravity();
let block_pos_below = get_block_pos_below_that_affects_movement(&position); let block_pos_below = get_block_pos_below_that_affects_movement(*position);
let block_state_below = world let block_state_below = world
.chunks .chunks
@ -144,7 +144,7 @@ fn travel_in_air(
attributes, attributes,
is_sprinting: *sprinting, is_sprinting: *sprinting,
on_climbable, on_climbable,
pose, pose: pose.copied(),
jumping, jumping,
entity, entity,
physics_query, physics_query,
@ -177,7 +177,7 @@ fn travel_in_fluid(
world: &Instance, world: &Instance,
entity: Entity, entity: Entity,
physics: &mut Physics, physics: &mut Physics,
direction: &LookDirection, direction: LookDirection,
mut position: Mut<Position>, mut position: Mut<Position>,
attributes: &Attributes, attributes: &Attributes,
sprinting: Sprinting, sprinting: Sprinting,
@ -212,10 +212,10 @@ fn travel_in_fluid(
// waterMovementSpeed = 0.96F; // waterMovementSpeed = 0.96F;
// } // }
move_relative(physics, direction, speed, &acceleration); move_relative(physics, direction, speed, acceleration);
move_colliding( move_colliding(
MoverType::Own, MoverType::Own,
&physics.velocity.clone(), physics.velocity.clone(),
world, world,
&mut position, &mut position,
physics, physics,
@ -236,10 +236,10 @@ fn travel_in_fluid(
physics.velocity = physics.velocity =
get_fluid_falling_adjusted_movement(gravity, moving_down, new_velocity, sprinting); get_fluid_falling_adjusted_movement(gravity, moving_down, new_velocity, sprinting);
} else { } else {
move_relative(physics, direction, 0.02, &acceleration); move_relative(physics, direction, 0.02, acceleration);
move_colliding( move_colliding(
MoverType::Own, MoverType::Own,
&physics.velocity.clone(), physics.velocity,
world, world,
&mut position, &mut position,
physics, physics,

View file

@ -31,7 +31,7 @@ pub fn tick(bot: Client, state: State) -> anyhow::Result<()> {
continue; continue;
} }
let distance = bot_position.distance_to(position); let distance = bot_position.distance_to(**position);
if distance < 4. && distance < nearest_distance { if distance < 4. && distance < nearest_distance {
nearest_entity = Some(entity_id); nearest_entity = Some(entity_id);
nearest_distance = distance; nearest_distance = distance;

View file

@ -229,7 +229,7 @@ fn look_at_listener(
for event in events.read() { for event in events.read() {
if let Ok((position, eye_height, mut look_direction)) = query.get_mut(event.entity) { if let Ok((position, eye_height, mut look_direction)) = query.get_mut(event.entity) {
let new_look_direction = let new_look_direction =
direction_looking_at(&position.up(eye_height.into()), &event.position); direction_looking_at(position.up(eye_height.into()), event.position);
trace!("look at {} (currently at {})", event.position, **position); trace!("look at {} (currently at {})", event.position, **position);
*look_direction = new_look_direction; *look_direction = new_look_direction;
} }
@ -238,7 +238,7 @@ fn look_at_listener(
/// Return the look direction that would make a client at `current` be /// Return the look direction that would make a client at `current` be
/// looking at `target`. /// looking at `target`.
pub fn direction_looking_at(current: &Vec3, target: &Vec3) -> LookDirection { pub fn direction_looking_at(current: Vec3, target: Vec3) -> LookDirection {
// borrowed from mineflayer's Bot.lookAt because i didn't want to do math // borrowed from mineflayer's Bot.lookAt because i didn't want to do math
let delta = target - current; let delta = target - current;
let y_rot = (PI - f64::atan2(-delta.x, -delta.z)) * (180.0 / PI); let y_rot = (PI - f64::atan2(-delta.x, -delta.z)) * (180.0 / PI);

View file

@ -69,7 +69,7 @@ where
/// multiple entities are within range, only the closest one is returned. /// multiple entities are within range, only the closest one is returned.
pub fn nearest_to_position( pub fn nearest_to_position(
&'a self, &'a self,
position: &Position, position: Position,
instance_name: &InstanceName, instance_name: &InstanceName,
max_distance: f64, max_distance: f64,
) -> Option<Entity> { ) -> Option<Entity> {
@ -81,7 +81,7 @@ where
continue; continue;
} }
let target_distance = position.distance_to(e_pos); let target_distance = position.distance_to(**e_pos);
if target_distance < min_distance { if target_distance < min_distance {
nearest_entity = Some(target_entity); nearest_entity = Some(target_entity);
min_distance = target_distance; min_distance = target_distance;
@ -111,7 +111,7 @@ where
continue; continue;
} }
let target_distance = position.distance_to(e_pos); let target_distance = position.distance_to(**e_pos);
if target_distance < min_distance { if target_distance < min_distance {
nearest_entity = Some(target_entity); nearest_entity = Some(target_entity);
min_distance = target_distance; min_distance = target_distance;
@ -140,7 +140,7 @@ where
return None; return None;
} }
let distance = position.distance_to(e_pos); let distance = position.distance_to(**e_pos);
if distance < max_distance { if distance < max_distance {
Some((target_entity, distance)) Some((target_entity, distance))
} else { } else {
@ -181,7 +181,7 @@ where
return None; return None;
} }
let distance = position.distance_to(e_pos); let distance = position.distance_to(**e_pos);
if distance < max_distance { if distance < max_distance {
Some((target_entity, distance)) Some((target_entity, distance))
} else { } else {

View file

@ -65,7 +65,7 @@ pub fn debug_render_path_with_particles(
let start_vec3 = start.center(); let start_vec3 = start.center();
let end_vec3 = end.center(); let end_vec3 = end.center();
let step_count = (start_vec3.distance_squared_to(&end_vec3).sqrt() * 4.0) as usize; let step_count = (start_vec3.distance_to(end_vec3) * 4.0) as usize;
let target_block_state = chunks.get_block_state(movement.target).unwrap_or_default(); let target_block_state = chunks.get_block_state(movement.target).unwrap_or_default();
let above_target_block_state = chunks let above_target_block_state = chunks

View file

@ -230,16 +230,16 @@ impl Goal for ReachBlockPosGoal {
} }
// only do the expensive check if we're close enough // only do the expensive check if we're close enough
let distance = self.pos.distance_squared_to(&n); let distance_squared = self.pos.distance_squared_to(n);
if distance > self.max_check_distance.pow(2) { if distance_squared > self.max_check_distance.pow(2) {
return false; return false;
} }
let eye_position = n.center_bottom().up(1.62); let eye_position = n.center_bottom().up(1.62);
let look_direction = crate::direction_looking_at(&eye_position, &self.pos.center()); let look_direction = crate::direction_looking_at(eye_position, self.pos.center());
let block_hit_result = azalea_client::interact::pick_block( let block_hit_result = azalea_client::interact::pick_block(
&look_direction, look_direction,
&eye_position, eye_position,
&self.chunk_storage, &self.chunk_storage,
self.distance, self.distance,
); );

View file

@ -641,7 +641,7 @@ pub fn timeout_movement(
// don't timeout if we're mining // don't timeout if we're mining
if let Some(mining) = mining { if let Some(mining) = mining {
// also make sure we're close enough to the block that's being mined // also make sure we're close enough to the block that's being mined
if mining.pos.distance_squared_to(&BlockPos::from(position)) < 6_i32.pow(2) { if mining.pos.distance_squared_to(position.into()) < 6_i32.pow(2) {
// also reset the last_node_reached_at so we don't timeout after we finish // also reset the last_node_reached_at so we don't timeout after we finish
// mining // mining
executing_path.last_node_reached_at = Instant::now(); executing_path.last_node_reached_at = Instant::now();