Skip to content

Commit

Permalink
Adds a new type of noise: Discrete Batched Perlin-like Noise (DBP) (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Djiq authored Jun 14, 2022
1 parent 974bc74 commit bb2b515
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 14 deletions.
58 changes: 44 additions & 14 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ rand = {version = "0.8", optional = true}
dmsort = {version = "1.0.0", optional = true }
toml-dep = { version = "0.5.8", package="toml", optional = true }
aho-corasick = { version = "0.7.18", optional = true}
dbpnoise = { version = "0.1.2", optional = true}

[features]
default = ["acreplace", "cellularnoise", "dmi", "file", "git", "http", "json", "log", "noise", "sql", "time", "toml", "url"]
Expand All @@ -64,6 +65,7 @@ toml = ["serde", "serde_json", "toml-dep"]
url = ["url-dep", "percent-encoding"]

# additional features
batchnoise = ["dbpnoise"]
hash = ["base64", "const-random", "md-5", "hex", "sha-1", "sha2", "twox-hash", "serde", "serde_json"]
redis_pubsub = ["flume", "redis", "serde", "serde_json"]
unzip = ["zip", "jobs"]
Expand Down
16 changes: 16 additions & 0 deletions dmsrc/dbpnoise.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* This proc generates a grid of perlin-like noise
*
* Returns a single string that goes row by row, with values of 1 representing an turned on cell, and a value of 0 representing a turned off cell.
*
* Arguments:
* * seed: seed for the function
* * accuracy: how close this is to the original perlin noise, as accuracy approaches infinity, the noise becomes more and more perlin-like
* * stamp_size: Size of a singular stamp used by the algorithm, think of this as the same stuff as frequency in perlin noise
* * world_size: size of the returned grid.
* * lower_range: lower bound of values selected for. (inclusive)
* * upper_range: upper bound of values selected for. (exclusive)
*/
#define rustg_dbp_generate(seed, accuracy, stamp_size, world_size, lower_range, upper_range) \
call(RUST_G, "dbp_generate")(seed, accuracy, stamp_size, world_size, lower_range, upper_range)

36 changes: 36 additions & 0 deletions src/dbpnoise.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::error::Result;
use dbpnoise::gen_noise;

byond_fn!(fn dbp_generate(seed, accuracy, stamp_size, world_size, lower_range, upper_range) {
gen_dbp_noise(seed, accuracy, stamp_size, world_size, lower_range, upper_range).ok()
});

fn gen_dbp_noise(
seed: &str,
accuracy_as_str: &str,
stamp_size_as_str: &str,
world_size_as_str: &str,
lower_range_as_str: &str,
upper_range_as_str: &str,
) -> Result<String> {
let accuracy = accuracy_as_str.parse::<usize>()?;
let stamp_size = stamp_size_as_str.parse::<usize>()?;
let world_size = world_size_as_str.parse::<usize>()?;
let lower_range = lower_range_as_str.parse::<f32>()?;
let upper_range = upper_range_as_str.parse::<f32>()?;
let map: Vec<Vec<bool>> = gen_noise(
seed,
accuracy,
stamp_size,
world_size,
lower_range,
upper_range,
);
let mut result = String::new();
for row in map {
for cell in row {
result.push(if cell { '1' } else { '0' });
}
}
Ok(result)
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod jobs;
pub mod acreplace;
#[cfg(feature = "cellularnoise")]
pub mod cellularnoise;
#[cfg(feature = "dbpnoise")]
pub mod dbpnoise;
#[cfg(feature = "dmi")]
pub mod dmi;
#[cfg(feature = "file")]
Expand Down

0 comments on commit bb2b515

Please sign in to comment.