collisions but queen can't spawn more than 3

master
Rostyslav Hnatyshyn 9 months ago
parent 04f4a91c0e
commit 37f1bb8022
  1. 108
      src/main.rs

@ -67,33 +67,49 @@ impl World {
fn get_valid_movements(&self, pos: &Point, b: &Board) -> Vec<Point> { fn get_valid_movements(&self, pos: &Point, b: &Board) -> Vec<Point> {
let moves = b.get_valid_movements(pos); let moves = b.get_valid_movements(pos);
moves.iter().filter(|p| !self.occupied.contains(p)).map(|p| p.clone()).collect() moves
.iter()
.filter(|p| !self.occupied.contains(p))
.map(|p| p.clone())
.collect()
} }
} }
trait Entity: AI + Renderable {} trait Entity: AI + Renderable {}
struct Colony { struct Colony {
ants: HashMap<Point, Box<dyn Entity>>, ants: HashMap<u32, Box<dyn Entity>>,
id_counter: u32,
} }
impl Colony { impl Colony {
fn new() -> Colony { fn new() -> Colony {
Colony { Colony {
ants: HashMap::new(), ants: HashMap::new(),
id_counter: 0,
} }
} }
fn add_entity<T: Entity + 'static>(&mut self, pos: &Point, e: T) { fn add_entity<T: Entity + 'static>(&mut self, e: T) {
self.ants.insert(*pos, Box::new(e)); self.ants.insert(self.id_counter, Box::new(e));
self.id_counter += 1;
} }
} }
trait AI { trait AI {
fn step(&mut self, pos: &Point, b: &Board, w: &World) -> BoardCommand; fn step(&mut self, id: u32, b: &Board, w: &World) -> BoardCommand;
fn get_position(&self) -> Point;
} }
struct Ant {} struct Ant {
pos: Point,
}
impl Ant {
fn new(x: i32, y: i32) -> Ant {
Ant { pos: Point(x, y) }
}
}
impl Entity for Ant {} impl Entity for Ant {}
@ -104,6 +120,7 @@ impl Renderable for Ant {
} }
struct Queen { struct Queen {
pos: Point,
egg_count: u8, egg_count: u8,
} }
@ -120,13 +137,17 @@ trait Renderable {
} }
impl Queen { impl Queen {
fn new() -> Queen { fn new(x: i32, y: i32) -> Queen {
Queen { egg_count: 0 } Queen {
pos: Point(x, y),
egg_count: 0,
}
} }
} }
#[derive(Debug)] #[derive(Debug)]
struct Egg { struct Egg {
pos: Point,
counter: u8, counter: u8,
} }
@ -137,34 +158,43 @@ impl Renderable for Egg {
} }
impl Egg { impl Egg {
fn new() -> Egg { fn new(x: i32, y: i32) -> Egg {
Egg { counter: 10 } Egg {
pos: Point(x, y),
counter: 10,
}
} }
} }
impl AI for Egg { impl AI for Egg {
fn step(&mut self, p: &Point, b: &Board, w: &World) -> BoardCommand { fn step(&mut self, id: u32, b: &Board, w: &World) -> BoardCommand {
if self.counter > 0 { if self.counter > 0 {
self.counter -= 1; self.counter -= 1;
return BoardCommand::Noop; return BoardCommand::Noop;
} else { } else {
BoardCommand::Hatch(*p) BoardCommand::Hatch(id)
} }
} }
fn get_position(&self) -> Point {
self.pos.clone()
}
} }
enum BoardCommand { enum BoardCommand {
Move(Point, Point),
Dig(Point), Dig(Point),
LayEgg(Point), LayEgg(Point),
Hatch(Point), Hatch(u32),
Noop, Noop,
} }
impl AI for Ant { impl AI for Ant {
fn get_position(&self) -> Point {
self.pos.clone()
}
// return the next move for this ant // return the next move for this ant
fn step(&mut self, p: &Point, b: &Board, w: &World) -> BoardCommand { fn step(&mut self, id: u32, b: &Board, w: &World) -> BoardCommand {
let valid = w.get_valid_movements(p, b); let valid = w.get_valid_movements(&self.pos, b);
let mut rng = thread_rng(); let mut rng = thread_rng();
let choice = valid.choose(&mut rng).cloned(); let choice = valid.choose(&mut rng).cloned();
@ -174,7 +204,8 @@ impl AI for Ant {
} else { } else {
let pos = choice.unwrap(); let pos = choice.unwrap();
if w.cleared.contains(&pos) { if w.cleared.contains(&pos) {
return BoardCommand::Move(*p, pos); self.pos = pos;
return BoardCommand::Noop;
} else { } else {
return BoardCommand::Dig(pos); return BoardCommand::Dig(pos);
} }
@ -186,9 +217,13 @@ impl Entity for Queen {}
impl Entity for Egg {} impl Entity for Egg {}
impl AI for Queen { impl AI for Queen {
fn step(&mut self, p: &Point, b: &Board, w: &World) -> BoardCommand { fn get_position(&self) -> Point {
let valid: Vec<Point> = w.get_valid_movements(p, b); self.pos.clone()
}
fn step(&mut self, id: u32, b: &Board, w: &World) -> BoardCommand {
let valid: Vec<Point> = w.get_valid_movements(&self.pos, b);
let mut rng = thread_rng(); let mut rng = thread_rng();
let choice = valid.choose(&mut rng).cloned(); let choice = valid.choose(&mut rng).cloned();
if choice.is_none() { if choice.is_none() {
@ -201,7 +236,8 @@ impl AI for Queen {
self.egg_count += 1; self.egg_count += 1;
return BoardCommand::LayEgg(pos); return BoardCommand::LayEgg(pos);
} else { } else {
return BoardCommand::Move(*p, pos); self.pos = pos;
return BoardCommand::Noop;
} }
} else { } else {
return BoardCommand::Noop; return BoardCommand::Noop;
@ -215,7 +251,8 @@ fn render(c: &Colony, w: &World, b: &Board) {
mvprintw(c.1 + b.center.1, c.0 + b.center.0, "x"); mvprintw(c.1 + b.center.1, c.0 + b.center.0, "x");
} }
for (pos, a) in c.ants.iter() { for a in c.ants.values() {
let pos = a.get_position();
mvprintw(pos.1 + b.center.1, pos.0 + b.center.0, a.render()); mvprintw(pos.1 + b.center.1, pos.0 + b.center.0, a.render());
} }
} }
@ -239,35 +276,26 @@ mod tests {
} }
fn simulate(c: &mut Colony, w: &mut World, b: &mut Board) { fn simulate(c: &mut Colony, w: &mut World, b: &mut Board) {
let cmds: Vec<BoardCommand> = c let cmds: Vec<BoardCommand> = c.ants.iter_mut().map(|(id, a)| a.step(*id, b, &w)).collect();
.ants
.iter_mut()
.map(|(p, a)| {
a.step(&p, b, &w)
})
.collect();
for cmd in cmds { for cmd in cmds {
match cmd { match cmd {
BoardCommand::Move(old_pos, pos) => {
let a = c.ants.remove(&old_pos).unwrap();
c.ants.insert(pos, a);
}
BoardCommand::Dig(pos) => { BoardCommand::Dig(pos) => {
w.clear(pos); w.clear(pos);
} }
BoardCommand::LayEgg(pos) => { BoardCommand::LayEgg(pos) => {
c.add_entity(&pos, Egg::new()); c.add_entity(Egg::new(pos.0, pos.1));
} }
BoardCommand::Hatch(pos) => { BoardCommand::Hatch(egg_id) => {
c.ants.remove(&pos); let egg = c.ants.remove(&egg_id);
c.add_entity(&pos, Ant {}); let pos = egg.unwrap().get_position();
c.add_entity(Ant::new(pos.0, pos.1));
} }
BoardCommand::Noop => {} BoardCommand::Noop => {}
} }
} }
w.occupied.clear(); w.occupied.clear();
let _ = c.ants.keys().map(|p| w.occupied.insert(*p)); let _ = c.ants.values().map(|p| w.occupied.insert(p.get_position()));
} }
fn main() { fn main() {
@ -285,7 +313,11 @@ fn main() {
let mut board = Board::new(max_x, max_y); let mut board = Board::new(max_x, max_y);
let mut world = World::new(); let mut world = World::new();
let q = Queen::new(0,0);
let mut colony = Colony::new(); let mut colony = Colony::new();
colony.add_entity(q);
for i in -3..3 { for i in -3..3 {
for j in -3..3 { for j in -3..3 {
@ -293,8 +325,6 @@ fn main() {
} }
} }
colony.add_entity(&Point(0, 0), Queen::new());
loop { loop {
// TODO: add way to break out of the loop by hitting a random key // TODO: add way to break out of the loop by hitting a random key
simulate(&mut colony, &mut world, &mut board); simulate(&mut colony, &mut world, &mut board);

Loading…
Cancel
Save