multiple home spots plus render

sideways
Rostyslav Hnatyshyn 9 months ago
parent 0b13c7bfa3
commit 9021e9e386
  1. 7
      src/lib/ai.rs
  2. 6
      src/lib/world.rs
  3. 11
      src/main.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<Pheremone> = valid.iter().map(|(_, pnt)| w.get_pheremone(pnt).clone()).collect();
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,

@ -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<BoardCommand> = e.data.values_mut().map(|a| a.plan(b, w)).collect();
let mut cmds: Vec<BoardCommand> = e.data.values_mut().map(|a| a.step(b, w)).collect();
cmds.extend(plan_cmds);

@ -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;

Loading…
Cancel
Save