2 use ::{point, time_scope};
3 use core::render::Renderer;
4 use noise::{NoiseFn, OpenSimplex, Seedable};
6 use sprites::SpriteManager;
8 ////////// LEVEL ///////////////////////////////////////////////////////////////
12 pub gravity: Point2D<f64>,
18 pub fn new(gravity: Point2D<f64>) -> Self {
19 Level { gravity, grid: Grid::generate(10), iterations: 10 }
22 pub fn regenerate(&mut self) {
23 self.grid = Grid::generate(self.iterations);
26 pub fn increase_iteration(&mut self) {
29 println!("iterate {} time(s)", self.iterations);
32 pub fn decrease_iteration(&mut self) {
35 println!("iterate {} time(s)", self.iterations);
38 pub fn filter_regions(&mut self) {
39 self.grid.filter_regions();
42 pub fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager) {
43 renderer.canvas().set_draw_color((64, 64, 64));
44 let size = self.grid.cell_size;
45 for x in 0..self.grid.width {
46 for y in 0..self.grid.height {
47 if self.grid.cells[x][y] {
48 renderer.canvas().fill_rect(sdl2::rect::Rect::new(x as i32 * size as i32, y as i32 * size as i32, size as u32, size as u32)).unwrap();
55 ////////// GRID ////////////////////////////////////////////////////////////////
62 pub cells: Vec<Vec<bool>>,
66 fn generate(iterations: u8) -> Grid {
67 time_scope!("grid generation");
70 let (width, height) = (2560 / cell_size, 1440 / cell_size);
76 cells: vec!(vec!(true; height); width),
79 // start with some noise
80 // grid.simplex_noise();
83 // smooth with cellular automata
84 grid.smooth(iterations);
85 // grid.smooth_until_equilibrium();
87 // increase resolution
89 grid = grid.subdivide();
90 grid.smooth(iterations);
97 fn simplex_noise(&mut self) {
98 let noise = OpenSimplex::new().set_seed(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as u32);
99 self.set_each(|x, y| noise.get([x as f64 / 12.0, y as f64 / 12.0]) > 0.055, 1);
103 fn random_noise(&mut self) {
104 let mut rng = rand::thread_rng();
105 let noise = OpenSimplex::new().set_seed(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as u32);
106 self.set_each(|_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
107 // let w = self.width as f64;
108 // self.set_each(|_x, _y| rng.gen_range(0, 100) > (45 + ((15 * _x) as f64 / w) as usize), 1); // opens up to the right
112 fn smooth(&mut self, iterations: u8) {
114 for _i in 0..iterations {
115 let mut next = vec!(vec!(true; self.height); self.width);
116 for x in distance..(self.width - distance) {
117 for y in distance..(self.height - distance) {
118 match Grid::neighbours(&self.cells, x, y, distance) {
119 n if n < 4 => next[x][y] = false,
120 n if n > 4 => next[x][y] = true,
121 _ => next[x][y] = self.cells[x][y]
125 if self.cells == next {
134 fn smooth_until_equilibrium(&mut self) {
139 let mut next = vec!(vec!(true; self.height); self.width);
140 for x in distance..(self.width - distance) {
141 for y in distance..(self.height - distance) {
142 match Grid::neighbours(&self.cells, x, y, distance) {
143 n if n < 4 => next[x][y] = false,
144 n if n > 4 => next[x][y] = true,
145 _ => next[x][y] = self.cells[x][y]
149 if self.cells == next {
155 println!("{} iterations needed", count);
158 fn neighbours(grid: &Vec<Vec<bool>>, px: usize, py: usize, distance: usize) -> u8 {
160 for x in (px - distance)..=(px + distance) {
161 for y in (py - distance)..=(py + distance) {
162 if !(x == px && y == py) && grid[x][y] {
170 fn set_each<F: FnMut(usize, usize) -> bool>(&mut self, mut func: F, walls: usize) {
171 for x in walls..(self.width - walls) {
172 for y in walls..(self.height - walls) {
173 self.cells[x][y] = func(x, y);
178 fn subdivide(&mut self) -> Grid {
179 let (width, height) = (self.width * 2, self.height * 2);
180 let mut cells = vec!(vec!(true; height); width);
181 for x in 1..(width - 1) {
182 for y in 1..(height - 1) {
183 cells[x][y] = self.cells[x / 2][y / 2];
187 cell_size: self.cell_size / 2,
194 fn find_regions(&self) -> Vec<Region> {
195 time_scope!("finding all regions");
196 let mut regions = vec!();
197 let mut marked = vec!(vec!(false; self.height); self.width);
198 for x in 0..self.width {
199 for y in 0..self.height {
201 regions.push(self.get_region_at_point(x, y, &mut marked));
208 fn get_region_at_point(&self, x: usize, y: usize, marked: &mut Vec<Vec<bool>>) -> Region {
209 let value = self.cells[x][y];
210 let mut cells = vec!();
211 let mut queue = vec!((x, y));
214 while let Some(p) = queue.pop() {
216 for i in &[(-1, 0), (1, 0), (0, -1), (0, 1)] {
217 let ip = (p.0 as isize + i.0, p.1 as isize + i.1);
218 if ip.0 >= 0 && ip.0 < self.width as isize && ip.1 >= 0 && ip.1 < self.height as isize {
219 let up = (ip.0 as usize, ip.1 as usize);
220 if self.cells[up.0][up.1] == value && !marked[up.0][up.1] {
221 marked[up.0][up.1] = true;
228 Region { value, cells }
231 fn delete_region(&mut self, region: &Region) {
232 for c in ®ion.cells {
233 self.cells[c.0][c.1] = !region.value;
237 pub fn filter_regions(&mut self) {
238 let min_wall_size = 0.0015;
239 println!("grid size: ({}, {}) = {} cells", self.width, self.height, self.width * self.height);
240 println!("min wall size: {}", (self.width * self.height) as f64 * min_wall_size);
242 // delete all smaller wall regions
243 for r in self.find_regions().iter().filter(|r| r.value) {
244 let percent = r.cells.len() as f64 / (self.width * self.height) as f64;
245 if percent < min_wall_size {
246 println!("delete wall region of size {}", r.cells.len());
247 self.delete_region(r);
251 // delete all rooms but the largest
252 let regions = self.find_regions(); // check again, because if a removed room contains a removed wall, the removed wall will become a room
253 let mut rooms: Vec<&Region> = regions.iter().filter(|r| !r.value).collect();
254 rooms.sort_by_key(|r| r.cells.len());
256 while rooms.len() > 1 {
257 self.delete_region(rooms.pop().unwrap());
262 ////////// REGION //////////////////////////////////////////////////////////////
266 cells: Vec<(usize, usize)>,