faction-based adjacent ai

This commit is contained in:
Llywelwyn 2023-08-15 16:29:01 +01:00
parent 76d835021b
commit 9b037dceeb
6 changed files with 135 additions and 23 deletions

View file

@ -0,0 +1,91 @@
use crate::{raws::Reaction, Faction, Map, Position, TakingTurn, WantsToMelee};
use rltk::prelude::*;
use specs::prelude::*;
pub struct AdjacentAI {}
impl<'a> System<'a> for AdjacentAI {
#[allow(clippy::type_complexity)]
type SystemData = (
WriteStorage<'a, TakingTurn>,
ReadStorage<'a, Faction>,
ReadStorage<'a, Position>,
ReadExpect<'a, Map>,
WriteStorage<'a, WantsToMelee>,
Entities<'a>,
ReadExpect<'a, Entity>,
);
fn run(&mut self, data: Self::SystemData) {
let (mut turns, factions, positions, map, mut wants_to_melee, entities, player) = data;
let mut turn_done: Vec<Entity> = Vec::new();
for (entity, _turn, faction, pos) in (&entities, &turns, &factions, &positions).join() {
if entity == *player {
continue;
}
let mut reactions: Vec<(Entity, Reaction)> = Vec::new();
let idx = map.xy_idx(pos.x, pos.y);
let (w, h) = (map.width, map.height);
// Evaluate adjacent squares, add possible reactions
if pos.x > 0 {
evaluate(idx - 1, &map, &factions, &faction.name, &mut reactions);
}
if pos.x < w - 1 {
evaluate(idx + 1, &map, &factions, &faction.name, &mut reactions);
}
if pos.y > 0 {
evaluate(idx - w as usize, &map, &factions, &faction.name, &mut reactions);
}
if pos.y < h - 1 {
evaluate(idx + w as usize, &map, &factions, &faction.name, &mut reactions);
}
if pos.y > 0 && pos.x > 0 {
evaluate((idx - w as usize) - 1, &map, &factions, &faction.name, &mut reactions);
}
if pos.y > 0 && pos.x < w - 1 {
evaluate((idx - w as usize) + 1, &map, &factions, &faction.name, &mut reactions);
}
if pos.y < h - 1 && pos.x > 0 {
evaluate((idx + w as usize) - 1, &map, &factions, &faction.name, &mut reactions);
}
if pos.y < h - 1 && pos.x < w - 1 {
evaluate((idx + w as usize) + 1, &map, &factions, &faction.name, &mut reactions);
}
let mut done = false;
for reaction in reactions.iter() {
if let Reaction::Attack = reaction.1 {
wants_to_melee
.insert(entity, WantsToMelee { target: reaction.0 })
.expect("Error inserting WantsToMelee");
done = true;
}
}
if done {
turn_done.push(entity);
}
}
// Remove turn from entities that are done
for done in turn_done.iter() {
turns.remove(*done);
}
}
}
/// Evaluates all possible reactions between this faction and all entities on a given tile idx.
fn evaluate(
idx: usize,
map: &Map,
factions: &ReadStorage<Faction>,
this_faction: &str,
reactions: &mut Vec<(Entity, Reaction)>,
) {
for other_entity in map.tile_content[idx].iter() {
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

@ -12,3 +12,5 @@ mod bystander_ai_system;
pub use bystander_ai_system::BystanderAI;
mod monster_ai_system;
pub use monster_ai_system::MonsterAI;
mod adjacent_ai_system;
pub use adjacent_ai_system::AdjacentAI;

View file

@ -96,6 +96,7 @@ impl State {
let mut encumbrance_system = ai::EncumbranceSystem {};
let mut turn_status_system = ai::TurnStatusSystem {};
let mut quip_system = ai::QuipSystem {};
let mut adjacent_ai = ai::AdjacentAI {};
let mut mob = ai::MonsterAI {};
let mut bystanders = ai::BystanderAI {};
let mut trigger_system = trigger_system::TriggerSystem {};
@ -116,6 +117,7 @@ impl State {
energy.run_now(&self.ecs);
turn_status_system.run_now(&self.ecs);
quip_system.run_now(&self.ecs);
adjacent_ai.run_now(&self.ecs);
mob.run_now(&self.ecs);
bystanders.run_now(&self.ecs);
trigger_system.run_now(&self.ecs);

View file

@ -12,7 +12,8 @@ use spawn_table_structs::*;
mod loot_table_structs;
use loot_table_structs::*;
mod faction_structs;
use faction_structs::{FactionData, Reaction};
use faction_structs::FactionData;
pub use faction_structs::Reaction;
use std::sync::Mutex;
lazy_static! {

View file

@ -290,6 +290,7 @@ pub fn spawn_named_mob(
eb = eb.with(get_renderable_component(renderable));
}
let mut has_mind = true;
let mut has_faction = false;
if let Some(flags) = &mob_template.flags {
for flag in flags.iter() {
match flag.as_str() {
@ -298,12 +299,21 @@ pub fn spawn_named_mob(
"MONSTER" => eb = eb.with(Monster {}),
"MINDLESS" => {
eb = eb.with(Faction { name: "mindless".to_string() });
has_faction = true;
has_mind = false;
}
"NEUTRAL" => eb = eb.with(Faction { name: "neutral".to_string() }),
"HOSTILE" => eb = eb.with(Faction { name: "hostile".to_string() }),
"HERBIVORE" => eb = eb.with(Faction { name: "herbivore".to_string() }),
"CARNIVORE" => eb = eb.with(Faction { name: "carnivore".to_string() }),
"NEUTRAL" => {
eb = eb.with(Faction { name: "neutral".to_string() });
has_faction = true;
}
"HERBIVORE" => {
eb = eb.with(Faction { name: "herbivore".to_string() });
has_faction = true;
}
"CARNIVORE" => {
eb = eb.with(Faction { name: "carnivore".to_string() });
has_faction = true;
}
"SMALL_GROUP" => {} // These flags are for region spawning,
"LARGE_GROUP" => {} // and don't matter here (yet)?
"MULTIATTACK" => {
@ -314,9 +324,15 @@ pub fn spawn_named_mob(
}
}
}
// If we're anything other than MINDLESS, add a mind.
if has_mind {
eb = eb.with(Mind {});
}
// If we didn't add a faction flag, default to hostile (attacks everything except other hostiles).
if !has_faction {
eb = eb.with(Faction { name: "hostile".to_string() });
}
// Add quips, if we have some listed.
if let Some(quips) = &mob_template.quips {
eb = eb.with(Quips { available: quips.clone() });
}