Match player swimming and ability-flight travel wrapper
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
use horizon_core::{geometry::Vec3, position::BlockPos};
|
||||
use horizon_world::player_travel::*;
|
||||
use serde_json::Value;
|
||||
use std::{
|
||||
fs,
|
||||
io::{self, Write},
|
||||
path::Path,
|
||||
};
|
||||
fn number(v: &Value) -> f64 {
|
||||
f64::from_bits(u64::from_str_radix(v.as_str().unwrap(), 16).unwrap())
|
||||
}
|
||||
fn single(v: &Value) -> f32 {
|
||||
f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap())
|
||||
}
|
||||
fn vec(v: &Value) -> Vec3 {
|
||||
Vec3::new(number(&v[0]), number(&v[1]), number(&v[2]))
|
||||
}
|
||||
fn bits(v: f64) -> u64 {
|
||||
if v.is_nan() {
|
||||
0x7ff8000000000000
|
||||
} else {
|
||||
v.to_bits()
|
||||
}
|
||||
}
|
||||
fn fbits(v: f32) -> u32 {
|
||||
if v.is_nan() { 0x7fc00000 } else { v.to_bits() }
|
||||
}
|
||||
fn vector(v: Vec3) -> String {
|
||||
format!("{:016x} {:016x} {:016x}", bits(v.x), bits(v.y), bits(v.z))
|
||||
}
|
||||
struct World<'a> {
|
||||
f: &'a Value,
|
||||
out: &'a mut dyn Write,
|
||||
calls: i64,
|
||||
velocity: Vec3,
|
||||
position: Vec3,
|
||||
flying: bool,
|
||||
}
|
||||
impl World<'_> {
|
||||
fn flag(&self, k: &str) -> bool {
|
||||
self.f[k].as_bool().unwrap()
|
||||
}
|
||||
fn query(&mut self, s: impl AsRef<str>) -> io::Result<()> {
|
||||
writeln!(self.out, "{}", s.as_ref())?;
|
||||
let n = self.calls;
|
||||
self.calls += 1;
|
||||
if n == self.f["fail"].as_i64().unwrap() {
|
||||
return Err(io::Error::other("injected player travel failure"));
|
||||
}
|
||||
if n == self.f["mutate_at"].as_i64().unwrap() {
|
||||
self.velocity = vec(&self.f["after_velocity"]);
|
||||
self.position = vec(&self.f["after_position"]);
|
||||
self.flying = self.flag("after_flying");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl PlayerTravelWorld for World<'_> {
|
||||
type Error = io::Error;
|
||||
fn is_passenger(&mut self) -> io::Result<bool> {
|
||||
self.query("passenger")?;
|
||||
Ok(self.flag("passenger"))
|
||||
}
|
||||
fn is_swimming(&mut self) -> io::Result<bool> {
|
||||
self.query("swimming")?;
|
||||
Ok(self.flag("swimming"))
|
||||
}
|
||||
fn look_angle(&mut self) -> io::Result<Vec3> {
|
||||
self.query("look")?;
|
||||
Ok(vec(&self.f["look"]))
|
||||
}
|
||||
fn jumping(&self) -> bool {
|
||||
self.flag("jumping")
|
||||
}
|
||||
fn position(&self) -> Vec3 {
|
||||
self.position
|
||||
}
|
||||
fn fluid_is_empty(&mut self, p: BlockPos) -> io::Result<bool> {
|
||||
self.query(format!("fluid {} {} {}", p.x, p.y, p.z))?;
|
||||
Ok(self.flag("empty"))
|
||||
}
|
||||
fn velocity(&self) -> Vec3 {
|
||||
self.velocity
|
||||
}
|
||||
fn set_velocity(&mut self, v: Vec3) -> io::Result<()> {
|
||||
self.query(format!("set {}", vector(v)))?;
|
||||
self.velocity = v;
|
||||
Ok(())
|
||||
}
|
||||
fn query_flying_ability(&mut self) -> io::Result<bool> {
|
||||
self.query("abilities")?;
|
||||
Ok(self.flying)
|
||||
}
|
||||
fn flying_ability(&self) -> bool {
|
||||
self.flying
|
||||
}
|
||||
fn ability_flying_speed(&mut self) -> io::Result<f32> {
|
||||
self.query("ability-speed")?;
|
||||
Ok(single(&self.f["speed"]))
|
||||
}
|
||||
fn sprinting(&mut self) -> io::Result<bool> {
|
||||
self.query("sprint")?;
|
||||
Ok(self.flag("sprint"))
|
||||
}
|
||||
fn living_travel(&mut self, input: Vec3) -> io::Result<()> {
|
||||
// Native parent dispatch is exercised through a controlled fluid branch.
|
||||
self.query("block-position")?;
|
||||
self.query("fluid 17 -64 -33")?;
|
||||
self.query("should-fluid")?;
|
||||
self.query(format!("base {}", vector(input)))?;
|
||||
if self.flag("base_mutate") {
|
||||
self.velocity = vec(&self.f["base_velocity"]);
|
||||
self.flying = self.flag("base_flying");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let arg = std::env::args().nth(1).unwrap();
|
||||
let dir = Path::new(&arg);
|
||||
let mut out = io::BufWriter::new(fs::File::create(dir.join("rust.txt"))?);
|
||||
for line in fs::read_to_string(dir.join("inputs.txt"))?.lines() {
|
||||
let f: Value = serde_json::from_str(line)?;
|
||||
let mut w = World {
|
||||
f: &f,
|
||||
out: &mut out,
|
||||
calls: 0,
|
||||
velocity: vec(&f["velocity"]),
|
||||
position: vec(&f["position"]),
|
||||
flying: f["flying"].as_bool().unwrap(),
|
||||
};
|
||||
let result: io::Result<()> = match f["mode"].as_str().unwrap() {
|
||||
"travel" => travel(&mut w, vec(&f["input"])),
|
||||
"speed" => flying_speed(&mut w).and_then(|v| writeln!(w.out, "float {:08x}", fbits(v))),
|
||||
"affected" => {
|
||||
let b = affected_by_fluids(&w);
|
||||
writeln!(w.out, "bool {b}")
|
||||
}
|
||||
_ => panic!("mode"),
|
||||
};
|
||||
writeln!(
|
||||
w.out,
|
||||
"result {}",
|
||||
if result.is_ok() { "ok" } else { "error" }
|
||||
)?;
|
||||
writeln!(
|
||||
w.out,
|
||||
"final {} {} {}",
|
||||
vector(w.velocity),
|
||||
vector(w.position),
|
||||
w.flying
|
||||
)?;
|
||||
writeln!(w.out, "queries {}", w.calls)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -40,6 +40,7 @@ pub mod movement_support;
|
||||
pub mod nbt;
|
||||
pub mod piston_movement;
|
||||
pub mod piston_shape;
|
||||
pub mod player_travel;
|
||||
pub mod powder_snow_collision;
|
||||
pub mod region;
|
||||
pub mod registry;
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
//! Player-specific swimming and ability-flight behavior around living travel.
|
||||
use horizon_core::{geometry::Vec3, position::BlockPos};
|
||||
pub trait PlayerTravelWorld {
|
||||
type Error;
|
||||
fn is_passenger(&mut self) -> Result<bool, Self::Error>;
|
||||
fn is_swimming(&mut self) -> Result<bool, Self::Error>;
|
||||
fn look_angle(&mut self) -> Result<Vec3, Self::Error>;
|
||||
fn jumping(&self) -> bool;
|
||||
fn position(&self) -> Vec3;
|
||||
fn fluid_is_empty(&mut self, position: BlockPos) -> Result<bool, Self::Error>;
|
||||
fn velocity(&self) -> Vec3;
|
||||
fn set_velocity(&mut self, velocity: Vec3) -> Result<(), Self::Error>;
|
||||
/// Virtual getAbilities(), as distinct from direct abilities-field reads.
|
||||
fn query_flying_ability(&mut self) -> Result<bool, Self::Error>;
|
||||
fn flying_ability(&self) -> bool;
|
||||
fn ability_flying_speed(&mut self) -> Result<f32, Self::Error>;
|
||||
fn sprinting(&mut self) -> Result<bool, Self::Error>;
|
||||
fn living_travel(&mut self, input: Vec3) -> Result<(), Self::Error>;
|
||||
}
|
||||
pub fn travel<W: PlayerTravelWorld>(world: &mut W, input: Vec3) -> Result<(), W::Error> {
|
||||
if world.is_passenger()? {
|
||||
return world.living_travel(input);
|
||||
}
|
||||
if world.is_swimming()? {
|
||||
let look_y = world.look_angle()?.y;
|
||||
let scale = if look_y < -0.2 { 0.085 } else { 0.06 };
|
||||
let apply = if look_y <= 0.0 || world.jumping() {
|
||||
true
|
||||
} else {
|
||||
let p = world.position();
|
||||
!world.fluid_is_empty(BlockPos::new(
|
||||
p.x.floor() as i32,
|
||||
(p.y + 1.0 - 0.1).floor() as i32,
|
||||
p.z.floor() as i32,
|
||||
))?
|
||||
};
|
||||
if apply {
|
||||
let v = world.velocity();
|
||||
world.set_velocity(Vec3::new(
|
||||
v.x + 0.0,
|
||||
v.y + (look_y - v.y) * scale,
|
||||
v.z + 0.0,
|
||||
))?;
|
||||
}
|
||||
}
|
||||
if world.query_flying_ability()? {
|
||||
let y = world.velocity().y;
|
||||
world.living_travel(input)?;
|
||||
let v = world.velocity();
|
||||
world.set_velocity(Vec3::new(v.x, y * 0.6, v.z))?;
|
||||
} else {
|
||||
world.living_travel(input)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn flying_speed<W: PlayerTravelWorld>(world: &mut W) -> Result<f32, W::Error> {
|
||||
if world.flying_ability() && !world.is_passenger()? {
|
||||
let sprinting = world.sprinting()?;
|
||||
let speed = world.ability_flying_speed()?;
|
||||
Ok(if sprinting { speed * 2.0 } else { speed })
|
||||
} else {
|
||||
Ok(if world.sprinting()? {
|
||||
0.025999999f32
|
||||
} else {
|
||||
0.02
|
||||
})
|
||||
}
|
||||
}
|
||||
pub fn affected_by_fluids<W: PlayerTravelWorld>(world: &W) -> bool {
|
||||
!world.flying_ability()
|
||||
}
|
||||
Reference in New Issue
Block a user