Mercurial > touhou
annotate src/util/prng.rs @ 679:6020e33d4fc4
Implement a .std renderer, and its associated VM.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 16 Aug 2019 13:40:38 +0200 |
parents | 1520b559cacc |
children | ac092b70c39a |
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 { |
647
1520b559cacc
Double checked prng.
Gauvain "GovanifY" Roussel-Tarbouriech <gauvain@govanify.com>
parents:
646
diff
changeset
|
21 let x = (self.seed ^ 0x9630) - 0x6553; |
646
7d92730bf543
Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 self.seed = (((x & 0xc000) >> 14) | (x << 2)) & 0xffff; |
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 } |