forked from StrafesNET/roblox-bot-player
Compare commits
25 Commits
xeno-debug
...
native
| Author | SHA1 | Date | |
|---|---|---|---|
|
ed3520e5fc
|
|||
|
c95f4fb17d
|
|||
|
4d0b441ad9
|
|||
|
384be4149a
|
|||
|
86a60009dd
|
|||
|
35fb7dc014
|
|||
|
b2b682187f
|
|||
|
75dabc09c9
|
|||
|
6f805a6cd2
|
|||
|
0eee8dadbd
|
|||
|
8ac5ac8800
|
|||
|
741a109637
|
|||
|
87107f7bf3
|
|||
|
2b40cc8dd6
|
|||
|
6cbcc7efe7
|
|||
|
67389d8b13
|
|||
|
24dc416316
|
|||
|
dc0cd4c8da
|
|||
|
daa9610b89
|
|||
|
c7076ee813
|
|||
|
fc0eacf70c
|
|||
|
5a40d13cca
|
|||
|
5690da3f02
|
|||
|
fc8ad743a6
|
|||
|
1d1b9c181d
|
1363
Cargo.lock
generated
1363
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,9 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
glam = "0.31.0"
|
||||
strafesnet_common.workspace = true
|
||||
strafesnet_graphics.workspace = true
|
||||
strafesnet_roblox_bot_file.workspace = true
|
||||
strafesnet_snf.workspace = true
|
||||
wgpu = "28.0.0"
|
||||
|
||||
31
lib/src/bot.rs
Normal file
31
lib/src/bot.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
/// A loaded bot file.
|
||||
pub struct Bot{
|
||||
//Instructions
|
||||
timelines:strafesnet_roblox_bot_file::v0::Block,
|
||||
offset:f64,
|
||||
duration:f64,
|
||||
}
|
||||
impl Bot{
|
||||
pub(crate) const CAMERA_OFFSET:glam::Vec3=glam::vec3(0.0,2.0,0.0);
|
||||
pub fn new(
|
||||
data:&[u8],
|
||||
)->Result<Self,strafesnet_roblox_bot_file::v0::Error>{
|
||||
let timelines=strafesnet_roblox_bot_file::v0::read_all_to_block(std::io::Cursor::new(data))?;
|
||||
let first=timelines.output_events.first().unwrap();
|
||||
let last=timelines.output_events.last().unwrap();
|
||||
Ok(Self{
|
||||
offset:first.time,
|
||||
duration:last.time-first.time,
|
||||
timelines,
|
||||
})
|
||||
}
|
||||
pub const fn offset(&self)->f64{
|
||||
self.offset
|
||||
}
|
||||
pub const fn duration(&self)->f64{
|
||||
self.duration
|
||||
}
|
||||
pub const fn timelines(&self)->&strafesnet_roblox_bot_file::v0::Block{
|
||||
&self.timelines
|
||||
}
|
||||
}
|
||||
75
lib/src/graphics.rs
Normal file
75
lib/src/graphics.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use strafesnet_graphics::graphics::GraphicsState;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error{
|
||||
File(strafesnet_snf::Error),
|
||||
Map(strafesnet_snf::map::Error),
|
||||
}
|
||||
|
||||
pub enum Instruction{
|
||||
Render{pos:glam::Vec3,angles:glam::Vec2},
|
||||
Resize(glam::UVec2),
|
||||
ChangeMap(strafesnet_common::map::CompleteMap),
|
||||
}
|
||||
|
||||
/// The graphics state, essentially a handle to all the information on the GPU.
|
||||
pub struct Graphics<'a>{
|
||||
graphics:GraphicsState,
|
||||
config:wgpu::SurfaceConfiguration,
|
||||
surface:wgpu::Surface<'a>,
|
||||
device:wgpu::Device,
|
||||
queue:wgpu::Queue,
|
||||
}
|
||||
impl<'a> Graphics<'a>{
|
||||
pub fn new(mut graphics:GraphicsState,data:&[u8],device:wgpu::Device,queue:wgpu::Queue,surface:wgpu::Surface<'a>,config:wgpu::SurfaceConfiguration,)->Result<Self,Error>{
|
||||
let map=strafesnet_snf::read_map(std::io::Cursor::new(data))
|
||||
.map_err(Error::File)?
|
||||
.into_complete_map()
|
||||
.map_err(Error::Map)?;
|
||||
graphics.generate_models(&device,&queue,&map);
|
||||
Ok(Self{
|
||||
graphics,
|
||||
device,
|
||||
queue,
|
||||
surface,
|
||||
config,
|
||||
})
|
||||
}
|
||||
pub fn send(&mut self,ins:Instruction){
|
||||
match ins{
|
||||
Instruction::ChangeMap(map)=>{
|
||||
self.graphics.clear();
|
||||
self.graphics.generate_models(&self.device,&self.queue,&map);
|
||||
},
|
||||
Instruction::Resize(size)=>{
|
||||
println!("Resizing to {:?}",size);
|
||||
let t0=std::time::Instant::now();
|
||||
self.config.width=size.x.max(1);
|
||||
self.config.height=size.y.max(1);
|
||||
self.surface.configure(&self.device,&self.config);
|
||||
self.graphics.resize(&self.device,&self.config,glam::Vec2::ONE);
|
||||
println!("Resize took {:?}",t0.elapsed());
|
||||
}
|
||||
Instruction::Render{pos,angles}=>{
|
||||
//this has to go deeper somehow
|
||||
let frame=match self.surface.get_current_texture(){
|
||||
Ok(frame)=>frame,
|
||||
Err(_)=>{
|
||||
self.surface.configure(&self.device,&self.config);
|
||||
self.surface
|
||||
.get_current_texture()
|
||||
.expect("Failed to acquire next surface texture!")
|
||||
}
|
||||
};
|
||||
let view=frame.texture.create_view(&wgpu::TextureViewDescriptor{
|
||||
format:Some(self.config.view_formats[0]),
|
||||
..wgpu::TextureViewDescriptor::default()
|
||||
});
|
||||
|
||||
self.graphics.render(&view,&self.device,&self.queue,strafesnet_graphics::graphics::view_inv(pos,angles));
|
||||
|
||||
frame.present();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
81
lib/src/head.rs
Normal file
81
lib/src/head.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use glam::Vec3Swizzles;
|
||||
use strafesnet_common::timer::{Timer,Scaled};
|
||||
use strafesnet_common::session::{Time as SessionTime,TimeInner as SessionTimeInner};
|
||||
use strafesnet_common::physics::{Time as PhysicsTime,TimeInner as PhysicsTimeInner};
|
||||
|
||||
pub enum PlaybackInstructionInternal{
|
||||
Sound
|
||||
}
|
||||
pub enum PlaybackInstructionExternal{
|
||||
SetPaused(bool),
|
||||
Idle,
|
||||
}
|
||||
|
||||
fn vector3_to_glam(v:&strafesnet_roblox_bot_file::v0::Vector3)->glam::Vec3{
|
||||
glam::vec3(v.x,v.y,v.z)
|
||||
}
|
||||
|
||||
/// A playback context. Advance time and then generate a camera position to pass to the renderer.
|
||||
pub struct PlaybackHead{
|
||||
//"Simulation"
|
||||
event_id:usize,
|
||||
offset:f64,
|
||||
timer:Timer<Scaled<SessionTimeInner,PhysicsTimeInner>>,
|
||||
}
|
||||
impl PlaybackHead{
|
||||
pub fn new(time:SessionTime)->Self{
|
||||
let timer=Timer::unpaused(time,PhysicsTime::ZERO);
|
||||
Self{
|
||||
event_id:0,
|
||||
offset:0.0,
|
||||
timer,
|
||||
}
|
||||
}
|
||||
pub fn advance_time(&mut self,bot:&crate::bot::Bot,time:SessionTime){
|
||||
let simulation_time=self.timer.time(time);
|
||||
let mut time_float=simulation_time.get() as f64/PhysicsTime::ONE_SECOND.get() as f64+bot.offset()+self.offset;
|
||||
loop{
|
||||
match bot.timelines().output_events.get(self.event_id+1){
|
||||
Some(next_event)=>{
|
||||
if next_event.time<time_float{
|
||||
self.event_id+=1;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
},
|
||||
None=>{
|
||||
//reset playback
|
||||
self.event_id=0;
|
||||
self.offset-=bot.duration();
|
||||
time_float-=bot.duration();
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn get_position_angles(&self,bot:&crate::bot::Bot,time:SessionTime)->(glam::Vec3,glam::Vec2){
|
||||
let time=self.timer.time(time);
|
||||
let event0=&bot.timelines().output_events[self.event_id];
|
||||
let event1=&bot.timelines().output_events[self.event_id+1];
|
||||
let p0=vector3_to_glam(&event0.event.position);
|
||||
let p1=vector3_to_glam(&event1.event.position);
|
||||
// let v0=vector3_to_glam(&event0.event.velocity);
|
||||
// let v1=vector3_to_glam(&event1.event.velocity);
|
||||
// let a0=vector3_to_glam(&event0.event.acceleration);
|
||||
// let a1=vector3_to_glam(&event1.event.acceleration);
|
||||
let t0=event0.time;
|
||||
let t1=event1.time;
|
||||
let time_float=time.get() as f64/PhysicsTime::ONE_SECOND.get() as f64;
|
||||
let t=((time_float+bot.offset()+self.offset-t0)/(t1-t0)) as f32;
|
||||
let p=p0.lerp(p1,t);
|
||||
// let v=v0.lerp(v1,t);
|
||||
// let a=a0.lerp(a1,t);
|
||||
|
||||
//println!("position={:?}",p);
|
||||
|
||||
let angles0=vector3_to_glam(&event0.event.angles);
|
||||
let angles1=vector3_to_glam(&event1.event.angles);
|
||||
let angles=angles0.lerp(angles1,t);
|
||||
|
||||
(p+crate::bot::Bot::CAMERA_OFFSET,angles.yx())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
pub mod bot;
|
||||
pub mod graphics;
|
||||
pub mod head;
|
||||
pub mod surface;
|
||||
|
||||
// Create Surface
|
||||
// Create Graphics from map file and with surface as sample
|
||||
// Create bot from bot file
|
||||
// Create playback head
|
||||
// loop{
|
||||
// advance head
|
||||
// render frame
|
||||
// }
|
||||
|
||||
2
lib/src/surface.rs
Normal file
2
lib/src/surface.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
/// A render surface configuration, containing information such as resolution and pixel format
|
||||
pub struct Surface{}
|
||||
@@ -4,4 +4,12 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
pollster = "0.4.0"
|
||||
strafesnet_roblox_bot_player = { version = "0.1.0", path = "../lib" }
|
||||
wgpu = "28.0.0"
|
||||
winit = "0.30.12"
|
||||
strafesnet_common.workspace = true
|
||||
strafesnet_graphics.workspace = true
|
||||
strafesnet_roblox_bot_file.workspace = true
|
||||
strafesnet_snf.workspace = true
|
||||
glam = "0.31.0"
|
||||
|
||||
68
native-player/src/app.rs
Normal file
68
native-player/src/app.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use crate::window::Instruction;
|
||||
use strafesnet_common::integer;
|
||||
use strafesnet_common::instruction::TimedInstruction;
|
||||
use crate::window::WindowContext;
|
||||
|
||||
pub struct App<'a>{
|
||||
root_time:std::time::Instant,
|
||||
window_thread:WindowContext<'a>,
|
||||
}
|
||||
impl<'a> App<'a>{
|
||||
pub fn new(
|
||||
root_time:std::time::Instant,
|
||||
window_thread:WindowContext<'a>,
|
||||
)->App<'a>{
|
||||
Self{
|
||||
root_time,
|
||||
window_thread,
|
||||
}
|
||||
}
|
||||
fn send_timed_instruction(&mut self,instruction:Instruction){
|
||||
let time=integer::Time::from_nanos(self.root_time.elapsed().as_nanos() as i64);
|
||||
self.window_thread.send(TimedInstruction{time,instruction});
|
||||
}
|
||||
}
|
||||
impl winit::application::ApplicationHandler for App<'_>{
|
||||
fn resumed(&mut self,_event_loop:&winit::event_loop::ActiveEventLoop){
|
||||
//
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
&mut self,
|
||||
event_loop:&winit::event_loop::ActiveEventLoop,
|
||||
_window_id:winit::window::WindowId,
|
||||
event:winit::event::WindowEvent,
|
||||
){
|
||||
match event{
|
||||
winit::event::WindowEvent::KeyboardInput{
|
||||
event:winit::event::KeyEvent{
|
||||
logical_key:winit::keyboard::Key::Named(winit::keyboard::NamedKey::Escape),
|
||||
state:winit::event::ElementState::Pressed,
|
||||
..
|
||||
},
|
||||
..
|
||||
}
|
||||
|winit::event::WindowEvent::CloseRequested=>{
|
||||
event_loop.exit();
|
||||
},
|
||||
_=>(),
|
||||
}
|
||||
self.send_timed_instruction(Instruction::WindowEvent(event));
|
||||
}
|
||||
|
||||
fn device_event(
|
||||
&mut self,
|
||||
_event_loop:&winit::event_loop::ActiveEventLoop,
|
||||
_device_id:winit::event::DeviceId,
|
||||
event:winit::event::DeviceEvent,
|
||||
){
|
||||
self.send_timed_instruction(Instruction::DeviceEvent(event));
|
||||
}
|
||||
|
||||
fn about_to_wait(
|
||||
&mut self,
|
||||
_event_loop:&winit::event_loop::ActiveEventLoop
|
||||
){
|
||||
self.send_timed_instruction(Instruction::WindowEvent(winit::event::WindowEvent::RedrawRequested));
|
||||
}
|
||||
}
|
||||
116
native-player/src/file.rs
Normal file
116
native-player/src/file.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
use std::io::Read;
|
||||
|
||||
#[cfg(any(feature="roblox",feature="source"))]
|
||||
use strafesnet_deferred_loader::deferred_loader::LoadFailureMode;
|
||||
|
||||
#[expect(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub enum ReadError{
|
||||
#[cfg(feature="roblox")]
|
||||
Roblox(strafesnet_rbx_loader::ReadError),
|
||||
#[cfg(feature="source")]
|
||||
Source(strafesnet_bsp_loader::ReadError),
|
||||
#[cfg(feature="snf")]
|
||||
StrafesNET(strafesnet_snf::Error),
|
||||
#[cfg(feature="snf")]
|
||||
StrafesNETMap(strafesnet_snf::map::Error),
|
||||
#[cfg(feature="snf")]
|
||||
StrafesNETBot(strafesnet_snf::bot::Error),
|
||||
Io(std::io::Error),
|
||||
UnknownFileFormat,
|
||||
}
|
||||
impl std::fmt::Display for ReadError{
|
||||
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
||||
write!(f,"{self:?}")
|
||||
}
|
||||
}
|
||||
impl std::error::Error for ReadError{}
|
||||
|
||||
pub enum ReadFormat{
|
||||
#[cfg(feature="roblox")]
|
||||
Roblox(strafesnet_rbx_loader::Model),
|
||||
#[cfg(feature="source")]
|
||||
Source(strafesnet_bsp_loader::Bsp),
|
||||
#[cfg(feature="snf")]
|
||||
SNFM(strafesnet_common::map::CompleteMap),
|
||||
#[cfg(feature="snf")]
|
||||
SNFB(strafesnet_snf::bot::Segment),
|
||||
}
|
||||
|
||||
pub fn read<R:Read+std::io::Seek>(input:R)->Result<ReadFormat,ReadError>{
|
||||
let mut buf=std::io::BufReader::new(input);
|
||||
let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?[0..4].to_owned();
|
||||
// reading the entire file is way faster than round tripping the disk constantly
|
||||
let mut entire_file=Vec::new();
|
||||
buf.read_to_end(&mut entire_file).map_err(ReadError::Io)?;
|
||||
let cursor=std::io::Cursor::new(entire_file);
|
||||
match peek.as_slice(){
|
||||
#[cfg(feature="roblox")]
|
||||
b"<rob"=>Ok(ReadFormat::Roblox(strafesnet_rbx_loader::read(cursor).map_err(ReadError::Roblox)?)),
|
||||
#[cfg(feature="source")]
|
||||
b"VBSP"=>Ok(ReadFormat::Source(strafesnet_bsp_loader::read(cursor).map_err(ReadError::Source)?)),
|
||||
#[cfg(feature="snf")]
|
||||
b"SNFM"=>Ok(ReadFormat::SNFM(
|
||||
strafesnet_snf::read_map(cursor).map_err(ReadError::StrafesNET)?
|
||||
.into_complete_map().map_err(ReadError::StrafesNETMap)?
|
||||
)),
|
||||
#[cfg(feature="snf")]
|
||||
b"SNFB"=>Ok(ReadFormat::SNFB(
|
||||
strafesnet_snf::read_bot(cursor).map_err(ReadError::StrafesNET)?
|
||||
.read_all().map_err(ReadError::StrafesNETBot)?
|
||||
)),
|
||||
_=>Err(ReadError::UnknownFileFormat),
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub enum LoadError{
|
||||
ReadError(ReadError),
|
||||
File(std::io::Error),
|
||||
#[cfg(feature="roblox")]
|
||||
LoadRoblox(strafesnet_rbx_loader::LoadError),
|
||||
#[cfg(feature="source")]
|
||||
LoadSource(strafesnet_bsp_loader::LoadError),
|
||||
}
|
||||
impl std::fmt::Display for LoadError{
|
||||
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
||||
write!(f,"{self:?}")
|
||||
}
|
||||
}
|
||||
impl std::error::Error for LoadError{}
|
||||
|
||||
pub enum LoadFormat{
|
||||
#[cfg(any(feature="snf",feature="roblox",feature="source"))]
|
||||
Map(strafesnet_common::map::CompleteMap),
|
||||
#[cfg(feature="snf")]
|
||||
Bot(strafesnet_snf::bot::Segment),
|
||||
}
|
||||
|
||||
pub fn load<P:AsRef<std::path::Path>>(path:P)->Result<LoadFormat,LoadError>{
|
||||
//blocking because it's simpler...
|
||||
let file=std::fs::File::open(path).map_err(LoadError::File)?;
|
||||
match read(file).map_err(LoadError::ReadError)?{
|
||||
#[cfg(feature="snf")]
|
||||
ReadFormat::SNFB(bot)=>Ok(LoadFormat::Bot(bot)),
|
||||
#[cfg(feature="snf")]
|
||||
ReadFormat::SNFM(map)=>Ok(LoadFormat::Map(map)),
|
||||
#[cfg(feature="roblox")]
|
||||
ReadFormat::Roblox(model)=>{
|
||||
let mut place=strafesnet_rbx_loader::Place::from(model);
|
||||
let script_errors=place.run_scripts().unwrap();
|
||||
for error in script_errors{
|
||||
println!("Script error: {error}");
|
||||
}
|
||||
let (map,errors)=place.to_snf(LoadFailureMode::DefaultToNone).map_err(LoadError::LoadRoblox)?;
|
||||
if errors.count()!=0{
|
||||
print!("Errors encountered while loading the map:\n{}",errors);
|
||||
}
|
||||
Ok(LoadFormat::Map(map))
|
||||
},
|
||||
#[cfg(feature="source")]
|
||||
ReadFormat::Source(bsp)=>Ok(LoadFormat::Map(
|
||||
bsp.to_snf(LoadFailureMode::DefaultToNone,&[]).map_err(LoadError::LoadSource)?
|
||||
)),
|
||||
}
|
||||
}
|
||||
@@ -1 +1,12 @@
|
||||
fn main() {}
|
||||
mod app;
|
||||
mod file;
|
||||
mod player;
|
||||
mod setup;
|
||||
mod window;
|
||||
|
||||
|
||||
const TITLE:&'static str=concat!("Roblox Bot Player v",env!("CARGO_PKG_VERSION"));
|
||||
|
||||
fn main(){
|
||||
setup::setup_and_start(TITLE);
|
||||
}
|
||||
|
||||
53
native-player/src/player.rs
Normal file
53
native-player/src/player.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use strafesnet_common::instruction::TimedInstruction;
|
||||
use strafesnet_common::session::Time as SessionTime;
|
||||
use strafesnet_roblox_bot_player::{bot::Bot,graphics::Graphics,head::PlaybackHead};
|
||||
|
||||
pub enum SessionControlInstruction{
|
||||
SetPaused(bool),
|
||||
}
|
||||
pub enum SessionPlaybackInstruction{
|
||||
SkipForward,
|
||||
SkipBack,
|
||||
DecreaseTimescale,
|
||||
IncreaseTimescale,
|
||||
}
|
||||
|
||||
pub enum Instruction{
|
||||
SessionControl(SessionControlInstruction),
|
||||
SessionPlayback(SessionPlaybackInstruction),
|
||||
Render,
|
||||
Resize(winit::dpi::PhysicalSize<u32>),
|
||||
}
|
||||
|
||||
pub struct PlayerWorker<'a>{
|
||||
graphics_thread:Graphics<'a>,
|
||||
bot:Bot,
|
||||
playback_head:PlaybackHead,
|
||||
}
|
||||
impl<'a> PlayerWorker<'a>{
|
||||
pub fn new(
|
||||
bot:Bot,
|
||||
graphics_thread:Graphics<'a>,
|
||||
)->Self{
|
||||
let playback_head=PlaybackHead::new(SessionTime::ZERO);
|
||||
Self{
|
||||
bot,
|
||||
graphics_thread,
|
||||
playback_head,
|
||||
}
|
||||
}
|
||||
pub fn send(&mut self,ins:TimedInstruction<Instruction,SessionTime>){
|
||||
match ins.instruction{
|
||||
Instruction::SessionControl(session_control_instruction)=>{},
|
||||
Instruction::SessionPlayback(session_playback_instruction)=>{},
|
||||
Instruction::Render=>{
|
||||
self.playback_head.advance_time(&self.bot,ins.time);
|
||||
let (pos,angles)=self.playback_head.get_position_angles(&self.bot,ins.time);
|
||||
self.graphics_thread.send(strafesnet_roblox_bot_player::graphics::Instruction::Render{pos,angles});
|
||||
},
|
||||
Instruction::Resize(physical_size)=>{
|
||||
self.graphics_thread.send(strafesnet_roblox_bot_player::graphics::Instruction::Resize(glam::uvec2(physical_size.width,physical_size.height)));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
199
native-player/src/setup.rs
Normal file
199
native-player/src/setup.rs
Normal file
@@ -0,0 +1,199 @@
|
||||
fn optional_features()->wgpu::Features{
|
||||
wgpu::Features::TEXTURE_COMPRESSION_ASTC
|
||||
|wgpu::Features::TEXTURE_COMPRESSION_ETC2
|
||||
}
|
||||
fn required_features()->wgpu::Features{
|
||||
wgpu::Features::TEXTURE_COMPRESSION_BC
|
||||
}
|
||||
fn required_downlevel_capabilities()->wgpu::DownlevelCapabilities{
|
||||
wgpu::DownlevelCapabilities{
|
||||
flags:wgpu::DownlevelFlags::empty(),
|
||||
shader_model:wgpu::ShaderModel::Sm5,
|
||||
..wgpu::DownlevelCapabilities::default()
|
||||
}
|
||||
}
|
||||
|
||||
struct SetupContextPartial1{
|
||||
backends:wgpu::Backends,
|
||||
instance:wgpu::Instance,
|
||||
}
|
||||
fn create_window(title:&str,event_loop:&winit::event_loop::EventLoop<()>)->Result<winit::window::Window,winit::error::OsError>{
|
||||
let mut attr=winit::window::WindowAttributes::default();
|
||||
attr=attr.with_title(title);
|
||||
event_loop.create_window(attr)
|
||||
}
|
||||
fn create_instance()->SetupContextPartial1{
|
||||
let backends=wgpu::Backends::from_env().unwrap_or_default();
|
||||
SetupContextPartial1{
|
||||
backends,
|
||||
instance:Default::default(),
|
||||
}
|
||||
}
|
||||
impl SetupContextPartial1{
|
||||
fn create_surface<'a>(self,window:&'a winit::window::Window)->Result<SetupContextPartial2<'a>,wgpu::CreateSurfaceError>{
|
||||
Ok(SetupContextPartial2{
|
||||
backends:self.backends,
|
||||
surface:self.instance.create_surface(window)?,
|
||||
instance:self.instance,
|
||||
})
|
||||
}
|
||||
}
|
||||
struct SetupContextPartial2<'a>{
|
||||
backends:wgpu::Backends,
|
||||
instance:wgpu::Instance,
|
||||
surface:wgpu::Surface<'a>,
|
||||
}
|
||||
impl<'a> SetupContextPartial2<'a>{
|
||||
fn pick_adapter(self)->SetupContextPartial3<'a>{
|
||||
//TODO: prefer adapter that implements optional features
|
||||
//let optional_features=optional_features();
|
||||
let required_features=required_features();
|
||||
|
||||
//no helper function smh gotta write it myself
|
||||
let adapters=pollster::block_on(self.instance.enumerate_adapters(self.backends));
|
||||
|
||||
let chosen_adapter=adapters.into_iter()
|
||||
// reverse because we want to select adapters that appear first in ties,
|
||||
// and max_by_key selects the last equal element in the iterator.
|
||||
.rev()
|
||||
.filter(|adapter|
|
||||
adapter.is_surface_supported(&self.surface)
|
||||
&&adapter.features().contains(required_features)
|
||||
)
|
||||
.max_by_key(|adapter|match adapter.get_info().device_type{
|
||||
wgpu::DeviceType::IntegratedGpu=>3,
|
||||
wgpu::DeviceType::DiscreteGpu=>4,
|
||||
wgpu::DeviceType::VirtualGpu=>2,
|
||||
wgpu::DeviceType::Other|wgpu::DeviceType::Cpu=>1,
|
||||
});
|
||||
|
||||
let adapter=chosen_adapter.expect("No suitable GPU adapters found on the system!");
|
||||
|
||||
let adapter_info=adapter.get_info();
|
||||
println!("Using {} ({:?})", adapter_info.name, adapter_info.backend);
|
||||
|
||||
let required_downlevel_capabilities=required_downlevel_capabilities();
|
||||
let downlevel_capabilities=adapter.get_downlevel_capabilities();
|
||||
assert!(
|
||||
downlevel_capabilities.shader_model >= required_downlevel_capabilities.shader_model,
|
||||
"Adapter does not support the minimum shader model required to run this example: {:?}",
|
||||
required_downlevel_capabilities.shader_model
|
||||
);
|
||||
assert!(
|
||||
downlevel_capabilities
|
||||
.flags
|
||||
.contains(required_downlevel_capabilities.flags),
|
||||
"Adapter does not support the downlevel capabilities required to run this example: {:?}",
|
||||
required_downlevel_capabilities.flags - downlevel_capabilities.flags
|
||||
);
|
||||
SetupContextPartial3{
|
||||
surface:self.surface,
|
||||
adapter,
|
||||
}
|
||||
}
|
||||
}
|
||||
struct SetupContextPartial3<'a>{
|
||||
surface:wgpu::Surface<'a>,
|
||||
adapter:wgpu::Adapter,
|
||||
}
|
||||
impl<'a> SetupContextPartial3<'a>{
|
||||
fn request_device(self)->SetupContextPartial4<'a>{
|
||||
let optional_features=optional_features();
|
||||
let required_features=required_features();
|
||||
|
||||
// Make sure we use the texture resolution limits from the adapter, so we can support images the size of the surface.
|
||||
let needed_limits=strafesnet_graphics::graphics::required_limits().using_resolution(self.adapter.limits());
|
||||
|
||||
let (device, queue)=pollster::block_on(self.adapter
|
||||
.request_device(
|
||||
&wgpu::DeviceDescriptor{
|
||||
label:None,
|
||||
required_features:(optional_features&self.adapter.features())|required_features,
|
||||
required_limits:needed_limits,
|
||||
memory_hints:wgpu::MemoryHints::Performance,
|
||||
trace:wgpu::Trace::Off,
|
||||
experimental_features:wgpu::ExperimentalFeatures::disabled(),
|
||||
},
|
||||
))
|
||||
.expect("Unable to find a suitable GPU adapter!");
|
||||
|
||||
SetupContextPartial4{
|
||||
surface:self.surface,
|
||||
adapter:self.adapter,
|
||||
device,
|
||||
queue,
|
||||
}
|
||||
}
|
||||
}
|
||||
struct SetupContextPartial4<'a>{
|
||||
surface:wgpu::Surface<'a>,
|
||||
adapter:wgpu::Adapter,
|
||||
device:wgpu::Device,
|
||||
queue:wgpu::Queue,
|
||||
}
|
||||
impl<'a> SetupContextPartial4<'a>{
|
||||
fn configure_surface(self,size:&'a winit::dpi::PhysicalSize<u32>)->SetupContext<'a>{
|
||||
let mut config=self.surface
|
||||
.get_default_config(&self.adapter, size.width, size.height)
|
||||
.expect("Surface isn't supported by the adapter.");
|
||||
let surface_view_format=config.format.add_srgb_suffix();
|
||||
config.view_formats.push(surface_view_format);
|
||||
config.present_mode=wgpu::PresentMode::AutoNoVsync;
|
||||
self.surface.configure(&self.device, &config);
|
||||
|
||||
SetupContext{
|
||||
surface:self.surface,
|
||||
device:self.device,
|
||||
queue:self.queue,
|
||||
config,
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct SetupContext<'a>{
|
||||
pub surface:wgpu::Surface<'a>,
|
||||
pub device:wgpu::Device,
|
||||
pub queue:wgpu::Queue,
|
||||
pub config:wgpu::SurfaceConfiguration,
|
||||
}
|
||||
|
||||
pub fn setup_and_start(title:&str){
|
||||
let event_loop=winit::event_loop::EventLoop::new().unwrap();
|
||||
|
||||
println!("Initializing the surface...");
|
||||
|
||||
let partial_1=create_instance();
|
||||
|
||||
let window=create_window(title,&event_loop).unwrap();
|
||||
|
||||
let partial_2=partial_1.create_surface(&window).unwrap();
|
||||
|
||||
let partial_3=partial_2.pick_adapter();
|
||||
|
||||
let partial_4=partial_3.request_device();
|
||||
|
||||
let size=window.inner_size();
|
||||
|
||||
let setup_context=partial_4.configure_surface(&size);
|
||||
|
||||
//dedicated thread to ping request redraw back and resize the window doesn't seem logical
|
||||
|
||||
//the thread that spawns the physics thread
|
||||
let mut window_thread=crate::window::WindowContext::new(
|
||||
&window,
|
||||
setup_context,
|
||||
);
|
||||
|
||||
for arg in std::env::args().skip(1){
|
||||
window_thread.send(strafesnet_common::instruction::TimedInstruction{
|
||||
time:strafesnet_common::integer::Time::ZERO,
|
||||
instruction:crate::window::Instruction::WindowEvent(winit::event::WindowEvent::DroppedFile(arg.into())),
|
||||
});
|
||||
};
|
||||
|
||||
println!("Entering event loop...");
|
||||
let mut app=crate::app::App::new(
|
||||
std::time::Instant::now(),
|
||||
window_thread
|
||||
);
|
||||
event_loop.run_app(&mut app).unwrap();
|
||||
}
|
||||
222
native-player/src/window.rs
Normal file
222
native-player/src/window.rs
Normal file
@@ -0,0 +1,222 @@
|
||||
use strafesnet_common::instruction::TimedInstruction;
|
||||
use strafesnet_common::session::Time as SessionTime;
|
||||
use strafesnet_common::physics::{MiscInstruction,SetControlInstruction};
|
||||
use crate::file::LoadFormat;
|
||||
use crate::player::{PlayerWorker,Instruction as PhysicsWorkerInstruction,SessionControlInstruction,SessionPlaybackInstruction};
|
||||
|
||||
pub enum Instruction{
|
||||
WindowEvent(winit::event::WindowEvent),
|
||||
DeviceEvent(winit::event::DeviceEvent),
|
||||
}
|
||||
|
||||
//holds thread handles to dispatch to
|
||||
pub struct WindowContext<'a>{
|
||||
manual_mouse_lock:bool,
|
||||
mouse_pos:glam::DVec2,
|
||||
simulation_paused:bool,
|
||||
screen_size:glam::UVec2,
|
||||
window:&'a winit::window::Window,
|
||||
physics_thread:PlayerWorker<'a>,
|
||||
}
|
||||
|
||||
impl WindowContext<'_>{
|
||||
fn get_middle_of_screen(&self)->winit::dpi::PhysicalPosition<u32>{
|
||||
winit::dpi::PhysicalPosition::new(self.screen_size.x/2,self.screen_size.y/2)
|
||||
}
|
||||
fn free_mouse(&mut self){
|
||||
self.manual_mouse_lock=false;
|
||||
match self.window.set_cursor_position(self.get_middle_of_screen()){
|
||||
Ok(())=>(),
|
||||
Err(e)=>println!("Could not set cursor position: {:?}",e),
|
||||
}
|
||||
match self.window.set_cursor_grab(winit::window::CursorGrabMode::None){
|
||||
Ok(())=>(),
|
||||
Err(e)=>println!("Could not release cursor: {:?}",e),
|
||||
}
|
||||
self.window.set_cursor_visible(true);
|
||||
}
|
||||
fn lock_mouse(&mut self){
|
||||
//if cursor is outside window don't lock but apparently there's no get pos function
|
||||
//let pos=window.get_cursor_pos();
|
||||
match self.window.set_cursor_grab(winit::window::CursorGrabMode::Locked){
|
||||
Ok(())=>(),
|
||||
Err(_)=>{
|
||||
match self.window.set_cursor_grab(winit::window::CursorGrabMode::Confined){
|
||||
Ok(())=>(),
|
||||
Err(e)=>{
|
||||
self.manual_mouse_lock=true;
|
||||
println!("Could not confine cursor: {:?}",e)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
self.window.set_cursor_visible(false);
|
||||
}
|
||||
fn window_event(&mut self,time:SessionTime,event:winit::event::WindowEvent){
|
||||
match event{
|
||||
winit::event::WindowEvent::DroppedFile(path)=>{
|
||||
match crate::file::load(path.as_path()){
|
||||
// Ok(LoadFormat::Map(map))=>self.physics_thread.send(TimedInstruction{time,instruction:PhysicsWorkerInstruction::ChangeMap(map)}),
|
||||
// Ok(LoadFormat::Bot(bot))=>self.physics_thread.send(TimedInstruction{time,instruction:PhysicsWorkerInstruction::LoadReplay(bot)}),
|
||||
Err(e)=>println!("Failed to load file: {e}"),
|
||||
}
|
||||
},
|
||||
winit::event::WindowEvent::Focused(state)=>{
|
||||
// don't unpause if manually paused
|
||||
if self.simulation_paused{
|
||||
return;
|
||||
}
|
||||
//pause unpause
|
||||
self.physics_thread.send(TimedInstruction{
|
||||
time,
|
||||
instruction:PhysicsWorkerInstruction::SessionControl(SessionControlInstruction::SetPaused(!state)),
|
||||
});
|
||||
//recalculate pressed keys on focus
|
||||
},
|
||||
winit::event::WindowEvent::KeyboardInput{
|
||||
event:winit::event::KeyEvent{state,logical_key,repeat:false,..},
|
||||
..
|
||||
}=>{
|
||||
match (logical_key,state){
|
||||
(winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab),winit::event::ElementState::Pressed)=>self.free_mouse(),
|
||||
(winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab),winit::event::ElementState::Released)=>self.lock_mouse(),
|
||||
(winit::keyboard::Key::Named(winit::keyboard::NamedKey::F11),winit::event::ElementState::Pressed)=>{
|
||||
if self.window.fullscreen().is_some(){
|
||||
self.window.set_fullscreen(None);
|
||||
}else{
|
||||
self.window.set_fullscreen(Some(winit::window::Fullscreen::Borderless(None)));
|
||||
}
|
||||
},
|
||||
(winit::keyboard::Key::Named(winit::keyboard::NamedKey::Escape),winit::event::ElementState::Pressed)=>{
|
||||
self.manual_mouse_lock=false;
|
||||
match self.window.set_cursor_grab(winit::window::CursorGrabMode::None){
|
||||
Ok(())=>(),
|
||||
Err(e)=>println!("Could not release cursor: {:?}",e),
|
||||
}
|
||||
self.window.set_cursor_visible(true);
|
||||
},
|
||||
(keycode,state)=>{
|
||||
let s=state.is_pressed();
|
||||
|
||||
// internal variants for this scope
|
||||
enum SessionInstructionSubset{
|
||||
Control(SessionControlInstruction),
|
||||
Playback(SessionPlaybackInstruction),
|
||||
}
|
||||
macro_rules! session_ctrl{
|
||||
($variant:ident,$state:expr)=>{
|
||||
s.then_some(SessionInstructionSubset::Control(SessionControlInstruction::$variant))
|
||||
};
|
||||
}
|
||||
macro_rules! session_playback{
|
||||
($variant:ident,$state:expr)=>{
|
||||
s.then_some(SessionInstructionSubset::Playback(SessionPlaybackInstruction::$variant))
|
||||
};
|
||||
}
|
||||
impl From<SessionInstructionSubset> for PhysicsWorkerInstruction{
|
||||
fn from(value:SessionInstructionSubset)->Self{
|
||||
match value{
|
||||
SessionInstructionSubset::Control(session_control_instruction)=>PhysicsWorkerInstruction::SessionControl(session_control_instruction),
|
||||
SessionInstructionSubset::Playback(session_playback_instruction)=>PhysicsWorkerInstruction::SessionPlayback(session_playback_instruction),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(session_instruction)=match keycode{
|
||||
// TODO: bind system so playback pausing can use spacebar
|
||||
winit::keyboard::Key::Named(winit::keyboard::NamedKey::Enter)=>if s{
|
||||
let paused=!self.simulation_paused;
|
||||
self.simulation_paused=paused;
|
||||
if paused{
|
||||
self.free_mouse();
|
||||
}else{
|
||||
self.lock_mouse();
|
||||
}
|
||||
Some(SessionInstructionSubset::Control(SessionControlInstruction::SetPaused(paused)))
|
||||
}else{None},
|
||||
winit::keyboard::Key::Named(winit::keyboard::NamedKey::ArrowUp)=>session_playback!(IncreaseTimescale,s),
|
||||
winit::keyboard::Key::Named(winit::keyboard::NamedKey::ArrowDown)=>session_playback!(DecreaseTimescale,s),
|
||||
winit::keyboard::Key::Named(winit::keyboard::NamedKey::ArrowLeft)=>session_playback!(SkipBack,s),
|
||||
winit::keyboard::Key::Named(winit::keyboard::NamedKey::ArrowRight)=>session_playback!(SkipForward,s),
|
||||
winit::keyboard::Key::Character(key)=>match key.as_str(){
|
||||
// "R"|"r"=>s.then(||{
|
||||
// //mouse needs to be reset since the position is absolute
|
||||
// self.mouse_pos=glam::DVec2::ZERO;
|
||||
// SessionInstructionSubset::Input(SessionInputInstruction::Mode(session::ImplicitModeInstruction::ResetAndRestart))
|
||||
// }),
|
||||
_=>None,
|
||||
},
|
||||
_=>None,
|
||||
}{
|
||||
self.physics_thread.send(TimedInstruction{
|
||||
time,
|
||||
instruction:session_instruction.into(),
|
||||
});
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
winit::event::WindowEvent::Resized(size)=>{
|
||||
self.physics_thread.send(
|
||||
TimedInstruction{
|
||||
time,
|
||||
instruction:PhysicsWorkerInstruction::Resize(size)
|
||||
}
|
||||
);
|
||||
},
|
||||
winit::event::WindowEvent::RedrawRequested=>{
|
||||
self.window.request_redraw();
|
||||
self.physics_thread.send(
|
||||
TimedInstruction{
|
||||
time,
|
||||
instruction:PhysicsWorkerInstruction::Render
|
||||
}
|
||||
);
|
||||
},
|
||||
_=>(),
|
||||
}
|
||||
}
|
||||
|
||||
fn device_event(&mut self,time:SessionTime,event:winit::event::DeviceEvent){
|
||||
}
|
||||
|
||||
pub fn send(&mut self,ins:TimedInstruction<Instruction,SessionTime>){
|
||||
match ins.instruction{
|
||||
Instruction::WindowEvent(window_event)=>{
|
||||
self.window_event(ins.time,window_event);
|
||||
},
|
||||
Instruction::DeviceEvent(device_event)=>{
|
||||
self.device_event(ins.time,device_event);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new<'a>(
|
||||
window:&'a winit::window::Window,
|
||||
setup_context:crate::setup::SetupContext<'a>,
|
||||
)->WindowContext<'a>{
|
||||
// WindowContextSetup::new
|
||||
|
||||
let mut graphics=strafesnet_graphics::graphics::GraphicsState::new(&setup_context.device,&setup_context.queue,&setup_context.config);
|
||||
|
||||
//WindowContextSetup::into_context
|
||||
let screen_size=glam::uvec2(setup_context.config.width,setup_context.config.height);
|
||||
graphics.resize(&setup_context.device,&setup_context.config,glam::Vec2::ONE);
|
||||
let bot=include_bytes!("../../web-demo/bhop_marble_7cf33a64-7120-4514-b9fa-4fe29d9523d.qbot");
|
||||
let map=include_bytes!("../../web-demo/bhop_marble_5692093612.snfm");
|
||||
let graphics=strafesnet_roblox_bot_player::graphics::Graphics::new(graphics,map,setup_context.device,setup_context.queue,setup_context.surface,setup_context.config).unwrap();
|
||||
let bot=strafesnet_roblox_bot_player::bot::Bot::new(bot).unwrap();
|
||||
WindowContext{
|
||||
manual_mouse_lock:false,
|
||||
mouse_pos:glam::DVec2::ZERO,
|
||||
simulation_paused:false,
|
||||
//make sure to update this!!!!!
|
||||
screen_size,
|
||||
window,
|
||||
physics_thread:crate::player::PlayerWorker::new(
|
||||
bot,
|
||||
graphics,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
2
web-demo/.gitattributes
vendored
Normal file
2
web-demo/.gitattributes
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
bhop_marble_7cf33a64-7120-4514-b9fa-4fe29d9523d.qbot filter=lfs diff=lfs merge=lfs -text
|
||||
bhop_marble_5692093612.snfm filter=lfs diff=lfs merge=lfs -text
|
||||
BIN
web-demo/bhop_marble_5692093612.snfm
LFS
Normal file
BIN
web-demo/bhop_marble_5692093612.snfm
LFS
Normal file
Binary file not shown.
BIN
web-demo/bhop_marble_7cf33a64-7120-4514-b9fa-4fe29d9523d.qbot
LFS
Normal file
BIN
web-demo/bhop_marble_7cf33a64-7120-4514-b9fa-4fe29d9523d.qbot
LFS
Normal file
Binary file not shown.
47
web-demo/index.html
Normal file
47
web-demo/index.html
Normal file
@@ -0,0 +1,47 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta
|
||||
name="viewport"
|
||||
content="width=device-width, height=device-height, initial-scale=1"
|
||||
/>
|
||||
<title>Strafe Client</title>
|
||||
|
||||
<base data-trunk-public-url />
|
||||
<style type="text/css">
|
||||
:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0px;
|
||||
background: #fff;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
#content {
|
||||
/* This allows the flexbox to grow to max size, this is needed for WebGPU */
|
||||
flex: 1 1 100%;
|
||||
/* This forces CSS to ignore the width/height of the canvas, this is needed for WebGL */
|
||||
contain: size;
|
||||
}
|
||||
|
||||
.main-canvas {
|
||||
margin: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<canvas class="main-canvas" id="canvas"></canvas>
|
||||
<link
|
||||
data-trunk
|
||||
rel="rust"
|
||||
href="../strafe-client/Cargo.toml"
|
||||
data-wasm-opt-params="--enable-bulk-memory --enable-nontrapping-float-to-int"
|
||||
/>
|
||||
</body>
|
||||
</html>
|
||||
0
web-demo/player.ts
Normal file
0
web-demo/player.ts
Normal file
Reference in New Issue
Block a user