forked from StrafesNET/strafe-client
Compare commits
17 Commits
master
...
bot-worker
Author | SHA1 | Date | |
---|---|---|---|
96eb23d66b | |||
6e206f4207 | |||
c485c02b05 | |||
af478bce28 | |||
a3ac815e45 | |||
78cf5e155e | |||
26b286af31 | |||
ad78fd22be | |||
3d0643e7cf | |||
78d7af70a3 | |||
b27ad3473e | |||
0985661e45 | |||
eb99ea3707 | |||
8d4db7e654 | |||
4eccd27237 | |||
a54836a9cb | |||
d3bede8b1c |
31
src/bot_worker.rs
Normal file
31
src/bot_worker.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use strafesnet_snf::bot::BotDebug;
|
||||
|
||||
pub enum Instruction{
|
||||
//TODO: pass map id
|
||||
Create,
|
||||
//Delete,
|
||||
Push{
|
||||
ins:strafesnet_common::instruction::TimedInstruction<strafesnet_common::physics::Instruction>,
|
||||
},
|
||||
}
|
||||
|
||||
//a new worker is spawned on a thread
|
||||
//create opens a new file
|
||||
//push pushes an instruction to the file
|
||||
|
||||
pub struct Worker{
|
||||
file:BotDebug,
|
||||
}
|
||||
|
||||
pub fn new<'a>(scope:&'a std::thread::Scope<'a,'_>)->crate::worker::QNWorker<'a,Instruction>{
|
||||
let mut worker=Worker{
|
||||
file:BotDebug::new().unwrap(),
|
||||
};
|
||||
crate::worker::QNWorker::new(scope,move|instruction|{
|
||||
match instruction{
|
||||
Instruction::Create=>worker.file=BotDebug::new().unwrap(),
|
||||
//Instruction::Delete=>worker.file.delete().unwrap(),
|
||||
Instruction::Push{ins}=>worker.file.push(ins).unwrap(),
|
||||
}
|
||||
})
|
||||
}
|
@ -816,7 +816,7 @@ impl GraphicsState{
|
||||
});
|
||||
|
||||
let camera=GraphicsCamera::default();
|
||||
let camera_uniforms=camera.to_uniform_data(crate::physics::PhysicsOutputState::default().extrapolate(crate::physics::MouseState::default()));
|
||||
let camera_uniforms=camera.to_uniform_data(crate::physics::PhysicsOutputState::default().extrapolate(strafesnet_common::mouse::MouseState::default()));
|
||||
let camera_buf=device.create_buffer_init(&wgpu::util::BufferInitDescriptor{
|
||||
label:Some("Camera"),
|
||||
contents:bytemuck::cast_slice(&camera_uniforms),
|
||||
@ -893,7 +893,7 @@ impl GraphicsState{
|
||||
let mut encoder=device.create_command_encoder(&wgpu::CommandEncoderDescriptor{label:None});
|
||||
|
||||
// update rotation
|
||||
let camera_uniforms=self.camera.to_uniform_data(physics_output.extrapolate(crate::physics::MouseState{pos:mouse_pos,time:predicted_time}));
|
||||
let camera_uniforms=self.camera.to_uniform_data(physics_output.extrapolate(strafesnet_common::mouse::MouseState{pos:mouse_pos,time:predicted_time}));
|
||||
self.staging_belt
|
||||
.write_buffer(
|
||||
&mut encoder,
|
||||
|
@ -4,8 +4,7 @@ pub enum Instruction{
|
||||
Render(crate::physics::PhysicsOutputState,integer::Time,glam::IVec2),
|
||||
//UpdateModel(crate::graphics::GraphicsModelUpdate),
|
||||
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
|
||||
GenerateModels(strafesnet_common::map::CompleteMap),
|
||||
ClearModels,
|
||||
ChangeMap(strafesnet_common::map::CompleteMap),
|
||||
}
|
||||
|
||||
//Ideally the graphics thread worker description is:
|
||||
@ -27,11 +26,9 @@ pub fn new<'a>(
|
||||
let mut resize=None;
|
||||
crate::compat_worker::INWorker::new(move |ins:Instruction|{
|
||||
match ins{
|
||||
Instruction::GenerateModels(map)=>{
|
||||
graphics.generate_models(&device,&queue,&map);
|
||||
},
|
||||
Instruction::ClearModels=>{
|
||||
Instruction::ChangeMap(map)=>{
|
||||
graphics.clear();
|
||||
graphics.generate_models(&device,&queue,&map);
|
||||
},
|
||||
Instruction::Resize(size,user_settings)=>{
|
||||
resize=Some((size,user_settings));
|
||||
@ -69,4 +66,4 @@ pub fn new<'a>(
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ mod worker;
|
||||
mod physics;
|
||||
mod graphics;
|
||||
mod settings;
|
||||
mod bot_worker;
|
||||
mod face_crawler;
|
||||
mod compat_worker;
|
||||
mod model_physics;
|
||||
|
125
src/physics.rs
125
src/physics.rs
@ -5,6 +5,7 @@ use strafesnet_common::map;
|
||||
use strafesnet_common::run;
|
||||
use strafesnet_common::aabb;
|
||||
use strafesnet_common::model::{MeshId,ModelId};
|
||||
use strafesnet_common::mouse::MouseState;
|
||||
use strafesnet_common::gameplay_attributes::{self,CollisionAttributesId};
|
||||
use strafesnet_common::gameplay_modes::{self,StageId};
|
||||
use strafesnet_common::gameplay_style::{self,StyleModifiers};
|
||||
@ -13,6 +14,10 @@ use strafesnet_common::instruction::{self,InstructionEmitter,InstructionConsumer
|
||||
use strafesnet_common::integer::{self,Time,Planar64,Planar64Vec3,Planar64Mat3,Angle32,Ratio64Vec2};
|
||||
use gameplay::ModeState;
|
||||
|
||||
//external influence
|
||||
//this is how you influence the physics from outside
|
||||
use strafesnet_common::physics::Instruction as PhysicsInputInstruction;
|
||||
|
||||
//internal influence
|
||||
//when the physics asks itself what happens next, this is how it's represented
|
||||
#[derive(Debug)]
|
||||
@ -22,34 +27,6 @@ enum PhysicsInternalInstruction{
|
||||
StrafeTick,
|
||||
ReachWalkTargetVelocity,
|
||||
// Water,
|
||||
// Spawn(
|
||||
// Option<SpawnId>,
|
||||
// bool,//true = Trigger; false = teleport
|
||||
// bool,//true = Force
|
||||
// )
|
||||
}
|
||||
//external influence
|
||||
//this is how you influence the physics from outside
|
||||
#[derive(Debug)]
|
||||
pub enum PhysicsInputInstruction{
|
||||
ReplaceMouse(MouseState,MouseState),
|
||||
SetNextMouse(MouseState),
|
||||
SetMoveRight(bool),
|
||||
SetMoveUp(bool),
|
||||
SetMoveBack(bool),
|
||||
SetMoveLeft(bool),
|
||||
SetMoveDown(bool),
|
||||
SetMoveForward(bool),
|
||||
SetJump(bool),
|
||||
SetZoom(bool),
|
||||
Restart,
|
||||
Spawn(gameplay_modes::ModeId,StageId),
|
||||
Idle,
|
||||
//Idle: there were no input events, but the simulation is safe to advance to this timestep
|
||||
//for interpolation / networking / playback reasons, most playback heads will always want
|
||||
//to be 1 instruction ahead to generate the next state for interpolation.
|
||||
PracticeFly,
|
||||
SetSensitivity(Ratio64Vec2),
|
||||
}
|
||||
#[derive(Debug)]
|
||||
enum PhysicsInstruction{
|
||||
@ -78,32 +55,6 @@ impl std::ops::Neg for Body{
|
||||
}
|
||||
}
|
||||
|
||||
//hey dumbass just use a delta
|
||||
#[derive(Clone,Debug)]
|
||||
pub struct MouseState {
|
||||
pub pos: glam::IVec2,
|
||||
pub time:Time,
|
||||
}
|
||||
impl Default for MouseState{
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
time:Time::ZERO,
|
||||
pos:glam::IVec2::ZERO,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl MouseState {
|
||||
pub fn lerp(&self,target:&MouseState,time:Time)->glam::IVec2 {
|
||||
let m0=self.pos.as_i64vec2();
|
||||
let m1=target.pos.as_i64vec2();
|
||||
//these are deltas
|
||||
let t1t=(target.time-time).nanos();
|
||||
let tt0=(time-self.time).nanos();
|
||||
let dt=(target.time-self.time).nanos();
|
||||
((m0*t1t+m1*tt0)/dt).as_ivec2()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone,Debug,Default)]
|
||||
pub struct InputState{
|
||||
mouse:MouseState,
|
||||
@ -1055,28 +1006,6 @@ impl instruction::InstructionEmitter<PhysicsInternalInstruction> for PhysicsCont
|
||||
}
|
||||
}
|
||||
impl PhysicsContext{
|
||||
pub fn clear(&mut self){
|
||||
self.state.clear();
|
||||
}
|
||||
//TODO: remove non-standard interfaces to process_instruction
|
||||
pub fn load_user_settings(&mut self,user_settings:&crate::settings::UserSettings){
|
||||
self.run_input_instruction(TimedInstruction{
|
||||
time:self.state.time,
|
||||
instruction:PhysicsInputInstruction::SetSensitivity(user_settings.calculate_sensitivity()),
|
||||
});
|
||||
}
|
||||
pub fn restart(&mut self){
|
||||
self.run_input_instruction(TimedInstruction{
|
||||
time:self.state.time,
|
||||
instruction:PhysicsInputInstruction::Restart,
|
||||
});
|
||||
}
|
||||
pub fn spawn(&mut self){
|
||||
self.run_input_instruction(TimedInstruction{
|
||||
time:self.state.time,
|
||||
instruction:PhysicsInputInstruction::Spawn(gameplay_modes::ModeId::MAIN,StageId::FIRST),
|
||||
});
|
||||
}
|
||||
pub const fn output(&self)->PhysicsOutputState{
|
||||
PhysicsOutputState{
|
||||
body:self.state.body,
|
||||
@ -1088,7 +1017,9 @@ impl PhysicsContext{
|
||||
pub const fn get_next_mouse(&self)->&MouseState{
|
||||
self.state.input_state.get_next_mouse()
|
||||
}
|
||||
/// use with caution, this is the only non-instruction way to mess with physics
|
||||
pub fn generate_models(&mut self,map:&map::CompleteMap){
|
||||
self.state.clear();
|
||||
self.data.modes=map.modes.clone();
|
||||
for mode in &mut self.data.modes.modes{
|
||||
mode.denormalize_data();
|
||||
@ -1542,6 +1473,7 @@ fn atomic_input_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedI
|
||||
//the body may as well be a quantum wave function
|
||||
//as far as these instruction are concerned (they don't care where it is)
|
||||
PhysicsInputInstruction::SetSensitivity(..)
|
||||
|PhysicsInputInstruction::Reset
|
||||
|PhysicsInputInstruction::Restart
|
||||
|PhysicsInputInstruction::Spawn(..)
|
||||
|PhysicsInputInstruction::SetZoom(..)
|
||||
@ -1603,10 +1535,13 @@ fn atomic_input_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedI
|
||||
state.input_state.set_control(Controls::Zoom,s);
|
||||
b_refresh_walk_target=false;
|
||||
},
|
||||
PhysicsInputInstruction::Restart=>{
|
||||
PhysicsInputInstruction::Reset=>{
|
||||
//totally reset physics state
|
||||
state.reset_to_default();
|
||||
//spawn at start zone
|
||||
b_refresh_walk_target=false;
|
||||
},
|
||||
PhysicsInputInstruction::Restart=>{
|
||||
//teleport to start zone
|
||||
let spawn_point=data.modes.get_mode(state.mode_state.get_mode_id()).map(|mode|
|
||||
//TODO: spawn at the bottom of the start zone plus the hitbox size
|
||||
//TODO: set camera andles to face the same way as the start zone
|
||||
@ -1879,4 +1814,38 @@ mod test{
|
||||
Time::ZERO
|
||||
),None);
|
||||
}
|
||||
fn simulate(map:&str,bot:&str){
|
||||
//create physics
|
||||
let mut physics=PhysicsContext::default();
|
||||
|
||||
//load map
|
||||
let map_file=std::fs::File::open(format!("/run/media/quat/Files/Documents/map-files/verify-scripts/maps/bhop_snfm/{map}.snfm")).unwrap();
|
||||
let map=strafesnet_snf::read_map(map_file).unwrap().into_complete_map().unwrap();
|
||||
physics.generate_models(&map);
|
||||
|
||||
//load bot
|
||||
let bot_file=std::fs::File::open(format!("/home/quat/strafesnet/strafe-client/tools/debug_bots/{bot}")).unwrap();
|
||||
let instructions=strafesnet_snf::bot::read_bot_debug(bot_file).unwrap();
|
||||
|
||||
//run bot on physics
|
||||
for ins in instructions{
|
||||
physics.run_input_instruction(ins);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn simulate_bot_1(){
|
||||
simulate("5692113331","ff4940efb50f724e48eb54ce3593d88f")
|
||||
}
|
||||
#[test]
|
||||
fn simulate_bot_2(){
|
||||
simulate("5692113331","17cf70412eba16d172a67385cab5727e")
|
||||
}
|
||||
#[test]
|
||||
fn simulate_bot_3(){
|
||||
simulate("5692176057","f0fe0dc2fda5f8b59a658e82d26fc69")
|
||||
}
|
||||
#[test]
|
||||
fn simulate_bot_4(){
|
||||
simulate("5692176057","c0631c6f524eebddbf75237cac48e78e")
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
use crate::physics::{MouseState,PhysicsInputInstruction};
|
||||
use strafesnet_common::mouse::MouseState;
|
||||
use strafesnet_common::physics::Instruction as PhysicsInputInstruction;
|
||||
use strafesnet_common::integer::Time;
|
||||
use strafesnet_common::instruction::TimedInstruction;
|
||||
use strafesnet_common::timer::{Scaled,Timer,TimerState};
|
||||
use mouse_interpolator::MouseInterpolator;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum InputInstruction{
|
||||
@ -14,27 +16,52 @@ pub enum InputInstruction{
|
||||
MoveForward(bool),
|
||||
Jump(bool),
|
||||
Zoom(bool),
|
||||
Restart,
|
||||
Spawn(strafesnet_common::gameplay_modes::ModeId,strafesnet_common::gameplay_modes::StageId),
|
||||
ResetAndRestart,
|
||||
ResetAndSpawn(strafesnet_common::gameplay_modes::ModeId,strafesnet_common::gameplay_modes::StageId),
|
||||
PracticeFly,
|
||||
}
|
||||
pub enum Instruction{
|
||||
Input(InputInstruction),
|
||||
Render,
|
||||
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
|
||||
GenerateModels(strafesnet_common::map::CompleteMap),
|
||||
ClearModels,
|
||||
Resize(winit::dpi::PhysicalSize<u32>),
|
||||
ChangeMap(strafesnet_common::map::CompleteMap),
|
||||
//SetPaused is not an InputInstruction: the physics doesn't know that it's paused.
|
||||
SetPaused(bool),
|
||||
//Graphics(crate::graphics_worker::Instruction),
|
||||
}
|
||||
pub struct MouseInterpolator{
|
||||
mod mouse_interpolator{
|
||||
use super::*;
|
||||
//TODO: move this or tab
|
||||
pub struct MouseInterpolator<'a>{
|
||||
//"PlayerController"
|
||||
timeline:std::collections::VecDeque<TimedInstruction<PhysicsInputInstruction>>,
|
||||
last_mouse_time:Time,//this value is pre-transformed to simulation time
|
||||
mouse_blocking:bool,
|
||||
//????
|
||||
bot_worker:crate::worker::QNWorker<'a,crate::bot_worker::Instruction>,
|
||||
user_settings:crate::settings::UserSettings,
|
||||
//"Simulation"
|
||||
timer:Timer<Scaled>,
|
||||
physics:crate::physics::PhysicsContext,
|
||||
|
||||
}
|
||||
impl MouseInterpolator{
|
||||
fn push_mouse_instruction(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>,m:glam::IVec2){
|
||||
impl MouseInterpolator<'_>{
|
||||
pub fn new<'a>(
|
||||
physics:crate::physics::PhysicsContext,
|
||||
bot_worker:crate::worker::QNWorker<'a,crate::bot_worker::Instruction>,
|
||||
user_settings:crate::settings::UserSettings,
|
||||
)->MouseInterpolator<'a>{
|
||||
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,
|
||||
bot_worker,
|
||||
user_settings,
|
||||
}
|
||||
}
|
||||
fn push_mouse_instruction(&mut self,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{
|
||||
@ -47,7 +74,7 @@ impl MouseInterpolator{
|
||||
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},
|
||||
MouseState{time:self.last_mouse_time,pos:self.physics.get_next_mouse().pos},
|
||||
MouseState{time:self.timer.time(ins.time),pos:m}
|
||||
),
|
||||
});
|
||||
@ -56,58 +83,84 @@ impl MouseInterpolator{
|
||||
}
|
||||
self.last_mouse_time=self.timer.time(ins.time);
|
||||
}
|
||||
/// returns the mapped physics input instruction
|
||||
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
|
||||
/// may or may not mutate internal state XD!
|
||||
fn map_instruction(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>)->Option<PhysicsInputInstruction>{
|
||||
fn map_instruction(&mut self,ins:&TimedInstruction<Instruction>)->bool{
|
||||
let mut update_mouse_blocking=true;
|
||||
match &ins.instruction{
|
||||
Instruction::Input(input_instruction)=>match input_instruction{
|
||||
&InputInstruction::MoveMouse(m)=>{
|
||||
if !self.timer.is_paused(){
|
||||
self.push_mouse_instruction(physics,ins,m);
|
||||
self.push_mouse_instruction(ins,m);
|
||||
}
|
||||
None
|
||||
update_mouse_blocking=false;
|
||||
},
|
||||
&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),
|
||||
InputInstruction::PracticeFly=>Some(PhysicsInputInstruction::PracticeFly),
|
||||
&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),
|
||||
},
|
||||
//do these really need to idle the physics?
|
||||
//sending None dumps the instruction queue
|
||||
Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle),
|
||||
Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle),
|
||||
Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle),
|
||||
Instruction::Render=>Some(PhysicsInputInstruction::Idle),
|
||||
Instruction::ChangeMap(_)=>self.push(ins.time,PhysicsInputInstruction::Idle),
|
||||
Instruction::Resize(_)=>self.push(ins.time,PhysicsInputInstruction::Idle),
|
||||
Instruction::Render=>self.push(ins.time,PhysicsInputInstruction::Idle),
|
||||
&Instruction::SetPaused(paused)=>{
|
||||
if let Err(e)=self.timer.set_paused(ins.time,paused){
|
||||
println!("Cannot pause: {e}");
|
||||
}
|
||||
Some(PhysicsInputInstruction::Idle)
|
||||
self.push(ins.time,PhysicsInputInstruction::Idle);
|
||||
},
|
||||
}
|
||||
if update_mouse_blocking{
|
||||
//this returns the bool for us
|
||||
self.update_mouse_blocking(ins.time)
|
||||
}else{
|
||||
//do flush that queue
|
||||
true
|
||||
}
|
||||
}
|
||||
fn update_mouse_blocking(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>)->bool{
|
||||
/// 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;
|
||||
}
|
||||
fn update_mouse_blocking(&mut self,time:Time)->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 :(
|
||||
if Time::from_millis(10)<self.timer.time(ins.time)-physics.get_next_mouse().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(ins.time),pos:physics.get_next_mouse().pos}),
|
||||
});
|
||||
self.last_mouse_time=self.timer.time(ins.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;
|
||||
if Time::from_millis(10)<self.timer.time(time)-self.physics.get_next_mouse().time{
|
||||
self.unblock_mouse(time);
|
||||
true
|
||||
}else{
|
||||
false
|
||||
@ -115,72 +168,79 @@ impl MouseInterpolator{
|
||||
}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
|
||||
self.last_mouse_time=self.timer.time(ins.time);
|
||||
self.last_mouse_time=self.timer.time(time);
|
||||
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{
|
||||
time:self.timer.time(ins.time),
|
||||
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){
|
||||
fn empty_queue(&mut self){
|
||||
while let Some(instruction)=self.timeline.pop_front(){
|
||||
physics.run_input_instruction(instruction);
|
||||
//makeshift clone because requiring all TimedInstructions to be Clone is troublesome
|
||||
self.bot_worker.send(crate::bot_worker::Instruction::Push{
|
||||
ins:TimedInstruction{
|
||||
time:instruction.time,
|
||||
instruction:instruction.instruction.clone(),
|
||||
}
|
||||
}).unwrap();
|
||||
self.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);
|
||||
pub fn handle_instruction(&mut self,ins:&TimedInstruction<Instruction>){
|
||||
let should_empty_queue=self.map_instruction(ins);
|
||||
if should_empty_queue{
|
||||
self.empty_queue(physics);
|
||||
self.empty_queue();
|
||||
}
|
||||
}
|
||||
pub fn get_render_stuff(&self,time:Time)->(crate::physics::PhysicsOutputState,Time,glam::IVec2){
|
||||
(self.physics.output(),self.timer.time(time),self.physics.get_next_mouse().pos)
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
timer:Timer::from_state(Scaled::identity(),false),
|
||||
};
|
||||
pub fn new<'a>(
|
||||
mut graphics_worker:crate::compat_worker::INWorker<'a,crate::graphics_worker::Instruction>,
|
||||
bot_worker:crate::worker::QNWorker<'a,crate::bot_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,bot_worker,user_settings);
|
||||
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
|
||||
interpolator.handle_instruction(&mut physics,&ins);
|
||||
interpolator.handle_instruction(&ins);
|
||||
match ins.instruction{
|
||||
Instruction::Render=>{
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),interpolator.timer.time(ins.time),physics.get_next_mouse().pos)).unwrap();
|
||||
let (physics_output,time,mouse_pos)=interpolator.get_render_stuff(ins.time);
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics_output,time,mouse_pos)).unwrap();
|
||||
},
|
||||
Instruction::Resize(size,user_settings)=>{
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap();
|
||||
Instruction::Resize(size)=>{
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,interpolator.user_settings().clone())).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
|
||||
physics.spawn();
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(map)).unwrap();
|
||||
Instruction::ChangeMap(map)=>{
|
||||
interpolator.change_map(ins.time,&map);
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::ChangeMap(map)).unwrap();
|
||||
},
|
||||
Instruction::ClearModels=>{
|
||||
physics.clear();
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::ClearModels).unwrap();
|
||||
},
|
||||
_=>(),
|
||||
Instruction::Input(_)=>(),
|
||||
Instruction::SetPaused(_)=>(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
27
src/setup.rs
27
src/setup.rs
@ -213,21 +213,22 @@ pub fn setup_and_start(title:String){
|
||||
|
||||
//dedicated thread to ping request redraw back and resize the window doesn't seem logical
|
||||
|
||||
let window=crate::window::WindowContextSetup::new(&setup_context,&window);
|
||||
//the thread that spawns the physics thread
|
||||
let mut window_thread=window.into_worker(setup_context);
|
||||
std::thread::scope(|scope|{
|
||||
//the thread that spawns the physics thread
|
||||
let mut window_thread=crate::window::worker(&window,setup_context,scope);
|
||||
|
||||
if let Some(arg)=std::env::args().nth(1){
|
||||
let path=std::path::PathBuf::from(arg);
|
||||
window_thread.send(TimedInstruction{
|
||||
time:integer::Time::ZERO,
|
||||
instruction:WindowInstruction::WindowEvent(winit::event::WindowEvent::DroppedFile(path)),
|
||||
}).unwrap();
|
||||
};
|
||||
if let Some(arg)=std::env::args().nth(1){
|
||||
let path=std::path::PathBuf::from(arg);
|
||||
window_thread.send(TimedInstruction{
|
||||
time:integer::Time::ZERO,
|
||||
instruction:WindowInstruction::WindowEvent(winit::event::WindowEvent::DroppedFile(path)),
|
||||
}).unwrap();
|
||||
};
|
||||
|
||||
println!("Entering event loop...");
|
||||
let root_time=std::time::Instant::now();
|
||||
run_event_loop(event_loop,window_thread,root_time).unwrap();
|
||||
println!("Entering event loop...");
|
||||
let root_time=std::time::Instant::now();
|
||||
run_event_loop(event_loop,window_thread,root_time).unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
fn run_event_loop(
|
||||
|
@ -13,9 +13,8 @@ pub enum WindowInstruction{
|
||||
//holds thread handles to dispatch to
|
||||
struct WindowContext<'a>{
|
||||
manual_mouse_lock:bool,
|
||||
mouse:crate::physics::MouseState,//std::sync::Arc<std::sync::Mutex<>>
|
||||
mouse:strafesnet_common::mouse::MouseState,//std::sync::Arc<std::sync::Mutex<>>
|
||||
screen_size:glam::UVec2,
|
||||
user_settings:crate::settings::UserSettings,
|
||||
window:&'a winit::window::Window,
|
||||
physics_thread:crate::compat_worker::QNWorker<'a, TimedInstruction<crate::physics_worker::Instruction>>,
|
||||
}
|
||||
@ -28,10 +27,7 @@ impl WindowContext<'_>{
|
||||
match event {
|
||||
winit::event::WindowEvent::DroppedFile(path)=>{
|
||||
match crate::file::load(path.as_path()){
|
||||
Ok(map)=>{
|
||||
self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::ClearModels}).unwrap();
|
||||
self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::GenerateModels(map)}).unwrap();
|
||||
},
|
||||
Ok(map)=>self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::ChangeMap(map)}).unwrap(),
|
||||
Err(e)=>println!("Failed to load map: {e}"),
|
||||
}
|
||||
},
|
||||
@ -113,8 +109,8 @@ impl WindowContext<'_>{
|
||||
"z"=>Some(InputInstruction::Zoom(s)),
|
||||
"r"=>if s{
|
||||
//mouse needs to be reset since the position is absolute
|
||||
self.mouse=crate::physics::MouseState::default();
|
||||
Some(InputInstruction::Restart)
|
||||
self.mouse=strafesnet_common::mouse::MouseState::default();
|
||||
Some(InputInstruction::ResetAndRestart)
|
||||
}else{None},
|
||||
"f"=>if s{Some(InputInstruction::PracticeFly)}else{None},
|
||||
_=>None,
|
||||
@ -169,48 +165,28 @@ impl WindowContext<'_>{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WindowContextSetup<'a>{
|
||||
user_settings:crate::settings::UserSettings,
|
||||
window:&'a winit::window::Window,
|
||||
physics:crate::physics::PhysicsContext,
|
||||
graphics:crate::graphics::GraphicsState,
|
||||
}
|
||||
|
||||
impl<'a> WindowContextSetup<'a>{
|
||||
pub fn new(context:&crate::setup::SetupContext,window:&'a winit::window::Window)->Self{
|
||||
pub fn worker<'a>(window:&'a winit::window::Window,setup_context:crate::setup::SetupContext<'a>,scope:&'a std::thread::Scope<'a,'_>)->crate::compat_worker::QNWorker<'a,TimedInstruction<WindowInstruction>>{
|
||||
// WindowContextSetup::new
|
||||
let user_settings=crate::settings::read_user_settings();
|
||||
|
||||
let mut physics=crate::physics::PhysicsContext::default();
|
||||
physics.load_user_settings(&user_settings);
|
||||
|
||||
let mut graphics=crate::graphics::GraphicsState::new(&context.device,&context.queue,&context.config);
|
||||
let mut graphics=crate::graphics::GraphicsState::new(&setup_context.device,&setup_context.queue,&setup_context.config);
|
||||
graphics.load_user_settings(&user_settings);
|
||||
|
||||
Self{
|
||||
user_settings,
|
||||
window,
|
||||
graphics,
|
||||
physics,
|
||||
}
|
||||
}
|
||||
|
||||
fn into_context(self,setup_context:crate::setup::SetupContext<'a>)->WindowContext<'a>{
|
||||
//WindowContextSetup::into_context
|
||||
let screen_size=glam::uvec2(setup_context.config.width,setup_context.config.height);
|
||||
let graphics_thread=crate::graphics_worker::new(self.graphics,setup_context.config,setup_context.surface,setup_context.device,setup_context.queue);
|
||||
WindowContext{
|
||||
let graphics_thread=crate::graphics_worker::new(graphics,setup_context.config,setup_context.surface,setup_context.device,setup_context.queue);
|
||||
//this obviously doesn't belong here, do something about it pls
|
||||
let bot_thread=crate::bot_worker::new(scope);
|
||||
let mut window_context=WindowContext{
|
||||
manual_mouse_lock:false,
|
||||
mouse:crate::physics::MouseState::default(),
|
||||
mouse:strafesnet_common::mouse::MouseState::default(),
|
||||
//make sure to update this!!!!!
|
||||
screen_size,
|
||||
user_settings:self.user_settings,
|
||||
window:self.window,
|
||||
physics_thread:crate::physics_worker::new(self.physics,graphics_thread),
|
||||
}
|
||||
}
|
||||
window,
|
||||
physics_thread:crate::physics_worker::new(graphics_thread,bot_thread,user_settings),
|
||||
};
|
||||
|
||||
pub fn into_worker(self,setup_context:crate::setup::SetupContext<'a>)->crate::compat_worker::QNWorker<'a,TimedInstruction<WindowInstruction>>{
|
||||
let mut window_context=self.into_context(setup_context);
|
||||
//WindowContextSetup::into_worker
|
||||
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<WindowInstruction>|{
|
||||
match ins.instruction{
|
||||
WindowInstruction::RequestRedraw=>{
|
||||
@ -226,7 +202,7 @@ impl<'a> WindowContextSetup<'a>{
|
||||
window_context.physics_thread.send(
|
||||
TimedInstruction{
|
||||
time:ins.time,
|
||||
instruction:crate::physics_worker::Instruction::Resize(size,window_context.user_settings.clone())
|
||||
instruction:crate::physics_worker::Instruction::Resize(size)
|
||||
}
|
||||
).unwrap();
|
||||
}
|
||||
@ -240,5 +216,4 @@ impl<'a> WindowContextSetup<'a>{
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -190,7 +190,7 @@ mod test{
|
||||
for _ in 0..5 {
|
||||
let task = instruction::TimedInstruction{
|
||||
time:integer::Time::ZERO,
|
||||
instruction:physics::PhysicsInputInstruction::Idle,
|
||||
instruction:strafesnet_common::physics::Instruction::Idle,
|
||||
};
|
||||
worker.send(task).unwrap();
|
||||
}
|
||||
@ -204,7 +204,7 @@ mod test{
|
||||
// Send a new task
|
||||
let task = instruction::TimedInstruction{
|
||||
time:integer::Time::ZERO,
|
||||
instruction:physics::PhysicsInputInstruction::Idle,
|
||||
instruction:strafesnet_common::physics::Instruction::Idle,
|
||||
};
|
||||
worker.send(task).unwrap();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user