abstracted reactions

This commit is contained in:
Llywelwyn 2023-08-21 10:13:29 +01:00
parent 3050219494
commit 614653e028
3 changed files with 62 additions and 45 deletions

View file

@ -28,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(entity, idx - 1, &ancestries, &factions, &my_faction.name, &mut reactions);
evaluate(entity, idx - 1, &ancestries, &factions, &mut reactions);
}
if pos.x < w - 1 {
evaluate(entity, idx + 1, &ancestries, &factions, &my_faction.name, &mut reactions);
evaluate(entity, idx + 1, &ancestries, &factions, &mut reactions);
}
if pos.y > 0 {
evaluate(entity, idx - w as usize, &ancestries, &factions, &my_faction.name, &mut reactions);
evaluate(entity, idx - w as usize, &ancestries, &factions, &mut reactions);
}
if pos.y < h - 1 {
evaluate(entity, idx + w as usize, &ancestries, &factions, &my_faction.name, &mut reactions);
evaluate(entity, idx + w as usize, &ancestries, &factions, &mut reactions);
}
if pos.y > 0 && pos.x > 0 {
evaluate(entity, (idx - w as usize) - 1, &ancestries, &factions, &my_faction.name, &mut reactions);
evaluate(entity, (idx - w as usize) - 1, &ancestries, &factions, &mut reactions);
}
if pos.y > 0 && pos.x < w - 1 {
evaluate(entity, (idx - w as usize) + 1, &ancestries, &factions, &my_faction.name, &mut reactions);
evaluate(entity, (idx - w as usize) + 1, &ancestries, &factions, &mut reactions);
}
if pos.y < h - 1 && pos.x > 0 {
evaluate(entity, (idx + w as usize) - 1, &ancestries, &factions, &my_faction.name, &mut reactions);
evaluate(entity, (idx + w as usize) - 1, &ancestries, &factions, &mut reactions);
}
if pos.y < h - 1 && pos.x < w - 1 {
evaluate(entity, (idx + w as usize) + 1, &ancestries, &factions, &my_faction.name, &mut reactions);
evaluate(entity, (idx + w as usize) + 1, &ancestries, &factions, &mut reactions);
}
let mut done = false;
@ -79,26 +79,16 @@ fn evaluate(
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| {
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()),
));
}
}
let result = crate::raws::get_reactions(
entity,
other_entity,
&factions,
&ancestries,
&crate::raws::RAWS.lock().unwrap(),
);
reactions.push((other_entity, result));
});
}

View file

@ -3,9 +3,9 @@ use super::{
gamelog,
gui::obfuscate_name_ecs,
raws::Reaction,
Attributes, BlocksTile, BlocksVisibility, Door, EntityMoved, Faction, Hidden, HungerClock, HungerState, Item, Map,
Name, ParticleBuilder, Player, Pools, Position, Renderable, RunState, State, Telepath, TileType, Viewshed,
WantsToMelee, WantsToPickupItem,
Attributes, BlocksTile, BlocksVisibility, Door, EntityMoved, Faction, HasAncestry, Hidden, HungerClock,
HungerState, Item, Map, Name, ParticleBuilder, Player, Pools, Position, Renderable, RunState, State, Telepath,
TileType, Viewshed, WantsToMelee, WantsToPickupItem,
};
use rltk::{Point, RandomNumberGenerator, Rltk, VirtualKeyCode};
use specs::prelude::*;
@ -284,6 +284,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> RunState
let mut telepaths = ecs.write_storage::<Telepath>();
let mut entity_moved = ecs.write_storage::<EntityMoved>();
let factions = ecs.read_storage::<Faction>();
let ancestries = ecs.read_storage::<HasAncestry>();
let pools = ecs.read_storage::<Pools>();
let map = ecs.fetch::<Map>();
let entities = ecs.entities();
@ -306,12 +307,15 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> RunState
result = crate::spatial::for_each_tile_content_with_runstate(destination_idx, |potential_target| {
let mut hostile = true;
if pools.get(potential_target).is_some() {
if let Some(faction) = factions.get(potential_target) {
let reaction =
crate::raws::faction_reaction(&faction.name, "player", &crate::raws::RAWS.lock().unwrap());
if reaction != Reaction::Attack {
hostile = false;
}
let result = crate::raws::get_reactions(
entity,
potential_target,
&factions,
&ancestries,
&crate::raws::RAWS.lock().unwrap(),
);
if result != Reaction::Attack {
hostile = false;
}
}
if !hostile {
@ -545,22 +549,22 @@ fn skip_turn(ecs: &mut World) -> RunState {
// Default to being able to heal by waiting.
let mut can_heal = true;
let factions = ecs.read_storage::<Faction>();
let ancestries = ecs.read_storage::<HasAncestry>();
// Check viewshed for monsters nearby. If we can see a monster, we can't heal.
let viewshed = viewsheds.get_mut(*player_entity).unwrap();
for tile in viewshed.visible_tiles.iter() {
let idx = worldmap_resource.xy_idx(tile.x, tile.y);
crate::spatial::for_each_tile_content(idx, |entity_id| {
let faction = factions.get(entity_id);
match faction {
None => {}
Some(faction) => {
let reaction =
crate::raws::faction_reaction(&faction.name, "player", &crate::raws::RAWS.lock().unwrap());
if reaction == Reaction::Attack {
can_heal = false;
}
}
let result = crate::raws::get_reactions(
*player_entity,
entity_id,
&factions,
&ancestries,
&crate::raws::RAWS.lock().unwrap(),
);
if result == Reaction::Attack {
can_heal = false;
}
});
}

View file

@ -862,6 +862,29 @@ pub fn ancestry_reaction(this_ancestry: Ancestry, other_ancestry: Ancestry, raws
return None;
}
pub fn get_reactions(
this_entity: Entity,
other_entity: Entity,
factions: &ReadStorage<Faction>,
ancestries: &ReadStorage<HasAncestry>,
raws: &RawMaster,
) -> Reaction {
if let Some(this_ancestry) = ancestries.get(this_entity) {
if let Some(other_ancestry) = ancestries.get(other_entity) {
let result = ancestry_reaction(this_ancestry.name, other_ancestry.name, raws);
if result.is_some() {
return result.unwrap();
}
}
}
if let Some(this_faction) = factions.get(this_entity) {
if let Some(other_faction) = factions.get(other_entity) {
return faction_reaction(&this_faction.name, &other_faction.name, raws);
}
}
return Reaction::Ignore;
}
fn get_ancestry_string(ancestry: Ancestry) -> &'static str {
match ancestry {
Ancestry::Human => return "human",