ancestries

This commit is contained in:
Llywelwyn 2023-08-21 09:57:47 +01:00
parent c8b28a9abd
commit 3050219494
11 changed files with 274 additions and 110 deletions

View file

@ -1,4 +1,4 @@
use crate::{raws::Reaction, Faction, Map, Position, TakingTurn, WantsToMelee};
use crate::{raws::Reaction, Faction, HasAncestry, Map, Position, TakingTurn, WantsToMelee};
use specs::prelude::*;
pub struct AdjacentAI {}
@ -8,6 +8,7 @@ impl<'a> System<'a> for AdjacentAI {
type SystemData = (
WriteStorage<'a, TakingTurn>,
ReadStorage<'a, Faction>,
ReadStorage<'a, HasAncestry>,
ReadStorage<'a, Position>,
ReadExpect<'a, Map>,
WriteStorage<'a, WantsToMelee>,
@ -16,7 +17,7 @@ impl<'a> System<'a> for AdjacentAI {
);
fn run(&mut self, data: Self::SystemData) {
let (mut turns, factions, positions, map, mut want_melee, entities, player) = data;
let (mut turns, factions, ancestries, positions, map, mut want_melee, entities, player) = data;
let mut turn_done: Vec<Entity> = Vec::new();
for (entity, _turn, my_faction, pos) in (&entities, &turns, &factions, &positions).join() {
@ -27,28 +28,28 @@ impl<'a> System<'a> for AdjacentAI {
let h = map.height;
// Add possible reactions to adjacents for each direction
if pos.x > 0 {
evaluate(idx - 1, &factions, &my_faction.name, &mut reactions);
evaluate(entity, idx - 1, &ancestries, &factions, &my_faction.name, &mut reactions);
}
if pos.x < w - 1 {
evaluate(idx + 1, &factions, &my_faction.name, &mut reactions);
evaluate(entity, idx + 1, &ancestries, &factions, &my_faction.name, &mut reactions);
}
if pos.y > 0 {
evaluate(idx - w as usize, &factions, &my_faction.name, &mut reactions);
evaluate(entity, idx - w as usize, &ancestries, &factions, &my_faction.name, &mut reactions);
}
if pos.y < h - 1 {
evaluate(idx + w as usize, &factions, &my_faction.name, &mut reactions);
evaluate(entity, idx + w as usize, &ancestries, &factions, &my_faction.name, &mut reactions);
}
if pos.y > 0 && pos.x > 0 {
evaluate((idx - w as usize) - 1, &factions, &my_faction.name, &mut reactions);
evaluate(entity, (idx - w as usize) - 1, &ancestries, &factions, &my_faction.name, &mut reactions);
}
if pos.y > 0 && pos.x < w - 1 {
evaluate((idx - w as usize) + 1, &factions, &my_faction.name, &mut reactions);
evaluate(entity, (idx - w as usize) + 1, &ancestries, &factions, &my_faction.name, &mut reactions);
}
if pos.y < h - 1 && pos.x > 0 {
evaluate((idx + w as usize) - 1, &factions, &my_faction.name, &mut reactions);
evaluate(entity, (idx + w as usize) - 1, &ancestries, &factions, &my_faction.name, &mut reactions);
}
if pos.y < h - 1 && pos.x < w - 1 {
evaluate((idx + w as usize) + 1, &factions, &my_faction.name, &mut reactions);
evaluate(entity, (idx + w as usize) + 1, &ancestries, &factions, &my_faction.name, &mut reactions);
}
let mut done = false;
@ -73,13 +74,31 @@ impl<'a> System<'a> for AdjacentAI {
}
/// Evaluates all possible reactions between this faction and all entities on a given tile idx.
fn evaluate(idx: usize, factions: &ReadStorage<Faction>, this_faction: &str, reactions: &mut Vec<(Entity, Reaction)>) {
fn evaluate(
entity: Entity,
idx: usize,
ancestries: &ReadStorage<HasAncestry>,
factions: &ReadStorage<Faction>,
this_faction: &str,
reactions: &mut Vec<(Entity, Reaction)>,
) {
crate::spatial::for_each_tile_content(idx, |other_entity| {
if let Some(faction) = factions.get(other_entity) {
reactions.push((
other_entity,
crate::raws::faction_reaction(this_faction, &faction.name, &crate::raws::RAWS.lock().unwrap()),
));
let mut shared_ancestry = false;
if let Some(this_ancestry) = ancestries.get(entity) {
if let Some(other_ancestry) = ancestries.get(other_entity) {
if this_ancestry.name == other_ancestry.name {
reactions.push((other_entity, Reaction::Ignore));
shared_ancestry = true;
}
}
}
if !shared_ancestry {
if let Some(faction) = factions.get(other_entity) {
reactions.push((
other_entity,
crate::raws::faction_reaction(this_faction, &faction.name, &crate::raws::RAWS.lock().unwrap()),
));
}
}
});
}

View file

@ -1,5 +1,6 @@
use crate::{
raws::Reaction, Chasing, Faction, Map, Mind, Position, TakingTurn, Telepath, Viewshed, WantsToApproach, WantsToFlee,
raws::Reaction, Chasing, Faction, HasAncestry, Map, Mind, Position, TakingTurn, Telepath, Viewshed,
WantsToApproach, WantsToFlee,
};
use specs::prelude::*;
use std::collections::HashSet;
@ -11,6 +12,7 @@ impl<'a> System<'a> for VisibleAI {
type SystemData = (
ReadStorage<'a, TakingTurn>,
ReadStorage<'a, Faction>,
ReadStorage<'a, HasAncestry>,
ReadStorage<'a, Position>,
ReadExpect<'a, Map>,
WriteStorage<'a, WantsToApproach>,
@ -27,6 +29,7 @@ impl<'a> System<'a> for VisibleAI {
let (
turns,
factions,
ancestries,
positions,
map,
mut wants_to_approach,
@ -50,7 +53,7 @@ impl<'a> System<'a> for VisibleAI {
for visible_tile in viewshed.visible_tiles.iter() {
let idx = map.xy_idx(visible_tile.x, visible_tile.y);
if this_idx != idx {
evaluate(idx, &factions, &faction.name, &mut reactions, None);
evaluate(entity, idx, &ancestries, &factions, &faction.name, &mut reactions, None);
idxs.insert(idx);
}
}
@ -61,7 +64,7 @@ impl<'a> System<'a> for VisibleAI {
// and it's not the idx we're standing on, then evaluate here w/ minds taken into
// account.
if this_idx != idx && idxs.contains(&idx) {
evaluate(idx, &factions, &faction.name, &mut reactions, Some(&minds));
evaluate(entity, idx, &ancestries, &factions, &faction.name, &mut reactions, Some(&minds));
}
}
}
@ -89,17 +92,42 @@ impl<'a> System<'a> for VisibleAI {
}
fn evaluate(
entity: Entity,
idx: usize,
ancestries: &ReadStorage<HasAncestry>,
factions: &ReadStorage<Faction>,
this_faction: &str,
reactions: &mut Vec<(usize, Reaction, Entity)>,
minds: Option<&ReadStorage<Mind>>,
) {
crate::spatial::for_each_tile_content(idx, |other_entity| {
// If minds are passed, we assume we're using telepathy here,
// so if the other entity is mindless, we skip it.
if minds.is_some() {
if minds.unwrap().get(other_entity).is_some() {
let mut shared_ancestry = false;
if let Some(this_ancestry) = ancestries.get(entity) {
if let Some(other_ancestry) = ancestries.get(other_entity) {
if this_ancestry.name == other_ancestry.name {
reactions.push((idx, Reaction::Ignore, other_entity));
shared_ancestry = true;
}
}
}
if !shared_ancestry {
// If minds are passed, we assume we're using telepathy here,
// so if the other entity is mindless, we skip it.
if minds.is_some() {
if minds.unwrap().get(other_entity).is_some() {
if let Some(faction) = factions.get(other_entity) {
reactions.push((
idx,
crate::raws::faction_reaction(
this_faction,
&faction.name,
&crate::raws::RAWS.lock().unwrap(),
),
other_entity,
));
}
}
} else {
if let Some(faction) = factions.get(other_entity) {
reactions.push((
idx,
@ -108,14 +136,6 @@ fn evaluate(
));
}
}
} else {
if let Some(faction) = factions.get(other_entity) {
reactions.push((
idx,
crate::raws::faction_reaction(this_faction, &faction.name, &crate::raws::RAWS.lock().unwrap()),
other_entity,
));
}
}
});
}