Skip to content

Commit a4321e1

Browse files
committed
#59 #115 #57 Chapter 71 spatial fixes.
1 parent 779bd56 commit a4321e1

16 files changed

Lines changed: 85 additions & 118 deletions

chapter-72-textlayers/src/ai/adjacent_ai_system.rs

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

7373
fn evaluate(idx : usize, map : &Map, factions : &ReadStorage<Faction>, my_faction : &str, reactions : &mut Vec<(Entity, Reaction)>) {
74-
for other_entity in map.tile_content[idx].iter() {
75-
if let Some(faction) = factions.get(*other_entity) {
74+
crate::spatial::for_each_tile_content(idx, |other_entity| {
75+
if let Some(faction) = factions.get(other_entity) {
7676
reactions.push((
77-
*other_entity,
77+
other_entity,
7878
crate::raws::faction_reaction(my_faction, &faction.name, &crate::raws::RAWS.lock().unwrap())
7979
));
8080
}
81-
}
81+
});
8282
}

chapter-72-textlayers/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-72-textlayers/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-72-textlayers/src/ai/visible_ai_system.rs

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

109109
fn evaluate(idx : usize, map : &Map, factions : &ReadStorage<Faction>, my_faction : &str, reactions : &mut Vec<(usize, Reaction, Entity)>) {
110-
for other_entity in map.tile_content[idx].iter() {
111-
if let Some(faction) = factions.get(*other_entity) {
110+
crate::spatial::for_each_tile_content(idx, |other_entity| {
111+
if let Some(faction) = factions.get(other_entity) {
112112
reactions.push((
113113
idx,
114114
crate::raws::faction_reaction(my_faction, &faction.name, &crate::raws::RAWS.lock().unwrap()),
115-
*other_entity
115+
other_entity
116116
));
117117
}
118-
}
118+
});
119119
}

chapter-72-textlayers/src/effects/damage.rs

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

6060
if let Some(pos) = entity_position(ecs, target) {
61-
let mut map_mut = ecs.fetch_mut::<Map>();
62-
map_mut.blocked[pos as usize] = false;
63-
std::mem::drop(map_mut);
61+
crate::spatial::remove_entity(target, pos as usize);
6462
}
6563

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

chapter-72-textlayers/src/effects/mod.rs

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

110110
fn affect_tile(ecs: &mut World, effect: &mut EffectSpawner, tile_idx : i32) {
111111
if tile_effect_hits_entities(&effect.effect_type) {
112-
let content = ecs.fetch::<Map>().tile_content[tile_idx as usize].clone();
112+
let content = crate::spatial::get_tile_content_clone(tile_idx as usize);
113113
content.iter().for_each(|entity| affect_entity(ecs, effect, *entity));
114114
}
115115

chapter-72-textlayers/src/gui/tooltips.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ pub fn draw_tooltips(ecs: &World, ctx : &mut Rltk) {
6666
if !map.visible_tiles[mouse_idx] { return; }
6767

6868
let mut tip_boxes : Vec<Tooltip> = Vec::new();
69-
for entity in map.tile_content[mouse_idx].iter().filter(|e| hidden.get(**e).is_none()) {
69+
crate::spatial::for_each_tile_content(mouse_idx, |entity| {
70+
if hidden.get(entity).is_some() { return; }
7071
let mut tip = Tooltip::new();
71-
tip.add(get_item_display_name(ecs, *entity));
72+
tip.add(get_item_display_name(ecs, entity));
7273

7374
// Comment on attributes
74-
let attr = attributes.get(*entity);
75+
let attr = attributes.get(entity);
7576
if let Some(attr) = attr {
7677
let mut s = "".to_string();
7778
if attr.might.bonus < 0 { s += "Weak. " };
@@ -89,7 +90,7 @@ pub fn draw_tooltips(ecs: &World, ctx : &mut Rltk) {
8990
}
9091

9192
// Comment on pools
92-
let stat = pools.get(*entity);
93+
let stat = pools.get(entity);
9394
if let Some(stat) = stat {
9495
tip.add(format!("Level: {}", stat.level));
9596
}
@@ -99,13 +100,13 @@ pub fn draw_tooltips(ecs: &World, ctx : &mut Rltk) {
99100
let durations = ecs.read_storage::<Duration>();
100101
let names = ecs.read_storage::<Name>();
101102
for (status, duration, name) in (&statuses, &durations, &names).join() {
102-
if status.target == *entity {
103+
if status.target == entity {
103104
tip.add(format!("{} ({})", name.name, duration.turns));
104105
}
105106
}
106107

107108
tip_boxes.push(tip);
108-
}
109+
});
109110

110111
if tip_boxes.is_empty() { return; }
111112

chapter-72-textlayers/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod movement_system;
4141
pub mod effects;
4242
#[macro_use]
4343
extern crate lazy_static;
44+
pub mod spatial;
4445

4546
const SHOW_MAPGEN_VISUALIZER : bool = false;
4647

chapter-72-textlayers/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-72-textlayers/src/map/mod.rs

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

3328
impl Map {
@@ -38,32 +33,30 @@ impl Map {
3833
fn is_exit_valid(&self, x:i32, y:i32) -> bool {
3934
if x < 1 || x > self.width-1 || y < 1 || y > self.height-1 { return false; }
4035
let idx = self.xy_idx(x, y);
41-
!self.blocked[idx]
36+
!crate::spatial::is_blocked(idx)
4237
}
4338

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

5043
pub fn populate_blocked_multi(&mut self, width : i32, height : i32) {
5144
self.populate_blocked();
5245
for y in 1 .. self.height-1 {
5346
for x in 1 .. self.width - 1 {
5447
let idx = self.xy_idx(x, y);
55-
if !self.blocked[idx] {
48+
if !crate::spatial::is_blocked(idx) {
5649
for cy in 0..height {
5750
for cx in 0..width {
5851
let tx = x + cx;
5952
let ty = y + cy;
6053
if tx < self.width-1 && ty < self.height-1 {
6154
let tidx = self.xy_idx(tx, ty);
62-
if self.blocked[tidx] {
63-
self.blocked[idx] = true;
55+
if crate::spatial::is_blocked(tidx) {
56+
crate::spatial::set_blocked(idx, true);
6457
}
6558
} else {
66-
self.blocked[idx] = true;
59+
crate::spatial::set_blocked(idx, true);
6760
}
6861
}
6962
}
@@ -73,22 +66,19 @@ impl Map {
7366
}
7467

7568
pub fn clear_content_index(&mut self) {
76-
for content in self.tile_content.iter_mut() {
77-
content.clear();
78-
}
69+
crate::spatial::clear();
7970
}
8071

8172
/// Generates an empty map, consisting entirely of solid walls
8273
pub fn new<S : ToString>(new_depth : i32, width: i32, height: i32, name: S) -> Map {
8374
let map_tile_count = (width*height) as usize;
75+
crate::spatial::set_size(map_tile_count);
8476
Map{
8577
tiles : vec![TileType::Wall; map_tile_count],
8678
width,
8779
height,
8880
revealed_tiles : vec![false; map_tile_count],
8981
visible_tiles : vec![false; map_tile_count],
90-
blocked : vec![false; map_tile_count],
91-
tile_content : vec![Vec::new(); map_tile_count],
9282
depth: new_depth,
9383
bloodstains: HashSet::new(),
9484
view_blocked : HashSet::new(),

0 commit comments

Comments
 (0)