tick: simplify parameter passing

This commit is contained in:
Simon Gardling 2025-03-28 17:27:53 -04:00
parent ec7cce80b4
commit b8f1e28eed
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 24 additions and 43 deletions

View File

@ -1,3 +1,4 @@
use crate::grid::PopulationConfig;
use crate::{buffer::Buf, util::wrap}; use crate::{buffer::Buf, util::wrap};
use fastapprox::faster::{cos, sin}; use fastapprox::faster::{cos, sin};
use rand::prelude::IndexedRandom; use rand::prelude::IndexedRandom;
@ -31,26 +32,17 @@ impl Agent {
} }
/// Tick an agent /// Tick an agent
pub fn tick( pub fn tick(&mut self, buf: &Buf, pop_config: PopulationConfig, width: usize, height: usize) {
&mut self, let xc = self.x + cos(self.heading) * pop_config.sensor_distance;
buf: &Buf, let yc = self.y + sin(self.heading) * pop_config.sensor_distance;
sensor_distance: f32,
sensor_angle: f32,
rotation_angle: f32,
step_distance: f32,
width: usize,
height: usize,
) {
let xc = self.x + cos(self.heading) * sensor_distance;
let yc = self.y + sin(self.heading) * sensor_distance;
let agent_add_sens = self.heading + sensor_angle; let agent_add_sens = self.heading + pop_config.sensor_angle;
let agent_sub_sens = self.heading - sensor_angle; let agent_sub_sens = self.heading - pop_config.sensor_angle;
let xl = self.x + cos(agent_sub_sens) * sensor_distance; let xl = self.x + cos(agent_sub_sens) * pop_config.sensor_distance;
let yl = self.y + sin(agent_sub_sens) * sensor_distance; let yl = self.y + sin(agent_sub_sens) * pop_config.sensor_distance;
let xr = self.x + cos(agent_add_sens) * sensor_distance; let xr = self.x + cos(agent_add_sens) * pop_config.sensor_distance;
let yr = self.y + sin(agent_add_sens) * sensor_distance; let yr = self.y + sin(agent_add_sens) * pop_config.sensor_distance;
// We sense from the buffer because this is where we previously combined data from all the grid. // We sense from the buffer because this is where we previously combined data from all the grid.
let center = buf.get_buf(xc, yc); let center = buf.get_buf(xc, yc);
@ -72,10 +64,16 @@ impl Agent {
0.0 0.0
}; };
let delta_angle = rotation_angle * direction; let delta_angle = pop_config.rotation_angle * direction;
self.heading = wrap(self.heading + delta_angle, TAU); self.heading = wrap(self.heading + delta_angle, TAU);
self.x = wrap(self.x + step_distance * cos(self.heading), width as f32); self.x = wrap(
self.y = wrap(self.y + step_distance * sin(self.heading), height as f32); self.x + pop_config.step_distance * cos(self.heading),
width as f32,
);
self.y = wrap(
self.y + pop_config.step_distance * sin(self.heading),
height as f32,
);
} }
} }

View File

@ -7,10 +7,10 @@ use std::fmt::{Display, Formatter};
/// A population configuration. /// A population configuration.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct PopulationConfig { pub struct PopulationConfig {
sensor_distance: f32, pub sensor_distance: f32,
step_distance: f32, pub step_distance: f32,
sensor_angle: f32, pub sensor_angle: f32,
rotation_angle: f32, pub rotation_angle: f32,
deposition_amount: f32, deposition_amount: f32,
} }
@ -90,25 +90,8 @@ impl Grid {
} }
pub fn tick(&mut self) { pub fn tick(&mut self) {
let (width, height) = (self.width, self.height);
let PopulationConfig {
sensor_distance,
sensor_angle,
rotation_angle,
step_distance,
..
} = self.config;
self.agents.par_iter_mut().for_each(|agent| { self.agents.par_iter_mut().for_each(|agent| {
agent.tick( agent.tick(&self.buf, self.config, self.width, self.height);
&self.buf,
sensor_distance,
sensor_angle,
rotation_angle,
step_distance,
width,
height,
);
}); });
self.deposit_all(); self.deposit_all();
} }