Initial pass at bogo AI
Not fully wired up. Need to figure out how to gate the pass-n-play based on the state and only change the state when in human mode. When in AI mode, just let the AI pick a piece and move without the camera changing.main
parent
5604796328
commit
6f5064a308
@ -0,0 +1,50 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub(crate) struct AiPlugin;
|
||||
|
||||
impl Plugin for AiPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_state::<PlayState>()
|
||||
// Bogo AI Systems
|
||||
.add_systems(Update,
|
||||
bogo_ai.run_if(in_state(PlayState::AiBogo))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, States, Hash, Default, PartialEq, Eq, Clone)]
|
||||
enum PlayState {
|
||||
#[default]
|
||||
Human,
|
||||
AiBogo,
|
||||
}
|
||||
|
||||
// Bogo AI logic
|
||||
fn bogo_ai(
|
||||
mut board: ResMut<Board>,
|
||||
mut move_events: EventWriter<Move>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
// TODO: Assuming AI is Side::A
|
||||
let ai_side = Side::A;
|
||||
|
||||
let on = board.on(ai_side);
|
||||
let set: Vec<(&BoardIndex, HashSet<BoardIndex>)> = on
|
||||
.iter()
|
||||
.filter_map(|(_, idx)| {
|
||||
let moves = board.valid_moves(*idx);
|
||||
(!moves.is_empty()).then_some((idx, moves))
|
||||
})
|
||||
.collect();
|
||||
|
||||
let r = time.elapsed_seconds() as usize;
|
||||
|
||||
let m_i = r % set.len();
|
||||
let m = set.get(m_i).unwrap();
|
||||
let m_bi = m.0;
|
||||
|
||||
let n_i = r % m.1.len();
|
||||
let n_bi = m.1.iter().nth(n_i).unwrap();
|
||||
|
||||
move_events.send(Move { from: *m_bi, to: Some(*n_bi), ..default() });
|
||||
}
|
||||
Loading…
Reference in New Issue