|
@ -1,5 +1,5 @@ |
|
|
use gntag::agent::{Agent, SimpleAgent};
|
|
|
use gntag::agent::{Agent, SimpleAgent};
|
|
|
use gntag::view::TerminalView;
|
|
|
|
|
|
|
|
|
use gntag::view::{Backend, TerminalView};
|
|
|
use gntag::world::ActualWorld;
|
|
|
use gntag::world::ActualWorld;
|
|
|
use std::io;
|
|
|
use std::io;
|
|
|
use std::io::Read;
|
|
|
use std::io::Read;
|
|
@ -9,6 +9,8 @@ use std::thread; |
|
|
|
|
|
|
|
|
fn main() {
|
|
|
fn main() {
|
|
|
let view = Arc::new(Mutex::new(Some(TerminalView::try_new().unwrap())));
|
|
|
let view = Arc::new(Mutex::new(Some(TerminalView::try_new().unwrap())));
|
|
|
|
|
|
|
|
|
|
|
|
// Exit on q, ESC and Ctrl-C and reset terminal
|
|
|
let view2 = view.clone();
|
|
|
let view2 = view.clone();
|
|
|
thread::spawn(move || {
|
|
|
thread::spawn(move || {
|
|
|
let stdin = io::stdin();
|
|
|
let stdin = io::stdin();
|
|
@ -22,48 +24,52 @@ fn main() { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
|
|
|
|
|
|
loop {
|
|
|
loop {
|
|
|
let (width, height) = (*view.lock().unwrap())
|
|
|
|
|
|
|
|
|
run_simulation(&view);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn run_simulation(view: &Arc<Mutex<Option<TerminalView<impl Backend>>>>) {
|
|
|
|
|
|
let (width, height) = (*view.lock().unwrap())
|
|
|
|
|
|
.as_mut()
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
.content_size()
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
let mut agents: Vec<(_, Box<dyn Agent>)> = vec![];
|
|
|
|
|
|
for x in (0..width).step_by(10) {
|
|
|
|
|
|
for y in (0..height).step_by(10) {
|
|
|
|
|
|
agents.push(((x, y).into(), Box::new(SimpleAgent)));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
let mut world = ActualWorld::new((width, height).into(), agents);
|
|
|
|
|
|
|
|
|
|
|
|
let mut gen = 0;
|
|
|
|
|
|
loop {
|
|
|
|
|
|
let resized = (*view.lock().unwrap())
|
|
|
.as_mut()
|
|
|
.as_mut()
|
|
|
.unwrap()
|
|
|
.unwrap()
|
|
|
.content_size()
|
|
|
|
|
|
|
|
|
.draw(
|
|
|
|
|
|
gen,
|
|
|
|
|
|
world
|
|
|
|
|
|
.state
|
|
|
|
|
|
.agent_positions
|
|
|
|
|
|
.get(&world.state.tagged)
|
|
|
|
|
|
.map(|pos| (pos.x, pos.y))
|
|
|
|
|
|
.unwrap(),
|
|
|
|
|
|
world
|
|
|
|
|
|
.state
|
|
|
|
|
|
.agent_positions
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.map(|(_id, pos)| (pos.x, pos.y))
|
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
|
.as_ref(),
|
|
|
|
|
|
)
|
|
|
.unwrap();
|
|
|
.unwrap();
|
|
|
let mut agents: Vec<(_, Box<dyn Agent>)> = vec![];
|
|
|
|
|
|
for x in (0..width).step_by(10) {
|
|
|
|
|
|
for y in (0..height).step_by(10) {
|
|
|
|
|
|
agents.push(((x, y).into(), Box::new(SimpleAgent)));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
let mut world = ActualWorld::new((width, height).into(), agents);
|
|
|
|
|
|
|
|
|
|
|
|
let mut gen = 0;
|
|
|
|
|
|
loop {
|
|
|
|
|
|
let resized = (*view.lock().unwrap())
|
|
|
|
|
|
.as_mut()
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
.draw(
|
|
|
|
|
|
gen,
|
|
|
|
|
|
world
|
|
|
|
|
|
.state
|
|
|
|
|
|
.agent_positions
|
|
|
|
|
|
.get(&world.state.tagged)
|
|
|
|
|
|
.map(|pos| (pos.x, pos.y))
|
|
|
|
|
|
.unwrap(),
|
|
|
|
|
|
world
|
|
|
|
|
|
.state
|
|
|
|
|
|
.agent_positions
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.map(|(_id, pos)| (pos.x, pos.y))
|
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
|
.as_ref(),
|
|
|
|
|
|
)
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
if resized {
|
|
|
|
|
|
break;
|
|
|
|
|
|
};
|
|
|
|
|
|
world.do_step();
|
|
|
|
|
|
gen += 1;
|
|
|
|
|
|
//std::thread::sleep(std::time::Duration::from_millis(500));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
if resized {
|
|
|
|
|
|
return;
|
|
|
|
|
|
};
|
|
|
|
|
|
world.do_step();
|
|
|
|
|
|
gen += 1;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|