Permalink
Browse files

Implement random seeds under wasm

  • Loading branch information...
tomassedovic committed Dec 21, 2017
1 parent 6a194bc commit fdbc828503b8cd0583852f4ca732e1f9455555b3
Showing with 20 additions and 4 deletions.
  1. +1 −0 dose-response.js
  2. +1 −0 src/main.rs
  3. +2 −4 src/state.rs
  4. +16 −0 src/util.rs
@@ -118,6 +118,7 @@ fetch('target/wasm32-unknown-unknown/release/dose-response.wasm')

.then(bytes => WebAssembly.instantiate(bytes, {
env: {
random: Math.random,
draw: function(ptr, len) {
if(len % 6 != 0) {
throw new Error("The drawcalls vector must have a multiple of 6 elements!");
@@ -376,6 +376,7 @@ pub fn initialise() -> *mut State {

extern {
fn draw(nums: *const u8, len: usize);
fn random() -> f32;
}


@@ -1,11 +1,10 @@


use animation::{AreaOfEffect, ScreenFade};
use engine::Mouse;
use keys::Keys;
use monster;
use player::Player;
use point::Point;
use util;
use rand::{self, IsaacRng, SeedableRng};

use stats::Stats;
@@ -203,8 +202,7 @@ impl State {
) -> State {
let commands = VecDeque::new();
let verifications = VecDeque::new();
//let seed = rand::random::<u32>();
let seed = 1234;
let seed = util::random_seed();
let mut writer: Box<Write> = if let Some(replay_path) = replay_path {
match File::create(&replay_path) {
Ok(f) => {
@@ -1,5 +1,7 @@
use std::time::Duration;

use rand;


/// The number of nanoseconds in a microsecond.
const NANOS_PER_MICRO: u32 = 1000;
@@ -26,3 +28,17 @@ pub fn num_microseconds(duration: Duration) -> Option<u64> {
}
None
}


#[cfg(not(feature = "web"))]
pub fn random_seed() -> u32 {
rand::random::<u32>()
}

#[cfg(feature = "web")]
pub fn random_seed() -> u32 {
#[allow(unsafe_code)]
// NOTE: this comes from `Math.random` and returns a float in the <0, 1> range:
let random_float = unsafe { ::random() };
(random_float * ::std::u32::MAX as f32) as u32
}

0 comments on commit fdbc828

Please sign in to comment.