2 use core::render::Renderer;
3 use noise::{NoiseFn, OpenSimplex, Seedable};
5 use sprites::SpriteManager;
7 ////////// LEVEL ///////////////////////////////////////////////////////////////
11 pub gravity: Point2D<f64>,
17 pub fn new(gravity: Point2D<f64>) -> Self {
18 Level { gravity, grid: Grid::generate(10), iterations: 10 }
21 pub fn regenerate(&mut self) {
22 self.grid = Grid::generate(self.iterations);
25 pub fn increase_iteration(&mut self) {
28 println!("iterate {} time(s)", self.iterations);
31 pub fn decrease_iteration(&mut self) {
34 println!("iterate {} time(s)", self.iterations);
37 pub fn filter_regions(&mut self) {
38 self.grid.filter_regions();
41 pub fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager) {
42 renderer.canvas().set_draw_color((64, 64, 64));
43 let size = self.grid.cell_size;
44 for x in 0..self.grid.width {
45 for y in 0..self.grid.height {
46 if self.grid.cells[x][y] {
47 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();
54 ////////// GRID ////////////////////////////////////////////////////////////////
61 pub cells: Vec<Vec<bool>>,
65 fn generate(iterations: u8) -> Grid {
67 let (width, height) = (2560 / cell_size, 1440 / cell_size);
73 cells: vec!(vec!(true; height); width),
76 // start with some noise
77 // grid.simplex_noise();
80 // smooth with cellular automata
81 grid.smooth(iterations);
82 // grid.smooth_until_equilibrium();
84 // increase resolution
86 grid = grid.subdivide();
87 grid.smooth(iterations);
94 fn simplex_noise(&mut self) {
95 let noise = OpenSimplex::new().set_seed(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as u32);
96 self.set_each(|x, y| noise.get([x as f64 / 12.0, y as f64 / 12.0]) > 0.055, 1);
100 fn random_noise(&mut self) {
101 let mut rng = rand::thread_rng();
102 let noise = OpenSimplex::new().set_seed(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as u32);
103 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
104 // let w = self.width as f64;
105 // self.set_each(|_x, _y| rng.gen_range(0, 100) > (45 + ((15 * _x) as f64 / w) as usize), 1); // opens up to the right
109 fn smooth(&mut self, iterations: u8) {
111 for _i in 0..iterations {
112 let mut next = vec!(vec!(true; self.height); self.width);
113 for x in distance..(self.width - distance) {
114 for y in distance..(self.height - distance) {
115 match Grid::neighbours(&self.cells, x, y, distance) {
116 n if n < 4 => next[x][y] = false,
117 n if n > 4 => next[x][y] = true,
118 _ => next[x][y] = self.cells[x][y]
122 if self.cells == next {
131 fn smooth_until_equilibrium(&mut self) {
136 let mut next = vec!(vec!(true; self.height); self.width);
137 for x in distance..(self.width - distance) {
138 for y in distance..(self.height - distance) {
139 match Grid::neighbours(&self.cells, x, y, distance) {
140 n if n < 4 => next[x][y] = false,
141 n if n > 4 => next[x][y] = true,
142 _ => next[x][y] = self.cells[x][y]
146 if self.cells == next {
152 println!("{} iterations needed", count);
155 fn neighbours(grid: &Vec<Vec<bool>>, px: usize, py: usize, distance: usize) -> u8 {
157 for x in (px - distance)..=(px + distance) {
158 for y in (py - distance)..=(py + distance) {
159 if !(x == px && y == py) && grid[x][y] {
167 fn set_each<F: FnMut(usize, usize) -> bool>(&mut self, mut func: F, walls: usize) {
168 for x in walls..(self.width - walls) {
169 for y in walls..(self.height - walls) {
170 self.cells[x][y] = func(x, y);
175 fn subdivide(&mut self) -> Grid {
176 let (width, height) = (self.width * 2, self.height * 2);
177 let mut cells = vec!(vec!(true; height); width);
178 for x in 1..(width - 1) {
179 for y in 1..(height - 1) {
180 cells[x][y] = self.cells[x / 2][y / 2];
184 cell_size: self.cell_size / 2,
191 fn find_regions(&self) -> Vec<Region> {
192 let mut regions = vec!();
193 let mut marked = vec!(vec!(false; self.height); self.width);
194 for x in 0..self.width {
195 for y in 0..self.height {
197 regions.push(self.get_region_at_point(x, y, &mut marked));
204 fn get_region_at_point(&self, x: usize, y: usize, marked: &mut Vec<Vec<bool>>) -> Region {
205 let value = self.cells[x][y];
206 let mut cells = vec!();
207 let mut queue = vec!((x, y));
210 while let Some(p) = queue.pop() {
212 for i in &[(-1, 0), (1, 0), (0, -1), (0, 1)] {
213 let ip = (p.0 as isize + i.0, p.1 as isize + i.1);
214 if ip.0 >= 0 && ip.0 < self.width as isize && ip.1 >= 0 && ip.1 < self.height as isize {
215 let up = (ip.0 as usize, ip.1 as usize);
216 if self.cells[up.0][up.1] == value && !marked[up.0][up.1] {
217 marked[up.0][up.1] = true;
224 Region { value, cells }
227 fn delete_region(&mut self, region: &Region) {
228 for c in ®ion.cells {
229 self.cells[c.0][c.1] = !region.value;
233 pub fn filter_regions(&mut self) {
234 let min_wall_size = 0.0015;
235 println!("grid size: ({}, {}) = {} cells", self.width, self.height, self.width * self.height);
236 println!("min wall size: {}", (self.width * self.height) as f64 * min_wall_size);
238 // delete all smaller wall regions
239 for r in self.find_regions().iter().filter(|r| r.value) {
240 let percent = r.cells.len() as f64 / (self.width * self.height) as f64;
241 if percent < min_wall_size {
242 println!("delete wall region of size {}", r.cells.len());
243 self.delete_region(r);
247 // delete all rooms but the largest
248 let regions = self.find_regions(); // check again, because if a removed room contains a removed wall, the removed wall will become a room
249 let mut rooms: Vec<&Region> = regions.iter().filter(|r| !r.value).collect();
250 rooms.sort_by_key(|r| r.cells.len());
252 while rooms.len() > 1 {
253 self.delete_region(rooms.pop().unwrap());
258 ////////// REGION //////////////////////////////////////////////////////////////
262 cells: Vec<(usize, usize)>,