2022-01-11 14:16:23 -05:00
|
|
|
use ::rltk::a_star_search;
|
|
|
|
use ::specs::prelude::*;
|
|
|
|
|
|
|
|
use crate::components::{EntityMoved, MyTurn, Position, Viewshed, WantsToApproach};
|
2022-01-12 10:45:13 -05:00
|
|
|
use crate::{spatial, 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>,
|
|
|
|
WriteStorage<'a, Position>,
|
2022-01-12 10:45:13 -05:00
|
|
|
ReadExpect<'a, Map>,
|
2022-01-11 14:16:23 -05:00
|
|
|
WriteStorage<'a, Viewshed>,
|
|
|
|
WriteStorage<'a, EntityMoved>,
|
|
|
|
Entities<'a>,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
|
|
|
let (
|
|
|
|
mut turns,
|
|
|
|
mut want_approach,
|
|
|
|
mut positions,
|
2022-01-12 10:45:13 -05:00
|
|
|
map,
|
2022-01-11 14:16:23 -05:00
|
|
|
mut viewsheds,
|
|
|
|
mut entity_moved,
|
|
|
|
entities,
|
|
|
|
) = data;
|
|
|
|
|
|
|
|
let mut turn_done: Vec<Entity> = Vec::new();
|
|
|
|
for (entity, mut pos, approach, mut viewshed, _myturn) in (
|
|
|
|
&entities,
|
|
|
|
&mut positions,
|
|
|
|
&want_approach,
|
|
|
|
&mut viewsheds,
|
|
|
|
&turns,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
|
|
|
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-12 10:45:13 -05:00
|
|
|
let idx = map.xy_idx(pos.x, pos.y);
|
2022-01-11 14:16:23 -05:00
|
|
|
pos.x = path.steps[1] as i32 % map.width;
|
|
|
|
pos.y = path.steps[1] as i32 / map.width;
|
|
|
|
entity_moved
|
|
|
|
.insert(entity, EntityMoved {})
|
|
|
|
.expect("Unable to insert moved marker");
|
|
|
|
|
2022-01-12 10:45:13 -05:00
|
|
|
let new_idx = map.xy_idx(pos.x, pos.y);
|
|
|
|
spatial::move_entity(entity, idx, new_idx);
|
2022-01-11 14:16:23 -05:00
|
|
|
viewshed.dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
want_approach.clear();
|
|
|
|
|
|
|
|
// Remove turn marker for those that are done
|
|
|
|
for done in turn_done.iter() {
|
|
|
|
turns.remove(*done);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|