Browse Source

Chapter 29 - mazes is done.

Herbert Wolverson 4 years ago
parent
commit
f51ce8917e

+ 1 - 0
.vscode/spellright.dict

@@ -31,3 +31,4 @@ Voronoi
 Tommi
 Jalkanen
 Cogmind
+Cyucelen

BIN
book/src/c29-s1.gif


File diff suppressed because it is too large
+ 371 - 1
book/src/chapter_29.md


+ 1 - 1
chapter-29-mazes/src/main.rs

@@ -137,7 +137,7 @@ impl GameState for State {
                 draw_map(&self.mapgen_history[self.mapgen_index], ctx);
 
                 self.mapgen_timer += ctx.frame_time_ms;
-                if self.mapgen_timer > 5.0 {
+                if self.mapgen_timer > 400.0 {
                     self.mapgen_timer = 0.0;
                     self.mapgen_index += 1;
                     if self.mapgen_index == self.mapgen_history.len() {

+ 1 - 1
chapter-29-mazes/src/map_builders/common.rs

@@ -34,7 +34,7 @@ pub fn apply_vertical_tunnel(map : &mut Map, y1:i32, y2:i32, x:i32) {
 /// Searches a map, removes unreachable areas and returns the most distant tile.
 pub fn remove_unreachable_areas_returning_most_distant(map : &mut Map, start_idx : usize) -> usize {
     let map_starts : Vec<i32> = vec![start_idx as i32];
-    let dijkstra_map = rltk::DijkstraMap::new(map.width, map.height, &map_starts , map, 200.0);
+    let dijkstra_map = rltk::DijkstraMap::new(map.width, map.height, &map_starts , map, 300.0);
     let mut exit_tile = (0, 0.0f32);
     for (i, tile) in map.tiles.iter_mut().enumerate() {
         if *tile == TileType::Floor {

+ 9 - 9
chapter-29-mazes/src/map_builders/maze.rs

@@ -67,13 +67,9 @@ impl MazeBuilder {
         maze.generate_maze(self);
 
         // Find a starting point; start at the middle and walk left until we find an open tile
-        self.starting_position = Position{ x: self.map.width / 2, y : self.map.height / 2 };
-        let mut start_idx = self.map.xy_idx(self.starting_position.x, self.starting_position.y);
-        /*while self.map.tiles[start_idx] != TileType::Floor {
-            self.starting_position.x -= 1;
-            start_idx = self.map.xy_idx(self.starting_position.x, self.starting_position.y);
-        }
-        self.take_snapshot();*/
+        self.starting_position = Position{ x: 2, y : 2 };
+        let start_idx = self.map.xy_idx(self.starting_position.x, self.starting_position.y);
+        self.take_snapshot();
 
         // Find all tiles we can reach from the starting point
         let exit_tile = remove_unreachable_areas_returning_most_distant(&mut self.map, start_idx);
@@ -208,6 +204,7 @@ impl<'a> Grid<'a> {
     }
 
     fn generate_maze(&mut self, generator : &mut MazeBuilder) {
+        let mut i = 0;
         loop {
             self.cells[self.current].visited = true;
             let next = self.find_next_cell();
@@ -233,8 +230,11 @@ impl<'a> Grid<'a> {
                 }
             }
 
-            self.copy_to_map(&mut generator.map);
-            generator.take_snapshot();    
+            if i % 50 == 0 {
+                self.copy_to_map(&mut generator.map);
+                generator.take_snapshot();    
+            }
+            i += 1;
         }
     }
 

+ 4 - 4
chapter-29-mazes/src/map_builders/mod.rs

@@ -25,8 +25,8 @@ pub trait MapBuilder {
 }
 
 pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
-    /*let mut rng = rltk::RandomNumberGenerator::new();
-    let builder = rng.roll_dice(1, 7);
+    let mut rng = rltk::RandomNumberGenerator::new();
+    let builder = rng.roll_dice(1, 8);
     match builder {
         1 => Box::new(BspDungeonBuilder::new(new_depth)),
         2 => Box::new(BspInteriorBuilder::new(new_depth)),
@@ -34,8 +34,8 @@ pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
         4 => Box::new(DrunkardsWalkBuilder::open_area(new_depth)),
         5 => Box::new(DrunkardsWalkBuilder::open_halls(new_depth)),
         6 => Box::new(DrunkardsWalkBuilder::winding_passages(new_depth)),
+        7 => Box::new(MazeBuilder::new(new_depth))
         _ => Box::new(SimpleMapBuilder::new(new_depth))
-    }*/
-    Box::new(MazeBuilder::new(new_depth))
+    }    
 }