diff --git a/README.md b/README.md index d0559a9..6b9fec5 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,13 @@ npm run start This command automatically watches the files, so you can build rust stuff and it will automatically appear. + +## Deploy + +``` +cd www +npm run build +``` + +Then copy the dist directory to a server. + diff --git a/src/lib.rs b/src/lib.rs index 5bdbf84..5df072e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ mod utils; use wasm_bindgen::prelude::*; extern crate js_sys; +use std::collections::HashSet; // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global // allocator. @@ -9,7 +10,6 @@ extern crate js_sys; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; - #[wasm_bindgen] pub struct Universe { width: i32, @@ -18,6 +18,7 @@ pub struct Universe { cells: Vec, base: Vec, + queue: HashSet<(i32, i32)>, } impl Universe { @@ -26,59 +27,72 @@ impl Universe { } fn in_bounds(&self, row: i32, col: i32) -> bool { - 0 <= row && row < self.height && 0 <= col && col < self.width + 0 <= row && row < self.height && 0 <= col && col < self.width } fn incr_cell(&mut self, row: i32, col: i32) { - if !self.in_bounds(row, col) { return; } + if !self.in_bounds(row, col) { + return; + } - let idx = self.get_index(row, col); - self.base[idx] += 1; + let idx = self.get_index(row, col); + self.base[idx] += 1; - if self.base[idx] >= 4 { - self.cells[idx] = 100; - self.base[idx] = 0; - for dc in [-1, 1].iter() { - let col2 = col + dc; - self.incr_cell(row, col2); - } - for dr in [-1, 1].iter() { - let row2 = row + dr; - self.incr_cell(row2, col); - } - } + if self.base[idx] >= 4 { + self.queue.insert((row, col)); + } } fn incr_random_cell(&mut self) { - let row = (js_sys::Math::random() * self.height as f64) as i32; - let col = (js_sys::Math::random() * self.width as f64) as i32; - let idx = self.get_index(row, col); - let count = self.base[idx]; + let row = (js_sys::Math::random() * self.height as f64) as i32; + let col = (js_sys::Math::random() * self.width as f64) as i32; + let idx = self.get_index(row, col); + let count = self.base[idx]; - if js_sys::Math::random() < 1.0 / (count + 1) as f64 { - self.incr_cell(row, col); - } + if js_sys::Math::random() < 1.0 / (count + 1) as f64 { + self.incr_cell(row, col); + } } } #[wasm_bindgen] impl Universe { pub fn tick(&mut self) { - self.stress = 0; + self.stress = 0; - for _i in 1..2 { - self.incr_random_cell(); - } + for _i in 1..2 { + self.incr_random_cell(); + } - for row in 0..self.height { + let queue_clone = self.queue.clone(); + self.queue.clear(); + for (row, col) in queue_clone { + let idx = self.get_index(row, col); + if self.base[idx] < 4 { + continue; + } + + self.cells[idx] = 100; + self.base[idx] -= 4; + for dc in [-1, 1].iter() { + let col2 = col + dc; + self.incr_cell(row, col2); + } + for dr in [-1, 1].iter() { + let row2 = row + dr; + self.incr_cell(row2, col); + } + } + + for row in 0..self.height { for col in 0..self.width { - let idx = self.get_index(row, col); + let idx = self.get_index(row, col); - self.stress += self.base[idx] as i32; + self.stress += self.base[idx] as i32; - if self.cells[idx] > 0 { - self.cells[idx] -= 1; - } + if self.cells[idx] > 0 { + self.cells[idx] -= 1; + } } } } @@ -88,21 +102,29 @@ impl Universe { let height = 81; let stress = 0; - let cells = (0..width * height) - .map(|_i| 0) - .collect(); + let cells = (0..width * height).map(|_i| 0).collect(); - let base = (0..width * height) - .map(|_i| 0) - .collect(); + let base = (0..width * height).map(|_i| 0).collect(); - Universe {width, height, stress, cells, base} + let queue = HashSet::new(); + + Universe { + width, + height, + stress, + cells, + base, + queue, + } } pub fn init(&mut self) { - for _i in 1..50000 { - self.incr_random_cell(); - } + for _i in 1..50000 { + self.incr_random_cell(); + } + for _i in 1..1000 { + self.tick(); + } } pub fn width(&self) -> i32 {