-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
225 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,14 @@ | ||
// SPDX-License-Identifier: MPL-2.0 | ||
use super::global; | ||
use crate::shared; | ||
use meowtonin::{ByondResult, ByondValue}; | ||
use rand::{ | ||
distributions::{Distribution, WeightedIndex}, | ||
Rng, | ||
}; | ||
|
||
#[byond_fn] | ||
pub fn pick(options: ByondValue, secure: Option<bool>) -> ByondResult<Option<ByondValue>> { | ||
if !options.is_list() { | ||
return Ok(None); | ||
} | ||
let length = options.length()?; | ||
match length { | ||
0 => return Ok(None), | ||
1 => return options.read_list_index(&1).map(Some), | ||
_ => {} | ||
} | ||
let idx = global(secure).gen_range::<usize, _>(1..=length); | ||
options.read_list_index(&idx).map(Some) | ||
pub fn pick(options: ByondValue, secure: Option<bool>) -> ByondResult<ByondValue> { | ||
shared::pick(&mut global(secure), options) | ||
} | ||
|
||
#[byond_fn] | ||
pub fn pick_weighted(options: ByondValue, secure: Option<bool>) -> ByondResult<Option<ByondValue>> { | ||
if !options.is_list() { | ||
return Ok(None); | ||
} | ||
let length = options.length::<usize>()?; | ||
match length { | ||
0 => return Ok(None), | ||
1 => return options.read_list_index(&1).map(Some), | ||
_ => {} | ||
} | ||
let options = options.read_assoc_list()?; | ||
let weights = options | ||
.iter() | ||
.map(|[_, weight]| weight.get_number()) | ||
.filter_map(|weight| weight.ok()) | ||
.filter(|weight| weight.is_normal() && weight.is_sign_positive()) | ||
.collect::<Vec<f32>>(); | ||
match weights.len() { | ||
0 => return Ok(None), | ||
1 => return Ok(options.get(1).and_then(|entry| entry.get(1)).cloned()), | ||
_ => {} | ||
} | ||
let dist = match WeightedIndex::new(weights) { | ||
Ok(dist) => dist, | ||
Err(_) => return Ok(None), | ||
}; | ||
let idx = dist.sample(&mut global(secure)); | ||
Ok(options.get(idx).and_then(|entry| entry.get(2)).cloned()) | ||
pub fn pick_weighted(options: ByondValue, secure: Option<bool>) -> ByondResult<ByondValue> { | ||
shared::pick_weighted(&mut global(secure), options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
// SPDX-License-Identifier: MPL-2.0 | ||
use super::global; | ||
use rand::Rng; | ||
use crate::shared; | ||
|
||
#[byond_fn] | ||
pub fn prob(probability: f64, secure: Option<bool>) -> bool { | ||
if !probability.is_finite() { | ||
return true; | ||
} | ||
let probability = (probability / 100.0).clamp(0.0, 1.0); | ||
global(secure).gen_bool(probability) | ||
shared::prob(&mut global(secure), probability) | ||
} | ||
|
||
#[byond_fn] | ||
pub fn prob_ratio(numerator: u32, denominator: u32, secure: Option<bool>) -> bool { | ||
global(secure).gen_ratio(numerator, denominator) | ||
shared::prob_ratio(&mut global(secure), numerator, denominator) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,20 @@ | ||
// SPDX-License-Identifier: MPL-2.0 | ||
use super::INSTANCES; | ||
use crate::shared; | ||
use aneri_core::ByondSlotKey; | ||
use rand::Rng; | ||
|
||
#[byond_fn] | ||
pub fn instanced_prob(src: ByondSlotKey, probability: f64) -> Option<bool> { | ||
if !probability.is_finite() { | ||
return Some(true); | ||
} | ||
let probability = (probability / 100.0).clamp(0.0, 1.0); | ||
INSTANCES | ||
.lock() | ||
.get_mut(src) | ||
.map(|rng| rng.gen_bool(probability)) | ||
.map(|rng| shared::prob(rng, probability)) | ||
} | ||
|
||
#[byond_fn] | ||
pub fn instanced_prob_ratio(src: ByondSlotKey, numerator: u32, denominator: u32) -> Option<bool> { | ||
INSTANCES | ||
.lock() | ||
.get_mut(src) | ||
.map(|rng| rng.gen_ratio(numerator, denominator)) | ||
.map(|rng| shared::prob_ratio(rng, numerator, denominator)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,26 @@ | ||
// SPDX-License-Identifier: MPL-2.0 | ||
use super::INSTANCES; | ||
use crate::shared; | ||
use aneri_core::ByondSlotKey; | ||
use rand::{ | ||
distributions::{Alphanumeric, Bernoulli, Distribution}, | ||
Rng, | ||
}; | ||
|
||
#[byond_fn] | ||
pub fn instanced_random_string_alphanumeric(src: ByondSlotKey, length: usize) -> Option<String> { | ||
INSTANCES.lock().get_mut(src).map(|rng| { | ||
(0..=length) | ||
.map(|_| rng.sample(Alphanumeric) as char) | ||
.collect() | ||
}) | ||
INSTANCES | ||
.lock() | ||
.get_mut(src) | ||
.map(|rng| shared::random_string_alphanumeric(rng, length)) | ||
} | ||
|
||
#[byond_fn] | ||
pub fn instnaced_replace_chars_prob( | ||
pub fn instanced_replace_chars_prob( | ||
src: ByondSlotKey, | ||
input: String, | ||
replacement: String, | ||
prob: f32, | ||
skip_whitespace: Option<bool>, | ||
) -> Option<String> { | ||
if !prob.is_normal() || !prob.is_sign_positive() { | ||
return Some(input); | ||
} | ||
let skip_whitespace = skip_whitespace.unwrap_or(false); | ||
INSTANCES.lock().get_mut(src).map(|rng| { | ||
let distro = Bernoulli::new((prob as f64 / 100.0).clamp(0.0, 1.0)) | ||
.expect("invalid probability, wtf???"); | ||
let mut output = String::with_capacity(input.len() * replacement.len()); // Allocate for worst case scenario. | ||
input.chars().for_each(|c| { | ||
if (!skip_whitespace || !c.is_whitespace()) && distro.sample(rng) { | ||
output.push_str(&replacement); | ||
} else { | ||
output.push(c); | ||
} | ||
}); | ||
output | ||
}) | ||
INSTANCES | ||
.lock() | ||
.get_mut(src) | ||
.map(|rng| shared::replace_chars_prob(rng, input, replacement, prob, skip_whitespace)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
mod number; | ||
mod pick; | ||
mod prob; | ||
mod string; | ||
|
||
pub(crate) use number::*; | ||
pub(crate) use pick::*; | ||
pub(crate) use prob::*; | ||
pub(crate) use string::*; |
Oops, something went wrong.