Apply pending player attribute callbacks during resident simulation
Refresh scale dimensions and clamp health and absorption after movement, preserving native callback order and queued synchronization. Replay per-reference JVM identities in the scene comparison and cover changing limits, modifiers and low tunnels. Waypoint callbacks remain explicit requests for the future manager runtime.
This commit is contained in:
@@ -165,6 +165,10 @@ fn snapshot(out: &mut impl Write, player: &PlayerMotion) -> Result<()> {
|
||||
.filter_map(|e| match e {
|
||||
MotionEvent::Fall(e) => Some(e.trace()),
|
||||
MotionEvent::Health(e) => Some(e.trace()),
|
||||
MotionEvent::Waypoint { receiver, enabled } => Some(format!(
|
||||
"waypoint {} {enabled}",
|
||||
if *receiver { "receive" } else { "transmit" }
|
||||
)),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
@@ -173,8 +177,10 @@ fn snapshot(out: &mut impl Write, player: &PlayerMotion) -> Result<()> {
|
||||
}
|
||||
fn main() -> Result<()> {
|
||||
let args: Vec<_> = std::env::args().skip(1).collect();
|
||||
if args.len() != 2 {
|
||||
return Err("usage: scene_motion_probe RESOURCES FIXTURE_DIRECTORY".into());
|
||||
if args.len() != 3 {
|
||||
return Err(
|
||||
"usage: scene_motion_probe RESOURCES FIXTURE_DIRECTORY --describe|REFERENCE".into(),
|
||||
);
|
||||
}
|
||||
let root = PathBuf::from(&args[0]);
|
||||
let dir = PathBuf::from(&args[1]);
|
||||
@@ -188,11 +194,44 @@ fn main() -> Result<()> {
|
||||
&serde_json::json!({"radius":1,"resident_chunks":scene.chunks.len(),"model_gallery":assets.model_gallery}),
|
||||
)?,
|
||||
)?;
|
||||
let mut out = std::io::BufWriter::new(fs::File::create(dir.join("rust.txt"))?);
|
||||
for line in std::io::BufReader::new(fs::File::open(dir.join("inputs.txt"))?).lines() {
|
||||
if args[2] == "--describe" {
|
||||
return Ok(());
|
||||
}
|
||||
let side = &args[2];
|
||||
let mut out = std::io::BufWriter::new(fs::File::create(dir.join(format!("rust-{side}.txt")))?);
|
||||
for line in
|
||||
std::io::BufReader::new(fs::File::open(dir.join(format!("inputs-{side}.txt")))?).lines()
|
||||
{
|
||||
let f: Value = serde_json::from_str(&line?)?;
|
||||
scene.health_rules = horizon_client::health::HealthRules::default();
|
||||
let mut player = PlayerMotion::new(&resources, vector(&f["position"]))?;
|
||||
let hashes: Vec<_> = resources
|
||||
.attributes
|
||||
.attributes()
|
||||
.map(|(_, a)| {
|
||||
f["holder_hashes"][&a.name]
|
||||
.as_i64()
|
||||
.expect("missing holder identity") as i32
|
||||
})
|
||||
.collect();
|
||||
let mut instances = f["instance_hashes"]
|
||||
.as_array()
|
||||
.ok_or("missing instance identities")?
|
||||
.iter()
|
||||
.map(|v| v.as_i64().unwrap() as i32)
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter();
|
||||
let count = instances.len();
|
||||
let used = std::rc::Rc::new(std::cell::Cell::new(0));
|
||||
let counter = used.clone();
|
||||
let mut player = PlayerMotion::with_attribute_identities(
|
||||
&resources,
|
||||
vector(&f["position"]),
|
||||
&hashes,
|
||||
Box::new(move || {
|
||||
counter.set(counter.get() + 1);
|
||||
instances.next().expect("unexpected instance allocation")
|
||||
}),
|
||||
)?;
|
||||
for op in f["ops"].as_array().unwrap() {
|
||||
let mut world = MotionWorld {
|
||||
player: &mut player,
|
||||
@@ -205,6 +244,7 @@ fn main() -> Result<()> {
|
||||
"shift" => world.player.shift_key_down = op["value"].as_bool().unwrap(),
|
||||
"pose_tick" => world.update_player_pose()?,
|
||||
"refresh_dimensions" => world.player.refresh_dimensions()?,
|
||||
"refresh_attributes" => world.player.refresh_attributes()?,
|
||||
"pose" => world.player.set_pose(match op["value"].as_str().unwrap() {
|
||||
"STANDING" => horizon_client::pose::Pose::Standing,
|
||||
"CROUCHING" => horizon_client::pose::Pose::Crouching,
|
||||
@@ -504,6 +544,9 @@ fn main() -> Result<()> {
|
||||
)?;
|
||||
}
|
||||
writeln!(out, "end")?;
|
||||
if used.get() != count {
|
||||
return Err("reference instance allocation count differs".into());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
//! Resident ServerPlayer pending attribute callbacks over its owned live map.
|
||||
//! Waypoint requests are observable boundaries; manager/network delivery is
|
||||
//! still supplied by the eventual multiplayer runtime.
|
||||
use crate::{
|
||||
Result,
|
||||
health::Updates,
|
||||
motion::{MotionEvent, PlayerMotion},
|
||||
};
|
||||
use horizon_world::{
|
||||
attribute_map::ValueKind,
|
||||
attribute_updates::{self, UpdateWorld, UpdatedAttribute},
|
||||
attributes::live::{self, Handle, Key, LiveAttributes},
|
||||
live_attribute_updates::{self as live_updates, LiveAttributeWorld},
|
||||
living_health::{self, HealthWorld},
|
||||
object_open_set::CursorError,
|
||||
};
|
||||
use std::io;
|
||||
|
||||
impl PlayerMotion {
|
||||
/// LivingEntity.tick refreshes these callbacks after aiStep and before the
|
||||
/// enclosing Player.tick selects its pose. Synchronization stays queued.
|
||||
pub fn refresh_attributes(&mut self) -> Result<()> {
|
||||
live_updates::refresh(self)
|
||||
}
|
||||
}
|
||||
impl HealthWorld for PlayerMotion {
|
||||
type Error = Box<dyn std::error::Error>;
|
||||
type Data = ();
|
||||
fn health(&mut self) -> Result<f32> {
|
||||
Ok(self.health.health())
|
||||
}
|
||||
fn health_data(&self) {}
|
||||
fn max_health_attribute(&mut self) -> Result<f64> {
|
||||
self.attribute("minecraft:max_health")
|
||||
}
|
||||
fn max_absorption_attribute(&mut self) -> Result<f64> {
|
||||
self.attribute("minecraft:max_absorption")
|
||||
}
|
||||
fn store_health(&mut self, _: (), value: f32) -> Result<()> {
|
||||
self.health.data.set(
|
||||
self.health.ids.health,
|
||||
value,
|
||||
false,
|
||||
&mut Updates(&mut self.events),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
fn store_absorption(&mut self, value: f32) -> Result<()> {
|
||||
// Player.internalSetAbsorptionAmount stores the already clamped value
|
||||
// through its current synchronized data, without another limit query.
|
||||
self.health.data.set(
|
||||
self.health.ids.absorption,
|
||||
value,
|
||||
false,
|
||||
&mut Updates(&mut self.events),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
fn dead_flag(&self) -> bool {
|
||||
self.health.dead
|
||||
}
|
||||
fn dispatch_set_health(&mut self, value: f32) -> Result<()> {
|
||||
living_health::set_health(self, value)
|
||||
}
|
||||
}
|
||||
impl UpdateWorld<Key> for PlayerMotion {
|
||||
type Level = ();
|
||||
type Manager = ();
|
||||
type Attributes = ();
|
||||
fn attribute_kind(&self, attribute: &Key) -> UpdatedAttribute {
|
||||
live_updates::kind(attribute)
|
||||
}
|
||||
fn absorption(&mut self) -> Result<f32> {
|
||||
Ok(self.health.absorption())
|
||||
}
|
||||
fn refresh_dimensions(&mut self) -> Result<()> {
|
||||
PlayerMotion::refresh_dimensions(self)
|
||||
}
|
||||
fn level(&mut self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
fn is_server_level(&self, _: ()) -> bool {
|
||||
true
|
||||
}
|
||||
fn waypoint_manager(&mut self, _: ()) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
fn attributes_field(&self) {}
|
||||
fn attribute_value_in(&mut self, _: (), attribute: Key) -> Result<f64> {
|
||||
self.attributes
|
||||
.value(Some(attribute), ValueKind::Computed)
|
||||
.map_err(Self::attribute_error)
|
||||
}
|
||||
fn track_waypoint(&mut self, _: (), track: bool) -> Result<()> {
|
||||
self.events.push(MotionEvent::Waypoint {
|
||||
receiver: false,
|
||||
enabled: track,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl LiveAttributeWorld for PlayerMotion {
|
||||
type Identity = Box<dyn FnMut() -> i32>;
|
||||
fn get_attributes(&mut self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
fn live_attributes(&self, _: ()) -> &LiveAttributes<Self::Identity> {
|
||||
&self.attributes
|
||||
}
|
||||
fn live_attributes_mut(&mut self, _: ()) -> &mut LiveAttributes<Self::Identity> {
|
||||
&mut self.attributes
|
||||
}
|
||||
fn instance_attribute(&self, instance: Option<Handle>) -> Result<Key> {
|
||||
let instance =
|
||||
instance.ok_or_else(|| io::Error::other("null pending attribute instance"))?;
|
||||
self.attributes
|
||||
.attribute(&instance)
|
||||
.cloned()
|
||||
.map_err(Self::attribute_error)
|
||||
}
|
||||
fn attribute_error(error: live::Error) -> Self::Error {
|
||||
io::Error::other(format!("resident attribute: {error:?}")).into()
|
||||
}
|
||||
fn cursor_error(error: CursorError) -> Self::Error {
|
||||
io::Error::other(format!("resident attribute cursor: {error:?}")).into()
|
||||
}
|
||||
fn dispatch_attribute_updated(&mut self, attribute: Key) -> Result<()> {
|
||||
// ServerPlayer.onAttributeUpdated handles receive range, then calls
|
||||
// the inherited LivingEntity handler, including for this same holder.
|
||||
if attribute.name() == "minecraft:waypoint_receive_range" {
|
||||
let value = self
|
||||
.attributes
|
||||
.value(Some(attribute.clone()), ValueKind::Computed)
|
||||
.map_err(Self::attribute_error)?;
|
||||
self.events.push(MotionEvent::Waypoint {
|
||||
receiver: true,
|
||||
enabled: value > 0.0,
|
||||
});
|
||||
}
|
||||
attribute_updates::updated(self, attribute)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Shared resident-scene loading and queries for the native development client.
|
||||
#![forbid(unsafe_code)]
|
||||
pub mod assets;
|
||||
pub mod attribute_updates;
|
||||
pub mod clock;
|
||||
pub mod collisions;
|
||||
pub mod health;
|
||||
|
||||
@@ -98,6 +98,10 @@ impl MotionResources {
|
||||
/// hurt dispatch updates stored health synchronously before these are drained.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum MotionEvent {
|
||||
Waypoint {
|
||||
receiver: bool,
|
||||
enabled: bool,
|
||||
},
|
||||
Health(crate::health::HealthEvent),
|
||||
Fall(crate::landing::FallEvent),
|
||||
Recorded {
|
||||
@@ -154,8 +158,8 @@ fn attribute_error(error: impl std::fmt::Debug) -> io::Error {
|
||||
}
|
||||
impl PlayerMotion {
|
||||
pub fn new(resources: &MotionResources, position: Vec3) -> Result<Self> {
|
||||
// Identity hashes affect map order, not queried movement values. These
|
||||
// are process-owned identities; no JVM allocation order is assumed.
|
||||
// Process-owned identities determine native collection callback order.
|
||||
// Differential fixtures can replay a reference process's identities.
|
||||
let hashes: Vec<_> = resources
|
||||
.attributes
|
||||
.attributes()
|
||||
@@ -167,10 +171,19 @@ impl PlayerMotion {
|
||||
identity = identity.wrapping_add(1);
|
||||
identity
|
||||
});
|
||||
Self::with_attribute_identities(resources, position, &hashes, next)
|
||||
}
|
||||
/// Explicit identity allocation for deterministic reference replay.
|
||||
pub fn with_attribute_identities(
|
||||
resources: &MotionResources,
|
||||
position: Vec3,
|
||||
hashes: &[i32],
|
||||
next: Box<dyn FnMut() -> i32>,
|
||||
) -> Result<Self> {
|
||||
let mut attributes = LiveAttributes::new(
|
||||
resources.attributes.clone(),
|
||||
"minecraft:player",
|
||||
&hashes,
|
||||
hashes,
|
||||
next,
|
||||
)
|
||||
.map_err(attribute_error)?;
|
||||
@@ -393,6 +406,7 @@ impl MotionWorld<'_, '_, '_> {
|
||||
self.player.health.advance_clock();
|
||||
let previous = self.player.position;
|
||||
self.player_movement_tick(direction, jumping)?;
|
||||
self.player.refresh_attributes()?;
|
||||
self.update_player_pose()?;
|
||||
// The resident scene accepts the resolved local movement directly.
|
||||
// Vanilla dispatches this separately from travel, on packet acceptance.
|
||||
|
||||
Reference in New Issue
Block a user