Skip to content

Commit

Permalink
Merge pull request #13 from JohnWildkins/poisson
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrow768 authored Apr 12, 2023
2 parents e94166b + 30ca67e commit f55a702
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ rayon = { version = "1.5", optional = true }
dbpnoise = { version = "0.1.2", optional = true }
pathfinding = { version = "3.0.13", optional = true }
num = { version = "0.4.0", optional = true }
fast_poisson = { version = "0.5.2", optional = true }

[features]
default = [
"acreplace",
# Aurora special snowflake noise gen
"auroranoise",
# Not default either but we need it
"batchnoise",
"cellularnoise",
"dmi",
"file",
Expand All @@ -80,6 +85,7 @@ default = [

# default features
acreplace = ["aho-corasick"]
auroranoise = ["fast_poisson"]
cellularnoise = ["rand", "rayon"]
dmi = ["png", "image"]
file = []
Expand Down
1 change: 1 addition & 0 deletions dmsrc/noise.dm
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#define rustg_noise_get_at_coordinates(seed, x, y) RUSTG_CALL(RUST_G, "noise_get_at_coordinates")(seed, x, y)
#define rustg_noise_poisson_sample(seed, x, y, r) RUSTG_CALL(RUST_G, "generate_poisson_sample")(seed, x, y, r)
44 changes: 44 additions & 0 deletions src/noise_gen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use fast_poisson::Poisson2D;
use noise::{NoiseFn, Perlin};
use std::{
cell::RefCell,
collections::hash_map::{Entry, HashMap},
fmt::Write,
};

use crate::error::Result;
Expand All @@ -14,6 +16,48 @@ byond_fn!(fn noise_get_at_coordinates(seed, x, y) {
get_at_coordinates(seed, x, y).ok()
});

byond_fn!(fn generate_poisson_sample(seed, x, y, r) {
get_poisson_sample(seed, x, y, r).ok()
});

fn get_poisson_sample(
seed_as_str: &str,
x_as_str: &str,
y_as_str: &str,
radius_as_str: &str,
) -> Result<String> {
let x = x_as_str.parse::<f64>()?;
let y = y_as_str.parse::<f64>()?;
let r = radius_as_str.parse::<f64>()?;
let seed = seed_as_str.parse::<u64>()?;

let points = Poisson2D::new()
.with_dimensions([x, y], r)
.with_seed(seed)
.iter();
let mut pointmap = vec![vec![0usize; y as usize]; x as usize];
let mut output = String::new();

// we're just gonna truncate these to the nearest point
for p in points {
let point_x = p[0] as usize;
let point_y = p[1] as usize;
pointmap[point_x][point_y] = 1;
}

for row in pointmap {
for cell in row {
if cell > 0 {
let _ = write!(output, "1");
} else {
let _ = write!(output, "0");
}
}
}

Ok(output)
}

//note that this will be 0 at integer x & y, scaling is left up to the caller
fn get_at_coordinates(seed_as_str: &str, x_as_str: &str, y_as_str: &str) -> Result<String> {
let x = x_as_str.parse::<f64>()?;
Expand Down

0 comments on commit f55a702

Please sign in to comment.