Skip to content

Commit 39b91c9

Browse files
committed
#59 #115 #57 - Chapter 65 spatial work.
1 parent e23acfe commit 39b91c9

16 files changed

Lines changed: 168 additions & 87 deletions

chapter-65-items/src/ai/adjacent_ai_system.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ impl<'a> System<'a> for AdjacentAI {
5555
}
5656

5757
fn evaluate(idx : usize, map : &Map, factions : &ReadStorage<Faction>, my_faction : &str, reactions : &mut Vec<(Entity, Reaction)>) {
58-
for other_entity in map.tile_content[idx].iter() {
59-
if let Some(faction) = factions.get(*other_entity) {
58+
crate::spatial::for_each_tile_content(idx, |other_entity| {
59+
if let Some(faction) = factions.get(other_entity) {
6060
reactions.push((
61-
*other_entity,
61+
other_entity,
6262
crate::raws::faction_reaction(my_faction, &faction.name, &crate::raws::RAWS.lock().unwrap())
6363
));
6464
}
65-
}
65+
});
6666
}

chapter-65-items/src/ai/default_move_system.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ impl<'a> System<'a> for DefaultMoveAI {
4242

4343
if x > 0 && x < map.width-1 && y > 0 && y < map.height-1 {
4444
let dest_idx = map.xy_idx(x, y);
45-
if !map.blocked[dest_idx] {
46-
apply_move.insert(entity, ApplyMove{ dest_idx : dest_idx })
45+
if !crate::spatial::is_blocked(dest_idx) {
46+
apply_move.insert(entity, ApplyMove{ dest_idx })
4747
.expect("Unable to insert");
4848
turn_done.push(entity);
4949
}
@@ -54,7 +54,7 @@ impl<'a> System<'a> for DefaultMoveAI {
5454
if let Some(path) = path {
5555
// We have a target - go there
5656
if path.len()>1 {
57-
if !map.blocked[path[1] as usize] {
57+
if !crate::spatial::is_blocked(path[1] as usize) {
5858
apply_move.insert(entity, ApplyMove{ dest_idx : path[1] })
5959
.expect("Unable to insert");
6060
path.remove(0); // Remove the first step in the path

chapter-65-items/src/ai/flee_ai_system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<'a> System<'a> for FleeAI {
2828
let flee_map = rltk::DijkstraMap::new(map.width as usize, map.height as usize, &flee.indices, &*map, 100.0);
2929
let flee_target = rltk::DijkstraMap::find_highest_exit(&flee_map, my_idx, &*map);
3030
if let Some(flee_target) = flee_target {
31-
if !map.blocked[flee_target as usize] {
31+
if !crate::spatial::is_blocked(flee_target as usize) {
3232
apply_move.insert(entity, ApplyMove{ dest_idx : flee_target }).expect("Unable to insert");
3333
turn_done.push(entity);
3434
}

chapter-65-items/src/ai/visible_ai_system.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ impl<'a> System<'a> for VisibleAI {
5959
}
6060

6161
fn evaluate(idx : usize, map : &Map, factions : &ReadStorage<Faction>, my_faction : &str, reactions : &mut Vec<(usize, Reaction, Entity)>) {
62-
for other_entity in map.tile_content[idx].iter() {
63-
if let Some(faction) = factions.get(*other_entity) {
62+
crate::spatial::for_each_tile_content(idx, |other_entity| {
63+
if let Some(faction) = factions.get(other_entity) {
6464
reactions.push((
6565
idx,
6666
crate::raws::faction_reaction(my_faction, &faction.name, &crate::raws::RAWS.lock().unwrap()),
67-
*other_entity
67+
other_entity
6868
));
6969
}
70-
}
70+
});
7171
}

chapter-65-items/src/effects/damage.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ pub fn death(ecs: &mut World, effect: &EffectSpawner, target : Entity) {
4545
let attributes = ecs.read_storage::<Attributes>();
4646

4747
if let Some(pos) = entity_position(ecs, target) {
48-
let mut map_mut = ecs.fetch_mut::<Map>();
49-
map_mut.blocked[pos as usize] = false;
50-
std::mem::drop(map_mut);
48+
crate::spatial::remove_entity(target, pos as usize);
5149
}
5250

5351
if let Some(source) = effect.creator {

chapter-65-items/src/effects/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn tile_effect_hits_entities(effect: &EffectType) -> bool {
9696

9797
fn affect_tile(ecs: &mut World, effect: &EffectSpawner, tile_idx : i32) {
9898
if tile_effect_hits_entities(&effect.effect_type) {
99-
let content = ecs.fetch::<Map>().tile_content[tile_idx as usize].clone();
99+
let content = crate::spatial::get_tile_content_clone(tile_idx as usize);
100100
content.iter().for_each(|entity| affect_entity(ecs, effect, *entity));
101101
}
102102

chapter-65-items/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ mod movement_system;
4040
pub mod effects;
4141
#[macro_use]
4242
extern crate lazy_static;
43-
44-
43+
pub mod spatial;
4544

4645
const SHOW_MAPGEN_VISUALIZER : bool = false;
4746

chapter-65-items/src/map/dungeon.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ impl MasterDungeonMap {
4545
pub fn get_map(&self, depth : i32) -> Option<Map> {
4646
if self.maps.contains_key(&depth) {
4747
let mut result = self.maps[&depth].clone();
48-
result.tile_content = vec![Vec::new(); (result.width * result.height) as usize];
4948
Some(result)
5049
} else {
5150
None

chapter-65-items/src/map/mod.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@ pub struct Map {
1616
pub height : i32,
1717
pub revealed_tiles : Vec<bool>,
1818
pub visible_tiles : Vec<bool>,
19-
pub blocked : Vec<bool>,
2019
pub depth : i32,
2120
pub bloodstains : HashSet<usize>,
2221
pub view_blocked : HashSet<usize>,
2322
pub name : String,
2423
pub outdoors : bool,
2524
pub light : Vec<rltk::RGB>,
26-
27-
#[serde(skip_serializing)]
28-
#[serde(skip_deserializing)]
29-
pub tile_content : Vec<Vec<Entity>>
3025
}
3126

3227
impl Map {
@@ -37,32 +32,27 @@ impl Map {
3732
fn is_exit_valid(&self, x:i32, y:i32) -> bool {
3833
if x < 1 || x > self.width-1 || y < 1 || y > self.height-1 { return false; }
3934
let idx = self.xy_idx(x, y);
40-
!self.blocked[idx]
35+
!crate::spatial::is_blocked(idx)
4136
}
4237

4338
pub fn populate_blocked(&mut self) {
44-
for (i,tile) in self.tiles.iter_mut().enumerate() {
45-
self.blocked[i] = !tile_walkable(*tile);
46-
}
39+
crate::spatial::populate_blocked_from_map(self);
4740
}
4841

4942
pub fn clear_content_index(&mut self) {
50-
for content in self.tile_content.iter_mut() {
51-
content.clear();
52-
}
43+
crate::spatial::clear();
5344
}
5445

5546
/// Generates an empty map, consisting entirely of solid walls
5647
pub fn new<S : ToString>(new_depth : i32, width: i32, height: i32, name: S) -> Map {
5748
let map_tile_count = (width*height) as usize;
49+
crate::spatial::set_size(map_tile_count);
5850
Map{
5951
tiles : vec![TileType::Wall; map_tile_count],
6052
width,
6153
height,
6254
revealed_tiles : vec![false; map_tile_count],
6355
visible_tiles : vec![false; map_tile_count],
64-
blocked : vec![false; map_tile_count],
65-
tile_content : vec![Vec::new(); map_tile_count],
6656
depth: new_depth,
6757
bloodstains: HashSet::new(),
6858
view_blocked : HashSet::new(),
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
use specs::prelude::*;
2-
use super::{Map, Position, BlocksTile};
2+
use super::{Map, Position, BlocksTile, Pools, spatial};
33

44
pub struct MapIndexingSystem {}
55

66
impl<'a> System<'a> for MapIndexingSystem {
7-
type SystemData = ( WriteExpect<'a, Map>,
7+
type SystemData = ( ReadExpect<'a, Map>,
88
ReadStorage<'a, Position>,
99
ReadStorage<'a, BlocksTile>,
10+
ReadStorage<'a, Pools>,
1011
Entities<'a>,);
1112

1213
fn run(&mut self, data : Self::SystemData) {
13-
let (mut map, position, blockers, entities) = data;
14+
let (map, position, blockers, pools, entities) = data;
1415

15-
map.populate_blocked();
16-
map.clear_content_index();
16+
spatial::clear();
17+
spatial::populate_blocked_from_map(&*map);
1718
for (entity, position) in (&entities, &position).join() {
18-
let idx = map.xy_idx(position.x, position.y);
19-
20-
// If they block, update the blocking list
21-
let _p : Option<&BlocksTile> = blockers.get(entity);
22-
if let Some(_p) = _p {
23-
map.blocked[idx] = true;
19+
let mut alive = true;
20+
if let Some(pools) = pools.get(entity) {
21+
if pools.hit_points.current < 1 {
22+
alive = false;
23+
}
24+
}
25+
if alive {
26+
let idx = map.xy_idx(position.x, position.y);
27+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
2428
}
25-
26-
// Push the entity to the appropriate index slot. It's a Copy
27-
// type, so we don't need to clone it (we want to avoid moving it out of the ECS!)
28-
map.tile_content[idx].push(entity);
2929
}
3030
}
3131
}

0 commit comments

Comments
 (0)