comparison pytouhou/game/eclrunner.py @ 51:0707ff53e7b5

Add support for a few instructions
author Thibaut Girka <thib@sitedethib.com>
date Mon, 22 Aug 2011 22:12:34 +0200
parents 811cefefb5c8
children ab826bc29aa2
comparison
equal deleted inserted replaced
50:811cefefb5c8 51:0707ff53e7b5
1 from math import atan2, cos, sin
2
3
1 class MetaRegistry(type): 4 class MetaRegistry(type):
2 def __new__(mcs, name, bases, classdict): 5 def __new__(mcs, name, bases, classdict):
3 instruction_handlers = {} 6 instruction_handlers = {}
4 for item in classdict.itervalues(): 7 for item in classdict.itervalues():
5 try: 8 try:
25 28
26 29
27 class ECLRunner(object): 30 class ECLRunner(object):
28 __metaclass__ = MetaRegistry 31 __metaclass__ = MetaRegistry
29 __slots__ = ('_ecl', '_enemy', '_game_state', 'variables', 'sub', 'frame', 32 __slots__ = ('_ecl', '_enemy', '_game_state', 'variables', 'sub', 'frame',
30 'instruction_pointer', 'stack') 33 'instruction_pointer', 'comparison_reg', 'stack')
31 34
32 def __init__(self, ecl, sub, enemy, game_state): 35 def __init__(self, ecl, sub, enemy, game_state):
33 # Things not supposed to change 36 # Things not supposed to change
34 self._ecl = ecl 37 self._ecl = ecl
35 self._enemy = enemy 38 self._enemy = enemy
37 40
38 # Things supposed to change (and be put in the stack) 41 # Things supposed to change (and be put in the stack)
39 self.variables = [0, 0, 0, 0, 42 self.variables = [0, 0, 0, 0,
40 0., 0., 0., 0., 43 0., 0., 0., 0.,
41 0, 0, 0, 0] 44 0, 0, 0, 0]
45 self.comparison_reg = 0
42 self.sub = sub 46 self.sub = sub
43 self.frame = 0 47 self.frame = 0
44 self.instruction_pointer = 0 48 self.instruction_pointer = 0
45 49
46 self.stack = [] 50 self.stack = []
129 raise IndexError #TODO: proper exception 133 raise IndexError #TODO: proper exception
130 else: 134 else:
131 raise IndexError #TODO: proper exception 135 raise IndexError #TODO: proper exception
132 136
133 137
138 @instruction(0)
139 def noop(self):
140 pass #TODO: Really?
141
142
134 @instruction(1) 143 @instruction(1)
135 def destroy(self, arg): 144 def destroy(self, arg):
136 #TODO: arg? 145 #TODO: arg?
137 self._enemy._removed = True 146 self._enemy._removed = True
138 147
176 def substract(self, variable_id, a, b): 185 def substract(self, variable_id, a, b):
177 #TODO: int vs float thing 186 #TODO: int vs float thing
178 self._setval(variable_id, self._getval(a) - self._getval(b)) 187 self._setval(variable_id, self._getval(a) - self._getval(b))
179 188
180 189
190 @instruction(27)
191 def compare_ints(self, a, b):
192 a, b = self._getval(a), self._getval(b)
193 if a < b:
194 self.comparison_reg = -1
195 elif a == b:
196 self.comparison_reg = 0
197 else:
198 self.comparison_reg = 1
199
200
201 @instruction(31)
202 def relative_jump_if_equal(self, frame, instruction_pointer):
203 if self.comparison_reg == 0:
204 self.relative_jump(frame, instruction_pointer)
205
206
181 @instruction(35) 207 @instruction(35)
182 def call(self, sub, param1, param2): 208 def call(self, sub, param1, param2):
183 self.stack.append((self.sub, self.frame, self.instruction_pointer, 209 self.stack.append((self.sub, self.frame, self.instruction_pointer,
184 self.variables)) 210 self.variables, self.comparison_reg))
185 self.sub = sub 211 self.sub = sub
186 self.frame = 0 212 self.frame = 0
187 self.instruction_pointer = 0 213 self.instruction_pointer = 0
188 self.variables = [param1, 0, 0, 0, 214 self.variables = [param1, 0, 0, 0,
189 param2, 0., 0., 0., 215 param2, 0., 0., 0.,
190 0, 0, 0, 0] 216 0, 0, 0, 0]
191 217
192 218
193 @instruction(36) 219 @instruction(36)
194 def ret(self): 220 def ret(self):
195 self.sub, self.frame, self.instruction_pointer, self.variables = self.stack.pop() 221 self.sub, self.frame, self.instruction_pointer, self.variables, self.comparison_reg = self.stack.pop()
196 222
197 223
198 @instruction(39) 224 @instruction(39)
199 def call_if_equal(self, sub, param1, param2, a, b): 225 def call_if_equal(self, sub, param1, param2, a, b):
200 if self._getval(a) == self._getval(b): 226 if self._getval(a) == self._getval(b):
222 248
223 249
224 @instruction(48) 250 @instruction(48)
225 def set_acceleration(self, acceleration): 251 def set_acceleration(self, acceleration):
226 self._enemy.acceleration = acceleration 252 self._enemy.acceleration = acceleration
253
254
255 @instruction(50)
256 def set_random_angle(self, min_angle, max_angle):
257 #TODO: This thing is really odd, check, double check, triple check, disassemble...
258 angle = self._game_state.prng.rand_double() * (max_angle - min_angle) + min_angle
259 self._enemy.angle = atan2(-abs(sin(angle)), -abs(cos(angle)))
227 260
228 261
229 @instruction(51) 262 @instruction(51)
230 def target_player(self, unknown, speed): 263 def target_player(self, unknown, speed):
231 #TODO: unknown 264 #TODO: unknown
307 self.sub = value 340 self.sub = value
308 self.frame = 0 341 self.frame = 0
309 self.instruction_pointer = 0 342 self.instruction_pointer = 0
310 343
311 344
345 @instruction(111)
346 def set_life(self, value):
347 self._enemy.life = value
348
349
312 @instruction(113) 350 @instruction(113)
313 def set_low_life_trigger(self, value): 351 def set_low_life_trigger(self, value):
314 self._enemy.low_life_trigger = value 352 self._enemy.low_life_trigger = value
315 353
316 354