comparison pytouhou/utils/random.py @ 12:776453783743

Add PRNG reverse-engineered from EoSD's 102h.exe.
author Thibaut Girka <thib@sitedethib.com>
date Fri, 05 Aug 2011 10:19:14 +0200
parents
children 493b503c81e0
comparison
equal deleted inserted replaced
11:548662d70860 12:776453783743
1 """
2 This file provides a pseudo-random number generator identical to the one used in
3 Touhou 6: The Embodiment of Scarlet Devil.
4 It is the only truly reverse-engineered piece of code of this project,
5 as it is needed in order to retain compatibility with replay files produced by
6 the offical game code.
7
8 It has been reverse engineered from 102h.exe@0x41e780."""
9
10
11 #TODO: maybe some post-processing is missing
12
13
14 from time import time
15
16 class Random(object):
17 def __init__(self, seed=None):
18 if seed is None:
19 seed = int(time.time() % 65536)
20 self.counter = 0
21
22
23 def set_seed(self, seed):
24 self.seed = seed
25 self.counter = 0
26
27
28 def cycle(self):
29 # Named this way because the actual return value may be different.
30 # Further reverse engineering might be needed.
31 x = ((seed ^ 0x9630) - 0x6553) & 0xffff
32 self.seed = (((x & 0x0c000) >> 0xe) + x*4) & 0xffff
33 self.counter += 1
34 self.counter &= 0xffff
35 return self.seed
36