diff options
| author | mat <github@matdoes.dev> | 2022-12-11 00:15:37 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-12-11 00:15:37 -0600 |
| commit | 37b9f10b3bcc74b48df2c6843a5286a7d41e2414 (patch) | |
| tree | 6a995f7ad3f3309e57c276e10dc7e655dae9c5bb /azalea-physics/src | |
| parent | 2d6737b247b74e964fd734d5ab4828a3193c296f (diff) | |
| download | azalea-drasl-37b9f10b3bcc74b48df2c6843a5286a7d41e2414.tar.xz | |
make entities have a reference to WeakWorlds instead
... and other exciting bug fixes
Diffstat (limited to 'azalea-physics/src')
| -rw-r--r-- | azalea-physics/src/collision/mod.rs | 10 | ||||
| -rw-r--r-- | azalea-physics/src/collision/world_collisions.rs | 14 | ||||
| -rw-r--r-- | azalea-physics/src/lib.rs | 27 |
3 files changed, 25 insertions, 26 deletions
diff --git a/azalea-physics/src/collision/mod.rs b/azalea-physics/src/collision/mod.rs index c057f8d7..458303c5 100644 --- a/azalea-physics/src/collision/mod.rs +++ b/azalea-physics/src/collision/mod.rs @@ -6,11 +6,11 @@ mod world_collisions; use azalea_core::{Axis, Vec3, AABB, EPSILON}; use azalea_world::entity::{Entity, EntityData}; -use azalea_world::{MoveEntityError, World}; +use azalea_world::{MoveEntityError, WeakWorld}; pub use blocks::BlockWithShape; pub use discrete_voxel_shape::*; pub use shape::*; -use std::ops::DerefMut; +use std::ops::Deref; use world_collisions::CollisionGetter; pub enum MoverType { @@ -33,7 +33,7 @@ pub trait MovableEntity { ) -> Result<(), MoveEntityError>; } -impl HasCollision for World { +impl<D: Deref<Target = WeakWorld>> HasCollision for D { // private Vec3 collide(Vec3 var1) { // AABB var2 = this.getBoundingBox(); // List var3 = this.level.getEntityCollisions(this, @@ -87,7 +87,7 @@ impl HasCollision for World { } } -impl<D: DerefMut<Target = World>> MovableEntity for Entity<'_, D> { +impl<D: Deref<Target = WeakWorld>> MovableEntity for Entity<'_, D> { /// Move an entity by a given delta, checking for collisions. fn move_colliding( &mut self, @@ -206,7 +206,7 @@ fn collide_bounding_box( entity: Option<&EntityData>, movement: &Vec3, entity_bounding_box: &AABB, - world: &World, + world: &WeakWorld, entity_collisions: Vec<VoxelShape>, ) -> Vec3 { let mut collision_boxes: Vec<VoxelShape> = Vec::with_capacity(entity_collisions.len() + 1); diff --git a/azalea-physics/src/collision/world_collisions.rs b/azalea-physics/src/collision/world_collisions.rs index a28bce14..9e238bf9 100644 --- a/azalea-physics/src/collision/world_collisions.rs +++ b/azalea-physics/src/collision/world_collisions.rs @@ -2,8 +2,8 @@ use crate::collision::{BlockWithShape, VoxelShape, AABB}; use azalea_block::BlockState; use azalea_core::{ChunkPos, ChunkSectionPos, Cursor3d, CursorIterationType, EPSILON}; use azalea_world::entity::EntityData; -use azalea_world::{Chunk, World}; -use parking_lot::Mutex; +use azalea_world::{Chunk, WeakWorld}; +use parking_lot::RwLock; use std::sync::Arc; use super::Shapes; @@ -16,7 +16,7 @@ pub trait CollisionGetter { ) -> BlockCollisions<'a>; } -impl CollisionGetter for World { +impl CollisionGetter for WeakWorld { fn get_block_collisions<'a>( &'a self, entity: Option<&EntityData>, @@ -27,7 +27,7 @@ impl CollisionGetter for World { } pub struct BlockCollisions<'a> { - pub world: &'a World, + pub world: &'a WeakWorld, // context: CollisionContext, pub aabb: AABB, pub entity_shape: VoxelShape, @@ -37,7 +37,7 @@ pub struct BlockCollisions<'a> { impl<'a> BlockCollisions<'a> { // TODO: the entity is stored in the context - pub fn new(world: &'a World, _entity: Option<&EntityData>, aabb: AABB) -> Self { + pub fn new(world: &'a WeakWorld, _entity: Option<&EntityData>, aabb: AABB) -> Self { let origin_x = (aabb.min_x - EPSILON) as i32 - 1; let origin_y = (aabb.min_y - EPSILON) as i32 - 1; let origin_z = (aabb.min_z - EPSILON) as i32 - 1; @@ -57,7 +57,7 @@ impl<'a> BlockCollisions<'a> { } } - fn get_chunk(&self, block_x: i32, block_z: i32) -> Option<Arc<Mutex<Chunk>>> { + fn get_chunk(&self, block_x: i32, block_z: i32) -> Option<Arc<RwLock<Chunk>>> { let chunk_x = ChunkSectionPos::block_to_section_coord(block_x); let chunk_z = ChunkSectionPos::block_to_section_coord(block_z); let chunk_pos = ChunkPos::new(chunk_x, chunk_z); @@ -96,7 +96,7 @@ impl<'a> Iterator for BlockCollisions<'a> { let pos = item.pos; let block_state: BlockState = chunk - .lock() + .read() .get(&(&pos).into(), self.world.min_y()) .unwrap_or(BlockState::Air); diff --git a/azalea-physics/src/lib.rs b/azalea-physics/src/lib.rs index d419535e..76ae3990 100644 --- a/azalea-physics/src/lib.rs +++ b/azalea-physics/src/lib.rs @@ -2,15 +2,14 @@ pub mod collision; -use std::ops::DerefMut; - use azalea_block::{Block, BlockState}; use azalea_core::{BlockPos, Vec3}; use azalea_world::{ entity::{Entity, EntityData}, - World, + WeakWorld, }; use collision::{MovableEntity, MoverType}; +use std::ops::Deref; pub trait HasPhysics { fn travel(&mut self, acceleration: &Vec3); @@ -19,7 +18,7 @@ pub trait HasPhysics { fn jump_from_ground(&mut self); } -impl<D: DerefMut<Target = World>> HasPhysics for Entity<'_, D> { +impl<D: Deref<Target = WeakWorld>> HasPhysics for Entity<'_, D> { /// Move the entity with the given acceleration while handling friction, /// gravity, collisions, and some other stuff. fn travel(&mut self, acceleration: &Vec3) { @@ -143,7 +142,7 @@ fn get_block_pos_below_that_affects_movement(entity: &EntityData) -> BlockPos { ) } -fn handle_relative_friction_and_calculate_movement<D: DerefMut<Target = World>>( +fn handle_relative_friction_and_calculate_movement<D: Deref<Target = WeakWorld>>( entity: &mut Entity<D>, acceleration: &Vec3, block_friction: f32, @@ -183,7 +182,7 @@ fn get_friction_influenced_speed(entity: &EntityData, friction: f32) -> f32 { /// Returns the what the entity's jump should be multiplied by based on the /// block they're standing on. -fn block_jump_factor<D: DerefMut<Target = World>>(entity: &Entity<D>) -> f32 { +fn block_jump_factor<D: Deref<Target = WeakWorld>>(entity: &Entity<D>) -> f32 { let block_at_pos = entity.world.get_block_state(&entity.pos().into()); let block_below = entity .world @@ -211,11 +210,11 @@ fn block_jump_factor<D: DerefMut<Target = World>>(entity: &Entity<D>) -> f32 { // public double getJumpBoostPower() { // return this.hasEffect(MobEffects.JUMP) ? (double)(0.1F * // (float)(this.getEffect(MobEffects.JUMP).getAmplifier() + 1)) : 0.0D; } -fn jump_power<D: DerefMut<Target = World>>(entity: &Entity<D>) -> f32 { +fn jump_power<D: Deref<Target = WeakWorld>>(entity: &Entity<D>) -> f32 { 0.42 * block_jump_factor(entity) } -fn jump_boost_power<D: DerefMut<Target = World>>(_entity: &Entity<D>) -> f64 { +fn jump_boost_power<D: Deref<Target = WeakWorld>>(_entity: &Entity<D>) -> f64 { // TODO: potion effects // if let Some(effects) = entity.effects() { // if let Some(jump_effect) = effects.get(&Effect::Jump) { @@ -235,13 +234,13 @@ mod tests { use azalea_core::ChunkPos; use azalea_world::{ entity::{metadata, EntityMetadata}, - Chunk, World, + Chunk, PartialWorld, }; use uuid::Uuid; #[test] fn test_gravity() { - let mut world = World::default(); + let mut world = PartialWorld::default(); world.add_entity( 0, @@ -272,7 +271,7 @@ mod tests { } #[test] fn test_collision() { - let mut world = World::default(); + let mut world = PartialWorld::default(); world .set_chunk(&ChunkPos { x: 0, z: 0 }, Some(Chunk::default())) .unwrap(); @@ -305,7 +304,7 @@ mod tests { #[test] fn test_slab_collision() { - let mut world = World::default(); + let mut world = PartialWorld::default(); world .set_chunk(&ChunkPos { x: 0, z: 0 }, Some(Chunk::default())) .unwrap(); @@ -339,7 +338,7 @@ mod tests { #[test] fn test_top_slab_collision() { - let mut world = World::default(); + let mut world = PartialWorld::default(); world .set_chunk(&ChunkPos { x: 0, z: 0 }, Some(Chunk::default())) .unwrap(); @@ -373,7 +372,7 @@ mod tests { #[test] fn test_weird_wall_collision() { - let mut world = World::default(); + let mut world = PartialWorld::default(); world .set_chunk(&ChunkPos { x: 0, z: 0 }, Some(Chunk::default())) .unwrap(); |
