annotate pytouhou/utils/random.py @ 36:f46c18872796

Minor refactoring
author Thibaut Girka <thib@sitedethib.com>
date Sun, 14 Aug 2011 14:58:30 +0200
parents 776453783743
children 493b503c81e0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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:
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 seed = int(time.time() % 65536)
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 self.counter = 0
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21
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 def set_seed(self, seed):
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 self.seed = seed
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 self.counter = 0
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26
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 def cycle(self):
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 # Named this way because the actual return value may be different.
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.
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 x = ((seed ^ 0x9630) - 0x6553) & 0xffff
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