Generic grids
[kaka/rust-sdl-test.git] / src / core / level / lvlgen.rs
index 3dcc3a7..e39f232 100644 (file)
@@ -1,5 +1,5 @@
 use {point, time_scope};
-use common::Point2D;
+use common::Point;
 use super::{Grid, Level};
 use noise::{NoiseFn, OpenSimplex, Seedable};
 use rand::Rng;
@@ -56,13 +56,13 @@ impl LevelGenerator {
     }
 
     #[allow(dead_code)]
-    fn simplex_noise(&self, grid: &mut Grid) {
+    fn simplex_noise(&self, grid: &mut Grid<bool>) {
        let noise = OpenSimplex::new().set_seed(self.seed);
        self.set_each(grid, |x, y| noise.get([x as f64 / 12.0, y as f64 / 12.0]) > 0.055, 1);
     }
 
     #[allow(dead_code)]
-    fn random_noise(&self, grid: &mut Grid) {
+    fn random_noise(&self, grid: &mut Grid<bool>) {
        let mut rng: rand::prelude::StdRng = rand::SeedableRng::seed_from_u64(self.seed as u64);
        let noise = OpenSimplex::new().set_seed(self.seed);
        self.set_each(grid, |_x, _y| rng.gen_range(0, 100) > (45 + (150.0 * noise.get([_x as f64 / 40.0, _y as f64 / 10.0])) as usize), 1); // more horizontal platforms
@@ -71,7 +71,7 @@ impl LevelGenerator {
     }
 
     #[allow(dead_code)]
-    fn smooth(&self, grid: &mut Grid) {
+    fn smooth(&self, grid: &mut Grid<bool>) {
        let distance = 1;
        for _i in 0..self.iterations {
            let mut next = vec!(vec!(true; grid.height); grid.width);
@@ -93,7 +93,7 @@ impl LevelGenerator {
     }
 
     #[allow(dead_code)]
-    fn smooth_until_equilibrium(&self, grid: &mut Grid) {
+    fn smooth_until_equilibrium(&self, grid: &mut Grid<bool>) {
        let distance = 1;
        let mut count = 0;
        loop {
@@ -129,7 +129,7 @@ impl LevelGenerator {
        count
     }
 
-    fn set_each<F: FnMut(usize, usize) -> bool>(&self, grid: &mut Grid, mut func: F, walls: usize) {
+    fn set_each<F: FnMut(usize, usize) -> bool>(&self, grid: &mut Grid<bool>, mut func: F, walls: usize) {
        for x in walls..(grid.width - walls) {
            for y in walls..(grid.height - walls) {
                grid.cells[x][y] = func(x, y);
@@ -137,7 +137,7 @@ impl LevelGenerator {
        }
     }
 
-    fn subdivide(&self, grid: &mut Grid) -> Grid {
+    fn subdivide(&self, grid: &mut Grid<bool>) -> Grid<bool> {
        let (width, height) = (grid.width * 2, grid.height * 2);
        let mut cells = vec!(vec!(true; height); width);
        for x in 1..(width - 1) {
@@ -153,7 +153,7 @@ impl LevelGenerator {
        }
     }
 
-    fn find_regions(&self, grid: &Grid) -> Vec<Region> {
+    fn find_regions(&self, grid: &Grid<bool>) -> Vec<Region> {
        time_scope!("finding all regions");
        let mut regions = vec!();
        let mut marked = vec!(vec!(false; grid.height); grid.width);
@@ -167,7 +167,7 @@ impl LevelGenerator {
        regions
     }
 
-    fn get_region_at_point(&self, grid: &Grid, x: usize, y: usize, marked: &mut Vec<Vec<bool>>) -> Region {
+    fn get_region_at_point(&self, grid: &Grid<bool>, x: usize, y: usize, marked: &mut Vec<Vec<bool>>) -> Region {
        let value = grid.cells[x][y];
        let mut cells = vec!();
        let mut queue = vec!((x, y));
@@ -190,13 +190,13 @@ impl LevelGenerator {
        Region { value, cells }
     }
 
-    fn delete_region(&self, grid: &mut Grid, region: &Region) {
+    fn delete_region(&self, grid: &mut Grid<bool>, region: &Region) {
        for c in &region.cells {
            grid.cells[c.0][c.1] = !region.value;
        }
     }
 
-    fn filter_regions(&self, grid: &mut Grid) {
+    fn filter_regions(&self, grid: &mut Grid<bool>) {
        let min_wall_size = 0.0015;
        println!("grid size: ({}, {}) = {} cells", grid.width, grid.height, grid.width * grid.height);
        println!("min wall size: {}", (grid.width * grid.height) as f64 * min_wall_size);
@@ -220,7 +220,7 @@ impl LevelGenerator {
        }
     }
 
-    fn find_walls(&self, grid: &Grid) -> Vec<Vec<Point2D<isize>>> {
+    fn find_walls(&self, grid: &Grid<bool>) -> Vec<Vec<Point<isize>>> {
        let mut walls = vec!();
        for r in self.find_regions(&grid) {
            if r.value {
@@ -256,7 +256,7 @@ impl Region {
        (min.0, min.1, 1 + max.0 - min.0, 1 + max.1 - min.1)
     }
 
-    pub fn outline(&self, scale: usize) -> Vec<Point2D<isize>> {
+    pub fn outline(&self, scale: usize) -> Vec<Point<isize>> {
        let rect = self.enclosing_rect();
        let (ox, oy, w, h) = rect;
        let grid = self.grid(&rect);
@@ -313,7 +313,7 @@ impl Region {
        grid
     }
 
-    fn find_first_point_of_outline(&self, rect: &(usize, usize, usize, usize), grid: &Vec<Vec<bool>>) -> Point2D<isize> {
+    fn find_first_point_of_outline(&self, rect: &(usize, usize, usize, usize), grid: &Vec<Vec<bool>>) -> Point<isize> {
        let (ox, oy, w, h) = rect;
        let is_outer_wall = (ox, oy) == (&0, &0); // we know this is always the outer wall of the level
        for x in 0..*w {
@@ -329,7 +329,7 @@ impl Region {
        panic!("no wall found!");
     }
 
-    fn find_next_point_of_outline(&self, grid: &Vec<Vec<bool>>, p: &mut Point2D<isize>, directions: &mut Vec<(isize, isize)>) {
+    fn find_next_point_of_outline(&self, grid: &Vec<Vec<bool>>, p: &mut Point<isize>, directions: &mut Vec<(isize, isize)>) {
        directions.rotate_left(2);
        loop {
            let d = directions[0];
@@ -341,7 +341,7 @@ impl Region {
        }
     }
 
-    fn check(&self, p: Point2D<isize>, grid: &Vec<Vec<bool>>) -> bool {
+    fn check(&self, p: Point<isize>, grid: &Vec<Vec<bool>>) -> bool {
        if p.x < 0 || p.x >= grid.len() as isize || p.y < 0 || p.y >= grid[0].len() as isize {
            false
        } else {