complete spatial indexing refactor - SpatialMap

This commit is contained in:
Llywelwyn 2023-08-16 01:17:38 +01:00
parent 2887bb9736
commit d439ff6d3f
18 changed files with 351 additions and 217 deletions

View file

@ -79,12 +79,12 @@ fn evaluate(
this_faction: &str,
reactions: &mut Vec<(Entity, Reaction)>,
) {
for other_entity in map.tile_content[idx].iter() {
if let Some(faction) = factions.get(*other_entity) {
crate::spatial::for_each_tile_content(idx, |other_entity| {
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()),
));
}
}
});
}

View file

@ -39,13 +39,12 @@ impl<'a> System<'a> for ApproachAI {
&mut *map,
);
if path.success && path.steps.len() > 1 {
let mut idx = map.xy_idx(pos.x, pos.y);
map.blocked[idx] = false;
let idx = map.xy_idx(pos.x, pos.y);
pos.x = path.steps[1] as i32 % map.width;
pos.y = path.steps[1] as i32 / map.width;
entity_moved.insert(entity, EntityMoved {}).expect("Unable to insert EntityMoved");
idx = map.xy_idx(pos.x, pos.y);
map.blocked[idx] = true;
let new_idx = map.xy_idx(pos.x, pos.y);
crate::spatial::move_entity(entity, idx, new_idx);
viewshed.dirty = true;
if let Some(telepath) = telepaths.get_mut(entity) {
telepath.dirty = true;

View file

@ -59,13 +59,12 @@ impl<'a> System<'a> for ChaseAI {
&mut *map,
);
if path.success && path.steps.len() > 1 && path.steps.len() < MAX_CHASE_DISTANCE {
let mut idx = map.xy_idx(pos.x, pos.y);
map.blocked[idx] = false;
let idx = map.xy_idx(pos.x, pos.y);
pos.x = path.steps[1] as i32 % map.width;
pos.y = path.steps[1] as i32 / map.width;
entity_moved.insert(entity, EntityMoved {}).expect("Failed to insert EntityMoved");
idx = map.xy_idx(pos.x, pos.y);
map.blocked[idx] = true;
let new_idx = map.xy_idx(pos.x, pos.y);
crate::spatial::move_entity(entity, idx, new_idx);
viewshed.dirty = true;
if let Some(is_telepath) = telepaths.get_mut(entity) {
is_telepath.dirty = true;

View file

@ -69,13 +69,12 @@ impl<'a> System<'a> for DefaultAI {
if x > 0 && x < map.width - 1 && y > 0 && y < map.height - 1 {
let dest_idx = map.xy_idx(x, y);
if !map.blocked[dest_idx] {
if !crate::spatial::is_blocked(dest_idx) {
let idx = map.xy_idx(pos.x, pos.y);
map.blocked[idx] = false;
pos.x = x;
pos.y = y;
entity_moved.insert(entity, EntityMoved {}).expect("Unable to insert EntityMoved");
map.blocked[dest_idx] = true;
crate::spatial::move_entity(entity, idx, dest_idx);
viewshed.dirty = true;
if let Some(is_telepath) = telepaths.get_mut(entity) {
is_telepath.dirty = true;
@ -88,13 +87,12 @@ impl<'a> System<'a> for DefaultAI {
// We have a path - follow it
let mut idx = map.xy_idx(pos.x, pos.y);
if path.len() > 1 {
if !map.blocked[path[1] as usize] {
map.blocked[idx] = false;
if !crate::spatial::is_blocked(path[1] as usize) {
pos.x = path[1] as i32 % map.width;
pos.y = path[1] as i32 / map.width;
entity_moved.insert(entity, EntityMoved {}).expect("Unable to insert EntityMoved");
idx = map.xy_idx(pos.x, pos.y);
map.blocked[idx] = true;
let new_idx = map.xy_idx(pos.x, pos.y);
crate::spatial::move_entity(entity, idx, new_idx);
viewshed.dirty = true;
if let Some(is_telepath) = telepaths.get_mut(entity) {
is_telepath.dirty = true;

View file

@ -38,9 +38,8 @@ impl<'a> System<'a> for FleeAI {
let flee_map = DijkstraMap::new(map.width as usize, map.height as usize, &fleeing.indices, &*map, 100.0);
let flee_target = DijkstraMap::find_highest_exit(&flee_map, my_idx, &*map);
if let Some(flee_target) = flee_target {
if !map.blocked[flee_target as usize] {
map.blocked[my_idx] = false;
map.blocked[flee_target as usize] = true;
if !crate::spatial::is_blocked(flee_target as usize) {
crate::spatial::move_entity(entity, my_idx, flee_target);
viewshed.dirty = true;
if let Some(is_telepath) = telepaths.get_mut(entity) {
is_telepath.dirty = true;

View file

@ -96,20 +96,27 @@ fn evaluate(
reactions: &mut Vec<(usize, Reaction, Entity)>,
minds: Option<&ReadStorage<Mind>>,
) {
for other_entity in map.tile_content[idx].iter() {
crate::spatial::for_each_tile_content(idx, |other_entity| {
// If minds are passed, we assume we're using telepathy here,
// so if the other entity is mindless, we skip it.
if minds.is_some() {
if minds.unwrap().get(*other_entity).is_none() {
continue;
if minds.unwrap().get(other_entity).is_some() {
if let Some(faction) = factions.get(other_entity) {
reactions.push((
idx,
crate::raws::faction_reaction(this_faction, &faction.name, &crate::raws::RAWS.lock().unwrap()),
other_entity,
));
}
}
} else {
if let Some(faction) = factions.get(other_entity) {
reactions.push((
idx,
crate::raws::faction_reaction(this_faction, &faction.name, &crate::raws::RAWS.lock().unwrap()),
other_entity,
));
}
}
if let Some(faction) = factions.get(*other_entity) {
reactions.push((
idx,
crate::raws::faction_reaction(this_faction, &faction.name, &crate::raws::RAWS.lock().unwrap()),
*other_entity,
));
}
}
});
}