2022-01-11 14:16:23 -05:00
|
|
|
use ::specs::prelude::*;
|
|
|
|
|
2022-01-11 15:35:59 -05:00
|
|
|
use crate::components::{
|
|
|
|
Chasing, Faction, MyTurn, Position, Viewshed, WantsToApproach, WantsToFlee,
|
|
|
|
};
|
2022-01-12 10:45:13 -05:00
|
|
|
use crate::raws::{self, Reaction, RAWS};
|
|
|
|
use crate::{spatial, Map};
|
2022-01-11 14:16:23 -05:00
|
|
|
|
|
|
|
pub struct VisibleAI {}
|
|
|
|
|
|
|
|
impl<'a> System<'a> for VisibleAI {
|
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
type SystemData = (
|
|
|
|
ReadStorage<'a, MyTurn>,
|
|
|
|
ReadStorage<'a, Faction>,
|
|
|
|
ReadStorage<'a, Position>,
|
|
|
|
ReadExpect<'a, Map>,
|
|
|
|
WriteStorage<'a, WantsToApproach>,
|
|
|
|
WriteStorage<'a, WantsToFlee>,
|
|
|
|
Entities<'a>,
|
|
|
|
ReadExpect<'a, Entity>,
|
|
|
|
ReadStorage<'a, Viewshed>,
|
2022-01-11 15:35:59 -05:00
|
|
|
WriteStorage<'a, Chasing>,
|
2022-01-11 14:16:23 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
|
|
|
let (
|
|
|
|
turns,
|
|
|
|
factions,
|
|
|
|
positions,
|
|
|
|
map,
|
|
|
|
mut want_approach,
|
|
|
|
mut want_flee,
|
|
|
|
entities,
|
|
|
|
player,
|
2022-01-11 15:35:59 -05:00
|
|
|
viewsheds,
|
|
|
|
mut chasing,
|
2022-01-11 14:16:23 -05:00
|
|
|
) = data;
|
|
|
|
|
|
|
|
for (entity, _turn, my_faction, pos, viewshed) in
|
2022-01-11 15:35:59 -05:00
|
|
|
(&entities, &turns, &factions, &positions, &viewsheds).join()
|
2022-01-11 14:16:23 -05:00
|
|
|
{
|
|
|
|
if entity != *player {
|
|
|
|
let my_idx = map.xy_idx(pos.x, pos.y);
|
2022-01-11 15:35:59 -05:00
|
|
|
let mut reactions: Vec<(usize, Reaction, Entity)> = Vec::new();
|
2022-01-11 14:16:23 -05:00
|
|
|
let mut flee: Vec<usize> = Vec::new();
|
|
|
|
for visible_tile in viewshed.visible_tiles.iter() {
|
|
|
|
let idx = map.xy_idx(visible_tile.x, visible_tile.y);
|
|
|
|
if my_idx != idx {
|
|
|
|
evaluate(idx, &map, &factions, &my_faction.name, &mut reactions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut done = false;
|
|
|
|
for reaction in reactions.iter() {
|
|
|
|
match reaction.1 {
|
|
|
|
Reaction::Attack => {
|
|
|
|
want_approach
|
|
|
|
.insert(
|
|
|
|
entity,
|
|
|
|
WantsToApproach {
|
|
|
|
idx: reaction.0 as i32,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("Unable to insert intent to approach");
|
2022-01-11 15:35:59 -05:00
|
|
|
chasing
|
|
|
|
.insert(entity, Chasing { target: reaction.2 })
|
|
|
|
.expect("Unable to insert intent to chase");
|
2022-01-11 14:16:23 -05:00
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
Reaction::Flee => {
|
|
|
|
flee.push(reaction.0);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !done && !flee.is_empty() {
|
|
|
|
want_flee
|
|
|
|
.insert(entity, WantsToFlee { indices: flee })
|
|
|
|
.expect("Unable to insert intent to flee");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn evaluate(
|
|
|
|
idx: usize,
|
2022-01-12 10:45:13 -05:00
|
|
|
_map: &Map,
|
2022-01-11 14:16:23 -05:00
|
|
|
factions: &ReadStorage<Faction>,
|
|
|
|
my_faction: &str,
|
2022-01-11 15:35:59 -05:00
|
|
|
reactions: &mut Vec<(usize, Reaction, Entity)>,
|
2022-01-11 14:16:23 -05:00
|
|
|
) {
|
2022-01-12 10:45:13 -05:00
|
|
|
spatial::for_each_tile_content(idx, |other_entity| {
|
|
|
|
if let Some(faction) = factions.get(other_entity) {
|
2022-01-11 14:16:23 -05:00
|
|
|
reactions.push((
|
|
|
|
idx,
|
2022-01-12 10:45:13 -05:00
|
|
|
raws::faction_reaction(my_faction, &faction.name, &RAWS.lock().unwrap()),
|
|
|
|
other_entity,
|
2022-01-11 14:16:23 -05:00
|
|
|
));
|
|
|
|
}
|
2022-01-12 10:45:13 -05:00
|
|
|
});
|
2022-01-11 14:16:23 -05:00
|
|
|
}
|