diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2023-12-15 11:26:40 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-15 11:26:40 -0600 |
| commit | a707e2eb82b74994a16083b31fa4576332cf1995 (patch) | |
| tree | db6c2ac94dd73590befd68a9b1b0ef960410b0df /azalea/benches | |
| parent | 59e140ddd655c7dc6e35109b91286118c51bcc06 (diff) | |
| download | azalea-drasl-a707e2eb82b74994a16083b31fa4576332cf1995.tar.xz | |
Add mining to the pathfinder (#122)
* basic pathfinder mining poc
* mining descending and autotool
* pathfinder mining descending
* pathfinder fixes
* allow disabling pathfinder miner and other fixes
* small optimization to avoid chunk vec iter lookup sometimes
* seeded rng in pathfinder bench
* consistently use f32::INFINITY
this brings performance much closer to how it was before
* astar heuristic optimization from baritone
* add downward_move
* fix downward move execute
* avoid liquids and falling blocks when mining
* fix COST_HEURISTIC
* fix to not path through flowing liquids
* only reset pathfinder timeout while mining if the block is close enough
* cache mining costs of block positions
* fix mine_while_at_start and move PathfinderDebugParticles to its own module
* add ReachBlockPosGoal
in other news: azalea's sin/cos functions were broken this whole time and i never noticed
* clippy and add things that i accidentally didn't commit
* improve wording on doc for azalea::pathfinder
Diffstat (limited to 'azalea/benches')
| -rw-r--r-- | azalea/benches/pathfinder.rs | 120 |
1 files changed, 86 insertions, 34 deletions
diff --git a/azalea/benches/pathfinder.rs b/azalea/benches/pathfinder.rs index 3b58ae51..842c6b5e 100644 --- a/azalea/benches/pathfinder.rs +++ b/azalea/benches/pathfinder.rs @@ -3,17 +3,16 @@ use std::{hint::black_box, sync::Arc, time::Duration}; use azalea::{ pathfinder::{ astar::{self, a_star}, - goals::BlockPosGoal, + goals::{BlockPosGoal, Goal}, mining::MiningCache, world::CachedWorld, - Goal, }, BlockPos, }; use azalea_core::position::{ChunkBlockPos, ChunkPos}; use azalea_inventory::Menu; use azalea_world::{Chunk, ChunkStorage, PartialChunkStorage}; -use criterion::{criterion_group, criterion_main, Criterion}; +use criterion::{criterion_group, criterion_main, Bencher, Criterion}; use parking_lot::RwLock; use rand::{rngs::StdRng, Rng, SeedableRng}; @@ -58,14 +57,14 @@ fn generate_bedrock_world( } let mut start = BlockPos::new(-64, 4, -64); - // move start down until it's on bedrock + // 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 bedrock + // move end down until it's on a solid block while chunks.get_block_state(&end).unwrap().is_air() { end = end.down(1); } @@ -74,37 +73,90 @@ fn generate_bedrock_world( (chunks, start, end) } +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_pathfinder_benchmark( + b: &mut Bencher<'_>, + generate_world: fn(&mut PartialChunkStorage, u32) -> (ChunkStorage, BlockPos, BlockPos), +) { + let mut partial_chunks = PartialChunkStorage::new(32); + let successors_fn = azalea::pathfinder::moves::default_move; + + let (world, start, end) = generate_world(&mut partial_chunks, 4); + + b.iter(|| { + let cached_world = CachedWorld::new(Arc::new(RwLock::new(world.clone().into()))); + let mining_cache = + MiningCache::new(Some(Menu::Player(azalea_inventory::Player::default()))); + let goal = BlockPosGoal(end); + + let successors = |pos: BlockPos| { + azalea::pathfinder::call_successors_fn(&cached_world, &mining_cache, successors_fn, pos) + }; + + let astar::Path { movements, partial } = a_star( + start, + |n| goal.heuristic(n), + successors, + |n| goal.success(n), + Duration::MAX, + ); + + assert!(!partial); + + black_box((movements, partial)); + }) +} + fn bench_pathfinder(c: &mut Criterion) { - c.bench_function("bedrock", |b| { - let mut partial_chunks = PartialChunkStorage::new(32); - let successors_fn = azalea::pathfinder::moves::default_move; - - b.iter(|| { - let (world, start, end) = generate_bedrock_world(&mut partial_chunks, 4); - let cached_world = CachedWorld::new(Arc::new(RwLock::new(world.into()))); - let mining_cache = MiningCache::new(Menu::Player(azalea_inventory::Player::default())); - let goal = BlockPosGoal(end); - - let successors = |pos: BlockPos| { - azalea::pathfinder::call_successors_fn( - &cached_world, - &mining_cache, - successors_fn, - pos, - ) - }; - - let astar::Path { movements, partial } = a_star( - start, - |n| goal.heuristic(n), - successors, - |n| goal.success(n), - Duration::MAX, - ); - - black_box((movements, partial)); - }) + // c.bench_function("bedrock", |b| { + // run_pathfinder_benchmark(b, generate_bedrock_world); + // }); + let mut slow_group = c.benchmark_group("slow"); + slow_group.sample_size(10); + slow_group.bench_function("mining", |b| { + run_pathfinder_benchmark(b, generate_mining_world); }); + slow_group.finish(); } criterion_group!(benches, bench_pathfinder); |
