From 614653e0285806cbcc226a7a86649e194a8f6af3 Mon Sep 17 00:00:00 2001 From: Llywelwyn Date: Mon, 21 Aug 2023 10:13:29 +0100 Subject: [PATCH] abstracted reactions --- src/ai/adjacent_ai_system.rs | 42 ++++++++++++++---------------------- src/player.rs | 42 ++++++++++++++++++++---------------- src/raws/rawmaster.rs | 23 ++++++++++++++++++++ 3 files changed, 62 insertions(+), 45 deletions(-) diff --git a/src/ai/adjacent_ai_system.rs b/src/ai/adjacent_ai_system.rs index 0f9bd3a..7588759 100644 --- a/src/ai/adjacent_ai_system.rs +++ b/src/ai/adjacent_ai_system.rs @@ -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, factions: &ReadStorage, - 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)); }); } diff --git a/src/player.rs b/src/player.rs index 9efb7b3..3290ba0 100644 --- a/src/player.rs +++ b/src/player.rs @@ -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::(); let mut entity_moved = ecs.write_storage::(); let factions = ecs.read_storage::(); + let ancestries = ecs.read_storage::(); let pools = ecs.read_storage::(); let map = ecs.fetch::(); 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::(); + let ancestries = ecs.read_storage::(); // 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; } }); } diff --git a/src/raws/rawmaster.rs b/src/raws/rawmaster.rs index c537ed6..b5c44ac 100644 --- a/src/raws/rawmaster.rs +++ b/src/raws/rawmaster.rs @@ -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, + ancestries: &ReadStorage, + 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",