2 use core::render::Renderer;
4 use sprites::SpriteManager;
6 ////////// LEVEL ///////////////////////////////////////////////////////////////
10 pub gravity: Point2D<f64>,
11 pub ground: f64, // just to have something
17 pub fn new(gravity: Point2D<f64>, ground: f64) -> Self {
18 Level { gravity, ground, 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 let w = renderer.viewport().0 as i32;
44 renderer.canvas().set_draw_color((64, 64, 64));
45 let size = self.grid.cell_size;
46 for x in 0..self.grid.width {
47 for y in 0..self.grid.height {
48 if self.grid.cells[x][y] {
49 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 let y = (i * i - 1) as i32 + self.ground as i32;
56 renderer.canvas().set_draw_color((255 - i * 20, 255 - i * 20, 0));
57 renderer.canvas().draw_line((0, y), (w, y)).unwrap();
62 ////////// GRID ////////////////////////////////////////////////////////////////
69 pub cells: Vec<Vec<bool>>,
73 fn generate(iterations: u8) -> Grid {
75 let (width, height) = (2560 / cell_size, 1440 / cell_size);
81 cells: vec!(vec!(true; height); width),
84 // start with some noise
85 // grid.simplex_noise();
88 // smooth with cellular automata
89 grid.smooth(iterations);
90 // grid.smooth_until_equilibrium();
92 // increase resolution
94 grid = grid.subdivide();
95 grid.smooth(iterations);
102 fn simplex_noise(&mut self) {
103 use noise::{NoiseFn, OpenSimplex, Seedable};
104 let noise = OpenSimplex::new().set_seed(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as u32);
105 self.set_each(|x, y| noise.get([x as f64 / 12.0, y as f64 / 12.0]) > 0.055, 1);
109 fn random_noise(&mut self) {
110 let mut rng = rand::thread_rng();
111 self.set_each(|_x, _y| rng.gen_range(0, 100) > 55, 1);
115 fn smooth(&mut self, iterations: u8) {
117 for _i in 0..iterations {
118 let mut next = vec!(vec!(true; self.height); self.width);
119 for x in distance..(self.width - distance) {
120 for y in distance..(self.height - distance) {
121 match Grid::neighbours(&self.cells, x, y, distance) {
122 n if n < 4 => next[x][y] = false,
123 n if n > 4 => next[x][y] = true,
124 _ => next[x][y] = self.cells[x][y]
128 if self.cells == next {
137 fn smooth_until_equilibrium(&mut self) {
142 let mut next = vec!(vec!(true; self.height); self.width);
143 for x in distance..(self.width - distance) {
144 for y in distance..(self.height - distance) {
145 match Grid::neighbours(&self.cells, x, y, distance) {
146 n if n < 4 => next[x][y] = false,
147 n if n > 4 => next[x][y] = true,
148 _ => next[x][y] = self.cells[x][y]
152 if self.cells == next {
158 println!("{} iterations needed", count);
161 fn neighbours(grid: &Vec<Vec<bool>>, px: usize, py: usize, distance: usize) -> u8 {
163 for x in (px - distance)..=(px + distance) {
164 for y in (py - distance)..=(py + distance) {
165 if !(x == px && y == py) && grid[x][y] {
173 fn set_each<F: FnMut(usize, usize) -> bool>(&mut self, mut func: F, walls: usize) {
174 for x in walls..(self.width - walls) {
175 for y in walls..(self.height - walls) {
176 self.cells[x][y] = func(x, y);
181 fn subdivide(&mut self) -> Grid {
182 let (width, height) = (self.width * 2, self.height * 2);
183 let mut cells = vec!(vec!(true; height); width);
184 for x in 1..(width - 1) {
185 for y in 1..(height - 1) {
186 cells[x][y] = self.cells[x / 2][y / 2];
190 cell_size: self.cell_size / 2,
197 fn find_regions(&self) -> Vec<Region> {
198 let mut regions = vec!();
199 let mut marked = vec!(vec!(false; self.height); self.width);
200 for x in 0..self.width {
201 for y in 0..self.height {
203 regions.push(self.get_region_at_point(x, y, &mut marked));
210 fn get_region_at_point(&self, x: usize, y: usize, marked: &mut Vec<Vec<bool>>) -> Region {
211 let value = self.cells[x][y];
212 let mut cells = vec!();
213 let mut queue = vec!((x, y));
216 while let Some(p) = queue.pop() {
218 for i in &[(-1, 0), (1, 0), (0, -1), (0, 1)] {
219 let ip = (p.0 as isize + i.0, p.1 as isize + i.1);
220 if ip.0 >= 0 && ip.0 < self.width as isize && ip.1 >= 0 && ip.1 < self.height as isize {
221 let up = (ip.0 as usize, ip.1 as usize);
222 if self.cells[up.0][up.1] == value && !marked[up.0][up.1] {
223 marked[up.0][up.1] = true;
230 Region { value, cells }
233 fn delete_region(&mut self, region: &Region) {
234 for c in ®ion.cells {
235 self.cells[c.0][c.1] = !region.value;
239 pub fn filter_regions(&mut self) {
240 let min_wall_size = 0.0015;
241 println!("grid size: ({}, {}) = {} cells", self.width, self.height, self.width * self.height);
242 println!("min wall size: {}", (self.width * self.height) as f64 * min_wall_size);
244 // delete all smaller wall regions
245 for r in self.find_regions().iter().filter(|r| r.value) {
246 let percent = r.cells.len() as f64 / (self.width * self.height) as f64;
247 if percent < min_wall_size {
248 println!("delete wall region of size {}", r.cells.len());
249 self.delete_region(r);
253 // delete all rooms but the largest
254 let regions = self.find_regions(); // check again, because if a removed room contains a removed wall, the removed wall will become a room
255 let mut rooms: Vec<&Region> = regions.iter().filter(|r| !r.value).collect();
256 rooms.sort_by_key(|r| r.cells.len());
258 while rooms.len() > 1 {
259 self.delete_region(rooms.pop().unwrap());
264 ////////// REGION //////////////////////////////////////////////////////////////
268 cells: Vec<(usize, usize)>,