Compare commits

...

23 Commits

Author SHA1 Message Date
431e03549b Revert "outrageous resize hack"
This reverts commit 00dce54aea.
2025-01-06 23:22:48 -08:00
f34d34564e hack camera offset 2025-01-06 23:21:04 -08:00
ff98a95bdb include bot in binary 2025-01-06 23:21:04 -08:00
d8437567ac marble 2025-01-06 23:21:04 -08:00
c31bad8a06 implement looping 2025-01-06 23:21:04 -08:00
f51215fe4d working playback 2025-01-06 23:21:04 -08:00
60af3e2f6a add strafesnet_roblox_bot_file dep 2025-01-06 23:21:04 -08:00
98b880a82a bot player 2025-01-06 23:21:04 -08:00
265f2a2c70 toc 2025-01-06 20:41:16 -08:00
3c516badd4 include map in binary 2025-01-06 20:41:16 -08:00
00dce54aea outrageous resize hack 2025-01-06 20:41:16 -08:00
1c86219dbe begone root 2025-01-04 01:12:27 -08:00
cee5731e6c begone flex 2025-01-04 01:05:34 -08:00
f8583702be use existing canvas 2025-01-03 20:53:44 -08:00
275373d9bc my size 2025-01-03 19:35:27 -08:00
48e68d76a4 cap size to work around loop resize bug 2025-01-03 18:51:50 -08:00
66d4f100c6 use chrono instead of std::time 2025-01-03 18:42:46 -08:00
86d6f7c6a7 pick up wasm dep 2025-01-03 17:46:14 -08:00
3c78d9c94c color functions 2025-01-03 17:45:57 -08:00
e8da9d2a72 attach canvas 2025-01-03 06:45:03 -08:00
2f422e97dd replace enumerate_adapters with request_adapter 2025-01-03 06:45:03 -08:00
eb004cbb21 drop Send requirement 2025-01-03 06:45:03 -08:00
e944e7d91a Initial trunk 2025-01-03 06:45:03 -08:00
13 changed files with 286 additions and 55 deletions

71
Cargo.lock generated
View File

@ -67,6 +67,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04"
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
@ -361,6 +367,20 @@ dependencies = [
"num-traits",
]
[[package]]
name = "chrono"
version = "0.4.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets 0.52.6",
]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
@ -781,6 +801,29 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df"
[[package]]
name = "iana-time-zone"
version = "0.1.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core 0.52.0",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "icu_collections"
version = "1.5.0"
@ -2229,6 +2272,7 @@ name = "strafe-client"
version = "0.10.5"
dependencies = [
"bytemuck",
"chrono",
"configparser",
"ddsfile",
"glam",
@ -2239,7 +2283,11 @@ dependencies = [
"strafesnet_common",
"strafesnet_deferred_loader",
"strafesnet_rbx_loader",
"strafesnet_roblox_bot_file",
"strafesnet_snf",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"winit",
]
@ -2292,6 +2340,16 @@ dependencies = [
"strafesnet_common",
]
[[package]]
name = "strafesnet_roblox_bot_file"
version = "0.2.0"
source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
checksum = "e9c0c75ba772a5c430083c73d352049b84dd218947473189552d2e10785087b6"
dependencies = [
"binrw",
"bitflags 2.6.0",
]
[[package]]
name = "strafesnet_snf"
version = "0.2.0"
@ -2891,7 +2949,7 @@ dependencies = [
"web-sys",
"wgpu-types",
"windows",
"windows-core",
"windows-core 0.58.0",
]
[[package]]
@ -2920,7 +2978,16 @@ version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6"
dependencies = [
"windows-core",
"windows-core 0.58.0",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-core"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets 0.52.6",
]

View File

@ -16,6 +16,7 @@ roblox = ["dep:strafesnet_deferred_loader", "dep:strafesnet_rbx_loader"]
[dependencies]
bytemuck = { version = "1.13.1", features = ["derive"] }
chrono = "0.4.39"
configparser = "3.0.2"
ddsfile = "0.5.1"
glam = "0.29.0"
@ -26,6 +27,10 @@ strafesnet_bsp_loader = { path = "../lib/bsp_loader", registry = "strafesnet", o
strafesnet_common = { path = "../lib/common", registry = "strafesnet" }
strafesnet_deferred_loader = { path = "../lib/deferred_loader", features = ["legacy"], registry = "strafesnet", optional = true }
strafesnet_rbx_loader = { path = "../lib/rbx_loader", registry = "strafesnet", optional = true }
strafesnet_roblox_bot_file = { version = "0.2.0", registry = "strafesnet" }
strafesnet_snf = { path = "../lib/snf", registry = "strafesnet", optional = true }
wasm-bindgen = "0.2.99"
wasm-bindgen-futures = "0.4.49"
web-sys = { version = "0.3.76", features = ["console"] }
wgpu = "23.0.1"
winit = "0.30.7"

View File

@ -3,11 +3,11 @@ pub type INWorker<'a,Task>=CompatNWorker<'a,Task>;
pub struct CompatNWorker<'a,Task>{
data:std::marker::PhantomData<Task>,
f:Box<dyn FnMut(Task)+Send+'a>,
f:Box<dyn FnMut(Task)+'a>,
}
impl<'a,Task> CompatNWorker<'a,Task>{
pub fn new(f:impl FnMut(Task)+Send+'a)->CompatNWorker<'a,Task>{
pub fn new(f:impl FnMut(Task)+'a)->CompatNWorker<'a,Task>{
Self{
data:std::marker::PhantomData,
f:Box::new(f),

View File

@ -59,10 +59,13 @@ impl std::fmt::Display for LoadError{
}
impl std::error::Error for LoadError{}
pub fn load<P:AsRef<std::path::Path>>(path:P)->Result<strafesnet_common::map::CompleteMap,LoadError>{
pub fn load_file<P:AsRef<std::path::Path>>(path:P)->Result<strafesnet_common::map::CompleteMap,LoadError>{
//blocking because it's simpler...
let file=std::fs::File::open(path).map_err(LoadError::File)?;
match read(file).map_err(LoadError::ReadError)?{
load(file)
}
pub fn load<R:Read+std::io::Seek>(reader:R)->Result<strafesnet_common::map::CompleteMap,LoadError>{
match read(reader).map_err(LoadError::ReadError)?{
#[cfg(feature="snf")]
DataStructure::StrafesNET(map)=>Ok(map),
#[cfg(feature="roblox")]

View File

@ -34,12 +34,12 @@ pub fn new<'a>(
Instruction::Render(frame_state)=>{
if let Some((size,user_settings))=resize.take(){
println!("Resizing to {:?}",size);
let t0=std::time::Instant::now();
config.width=size.width.max(1);
config.height=size.height.max(1);
//let t0=std::time::Instant::now();
config.width=size.width.clamp(1,2560/2);
config.height=size.height.clamp(1,1440/2);
surface.configure(&device,&config);
graphics.resize(&device,&config,&user_settings);
println!("Resize took {:?}",t0.elapsed());
//println!("Resize took {:?}",t0.elapsed());
}
//this has to go deeper somehow
let frame=match surface.get_current_texture(){

View File

@ -13,5 +13,9 @@ mod physics_worker;
mod graphics_worker;
fn main(){
setup::setup_and_start(format!("Strafe Client v{}",env!("CARGO_PKG_VERSION")));
let title=format!("Strafe Client v{}",env!("CARGO_PKG_VERSION"));
#[cfg(target_arch="wasm32")]
wasm_bindgen_futures::spawn_local(setup::setup_and_start(title));
#[cfg(not(target_arch="wasm32"))]
pollster::block_on(setup::setup_and_start(title));
}

View File

@ -273,6 +273,15 @@ pub struct PhysicsCamera{
impl PhysicsCamera{
const ANGLE_PITCH_LOWER_LIMIT:Angle32=Angle32::NEG_FRAC_PI_2;
const ANGLE_PITCH_UPPER_LIMIT:Angle32=Angle32::FRAC_PI_2;
pub fn new(
sensitivity:Ratio64Vec2,
clamped_mouse_pos:glam::IVec2,
)->Self{
Self{
sensitivity,
clamped_mouse_pos,
}
}
pub fn move_mouse(&mut self,mouse_delta:glam::IVec2){
let mut unclamped_mouse_pos=self.clamped_mouse_pos+mouse_delta;
unclamped_mouse_pos.y=unclamped_mouse_pos.y.clamp(
@ -965,7 +974,7 @@ pub struct PhysicsState{
//input_state
input_state:InputState,
//style
style:StyleModifiers,//mode style with custom style updates applied
pub style:StyleModifiers,//mode style with custom style updates applied
//gameplay_state
mode_state:ModeState,
move_state:MoveState,
@ -1067,7 +1076,7 @@ impl PhysicsState{
#[derive(Default)]
pub struct PhysicsContext{
state:PhysicsState,//this captures the entire state of the physics.
pub state:PhysicsState,//this captures the entire state of the physics.
data:PhysicsData,//data currently loaded into memory which is needded for physics to run, but is not part of the state.
}
//the physics consumes the generic PhysicsInstruction, but can only emit the more narrow PhysicsInternalInstruction

View File

@ -212,14 +212,135 @@ impl MouseInterpolator{
}
}
fn vector3_to_glam(v:&strafesnet_roblox_bot_file::v0::Vector3)->glam::Vec3{
glam::vec3(v.x,v.y,v.z)
}
fn f32_to_p64(f:f32)->strafesnet_common::integer::Planar64{
f.try_into().unwrap_or(strafesnet_common::integer::Planar64::ZERO)
}
struct PlayBacker{
//Instructions
timelines:strafesnet_roblox_bot_file::v0::Block,
//"Simulation"
event_id:usize,
offset:f64,
duration:f64,
timer:Timer<Scaled>,
physics:crate::physics::PhysicsContext,
}
impl PlayBacker{
pub fn new(
physics:crate::physics::PhysicsContext,
timelines:strafesnet_roblox_bot_file::v0::Block,
)->Self{
let first=timelines.output_events.first().unwrap();
let last=timelines.output_events.last().unwrap();
Self{
offset:first.time,
duration:last.time-first.time,
timelines,
event_id:0,
timer:Timer::from_state(Scaled::identity(),false),
physics,
}
}
pub fn handle_instruction(&mut self,TimedInstruction{time,instruction}:&TimedInstruction<Instruction>){
//match the instruction so the playback is pausable :D
match instruction{
&Instruction::SetPaused(paused)=>{
let _=self.timer.set_paused(*time,paused);
},
_=>(),
}
let simulation_time=self.timer.time(*time);
let mut time_float=simulation_time.get() as f64/Time::ONE_SECOND.get() as f64+self.offset;
loop{
match self.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-=self.duration;
time_float-=self.duration;
},
}
}
}
pub fn get_frame_state(&self,time:Time)->crate::graphics::FrameState{
let time=self.timer.time(time);
let event0=&self.timelines.output_events[self.event_id];
let event1=&self.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/Time::ONE_SECOND.get() as f64;
let t=((time_float+self.offset-t0)/(t1-t0)) as f32;
let p=p0.lerp(p1,t).to_array().map(f32_to_p64);
let v=v0.lerp(v1,t).to_array().map(f32_to_p64);
let a=a0.lerp(a1,t).to_array().map(f32_to_p64);
//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);
// mask mantissa out and set it to minimum value
// let ax_epsilon=f32::from_bits(angles.x.to_bits()&!((1<<23)-1)|1);
// let ay_epsilon=f32::from_bits(angles.y.to_bits()&!((1<<23)-1)|1);
let body=crate::physics::Body{
time,
position:strafesnet_common::integer::Planar64Vec3::new(p)+self.physics.state.style.camera_offset,
velocity:strafesnet_common::integer::Planar64Vec3::new(v),
acceleration:strafesnet_common::integer::Planar64Vec3::new(a),
};
const FLOAT64_TO_ANGLE32_RADIANS:f64=((1i64<<31) as f64)/std::f64::consts::PI;
// xy is reversed in strafe client for some reason
let (ax,ay)=(
-angles.y as f64*FLOAT64_TO_ANGLE32_RADIANS,
-angles.x as f64*FLOAT64_TO_ANGLE32_RADIANS,
);
let camera=crate::physics::PhysicsCamera::new(
strafesnet_common::integer::Ratio64Vec2::new(1.0f32.try_into().unwrap(),1.0f32.try_into().unwrap()),
glam::ivec2(ax as i64 as i32,ay as i64 as i32)
);
crate::graphics::FrameState{
body,
camera,
time,
}
}
pub fn user_settings(&self)->crate::settings::UserSettings{
//oof, settings ignored
crate::settings::UserSettings::default()
}
pub fn change_map(&mut self,time:Time,map:&strafesnet_common::map::CompleteMap){
self.physics.generate_models(&map);
}
}
pub fn new<'a>(
mut graphics_worker:crate::compat_worker::INWorker<'a,crate::graphics_worker::Instruction>,
user_settings:crate::settings::UserSettings,
)->crate::compat_worker::QNWorker<'a,TimedInstruction<Instruction>>{
let physics=crate::physics::PhysicsContext::default();
let mut interpolator=MouseInterpolator::new(
//load bot
let data=include_bytes!("/home/quat/strafesnet/roblox_bot_file/files/bhop_marble_7cf33a64-7120-4514-b9fa-4fe29d9523d");
let mut bot_file=strafesnet_roblox_bot_file::v0::File::new(std::io::BufReader::new(std::io::Cursor::new(data))).unwrap();
let mut interpolator=PlayBacker::new(
physics,
user_settings
bot_file.read_all().unwrap(),
);
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
interpolator.handle_instruction(&ins);
@ -229,7 +350,7 @@ pub fn new<'a>(
graphics_worker.send(crate::graphics_worker::Instruction::Render(frame_state)).unwrap();
},
Instruction::Resize(size)=>{
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,interpolator.user_settings().clone())).unwrap();
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,interpolator.user_settings())).unwrap();
},
Instruction::ChangeMap(map)=>{
interpolator.change_map(ins.time,&map);

View File

@ -32,6 +32,16 @@ fn create_window(title:&str,event_loop:&winit::event_loop::EventLoop<()>)->Resul
use winit::platform::windows::WindowBuilderExtWindows;
builder=builder.with_no_redirection_bitmap(true);
}
#[cfg(target_arch="wasm32")]
{
use wasm_bindgen::JsCast;
use winit::platform::web::WindowAttributesExtWebSys;
let canvas=web_sys::window().unwrap()
.document().unwrap()
.get_element_by_id("canvas").unwrap()
.dyn_into::<web_sys::HtmlCanvasElement>().unwrap();
attr=attr.with_canvas(Some(canvas));
}
event_loop.create_window(attr)
}
fn create_instance()->SetupContextPartial1{
@ -61,36 +71,18 @@ struct SetupContextPartial2<'a>{
surface:wgpu::Surface<'a>,
}
impl<'a> SetupContextPartial2<'a>{
fn pick_adapter(self)->SetupContextPartial3<'a>{
async fn pick_adapter(self)->SetupContextPartial3<'a>{
let adapter;
//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=self.instance.enumerate_adapters(self.backends);
let mut chosen_adapter=None;
let mut chosen_adapter_score=0;
for adapter in adapters {
if !adapter.is_surface_supported(&self.surface) {
continue;
}
let score=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_features=adapter.features();
if chosen_adapter_score<score&&adapter_features.contains(required_features) {
chosen_adapter_score=score;
chosen_adapter=Some(adapter);
}
}
let chosen_adapter=self.instance.request_adapter(&wgpu::RequestAdapterOptions{
power_preference:wgpu::PowerPreference::HighPerformance,
force_fallback_adapter:false,
compatible_surface:Some(&self.surface),
}).await;
if let Some(maybe_chosen_adapter)=chosen_adapter{
adapter=maybe_chosen_adapter;
@ -129,7 +121,7 @@ struct SetupContextPartial3<'a>{
adapter:wgpu::Adapter,
}
impl<'a> SetupContextPartial3<'a>{
fn request_device(self)->SetupContextPartial4<'a>{
async fn request_device(self)->SetupContextPartial4<'a>{
let optional_features=optional_features();
let required_features=required_features();
@ -137,7 +129,7 @@ impl<'a> SetupContextPartial3<'a>{
let needed_limits=required_limits().using_resolution(self.adapter.limits());
let trace_dir=std::env::var("WGPU_TRACE");
let (device, queue)=pollster::block_on(self.adapter
let (device, queue)=self.adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
@ -146,7 +138,7 @@ impl<'a> SetupContextPartial3<'a>{
memory_hints:wgpu::MemoryHints::Performance,
},
trace_dir.ok().as_ref().map(std::path::Path::new),
))
).await
.expect("Unable to find a suitable GPU adapter!");
SetupContextPartial4{
@ -192,7 +184,7 @@ pub struct SetupContext<'a>{
pub config:wgpu::SurfaceConfiguration,
}
pub fn setup_and_start(title:String){
pub async fn setup_and_start(title:String){
let event_loop=winit::event_loop::EventLoop::new().unwrap();
println!("Initializing the surface...");
@ -203,9 +195,9 @@ pub fn setup_and_start(title:String){
let partial_2=partial_1.create_surface(&window).unwrap();
let partial_3=partial_2.pick_adapter();
let partial_3=partial_2.pick_adapter().await;
let partial_4=partial_3.request_device();
let partial_4=partial_3.request_device().await;
let size=window.inner_size();
@ -219,26 +211,26 @@ pub fn setup_and_start(title:String){
setup_context,
);
if let Some(arg)=std::env::args().nth(1){
let path=std::path::PathBuf::from(arg);
//if let Some(arg)=std::env::args().nth(1){
//let path=std::path::PathBuf::from(arg);
window_thread.send(TimedInstruction{
time:integer::Time::ZERO,
instruction:WindowInstruction::WindowEvent(winit::event::WindowEvent::DroppedFile(path)),
instruction:WindowInstruction::WindowEvent(winit::event::WindowEvent::DroppedFile("".into())),
}).unwrap();
};
//};
println!("Entering event loop...");
let root_time=std::time::Instant::now();
let root_time=chrono::Utc::now();
run_event_loop(event_loop,window_thread,root_time).unwrap();
}
fn run_event_loop(
event_loop:winit::event_loop::EventLoop<()>,
mut window_thread:crate::compat_worker::QNWorker<TimedInstruction<WindowInstruction>>,
root_time:std::time::Instant
root_time:chrono::DateTime<chrono::Utc>,
)->Result<(),winit::error::EventLoopError>{
event_loop.run(move |event,elwt|{
let time=integer::Time::from_nanos(root_time.elapsed().as_nanos() as i64);
let time=integer::Time::from_nanos((chrono::Utc::now()-root_time).num_nanoseconds().unwrap());
// *control_flow=if cfg!(feature="metal-auto-capture"){
// winit::event_loop::ControlFlow::Exit
// }else{

View File

@ -25,8 +25,10 @@ impl WindowContext<'_>{
}
fn window_event(&mut self,time:integer::Time,event:winit::event::WindowEvent){
match event{
winit::event::WindowEvent::DroppedFile(path)=>{
match crate::file::load(path.as_path()){
winit::event::WindowEvent::DroppedFile(_path)=>{
//match crate::file::load_file(path.as_path()){
let data=include_bytes!("/run/media/quat/Files/Documents/map-files/verify-scripts/maps/bhop_snfm/5692093612.snfm");
match crate::file::load(std::io::Cursor::new(data.as_slice())){
Ok(map)=>self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::ChangeMap(map)}).unwrap(),
Err(e)=>println!("Failed to load map: {e}"),
}

1
web-client/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/dist

2
web-client/Trunk.toml Normal file
View File

@ -0,0 +1,2 @@
[build]
target = "index.html"

25
web-client/index.html Normal file
View File

@ -0,0 +1,25 @@
<!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">
body {
margin: 0px;
background: #fff;
width: 100%;
height: 100%;
}
.main-canvas {
margin: 0;
}
</style>
</head>
<body>
<canvas class="main-canvas" id="canvas"></canvas>
<link data-trunk rel="rust" href="../strafe-client/Cargo.toml"/>
</body>
</html>