about summary refs log tree commit diff
path: root/src/level_gen/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/level_gen/util.rs')
-rw-r--r--src/level_gen/util.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/level_gen/util.rs b/src/level_gen/util.rs
new file mode 100644
index 000000000000..89a4a6a882da
--- /dev/null
+++ b/src/level_gen/util.rs
@@ -0,0 +1,31 @@
+use crate::types::Dimensions;
+use rand::{distributions, Rng};
+
+pub fn falses(dims: &Dimensions) -> Vec<Vec<bool>> {
+    let mut ret = Vec::with_capacity(dims.h as usize);
+    for _ in 0..dims.h {
+        let mut row = Vec::with_capacity(dims.w as usize);
+        for _ in 0..dims.w {
+            row.push(false);
+        }
+        ret.push(row);
+    }
+    ret
+}
+
+pub fn rand_initialize<R: Rng + ?Sized>(
+    dims: &Dimensions,
+    rng: &mut R,
+    alive_chance: f64,
+) -> Vec<Vec<bool>> {
+    let distrib = distributions::Bernoulli::new(alive_chance).unwrap();
+    let mut ret = Vec::with_capacity(dims.h as usize);
+    for _ in 0..dims.h {
+        let mut row = Vec::with_capacity(dims.w as usize);
+        for _ in 0..dims.w {
+            row.push(rng.sample(distrib));
+        }
+        ret.push(row);
+    }
+    ret
+}