Merge pull request #430 from ludovic111/fix/lan-guest-damage
LAN guest: fall damage, regeneration and hunger on the host
This commit is contained in:
@@ -12,13 +12,14 @@ use horizon_client::{
|
||||
entities::Experience,
|
||||
items::ItemStack,
|
||||
lan::{guest::SPECTATOR, menu::Rules},
|
||||
motion::PlayerMotion,
|
||||
motion::{MotionWorld, PlayerMotion},
|
||||
};
|
||||
use horizon_core::geometry::Vec3;
|
||||
use horizon_protocol::{
|
||||
packets::play::{self, Clientbound as C, Serverbound as S},
|
||||
text::literal,
|
||||
};
|
||||
use horizon_world::entity_move::EntityMoveWorld;
|
||||
use winit::keyboard::KeyCode;
|
||||
|
||||
/// Per-guest state of the gameplay code that lives outside `Guest`.
|
||||
@@ -743,6 +744,7 @@ impl Game<'_> {
|
||||
}
|
||||
});
|
||||
}
|
||||
self.guest_server_tick(host, conn);
|
||||
self.guest_dig_feedback(host, conn);
|
||||
self.guest_pickups(host, gi);
|
||||
let g = &mut host.hub.guests[gi];
|
||||
@@ -760,6 +762,51 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The guest's body on the server each tick: ServerPlayer.tick's damage
|
||||
/// clock and Entity.baseTick's fluid phase, then
|
||||
/// ServerPlayer.doCheckFallDamage for each move the guest made (the fall
|
||||
/// distance grows while they go down and hurts them where they land, as
|
||||
/// the host's own landing does; water resets it, lava halves it), then
|
||||
/// regeneration and the food tick (ServerPlayer.doTick).
|
||||
fn guest_server_tick(&mut self, host: &mut LanHost, conn: u32) {
|
||||
let Some(g) = host.hub.guest_mut(conn) else {
|
||||
return;
|
||||
};
|
||||
let moves = std::mem::take(&mut g.moves);
|
||||
if g.spectator() {
|
||||
return;
|
||||
}
|
||||
self.as_guest(host, conn, |game| {
|
||||
let (rules, health) = (game.world.health_rules, &game.motion.health);
|
||||
let Some(player) = game.player.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let mut w = MotionWorld {
|
||||
player,
|
||||
scene: &mut game.world,
|
||||
assets: &game.res.assets,
|
||||
collision_binding: game.collision,
|
||||
resources: game.motion,
|
||||
};
|
||||
w.player.health.advance_clock();
|
||||
let _ = w.fluid_base_tick();
|
||||
for (moved, on_ground) in moves {
|
||||
let movement = Vec3::new(moved[0], moved[1], moved[2]);
|
||||
let landed = (|| -> horizon_client::Result<()> {
|
||||
w.set_on_ground_with_movement(on_ground, false, movement)?;
|
||||
let pos = w.legacy_on_position()?;
|
||||
let state = w.block_state(pos)?;
|
||||
w.check_fall_damage(moved[1], on_ground, state, pos)
|
||||
})();
|
||||
if landed.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let _ = w.player.peaceful_regeneration(rules);
|
||||
let _ = w.player.food_tick(health, rules);
|
||||
});
|
||||
}
|
||||
|
||||
/// The crack animation other players see.
|
||||
fn guest_dig_feedback(&mut self, host: &mut LanHost, conn: u32) {
|
||||
let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else {
|
||||
|
||||
@@ -99,6 +99,9 @@ pub struct Guest {
|
||||
pub inbox: VecDeque<play::Serverbound>,
|
||||
pub joined_tick: u64,
|
||||
pub ticks: u64,
|
||||
/// The moves accepted since the game last looked (movement, on ground),
|
||||
/// for ServerPlayer.doCheckFallDamage.
|
||||
pub moves: Vec<([f64; 3], bool)>,
|
||||
pub dead: bool,
|
||||
/// The join packets were sent (PlayerList.placeNewPlayer ran).
|
||||
pub placed: bool,
|
||||
@@ -174,6 +177,7 @@ impl Guest {
|
||||
inbox: VecDeque::new(),
|
||||
joined_tick: 0,
|
||||
ticks: 0,
|
||||
moves: Vec::new(),
|
||||
dead: false,
|
||||
placed: false,
|
||||
menu: None,
|
||||
|
||||
@@ -1394,9 +1394,7 @@ impl Hub {
|
||||
S::MovePlayerRot(p) => {
|
||||
self.move_player(i, None, Some((p.yaw, p.pitch)), p.flags);
|
||||
}
|
||||
S::MovePlayerStatus(p) => {
|
||||
self.guests[i].on_ground = p.flags & 1 != 0;
|
||||
}
|
||||
S::MovePlayerStatus(p) => self.move_player(i, None, None, p.flags),
|
||||
S::PlayerLoaded(_) => self.guests[i].loaded = true,
|
||||
S::ChunkBatchReceived(p) => {
|
||||
let g = &mut self.guests[i];
|
||||
@@ -1460,6 +1458,10 @@ impl Hub {
|
||||
g.previous_position = g.position;
|
||||
g.position = p;
|
||||
}
|
||||
let moved = pos.map_or([0.0; 3], |_| {
|
||||
[0, 1, 2].map(|k| g.position[k] - g.previous_position[k])
|
||||
});
|
||||
g.moves.push((moved, flags & 1 != 0));
|
||||
if let Some((yaw, pitch)) = rot
|
||||
&& yaw.is_finite()
|
||||
&& pitch.is_finite()
|
||||
|
||||
Reference in New Issue
Block a user