annotate pytouhou/game/eclrunner.py @ 48:8353c33d53d4

Support a few more instructions
author Thibaut Girka <thib@sitedethib.com>
date Mon, 22 Aug 2011 15:45:42 +0200
parents 1f1793e7ec8e
children cbe1cb50f2fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 class ECLRunner(object):
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 def __init__(self, ecl, sub, frame=0, instruction_pointer=0, implementation=None):
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 self.ecl = ecl
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
5 self.variables = [0, 0, 0, 0,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
6 0., 0., 0., 0.,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
7 0, 0, 0, 0]
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 self.sub = sub
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 self.frame = frame
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 self.instruction_pointer = instruction_pointer
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
12 self.stack = []
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
14 self.implementation = {4: self.set_variable,
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
15 5: self.set_variable,
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
16 2: self.relative_jump,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
17 3: self.relative_jump_ex,
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
18 20: self.add,
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
19 21: self.substract,
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
20 35: self.call,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
21 36: self.ret,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
22 109: self.memory_write}
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
23 if implementation:
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
24 self.implementation.update(implementation)
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
25
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
26
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
27 def _get_value(self, value): #TODO: -10013 and beyond!
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
28 assert not -10025 <= value <= -10013
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
29 if -10012 <= value <= -10001:
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
30 return self.variables[int(-10001-value)]
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
31 else:
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
32 return value
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
33
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
34
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
35 def add(self, variable_id, a, b):
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
36 #TODO: proper variable handling
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
37 #TODO: int vs float thing
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
38 self.variables[-10001-variable_id] = self._get_value(a) + self._get_value(b)
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
39
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
40
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
41 def substract(self, variable_id, a, b):
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
42 #TODO: proper variable handling
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
43 #TODO: int vs float thing
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
44 self.variables[-10001-variable_id] = self._get_value(a) - self._get_value(b)
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
45
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
46
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
47
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
48 def memory_write(self, value, index):
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
49 #TODO
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
50 #XXX: this is a hack to display bosses although we don't handle MSG :)
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
51 if index == 0:
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
52 self.sub = value
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
53 self.frame = 0
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
54 self.instruction_pointer = 0
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
55
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
56
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
57 def call(self, sub, param1, param2):
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
58 self.stack.append((self.sub, self.frame, self.instruction_pointer,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
59 self.variables))
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
60 self.sub = sub
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
61 self.frame = 0
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
62 self.instruction_pointer = 0
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
63 self.variables = [param1, 0, 0, 0,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
64 param2, 0., 0., 0.,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
65 0, 0, 0, 0]
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
66
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
67
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
68 def ret(self):
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
69 self.sub, self.frame, self.instruction_pointer, self.variables = self.stack.pop()
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
70
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
71
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
72 def set_variable(self, variable_id, value):
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
73 #TODO: -10013 and beyond!
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
74 self.variables[-10001-variable_id] = self._get_value(value)
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76
43
7195aaf95f6e Fix set_counter, and relative_jump(_ex)
Thibaut Girka <thib@sitedethib.com>
parents: 42
diff changeset
77 def relative_jump(self, frame, instruction_pointer):
7195aaf95f6e Fix set_counter, and relative_jump(_ex)
Thibaut Girka <thib@sitedethib.com>
parents: 42
diff changeset
78 self.frame, self.instruction_pointer = frame, instruction_pointer
7195aaf95f6e Fix set_counter, and relative_jump(_ex)
Thibaut Girka <thib@sitedethib.com>
parents: 42
diff changeset
79
7195aaf95f6e Fix set_counter, and relative_jump(_ex)
Thibaut Girka <thib@sitedethib.com>
parents: 42
diff changeset
80
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
81 def relative_jump_ex(self, frame, instruction_pointer, variable_id):
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
82 if self.variables[-10001-variable_id]:
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 47
diff changeset
83 self.variables[-10001-variable_id] -= 1
43
7195aaf95f6e Fix set_counter, and relative_jump(_ex)
Thibaut Girka <thib@sitedethib.com>
parents: 42
diff changeset
84 self.frame, self.instruction_pointer = frame, instruction_pointer
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 def update(self):
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 frame = self.frame
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
89 try:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
90 while frame <= self.frame:
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 frame, instr_type, rank_mask, param_mask, args = self.ecl.subs[self.sub][self.instruction_pointer]
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
93 if frame == self.frame:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
94 try:
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
95 callback = self.implementation[instr_type]
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
96 except KeyError:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
97 print('Warning: unhandled opcode %d!' % instr_type) #TODO
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
98 else:
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
99 callback(*args)
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
100 frame, instr_type, rank_mask, param_mask, args = self.ecl.subs[self.sub][self.instruction_pointer]
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
101 if frame <= self.frame:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
102 self.instruction_pointer += 1
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
103 except IndexError:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
104 pass #TODO: script ended, destroy enemy
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
105
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106 self.frame += 1
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
107