Compare commits

...

4 Commits

Author SHA1 Message Date
96d813a3a6 work 2024-02-03 16:04:59 -08:00
9291b77679 refine Updatable concept 2024-02-03 16:04:59 -08:00
fa97ab8c6d the work 2024-02-03 16:04:59 -08:00
f38d990a54 use scale const instead of magic numbers 2024-02-03 16:04:47 -08:00
7 changed files with 345 additions and 73 deletions

@ -1,3 +1,4 @@
use crate::model;
use crate::integer::{Time,Planar64,Planar64Vec3};
//you have this effect while in contact
@ -8,8 +9,9 @@ pub struct ContactingLadder{
#[derive(Clone,Hash,Eq,PartialEq)]
pub enum ContactingBehaviour{
Surf,
Cling,//usable as a zipline, or other weird and wonderful things
Ladder(ContactingLadder),
NoJump,
Cling,//usable as a zipline, or other weird and wonderful things
Elastic(u32),//[1/2^32,1] 0=None (elasticity+1)/2^32
}
//you have this effect while intersecting
@ -76,7 +78,7 @@ pub struct Wormhole{
//destination does not need to be another wormhole
//this defines a one way portal to a destination model transform
//two of these can create a two way wormhole
pub destination_model_id:u32,
pub destination_model:model::ModelId,
//(position,angles)*=origin.transform.inverse()*destination.transform
}
//attributes listed in order of handling
@ -131,6 +133,12 @@ impl IntersectingAttributes{
}
}
pub struct CollisionAttributesId(u32);
impl CollisionAttributesId{
pub fn new(id:u32)->Self{
Self(id)
}
}
#[derive(Clone,Hash,Eq,PartialEq)]
pub enum CollisionAttributes{
Decoration,//visual only
Contact{//track whether you are contacting the object

@ -1,11 +1,22 @@
use std::collections::{HashSet,HashMap};
use crate::model::ModelId;
use crate::gameplay_style;
use crate::updatable::Updatable;
#[derive(Clone)]
pub struct StageElement{
stage:StageId,//which stage spawn to send to
force:bool,//allow setting to lower spawn id i.e. 7->3
behaviour:StageElementBehaviour
pub stage:StageId,//which stage spawn to send to
pub force:bool,//allow setting to lower spawn id i.e. 7->3
pub behaviour:StageElementBehaviour
}
impl StageElement{
pub fn new(stage_id:u32,force:bool,behaviour:StageElementBehaviour)->Self{
Self{
stage:StageId(stage_id),
force,
behaviour,
}
}
}
#[derive(Clone,Hash,Eq,PartialEq)]
@ -20,50 +31,127 @@ pub enum StageElementBehaviour{
Checkpoint,//this is a combined behaviour for Ordered & Unordered in case a model is used multiple times or for both.
}
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
pub struct CheckpointId(usize);
#[derive(Clone,Hash,Eq,PartialEq,Ord,PartialOrd)]
pub struct StageId(u32);
impl StageId{
pub const fn id(id:u32)->Self{
Self(id)
}
pub const fn get(self)->u32{
self.0
}
}
pub struct Stage{
spawn:ModelId,
//open world support lol
ordered_checkpoints_count:u32,
//other behaviour models of this stage can have
ordered_checkpoints:Vec<ModelId>,
ordered_checkpoints:HashMap<CheckpointId,ModelId>,
unordered_checkpoints:HashSet<ModelId>,
}
#[derive(Clone,Hash,Eq,PartialEq)]
pub enum ZoneBehaviour{
Finish,
Anitcheat,
impl Stage{
pub fn new(spawn:ModelId)->Self{
Self{
spawn,
ordered_checkpoints_count:0,
ordered_checkpoints:HashMap::new(),
unordered_checkpoints:HashSet::new(),
}
}
}
#[derive(Default)]
pub struct StageUpdate{
//other behaviour models of this stage can have
ordered_checkpoints:HashMap<CheckpointId,ModelId>,
unordered_checkpoints:HashSet<ModelId>,
}
impl Updatable<StageUpdate> for Stage{
fn update(&mut self,update:StageUpdate){
self.ordered_checkpoints.extend(update.ordered_checkpoints);
self.unordered_checkpoints.extend(update.unordered_checkpoints);
}
}
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
pub enum Zone{
Start,
Finish,
Anticheat,
}
#[derive(Clone,Hash,Eq,PartialEq,Ord,PartialOrd)]
pub struct ModeId(u32);
impl ModeId{
pub const MAIN:Self=Self(0);
pub const BONUS:Self=Self(1);
pub const fn id(id:u32)->Self{
Self(id)
}
pub const fn get(&self)->u32{
self.0
}
}
pub struct Mode{
style:gameplay_style::StyleModifiers,
start:ModelId,
zones:HashMap<ModelId,ZoneBehaviour>,
stages:Vec<Stage>,
start:ModelId,//when you press reset you go here
zones:HashMap<ModelId,Zone>,
stages:Vec<Stage>,//when you load the map you go to stages[0].spawn
//mutually exlusive stage element behaviour
elements:HashMap<ModelId,StageElement>,
jump_limit:HashMap<ModelId,u32>,
}
impl Mode{
pub fn new(style:gameplay_style::StyleModifiers,start:ModelId)->Self{
Self{
style,
start,
zones:HashMap::new(),
stages:Vec::new(),
elements:HashMap::new(),
jump_limit:HashMap::new(),
}
}
pub fn push_stage(&mut self,stage:Stage){
self.stages.push(stage)
}
pub fn get_stage_mut(&mut self,stage:StageId)->Option<&mut Stage>{
self.stages.get_mut(stage.0 as usize)
}
pub fn get_spawn_model_id(&self,stage:StageId)->Option<ModelId>{
self.stages.get(stage.0 as usize).map(|s|s.spawn)
}
pub fn get_zone(&self,model_id:ModelId)->Option<&Zone>{
self.zones.get(&model_id)
}
pub fn get_stage(&self,stage_id:StageId)->Option<&Stage>{
self.stages.get(stage_id.0 as usize)
}
pub fn get_element(&self,model_id:ModelId)->Option<&StageElement>{
self.elements.get(&model_id)
}
pub fn get_jump_limit(&self,model_id:ModelId)->Option<u32>{
self.jump_limit.get(&model_id).copied()
}
//TODO: put this in the SNF
pub fn denormalize_data(&mut self){
//expand and index normalized data
self.zones.insert(self.start,Zone::Start);
for (stage_id,stage) in self.stages.iter().enumerate(){
self.elements.insert(stage.spawn,StageElement{
stage:StageId(stage_id as u32),
force:false,
behaviour:StageElementBehaviour::SpawnAt,
});
for &model_id in &stage.ordered_checkpoints{
self.elements.insert(model_id,StageElement{
for (_,&model) in &stage.ordered_checkpoints{
self.elements.insert(model,StageElement{
stage:StageId(stage_id as u32),
force:false,
behaviour:StageElementBehaviour::Checkpoint,
});
}
for &model_id in &stage.unordered_checkpoints{
self.elements.insert(model_id,StageElement{
for &model in &stage.unordered_checkpoints{
self.elements.insert(model,StageElement{
stage:StageId(stage_id as u32),
force:false,
behaviour:StageElementBehaviour::Checkpoint,
@ -72,19 +160,76 @@ impl Mode{
}
}
}
//this would be nice as a macro
#[derive(Default)]
pub struct ModeUpdate{
zones:HashMap<ModelId,Zone>,
stages:HashMap<StageId,StageUpdate>,
//mutually exlusive stage element behaviour
elements:HashMap<ModelId,StageElement>,
jump_limit:HashMap<ModelId,u32>,
}
impl Updatable<ModeUpdate> for Mode{
fn update(&mut self,update:ModeUpdate){
self.zones.extend(update.zones);
for (stage,stage_update) in update.stages{
if let Some(stage)=self.stages.get_mut(stage.0 as usize){
stage.update(stage_update);
}
}
self.elements.extend(update.elements);
self.jump_limit.extend(update.jump_limit);
}
}
impl ModeUpdate{
pub fn zone(model_id:ModelId,zone:Zone)->Self{
let mut mu=Self::default();
mu.zones.insert(model_id,zone);
mu
}
pub fn stage(stage_id:StageId,stage_update:StageUpdate)->Self{
let mut mu=Self::default();
mu.stages.insert(stage_id,stage_update);
mu
}
pub fn element(model_id:ModelId,element:StageElement)->Self{
let mut mu=Self::default();
mu.elements.insert(model_id,element);
mu
}
pub fn jump_limit(model_id:ModelId,jump_limit:u32)->Self{
let mut mu=Self::default();
mu.jump_limit.insert(model_id,jump_limit);
mu
}
}
#[derive(Default)]
pub struct Modes{
modes:Vec<Mode>,
}
impl Modes{
pub fn clear(&mut self){
self.modes.clear();
pub fn new(modes:Vec<Mode>)->Self{
Self{
modes,
}
}
pub fn push_mode(&mut self,mode:Mode){
self.modes.push(mode)
}
pub fn get_mode(&self,mode:ModeId)->Option<&Mode>{
self.modes.get(mode.0 as usize)
}
pub fn insert(&mut self,mode:Mode){
self.modes.push(mode);
}
pub struct ModesUpdate{
modes:HashMap<ModeId,ModeUpdate>,
}
impl Updatable<ModesUpdate> for Modes{
fn update(&mut self,update:ModesUpdate){
for (mode,mode_update) in update.modes{
if let Some(mode)=self.modes.get_mut(mode.0 as usize){
mode.update(mode_update);
}
}
}
}

@ -1,26 +1,28 @@
const VALVE_SCALE:i64=16;
use crate::integer::{Time,Ratio64,Planar64,Planar64Vec3};
pub struct StyleModifiers{
controls_used:u32,//controls which are allowed to pass into gameplay
controls_mask:u32,//controls which are masked from control state (e.g. jump in scroll style)
strafe:Option<StrafeSettings>,
jump_impulse:JumpImpulse,
jump_calculation:JumpCalculation,
static_friction:Planar64,
kinetic_friction:Planar64,
walk_speed:Planar64,
walk_accel:Planar64,
ladder_speed:Planar64,
ladder_accel:Planar64,
ladder_dot:Planar64,
swim_speed:Planar64,
mass:Planar64,
mv:Planar64,
surf_slope:Option<Planar64>,
rocket_force:Option<Planar64>,
gravity:Planar64Vec3,
hitbox:Hitbox,
camera_offset:Planar64Vec3,
pub controls_used:u32,//controls which are allowed to pass into gameplay
pub controls_mask:u32,//controls which are masked from control state (e.g. jump in scroll style)
pub strafe:Option<StrafeSettings>,
pub jump_impulse:JumpImpulse,
pub jump_calculation:JumpCalculation,
pub static_friction:Planar64,
pub kinetic_friction:Planar64,
pub walk_speed:Planar64,
pub walk_accel:Planar64,
pub ladder_speed:Planar64,
pub ladder_accel:Planar64,
pub ladder_dot:Planar64,
pub swim_speed:Planar64,
pub mass:Planar64,
pub mv:Planar64,
pub surf_slope:Option<Planar64>,
pub rocket_force:Option<Planar64>,
pub gravity:Planar64Vec3,
pub hitbox:Hitbox,
pub camera_offset:Planar64Vec3,
}
impl std::default::Default for StyleModifiers{
fn default()->Self{
@ -28,18 +30,18 @@ impl std::default::Default for StyleModifiers{
}
}
impl StyleModifiers{
const CONTROL_MOVEFORWARD:u32=0b00000001;
const CONTROL_MOVEBACK:u32=0b00000010;
const CONTROL_MOVERIGHT:u32=0b00000100;
const CONTROL_MOVELEFT:u32=0b00001000;
const CONTROL_MOVEUP:u32=0b00010000;
const CONTROL_MOVEDOWN:u32=0b00100000;
const CONTROL_JUMP:u32=0b01000000;
const CONTROL_ZOOM:u32=0b10000000;
pub const CONTROL_MOVEFORWARD:u32=0b00000001;
pub const CONTROL_MOVEBACK:u32=0b00000010;
pub const CONTROL_MOVERIGHT:u32=0b00000100;
pub const CONTROL_MOVELEFT:u32=0b00001000;
pub const CONTROL_MOVEUP:u32=0b00010000;
pub const CONTROL_MOVEDOWN:u32=0b00100000;
pub const CONTROL_JUMP:u32=0b01000000;
pub const CONTROL_ZOOM:u32=0b10000000;
const RIGHT_DIR:Planar64Vec3=Planar64Vec3::X;
const UP_DIR:Planar64Vec3=Planar64Vec3::Y;
const FORWARD_DIR:Planar64Vec3=Planar64Vec3::NEG_Z;
pub const RIGHT_DIR:Planar64Vec3=Planar64Vec3::X;
pub const UP_DIR:Planar64Vec3=Planar64Vec3::Y;
pub const FORWARD_DIR:Planar64Vec3=Planar64Vec3::NEG_Z;
fn neo()->Self{
Self{
@ -70,7 +72,7 @@ impl StyleModifiers{
}
}
fn roblox_bhop()->Self{
pub fn roblox_bhop()->Self{
Self{
controls_used:!0,
controls_mask:!0,//&!(Self::CONTROL_MOVEUP|Self::CONTROL_MOVEDOWN),
@ -136,13 +138,13 @@ impl StyleModifiers{
air_accel_limit:Some(Planar64::raw(150<<28)*100),
tick_rate:Ratio64::new(100,Time::ONE_SECOND.nanos() as u64).unwrap(),
}),
jump_impulse:JumpImpulse::FromHeight(Planar64::raw(52<<28)),
jump_impulse:JumpImpulse::FromHeight(Planar64::int(52)/VALVE_SCALE),
jump_calculation:JumpCalculation::Linear,
gravity:Planar64Vec3::raw(0,-800<<28,0),
gravity:Planar64Vec3::int(0,-800,0)/VALVE_SCALE,
static_friction:Planar64::int(2),//?
kinetic_friction:Planar64::int(3),//?
mass:Planar64::int(1),
mv:Planar64::raw(30<<28),
mv:Planar64::raw(30)/VALVE_SCALE,
rocket_force:None,
walk_speed:Planar64::int(18),//?
walk_accel:Planar64::int(90),//?
@ -152,7 +154,7 @@ impl StyleModifiers{
swim_speed:Planar64::int(12),//?
surf_slope:Some(Planar64::raw(3787805118)),// normal.y=0.75
hitbox:Hitbox::source(),
camera_offset:Planar64Vec3::raw(0,(64<<28)-(73<<27),0),
camera_offset:(Planar64Vec3::int(0,64,0)-Planar64Vec3::int(0,73,0)/2)/VALVE_SCALE,
}
}
fn source_surf()->Self{
@ -161,16 +163,16 @@ impl StyleModifiers{
controls_mask:!0,//&!(Self::CONTROL_MOVEUP|Self::CONTROL_MOVEDOWN),
strafe:Some(StrafeSettings{
enable:EnableStrafe::Always,
air_accel_limit:Some(Planar64::raw(150<<28)*66),
air_accel_limit:Some(Planar64::int(150)*66/VALVE_SCALE),
tick_rate:Ratio64::new(66,Time::ONE_SECOND.nanos() as u64).unwrap(),
}),
jump_impulse:JumpImpulse::FromHeight(Planar64::raw(52<<28)),
jump_impulse:JumpImpulse::FromHeight(Planar64::int(52)/VALVE_SCALE),
jump_calculation:JumpCalculation::Linear,
gravity:Planar64Vec3::raw(0,-800<<28,0),
gravity:Planar64Vec3::int(0,-800,0)/VALVE_SCALE,
static_friction:Planar64::int(2),//?
kinetic_friction:Planar64::int(3),//?
mass:Planar64::int(1),
mv:Planar64::raw(30<<28),
mv:Planar64::int(30)/VALVE_SCALE,
rocket_force:None,
walk_speed:Planar64::int(18),//?
walk_accel:Planar64::int(90),//?
@ -180,7 +182,7 @@ impl StyleModifiers{
swim_speed:Planar64::int(12),//?
surf_slope:Some(Planar64::raw(3787805118)),// normal.y=0.75
hitbox:Hitbox::source(),
camera_offset:Planar64Vec3::raw(0,(64<<28)-(73<<27),0),
camera_offset:(Planar64Vec3::int(0,64,0)-Planar64Vec3::int(0,73,0)/2)/VALVE_SCALE,
}
}
fn roblox_rocket()->Self{
@ -209,13 +211,13 @@ impl StyleModifiers{
}
}
enum JumpCalculation{
pub enum JumpCalculation{
Capped,//roblox
Energy,//new
Linear,//source
}
enum JumpImpulse{
pub enum JumpImpulse{
FromTime(Time),//jump time is invariant across mass and gravity changes
FromHeight(Planar64),//jump height is invariant across mass and gravity changes
FromDeltaV(Planar64),//jump velocity is invariant across mass and gravity changes
@ -226,20 +228,32 @@ enum JumpImpulse{
//Energy means it adds energy
//Linear means it linearly adds on
enum EnableStrafe{
pub enum EnableStrafe{
Always,
MaskAny(u32),//hsw, shsw
MaskAll(u32),
//Function(Box<dyn Fn(u32)->bool>),
}
struct StrafeSettings{
pub struct StrafeSettings{
enable:EnableStrafe,
air_accel_limit:Option<Planar64>,
tick_rate:Ratio64,
}
impl StrafeSettings{
pub fn next_tick(&self,time:Time)->Time{
Time::from_nanos(self.tick_rate.rhs_div_int(self.tick_rate.mul_int(time.nanos())+1))
}
pub fn mask(&self,controls:u32)->bool{
match self.enable{
EnableStrafe::Always=>true,
EnableStrafe::MaskAny(mask)=>mask&controls!=0,
EnableStrafe::MaskAll(mask)=>mask&controls==mask,
}
}
}
enum HitboxMesh{
pub enum HitboxMesh{
Box,//source
Cylinder,//roblox
//Sphere,//roblox old physics
@ -248,9 +262,9 @@ enum HitboxMesh{
//DualCone,
}
struct Hitbox{
halfsize:Planar64Vec3,
mesh:HitboxMesh,
pub struct Hitbox{
pub halfsize:Planar64Vec3,
pub mesh:HitboxMesh,
}
impl Hitbox{
fn roblox()->Self{
@ -261,7 +275,7 @@ impl Hitbox{
}
fn source()->Self{
Self{
halfsize:Planar64Vec3::raw(33,73,33)/16/2,
halfsize:Planar64Vec3::raw(33,73,33)/2/VALVE_SCALE,
mesh:HitboxMesh::Box,
}
}

@ -1,8 +1,10 @@
pub mod bvh;
pub mod map;
pub mod aabb;
pub mod model;
pub mod zeroes;
pub mod integer;
pub mod updatable;
pub mod instruction;
pub mod gameplay_modes;
pub mod gameplay_style;

11
src/map.rs Normal file

@ -0,0 +1,11 @@
use crate::model;
use crate::gameplay_modes;
use crate::gameplay_attributes;
//this is the current map data loaded in memory
pub struct Map{
pub modes:gameplay_modes::Modes,
pub models:model::Models,
pub attributes:Vec<gameplay_attributes::CollisionAttributes>,
//RenderPattern
pub textures:Vec<Vec<u8>>,
}

@ -1,5 +1,7 @@
use std::collections::HashMap;
use crate::integer::{Planar64Vec3,Planar64Affine3};
use crate::gameplay_attributes;
use crate::updatable::Updatable;
pub type TextureCoordinate=glam::Vec2;
pub type Color4=glam::Vec4;
@ -30,6 +32,13 @@ pub struct IndexedPhysicsGroup{
pub groups:Vec<GroupId>,
}
//This is a superset of PhysicsModel and GraphicsModel
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
pub struct IndexedModelId(u32);
impl IndexedModelId{
pub const fn id(id:u32)->Self{
Self(id)
}
}
pub struct IndexedModel{
pub unique_pos:Vec<Planar64Vec3>,//Unit32Vec3
pub unique_normal:Vec<Planar64Vec3>,//Unit32Vec3
@ -46,9 +55,36 @@ pub struct IndexedModel{
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
pub struct ModelId(u32);
impl ModelId{
pub const fn id(id:u32)->Self{
Self(id)
}
}
pub struct Model{
pub model:ModelId,
pub model:IndexedModelId,
pub attributes:gameplay_attributes::CollisionAttributesId,
pub color:Color4,//transparency is in here
pub transform:Planar64Affine3,
}
pub struct Models{
indexed_models:HashMap<IndexedModelId,IndexedModel>,
models:HashMap<ModelId,Model>,
}
impl Models{
pub fn new(
indexed_models:HashMap<IndexedModelId,IndexedModel>,
models:HashMap<ModelId,Model>,
)->Self{
Self{
indexed_models,
models,
}
}
}
impl Updatable<Models> for Models{
fn update(&mut self,update:Models){
self.indexed_models.extend(update.indexed_models);
self.models.extend(update.models);
}
}

56
src/updatable.rs Normal file

@ -0,0 +1,56 @@
pub trait Updatable<Updater>{
fn update(&mut self,update:Updater);
}
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
struct InnerId(u32);
#[derive(Clone)]
struct Inner{
id:InnerId,
enabled:bool,
}
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
struct OuterId(u32);
struct Outer{
id:OuterId,
inners:std::collections::HashMap<InnerId,Inner>,
}
enum Update<I,U>{
Insert(I),
Update(U),
Remove
}
struct InnerUpdate{
//#[updatable(Update)]
enabled:Option<bool>,
}
struct OuterUpdate{
//#[updatable(Insert,Update,Remove)]
inners:std::collections::HashMap<InnerId,Update<Inner,InnerUpdate>>,
//#[updatable(Update)]
//inners:std::collections::HashMap<InnerId,InnerUpdate>,
}
impl Updatable<InnerUpdate> for Inner{
fn update(&mut self,update:InnerUpdate){
if let Some(enabled)=update.enabled{
self.enabled=enabled;
}
}
}
impl Updatable<OuterUpdate> for Outer{
fn update(&mut self,update:OuterUpdate){
for (id,up) in update.inners{
match up{
Update::Insert(new_inner)=>self.inners.insert(id,new_inner),
Update::Update(inner_update)=>self.inners.get_mut(&id).map(|inner|{
let old=inner.clone();
inner.update(inner_update);
old
}),
Update::Remove=>self.inners.remove(&id),
};
}
}
}
//*/