Skip to main content

antbox_engine/
spawn.rs

1use antbox_cellauto::Evolvable as _;
2use antbox_state::GenParams;
3
4use crate::{Notifier, TickTimer};
5
6/// Spawn the engine thread
7pub fn spawn<N>(genparams: GenParams, notifier: N)
8where
9    N: Notifier + 'static,
10{
11    std::thread::spawn(move || {
12        if let Err(err) = run_inner(genparams, notifier) {
13            log::error!("{err}");
14        }
15    });
16}
17
18fn run_inner<N>(genparams: GenParams, notifier: N) -> Result<(), N::Error>
19where
20    N: Notifier,
21{
22    let mut state = genparams.generate_state();
23    let mut tt = TickTimer::start(200); // TODO: allow application control
24
25    loop {
26        let next = state.evolve();
27        let previous = std::mem::replace(&mut state, next);
28        notifier.post(previous)?;
29        tt.wait_for_tick();
30    }
31}