From b8f1e28eed1682599b04eebabc7d72a4c3984fda Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 28 Mar 2025 17:27:53 -0400 Subject: [PATCH] tick: simplify parameter passing --- src/agent.rs | 40 +++++++++++++++++++--------------------- src/grid.rs | 27 +++++---------------------- 2 files changed, 24 insertions(+), 43 deletions(-) diff --git a/src/agent.rs b/src/agent.rs index 3f73670..62ce389 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -1,3 +1,4 @@ +use crate::grid::PopulationConfig; use crate::{buffer::Buf, util::wrap}; use fastapprox::faster::{cos, sin}; use rand::prelude::IndexedRandom; @@ -31,26 +32,17 @@ impl Agent { } /// Tick an agent - pub fn tick( - &mut self, - buf: &Buf, - 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; + pub fn tick(&mut self, buf: &Buf, pop_config: PopulationConfig, width: usize, height: usize) { + let xc = self.x + cos(self.heading) * pop_config.sensor_distance; + let yc = self.y + sin(self.heading) * pop_config.sensor_distance; - let agent_add_sens = self.heading + sensor_angle; - let agent_sub_sens = self.heading - sensor_angle; + let agent_add_sens = self.heading + pop_config.sensor_angle; + let agent_sub_sens = self.heading - pop_config.sensor_angle; - let xl = self.x + cos(agent_sub_sens) * sensor_distance; - let yl = self.y + sin(agent_sub_sens) * sensor_distance; - let xr = self.x + cos(agent_add_sens) * sensor_distance; - let yr = self.y + sin(agent_add_sens) * sensor_distance; + let xl = self.x + cos(agent_sub_sens) * pop_config.sensor_distance; + let yl = self.y + sin(agent_sub_sens) * pop_config.sensor_distance; + let xr = self.x + cos(agent_add_sens) * pop_config.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. let center = buf.get_buf(xc, yc); @@ -72,10 +64,16 @@ impl Agent { 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.x = wrap(self.x + step_distance * cos(self.heading), width as f32); - self.y = wrap(self.y + step_distance * sin(self.heading), height as f32); + self.x = wrap( + 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, + ); } } diff --git a/src/grid.rs b/src/grid.rs index 8ef22df..7871ab1 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -7,10 +7,10 @@ use std::fmt::{Display, Formatter}; /// A population configuration. #[derive(Debug, Clone, Copy)] pub struct PopulationConfig { - sensor_distance: f32, - step_distance: f32, - sensor_angle: f32, - rotation_angle: f32, + pub sensor_distance: f32, + pub step_distance: f32, + pub sensor_angle: f32, + pub rotation_angle: f32, deposition_amount: f32, } @@ -90,25 +90,8 @@ impl Grid { } 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| { - agent.tick( - &self.buf, - sensor_distance, - sensor_angle, - rotation_angle, - step_distance, - width, - height, - ); + agent.tick(&self.buf, self.config, self.width, self.height); }); self.deposit_all(); }