diff --git a/src/bot_worker.rs b/src/bot_worker.rs new file mode 100644 index 0000000..29c78ad --- /dev/null +++ b/src/bot_worker.rs @@ -0,0 +1,31 @@ +use strafesnet_snf::bot::BotDebug; + +pub enum Instruction{ + //TODO: pass map id + Create, + //Delete, + Push{ + ins:strafesnet_common::instruction::TimedInstruction, + }, +} + +//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(), + } + }) +} diff --git a/src/main.rs b/src/main.rs index 337ba79..9ea3bcf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod worker; mod physics; mod graphics; mod settings; +mod bot_worker; mod face_crawler; mod compat_worker; mod model_physics; diff --git a/src/physics_worker.rs b/src/physics_worker.rs index 0a1aec3..f49da0e 100644 --- a/src/physics_worker.rs +++ b/src/physics_worker.rs @@ -32,29 +32,33 @@ pub enum Instruction{ mod mouse_interpolator{ use super::*; //TODO: move this or tab -pub struct MouseInterpolator{ +pub struct MouseInterpolator<'a>{ //"PlayerController" user_settings:crate::settings::UserSettings, //"MouseInterpolator" timeline:std::collections::VecDeque>, 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>, //"Simulation" timer:Timer, physics:crate::physics::PhysicsContext, } -impl MouseInterpolator{ - pub fn new( +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{ + )->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, } } @@ -171,6 +175,13 @@ impl MouseInterpolator{ } fn empty_queue(&mut self){ while let Some(instruction)=self.timeline.pop_front(){ + //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); } } @@ -211,10 +222,12 @@ impl MouseInterpolator{ pub fn new<'a>( mut graphics_worker:crate::compat_worker::INWorker<'a,crate::graphics_worker::Instruction>, user_settings:crate::settings::UserSettings, + bot_worker:crate::worker::QNWorker<'a,crate::bot_worker::Instruction>, )->crate::compat_worker::QNWorker<'a,TimedInstruction>{ let physics=crate::physics::PhysicsContext::default(); let mut interpolator=MouseInterpolator::new( physics, + bot_worker, user_settings ); crate::compat_worker::QNWorker::new(move |ins:TimedInstruction|{ diff --git a/src/setup.rs b/src/setup.rs index 790352d..3a2568e 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -213,10 +213,13 @@ pub fn setup_and_start(title:String){ //dedicated thread to ping request redraw back and resize the window doesn't seem logical + std::thread::scope(|scope|{ + //TODO: TAB //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){ @@ -230,6 +233,7 @@ pub fn setup_and_start(title:String){ 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( diff --git a/src/window.rs b/src/window.rs index 4320c67..c2c57e4 100644 --- a/src/window.rs +++ b/src/window.rs @@ -168,6 +168,7 @@ impl WindowContext<'_>{ 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>{ // WindowContextSetup::new let user_settings=crate::settings::read_user_settings(); @@ -178,6 +179,8 @@ pub fn worker<'a>( //WindowContextSetup::into_context let screen_size=glam::uvec2(setup_context.config.width,setup_context.config.height); 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:strafesnet_common::mouse::MouseState::default(), @@ -187,6 +190,7 @@ pub fn worker<'a>( physics_thread:crate::physics_worker::new( graphics_thread, user_settings, + bot_thread, ), };