cleanup and bugfixes

This commit is contained in:
Llywelwyn 2023-08-16 01:23:57 +01:00
parent d439ff6d3f
commit 2ba7cfad8d
7 changed files with 20 additions and 24 deletions

View file

@ -27,29 +27,33 @@ impl<'a> System<'a> for AdjacentAI {
let idx = map.xy_idx(pos.x, pos.y); let idx = map.xy_idx(pos.x, pos.y);
let (w, h) = (map.width, map.height); let (w, h) = (map.width, map.height);
// Evaluate adjacent squares, add possible reactions // Evaluate adjacent squares, add possible reactions
let mut eval_idx: usize = idx;
if pos.x > 0 { if pos.x > 0 {
evaluate(idx - 1, &map, &factions, &faction.name, &mut reactions); eval_idx = idx - 1;
} }
if pos.x < w - 1 { if pos.x < w - 1 {
evaluate(idx + 1, &map, &factions, &faction.name, &mut reactions); eval_idx = idx + 1;
} }
if pos.y > 0 { if pos.y > 0 {
evaluate(idx - w as usize, &map, &factions, &faction.name, &mut reactions); eval_idx = idx - w as usize;
} }
if pos.y < h - 1 { if pos.y < h - 1 {
evaluate(idx + w as usize, &map, &factions, &faction.name, &mut reactions); eval_idx = idx + w as usize;
} }
if pos.y > 0 && pos.x > 0 { if pos.y > 0 && pos.x > 0 {
evaluate((idx - w as usize) - 1, &map, &factions, &faction.name, &mut reactions); eval_idx = (idx - w as usize) - 1;
} }
if pos.y > 0 && pos.x < w - 1 { if pos.y > 0 && pos.x < w - 1 {
evaluate((idx - w as usize) + 1, &map, &factions, &faction.name, &mut reactions); eval_idx = (idx - w as usize) + 1;
} }
if pos.y < h - 1 && pos.x > 0 { if pos.y < h - 1 && pos.x > 0 {
evaluate((idx + w as usize) - 1, &map, &factions, &faction.name, &mut reactions); eval_idx = (idx + w as usize) - 1;
} }
if pos.y < h - 1 && pos.x < w - 1 { if pos.y < h - 1 && pos.x < w - 1 {
evaluate((idx + w as usize) + 1, &map, &factions, &faction.name, &mut reactions); eval_idx = (idx + w as usize) + 1;
}
if eval_idx != idx {
evaluate(eval_idx, &factions, &faction.name, &mut reactions);
} }
let mut done = false; let mut done = false;
for reaction in reactions.iter() { for reaction in reactions.iter() {
@ -72,13 +76,7 @@ impl<'a> System<'a> for AdjacentAI {
} }
/// Evaluates all possible reactions between this faction and all entities on a given tile idx. /// Evaluates all possible reactions between this faction and all entities on a given tile idx.
fn evaluate( fn evaluate(idx: usize, factions: &ReadStorage<Faction>, this_faction: &str, reactions: &mut Vec<(Entity, Reaction)>) {
idx: usize,
map: &Map,
factions: &ReadStorage<Faction>,
this_faction: &str,
reactions: &mut Vec<(Entity, Reaction)>,
) {
crate::spatial::for_each_tile_content(idx, |other_entity| { crate::spatial::for_each_tile_content(idx, |other_entity| {
if let Some(faction) = factions.get(other_entity) { if let Some(faction) = factions.get(other_entity) {
reactions.push(( reactions.push((

View file

@ -85,7 +85,7 @@ impl<'a> System<'a> for DefaultAI {
Movement::RandomWaypoint { path } => { Movement::RandomWaypoint { path } => {
if let Some(path) = path { if let Some(path) = path {
// We have a path - follow it // We have a path - follow it
let mut idx = map.xy_idx(pos.x, pos.y); let idx = map.xy_idx(pos.x, pos.y);
if path.len() > 1 { if path.len() > 1 {
if !crate::spatial::is_blocked(path[1] as usize) { if !crate::spatial::is_blocked(path[1] as usize) {
pos.x = path[1] as i32 % map.width; pos.x = path[1] as i32 % map.width;

View file

@ -50,7 +50,7 @@ impl<'a> System<'a> for VisibleAI {
for visible_tile in viewshed.visible_tiles.iter() { for visible_tile in viewshed.visible_tiles.iter() {
let idx = map.xy_idx(visible_tile.x, visible_tile.y); let idx = map.xy_idx(visible_tile.x, visible_tile.y);
if this_idx != idx { if this_idx != idx {
evaluate(idx, &map, &factions, &faction.name, &mut reactions, None); evaluate(idx, &factions, &faction.name, &mut reactions, None);
idxs.insert(idx); idxs.insert(idx);
} }
} }
@ -61,7 +61,7 @@ impl<'a> System<'a> for VisibleAI {
// and it's not the idx we're standing on, then evaluate here w/ minds taken into // and it's not the idx we're standing on, then evaluate here w/ minds taken into
// account. // account.
if this_idx != idx && idxs.contains(&idx) { if this_idx != idx && idxs.contains(&idx) {
evaluate(idx, &map, &factions, &faction.name, &mut reactions, Some(&minds)); evaluate(idx, &factions, &faction.name, &mut reactions, Some(&minds));
} }
} }
} }
@ -90,7 +90,6 @@ impl<'a> System<'a> for VisibleAI {
fn evaluate( fn evaluate(
idx: usize, idx: usize,
map: &Map,
factions: &ReadStorage<Faction>, factions: &ReadStorage<Faction>,
this_faction: &str, this_faction: &str,
reactions: &mut Vec<(usize, Reaction, Entity)>, reactions: &mut Vec<(usize, Reaction, Entity)>,

View file

@ -50,7 +50,7 @@ impl MasterDungeonMap {
/// Gets a map by ID from the MasterDungeonMap /// Gets a map by ID from the MasterDungeonMap
pub fn get_map(&self, id: i32) -> Option<Map> { pub fn get_map(&self, id: i32) -> Option<Map> {
if self.maps.contains_key(&id) { if self.maps.contains_key(&id) {
let mut result = self.maps[&id].clone(); let result = self.maps[&id].clone();
return Some(result); return Some(result);
} else { } else {
return None; return None;

View file

@ -1,6 +1,5 @@
use rltk::{Algorithm2D, BaseMap, Point}; use rltk::{Algorithm2D, BaseMap, Point};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use specs::prelude::*;
use std::collections::HashSet; use std::collections::HashSet;
pub mod colours; pub mod colours;
mod glyphs; mod glyphs;

View file

@ -4,10 +4,10 @@ use specs::prelude::*;
pub struct MapIndexingSystem {} pub struct MapIndexingSystem {}
impl<'a> System<'a> for MapIndexingSystem { impl<'a> System<'a> for MapIndexingSystem {
type SystemData = (WriteExpect<'a, Map>, ReadStorage<'a, Position>, ReadStorage<'a, BlocksTile>, Entities<'a>); type SystemData = (ReadExpect<'a, Map>, ReadStorage<'a, Position>, ReadStorage<'a, BlocksTile>, Entities<'a>);
fn run(&mut self, data: Self::SystemData) { fn run(&mut self, data: Self::SystemData) {
let (mut map, position, blockers, entities) = data; let (map, position, blockers, entities) = data;
spatial::clear(); spatial::clear();
spatial::populate_blocked_from_map(&*map); spatial::populate_blocked_from_map(&*map);

View file

@ -141,7 +141,7 @@ pub fn move_entity(entity: Entity, moving_from: usize, moving_to: usize) {
}); });
lock.tile_content[moving_to].iter().for_each(|(_, blocks)| { lock.tile_content[moving_to].iter().for_each(|(_, blocks)| {
if *blocks { if *blocks {
from_blocked = true; to_blocked = true;
} }
}); });
lock.blocked[moving_from].1 = from_blocked; lock.blocked[moving_from].1 = from_blocked;