From 1a702b7fd4e5746823d991ce489bedffab4d808f Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sun, 4 Feb 2024 10:38:54 +0000 Subject: [PATCH] Use ndarray for chunk cells --- Cargo.lock | 51 +++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++- src/falling_sand/mod.rs | 13 +++++----- src/falling_sand/systems.rs | 2 +- 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f44800..82c3639 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2051,7 +2051,9 @@ version = "0.1.0" dependencies = [ "bevy", "criterion", + "ndarray", "rand", + "rayon", ] [[package]] @@ -2406,6 +2408,16 @@ dependencies = [ "regex-automata 0.1.10", ] +[[package]] +name = "matrixmultiply" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7574c1cf36da4798ab73da5b215bbf444f50718207754cb522201d78d1cd0ff2" +dependencies = [ + "autocfg", + "rawpointer", +] + [[package]] name = "memchr" version = "2.7.1" @@ -2496,6 +2508,20 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "ndarray" +version = "0.15.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb12d4e967ec485a5f71c6311fe28158e9d6f4bc4a447b474184d0f91a8fa32" +dependencies = [ + "matrixmultiply", + "num-complex", + "num-integer", + "num-traits", + "rawpointer", + "rayon", +] + [[package]] name = "ndk" version = "0.7.0" @@ -2582,6 +2608,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + [[package]] name = "num-derive" version = "0.3.3" @@ -2593,6 +2628,16 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.17" @@ -2984,6 +3029,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9" +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" + [[package]] name = "rayon" version = "1.8.1" diff --git a/Cargo.toml b/Cargo.toml index 3c106c0..e91198b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,11 +8,13 @@ edition = "2021" [dependencies] bevy = "0.12.1" criterion = "0.5.1" +ndarray = { version = "0.15.6", features = ["rayon"] } rand = "0.8.5" +rayon = "1.8.1" [profile.dev.package."*"] opt-level = 3 [[bench]] name = "rule_generation" -harness = false \ No newline at end of file +harness = false diff --git a/src/falling_sand/mod.rs b/src/falling_sand/mod.rs index 2d83d59..1cd2369 100644 --- a/src/falling_sand/mod.rs +++ b/src/falling_sand/mod.rs @@ -15,6 +15,7 @@ use bevy::{ sprite::SpriteBundle, transform::components::Transform, }; +use ndarray::Array2; use rand::Rng; use crate::util::DirtyRect; @@ -75,7 +76,7 @@ pub struct Chunk { step: usize, width: usize, height: usize, - cells: Vec, + cells: Array2, dirty_rect: DirtyRect, } @@ -85,7 +86,7 @@ impl Chunk { step: 0, width, height, - cells: vec![Element::Air; width * height], + cells: Array2::from_elem((width, height), Element::Air), dirty_rect: DirtyRect::default(), }; @@ -103,7 +104,7 @@ impl Chunk { return; } - self.cells[x + y * self.width] = element; + self.cells[(x, y)] = element; self.dirty_rect.add_point(x, y); } @@ -112,9 +113,7 @@ impl Chunk { return; } - let i0 = x0 + y0 * self.width; - let i1 = x1 + y1 * self.width; - self.cells.swap(i0, i1); + self.cells.swap((x0, y0), (x1, y1)); self.dirty_rect.add_point(x0, y0); self.dirty_rect.add_point(x1, y1); } @@ -124,6 +123,6 @@ impl Chunk { return None; } - Some(self.cells[x + y * self.width]) + Some(self.cells[(x, y)]) } } diff --git a/src/falling_sand/systems.rs b/src/falling_sand/systems.rs index 530fdfb..9e3f419 100644 --- a/src/falling_sand/systems.rs +++ b/src/falling_sand/systems.rs @@ -79,7 +79,7 @@ pub fn update_chunk_texture( for y in chunk.dirty_rect.range_y() { for x in chunk.dirty_rect.range_x() { let mut colour = (0, 0, 0); - if let Some(element) = chunk.cells.get(x + y * chunk.width) { + if let Some(element) = chunk.get_cell(x, y) { match element { Element::Air => colour = (25, 24, 26), Element::Sand => colour = (255, 216, 102),