c048369be8
/execute now has every vanilla subcommand: store (result or success into scores, boss bars and entity, block or storage data with a type and scale), positioned over a heightmap, summon, on (owner, vehicle, controller, passengers, target, attacker), and the if/unless conditions score (all comparisons and matches), blocks, biome, predicate, function, data, items and slots on top of block, entity, loaded and dimension, with the vanilla pass, fail and count messages. Data packs in the world's datapacks folder (folders and zips) provide functions with comments, line continuation and $ macros, function tags including minecraft:load and minecraft:tick, and predicates; /function (with arguments or "with" sources), /return (value, fail and run), /schedule, /reload and /datapack list, enable and disable work against them. Also added: /bossbar with a boss bar overlay and saving, /item replace, fill and override over player, mob, armor stand, item frame and container slots, /random and /teammsg, and /tick rate, freeze, step and sprint on the game clock. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
108 lines
3.7 KiB
Rust
108 lines
3.7 KiB
Rust
//! `RandomSequences`: the named random streams `/random` draws from.
|
|
use horizon_core::random::{XoroshiroRandom, mix_stafford13, seed_from_hash};
|
|
use std::collections::BTreeMap;
|
|
|
|
const GOLDEN_RATIO: u64 = 0x9e37_79b9_7f4a_7c15;
|
|
const SILVER_RATIO: u64 = 0x6a09_e667_f3bc_c909;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct RandomSequences {
|
|
salt: i32,
|
|
include_world_seed: bool,
|
|
include_sequence_id: bool,
|
|
sequences: BTreeMap<String, XoroshiroRandom>,
|
|
}
|
|
impl Default for RandomSequences {
|
|
fn default() -> Self {
|
|
Self {
|
|
salt: 0,
|
|
include_world_seed: true,
|
|
include_sequence_id: true,
|
|
sequences: BTreeMap::new(),
|
|
}
|
|
}
|
|
}
|
|
/// `RandomSequence.createSequence`.
|
|
fn create(id: &str, world_seed: i64, salt: i32, world: bool, with_id: bool) -> XoroshiroRandom {
|
|
let seed = (if world { world_seed } else { 0 }) ^ i64::from(salt);
|
|
let mut lo = seed as u64 ^ SILVER_RATIO;
|
|
let mut hi = lo.wrapping_add(GOLDEN_RATIO);
|
|
if with_id {
|
|
let [a, b] = seed_from_hash(id);
|
|
lo ^= a;
|
|
hi ^= b;
|
|
}
|
|
XoroshiroRandom::from_state(mix_stafford13(lo), mix_stafford13(hi))
|
|
}
|
|
impl RandomSequences {
|
|
pub fn get(&mut self, id: &str, world_seed: i64) -> &mut XoroshiroRandom {
|
|
let (salt, w, i) = (self.salt, self.include_world_seed, self.include_sequence_id);
|
|
self.sequences
|
|
.entry(id.to_owned())
|
|
.or_insert_with(|| create(id, world_seed, salt, w, i))
|
|
}
|
|
pub fn ids(&self) -> Vec<String> {
|
|
self.sequences.keys().cloned().collect()
|
|
}
|
|
pub fn clear(&mut self) -> usize {
|
|
let n = self.sequences.len();
|
|
self.sequences.clear();
|
|
n
|
|
}
|
|
pub fn set_defaults(&mut self, salt: i32, include_world_seed: bool, include_sequence_id: bool) {
|
|
self.salt = salt;
|
|
self.include_world_seed = include_world_seed;
|
|
self.include_sequence_id = include_sequence_id;
|
|
}
|
|
pub fn reset(&mut self, id: &str, world_seed: i64) {
|
|
let (salt, w, i) = (self.salt, self.include_world_seed, self.include_sequence_id);
|
|
self.sequences
|
|
.insert(id.to_owned(), create(id, world_seed, salt, w, i));
|
|
}
|
|
pub fn reset_with(&mut self, id: &str, world_seed: i64, salt: i32, w: bool, i: bool) {
|
|
self.sequences
|
|
.insert(id.to_owned(), create(id, world_seed, salt, w, i));
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use horizon_core::random::RandomSource;
|
|
|
|
#[test]
|
|
fn sequences_are_deterministic_and_resettable() {
|
|
let mut a = RandomSequences::default();
|
|
let first: Vec<i32> = (0..4)
|
|
.map(|_| a.get("minecraft:x", 42).next_int_bound(100).unwrap())
|
|
.collect();
|
|
let mut b = RandomSequences::default();
|
|
let again: Vec<i32> = (0..4)
|
|
.map(|_| b.get("minecraft:x", 42).next_int_bound(100).unwrap())
|
|
.collect();
|
|
assert_eq!(first, again);
|
|
// Another id or seed gives another stream.
|
|
let other = RandomSequences::default()
|
|
.get("minecraft:y", 42)
|
|
.next_long();
|
|
let same = RandomSequences::default()
|
|
.get("minecraft:x", 42)
|
|
.next_long();
|
|
assert_ne!(other, same);
|
|
// Reset restarts it.
|
|
a.reset("minecraft:x", 42);
|
|
assert_eq!(
|
|
a.get("minecraft:x", 42).next_int_bound(100).unwrap(),
|
|
first[0]
|
|
);
|
|
// Defaults change what new sequences start from.
|
|
a.set_defaults(7, false, false);
|
|
a.reset("minecraft:x", 42);
|
|
let v = a.get("minecraft:x", 1).next_long();
|
|
let mut c = RandomSequences::default();
|
|
c.set_defaults(7, false, false);
|
|
assert_eq!(c.get("minecraft:z", 999).next_long(), v);
|
|
assert_eq!(a.clear(), 1);
|
|
}
|
|
}
|