strafe-client/src/physics_worker.rs

187 lines
7.8 KiB
Rust
Raw Normal View History

use crate::physics::{MouseState,PhysicsInputInstruction};
2024-01-30 00:19:41 +00:00
use strafesnet_common::integer::Time;
2024-08-02 17:41:42 +00:00
use strafesnet_common::instruction::TimedInstruction;
use strafesnet_common::timer::{Scaled,Timer,TimerState};
#[derive(Debug)]
pub enum InputInstruction{
MoveMouse(glam::IVec2),
MoveRight(bool),
MoveUp(bool),
MoveBack(bool),
MoveLeft(bool),
MoveDown(bool),
MoveForward(bool),
Jump(bool),
Zoom(bool),
Restart,
Spawn(strafesnet_common::gameplay_modes::ModeId,strafesnet_common::gameplay_modes::StageId),
2024-02-16 08:19:44 +00:00
PracticeFly,
}
pub enum Instruction{
Input(InputInstruction),
Render,
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
2024-02-15 07:19:15 +00:00
GenerateModels(strafesnet_common::map::CompleteMap),
ClearModels,
2024-08-02 17:41:42 +00:00
SetPaused(bool),
//Graphics(crate::graphics_worker::Instruction),
}
2024-07-30 18:52:36 +00:00
pub struct MouseInterpolator{
timeline:std::collections::VecDeque<TimedInstruction<PhysicsInputInstruction>>,
2024-08-02 17:41:42 +00:00
last_mouse_time:Time,//this value is pre-transformed to simulation time
2024-07-30 18:52:36 +00:00
mouse_blocking:bool,
2024-08-02 17:41:42 +00:00
timer:Timer<Scaled>,
2024-07-30 18:52:36 +00:00
}
impl MouseInterpolator{
fn push_mouse_instruction(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>,m:glam::IVec2){
if self.mouse_blocking{
//tell the game state which is living in the past about its future
self.timeline.push_front(TimedInstruction{
time:self.last_mouse_time,
2024-08-02 17:41:42 +00:00
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:self.timer.time(ins.time),pos:m}),
2024-07-30 18:52:36 +00:00
});
}else{
//mouse has just started moving again after being still for longer than 10ms.
//replace the entire mouse interpolation state to avoid an intermediate state with identical m0.t m1.t timestamps which will divide by zero
self.timeline.push_front(TimedInstruction{
time:self.last_mouse_time,
instruction:PhysicsInputInstruction::ReplaceMouse(
MouseState{time:self.last_mouse_time,pos:physics.get_next_mouse().pos},
2024-08-02 17:41:42 +00:00
MouseState{time:self.timer.time(ins.time),pos:m}
2024-07-30 18:52:36 +00:00
),
});
//delay physics execution until we have an interpolation target
self.mouse_blocking=true;
}
2024-08-02 17:41:42 +00:00
self.last_mouse_time=self.timer.time(ins.time);
2024-07-30 18:52:36 +00:00
}
/// returns the mapped physics input instruction
/// may or may not mutate internal state XD!
fn map_instruction(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>)->Option<PhysicsInputInstruction>{
match &ins.instruction{
Instruction::Input(input_instruction)=>match input_instruction{
&InputInstruction::MoveMouse(m)=>{
2024-08-02 17:41:42 +00:00
if !self.timer.is_paused(){
self.push_mouse_instruction(physics,ins,m);
}
2024-07-30 18:52:36 +00:00
None
},
2024-07-30 18:52:36 +00:00
&InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)),
&InputInstruction::MoveLeft(s)=>Some(PhysicsInputInstruction::SetMoveLeft(s)),
&InputInstruction::MoveBack(s)=>Some(PhysicsInputInstruction::SetMoveBack(s)),
&InputInstruction::MoveRight(s)=>Some(PhysicsInputInstruction::SetMoveRight(s)),
&InputInstruction::MoveUp(s)=>Some(PhysicsInputInstruction::SetMoveUp(s)),
&InputInstruction::MoveDown(s)=>Some(PhysicsInputInstruction::SetMoveDown(s)),
&InputInstruction::Jump(s)=>Some(PhysicsInputInstruction::SetJump(s)),
&InputInstruction::Zoom(s)=>Some(PhysicsInputInstruction::SetZoom(s)),
&InputInstruction::Spawn(mode_id,stage_id)=>Some(PhysicsInputInstruction::Spawn(mode_id,stage_id)),
InputInstruction::Restart=>Some(PhysicsInputInstruction::Restart),
2024-07-30 18:52:36 +00:00
InputInstruction::PracticeFly=>Some(PhysicsInputInstruction::PracticeFly),
},
2024-08-02 17:41:42 +00:00
//do these really need to idle the physics?
//sending None dumps the instruction queue
2024-07-30 18:52:36 +00:00
Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle),
Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle),
Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle),
Instruction::Render=>Some(PhysicsInputInstruction::Idle),
2024-08-02 17:41:42 +00:00
&Instruction::SetPaused(paused)=>{
if let Err(e)=self.timer.set_paused(ins.time,paused){
println!("Cannot pause: {e}");
}
Some(PhysicsInputInstruction::Idle)
},
2024-07-30 18:52:36 +00:00
}
}
fn update_mouse_blocking(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>)->bool{
if self.mouse_blocking{
//assume the mouse has stopped moving after 10ms.
//shitty mice are 125Hz which is 8ms so this should cover that.
//setting this to 100us still doesn't print even though it's 10x lower than the polling rate,
//so mouse events are probably not handled separately from drawing and fire right before it :(
2024-08-02 17:41:42 +00:00
if Time::from_millis(10)<self.timer.time(ins.time)-physics.get_next_mouse().time{
2024-07-30 18:52:36 +00:00
//push an event to extrapolate no movement from
self.timeline.push_front(TimedInstruction{
time:self.last_mouse_time,
2024-08-02 17:41:42 +00:00
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:self.timer.time(ins.time),pos:physics.get_next_mouse().pos}),
});
2024-08-02 17:41:42 +00:00
self.last_mouse_time=self.timer.time(ins.time);
2024-07-30 18:52:36 +00:00
//stop blocking. the mouse is not moving so the physics does not need to live in the past and wait for interpolation targets.
self.mouse_blocking=false;
true
2024-07-30 18:52:36 +00:00
}else{
false
}
2024-07-30 18:52:36 +00:00
}else{
//keep this up to date so that it can be used as a known-timestamp
//that the mouse was not moving when the mouse starts moving again
2024-08-02 17:41:42 +00:00
self.last_mouse_time=self.timer.time(ins.time);
2024-07-30 18:52:36 +00:00
true
}
}
/// returns whether or not to empty the instruction queue
fn handle_physics_input(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>,phys_input_option:Option<PhysicsInputInstruction>)->bool{
if let Some(phys_input)=phys_input_option{
//non-mouse event
self.timeline.push_back(TimedInstruction{
2024-08-02 17:41:42 +00:00
time:self.timer.time(ins.time),
2024-07-30 18:52:36 +00:00
instruction:phys_input,
});
//this returns the bool for us
self.update_mouse_blocking(physics,ins)
}else{
//mouse event
true
}
}
fn empty_queue(&mut self,physics:&mut crate::physics::PhysicsContext){
while let Some(instruction)=self.timeline.pop_front(){
physics.run_input_instruction(instruction);
}
}
fn handle_instruction(&mut self,physics:&mut crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>){
let physics_input_option=self.map_instruction(physics,ins);
let should_empty_queue=self.handle_physics_input(physics,ins,physics_input_option);
if should_empty_queue{
self.empty_queue(physics);
}
}
}
pub fn new(mut physics:crate::physics::PhysicsContext,mut graphics_worker:crate::compat_worker::INWorker<crate::graphics_worker::Instruction>)->crate::compat_worker::QNWorker<TimedInstruction<Instruction>>{
let mut interpolator=MouseInterpolator{
mouse_blocking:true,
last_mouse_time:physics.get_next_mouse().time,
timeline:std::collections::VecDeque::new(),
2024-08-02 17:41:42 +00:00
timer:Timer::from_state(Scaled::identity(),false),
2024-07-30 18:52:36 +00:00
};
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
interpolator.handle_instruction(&mut physics,&ins);
match ins.instruction{
Instruction::Render=>{
2024-08-02 17:41:42 +00:00
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),interpolator.timer.time(ins.time),physics.get_next_mouse().pos)).unwrap();
2024-07-30 18:52:36 +00:00
},
Instruction::Resize(size,user_settings)=>{
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap();
},
Instruction::GenerateModels(map)=>{
physics.generate_models(&map);
//important!
//bots will not work properly without this exact restart + spawn setup
//reset the physics state to start a new run on the new map
physics.restart();
//generate a spawn event so bots work properly on the first run
//no run started so does not invalidate the run
2024-07-30 18:52:36 +00:00
physics.spawn();
graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(map)).unwrap();
},
Instruction::ClearModels=>{
physics.clear();
graphics_worker.send(crate::graphics_worker::Instruction::ClearModels).unwrap();
},
_=>(),
}
})
}