multiple goal ai
This commit is contained in:
+20
-21
@@ -10,40 +10,46 @@ pub trait AI {
|
||||
fn plan(&mut self, w: &World) {}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum AIGoal {
|
||||
Reach(Point),
|
||||
//Pickup(Point),
|
||||
//Drop(),
|
||||
Idle,
|
||||
}
|
||||
|
||||
impl AI for Ant {
|
||||
fn plan(&mut self, w: &World) {
|
||||
if self.plan.len() == 0 {
|
||||
self.plan = match self.goal {
|
||||
// check last part of plan
|
||||
if let Some(goal) = self.plan.last() {
|
||||
match goal {
|
||||
AIGoal::Reach(target) => {
|
||||
if &self.pos == &target {
|
||||
self.goal = AIGoal::Idle;
|
||||
vec![]
|
||||
} else {
|
||||
astar(&self.pos, &target)
|
||||
if self.pos == *target {
|
||||
self.plan.pop();
|
||||
}
|
||||
}
|
||||
AIGoal::Idle => vec![],
|
||||
AIGoal::Idle => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return the next move for this ant
|
||||
fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand {
|
||||
let choice = match self.goal {
|
||||
|
||||
let goal = match self.plan.last() {
|
||||
Some(g) => g,
|
||||
None => &AIGoal::Idle,
|
||||
};
|
||||
|
||||
let choice = match goal {
|
||||
AIGoal::Idle => {
|
||||
let valid = w.get_valid_movements(&self.pos, b);
|
||||
let mut rng = thread_rng();
|
||||
valid.choose(&mut rng).cloned()
|
||||
}
|
||||
AIGoal::Reach(_) => {
|
||||
let movement = self.plan.pop();
|
||||
movement
|
||||
AIGoal::Reach(target) => {
|
||||
let mut movements = astar(&self.pos, &target);
|
||||
movements.pop()
|
||||
}
|
||||
};
|
||||
|
||||
@@ -55,13 +61,6 @@ impl AI for Ant {
|
||||
w.update_occupied(&old_pos, &pos);
|
||||
} else {
|
||||
w.clear(pos);
|
||||
match self.goal {
|
||||
// push plan back because we haven't actually moved towards it
|
||||
AIGoal::Reach(_) => {
|
||||
self.plan.push(pos);
|
||||
}
|
||||
AIGoal::Idle => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-3
@@ -40,8 +40,7 @@ impl_downcast!(Renderable);
|
||||
pub struct Ant {
|
||||
pub pos: Point,
|
||||
pub id: u32,
|
||||
pub goal: AIGoal,
|
||||
pub plan: Vec<Point>,
|
||||
pub plan: Vec<AIGoal>,
|
||||
}
|
||||
|
||||
impl Ant {
|
||||
@@ -50,7 +49,6 @@ impl Ant {
|
||||
pos: Point(x, y),
|
||||
id: 0,
|
||||
plan: vec![],
|
||||
goal: AIGoal::Idle,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -45,7 +45,9 @@ pub fn astar(start: &Point, goal: &Point) -> Vec<Point> {
|
||||
answer.push(current.clone());
|
||||
while came_from.contains_key(¤t) {
|
||||
current = came_from[¤t].clone();
|
||||
answer.push(current.clone());
|
||||
if current != *start {
|
||||
answer.push(current.clone());
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user