diff --git a/src/lib/ai.rs b/src/lib/ai.rs index 18fdf64..41552e6 100644 --- a/src/lib/ai.rs +++ b/src/lib/ai.rs @@ -24,7 +24,14 @@ impl AI for Ant { fn plan(&mut self, w: &World) { if self.plan.len() == 0 { self.plan = match self.goal { - AIGoal::Reach(target) => astar(&self.pos, &target), + AIGoal::Reach(target) => { + if &self.pos == &target { + self.goal = AIGoal::Idle; + vec![] + } else { + astar(&self.pos, &target) + } + }, AIGoal::Idle => vec![], } } @@ -54,6 +61,7 @@ impl AI for Ant { } else { w.clear(pos); match self.goal { + // push plan back because we haven't actually moved towards it AIGoal::Reach(_) => { self.plan.push(pos); } @@ -62,7 +70,6 @@ impl AI for Ant { } } - // need to check if progress has been made towards the plan BoardCommand::Noop } } diff --git a/tests/ai_test.rs b/tests/ai_test.rs new file mode 100644 index 0000000..be2bd63 --- /dev/null +++ b/tests/ai_test.rs @@ -0,0 +1,32 @@ +use antf::lib::screen::init_screen; +use antf::lib::point::Point; +use antf::lib::ai::AIGoal; +use antf::lib::world::{World, simulate, render}; +use antf::lib::entity::{Entities, Ant}; + +use ncurses::*; +use std::thread::sleep; +use std::time; + +#[test] +fn test_astar() { + let mut board = init_screen(); + let mut world = World::new(); + + let mut entities = Entities::new(); + let id = entities.add_ant(0,0); + + let a = entities.data.get_mut(&id).unwrap(); + let ant: &mut Ant = a.downcast_mut::().unwrap(); + ant.goal = AIGoal::Reach(Point(10,10)); + + for _ in 0..60 { + // TODO: add way to break out of the loop by hitting a random key + simulate(&mut entities, &mut world, &mut board); + render(&entities, &world, &board); + sleep(time::Duration::from_millis(100)); + refresh(); + } + endwin(); +} +