#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WaveForm {
#[default]
Particle,
Humanoid,
Vehicle,
Fluid,
}
impl WaveForm {
pub fn label(self) -> &'static str {
match self {
Self::Particle => "Particle",
Self::Humanoid => "Humanoid",
Self::Vehicle => "Vehicle",
Self::Fluid => "Fluid",
}
}
}
#[derive(Debug, Clone)]
pub struct DecoherenceField {
pub position: [f32; 3],
pub radius: f32,
pub target_form: WaveForm,
pub gamma: f32,
pub animation_clip: String,
pub source_tag: String,
}
impl DecoherenceField {
#[inline]
pub fn effective_gamma(&self, observer_pos: &[f32; 3]) -> f32 {
let dx = observer_pos[0] - self.position[0];
let dy = observer_pos[1] - self.position[1];
let dz = observer_pos[2] - self.position[2];
let dist = (dx * dx + dy * dy + dz * dz).sqrt();
if dist >= self.radius {
return 0.0;
}
self.gamma * (1.0 - dist / self.radius)
}
}
#[derive(Debug, Clone)]
pub struct DecoherenceState {
pub coherence: f32,
pub time_in_field: f32,
pub active_gamma: f32,
pub current_form: WaveForm,
pub target_form: Option<WaveForm>,
pub recoherence_rate: f32,
pub in_field: bool,
pub active_clip: String,
}
impl Default for DecoherenceState {
fn default() -> Self {
Self {
coherence: 1.0,
time_in_field: 0.0,
active_gamma: 0.0,
current_form: WaveForm::Particle,
target_form: None,
recoherence_rate: 2.0,
in_field: false,
active_clip: String::new(),
}
}
}
pub struct DreamWaveController {
pub particle: super::particle::ParticleController,
pub decoherence: DecoherenceState,
pub quantum: QuantumLocomotionState,
pub bone_targets: Vec<[f32; 3]>,
pub particle_weights: Vec<f32>,
#[allow(dead_code)]
active_fields: Vec<DecoherenceField>,
#[allow(dead_code)]
field_scratch: Vec<DecoherenceField>,
}
impl DreamWaveController {
pub fn new(position: [f32; 3], particle_count: u32) -> Self {
Self {
particle: super::particle::ParticleController::new(position, particle_count),
decoherence: DecoherenceState::default(),
quantum: QuantumLocomotionState::new(particle_count),
bone_targets: vec![[0.0; 3]; particle_count as usize],
particle_weights: vec![1.0; particle_count as usize],
active_fields: Vec::with_capacity(4),
field_scratch: Vec::with_capacity(8),
}
}
pub fn update(
&mut self,
forward: bool,
back: bool,
left: bool,
right: bool,
jump: bool,
sprint: bool,
camera_yaw: f32,
dt: f32,
fields: &[DecoherenceField],
) -> bool {
let moved = self
.particle
.update(forward, back, left, right, jump, sprint, camera_yaw, dt);
let moving = forward || back || left || right;
let input_bias = [
if !moving { 2.0 } else { 0.0 }, if moving && !sprint { 2.0 } else { 0.0 }, if jump { 3.0 } else { 0.0 }, if self.particle.landing_timer > 0.0 { 2.0 } else { 0.0 }, if sprint && moving { 2.0 } else { 0.0 }, ];
let epsilon = if self.particle.grounded { 0.3 * dt } else { 0.05 * dt };
let _measured_mode = self.quantum.tick(dt, &input_bias, epsilon);
let pos = &self.particle.position;
let mut strongest_gamma = 0.0f32;
let mut strongest_field: Option<&DecoherenceField> = None;
for field in fields {
let g = field.effective_gamma(pos);
if g > strongest_gamma {
strongest_gamma = g;
strongest_field = Some(field);
}
}
if let Some(field) = strongest_field {
self.decoherence.in_field = true;
self.decoherence.active_gamma = strongest_gamma;
let modulated_gamma = if sprint { strongest_gamma * 2.0 } else { strongest_gamma };
self.decoherence.time_in_field += dt;
self.decoherence.coherence = (-modulated_gamma * self.decoherence.time_in_field).exp();
if self.decoherence.target_form.is_none() || self.decoherence.target_form != Some(field.target_form) {
self.decoherence.target_form = Some(field.target_form);
self.decoherence.active_clip = field.animation_clip.clone();
}
if self.decoherence.coherence < 0.01 {
self.decoherence.coherence = 0.0;
self.decoherence.current_form = field.target_form;
}
} else {
self.decoherence.in_field = false;
self.decoherence.time_in_field = 0.0;
if self.decoherence.coherence < 1.0 {
let rate = self.decoherence.recoherence_rate;
self.decoherence.coherence += (1.0 - self.decoherence.coherence) * (1.0 - (-rate * dt).exp());
if self.decoherence.coherence > 0.99 {
self.decoherence.coherence = 1.0;
self.decoherence.current_form = WaveForm::Particle;
self.decoherence.target_form = None;
self.decoherence.active_clip.clear();
}
}
}
let w = self.decoherence.coherence;
for pw in &mut self.particle_weights {
*pw = w;
}
moved
}
pub fn coherence(&self) -> f32 {
self.decoherence.coherence
}
pub fn current_form(&self) -> WaveForm {
self.decoherence.current_form
}
pub fn is_decohering(&self) -> bool {
self.decoherence.in_field && self.decoherence.coherence > 0.01
}
pub fn is_collapsed(&self) -> bool {
self.decoherence.coherence < 0.01
}
pub fn is_recohering(&self) -> bool {
!self.decoherence.in_field && self.decoherence.coherence < 0.99
}
pub fn set_bone_targets(&mut self, bones: &[[f32; 3]]) {
let particle_count = self.particle.particle_count as usize;
let offsets = super::particle::particle_sphere_offsets(self.particle.particle_count);
let params = self.particle.shape_params();
let radius = self.particle.cloud_radius * params.radius_scale;
for i in 0..particle_count.min(self.bone_targets.len()) {
if i >= offsets.len() {
break;
}
let px = self.particle.position[0] + offsets[i][0] * params.stretch[0] * radius;
let py = self.particle.position[1] + offsets[i][1] * params.stretch[1] * radius;
let pz = self.particle.position[2] + offsets[i][2] * params.stretch[2] * radius;
let mut best_dist_sq = f32::MAX;
let mut best_bone = [0.0f32; 3];
for bone in bones {
let dx = px - bone[0];
let dy = py - bone[1];
let dz = pz - bone[2];
let d2 = dx * dx + dy * dy + dz * dz;
if d2 < best_dist_sq {
best_dist_sq = d2;
best_bone = *bone;
}
}
self.bone_targets[i] = best_bone;
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Complex {
pub re: f32,
pub im: f32,
}
impl Complex {
pub const ZERO: Self = Self { re: 0.0, im: 0.0 };
pub const ONE: Self = Self { re: 1.0, im: 0.0 };
pub fn new(re: f32, im: f32) -> Self {
Self { re, im }
}
pub fn from_real(r: f32) -> Self {
Self { re: r, im: 0.0 }
}
pub fn conj(self) -> Self {
Self {
re: self.re,
im: -self.im,
}
}
pub fn norm_sq(self) -> f32 {
self.re * self.re + self.im * self.im
}
pub fn norm(self) -> f32 {
self.norm_sq().sqrt()
}
pub fn mul(self, other: Self) -> Self {
Self {
re: self.re * other.re - self.im * other.im,
im: self.re * other.im + self.im * other.re,
}
}
pub fn add(self, other: Self) -> Self {
Self {
re: self.re + other.re,
im: self.im + other.im,
}
}
pub fn sub(self, other: Self) -> Self {
Self {
re: self.re - other.re,
im: self.im - other.im,
}
}
pub fn scale(self, s: f32) -> Self {
Self {
re: self.re * s,
im: self.im * s,
}
}
#[allow(dead_code)]
pub fn exp_i(theta: f32) -> Self {
Self {
re: theta.cos(),
im: theta.sin(),
}
}
}
pub struct DensityMatrix5 {
pub entries: [Complex; 25],
}
impl DensityMatrix5 {
pub fn pure_state(k: usize) -> Self {
let mut entries = [Complex::ZERO; 25];
if k < 5 {
entries[k * 5 + k] = Complex::ONE;
}
Self { entries }
}
pub fn from_state_vector(amplitudes: &[Complex; 5]) -> Self {
let mut entries = [Complex::ZERO; 25];
for i in 0..5 {
for j in 0..5 {
entries[i * 5 + j] = amplitudes[i].mul(amplitudes[j].conj());
}
}
Self { entries }
}
pub fn trace(&self) -> f32 {
(0..5).map(|k| self.entries[k * 5 + k].re).sum()
}
pub fn populations(&self) -> [f32; 5] {
let mut p = [0.0f32; 5];
for k in 0..5 {
p[k] = self.entries[k * 5 + k].re;
}
p
}
pub fn coherence_magnitude(&self) -> f32 {
let mut c = 0.0f32;
for i in 0..5 {
for j in (i + 1)..5 {
c += self.entries[i * 5 + j].norm();
}
}
c
}
pub fn dephased(&self) -> Self {
let mut result = Self {
entries: [Complex::ZERO; 25],
};
for k in 0..5 {
result.entries[k * 5 + k] = self.entries[k * 5 + k];
}
result
}
pub fn apply_dephasing(&mut self, epsilon: f32) {
let retain = 1.0 - epsilon.clamp(0.0, 1.0);
for i in 0..5 {
for j in 0..5 {
if i != j {
self.entries[i * 5 + j] = self.entries[i * 5 + j].scale(retain);
}
}
}
}
pub fn apply_unitary(&mut self, u: &[Complex; 25]) {
let mut u_rho = [Complex::ZERO; 25];
for i in 0..5 {
for j in 0..5 {
let mut sum = Complex::ZERO;
for k in 0..5 {
sum = sum.add(u[i * 5 + k].mul(self.entries[k * 5 + j]));
}
u_rho[i * 5 + j] = sum;
}
}
for i in 0..5 {
for j in 0..5 {
let mut sum = Complex::ZERO;
for k in 0..5 {
sum = sum.add(u_rho[i * 5 + k].mul(u[j * 5 + k].conj()));
}
self.entries[i * 5 + j] = sum;
}
}
}
pub fn measure(&self, seed: u64) -> usize {
let pops = self.populations();
let mut s = seed;
s = s.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
let r = (s >> 33) as f32 / (1u64 << 31) as f32; let mut cumulative = 0.0;
for (i, &p) in pops.iter().enumerate() {
cumulative += p;
if r < cumulative {
return i;
}
}
4 }
}
pub struct LocomotionHamiltonian {
pub hbar_eff: f32,
pub bias: [f32; 5],
pub couplings: [f32; 5],
}
impl Default for LocomotionHamiltonian {
fn default() -> Self {
Self {
hbar_eff: 1.0,
bias: [0.0, 0.2, 0.5, 0.3, 0.4], couplings: [1.0, 0.8, 0.6, 0.9, 0.7], }
}
}
impl LocomotionHamiltonian {
pub fn build_matrix(&self, input_bias: &[f32; 5], phase_time: f32) -> [Complex; 25] {
let mut h = [Complex::ZERO; 25];
for k in 0..5 {
h[k * 5 + k] = Complex::from_real(self.bias[k] + input_bias[k] + 0.1 * (k as f32 * phase_time).sin());
}
let pairs: [(usize, usize); 5] = [(0, 1), (1, 4), (1, 2), (2, 3), (3, 0)];
for (idx, &(i, j)) in pairs.iter().enumerate() {
let w = Complex::from_real(self.couplings[idx]);
h[i * 5 + j] = w;
h[j * 5 + i] = w.conj(); }
h
}
pub fn compute_unitary(&self, h: &[Complex; 25], dt: f32) -> [Complex; 25] {
let scale = dt / self.hbar_eff;
let mut u = [Complex::ZERO; 25];
for k in 0..5 {
u[k * 5 + k] = Complex::ONE;
}
for i in 0..5 {
for j in 0..5 {
let ih = Complex::new(h[i * 5 + j].im * scale, -h[i * 5 + j].re * scale);
u[i * 5 + j] = u[i * 5 + j].sub(ih);
}
}
let s2 = scale * scale * 0.5;
for i in 0..5 {
for j in 0..5 {
let mut h2_ij = Complex::ZERO;
for k in 0..5 {
h2_ij = h2_ij.add(h[i * 5 + k].mul(h[k * 5 + j]));
}
u[i * 5 + j] = u[i * 5 + j].sub(h2_ij.scale(s2));
}
}
u
}
}
pub struct InterferenceKernels {
pub kernels: Vec<[[Complex; 3]; 10]>,
}
impl InterferenceKernels {
pub fn generate(particle_count: u32) -> Self {
let n = particle_count as usize;
let mut kernels = Vec::with_capacity(n);
let golden = (1.0 + 5.0f32.sqrt()) / 2.0;
for p in 0..n {
let t = p as f32 / n.max(1) as f32;
let mut pair_kernels = [[Complex::ZERO; 3]; 10];
pair_kernels[0] = [
Complex::new(0.3 * (t * 12.0).sin(), 0.1 * (t * 8.0).cos()),
Complex::new(0.0, 0.15 * (t * 10.0).sin()),
Complex::new(0.2 * (t * 6.0).cos(), 0.0),
];
pair_kernels[1] = [
Complex::new(0.0, 0.1 * (t * 4.0).sin()),
Complex::new(0.4 * (t * 8.0).sin(), 0.2 * (t * golden * 5.0).cos()),
Complex::new(0.0, 0.1 * (t * 4.0).cos()),
];
pair_kernels[2] = [
Complex::new(0.15 * (t * 16.0).sin(), 0.0),
Complex::new(-0.3 * (t * 12.0).cos(), 0.1),
Complex::new(0.15 * (t * 16.0).cos(), 0.0),
];
pair_kernels[3] = [
Complex::new(0.1, 0.2 * (t * 20.0).sin()),
Complex::new(0.0, 0.05),
Complex::new(0.35 * (t * 14.0).sin(), 0.15 * (t * 7.0).cos()),
];
pair_kernels[4] = [
Complex::new(0.1 * (t * 6.0).cos(), 0.0),
Complex::new(0.25 * (t * 10.0).sin(), 0.2 * (t * 5.0).cos()),
Complex::new(0.15 * (t * 8.0).sin(), 0.0),
];
pair_kernels[5] = [
Complex::new(0.2 * (t * 14.0).sin(), 0.1 * (t * 7.0).cos()),
Complex::new(-0.15 * (t * 18.0).cos(), 0.0),
Complex::new(0.2 * (t * 14.0).cos(), -0.1 * (t * 7.0).sin()),
];
pair_kernels[6] = [
Complex::new(0.05, 0.1 * (t * 22.0).sin()),
Complex::new(0.0, 0.05),
Complex::new(0.3 * (t * 18.0).sin(), 0.2 * (t * 9.0).cos()),
];
pair_kernels[7] = [
Complex::new(0.1 * (t * 8.0).cos(), 0.0),
Complex::new(0.35 * (t * 16.0).sin(), -0.15 * (t * 8.0).cos()),
Complex::new(0.1 * (t * 8.0).sin(), 0.0),
];
pair_kernels[8] = [
Complex::new(0.15 * (t * 10.0).sin(), 0.1),
Complex::new(0.2 * (t * 12.0).cos(), 0.15 * (t * 6.0).sin()),
Complex::new(0.25 * (t * 15.0).sin(), 0.0),
];
pair_kernels[9] = [
Complex::new(0.2 * (t * 20.0).sin(), 0.1 * (t * 10.0).cos()),
Complex::new(-0.1 * (t * 24.0).cos(), 0.05),
Complex::new(0.15 * (t * 16.0).cos(), 0.15 * (t * 8.0).sin()),
];
kernels.push(pair_kernels);
}
Self { kernels }
}
pub fn interference_displacement(&self, n: usize, rho: &DensityMatrix5) -> [f32; 3] {
if n >= self.kernels.len() {
return [0.0; 3];
}
let k = &self.kernels[n];
let mut disp = [0.0f32; 3];
let pairs: [(usize, usize); 10] = [
(0, 1),
(0, 2),
(0, 3),
(0, 4),
(1, 2),
(1, 3),
(1, 4),
(2, 3),
(2, 4),
(3, 4),
];
for (idx, &(i, j)) in pairs.iter().enumerate() {
let rho_ij = rho.entries[i * 5 + j];
for axis in 0..3 {
let product = k[idx][axis].mul(rho_ij);
disp[axis] += 2.0 * product.re;
}
}
disp
}
}
pub struct QuantumLocomotionState {
pub rho: DensityMatrix5,
pub hamiltonian: LocomotionHamiltonian,
pub kernels: InterferenceKernels,
pub phase_time: f32,
pub frame: u64,
}
impl QuantumLocomotionState {
pub fn new(particle_count: u32) -> Self {
Self {
rho: DensityMatrix5::pure_state(0),
hamiltonian: LocomotionHamiltonian::default(),
kernels: InterferenceKernels::generate(particle_count),
phase_time: 0.0,
frame: 0,
}
}
pub fn tick(&mut self, dt: f32, input_bias: &[f32; 5], decoherence_epsilon: f32) -> usize {
self.phase_time += dt;
self.frame += 1;
let h = self.hamiltonian.build_matrix(input_bias, self.phase_time);
let u = self.hamiltonian.compute_unitary(&h, dt);
self.rho.apply_unitary(&u);
self.rho.apply_dephasing(decoherence_epsilon);
self.rho.measure(self.frame)
}
pub fn particle_position_with_interference(
&self,
n: usize,
root: [f32; 3],
mode_templates: &[[f32; 3]; 5],
) -> [f32; 3] {
let pops = self.rho.populations();
let interference = self.kernels.interference_displacement(n, &self.rho);
let mut pos = root;
for k in 0..5 {
pos[0] += pops[k] * mode_templates[k][0];
pos[1] += pops[k] * mode_templates[k][1];
pos[2] += pops[k] * mode_templates[k][2];
}
pos[0] += interference[0];
pos[1] += interference[1];
pos[2] += interference[2];
pos
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum DecoherenceTagValue {
Form(WaveForm, String),
Gamma(f32),
}
pub fn parse_decoherence_tag(tag: &str) -> Option<DecoherenceTagValue> {
if let Some(rest) = tag.strip_prefix("decohere:") {
let mut parts = rest.splitn(2, ':');
let form_str = parts.next()?;
let form = match form_str {
"humanoid" => WaveForm::Humanoid,
"vehicle" => WaveForm::Vehicle,
"fluid" => WaveForm::Fluid,
"particle" => WaveForm::Particle,
_ => return None,
};
let clip = parts.next().unwrap_or("").to_string();
Some(DecoherenceTagValue::Form(form, clip))
} else if let Some(rest) = tag.strip_prefix("gamma:") {
let rate: f32 = rest.parse().ok()?;
Some(DecoherenceTagValue::Gamma(rate))
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wave_form_default_is_particle() {
assert_eq!(WaveForm::default(), WaveForm::Particle);
}
#[test]
fn decoherence_field_outside_radius_zero() {
let field = DecoherenceField {
position: [0.0, 0.0, 0.0],
radius: 5.0,
target_form: WaveForm::Humanoid,
gamma: 4.0,
animation_clip: String::new(),
source_tag: String::new(),
};
let g = field.effective_gamma(&[10.0, 0.0, 0.0]);
assert_eq!(g, 0.0, "gamma should be 0 outside field radius");
}
#[test]
fn decoherence_field_at_center_full_gamma() {
let field = DecoherenceField {
position: [5.0, 0.0, 5.0],
radius: 10.0,
target_form: WaveForm::Humanoid,
gamma: 8.0,
animation_clip: String::new(),
source_tag: String::new(),
};
let g = field.effective_gamma(&[5.0, 0.0, 5.0]);
assert!(
(g - 8.0).abs() < 1e-5,
"gamma at center should equal field gamma (8.0), got {}",
g
);
}
#[test]
fn decoherence_field_at_edge_near_zero() {
let field = DecoherenceField {
position: [0.0, 0.0, 0.0],
radius: 10.0,
target_form: WaveForm::Fluid,
gamma: 4.0,
animation_clip: String::new(),
source_tag: String::new(),
};
let g = field.effective_gamma(&[9.99, 0.0, 0.0]);
assert!(g < 0.01, "gamma near the edge should be near zero, got {}", g);
assert!(g > 0.0, "gamma just inside edge should be >0, got {}", g);
}
#[test]
fn dream_wave_controller_starts_coherent() {
let ctrl = DreamWaveController::new([0.0, 1.0, 0.0], 64);
assert!(
(ctrl.coherence() - 1.0).abs() < 1e-5,
"new controller should start fully coherent (1.0), got {}",
ctrl.coherence()
);
assert_eq!(ctrl.current_form(), WaveForm::Particle);
assert!(!ctrl.is_decohering());
assert!(!ctrl.is_collapsed());
assert!(!ctrl.is_recohering());
}
#[test]
fn dream_wave_controller_decoheres_in_field() {
let mut ctrl = DreamWaveController::new([0.0, 1.0, 0.0], 64);
let fields = vec![DecoherenceField {
position: [0.0, 1.0, 0.0],
radius: 10.0,
target_form: WaveForm::Humanoid,
gamma: 4.0,
animation_clip: "idle".into(),
source_tag: "decohere:humanoid".into(),
}];
for _ in 0..60 {
ctrl.update(false, false, false, false, false, false, 0.0, 0.016, &fields);
}
assert!(
ctrl.coherence() < 1.0,
"coherence should decrease inside a field, got {}",
ctrl.coherence()
);
assert!(ctrl.decoherence.in_field, "should be marked as in_field");
assert_eq!(
ctrl.decoherence.target_form,
Some(WaveForm::Humanoid),
"target form should be Humanoid"
);
}
#[test]
fn dream_wave_controller_recoheres_outside_field() {
let mut ctrl = DreamWaveController::new([0.0, 1.0, 0.0], 64);
let fields = vec![DecoherenceField {
position: [0.0, 1.0, 0.0],
radius: 10.0,
target_form: WaveForm::Humanoid,
gamma: 8.0,
animation_clip: String::new(),
source_tag: String::new(),
}];
for _ in 0..120 {
ctrl.update(false, false, false, false, false, false, 0.0, 0.016, &fields);
}
let coherence_in_field = ctrl.coherence();
assert!(
coherence_in_field < 0.5,
"should be significantly decohered after 120 frames at gamma=8, got {}",
coherence_in_field
);
let no_fields: Vec<DecoherenceField> = vec![];
for _ in 0..300 {
ctrl.update(false, false, false, false, false, false, 0.0, 0.016, &no_fields);
}
assert!(
ctrl.coherence() > coherence_in_field,
"coherence should increase after leaving field: was {}, now {}",
coherence_in_field,
ctrl.coherence()
);
assert!(!ctrl.decoherence.in_field, "should not be in_field after leaving");
}
#[test]
fn parse_decoherence_tag_humanoid() {
let result = parse_decoherence_tag("decohere:humanoid:climb_stairs");
assert_eq!(
result,
Some(DecoherenceTagValue::Form(WaveForm::Humanoid, "climb_stairs".into()))
);
}
#[test]
fn parse_decoherence_tag_gamma() {
let result = parse_decoherence_tag("gamma:4.0");
assert_eq!(result, Some(DecoherenceTagValue::Gamma(4.0)));
}
#[test]
fn parse_decoherence_tag_fluid_no_clip() {
let result = parse_decoherence_tag("decohere:fluid");
assert_eq!(result, Some(DecoherenceTagValue::Form(WaveForm::Fluid, String::new())));
}
#[test]
fn parse_decoherence_tag_unknown_form_returns_none() {
assert_eq!(parse_decoherence_tag("decohere:dragon"), None);
}
#[test]
fn parse_decoherence_tag_unrelated_returns_none() {
assert_eq!(parse_decoherence_tag("some_other_tag"), None);
}
#[test]
fn parse_decoherence_tag_gamma_invalid_returns_none() {
assert_eq!(parse_decoherence_tag("gamma:notanumber"), None);
}
#[test]
fn wave_form_labels() {
assert_eq!(WaveForm::Particle.label(), "Particle");
assert_eq!(WaveForm::Humanoid.label(), "Humanoid");
assert_eq!(WaveForm::Vehicle.label(), "Vehicle");
assert_eq!(WaveForm::Fluid.label(), "Fluid");
}
#[test]
fn dream_wave_controller_fully_collapses() {
let mut ctrl = DreamWaveController::new([0.0, 1.0, 0.0], 32);
let fields = vec![DecoherenceField {
position: [0.0, 1.0, 0.0],
radius: 10.0,
target_form: WaveForm::Vehicle,
gamma: 20.0, animation_clip: String::new(),
source_tag: String::new(),
}];
for _ in 0..200 {
ctrl.update(false, false, false, false, false, false, 0.0, 0.016, &fields);
}
assert!(ctrl.is_collapsed(), "should be fully collapsed");
assert_eq!(ctrl.current_form(), WaveForm::Vehicle);
assert_eq!(ctrl.coherence(), 0.0);
}
#[test]
fn dream_wave_controller_particle_weights_match_coherence() {
let mut ctrl = DreamWaveController::new([0.0, 1.0, 0.0], 16);
let fields = vec![DecoherenceField {
position: [0.0, 1.0, 0.0],
radius: 10.0,
target_form: WaveForm::Humanoid,
gamma: 4.0,
animation_clip: String::new(),
source_tag: String::new(),
}];
ctrl.update(false, false, false, false, false, false, 0.0, 0.016, &fields);
let c = ctrl.coherence();
for &w in &ctrl.particle_weights {
assert!(
(w - c).abs() < 1e-5,
"particle weight {} should match coherence {}",
w,
c
);
}
}
#[test]
fn ablation_interference_changes_output() {
let mut state = QuantumLocomotionState::new(64);
let sq2 = 1.0 / 2.0f32.sqrt();
state.rho = DensityMatrix5::from_state_vector(&[
Complex::new(sq2, 0.0), Complex::new(sq2, 0.0), Complex::ZERO,
Complex::ZERO,
Complex::ZERO,
]);
let root = [0.0; 3];
let templates: [[f32; 3]; 5] = [
[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [0.0, -0.5, 0.0], [0.0, 0.0, 2.0], ];
let pos_quantum = state.particle_position_with_interference(0, root, &templates);
let dephased = state.rho.dephased();
let old_rho_entries = state.rho.entries;
state.rho = dephased;
let pos_dephased = state.particle_position_with_interference(0, root, &templates);
state.rho.entries = old_rho_entries;
let diff_sq = (pos_quantum[0] - pos_dephased[0]).powi(2)
+ (pos_quantum[1] - pos_dephased[1]).powi(2)
+ (pos_quantum[2] - pos_dephased[2]).powi(2);
assert!(
diff_sq > 1e-6,
"QUANTUM.md S7 FAILED: interference does not affect output. diff_sq={diff_sq}"
);
}
#[test]
fn density_matrix_trace_one() {
let rho = DensityMatrix5::pure_state(2);
assert!((rho.trace() - 1.0).abs() < 1e-6);
}
#[test]
fn density_matrix_from_state_vector_trace() {
let sq2 = 1.0 / 2.0f32.sqrt();
let rho = DensityMatrix5::from_state_vector(&[
Complex::new(sq2, 0.0),
Complex::new(sq2, 0.0),
Complex::ZERO,
Complex::ZERO,
Complex::ZERO,
]);
assert!((rho.trace() - 1.0).abs() < 1e-6);
assert!(rho.coherence_magnitude() > 0.1);
}
#[test]
fn dephasing_removes_coherence() {
let sq2 = 1.0 / 2.0f32.sqrt();
let rho = DensityMatrix5::from_state_vector(&[
Complex::new(sq2, 0.0),
Complex::new(sq2, 0.0),
Complex::ZERO,
Complex::ZERO,
Complex::ZERO,
]);
let dephased = rho.dephased();
assert!(dephased.coherence_magnitude() < 1e-6);
assert!((dephased.trace() - 1.0).abs() < 1e-6);
}
#[test]
fn hamiltonian_evolution_preserves_trace() {
let mut state = QuantumLocomotionState::new(16);
let input = [1.0, 0.5, 0.0, 0.0, 0.0];
state.tick(1.0 / 60.0, &input, 0.0);
assert!(
(state.rho.trace() - 1.0).abs() < 0.1,
"Trace drifted: {}",
state.rho.trace()
);
}
#[test]
fn born_rule_measurement_valid() {
let rho = DensityMatrix5::pure_state(2); for seed in 0..100 {
assert_eq!(rho.measure(seed), 2);
}
}
#[test]
fn quantum_locomotion_full_tick_cycle() {
let mut state = QuantumLocomotionState::new(32);
for _ in 0..120 {
state.tick(1.0 / 60.0, &[0.0, 2.0, 0.0, 0.0, 0.0], 0.02);
}
let pops = state.rho.populations();
assert!(pops[1] > 0.1, "Moving population should be significant: {:?}", pops);
let trace: f32 = pops.iter().sum();
assert!((trace - 1.0).abs() < 0.2, "Trace should be near 1.0: {}", trace);
}
}