annotate pytouhou/game/eclrunner.py @ 47:1f1793e7ec8e

Handle a few more opcodes
author Thibaut Girka <thib@sitedethib.com>
date Mon, 22 Aug 2011 10:49:59 +0200
parents 7195aaf95f6e
children 8353c33d53d4
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,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
15 2: self.relative_jump,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
16 3: self.relative_jump_ex,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
17 35: self.call,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
18 36: self.ret,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
19 109: self.memory_write}
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
20 if implementation:
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
21 self.implementation.update(implementation)
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
22
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
23
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
24 def memory_write(self, value, index):
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
25 #TODO
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
26 #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
27 if index == 0:
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
28 self.sub = value
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
29 self.frame = 0
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
30 self.instruction_pointer = 0
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
31
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
32
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
33 def call(self, sub, param1, param2):
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
34 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
35 self.variables))
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
36 self.sub = sub
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
37 self.frame = 0
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
38 self.instruction_pointer = 0
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
39 self.variables = [param1, 0, 0, 0,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
40 param2, 0., 0., 0.,
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
41 0, 0, 0, 0]
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
42
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
43
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
44 def ret(self):
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
45 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
46
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
48
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
49 def set_variable(self, variable_id, value):
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
50 #TODO: -10013 and beyond!
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
51 self.variables[-10000-variable_id] = value
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53
43
7195aaf95f6e Fix set_counter, and relative_jump(_ex)
Thibaut Girka <thib@sitedethib.com>
parents: 42
diff changeset
54 def relative_jump(self, frame, instruction_pointer):
7195aaf95f6e Fix set_counter, and relative_jump(_ex)
Thibaut Girka <thib@sitedethib.com>
parents: 42
diff changeset
55 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
56
7195aaf95f6e Fix set_counter, and relative_jump(_ex)
Thibaut Girka <thib@sitedethib.com>
parents: 42
diff changeset
57
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
58 def relative_jump_ex(self, frame, instruction_pointer, variable_id):
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
59 if self.variables[-10000-variable_id]:
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
60 self.variables[-10000-variable_id] -= 1
43
7195aaf95f6e Fix set_counter, and relative_jump(_ex)
Thibaut Girka <thib@sitedethib.com>
parents: 42
diff changeset
61 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
62
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 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
65 frame = self.frame
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
66 try:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
67 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
68 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
69
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
70 if frame == self.frame:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
71 try:
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
72 callback = self.implementation[instr_type]
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
73 except KeyError:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
74 print('Warning: unhandled opcode %d!' % instr_type) #TODO
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
75 else:
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
76 callback(*args)
47
1f1793e7ec8e Handle a few more opcodes
Thibaut Girka <thib@sitedethib.com>
parents: 43
diff changeset
77 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
78 if frame <= self.frame:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
79 self.instruction_pointer += 1
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
80 except IndexError:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
81 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
82
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 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
84