Mercurial > touhou
annotate pytouhou/utils/random.py @ 50:811cefefb5c8
Fix a few bugs and add support for a few instructions
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 22 Aug 2011 21:16:47 +0200 |
parents | 493b503c81e0 |
children | ab826bc29aa2 |
rev | line source |
---|---|
12
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
1 """ |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
2 This file provides a pseudo-random number generator identical to the one used in |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
3 Touhou 6: The Embodiment of Scarlet Devil. |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
4 It is the only truly reverse-engineered piece of code of this project, |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
5 as it is needed in order to retain compatibility with replay files produced by |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
6 the offical game code. |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
7 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
8 It has been reverse engineered from 102h.exe@0x41e780.""" |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
9 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
10 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
11 #TODO: maybe some post-processing is missing |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
12 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
13 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
14 from time import time |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
15 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
16 class Random(object): |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
17 def __init__(self, seed=None): |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
18 if seed is None: |
39
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
19 seed = int(time() % 65536) |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
20 self.seed = seed |
12
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
21 self.counter = 0 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
22 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
23 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
24 def set_seed(self, seed): |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
25 self.seed = seed |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
26 self.counter = 0 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
27 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
28 |
39
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
29 def rand_uint16(self): |
12
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
30 # Further reverse engineering might be needed. |
39
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
31 x = ((self.seed ^ 0x9630) - 0x6553) & 0xffff |
12
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
32 self.seed = (((x & 0x0c000) >> 0xe) + x*4) & 0xffff |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
33 self.counter += 1 |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
34 self.counter &= 0xffff |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
35 return self.seed |
776453783743
Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
36 |
39
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
37 |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
38 def rand_uint32(self): |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
39 # 102h.exe@0x41e7f0 |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
40 a = self.rand_uint16() << 16 |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
41 a |= self.rand_uint16() |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
42 return a |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
43 |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
44 |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
45 def rand_double(self): |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
46 # 102h.exe@0x41e820 |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
47 return float(self.rand_uint32()) / 0x100000000 |
493b503c81e0
Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents:
12
diff
changeset
|
48 |