Skip to content

Commit 26c9431

Browse files
committed
#59 #115 #57 for chapter 60.
1 parent 3bdd34d commit 26c9431

15 files changed

Lines changed: 161 additions & 180 deletions

chapter-60-caverns3/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-60-caverns3/src/ai/approach_ai_system.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ impl<'a> System<'a> for ApproachAI {
3030
&mut *map
3131
);
3232
if path.success && path.steps.len()>1 {
33-
let mut idx = map.xy_idx(pos.x, pos.y);
34-
map.blocked[idx] = false;
33+
let idx = map.xy_idx(pos.x, pos.y);
3534
pos.x = path.steps[1] as i32 % map.width;
3635
pos.y = path.steps[1] as i32 / map.width;
3736
entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
38-
idx = map.xy_idx(pos.x, pos.y);
39-
map.blocked[idx] = true;
37+
let new_idx = map.xy_idx(pos.x, pos.y);
38+
crate::spatial::move_entity(entity, idx, new_idx);
4039
viewshed.dirty = true;
4140
}
4241
}

chapter-60-caverns3/src/ai/chase_ai_system.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@ impl<'a> System<'a> for ChaseAI {
4848
&mut *map
4949
);
5050
if path.success && path.steps.len()>1 && path.steps.len()<15 {
51-
let mut idx = map.xy_idx(pos.x, pos.y);
52-
map.blocked[idx] = false;
51+
let idx = map.xy_idx(pos.x, pos.y);
5352
pos.x = path.steps[1] as i32 % map.width;
5453
pos.y = path.steps[1] as i32 / map.width;
5554
entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
56-
idx = map.xy_idx(pos.x, pos.y);
57-
map.blocked[idx] = true;
55+
let new_idx = map.xy_idx(pos.x, pos.y);
5856
viewshed.dirty = true;
57+
crate::spatial::move_entity(entity, idx, new_idx);
5958
turn_done.push(entity);
6059
} else {
6160
end_chase.push(entity);

chapter-60-caverns3/src/ai/default_move_system.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ impl<'a> System<'a> for DefaultMoveAI {
4343

4444
if x > 0 && x < map.width-1 && y > 0 && y < map.height-1 {
4545
let dest_idx = map.xy_idx(x, y);
46-
if !map.blocked[dest_idx] {
46+
if !crate::spatial::is_blocked(dest_idx) {
4747
let idx = map.xy_idx(pos.x, pos.y);
48-
map.blocked[idx] = false;
4948
pos.x = x;
5049
pos.y = y;
5150
entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
52-
map.blocked[dest_idx] = true;
51+
crate::spatial::move_entity(entity, idx, dest_idx);
5352
viewshed.dirty = true;
5453
}
5554
}
@@ -60,13 +59,12 @@ impl<'a> System<'a> for DefaultMoveAI {
6059
// We have a target - go there
6160
let mut idx = map.xy_idx(pos.x, pos.y);
6261
if path.len()>1 {
63-
if !map.blocked[path[1] as usize] {
64-
map.blocked[idx] = false;
62+
if !crate::spatial::is_blocked(path[1] as usize) {
6563
pos.x = path[1] as i32 % map.width;
6664
pos.y = path[1] as i32 / map.width;
6765
entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
68-
idx = map.xy_idx(pos.x, pos.y);
69-
map.blocked[idx] = true;
66+
let new_idx = map.xy_idx(pos.x, pos.y);
67+
crate::spatial::move_entity(entity, idx, new_idx);
7068
viewshed.dirty = true;
7169
path.remove(0); // Remove the first step in the path
7270
}

chapter-60-caverns3/src/ai/flee_ai_system.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@ impl<'a> System<'a> for FleeAI {
2525
{
2626
turn_done.push(entity);
2727
let my_idx = map.xy_idx(pos.x, pos.y);
28-
map.populate_blocked();
29-
let flee_map = rltk::DijkstraMap::new(map.width as usize, map.height as usize, &flee.indices, &*map, 100.0);
30-
let flee_target = rltk::DijkstraMap::find_highest_exit(&flee_map, my_idx, &*map);
31-
if let Some(flee_target) = flee_target {
32-
if !map.blocked[flee_target as usize] {
33-
map.blocked[my_idx] = false;
34-
map.blocked[flee_target as usize] = true;
35-
viewshed.dirty = true;
36-
pos.x = flee_target as i32 % map.width;
37-
pos.y = flee_target as i32 / map.width;
38-
entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
39-
}
28+
map.populate_blocked();
29+
let flee_map = rltk::DijkstraMap::new(map.width as usize, map.height as usize, &flee.indices, &*map, 100.0);
30+
let flee_target = rltk::DijkstraMap::find_highest_exit(&flee_map, my_idx, &*map);
31+
if let Some(flee_target) = flee_target {
32+
if !crate::spatial::is_blocked(flee_target as usize) {
33+
crate::spatial::move_entity(entity, my_idx, flee_target);
34+
viewshed.dirty = true;
35+
pos.x = flee_target as i32 % map.width;
36+
pos.y = flee_target as i32 / map.width;
37+
entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
4038
}
39+
}
4140
}
4241

4342
want_flee.clear();

chapter-60-caverns3/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-60-caverns3/src/inventory_system.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,15 @@ impl<'a> System<'a> for ItemUseSystem {
8585
None => {
8686
// Single target in tile
8787
let idx = map.xy_idx(target.x, target.y);
88-
for mob in map.tile_content[idx].iter() {
89-
targets.push(*mob);
90-
}
88+
crate::spatial::for_each_tile_content(idx, |mob| targets.push(mob) );
9189
}
9290
Some(area_effect) => {
9391
// AoE
9492
let mut blast_tiles = rltk::field_of_view(target, area_effect.radius, &*map);
9593
blast_tiles.retain(|p| p.x > 0 && p.x < map.width-1 && p.y > 0 && p.y < map.height-1 );
9694
for tile_idx in blast_tiles.iter() {
9795
let idx = map.xy_idx(tile_idx.x, tile_idx.y);
98-
for mob in map.tile_content[idx].iter() {
99-
targets.push(*mob);
100-
}
96+
crate::spatial::for_each_tile_content(idx, |mob| targets.push(mob));
10197
particle_builder.request(tile_idx.x, tile_idx.y, rltk::RGB::named(rltk::ORANGE), rltk::RGB::named(rltk::BLACK), rltk::to_cp437('░'), 200.0);
10298
}
10399
}

chapter-60-caverns3/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ mod lighting_system;
3939
mod ai;
4040
#[macro_use]
4141
extern crate lazy_static;
42-
43-
42+
pub mod spatial;
4443

4544
const SHOW_MAPGEN_VISUALIZER : bool = false;
4645

chapter-60-caverns3/src/map/dungeon.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ impl MasterDungeonMap {
2323
pub fn get_map(&self, depth : i32) -> Option<Map> {
2424
if self.maps.contains_key(&depth) {
2525
let mut result = self.maps[&depth].clone();
26-
result.tile_content = vec![Vec::new(); (result.width * result.height) as usize];
2726
Some(result)
2827
} else {
2928
None

chapter-60-caverns3/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(),

0 commit comments

Comments
 (0)