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; let h = map.height;
// Add possible reactions to adjacents for each direction // Add possible reactions to adjacents for each direction
if pos.x > 0 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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; let mut done = false;
@ -79,26 +79,16 @@ fn evaluate(
idx: usize, idx: usize,
ancestries: &ReadStorage<HasAncestry>, ancestries: &ReadStorage<HasAncestry>,
factions: &ReadStorage<Faction>, factions: &ReadStorage<Faction>,
this_faction: &str,
reactions: &mut Vec<(Entity, Reaction)>, reactions: &mut Vec<(Entity, Reaction)>,
) { ) {
crate::spatial::for_each_tile_content(idx, |other_entity| { crate::spatial::for_each_tile_content(idx, |other_entity| {
let mut shared_ancestry = false; let result = crate::raws::get_reactions(
if let Some(this_ancestry) = ancestries.get(entity) { 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, other_entity,
crate::raws::faction_reaction(this_faction, &faction.name, &crate::raws::RAWS.lock().unwrap()), &factions,
)); &ancestries,
} &crate::raws::RAWS.lock().unwrap(),
} );
reactions.push((other_entity, result));
}); });
} }

View file

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

View file

@ -862,6 +862,29 @@ pub fn ancestry_reaction(this_ancestry: Ancestry, other_ancestry: Ancestry, raws
return None; 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 { fn get_ancestry_string(ancestry: Ancestry) -> &'static str {
match ancestry { match ancestry {
Ancestry::Human => return "human", Ancestry::Human => return "human",