diff options
| author | mat <git@matdoes.dev> | 2024-02-25 16:53:08 -0600 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2024-02-25 16:53:08 -0600 |
| commit | 018ab55bdb02e7774044198c8a30e0d02a7c6e29 (patch) | |
| tree | ca9eb536985833eb158383153a322adfacf86267 /azalea | |
| parent | 13426b035e43c4435854f175155439ab28a18544 (diff) | |
| download | azalea-drasl-018ab55bdb02e7774044198c8a30e0d02a7c6e29.tar.xz | |
optimize physics
Diffstat (limited to 'azalea')
| -rw-r--r-- | azalea/Cargo.toml | 4 | ||||
| -rw-r--r-- | azalea/benches/physics.rs | 144 | ||||
| -rw-r--r-- | azalea/src/pathfinder/simulation.rs | 3 |
3 files changed, 149 insertions, 2 deletions
diff --git a/azalea/Cargo.toml b/azalea/Cargo.toml index 8a11fc46..d1d9a4f8 100644 --- a/azalea/Cargo.toml +++ b/azalea/Cargo.toml @@ -57,3 +57,7 @@ log = ["azalea-client/log"] [[bench]] name = "pathfinder" harness = false + +[[bench]] +name = "physics" +harness = false diff --git a/azalea/benches/physics.rs b/azalea/benches/physics.rs new file mode 100644 index 00000000..6f8dc7bc --- /dev/null +++ b/azalea/benches/physics.rs @@ -0,0 +1,144 @@ +use std::{hint::black_box, sync::Arc, time::Duration}; + +use azalea::{ + pathfinder::{ + astar::{self, a_star}, + goals::{BlockPosGoal, Goal}, + mining::MiningCache, + simulation::{SimulatedPlayerBundle, Simulation, SimulationSet}, + world::CachedWorld, + }, + BlockPos, Vec3, +}; +use azalea_core::position::{ChunkBlockPos, ChunkPos}; +use azalea_inventory::Menu; +use azalea_world::{Chunk, ChunkStorage, PartialChunkStorage}; +use criterion::{criterion_group, criterion_main, Bencher, Criterion}; +use parking_lot::RwLock; + +#[allow(dead_code)] +fn generate_world(partial_chunks: &mut PartialChunkStorage, size: u32) -> ChunkStorage { + let size = size as i32; + + let mut chunks = ChunkStorage::default(); + for chunk_x in -size..size { + for chunk_z in -size..size { + let chunk_pos = ChunkPos::new(chunk_x, chunk_z); + partial_chunks.set(&chunk_pos, Some(Chunk::default()), &mut chunks); + } + } + + // for chunk_x in -size..size { + // for chunk_z in -size..size { + // let chunk_pos = ChunkPos::new(chunk_x, chunk_z); + // let chunk = chunks.get(&chunk_pos).unwrap(); + // let mut chunk = chunk.write(); + // for x in 0..16_u8 { + // for z in 0..16_u8 { + // chunk.set( + // &ChunkBlockPos::new(x, 1, z), + // azalea_registry::Block::Bedrock.into(), + // chunks.min_y, + // ); + // if rng.gen_bool(0.5) { + // chunk.set( + // &ChunkBlockPos::new(x, 2, z), + // azalea_registry::Block::Bedrock.into(), + // chunks.min_y, + // ); + // } + // } + // } + // } + // } + + // let mut start = BlockPos::new(-64, 4, -64); + // // move start down until it's on a solid block + // while chunks.get_block_state(&start).unwrap().is_air() { + // start = start.down(1); + // } + // start = start.up(1); + + // let mut end = BlockPos::new(63, 4, 63); + // // move end down until it's on a solid block + // while chunks.get_block_state(&end).unwrap().is_air() { + // end = end.down(1); + // } + // end = end.up(1); + + chunks +} + +fn generate_mining_world( + partial_chunks: &mut PartialChunkStorage, + size: u32, +) -> (ChunkStorage, BlockPos, BlockPos) { + let size = size as i32; + + let mut chunks = ChunkStorage::default(); + for chunk_x in -size..size { + for chunk_z in -size..size { + let chunk_pos = ChunkPos::new(chunk_x, chunk_z); + partial_chunks.set(&chunk_pos, Some(Chunk::default()), &mut chunks); + } + } + + // let mut rng = StdRng::seed_from_u64(0); + + for chunk_x in -size..size { + for chunk_z in -size..size { + let chunk_pos = ChunkPos::new(chunk_x, chunk_z); + let chunk = chunks.get(&chunk_pos).unwrap(); + let mut chunk = chunk.write(); + for y in chunks.min_y..(chunks.min_y + chunks.height as i32) { + for x in 0..16_u8 { + for z in 0..16_u8 { + chunk.set( + &ChunkBlockPos::new(x, y, z), + azalea_registry::Block::Stone.into(), + chunks.min_y, + ); + } + } + } + } + } + + let start = BlockPos::new(-64, 4, -64); + let end = BlockPos::new(0, 4, 0); + + (chunks, start, end) +} + +fn run_physics_benchmark(b: &mut Bencher<'_>) { + let mut partial_chunks = PartialChunkStorage::new(32); + + let world = generate_world(&mut partial_chunks, 4); + + let mut simulation_set = SimulationSet::new(world); + + // let entity = simulation_set.spawn(SimulatedPlayerBundle::new(Vec3::new(0.0, + // 4.0, 0.0))); for _ in 0..20 { + // simulation_set.tick(); + // println!("tick over"); + // } + // simulation_set.despawn(entity); + // std::process::exit(0); + + b.iter(|| { + let entity = simulation_set.spawn(SimulatedPlayerBundle::new(Vec3::new(0.0, 4.0, 0.0))); + for _ in 0..20 { + simulation_set.tick(); + } + simulation_set.despawn(entity); + }) +} + +fn bench_pathfinder(c: &mut Criterion) { + c.bench_function("physics", |b| { + run_physics_benchmark(b); + }); +} + +criterion_group!(benches, bench_pathfinder); +criterion_main!(benches); diff --git a/azalea/src/pathfinder/simulation.rs b/azalea/src/pathfinder/simulation.rs index bea99e93..a5e8113f 100644 --- a/azalea/src/pathfinder/simulation.rs +++ b/azalea/src/pathfinder/simulation.rs @@ -113,8 +113,7 @@ fn create_simulation_player( )); entity.insert(player); - let entity_id = entity.id(); - entity_id + entity.id() } /// Simulate the Minecraft world to see if certain movements would be possible. |
