|
|
|
@ -1,122 +1,80 @@ |
|
|
|
|
use crate::lib::entity::{Ant, Egg, Food, FoodGenerator, Queen}; |
|
|
|
|
use crate::lib::point::Point; |
|
|
|
|
use crate::lib::entity::{Ant, Egg, Queen}; |
|
|
|
|
use crate::lib::point::{astar, Point}; |
|
|
|
|
use crate::lib::screen::{BoardCommand, Screen}; |
|
|
|
|
use crate::lib::world::{Pheremone, World}; |
|
|
|
|
use rand::Rng; |
|
|
|
|
use crate::lib::world::World; |
|
|
|
|
use rand::prelude::SliceRandom; |
|
|
|
|
use rand::thread_rng; |
|
|
|
|
use std::iter::zip; |
|
|
|
|
|
|
|
|
|
pub trait AI { |
|
|
|
|
fn step(&mut self, b: &Screen, w: &mut World, step: u32) -> BoardCommand; |
|
|
|
|
fn plan(&mut self, b: &Screen, w: &mut World) -> BoardCommand { |
|
|
|
|
BoardCommand::Noop |
|
|
|
|
} |
|
|
|
|
fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand; |
|
|
|
|
fn plan(&mut self, w: &World) {} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)] |
|
|
|
|
pub enum AIGoal { |
|
|
|
|
Seek, |
|
|
|
|
Return, |
|
|
|
|
Reach(Point), |
|
|
|
|
//Pickup(Point),
|
|
|
|
|
//Drop(),
|
|
|
|
|
Idle, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl AI for Ant { |
|
|
|
|
fn plan(&mut self, b: &Screen, w: &mut World) -> BoardCommand { |
|
|
|
|
match self.goal { |
|
|
|
|
AIGoal::Seek => { |
|
|
|
|
// if we reach food, we change state
|
|
|
|
|
if w.food.contains(&self.pos) { |
|
|
|
|
for p in &self.history { |
|
|
|
|
w.drop_pheremone(&p, &self.goal); |
|
|
|
|
} |
|
|
|
|
self.history.clear(); |
|
|
|
|
self.about_face(); |
|
|
|
|
self.goal = AIGoal::Return; |
|
|
|
|
} |
|
|
|
|
BoardCommand::Noop |
|
|
|
|
fn plan(&mut self, w: &World) { |
|
|
|
|
// check last part of plan
|
|
|
|
|
if let Some(goal) = self.plan.last() { |
|
|
|
|
match goal { |
|
|
|
|
AIGoal::Reach(target) => { |
|
|
|
|
if self.pos == *target { |
|
|
|
|
self.plan.pop(); |
|
|
|
|
} |
|
|
|
|
AIGoal::Return => { |
|
|
|
|
if w.home.contains(&self.pos) { |
|
|
|
|
for p in &self.history { |
|
|
|
|
w.drop_pheremone(&p, &self.goal); |
|
|
|
|
} |
|
|
|
|
self.history.clear(); |
|
|
|
|
self.about_face(); |
|
|
|
|
self.goal = AIGoal::Seek; |
|
|
|
|
return BoardCommand::SpawnAnt; |
|
|
|
|
} else { |
|
|
|
|
BoardCommand::Noop |
|
|
|
|
} |
|
|
|
|
AIGoal::Idle => {} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn step(&mut self, b: &Screen, w: &mut World, t: u32) -> BoardCommand { |
|
|
|
|
let valid = vec![ |
|
|
|
|
(self.dir, self.dir.relative_point(&self.pos)), |
|
|
|
|
(self.dir.ccw(), self.dir.ccw().relative_point(&self.pos)), |
|
|
|
|
(self.dir.cw(), self.dir.cw().relative_point(&self.pos)), |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
let ph: Vec<Pheremone> = valid |
|
|
|
|
.iter() |
|
|
|
|
.map(|(_, pnt)| w.get_pheremone(pnt).clone()) |
|
|
|
|
.collect(); |
|
|
|
|
|
|
|
|
|
let ph_fn = match self.goal { |
|
|
|
|
AIGoal::Seek => |ph: &Pheremone| ph.food, |
|
|
|
|
AIGoal::Return => |ph: &Pheremone| ph.home, |
|
|
|
|
// return the next move for this ant
|
|
|
|
|
fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand { |
|
|
|
|
let goal = match self.plan.last() { |
|
|
|
|
Some(g) => g, |
|
|
|
|
None => &AIGoal::Idle, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
let r: f32 = rand::random(); |
|
|
|
|
|
|
|
|
|
let mut dir = &valid[0].0; |
|
|
|
|
if r < 0.1 || ph.len() == 0 { |
|
|
|
|
let choice = match goal { |
|
|
|
|
AIGoal::Idle => { |
|
|
|
|
// valid_movements does not return any diggables!
|
|
|
|
|
let valid = w.get_valid_movements(&self.pos, b, true); |
|
|
|
|
let mut rng = thread_rng(); |
|
|
|
|
let choice = valid.choose(&mut rng).unwrap(); |
|
|
|
|
dir = &choice.0; |
|
|
|
|
} else { |
|
|
|
|
let mut greatest = &ph[0]; |
|
|
|
|
for (tup, p) in zip(&valid, &ph) { |
|
|
|
|
if ph_fn(p) > ph_fn(greatest) { |
|
|
|
|
greatest = p; |
|
|
|
|
dir = &tup.0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
valid.choose(&mut rng).cloned() |
|
|
|
|
} |
|
|
|
|
AIGoal::Reach(target) => { |
|
|
|
|
// here astar only produces a path between the target & pos without digging
|
|
|
|
|
let result = astar(&self.pos, &target, Some(w), Some(b)); |
|
|
|
|
let mut movements = match result { |
|
|
|
|
Ok(m) => m, |
|
|
|
|
Err(_) => vec![] |
|
|
|
|
}; |
|
|
|
|
movements.pop() |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if dir == &self.dir { |
|
|
|
|
self.forward(b); |
|
|
|
|
} else if dir == &self.dir.cw() { |
|
|
|
|
self.cw(); |
|
|
|
|
} else if dir == &self.dir.ccw() { |
|
|
|
|
self.ccw(); |
|
|
|
|
if !choice.is_none() { |
|
|
|
|
let pos = choice.unwrap(); |
|
|
|
|
if w.cleared.contains(&pos) { |
|
|
|
|
self.pos = pos; |
|
|
|
|
} else { |
|
|
|
|
if w.is_safe_to_dig(&pos, &b) { |
|
|
|
|
w.clear(pos); |
|
|
|
|
} |
|
|
|
|
BoardCommand::Noop |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl AI for FoodGenerator { |
|
|
|
|
fn step(&mut self, b: &Screen, w: &mut World, t: u32) -> BoardCommand { |
|
|
|
|
// eventually might want to use poisson disk distrib instead
|
|
|
|
|
if t % 600 == 0 && self.counter < 10 { |
|
|
|
|
// generate random coords, if valid, spawn
|
|
|
|
|
// if not, try again next step
|
|
|
|
|
let (min, max) = b.get_dimensions(); |
|
|
|
|
|
|
|
|
|
let mut rng = rand::thread_rng(); |
|
|
|
|
let r_x = rng.gen_range(min.0..max.0 + 1); |
|
|
|
|
let r_y = rng.gen_range(min.1..max.1 + 1); |
|
|
|
|
|
|
|
|
|
self.counter += 1; |
|
|
|
|
return BoardCommand::SpawnFood(Point(r_x, r_y)); |
|
|
|
|
} |
|
|
|
|
BoardCommand::Noop |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl AI for Egg { |
|
|
|
|
fn step(&mut self, b: &Screen, w: &mut World, t: u32) -> BoardCommand { |
|
|
|
|
fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand { |
|
|
|
|
if self.counter > 0 { |
|
|
|
|
self.counter -= 1; |
|
|
|
|
return BoardCommand::Noop; |
|
|
|
@ -127,14 +85,15 @@ impl AI for Egg { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl AI for Queen { |
|
|
|
|
fn step(&mut self, b: &Screen, w: &mut World, t: u32) -> BoardCommand { |
|
|
|
|
let valid: Vec<Point> = self.pos.get_neighbors(); |
|
|
|
|
fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand { |
|
|
|
|
let valid: Vec<Point> = w.get_valid_movements(&self.pos, b, false); |
|
|
|
|
let mut rng = thread_rng(); |
|
|
|
|
|
|
|
|
|
let choice = valid.choose(&mut rng).cloned(); |
|
|
|
|
|
|
|
|
|
if !choice.is_none() { |
|
|
|
|
let pos = choice.unwrap(); |
|
|
|
|
if w.cleared.contains(&pos) { |
|
|
|
|
// choose between laying an egg and moving
|
|
|
|
|
if self.egg_count < 3 { |
|
|
|
|
self.egg_count += 1; |
|
|
|
@ -143,17 +102,7 @@ impl AI for Queen { |
|
|
|
|
self.pos = pos; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
BoardCommand::Noop |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl AI for Food { |
|
|
|
|
fn step(&mut self, b: &Screen, w: &mut World, t: u32) -> BoardCommand { |
|
|
|
|
for n in self.pos.get_neighbors() { |
|
|
|
|
w.drop_pheremone(&n, &AIGoal::Return); |
|
|
|
|
} |
|
|
|
|
w.drop_pheremone(&self.pos, &AIGoal::Return); |
|
|
|
|
|
|
|
|
|
BoardCommand::Noop |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|