From 9021e9e3864943013fd7a7bc8ee6b63e13a15dab Mon Sep 17 00:00:00 2001 From: Rostyslav Hnatyshyn Date: Mon, 8 Jan 2024 21:29:00 -0700 Subject: [PATCH] multiple home spots plus render --- src/lib/ai.rs | 7 +++++-- src/lib/world.rs | 6 +++++- src/main.rs | 11 ++++++----- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/lib/ai.rs b/src/lib/ai.rs index a00a393..d880016 100644 --- a/src/lib/ai.rs +++ b/src/lib/ai.rs @@ -36,7 +36,7 @@ impl AI for Ant { BoardCommand::Noop } AIGoal::Return => { - if self.pos == b.center { + if w.home.contains(&self.pos) { for p in &self.history { w.drop_pheremone(&p, &self.goal); } @@ -59,7 +59,10 @@ impl AI for Ant { (self.dir.cw(), self.dir.cw().relative_point(&self.pos)), ]; - let ph: Vec = valid.iter().map(|(_, pnt)| w.get_pheremone(pnt).clone()).collect(); + let ph: Vec = valid + .iter() + .map(|(_, pnt)| w.get_pheremone(pnt).clone()) + .collect(); let ph_fn = match self.goal { AIGoal::Seek => |ph: &Pheremone| ph.food, diff --git a/src/lib/world.rs b/src/lib/world.rs index 7ef04c5..5e776d5 100644 --- a/src/lib/world.rs +++ b/src/lib/world.rs @@ -69,6 +69,11 @@ impl World { pub fn render(e: &Entities, w: &World, b: &Screen) { erase(); + + for h in w.home.iter() { + b.render(h, "x"); + } + for a in e.data.values() { a.render(b); } @@ -76,7 +81,6 @@ pub fn render(e: &Entities, w: &World, b: &Screen) { pub fn simulate(e: &mut Entities, w: &mut World, b: &mut Screen, step: u32) { let plan_cmds: Vec = e.data.values_mut().map(|a| a.plan(b, w)).collect(); - let mut cmds: Vec = e.data.values_mut().map(|a| a.step(b, w)).collect(); cmds.extend(plan_cmds); diff --git a/src/main.rs b/src/main.rs index 8c8dc34..a867dbf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,8 +20,9 @@ use lib::screen::init_screen; use lib::world::{render, simulate, World}; fn main() { - let mut board = init_screen(); - let mut world = World::new(&Point(board.max_x, board.max_y)); + let mut screen = init_screen(); + let mut world = World::new(&Point(screen.max_x, screen.max_y)); + world.home.insert(screen.center); let mut entities = Entities::new(); @@ -32,7 +33,7 @@ fn main() { entities.add_entity(&fg); for _ in 0..5 { - let mut a = Ant::new(board.center.0, board.center.1); + let mut a = Ant::new(screen.center.0, screen.center.1); a.goal = lib::ai::AIGoal::Seek; entities.add_entity(&a); } @@ -42,8 +43,8 @@ fn main() { let mut t = 0; loop { // TODO: add way to break out of the loop by hitting a random key - simulate(&mut entities, &mut world, &mut board, t); - render(&entities, &world, &board); + simulate(&mut entities, &mut world, &mut screen, t); + render(&entities, &world, &screen); sleep(time::Duration::from_millis(100)); refresh(); t += 1;