azalea/pathfinder/
costs.rs

1use std::sync::LazyLock;
2
3use num_traits::Float;
4
5// based on https://github.com/cabaletta/baritone/blob/1.20.1/src/api/java/baritone/api/pathing/movement/ActionCosts.java
6pub const WALK_ONE_BLOCK_COST: f32 = 20. / 4.317; // 4.633
7pub const SPRINT_ONE_BLOCK_COST: f32 = 20. / 5.612; // 3.564
8pub const WALK_OFF_BLOCK_COST: f32 = WALK_ONE_BLOCK_COST * 0.8;
9pub const SPRINT_MULTIPLIER: f32 = SPRINT_ONE_BLOCK_COST / WALK_ONE_BLOCK_COST;
10pub const JUMP_PENALTY: f32 = 2.;
11pub const CENTER_AFTER_FALL_COST: f32 = WALK_ONE_BLOCK_COST - WALK_OFF_BLOCK_COST; // 0.927
12
13// explanation here:
14// https://github.com/cabaletta/baritone/blob/f147519a5c291015d4f18c94558a3f1bdcdb9588/src/api/java/baritone/api/Settings.java#L405
15// it's basically just the heuristic multiplier
16pub const COST_HEURISTIC: f32 = 3.563;
17
18// this one is also from baritone, it's helpful as a tiebreaker to avoid
19// breaking blocks if it can be avoided
20pub const BLOCK_BREAK_ADDITIONAL_PENALTY: f32 = 2.;
21
22pub static FALL_1_25_BLOCKS_COST: LazyLock<f32> = LazyLock::new(|| distance_to_ticks(1.25));
23pub static FALL_0_25_BLOCKS_COST: LazyLock<f32> = LazyLock::new(|| distance_to_ticks(0.25));
24pub static JUMP_ONE_BLOCK_COST: LazyLock<f32> =
25    LazyLock::new(|| *FALL_1_25_BLOCKS_COST - *FALL_0_25_BLOCKS_COST); // 3.163
26
27pub static FALL_N_BLOCKS_COST: LazyLock<[f32; 4097]> = LazyLock::new(|| {
28    let mut fall_n_blocks_cost = [0.; 4097];
29
30    let mut distance = 0;
31
32    // this is the same as calling distance_to_ticks a bunch of times but more
33    // efficient
34    let mut temp_distance = distance as f32;
35    let mut tick_count = 0;
36    loop {
37        let fall_distance = velocity(tick_count);
38        if temp_distance <= fall_distance {
39            fall_n_blocks_cost[distance] = tick_count as f32 + temp_distance / fall_distance;
40            distance += 1;
41            if distance >= fall_n_blocks_cost.len() {
42                break;
43            }
44        }
45        temp_distance -= fall_distance;
46        tick_count += 1;
47    }
48
49    fall_n_blocks_cost
50});
51
52fn velocity(ticks: usize) -> f32 {
53    (0.98.powi(ticks.try_into().unwrap()) - 1.) * -3.92
54}
55
56fn distance_to_ticks(distance: f32) -> f32 {
57    if distance == 0. {
58        // Avoid 0/0 NaN
59        return 0.;
60    }
61    let mut temp_distance = distance;
62    let mut tick_count = 0;
63    loop {
64        let fall_distance = velocity(tick_count);
65        if temp_distance <= fall_distance {
66            return tick_count as f32 + temp_distance / fall_distance;
67        }
68        temp_distance -= fall_distance;
69        tick_count += 1;
70    }
71}