diff --git a/Cargo.lock b/Cargo.lock index fb372bc6f4..614a386441 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3174,6 +3174,7 @@ dependencies = [ "rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "vek 0.9.8 (registry+https://github.com/rust-lang/crates.io-index)", "veloren-common 0.3.0", + "zerocopy 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -3363,6 +3364,25 @@ name = "xml-rs" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "zerocopy" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "zerocopy-derive 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "zerocopy-derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)", + "synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [metadata] "checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c" "checksum aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5" @@ -3725,3 +3745,5 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum x11-dl 2.18.4 (registry+https://github.com/rust-lang/crates.io-index)" = "be65e1342a3baae65439cd03306778831a3d133b0d20243a7fb83fd5cf403c58" "checksum xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" "checksum xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "541b12c998c5b56aa2b4e6f18f03664eef9a4fd0a246a55594efae6cc2d964b5" +"checksum zerocopy 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "992b9b31f80fd4a167f903f879b8ca43d6716cc368ea01df90538baa2dd34056" +"checksum zerocopy-derive 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b090467ecd0624026e8a6405d343ac7382592530d54881330b3fc8e400280fa5" diff --git a/world/Cargo.toml b/world/Cargo.toml index 4b86cfb732..4c27a4dadf 100644 --- a/world/Cargo.toml +++ b/world/Cargo.toml @@ -12,6 +12,7 @@ hashbrown = { version = "0.5.0", features = ["serde", "nightly"] } lazy_static = "1.3.0" rand = "0.7.0" rand_chacha = "0.2.1" +zerocopy = "0.2.8" [dev-dependencies] minifb = { git = "https://github.com/emoon/rust_minifb.git" } diff --git a/world/src/sim/mod.rs b/world/src/sim/mod.rs index 0585e3528c..5c22d573c4 100644 --- a/world/src/sim/mod.rs +++ b/world/src/sim/mod.rs @@ -55,9 +55,10 @@ pub struct WorldSim { impl WorldSim { pub fn generate(mut seed: u32) -> Self { + let mut seed = &mut seed; let mut gen_seed = || { - seed = seed_expan::diffuse(seed + 1); - seed + *seed = seed_expan::diffuse(*seed); + *seed }; let mut gen_ctx = GenCtx { @@ -95,11 +96,11 @@ impl WorldSim { } let mut this = Self { - seed, + seed: *seed, chunks, locations: Vec::new(), gen_ctx, - rng: ChaChaRng::from_seed(seed_expan::rng_state(seed)), + rng: ChaChaRng::from_seed(seed_expan::rng_state(*seed)), }; this.seed_elements(); diff --git a/world/src/util/seed_expan.rs b/world/src/util/seed_expan.rs index 899c2d435e..61c19e80b7 100644 --- a/world/src/util/seed_expan.rs +++ b/world/src/util/seed_expan.rs @@ -1,3 +1,5 @@ +use zerocopy::AsBytes; + /// Simple non-cryptographic diffusion function. pub fn diffuse(mut x: u32) -> u32 { x = x.wrapping_add(0x7ed55d16).wrapping_add(x << 12); @@ -16,5 +18,8 @@ pub fn rng_state(mut x: u32) -> [u8; 32] { x = diffuse(x); *s = x; } - unsafe { std::mem::transmute(r) } + let bytes = r.as_bytes(); + let mut a: [u8; 32] = [0; 32]; + a.copy_from_slice(bytes); + a }