changeset 687:ac092b70c39a

Fix PRNG not to panic, pass it to Enemy, and initialise it with time in eclrenderer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 17 Aug 2019 13:49:31 +0200
parents aefe5b5f481e
children 1adecaddf442
files examples/eclrenderer.rs src/th06/enemy.rs src/util/prng.rs
diffstat 3 files changed, 9 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/examples/eclrenderer.rs	Sat Aug 17 04:28:24 2019 +0200
+++ b/examples/eclrenderer.rs	Sat Aug 17 13:49:31 2019 +0200
@@ -121,8 +121,9 @@
         return;
     }
 
-    // TODO: seed this PRNG with a valid seed.
-    let prng = Rc::new(RefCell::new(Prng::new(0)));
+    // Get the time since January 1970 as a seed for the PRNG.
+    let time = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap();
+    let prng = Rc::new(RefCell::new(Prng::new(time.subsec_micros() as u16)));
 
     // Create the Game god object.
     let game = Game::new(prng, rank);
--- a/src/th06/enemy.rs	Sat Aug 17 04:28:24 2019 +0200
+++ b/src/th06/enemy.rs	Sat Aug 17 13:49:31 2019 +0200
@@ -258,7 +258,7 @@
     /// Create a new enemy.
     pub fn new(pos: Position, life: i32, bonus_dropped: u32, die_score: u32, anm0: Weak<RefCell<Anm0>>, game: Weak<RefCell<Game>>) -> Rc<RefCell<Enemy>> {
         let game_rc = game.upgrade().unwrap();
-        let enemy = Enemy {
+        let mut enemy = Enemy {
             pos,
             anm0,
             game,
@@ -271,8 +271,10 @@
             damageable: true,
             ..Default::default()
         };
+        let mut game = game_rc.borrow_mut();
+        enemy.prng = Rc::downgrade(&game.prng);
         let enemy = Rc::new(RefCell::new(enemy));
-        game_rc.borrow_mut().enemies.push(enemy.clone());
+        game.enemies.push(enemy.clone());
         enemy
     }
 
--- a/src/util/prng.rs	Sat Aug 17 04:28:24 2019 +0200
+++ b/src/util/prng.rs	Sat Aug 17 13:49:31 2019 +0200
@@ -18,8 +18,8 @@
     ///
     /// RE’d from 102h.exe@0x41e780
     pub fn get_u16(&mut self) -> u16 {
-        let x = (self.seed ^ 0x9630) - 0x6553;
-        self.seed = (((x & 0xc000) >> 14) | (x << 2)) & 0xffff;
+        let x = (self.seed ^ 0x9630).wrapping_sub(0x6553);
+        self.seed = ((x & 0xc000) >> 14) | (x << 2);
         self.seed
     }