test + cleanup agent direction code

This commit is contained in:
Simon Gardling 2025-03-27 23:47:46 -04:00
parent 8dd01ab105
commit a60847ad6f
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 22 additions and 11 deletions

View File

@ -67,20 +67,19 @@ impl Agent {
let right = buf.get_buf(xr, yr);
// Rotate and move logic
let mut rng = rand::thread_rng();
let mut direction: f32 = 0.0;
if (center > left) && (center > right) {
direction = 0.0;
let direction = if (center > left) && (center > right) {
0.0
} else if (center < left) && (center < right) {
direction = *[-1.0, 1.0]
.choose(&mut rng)
.expect("unable to choose random direction");
*[-1.0, 1.0]
.choose(&mut rand::thread_rng())
.expect("unable to choose random direction")
} else if left < right {
direction = 1.0;
1.0
} else if right < left {
direction = -1.0;
}
-1.0
} else {
0.0
};
let delta_angle = rotation_angle * direction;

View File

@ -11,3 +11,15 @@ pub const fn index(width: usize, height: usize, x: f32, y: f32) -> usize {
let j = (y + height as f32) as usize & (height - 1);
j * width + i
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn wrap_test() {
assert_eq!(wrap(1.1, 1.0), 0.100000024);
assert_eq!(wrap(0.5, 1.0), 0.5);
assert_eq!(wrap(-1.0, 2.0), 1.0);
}
}