forked from StrafesNET/roblox-bot-player
Compare commits
11 Commits
playback-i
...
ai-ratio2
| Author | SHA1 | Date | |
|---|---|---|---|
|
2ffce9d9ca
|
|||
|
8a55fbffd9
|
|||
|
8ecb79a0b4
|
|||
|
98f56d0608
|
|||
|
cf59852468
|
|||
|
2f584744c7
|
|||
|
c33daaf0c6
|
|||
|
3dea810a50
|
|||
|
a238793cdc
|
|||
|
d9610901cb
|
|||
|
dd9cf502f1
|
2
.cargo/config.toml
Normal file
2
.cargo/config.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[registries.strafesnet]
|
||||
index = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
||||
1
.rustfmt.toml
Normal file
1
.rustfmt.toml
Normal file
@@ -0,0 +1 @@
|
||||
hard_tabs=true
|
||||
19
Cargo.lock
generated
19
Cargo.lock
generated
@@ -1407,6 +1407,13 @@ version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3d6831663a5098ea164f89cff59c6284e95f4e3c76ce9848d4529f5ccca9bde"
|
||||
|
||||
[[package]]
|
||||
name = "ratio_from_float"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"strafesnet_common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ratio_ops"
|
||||
version = "0.1.1"
|
||||
@@ -1632,9 +1639,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "strafesnet_common"
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
||||
checksum = "171f2b754a8c59b335578824d5465d9637fb41ec4906c6a8c1fd39206891b09c"
|
||||
checksum = "cba84ffce9c31b70b5dcaab0b9e946fe34ca4d6613a4c83980745b47dfc86edf"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bitflags 2.10.0",
|
||||
@@ -1661,9 +1668,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "strafesnet_roblox_bot_file"
|
||||
version = "0.8.1"
|
||||
version = "0.9.0"
|
||||
source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
||||
checksum = "33d0fa524476d8b6cf23269b0c9cff6334b70585546b807cb8ec193858defecd"
|
||||
checksum = "3ee6a8c592794145fdbbb2fbdaedbbc5813373e2e2bbbb9bac6ab7944775cc0a"
|
||||
dependencies = [
|
||||
"binrw",
|
||||
"bitflags 2.10.0",
|
||||
@@ -1678,7 +1685,6 @@ dependencies = [
|
||||
"strafesnet_common",
|
||||
"strafesnet_graphics",
|
||||
"strafesnet_roblox_bot_file",
|
||||
"strafesnet_snf",
|
||||
"wgpu",
|
||||
]
|
||||
|
||||
@@ -1688,6 +1694,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"glam",
|
||||
"pollster",
|
||||
"ratio_from_float",
|
||||
"strafesnet_common",
|
||||
"strafesnet_graphics",
|
||||
"strafesnet_roblox_bot_file",
|
||||
@@ -1701,10 +1708,12 @@ dependencies = [
|
||||
name = "strafesnet_roblox_bot_player_wasm_module"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ratio_from_float",
|
||||
"strafesnet_common",
|
||||
"strafesnet_graphics",
|
||||
"strafesnet_roblox_bot_file",
|
||||
"strafesnet_roblox_bot_player",
|
||||
"strafesnet_snf",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"web-sys",
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
members = [
|
||||
"lib",
|
||||
"native-player",
|
||||
"ratio_from_float",
|
||||
"wasm-module"
|
||||
]
|
||||
resolver = "3"
|
||||
@@ -12,7 +13,7 @@ strip = true
|
||||
codegen-units = 1
|
||||
|
||||
[workspace.dependencies]
|
||||
strafesnet_common = { version = "0.8.0", registry = "strafesnet" }
|
||||
strafesnet_common = { version = "0.8.1", registry = "strafesnet" }
|
||||
strafesnet_graphics = { version = "0.0.2", registry = "strafesnet" }
|
||||
strafesnet_roblox_bot_file = { version = "0.8.1", registry = "strafesnet" }
|
||||
strafesnet_roblox_bot_file = { version = "0.9.0", registry = "strafesnet" }
|
||||
strafesnet_snf = { version = "0.3.2", registry = "strafesnet" }
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
This respository uses git lfs for large file storage. You will need to run `git lfs pull` after cloning.
|
||||
|
||||
How to build the wasm module:
|
||||
```
|
||||
cd wasm-module
|
||||
@@ -13,5 +15,6 @@ python3 -m http.server
|
||||
How to run the native player:
|
||||
```
|
||||
cd native-player
|
||||
cargo run --release
|
||||
cargo run --release -- ../web-demo/bhop_marble_5692093612.snfm ../web-demo/bhop_marble_7cf33a64-7120-4514-b9fa-4fe29d9523d.qbot
|
||||
```
|
||||
You can drag and drop map files and bot files to load them.
|
||||
|
||||
@@ -8,5 +8,4 @@ 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"
|
||||
|
||||
@@ -1,31 +1,47 @@
|
||||
use strafesnet_roblox_bot_file::v0;
|
||||
use strafesnet_common::physics::{Time as PhysicsTime};
|
||||
|
||||
/// A loaded bot file.
|
||||
pub struct CompleteBot{
|
||||
//Instructions
|
||||
timelines:strafesnet_roblox_bot_file::v0::Block,
|
||||
offset:f64,
|
||||
duration:f64,
|
||||
timelines:v0::Block,
|
||||
time_base:PhysicsTime,
|
||||
duration:PhysicsTime,
|
||||
}
|
||||
impl CompleteBot{
|
||||
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))?;
|
||||
timelines:v0::Block,
|
||||
)->Self{
|
||||
let first=timelines.output_events.first().unwrap();
|
||||
let last=timelines.output_events.last().unwrap();
|
||||
Ok(Self{
|
||||
offset:first.time,
|
||||
duration:last.time-first.time,
|
||||
Self{
|
||||
time_base:crate::time::from_float(first.time).unwrap(),
|
||||
duration:crate::time::from_float(last.time-first.time).unwrap(),
|
||||
timelines,
|
||||
})
|
||||
}
|
||||
}
|
||||
pub const fn offset(&self)->f64{
|
||||
self.offset
|
||||
pub const fn time_base(&self)->PhysicsTime{
|
||||
self.time_base
|
||||
}
|
||||
pub const fn duration(&self)->f64{
|
||||
pub const fn duration(&self)->PhysicsTime{
|
||||
self.duration
|
||||
}
|
||||
pub const fn timelines(&self)->&strafesnet_roblox_bot_file::v0::Block{
|
||||
pub const fn timelines(&self)->&v0::Block{
|
||||
&self.timelines
|
||||
}
|
||||
pub fn run_duration(&self,mode_id:v0::ModeID)->Option<PhysicsTime>{
|
||||
let mut it=self.timelines.run_events.iter().rev();
|
||||
let end=it.find_map(|event|match &event.event{
|
||||
v0::RunEvent::Finish(run_start_event) if run_start_event.mode==mode_id=>Some(event.time),
|
||||
_=>None,
|
||||
})?;
|
||||
let start=it.find_map(|event|match &event.event{
|
||||
v0::RunEvent::Start(run_start_event) if run_start_event.mode==mode_id=>Some(event.time),
|
||||
_=>None,
|
||||
})?;
|
||||
let start=crate::time::from_float(start).unwrap();
|
||||
let end=crate::time::from_float(end).unwrap();
|
||||
Some(end-start)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@ impl Graphics{
|
||||
config,
|
||||
}
|
||||
}
|
||||
pub fn change_map(&mut self,map:&crate::map::CompleteMap){
|
||||
pub fn change_map(&mut self,map:&strafesnet_common::map::CompleteMap){
|
||||
self.graphics.clear();
|
||||
self.graphics.generate_models(&self.device,&self.queue,map.map());
|
||||
self.graphics.generate_models(&self.device,&self.queue,map);
|
||||
}
|
||||
pub fn resize(&mut self,surface:&wgpu::Surface<'_>,size:glam::UVec2){
|
||||
self.config.width=size.x.max(1);
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
use glam::Vec3Swizzles;
|
||||
use strafesnet_common::timer::{Timer,Scaled};
|
||||
use strafesnet_common::timer::{Scaled,Timer,TimerState};
|
||||
use strafesnet_common::session::{Time as SessionTime,TimeInner as SessionTimeInner};
|
||||
use strafesnet_common::physics::{Time as PhysicsTime,TimeInner as PhysicsTimeInner};
|
||||
use strafesnet_roblox_bot_file::v0::{EventType,Head,Timed};
|
||||
|
||||
pub enum PlaybackInstructionInternal{
|
||||
Sound
|
||||
}
|
||||
pub enum PlaybackInstructionExternal{
|
||||
SetPaused(bool),
|
||||
Idle,
|
||||
}
|
||||
use crate::bot::CompleteBot;
|
||||
use crate::state::PlaybackState;
|
||||
|
||||
fn vector3_to_glam(v:&strafesnet_roblox_bot_file::v0::Vector3)->glam::Vec3{
|
||||
glam::vec3(v.x,v.y,v.z)
|
||||
@@ -17,45 +13,86 @@ fn vector3_to_glam(v:&strafesnet_roblox_bot_file::v0::Vector3)->glam::Vec3{
|
||||
|
||||
/// 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,
|
||||
head:Head,
|
||||
loop_offset:PhysicsTime,
|
||||
timer:Timer<Scaled<SessionTimeInner,PhysicsTimeInner>>,
|
||||
state:PlaybackState,
|
||||
}
|
||||
const HEAD_NO_CRASH:Head={
|
||||
let mut head=Head::new();
|
||||
// push one output event so that output-1 doesn't underflow
|
||||
head.push(EventType::Output);
|
||||
head
|
||||
};
|
||||
impl PlaybackHead{
|
||||
pub fn new(time:SessionTime)->Self{
|
||||
let timer=Timer::unpaused(time,PhysicsTime::ZERO);
|
||||
Self{
|
||||
event_id:0,
|
||||
offset:0.0,
|
||||
head:HEAD_NO_CRASH,
|
||||
loop_offset:PhysicsTime::ZERO,
|
||||
timer,
|
||||
state:PlaybackState::new(),
|
||||
}
|
||||
}
|
||||
pub fn advance_time(&mut self,bot:&crate::bot::CompleteBot,time:SessionTime){
|
||||
let simulation_time=self.timer.time(time);
|
||||
let mut time_float=simulation_time.get() as f64/PhysicsTime::ONE_SECOND.get() as f64+self.offset+bot.offset();
|
||||
pub const fn state(&self)->&PlaybackState{
|
||||
&self.state
|
||||
}
|
||||
pub fn time(&self,bot:&CompleteBot,time:SessionTime)->PhysicsTime{
|
||||
bot.time_base()+self.timer.time(time)+self.loop_offset
|
||||
}
|
||||
pub fn next_event(&self,bot:&CompleteBot)->Option<Timed<EventType>>{
|
||||
self.head.next_event(bot.timelines())
|
||||
}
|
||||
pub fn process_event(&mut self,bot:&CompleteBot,event_type:EventType){
|
||||
self.state.process_event(bot,event_type,self.head.get_event_index(event_type));
|
||||
self.head.push(event_type);
|
||||
}
|
||||
pub fn set_paused(&mut self,time:SessionTime,paused:bool){
|
||||
_=self.timer.set_paused(time,paused);
|
||||
}
|
||||
pub fn seek_backward(&mut self,time:SessionTime){
|
||||
let (mut state,paused)=self.timer.clone().into_state();
|
||||
let offset=state.get_offset()-time.coerce();
|
||||
state.set_offset(offset);
|
||||
self.timer=Timer::from_state(state,paused);
|
||||
// reset head
|
||||
self.head=HEAD_NO_CRASH;
|
||||
}
|
||||
pub fn seek_forward(&mut self,time:SessionTime){
|
||||
let (mut state,paused)=self.timer.clone().into_state();
|
||||
let offset=state.get_offset()+time.coerce();
|
||||
state.set_offset(offset);
|
||||
self.timer=Timer::from_state(state,paused);
|
||||
}
|
||||
pub fn set_scale(&mut self,time:SessionTime,new_scale:strafesnet_common::integer::Ratio64){
|
||||
self.timer.set_scale(time,new_scale);
|
||||
}
|
||||
pub fn advance_time(&mut self,bot:&CompleteBot,time:SessionTime){
|
||||
let mut simulation_time=self.time(bot,time);
|
||||
let mut time_float=simulation_time.get() as f64/PhysicsTime::ONE_SECOND.get() as f64;
|
||||
loop{
|
||||
match bot.timelines().output_events.get(self.event_id+1){
|
||||
match self.next_event(bot){
|
||||
Some(next_event)=>{
|
||||
if next_event.time<time_float{
|
||||
self.event_id+=1;
|
||||
self.process_event(bot,next_event.event);
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
},
|
||||
None=>{
|
||||
//reset playback
|
||||
self.event_id=0;
|
||||
self.offset-=bot.duration();
|
||||
time_float-=bot.duration();
|
||||
self.head=HEAD_NO_CRASH;
|
||||
self.loop_offset-=bot.duration();
|
||||
simulation_time-=bot.duration();
|
||||
time_float=simulation_time.get() as f64/PhysicsTime::ONE_SECOND.get() as f64;
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn get_position_angles(&self,bot:&crate::bot::CompleteBot,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];
|
||||
pub fn get_position_angles(&self,bot:&CompleteBot,time:SessionTime)->(glam::Vec3,glam::Vec2){
|
||||
let time=self.time(bot,time);
|
||||
let event0=&bot.timelines().output_events[self.head.get_event_index(EventType::Output)-1];
|
||||
let event1=&bot.timelines().output_events[self.head.get_event_index(EventType::Output)];
|
||||
let p0=vector3_to_glam(&event0.event.position);
|
||||
let p1=vector3_to_glam(&event1.event.position);
|
||||
// let v0=vector3_to_glam(&event0.event.velocity);
|
||||
@@ -65,7 +102,7 @@ impl PlaybackHead{
|
||||
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+self.offset+bot.offset()-t0)/(t1-t0)) as f32;
|
||||
let t=((time_float-t0)/(t1-t0)) as f32;
|
||||
let p=p0.lerp(p1,t);
|
||||
// let v=v0.lerp(v1,t);
|
||||
// let a=a0.lerp(a1,t);
|
||||
@@ -76,6 +113,6 @@ impl PlaybackHead{
|
||||
let angles1=vector3_to_glam(&event1.event.angles);
|
||||
let angles=angles0.lerp(angles1,t);
|
||||
|
||||
(p+crate::bot::CompleteBot::CAMERA_OFFSET,angles.yx())
|
||||
(p+CompleteBot::CAMERA_OFFSET,angles.yx())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
pub mod bot;
|
||||
pub mod map;
|
||||
pub mod head;
|
||||
pub mod time;
|
||||
pub mod state;
|
||||
// pub mod surface;
|
||||
pub mod graphics;
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
#[derive(Debug)]
|
||||
pub enum Error{
|
||||
File(strafesnet_snf::Error),
|
||||
Map(strafesnet_snf::map::Error),
|
||||
}
|
||||
impl std::fmt::Display for Error{
|
||||
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
||||
write!(f,"{self:?}")
|
||||
}
|
||||
}
|
||||
impl std::error::Error for Error{}
|
||||
|
||||
pub struct CompleteMap{
|
||||
complete_map:strafesnet_common::map::CompleteMap,
|
||||
}
|
||||
impl CompleteMap{
|
||||
pub fn new(
|
||||
data:&[u8],
|
||||
)->Result<Self,Error>{
|
||||
let complete_map=strafesnet_snf::read_map(std::io::Cursor::new(data))
|
||||
.map_err(Error::File)?
|
||||
.into_complete_map()
|
||||
.map_err(Error::Map)?;
|
||||
Ok(Self{complete_map})
|
||||
}
|
||||
pub const fn map(&self)->&strafesnet_common::map::CompleteMap{
|
||||
&self.complete_map
|
||||
}
|
||||
}
|
||||
210
lib/src/state.rs
Normal file
210
lib/src/state.rs
Normal file
@@ -0,0 +1,210 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use strafesnet_common::run;
|
||||
use strafesnet_common::physics::Time as PhysicsTime;
|
||||
use strafesnet_roblox_bot_file::v0;
|
||||
|
||||
use crate::bot::CompleteBot;
|
||||
|
||||
pub struct Run{
|
||||
run:run::RunState,
|
||||
flag_reason:Option<v0::FlagReason>,
|
||||
}
|
||||
impl Run{
|
||||
fn new()->Self{
|
||||
Self{
|
||||
run:run::RunState::Created,
|
||||
flag_reason:None,
|
||||
}
|
||||
}
|
||||
fn flag(&mut self,flag_reason:v0::FlagReason){
|
||||
if self.flag_reason.is_none(){
|
||||
self.flag_reason=Some(flag_reason);
|
||||
}
|
||||
}
|
||||
pub fn time(&self,time:PhysicsTime)->run::Time{
|
||||
self.run.time(time)
|
||||
}
|
||||
pub fn is_invalid(&self)->bool{
|
||||
self.flag_reason.is_some()
|
||||
}
|
||||
pub fn is_in_progress(&self)->bool{
|
||||
matches!(&self.run,run::RunState::Started{..})
|
||||
}
|
||||
pub fn is_finished(&self)->bool{
|
||||
matches!(&self.run,run::RunState::Finished{..})
|
||||
}
|
||||
pub fn get_flag_reason_text(&self)->Option<&'static str>{
|
||||
Some(match self.flag_reason{
|
||||
Some(v0::FlagReason::Anticheat)=>"Passed through anticheat zone.",
|
||||
Some(v0::FlagReason::StyleChange)=>"Changed style.",
|
||||
Some(v0::FlagReason::Clock)=>"Incorrect clock. (This can be caused by internet hiccups)",
|
||||
Some(v0::FlagReason::Pause)=>"Pausing is not allowed in this style.",
|
||||
Some(v0::FlagReason::Flying)=>"Flying is not allowed in this style.",
|
||||
Some(v0::FlagReason::Gravity)=>"Gravity modification is not allowed in this style.",
|
||||
Some(v0::FlagReason::Timescale)=>"Timescale is not allowed in this style.",
|
||||
Some(v0::FlagReason::Timetravel)=>"Time travel is not allowed in this style.",
|
||||
Some(v0::FlagReason::Teleport)=>"Illegal teleport.",
|
||||
Some(v0::FlagReason::Practice)=>"Practice mode triggers invalidation.",
|
||||
None=>return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PlaybackState{
|
||||
// EventType::Input
|
||||
game_controls:v0::GameControls,
|
||||
mouse_pos:v0::Vector2,
|
||||
// EventType::Output
|
||||
jump_count:u32,
|
||||
// EventType::Sound
|
||||
// EventType::World
|
||||
// EventType::Gravity
|
||||
gravity:v0::Vector3,
|
||||
// EventType::Run
|
||||
runs:HashMap<v0::ModeID,Run>,
|
||||
style:v0::Style,
|
||||
// EventType::Camera
|
||||
// TODO: camera punch
|
||||
// EventType::Setting
|
||||
absolute_sensitivity_enabled:bool,
|
||||
fov_y:f64,
|
||||
sens_x:f64,
|
||||
vertical_sensitivity_multipler:f64,
|
||||
turn_speed:f64,
|
||||
}
|
||||
impl PlaybackState{
|
||||
pub fn new()->Self{
|
||||
Self{
|
||||
game_controls:v0::GameControls::empty(),
|
||||
mouse_pos:v0::Vector2{x:0.0,y:0.0},
|
||||
jump_count:0,
|
||||
gravity:v0::Vector3{x:0.0,y:0.0,z:0.0},
|
||||
runs:HashMap::new(),
|
||||
style:v0::Style::Autohop,
|
||||
absolute_sensitivity_enabled:false,
|
||||
fov_y:1.0,
|
||||
sens_x:1.0,
|
||||
vertical_sensitivity_multipler:1.0,
|
||||
turn_speed:1.0,
|
||||
}
|
||||
}
|
||||
pub fn get_run(&self,mode:v0::ModeID)->Option<&Run>{
|
||||
self.runs.get(&mode)
|
||||
}
|
||||
fn push_output(&mut self,event:&v0::OutputEvent){
|
||||
if event.tick_info.contains(v0::TickInfo::Jump){
|
||||
self.jump_count+=1;
|
||||
}
|
||||
}
|
||||
fn push_input(&mut self,event:&v0::InputEvent){
|
||||
self.game_controls=event.game_controls;
|
||||
self.mouse_pos=event.mouse_pos;
|
||||
}
|
||||
fn push_gravity(&mut self,event:&v0::GravityEvent){
|
||||
self.gravity=event.gravity;
|
||||
}
|
||||
fn push_run(&mut self,event:&v0::Timed<v0::RunEvent>){
|
||||
match &event.event{
|
||||
v0::RunEvent::Prepare(run_event_prepare)=>{
|
||||
self.runs.insert(run_event_prepare.mode,Run::new());
|
||||
self.style=run_event_prepare.style;
|
||||
},
|
||||
v0::RunEvent::Start(run_event_zone)=>{
|
||||
let time=PhysicsTime::raw((event.time*PhysicsTime::ONE_SECOND.get() as f64) as i64);
|
||||
if let Some(run)=self.runs.get_mut(&run_event_zone.mode){
|
||||
_=run.run.start(time);
|
||||
}
|
||||
},
|
||||
v0::RunEvent::Finish(run_event_zone)=>{
|
||||
let time=PhysicsTime::raw((event.time*PhysicsTime::ONE_SECOND.get() as f64) as i64);
|
||||
if let Some(run)=self.runs.get_mut(&run_event_zone.mode){
|
||||
_=run.run.finish(time);
|
||||
}
|
||||
},
|
||||
v0::RunEvent::Clear(run_event_clear)=>{
|
||||
match run_event_clear.mode{
|
||||
v0::ModeSpec::Exactly(mode_id)=>{
|
||||
self.runs.remove(&mode_id);
|
||||
},
|
||||
v0::ModeSpec::All=>{
|
||||
self.runs.clear();
|
||||
},
|
||||
v0::ModeSpec::Invalid=>{
|
||||
self.runs.retain(|_,run|!run.is_invalid());
|
||||
},
|
||||
v0::ModeSpec::InProgress=>{
|
||||
self.runs.retain(|_,run|!run.is_in_progress());
|
||||
},
|
||||
}
|
||||
},
|
||||
v0::RunEvent::Flag(run_event_flag)=>{
|
||||
match run_event_flag.mode{
|
||||
v0::ModeSpec::Exactly(mode_id)=>{
|
||||
if let Some(run)=self.runs.get_mut(&mode_id){
|
||||
run.flag(run_event_flag.flag_reason);
|
||||
}
|
||||
},
|
||||
v0::ModeSpec::All=>{
|
||||
for run in self.runs.values_mut(){
|
||||
run.flag(run_event_flag.flag_reason);
|
||||
}
|
||||
},
|
||||
v0::ModeSpec::Invalid=>{
|
||||
for run in self.runs.values_mut(){
|
||||
if run.is_invalid(){
|
||||
run.flag(run_event_flag.flag_reason);
|
||||
}
|
||||
}
|
||||
},
|
||||
v0::ModeSpec::InProgress=>{
|
||||
for run in self.runs.values_mut(){
|
||||
if run.is_in_progress(){
|
||||
run.flag(run_event_flag.flag_reason);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
// these should never appear in a uploaded bot file,
|
||||
// they are just part of the network protocol for spectating
|
||||
// someone in practice mode.
|
||||
//
|
||||
// Yes, this is a design mistake.
|
||||
// I didn't understand Session vs Simulation when I rewrote bhop in 2022
|
||||
v0::RunEvent::LoadState(_run_event_practice)=>{},
|
||||
v0::RunEvent::SaveState(_run_event_practice)=>{},
|
||||
}
|
||||
}
|
||||
fn push_setting(&mut self,event:&v0::SettingEvent){
|
||||
match event{
|
||||
v0::SettingEvent::FieldOfView(setting_event_field_of_view)=>{
|
||||
self.fov_y=setting_event_field_of_view.fov;
|
||||
},
|
||||
v0::SettingEvent::Sensitivity(setting_event_sensitivity)=>{
|
||||
self.sens_x=setting_event_sensitivity.sensitivity;
|
||||
},
|
||||
v0::SettingEvent::VerticalSensitivityMultiplier(setting_event_vertical_sensitivity_multiplier)=>{
|
||||
self.vertical_sensitivity_multipler=setting_event_vertical_sensitivity_multiplier.multiplier;
|
||||
},
|
||||
v0::SettingEvent::AbsoluteSensitivity(setting_event_absolute_sensitivity)=>{
|
||||
self.absolute_sensitivity_enabled=setting_event_absolute_sensitivity.enabled;
|
||||
},
|
||||
v0::SettingEvent::TurnSpeed(setting_event_turn_speed)=>{
|
||||
self.turn_speed=setting_event_turn_speed.turn_speed;
|
||||
},
|
||||
}
|
||||
}
|
||||
pub(crate) fn process_event(&mut self,bot:&CompleteBot,event_type:v0::EventType,event_index:usize){
|
||||
match event_type{
|
||||
v0::EventType::Input=>self.push_input(&bot.timelines().input_events[event_index].event),
|
||||
v0::EventType::Output=>self.push_output(&bot.timelines().output_events[event_index].event),
|
||||
v0::EventType::Sound=>{},
|
||||
v0::EventType::World=>{},
|
||||
v0::EventType::Gravity=>self.push_gravity(&bot.timelines().gravity_events[event_index].event),
|
||||
v0::EventType::Run=>self.push_run(&bot.timelines().run_events[event_index]),
|
||||
v0::EventType::Camera=>{},
|
||||
v0::EventType::Setting=>self.push_setting(&bot.timelines().setting_events[event_index].event),
|
||||
}
|
||||
}
|
||||
}
|
||||
32
lib/src/time.rs
Normal file
32
lib/src/time.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use strafesnet_common::integer::Time;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error{
|
||||
Underflow,
|
||||
Overflow,
|
||||
Nan,
|
||||
}
|
||||
impl std::fmt::Display for Error{
|
||||
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
||||
write!(f,"{self:?}")
|
||||
}
|
||||
}
|
||||
impl std::error::Error for Error{}
|
||||
|
||||
pub fn from_float<T>(time:f64)->Result<Time<T>,Error>{
|
||||
match time.classify(){
|
||||
core::num::FpCategory::Nan=>Err(Error::Nan),
|
||||
core::num::FpCategory::Zero=>Ok(Time::ZERO),
|
||||
core::num::FpCategory::Infinite
|
||||
|core::num::FpCategory::Subnormal
|
||||
|core::num::FpCategory::Normal=>{
|
||||
if time<Time::<T>::MIN.get() as f64{
|
||||
return Err(Error::Underflow);
|
||||
}
|
||||
if (Time::<T>::MAX.get() as f64)<time{
|
||||
return Err(Error::Overflow);
|
||||
}
|
||||
Ok(Time::raw((time*Time::<T>::ONE_SECOND.get() as f64) as i64))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,9 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
pollster = "0.4.0"
|
||||
ratio_from_float = { version = "0.1.0", path = "../ratio_from_float" }
|
||||
strafesnet_roblox_bot_player = { version = "0.1.0", path = "../lib" }
|
||||
pollster = "0.4.0"
|
||||
wgpu = "28.0.0"
|
||||
winit = "0.30.12"
|
||||
strafesnet_common.workspace = true
|
||||
|
||||
@@ -1,21 +1,11 @@
|
||||
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),
|
||||
RobloxBot(strafesnet_roblox_bot_file::v0::Error),
|
||||
Io(std::io::Error),
|
||||
UnknownFileFormat,
|
||||
}
|
||||
@@ -27,14 +17,8 @@ impl std::fmt::Display for ReadError{
|
||||
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),
|
||||
QBOT(strafesnet_roblox_bot_file::v0::Block),
|
||||
}
|
||||
|
||||
pub fn read<R:Read+std::io::Seek>(input:R)->Result<ReadFormat,ReadError>{
|
||||
@@ -45,19 +29,12 @@ pub fn read<R:Read+std::io::Seek>(input:R)->Result<ReadFormat,ReadError>{
|
||||
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)?
|
||||
b"qbot"=>Ok(ReadFormat::QBOT(
|
||||
strafesnet_roblox_bot_file::v0::read_all_to_block(cursor).map_err(ReadError::RobloxBot)?
|
||||
)),
|
||||
_=>Err(ReadError::UnknownFileFormat),
|
||||
}
|
||||
@@ -68,10 +45,6 @@ pub fn read<R:Read+std::io::Seek>(input:R)->Result<ReadFormat,ReadError>{
|
||||
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{
|
||||
@@ -81,36 +54,15 @@ impl std::fmt::Display for LoadError{
|
||||
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),
|
||||
Bot(strafesnet_roblox_bot_file::v0::Block),
|
||||
}
|
||||
|
||||
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::QBOT(bot)=>Ok(LoadFormat::Bot(bot)),
|
||||
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)?
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@ use strafesnet_roblox_bot_player::{bot::CompleteBot,graphics::Graphics,head::Pla
|
||||
|
||||
pub enum SessionControlInstruction{
|
||||
SetPaused(bool),
|
||||
}
|
||||
pub enum SessionPlaybackInstruction{
|
||||
SkipForward,
|
||||
SkipBack,
|
||||
DecreaseTimescale,
|
||||
@@ -14,43 +12,68 @@ pub enum SessionPlaybackInstruction{
|
||||
|
||||
pub enum Instruction{
|
||||
SessionControl(SessionControlInstruction),
|
||||
SessionPlayback(SessionPlaybackInstruction),
|
||||
Render,
|
||||
Resize(winit::dpi::PhysicalSize<u32>),
|
||||
ChangeMap(strafesnet_common::map::CompleteMap),
|
||||
LoadReplay(strafesnet_roblox_bot_file::v0::Block),
|
||||
}
|
||||
|
||||
pub struct PlayerWorker<'a>{
|
||||
surface:wgpu::Surface<'a>,
|
||||
graphics_thread:Graphics,
|
||||
bot:CompleteBot,
|
||||
bot:Option<CompleteBot>,
|
||||
playback_head:PlaybackHead,
|
||||
playback_speed:i8,
|
||||
}
|
||||
impl<'a> PlayerWorker<'a>{
|
||||
pub fn new(
|
||||
surface:wgpu::Surface<'a>,
|
||||
bot:CompleteBot,
|
||||
graphics_thread:Graphics,
|
||||
)->Self{
|
||||
let playback_head=PlaybackHead::new(SessionTime::ZERO);
|
||||
Self{
|
||||
surface,
|
||||
graphics_thread,
|
||||
bot,
|
||||
bot:None,
|
||||
playback_head,
|
||||
playback_speed:0,
|
||||
}
|
||||
}
|
||||
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);
|
||||
Instruction::SessionControl(SessionControlInstruction::SetPaused(paused))=>{
|
||||
self.playback_head.set_paused(ins.time,paused);
|
||||
},
|
||||
Instruction::SessionControl(SessionControlInstruction::SkipForward)=>{
|
||||
self.playback_head.seek_forward(SessionTime::from_secs(5));
|
||||
},
|
||||
Instruction::SessionControl(SessionControlInstruction::SkipBack)=>{
|
||||
self.playback_head.seek_backward(SessionTime::from_secs(5));
|
||||
},
|
||||
Instruction::SessionControl(SessionControlInstruction::DecreaseTimescale)=>{
|
||||
self.playback_speed=self.playback_speed.saturating_sub(1).max(-48);
|
||||
let speed=2.0f64.powf(self.playback_speed as f64/3.0);
|
||||
self.playback_head.set_scale(ins.time,ratio_from_float::ratio_from_f64(speed).unwrap());
|
||||
},
|
||||
Instruction::SessionControl(SessionControlInstruction::IncreaseTimescale)=>{
|
||||
self.playback_speed=self.playback_speed.saturating_add(1).min(48);
|
||||
let speed=2.0f64.powf(self.playback_speed as f64/3.0);
|
||||
self.playback_head.set_scale(ins.time,ratio_from_float::ratio_from_f64(speed).unwrap());
|
||||
},
|
||||
Instruction::Render=>if let Some(bot)=&self.bot{
|
||||
self.playback_head.advance_time(bot,ins.time);
|
||||
let (pos,angles)=self.playback_head.get_position_angles(bot,ins.time);
|
||||
self.graphics_thread.render(&self.surface,pos,angles);
|
||||
},
|
||||
Instruction::Resize(physical_size)=>{
|
||||
self.graphics_thread.resize(&self.surface,glam::uvec2(physical_size.width,physical_size.height));
|
||||
},
|
||||
Instruction::ChangeMap(complete_map)=>{
|
||||
self.graphics_thread.change_map(&complete_map);
|
||||
},
|
||||
Instruction::LoadReplay(bot)=>{
|
||||
self.bot=Some(CompleteBot::new(bot));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ 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};
|
||||
use crate::player::{PlayerWorker,Instruction as PhysicsWorkerInstruction,SessionControlInstruction};
|
||||
|
||||
pub enum Instruction{
|
||||
WindowEvent(winit::event::WindowEvent),
|
||||
@@ -11,8 +11,6 @@ pub enum Instruction{
|
||||
|
||||
//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,
|
||||
@@ -20,44 +18,12 @@ pub struct WindowContext<'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)}),
|
||||
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}"),
|
||||
}
|
||||
},
|
||||
@@ -78,8 +44,6 @@ impl WindowContext<'_>{
|
||||
..
|
||||
}=>{
|
||||
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);
|
||||
@@ -87,70 +51,30 @@ impl WindowContext<'_>{
|
||||
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))
|
||||
s.then_some(PhysicsWorkerInstruction::SessionControl(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{
|
||||
if let Some(instruction)=match keycode{
|
||||
winit::keyboard::Key::Named(winit::keyboard::NamedKey::Space)=>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)))
|
||||
Some(PhysicsWorkerInstruction::SessionControl(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,
|
||||
},
|
||||
winit::keyboard::Key::Named(winit::keyboard::NamedKey::ArrowUp)=>session_ctrl!(IncreaseTimescale,s),
|
||||
winit::keyboard::Key::Named(winit::keyboard::NamedKey::ArrowDown)=>session_ctrl!(DecreaseTimescale,s),
|
||||
winit::keyboard::Key::Named(winit::keyboard::NamedKey::ArrowLeft)=>session_ctrl!(SkipBack,s),
|
||||
winit::keyboard::Key::Named(winit::keyboard::NamedKey::ArrowRight)=>session_ctrl!(SkipForward,s),
|
||||
_=>None,
|
||||
}{
|
||||
self.physics_thread.send(TimedInstruction{
|
||||
time,
|
||||
instruction:session_instruction.into(),
|
||||
instruction,
|
||||
});
|
||||
}
|
||||
},
|
||||
@@ -199,22 +123,14 @@ impl WindowContext<'_>{
|
||||
config:wgpu::SurfaceConfiguration,
|
||||
)->WindowContext<'a>{
|
||||
let screen_size=glam::uvec2(config.width,config.height);
|
||||
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 bot=strafesnet_roblox_bot_player::bot::CompleteBot::new(bot).unwrap();
|
||||
let map=strafesnet_roblox_bot_player::map::CompleteMap::new(map).unwrap();
|
||||
let mut graphics=strafesnet_roblox_bot_player::graphics::Graphics::new(device,queue,config);
|
||||
graphics.change_map(&map);
|
||||
let graphics=strafesnet_roblox_bot_player::graphics::Graphics::new(device,queue,config);
|
||||
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(
|
||||
surface,
|
||||
bot,
|
||||
graphics,
|
||||
),
|
||||
}
|
||||
|
||||
7
ratio_from_float/Cargo.toml
Normal file
7
ratio_from_float/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "ratio_from_float"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
strafesnet_common.workspace = true
|
||||
128
ratio_from_float/src/lib.rs
Normal file
128
ratio_from_float/src/lib.rs
Normal file
@@ -0,0 +1,128 @@
|
||||
use strafesnet_common::integer::Ratio64;
|
||||
|
||||
/// Convert an `f64` to a `Ratio64`.
|
||||
///
|
||||
/// Returns `None` for NaN, infinities, or when the exact fraction would overflow `i64`/`u64`.
|
||||
/// The result is always reduced to lowest terms.
|
||||
pub fn ratio_from_f64(x: f64) -> Option<Ratio64> {
|
||||
// Handle special values first
|
||||
match x.classify() {
|
||||
core::num::FpCategory::Nan | core::num::FpCategory::Infinite => return None,
|
||||
core::num::FpCategory::Zero => return Ratio64::new(0, 1),
|
||||
core::num::FpCategory::Subnormal | core::num::FpCategory::Normal => {
|
||||
if x < i64::MIN as f64 {
|
||||
return None;
|
||||
}
|
||||
if (i64::MAX as f64) < x {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2️⃣ Pull out the raw bits
|
||||
let bits: u64 = x.to_bits();
|
||||
let sign: i64 = if (bits >> 63) != 0 { -1 } else { 1 };
|
||||
let exp_raw: u32 = ((bits >> 52) & 0x7FF) as u32;
|
||||
let mant: u64 = bits & 0xFFFFFFFFFFFFF; // 52 bits
|
||||
|
||||
// 3️⃣ Normalise exponent and mantissa
|
||||
let (exp, mant) = if exp_raw == 0 {
|
||||
// subnormal
|
||||
(1 - 1023, mant) // unbiased exponent = -1022
|
||||
} else {
|
||||
// normal
|
||||
((exp_raw as i32) - 1023, mant | (1 << 52)) // implicit leading 1
|
||||
};
|
||||
|
||||
// value = sign * mant * 2^(exp-52)
|
||||
let shift = exp - 52; // may be negative
|
||||
|
||||
// 4️⃣ Build numerator / denominator as 64‑bit values
|
||||
// ────────────────────────────────────────
|
||||
// If shift is positive → numerator = mant << shift
|
||||
// If shift is negative → denominator = 1 << (-shift)
|
||||
// We use the checked arithmetic helpers to catch overflow.
|
||||
let (mut num, den) = if shift >= 0 {
|
||||
// shift <= 63 because 53‑bit mantissa * 2^shift must fit in i64
|
||||
let s = shift as u32;
|
||||
let n = (mant as i64).checked_shl(s)?;
|
||||
(n, 1)
|
||||
} else {
|
||||
// shift is negative
|
||||
let s = (-shift) as u32;
|
||||
if s > 63 {
|
||||
// 2^s would not fit in a u64 → underflow
|
||||
return Ratio64::new(0, 1);
|
||||
}
|
||||
(mant as i64, 1u64 << s)
|
||||
};
|
||||
|
||||
// 5️⃣ Apply the sign
|
||||
num *= sign;
|
||||
|
||||
Ratio64::new(num, den)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let r = ratio_from_f64(1.5).unwrap();
|
||||
assert_eq!(r.num(), 3);
|
||||
assert_eq!(r.den(), 2);
|
||||
|
||||
let r = ratio_from_f64(0.1).unwrap();
|
||||
// 0.1 = 3602879701896397 / 36028797018963968
|
||||
assert_eq!(r.num(), 3602879701896397);
|
||||
assert_eq!(r.den(), 36028797018963968);
|
||||
|
||||
let r = ratio_from_f64(-3.141592653589793).unwrap();
|
||||
assert_eq!(r.num(), -884279719003555);
|
||||
assert_eq!(r.den(), 281474976710656);
|
||||
|
||||
// NaN / Infinity → None
|
||||
assert!(ratio_from_f64(f64::NAN).is_none());
|
||||
assert!(ratio_from_f64(f64::INFINITY).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflow() {
|
||||
// value that would need > 64‑bit numerator
|
||||
let f = (i64::MAX as f64) * 2.0; // just above i64::MAX
|
||||
assert!(ratio_from_f64(f).is_none());
|
||||
|
||||
// subnormal: denominator would need 2^1074 > u64::MAX
|
||||
let sub = f64::MIN_POSITIVE / 2.0; // 2.22507e‑308 / 2 = 1.1125e‑308
|
||||
assert_eq!(ratio_from_f64(sub).unwrap().num(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
let numbers = [
|
||||
0.0,
|
||||
-0.0,
|
||||
1.0,
|
||||
-1.0,
|
||||
3.141592653589793,
|
||||
1.5,
|
||||
0.1,
|
||||
2.225073858507201e-308, // subnormal
|
||||
1.7976931348623157e308, // max normal
|
||||
];
|
||||
|
||||
for f in numbers {
|
||||
match ratio_from_f64(f) {
|
||||
Some(r) => println!(
|
||||
"{:>15} → {:>15} / {:>15} (≈ {:.20})",
|
||||
f,
|
||||
r.num(),
|
||||
r.den(),
|
||||
f
|
||||
),
|
||||
None => println!("{:>15} → overflow / NaN / infinite", f),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,12 @@ edition = "2024"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
ratio_from_float = { version = "0.1.0", path = "../ratio_from_float" }
|
||||
strafesnet_roblox_bot_player = { version = "0.1.0", path = "../lib" }
|
||||
strafesnet_common.workspace = true
|
||||
strafesnet_graphics.workspace = true
|
||||
strafesnet_roblox_bot_file.workspace = true
|
||||
strafesnet_snf.workspace = true
|
||||
wasm-bindgen = "0.2.108"
|
||||
wasm-bindgen-futures = "0.4.58"
|
||||
web-sys = { version = "0.3.85", features = ["HtmlCanvasElement"] }
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
use wasm_bindgen::JsValue;
|
||||
use strafesnet_roblox_bot_player::{bot,map,head,graphics};
|
||||
use strafesnet_roblox_bot_file::v0;
|
||||
use strafesnet_roblox_bot_player::{bot,head,time,graphics};
|
||||
use strafesnet_graphics::setup;
|
||||
use strafesnet_common::session::Time as SessionTime;
|
||||
use strafesnet_common::physics::Time as PhysicsTime;
|
||||
|
||||
// Hack to keep the code compiling,
|
||||
// SurfaceTarget::Canvas is not available in IDE for whatever reason.
|
||||
@@ -42,8 +43,7 @@ pub async fn setup_graphics(canvas:web_sys::HtmlCanvasElement)->Graphics{
|
||||
impl Graphics{
|
||||
#[wasm_bindgen]
|
||||
pub fn render(&mut self,bot:&CompleteBot,head:&PlaybackHead,time:f64){
|
||||
// TODO: check f64 range
|
||||
let time=SessionTime::raw((time*SessionTime::ONE_SECOND.get() as f64) as i64);
|
||||
let time=time::from_float(time).unwrap();
|
||||
let (pos,angles)=head.head.get_position_angles(&bot.bot,time);
|
||||
self.graphics.render(&self.surface,pos,angles);
|
||||
}
|
||||
@@ -65,22 +65,36 @@ pub struct CompleteBot{
|
||||
impl CompleteBot{
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(data:&[u8])->Result<Self,JsValue>{
|
||||
let timelines=v0::read_all_to_block(std::io::Cursor::new(data)).map_err(|e|JsValue::from_str(&e.to_string()))?;
|
||||
Ok(Self{
|
||||
bot:bot::CompleteBot::new(data).map_err(|e|JsValue::from_str(&e.to_string()))?,
|
||||
bot:bot::CompleteBot::new(timelines),
|
||||
})
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
pub fn duration(&self)->f64{
|
||||
self.bot.duration().get() as f64/PhysicsTime::ONE_SECOND.get() as f64
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
pub fn run_duration(&self,mode_id:u32)->Option<f64>{
|
||||
let mode=v0::ModeID(mode_id);
|
||||
Some(self.bot.run_duration(mode)?.get() as f64/PhysicsTime::ONE_SECOND.get() as f64)
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct CompleteMap{
|
||||
map:map::CompleteMap,
|
||||
map:strafesnet_common::map::CompleteMap,
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
impl CompleteMap{
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(data:&[u8])->Result<Self,JsValue>{
|
||||
let map=strafesnet_snf::read_map(std::io::Cursor::new(data))
|
||||
.map_err(|e|JsValue::from_str(&e.to_string()))?
|
||||
.into_complete_map()
|
||||
.map_err(|e|JsValue::from_str(&e.to_string()))?;
|
||||
Ok(Self{
|
||||
map:map::CompleteMap::new(data).map_err(|e|JsValue::from_str(&e.to_string()))?,
|
||||
map,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -93,16 +107,32 @@ pub struct PlaybackHead{
|
||||
impl PlaybackHead{
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(time:f64)->Result<Self,JsValue>{
|
||||
// TODO: check f64 range
|
||||
let time=SessionTime::raw((time*SessionTime::ONE_SECOND.get() as f64) as i64);
|
||||
let time=time::from_float(time).unwrap();
|
||||
Ok(Self{
|
||||
head:head::PlaybackHead::new(time),
|
||||
})
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
pub fn advance_time(&mut self,bot:&CompleteBot,time:f64){
|
||||
// TODO: check f64 range
|
||||
let time=SessionTime::raw((time*SessionTime::ONE_SECOND.get() as f64) as i64);
|
||||
let time=time::from_float(time).unwrap();
|
||||
self.head.advance_time(&bot.bot,time);
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
pub fn get_run_time(&self,bot:&CompleteBot,time:f64,mode_id:u32)->Option<f64>{
|
||||
let time=time::from_float(time).unwrap();
|
||||
let time=self.head.time(&bot.bot,time);
|
||||
let mode=v0::ModeID(mode_id);
|
||||
let run_time=self.head.state().get_run(mode)?.time(time);
|
||||
Some(run_time.get() as f64/strafesnet_common::run::Time::ONE_SECOND.get() as f64)
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
pub fn is_run_in_progress(&self,mode_id:u32)->Option<bool>{
|
||||
let mode=v0::ModeID(mode_id);
|
||||
Some(self.head.state().get_run(mode)?.is_in_progress())
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
pub fn is_run_finished(&self,mode_id:u32)->Option<bool>{
|
||||
let mode=v0::ModeID(mode_id);
|
||||
Some(self.head.state().get_run(mode)?.is_finished())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,32 @@
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
.hud {
|
||||
position: fixed;
|
||||
bottom: 10px;
|
||||
left: 10px;
|
||||
display: flex;
|
||||
flex-direction: column; /* stack vertically */
|
||||
gap: 4px; /* space between timers */
|
||||
}
|
||||
|
||||
.timer {
|
||||
background: #000;
|
||||
color: #fff;
|
||||
padding: 4px 8px;
|
||||
font-family: sans-serif;
|
||||
font-size: 14px;
|
||||
border-radius: 3px;
|
||||
}
|
||||
</style>
|
||||
<script defer src="player.js" type="module"></script>
|
||||
<script defer type="module" src="iframe-helper.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<canvas></canvas>
|
||||
<canvas id="viewport"> </canvas>
|
||||
<div class="hud">
|
||||
<div id="duration" class="timer">00:00:00</div>
|
||||
<div id="timer" class="timer">00:00:00</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -10,7 +10,7 @@ await init(); // load the wasm module
|
||||
const b = await fetch("bhop_marble_7cf33a64-7120-4514-b9fa-4fe29d9523d.qbot");
|
||||
const m = await fetch("bhop_marble_5692093612.snfm");
|
||||
|
||||
const canvas = document.querySelector("canvas");
|
||||
const canvas = document.getElementById("viewport");
|
||||
|
||||
const graphics = await setup_graphics(canvas);
|
||||
const bot = new CompleteBot(new Uint8Array(await b.arrayBuffer()));
|
||||
@@ -20,6 +20,19 @@ const playback = new PlaybackHead(0);
|
||||
graphics.change_map(map);
|
||||
|
||||
const startTime = performance.now();
|
||||
const timer = document.getElementById("timer");
|
||||
const duration = document.getElementById("duration");
|
||||
const MODE_MAIN = 0;
|
||||
|
||||
function timer_text(t) {
|
||||
const h = Math.floor(t / 3600);
|
||||
const m = Math.floor((t % 3600) / 60);
|
||||
const s = Math.floor(t % 60);
|
||||
const ms = Math.floor((t % 1) * 1000);
|
||||
return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}.${String(ms).padStart(3, "0")}`;
|
||||
}
|
||||
|
||||
duration.textContent = timer_text(bot.run_duration(MODE_MAIN));
|
||||
|
||||
function animate(now) {
|
||||
const elapsedMs = now - startTime;
|
||||
@@ -28,6 +41,10 @@ function animate(now) {
|
||||
// Advance the playback head to the current time
|
||||
playback.advance_time(bot, elapsedSec);
|
||||
|
||||
// update the timer text
|
||||
const time = playback.get_run_time(bot, elapsedSec, MODE_MAIN);
|
||||
timer.textContent = timer_text(time);
|
||||
|
||||
// Render the frame that the bot is at that time
|
||||
graphics.render(bot, playback, elapsedSec);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user