fix timeout timestamp

This commit is contained in:
Quaternions 2025-01-15 00:02:19 -08:00
parent 292df72709
commit ada34237c9
2 changed files with 19 additions and 11 deletions

View File

@ -2,6 +2,7 @@ use strafesnet_common::mouse::MouseState;
use strafesnet_common::physics::{ use strafesnet_common::physics::{
Instruction as PhysicsInputInstruction, Instruction as PhysicsInputInstruction,
TimeInner as PhysicsTimeInner, TimeInner as PhysicsTimeInner,
Time as PhysicsTime,
MouseInstruction, MouseInstruction,
OtherInstruction, OtherInstruction,
}; };
@ -85,7 +86,7 @@ impl MouseInterpolator{
} }
} }
} }
fn timeout(&mut self){ fn timeout(&mut self,time:PhysicsTime){
let buffer_state=core::mem::replace(&mut self.buffer_state,BufferState::Unbuffered); let buffer_state=core::mem::replace(&mut self.buffer_state,BufferState::Unbuffered);
match buffer_state{ match buffer_state{
BufferState::Unbuffered=>(), BufferState::Unbuffered=>(),
@ -100,8 +101,8 @@ impl MouseInterpolator{
// but the timer is not accessible from this scope // but the timer is not accessible from this scope
// and it's just here to say that the mouse isn't moving anyways. // and it's just here to say that the mouse isn't moving anyways.
// I think this is a divide by zero bug, two identical mouse_states will occupy the interpolation state // I think this is a divide by zero bug, two identical mouse_states will occupy the interpolation state
time:mouse_state.time, time,
instruction:MouseInstruction::SetNextMouse(mouse_state), instruction:MouseInstruction::SetNextMouse(MouseState{pos:mouse_state.pos,time}),
}); });
}, },
} }
@ -120,7 +121,7 @@ impl MouseInterpolator{
// push buffered mouse instruction and flush buffer to output // push buffered mouse instruction and flush buffer to output
if self.get_timedout_at(ins.time).is_some(){ if self.get_timedout_at(ins.time).is_some(){
self.timeout(); self.timeout(ins.instruction.time);
} }
// replace_with allows the enum variant to safely be replaced from behind a mutable reference // replace_with allows the enum variant to safely be replaced from behind a mutable reference
let (ins_mouse,ins_other)=replace_with::replace_with_or_abort_and_return(&mut self.buffer_state,|buffer_state|{ let (ins_mouse,ins_other)=replace_with::replace_with_or_abort_and_return(&mut self.buffer_state,|buffer_state|{
@ -187,11 +188,10 @@ impl MouseInterpolator{
}), }),
} }
} }
// include timestamp and implement InstructionConsumer? pub fn pop_buffered_instruction(&mut self,ins:TimedInstruction<StepInstruction,PhysicsTimeInner>)->Option<TimedInstruction<PhysicsInputInstruction,PhysicsTimeInner>>{
pub fn pop_buffered_instruction(&mut self,ins:StepInstruction)->Option<TimedInstruction<PhysicsInputInstruction,PhysicsTimeInner>>{ match ins.instruction{
match ins{
StepInstruction::Pop=>(), StepInstruction::Pop=>(),
StepInstruction::Timeout=>self.timeout(), StepInstruction::Timeout=>self.timeout(ins.time),
} }
self.output.pop_front() self.output.pop_front()
} }
@ -204,7 +204,7 @@ mod test{
fn test(){ fn test(){
let mut interpolator=MouseInterpolator::new(); let mut interpolator=MouseInterpolator::new();
let timer=strafesnet_common::timer::Timer::<strafesnet_common::timer::Scaled<SessionTimeInner,PhysicsTimeInner>>::unpaused(SessionTime::ZERO,strafesnet_common::physics::Time::from_secs(1000)); let timer=strafesnet_common::timer::Timer::<strafesnet_common::timer::Scaled<SessionTimeInner,PhysicsTimeInner>>::unpaused(SessionTime::ZERO,PhysicsTime::from_secs(1000));
macro_rules! push{ macro_rules! push{
($time:expr,$ins:expr)=>{ ($time:expr,$ins:expr)=>{
@ -217,7 +217,11 @@ mod test{
} }
}); });
while let Some(ins)=interpolator.buffered_instruction_with_timeout($time){ while let Some(ins)=interpolator.buffered_instruction_with_timeout($time){
let out=interpolator.pop_buffered_instruction(ins.instruction); let ins_retimed=TimedInstruction{
time:timer.time(ins.time),
instruction:ins.instruction,
};
let out=interpolator.pop_buffered_instruction(ins_retimed);
println!("out={out:?}"); println!("out={out:?}");
} }
}; };

View File

@ -176,7 +176,11 @@ impl InstructionConsumer<StepInstruction> for Session{
type TimeInner=SessionTimeInner; type TimeInner=SessionTimeInner;
fn process_instruction(&mut self,ins:TimedInstruction<StepInstruction,Self::TimeInner>){ fn process_instruction(&mut self,ins:TimedInstruction<StepInstruction,Self::TimeInner>){
// ins.time ignored??? // ins.time ignored???
if let Some(instruction)=self.mouse_interpolator.pop_buffered_instruction(ins.instruction){ let ins_retimed=TimedInstruction{
time:self.simulation.timer.time(ins.time),
instruction:ins.instruction,
};
if let Some(instruction)=self.mouse_interpolator.pop_buffered_instruction(ins_retimed){
self.simulation.physics.run_input_instruction(instruction); self.simulation.physics.run_input_instruction(instruction);
} }
} }