use std::sync::OnceLock;
use ghast_physics::Keys;
use crate::Version;
#[cfg(feature = "v1_12_2")]
use crate::physics::P12;
#[cfg(feature = "v1_16_5")]
use crate::physics::P16;
#[cfg(feature = "v1_8")]
use crate::physics::P18;
#[cfg(feature = "v26_1")]
use crate::physics::P26;
#[cfg(feature = "v26_2")]
use crate::physics::P262;
#[cfg(feature = "v26_3")]
use crate::physics::P263;
use crate::physics::{Physics, SourceView};
#[derive(Clone, Debug, PartialEq)]
pub struct Costs {
pub walk_speed: f64,
pub sprint_speed: f64,
pub jump_up: f64,
pub fall: Vec<f64>,
pub climb_up_speed: f64,
pub climb_down_speed: f64,
pub swim_speed: f64,
pub swim_up_speed: f64,
pub swim_down_speed: f64,
pub jump_air: f64,
}
impl Costs {
#[must_use]
pub fn of(version: Version) -> &'static Costs {
#[cfg(feature = "v1_8")]
static C18: OnceLock<Costs> = OnceLock::new();
#[cfg(feature = "v1_12_2")]
static C12: OnceLock<Costs> = OnceLock::new();
#[cfg(feature = "v1_16_5")]
static C16: OnceLock<Costs> = OnceLock::new();
#[cfg(feature = "v26_1")]
static C26: OnceLock<Costs> = OnceLock::new();
#[cfg(feature = "v26_2")]
static C262: OnceLock<Costs> = OnceLock::new();
#[cfg(feature = "v26_3")]
static C263: OnceLock<Costs> = OnceLock::new();
match version {
#[cfg(feature = "v1_8")]
Version::V1_8 => C18.get_or_init(measure::<P18>),
#[cfg(feature = "v1_12_2")]
Version::V1_12 => C12.get_or_init(measure::<P12>),
#[cfg(feature = "v1_16_5")]
Version::V1_16_5 => C16.get_or_init(measure::<P16>),
#[cfg(feature = "v26_1")]
Version::V26_1 => C26.get_or_init(measure::<P26>),
#[cfg(feature = "v26_2")]
Version::V26_2 => C262.get_or_init(measure::<P262>),
#[cfg(feature = "v26_3")]
Version::V26_3 => C263.get_or_init(measure::<P263>),
}
}
#[must_use]
pub fn fall_ticks(&self, blocks: f64) -> f64 {
if blocks <= 0.0 {
return 0.0;
}
let n = blocks.floor() as usize;
let last = self.fall.len() - 1;
if n >= last {
return self.fall[last] + (blocks - last as f64) / 3.5;
}
let a = self.fall[n];
let b = self.fall[n + 1];
a + (b - a) * (blocks - n as f64)
}
}
fn raws(version: Version) -> (u32, u32, u32) {
match version {
#[cfg(feature = "v1_8")]
Version::V1_8 => (1 << 4, 65 << 4 | 2, 9 << 4),
#[cfg(feature = "v1_12_2")]
Version::V1_12 => (1 << 4, 65 << 4 | 2, 9 << 4),
#[cfg(feature = "v1_16_5")]
Version::V1_16_5 => {
use ghast_data::v1_16_5::{Block, Property};
let ladder = Block::LADDER
.state_with(&[(Property::Facing, "north")])
.map_or(0, |s| u32::from(s.id()));
(
u32::from(Block::STONE.default_state().id()),
ladder,
u32::from(Block::WATER.default_state().id()),
)
}
#[cfg(feature = "v26_1")]
Version::V26_1 => {
use ghast_data::v26_1::BlockState;
let id = |s: &str| BlockState::parse(s).map_or(0, |s| u32::from(s.id()));
(id("stone"), id("ladder[facing=north]"), id("water"))
}
#[cfg(feature = "v26_2")]
Version::V26_2 => {
use ghast_data::v26_2::BlockState;
let id = |s: &str| BlockState::parse(s).map_or(0, |s| u32::from(s.id()));
(id("stone"), id("ladder[facing=north]"), id("water"))
}
#[cfg(feature = "v26_3")]
Version::V26_3 => {
use ghast_data::v26_3::BlockState;
let id = |s: &str| BlockState::parse(s).map_or(0, |s| u32::from(s.id()));
(id("stone"), id("ladder[facing=north]"), id("water"))
}
}
}
struct Lab {
blocks: std::collections::HashMap<[i32; 3], u32>,
}
impl crate::BlockSource for Lab {
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
if !(0..256).contains(&y) {
return None;
}
Some(self.blocks.get(&[x, y, z]).copied().unwrap_or(0))
}
fn is_column_loaded(&self, _: i32, _: i32) -> bool {
true
}
}
impl Lab {
fn floor(stone: u32) -> Lab {
let mut blocks = std::collections::HashMap::new();
for x in -4..=4 {
for z in -4..=40 {
blocks.insert([x, 63, z], stone);
}
}
Lab { blocks }
}
}
fn run<P: Physics>(
lab: &Lab,
s: &mut P::State,
keys: Keys,
yaw: f32,
mut until: impl FnMut(&P::State) -> bool,
max: usize,
) -> usize {
let view = SourceView::<P, Lab>::new(lab);
for t in 0..max {
P::tick(s, keys, yaw, 0.0, &view);
if until(s) {
return t + 1;
}
}
max
}
fn settle<P: Physics>(lab: &Lab, pos: [f64; 3]) -> P::State {
let mut s = P::new_state(pos, 0.0, 0.0);
run::<P>(lab, &mut s, Keys::NONE, 0.0, |_| false, 3);
s
}
fn measure<P: Physics>() -> Costs {
let (stone, ladder, water) = raws(P::VERSION);
let fwd = Keys {
forward: true,
..Keys::NONE
};
let lab = Lab::floor(stone);
let speed = |keys: Keys| {
let mut s = settle::<P>(&lab, [0.5, 64.0, 0.5]);
run::<P>(&lab, &mut s, keys, 0.0, |_| false, 30);
let z0 = P::observe(&s).pos[2];
run::<P>(&lab, &mut s, keys, 0.0, |_| false, 5);
(P::observe(&s).pos[2] - z0) / 5.0
};
let walk_speed = speed(fwd);
let sprint_speed = speed(Keys {
sprint: true,
..fwd
});
let mut step = Lab::floor(stone);
step.blocks.insert([0, 64, 1], stone);
let mut s = settle::<P>(&step, [0.5, 64.0, 0.5]);
let jump_up = run::<P>(
&step,
&mut s,
Keys { jump: true, ..fwd },
0.0,
|s| {
let o = P::observe(s);
o.on_ground && o.pos[1] >= 65.0 - 1e-9 && o.pos[2] >= 1.3
},
60,
) as f64;
let mut s = settle::<P>(&lab, [0.5, 64.0, 0.5]);
let mut left = false;
let jump_air = run::<P>(
&lab,
&mut s,
Keys {
jump: true,
..Keys::NONE
},
0.0,
|s| {
let g = P::observe(s).on_ground;
if !g {
left = true;
}
left && g
},
60,
) as f64;
let mut fall = vec![0.0];
for n in 1..=24 {
let mut s = P::new_state([0.5, 64.0 + f64::from(n), 0.5], 0.0, 0.0);
let t = run::<P>(
&lab,
&mut s,
Keys::NONE,
0.0,
|s| P::observe(s).on_ground,
200,
);
fall.push(t as f64);
}
let mut lad = Lab::floor(stone);
for y in 64..80 {
lad.blocks.insert([0, y, 0], ladder);
lad.blocks.insert([0, y, 1], stone);
}
let mut s = settle::<P>(&lad, [0.5, 64.0, 0.5]);
run::<P>(&lad, &mut s, fwd, 0.0, |_| false, 10);
let y0 = P::observe(&s).pos[1];
run::<P>(&lad, &mut s, fwd, 0.0, |_| false, 20);
let climb_up_speed = (P::observe(&s).pos[1] - y0) / 20.0;
let mut s = P::new_state([0.5, 76.0, 0.5], 0.0, 0.0);
run::<P>(&lad, &mut s, Keys::NONE, 0.0, |_| false, 10);
let y0 = P::observe(&s).pos[1];
run::<P>(&lad, &mut s, Keys::NONE, 0.0, |_| false, 10);
let climb_down_speed = (y0 - P::observe(&s).pos[1]) / 10.0;
let mut pool = Lab::floor(stone);
for x in -4..=4 {
for z in -4..=40 {
pool.blocks.insert([x, 59, z], stone);
for y in 60..=63 {
pool.blocks.insert([x, y, z], water);
}
}
}
let swim_keys = Keys { jump: true, ..fwd };
let mut s = P::new_state([0.5, 62.6, 0.5], 0.0, 0.0);
run::<P>(&pool, &mut s, swim_keys, 0.0, |_| false, 40);
let z0 = P::observe(&s).pos[2];
run::<P>(&pool, &mut s, swim_keys, 0.0, |_| false, 20);
let swim_speed = (P::observe(&s).pos[2] - z0) / 20.0;
let mut s = P::new_state([0.5, 60.0, 0.5], 0.0, 0.0);
let up = Keys {
jump: true,
..Keys::NONE
};
run::<P>(&pool, &mut s, up, 0.0, |_| false, 5);
let y0 = P::observe(&s).pos[1];
run::<P>(&pool, &mut s, up, 0.0, |_| false, 10);
let swim_up_speed = (P::observe(&s).pos[1] - y0) / 10.0;
let mut s = P::new_state([0.5, 63.0, 0.5], 0.0, 0.0);
run::<P>(&pool, &mut s, Keys::NONE, 0.0, |_| false, 10);
let y0 = P::observe(&s).pos[1];
run::<P>(&pool, &mut s, Keys::NONE, 0.0, |_| false, 10);
let swim_down_speed = ((y0 - P::observe(&s).pos[1]) / 10.0).max(0.02);
Costs {
walk_speed,
sprint_speed,
jump_up,
fall,
climb_up_speed: climb_up_speed.max(0.05),
climb_down_speed: climb_down_speed.max(0.05),
swim_speed: swim_speed.max(0.05),
swim_up_speed: swim_up_speed.max(0.02),
swim_down_speed,
jump_air,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn measured_values_are_vanilla_like() {
for v in crate::tests_util::versions() {
let c = Costs::of(v);
eprintln!("{v:?}: {c:?}");
assert!(
(c.walk_speed - 0.2158).abs() < 0.002,
"{v:?} walk {}",
c.walk_speed
);
assert!(
(c.sprint_speed - 0.2806).abs() < 0.003,
"{v:?} sprint {}",
c.sprint_speed
);
assert!(
c.jump_up > 4.0 && c.jump_up < 20.0,
"{v:?} jump_up {}",
c.jump_up
);
assert_eq!(c.jump_air, 12.0);
assert!(c.fall[3] > 5.0 && c.fall[3] < 12.0);
assert!(
c.climb_up_speed > 0.08 && c.climb_up_speed < 0.25,
"{}",
c.climb_up_speed
);
assert!(c.climb_down_speed > 0.1 && c.climb_down_speed <= 0.16);
assert!(
c.swim_speed > 0.05 && c.swim_speed < 0.25,
"{}",
c.swim_speed
);
}
}
#[cfg(all(feature = "v26_2", feature = "v26_3"))]
#[test]
fn costs_26_3_match_26_2_on_dry_land() {
let (a, b) = (Costs::of(Version::V26_2), Costs::of(Version::V26_3));
eprintln!("26.2: {a:?}\n26.3: {b:?}");
assert_eq!(a.walk_speed, b.walk_speed);
assert_eq!(a.sprint_speed, b.sprint_speed);
assert_eq!(a.jump_up, b.jump_up);
assert_eq!(a.jump_air, b.jump_air);
assert_eq!(a.fall, b.fall);
assert_eq!(a.climb_up_speed, b.climb_up_speed);
assert_eq!(a.climb_down_speed, b.climb_down_speed);
}
}