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
Elijah C. Voigt 1 year ago
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() });
}

@ -3,6 +3,7 @@
#![feature(iter_intersperse)] // used in debug.rs
#![feature(async_closure)] // Loading tweakfiles
mod ai;
mod audio;
mod credits;
mod debug;
@ -60,6 +61,7 @@ fn main() {
app.add_plugins(tweak::TweakPlugin);
app.add_plugins(intro::IntroPlugin);
app.add_plugins(tutorial::TutorialPlugin);
app.add_plugins(ai::AiPlugin);
app.run();
}

@ -1,4 +1,4 @@
pub(crate) use crate::{audio::AudioEvent, game::*, menu::*, tutorial::*, tweak::*, *};
pub(crate) use crate::{audio::AudioEvent, game::*, menu::*, tutorial::*, tweak::*, ai::*, *};
pub(crate) use bevy::{
animation::RepeatAnimation,

Loading…
Cancel
Save