diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2023-02-04 19:32:27 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-04 19:32:27 -0600 |
| commit | a5672815ccef520b433363ac622dbb6d6af60c91 (patch) | |
| tree | f9bb1b41876d81423ac3f188f4d368e6d362eed1 /azalea-physics/src/collision | |
| parent | 7c7446ab1e467c29f86e9bfba260741fc469389a (diff) | |
| download | azalea-drasl-a5672815ccef520b433363ac622dbb6d6af60c91.tar.xz | |
Use an ECS (#52)
* add EntityData::kind
* start making metadata use hecs
* make entity codegen generate ecs stuff
* fix registry codegen
* get rid of worldhaver
it's not even used
* add bevy_ecs to deps
* rename Component to FormattedText
also start making the metadata use bevy_ecs but bevy_ecs doesn't let you query on Bundles so it's annoying
* generate metadata.rs correctly for bevy_ecs
* start switching more entity stuff to use ecs
* more ecs stuff for entity storage
* ok well it compiles but
it definitely doesn't work
* random fixes
* change a bunch of entity things to use the components
* some ecs stuff in az-client
* packet handler uses the ecs now
and other fun changes
i still need to make ticking use the ecs but that's tricker, i'm considering using bevy_ecs systems for those
bevy_ecs systems can't be async but the only async things in ticking is just sending packets which can just be done as a tokio task so that's not a big deal
* start converting some functions in az-client into systems
committing because i'm about to try something that might go horribly wrong
* start splitting client
i'm probably gonna change it so azalea entity ids are separate from minecraft entity ids next (so stuff like player ids can be consistent and we don't have to wait for the login packet)
* separate minecraft entity ids from azalea entity ids + more ecs stuff
i guess i'm using bevy_app now too huh
it's necessary for plugins and it lets us control the tick rate anyways so it's fine i think
i'm still not 100% sure how packet handling that interacts with the world will work, but i think if i can sneak the ecs world into there it'll be fine. Can't put packet handling in the schedule because that'd make it tick-bound, which it's not (technically it'd still work but it'd be wrong and anticheats might realize).
* packet handling
now it runs the schedule only when we get a tick or packet :smile:
also i systemified some more functions and did other random fixes so az-world and az-physics compile
making azalea-client use the ecs is almost done! all the hard parts are done now i hope, i just have to finish writing all the code so it actually works
* start figuring out how functions in Client will work
generally just lifetimes being annoying but i think i can get it all to work
* make writing packets work synchronously*
* huh az-client compiles
* start fixing stuff
* start fixing some packets
* make packet handler work
i still haven't actually tested any of this yet lol but in theory it should all work
i'll probably either actually test az-client and fix all the remaining issues or update the azalea crate next
ok also one thing that i'm not particularly happy with is how the packet handlers are doing ugly queries like
```rs
let local_player = ecs
.query::<&LocalPlayer>()
.get_mut(ecs, player_entity)
.unwrap();
```
i think the right way to solve it would be by putting every packet handler in its own system but i haven't come up with a way to make that not be really annoying yet
* fix warnings
* ok what if i just have a bunch of queries and a single packet handler system
* simple example for azalea-client
* :bug:
* maybe fix deadlock idk
can't test it rn lmao
* make physicsstate its own component
* use the default plugins
* azalea compiles lol
* use systemstate for packet handler
* fix entities
basically moved some stuff from being in the world to just being components
* physics (ticking) works
* try to add a .entity_by function
still doesn't work because i want to make the predicate magic
* try to make entity_by work
well it does work but i couldn't figure out how to make it look not terrible. Will hopefully change in the future
* everything compiles
* start converting swarm to use builder
* continue switching swarm to builder and fix stuff
* make swarm use builder
still have to fix some stuff and make client use builder
* fix death event
* client builder
* fix some warnings
* document plugins a bit
* start trying to fix tests
* azalea-ecs
* azalea-ecs stuff compiles
* az-physics tests pass :tada:
* fix all the tests
* clippy on azalea-ecs-macros
* remove now-unnecessary trait_upcasting feature
* fix some clippy::pedantic warnings lol
* why did cargo fmt not remove the trailing spaces
* FIX ALL THE THINGS
* when i said 'all' i meant non-swarm bugs
* start adding task pool
* fix entity deduplication
* fix pathfinder not stopping
* fix some more random bugs
* fix panic that sometimes happens in swarms
* make pathfinder run in task
* fix some tests
* fix doctests and clippy
* deadlock
* fix systems running in wrong order
* fix non-swarm bots
Diffstat (limited to 'azalea-physics/src/collision')
| -rwxr-xr-x | azalea-physics/src/collision/discrete_voxel_shape.rs | 2 | ||||
| -rw-r--r-- | azalea-physics/src/collision/mod.rs | 304 | ||||
| -rwxr-xr-x | azalea-physics/src/collision/shape.rs | 2 | ||||
| -rw-r--r-- | azalea-physics/src/collision/world_collisions.rs | 39 |
4 files changed, 154 insertions, 193 deletions
diff --git a/azalea-physics/src/collision/discrete_voxel_shape.rs b/azalea-physics/src/collision/discrete_voxel_shape.rs index 51d45316..4a329398 100755 --- a/azalea-physics/src/collision/discrete_voxel_shape.rs +++ b/azalea-physics/src/collision/discrete_voxel_shape.rs @@ -64,7 +64,7 @@ impl DiscreteVoxelShape { } pub fn for_all_boxes(&self, consumer: impl IntLineConsumer, swap: bool) { - BitSetDiscreteVoxelShape::for_all_boxes(self, consumer, swap) + BitSetDiscreteVoxelShape::for_all_boxes(self, consumer, swap); } } diff --git a/azalea-physics/src/collision/mod.rs b/azalea-physics/src/collision/mod.rs index 458303c5..7d934020 100644 --- a/azalea-physics/src/collision/mod.rs +++ b/azalea-physics/src/collision/mod.rs @@ -5,13 +5,15 @@ mod shape; mod world_collisions; use azalea_core::{Axis, Vec3, AABB, EPSILON}; -use azalea_world::entity::{Entity, EntityData}; -use azalea_world::{MoveEntityError, WeakWorld}; +use azalea_world::{ + entity::{self}, + MoveEntityError, World, +}; pub use blocks::BlockWithShape; pub use discrete_voxel_shape::*; pub use shape::*; -use std::ops::Deref; -use world_collisions::CollisionGetter; + +use self::world_collisions::get_block_collisions; pub enum MoverType { Own, @@ -21,192 +23,170 @@ pub enum MoverType { Shulker, } -pub trait HasCollision { - fn collide(&self, movement: &Vec3, entity: &EntityData) -> Vec3; -} +// private Vec3 collide(Vec3 var1) { +// AABB var2 = this.getBoundingBox(); +// List var3 = this.level.getEntityCollisions(this, +// var2.expandTowards(var1)); Vec3 var4 = var1.lengthSqr() == 0.0D ? +// var1 : collideBoundingBox(this, var1, var2, this.level, var3); +// boolean var5 = var1.x != var4.x; +// boolean var6 = var1.y != var4.y; +// boolean var7 = var1.z != var4.z; +// boolean var8 = this.onGround || var6 && var1.y < 0.0D; +// if (this.maxUpStep > 0.0F && var8 && (var5 || var7)) { +// Vec3 var9 = collideBoundingBox(this, new Vec3(var1.x, +// (double)this.maxUpStep, var1.z), var2, this.level, var3); Vec3 +// var10 = collideBoundingBox(this, new Vec3(0.0D, (double)this.maxUpStep, +// 0.0D), var2.expandTowards(var1.x, 0.0D, var1.z), this.level, var3); +// if (var10.y < (double)this.maxUpStep) { +// Vec3 var11 = collideBoundingBox(this, new Vec3(var1.x, 0.0D, +// var1.z), var2.move(var10), this.level, var3).add(var10); if +// (var11.horizontalDistanceSqr() > var9.horizontalDistanceSqr()) { +// var9 = var11; +// } +// } + +// if (var9.horizontalDistanceSqr() > var4.horizontalDistanceSqr()) { +// return var9.add(collideBoundingBox(this, new Vec3(0.0D, -var9.y + +// var1.y, 0.0D), var2.move(var9), this.level, var3)); } +// } + +// return var4; +// } +fn collide(movement: &Vec3, world: &World, physics: &entity::Physics) -> Vec3 { + let entity_bounding_box = physics.bounding_box; + // TODO: get_entity_collisions + // let entity_collisions = world.get_entity_collisions(self, + // entity_bounding_box.expand_towards(movement)); + let entity_collisions = Vec::new(); + if movement.length_sqr() == 0.0 { + *movement + } else { + collide_bounding_box(movement, &entity_bounding_box, world, entity_collisions) + } -pub trait MovableEntity { - fn move_colliding( - &mut self, - mover_type: &MoverType, - movement: &Vec3, - ) -> Result<(), MoveEntityError>; + // TODO: stepping (for stairs and stuff) + + // collided_movement } -impl<D: Deref<Target = WeakWorld>> HasCollision for D { - // private Vec3 collide(Vec3 var1) { - // AABB var2 = this.getBoundingBox(); - // List var3 = this.level.getEntityCollisions(this, - // var2.expandTowards(var1)); Vec3 var4 = var1.lengthSqr() == 0.0D ? - // var1 : collideBoundingBox(this, var1, var2, this.level, var3); - // boolean var5 = var1.x != var4.x; - // boolean var6 = var1.y != var4.y; - // boolean var7 = var1.z != var4.z; - // boolean var8 = this.onGround || var6 && var1.y < 0.0D; - // if (this.maxUpStep > 0.0F && var8 && (var5 || var7)) { - // Vec3 var9 = collideBoundingBox(this, new Vec3(var1.x, - // (double)this.maxUpStep, var1.z), var2, this.level, var3); Vec3 - // var10 = collideBoundingBox(this, new Vec3(0.0D, (double)this.maxUpStep, - // 0.0D), var2.expandTowards(var1.x, 0.0D, var1.z), this.level, var3); - // if (var10.y < (double)this.maxUpStep) { - // Vec3 var11 = collideBoundingBox(this, new Vec3(var1.x, 0.0D, - // var1.z), var2.move(var10), this.level, var3).add(var10); if - // (var11.horizontalDistanceSqr() > var9.horizontalDistanceSqr()) { - // var9 = var11; - // } - // } - - // if (var9.horizontalDistanceSqr() > var4.horizontalDistanceSqr()) { - // return var9.add(collideBoundingBox(this, new Vec3(0.0D, -var9.y + - // var1.y, 0.0D), var2.move(var9), this.level, var3)); } +/// Move an entity by a given delta, checking for collisions. +pub fn move_colliding( + _mover_type: &MoverType, + movement: &Vec3, + world: &World, + position: &mut entity::Position, + physics: &mut entity::Physics, +) -> Result<(), MoveEntityError> { + // TODO: do all these + + // if self.no_physics { + // return; + // }; + + // if (var1 == MoverType.PISTON) { + // var2 = this.limitPistonMovement(var2); + // if (var2.equals(Vec3.ZERO)) { + // return; // } + // } - // return var4; + // if (this.stuckSpeedMultiplier.lengthSqr() > 1.0E-7D) { + // var2 = var2.multiply(this.stuckSpeedMultiplier); + // this.stuckSpeedMultiplier = Vec3.ZERO; + // this.setDeltaMovement(Vec3.ZERO); // } - fn collide(&self, movement: &Vec3, entity: &EntityData) -> Vec3 { - let entity_bounding_box = entity.bounding_box; - // TODO: get_entity_collisions - // let entity_collisions = world.get_entity_collisions(self, - // entity_bounding_box.expand_towards(movement)); - let entity_collisions = Vec::new(); - if movement.length_sqr() == 0.0 { - *movement - } else { - collide_bounding_box( - Some(entity), - movement, - &entity_bounding_box, - self, - entity_collisions, - ) - } - // TODO: stepping (for stairs and stuff) + // movement = this.maybeBackOffFromEdge(movement, moverType); - // collided_movement - } -} + let collide_result = collide(movement, world, physics); -impl<D: Deref<Target = WeakWorld>> MovableEntity for Entity<'_, D> { - /// Move an entity by a given delta, checking for collisions. - fn move_colliding( - &mut self, - _mover_type: &MoverType, - movement: &Vec3, - ) -> Result<(), MoveEntityError> { - // TODO: do all these - - // if self.no_physics { - // return; - // }; - - // if (var1 == MoverType.PISTON) { - // var2 = this.limitPistonMovement(var2); - // if (var2.equals(Vec3.ZERO)) { - // return; - // } - // } - - // if (this.stuckSpeedMultiplier.lengthSqr() > 1.0E-7D) { - // var2 = var2.multiply(this.stuckSpeedMultiplier); - // this.stuckSpeedMultiplier = Vec3.ZERO; - // this.setDeltaMovement(Vec3.ZERO); - // } - - // movement = this.maybeBackOffFromEdge(movement, moverType); - - let collide_result = { self.world.collide(movement, self) }; - - let move_distance = collide_result.length_sqr(); - - if move_distance > EPSILON { - // TODO: fall damage - - let new_pos = { - let entity_pos = self.pos(); - Vec3 { - x: entity_pos.x + collide_result.x, - y: entity_pos.y + collide_result.y, - z: entity_pos.z + collide_result.z, - } - }; - - self.world.set_entity_pos(self.id, new_pos)?; - } + let move_distance = collide_result.length_sqr(); + + if move_distance > EPSILON { + // TODO: fall damage + + let new_pos = { + Vec3 { + x: position.x + collide_result.x, + y: position.y + collide_result.y, + z: position.z + collide_result.z, + } + }; - let x_collision = movement.x != collide_result.x; - let z_collision = movement.z != collide_result.z; - let horizontal_collision = x_collision || z_collision; - let vertical_collision = movement.y != collide_result.y; - let on_ground = vertical_collision && movement.y < 0.; - self.on_ground = on_ground; + **position = new_pos; + } - // TODO: minecraft checks for a "minor" horizontal collision here + let x_collision = movement.x != collide_result.x; + let z_collision = movement.z != collide_result.z; + let horizontal_collision = x_collision || z_collision; + let vertical_collision = movement.y != collide_result.y; + let on_ground = vertical_collision && movement.y < 0.; + physics.on_ground = on_ground; - let _block_pos_below = self.on_pos_legacy(); - // let _block_state_below = self - // .world - // .get_block_state(&block_pos_below) - // .expect("Couldn't get block state below"); + // TODO: minecraft checks for a "minor" horizontal collision here - // self.check_fall_damage(collide_result.y, on_ground, block_state_below, - // block_pos_below); + let _block_pos_below = entity::on_pos_legacy(&world.chunks, position); + // let _block_state_below = self + // .world + // .get_block_state(&block_pos_below) + // .expect("Couldn't get block state below"); - // if self.isRemoved() { return; } + // self.check_fall_damage(collide_result.y, on_ground, block_state_below, + // block_pos_below); - if horizontal_collision { - let delta_movement = &self.delta; - self.delta = Vec3 { - x: if x_collision { 0. } else { delta_movement.x }, - y: delta_movement.y, - z: if z_collision { 0. } else { delta_movement.z }, - } - } + // if self.isRemoved() { return; } - if vertical_collision { - // blockBelow.updateEntityAfterFallOn(this.level, this); - // the default implementation of updateEntityAfterFallOn sets the y movement to - // 0 - self.delta.y = 0.; + if horizontal_collision { + let delta_movement = &physics.delta; + physics.delta = Vec3 { + x: if x_collision { 0. } else { delta_movement.x }, + y: delta_movement.y, + z: if z_collision { 0. } else { delta_movement.z }, } + } - if on_ground { - // blockBelow.stepOn(this.level, blockPosBelow, blockStateBelow, - // this); - } + if vertical_collision { + // blockBelow.updateEntityAfterFallOn(this.level, this); + // the default implementation of updateEntityAfterFallOn sets the y movement to + // 0 + physics.delta.y = 0.; + } + + if on_ground { + // blockBelow.stepOn(this.level, blockPosBelow, blockStateBelow, + // this); + } - // sounds + // sounds - // this.tryCheckInsideBlocks(); + // this.tryCheckInsideBlocks(); - // float var25 = this.getBlockSpeedFactor(); - // this.setDeltaMovement(this.getDeltaMovement().multiply((double)var25, 1.0D, - // (double)var25)); if (this.level.getBlockStatesIfLoaded(this. - // getBoundingBox().deflate(1.0E-6D)).noneMatch((var0) -> { - // return var0.is(BlockTags.FIRE) || var0.is(Blocks.LAVA); - // })) { - // if (this.remainingFireTicks <= 0) { - // this.setRemainingFireTicks(-this.getFireImmuneTicks()); - // } + // float var25 = this.getBlockSpeedFactor(); + // this.setDeltaMovement(this.getDeltaMovement().multiply((double)var25, 1.0D, + // (double)var25)); if (this.level.getBlockStatesIfLoaded(this. + // getBoundingBox().deflate(1.0E-6D)).noneMatch((var0) -> { + // return var0.is(BlockTags.FIRE) || var0.is(Blocks.LAVA); + // })) { + // if (this.remainingFireTicks <= 0) { + // this.setRemainingFireTicks(-this.getFireImmuneTicks()); + // } - // if (this.wasOnFire && (this.isInPowderSnow || - // this.isInWaterRainOrBubble())) { this. - // playEntityOnFireExtinguishedSound(); } - // } + // if (this.wasOnFire && (this.isInPowderSnow || + // this.isInWaterRainOrBubble())) { this. + // playEntityOnFireExtinguishedSound(); } + // } - // if (this.isOnFire() && (this.isInPowderSnow || this.isInWaterRainOrBubble())) - // { this.setRemainingFireTicks(-this.getFireImmuneTicks()); - // } + // if (this.isOnFire() && (this.isInPowderSnow || this.isInWaterRainOrBubble())) + // { this.setRemainingFireTicks(-this.getFireImmuneTicks()); + // } - Ok(()) - } + Ok(()) } fn collide_bounding_box( - entity: Option<&EntityData>, movement: &Vec3, entity_bounding_box: &AABB, - world: &WeakWorld, + world: &World, entity_collisions: Vec<VoxelShape>, ) -> Vec3 { let mut collision_boxes: Vec<VoxelShape> = Vec::with_capacity(entity_collisions.len() + 1); @@ -218,7 +198,7 @@ fn collide_bounding_box( // TODO: world border let block_collisions = - world.get_block_collisions(entity, entity_bounding_box.expand_towards(movement)); + get_block_collisions(world, entity_bounding_box.expand_towards(movement)); let block_collisions = block_collisions.collect::<Vec<_>>(); collision_boxes.extend(block_collisions); collide_with_shapes(movement, *entity_bounding_box, &collision_boxes) diff --git a/azalea-physics/src/collision/shape.rs b/azalea-physics/src/collision/shape.rs index bb2ed2e5..cc184591 100755 --- a/azalea-physics/src/collision/shape.rs +++ b/azalea-physics/src/collision/shape.rs @@ -539,7 +539,7 @@ impl VoxelShape { x_coords[var7 as usize], y_coords[var8 as usize], z_coords[var9 as usize], - ) + ); }, true, ); diff --git a/azalea-physics/src/collision/world_collisions.rs b/azalea-physics/src/collision/world_collisions.rs index 9e238bf9..c7b2a91b 100644 --- a/azalea-physics/src/collision/world_collisions.rs +++ b/azalea-physics/src/collision/world_collisions.rs @@ -1,34 +1,17 @@ +use super::Shapes; 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, WeakWorld}; +use azalea_world::{Chunk, World}; use parking_lot::RwLock; use std::sync::Arc; -use super::Shapes; - -pub trait CollisionGetter { - fn get_block_collisions<'a>( - &'a self, - entity: Option<&EntityData>, - aabb: AABB, - ) -> BlockCollisions<'a>; -} - -impl CollisionGetter for WeakWorld { - fn get_block_collisions<'a>( - &'a self, - entity: Option<&EntityData>, - aabb: AABB, - ) -> BlockCollisions<'a> { - BlockCollisions::new(self, entity, aabb) - } +pub fn get_block_collisions(world: &World, aabb: AABB) -> BlockCollisions<'_> { + BlockCollisions::new(world, aabb) } pub struct BlockCollisions<'a> { - pub world: &'a WeakWorld, - // context: CollisionContext, + pub world: &'a World, pub aabb: AABB, pub entity_shape: VoxelShape, pub cursor: Cursor3d, @@ -36,8 +19,7 @@ pub struct BlockCollisions<'a> { } impl<'a> BlockCollisions<'a> { - // TODO: the entity is stored in the context - pub fn new(world: &'a WeakWorld, _entity: Option<&EntityData>, aabb: AABB) -> Self { + pub fn new(world: &'a World, 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; @@ -75,7 +57,7 @@ impl<'a> BlockCollisions<'a> { // return var7; // } - self.world.get_chunk(&chunk_pos) + self.world.chunks.get(&chunk_pos) } } @@ -89,15 +71,14 @@ impl<'a> Iterator for BlockCollisions<'a> { } let chunk = self.get_chunk(item.pos.x, item.pos.z); - let chunk = match chunk { - Some(chunk) => chunk, - None => continue, + let Some(chunk) = chunk else { + continue }; let pos = item.pos; let block_state: BlockState = chunk .read() - .get(&(&pos).into(), self.world.min_y()) + .get(&(&pos).into(), self.world.chunks.min_y) .unwrap_or(BlockState::Air); // TODO: continue if self.only_suffocating_blocks and the block is not |
