annotate pytouhou/game/eclrunner.py @ 42:1b0ca2fb89f9

Refactor ECL parsing/etc.
author Thibaut Girka <thib@sitedethib.com>
date Thu, 18 Aug 2011 22:11:39 +0200
parents e3ba2fa966f6
children 7195aaf95f6e
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
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 self.labels = {}
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
6 self.implementation = {4: (self.set_label),
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
7 3: (self.goto)}
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 if implementation:
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 self.implementation.update(implementation)
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 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
12 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
13 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
14
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
16 def set_label(self, label, count):
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
17 self.labels[label & 0xffff] = (self.sub, self.instruction_pointer, count)
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
20 def goto(self, frame, instruction_pointer, label):
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 try:
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
22 sub, instruction_pointer, count = self.labels[label & 0xffff]
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 except KeyError:
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 pass
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 else:
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 count -= 1
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 if count:
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
28 self.labels[label & 0xffff] = sub, instruction_pointer, count
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 else:
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
30 del self.labels[label & 0xffff]
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 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
32 self.sub, self.instruction_pointer = sub, instruction_pointer
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 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
36 frame = self.frame
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
37 try:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
38 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
39 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
40
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
41 if frame == self.frame:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
42 try:
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
43 callback = self.implementation[instr_type]
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
44 except KeyError:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
45 print('Warning: unhandled opcode %d!' % instr_type) #TODO
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
46 else:
42
1b0ca2fb89f9 Refactor ECL parsing/etc.
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
47 callback(*args)
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
48 if frame <= self.frame:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
49 self.instruction_pointer += 1
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
50 except IndexError:
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 24
diff changeset
51 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
52
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 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
54