A simple actor-based simulation of tag, implemented in rust.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.4 KiB

use gntag::get_world;
use gntag::view::{Backend, TerminalView};
use std::io;
use std::io::Read;
use std::process::exit;
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
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();
thread::spawn(move || {
let stdin = io::stdin();
for byte in stdin.bytes().flatten() {
if byte == b'q' || byte == 0x1b || byte == 0x03 {
if let Ok(mut view) = view2.lock() {
*view = None; // drop view
println!("\n");
}
exit(0);
}
}
});
loop {
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 world = get_world(width, height, 10, true);
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 {
return;
};
world.do_step();
gen += 1;
}
}