annotate src/util/prng.rs @ 742:0a250ddfae79

examples: Update luminance.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 07 Jan 2020 00:23:15 +0100
parents ac092b70c39a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
646
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 //! Random number generator extracted from EoSD.
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 /// Pseudo-random number generator from EoSD.
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 #[derive(Debug)]
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 pub struct Prng {
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 seed: u16,
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 }
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 impl Prng {
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 /// Create a new pseudo-random number generator from this seed.
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 pub fn new(seed: u16) -> Prng {
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 Prng {
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 seed,
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 }
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 }
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 /// Generates a pseudo-random u16.
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 ///
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 /// RE’d from 102h.exe@0x41e780
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 pub fn get_u16(&mut self) -> u16 {
687
ac092b70c39a Fix PRNG not to panic, pass it to Enemy, and initialise it with time in eclrenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 647
diff changeset
21 let x = (self.seed ^ 0x9630).wrapping_sub(0x6553);
ac092b70c39a Fix PRNG not to panic, pass it to Enemy, and initialise it with time in eclrenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 647
diff changeset
22 self.seed = ((x & 0xc000) >> 14) | (x << 2);
646
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 self.seed
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 }
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 /// Combines two u16 into a single u32.
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 ///
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 /// RE’d from 102h.exe@0x41e7f0
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 pub fn get_u32(&mut self) -> u32 {
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 ((self.get_u16() as u32) << 16) | self.get_u16() as u32
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 }
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 /// Transforms an u32 into a f64.
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 ///
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 /// RE’d from 102h.exe@0x41e820
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 pub fn get_f64(&mut self) -> f64 {
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 self.get_u32() as f64 / (0x100000000u64 as f64)
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 }
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 }