Mercurial > touhou
view utils/src/prng.rs @ 795:2d60a14f4816 default tip
python: Rewrite the main entrypoint in Rust
This lets us progressively replace Python modules with Rust ones.
Currently missing features include:
- Saving replays
- Networking code for cooperative mode
- Reading a configuration file for options
- Maybe more.
But the base game is working, so yay!
| author | Link Mauve <linkmauve@linkmauve.fr> |
|---|---|
| date | Tue, 02 Jun 2026 19:06:16 +0200 |
| parents | 1f152ca95658 |
| children |
line wrap: on
line source
//! Random number generator extracted from EoSD. /// Pseudo-random number generator from EoSD. #[derive(Debug)] pub struct Prng { seed: u16, } impl Prng { /// Create a new pseudo-random number generator from this seed. pub fn new(seed: Option<u16>) -> Prng { // TODO: Maybe add a getrandom::u16() to getrandom instead? let seed = seed.unwrap_or_else(|| getrandom::u32().unwrap() as u16); Prng { seed, } } /// Returns the current seed. pub fn get_seed(&self) -> u16 { self.seed } /// Generates a pseudo-random u16. /// /// RE’d from 102h.exe@0x41e780 pub fn get_u16(&mut self) -> u16 { let x = (self.seed ^ 0x9630).wrapping_sub(0x6553); self.seed = ((x & 0xc000) >> 14) | (x << 2); self.seed } /// Combines two u16 into a single u32. /// /// RE’d from 102h.exe@0x41e7f0 pub fn get_u32(&mut self) -> u32 { ((self.get_u16() as u32) << 16) | self.get_u16() as u32 } /// Transforms an u32 into a f64. /// /// RE’d from 102h.exe@0x41e820 pub fn get_f64(&mut self) -> f64 { self.get_u32() as f64 / (0x100000000u64 as f64) } }
