annotate pytouhou/utils/random.py @ 159:4684d311a32d

Remove remaining pop_enemy/new_enemy mess
author Thibaut Girka <thib@sitedethib.com>
date Mon, 10 Oct 2011 19:01:23 +0200
parents ab826bc29aa2
children 3c2a9e28198c
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
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 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
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
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
44 def rand_uint16(self):
12
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 # 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
46 x = ((self.seed ^ 0x9630) - 0x6553) & 0xffff
52
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 39
diff changeset
47 self.seed = (((x & 0x0c000) >> 0xe) | (x << 2)) & 0xffff
12
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 self.counter += 1
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 self.counter &= 0xffff
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 return self.seed
776453783743 Add PRNG reverse-engineered from EoSD's 102h.exe.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51
39
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
52
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
53 def rand_uint32(self):
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
54 # 102h.exe@0x41e7f0
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
55 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
56 a |= self.rand_uint16()
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
57 return a
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
58
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
59
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
60 def rand_double(self):
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
61 # 102h.exe@0x41e820
493b503c81e0 Fix PRNG and add some methods to perform like EoSD
Thibaut Girka <thib@sitedethib.com>
parents: 12
diff changeset
62 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
63