comparison pytouhou/game/eclrunner.py @ 23:444ac7bca7bc

Refacto ECL stuff, add support for a few instructions, and add some culling
author Thibaut Girka <thib@sitedethib.com>
date Fri, 12 Aug 2011 19:13:43 +0200
parents
children 93aa1b55d97c
comparison
equal deleted inserted replaced
22:fa87db09fc3a 23:444ac7bca7bc
1 from struct import unpack
2
3
4 class ECLRunner(object):
5 def __init__(self, ecl, sub, frame=0, instruction_pointer=0, implementation=None):
6 self.ecl = ecl
7
8 self.labels = {}
9 self.implementation = {4: ('HHI', self.set_label),
10 3: ('IHHHH', self.goto)}
11 if implementation:
12 self.implementation.update(implementation)
13
14 self.sub = sub
15 self.frame = frame
16 self.instruction_pointer = instruction_pointer
17
18
19 def set_label(self, label, unknown, count):
20 assert unknown == 0xffff
21 self.labels[label] = (self.sub, self.instruction_pointer, count)
22
23
24 def goto(self, frame, unknown1, unknown2, label, unknown3):
25 try:
26 sub, instruction_pointer, count = self.labels[label]
27 except KeyError:
28 pass
29 else:
30 count -= 1
31 if count:
32 self.labels[label] = sub, instruction_pointer, count
33 else:
34 del self.labels[label]
35 self.frame = frame
36 self.sub, self.instruction_pointer = sub, instruction_pointer
37
38
39 def update(self):
40 frame = self.frame
41 while frame <= self.frame:
42 try:
43 frame, instr_type, rank_mask, param_mask, args = self.ecl.subs[self.sub][self.instruction_pointer]
44 except IndexError:
45 break #TODO: script ended, destroy enemy
46
47 if frame == self.frame:
48 try:
49 format, callback = self.implementation[instr_type]
50 except KeyError:
51 print('Warning: unhandled opcode %d!' % instr_type) #TODO
52 else:
53 callback(*unpack('<' + format, args))
54 self.instruction_pointer += 1
55
56 self.frame += 1
57