Files
roblox-bot-player/native-player/src/player.rs
2026-03-18 10:25:35 -07:00

116 lines
4.3 KiB
Rust

use strafesnet_common::instruction::TimedInstruction;
use strafesnet_common::session::Time as SessionTime;
use strafesnet_common::timer::TimerState;
use strafesnet_roblox_bot_player::{bot::CompleteBot,graphics::Graphics,head::{PlaybackHead,Time as PlaybackTime}};
use strafesnet_graphics::surface::Surface;
pub enum SessionControlInstruction{
SetPaused(bool),
Restart,
SkipForward,
SkipBack,
DecreaseTimescale,
IncreaseTimescale,
}
pub enum Instruction{
SessionControl(SessionControlInstruction),
Render,
Resize(winit::dpi::PhysicalSize<u32>),
ChangeMap(strafesnet_common::map::CompleteMap),
LoadReplay(strafesnet_roblox_bot_file::v0::Block),
}
fn speed_ratio(speed:i8)->strafesnet_common::integer::Ratio64{
if speed.is_negative(){
strafesnet_common::integer::Ratio64::new(4i64.pow(-speed as u32),5u64.pow(-speed as u32)).unwrap()
}else{
strafesnet_common::integer::Ratio64::new(5i64.pow(speed as u32),4u64.pow(speed as u32)).unwrap()
}
}
struct Playback{
bot:CompleteBot,
playback_head:PlaybackHead,
playback_speed:i8,
}
pub struct PlayerWorker<'a>{
graphics_thread:Graphics,
surface:Surface<'a>,
playback:Option<Playback>,
}
impl<'a> PlayerWorker<'a>{
pub fn new(
graphics_thread:Graphics,
surface:Surface<'a>,
)->Self{
Self{
graphics_thread,
surface,
playback:None,
}
}
pub fn send(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,ins:TimedInstruction<Instruction,SessionTime>){
match ins.instruction{
Instruction::SessionControl(SessionControlInstruction::SetPaused(paused))=>if let Some(playback)=&mut self.playback{
playback.playback_head.set_paused(ins.time,paused);
},
Instruction::SessionControl(SessionControlInstruction::Restart)=>if let Some(playback)=&mut self.playback{
playback.playback_head.set_time(&playback.bot,ins.time,PlaybackTime::ZERO);
},
Instruction::SessionControl(SessionControlInstruction::SkipForward)=>if let Some(playback)=&mut self.playback{
let head_time=playback.playback_head.timer().clone().into_state().0.get_time(ins.time+SessionTime::from_secs(2));
playback.playback_head.set_time(&playback.bot,ins.time,head_time);
},
Instruction::SessionControl(SessionControlInstruction::SkipBack)=>if let Some(playback)=&mut self.playback{
let head_time=playback.playback_head.timer().clone().into_state().0.get_time(ins.time-SessionTime::from_secs(2));
playback.playback_head.set_time(&playback.bot,ins.time,head_time);
},
Instruction::SessionControl(SessionControlInstruction::DecreaseTimescale)=>if let Some(playback)=&mut self.playback{
playback.playback_speed=playback.playback_speed.saturating_sub(1).max(-27);
playback.playback_head.set_scale(ins.time,speed_ratio(playback.playback_speed));
},
Instruction::SessionControl(SessionControlInstruction::IncreaseTimescale)=>if let Some(playback)=&mut self.playback{
playback.playback_speed=playback.playback_speed.saturating_add(1).min(27);
playback.playback_head.set_scale(ins.time,speed_ratio(playback.playback_speed));
},
Instruction::Render=>if let Some(playback)=&mut self.playback{
playback.playback_head.advance_time(&playback.bot,ins.time);
let (pos,angles)=playback.playback_head.get_position_angles(&playback.bot,ins.time);
//this has to go deeper somehow
let frame=self.surface.new_frame(device);
let mut encoder=device.create_command_encoder(&wgpu::CommandEncoderDescriptor{label:None});
self.graphics_thread.encode_commands(&mut encoder,frame.view(),pos,angles);
queue.submit([encoder.finish()]);
frame.present();
},
Instruction::Resize(physical_size)=>if let Some(playback)=&self.playback{
let fov_y=playback.playback_head.state().get_fov_y();
let fov_x=fov_y*physical_size.width as f64/physical_size.height as f64;
let fov=glam::vec2(fov_x as f32,fov_y as f32);
let size=glam::uvec2(physical_size.width,physical_size.height);
self.surface.configure(device,size);
self.graphics_thread.resize(device,size,fov);
},
Instruction::ChangeMap(complete_map)=>{
self.graphics_thread.change_map(device,queue,&complete_map).unwrap();
},
Instruction::LoadReplay(bot)=>{
let bot=CompleteBot::new(bot).unwrap();
let playback_head=PlaybackHead::new(&bot,SessionTime::ZERO);
self.playback=Some(Playback{
bot,
playback_head,
playback_speed:0,
});
},
}
}
}