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 (w, h) = (map.width, map.height);
// Evaluate adjacent squares, add possible reactions
let mut eval_idx: usize = idx;
if pos.x > 0 {
evaluate(idx - 1, &map, &factions, &faction.name, &mut reactions);
eval_idx = idx - 1;
}
if pos.x < w - 1 {
evaluate(idx + 1, &map, &factions, &faction.name, &mut reactions);
eval_idx = idx + 1;
}
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 {
evaluate(idx + w as usize, &map, &factions, &faction.name, &mut reactions);
eval_idx = idx + w as usize;
}
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 {
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 {
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 {
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;
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.
fn evaluate(
idx: usize,
map: &Map,
factions: &ReadStorage<Faction>,
this_faction: &str,
reactions: &mut Vec<(Entity, Reaction)>,
) {
fn evaluate(idx: usize, factions: &ReadStorage<Faction>, this_faction: &str, reactions: &mut Vec<(Entity, Reaction)>) {
crate::spatial::for_each_tile_content(idx, |other_entity| {
if let Some(faction) = factions.get(other_entity) {
reactions.push((

View file

@ -85,7 +85,7 @@ impl<'a> System<'a> for DefaultAI {
Movement::RandomWaypoint { path } => {
if let Some(path) = path {
// 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 !crate::spatial::is_blocked(path[1] as usize) {
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() {
let idx = map.xy_idx(visible_tile.x, visible_tile.y);
if this_idx != idx {
evaluate(idx, &map, &factions, &faction.name, &mut reactions, None);
evaluate(idx, &factions, &faction.name, &mut reactions, None);
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
// account.
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(
idx: usize,
map: &Map,
factions: &ReadStorage<Faction>,
this_faction: &str,
reactions: &mut Vec<(usize, Reaction, Entity)>,