A guest hears its door at once, and only once
Vanilla plays a block use's sound with `level.playSound(player, ...)`: the player who clicked hears it from their own client's use, and the server sends it to everyone else. Horizon's guest waited for the server's sound, one round trip after its predicted door had already opened. The guest now plays the sounds of the use it predicts (doors, trapdoors, fence gates, levers, buttons), and the server sends the sounds of such a use to the other players only. The control socket's `status` counts the positional sounds each game played (`heard`). lan_e2e `predict`: with the host frozen, the guest already hears its door; once the server answers, the guest and the host have each heard it once. Fixes #869 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -157,6 +157,9 @@ pub struct Audio {
|
||||
/// Positional sounds played since the last drain, kept while the world
|
||||
/// is open to the network so guests hear them too.
|
||||
pub capture: Option<Vec<(String, [f64; 3], f32, f32)>>,
|
||||
/// How many times each positional sound played (the control socket's `status`),
|
||||
/// with or without an audio device.
|
||||
pub heard: std::collections::BTreeMap<String, u32>,
|
||||
}
|
||||
fn decode(path: &Path) -> Option<(Vec<f32>, u32)> {
|
||||
use symphonia::core::{
|
||||
@@ -542,6 +545,7 @@ impl Audio {
|
||||
random: LegacyRandom::new(crate::sound_engine::unique_seed()),
|
||||
source_override: None,
|
||||
capture: None,
|
||||
heard: Default::default(),
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "audio"))]
|
||||
@@ -682,6 +686,9 @@ impl Audio {
|
||||
if let (Some(capture), Some(at)) = (self.capture.as_mut(), at) {
|
||||
capture.push((event.to_owned(), at, volume, pitch));
|
||||
}
|
||||
if at.is_some() {
|
||||
*self.heard.entry(event.to_owned()).or_default() += 1;
|
||||
}
|
||||
self.sync_volumes();
|
||||
let event = crate::sound_events::normalize(event);
|
||||
let source = self
|
||||
|
||||
@@ -912,6 +912,7 @@ impl Game<'_> {
|
||||
status["pending_uploads"] = self.pending_uploads.len().into();
|
||||
status["destroying"] = json!(destroying);
|
||||
status["physical_keys"] = json!(physical_keys);
|
||||
status["heard"] = json!(self.audio.heard);
|
||||
// Boats, minecarts, armor stands, item frames and paintings.
|
||||
status["objects"] = json!(self.objects_status());
|
||||
// Primed TNT, falling blocks, projectiles, tridents and end crystals.
|
||||
|
||||
@@ -805,6 +805,8 @@ impl Game<'_> {
|
||||
],
|
||||
};
|
||||
let hand = if p.hand == 1 { Hand::Off } else { Hand::Main };
|
||||
let logged = self.world.block_log.as_ref().map_or(0, Vec::len);
|
||||
let heard = self.audio.capture.as_ref().map_or(0, Vec::len);
|
||||
let swung = self.as_guest(host, conn, |g| {
|
||||
g.forced_hit = Some(hit);
|
||||
g.guest_use = Some(GuestUse::OnBlock(hand));
|
||||
@@ -815,6 +817,7 @@ impl Game<'_> {
|
||||
g.swings_started != swings
|
||||
});
|
||||
self.lan_swing_back(host, conn, swung);
|
||||
self.guest_use_sounds(host, conn, logged, heard);
|
||||
// The client predicted a result: correct it with what really is.
|
||||
for pos in [
|
||||
p.pos,
|
||||
@@ -1962,6 +1965,33 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// `level.playSound(player, ...)` of a guest's use it predicted (`remote::hand_operated`
|
||||
/// blocks only, as the guest decides): the others hear it now, the guest already did.
|
||||
fn guest_use_sounds(&mut self, host: &mut LanHost, conn: u32, logged: usize, heard: usize) {
|
||||
let changed: Vec<(i32, i32, i32)> = self
|
||||
.world
|
||||
.block_log
|
||||
.as_ref()
|
||||
.map_or(&[][..], |log| log.get(logged..).unwrap_or(&[]))
|
||||
.to_vec();
|
||||
let facts = self.res.facts();
|
||||
if changed.is_empty()
|
||||
|| !changed.iter().all(|&(x, y, z)| {
|
||||
super::remote::hand_operated(&facts.get(self.world.block(x, y, z)).name)
|
||||
})
|
||||
{
|
||||
return;
|
||||
}
|
||||
let Some(capture) = self.audio.capture.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let sounds: Vec<_> = capture.drain(heard.min(capture.len())..).collect();
|
||||
for (name, at, volume, pitch) in sounds {
|
||||
let source = horizon_client::settings::sound_source(&name) as i32;
|
||||
host.hub.sound(&name, at, volume, pitch, source, Some(conn));
|
||||
}
|
||||
}
|
||||
|
||||
/// Positional sounds and level events of the tick, for the guests.
|
||||
pub(super) fn lan_broadcast_effects(&mut self, host: &mut LanHost) {
|
||||
let sounds = self.audio.capture.take().unwrap_or_default();
|
||||
|
||||
@@ -3247,7 +3247,7 @@ impl Game<'_> {
|
||||
let (reacts, changes) = if hand == Hand::Main {
|
||||
self.remote_block_reacts(&hit, held.as_ref())
|
||||
} else {
|
||||
(false, Vec::new())
|
||||
(false, DryUse::default())
|
||||
};
|
||||
let seq = self.next_sequence();
|
||||
let cursor = [
|
||||
@@ -3292,19 +3292,9 @@ impl Game<'_> {
|
||||
/// server refused goes back). Only the blocks a hand opens or flips: a
|
||||
/// wooden or copper door, trapdoor or fence gate, a lever or a button;
|
||||
/// whatever else the use changes waits for the server.
|
||||
fn predict_use(
|
||||
&mut self,
|
||||
changes: &[(horizon_core::position::BlockPos, BlockStateId)],
|
||||
seq: i32,
|
||||
) {
|
||||
fn predict_use(&mut self, (changes, sounds): &DryUse, seq: i32) {
|
||||
let facts = self.res.facts();
|
||||
let predicted = |state: BlockStateId| {
|
||||
let name = facts.get(state).name.trim_start_matches("minecraft:");
|
||||
((name.ends_with("_door") || name.ends_with("_trapdoor")) && !name.starts_with("iron_"))
|
||||
|| name.ends_with("_fence_gate")
|
||||
|| name.ends_with("_button")
|
||||
|| name == "lever"
|
||||
};
|
||||
let predicted = |state: BlockStateId| hand_operated(&facts.get(state).name);
|
||||
if changes.is_empty()
|
||||
|| !changes
|
||||
.iter()
|
||||
@@ -3324,6 +3314,21 @@ impl Game<'_> {
|
||||
}
|
||||
self.set_block(pos, state.0 as i32);
|
||||
}
|
||||
// `level.playSound(player, ...)`: the clicker hears it from its own use; the
|
||||
// server sends it to the others only.
|
||||
for (pos, sound, volume, pitch) in sounds {
|
||||
let at = [
|
||||
f64::from(pos.x) + 0.5,
|
||||
f64::from(pos.y) + 0.5,
|
||||
f64::from(pos.z) + 0.5,
|
||||
];
|
||||
let name = if sound.contains(':') {
|
||||
sound.to_string()
|
||||
} else {
|
||||
format!("minecraft:{sound}")
|
||||
};
|
||||
self.audio.play(&name, Some(at), *volume, *pitch);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the block hit takes a main-hand right click with `held`
|
||||
@@ -3331,15 +3336,11 @@ impl Game<'_> {
|
||||
/// container, and the blocks it changed. Vanilla's client runs it on its
|
||||
/// own level first; this runs it on a copy, and `predict_use` keeps the
|
||||
/// changes it predicts.
|
||||
fn remote_block_reacts(
|
||||
&self,
|
||||
hit: &super::Hit,
|
||||
held: Option<&ItemStack>,
|
||||
) -> (bool, Vec<(horizon_core::position::BlockPos, BlockStateId)>) {
|
||||
fn remote_block_reacts(&self, hit: &super::Hit, held: Option<&ItemStack>) -> (bool, DryUse) {
|
||||
// Player.isSecondaryUseActive with something in either hand.
|
||||
let hands_full = self.stack_in(self.selected).is_some() || self.offhand.is_some();
|
||||
if self.keys.contains(&KeyCode::ShiftLeft) && hands_full {
|
||||
return (false, Vec::new());
|
||||
return (false, DryUse::default());
|
||||
}
|
||||
use horizon_core::geometry::Direction;
|
||||
let face = match hit.face {
|
||||
@@ -3354,6 +3355,7 @@ impl Game<'_> {
|
||||
world: &self.world,
|
||||
changed: HashMap::new(),
|
||||
random: horizon_core::random::LegacyRandom::new(0),
|
||||
sounds: Vec::new(),
|
||||
};
|
||||
let reacts = self
|
||||
.res
|
||||
@@ -3368,7 +3370,7 @@ impl Game<'_> {
|
||||
held.map(|s| &*s.item),
|
||||
)
|
||||
.consumes_action();
|
||||
(reacts, level.changed.into_iter().collect())
|
||||
(reacts, (level.changed.into_iter().collect(), level.sounds))
|
||||
}
|
||||
|
||||
/// MultiPlayerGameMode.useItem for `hand`, if it holds something: whether
|
||||
@@ -4270,9 +4272,45 @@ struct DryLevel<'w> {
|
||||
world: &'w horizon_client::world::World,
|
||||
changed: HashMap<horizon_core::position::BlockPos, BlockStateId>,
|
||||
random: horizon_core::random::LegacyRandom,
|
||||
/// The sounds the use plays (a door's creak).
|
||||
sounds: Vec<DrySound>,
|
||||
}
|
||||
|
||||
type DrySound = (
|
||||
horizon_core::position::BlockPos,
|
||||
std::sync::Arc<str>,
|
||||
f32,
|
||||
f32,
|
||||
);
|
||||
/// What a use on the dry level changed, and the sounds it played.
|
||||
type DryUse = (
|
||||
Vec<(horizon_core::position::BlockPos, BlockStateId)>,
|
||||
Vec<DrySound>,
|
||||
);
|
||||
|
||||
/// The blocks a guest's hand flips that its use predicts (and hears at once): a wooden or
|
||||
/// copper door, trapdoor or fence gate, a lever or a button. An iron door or trapdoor has
|
||||
/// no hand use.
|
||||
pub(super) fn hand_operated(name: &str) -> bool {
|
||||
let name = name.trim_start_matches("minecraft:");
|
||||
((name.ends_with("_door") || name.ends_with("_trapdoor")) && !name.starts_with("iron_"))
|
||||
|| name.ends_with("_fence_gate")
|
||||
|| name.ends_with("_button")
|
||||
|| name == "lever"
|
||||
}
|
||||
|
||||
impl horizon_world::level::LevelAccess for DryLevel<'_> {
|
||||
fn emit(&mut self, event: horizon_world::level::LevelEvent) {
|
||||
if let horizon_world::level::LevelEvent::Sound {
|
||||
pos,
|
||||
sound,
|
||||
volume,
|
||||
pitch,
|
||||
} = event
|
||||
{
|
||||
self.sounds.push((pos, sound, volume, pitch));
|
||||
}
|
||||
}
|
||||
fn block_state(&self, pos: horizon_core::position::BlockPos) -> BlockStateId {
|
||||
self.changed
|
||||
.get(&pos)
|
||||
|
||||
+10
-1
@@ -53,7 +53,8 @@ address. The script then checks, from both sides, what a guest's game used to ge
|
||||
- sleep: at night the guest lies down in its own bed (not the host); alone it stays night, it
|
||||
gets up with Escape, and once the host sleeps too the night ends and both wake.
|
||||
- predict: with the host frozen, the oak door the guest opens opens on the guest's screen at once (its
|
||||
prediction), and stays open once the server answers; an iron door stays shut (#869).
|
||||
prediction) and creaks there at once, stays open once the server answers, and each side hears
|
||||
it once; an iron door stays shut (#869).
|
||||
- push: a cow standing in the guest pushes it just clear of the cow (the guest's own client
|
||||
pushes it, as in vanilla), not ever faster across the ground.
|
||||
- eat: holding right click with bread, the guest's own screen plays the eating (the item in use
|
||||
@@ -1271,10 +1272,14 @@ def check_predict(host, guest, out):
|
||||
guest.wait(20)
|
||||
state = lambda game, at: game.raw('block', *at)['state']
|
||||
seen = {}
|
||||
creaks = lambda game: sum(n for s, n in game('status')['heard'].items() if 'door' in s)
|
||||
heard = {'guest': creaks(guest), 'host': creaks(host)}
|
||||
os.kill(host.pid, signal.SIGSTOP)
|
||||
try:
|
||||
guest.raw('interact', *wood)
|
||||
seen['guest_frozen'] = (state(guest, wood), state(guest, (wood[0], wood[1] + 1, wood[2])))
|
||||
# level.playSound(player, ...): the clicker hears its own door at once.
|
||||
seen['guest_heard_frozen'] = creaks(guest) - heard['guest']
|
||||
guest.raw('interact', *iron)
|
||||
seen['guest_iron_frozen'] = state(guest, iron)
|
||||
finally:
|
||||
@@ -1283,6 +1288,8 @@ def check_predict(host, guest, out):
|
||||
seen['guest_after'] = state(guest, wood)
|
||||
seen['host_after'] = state(host, wood)
|
||||
seen['iron_after'] = (state(guest, iron), state(host, iron))
|
||||
# ... and not a second time from the server; the host hears it once.
|
||||
seen['heard_after'] = {'guest': creaks(guest) - heard['guest'], 'host': creaks(host) - heard['host']}
|
||||
guest.shot(out, 'predict')
|
||||
for (bx, by, bz) in (wood, iron):
|
||||
host('command', command=f'fill {bx} {by} {bz} {bx} {by + 1} {bz} minecraft:air')
|
||||
@@ -1290,6 +1297,8 @@ def check_predict(host, guest, out):
|
||||
assert 'open=true' in seen['guest_after'] and 'open=true' in seen['host_after'], f'the door did not stay open: {seen}'
|
||||
assert 'open=false' in seen['guest_iron_frozen'], f'the guest opened an iron door by hand: {seen}'
|
||||
assert all('open=false' in s for s in seen['iron_after']), f'an iron door opened: {seen}'
|
||||
assert seen['guest_heard_frozen'] == 1 and seen['heard_after'] == {'guest': 1, 'host': 1}, \
|
||||
f'the door is not heard once on each side: {seen}'
|
||||
return seen
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user