annotate pytouhou/utils/random.py @ 474:ca22df9e70bc

Fix a crash when handling non-ascii paths
author Thibaut Girka <thib@sitedethib.com>
date Fri, 27 Sep 2013 18:02:04 +0200
parents 3c2a9e28198c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
1 # -*- encoding: utf-8 -*-
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
2 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
4 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
6 ## it under the terms of the GNU General Public License as published
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
7 ## by the Free Software Foundation; version 3 only.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
8 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
9 ## This program is distributed in the hope that it will be useful,
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
12 ## GNU General Public License for more details.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
13 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
14
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
15
12
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 """
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 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
18 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
19 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
20 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
21 the offical game code.
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22
179
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
23 It has been reverse engineered from 102h.exe."""
12
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 #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
27
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 from time import time
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 class Random(object):
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 def __init__(self, seed=None):
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 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
34 seed = int(time() % 65536)
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
35 self.seed = seed
12
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 self.counter = 0
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 def set_seed(self, seed):
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 self.seed = seed
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 self.counter = 0
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43
179
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
44 def rewind(self):
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
45 """Rewind the PRNG by 1 step. This is the reverse of rand_uint16.
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
46 Might be useful for debugging purposes.
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
47 """
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
48 x = self.seed
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
49 x = (x >> 2) | ((x & 3) << 14)
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
50 self.seed = ((x + 0x6553) & 0xffff) ^ 0x9630
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
51 self.counter -= 1
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
52 return self.seed
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
53
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
54
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
55 def rand_uint16(self):
179
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
56 # 102h.exe@0x41e780
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
57 x = ((self.seed ^ 0x9630) - 0x6553) & 0xffff
179
3c2a9e28198c Make rand_uint16 slighty easier to understand, and add a rewind function for debugging purposes.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
58 self.seed = (((x & 0xc000) >> 14) | (x << 2)) & 0xffff
12
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 self.counter += 1
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 return self.seed
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
62
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
63 def rand_uint32(self):
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
64 # 102h.exe@0x41e7f0
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
65 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
66 a |= self.rand_uint16()
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
67 return a
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
68
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
69
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
70 def rand_double(self):
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
71 # 102h.exe@0x41e820
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
72 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
73