forked from StrafesNET/strafe-project
Compare commits
13 Commits
bot-worker
...
mouse-inte
Author | SHA1 | Date | |
---|---|---|---|
ead0e33546 | |||
b376c03bde | |||
d7dc08092a | |||
d898d7cf67 | |||
7f68fd9b21 | |||
0b8a433640 | |||
9646804dcd | |||
9cd72d5809 | |||
9ecfe26a0c | |||
fbb2ba369c | |||
d1f78b6e18 | |||
8cade8134f | |||
3268d92d24 |
90
Cargo.lock
generated
90
Cargo.lock
generated
@ -262,9 +262,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck"
|
||||
version = "1.16.3"
|
||||
version = "1.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "102087e286b4677862ea56cf8fc58bb2cdfa8725c40ffb80fe3a008eb7f2fc83"
|
||||
checksum = "b236fc92302c97ed75b38da1f4917b5cdda4984745740f153a5d3059e48d725e"
|
||||
dependencies = [
|
||||
"bytemuck_derive",
|
||||
]
|
||||
@ -294,9 +294,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.7.1"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50"
|
||||
checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952"
|
||||
|
||||
[[package]]
|
||||
name = "calloop"
|
||||
@ -326,9 +326,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.1.8"
|
||||
version = "1.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "504bdec147f2cc13c8b57ed9401fd8a147cc66b67ad5cb241394244f2c947549"
|
||||
checksum = "26a5c3fd7bfa1ce3897a3a3501d362b2d87b7f2583ebcb4a949ec25911025cbc"
|
||||
dependencies = [
|
||||
"jobserver",
|
||||
"libc",
|
||||
@ -802,9 +802,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.3.0"
|
||||
version = "2.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "de3fc2e30ba82dd1b3911c8de1ffc143c74a914a14e99514d7637e3099df5ea0"
|
||||
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
|
||||
dependencies = [
|
||||
"equivalent",
|
||||
"hashbrown",
|
||||
@ -1040,9 +1040,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "naga"
|
||||
version = "22.1.0"
|
||||
version = "22.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8bd5a652b6faf21496f2cfd88fc49989c8db0825d1f6746b1a71a6ede24a63ad"
|
||||
checksum = "09eeccb9b50f4f7839b214aa3e08be467159506a986c18e0702170ccf720a453"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bit-set",
|
||||
@ -1460,12 +1460,9 @@ checksum = "22686f4785f02a4fcc856d3b3bb19bf6c8160d103f7a99cc258bddd0251dc7f2"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.20"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
|
||||
dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
||||
|
||||
[[package]]
|
||||
name = "presser"
|
||||
@ -1677,9 +1674,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.6"
|
||||
version = "1.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619"
|
||||
checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
@ -1880,7 +1877,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "strafe-client"
|
||||
version = "0.10.3"
|
||||
version = "0.10.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"configparser",
|
||||
@ -1900,9 +1897,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "strafesnet_bsp_loader"
|
||||
version = "0.1.4"
|
||||
version = "0.1.3"
|
||||
source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
||||
checksum = "b6b3e1324034abfd648e339580989f8f2c30ac2009296229349d88b8fcb4eedd"
|
||||
checksum = "6d4af68c422b5f57febbaa218f44ba02d413fd25e84afff9e45e557a8caee2ce"
|
||||
dependencies = [
|
||||
"glam",
|
||||
"strafesnet_common",
|
||||
@ -1912,11 +1909,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "strafesnet_common"
|
||||
version = "0.3.0"
|
||||
version = "0.2.0"
|
||||
source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
||||
checksum = "1077d45a0b064964906a57de765a5a2bfe47b41f2f807d13b18c70765e76d3dd"
|
||||
checksum = "74580c59a09194ce39db49cd814a5c2fc2d61513c88c6b811b5b40c0da6de057"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bitflags 2.6.0",
|
||||
"glam",
|
||||
"id",
|
||||
@ -1924,9 +1920,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "strafesnet_deferred_loader"
|
||||
version = "0.3.2"
|
||||
version = "0.3.1"
|
||||
source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
||||
checksum = "9d5ad437524fb201fd5be68f76c53dd831e81ccad4655e19e3d1ca201863b566"
|
||||
checksum = "c3891dcbdbc20b03cf561786b810e839ae7c11dd8810fd005f2474805ee9cccc"
|
||||
dependencies = [
|
||||
"lazy-regex",
|
||||
"strafesnet_common",
|
||||
@ -1935,9 +1931,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "strafesnet_rbx_loader"
|
||||
version = "0.3.3"
|
||||
version = "0.3.2"
|
||||
source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
||||
checksum = "3a910867e1f5ab2d9cc9c178973aee7fa029547e27465e47fea2eb99b860bb81"
|
||||
checksum = "21ea93b0170063dd2a063a138c41e6f7a6c14a82c6553fa4ba32df65a26efc6e"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"glam",
|
||||
@ -1952,9 +1948,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "strafesnet_snf"
|
||||
version = "0.1.3-bot"
|
||||
version = "0.1.1"
|
||||
source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
||||
checksum = "69448a3ed6ab5e9886cf83a3b2358c1f05d652cde4839963cd867978b924b52b"
|
||||
checksum = "78479f73437a3f10230efd2304be0f3ef30dff98c54d93613ed1621bfd6a7da6"
|
||||
dependencies = [
|
||||
"binrw 0.14.0",
|
||||
"id",
|
||||
@ -2056,9 +2052,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "0.6.8"
|
||||
version = "0.6.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
|
||||
checksum = "f8fb9f64314842840f1d940ac544da178732128f1c78c21772e876579e0da1db"
|
||||
|
||||
[[package]]
|
||||
name = "toml_edit"
|
||||
@ -2104,9 +2100,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ttf-parser"
|
||||
version = "0.24.1"
|
||||
version = "0.24.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5be21190ff5d38e8b4a2d3b6a3ae57f612cc39c96e83cedeaf7abc338a8bac4a"
|
||||
checksum = "8686b91785aff82828ed725225925b33b4fde44c4bb15876e5f7c832724c420a"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
@ -2400,9 +2396,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "wgpu"
|
||||
version = "22.1.0"
|
||||
version = "22.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e1d1c4ba43f80542cf63a0a6ed3134629ae73e8ab51e4b765a67f3aa062eb433"
|
||||
checksum = "c87e07e87a179614940ad845397e03201847453a37b43a31a3b54eee2e6e32ce"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"cfg_aliases 0.1.1",
|
||||
@ -2425,9 +2421,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "wgpu-core"
|
||||
version = "22.1.0"
|
||||
version = "22.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0348c840d1051b8e86c3bcd31206080c5e71e5933dabd79be1ce732b0b2f089a"
|
||||
checksum = "e0f191908a21968991463fcf3b42cb6c9648c0fb7fa301b8fc733bc21a9ed9bd"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bit-vec",
|
||||
@ -2528,11 +2524,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.9"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
||||
checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b"
|
||||
dependencies = [
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2578,15 +2574,6 @@ dependencies = [
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.59.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
|
||||
dependencies = [
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.42.2"
|
||||
@ -2885,9 +2872,9 @@ checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56"
|
||||
|
||||
[[package]]
|
||||
name = "xml-rs"
|
||||
version = "0.8.21"
|
||||
version = "0.8.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "539a77ee7c0de333dcc6da69b177380a0b81e0dacfa4f7344c465a36871ee601"
|
||||
checksum = "791978798f0597cfc70478424c2b4fdc2b7a8024aaff78497ef00f24ef674193"
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
@ -2895,7 +2882,6 @@ version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||
dependencies = [
|
||||
"byteorder 1.5.0",
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "strafe-client"
|
||||
version = "0.10.3"
|
||||
version = "0.10.1"
|
||||
edition = "2021"
|
||||
repository = "https://git.itzana.me/StrafesNET/strafe-client"
|
||||
license = "Custom"
|
||||
@ -23,10 +23,10 @@ id = { version = "0.1.0", registry = "strafesnet" }
|
||||
parking_lot = "0.12.1"
|
||||
pollster = "0.3.0"
|
||||
strafesnet_bsp_loader = { version = "0.1.3", registry = "strafesnet", optional = true }
|
||||
strafesnet_common = { version = "0.3.0", registry = "strafesnet" }
|
||||
strafesnet_common = { version = "0.2.0", registry = "strafesnet" }
|
||||
strafesnet_deferred_loader = { version = "0.3.1", features = ["legacy"], registry = "strafesnet", optional = true }
|
||||
strafesnet_rbx_loader = { version = "0.3.2", registry = "strafesnet", optional = true }
|
||||
strafesnet_snf = { version = "0.1.3-bot", registry = "strafesnet", optional = true }
|
||||
strafesnet_snf = { version = "0.1.0", registry = "strafesnet", optional = true }
|
||||
wgpu = "22.0.0"
|
||||
winit = "0.30.4"
|
||||
|
||||
|
@ -1,35 +0,0 @@
|
||||
use strafesnet_snf::bot::BotDebug;
|
||||
|
||||
pub enum Instruction{
|
||||
//TODO: pass map id
|
||||
Create,
|
||||
//Delete,
|
||||
Push{
|
||||
ins:strafesnet_common::instruction::TimedInstruction<strafesnet_common::physics::Instruction>,
|
||||
},
|
||||
}
|
||||
|
||||
//a new worker is spawned on a thread
|
||||
//create opens a new file
|
||||
//push pushes an instruction to the file
|
||||
|
||||
pub struct Worker{
|
||||
file:BotDebug,
|
||||
}
|
||||
|
||||
fn date()->String{
|
||||
format!("/run/media/quat/Files/Documents/Strafe Client/debug_bots_v2/{}",std::time::SystemTime::UNIX_EPOCH.elapsed().unwrap().as_nanos())
|
||||
}
|
||||
|
||||
pub fn new<'a>(scope:&'a std::thread::Scope<'a,'_>)->crate::worker::QNWorker<'a,Instruction>{
|
||||
let mut worker=Worker{
|
||||
file:BotDebug::new(date()).unwrap(),
|
||||
};
|
||||
crate::worker::QNWorker::new(scope,move|instruction|{
|
||||
match instruction{
|
||||
Instruction::Create=>worker.file=BotDebug::new(date()).unwrap(),
|
||||
//Instruction::Delete=>worker.file.delete().unwrap(),
|
||||
Instruction::Push{ins}=>worker.file.push(ins).unwrap(),
|
||||
}
|
||||
})
|
||||
}
|
@ -816,7 +816,7 @@ impl GraphicsState{
|
||||
});
|
||||
|
||||
let camera=GraphicsCamera::default();
|
||||
let camera_uniforms=camera.to_uniform_data(crate::physics::PhysicsOutputState::default().extrapolate(strafesnet_common::mouse::MouseState::default()));
|
||||
let camera_uniforms=camera.to_uniform_data(crate::physics::PhysicsOutputState::default().extrapolate(crate::physics::MouseState::default()));
|
||||
let camera_buf=device.create_buffer_init(&wgpu::util::BufferInitDescriptor{
|
||||
label:Some("Camera"),
|
||||
contents:bytemuck::cast_slice(&camera_uniforms),
|
||||
@ -893,7 +893,7 @@ impl GraphicsState{
|
||||
let mut encoder=device.create_command_encoder(&wgpu::CommandEncoderDescriptor{label:None});
|
||||
|
||||
// update rotation
|
||||
let camera_uniforms=self.camera.to_uniform_data(physics_output.extrapolate(strafesnet_common::mouse::MouseState{pos:mouse_pos,time:predicted_time}));
|
||||
let camera_uniforms=self.camera.to_uniform_data(physics_output.extrapolate(crate::physics::MouseState{pos:mouse_pos,time:predicted_time}));
|
||||
self.staging_belt
|
||||
.write_buffer(
|
||||
&mut encoder,
|
||||
|
@ -4,7 +4,8 @@ pub enum Instruction{
|
||||
Render(crate::physics::PhysicsOutputState,integer::Time,glam::IVec2),
|
||||
//UpdateModel(crate::graphics::GraphicsModelUpdate),
|
||||
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
|
||||
ChangeMap(strafesnet_common::map::CompleteMap),
|
||||
GenerateModels(strafesnet_common::map::CompleteMap),
|
||||
ClearModels,
|
||||
}
|
||||
|
||||
//Ideally the graphics thread worker description is:
|
||||
@ -26,10 +27,12 @@ pub fn new<'a>(
|
||||
let mut resize=None;
|
||||
crate::compat_worker::INWorker::new(move |ins:Instruction|{
|
||||
match ins{
|
||||
Instruction::ChangeMap(map)=>{
|
||||
graphics.clear();
|
||||
Instruction::GenerateModels(map)=>{
|
||||
graphics.generate_models(&device,&queue,&map);
|
||||
},
|
||||
Instruction::ClearModels=>{
|
||||
graphics.clear();
|
||||
},
|
||||
Instruction::Resize(size,user_settings)=>{
|
||||
resize=Some((size,user_settings));
|
||||
}
|
||||
@ -66,4 +69,4 @@ pub fn new<'a>(
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
mod file;
|
||||
mod setup;
|
||||
mod timer;
|
||||
mod window;
|
||||
mod worker;
|
||||
mod physics;
|
||||
mod graphics;
|
||||
mod settings;
|
||||
mod bot_worker;
|
||||
mod face_crawler;
|
||||
mod compat_worker;
|
||||
mod model_physics;
|
||||
|
344
src/physics.rs
344
src/physics.rs
@ -2,10 +2,8 @@ use std::collections::{HashMap,HashSet};
|
||||
use crate::model_physics::{self,PhysicsMesh,PhysicsMeshTransform,TransformedMesh,MeshQuery,PhysicsMeshId,PhysicsSubmeshId};
|
||||
use strafesnet_common::bvh;
|
||||
use strafesnet_common::map;
|
||||
use strafesnet_common::run;
|
||||
use strafesnet_common::aabb;
|
||||
use strafesnet_common::model::{MeshId,ModelId};
|
||||
use strafesnet_common::mouse::MouseState;
|
||||
use strafesnet_common::gameplay_attributes::{self,CollisionAttributesId};
|
||||
use strafesnet_common::gameplay_modes::{self,StageId};
|
||||
use strafesnet_common::gameplay_style::{self,StyleModifiers};
|
||||
@ -14,26 +12,40 @@ use strafesnet_common::instruction::{self,InstructionEmitter,InstructionConsumer
|
||||
use strafesnet_common::integer::{self,Time,Planar64,Planar64Vec3,Planar64Mat3,Angle32,Ratio64Vec2};
|
||||
use gameplay::ModeState;
|
||||
|
||||
//external influence
|
||||
//this is how you influence the physics from outside
|
||||
use strafesnet_common::physics::Instruction as PhysicsInputInstruction;
|
||||
|
||||
//internal influence
|
||||
//when the physics asks itself what happens next, this is how it's represented
|
||||
#[derive(Debug)]
|
||||
enum PhysicsInternalInstruction{
|
||||
pub enum PhysicsInstruction {
|
||||
CollisionStart(Collision),
|
||||
CollisionEnd(Collision),
|
||||
StrafeTick,
|
||||
ReachWalkTargetVelocity,
|
||||
// Water,
|
||||
// Spawn(
|
||||
// Option<SpawnId>,
|
||||
// bool,//true = Trigger; false = teleport
|
||||
// bool,//true = Force
|
||||
// )
|
||||
//InputInstructions conditionally activate RefreshWalkTarget (by doing what SetWalkTargetVelocity used to do and then flagging it)
|
||||
Input(PhysicsInputInstruction),
|
||||
SetSensitivity(Ratio64Vec2),
|
||||
}
|
||||
#[derive(Debug)]
|
||||
enum PhysicsInstruction{
|
||||
Internal(PhysicsInternalInstruction),
|
||||
//InputInstructions conditionally activate RefreshWalkTarget
|
||||
//(by doing what SetWalkTargetVelocity used to do and then flagging it)
|
||||
Input(PhysicsInputInstruction),
|
||||
pub enum PhysicsInputInstruction {
|
||||
ReplaceMouse(MouseState,MouseState),
|
||||
SetNextMouse(MouseState),
|
||||
SetMoveRight(bool),
|
||||
SetMoveUp(bool),
|
||||
SetMoveBack(bool),
|
||||
SetMoveLeft(bool),
|
||||
SetMoveDown(bool),
|
||||
SetMoveForward(bool),
|
||||
SetJump(bool),
|
||||
SetZoom(bool),
|
||||
Reset,
|
||||
Idle,
|
||||
//Idle: there were no input events, but the simulation is safe to advance to this timestep
|
||||
//for interpolation / networking / playback reasons, most playback heads will always want
|
||||
//to be 1 instruction ahead to generate the next state for interpolation.
|
||||
PracticeFly,
|
||||
}
|
||||
|
||||
#[derive(Clone,Copy,Debug,Default,Hash)]
|
||||
@ -55,6 +67,32 @@ impl std::ops::Neg for Body{
|
||||
}
|
||||
}
|
||||
|
||||
//hey dumbass just use a delta
|
||||
#[derive(Clone,Debug)]
|
||||
pub struct MouseState {
|
||||
pub pos: glam::IVec2,
|
||||
pub time:Time,
|
||||
}
|
||||
impl Default for MouseState{
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
time:Time::ZERO,
|
||||
pos:glam::IVec2::ZERO,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl MouseState {
|
||||
pub fn lerp(&self,target:&MouseState,time:Time)->glam::IVec2 {
|
||||
let m0=self.pos.as_i64vec2();
|
||||
let m1=target.pos.as_i64vec2();
|
||||
//these are deltas
|
||||
let t1t=(target.time-time).nanos();
|
||||
let tt0=(time-self.time).nanos();
|
||||
let dt=(target.time-self.time).nanos();
|
||||
((m0*t1t+m1*tt0)/dt).as_ivec2()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone,Debug,Default)]
|
||||
pub struct InputState{
|
||||
mouse:MouseState,
|
||||
@ -68,8 +106,6 @@ impl InputState{
|
||||
fn set_next_mouse(&mut self,next_mouse:MouseState){
|
||||
//I like your functions magic language
|
||||
self.mouse=std::mem::replace(&mut self.next_mouse,next_mouse);
|
||||
//equivalently:
|
||||
//(self.next_mouse,self.mouse)=(next_mouse,self.next_mouse.clone());
|
||||
}
|
||||
fn replace_mouse(&mut self,mouse:MouseState,next_mouse:MouseState){
|
||||
(self.next_mouse,self.mouse)=(next_mouse,mouse);
|
||||
@ -211,8 +247,8 @@ impl PhysicsModels{
|
||||
&model.transform
|
||||
)
|
||||
}
|
||||
fn model(&self,model_id:PhysicsModelId)->&PhysicsModel{
|
||||
&self.models[&model_id]
|
||||
fn model(&self,model_id:PhysicsModelId)->Option<&PhysicsModel>{
|
||||
self.models.get(&model_id)
|
||||
}
|
||||
fn attr(&self,model_id:PhysicsModelId)->&PhysicsCollisionAttributes{
|
||||
&self.attributes[&self.models[&model_id].attr_id]
|
||||
@ -520,13 +556,13 @@ impl MoveState{
|
||||
=>None,
|
||||
}
|
||||
}
|
||||
fn next_move_instruction(&self,strafe:&Option<gameplay_style::StrafeSettings>,time:Time)->Option<TimedInstruction<PhysicsInternalInstruction>>{
|
||||
fn next_move_instruction(&self,strafe:&Option<gameplay_style::StrafeSettings>,time:Time)->Option<TimedInstruction<PhysicsInstruction>>{
|
||||
//check if you have a valid walk state and create an instruction
|
||||
match self{
|
||||
MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>match &walk_state.target{
|
||||
&TransientAcceleration::Reachable{acceleration:_,time}=>Some(TimedInstruction{
|
||||
time,
|
||||
instruction:PhysicsInternalInstruction::ReachWalkTargetVelocity
|
||||
instruction:PhysicsInstruction::ReachWalkTargetVelocity
|
||||
}),
|
||||
TransientAcceleration::Unreachable{acceleration:_}
|
||||
|TransientAcceleration::Reached
|
||||
@ -536,7 +572,7 @@ impl MoveState{
|
||||
TimedInstruction{
|
||||
time:strafe.next_tick(time),
|
||||
//only poll the physics if there is a before and after mouse event
|
||||
instruction:PhysicsInternalInstruction::StrafeTick
|
||||
instruction:PhysicsInstruction::StrafeTick
|
||||
}
|
||||
}),
|
||||
MoveState::Water=>None,//TODO
|
||||
@ -730,7 +766,7 @@ impl TouchingState{
|
||||
}
|
||||
}
|
||||
}
|
||||
fn predict_collision_end(&self,collector:&mut instruction::InstructionCollector<PhysicsInternalInstruction>,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,body:&Body,time:Time){
|
||||
fn predict_collision_end(&self,collector:&mut instruction::InstructionCollector<PhysicsInstruction>,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,body:&Body,time:Time){
|
||||
let relative_body=VirtualBody::relative(&Body::default(),body).body(time);
|
||||
for contact in &self.contacts{
|
||||
//detect face slide off
|
||||
@ -739,7 +775,7 @@ impl TouchingState{
|
||||
collector.collect(minkowski.predict_collision_face_out(&relative_body,collector.time(),contact.face_id).map(|(_face,time)|{
|
||||
TimedInstruction{
|
||||
time,
|
||||
instruction:PhysicsInternalInstruction::CollisionEnd(
|
||||
instruction:PhysicsInstruction::CollisionEnd(
|
||||
Collision::Contact(ContactCollision{convex_mesh_id:contact.convex_mesh_id,face_id:contact.face_id})
|
||||
),
|
||||
}
|
||||
@ -752,7 +788,7 @@ impl TouchingState{
|
||||
collector.collect(minkowski.predict_collision_out(&relative_body,collector.time()).map(|(_face,time)|{
|
||||
TimedInstruction{
|
||||
time,
|
||||
instruction:PhysicsInternalInstruction::CollisionEnd(
|
||||
instruction:PhysicsInstruction::CollisionEnd(
|
||||
Collision::Intersect(IntersectCollision{convex_mesh_id:intersect.convex_mesh_id})
|
||||
),
|
||||
}
|
||||
@ -868,10 +904,6 @@ pub struct PhysicsState{
|
||||
//gameplay_state
|
||||
mode_state:ModeState,
|
||||
move_state:MoveState,
|
||||
//run is non optional: when you spawn in a run is created
|
||||
//the run cannot be finished unless you start it by visiting
|
||||
//a start zone. If you change mode, a new run is created.
|
||||
run:run::Run,
|
||||
}
|
||||
//random collection of contextual data that doesn't belong in PhysicsState
|
||||
pub struct PhysicsData{
|
||||
@ -891,12 +923,11 @@ impl Default for PhysicsState{
|
||||
time:Time::ZERO,
|
||||
style:StyleModifiers::default(),
|
||||
touching:TouchingState::default(),
|
||||
move_state:MoveState::Air,
|
||||
move_state: MoveState::Air,
|
||||
camera:PhysicsCamera::default(),
|
||||
input_state:InputState::default(),
|
||||
world:WorldState{},
|
||||
mode_state:ModeState::default(),
|
||||
run:run::Run::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -911,16 +942,15 @@ impl Default for PhysicsData{
|
||||
}
|
||||
}
|
||||
|
||||
impl PhysicsState{
|
||||
impl PhysicsState {
|
||||
fn clear(&mut self){
|
||||
self.touching.clear();
|
||||
}
|
||||
fn reset_to_default(&mut self){
|
||||
let mut new_state=Self::default();
|
||||
new_state.camera.sensitivity=self.camera.sensitivity;
|
||||
*self=new_state;
|
||||
fn advance_time(&mut self, time: Time){
|
||||
self.body.advance_time(time);
|
||||
self.time=time;
|
||||
}
|
||||
fn next_move_instruction(&self)->Option<TimedInstruction<PhysicsInternalInstruction>>{
|
||||
fn next_move_instruction(&self)->Option<TimedInstruction<PhysicsInstruction>>{
|
||||
self.move_state.next_move_instruction(&self.style.strafe,self.time)
|
||||
}
|
||||
//lmao idk this is convenient
|
||||
@ -993,19 +1023,33 @@ pub struct PhysicsContext{
|
||||
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
|
||||
impl instruction::InstructionConsumer<PhysicsInstruction> for PhysicsContext{
|
||||
fn process_instruction(&mut self,ins:TimedInstruction<PhysicsInstruction>){
|
||||
atomic_state_update(&mut self.state,&self.data,ins)
|
||||
}
|
||||
}
|
||||
impl instruction::InstructionEmitter<PhysicsInternalInstruction> for PhysicsContext{
|
||||
impl instruction::InstructionEmitter<PhysicsInstruction> for PhysicsContext{
|
||||
//this little next instruction function can cache its return value and invalidate the cached value by watching the State.
|
||||
fn next_instruction(&self,time_limit:Time)->Option<TimedInstruction<PhysicsInternalInstruction>>{
|
||||
next_instruction_internal(&self.state,&self.data,time_limit)
|
||||
fn next_instruction(&self,time_limit:Time)->Option<TimedInstruction<PhysicsInstruction>>{
|
||||
literally_next_instruction_but_with_context(&self.state,&self.data,time_limit)
|
||||
}
|
||||
}
|
||||
impl PhysicsContext{
|
||||
pub fn clear(&mut self){
|
||||
self.state.clear();
|
||||
}
|
||||
pub fn load_user_settings(&mut self,user_settings:&crate::settings::UserSettings){
|
||||
self.process_instruction(TimedInstruction{
|
||||
time:self.state.time,
|
||||
instruction:PhysicsInstruction::SetSensitivity(user_settings.calculate_sensitivity()),
|
||||
});
|
||||
}
|
||||
pub fn spawn(&mut self){
|
||||
self.process_instruction(TimedInstruction{
|
||||
time:self.state.time,
|
||||
instruction:PhysicsInstruction::Input(PhysicsInputInstruction::Reset),
|
||||
});
|
||||
}
|
||||
pub const fn output(&self)->PhysicsOutputState{
|
||||
PhysicsOutputState{
|
||||
body:self.state.body,
|
||||
@ -1017,13 +1061,8 @@ impl PhysicsContext{
|
||||
pub const fn get_next_mouse(&self)->&MouseState{
|
||||
self.state.input_state.get_next_mouse()
|
||||
}
|
||||
/// use with caution, this is the only non-instruction way to mess with physics
|
||||
pub fn generate_models(&mut self,map:&map::CompleteMap){
|
||||
self.state.clear();
|
||||
self.data.modes=map.modes.clone();
|
||||
for mode in &mut self.data.modes.modes{
|
||||
mode.denormalize_data();
|
||||
}
|
||||
let mut used_attributes=Vec::new();
|
||||
let mut physics_attr_id_from_model_attr_id=HashMap::<CollisionAttributesId,PhysicsAttributesId>::new();
|
||||
let mut used_meshes=Vec::new();
|
||||
@ -1091,19 +1130,16 @@ impl PhysicsContext{
|
||||
}
|
||||
|
||||
//tickless gaming
|
||||
fn run_internal_exhaustive(&mut self,time_limit:Time){
|
||||
fn run(&mut self,time_limit:Time){
|
||||
//prepare is ommitted - everything is done via instructions.
|
||||
while let Some(instruction)=self.next_instruction(time_limit){//collect
|
||||
//process
|
||||
self.process_instruction(TimedInstruction{
|
||||
time:instruction.time,
|
||||
instruction:PhysicsInstruction::Internal(instruction.instruction),
|
||||
});
|
||||
self.process_instruction(instruction);
|
||||
//write hash lol
|
||||
}
|
||||
}
|
||||
pub fn run_input_instruction(&mut self,instruction:TimedInstruction<PhysicsInputInstruction>){
|
||||
self.run_internal_exhaustive(instruction.time);
|
||||
self.run(instruction.time);
|
||||
self.process_instruction(TimedInstruction{
|
||||
time:instruction.time,
|
||||
instruction:PhysicsInstruction::Input(instruction.instruction),
|
||||
@ -1111,8 +1147,7 @@ impl PhysicsContext{
|
||||
}
|
||||
}
|
||||
|
||||
//this is the one who asks
|
||||
fn next_instruction_internal(state:&PhysicsState,data:&PhysicsData,time_limit:Time)->Option<TimedInstruction<PhysicsInternalInstruction>>{
|
||||
fn literally_next_instruction_but_with_context(state:&PhysicsState,data:&PhysicsData,time_limit:Time)->Option<TimedInstruction<PhysicsInstruction>>{
|
||||
//JUST POLLING!!! NO MUTATION
|
||||
let mut collector = instruction::InstructionCollector::new(time_limit);
|
||||
|
||||
@ -1124,18 +1159,17 @@ impl PhysicsContext{
|
||||
let mut aabb=aabb::Aabb::default();
|
||||
state.body.grow_aabb(&mut aabb,state.time,collector.time());
|
||||
aabb.inflate(data.hitbox_mesh.halfsize);
|
||||
//relative to moving platforms
|
||||
//let relative_body=&VirtualBody::relative(&Body::default(),&state.body).body(state.time);
|
||||
let relative_body=&state.body;
|
||||
//common body
|
||||
let relative_body=VirtualBody::relative(&Body::default(),&state.body).body(state.time);
|
||||
data.bvh.the_tester(&aabb,&mut |&convex_mesh_id|{
|
||||
//no checks are needed because of the time limits.
|
||||
let model_mesh=data.models.mesh(convex_mesh_id);
|
||||
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(model_mesh,data.hitbox_mesh.transformed_mesh());
|
||||
collector.collect(minkowski.predict_collision_in(relative_body,collector.time())
|
||||
collector.collect(minkowski.predict_collision_in(&relative_body,collector.time())
|
||||
//temp (?) code to avoid collision loops
|
||||
.map_or(None,|(face,time)|if time<=state.time{None}else{Some((face,time))})
|
||||
.map_or(None,|(face,time)|if time==state.time{None}else{Some((face,time))})
|
||||
.map(|(face,time)|{
|
||||
TimedInstruction{time,instruction:PhysicsInternalInstruction::CollisionStart(match data.models.attr(convex_mesh_id.model_id){
|
||||
TimedInstruction{time,instruction:PhysicsInstruction::CollisionStart(match data.models.attr(convex_mesh_id.model_id){
|
||||
PhysicsCollisionAttributes::Contact{contacting:_,general:_}=>Collision::Contact(ContactCollision{convex_mesh_id,face_id:face}),
|
||||
PhysicsCollisionAttributes::Intersect{intersecting:_,general:_}=>Collision::Intersect(IntersectCollision{convex_mesh_id}),
|
||||
})}
|
||||
@ -1206,7 +1240,7 @@ fn teleport(body:&mut Body,touching:&mut TouchingState,models:&PhysicsModels,sty
|
||||
MoveState::Air
|
||||
}
|
||||
fn teleport_to_spawn(body:&mut Body,touching:&mut TouchingState,style:&StyleModifiers,hitbox_mesh:&HitboxMesh,mode:&gameplay_modes::Mode,models:&PhysicsModels,stage_id:gameplay_modes::StageId)->Option<MoveState>{
|
||||
let model=models.model(mode.get_spawn_model_id(stage_id)?.into());
|
||||
let model=models.model(mode.get_spawn_model_id(stage_id)?.into()).unwrap();
|
||||
let point=model.transform.vertex.transform_point3(Planar64Vec3::Y)+Planar64Vec3::Y*(style.hitbox.halfsize.y()+Planar64::ONE/16);
|
||||
Some(teleport(body,touching,models,style,hitbox_mesh,point))
|
||||
}
|
||||
@ -1262,8 +1296,8 @@ fn run_teleport_behaviour(wormhole:&Option<gameplay_attributes::Wormhole>,models
|
||||
}
|
||||
match wormhole{
|
||||
&Some(gameplay_attributes::Wormhole{destination_model})=>{
|
||||
let origin_model=models.model(convex_mesh_id.model_id);
|
||||
let destination_model=models.model(destination_model.into());
|
||||
let origin_model=models.model(convex_mesh_id.model_id).unwrap();
|
||||
let destination_model=models.model(destination_model.into()).unwrap();
|
||||
//ignore the transform for now
|
||||
Some(teleport(body,touching,models,style,hitbox_mesh,body.position-origin_model.transform.vertex.translation+destination_model.transform.vertex.translation))
|
||||
}
|
||||
@ -1271,19 +1305,27 @@ fn run_teleport_behaviour(wormhole:&Option<gameplay_attributes::Wormhole>,models
|
||||
}
|
||||
}
|
||||
|
||||
fn atomic_internal_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedInstruction<PhysicsInternalInstruction>){
|
||||
state.time=ins.time;
|
||||
let should_advance_body=match ins.instruction{
|
||||
PhysicsInternalInstruction::CollisionStart(_)
|
||||
|PhysicsInternalInstruction::CollisionEnd(_)
|
||||
|PhysicsInternalInstruction::StrafeTick
|
||||
|PhysicsInternalInstruction::ReachWalkTargetVelocity=>true,
|
||||
};
|
||||
if should_advance_body{
|
||||
state.body.advance_time(state.time);
|
||||
}
|
||||
fn atomic_state_update(state:&mut PhysicsState,data:&PhysicsData,ins:TimedInstruction<PhysicsInstruction>){
|
||||
match &ins.instruction{
|
||||
PhysicsInstruction::Input(PhysicsInputInstruction::Idle)
|
||||
|PhysicsInstruction::Input(PhysicsInputInstruction::SetNextMouse(_))
|
||||
|PhysicsInstruction::Input(PhysicsInputInstruction::ReplaceMouse(_,_))
|
||||
|PhysicsInstruction::StrafeTick=>(),
|
||||
_=>println!("{}|{:?}",ins.time,ins.instruction),
|
||||
}
|
||||
//selectively update body
|
||||
match &ins.instruction{
|
||||
PhysicsInstruction::Input(PhysicsInputInstruction::Idle)=>state.time=ins.time,//idle simply updates time
|
||||
PhysicsInstruction::Input(_)
|
||||
|PhysicsInstruction::ReachWalkTargetVelocity
|
||||
|PhysicsInstruction::CollisionStart(_)
|
||||
|PhysicsInstruction::CollisionEnd(_)
|
||||
|PhysicsInstruction::StrafeTick
|
||||
|PhysicsInstruction::SetSensitivity(_)
|
||||
=>state.advance_time(ins.time),
|
||||
}
|
||||
match ins.instruction{
|
||||
PhysicsInternalInstruction::CollisionStart(collision)=>{
|
||||
PhysicsInstruction::CollisionStart(collision)=>{
|
||||
let convex_mesh_id=collision.convex_mesh_id();
|
||||
match (data.models.attr(convex_mesh_id.model_id),&collision){
|
||||
(PhysicsCollisionAttributes::Contact{contacting,general},&Collision::Contact(contact))=>{
|
||||
@ -1326,10 +1368,25 @@ fn atomic_internal_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:Tim
|
||||
if let Some(mode)=data.modes.get_mode(state.mode_state.get_mode_id()){
|
||||
run_teleport_behaviour(&general.wormhole,&data.models,mode,&state.style,&data.hitbox_mesh,&mut state.mode_state,&mut state.touching,&mut state.body,convex_mesh_id);
|
||||
}
|
||||
match &general.booster{
|
||||
Some(booster)=>{
|
||||
//DELETE THIS when boosters get converted to height machines
|
||||
match booster{
|
||||
//&gameplay_attributes::Booster::Affine(transform)=>v=transform.transform_point3(v),
|
||||
&gameplay_attributes::Booster::Velocity(velocity)=>{
|
||||
let boosted_velocity=state.body.velocity+velocity;
|
||||
//fall through boosters
|
||||
state.cull_velocity(data,boosted_velocity);
|
||||
},
|
||||
&gameplay_attributes::Booster::Energy{direction: _,energy: _}=>todo!(),
|
||||
}
|
||||
},
|
||||
None=>(),
|
||||
}
|
||||
if state.style.get_control(Controls::Jump,state.input_state.controls){
|
||||
if let (Some(jump_settings),Some(walk_state))=(&state.style.jump,state.move_state.get_walk_state()){
|
||||
let jump_dir=walk_state.jump_direction.direction(&data.models,&data.hitbox_mesh,&walk_state.contact);
|
||||
let jumped_velocity=jump_settings.jumped_velocity(&state.style,jump_dir,state.body.velocity,general.booster.as_ref());
|
||||
let jumped_velocity=jump_settings.jumped_velocity(&state.style,jump_dir,state.body.velocity);
|
||||
state.cull_velocity(data,jumped_velocity);
|
||||
}
|
||||
}
|
||||
@ -1352,36 +1409,17 @@ fn atomic_internal_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:Tim
|
||||
//doing input_and_body to refresh the walk state if you hit a wall while accelerating
|
||||
state.apply_enum_and_input_and_body(data);
|
||||
},
|
||||
(PhysicsCollisionAttributes::Intersect{intersecting:_,general},Collision::Intersect(_intersect))=>{
|
||||
(PhysicsCollisionAttributes::Intersect{intersecting: _,general},Collision::Intersect(intersect))=>{
|
||||
//I think that setting the velocity to 0 was preventing surface contacts from entering an infinite loop
|
||||
state.touching.insert(collision);
|
||||
//insta booster!
|
||||
if let Some(booster)=&general.booster{
|
||||
state.cull_velocity(data,booster.boost(state.body.velocity));
|
||||
}
|
||||
if let Some(mode)=data.modes.get_mode(state.mode_state.get_mode_id()){
|
||||
let zone=mode.get_zone(convex_mesh_id.model_id.into());
|
||||
match zone{
|
||||
Some(gameplay_modes::Zone::Start)=>{
|
||||
println!("@@@@ Starting new run!");
|
||||
state.run=run::Run::new();
|
||||
},
|
||||
Some(gameplay_modes::Zone::Finish)=>{
|
||||
match state.run.finish(state.time){
|
||||
Ok(())=>println!("@@@@ Finished run time={}",state.run.time(state.time)),
|
||||
Err(e)=>println!("@@@@ Run Finish error:{e:?}"),
|
||||
}
|
||||
},
|
||||
Some(gameplay_modes::Zone::Anticheat)=>state.run.flag(run::FlagReason::Anticheat),
|
||||
None=>(),
|
||||
}
|
||||
run_teleport_behaviour(&general.wormhole,&data.models,mode,&state.style,&data.hitbox_mesh,&mut state.mode_state,&mut state.touching,&mut state.body,convex_mesh_id);
|
||||
}
|
||||
},
|
||||
_=>panic!("invalid pair"),
|
||||
}
|
||||
},
|
||||
PhysicsInternalInstruction::CollisionEnd(collision)=>{
|
||||
PhysicsInstruction::CollisionEnd(collision)=>{
|
||||
match (data.models.attr(collision.convex_mesh_id().model_id),&collision){
|
||||
(PhysicsCollisionAttributes::Contact{contacting:_,general:_},&Collision::Contact(contact))=>{
|
||||
state.touching.remove(&collision);//remove contact before calling contact_constrain_acceleration
|
||||
@ -1398,23 +1436,11 @@ fn atomic_internal_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:Tim
|
||||
},
|
||||
(PhysicsCollisionAttributes::Intersect{intersecting: _,general:_},Collision::Intersect(_))=>{
|
||||
state.touching.remove(&collision);
|
||||
if let Some(mode)=data.modes.get_mode(state.mode_state.get_mode_id()){
|
||||
let zone=mode.get_zone(collision.convex_mesh_id().model_id.into());
|
||||
match zone{
|
||||
Some(gameplay_modes::Zone::Start)=>{
|
||||
match state.run.start(state.time){
|
||||
Ok(())=>println!("@@@@ Started run"),
|
||||
Err(e)=>println!("@@@@ Run Start error:{e:?}"),
|
||||
}
|
||||
},
|
||||
_=>(),
|
||||
}
|
||||
}
|
||||
},
|
||||
_=>panic!("invalid pair"),
|
||||
}
|
||||
},
|
||||
PhysicsInternalInstruction::StrafeTick=>{
|
||||
PhysicsInstruction::StrafeTick=>{
|
||||
//TODO make this less huge
|
||||
if let Some(strafe_settings)=&state.style.strafe{
|
||||
let controls=state.input_state.controls;
|
||||
@ -1432,7 +1458,7 @@ fn atomic_internal_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:Tim
|
||||
}
|
||||
}
|
||||
}
|
||||
PhysicsInternalInstruction::ReachWalkTargetVelocity=>{
|
||||
PhysicsInstruction::ReachWalkTargetVelocity=>{
|
||||
match &mut state.move_state{
|
||||
MoveState::Air
|
||||
|MoveState::Water
|
||||
@ -1456,48 +1482,10 @@ fn atomic_internal_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:Tim
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn atomic_input_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedInstruction<PhysicsInputInstruction>){
|
||||
state.time=ins.time;
|
||||
let should_advance_body=match ins.instruction{
|
||||
//the body may as well be a quantum wave function
|
||||
//as far as these instruction are concerned (they don't care where it is)
|
||||
PhysicsInputInstruction::SetSensitivity(..)
|
||||
|PhysicsInputInstruction::Reset
|
||||
|PhysicsInputInstruction::Restart
|
||||
|PhysicsInputInstruction::Spawn(..)
|
||||
|PhysicsInputInstruction::SetZoom(..)
|
||||
|PhysicsInputInstruction::Idle=>false,
|
||||
//these controls only update the body if you are on the ground
|
||||
PhysicsInputInstruction::SetNextMouse(..)
|
||||
|PhysicsInputInstruction::ReplaceMouse(..)
|
||||
|PhysicsInputInstruction::SetMoveForward(..)
|
||||
|PhysicsInputInstruction::SetMoveLeft(..)
|
||||
|PhysicsInputInstruction::SetMoveBack(..)
|
||||
|PhysicsInputInstruction::SetMoveRight(..)
|
||||
|PhysicsInputInstruction::SetMoveUp(..)
|
||||
|PhysicsInputInstruction::SetMoveDown(..)
|
||||
|PhysicsInputInstruction::SetJump(..)=>{
|
||||
match &state.move_state{
|
||||
MoveState::Fly
|
||||
|MoveState::Water
|
||||
|MoveState::Walk(_)
|
||||
|MoveState::Ladder(_)=>true,
|
||||
MoveState::Air=>false,
|
||||
}
|
||||
},
|
||||
//the body must be updated unconditionally
|
||||
PhysicsInputInstruction::PracticeFly=>true,
|
||||
};
|
||||
if should_advance_body{
|
||||
state.body.advance_time(state.time);
|
||||
}
|
||||
//TODO: UNTAB
|
||||
PhysicsInstruction::SetSensitivity(sensitivity)=>state.camera.sensitivity=sensitivity,
|
||||
PhysicsInstruction::Input(input_instruction)=>{
|
||||
let mut b_refresh_walk_target=true;
|
||||
match ins.instruction{
|
||||
PhysicsInputInstruction::SetSensitivity(sensitivity)=>state.camera.sensitivity=sensitivity,
|
||||
match input_instruction{
|
||||
PhysicsInputInstruction::SetNextMouse(m)=>{
|
||||
state.camera.move_mouse(state.input_state.mouse_delta());
|
||||
state.input_state.set_next_mouse(m);
|
||||
@ -1513,47 +1501,32 @@ fn atomic_input_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedI
|
||||
PhysicsInputInstruction::SetMoveUp(s)=>state.input_state.set_control(Controls::MoveUp,s),
|
||||
PhysicsInputInstruction::SetMoveDown(s)=>state.input_state.set_control(Controls::MoveDown,s),
|
||||
PhysicsInputInstruction::SetJump(s)=>{
|
||||
b_refresh_walk_target=false;
|
||||
state.input_state.set_control(Controls::Jump,s);
|
||||
if let Some(walk_state)=state.move_state.get_walk_state(){
|
||||
if let Some(jump_settings)=&state.style.jump{
|
||||
let jump_dir=walk_state.jump_direction.direction(&data.models,&data.hitbox_mesh,&walk_state.contact);
|
||||
let booster_option=match data.models.attr(walk_state.contact.convex_mesh_id.model_id){
|
||||
PhysicsCollisionAttributes::Contact{contacting:_,general}=>general.booster.as_ref(),
|
||||
PhysicsCollisionAttributes::Intersect{..}=>None,
|
||||
};
|
||||
let jumped_velocity=jump_settings.jumped_velocity(&state.style,jump_dir,state.body.velocity,booster_option);
|
||||
let jumped_velocity=jump_settings.jumped_velocity(&state.style,jump_dir,state.body.velocity);
|
||||
state.cull_velocity(&data,jumped_velocity);
|
||||
}
|
||||
}
|
||||
b_refresh_walk_target=false;
|
||||
},
|
||||
PhysicsInputInstruction::SetZoom(s)=>{
|
||||
state.input_state.set_control(Controls::Zoom,s);
|
||||
b_refresh_walk_target=false;
|
||||
},
|
||||
PhysicsInputInstruction::Reset=>{
|
||||
//totally reset physics state
|
||||
state.reset_to_default();
|
||||
b_refresh_walk_target=false;
|
||||
},
|
||||
PhysicsInputInstruction::Restart=>{
|
||||
//teleport to start zone
|
||||
let spawn_point=data.modes.get_mode(state.mode_state.get_mode_id()).map(|mode|
|
||||
//it matters which of these runs first, but I have not thought it through yet as it doesn't matter yet
|
||||
state.mode_state.clear();
|
||||
state.mode_state.set_stage_id(gameplay_modes::StageId::FIRST);
|
||||
let spawn_point=data.modes.get_mode(state.mode_state.get_mode_id()).and_then(|mode|
|
||||
//TODO: spawn at the bottom of the start zone plus the hitbox size
|
||||
//TODO: set camera andles to face the same way as the start zone
|
||||
data.models.model(mode.get_start().into()).transform.vertex.translation
|
||||
data.models.model(mode.get_start().into()).map(|model|model.transform.vertex.translation)
|
||||
).unwrap_or(Planar64Vec3::ZERO);
|
||||
set_position(&mut state.body,&mut state.touching,spawn_point);
|
||||
set_velocity(&mut state.body,&state.touching,&data.models,&data.hitbox_mesh,Planar64Vec3::ZERO);
|
||||
state.set_move_state(data,MoveState::Air);
|
||||
b_refresh_walk_target=false;
|
||||
}
|
||||
PhysicsInputInstruction::Spawn(mode_id,stage_id)=>{
|
||||
//spawn at a particular stage
|
||||
if let Some(mode)=data.modes.get_mode(mode_id){
|
||||
teleport_to_spawn(&mut state.body,&mut state.touching,&state.style,&data.hitbox_mesh,mode,&data.models,stage_id);
|
||||
}
|
||||
b_refresh_walk_target=false;
|
||||
},
|
||||
PhysicsInputInstruction::PracticeFly=>{
|
||||
match &state.move_state{
|
||||
@ -1566,34 +1539,13 @@ fn atomic_input_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedI
|
||||
}
|
||||
b_refresh_walk_target=false;
|
||||
},
|
||||
PhysicsInputInstruction::Idle=>{
|
||||
//literally idle!
|
||||
b_refresh_walk_target=false;
|
||||
},
|
||||
PhysicsInputInstruction::Idle=>{b_refresh_walk_target=false;},//literally idle!
|
||||
}
|
||||
if b_refresh_walk_target{
|
||||
state.apply_input_and_body(data);
|
||||
state.cull_velocity(data,state.body.velocity);
|
||||
}
|
||||
}
|
||||
|
||||
fn atomic_state_update(state:&mut PhysicsState,data:&PhysicsData,ins:TimedInstruction<PhysicsInstruction>){
|
||||
match &ins.instruction{
|
||||
PhysicsInstruction::Input(PhysicsInputInstruction::Idle)
|
||||
|PhysicsInstruction::Input(PhysicsInputInstruction::SetNextMouse(_))
|
||||
|PhysicsInstruction::Input(PhysicsInputInstruction::ReplaceMouse(_,_))
|
||||
|PhysicsInstruction::Internal(PhysicsInternalInstruction::StrafeTick)
|
||||
|PhysicsInstruction::Internal(PhysicsInternalInstruction::ReachWalkTargetVelocity)=>(),
|
||||
_=>println!("{}|{:?}",ins.time,ins.instruction),
|
||||
}
|
||||
if ins.time<state.time{
|
||||
println!("@@@@ Time travel warning! {:?}",ins);
|
||||
}
|
||||
//idle is special, it is specifically a no-op to get Internal events to catch up to real time
|
||||
match ins.instruction{
|
||||
PhysicsInstruction::Input(PhysicsInputInstruction::Idle)=>(),
|
||||
PhysicsInstruction::Internal(instruction)=>atomic_internal_instruction(state,data,TimedInstruction{time:ins.time,instruction}),
|
||||
PhysicsInstruction::Input(instruction)=>atomic_input_instruction(state,data,TimedInstruction{time:ins.time,instruction}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,6 @@
|
||||
use strafesnet_common::mouse::MouseState;
|
||||
use strafesnet_common::physics::Instruction as PhysicsInputInstruction;
|
||||
use crate::physics::{MouseState,PhysicsInputInstruction};
|
||||
use strafesnet_common::integer::Time;
|
||||
use strafesnet_common::instruction::TimedInstruction;
|
||||
use strafesnet_common::timer::{Scaled,Timer,TimerState};
|
||||
use mouse_interpolator::MouseInterpolator;
|
||||
|
||||
use strafesnet_common::instruction::{TimedInstruction,InstructionConsumer};
|
||||
#[derive(Debug)]
|
||||
pub enum InputInstruction{
|
||||
MoveMouse(glam::IVec2),
|
||||
@ -16,236 +12,194 @@ pub enum InputInstruction{
|
||||
MoveForward(bool),
|
||||
Jump(bool),
|
||||
Zoom(bool),
|
||||
ResetAndRestart,
|
||||
ResetAndSpawn(strafesnet_common::gameplay_modes::ModeId,strafesnet_common::gameplay_modes::StageId),
|
||||
Reset,
|
||||
PracticeFly,
|
||||
}
|
||||
pub enum Instruction{
|
||||
Input(InputInstruction),
|
||||
Passthrough(PassthroughInstruction),
|
||||
Interpolate(InputInstruction),
|
||||
}
|
||||
pub enum PassthroughInstruction{
|
||||
Render,
|
||||
Resize(winit::dpi::PhysicalSize<u32>),
|
||||
ChangeMap(strafesnet_common::map::CompleteMap),
|
||||
//SetPaused is not an InputInstruction: the physics doesn't know that it's paused.
|
||||
SetPaused(bool),
|
||||
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
|
||||
GenerateModels(strafesnet_common::map::CompleteMap),
|
||||
ClearModels,
|
||||
//Graphics(crate::graphics_worker::Instruction),
|
||||
}
|
||||
mod mouse_interpolator{
|
||||
use super::*;
|
||||
//TODO: move this or tab
|
||||
pub struct MouseInterpolator<'a>{
|
||||
//"PlayerController"
|
||||
user_settings:crate::settings::UserSettings,
|
||||
//"MouseInterpolator"
|
||||
timeline:std::collections::VecDeque<TimedInstruction<PhysicsInputInstruction>>,
|
||||
last_mouse_time:Time,//this value is pre-transformed to simulation time
|
||||
mouse_blocking:bool,
|
||||
//????
|
||||
bot_worker:crate::worker::QNWorker<'a,crate::bot_worker::Instruction>,
|
||||
//"Simulation"
|
||||
timer:Timer<Scaled>,
|
||||
physics:crate::physics::PhysicsContext,
|
||||
|
||||
pub struct MouseInterpolator{
|
||||
queue:std::collections::VecDeque<TimedInstruction<InputInstruction>>,
|
||||
}
|
||||
impl MouseInterpolator<'_>{
|
||||
pub fn new<'a>(
|
||||
physics:crate::physics::PhysicsContext,
|
||||
bot_worker:crate::worker::QNWorker<'a,crate::bot_worker::Instruction>,
|
||||
user_settings:crate::settings::UserSettings,
|
||||
)->MouseInterpolator<'a>{
|
||||
MouseInterpolator{
|
||||
mouse_blocking:true,
|
||||
last_mouse_time:physics.get_next_mouse().time,
|
||||
timeline:std::collections::VecDeque::new(),
|
||||
timer:Timer::from_state(Scaled::identity(),false),
|
||||
physics,
|
||||
bot_worker,
|
||||
user_settings,
|
||||
}
|
||||
}
|
||||
fn push_mouse_instruction(&mut self,ins:&TimedInstruction<Instruction>,m:glam::IVec2){
|
||||
if self.mouse_blocking{
|
||||
//tell the game state which is living in the past about its future
|
||||
self.timeline.push_front(TimedInstruction{
|
||||
time:self.last_mouse_time,
|
||||
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:self.timer.time(ins.time),pos:m}),
|
||||
});
|
||||
}else{
|
||||
//mouse has just started moving again after being still for longer than 10ms.
|
||||
//replace the entire mouse interpolation state to avoid an intermediate state with identical m0.t m1.t timestamps which will divide by zero
|
||||
self.timeline.push_front(TimedInstruction{
|
||||
time:self.last_mouse_time,
|
||||
instruction:PhysicsInputInstruction::ReplaceMouse(
|
||||
MouseState{time:self.last_mouse_time,pos:self.physics.get_next_mouse().pos},
|
||||
MouseState{time:self.timer.time(ins.time),pos:m}
|
||||
),
|
||||
});
|
||||
//delay physics execution until we have an interpolation target
|
||||
self.mouse_blocking=true;
|
||||
}
|
||||
self.last_mouse_time=self.timer.time(ins.time);
|
||||
}
|
||||
fn push(&mut self,time:Time,phys_input:PhysicsInputInstruction){
|
||||
//This is always a non-mouse event
|
||||
self.timeline.push_back(TimedInstruction{
|
||||
time:self.timer.time(time),
|
||||
instruction:phys_input,
|
||||
fn drain_queue(physics:&mut crate::physics::PhysicsContext,iterable:impl IntoIterator<Item=TimedInstruction<InputInstruction>>){
|
||||
for ins in iterable{
|
||||
let physics_input=match &ins.instruction{
|
||||
InputInstruction::MoveMouse(_)=>panic!("Queue was confirmed to contain no MoveMouse events1"),
|
||||
&InputInstruction::MoveForward(s)=>PhysicsInputInstruction::SetMoveForward(s),
|
||||
&InputInstruction::MoveLeft(s)=>PhysicsInputInstruction::SetMoveLeft(s),
|
||||
&InputInstruction::MoveBack(s)=>PhysicsInputInstruction::SetMoveBack(s),
|
||||
&InputInstruction::MoveRight(s)=>PhysicsInputInstruction::SetMoveRight(s),
|
||||
&InputInstruction::MoveUp(s)=>PhysicsInputInstruction::SetMoveUp(s),
|
||||
&InputInstruction::MoveDown(s)=>PhysicsInputInstruction::SetMoveDown(s),
|
||||
&InputInstruction::Jump(s)=>PhysicsInputInstruction::SetJump(s),
|
||||
&InputInstruction::Zoom(s)=>PhysicsInputInstruction::SetZoom(s),
|
||||
InputInstruction::Reset=>PhysicsInputInstruction::Reset,
|
||||
InputInstruction::PracticeFly=>PhysicsInputInstruction::PracticeFly,
|
||||
};
|
||||
physics.run_input_instruction(TimedInstruction{
|
||||
time:ins.time,
|
||||
instruction:physics_input,
|
||||
});
|
||||
}
|
||||
/// returns should_empty_queue
|
||||
/// may or may not mutate internal state XD!
|
||||
fn map_instruction(&mut self,ins:&TimedInstruction<Instruction>)->bool{
|
||||
let mut update_mouse_blocking=true;
|
||||
match &ins.instruction{
|
||||
Instruction::Input(input_instruction)=>match input_instruction{
|
||||
&InputInstruction::MoveMouse(m)=>{
|
||||
if !self.timer.is_paused(){
|
||||
self.push_mouse_instruction(ins,m);
|
||||
}
|
||||
update_mouse_blocking=false;
|
||||
}
|
||||
impl MouseInterpolator{
|
||||
fn handle_instruction(&mut self,physics:&mut crate::physics::PhysicsContext,ins:TimedInstruction<InputInstruction>){
|
||||
//need to handle the case where mouse polling rate is less than 100hz
|
||||
//also the whole thing is probably wrong lol
|
||||
let is_inserting_mouse_instruction=matches!(ins.instruction,InputInstruction::MoveMouse(_));
|
||||
self.queue.push_back(ins);
|
||||
//We just pushed an element.
|
||||
//The first element is guaranteed to exist.
|
||||
let mut iter=self.queue.iter();
|
||||
//find a mouse input
|
||||
'outer:loop{
|
||||
match iter.next(){
|
||||
Some(ins0)=>{
|
||||
let physics_input=match &ins0.instruction{
|
||||
&InputInstruction::MoveMouse(mut mouse0)=>{
|
||||
//mouse instruction found.
|
||||
//enter a new loop with different behaviour
|
||||
//we have to wait for the next mouse event
|
||||
//so there is a before and after interpolation target
|
||||
//write down ins0.time to appease the borrow checker
|
||||
let mut t0=ins0.time;
|
||||
'inner:loop{
|
||||
match iter.next(){
|
||||
Some(ins1)=>match &ins1.instruction{
|
||||
&InputInstruction::MoveMouse(mouse1)=>{
|
||||
//we found two mouse events to interpolate between
|
||||
let consume_count=self.queue.len()-iter.len()-1;//don't consume the mouse1 instruction
|
||||
//fire off a mouse instruction
|
||||
physics.run_input_instruction(TimedInstruction{
|
||||
time:t0,
|
||||
instruction:PhysicsInputInstruction::SetNextMouse(
|
||||
MouseState{time:ins1.time,pos:mouse1}
|
||||
),
|
||||
});
|
||||
//update inner loop state
|
||||
mouse0=mouse1;
|
||||
t0=ins1.time;
|
||||
//drain and handle the elements from the front
|
||||
std::mem::drop(iter);
|
||||
let mut hot_queue=self.queue.drain(0..consume_count);
|
||||
hot_queue.next();
|
||||
drain_queue(physics,hot_queue);
|
||||
iter=self.queue.iter();
|
||||
//keep looking for another mouse instruction in the inner loop
|
||||
continue 'inner;
|
||||
},
|
||||
_=>if Time::from_millis(10)<ins1.time-t0{
|
||||
//we have passed more than 10ms of instructions and have not seen a mouse event.
|
||||
let consume_count=self.queue.len()-iter.len();
|
||||
//run an event to extrapolate no movement from
|
||||
let last_mouse=physics.get_next_mouse();
|
||||
physics.run_input_instruction(TimedInstruction{
|
||||
time:last_mouse.time,
|
||||
instruction:PhysicsInputInstruction::SetNextMouse(
|
||||
MouseState{time:ins1.time,pos:last_mouse.pos}
|
||||
),
|
||||
});
|
||||
//drop the iterator so we can consume the queue up to this point
|
||||
std::mem::drop(iter);
|
||||
//consume queue up to the scanned point
|
||||
let mut hot_queue=self.queue.drain(0..consume_count);
|
||||
//the first element is always the last mouse instruction (last_mouse above)
|
||||
hot_queue.next();
|
||||
drain_queue(physics,hot_queue);
|
||||
//make a new iterator starting from the new beginning
|
||||
//and continue looping like nothing happened
|
||||
iter=self.queue.iter();
|
||||
continue 'outer;
|
||||
},
|
||||
},
|
||||
None=>{
|
||||
if is_inserting_mouse_instruction{
|
||||
//the mouse started moving again after being still for over 10ms.
|
||||
//replace the entire mouse state
|
||||
physics.run_input_instruction(TimedInstruction{
|
||||
time:physics.get_next_mouse().time,
|
||||
instruction:PhysicsInputInstruction::ReplaceMouse(
|
||||
physics.get_next_mouse().clone(),
|
||||
MouseState{time:t0,pos:mouse0}
|
||||
),
|
||||
});
|
||||
}
|
||||
break 'outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
&InputInstruction::MoveForward(s)=>PhysicsInputInstruction::SetMoveForward(s),
|
||||
&InputInstruction::MoveLeft(s)=>PhysicsInputInstruction::SetMoveLeft(s),
|
||||
&InputInstruction::MoveBack(s)=>PhysicsInputInstruction::SetMoveBack(s),
|
||||
&InputInstruction::MoveRight(s)=>PhysicsInputInstruction::SetMoveRight(s),
|
||||
&InputInstruction::MoveUp(s)=>PhysicsInputInstruction::SetMoveUp(s),
|
||||
&InputInstruction::MoveDown(s)=>PhysicsInputInstruction::SetMoveDown(s),
|
||||
&InputInstruction::Jump(s)=>PhysicsInputInstruction::SetJump(s),
|
||||
&InputInstruction::Zoom(s)=>PhysicsInputInstruction::SetZoom(s),
|
||||
InputInstruction::Reset=>PhysicsInputInstruction::Reset,
|
||||
InputInstruction::PracticeFly=>PhysicsInputInstruction::PracticeFly,
|
||||
};
|
||||
//handle each event immediately, we are not waiting for mouse
|
||||
physics.run_input_instruction(TimedInstruction{
|
||||
time:ins0.time,
|
||||
instruction:physics_input,
|
||||
});
|
||||
//drop it and pop it! consume one element and continue the loop
|
||||
std::mem::drop(iter);
|
||||
self.queue.pop_front();
|
||||
iter=self.queue.iter();
|
||||
},
|
||||
&InputInstruction::MoveForward(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveForward(s)),
|
||||
&InputInstruction::MoveLeft(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveLeft(s)),
|
||||
&InputInstruction::MoveBack(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveBack(s)),
|
||||
&InputInstruction::MoveRight(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveRight(s)),
|
||||
&InputInstruction::MoveUp(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveUp(s)),
|
||||
&InputInstruction::MoveDown(s)=>self.push(ins.time,PhysicsInputInstruction::SetMoveDown(s)),
|
||||
&InputInstruction::Jump(s)=>self.push(ins.time,PhysicsInputInstruction::SetJump(s)),
|
||||
&InputInstruction::Zoom(s)=>self.push(ins.time,PhysicsInputInstruction::SetZoom(s)),
|
||||
&InputInstruction::ResetAndSpawn(mode_id,stage_id)=>{
|
||||
self.push(ins.time,PhysicsInputInstruction::Reset);
|
||||
self.push(ins.time,PhysicsInputInstruction::SetSensitivity(self.user_settings.calculate_sensitivity()));
|
||||
self.push(ins.time,PhysicsInputInstruction::Spawn(mode_id,stage_id));
|
||||
},
|
||||
InputInstruction::ResetAndRestart=>{
|
||||
self.push(ins.time,PhysicsInputInstruction::Reset);
|
||||
self.push(ins.time,PhysicsInputInstruction::SetSensitivity(self.user_settings.calculate_sensitivity()));
|
||||
self.push(ins.time,PhysicsInputInstruction::Restart);
|
||||
},
|
||||
InputInstruction::PracticeFly=>self.push(ins.time,PhysicsInputInstruction::PracticeFly),
|
||||
},
|
||||
//do these really need to idle the physics?
|
||||
//sending None dumps the instruction queue
|
||||
Instruction::ChangeMap(_)=>self.push(ins.time,PhysicsInputInstruction::Idle),
|
||||
Instruction::Resize(_)=>self.push(ins.time,PhysicsInputInstruction::Idle),
|
||||
Instruction::Render=>self.push(ins.time,PhysicsInputInstruction::Idle),
|
||||
&Instruction::SetPaused(paused)=>{
|
||||
if let Err(e)=self.timer.set_paused(ins.time,paused){
|
||||
println!("Cannot pause: {e}");
|
||||
None=>{
|
||||
//if mouse0 is never found and the loop ends, we can drain the entire queue
|
||||
//because we are not waiting for mouse events.
|
||||
drain_queue(physics,self.queue.drain(..));
|
||||
break 'outer;
|
||||
}
|
||||
self.push(ins.time,PhysicsInputInstruction::Idle);
|
||||
},
|
||||
}
|
||||
if update_mouse_blocking{
|
||||
//this returns the bool for us
|
||||
self.update_mouse_blocking(ins.time)
|
||||
}else{
|
||||
//do flush that queue
|
||||
true
|
||||
}
|
||||
}
|
||||
/// must check if self.mouse_blocking==true before calling!
|
||||
fn unblock_mouse(&mut self,time:Time){
|
||||
//push an event to extrapolate no movement from
|
||||
self.timeline.push_front(TimedInstruction{
|
||||
time:self.last_mouse_time,
|
||||
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:self.timer.time(time),pos:self.physics.get_next_mouse().pos}),
|
||||
});
|
||||
self.last_mouse_time=self.timer.time(time);
|
||||
//stop blocking. the mouse is not moving so the physics does not need to live in the past and wait for interpolation targets.
|
||||
self.mouse_blocking=false;
|
||||
}
|
||||
fn update_mouse_blocking(&mut self,time:Time)->bool{
|
||||
if self.mouse_blocking{
|
||||
//assume the mouse has stopped moving after 10ms.
|
||||
//shitty mice are 125Hz which is 8ms so this should cover that.
|
||||
//setting this to 100us still doesn't print even though it's 10x lower than the polling rate,
|
||||
//so mouse events are probably not handled separately from drawing and fire right before it :(
|
||||
if Time::from_millis(10)<self.timer.time(time)-self.physics.get_next_mouse().time{
|
||||
self.unblock_mouse(time);
|
||||
true
|
||||
}else{
|
||||
false
|
||||
}
|
||||
}else{
|
||||
//keep this up to date so that it can be used as a known-timestamp
|
||||
//that the mouse was not moving when the mouse starts moving again
|
||||
self.last_mouse_time=self.timer.time(time);
|
||||
true
|
||||
}
|
||||
}
|
||||
fn empty_queue(&mut self){
|
||||
while let Some(instruction)=self.timeline.pop_front(){
|
||||
//makeshift clone because requiring all TimedInstructions to be Clone is troublesome
|
||||
self.bot_worker.send(crate::bot_worker::Instruction::Push{
|
||||
ins:TimedInstruction{
|
||||
time:instruction.time,
|
||||
instruction:instruction.instruction.clone(),
|
||||
}
|
||||
}).unwrap();
|
||||
self.physics.run_input_instruction(instruction);
|
||||
}
|
||||
}
|
||||
pub fn handle_instruction(&mut self,ins:&TimedInstruction<Instruction>){
|
||||
let should_empty_queue=self.map_instruction(ins);
|
||||
if should_empty_queue{
|
||||
self.empty_queue();
|
||||
}
|
||||
}
|
||||
pub fn get_render_stuff(&self,time:Time)->(crate::physics::PhysicsOutputState,Time,glam::IVec2){
|
||||
(self.physics.output(),self.timer.time(time),self.physics.get_next_mouse().pos)
|
||||
}
|
||||
pub fn change_map(&mut self,time:Time,map:&strafesnet_common::map::CompleteMap){
|
||||
//dump any pending interpolation state
|
||||
if self.mouse_blocking{
|
||||
self.unblock_mouse(time);
|
||||
}
|
||||
self.empty_queue();
|
||||
|
||||
//doing it like this to avoid doing PhysicsInstruction::ChangeMap(Rc<CompleteMap>)
|
||||
self.physics.generate_models(&map);
|
||||
|
||||
//use the standard input interface so the instructions are written out to bots
|
||||
self.handle_instruction(&TimedInstruction{
|
||||
time:self.timer.time(time),
|
||||
instruction:Instruction::Input(InputInstruction::ResetAndSpawn(
|
||||
strafesnet_common::gameplay_modes::ModeId::MAIN,
|
||||
strafesnet_common::gameplay_modes::StageId::FIRST,
|
||||
)),
|
||||
});
|
||||
}
|
||||
pub const fn user_settings(&self)->&crate::settings::UserSettings{
|
||||
&self.user_settings
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new<'a>(
|
||||
mut graphics_worker:crate::compat_worker::INWorker<'a,crate::graphics_worker::Instruction>,
|
||||
user_settings:crate::settings::UserSettings,
|
||||
bot_worker:crate::worker::QNWorker<'a,crate::bot_worker::Instruction>,
|
||||
)->crate::compat_worker::QNWorker<'a,TimedInstruction<Instruction>>{
|
||||
let physics=crate::physics::PhysicsContext::default();
|
||||
let mut interpolator=MouseInterpolator::new(
|
||||
physics,
|
||||
bot_worker,
|
||||
user_settings
|
||||
);
|
||||
pub fn new(mut physics:crate::physics::PhysicsContext,mut graphics_worker:crate::compat_worker::INWorker<crate::graphics_worker::Instruction>)->crate::compat_worker::QNWorker<TimedInstruction<Instruction>>{
|
||||
let mut interpolator=MouseInterpolator{
|
||||
queue:std::collections::VecDeque::new(),
|
||||
};
|
||||
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
|
||||
interpolator.handle_instruction(&ins);
|
||||
match ins.instruction{
|
||||
Instruction::Render=>{
|
||||
let (physics_output,time,mouse_pos)=interpolator.get_render_stuff(ins.time);
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics_output,time,mouse_pos)).unwrap();
|
||||
let passthrough_instruction=match ins.instruction{
|
||||
Instruction::Passthrough(passthrough_instruction)=>passthrough_instruction,
|
||||
Instruction::Interpolate(input_instruction)=>{
|
||||
interpolator.handle_instruction(&mut physics,TimedInstruction{
|
||||
instruction:input_instruction,
|
||||
time:ins.time,
|
||||
});
|
||||
return;
|
||||
},
|
||||
Instruction::Resize(size)=>{
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,interpolator.user_settings().clone())).unwrap();
|
||||
};
|
||||
match passthrough_instruction{
|
||||
PassthroughInstruction::Render=>{
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),ins.time,physics.get_next_mouse().pos)).unwrap();
|
||||
},
|
||||
Instruction::ChangeMap(map)=>{
|
||||
interpolator.change_map(ins.time,&map);
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::ChangeMap(map)).unwrap();
|
||||
PassthroughInstruction::Resize(size,user_settings)=>{
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap();
|
||||
},
|
||||
PassthroughInstruction::GenerateModels(map)=>{
|
||||
physics.generate_models(&map);
|
||||
physics.spawn();
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(map)).unwrap();
|
||||
},
|
||||
PassthroughInstruction::ClearModels=>{
|
||||
physics.clear();
|
||||
graphics_worker.send(crate::graphics_worker::Instruction::ClearModels).unwrap();
|
||||
},
|
||||
Instruction::Input(_)=>(),
|
||||
Instruction::SetPaused(_)=>(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
15
src/setup.rs
15
src/setup.rs
@ -213,17 +213,13 @@ pub fn setup_and_start(title:String){
|
||||
|
||||
//dedicated thread to ping request redraw back and resize the window doesn't seem logical
|
||||
|
||||
std::thread::scope(|scope|{
|
||||
//TODO: TAB
|
||||
let window=crate::window::WindowContextSetup::new(&setup_context,&window);
|
||||
//the thread that spawns the physics thread
|
||||
let mut window_thread=crate::window::worker(
|
||||
&window,
|
||||
setup_context,
|
||||
scope
|
||||
);
|
||||
let mut window_thread=window.into_worker(setup_context);
|
||||
|
||||
if let Some(arg)=std::env::args().nth(1){
|
||||
let path=std::path::PathBuf::from(arg);
|
||||
let args:Vec<String>=std::env::args().collect();
|
||||
if args.len()==2{
|
||||
let path=std::path::PathBuf::from(&args[1]);
|
||||
window_thread.send(TimedInstruction{
|
||||
time:integer::Time::ZERO,
|
||||
instruction:WindowInstruction::WindowEvent(winit::event::WindowEvent::DroppedFile(path)),
|
||||
@ -233,7 +229,6 @@ pub fn setup_and_start(title:String){
|
||||
println!("Entering event loop...");
|
||||
let root_time=std::time::Instant::now();
|
||||
run_event_loop(event_loop,window_thread,root_time).unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
fn run_event_loop(
|
||||
|
179
src/timer.rs
Normal file
179
src/timer.rs
Normal file
@ -0,0 +1,179 @@
|
||||
use strafesnet_common::integer::{Time,Ratio64};
|
||||
|
||||
pub trait TimerState:Copy{
|
||||
fn get_time(&self,time:Time)->Time;
|
||||
fn set_time(&mut self,time:Time,new_time:Time);
|
||||
fn get_offset(&self)->Time;
|
||||
fn set_offset(&mut self,offset:Time);
|
||||
}
|
||||
|
||||
#[derive(Clone,Copy,Debug)]
|
||||
struct Scaled{
|
||||
scale:Ratio64,
|
||||
offset:Time,
|
||||
}
|
||||
impl Scaled{
|
||||
fn scale(&self,time:Time)->Time{
|
||||
Time::raw(self.scale.mul_int(time.get()))
|
||||
}
|
||||
fn get_scale(&self)->Ratio64{
|
||||
self.scale
|
||||
}
|
||||
fn set_scale(&mut self,time:Time,new_scale:Ratio64){
|
||||
let new_time=self.get_time(time);
|
||||
self.scale=new_scale;
|
||||
self.set_time(time,new_time);
|
||||
}
|
||||
}
|
||||
impl TimerState for Scaled{
|
||||
fn get_time(&self,time:Time)->Time{
|
||||
self.scale(time)+self.offset
|
||||
}
|
||||
fn set_time(&mut self,time:Time,new_time:Time){
|
||||
self.offset=new_time-self.scale(time);
|
||||
}
|
||||
fn get_offset(&self)->Time{
|
||||
self.offset
|
||||
}
|
||||
fn set_offset(&mut self,offset:Time){
|
||||
self.offset=offset;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone,Copy,Debug)]
|
||||
struct Realtime{
|
||||
offset:Time,
|
||||
}
|
||||
impl TimerState for Realtime{
|
||||
fn get_time(&self,time:Time)->Time{
|
||||
time+self.offset
|
||||
}
|
||||
fn set_time(&mut self,time:Time,new_time:Time){
|
||||
self.offset=new_time-time;
|
||||
}
|
||||
fn get_offset(&self)->Time{
|
||||
self.offset
|
||||
}
|
||||
fn set_offset(&mut self,offset:Time){
|
||||
self.offset=offset;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone,Debug)]
|
||||
pub struct Timer<T>{
|
||||
state:T,
|
||||
paused:bool,
|
||||
}
|
||||
|
||||
impl Timer<Realtime>{
|
||||
pub fn realtime(offset:Time)->Self{
|
||||
Self{
|
||||
state:Realtime{offset},
|
||||
paused:false,
|
||||
}
|
||||
}
|
||||
pub fn realtime_paused(offset:Time)->Self{
|
||||
Self{
|
||||
state:Realtime{offset},
|
||||
paused:true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error{
|
||||
AlreadyPaused,
|
||||
AlreadyUnpaused,
|
||||
}
|
||||
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{}
|
||||
|
||||
impl Timer<Scaled>{
|
||||
pub fn scaled(scale:Ratio64,offset:Time)->Self{
|
||||
Self{
|
||||
state:Scaled{scale,offset},
|
||||
paused:false,
|
||||
}
|
||||
}
|
||||
pub fn scaled_paused(scale:Ratio64,offset:Time)->Self{
|
||||
Self{
|
||||
state:Scaled{scale,offset},
|
||||
paused:true,
|
||||
}
|
||||
}
|
||||
pub fn get_scale(&mut self)->Ratio64{
|
||||
self.state.get_scale()
|
||||
}
|
||||
pub fn set_scale(&mut self,time:Time,new_scale:Ratio64){
|
||||
self.state.set_scale(time,new_scale)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:TimerState> Timer<T>{
|
||||
pub fn time(&self,time:Time)->Time{
|
||||
match self.paused{
|
||||
true=>self.state.get_offset(),
|
||||
false=>self.state.get_time(time),
|
||||
}
|
||||
}
|
||||
pub fn set_time(&mut self,time:Time,new_time:Time){
|
||||
match self.paused{
|
||||
true=>self.state.set_offset(new_time),
|
||||
false=>self.state.set_time(time,new_time),
|
||||
}
|
||||
}
|
||||
pub fn pause(&mut self,time:Time)->Result<(),Error>{
|
||||
match self.paused{
|
||||
true=>Err(Error::AlreadyPaused),
|
||||
false=>{
|
||||
let new_time=self.time(time);
|
||||
self.state.set_offset(new_time);
|
||||
self.paused=true;
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
}
|
||||
pub fn unpause(&mut self,time:Time)->Result<(),Error>{
|
||||
match self.paused{
|
||||
true=>{
|
||||
let new_time=self.time(time);
|
||||
self.state.set_time(time,new_time);
|
||||
self.paused=false;
|
||||
Ok(())
|
||||
},
|
||||
false=>Err(Error::AlreadyUnpaused),
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod test{
|
||||
use super::{Time,Timer,Error};
|
||||
macro_rules! sec {
|
||||
($s: expr) => {
|
||||
Time::from_secs($s)
|
||||
};
|
||||
}
|
||||
#[test]
|
||||
fn test_timer()->Result<(),Error>{
|
||||
//create a paused timer that reads 0s
|
||||
let mut timer=Timer::realtime_paused(sec!(0));
|
||||
//the paused timer at 1 second should read 0s
|
||||
assert_eq!(timer.time(sec!(1)),sec!(0));
|
||||
|
||||
//unpause it after one second
|
||||
timer.unpause(sec!(1))?;
|
||||
//the timer at 6 seconds should read 5s
|
||||
assert_eq!(timer.time(sec!(6)),sec!(5));
|
||||
|
||||
//pause the timer after 11 seconds
|
||||
timer.pause(sec!(11))?;
|
||||
//the paused timer at 20 seconds should read 10s
|
||||
assert_eq!(timer.time(sec!(20)),sec!(10));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -13,8 +13,9 @@ pub enum WindowInstruction{
|
||||
//holds thread handles to dispatch to
|
||||
struct WindowContext<'a>{
|
||||
manual_mouse_lock:bool,
|
||||
mouse:strafesnet_common::mouse::MouseState,//std::sync::Arc<std::sync::Mutex<>>
|
||||
mouse:crate::physics::MouseState,//std::sync::Arc<std::sync::Mutex<>>
|
||||
screen_size:glam::UVec2,
|
||||
user_settings:crate::settings::UserSettings,
|
||||
window:&'a winit::window::Window,
|
||||
physics_thread:crate::compat_worker::QNWorker<'a, TimedInstruction<crate::physics_worker::Instruction>>,
|
||||
}
|
||||
@ -27,16 +28,19 @@ impl WindowContext<'_>{
|
||||
match event {
|
||||
winit::event::WindowEvent::DroppedFile(path)=>{
|
||||
match crate::file::load(path.as_path()){
|
||||
Ok(map)=>self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::ChangeMap(map)}).unwrap(),
|
||||
Ok(map)=>{
|
||||
self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::Passthrough(
|
||||
crate::physics_worker::PassthroughInstruction::ClearModels
|
||||
)}).unwrap();
|
||||
self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::Passthrough(
|
||||
crate::physics_worker::PassthroughInstruction::GenerateModels(map)
|
||||
)}).unwrap();
|
||||
},
|
||||
Err(e)=>println!("Failed to load map: {e}"),
|
||||
}
|
||||
},
|
||||
winit::event::WindowEvent::Focused(state)=>{
|
||||
winit::event::WindowEvent::Focused(_state)=>{
|
||||
//pause unpause
|
||||
self.physics_thread.send(TimedInstruction{
|
||||
time,
|
||||
instruction:crate::physics_worker::Instruction::SetPaused(!state),
|
||||
}).unwrap();
|
||||
//recalculate pressed keys on focus
|
||||
},
|
||||
winit::event::WindowEvent::KeyboardInput{
|
||||
@ -107,11 +111,7 @@ impl WindowContext<'_>{
|
||||
"e"=>Some(InputInstruction::MoveUp(s)),
|
||||
"q"=>Some(InputInstruction::MoveDown(s)),
|
||||
"z"=>Some(InputInstruction::Zoom(s)),
|
||||
"r"=>if s{
|
||||
//mouse needs to be reset since the position is absolute
|
||||
self.mouse=strafesnet_common::mouse::MouseState::default();
|
||||
Some(InputInstruction::ResetAndRestart)
|
||||
}else{None},
|
||||
"r"=>if s{Some(InputInstruction::Reset)}else{None},
|
||||
"f"=>if s{Some(InputInstruction::PracticeFly)}else{None},
|
||||
_=>None,
|
||||
},
|
||||
@ -119,7 +119,7 @@ impl WindowContext<'_>{
|
||||
}{
|
||||
self.physics_thread.send(TimedInstruction{
|
||||
time,
|
||||
instruction:crate::physics_worker::Instruction::Input(input_instruction),
|
||||
instruction:crate::physics_worker::Instruction::Interpolate(input_instruction),
|
||||
}).unwrap();
|
||||
}
|
||||
},
|
||||
@ -147,7 +147,7 @@ impl WindowContext<'_>{
|
||||
self.mouse.pos+=delta;
|
||||
self.physics_thread.send(TimedInstruction{
|
||||
time,
|
||||
instruction:crate::physics_worker::Instruction::Input(InputInstruction::MoveMouse(self.mouse.pos)),
|
||||
instruction:crate::physics_worker::Instruction::Interpolate(InputInstruction::MoveMouse(self.mouse.pos)),
|
||||
}).unwrap();
|
||||
},
|
||||
winit::event::DeviceEvent::MouseWheel {
|
||||
@ -157,7 +157,7 @@ impl WindowContext<'_>{
|
||||
if false{//self.physics.style.use_scroll{
|
||||
self.physics_thread.send(TimedInstruction{
|
||||
time,
|
||||
instruction:crate::physics_worker::Instruction::Input(InputInstruction::Jump(true)),//activates the immediate jump path, but the style modifier prevents controls&CONTROL_JUMP bit from being set to auto jump
|
||||
instruction:crate::physics_worker::Instruction::Interpolate(InputInstruction::Jump(true)),//activates the immediate jump path, but the style modifier prevents controls&CONTROL_JUMP bit from being set to auto jump
|
||||
}).unwrap();
|
||||
}
|
||||
}
|
||||
@ -165,36 +165,48 @@ impl WindowContext<'_>{
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn worker<'a>(
|
||||
|
||||
pub struct WindowContextSetup<'a>{
|
||||
user_settings:crate::settings::UserSettings,
|
||||
window:&'a winit::window::Window,
|
||||
setup_context:crate::setup::SetupContext<'a>,
|
||||
scope:&'a std::thread::Scope<'a,'_>,
|
||||
)->crate::compat_worker::QNWorker<'a,TimedInstruction<WindowInstruction>>{
|
||||
// WindowContextSetup::new
|
||||
physics:crate::physics::PhysicsContext,
|
||||
graphics:crate::graphics::GraphicsState,
|
||||
}
|
||||
|
||||
impl<'a> WindowContextSetup<'a>{
|
||||
pub fn new(context:&crate::setup::SetupContext,window:&'a winit::window::Window)->Self{
|
||||
let user_settings=crate::settings::read_user_settings();
|
||||
|
||||
let mut graphics=crate::graphics::GraphicsState::new(&setup_context.device,&setup_context.queue,&setup_context.config);
|
||||
let mut physics=crate::physics::PhysicsContext::default();
|
||||
physics.load_user_settings(&user_settings);
|
||||
|
||||
let mut graphics=crate::graphics::GraphicsState::new(&context.device,&context.queue,&context.config);
|
||||
graphics.load_user_settings(&user_settings);
|
||||
|
||||
//WindowContextSetup::into_context
|
||||
Self{
|
||||
user_settings,
|
||||
window,
|
||||
graphics,
|
||||
physics,
|
||||
}
|
||||
}
|
||||
|
||||
fn into_context(self,setup_context:crate::setup::SetupContext<'a>)->WindowContext<'a>{
|
||||
let screen_size=glam::uvec2(setup_context.config.width,setup_context.config.height);
|
||||
let graphics_thread=crate::graphics_worker::new(graphics,setup_context.config,setup_context.surface,setup_context.device,setup_context.queue);
|
||||
//this obviously doesn't belong here, do something about it pls
|
||||
let bot_thread=crate::bot_worker::new(scope);
|
||||
let mut window_context=WindowContext{
|
||||
let graphics_thread=crate::graphics_worker::new(self.graphics,setup_context.config,setup_context.surface,setup_context.device,setup_context.queue);
|
||||
WindowContext{
|
||||
manual_mouse_lock:false,
|
||||
mouse:strafesnet_common::mouse::MouseState::default(),
|
||||
mouse:crate::physics::MouseState::default(),
|
||||
//make sure to update this!!!!!
|
||||
screen_size,
|
||||
window,
|
||||
physics_thread:crate::physics_worker::new(
|
||||
graphics_thread,
|
||||
user_settings,
|
||||
bot_thread,
|
||||
),
|
||||
};
|
||||
user_settings:self.user_settings,
|
||||
window:self.window,
|
||||
physics_thread:crate::physics_worker::new(self.physics,graphics_thread),
|
||||
}
|
||||
}
|
||||
|
||||
//WindowContextSetup::into_worker
|
||||
pub fn into_worker(self,setup_context:crate::setup::SetupContext<'a>)->crate::compat_worker::QNWorker<'a,TimedInstruction<WindowInstruction>>{
|
||||
let mut window_context=self.into_context(setup_context);
|
||||
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<WindowInstruction>|{
|
||||
match ins.instruction{
|
||||
WindowInstruction::RequestRedraw=>{
|
||||
@ -210,7 +222,9 @@ pub fn worker<'a>(
|
||||
window_context.physics_thread.send(
|
||||
TimedInstruction{
|
||||
time:ins.time,
|
||||
instruction:crate::physics_worker::Instruction::Resize(size)
|
||||
instruction:crate::physics_worker::Instruction::Passthrough(
|
||||
crate::physics_worker::PassthroughInstruction::Resize(size,window_context.user_settings.clone())
|
||||
)
|
||||
}
|
||||
).unwrap();
|
||||
}
|
||||
@ -218,10 +232,13 @@ pub fn worker<'a>(
|
||||
window_context.physics_thread.send(
|
||||
TimedInstruction{
|
||||
time:ins.time,
|
||||
instruction:crate::physics_worker::Instruction::Render
|
||||
instruction:crate::physics_worker::Instruction::Passthrough(
|
||||
crate::physics_worker::PassthroughInstruction::Render
|
||||
)
|
||||
}
|
||||
).unwrap();
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -190,7 +190,7 @@ mod test{
|
||||
for _ in 0..5 {
|
||||
let task = instruction::TimedInstruction{
|
||||
time:integer::Time::ZERO,
|
||||
instruction:strafesnet_common::physics::Instruction::Idle,
|
||||
instruction:physics::PhysicsInstruction::StrafeTick,
|
||||
};
|
||||
worker.send(task).unwrap();
|
||||
}
|
||||
@ -204,7 +204,7 @@ mod test{
|
||||
// Send a new task
|
||||
let task = instruction::TimedInstruction{
|
||||
time:integer::Time::ZERO,
|
||||
instruction:strafesnet_common::physics::Instruction::Idle,
|
||||
instruction:physics::PhysicsInstruction::StrafeTick,
|
||||
};
|
||||
worker.send(task).unwrap();
|
||||
|
||||
|
@ -1 +0,0 @@
|
||||
mangohud ../target/release/strafe-client bhop_maps/5692124338.snfm
|
Reference in New Issue
Block a user