This commit is contained in:
Quaternions 2023-10-18 20:54:52 -07:00
parent 99db653c14
commit abbab00eea
2 changed files with 75 additions and 48 deletions

View File

@ -50,6 +50,45 @@ pub enum InputInstruction {
//for interpolation / networking / playback reasons, most playback heads will always want
//to be 1 instruction ahead to generate the next state for interpolation.
}
impl InputInstruction{
pub fn id(&self)->u32{
let parity=match self{
crate::physics::InputInstruction::MoveRight(true)
|crate::physics::InputInstruction::MoveUp(true)
|crate::physics::InputInstruction::MoveBack(true)
|crate::physics::InputInstruction::MoveLeft(true)
|crate::physics::InputInstruction::MoveDown(true)
|crate::physics::InputInstruction::MoveForward(true)
|crate::physics::InputInstruction::Jump(true)
|crate::physics::InputInstruction::Zoom(true)=>1u32<<31,
crate::physics::InputInstruction::MoveRight(false)
|crate::physics::InputInstruction::MoveUp(false)
|crate::physics::InputInstruction::MoveBack(false)
|crate::physics::InputInstruction::MoveLeft(false)
|crate::physics::InputInstruction::MoveDown(false)
|crate::physics::InputInstruction::MoveForward(false)
|crate::physics::InputInstruction::Jump(false)
|crate::physics::InputInstruction::Zoom(false)
|crate::physics::InputInstruction::MoveMouse(_)
|crate::physics::InputInstruction::Reset
|crate::physics::InputInstruction::Idle=>0u32,
};
let id=match self{
crate::physics::InputInstruction::MoveRight(_)=>0,
crate::physics::InputInstruction::MoveUp(_)=>1,
crate::physics::InputInstruction::MoveBack(_)=>2,
crate::physics::InputInstruction::MoveLeft(_)=>3,
crate::physics::InputInstruction::MoveDown(_)=>4,
crate::physics::InputInstruction::MoveForward(_)=>5,
crate::physics::InputInstruction::Jump(_)=>6,
crate::physics::InputInstruction::Zoom(_)=>7,
crate::physics::InputInstruction::MoveMouse(_)=>8,
crate::physics::InputInstruction::Reset=>9,
crate::physics::InputInstruction::Idle=>10,
};
id|parity
}
}
#[derive(Clone,Hash)]
pub struct Body {
position: Planar64Vec3,//I64 where 2^32 = 1 u

View File

@ -87,48 +87,26 @@ loop{
BLOCK_DEMO_HEADER:
//timeline of loading maps, player equipment, bots
*/
struct InputInstructionDeltaState{
struct InputInstructionCodecState{
mouse_pos:glam::IVec2,
time:crate::integer::Time,
}
//everything must be 4 byte aligned, it's all going to be compressed so don't think too had about saving less than 4 bytes
//8B - 12B
//TODO: Omit (mouse only?) instructions that don't surround an actual physics instruction
fn write_input_instruction<W:std::io::Write>(state:&mut InputInstructionDeltaState,w:&mut W,ins:&crate::instruction::TimedInstruction<crate::physics::InputInstruction>){
let dt=ins.time-state.time;
//TODO: insert idle instruction if gap is over u32 nanoseconds
//OR: end the data block! the full state at the start of the next block will contain an absolute timestamp
w.write(&(dt.nanos() as u32).to_le_bytes());//4B
let parity=match &ins.instruction{
crate::physics::InputInstruction::MoveRight(true)
|crate::physics::InputInstruction::MoveUp(true)
|crate::physics::InputInstruction::MoveBack(true)
|crate::physics::InputInstruction::MoveLeft(true)
|crate::physics::InputInstruction::MoveDown(true)
|crate::physics::InputInstruction::MoveForward(true)
|crate::physics::InputInstruction::Jump(true)
|crate::physics::InputInstruction::Zoom(true)=>1u32<<31,
crate::physics::InputInstruction::MoveRight(false)
|crate::physics::InputInstruction::MoveUp(false)
|crate::physics::InputInstruction::MoveBack(false)
|crate::physics::InputInstruction::MoveLeft(false)
|crate::physics::InputInstruction::MoveDown(false)
|crate::physics::InputInstruction::MoveForward(false)
|crate::physics::InputInstruction::Jump(false)
|crate::physics::InputInstruction::Zoom(false)
|crate::physics::InputInstruction::MoveMouse(_)
|crate::physics::InputInstruction::Reset
|crate::physics::InputInstruction::Idle=>0u32,//TODO: don't write idle instructions
};
impl InputInstructionCodecState{
pub fn encode(&mut self,ins:&crate::instruction::TimedInstruction<crate::physics::InputInstruction>)->([u8;12],usize){
let dt=ins.time-self.time;
self.time=ins.time;
let mut data=[0u8;12];
[data[0],data[1],data[2],data[3]]=(dt.nanos() as u32).to_le_bytes();//4B
//instruction id packed with game control parity bit. This could be 1 byte but it ruins the alignment
w.write(&(unsafe{std::mem::transmute::<crate::physics::InputInstruction,u32>(ins.instruction)}|parity).to_le_bytes());//4B
[data[4],data[5],data[6],data[7]]=ins.instruction.id().to_le_bytes();//4B
match &ins.instruction{
&crate::physics::InputInstruction::MoveMouse(m)=>{//4B
let dm=m-state.mouse_pos;
w.write(&(dm.x as i16).to_le_bytes());
w.write(&(dm.y as i16).to_le_bytes());
state.mouse_pos=m;
let dm=m-self.mouse_pos;
[data[8],data[9]]=(dm.x as i16).to_le_bytes();
[data[10],data[11]]=(dm.y as i16).to_le_bytes();
self.mouse_pos=m;
(data,12)
},
//0B
crate::physics::InputInstruction::MoveRight(_)
@ -140,7 +118,17 @@ fn write_input_instruction<W:std::io::Write>(state:&mut InputInstructionDeltaSta
|crate::physics::InputInstruction::Jump(_)
|crate::physics::InputInstruction::Zoom(_)
|crate::physics::InputInstruction::Reset
|crate::physics::InputInstruction::Idle=>(),
|crate::physics::InputInstruction::Idle=>(data,8),
}
}
state.time=ins.time;
}
//everything must be 4 byte aligned, it's all going to be compressed so don't think too had about saving less than 4 bytes
//TODO: Omit (mouse only?) instructions that don't surround an actual physics instruction
fn write_input_instruction<W:std::io::Write>(state:&mut InputInstructionCodecState,w:&mut W,ins:&crate::instruction::TimedInstruction<crate::physics::InputInstruction>)->Result<usize,std::io::Error>{
//TODO: insert idle instruction if gap is over u32 nanoseconds
//TODO: don't write idle instructions
//OR: end the data block! the full state at the start of the next block will contain an absolute timestamp
let (data,size)=state.encode(ins);
w.write(&data[0..size])//8B-12B
}