SHARE
TWEET

maize lol

a guest Jan 18th, 2020 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use rand::prelude::*;
  2.  
  3. // Constants for the dimensions of the maze.
  4. const WIDTH: usize = 30;
  5. const HEIGHT: usize = 30;
  6.  
  7. // The index of the current row being generated.
  8. static mut CURRENT_ROW: usize = 0;
  9.  
  10. // Row information arrays.
  11. static mut GROUP: [usize; WIDTH] = [0usize; WIDTH];
  12. static mut WALL: [bool; WIDTH] = [false; WIDTH];
  13. static mut FLOOR: [bool; WIDTH] = [false; WIDTH];
  14.  
  15. // Probabilities for building walls and floors.
  16. // Wall parameter can be tweaked to introduce bias toward walls or floors.
  17. const WALL_P: f32 = 0.5;
  18. const FLOOR_P: f32 = 1.0 - WALL_P;
  19.  
  20. // Counter for allocating group IDs.
  21. static mut COUNTER: usize = 0;
  22.  
  23. // Helper function for allocating cell groups, automatically increments COUNTER.
  24. fn next_group() -> usize {
  25.     unsafe {
  26.         let tmp = COUNTER;
  27.         COUNTER += 1;
  28.         return tmp;
  29.     }
  30. }
  31.  
  32. // returns true or false based on weighted coin flip.
  33. fn choose(threshold: f32) -> bool {
  34.     let mut rng = rand::thread_rng();
  35.     let sample: f32 = rng.gen();
  36.     return sample < threshold;
  37. }
  38.  
  39. unsafe fn is_first_row() -> bool {
  40.     return CURRENT_ROW == 0;
  41. }
  42.  
  43. unsafe fn is_last_row() -> bool {
  44.     return CURRENT_ROW == HEIGHT - 1;
  45. }
  46.  
  47. fn draw_ceiling() {
  48.     for _ in 0..WIDTH {
  49.         print!(" ___");
  50.     }
  51.     println!("");
  52. }
  53.  
  54. unsafe fn draw_row() {
  55.     print!("|");
  56.     for i in 0..WIDTH {
  57.         let f = if FLOOR[i] { "_" } else { " " };
  58.         let w = if WALL[i] { "|" } else { " " };
  59.         print!("{f}{f}{f}{w}", f=f, w=w);
  60.     }
  61.     println!("");
  62. }
  63.  
  64. unsafe fn prep_row() {
  65.     if is_first_row() {
  66.         for i in 0..WIDTH {
  67.             GROUP[i] = next_group();
  68.         }
  69.     } else {
  70.         for i in 0..WIDTH {
  71.             WALL[i] = false;
  72.             if FLOOR[i] {
  73.                 FLOOR[i] = false;
  74.                 GROUP[i] = next_group();
  75.             }
  76.         }
  77.     }
  78. }
  79.  
  80. unsafe fn add_walls() {
  81.     for i in 0..WIDTH {
  82.         if i == WIDTH - 1 || (!is_first_row() && GROUP[i] == GROUP[i+1]) || choose(WALL_P) {
  83.             WALL[i] = true;
  84.         } else {
  85.             GROUP[i+1] = GROUP[i];
  86.         }
  87.     }
  88. }
  89.  
  90. unsafe fn add_floors() {
  91.     unsafe fn add_floors_to_group(group_start: usize, group_end: usize) {
  92.         let mut open_floors = group_end - group_start;
  93.         let mut i = group_start;
  94.         while open_floors > 1 && i < group_end {
  95.             if choose(FLOOR_P) {
  96.                 FLOOR[i] = true;
  97.                 open_floors -= 1;
  98.             }
  99.             i += 1;
  100.         }
  101.     }
  102.  
  103.     let mut i = 0;
  104.     let mut curr_group = GROUP[0];
  105.     let mut curr_group_start = 0;
  106.     while i <= WIDTH {
  107.         if i == WIDTH {
  108.             add_floors_to_group(curr_group_start, i);
  109.         } else if is_last_row() {
  110.             FLOOR[i] = true;
  111.         } else if GROUP[i] != curr_group {
  112.             add_floors_to_group(curr_group_start, i);
  113.             curr_group = GROUP[i];
  114.             curr_group_start = i;
  115.         }
  116.         i += 1;
  117.     }
  118. }
  119.  
  120. fn main() {
  121.     draw_ceiling();
  122.     unsafe {
  123.         while CURRENT_ROW < HEIGHT {
  124.             prep_row();
  125.             add_walls();
  126.             add_floors();
  127.  
  128.             // Do special wall cleanup on the last row
  129.             if is_last_row() {
  130.                 for i in 0..WIDTH-1 {
  131.                     if WALL[i] && GROUP[i] != GROUP[i+1] {
  132.                         WALL[i] = false;
  133.                     }
  134.                 }
  135.             }
  136.  
  137.             draw_row();
  138.             CURRENT_ROW += 1;
  139.         }
  140.     }
  141. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top