Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3805921e36 | |||
| 2f276d3828 | |||
| a54055271c | |||
| 6d77b5661e | |||
| fe440249c3 | |||
| 6fdccb8b32 | |||
| 4db38c6496 | |||
| 32c16778c8 | |||
| 5810a55209 | |||
| 5a2231221d | |||
| 091da88b5c | |||
| 045e540020 | |||
| c14f8975fc | |||
| 8e228033e0 | |||
| eaecbc5b73 | |||
| 91a1b3d65e | |||
| e1d51ff2da | |||
| c5f2395b3a | |||
| 77aa83d6ac | |||
| 184b12a9cc | |||
| 9ba3484e25 | |||
| adcd7db4f1 | |||
| 710670d78e | |||
| 70e1514ef9 | |||
| 45e9b3d0ce | |||
| 1eac734c35 | |||
| 25e1312fe4 | |||
| cc8f6b059d | |||
| 4cd287dbb8 | |||
| 40ea0e6c7d | |||
| b45b92c627 | |||
| e90f53a111 | |||
| 6beb6c5f9a | |||
| a1507d4f26 | |||
| 77db5a7a6b |
@@ -1,6 +1,2 @@
|
|||||||
[registries.strafesnet]
|
[registries.strafesnet]
|
||||||
index = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
index = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
||||||
|
|
||||||
[target.x86_64-unknown-linux-gnu]
|
|
||||||
linker = "clang"
|
|
||||||
rustflags = ["-C", "link-arg=-fuse-ld=/usr/bin/mold"]
|
|
||||||
|
|||||||
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2251,7 +2251,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strafe-client"
|
name = "strafe-client"
|
||||||
version = "0.10.5"
|
version = "0.11.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arrayvec",
|
"arrayvec",
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ impl<I,T> InstructionCollector<I,T>
|
|||||||
pub const fn time(&self)->Time<T>{
|
pub const fn time(&self)->Time<T>{
|
||||||
self.time
|
self.time
|
||||||
}
|
}
|
||||||
|
#[inline]
|
||||||
pub fn collect(&mut self,instruction:Option<TimedInstruction<I,T>>){
|
pub fn collect(&mut self,instruction:Option<TimedInstruction<I,T>>){
|
||||||
if let Some(ins)=instruction{
|
if let Some(ins)=instruction{
|
||||||
if ins.time<self.time{
|
if ins.time<self.time{
|
||||||
@@ -70,7 +71,8 @@ impl<I,T> InstructionCollector<I,T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn instruction(self)->Option<TimedInstruction<I,T>>{
|
#[inline]
|
||||||
|
pub fn take(self)->Option<TimedInstruction<I,T>>{
|
||||||
//STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR
|
//STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR
|
||||||
self.instruction.map(|instruction|TimedInstruction{
|
self.instruction.map(|instruction|TimedInstruction{
|
||||||
time:self.time,
|
time:self.time,
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ impl Run{
|
|||||||
match &self.state{
|
match &self.state{
|
||||||
RunState::Created=>Time::ZERO,
|
RunState::Created=>Time::ZERO,
|
||||||
RunState::Started{timer}=>timer.time(time),
|
RunState::Started{timer}=>timer.time(time),
|
||||||
RunState::Finished{timer}=>timer.time(time),
|
RunState::Finished{timer}=>timer.time(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn start(&mut self,time:PhysicsTime)->Result<(),Error>{
|
pub fn start(&mut self,time:PhysicsTime)->Result<(),Error>{
|
||||||
@@ -110,4 +110,10 @@ impl Run{
|
|||||||
self.flagged=Some(flag_reason);
|
self.flagged=Some(flag_reason);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn get_finish_time(&self)->Option<Time>{
|
||||||
|
match &self.state{
|
||||||
|
RunState::Finished{timer}=>Some(timer.time()),
|
||||||
|
_=>None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -157,7 +157,7 @@ impl<T:TimerState> TimerFixed<T,Paused>
|
|||||||
where Time<T::In>:Copy,
|
where Time<T::In>:Copy,
|
||||||
{
|
{
|
||||||
pub fn into_unpaused(self,time:Time<T::In>)->TimerFixed<T,Unpaused>{
|
pub fn into_unpaused(self,time:Time<T::In>)->TimerFixed<T,Unpaused>{
|
||||||
let new_time=self.time(time);
|
let new_time=self.time();
|
||||||
let mut timer=TimerFixed{
|
let mut timer=TimerFixed{
|
||||||
state:self.state,
|
state:self.state,
|
||||||
_paused:Unpaused,
|
_paused:Unpaused,
|
||||||
@@ -165,6 +165,9 @@ impl<T:TimerState> TimerFixed<T,Paused>
|
|||||||
timer.set_time(time,new_time);
|
timer.set_time(time,new_time);
|
||||||
timer
|
timer
|
||||||
}
|
}
|
||||||
|
pub fn time(&self)->Time<T::Out>{
|
||||||
|
self.state.get_offset().coerce()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl<T:TimerState> TimerFixed<T,Unpaused>
|
impl<T:TimerState> TimerFixed<T,Unpaused>
|
||||||
where Time<T::In>:Copy,
|
where Time<T::In>:Copy,
|
||||||
@@ -178,6 +181,9 @@ impl<T:TimerState> TimerFixed<T,Unpaused>
|
|||||||
timer.set_time(time,new_time);
|
timer.set_time(time,new_time);
|
||||||
timer
|
timer
|
||||||
}
|
}
|
||||||
|
pub fn time(&self,time:Time<T::In>)->Time<T::Out>{
|
||||||
|
self.state.get_time(time)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//the new constructor and time queries are generic across both
|
//the new constructor and time queries are generic across both
|
||||||
@@ -199,12 +205,6 @@ impl<T:TimerState,P:PauseState> TimerFixed<T,P>{
|
|||||||
pub fn into_state(self)->T{
|
pub fn into_state(self)->T{
|
||||||
self.state
|
self.state
|
||||||
}
|
}
|
||||||
pub fn time(&self,time:Time<T::In>)->Time<T::Out>{
|
|
||||||
match P::IS_PAUSED{
|
|
||||||
true=>self.state.get_offset().coerce(),
|
|
||||||
false=>self.state.get_time(time),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn set_time(&mut self,time:Time<T::In>,new_time:Time<T::Out>){
|
pub fn set_time(&mut self,time:Time<T::In>,new_time:Time<T::Out>){
|
||||||
match P::IS_PAUSED{
|
match P::IS_PAUSED{
|
||||||
true=>self.state.set_offset(new_time.coerce()),
|
true=>self.state.set_offset(new_time.coerce()),
|
||||||
@@ -256,7 +256,7 @@ impl<T:TimerState> Timer<T>
|
|||||||
}
|
}
|
||||||
pub fn time(&self,time:Time<T::In>)->Time<T::Out>{
|
pub fn time(&self,time:Time<T::In>)->Time<T::Out>{
|
||||||
match self{
|
match self{
|
||||||
Self::Paused(timer)=>timer.time(time),
|
Self::Paused(timer)=>timer.time(),
|
||||||
Self::Unpaused(timer)=>timer.time(time),
|
Self::Unpaused(timer)=>timer.time(time),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -329,7 +329,7 @@ mod test{
|
|||||||
//create a paused timer that reads 0s
|
//create a paused timer that reads 0s
|
||||||
let timer=TimerFixed::<Scaled<Parent,Calculated>,Paused>::from_state(Scaled::new(0.5f32.try_into().unwrap(),sec!(0)));
|
let timer=TimerFixed::<Scaled<Parent,Calculated>,Paused>::from_state(Scaled::new(0.5f32.try_into().unwrap(),sec!(0)));
|
||||||
//the paused timer at 1 second should read 0s
|
//the paused timer at 1 second should read 0s
|
||||||
assert_eq!(timer.time(sec!(1)),sec!(0));
|
assert_eq!(timer.time(),sec!(0));
|
||||||
|
|
||||||
//unpause it after one second
|
//unpause it after one second
|
||||||
let timer=timer.into_unpaused(sec!(1));
|
let timer=timer.into_unpaused(sec!(1));
|
||||||
@@ -339,7 +339,7 @@ mod test{
|
|||||||
//pause the timer after 11 seconds
|
//pause the timer after 11 seconds
|
||||||
let timer=timer.into_paused(sec!(11));
|
let timer=timer.into_paused(sec!(11));
|
||||||
//the paused timer at 20 seconds should read 5s
|
//the paused timer at 20 seconds should read 5s
|
||||||
assert_eq!(timer.time(sec!(20)),sec!(5));
|
assert_eq!(timer.time(),sec!(5));
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_timer()->Result<(),Error>{
|
fn test_timer()->Result<(),Error>{
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
macro_rules! type_from_lua_userdata{
|
macro_rules! type_from_lua_userdata{
|
||||||
($asd:ident)=>{
|
($ty:ident)=>{
|
||||||
impl mlua::FromLua for $asd{
|
impl mlua::FromLua for $ty{
|
||||||
fn from_lua(value:mlua::Value,_lua:&mlua::Lua)->Result<Self,mlua::Error>{
|
fn from_lua(value:mlua::Value,_lua:&mlua::Lua)->Result<Self,mlua::Error>{
|
||||||
match value{
|
match value{
|
||||||
mlua::Value::UserData(ud)=>Ok(*ud.borrow::<Self>()?),
|
mlua::Value::UserData(ud)=>Ok(*ud.borrow::<Self>()?),
|
||||||
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($asd),other))),
|
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($ty),other))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
macro_rules! type_from_lua_userdata_lua_lifetime{
|
macro_rules! type_from_lua_userdata_lua_lifetime{
|
||||||
($asd:ident)=>{
|
($ty:ident)=>{
|
||||||
impl mlua::FromLua for $asd<'static>{
|
impl mlua::FromLua for $ty<'static>{
|
||||||
fn from_lua(value:mlua::Value,_lua:&mlua::Lua)->Result<Self,mlua::Error>{
|
fn from_lua(value:mlua::Value,_lua:&mlua::Lua)->Result<Self,mlua::Error>{
|
||||||
match value{
|
match value{
|
||||||
mlua::Value::UserData(ud)=>Ok(*ud.borrow::<Self>()?),
|
mlua::Value::UserData(ud)=>Ok(*ud.borrow::<Self>()?),
|
||||||
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($asd),other))),
|
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($ty),other))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,8 +59,8 @@ fn schedule_thread(lua:&mlua::Lua,dt:mlua::Value)->Result<(),mlua::Error>{
|
|||||||
match delay.classify(){
|
match delay.classify(){
|
||||||
std::num::FpCategory::Nan=>Err(mlua::Error::runtime("NaN"))?,
|
std::num::FpCategory::Nan=>Err(mlua::Error::runtime("NaN"))?,
|
||||||
// cases where the number is too large to schedule
|
// cases where the number is too large to schedule
|
||||||
std::num::FpCategory::Infinite=>return Ok(()),
|
std::num::FpCategory::Infinite
|
||||||
std::num::FpCategory::Normal=>if (u64::MAX as f64)<delay{
|
|std::num::FpCategory::Normal if (u64::MAX as f64)<delay=>{
|
||||||
return Ok(());
|
return Ok(());
|
||||||
},
|
},
|
||||||
_=>(),
|
_=>(),
|
||||||
|
|||||||
@@ -1,98 +1,347 @@
|
|||||||
use binrw::{BinReaderExt, binrw};
|
use binrw::{binrw,BinReaderExt,BinWrite,BinWriterExt};
|
||||||
|
|
||||||
|
use crate::newtypes;
|
||||||
|
use crate::file::BlockId;
|
||||||
|
use strafesnet_common::physics::Time;
|
||||||
|
|
||||||
|
const VERSION:u32=0;
|
||||||
|
|
||||||
|
type TimedPhysicsInstruction=strafesnet_common::instruction::TimedInstruction<strafesnet_common::physics::Instruction,strafesnet_common::physics::TimeInner>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error{
|
pub enum Error{
|
||||||
InvalidHeader,
|
InvalidHeader(binrw::Error),
|
||||||
InvalidSegment(binrw::Error),
|
InvalidSegment(binrw::Error),
|
||||||
|
SegmentConvert(newtypes::integer::RatioError),
|
||||||
|
InstructionConvert(newtypes::physics::InstructionConvert),
|
||||||
|
InstructionWrite(binrw::Error),
|
||||||
InvalidSegmentId(SegmentId),
|
InvalidSegmentId(SegmentId),
|
||||||
|
InvalidData(binrw::Error),
|
||||||
|
IO(std::io::Error),
|
||||||
File(crate::file::Error),
|
File(crate::file::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bot files are simply the sequence of instructions that the physics received during the run.
|
||||||
|
// The instructions are partitioned into timestamped blocks for ease of streaming.
|
||||||
|
//
|
||||||
|
// Keyframe information required for efficient seeking
|
||||||
|
// is part of a different file, and is generated from this file.
|
||||||
|
|
||||||
/* block types
|
/* block types
|
||||||
|
|
||||||
BLOCK_BOT_HEADER:
|
BLOCK_BOT_HEADER:
|
||||||
u128 map_resource_uuid //which map is this bot running
|
// Segments are laid out in chronological order,
|
||||||
//don't include style info in bot header because it's in the simulation state
|
// but block_id is not necessarily in ascending order.
|
||||||
//blocks are laid out in chronological order, but indices may jump around.
|
//
|
||||||
u64 num_segments
|
// This is to place the final segment close to the start of the file,
|
||||||
|
// which allows the duration of the bot to be conveniently calculated
|
||||||
|
// from the first and last instruction timestamps.
|
||||||
|
//
|
||||||
|
// Use exact physics version for replay playback
|
||||||
|
// Use highest compatible physics version for verification
|
||||||
|
u32 physics_version
|
||||||
|
u32 num_segments
|
||||||
for _ in 0..num_segments{
|
for _ in 0..num_segments{
|
||||||
i64 time //simulation_state timestamp
|
i64 time
|
||||||
u64 block_id
|
u32 instruction_count
|
||||||
|
u32 block_id
|
||||||
}
|
}
|
||||||
|
|
||||||
BLOCK_BOT_SEGMENT:
|
BLOCK_BOT_SEGMENT:
|
||||||
//format version indicates what version of these structures to use
|
// segments can potentially be losslessly compressed!
|
||||||
SimulationState simulation_state //SimulationState is just under ClientState which includes Play/Pause events that the simulation doesn't know about.
|
for _ in 0..instruction_count{
|
||||||
//to read, greedily decode instructions until eof
|
// TODO: delta encode as much as possible (time,mousepos)
|
||||||
loop{
|
i64 time
|
||||||
//delta encode as much as possible (time,mousepos)
|
physics::Instruction instruction
|
||||||
//strafe ticks are implied
|
|
||||||
//physics can be implied in an input-only bot file
|
|
||||||
TimedInstruction<SimulationInstruction> instruction
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//error hiding mock code
|
#[binrw]
|
||||||
mod simulation{
|
#[brw(little)]
|
||||||
#[super::binrw]
|
struct SegmentHeader{
|
||||||
#[brw(little)]
|
time:i64,
|
||||||
pub struct State{}
|
instruction_count:u32,
|
||||||
#[super::binrw]
|
block_id:BlockId,
|
||||||
#[brw(little)]
|
}
|
||||||
pub struct Instruction{}
|
#[binrw]
|
||||||
|
#[brw(little)]
|
||||||
|
struct Header{
|
||||||
|
physics_version:u32,
|
||||||
|
num_segments:u32,
|
||||||
|
#[br(count=num_segments)]
|
||||||
|
segments:Vec<SegmentHeader>,
|
||||||
}
|
}
|
||||||
// mod instruction{
|
|
||||||
// #[super::binrw]
|
|
||||||
// #[brw(little)]
|
|
||||||
// pub struct TimedInstruction<Instruction:binrw::BinRead+binrw::BinWrite>{
|
|
||||||
// time:u64,
|
|
||||||
// instruction:Instruction
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// mod timeline{
|
|
||||||
// #[super::binrw]
|
|
||||||
// #[brw(little)]
|
|
||||||
// pub struct Timeline<Instruction:binrw::BinRead+binrw::BinWrite>{
|
|
||||||
// #[bw(try_calc(u32::try_from(instructions.len())))]
|
|
||||||
// instruction_count:u32,
|
|
||||||
// #[br(count=instruction_count)]
|
|
||||||
// instructions:Vec<super::instruction::TimedInstruction<Instruction>>
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
//serious code
|
|
||||||
|
|
||||||
#[binrw]
|
#[binrw]
|
||||||
#[brw(little)]
|
#[brw(little)]
|
||||||
#[derive(Clone,Copy,Debug,id::Id)]
|
#[derive(Clone,Copy,Debug,id::Id)]
|
||||||
pub struct SegmentId(u32);
|
pub struct SegmentId(u32);
|
||||||
|
|
||||||
#[binrw]
|
|
||||||
#[brw(little)]
|
|
||||||
pub struct Segment{
|
pub struct Segment{
|
||||||
state:simulation::State,
|
pub instructions:Vec<TimedPhysicsInstruction>
|
||||||
//#[bw(try_calc(u32::try_from(instructions.len())))]
|
}
|
||||||
//instruction_count:u32,
|
|
||||||
//#[br(count=instruction_count)]
|
|
||||||
//instructions:Vec<instruction::TimedInstruction<simulation::Instruction>>
|
|
||||||
|
|
||||||
//please remember that strafe ticks are implicit! 33% smaller bot files
|
#[derive(Clone,Copy,Debug)]
|
||||||
|
pub struct SegmentInfo{
|
||||||
|
/// time of the first instruction in this segment.
|
||||||
|
time:Time,
|
||||||
|
instruction_count:u32,
|
||||||
|
/// How many total instructions in segments up to and including this segment
|
||||||
|
/// Alternatively, the id of the first instruction be in the _next_ segment
|
||||||
|
instructions_subtotal:u64,
|
||||||
|
block_id:BlockId,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct StreamableBot<R:BinReaderExt>{
|
pub struct StreamableBot<R:BinReaderExt>{
|
||||||
file:crate::file::File<R>,
|
file:crate::file::File<R>,
|
||||||
//timeline:timeline::Timeline<SegmentId>,
|
segment_map:Vec<SegmentInfo>,
|
||||||
segment_id_to_block_id:Vec<crate::file::BlockId>,
|
|
||||||
}
|
}
|
||||||
impl<R:BinReaderExt> StreamableBot<R>{
|
impl<R:BinReaderExt> StreamableBot<R>{
|
||||||
pub(crate) fn new(file:crate::file::File<R>)->Result<Self,Error>{
|
pub(crate) fn new(mut file:crate::file::File<R>)->Result<Self,Error>{
|
||||||
Err(Error::InvalidHeader)
|
//assume the file seek is in the right place to start reading header
|
||||||
|
let header:Header=file.data_mut().read_le().map_err(Error::InvalidHeader)?;
|
||||||
|
let mut instructions_subtotal=0;
|
||||||
|
let segment_map=header.segments.into_iter().map(|SegmentHeader{time,instruction_count,block_id}|{
|
||||||
|
instructions_subtotal+=instruction_count as u64;
|
||||||
|
SegmentInfo{
|
||||||
|
time:Time::raw(time),
|
||||||
|
instruction_count,
|
||||||
|
instructions_subtotal,
|
||||||
|
block_id,
|
||||||
|
}
|
||||||
|
}).collect();
|
||||||
|
Ok(Self{
|
||||||
|
file,
|
||||||
|
segment_map,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
pub fn load_segment(&mut self,segment_id:SegmentId)->Result<Segment,Error>{
|
fn get_segment_info(&self,segment_id:SegmentId)->Result<SegmentInfo,Error>{
|
||||||
let block_id=*self.segment_id_to_block_id.get(segment_id.get() as usize).ok_or(Error::InvalidSegmentId(segment_id))?;
|
Ok(*self.segment_map.get(segment_id.get() as usize).ok_or(Error::InvalidSegmentId(segment_id))?)
|
||||||
let mut block=self.file.block_reader(block_id).map_err(Error::File)?;
|
}
|
||||||
let segment=block.read_le().map_err(Error::InvalidSegment)?;
|
pub fn find_segments_instruction_range(&self,start_instruction:u64,end_instruction:u64)->&[SegmentInfo]{
|
||||||
|
let start=self.segment_map.partition_point(|segment_info|segment_info.instructions_subtotal<start_instruction);
|
||||||
|
let end=self.segment_map.partition_point(|segment_info|segment_info.instructions_subtotal<end_instruction);
|
||||||
|
&self.segment_map[start..=end]
|
||||||
|
}
|
||||||
|
// pub fn find_segments_time_range(&self,start_time:Time,end_time:Time)->&[SegmentInfo]{
|
||||||
|
// // TODO: This is off by one, both should be one less
|
||||||
|
// let start=self.segment_map.partition_point(|segment_info|segment_info.time<start_time);
|
||||||
|
// let end=self.segment_map.partition_point(|segment_info|segment_info.time<end_time);
|
||||||
|
// &self.segment_map[start..=end]
|
||||||
|
// }
|
||||||
|
fn append_to_segment(&mut self,segment_info:SegmentInfo,segment:&mut Segment)->Result<(),Error>{
|
||||||
|
let mut block=self.file.block_reader(segment_info.block_id).map_err(Error::File)?;
|
||||||
|
for _ in 0..segment_info.instruction_count{
|
||||||
|
let instruction:newtypes::physics::TimedInstruction=block.read_le().map_err(Error::InvalidSegment)?;
|
||||||
|
segment.instructions.push(instruction.try_into().map_err(Error::SegmentConvert)?);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn load_segment(&mut self,segment_info:SegmentInfo)->Result<Segment,Error>{
|
||||||
|
let mut segment=Segment{
|
||||||
|
instructions:Vec::with_capacity(segment_info.instruction_count as usize),
|
||||||
|
};
|
||||||
|
self.append_to_segment(segment_info,&mut segment)?;
|
||||||
|
Ok(segment)
|
||||||
|
}
|
||||||
|
pub fn read_all(&mut self)->Result<Segment,Error>{
|
||||||
|
let mut segment=Segment{
|
||||||
|
instructions:Vec::new(),
|
||||||
|
};
|
||||||
|
for i in 0..self.segment_map.len(){
|
||||||
|
let segment_info=self.segment_map[i];
|
||||||
|
self.append_to_segment(segment_info,&mut segment)?;
|
||||||
|
}
|
||||||
Ok(segment)
|
Ok(segment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MAX_BLOCK_SIZE:usize=64*1024;//64 kB
|
||||||
|
pub fn write_bot<W:BinWriterExt>(mut writer:W,physics_version:u32,instructions:impl IntoIterator<Item=TimedPhysicsInstruction>)->Result<(),Error>{
|
||||||
|
// decide which instructions to put in which segment
|
||||||
|
// write segment 1 to block 1
|
||||||
|
// write segment N to block 2
|
||||||
|
// write rest of segments
|
||||||
|
// 1 2 3 4 5
|
||||||
|
// becomes
|
||||||
|
// [1 5] 2 3 4
|
||||||
|
struct SegmentHeaderInfo{
|
||||||
|
time:Time,
|
||||||
|
instruction_count:u32,
|
||||||
|
range:core::ops::Range<usize>
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut segment_header_infos=Vec::new();
|
||||||
|
let mut raw_segments=std::io::Cursor::new(Vec::new());
|
||||||
|
|
||||||
|
// block info
|
||||||
|
let mut start_time=Time::ZERO;
|
||||||
|
let mut start_position=raw_segments.position() as usize;
|
||||||
|
let mut instruction_count=0;
|
||||||
|
|
||||||
|
let mut last_position=start_position;
|
||||||
|
|
||||||
|
let mut iter=instructions.into_iter();
|
||||||
|
|
||||||
|
macro_rules! collect_instruction{
|
||||||
|
($instruction:expr)=>{
|
||||||
|
let time=$instruction.time;
|
||||||
|
let instruction_writable:newtypes::physics::TimedInstruction=$instruction.try_into().map_err(Error::InstructionConvert)?;
|
||||||
|
instruction_writable.write_le(&mut raw_segments).map_err(Error::InstructionWrite)?;
|
||||||
|
instruction_count+=1;
|
||||||
|
let position=raw_segments.position() as usize;
|
||||||
|
// exceeds max block size
|
||||||
|
if MAX_BLOCK_SIZE<position-last_position{
|
||||||
|
segment_header_infos.push(SegmentHeaderInfo{
|
||||||
|
time:start_time,
|
||||||
|
instruction_count,
|
||||||
|
range:start_position..last_position,
|
||||||
|
});
|
||||||
|
start_position=last_position;
|
||||||
|
instruction_count=0;
|
||||||
|
start_time=time;
|
||||||
|
}
|
||||||
|
last_position=position;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// unroll one loop iteration to grab the starting time
|
||||||
|
if let Some(instruction)=iter.next(){
|
||||||
|
start_time=instruction.time;
|
||||||
|
collect_instruction!(instruction);
|
||||||
|
}
|
||||||
|
|
||||||
|
for instruction in iter{
|
||||||
|
collect_instruction!(instruction);
|
||||||
|
}
|
||||||
|
//last block, whatever size it happens to be
|
||||||
|
{
|
||||||
|
let final_position=raw_segments.position() as usize;
|
||||||
|
segment_header_infos.push(SegmentHeaderInfo{
|
||||||
|
time:start_time,
|
||||||
|
instruction_count,
|
||||||
|
range:start_position..final_position,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// drop cursor
|
||||||
|
let raw_segments=raw_segments.into_inner();
|
||||||
|
|
||||||
|
let num_segments=segment_header_infos.len();
|
||||||
|
|
||||||
|
// segments list is in chronological order
|
||||||
|
let make_segment_header=|block_id,&SegmentHeaderInfo{time,instruction_count,range:ref _range}|SegmentHeader{
|
||||||
|
time:time.get(),
|
||||||
|
instruction_count,
|
||||||
|
block_id,
|
||||||
|
};
|
||||||
|
let segments=if 2<num_segments{
|
||||||
|
let mut segments=Vec::with_capacity(num_segments);
|
||||||
|
// segment 1 is second block
|
||||||
|
if let Some(seg)=segment_header_infos.first(){
|
||||||
|
segments.push(make_segment_header(BlockId::new(1),seg));
|
||||||
|
}
|
||||||
|
// rest of segments start at fourth block
|
||||||
|
for (i,seg) in segment_header_infos[1..num_segments-1].iter().enumerate(){
|
||||||
|
make_segment_header(BlockId::new(3+i as u32),seg);
|
||||||
|
}
|
||||||
|
// last segment is third block
|
||||||
|
if let Some(seg)=segment_header_infos.last(){
|
||||||
|
segments.push(make_segment_header(BlockId::new(2),seg));
|
||||||
|
}
|
||||||
|
segments
|
||||||
|
}else{
|
||||||
|
// all segments in order
|
||||||
|
segment_header_infos.iter().enumerate().map(|(i,seg)|
|
||||||
|
make_segment_header(BlockId::new(1+i as u32),seg)
|
||||||
|
).collect()
|
||||||
|
};
|
||||||
|
|
||||||
|
let header=Header{
|
||||||
|
physics_version,
|
||||||
|
num_segments:num_segments as u32,
|
||||||
|
segments,
|
||||||
|
};
|
||||||
|
|
||||||
|
// map header is +1
|
||||||
|
let block_count=1+num_segments as u32;
|
||||||
|
|
||||||
|
let mut offset=crate::file::Header::calculate_size(block_count) as u64;
|
||||||
|
// block_location is one longer than block_count
|
||||||
|
let mut block_location=Vec::with_capacity(1+block_count as usize);
|
||||||
|
|
||||||
|
//probe header length
|
||||||
|
let mut bot_header_data=Vec::new();
|
||||||
|
binrw::BinWrite::write_le(&header,&mut std::io::Cursor::new(&mut bot_header_data)).map_err(Error::InvalidData)?;
|
||||||
|
|
||||||
|
// the first block location is the map header
|
||||||
|
block_location.push(offset);
|
||||||
|
offset+=bot_header_data.len() as u64;
|
||||||
|
block_location.push(offset);
|
||||||
|
|
||||||
|
// priming includes file header + first 3 blocks [bot header, first segment, last segment]
|
||||||
|
let priming=if 2<num_segments{
|
||||||
|
// segment 1 is block 2
|
||||||
|
if let Some(seg)=segment_header_infos.first(){
|
||||||
|
offset+=seg.range.len() as u64;
|
||||||
|
block_location.push(offset);
|
||||||
|
}
|
||||||
|
// last segment is block 3
|
||||||
|
if let Some(seg)=segment_header_infos.last(){
|
||||||
|
offset+=seg.range.len() as u64;
|
||||||
|
block_location.push(offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
let priming=offset;
|
||||||
|
|
||||||
|
// rest of segments
|
||||||
|
for seg in &segment_header_infos[1..num_segments-1]{
|
||||||
|
offset+=seg.range.len() as u64;
|
||||||
|
block_location.push(offset);
|
||||||
|
}
|
||||||
|
priming
|
||||||
|
}else{
|
||||||
|
// all segments in order
|
||||||
|
for seg in &segment_header_infos{
|
||||||
|
offset+=seg.range.len() as u64;
|
||||||
|
block_location.push(offset);
|
||||||
|
}
|
||||||
|
offset
|
||||||
|
};
|
||||||
|
|
||||||
|
let file_header=crate::file::Header{
|
||||||
|
fourcc:crate::file::FourCC::Bot,
|
||||||
|
version:VERSION,
|
||||||
|
priming,
|
||||||
|
resource:0,
|
||||||
|
block_count,
|
||||||
|
block_location,
|
||||||
|
};
|
||||||
|
|
||||||
|
// write file header
|
||||||
|
writer.write_le(&file_header).map_err(Error::InvalidData)?;
|
||||||
|
// write bot header
|
||||||
|
writer.write(&bot_header_data).map_err(Error::IO)?;
|
||||||
|
|
||||||
|
// write blocks
|
||||||
|
if 2<num_segments{
|
||||||
|
// segment 1 is block 2
|
||||||
|
if let Some(seg)=segment_header_infos.first(){
|
||||||
|
writer.write(&raw_segments[seg.range.clone()]).map_err(Error::IO)?;
|
||||||
|
}
|
||||||
|
// last segment is block 3
|
||||||
|
if let Some(seg)=segment_header_infos.last(){
|
||||||
|
writer.write(&raw_segments[seg.range.clone()]).map_err(Error::IO)?;
|
||||||
|
}
|
||||||
|
// rest of segments
|
||||||
|
for seg in &segment_header_infos[1..num_segments-1]{
|
||||||
|
writer.write(&raw_segments[seg.range.clone()]).map_err(Error::IO)?;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
// all segments in order
|
||||||
|
for seg in segment_header_infos{
|
||||||
|
writer.write(&raw_segments[seg.range]).map_err(Error::IO)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,9 @@
|
|||||||
pub const fn flag(b:bool,mask:u8)->u8{
|
pub const fn flag(b:bool,mask:u8)->u8{
|
||||||
(-(b as i8) as u8)&mask
|
(-(b as i8) as u8)&mask
|
||||||
}
|
}
|
||||||
|
pub fn bool_from_u8(value:u8)->bool{
|
||||||
|
value!=0
|
||||||
|
}
|
||||||
|
pub fn bool_into_u8(value:&bool)->u8{
|
||||||
|
*value as u8
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,6 +38,23 @@ pub struct Ratio64Vec2{
|
|||||||
pub x:Ratio64,
|
pub x:Ratio64,
|
||||||
pub y:Ratio64,
|
pub y:Ratio64,
|
||||||
}
|
}
|
||||||
|
impl TryInto<strafesnet_common::integer::Ratio64Vec2> for Ratio64Vec2{
|
||||||
|
type Error=RatioError;
|
||||||
|
fn try_into(self)->Result<strafesnet_common::integer::Ratio64Vec2,Self::Error>{
|
||||||
|
Ok(strafesnet_common::integer::Ratio64Vec2{
|
||||||
|
x:self.x.try_into()?,
|
||||||
|
y:self.y.try_into()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<strafesnet_common::integer::Ratio64Vec2> for Ratio64Vec2{
|
||||||
|
fn from(value:strafesnet_common::integer::Ratio64Vec2)->Self{
|
||||||
|
Self{
|
||||||
|
x:value.x.into(),
|
||||||
|
y:value.y.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub type Angle32=i32;
|
pub type Angle32=i32;
|
||||||
pub type Planar64=i64;
|
pub type Planar64=i64;
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
mod common;
|
mod common;
|
||||||
pub mod aabb;
|
pub mod aabb;
|
||||||
pub mod model;
|
pub mod model;
|
||||||
|
pub mod mouse;
|
||||||
pub mod integer;
|
pub mod integer;
|
||||||
|
pub mod physics;
|
||||||
pub mod gameplay_modes;
|
pub mod gameplay_modes;
|
||||||
pub mod gameplay_style;
|
pub mod gameplay_style;
|
||||||
pub mod gameplay_attributes;
|
pub mod gameplay_attributes;
|
||||||
|
|||||||
25
lib/snf/src/newtypes/mouse.rs
Normal file
25
lib/snf/src/newtypes/mouse.rs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
use super::integer::Time;
|
||||||
|
|
||||||
|
#[binrw::binrw]
|
||||||
|
#[brw(little)]
|
||||||
|
pub struct MouseState{
|
||||||
|
pub pos:[i32;2],
|
||||||
|
pub time:Time,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Into<strafesnet_common::mouse::MouseState<T>> for MouseState{
|
||||||
|
fn into(self)->strafesnet_common::mouse::MouseState<T>{
|
||||||
|
strafesnet_common::mouse::MouseState{
|
||||||
|
pos:self.pos.into(),
|
||||||
|
time:strafesnet_common::integer::Time::raw(self.time),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T> From<strafesnet_common::mouse::MouseState<T>> for MouseState{
|
||||||
|
fn from(value:strafesnet_common::mouse::MouseState<T>)->Self{
|
||||||
|
Self{
|
||||||
|
pos:value.pos.to_array(),
|
||||||
|
time:value.time.get(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
156
lib/snf/src/newtypes/physics.rs
Normal file
156
lib/snf/src/newtypes/physics.rs
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
use super::integer::Time;
|
||||||
|
use super::common::{bool_from_u8,bool_into_u8};
|
||||||
|
|
||||||
|
type TimedPhysicsInstruction=strafesnet_common::instruction::TimedInstruction<strafesnet_common::physics::Instruction,strafesnet_common::physics::TimeInner>;
|
||||||
|
|
||||||
|
#[binrw::binrw]
|
||||||
|
#[brw(little)]
|
||||||
|
pub struct TimedInstruction{
|
||||||
|
pub time:Time,
|
||||||
|
pub instruction:Instruction,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryInto<TimedPhysicsInstruction> for TimedInstruction{
|
||||||
|
type Error=super::integer::RatioError;
|
||||||
|
fn try_into(self)->Result<TimedPhysicsInstruction,Self::Error>{
|
||||||
|
Ok(strafesnet_common::instruction::TimedInstruction{
|
||||||
|
time:strafesnet_common::integer::Time::raw(self.time),
|
||||||
|
instruction:self.instruction.try_into()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl TryFrom<TimedPhysicsInstruction> for TimedInstruction{
|
||||||
|
type Error=super::physics::InstructionConvert;
|
||||||
|
fn try_from(value:TimedPhysicsInstruction)->Result<Self,Self::Error>{
|
||||||
|
Ok(Self{
|
||||||
|
time:value.time.get(),
|
||||||
|
instruction:value.instruction.try_into()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[binrw::binrw]
|
||||||
|
#[brw(little)]
|
||||||
|
pub enum Instruction{
|
||||||
|
#[brw(magic=0u8)]
|
||||||
|
ReplaceMouse{
|
||||||
|
m0:super::mouse::MouseState,
|
||||||
|
m1:super::mouse::MouseState
|
||||||
|
},
|
||||||
|
#[brw(magic=1u8)]
|
||||||
|
SetNextMouse(super::mouse::MouseState),
|
||||||
|
#[brw(magic=2u8)]
|
||||||
|
SetMoveRight(
|
||||||
|
#[br(map=bool_from_u8)]
|
||||||
|
#[bw(map=bool_into_u8)]
|
||||||
|
bool),
|
||||||
|
#[brw(magic=3u8)]
|
||||||
|
SetMoveUp(
|
||||||
|
#[br(map=bool_from_u8)]
|
||||||
|
#[bw(map=bool_into_u8)]
|
||||||
|
bool),
|
||||||
|
#[brw(magic=4u8)]
|
||||||
|
SetMoveBack(
|
||||||
|
#[br(map=bool_from_u8)]
|
||||||
|
#[bw(map=bool_into_u8)]
|
||||||
|
bool),
|
||||||
|
#[brw(magic=5u8)]
|
||||||
|
SetMoveLeft(
|
||||||
|
#[br(map=bool_from_u8)]
|
||||||
|
#[bw(map=bool_into_u8)]
|
||||||
|
bool),
|
||||||
|
#[brw(magic=6u8)]
|
||||||
|
SetMoveDown(
|
||||||
|
#[br(map=bool_from_u8)]
|
||||||
|
#[bw(map=bool_into_u8)]
|
||||||
|
bool),
|
||||||
|
#[brw(magic=7u8)]
|
||||||
|
SetMoveForward(
|
||||||
|
#[br(map=bool_from_u8)]
|
||||||
|
#[bw(map=bool_into_u8)]
|
||||||
|
bool),
|
||||||
|
#[brw(magic=8u8)]
|
||||||
|
SetJump(
|
||||||
|
#[br(map=bool_from_u8)]
|
||||||
|
#[bw(map=bool_into_u8)]
|
||||||
|
bool),
|
||||||
|
#[brw(magic=9u8)]
|
||||||
|
SetZoom(
|
||||||
|
#[br(map=bool_from_u8)]
|
||||||
|
#[bw(map=bool_into_u8)]
|
||||||
|
bool),
|
||||||
|
#[brw(magic=10u8)]
|
||||||
|
Reset,
|
||||||
|
#[brw(magic=11u8)]
|
||||||
|
Restart(super::gameplay_modes::ModeId),
|
||||||
|
#[brw(magic=12u8)]
|
||||||
|
Spawn(super::gameplay_modes::ModeId,super::gameplay_modes::StageId),
|
||||||
|
#[brw(magic=13u8)]
|
||||||
|
PracticeFly,
|
||||||
|
#[brw(magic=14u8)]
|
||||||
|
SetSensitivity(super::integer::Ratio64Vec2),
|
||||||
|
#[brw(magic=255u8)]
|
||||||
|
Idle,
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum InstructionConvert{
|
||||||
|
/// This is an instruction that can be dropped when serializing
|
||||||
|
DropInstruction,
|
||||||
|
}
|
||||||
|
impl std::fmt::Display for InstructionConvert{
|
||||||
|
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
||||||
|
write!(f,"{self:?}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl std::error::Error for InstructionConvert{}
|
||||||
|
impl TryInto<strafesnet_common::physics::Instruction> for Instruction{
|
||||||
|
type Error=super::integer::RatioError;
|
||||||
|
fn try_into(self)->Result<strafesnet_common::physics::Instruction,Self::Error>{
|
||||||
|
Ok(match self{
|
||||||
|
Instruction::ReplaceMouse{m0,m1}=>strafesnet_common::physics::Instruction::Mouse(strafesnet_common::physics::MouseInstruction::ReplaceMouse{m0:m0.into(),m1:m1.into()}),
|
||||||
|
Instruction::SetNextMouse(m)=>strafesnet_common::physics::Instruction::Mouse(strafesnet_common::physics::MouseInstruction::SetNextMouse(m.into())),
|
||||||
|
Instruction::SetMoveRight(state)=>strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveRight(state.into())),
|
||||||
|
Instruction::SetMoveUp(state)=>strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveUp(state.into())),
|
||||||
|
Instruction::SetMoveBack(state)=>strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveBack(state.into())),
|
||||||
|
Instruction::SetMoveLeft(state)=>strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveLeft(state.into())),
|
||||||
|
Instruction::SetMoveDown(state)=>strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveDown(state.into())),
|
||||||
|
Instruction::SetMoveForward(state)=>strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveForward(state.into())),
|
||||||
|
Instruction::SetJump(state)=>strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetJump(state.into())),
|
||||||
|
Instruction::SetZoom(state)=>strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetZoom(state.into())),
|
||||||
|
Instruction::Reset=>strafesnet_common::physics::Instruction::Mode(strafesnet_common::physics::ModeInstruction::Reset),
|
||||||
|
Instruction::Restart(mode_id)=>strafesnet_common::physics::Instruction::Mode(strafesnet_common::physics::ModeInstruction::Restart(strafesnet_common::gameplay_modes::ModeId::new(mode_id))),
|
||||||
|
Instruction::Spawn(mode_id,stage_id)=>strafesnet_common::physics::Instruction::Mode(strafesnet_common::physics::ModeInstruction::Spawn(
|
||||||
|
strafesnet_common::gameplay_modes::ModeId::new(mode_id),
|
||||||
|
strafesnet_common::gameplay_modes::StageId::new(stage_id),
|
||||||
|
)),
|
||||||
|
Instruction::PracticeFly=>strafesnet_common::physics::Instruction::Misc(strafesnet_common::physics::MiscInstruction::PracticeFly),
|
||||||
|
Instruction::SetSensitivity(sensitivity)=>strafesnet_common::physics::Instruction::Misc(strafesnet_common::physics::MiscInstruction::SetSensitivity(sensitivity.try_into()?)),
|
||||||
|
Instruction::Idle=>strafesnet_common::physics::Instruction::Idle,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl TryFrom<strafesnet_common::physics::Instruction> for Instruction{
|
||||||
|
type Error=InstructionConvert;
|
||||||
|
fn try_from(value:strafesnet_common::physics::Instruction)->Result<Self,Self::Error>{
|
||||||
|
match value{
|
||||||
|
strafesnet_common::physics::Instruction::Mouse(strafesnet_common::physics::MouseInstruction::ReplaceMouse{m0,m1})=>Ok(Instruction::ReplaceMouse{m0:m0.into(),m1:m1.into()}),
|
||||||
|
strafesnet_common::physics::Instruction::Mouse(strafesnet_common::physics::MouseInstruction::SetNextMouse(m))=>Ok(Instruction::SetNextMouse(m.into())),
|
||||||
|
strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveRight(state))=>Ok(Instruction::SetMoveRight(state.into())),
|
||||||
|
strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveUp(state))=>Ok(Instruction::SetMoveUp(state.into())),
|
||||||
|
strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveBack(state))=>Ok(Instruction::SetMoveBack(state.into())),
|
||||||
|
strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveLeft(state))=>Ok(Instruction::SetMoveLeft(state.into())),
|
||||||
|
strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveDown(state))=>Ok(Instruction::SetMoveDown(state.into())),
|
||||||
|
strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetMoveForward(state))=>Ok(Instruction::SetMoveForward(state.into())),
|
||||||
|
strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetJump(state))=>Ok(Instruction::SetJump(state.into())),
|
||||||
|
strafesnet_common::physics::Instruction::SetControl(strafesnet_common::physics::SetControlInstruction::SetZoom(state))=>Ok(Instruction::SetZoom(state.into())),
|
||||||
|
strafesnet_common::physics::Instruction::Mode(strafesnet_common::physics::ModeInstruction::Reset)=>Ok(Instruction::Reset),
|
||||||
|
strafesnet_common::physics::Instruction::Mode(strafesnet_common::physics::ModeInstruction::Restart(mode_id))=>Ok(Instruction::Restart(mode_id.get())),
|
||||||
|
strafesnet_common::physics::Instruction::Mode(strafesnet_common::physics::ModeInstruction::Spawn(mode_id,stage_id))=>Ok(Instruction::Spawn(
|
||||||
|
mode_id.get(),
|
||||||
|
stage_id.get(),
|
||||||
|
)),
|
||||||
|
strafesnet_common::physics::Instruction::Misc(strafesnet_common::physics::MiscInstruction::PracticeFly)=>Ok(Instruction::PracticeFly),
|
||||||
|
strafesnet_common::physics::Instruction::Misc(strafesnet_common::physics::MiscInstruction::SetSensitivity(sensitivity))=>Ok(Instruction::SetSensitivity(sensitivity.into())),
|
||||||
|
strafesnet_common::physics::Instruction::Idle=>Ok(Instruction::Idle),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "strafe-client"
|
name = "strafe-client"
|
||||||
version = "0.10.5"
|
version = "0.11.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
repository = "https://git.itzana.me/StrafesNET/strafe-project"
|
repository = "https://git.itzana.me/StrafesNET/strafe-project"
|
||||||
license = "Custom"
|
license = "Custom"
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ pub enum ReadError{
|
|||||||
StrafesNET(strafesnet_snf::Error),
|
StrafesNET(strafesnet_snf::Error),
|
||||||
#[cfg(feature="snf")]
|
#[cfg(feature="snf")]
|
||||||
StrafesNETMap(strafesnet_snf::map::Error),
|
StrafesNETMap(strafesnet_snf::map::Error),
|
||||||
|
#[cfg(feature="snf")]
|
||||||
|
StrafesNETBot(strafesnet_snf::bot::Error),
|
||||||
Io(std::io::Error),
|
Io(std::io::Error),
|
||||||
UnknownFileFormat,
|
UnknownFileFormat,
|
||||||
}
|
}
|
||||||
@@ -20,28 +22,39 @@ impl std::fmt::Display for ReadError{
|
|||||||
}
|
}
|
||||||
impl std::error::Error for ReadError{}
|
impl std::error::Error for ReadError{}
|
||||||
|
|
||||||
pub enum DataStructure{
|
pub enum ReadFormat{
|
||||||
#[cfg(feature="roblox")]
|
#[cfg(feature="roblox")]
|
||||||
Roblox(strafesnet_rbx_loader::Model),
|
Roblox(strafesnet_rbx_loader::Model),
|
||||||
#[cfg(feature="source")]
|
#[cfg(feature="source")]
|
||||||
Source(strafesnet_bsp_loader::Bsp),
|
Source(strafesnet_bsp_loader::Bsp),
|
||||||
#[cfg(feature="snf")]
|
#[cfg(feature="snf")]
|
||||||
StrafesNET(strafesnet_common::map::CompleteMap),
|
SNFM(strafesnet_common::map::CompleteMap),
|
||||||
|
#[cfg(feature="snf")]
|
||||||
|
SNFB(strafesnet_snf::bot::Segment),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read<R:Read+std::io::Seek>(input:R)->Result<DataStructure,ReadError>{
|
pub fn read<R:Read+std::io::Seek>(input:R)->Result<ReadFormat,ReadError>{
|
||||||
let mut buf=std::io::BufReader::new(input);
|
let mut buf=std::io::BufReader::new(input);
|
||||||
let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?;
|
let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?[0..4].to_owned();
|
||||||
match &peek[0..4]{
|
// reading the entire file is way faster than round tripping the disk constantly
|
||||||
|
let mut entire_file=Vec::new();
|
||||||
|
buf.read_to_end(&mut entire_file).map_err(ReadError::Io)?;
|
||||||
|
let cursor=std::io::Cursor::new(entire_file);
|
||||||
|
match peek.as_slice(){
|
||||||
#[cfg(feature="roblox")]
|
#[cfg(feature="roblox")]
|
||||||
b"<rob"=>Ok(DataStructure::Roblox(strafesnet_rbx_loader::read(buf).map_err(ReadError::Roblox)?)),
|
b"<rob"=>Ok(ReadFormat::Roblox(strafesnet_rbx_loader::read(cursor).map_err(ReadError::Roblox)?)),
|
||||||
#[cfg(feature="source")]
|
#[cfg(feature="source")]
|
||||||
b"VBSP"=>Ok(DataStructure::Source(strafesnet_bsp_loader::read(buf).map_err(ReadError::Source)?)),
|
b"VBSP"=>Ok(ReadFormat::Source(strafesnet_bsp_loader::read(cursor).map_err(ReadError::Source)?)),
|
||||||
#[cfg(feature="snf")]
|
#[cfg(feature="snf")]
|
||||||
b"SNFM"=>Ok(DataStructure::StrafesNET(
|
b"SNFM"=>Ok(ReadFormat::SNFM(
|
||||||
strafesnet_snf::read_map(buf).map_err(ReadError::StrafesNET)?
|
strafesnet_snf::read_map(cursor).map_err(ReadError::StrafesNET)?
|
||||||
.into_complete_map().map_err(ReadError::StrafesNETMap)?
|
.into_complete_map().map_err(ReadError::StrafesNETMap)?
|
||||||
)),
|
)),
|
||||||
|
#[cfg(feature="snf")]
|
||||||
|
b"SNFB"=>Ok(ReadFormat::SNFB(
|
||||||
|
strafesnet_snf::read_bot(cursor).map_err(ReadError::StrafesNET)?
|
||||||
|
.read_all().map_err(ReadError::StrafesNETBot)?
|
||||||
|
)),
|
||||||
_=>Err(ReadError::UnknownFileFormat),
|
_=>Err(ReadError::UnknownFileFormat),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,14 +72,23 @@ impl std::fmt::Display for LoadError{
|
|||||||
}
|
}
|
||||||
impl std::error::Error 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 enum LoadFormat{
|
||||||
|
#[cfg(feature="snf")]
|
||||||
|
Map(strafesnet_common::map::CompleteMap),
|
||||||
|
#[cfg(feature="snf")]
|
||||||
|
Bot(strafesnet_snf::bot::Segment),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load<P:AsRef<std::path::Path>>(path:P)->Result<LoadFormat,LoadError>{
|
||||||
//blocking because it's simpler...
|
//blocking because it's simpler...
|
||||||
let file=std::fs::File::open(path).map_err(LoadError::File)?;
|
let file=std::fs::File::open(path).map_err(LoadError::File)?;
|
||||||
match read(file).map_err(LoadError::ReadError)?{
|
match read(file).map_err(LoadError::ReadError)?{
|
||||||
#[cfg(feature="snf")]
|
#[cfg(feature="snf")]
|
||||||
DataStructure::StrafesNET(map)=>Ok(map),
|
ReadFormat::SNFB(bot)=>Ok(LoadFormat::Bot(bot)),
|
||||||
|
#[cfg(feature="snf")]
|
||||||
|
ReadFormat::SNFM(map)=>Ok(LoadFormat::Map(map)),
|
||||||
#[cfg(feature="roblox")]
|
#[cfg(feature="roblox")]
|
||||||
DataStructure::Roblox(model)=>{
|
ReadFormat::Roblox(model)=>{
|
||||||
let mut place=model.into_place();
|
let mut place=model.into_place();
|
||||||
place.run_scripts();
|
place.run_scripts();
|
||||||
|
|
||||||
@@ -99,10 +121,10 @@ pub fn load<P:AsRef<std::path::Path>>(path:P)->Result<strafesnet_common::map::Co
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(map)
|
Ok(LoadFormat::Map(map))
|
||||||
},
|
},
|
||||||
#[cfg(feature="source")]
|
#[cfg(feature="source")]
|
||||||
DataStructure::Source(bsp)=>{
|
ReadFormat::Source(bsp)=>{
|
||||||
let mut loader=strafesnet_deferred_loader::source_legacy();
|
let mut loader=strafesnet_deferred_loader::source_legacy();
|
||||||
|
|
||||||
let (texture_loader,mesh_loader)=loader.get_inner_mut();
|
let (texture_loader,mesh_loader)=loader.get_inner_mut();
|
||||||
@@ -138,7 +160,7 @@ pub fn load<P:AsRef<std::path::Path>>(path:P)->Result<strafesnet_common::map::Co
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(map)
|
Ok(LoadFormat::Map(map))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -718,7 +718,7 @@ impl MinkowskiMesh<'_>{
|
|||||||
//
|
//
|
||||||
// Most of the calculation time is just calculating the starting point
|
// Most of the calculation time is just calculating the starting point
|
||||||
// for the "actual" crawling algorithm below (predict_collision_{in|out}).
|
// for the "actual" crawling algorithm below (predict_collision_{in|out}).
|
||||||
fn closest_fev_not_inside(&self,mut infinity_body:Body)->Option<FEV<MinkowskiMesh>>{
|
fn closest_fev_not_inside(&self,mut infinity_body:Body,start_time:Time,)->Option<FEV<MinkowskiMesh>>{
|
||||||
infinity_body.infinity_dir().map_or(None,|dir|{
|
infinity_body.infinity_dir().map_or(None,|dir|{
|
||||||
let infinity_fev=self.infinity_fev(-dir,infinity_body.position);
|
let infinity_fev=self.infinity_fev(-dir,infinity_body.position);
|
||||||
//a line is simpler to solve than a parabola
|
//a line is simpler to solve than a parabola
|
||||||
@@ -726,40 +726,39 @@ impl MinkowskiMesh<'_>{
|
|||||||
infinity_body.acceleration=vec3::ZERO;
|
infinity_body.acceleration=vec3::ZERO;
|
||||||
//crawl in from negative infinity along a tangent line to get the closest fev
|
//crawl in from negative infinity along a tangent line to get the closest fev
|
||||||
// TODO: change crawl_fev args to delta time? Optional values?
|
// TODO: change crawl_fev args to delta time? Optional values?
|
||||||
match infinity_fev.crawl(self,&infinity_body,Time::MIN/4,infinity_body.time){
|
match infinity_fev.crawl(self,&infinity_body,Time::MIN/4,start_time){
|
||||||
crate::face_crawler::CrawlResult::Miss(fev)=>Some(fev),
|
crate::face_crawler::CrawlResult::Miss(fev)=>Some(fev),
|
||||||
crate::face_crawler::CrawlResult::Hit(_,_)=>None,
|
crate::face_crawler::CrawlResult::Hit(_,_)=>None,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn predict_collision_in(&self,relative_body:&Body,time_limit:Time)->Option<(MinkowskiFace,GigaTime)>{
|
pub fn predict_collision_in(&self,relative_body:&Body,start_time:Time,time_limit:Time)->Option<(MinkowskiFace,GigaTime)>{
|
||||||
self.closest_fev_not_inside(relative_body.clone()).map_or(None,|fev|{
|
self.closest_fev_not_inside(relative_body.clone(),start_time).map_or(None,|fev|{
|
||||||
//continue forwards along the body parabola
|
//continue forwards along the body parabola
|
||||||
match fev.crawl(self,relative_body,relative_body.time,time_limit){
|
match fev.crawl(self,relative_body,start_time,time_limit){
|
||||||
crate::face_crawler::CrawlResult::Miss(_)=>None,
|
crate::face_crawler::CrawlResult::Miss(_)=>None,
|
||||||
crate::face_crawler::CrawlResult::Hit(face,time)=>Some((face,time)),
|
crate::face_crawler::CrawlResult::Hit(face,time)=>Some((face,time)),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn predict_collision_out(&self,relative_body:&Body,time_limit:Time)->Option<(MinkowskiFace,GigaTime)>{
|
pub fn predict_collision_out(&self,relative_body:&Body,start_time:Time,time_limit:Time)->Option<(MinkowskiFace,GigaTime)>{
|
||||||
//create an extrapolated body at time_limit
|
//create an extrapolated body at time_limit
|
||||||
let infinity_body=Body::new(
|
let infinity_body=-relative_body.clone();
|
||||||
relative_body.extrapolated_position(time_limit),
|
self.closest_fev_not_inside(infinity_body,-time_limit).map_or(None,|fev|{
|
||||||
-relative_body.extrapolated_velocity(time_limit),
|
|
||||||
relative_body.acceleration,
|
|
||||||
-time_limit,
|
|
||||||
);
|
|
||||||
self.closest_fev_not_inside(infinity_body).map_or(None,|fev|{
|
|
||||||
//continue backwards along the body parabola
|
//continue backwards along the body parabola
|
||||||
match fev.crawl(self,&-relative_body.clone(),-time_limit,-relative_body.time){
|
match fev.crawl(self,&infinity_body,-time_limit,-start_time){
|
||||||
crate::face_crawler::CrawlResult::Miss(_)=>None,
|
crate::face_crawler::CrawlResult::Miss(_)=>None,
|
||||||
crate::face_crawler::CrawlResult::Hit(face,time)=>Some((face,-time)),//no need to test -time<time_limit because of the first step
|
crate::face_crawler::CrawlResult::Hit(face,time)=>Some((face,-time)),//no need to test -time<time_limit because of the first step
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn predict_collision_face_out(&self,relative_body:&Body,time_limit:Time,contact_face_id:MinkowskiFace)->Option<(MinkowskiEdge,GigaTime)>{
|
pub fn predict_collision_face_out(&self,relative_body:&Body,start_time:Time,time_limit:Time,contact_face_id:MinkowskiFace)->Option<(MinkowskiEdge,GigaTime)>{
|
||||||
//no algorithm needed, there is only one state and two cases (Edge,None)
|
//no algorithm needed, there is only one state and two cases (Edge,None)
|
||||||
//determine when it passes an edge ("sliding off" case)
|
//determine when it passes an edge ("sliding off" case)
|
||||||
|
let start_time={
|
||||||
|
let r=(start_time-relative_body.time).to_ratio();
|
||||||
|
Ratio::new(r.num,r.den)
|
||||||
|
};
|
||||||
let mut best_time={
|
let mut best_time={
|
||||||
let r=(time_limit-relative_body.time).to_ratio();
|
let r=(time_limit-relative_body.time).to_ratio();
|
||||||
Ratio::new(r.num.fix_4(),r.den.fix_4())
|
Ratio::new(r.num.fix_4(),r.den.fix_4())
|
||||||
@@ -775,7 +774,7 @@ impl MinkowskiMesh<'_>{
|
|||||||
//WARNING! d outside of *2
|
//WARNING! d outside of *2
|
||||||
//WARNING: truncated precision
|
//WARNING: truncated precision
|
||||||
for dt in Fixed::<4,128>::zeroes2(((n.dot(relative_body.position))*2-d).fix_4(),n.dot(relative_body.velocity).fix_4()*2,n.dot(relative_body.acceleration).fix_4()){
|
for dt in Fixed::<4,128>::zeroes2(((n.dot(relative_body.position))*2-d).fix_4(),n.dot(relative_body.velocity).fix_4()*2,n.dot(relative_body.acceleration).fix_4()){
|
||||||
if Ratio::new(Planar64::ZERO,Planar64::EPSILON).le_ratio(dt)&&dt.lt_ratio(best_time)&&n.dot(relative_body.extrapolated_velocity_ratio_dt(dt)).is_negative(){
|
if start_time.le_ratio(dt)&&dt.lt_ratio(best_time)&&n.dot(relative_body.extrapolated_velocity_ratio_dt(dt)).is_negative(){
|
||||||
best_time=dt;
|
best_time=dt;
|
||||||
best_edge=Some(directed_edge_id);
|
best_edge=Some(directed_edge_id);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -14,6 +14,45 @@ use strafesnet_common::integer::{self,vec3,mat3,Planar64,Planar64Vec3,Planar64Ma
|
|||||||
pub use strafesnet_common::physics::{Time,TimeInner};
|
pub use strafesnet_common::physics::{Time,TimeInner};
|
||||||
use gameplay::ModeState;
|
use gameplay::ModeState;
|
||||||
|
|
||||||
|
// Physics bug fixes can easily desync all bots.
|
||||||
|
//
|
||||||
|
// When replaying a bot, use the exact physics version which it was recorded with.
|
||||||
|
//
|
||||||
|
// When validating a new bot, use the latest compatible physics version
|
||||||
|
// from the compatiblity matrix, since it may include bugfixes
|
||||||
|
// for things like clipping through walls with surgical precision
|
||||||
|
// i.e. without breaking bots which don't exploit the bug.
|
||||||
|
//
|
||||||
|
// Compatible physics versions should be determined empirically via leaderboard resimulation.
|
||||||
|
// Compatible physics versions should result in an identical leaderboard state,
|
||||||
|
// or the only bots which fail are ones exploiting a surgically patched bug.
|
||||||
|
#[derive(Clone,Copy,Hash,Debug,id::Id,Eq,PartialEq,Ord,PartialOrd)]
|
||||||
|
pub struct PhysicsVersion(u32);
|
||||||
|
pub const VERSION:PhysicsVersion=PhysicsVersion(0);
|
||||||
|
const LATEST_COMPATIBLE_VERSION:[u32;1+VERSION.0 as usize]=const{
|
||||||
|
let compat=[0];
|
||||||
|
|
||||||
|
let mut input_version=0;
|
||||||
|
while input_version<compat.len(){
|
||||||
|
// compatible version must be greater that or equal to the input version
|
||||||
|
assert!(input_version as u32<=compat[input_version]);
|
||||||
|
// compatible version must be a version that exists
|
||||||
|
assert!(compat[input_version]<=VERSION.0);
|
||||||
|
input_version+=1;
|
||||||
|
}
|
||||||
|
compat
|
||||||
|
};
|
||||||
|
pub enum PhysicsVersionError{
|
||||||
|
UnknownPhysicsVersion,
|
||||||
|
}
|
||||||
|
pub const fn get_latest_compatible_version(PhysicsVersion(version):PhysicsVersion)->Result<PhysicsVersion,PhysicsVersionError>{
|
||||||
|
if (version as usize)<LATEST_COMPATIBLE_VERSION.len(){
|
||||||
|
Ok(PhysicsVersion(LATEST_COMPATIBLE_VERSION[version as usize]))
|
||||||
|
}else{
|
||||||
|
Err(PhysicsVersionError::UnknownPhysicsVersion)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub type Body=crate::body::Body<TimeInner>;
|
pub type Body=crate::body::Body<TimeInner>;
|
||||||
type MouseState=strafesnet_common::mouse::MouseState<TimeInner>;
|
type MouseState=strafesnet_common::mouse::MouseState<TimeInner>;
|
||||||
|
|
||||||
@@ -776,13 +815,14 @@ impl TouchingState{
|
|||||||
}).collect();
|
}).collect();
|
||||||
*acceleration=crate::push_solve::push_solve(&contacts,*acceleration);
|
*acceleration=crate::push_solve::push_solve(&contacts,*acceleration);
|
||||||
}
|
}
|
||||||
fn predict_collision_end(&self,collector:&mut instruction::InstructionCollector<InternalInstruction,TimeInner>,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,body:&Body,time:Time){
|
fn predict_collision_end(&self,collector:&mut instruction::InstructionCollector<InternalInstruction,TimeInner>,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,body:&Body,start_time:Time){
|
||||||
let relative_body=crate::body::VirtualBody::relative(&Body::ZERO,body).body(time);
|
// let relative_body=crate::body::VirtualBody::relative(&Body::ZERO,body).body(time);
|
||||||
|
let relative_body=body;
|
||||||
for contact in &self.contacts{
|
for contact in &self.contacts{
|
||||||
//detect face slide off
|
//detect face slide off
|
||||||
let model_mesh=models.contact_mesh(contact);
|
let model_mesh=models.contact_mesh(contact);
|
||||||
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(model_mesh,hitbox_mesh.transformed_mesh());
|
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(model_mesh,hitbox_mesh.transformed_mesh());
|
||||||
collector.collect(minkowski.predict_collision_face_out(&relative_body,collector.time(),contact.face_id).map(|(_face,time)|{
|
collector.collect(minkowski.predict_collision_face_out(&relative_body,start_time,collector.time(),contact.face_id).map(|(_face,time)|{
|
||||||
TimedInstruction{
|
TimedInstruction{
|
||||||
time:relative_body.time+time.into(),
|
time:relative_body.time+time.into(),
|
||||||
instruction:InternalInstruction::CollisionEnd(
|
instruction:InternalInstruction::CollisionEnd(
|
||||||
@@ -796,7 +836,7 @@ impl TouchingState{
|
|||||||
//detect model collision in reverse
|
//detect model collision in reverse
|
||||||
let model_mesh=models.intersect_mesh(intersect);
|
let model_mesh=models.intersect_mesh(intersect);
|
||||||
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(model_mesh,hitbox_mesh.transformed_mesh());
|
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(model_mesh,hitbox_mesh.transformed_mesh());
|
||||||
collector.collect(minkowski.predict_collision_out(&relative_body,collector.time()).map(|(_face,time)|{
|
collector.collect(minkowski.predict_collision_out(&relative_body,start_time,collector.time()).map(|(_face,time)|{
|
||||||
TimedInstruction{
|
TimedInstruction{
|
||||||
time:relative_body.time+time.into(),
|
time:relative_body.time+time.into(),
|
||||||
instruction:InternalInstruction::CollisionEnd(
|
instruction:InternalInstruction::CollisionEnd(
|
||||||
@@ -860,6 +900,9 @@ impl PhysicsState{
|
|||||||
pub const fn mode(&self)->gameplay_modes::ModeId{
|
pub const fn mode(&self)->gameplay_modes::ModeId{
|
||||||
self.mode_state.get_mode_id()
|
self.mode_state.get_mode_id()
|
||||||
}
|
}
|
||||||
|
pub fn get_finish_time(&self)->Option<run::Time>{
|
||||||
|
self.run.get_finish_time()
|
||||||
|
}
|
||||||
pub fn clear(&mut self){
|
pub fn clear(&mut self){
|
||||||
self.touching.clear();
|
self.touching.clear();
|
||||||
}
|
}
|
||||||
@@ -1111,7 +1154,7 @@ impl PhysicsData{
|
|||||||
//this is the one who asks
|
//this is the one who asks
|
||||||
fn next_instruction_internal(state:&PhysicsState,data:&PhysicsData,time_limit:Time)->Option<TimedInstruction<InternalInstruction,TimeInner>>{
|
fn next_instruction_internal(state:&PhysicsState,data:&PhysicsData,time_limit:Time)->Option<TimedInstruction<InternalInstruction,TimeInner>>{
|
||||||
//JUST POLLING!!! NO MUTATION
|
//JUST POLLING!!! NO MUTATION
|
||||||
let mut collector = instruction::InstructionCollector::new(time_limit);
|
let mut collector=instruction::InstructionCollector::new(time_limit);
|
||||||
|
|
||||||
collector.collect(state.next_move_instruction());
|
collector.collect(state.next_move_instruction());
|
||||||
|
|
||||||
@@ -1128,7 +1171,7 @@ impl PhysicsData{
|
|||||||
//no checks are needed because of the time limits.
|
//no checks are needed because of the time limits.
|
||||||
let model_mesh=data.models.mesh(convex_mesh_id);
|
let model_mesh=data.models.mesh(convex_mesh_id);
|
||||||
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(model_mesh,data.hitbox_mesh.transformed_mesh());
|
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,state.time,collector.time())
|
||||||
//temp (?) code to avoid collision loops
|
//temp (?) code to avoid collision loops
|
||||||
.map_or(None,|(face,dt)|{
|
.map_or(None,|(face,dt)|{
|
||||||
// this must be rounded to avoid the infinite loop when hitting the start zone
|
// this must be rounded to avoid the infinite loop when hitting the start zone
|
||||||
@@ -1145,7 +1188,7 @@ impl PhysicsData{
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
collector.instruction()
|
collector.take()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1859,8 +1902,9 @@ fn atomic_input_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedI
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test{
|
mod test{
|
||||||
use strafesnet_common::integer::{vec3::{self,int as int3},mat3};
|
use crate::file;
|
||||||
use crate::body::VirtualBody;
|
use crate::body::VirtualBody;
|
||||||
|
use strafesnet_common::integer::{vec3::{self,int as int3},mat3};
|
||||||
use super::*;
|
use super::*;
|
||||||
fn test_collision_axis_aligned(relative_body:Body,expected_collision_time:Option<Time>){
|
fn test_collision_axis_aligned(relative_body:Body,expected_collision_time:Option<Time>){
|
||||||
let h0=HitboxMesh::new(PhysicsMesh::unit_cube(),integer::Planar64Affine3::new(mat3::from_diagonal(int3(5,1,5)>>1),vec3::ZERO));
|
let h0=HitboxMesh::new(PhysicsMesh::unit_cube(),integer::Planar64Affine3::new(mat3::from_diagonal(int3(5,1,5)>>1),vec3::ZERO));
|
||||||
@@ -1868,7 +1912,7 @@ mod test{
|
|||||||
let hitbox_mesh=h1.transformed_mesh();
|
let hitbox_mesh=h1.transformed_mesh();
|
||||||
let platform_mesh=h0.transformed_mesh();
|
let platform_mesh=h0.transformed_mesh();
|
||||||
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(platform_mesh,hitbox_mesh);
|
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(platform_mesh,hitbox_mesh);
|
||||||
let collision=minkowski.predict_collision_in(&relative_body,Time::from_secs(10));
|
let collision=minkowski.predict_collision_in(&relative_body,Time::ZERO,Time::from_secs(10));
|
||||||
assert_eq!(collision.map(|tup|relative_body.time+tup.1.into()),expected_collision_time,"Incorrect time of collision");
|
assert_eq!(collision.map(|tup|relative_body.time+tup.1.into()),expected_collision_time,"Incorrect time of collision");
|
||||||
}
|
}
|
||||||
fn test_collision_rotated(relative_body:Body,expected_collision_time:Option<Time>){
|
fn test_collision_rotated(relative_body:Body,expected_collision_time:Option<Time>){
|
||||||
@@ -1886,7 +1930,7 @@ mod test{
|
|||||||
let hitbox_mesh=h1.transformed_mesh();
|
let hitbox_mesh=h1.transformed_mesh();
|
||||||
let platform_mesh=h0.transformed_mesh();
|
let platform_mesh=h0.transformed_mesh();
|
||||||
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(platform_mesh,hitbox_mesh);
|
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(platform_mesh,hitbox_mesh);
|
||||||
let collision=minkowski.predict_collision_in(&relative_body,Time::from_secs(10));
|
let collision=minkowski.predict_collision_in(&relative_body,Time::ZERO,Time::from_secs(10));
|
||||||
assert_eq!(collision.map(|tup|relative_body.time+tup.1.into()),expected_collision_time,"Incorrect time of collision");
|
assert_eq!(collision.map(|tup|relative_body.time+tup.1.into()),expected_collision_time,"Incorrect time of collision");
|
||||||
}
|
}
|
||||||
fn test_collision(relative_body:Body,expected_collision_time:Option<Time>){
|
fn test_collision(relative_body:Body,expected_collision_time:Option<Time>){
|
||||||
@@ -2073,4 +2117,197 @@ mod test{
|
|||||||
Time::ZERO
|
Time::ZERO
|
||||||
),None);
|
),None);
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn run_replay(){
|
||||||
|
println!("loading map file..");
|
||||||
|
let map=file::load("../tools/bhop_maps/5692113331.snfm");
|
||||||
|
println!("loading bot file..");
|
||||||
|
let bot=file::load("../tools/replays/534s+997497968ns.snfb");
|
||||||
|
if let (Ok(file::LoadFormat::Map(map)),Ok(file::LoadFormat::Bot(bot)))=(map,bot){
|
||||||
|
// create recording
|
||||||
|
let mut physics_data=PhysicsData::default();
|
||||||
|
println!("generating models..");
|
||||||
|
physics_data.generate_models(&map);
|
||||||
|
println!("simulating...");
|
||||||
|
let mut physics=PhysicsState::default();
|
||||||
|
for ins in bot.instructions{
|
||||||
|
PhysicsContext::run_input_instruction(&mut physics,&physics_data,ins);
|
||||||
|
}
|
||||||
|
match physics.get_finish_time(){
|
||||||
|
Some(time)=>println!("finish time:{}",time),
|
||||||
|
None=>println!("simulation did not end in finished state"),
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
panic!("missing files");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
enum DeterminismResult{
|
||||||
|
Deterministic,
|
||||||
|
NonDeterministic,
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum DetErr{
|
||||||
|
Load(file::LoadError),
|
||||||
|
IO(std::io::Error),
|
||||||
|
}
|
||||||
|
impl From<file::LoadError> for DetErr{
|
||||||
|
fn from(value:file::LoadError)->Self{
|
||||||
|
Self::Load(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<std::io::Error> for DetErr{
|
||||||
|
fn from(value:std::io::Error)->Self{
|
||||||
|
Self::IO(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn run_bot_on_map(bot:strafesnet_snf::bot::Segment,physics_data:&PhysicsData)->DeterminismResult{
|
||||||
|
// create default physics state
|
||||||
|
let mut physics_deterministic=PhysicsState::default();
|
||||||
|
// create a second physics state
|
||||||
|
let mut physics_filtered=PhysicsState::default();
|
||||||
|
|
||||||
|
// invent a new bot id and insert the replay
|
||||||
|
println!("simulating...");
|
||||||
|
|
||||||
|
for (i,ins) in bot.instructions.into_iter().enumerate(){
|
||||||
|
let state_deterministic=physics_deterministic.clone();
|
||||||
|
let state_filtered=physics_filtered.clone();
|
||||||
|
PhysicsContext::run_input_instruction(&mut physics_deterministic,&physics_data,ins.clone());
|
||||||
|
match ins{
|
||||||
|
strafesnet_common::instruction::TimedInstruction{instruction:strafesnet_common::physics::Instruction::Idle,..}=>(),
|
||||||
|
other=>{
|
||||||
|
// run
|
||||||
|
PhysicsContext::run_input_instruction(&mut physics_filtered,&physics_data,other.clone());
|
||||||
|
// check if position matches
|
||||||
|
let b0=physics_deterministic.camera_body();
|
||||||
|
let b1=physics_filtered.camera_body();
|
||||||
|
if b0.position!=b1.position{
|
||||||
|
println!("instruction #{i}={:?}",other);
|
||||||
|
println!("desync at instruction #{}",i);
|
||||||
|
println!("deterministic state0:\n{state_deterministic:?}");
|
||||||
|
println!("filtered state0:\n{state_filtered:?}");
|
||||||
|
println!("deterministic state1:\n{:?}",physics_deterministic);
|
||||||
|
println!("filtered state1:\n{:?}",physics_filtered);
|
||||||
|
return DeterminismResult::NonDeterministic;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
match physics_deterministic.get_finish_time(){
|
||||||
|
Some(time)=>println!("[with idle] finish time:{}",time),
|
||||||
|
None=>println!("[with idle] simulation did not end in finished state"),
|
||||||
|
}
|
||||||
|
match physics_filtered.get_finish_time(){
|
||||||
|
Some(time)=>println!("[filtered] finish time:{}",time),
|
||||||
|
None=>println!("[filtered] simulation did not end in finished state"),
|
||||||
|
}
|
||||||
|
DeterminismResult::Deterministic
|
||||||
|
}
|
||||||
|
type LeSend=Result<Option<DeterminismResult>,file::LoadError>;
|
||||||
|
fn do_thread<'a>(s:&'a std::thread::Scope<'a,'_>,file_path:std::path::PathBuf,send:std::sync::mpsc::Sender<LeSend>,physics_data:&'a PhysicsData){
|
||||||
|
s.spawn(move ||{
|
||||||
|
let result=match file::load(file_path.as_path()){
|
||||||
|
Ok(file::LoadFormat::Bot(bot))=>{
|
||||||
|
println!("Running {:?}",file_path.file_stem());
|
||||||
|
Ok(Some(run_bot_on_map(bot,physics_data)))
|
||||||
|
},
|
||||||
|
Ok(_)=>{
|
||||||
|
println!("Provided bot file is not a bot file!");
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
Err(e)=>{
|
||||||
|
println!("Load error");
|
||||||
|
Err(e)
|
||||||
|
},
|
||||||
|
};
|
||||||
|
// send when thread is complete
|
||||||
|
send.send(result).unwrap();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
fn get_file_path(dir_entry:std::fs::DirEntry)->Result<Option<std::path::PathBuf>,std::io::Error>{
|
||||||
|
Ok(dir_entry.file_type()?.is_file().then_some(
|
||||||
|
dir_entry.path()
|
||||||
|
))
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_determinism()->Result<(),DetErr>{
|
||||||
|
let thread_limit=std::thread::available_parallelism().unwrap().get();
|
||||||
|
println!("loading map file..");
|
||||||
|
let file::LoadFormat::Map(map)=file::load("../tools/bhop_maps/5692113331.snfm")? else{
|
||||||
|
panic!("Provided map file is not a map file!");
|
||||||
|
};
|
||||||
|
let mut physics_data=PhysicsData::default();
|
||||||
|
println!("generating models..");
|
||||||
|
physics_data.generate_models(&map);
|
||||||
|
let (send,recv)=std::sync::mpsc::channel();
|
||||||
|
|
||||||
|
let mut read_dir=std::fs::read_dir("../tools/replays")?;
|
||||||
|
|
||||||
|
// promise that &physics_data will outlive the spawned threads
|
||||||
|
let thread_results=std::thread::scope(|s|{
|
||||||
|
let mut thread_results=Vec::new();
|
||||||
|
|
||||||
|
// spawn threads
|
||||||
|
println!("spawning up to {thread_limit} threads...");
|
||||||
|
let mut active_thread_count=0;
|
||||||
|
for _ in 0..thread_limit{
|
||||||
|
if let Some(dir_entry_result)=read_dir.next(){
|
||||||
|
if let Some(file_path)=get_file_path(dir_entry_result?)?{
|
||||||
|
active_thread_count+=1;
|
||||||
|
do_thread(s,file_path,send.clone(),&physics_data);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// spawn another thread every time a message is received from the channel
|
||||||
|
println!("riding parallelism wave...");
|
||||||
|
while let Some(dir_entry_result)=read_dir.next(){
|
||||||
|
if let Some(file_path)=get_file_path(dir_entry_result?)?{
|
||||||
|
// wait for a thread to complete
|
||||||
|
thread_results.push(recv.recv().unwrap());
|
||||||
|
do_thread(s,file_path,send.clone(),&physics_data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait for remaining threads to complete
|
||||||
|
println!("waiting for all threads to complete...");
|
||||||
|
for _ in 0..active_thread_count{
|
||||||
|
thread_results.push(recv.recv().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("done.");
|
||||||
|
Ok::<_,DetErr>(thread_results)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// tally results
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Totals{
|
||||||
|
deterministic:u32,
|
||||||
|
nondeterministic:u32,
|
||||||
|
invalid:u32,
|
||||||
|
failed:u32,
|
||||||
|
}
|
||||||
|
let Totals{deterministic,nondeterministic,invalid,failed}=thread_results.into_iter().fold(Totals::default(),|mut totals,result|{
|
||||||
|
match result{
|
||||||
|
Ok(Some(DeterminismResult::Deterministic))=>totals.deterministic+=1,
|
||||||
|
Ok(Some(DeterminismResult::NonDeterministic))=>totals.nondeterministic+=1,
|
||||||
|
Ok(None)=>totals.invalid+=1,
|
||||||
|
Err(_)=>totals.failed+=1,
|
||||||
|
}
|
||||||
|
totals
|
||||||
|
});
|
||||||
|
|
||||||
|
println!("deterministic={deterministic}");
|
||||||
|
println!("nondeterministic={nondeterministic}");
|
||||||
|
println!("invalid={invalid}");
|
||||||
|
println!("failed={failed}");
|
||||||
|
|
||||||
|
assert!(nondeterministic==0);
|
||||||
|
assert!(invalid==0);
|
||||||
|
assert!(failed==0);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ pub enum Instruction{
|
|||||||
Render,
|
Render,
|
||||||
Resize(winit::dpi::PhysicalSize<u32>),
|
Resize(winit::dpi::PhysicalSize<u32>),
|
||||||
ChangeMap(strafesnet_common::map::CompleteMap),
|
ChangeMap(strafesnet_common::map::CompleteMap),
|
||||||
|
LoadReplay(strafesnet_snf::bot::Segment),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new<'a>(
|
pub fn new<'a>(
|
||||||
@@ -69,6 +70,9 @@ pub fn new<'a>(
|
|||||||
run_session_instruction!(ins.time,SessionInstruction::Input(SessionInputInstruction::Mode(crate::session::ImplicitModeInstruction::ResetAndSpawn(strafesnet_common::gameplay_modes::ModeId::MAIN,strafesnet_common::gameplay_modes::StageId::FIRST))));
|
run_session_instruction!(ins.time,SessionInstruction::Input(SessionInputInstruction::Mode(crate::session::ImplicitModeInstruction::ResetAndSpawn(strafesnet_common::gameplay_modes::ModeId::MAIN,strafesnet_common::gameplay_modes::StageId::FIRST))));
|
||||||
run_graphics_worker_instruction!(GraphicsInstruction::ChangeMap(complete_map));
|
run_graphics_worker_instruction!(GraphicsInstruction::ChangeMap(complete_map));
|
||||||
},
|
},
|
||||||
|
Instruction::LoadReplay(bot)=>{
|
||||||
|
run_session_instruction!(ins.time,SessionInstruction::LoadReplay(bot));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ pub enum Instruction<'a>{
|
|||||||
Control(SessionControlInstruction),
|
Control(SessionControlInstruction),
|
||||||
Playback(SessionPlaybackInstruction),
|
Playback(SessionPlaybackInstruction),
|
||||||
ChangeMap(&'a strafesnet_common::map::CompleteMap),
|
ChangeMap(&'a strafesnet_common::map::CompleteMap),
|
||||||
|
LoadReplay(strafesnet_snf::bot::Segment),
|
||||||
Idle,
|
Idle,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +45,8 @@ pub enum SessionControlInstruction{
|
|||||||
// copy the current session simulation recording into a replay and view it
|
// copy the current session simulation recording into a replay and view it
|
||||||
CopyRecordingIntoReplayAndSpectate,
|
CopyRecordingIntoReplayAndSpectate,
|
||||||
StopSpectate,
|
StopSpectate,
|
||||||
|
SaveReplay,
|
||||||
|
LoadIntoReplayState,
|
||||||
}
|
}
|
||||||
pub enum SessionPlaybackInstruction{
|
pub enum SessionPlaybackInstruction{
|
||||||
SkipForward,
|
SkipForward,
|
||||||
@@ -87,12 +90,17 @@ pub struct Recording{
|
|||||||
instructions:Vec<TimedInstruction<PhysicsInputInstruction,PhysicsTimeInner>>,
|
instructions:Vec<TimedInstruction<PhysicsInputInstruction,PhysicsTimeInner>>,
|
||||||
}
|
}
|
||||||
impl Recording{
|
impl Recording{
|
||||||
|
pub fn new(
|
||||||
|
instructions:Vec<TimedInstruction<PhysicsInputInstruction,PhysicsTimeInner>>,
|
||||||
|
)->Self{
|
||||||
|
Self{instructions}
|
||||||
|
}
|
||||||
fn clear(&mut self){
|
fn clear(&mut self){
|
||||||
self.instructions.clear();
|
self.instructions.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub struct Replay{
|
pub struct Replay{
|
||||||
last_instruction_id:usize,
|
next_instruction_id:usize,
|
||||||
recording:Recording,
|
recording:Recording,
|
||||||
simulation:Simulation,
|
simulation:Simulation,
|
||||||
}
|
}
|
||||||
@@ -102,7 +110,7 @@ impl Replay{
|
|||||||
simulation:Simulation,
|
simulation:Simulation,
|
||||||
)->Self{
|
)->Self{
|
||||||
Self{
|
Self{
|
||||||
last_instruction_id:0,
|
next_instruction_id:0,
|
||||||
recording,
|
recording,
|
||||||
simulation,
|
simulation,
|
||||||
}
|
}
|
||||||
@@ -110,16 +118,16 @@ impl Replay{
|
|||||||
pub fn advance(&mut self,physics_data:&PhysicsData,time_limit:SessionTime){
|
pub fn advance(&mut self,physics_data:&PhysicsData,time_limit:SessionTime){
|
||||||
let mut time=self.simulation.timer.time(time_limit);
|
let mut time=self.simulation.timer.time(time_limit);
|
||||||
loop{
|
loop{
|
||||||
if let Some(ins)=self.recording.instructions.get(self.last_instruction_id){
|
if let Some(ins)=self.recording.instructions.get(self.next_instruction_id){
|
||||||
if ins.time<time{
|
if ins.time<time{
|
||||||
PhysicsContext::run_input_instruction(&mut self.simulation.physics,physics_data,ins.clone());
|
PhysicsContext::run_input_instruction(&mut self.simulation.physics,physics_data,ins.clone());
|
||||||
self.last_instruction_id+=1;
|
self.next_instruction_id+=1;
|
||||||
}else{
|
}else{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
// loop playback
|
// loop playback
|
||||||
self.last_instruction_id=0;
|
self.next_instruction_id=0;
|
||||||
// No need to reset physics because the very first instruction is 'Reset'
|
// No need to reset physics because the very first instruction is 'Reset'
|
||||||
let new_time=self.recording.instructions.first().map_or(PhysicsTime::ZERO,|ins|ins.time);
|
let new_time=self.recording.instructions.first().map_or(PhysicsTime::ZERO,|ins|ins.time);
|
||||||
self.simulation.timer.set_time(time_limit,new_time);
|
self.simulation.timer.set_time(time_limit,new_time);
|
||||||
@@ -243,7 +251,7 @@ impl InstructionConsumer<Instruction<'_>> for Session{
|
|||||||
// what if they pause for 5ms lmao
|
// what if they pause for 5ms lmao
|
||||||
_=self.simulation.timer.set_paused(ins.time,paused);
|
_=self.simulation.timer.set_paused(ins.time,paused);
|
||||||
},
|
},
|
||||||
Instruction::Control(SessionControlInstruction::CopyRecordingIntoReplayAndSpectate)=>{
|
Instruction::Control(SessionControlInstruction::CopyRecordingIntoReplayAndSpectate)=> if let ViewState::Play=self.view_state{
|
||||||
// Bind: B
|
// Bind: B
|
||||||
|
|
||||||
// pause simulation
|
// pause simulation
|
||||||
@@ -281,6 +289,34 @@ impl InstructionConsumer<Instruction<'_>> for Session{
|
|||||||
}
|
}
|
||||||
_=self.simulation.timer.set_paused(ins.time,false);
|
_=self.simulation.timer.set_paused(ins.time,false);
|
||||||
},
|
},
|
||||||
|
Instruction::Control(SessionControlInstruction::SaveReplay)=>{
|
||||||
|
// Bind: N
|
||||||
|
let view_state=core::mem::replace(&mut self.view_state,ViewState::Play);
|
||||||
|
match view_state{
|
||||||
|
ViewState::Play=>(),
|
||||||
|
ViewState::Replay(bot_id)=>if let Some(replay)=self.replays.remove(&bot_id){
|
||||||
|
let file_name=format!("replays/{}.snfb",ins.time);
|
||||||
|
std::thread::spawn(move ||{
|
||||||
|
std::fs::create_dir_all("replays").unwrap();
|
||||||
|
let file=std::fs::File::create(file_name).unwrap();
|
||||||
|
strafesnet_snf::bot::write_bot(std::io::BufWriter::new(file),crate::physics::VERSION.get(),replay.recording.instructions).unwrap();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
}
|
||||||
|
_=self.simulation.timer.set_paused(ins.time,false);
|
||||||
|
},
|
||||||
|
Instruction::Control(SessionControlInstruction::LoadIntoReplayState)=>{
|
||||||
|
// Bind: J
|
||||||
|
let view_state=core::mem::replace(&mut self.view_state,ViewState::Play);
|
||||||
|
match view_state{
|
||||||
|
ViewState::Play=>(),
|
||||||
|
ViewState::Replay(bot_id)=>if let Some(replay)=self.replays.remove(&bot_id){
|
||||||
|
self.recording.instructions=replay.recording.instructions.into_iter().take(replay.next_instruction_id).collect();
|
||||||
|
self.simulation=replay.simulation;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// don't unpause -- use the replay timer state whether it is pasued or unpaused
|
||||||
|
},
|
||||||
Instruction::Playback(SessionPlaybackInstruction::IncreaseTimescale)=>{
|
Instruction::Playback(SessionPlaybackInstruction::IncreaseTimescale)=>{
|
||||||
match &self.view_state{
|
match &self.view_state{
|
||||||
ViewState::Play=>{
|
ViewState::Play=>{
|
||||||
@@ -323,7 +359,7 @@ impl InstructionConsumer<Instruction<'_>> for Session{
|
|||||||
let time=replay.simulation.timer.time(ins.time+SessionTime::from_secs(5));
|
let time=replay.simulation.timer.time(ins.time+SessionTime::from_secs(5));
|
||||||
replay.simulation.timer.set_time(ins.time,time);
|
replay.simulation.timer.set_time(ins.time,time);
|
||||||
// resimulate the entire playback lol
|
// resimulate the entire playback lol
|
||||||
replay.last_instruction_id=0;
|
replay.next_instruction_id=0;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -339,6 +375,30 @@ impl InstructionConsumer<Instruction<'_>> for Session{
|
|||||||
self.clear_recording();
|
self.clear_recording();
|
||||||
self.change_map(complete_map);
|
self.change_map(complete_map);
|
||||||
},
|
},
|
||||||
|
Instruction::LoadReplay(bot)=>{
|
||||||
|
// pause simulation
|
||||||
|
_=self.simulation.timer.set_paused(ins.time,true);
|
||||||
|
|
||||||
|
// create recording
|
||||||
|
let recording=Recording::new(bot.instructions);
|
||||||
|
|
||||||
|
// create timer starting at first instruction (or zero if the list is empty)
|
||||||
|
let new_time=recording.instructions.first().map_or(PhysicsTime::ZERO,|ins|ins.time);
|
||||||
|
let timer=Timer::unpaused(ins.time,new_time);
|
||||||
|
|
||||||
|
// create default physics state
|
||||||
|
let simulation=Simulation::new(timer,Default::default());
|
||||||
|
|
||||||
|
// invent a new bot id and insert the replay
|
||||||
|
let bot_id=BotId(self.replays.len() as u32);
|
||||||
|
self.replays.insert(bot_id,Replay::new(
|
||||||
|
recording,
|
||||||
|
simulation,
|
||||||
|
));
|
||||||
|
|
||||||
|
// begin spectate
|
||||||
|
self.view_state=ViewState::Replay(bot_id);
|
||||||
|
},
|
||||||
Instruction::Idle=>{
|
Instruction::Idle=>{
|
||||||
run_mouse_interpolator_instruction!(MouseInterpolatorInstruction::Idle);
|
run_mouse_interpolator_instruction!(MouseInterpolatorInstruction::Idle);
|
||||||
// this just refreshes the replays
|
// this just refreshes the replays
|
||||||
|
|||||||
@@ -215,7 +215,7 @@ pub fn setup_and_start(title:&str){
|
|||||||
setup_context,
|
setup_context,
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(arg)=std::env::args().nth(1){
|
for arg in std::env::args().skip(1){
|
||||||
let path=std::path::PathBuf::from(arg);
|
let path=std::path::PathBuf::from(arg);
|
||||||
window_thread.send(TimedInstruction{
|
window_thread.send(TimedInstruction{
|
||||||
time:integer::Time::ZERO,
|
time:integer::Time::ZERO,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use strafesnet_common::instruction::TimedInstruction;
|
use strafesnet_common::instruction::TimedInstruction;
|
||||||
use strafesnet_common::session::{Time as SessionTime,TimeInner as SessionTimeInner};
|
use strafesnet_common::session::{Time as SessionTime,TimeInner as SessionTimeInner};
|
||||||
use strafesnet_common::physics::{MiscInstruction,SetControlInstruction};
|
use strafesnet_common::physics::{MiscInstruction,SetControlInstruction};
|
||||||
|
use crate::file::LoadFormat;
|
||||||
use crate::physics_worker::Instruction as PhysicsWorkerInstruction;
|
use crate::physics_worker::Instruction as PhysicsWorkerInstruction;
|
||||||
use crate::session::{SessionInputInstruction,SessionControlInstruction,SessionPlaybackInstruction};
|
use crate::session::{SessionInputInstruction,SessionControlInstruction,SessionPlaybackInstruction};
|
||||||
|
|
||||||
@@ -29,8 +30,9 @@ impl WindowContext<'_>{
|
|||||||
match event{
|
match event{
|
||||||
winit::event::WindowEvent::DroppedFile(path)=>{
|
winit::event::WindowEvent::DroppedFile(path)=>{
|
||||||
match crate::file::load(path.as_path()){
|
match crate::file::load(path.as_path()){
|
||||||
Ok(map)=>self.physics_thread.send(TimedInstruction{time,instruction:PhysicsWorkerInstruction::ChangeMap(map)}).unwrap(),
|
Ok(LoadFormat::Map(map))=>self.physics_thread.send(TimedInstruction{time,instruction:PhysicsWorkerInstruction::ChangeMap(map)}).unwrap(),
|
||||||
Err(e)=>println!("Failed to load map: {e}"),
|
Ok(LoadFormat::Bot(bot))=>self.physics_thread.send(TimedInstruction{time,instruction:PhysicsWorkerInstruction::LoadReplay(bot)}).unwrap(),
|
||||||
|
Err(e)=>println!("Failed to load file: {e}"),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
winit::event::WindowEvent::Focused(state)=>{
|
winit::event::WindowEvent::Focused(state)=>{
|
||||||
@@ -153,6 +155,8 @@ impl WindowContext<'_>{
|
|||||||
"F"|"f"=>input_misc!(PracticeFly,s),
|
"F"|"f"=>input_misc!(PracticeFly,s),
|
||||||
"B"|"b"=>session_ctrl!(CopyRecordingIntoReplayAndSpectate,s),
|
"B"|"b"=>session_ctrl!(CopyRecordingIntoReplayAndSpectate,s),
|
||||||
"X"|"x"=>session_ctrl!(StopSpectate,s),
|
"X"|"x"=>session_ctrl!(StopSpectate,s),
|
||||||
|
"N"|"n"=>session_ctrl!(SaveReplay,s),
|
||||||
|
"J"|"j"=>session_ctrl!(LoadIntoReplayState,s),
|
||||||
_=>None,
|
_=>None,
|
||||||
},
|
},
|
||||||
_=>None,
|
_=>None,
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
mangohud ../target/release/strafe-client bhop_maps/5692113331.snfm
|
mangohud ../target/release/strafe-client bhop_maps/5692113331.snfm "$@"
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
mangohud ../target/release/strafe-client bhop_maps/5692124338.snfm
|
mangohud ../target/release/strafe-client bhop_maps/5692124338.snfm "$@"
|
||||||
|
|||||||
1
tools/replays
Symbolic link
1
tools/replays
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
/run/media/quat/Files/Documents/map-files/verify-scripts/replays
|
||||||
@@ -1 +1 @@
|
|||||||
mangohud ../target/release/strafe-client "$1"
|
mangohud ../target/release/strafe-client "$@"
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
mangohud ../target/release/strafe-client bhop_maps/5692152916.snfm
|
mangohud ../target/release/strafe-client bhop_maps/5692152916.snfm "$@"
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
mangohud ../target/release/strafe-client surf_maps/5692145408.snfm
|
mangohud ../target/release/strafe-client surf_maps/5692145408.snfm "$@"
|
||||||
|
|||||||
Reference in New Issue
Block a user