Mercurial > touhou
comparison pytouhou/vm/eclrunner.py @ 97:ac2e5e1c2c3c
Refactor \o/
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 04 Sep 2011 23:50:00 +0200 |
parents | 54929d495654 |
children | 68aa8bf00c88 |
comparison
equal
deleted
inserted
replaced
96:54929d495654 | 97:ac2e5e1c2c3c |
---|---|
18 from pytouhou.utils.helpers import get_logger | 18 from pytouhou.utils.helpers import get_logger |
19 | 19 |
20 from pytouhou.vm.common import MetaRegistry, instruction | 20 from pytouhou.vm.common import MetaRegistry, instruction |
21 | 21 |
22 logger = get_logger(__name__) | 22 logger = get_logger(__name__) |
23 | |
24 | |
25 | |
26 class ECLMainRunner(object): | |
27 __metaclass__ = MetaRegistry | |
28 __slots__ = ('_ecl', '_new_enemy_func', '_game_state', 'processes', | |
29 'instruction_pointer') | |
30 | |
31 def __init__(self, ecl, new_enemy_func, game_state): | |
32 self._ecl = ecl | |
33 self._new_enemy_func = new_enemy_func | |
34 self._game_state = game_state | |
35 | |
36 self.processes = [] | |
37 | |
38 self.instruction_pointer = 0 | |
39 | |
40 | |
41 def run_iter(self): | |
42 while True: | |
43 try: | |
44 frame, sub, instr_type, args = self._ecl.main[self.instruction_pointer] | |
45 except IndexError: | |
46 break | |
47 | |
48 if frame > self._game_state.frame: | |
49 break | |
50 else: | |
51 self.instruction_pointer += 1 | |
52 | |
53 if frame == self._game_state.frame: | |
54 try: | |
55 callback = self._handlers[instr_type] | |
56 except KeyError: | |
57 logger.warn('unhandled opcode %d (args: %r)', instr_type, args) | |
58 else: | |
59 callback(self, sub, instr_type, *args) | |
60 | |
61 self.processes[:] = (process for process in self.processes | |
62 if process.run_iteration()) | |
63 | |
64 | |
65 @instruction(0) | |
66 @instruction(2) | |
67 @instruction(4) | |
68 @instruction(6) | |
69 def pop_enemy(self, sub, instr_type, x, y, z, life, unknown1, unknown2, unknown3): | |
70 if self._game_state.boss: | |
71 return | |
72 if instr_type & 4: | |
73 if x < -990: #102h.exe@0x411820 | |
74 x = self._game_state.prng.rand_double() * 368 | |
75 if y < -990: #102h.exe@0x41184b | |
76 y = self._game_state.prng.rand_double() * 416 | |
77 if z < -990: #102h.exe@0x411881 | |
78 y = self._game_state.prng.rand_double() * 800 | |
79 enemy = self._new_enemy_func((x, y), life, instr_type) | |
80 self.processes.append(ECLRunner(self._ecl, sub, enemy, self._game_state)) | |
81 | |
23 | 82 |
24 | 83 |
25 | 84 |
26 class ECLRunner(object): | 85 class ECLRunner(object): |
27 __metaclass__ = MetaRegistry | 86 __metaclass__ = MetaRegistry |