annotate pytouhou/utils/random.pyx @ 515:b3193b43a86c

Add an indirection layer for textures, to cope with drivers assigning them random names.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 06 Dec 2013 19:02:42 +0100
parents 292fea5c584e
children 4ce3ef053a25
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
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
31 cdef class Random:
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
32 def __init__(self, long seed=-1):
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
33 if seed < 0:
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
34 seed = time()
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
35 self.set_seed(<unsigned short>(seed & 65535))
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
36
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
37
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
38 cdef void set_seed(self, unsigned short seed) nogil:
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
39 self.seed = seed
12
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 self.counter = 0
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
43 cdef unsigned short rewind(self) nogil:
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 """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
45 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
46 """
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 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
48 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
49 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
50 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
51 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
52
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
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
54 cpdef unsigned short 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
55 # 102h.exe@0x41e780
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
56 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
57 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
58 self.counter += 1
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 return self.seed
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
61
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
62 cpdef unsigned int rand_uint32(self):
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
63 # 102h.exe@0x41e7f0
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
64 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
65 a |= self.rand_uint16()
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
66 return a
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
67
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
68
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
69 cpdef double rand_double(self):
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
70 # 102h.exe@0x41e820
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
71 return self.rand_uint32() / <double>0x100000000