strafe-client/src/physics_worker.rs

243 lines
9.1 KiB
Rust
Raw Normal View History

2024-08-06 18:10:43 +00:00
use strafesnet_common::mouse::MouseState;
use strafesnet_common::physics::Instruction as 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};
2024-08-06 18:26:27 +00:00
use mouse_interpolator::MouseInterpolator;
2024-08-02 17:41:42 +00:00
#[derive(Debug)]
pub enum InputInstruction{
MoveMouse(glam::IVec2),
MoveRight(bool),
MoveUp(bool),
MoveBack(bool),
MoveLeft(bool),
MoveDown(bool),
MoveForward(bool),
Jump(bool),
Zoom(bool),
2024-08-06 18:26:27 +00:00
ResetAndRestart,
ResetAndSpawn(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,
2024-08-06 18:26:27 +00:00
Resize(winit::dpi::PhysicalSize<u32>),
ChangeMap(strafesnet_common::map::CompleteMap),
//SetPaused is not an InputInstruction: the physics doesn't know that it's paused.
2024-08-02 17:41:42 +00:00
SetPaused(bool),
//Graphics(crate::graphics_worker::Instruction),
}
2024-08-06 18:26:27 +00:00
mod mouse_interpolator{
use super::*;
//TODO: move this or tab
2024-07-30 18:52:36 +00:00
pub struct MouseInterpolator{
2024-08-06 18:26:27 +00:00
//"PlayerController"
user_settings:crate::settings::UserSettings,
//"MouseInterpolator"
2024-07-30 18:52:36 +00:00
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-06 18:26:27 +00:00
//"Simulation"
2024-08-02 17:41:42 +00:00
timer:Timer<Scaled>,
2024-08-06 18:26:27 +00:00
physics:crate::physics::PhysicsContext,
2024-07-30 18:52:36 +00:00
}
impl MouseInterpolator{
2024-08-06 18:26:27 +00:00
pub fn new(
physics:crate::physics::PhysicsContext,
user_settings:crate::settings::UserSettings,
)->MouseInterpolator{
MouseInterpolator{
mouse_blocking:true,
last_mouse_time:physics.get_next_mouse().time,
timeline:std::collections::VecDeque::new(),
timer:Timer::from_state(Scaled::identity(),false),
physics,
user_settings,
}
}
fn push_mouse_instruction(&mut self,ins:&TimedInstruction<Instruction>,m:glam::IVec2){
2024-07-30 18:52:36 +00:00
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(
2024-08-06 18:26:27 +00:00
MouseState{time:self.last_mouse_time,pos:self.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
}
2024-08-06 18:26:27 +00:00
fn push(&mut self,time:Time,phys_input:PhysicsInputInstruction){
//This is always a non-mouse event
self.timeline.push_back(TimedInstruction{
time:self.timer.time(time),
instruction:phys_input,
});
}
/// returns should_empty_queue
2024-07-30 18:52:36 +00:00
/// may or may not mutate internal state XD!
2024-08-06 18:26:27 +00:00
fn map_instruction(&mut self,ins:&TimedInstruction<Instruction>)->bool{
let mut update_mouse_blocking=true;
2024-07-30 18:52:36 +00:00
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(){
2024-08-06 18:26:27 +00:00
self.push_mouse_instruction(ins,m);
2024-08-02 17:41:42 +00:00
}
2024-08-06 18:26:27 +00:00
update_mouse_blocking=false;
},
2024-08-06 18:26:27 +00:00
&InputInstruction::MoveForward(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveForward(s)),
&InputInstruction::MoveLeft(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveLeft(s)),
&InputInstruction::MoveBack(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveBack(s)),
&InputInstruction::MoveRight(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveRight(s)),
&InputInstruction::MoveUp(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveUp(s)),
&InputInstruction::MoveDown(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveDown(s)),
&InputInstruction::Jump(s)=>self.push(ins.time,PhysicsInputInstruction::SetJump(s)),
&InputInstruction::Zoom(s)=>self.push(ins.time,PhysicsInputInstruction::SetZoom(s)),
&InputInstruction::ResetAndSpawn(mode_id,stage_id)=>{
self.push(ins.time,PhysicsInputInstruction::Reset);
self.push(ins.time,PhysicsInputInstruction::SetSensitivity(self.user_settings.calculate_sensitivity()));
self.push(ins.time,PhysicsInputInstruction::Spawn(mode_id,stage_id));
},
InputInstruction::ResetAndRestart=>{
self.push(ins.time,PhysicsInputInstruction::Reset);
self.push(ins.time,PhysicsInputInstruction::SetSensitivity(self.user_settings.calculate_sensitivity()));
self.push(ins.time,PhysicsInputInstruction::Restart);
},
InputInstruction::PracticeFly=>self.push(ins.time,PhysicsInputInstruction::PracticeFly),
2024-07-30 18:52:36 +00:00
},
2024-08-02 17:41:42 +00:00
//do these really need to idle the physics?
//sending None dumps the instruction queue
2024-08-06 18:26:27 +00:00
Instruction::ChangeMap(_)=>self.push(ins.time,PhysicsInputInstruction::Idle),
Instruction::Resize(_)=>self.push(ins.time,PhysicsInputInstruction::Idle),
Instruction::Render=>self.push(ins.time,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}");
}
2024-08-06 18:26:27 +00:00
self.push(ins.time,PhysicsInputInstruction::Idle);
2024-08-02 17:41:42 +00:00
},
2024-07-30 18:52:36 +00:00
}
2024-08-06 18:26:27 +00:00
if update_mouse_blocking{
//this returns the bool for us
self.update_mouse_blocking(ins.time)
}else{
//do flush that queue
true
}
}
/// must check if self.mouse_blocking==true before calling!
fn unblock_mouse(&mut self,time:Time){
//push an event to extrapolate no movement from
self.timeline.push_front(TimedInstruction{
time:self.last_mouse_time,
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:self.timer.time(time),pos:self.physics.get_next_mouse().pos}),
});
self.last_mouse_time=self.timer.time(time);
//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;
2024-07-30 18:52:36 +00:00
}
2024-08-06 18:26:27 +00:00
fn update_mouse_blocking(&mut self,time:Time)->bool{
2024-07-30 18:52:36 +00:00
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-06 18:26:27 +00:00
if Time::from_millis(10)<self.timer.time(time)-self.physics.get_next_mouse().time{
self.unblock_mouse(time);
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-06 18:26:27 +00:00
self.last_mouse_time=self.timer.time(time);
2024-07-30 18:52:36 +00:00
true
}
}
2024-08-06 18:26:27 +00:00
fn empty_queue(&mut self){
2024-07-30 18:52:36 +00:00
while let Some(instruction)=self.timeline.pop_front(){
2024-08-06 18:26:27 +00:00
self.physics.run_input_instruction(instruction);
2024-07-30 18:52:36 +00:00
}
}
2024-08-06 18:26:27 +00:00
pub fn handle_instruction(&mut self,ins:&TimedInstruction<Instruction>){
let should_empty_queue=self.map_instruction(ins);
2024-07-30 18:52:36 +00:00
if should_empty_queue{
2024-08-06 18:26:27 +00:00
self.empty_queue();
2024-07-30 18:52:36 +00:00
}
}
pub fn get_frame_state(&self,time:Time)->crate::graphics::FrameState{
crate::graphics::FrameState{
body:self.physics.camera_body(),
camera:self.physics.camera(),
time:self.timer.time(time),
}
2024-08-06 18:26:27 +00:00
}
pub fn change_map(&mut self,time:Time,map:&strafesnet_common::map::CompleteMap){
//dump any pending interpolation state
if self.mouse_blocking{
self.unblock_mouse(time);
}
self.empty_queue();
//doing it like this to avoid doing PhysicsInstruction::ChangeMap(Rc<CompleteMap>)
self.physics.generate_models(&map);
//use the standard input interface so the instructions are written out to bots
self.handle_instruction(&TimedInstruction{
time:self.timer.time(time),
instruction:Instruction::Input(InputInstruction::ResetAndSpawn(
strafesnet_common::gameplay_modes::ModeId::MAIN,
strafesnet_common::gameplay_modes::StageId::FIRST,
)),
});
}
pub const fn user_settings(&self)->&crate::settings::UserSettings{
&self.user_settings
}
}
2024-07-30 18:52:36 +00:00
}
2024-08-06 18:26:27 +00:00
pub fn new<'a>(
mut graphics_worker:crate::compat_worker::INWorker<'a,crate::graphics_worker::Instruction>,
user_settings:crate::settings::UserSettings,
)->crate::compat_worker::QNWorker<'a,TimedInstruction<Instruction>>{
let physics=crate::physics::PhysicsContext::default();
let mut interpolator=MouseInterpolator::new(
physics,
user_settings
);
2024-07-30 18:52:36 +00:00
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
2024-08-06 18:26:27 +00:00
interpolator.handle_instruction(&ins);
2024-07-30 18:52:36 +00:00
match ins.instruction{
Instruction::Render=>{
let frame_state=interpolator.get_frame_state(ins.time);
graphics_worker.send(crate::graphics_worker::Instruction::Render(frame_state)).unwrap();
2024-07-30 18:52:36 +00:00
},
2024-08-06 18:26:27 +00:00
Instruction::Resize(size)=>{
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,interpolator.user_settings().clone())).unwrap();
2024-07-30 18:52:36 +00:00
},
2024-08-06 18:26:27 +00:00
Instruction::ChangeMap(map)=>{
interpolator.change_map(ins.time,&map);
graphics_worker.send(crate::graphics_worker::Instruction::ChangeMap(map)).unwrap();
2024-07-30 18:52:36 +00:00
},
2024-08-06 18:26:27 +00:00
Instruction::Input(_)=>(),
Instruction::SetPaused(_)=>(),
2024-07-30 18:52:36 +00:00
}
})
}