nmarshall's picture
gameplay: compose registered defuse feedback
3736d96
Raw History Blame Contribute Delete
5.09 kB
//! Recovered finite-domain gameplay contracts; no engine or rendering dependency.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Motion {
pub velocity: [f32; 3],
pub timer: i32,
pub flags: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ImpulseError {
NonFinite,
TimerOutOfRange,
}
/// QVM213009: direction is supplied, not normalized here. Preserve f32 order.
/// Active paths reject nonfinite values/results and unsupported CVFI domains.
pub fn impulse(
has_client: bool,
direction: [f32; 3],
scale: f32,
knockback: f32,
state: Motion,
) -> Result<Motion, ImpulseError> {
if !has_client || knockback == 0.0 {
return Ok(state);
}
if !direction
.iter()
.chain(state.velocity.iter())
.all(|v| v.is_finite())
|| !scale.is_finite()
|| !knockback.is_finite()
{
return Err(ImpulseError::NonFinite);
}
let coefficient = (scale * knockback) / 200.0;
let mut next = state;
for (velocity, component) in next.velocity.iter_mut().zip(direction) {
*velocity += component * coefficient;
}
if !next.velocity.iter().all(|v| v.is_finite()) {
return Err(ImpulseError::NonFinite);
}
if state.timer == 0 {
let timer = knockback * 2.0;
if !timer.is_finite()
|| (timer as f64) < (i32::MIN as f64)
|| (timer as f64) > (i32::MAX as f64)
{
return Err(ImpulseError::TimerOutOfRange);
}
next.timer = (timer as i32).clamp(50, 200);
next.flags |= 64;
}
Ok(next)
}
#[cfg(test)]
mod tests {
use super::*;
fn state() -> Motion {
Motion {
velocity: [1., -2., 3.],
timer: 0,
flags: 8,
}
}
#[test]
fn zero_and_no_client_preserve_state() {
assert_eq!(
impulse(true, [1.; 3], 1000., -0., state()).unwrap(),
state()
);
assert_eq!(
impulse(false, [f32::NAN; 3], f32::NAN, f32::NAN, state()).unwrap(),
state()
);
}
#[test]
fn source_order_fixture() {
let r = impulse(true, [0.6, -0.8, 0.25], 1000., 25., state()).unwrap();
assert_eq!(
r,
Motion {
velocity: [76., -102., 34.25],
timer: 50,
flags: 72
}
);
}
#[test]
fn timer_bounds() {
assert_eq!(impulse(true, [0.; 3], 0., 0.25, state()).unwrap().timer, 50);
assert_eq!(
impulse(true, [0.; 3], 0., 101., state()).unwrap().timer,
200
);
}
#[test]
fn existing_timer_keeps_flags_but_changes_velocity() {
let s = Motion {
timer: -1,
..state()
};
let r = impulse(true, [1., 0., 0.], 1000., 25., s).unwrap();
assert_eq!(r.timer, -1);
assert_eq!(r.flags, 8);
assert_eq!(r.velocity, [126., -2., 3.]);
}
#[test]
fn negative_knockback_is_not_clamped_to_zero() {
let r = impulse(true, [0., 1., 0.], 1000., -2., state()).unwrap();
assert_eq!(r.velocity, [1., -12., 3.]);
assert_eq!(r.timer, 50);
}
#[test]
fn existing_flag_bits_are_preserved() {
let r = impulse(
true,
[0.; 3],
0.,
1.,
Motion {
flags: 0x80000008,
..state()
},
)
.unwrap();
assert_eq!(r.flags, 0x80000048);
}
#[test]
fn active_nonfinite_and_overflow_are_rejected() {
assert_eq!(
impulse(true, [f32::NAN; 3], 1., 1., state()),
Err(ImpulseError::NonFinite)
);
assert_eq!(
impulse(true, [f32::MAX; 3], f32::MAX, 100., state()),
Err(ImpulseError::NonFinite)
);
}
#[test]
fn unsupported_integer_conversion_is_rejected() {
assert_eq!(
impulse(true, [0.; 3], 0., f32::MAX, state()),
Err(ImpulseError::TimerOutOfRange)
);
}
}
pub mod intercept_animation;
pub mod leaderboard;
pub mod method_stats;
pub mod mode10;
pub mod protected;
pub mod ranking;
pub mod recent_awards;
pub mod recipient_payload;
pub mod recipient_wire;
pub mod record_clear;
pub mod score;
pub mod stat_totals;
pub mod rank_sort;
pub mod ranking_population;
pub mod scoreboard;
pub mod scoreboard_wire;
pub mod qvm_format;
pub mod exit_prefix;
pub mod exit_rules;
pub mod intermission;
pub mod transition;
pub mod camera_world;
pub mod map_commands;
pub mod native_policy;
pub mod filter_lifecycle;
pub mod filter_loading;
pub mod filter_commands;
pub mod client_timer;
pub mod client_processing;
pub mod client_move_context;
pub mod move_callbacks;
pub mod assistance;
pub mod defuse;
pub mod effect_feedback;
pub mod location_feedback;
pub mod spatial_index;
pub mod area_portals;
pub mod world_visibility;
pub mod entity_links;
pub mod server_entities;
pub mod server_client_events;
pub mod server_defuse_frame;