Compare commits

...

10 Commits

Author SHA1 Message Date
50d1a3188e newtypes 2024-07-25 10:42:03 -07:00
9cc099a6eb don't allocate models twice 2024-07-24 14:39:21 -07:00
5d9c124e76 complete map loader 2024-07-24 14:39:21 -07:00
d60486acc3 newtypes 2024-07-24 14:39:21 -07:00
2eef7843a2 generate resource ids + implement load_mesh 2024-07-24 14:39:21 -07:00
5e644c7eed newtypes 2024-07-24 12:00:24 -07:00
567ce86370 wip 2024-07-24 12:00:03 -07:00
9cef23c519 wip 2024-07-23 19:32:29 -07:00
4f90956e16 implement map file format 2024-07-23 19:08:30 -07:00
1883c06f12 newtypes (boilerplate) 2024-07-23 19:08:30 -07:00
10 changed files with 1017 additions and 59 deletions

View File

@ -84,6 +84,9 @@ impl<R:BinReaderExt> File<R>{
data:input,
})
}
pub(crate) fn as_mut(&mut self)->&mut R{
&mut self.data
}
pub(crate) fn block_reader(&mut self,block_id:BlockId)->Result<binrw::io::TakeSeek<&mut R>,Error>{
if self.header.block_location.len() as u32<=block_id.get(){
return Err(Error::InvalidBlockId(block_id))
@ -91,7 +94,7 @@ impl<R:BinReaderExt> File<R>{
let block_start=self.header.block_location[block_id.get() as usize];
let block_end=self.header.block_location[block_id.get() as usize+1];
self.data.seek(std::io::SeekFrom::Start(block_start)).map_err(Error::Seek)?;
Ok((&mut self.data).take_seek(block_end-block_start))
Ok(self.as_mut().take_seek(block_end-block_start))
}
pub(crate) fn fourcc(&self)->FourCC{
self.header.fourcc

View File

@ -1,6 +1,8 @@
use binrw::BinReaderExt;
pub mod file;
mod newtypes;
mod file;
pub mod map;
pub mod bot;
pub mod demo;

View File

@ -1,11 +1,21 @@
//use strafesnet_common::model;
//use strafesnet_common::gameplay_modes;
use binrw::{BinReaderExt, binrw};
use std::io::Read;
use std::collections::HashMap;
use crate::newtypes;
use crate::file::BlockId;
use binrw::{binrw,BinReaderExt};
use strafesnet_common::model;
use strafesnet_common::aabb::Aabb;
use strafesnet_common::bvh::BvhNode;
use strafesnet_common::gameplay_modes;
pub enum Error{
InvalidHeader,
InvalidBvhNodeId(BvhNodeId),
InvalidRegion(binrw::Error),
InvalidHeader(binrw::Error),
InvalidBlockId(BlockId),
InvalidMeshId(model::MeshId),
InvalidTextureId(model::TextureId),
InvalidData(binrw::Error),
IO(std::io::Error),
File(crate::file::Error),
}
@ -14,10 +24,13 @@ pub enum Error{
BLOCK_MAP_HEADER:
DefaultStyleInfo style_info
//bvh goes here
u64 num_nodes
u32 num_nodes
u32 num_spacial_blocks
u32 num_resource_blocks
u32 num_resources_external
//node 0 parent node is implied to be None
for node_id in 1..num_nodes{
u64 parent_node
u32 parent_node
}
//NOTE: alternate realities are not necessary.
@ -26,17 +39,21 @@ for node_id in 1..num_nodes{
//ideally spacial blocks are sorted from distance to start zone
//texture blocks are inserted before the first spacial block they are used in
u64 num_spacial_blocks
for spacial_block_id in 0..num_spacial_blocks{
u64 node_id
u64 block_id //data block
Aabb block_extents
u32 node_id
u32 block_id //data block
Aabb extents
}
//if the map file references external resources, num_resources = 0
u64 num_resources
for resource_id in 0..num_resources{
u64 block_id
u128 resource_id
//the order of these lists uniquely generates the incremental Ids
//MeshId, TextureId etc. based on resource type
//the first 8 bits of resource_uuid describe the type (mesh, texture, etc)
//if the map file references only external resources, num_resource_blocks = 0
for resource_idx in 0..num_resource_blocks{
Resource resource_type
u32 block_id
}
for resource_idx in 0..num_resources_external{
u128 resource_uuid
}
BLOCK_MAP_RESOURCE:
@ -52,71 +69,218 @@ Resource resource_type
BLOCK_MAP_REGION:
u64 num_models
for model_id in 0..num_models{
u128 model_resource_uuid
ModelInstance mode_instance
}
*/
//error hiding mock code
mod gameplay_modes{
pub struct Modes{}
//if you hash the resource itself and set the first 8 bits to this, that's the resource uuid
#[binrw]
#[brw(little,repr=u8)]
enum ResourceType{
Mesh,
Texture,
//Shader,
//Sound,
//Video,
//Animation,
}
mod model{
pub struct Mesh{}
#[super::binrw]
#[brw(little)]
pub struct Model{}
}
mod image{
pub struct Image{}
const RESOURCE_TYPE_VARIANT_COUNT:u8=2;
#[binrw]
#[brw(little)]
struct ResourceId(u128);
impl ResourceId{
fn resource_type(&self)->Option<ResourceType>{
let discriminant=self.0 as u8;
//TODO: use this when it is stabilized https://github.com/rust-lang/rust/issues/73662
//if (discriminant as usize)<std::mem::variant_count::<ResourceType>(){
match discriminant<RESOURCE_TYPE_VARIANT_COUNT{
true=>Some(unsafe{std::mem::transmute::<u8,ResourceType>(discriminant)}),
false=>None,
}
}
}
//serious code
struct ModelUuid(u128);
struct ImageUuid(u128);
struct ResourceMap<T>{
meshes:HashMap<strafesnet_common::model::MeshId,T>,
textures:HashMap<strafesnet_common::model::TextureId,T>,
}
impl<T> Default for ResourceMap<T>{
fn default()->Self{
Self{
meshes:HashMap::new(),
textures:HashMap::new(),
}
}
}
#[binrw]
#[brw(little)]
#[derive(Clone,Copy,id::Id)]
pub struct BvhNodeId(u32);
struct BvhNode{
//aabb
//child
struct SpacialBlockHeader{
id:BlockId,
extents:newtypes::aabb::Aabb,
}
#[binrw]
#[brw(little)]
struct ResourceBlockHeader{
resource:ResourceType,
id:BlockId,
}
#[binrw]
#[brw(little)]
struct ResourceExternalHeader{
resource_uuid:ResourceId,
}
#[binrw]
#[brw(little)]
struct MapHeader{
num_nodes:u32,
num_spacial_blocks:u32,
num_resource_blocks:u32,
num_resources_external:u32,
num_modes:u32,
num_attributes:u32,
num_render_configs:u32,
#[br(count=num_nodes)]
nodes:Vec<u32>,
#[br(count=num_spacial_blocks)]
spacial_blocks:Vec<SpacialBlockHeader>,
#[br(count=num_resource_blocks)]
resource_blocks:Vec<ResourceBlockHeader>,
#[br(count=num_resources_external)]
external_resources:Vec<ResourceExternalHeader>,
#[br(count=num_modes)]
modes:Vec<newtypes::gameplay_modes::Mode>,
#[br(count=num_attributes)]
attributes:Vec<newtypes::gameplay_attributes::CollisionAttributes>,
#[br(count=num_render_configs)]
render_configs:Vec<newtypes::model::RenderConfig>,
}
#[binrw]
#[brw(little)]
struct Region{
//consider including a bvh in the region instead of needing to rebalance the physics bvh on the fly
model_count:u32,
#[br(count=model_count)]
models:Vec<model::Model>,
models:Vec<newtypes::model::Model>,
}
//code deduplication reused in into_complete_map
fn read_region<R:BinReaderExt>(file:&mut crate::file::File<R>,block_id:BlockId)->Result<Vec<model::Model>,Error>{
//load region from disk
//parse the models and determine what resources need to be loaded
//load resources into self.resources
//return Region
let mut block=file.block_reader(block_id).map_err(Error::File)?;
let region:Region=block.read_le().map_err(Error::InvalidData)?;
Ok(region.models.into_iter().map(Into::into).collect())
}
fn read_mesh<R:BinReaderExt>(file:&mut crate::file::File<R>,block_id:BlockId)->Result<model::Mesh,Error>{
let mut block=file.block_reader(block_id).map_err(Error::File)?;
let mesh:newtypes::model::Mesh=block.read_le().map_err(Error::InvalidData)?;
Ok(mesh.into())
}
fn read_texture<R:BinReaderExt>(file:&mut crate::file::File<R>,block_id:BlockId)->Result<Vec<u8>,Error>{
let mut block=file.block_reader(block_id).map_err(Error::File)?;
let mut texture=Vec::new();
block.read_to_end(&mut texture).map_err(Error::IO)?;
Ok(texture)
}
pub struct StreamableMap<R:BinReaderExt>{
file:crate::file::File<R>,
//this includes every platform... move the unconstrained datas to their appropriate data block?
modes:gameplay_modes::Modes,
bvh:BvhNode,
node_id_to_block_id:Vec<crate::file::BlockId>,
//this is every possible attribute... need some sort of streaming system
attributes:Vec<strafesnet_common::gameplay_attributes::CollisionAttributes>,
//this is every possible render configuration... shaders and such... need streaming
render_configs:Vec<strafesnet_common::model::RenderConfig>,
//this makes sense to keep in memory for streaming, a map of which blocks occupy what space
bvh:BvhNode<BlockId>,
//something something resources hashmaps
resource_blocks:ResourceMap<BlockId>,
//resource_external:ResourceMap<ResourceId>,
}
impl<R:BinReaderExt> StreamableMap<R>{
pub(crate) fn new(file:crate::file::File<R>)->Result<Self,Error>{
Err(Error::InvalidHeader)
pub(crate) fn new(mut file:crate::file::File<R>)->Result<Self,Error>{
//assume the file seek is in the right place to start reading a map header
let header:MapHeader=file.as_mut().read_le().map_err(Error::InvalidHeader)?;
let modes=header.modes.into_iter().map(Into::into).collect();
let attributes=header.attributes.into_iter().map(Into::into).collect();
let render_configs=header.render_configs.into_iter().map(Into::into).collect();
let bvh=header.spacial_blocks.into_iter().map(|spacial_block|
(spacial_block.id,spacial_block.extents.into())
).collect();
//generate mesh ids and texture ids from resource list order
let mut resource_blocks=ResourceMap::default();
for resource_block_header in header.resource_blocks{
match resource_block_header.resource{
ResourceType::Mesh=>{
resource_blocks.meshes.insert(
//generate the id from the current length
model::MeshId::new(resource_blocks.meshes.len() as u32),
resource_block_header.id
);
},
ResourceType::Texture=>{
resource_blocks.textures.insert(
model::TextureId::new(resource_blocks.textures.len() as u32),
resource_block_header.id
);
},
}
pub fn load_node(&mut self,node_id:BvhNodeId)->Result<Vec<model::Model>,Error>{
//load region from disk
//parse the models and determine what resources need to be loaded
//load resources into self.resources
//return Region
let block_id=*self.node_id_to_block_id.get(node_id.get() as usize).ok_or(Error::InvalidBvhNodeId(node_id))?;
let mut block=self.file.block_reader(block_id).map_err(Error::File)?;
let region:Region=block.read_le().map_err(Error::InvalidRegion)?;
Ok(region.models)
}
// pub fn load_resource(&mut self,resource_id:ResourceId)->Resource{
// //
// }
Ok(Self{
file,
modes:strafesnet_common::gameplay_modes::Modes::new(modes),
attributes,
render_configs,
bvh:strafesnet_common::bvh::generate_bvh(bvh),
resource_blocks,
//resource_external:Default::default(),
})
}
pub fn get_intersecting_region_block_ids(&self,aabb:&Aabb)->Vec<BlockId>{
let mut block_ids=Vec::new();
self.bvh.the_tester(aabb,&mut |block_id|block_ids.push(block_id));
block_ids
}
pub fn load_region(&mut self,block_id:BlockId)->Result<Vec<model::Model>,Error>{
read_region(&mut self.file,block_id)
}
pub fn load_mesh(&mut self,mesh_id:model::MeshId)->Result<model::Mesh,Error>{
let block_id=*self.resource_blocks.meshes.get(&mesh_id).ok_or(Error::InvalidMeshId(mesh_id))?;
read_mesh(&mut self.file,block_id)
}
pub fn load_texture(&mut self,texture_id:model::TextureId)->Result<Vec<u8>,Error>{
let block_id=*self.resource_blocks.textures.get(&texture_id).ok_or(Error::InvalidTextureId(texture_id))?;
read_texture(&mut self.file,block_id)
}
pub fn into_complete_map(mut self)->Result<strafesnet_common::map::CompleteMap,Error>{
//load all meshes
let meshes=self.resource_blocks.meshes.into_values().map(|block_id|
read_mesh(&mut self.file,block_id)
).collect::<Result<Vec<_>,_>>()?;
//load all textures
let textures=self.resource_blocks.textures.into_values().map(|block_id|
read_texture(&mut self.file,block_id)
).collect::<Result<Vec<_>,_>>()?;
let mut block_ids=Vec::new();
self.bvh.into_visitor(&mut |block_id|block_ids.push(block_id));
//load all regions
let mut models=Vec::new();
for block_id in block_ids{
models.append(&mut read_region(&mut self.file,block_id)?);
}
Ok(strafesnet_common::map::CompleteMap{
modes:self.modes,
attributes:self.attributes,
meshes,
models,
textures,
render_configs:self.render_configs,
})
}
}

6
src/newtypes.rs Normal file
View File

@ -0,0 +1,6 @@
pub mod aabb;
pub mod model;
pub mod integer;
pub mod gameplay_modes;
pub mod gameplay_style;
pub mod gameplay_attributes;

15
src/newtypes/aabb.rs Normal file
View File

@ -0,0 +1,15 @@
use super::integer::Planar64Vec3;
#[binrw::binrw]
#[brw(little)]
pub struct Aabb{
pub min:Planar64Vec3,
pub max:Planar64Vec3,
}
impl Into<strafesnet_common::aabb::Aabb> for Aabb{
fn into(self)->strafesnet_common::aabb::Aabb{
strafesnet_common::aabb::Aabb::new(
strafesnet_common::integer::Planar64Vec3::raw_array(self.min),
strafesnet_common::integer::Planar64Vec3::raw_array(self.max),
)
}
}

View File

@ -0,0 +1,246 @@
use super::integer::{Time,Planar64,Planar64Vec3};
#[binrw::binrw]
#[brw(little)]
pub struct ContactingLadder{
pub sticky:Option<()>,
}
impl Into<strafesnet_common::gameplay_attributes::ContactingLadder> for ContactingLadder{
fn into(self)->strafesnet_common::gameplay_attributes::ContactingLadder{
strafesnet_common::gameplay_attributes::ContactingLadder{
sticky:self.sticky.is_some(),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub enum ContactingBehaviour{
Surf,
Ladder(ContactingLadder),
NoJump,
Cling,
Elastic(u32),
}
impl Into<strafesnet_common::gameplay_attributes::ContactingBehaviour> for ContactingBehaviour{
fn into(self)->strafesnet_common::gameplay_attributes::ContactingBehaviour{
match self{
ContactingBehaviour::Surf=>
strafesnet_common::gameplay_attributes::ContactingBehaviour::Surf,
ContactingBehaviour::Ladder(contacting_ladder)=>
strafesnet_common::gameplay_attributes::ContactingBehaviour::Ladder(
contacting_ladder.into(),
),
ContactingBehaviour::NoJump=>
strafesnet_common::gameplay_attributes::ContactingBehaviour::NoJump,
ContactingBehaviour::Cling=>
strafesnet_common::gameplay_attributes::ContactingBehaviour::Cling,
ContactingBehaviour::Elastic(elasticity)=>
strafesnet_common::gameplay_attributes::ContactingBehaviour::Elastic(elasticity),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct IntersectingWater{
pub viscosity:Planar64,
pub density:Planar64,
pub velocity:Planar64Vec3,
}
impl Into<strafesnet_common::gameplay_attributes::IntersectingWater> for IntersectingWater{
fn into(self)->strafesnet_common::gameplay_attributes::IntersectingWater{
strafesnet_common::gameplay_attributes::IntersectingWater{
viscosity:strafesnet_common::integer::Planar64::raw(self.viscosity),
density:strafesnet_common::integer::Planar64::raw(self.density),
velocity:strafesnet_common::integer::Planar64Vec3::raw_array(self.velocity),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct Accelerator{
pub acceleration:Planar64Vec3
}
impl Into<strafesnet_common::gameplay_attributes::Accelerator> for Accelerator{
fn into(self)->strafesnet_common::gameplay_attributes::Accelerator{
strafesnet_common::gameplay_attributes::Accelerator{
acceleration:strafesnet_common::integer::Planar64Vec3::raw_array(self.acceleration)
}
}
}
#[binrw::binrw]
#[brw(little)]
pub enum Booster{
Velocity(Planar64Vec3),
Energy{direction:Planar64Vec3,energy:Planar64},
}
impl Into<strafesnet_common::gameplay_attributes::Booster> for Booster{
fn into(self)->strafesnet_common::gameplay_attributes::Booster{
match self{
Booster::Velocity(velocity)=>
strafesnet_common::gameplay_attributes::Booster::Velocity(
strafesnet_common::integer::Planar64Vec3::raw_array(velocity)
),
Booster::Energy{direction,energy}=>
strafesnet_common::gameplay_attributes::Booster::Energy{
direction:strafesnet_common::integer::Planar64Vec3::raw_array(direction),
energy:strafesnet_common::integer::Planar64::raw(energy)
},
}
}
}
#[binrw::binrw]
#[brw(little,repr=u8)]
pub enum TrajectoryChoice{
HighArcLongDuration,
LowArcShortDuration,
}
impl Into<strafesnet_common::gameplay_attributes::TrajectoryChoice> for TrajectoryChoice{
fn into(self)->strafesnet_common::gameplay_attributes::TrajectoryChoice{
match self{
TrajectoryChoice::HighArcLongDuration=>
strafesnet_common::gameplay_attributes::TrajectoryChoice::HighArcLongDuration,
TrajectoryChoice::LowArcShortDuration=>
strafesnet_common::gameplay_attributes::TrajectoryChoice::LowArcShortDuration,
}
}
}
#[binrw::binrw]
#[brw(little)]
pub enum SetTrajectory{
AirTime(Time),
Height(Planar64),
DotVelocity{direction:Planar64Vec3,dot:Planar64},
TargetPointTime{
target_point:Planar64Vec3,
time:Time,
},
TargetPointSpeed{
target_point:Planar64Vec3,
speed:Planar64,
trajectory_choice:TrajectoryChoice,
},
Velocity(Planar64Vec3),
}
impl Into<strafesnet_common::gameplay_attributes::SetTrajectory> for SetTrajectory{
fn into(self)->strafesnet_common::gameplay_attributes::SetTrajectory{
match self{
SetTrajectory::AirTime(time)=>
strafesnet_common::gameplay_attributes::SetTrajectory::AirTime(
strafesnet_common::integer::Time::raw(time)
),
SetTrajectory::Height(height)=>
strafesnet_common::gameplay_attributes::SetTrajectory::Height(
strafesnet_common::integer::Planar64::raw(height)
),
SetTrajectory::DotVelocity{direction,dot}=>
strafesnet_common::gameplay_attributes::SetTrajectory::DotVelocity{
direction:strafesnet_common::integer::Planar64Vec3::raw_array(direction),
dot:strafesnet_common::integer::Planar64::raw(dot),
},
SetTrajectory::TargetPointTime{target_point,time}=>
strafesnet_common::gameplay_attributes::SetTrajectory::TargetPointTime{
target_point:strafesnet_common::integer::Planar64Vec3::raw_array(target_point),
time:strafesnet_common::integer::Time::raw(time),
},
SetTrajectory::TargetPointSpeed{target_point,speed,trajectory_choice}=>
strafesnet_common::gameplay_attributes::SetTrajectory::TargetPointSpeed{
target_point:strafesnet_common::integer::Planar64Vec3::raw_array(target_point),
speed:strafesnet_common::integer::Planar64::raw(speed),
trajectory_choice:trajectory_choice.into(),
},
SetTrajectory::Velocity(velocity)=>
strafesnet_common::gameplay_attributes::SetTrajectory::Velocity(
strafesnet_common::integer::Planar64Vec3::raw_array(velocity)
),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct Wormhole{
pub destination_model:u32,
}
impl Into<strafesnet_common::gameplay_attributes::Wormhole> for Wormhole{
fn into(self)->strafesnet_common::gameplay_attributes::Wormhole{
strafesnet_common::gameplay_attributes::Wormhole{
destination_model:strafesnet_common::model::ModelId::new(self.destination_model),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct GeneralAttributes{
pub booster:Option<Booster>,
pub trajectory:Option<SetTrajectory>,
pub wormhole:Option<Wormhole>,
pub accelerator:Option<Accelerator>,
}
impl Into<strafesnet_common::gameplay_attributes::GeneralAttributes> for GeneralAttributes{
fn into(self)->strafesnet_common::gameplay_attributes::GeneralAttributes{
strafesnet_common::gameplay_attributes::GeneralAttributes{
booster:self.booster.map(Into::into),
trajectory:self.trajectory.map(Into::into),
wormhole:self.wormhole.map(Into::into),
accelerator:self.accelerator.map(Into::into),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct ContactingAttributes{
pub contact_behaviour:Option<ContactingBehaviour>,
}
impl Into<strafesnet_common::gameplay_attributes::ContactingAttributes> for ContactingAttributes{
fn into(self)->strafesnet_common::gameplay_attributes::ContactingAttributes{
strafesnet_common::gameplay_attributes::ContactingAttributes{
contact_behaviour:self.contact_behaviour.map(Into::into),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct IntersectingAttributes{
pub water:Option<IntersectingWater>,
}
impl Into<strafesnet_common::gameplay_attributes::IntersectingAttributes> for IntersectingAttributes{
fn into(self)->strafesnet_common::gameplay_attributes::IntersectingAttributes{
strafesnet_common::gameplay_attributes::IntersectingAttributes{
water:self.water.map(Into::into),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub enum CollisionAttributes{
Decoration,
Contact{
contacting:ContactingAttributes,
general:GeneralAttributes,
},
Intersect{
intersecting:IntersectingAttributes,
general:GeneralAttributes,
},
}
impl Into<strafesnet_common::gameplay_attributes::CollisionAttributes> for CollisionAttributes{
fn into(self)->strafesnet_common::gameplay_attributes::CollisionAttributes{
match self{
CollisionAttributes::Decoration=>
strafesnet_common::gameplay_attributes::CollisionAttributes::Decoration,
CollisionAttributes::Contact{contacting,general}=>
strafesnet_common::gameplay_attributes::CollisionAttributes::Contact{contacting:contacting.into(),general:general.into()},
CollisionAttributes::Intersect{intersecting,general}=>
strafesnet_common::gameplay_attributes::CollisionAttributes::Intersect{intersecting:intersecting.into(),general:general.into()},
}
}
}

View File

@ -0,0 +1,126 @@
#[binrw::binrw]
#[brw(little,repr=u8)]
pub enum StageElementBehaviour{
SpawnAt,//must be standing on top to get effect. except cancollide false
Trigger,
Teleport,
Platform,
//Check(point) acts like a trigger if you haven't hit all the checkpoints on previous stages yet.
//Note that all stage elements act like this, this is just the isolated behaviour.
Check,
Checkpoint,//this is a combined behaviour for Ordered & Unordered in case a model is used multiple times or for both.
}
impl Into<strafesnet_common::gameplay_modes::StageElementBehaviour> for StageElementBehaviour{
fn into(self)->strafesnet_common::gameplay_modes::StageElementBehaviour{
match self{
StageElementBehaviour::SpawnAt=>strafesnet_common::gameplay_modes::StageElementBehaviour::SpawnAt,
StageElementBehaviour::Trigger=>strafesnet_common::gameplay_modes::StageElementBehaviour::Trigger,
StageElementBehaviour::Teleport=>strafesnet_common::gameplay_modes::StageElementBehaviour::Teleport,
StageElementBehaviour::Platform=>strafesnet_common::gameplay_modes::StageElementBehaviour::Platform,
StageElementBehaviour::Check=>strafesnet_common::gameplay_modes::StageElementBehaviour::Check,
StageElementBehaviour::Checkpoint=>strafesnet_common::gameplay_modes::StageElementBehaviour::Checkpoint,
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct StageElement{
pub stage_id:u32,//which stage spawn to send to
pub behaviour:StageElementBehaviour,
pub jump_limit:Option<u8>,
pub force:Option<()>,//allow setting to lower spawn id i.e. 7->3
}
impl Into<strafesnet_common::gameplay_modes::StageElement> for StageElement{
fn into(self)->strafesnet_common::gameplay_modes::StageElement{
strafesnet_common::gameplay_modes::StageElement::new(
strafesnet_common::gameplay_modes::StageId::new(self.stage_id),
self.force.is_some(),
self.behaviour.into(),
self.jump_limit,
)
}
}
#[binrw::binrw]
#[brw(little)]
pub struct Stage{
pub spawn:u32,
//open world support lol
pub ordered_checkpoints_count:u32,
pub unordered_checkpoints_count:u32,
//currently loaded checkpoint models
#[br(count=ordered_checkpoints_count)]
pub ordered_checkpoints:Vec<(u32,u32)>,
#[br(count=unordered_checkpoints_count)]
pub unordered_checkpoints:Vec<u32>,
}
impl Into<strafesnet_common::gameplay_modes::Stage> for Stage{
fn into(self)->strafesnet_common::gameplay_modes::Stage{
strafesnet_common::gameplay_modes::Stage::new(
strafesnet_common::model::ModelId::new(self.spawn),
self.ordered_checkpoints_count,
self.unordered_checkpoints_count,
self.ordered_checkpoints.into_iter().map(|(checkpoint_id,model_id)|(
strafesnet_common::gameplay_modes::CheckpointId::new(checkpoint_id),
strafesnet_common::model::ModelId::new(model_id),
)).collect(),
self.unordered_checkpoints.into_iter()
.map(strafesnet_common::model::ModelId::new)
.collect(),
)
}
}
#[binrw::binrw]
#[brw(little,repr=u8)]
pub enum Zone{
Start,
Finish,
Anticheat,
}
impl Into<strafesnet_common::gameplay_modes::Zone> for Zone{
fn into(self)->strafesnet_common::gameplay_modes::Zone{
match self{
Zone::Start=>strafesnet_common::gameplay_modes::Zone::Start,
Zone::Finish=>strafesnet_common::gameplay_modes::Zone::Finish,
Zone::Anticheat=>strafesnet_common::gameplay_modes::Zone::Anticheat,
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct ModeHeader{
pub zones:u32,
pub stages:u32,
pub elements:u32,
}
#[binrw::binrw]
#[brw(little)]
pub struct Mode{
pub header:ModeHeader,
pub style:super::gameplay_style::StyleModifiers,
pub start:u32,
#[br(count=header.zones)]
pub zones:Vec<(u32,Zone)>,
#[br(count=header.stages)]
pub stages:Vec<Stage>,
#[br(count=header.elements)]
pub elements:Vec<(u32,StageElement)>,
}
impl Into<strafesnet_common::gameplay_modes::Mode> for Mode{
fn into(self)->strafesnet_common::gameplay_modes::Mode{
strafesnet_common::gameplay_modes::Mode::new(
self.style.into(),
strafesnet_common::model::ModelId::new(self.start),
self.zones.into_iter().map(|(model_id,zone)|
(strafesnet_common::model::ModelId::new(model_id),zone.into())
).collect(),
self.stages.into_iter().map(Into::into).collect(),
self.elements.into_iter().map(|(model_id,stage_element)|
(strafesnet_common::model::ModelId::new(model_id),stage_element.into())
).collect(),
)
}
}

View File

@ -0,0 +1,218 @@
use super::integer::{Time,Ratio64,Planar64,Planar64Vec3};
pub type Controls=u32;
#[binrw::binrw]
#[brw(little)]
pub struct StyleModifiers{
pub controls_mask:Controls,
pub controls_mask_state:Controls,
pub strafe:Option<StrafeSettings>,
pub rocket:Option<PropulsionSettings>,
pub jump:Option<JumpSettings>,
pub walk:Option<WalkSettings>,
pub ladder:Option<LadderSettings>,
pub swim:Option<PropulsionSettings>,
pub gravity:Planar64Vec3,
pub hitbox:Hitbox,
pub camera_offset:Planar64Vec3,
pub mass:Planar64,
}
impl Into<strafesnet_common::gameplay_style::StyleModifiers> for StyleModifiers{
fn into(self)->strafesnet_common::gameplay_style::StyleModifiers{
strafesnet_common::gameplay_style::StyleModifiers{
//TODO: fail gracefully in binrw instead of panicing here
controls_mask:strafesnet_common::controls_bitflag::Controls::from_bits(self.controls_mask).unwrap(),
controls_mask_state:strafesnet_common::controls_bitflag::Controls::from_bits(self.controls_mask_state).unwrap(),
strafe:self.strafe.map(Into::into),
rocket:self.rocket.map(Into::into),
jump:self.jump.map(Into::into),
walk:self.walk.map(Into::into),
ladder:self.ladder.map(Into::into),
swim:self.swim.map(Into::into),
gravity:strafesnet_common::integer::Planar64Vec3::raw_array(self.gravity),
hitbox:self.hitbox.into(),
camera_offset:strafesnet_common::integer::Planar64Vec3::raw_array(self.camera_offset),
mass:strafesnet_common::integer::Planar64::raw(self.mass),
}
}
}
#[binrw::binrw]
#[brw(little,repr=u8)]
pub enum JumpCalculation{
Capped,
Energy,
Linear,
}
impl Into<strafesnet_common::gameplay_style::JumpCalculation> for JumpCalculation{
fn into(self)->strafesnet_common::gameplay_style::JumpCalculation{
match self{
JumpCalculation::Capped=>strafesnet_common::gameplay_style::JumpCalculation::Capped,
JumpCalculation::Energy=>strafesnet_common::gameplay_style::JumpCalculation::Energy,
JumpCalculation::Linear=>strafesnet_common::gameplay_style::JumpCalculation::Linear,
}
}
}
#[binrw::binrw]
#[brw(little)]
pub enum JumpImpulse{
FromTime(Time),
FromHeight(Planar64),
FromDeltaV(Planar64),
FromEnergy(Planar64),
}
impl Into<strafesnet_common::gameplay_style::JumpImpulse> for JumpImpulse{
fn into(self)->strafesnet_common::gameplay_style::JumpImpulse{
match self{
JumpImpulse::FromTime(time)=>strafesnet_common::gameplay_style::JumpImpulse::FromTime(strafesnet_common::integer::Time::raw(time)),
JumpImpulse::FromHeight(height)=>strafesnet_common::gameplay_style::JumpImpulse::FromHeight(strafesnet_common::integer::Planar64::raw(height)),
JumpImpulse::FromDeltaV(deltav)=>strafesnet_common::gameplay_style::JumpImpulse::FromDeltaV(strafesnet_common::integer::Planar64::raw(deltav)),
JumpImpulse::FromEnergy(energy)=>strafesnet_common::gameplay_style::JumpImpulse::FromEnergy(strafesnet_common::integer::Planar64::raw(energy)),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct ControlsActivation{
controls_mask:Controls,
controls_intersects:Controls,
controls_contains:Controls,
}
impl Into<strafesnet_common::gameplay_style::ControlsActivation> for ControlsActivation{
fn into(self)->strafesnet_common::gameplay_style::ControlsActivation{
strafesnet_common::gameplay_style::ControlsActivation::new(
strafesnet_common::controls_bitflag::Controls::from_bits(self.controls_mask).unwrap(),
strafesnet_common::controls_bitflag::Controls::from_bits(self.controls_intersects).unwrap(),
strafesnet_common::controls_bitflag::Controls::from_bits(self.controls_contains).unwrap(),
)
}
}
#[binrw::binrw]
#[brw(little)]
pub struct StrafeSettings{
enable:ControlsActivation,
mv:Planar64,
air_accel_limit:Option<Planar64>,
tick_rate:Ratio64,
}
impl Into<strafesnet_common::gameplay_style::StrafeSettings> for StrafeSettings{
fn into(self)->strafesnet_common::gameplay_style::StrafeSettings{
strafesnet_common::gameplay_style::StrafeSettings::new(
self.enable.into(),
strafesnet_common::integer::Planar64::raw(self.mv),
self.air_accel_limit.map(strafesnet_common::integer::Planar64::raw),
self.tick_rate.into(),
)
}
}
#[binrw::binrw]
#[brw(little)]
pub struct PropulsionSettings{
magnitude:Planar64,
}
impl Into<strafesnet_common::gameplay_style::PropulsionSettings> for PropulsionSettings{
fn into(self)->strafesnet_common::gameplay_style::PropulsionSettings{
strafesnet_common::gameplay_style::PropulsionSettings::new(
strafesnet_common::integer::Planar64::raw(self.magnitude)
)
}
}
#[binrw::binrw]
#[brw(little)]
pub struct JumpSettings{
impulse:JumpImpulse,
calculation:JumpCalculation,
}
impl Into<strafesnet_common::gameplay_style::JumpSettings> for JumpSettings{
fn into(self)->strafesnet_common::gameplay_style::JumpSettings{
strafesnet_common::gameplay_style::JumpSettings::new(
self.impulse.into(),
self.calculation.into(),
)
}
}
#[binrw::binrw]
#[brw(little)]
pub struct AccelerateSettings{
accel:Planar64,
topspeed:Planar64,
}
impl Into<strafesnet_common::gameplay_style::AccelerateSettings> for AccelerateSettings{
fn into(self)->strafesnet_common::gameplay_style::AccelerateSettings{
strafesnet_common::gameplay_style::AccelerateSettings::new(
strafesnet_common::integer::Planar64::raw(self.accel),
strafesnet_common::integer::Planar64::raw(self.topspeed),
)
}
}
#[binrw::binrw]
#[brw(little)]
pub struct WalkSettings{
accelerate:AccelerateSettings,
static_friction:Planar64,
kinetic_friction:Planar64,
surf_dot:Planar64,
}
impl Into<strafesnet_common::gameplay_style::WalkSettings> for WalkSettings{
fn into(self)->strafesnet_common::gameplay_style::WalkSettings{
strafesnet_common::gameplay_style::WalkSettings::new(
self.accelerate.into(),
strafesnet_common::integer::Planar64::raw(self.static_friction),
strafesnet_common::integer::Planar64::raw(self.kinetic_friction),
strafesnet_common::integer::Planar64::raw(self.surf_dot),
)
}
}
#[binrw::binrw]
#[brw(little)]
pub struct LadderSettings{
accelerate:AccelerateSettings,
dot:Planar64,
}
impl Into<strafesnet_common::gameplay_style::LadderSettings> for LadderSettings{
fn into(self)->strafesnet_common::gameplay_style::LadderSettings{
strafesnet_common::gameplay_style::LadderSettings::new(
self.accelerate.into(),
strafesnet_common::integer::Planar64::raw(self.dot),
)
}
}
#[binrw::binrw]
#[brw(little,repr=u8)]
pub enum HitboxMesh{
Box,
Cylinder,
}
impl Into<strafesnet_common::gameplay_style::HitboxMesh> for HitboxMesh{
fn into(self)->strafesnet_common::gameplay_style::HitboxMesh{
match self{
HitboxMesh::Box=>strafesnet_common::gameplay_style::HitboxMesh::Box,
HitboxMesh::Cylinder=>strafesnet_common::gameplay_style::HitboxMesh::Cylinder,
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct Hitbox{
pub halfsize:Planar64Vec3,
pub mesh:HitboxMesh,
}
impl Into<strafesnet_common::gameplay_style::Hitbox> for Hitbox{
fn into(self)->strafesnet_common::gameplay_style::Hitbox{
strafesnet_common::gameplay_style::Hitbox{
halfsize:strafesnet_common::integer::Planar64Vec3::raw_array(self.halfsize),
mesh:self.mesh.into(),
}
}
}

26
src/newtypes/integer.rs Normal file
View File

@ -0,0 +1,26 @@
pub type Time=i64;
#[binrw::binrw]
#[brw(little)]
pub struct Ratio64{
num:i64,
den:u64,
}
impl Into<strafesnet_common::integer::Ratio64> for Ratio64{
fn into(self)->strafesnet_common::integer::Ratio64{
strafesnet_common::integer::Ratio64::new(self.num,self.den).unwrap()
}
}
#[binrw::binrw]
#[brw(little)]
pub struct Ratio64Vec2{
pub x:Ratio64,
pub y:Ratio64,
}
pub type Angle32=i32;
pub type Planar64=i64;
pub type Planar64Vec3=[i64;3];
pub type Planar64Mat3=[i64;9];
pub type Planar64Affine3=[i64;12];

152
src/newtypes/model.rs Normal file
View File

@ -0,0 +1,152 @@
use super::integer::{Planar64Vec3,Planar64Affine3};
pub type TextureCoordinate=[f32;2];
pub type Color4=[f32;4];
#[binrw::binrw]
#[brw(little)]
pub struct IndexedVertex{
pub pos:u32,
pub tex:u32,
pub normal:u32,
pub color:u32,
}
#[binrw::binrw]
#[brw(little)]
pub struct Polygon{
pub count:u32,
#[br(count=count)]
pub vertices:Vec<u32>,
}
#[binrw::binrw]
#[brw(little)]
pub struct PolygonGroup{
pub count:u32,
#[br(count=count)]
pub polys:Vec<Polygon>,
}
#[binrw::binrw]
#[brw(little)]
pub struct RenderConfig{
pub texture:Option<u32>,
}
impl Into<strafesnet_common::model::RenderConfig> for RenderConfig{
fn into(self)->strafesnet_common::model::RenderConfig{
strafesnet_common::model::RenderConfig{
texture:self.texture.map(strafesnet_common::model::TextureId::new),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct IndexedGraphicsGroup{
pub count:u32,
pub render:u32,
#[br(count=count)]
pub groups:Vec<u32>,
}
#[binrw::binrw]
#[brw(little)]
pub struct IndexedPhysicsGroup{
pub count:u32,
#[br(count=count)]
pub groups:Vec<u32>,
}
#[binrw::binrw]
#[brw(little)]
pub struct MeshHeader{
pub unique_pos:u32,
pub unique_normal:u32,
pub unique_tex:u32,
pub unique_color:u32,
pub unique_vertices:u32,
pub polygon_groups:u32,
pub graphics_groups:u32,
pub physics_groups:u32,
}
#[binrw::binrw]
#[brw(little)]
pub struct Mesh{
pub header:MeshHeader,
#[br(count=header.unique_pos)]
pub unique_pos:Vec<Planar64Vec3>,
#[br(count=header.unique_normal)]
pub unique_normal:Vec<Planar64Vec3>,
#[br(count=header.unique_tex)]
pub unique_tex:Vec<TextureCoordinate>,
#[br(count=header.unique_color)]
pub unique_color:Vec<Color4>,
#[br(count=header.unique_vertices)]
pub unique_vertices:Vec<IndexedVertex>,
#[br(count=header.polygon_groups)]
pub polygon_groups:Vec<PolygonGroup>,
#[br(count=header.graphics_groups)]
pub graphics_groups:Vec<IndexedGraphicsGroup>,
#[br(count=header.physics_groups)]
pub physics_groups:Vec<IndexedPhysicsGroup>,
}
impl Into<strafesnet_common::model::Mesh> for Mesh{
fn into(self)->strafesnet_common::model::Mesh{
strafesnet_common::model::Mesh{
unique_pos:self.unique_pos.into_iter().map(strafesnet_common::integer::Planar64Vec3::raw_array).collect(),
unique_normal:self.unique_normal.into_iter().map(strafesnet_common::integer::Planar64Vec3::raw_array).collect(),
unique_tex:self.unique_tex.into_iter().map(strafesnet_common::model::TextureCoordinate::from_array).collect(),
unique_color:self.unique_color.into_iter().map(strafesnet_common::model::Color4::from_array).collect(),
unique_vertices:self.unique_vertices.into_iter().map(|vert|strafesnet_common::model::IndexedVertex{
pos:strafesnet_common::model::PositionId::new(vert.pos),
tex:strafesnet_common::model::TextureCoordinateId::new(vert.tex),
normal:strafesnet_common::model::NormalId::new(vert.normal),
color:strafesnet_common::model::ColorId::new(vert.color),
}).collect(),
polygon_groups:self.polygon_groups.into_iter().map(|group|
strafesnet_common::model::PolygonGroup::PolygonList(
strafesnet_common::model::PolygonList::new(
group.polys.into_iter().map(|vert|
vert.vertices.into_iter().map(strafesnet_common::model::VertexId::new).collect()
).collect()
)
)
).collect(),
graphics_groups:self.graphics_groups.into_iter().map(|group|
strafesnet_common::model::IndexedGraphicsGroup{
render:strafesnet_common::model::RenderConfigId::new(group.render),
groups:group.groups.into_iter().map(strafesnet_common::model::PolygonGroupId::new).collect(),
}
).collect(),
physics_groups:self.physics_groups.into_iter().map(|group|
strafesnet_common::model::IndexedPhysicsGroup{
groups:group.groups.into_iter().map(strafesnet_common::model::PolygonGroupId::new).collect(),
}
).collect(),
}
}
}
#[binrw::binrw]
#[brw(little)]
pub struct Model{
pub mesh:u32,
pub attributes:u32,
pub color:Color4,
pub transform:Planar64Affine3,
}
impl Into<strafesnet_common::model::Model> for Model{
fn into(self)->strafesnet_common::model::Model{
let [_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b]=self.transform;
strafesnet_common::model::Model{
mesh:strafesnet_common::model::MeshId::new(self.mesh),
attributes:strafesnet_common::gameplay_attributes::CollisionAttributesId::new(self.attributes),
color:strafesnet_common::model::Color4::from_array(self.color),
transform:strafesnet_common::integer::Planar64Affine3::new(
strafesnet_common::integer::Planar64Mat3::from_cols(
strafesnet_common::integer::Planar64Vec3::raw_xyz(_0,_1,_2),
strafesnet_common::integer::Planar64Vec3::raw_xyz(_3,_4,_5),
strafesnet_common::integer::Planar64Vec3::raw_xyz(_6,_7,_8)
),
strafesnet_common::integer::Planar64Vec3::raw_xyz(_9,_a,_b)
),
}
}
}