annotate pytouhou/utils/random.pyx @ 616:4ce3ef053a25

Remove every case where an exception could be silently eaten by a cdef function.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 28 Mar 2015 23:21:15 +0100
parents 292fea5c584e
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
616
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 509
diff changeset
28 cimport cython
12
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
616
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 509
diff changeset
31
4ce3ef053a25 Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 509
diff changeset
32 @cython.final
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
33 cdef class Random:
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
34 def __init__(self, long seed=-1):
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
35 if seed < 0:
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
36 seed = time()
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
37 self.set_seed(<unsigned short>(seed & 65535))
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
38
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
39
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
40 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
41 self.seed = seed
12
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 self.counter = 0
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
45 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
46 """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
47 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
48 """
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 = 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
50 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
51 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
52 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
53 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
54
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
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
56 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
57 # 102h.exe@0x41e780
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
58 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
59 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
60 self.counter += 1
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 return self.seed
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
63
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
64 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
65 # 102h.exe@0x41e7f0
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() << 16
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
67 a |= self.rand_uint16()
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
68 return a
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
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
71 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
72 # 102h.exe@0x41e820
509
292fea5c584e Some more type optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 179
diff changeset
73 return self.rand_uint32() / <double>0x100000000