Skip to content

Commit e942769

Browse files
committed
#59 #115 #57 - Chapter 68 update spatial.
1 parent 27c0b9c commit e942769

17 files changed

Lines changed: 182 additions & 113 deletions

chapter-68-mushrooms/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-68-mushrooms/src/ai/default_move_system.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ 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] {
45+
if !crate::spatial::is_blocked(dest_idx) {
4646
apply_move.insert(entity, ApplyMove{ dest_idx : dest_idx })
4747
.expect("Unable to insert");
4848
turn_done.push(entity);
@@ -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-68-mushrooms/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-68-mushrooms/src/ai/visible_ai_system.rs

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

8989
fn evaluate(idx : usize, map : &Map, factions : &ReadStorage<Faction>, my_faction : &str, reactions : &mut Vec<(usize, Reaction, Entity)>) {
90-
for other_entity in map.tile_content[idx].iter() {
91-
if let Some(faction) = factions.get(*other_entity) {
90+
crate::spatial::for_each_tile_content(idx, |other_entity| {
91+
if let Some(faction) = factions.get(other_entity) {
9292
reactions.push((
9393
idx,
9494
crate::raws::faction_reaction(my_faction, &faction.name, &crate::raws::RAWS.lock().unwrap()),
95-
*other_entity
95+
other_entity
9696
));
9797
}
98-
}
98+
});
9999
}

chapter-68-mushrooms/src/effects/damage.rs

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

5252
if let Some(pos) = entity_position(ecs, target) {
53-
let mut map_mut = ecs.fetch_mut::<Map>();
54-
map_mut.blocked[pos as usize] = false;
55-
std::mem::drop(map_mut);
53+
crate::spatial::remove_entity(target, pos as usize);
5654
}
5755

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

chapter-68-mushrooms/src/effects/mod.rs

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

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

chapter-68-mushrooms/src/gui.rs

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

302302
let mut tip_boxes : Vec<Tooltip> = Vec::new();
303-
for entity in map.tile_content[mouse_idx].iter().filter(|e| hidden.get(**e).is_none()) {
303+
crate::spatial::for_each_tile_content(mouse_idx, |entity| {
304+
if hidden.get(entity).is_some() { return; }
304305
let mut tip = Tooltip::new();
305-
tip.add(get_item_display_name(ecs, *entity));
306+
tip.add(get_item_display_name(ecs, entity));
306307

307308
// Comment on attributes
308-
let attr = attributes.get(*entity);
309+
let attr = attributes.get(entity);
309310
if let Some(attr) = attr {
310311
let mut s = "".to_string();
311312
if attr.might.bonus < 0 { s += "Weak. " };
@@ -323,7 +324,7 @@ fn draw_tooltips(ecs: &World, ctx : &mut Rltk) {
323324
}
324325

325326
// Comment on pools
326-
let stat = pools.get(*entity);
327+
let stat = pools.get(entity);
327328
if let Some(stat) = stat {
328329
tip.add(format!("Level: {}", stat.level));
329330
}
@@ -333,13 +334,13 @@ fn draw_tooltips(ecs: &World, ctx : &mut Rltk) {
333334
let durations = ecs.read_storage::<Duration>();
334335
let names = ecs.read_storage::<Name>();
335336
for (status, duration, name) in (&statuses, &durations, &names).join() {
336-
if status.target == *entity {
337+
if status.target == entity {
337338
tip.add(format!("{} ({})", name.name, duration.turns));
338339
}
339340
}
340341

341342
tip_boxes.push(tip);
342-
}
343+
});
343344

344345
if tip_boxes.is_empty() { return; }
345346

chapter-68-mushrooms/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-68-mushrooms/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-68-mushrooms/src/map/mod.rs

Lines changed: 8 additions & 18 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,30 @@ 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 populate_blocked_multi(&mut self, width : i32, height : i32) {
5043
self.populate_blocked();
5144
for y in 1 .. self.height-1 {
5245
for x in 1 .. self.width - 1 {
5346
let idx = self.xy_idx(x, y);
54-
if !self.blocked[idx] {
47+
if !crate::spatial::is_blocked(idx) {
5548
for cy in 0..height {
5649
for cx in 0..width {
5750
let tx = x + cx;
5851
let ty = y + cy;
5952
if tx < self.width-1 && ty < self.height-1 {
6053
let tidx = self.xy_idx(tx, ty);
61-
if self.blocked[tidx] {
62-
self.blocked[idx] = true;
54+
if crate::spatial::is_blocked(tidx) {
55+
crate::spatial::set_blocked(idx, true);
6356
}
6457
} else {
65-
self.blocked[idx] = true;
58+
crate::spatial::set_blocked(idx, true);
6659
}
6760
}
6861
}
@@ -72,22 +65,19 @@ impl Map {
7265
}
7366

7467
pub fn clear_content_index(&mut self) {
75-
for content in self.tile_content.iter_mut() {
76-
content.clear();
77-
}
68+
crate::spatial::clear();
7869
}
7970

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

0 commit comments

Comments
 (0)