From a2606569bb79867d07a075bcf7b05730e4264d72 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 11 Jun 2025 22:58:41 -0630 Subject: use owned instead of borrowed Vec3 more --- azalea-physics/src/clip.rs | 28 ++++++------- azalea-physics/src/collision/mod.rs | 79 +++++++++++++---------------------- azalea-physics/src/collision/shape.rs | 16 +++---- azalea-physics/src/fluids.rs | 4 +- azalea-physics/src/lib.rs | 73 ++++++++++++++++---------------- azalea-physics/src/travel.rs | 28 ++++++------- 6 files changed, 105 insertions(+), 123 deletions(-) (limited to 'azalea-physics/src') diff --git a/azalea-physics/src/clip.rs b/azalea-physics/src/clip.rs index 2cef15c4..8d2b5dd1 100644 --- a/azalea-physics/src/clip.rs +++ b/azalea-physics/src/clip.rs @@ -101,22 +101,22 @@ pub fn clip(chunk_storage: &ChunkStorage, context: ClipContext) -> BlockHitResul let block_shape = ctx.block_shape(block_state); let interaction_clip = clip_with_interaction_override( - &ctx.from, - &ctx.to, + ctx.from, + ctx.to, block_pos, block_shape, - &block_state, + block_state, ); 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 .as_ref() - .map(|hit| ctx.from.distance_squared_to(&hit.location)) + .map(|hit| ctx.from.distance_squared_to(hit.location)) .unwrap_or(f64::MAX); let distance_to_fluid = fluid_clip .as_ref() - .map(|hit| ctx.from.distance_squared_to(&hit.location)) + .map(|hit| ctx.from.distance_squared_to(hit.location)) .unwrap_or(f64::MAX); 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( - from: &Vec3, - to: &Vec3, + from: Vec3, + to: Vec3, block_pos: BlockPos, block_shape: &VoxelShape, - _block_state: &BlockState, + _block_state: BlockState, ) -> Option { let block_hit_result = block_shape.clip(from, to, block_pos); @@ -255,7 +255,7 @@ pub fn traverse_blocks( } } -pub fn box_traverse_blocks(from: &Vec3, to: &Vec3, aabb: &AABB) -> HashSet { +pub fn box_traverse_blocks(from: Vec3, to: Vec3, aabb: &AABB) -> HashSet { let delta = to - from; let traversed_blocks = BlockPos::between_closed_aabb(aabb); if delta.length_squared() < (0.99999_f32 * 0.99999) as f64 { @@ -346,10 +346,10 @@ pub fn add_collisions_along_travel( step_count += 1; 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 + 1) as f64, (min_y + 1) as f64, (min_z + 1) as f64), - &from, - &to, + 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), + from, + to, ) else { continue; }; diff --git a/azalea-physics/src/collision/mod.rs b/azalea-physics/src/collision/mod.rs index ef994deb..41fc6c85 100644 --- a/azalea-physics/src/collision/mod.rs +++ b/azalea-physics/src/collision/mod.rs @@ -35,7 +35,7 @@ pub enum MoverType { // Entity.collide fn collide( - movement: &Vec3, + movement: Vec3, world: &Instance, physics: &azalea_entity::Physics, source_entity: Option, @@ -51,7 +51,7 @@ fn collide( collidable_entity_query, ); let collided_delta = if movement.length_squared() == 0.0 { - *movement + movement } else { collide_bounding_box(movement, &entity_bounding_box, world, &entity_collisions) }; @@ -65,20 +65,20 @@ fn collide( let max_up_step = 0.6; if max_up_step > 0. && on_ground && (x_collision || z_collision) { let mut step_to_delta = collide_bounding_box( - &movement.with_y(max_up_step), + movement.with_y(max_up_step), &entity_bounding_box, world, &entity_collisions, ); let directly_up_delta = collide_bounding_box( - &Vec3::ZERO.with_y(max_up_step), - &entity_bounding_box.expand_towards(&Vec3::new(movement.x, 0., movement.z)), + Vec3::ZERO.with_y(max_up_step), + &entity_bounding_box.expand_towards(Vec3::new(movement.x, 0., movement.z)), world, &entity_collisions, ); if directly_up_delta.y < max_up_step { let target_movement = collide_bounding_box( - &movement.with_y(0.), + movement.with_y(0.), &entity_bounding_box.move_relative(directly_up_delta), world, &entity_collisions, @@ -95,7 +95,7 @@ fn collide( > collided_delta.horizontal_distance_squared() { 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), world, &entity_collisions, @@ -112,7 +112,7 @@ fn collide( #[allow(clippy::too_many_arguments)] pub fn move_colliding( _mover_type: MoverType, - movement: &Vec3, + movement: Vec3, world: &Instance, position: &mut Mut, physics: &mut azalea_entity::Physics, @@ -180,7 +180,7 @@ pub fn move_colliding( // 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 // .world // .get_block_state(&block_pos_below) @@ -239,7 +239,7 @@ pub fn move_colliding( } fn collide_bounding_box( - movement: &Vec3, + movement: Vec3, entity_bounding_box: &AABB, world: &Instance, entity_collisions: &[VoxelShape], @@ -259,63 +259,44 @@ fn collide_bounding_box( } fn collide_with_shapes( - movement: &Vec3, + mut movement: Vec3, mut entity_box: AABB, - collision_boxes: &Vec, + collision_boxes: &[VoxelShape], ) -> Vec3 { if collision_boxes.is_empty() { - return *movement; + return movement; } - let mut x_movement = movement.x; - let mut y_movement = movement.y; - let mut z_movement = movement.z; - if y_movement != 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., - }); + if movement.y != 0. { + movement.y = Shapes::collide(Axis::Y, &entity_box, collision_boxes, movement.y); + if movement.y != 0. { + entity_box = entity_box.move_relative(Vec3::new(0., movement.y, 0.)); } } // whether the player is moving more in the z axis than x // this is done to fix a movement bug, minecraft does this too - let more_z_movement = x_movement.abs() < z_movement.abs(); - - if more_z_movement && z_movement != 0. { - z_movement = Shapes::collide(&Axis::Z, &entity_box, collision_boxes, z_movement); - if z_movement != 0. { - entity_box = entity_box.move_relative(Vec3 { - x: 0., - y: 0., - z: z_movement, - }); + let more_z_movement = movement.x.abs() < movement.z.abs(); + + if more_z_movement && movement.z != 0. { + movement.z = Shapes::collide(Axis::Z, &entity_box, collision_boxes, movement.z); + if movement.z != 0. { + entity_box = entity_box.move_relative(Vec3::new(0., 0., movement.z)); } } - if x_movement != 0. { - x_movement = Shapes::collide(&Axis::X, &entity_box, collision_boxes, x_movement); - if x_movement != 0. { - entity_box = entity_box.move_relative(Vec3 { - x: x_movement, - y: 0., - z: 0., - }); + if movement.x != 0. { + movement.x = Shapes::collide(Axis::X, &entity_box, collision_boxes, movement.x); + if movement.x != 0. { + entity_box = entity_box.move_relative(Vec3::new(movement.x, 0., 0.)); } } - if !more_z_movement && z_movement != 0. { - z_movement = Shapes::collide(&Axis::Z, &entity_box, collision_boxes, z_movement); + if !more_z_movement && movement.z != 0. { + movement.z = Shapes::collide(Axis::Z, &entity_box, collision_boxes, movement.z); } - Vec3 { - x: x_movement, - y: y_movement, - z: z_movement, - } + movement } /// Get the [`VoxelShape`] for the given fluid state. diff --git a/azalea-physics/src/collision/shape.rs b/azalea-physics/src/collision/shape.rs index 4d430ee7..9caae590 100644 --- a/azalea-physics/src/collision/shape.rs +++ b/azalea-physics/src/collision/shape.rs @@ -98,9 +98,9 @@ impl Shapes { } pub fn collide( - axis: &Axis, + axis: Axis, entity_box: &AABB, - collision_boxes: &Vec, + collision_boxes: &[VoxelShape], mut movement: f64, ) -> f64 { for shape in collision_boxes { @@ -408,7 +408,7 @@ impl VoxelShape { } } - pub fn clip(&self, from: &Vec3, to: &Vec3, block_pos: BlockPos) -> Option { + pub fn clip(&self, from: Vec3, to: Vec3, block_pos: BlockPos) -> Option { if self.is_empty() { return None; } @@ -416,7 +416,7 @@ impl VoxelShape { if vector.length_squared() < EPSILON { return None; } - let right_after_start = from + &(vector * 0.001); + let right_after_start = from + (vector * 0.001); if self.shape().is_full_wide( 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 { - self.collide_x(AxisCycle::between(*axis, Axis::X), entity_box, movement) + pub fn collide(&self, axis: Axis, entity_box: &AABB, movement: f64) -> f64 { + 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 { if self.shape().is_empty() { @@ -753,8 +753,8 @@ mod tests { let block_shape = &*BLOCK_SHAPE; let block_hit_result = block_shape .clip( - &Vec3::new(-0.3, 0.5, 0.), - &Vec3::new(5.3, 0.5, 0.), + Vec3::new(-0.3, 0.5, 0.), + Vec3::new(5.3, 0.5, 0.), BlockPos::new(0, 0, 0), ) .unwrap(); diff --git a/azalea-physics/src/fluids.rs b/azalea-physics/src/fluids.rs index 5ea6194a..ea0f12e2 100644 --- a/azalea-physics/src/fluids.rs +++ b/azalea-physics/src/fluids.rs @@ -32,7 +32,7 @@ pub fn update_in_water_state_and_do_fluid_pushing( physics.water_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 // 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( physics: &mut Physics, world: &Instance, - _position: &Position, + _position: Position, ) { // TODO: implement vehicles and boats // if vehicle == AbstractBoat { diff --git a/azalea-physics/src/lib.rs b/azalea-physics/src/lib.rs index 1f381174..d0db3c22 100644 --- a/azalea-physics/src/lib.rs +++ b/azalea-physics/src/lib.rs @@ -18,6 +18,7 @@ use azalea_entity::{ Attributes, InLoadedChunk, Jumping, LocalEntity, LookDirection, OnClimbable, Physics, Pose, Position, metadata::Sprinting, move_relative, }; +use azalea_registry::Block; use azalea_world::{Instance, InstanceContainer, InstanceName}; use bevy_app::{App, Plugin}; use bevy_ecs::prelude::*; @@ -110,9 +111,9 @@ pub fn ai_step( { jump_from_ground( &mut physics, - position, - look_direction, - sprinting, + *position, + *look_direction, + *sprinting, instance_name, &instance_container, ); @@ -187,11 +188,11 @@ fn check_inside_blocks( for movement in movements { let bounding_box_at_target = physics .dimensions - .make_bounding_box(&movement.to) + .make_bounding_box(movement.to) .deflate_all(1.0E-5); 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()) { // return; @@ -221,8 +222,8 @@ fn check_inside_blocks( if entity_inside_collision_shape != &*BLOCK_SHAPE && !collided_with_shape_moving_from( - &movement.from, - &movement.to, + movement.from, + movement.to, traversed_block, entity_inside_collision_shape, physics, @@ -241,8 +242,8 @@ fn check_inside_blocks( } fn collided_with_shape_moving_from( - from: &Vec3, - to: &Vec3, + from: Vec3, + to: Vec3, traversed_block: BlockPos, entity_inside_collision_shape: &VoxelShape, physics: &Physics, @@ -304,9 +305,9 @@ pub struct EntityMovement { pub fn jump_from_ground( physics: &mut Physics, - position: &Position, - look_direction: &LookDirection, - sprinting: &Sprinting, + position: Position, + look_direction: LookDirection, + sprinting: Sprinting, instance_name: &InstanceName, instance_container: &InstanceContainer, ) { @@ -322,7 +323,7 @@ pub fn jump_from_ground( y: jump_power, z: old_delta_movement.z, }; - if **sprinting { + if *sprinting { // sprint jumping gives some extra velocity let y_rot = look_direction.y_rot * 0.017453292; physics.velocity += Vec3 { @@ -337,11 +338,11 @@ pub fn jump_from_ground( pub fn update_old_position(mut query: Query<(&mut Physics, &Position)>) { 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( position.x.floor() as i32, // TODO: this uses bounding_box.min_y instead of position.y @@ -355,13 +356,13 @@ struct HandleRelativeFrictionAndCalculateMovementOpts<'a, 'b, 'world, 'state> { block_friction: f32, world: &'a Instance, physics: &'a mut Physics, - direction: &'a LookDirection, + direction: LookDirection, position: Mut<'a, Position>, attributes: &'a Attributes, is_sprinting: bool, - on_climbable: &'a OnClimbable, - pose: Option<&'a Pose>, - jumping: &'a Jumping, + on_climbable: OnClimbable, + pose: Option, + jumping: Jumping, entity: Entity, physics_query: &'a PhysicsQuery<'world, 'state, 'b>, collidable_entity_query: &'a CollidableEntityQuery<'world, 'state>, @@ -387,18 +388,18 @@ fn handle_relative_friction_and_calculate_movement( physics, direction, get_friction_influenced_speed(physics, attributes, block_friction, is_sprinting), - &Vec3 { - x: physics.x_acceleration as f64, - y: physics.y_acceleration as f64, - z: physics.z_acceleration as f64, - }, + Vec3::new( + physics.x_acceleration as f64, + physics.y_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( MoverType::Own, - &physics.velocity.clone(), + physics.velocity, world, &mut position, physics, @@ -414,14 +415,14 @@ fn handle_relative_friction_and_calculate_movement( // PowderSnowBlock.canEntityWalkOnPowderSnow(entity))) { var3 = new // Vec3(var3.x, 0.2D, var3.z); } - if physics.horizontal_collision || **jumping { - let block_at_feet: azalea_registry::Block = world + if physics.horizontal_collision || *jumping { + let block_at_feet: Block = world .chunks .get_block_state((*position).into()) .unwrap_or_default() .into(); - if **on_climbable || block_at_feet == azalea_registry::Block::PowderSnow { + if *on_climbable || block_at_feet == Block::PowderSnow { physics.velocity.y = 0.2; } } @@ -431,12 +432,12 @@ fn handle_relative_friction_and_calculate_movement( fn handle_on_climbable( velocity: Vec3, - on_climbable: &OnClimbable, - position: &Position, + on_climbable: OnClimbable, + position: Position, world: &Instance, - pose: Option<&Pose>, + pose: Option, ) -> Vec3 { - if !**on_climbable { + if !*on_climbable { return velocity; } @@ -450,7 +451,7 @@ fn handle_on_climbable( // sneaking on ladders/vines if y < 0.0 - && pose.copied() == Some(Pose::Sneaking) + && pose == Some(Pose::Sneaking) && azalea_registry::Block::from( world .chunks @@ -485,7 +486,7 @@ fn get_friction_influenced_speed( /// Returns the what the entity's jump should be multiplied by based on the /// 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_below = world .chunks @@ -513,7 +514,7 @@ fn block_jump_factor(world: &Instance, position: &Position) -> f32 { // public double getJumpBoostPower() { // return this.hasEffect(MobEffects.JUMP) ? (double)(0.1F * // (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) } diff --git a/azalea-physics/src/travel.rs b/azalea-physics/src/travel.rs index a442f629..dba991d5 100644 --- a/azalea-physics/src/travel.rs +++ b/azalea-physics/src/travel.rs @@ -73,7 +73,7 @@ pub fn travel( &world, entity, &mut physics, - &direction, + *direction, position, attributes, sprinting, @@ -86,13 +86,13 @@ pub fn travel( &world, entity, &mut physics, - &direction, + *direction, position, attributes, sprinting, - on_climbable, + *on_climbable, pose, - jumping, + *jumping, &physics_query, &collidable_entity_query, ); @@ -106,19 +106,19 @@ fn travel_in_air( world: &Instance, entity: Entity, physics: &mut Physics, - direction: &LookDirection, + direction: LookDirection, position: Mut, attributes: &Attributes, sprinting: Sprinting, - on_climbable: &OnClimbable, + on_climbable: OnClimbable, pose: Option<&Pose>, - jumping: &Jumping, + jumping: Jumping, physics_query: &PhysicsQuery, collidable_entity_query: &CollidableEntityQuery, ) { 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 .chunks @@ -144,7 +144,7 @@ fn travel_in_air( attributes, is_sprinting: *sprinting, on_climbable, - pose, + pose: pose.copied(), jumping, entity, physics_query, @@ -177,7 +177,7 @@ fn travel_in_fluid( world: &Instance, entity: Entity, physics: &mut Physics, - direction: &LookDirection, + direction: LookDirection, mut position: Mut, attributes: &Attributes, sprinting: Sprinting, @@ -212,10 +212,10 @@ fn travel_in_fluid( // waterMovementSpeed = 0.96F; // } - move_relative(physics, direction, speed, &acceleration); + move_relative(physics, direction, speed, acceleration); move_colliding( MoverType::Own, - &physics.velocity.clone(), + physics.velocity.clone(), world, &mut position, physics, @@ -236,10 +236,10 @@ fn travel_in_fluid( physics.velocity = get_fluid_falling_adjusted_movement(gravity, moving_down, new_velocity, sprinting); } else { - move_relative(physics, direction, 0.02, &acceleration); + move_relative(physics, direction, 0.02, acceleration); move_colliding( MoverType::Own, - &physics.velocity.clone(), + physics.velocity, world, &mut position, physics, -- cgit v1.2.3