prototype bot worker

This commit is contained in:
Quaternions 2024-08-06 11:26:41 -07:00
parent a4077ed0e3
commit 2366ca0f5f
5 changed files with 57 additions and 4 deletions

31
src/bot_worker.rs Normal file
View 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(),
}
})
}

View File

@ -5,6 +5,7 @@ mod worker;
mod physics; mod physics;
mod graphics; mod graphics;
mod settings; mod settings;
mod bot_worker;
mod face_crawler; mod face_crawler;
mod compat_worker; mod compat_worker;
mod model_physics; mod model_physics;

View File

@ -32,29 +32,33 @@ pub enum Instruction{
mod mouse_interpolator{ mod mouse_interpolator{
use super::*; use super::*;
//TODO: move this or tab //TODO: move this or tab
pub struct MouseInterpolator{ pub struct MouseInterpolator<'a>{
//"PlayerController" //"PlayerController"
user_settings:crate::settings::UserSettings, user_settings:crate::settings::UserSettings,
//"MouseInterpolator" //"MouseInterpolator"
timeline:std::collections::VecDeque<TimedInstruction<PhysicsInputInstruction>>, timeline:std::collections::VecDeque<TimedInstruction<PhysicsInputInstruction>>,
last_mouse_time:Time,//this value is pre-transformed to simulation time last_mouse_time:Time,//this value is pre-transformed to simulation time
mouse_blocking:bool, mouse_blocking:bool,
//????
bot_worker:crate::worker::QNWorker<'a,crate::bot_worker::Instruction>,
//"Simulation" //"Simulation"
timer:Timer<Scaled>, timer:Timer<Scaled>,
physics:crate::physics::PhysicsContext, physics:crate::physics::PhysicsContext,
} }
impl MouseInterpolator{ impl MouseInterpolator<'_>{
pub fn new( pub fn new<'a>(
physics:crate::physics::PhysicsContext, physics:crate::physics::PhysicsContext,
bot_worker:crate::worker::QNWorker<'a,crate::bot_worker::Instruction>,
user_settings:crate::settings::UserSettings, user_settings:crate::settings::UserSettings,
)->MouseInterpolator{ )->MouseInterpolator<'a>{
MouseInterpolator{ MouseInterpolator{
mouse_blocking:true, mouse_blocking:true,
last_mouse_time:physics.get_next_mouse().time, last_mouse_time:physics.get_next_mouse().time,
timeline:std::collections::VecDeque::new(), timeline:std::collections::VecDeque::new(),
timer:Timer::from_state(Scaled::identity(),false), timer:Timer::from_state(Scaled::identity(),false),
physics, physics,
bot_worker,
user_settings, user_settings,
} }
} }
@ -171,6 +175,13 @@ impl MouseInterpolator{
} }
fn empty_queue(&mut self){ fn empty_queue(&mut self){
while let Some(instruction)=self.timeline.pop_front(){ 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); self.physics.run_input_instruction(instruction);
} }
} }
@ -211,10 +222,12 @@ impl MouseInterpolator{
pub fn new<'a>( pub fn new<'a>(
mut graphics_worker:crate::compat_worker::INWorker<'a,crate::graphics_worker::Instruction>, mut graphics_worker:crate::compat_worker::INWorker<'a,crate::graphics_worker::Instruction>,
user_settings:crate::settings::UserSettings, user_settings:crate::settings::UserSettings,
bot_worker:crate::worker::QNWorker<'a,crate::bot_worker::Instruction>,
)->crate::compat_worker::QNWorker<'a,TimedInstruction<Instruction>>{ )->crate::compat_worker::QNWorker<'a,TimedInstruction<Instruction>>{
let physics=crate::physics::PhysicsContext::default(); let physics=crate::physics::PhysicsContext::default();
let mut interpolator=MouseInterpolator::new( let mut interpolator=MouseInterpolator::new(
physics, physics,
bot_worker,
user_settings user_settings
); );
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{ crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{

View File

@ -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 //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 //the thread that spawns the physics thread
let mut window_thread=crate::window::worker( let mut window_thread=crate::window::worker(
&window, &window,
setup_context, setup_context,
scope
); );
if let Some(arg)=std::env::args().nth(1){ if let Some(arg)=std::env::args().nth(1){
@ -230,6 +233,7 @@ pub fn setup_and_start(title:String){
println!("Entering event loop..."); println!("Entering event loop...");
let root_time=std::time::Instant::now(); let root_time=std::time::Instant::now();
run_event_loop(event_loop,window_thread,root_time).unwrap(); run_event_loop(event_loop,window_thread,root_time).unwrap();
});
} }
fn run_event_loop( fn run_event_loop(

View File

@ -168,6 +168,7 @@ impl WindowContext<'_>{
pub fn worker<'a>( pub fn worker<'a>(
window:&'a winit::window::Window, window:&'a winit::window::Window,
setup_context:crate::setup::SetupContext<'a>, setup_context:crate::setup::SetupContext<'a>,
scope:&'a std::thread::Scope<'a,'_>,
)->crate::compat_worker::QNWorker<'a,TimedInstruction<WindowInstruction>>{ )->crate::compat_worker::QNWorker<'a,TimedInstruction<WindowInstruction>>{
// WindowContextSetup::new // WindowContextSetup::new
let user_settings=crate::settings::read_user_settings(); let user_settings=crate::settings::read_user_settings();
@ -178,6 +179,8 @@ pub fn worker<'a>(
//WindowContextSetup::into_context //WindowContextSetup::into_context
let screen_size=glam::uvec2(setup_context.config.width,setup_context.config.height); 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); 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{ let mut window_context=WindowContext{
manual_mouse_lock:false, manual_mouse_lock:false,
mouse:strafesnet_common::mouse::MouseState::default(), mouse:strafesnet_common::mouse::MouseState::default(),
@ -187,6 +190,7 @@ pub fn worker<'a>(
physics_thread:crate::physics_worker::new( physics_thread:crate::physics_worker::new(
graphics_thread, graphics_thread,
user_settings, user_settings,
bot_thread,
), ),
}; };