2022-01-11 14:16:23 -05:00
|
|
|
use ::rltk::a_star_search;
|
|
|
|
use ::specs::prelude::*;
|
|
|
|
|
2022-01-18 11:00:13 -05:00
|
|
|
use crate::components::{ApplyMove, MyTurn, Position, WantsToApproach};
|
|
|
|
use crate::Map;
|
2022-01-11 14:16:23 -05:00
|
|
|
|
|
|
|
pub struct ApproachAI {}
|
|
|
|
|
|
|
|
impl<'a> System<'a> for ApproachAI {
|
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
type SystemData = (
|
|
|
|
WriteStorage<'a, MyTurn>,
|
|
|
|
WriteStorage<'a, WantsToApproach>,
|
2022-01-18 11:00:13 -05:00
|
|
|
ReadStorage<'a, Position>,
|
2022-01-12 10:45:13 -05:00
|
|
|
ReadExpect<'a, Map>,
|
2022-01-11 14:16:23 -05:00
|
|
|
Entities<'a>,
|
2022-01-18 11:00:13 -05:00
|
|
|
WriteStorage<'a, ApplyMove>,
|
2022-01-11 14:16:23 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
2022-01-18 11:00:13 -05:00
|
|
|
let (mut turns, mut want_approach, positions, map, entities, mut apply_move) = data;
|
2022-01-11 14:16:23 -05:00
|
|
|
|
|
|
|
let mut turn_done: Vec<Entity> = Vec::new();
|
2022-01-18 11:00:13 -05:00
|
|
|
for (entity, pos, approach, _myturn) in
|
|
|
|
(&entities, &positions, &want_approach, &turns).join()
|
2022-01-11 14:16:23 -05:00
|
|
|
{
|
|
|
|
turn_done.push(entity);
|
|
|
|
|
|
|
|
let path = a_star_search(
|
|
|
|
map.xy_idx(pos.x, pos.y) as i32,
|
|
|
|
map.xy_idx(approach.idx % map.width, approach.idx / map.width) as i32,
|
|
|
|
&*map,
|
|
|
|
);
|
|
|
|
|
|
|
|
if path.success && path.steps.len() > 1 {
|
2022-01-18 11:00:13 -05:00
|
|
|
apply_move
|
|
|
|
.insert(
|
|
|
|
entity,
|
|
|
|
ApplyMove {
|
|
|
|
dest_idx: path.steps[1],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("Unable to insert intent to move.");
|
2022-01-11 14:16:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
want_approach.clear();
|
|
|
|
|
|
|
|
// Remove turn marker for those that are done
|
|
|
|
for done in turn_done.iter() {
|
|
|
|
turns.remove(*done);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|