comparison pytouhou/game/eclrunner.py @ 50:811cefefb5c8

Fix a few bugs and add support for a few instructions
author Thibaut Girka <thib@sitedethib.com>
date Mon, 22 Aug 2011 21:16:47 +0200
parents cbe1cb50f2fd
children 0707ff53e7b5
comparison
equal deleted inserted replaced
49:cbe1cb50f2fd 50:811cefefb5c8
54 # Then, check for callbacks 54 # Then, check for callbacks
55 #TODO 55 #TODO
56 56
57 # Now, process script 57 # Now, process script
58 frame = self.frame 58 frame = self.frame
59 try: 59 while True:
60 while frame <= self.frame: 60 try:
61 frame, instr_type, rank_mask, param_mask, args = self._ecl.subs[self.sub][self.instruction_pointer] 61 frame, instr_type, rank_mask, param_mask, args = self._ecl.subs[self.sub][self.instruction_pointer]
62 62 except IndexError:
63 #TODO: skip bad ranks 63 return False
64 64
65 if frame == self.frame: 65 #TODO: skip bad ranks
66 try: 66
67 callback = self._handlers[instr_type] 67 if frame > self.frame:
68 except KeyError: 68 break
69 print('Warning: unhandled opcode %d!' % instr_type) #TODO 69 else:
70 else: 70 self.instruction_pointer += 1
71 callback(self, *args) 71 if frame == self.frame:
72 frame, instr_type, rank_mask, param_mask, args = self._ecl.subs[self.sub][self.instruction_pointer] 72 try:
73 if frame <= self.frame: 73 callback = self._handlers[instr_type]
74 self.instruction_pointer += 1 74 except KeyError:
75 except IndexError: 75 print('Warning: unhandled opcode %d!' % instr_type) #TODO
76 return False 76 else:
77 callback(self, *args)
77 78
78 self.frame += 1 79 self.frame += 1
79 return True 80 return True
80 81
81 82
82 def _getval(self, value): 83 def _getval(self, value):
83 if -10012 <= value <= -10001: 84 if -10012 <= value <= -10001:
84 return self.variables[int(-10001-value)] 85 return self.variables[int(-10001-value)]
85 elif -10025 <= value <= -10013: 86 elif -10025 <= value <= -10013:
87 if value == -10013:
88 return self._game_state.rank
89 elif value == -10014:
90 return self._game_state.difficulty
91 elif value == -10015:
92 return self._enemy.x
93 elif value == -10016:
94 return self._enemy.y
95 elif value == -10017:
96 return self._enemy.z
97 elif value == -10018:
98 player = self._enemy.select_player(self._game_state.players)
99 return player.x
100 elif value == -10019:
101 player = self._enemy.select_player(self._game_state.players)
102 return player.y
103 elif value == -10021:
104 player = self._enemy.select_player(self._game_state.players)
105 return self._enemy.get_player_angle(player)
106 elif value == -10022:
107 return self._enemy.frame
108 elif value == -10024:
109 return self._enemy.life
86 raise NotImplementedError #TODO 110 raise NotImplementedError #TODO
87 else: 111 else:
88 return value 112 return value
89 113
90 114
91 def _setval(self, variable_id, value): 115 def _setval(self, variable_id, value):
92 if -10012 <= value <= -10001: 116 if -10012 <= variable_id <= -10001:
93 self.variables[int(-10001-variable_id)] = value 117 self.variables[int(-10001-variable_id)] = value
94 elif -10025 <= value <= -10013: 118 elif -10025 <= variable_id <= -10013:
95 raise NotImplementedError #TODO 119 if value == -10015:
120 self._enemy.x = value
121 elif value == -10016:
122 self._enemy.y = value
123 elif value == -10017:
124 self._enemy.z = value
125 elif value == -10022:
126 self._enemy.frame = value
127 elif value == -10024:
128 self._enemy.life = value
129 raise IndexError #TODO: proper exception
96 else: 130 else:
97 raise IndexError #TODO 131 raise IndexError #TODO: proper exception
98 132
99 133
100 @instruction(1) 134 @instruction(1)
101 def destroy(self, arg): 135 def destroy(self, arg):
102 #TODO: arg? 136 #TODO: arg?
108 self.frame, self.instruction_pointer = frame, instruction_pointer 142 self.frame, self.instruction_pointer = frame, instruction_pointer
109 143
110 144
111 @instruction(3) 145 @instruction(3)
112 def relative_jump_ex(self, frame, instruction_pointer, variable_id): 146 def relative_jump_ex(self, frame, instruction_pointer, variable_id):
113 if self.variables[-10001-variable_id]: 147 counter_value = self._getval(variable_id)
114 self.variables[-10001-variable_id] -= 1 148 if counter_value:
149 self._setval(variable_id, counter_value - 1)
115 self.frame, self.instruction_pointer = frame, instruction_pointer 150 self.frame, self.instruction_pointer = frame, instruction_pointer
116 151
117 152
118 @instruction(4) 153 @instruction(4)
119 @instruction(5) 154 @instruction(5)
120 def set_variable(self, variable_id, value): 155 def set_variable(self, variable_id, value):
121 #TODO: -10013 and beyond! 156 self._setval(variable_id, self._getval(value))
122 self.variables[-10001-variable_id] = self._getval(value) 157
158
159 @instruction(6)
160 def set_random_int(self, variable_id, maxval):
161 self._setval(variable_id, int(self._getval(maxval) * self._game_state.prng.rand_double()))
162
163
164 @instruction(8)
165 def set_random_float(self, variable_id, maxval):
166 self._setval(variable_id, self._getval(maxval) * self._game_state.prng.rand_double())
167
168
169 @instruction(20)
170 def add(self, variable_id, a, b):
171 #TODO: int vs float thing
172 self._setval(variable_id, self._getval(a) + self._getval(b))
173
174
175 @instruction(21)
176 def substract(self, variable_id, a, b):
177 #TODO: int vs float thing
178 self._setval(variable_id, self._getval(a) - self._getval(b))
123 179
124 180
125 @instruction(35) 181 @instruction(35)
126 def call(self, sub, param1, param2): 182 def call(self, sub, param1, param2):
127 self.stack.append((self.sub, self.frame, self.instruction_pointer, 183 self.stack.append((self.sub, self.frame, self.instruction_pointer,
137 @instruction(36) 193 @instruction(36)
138 def ret(self): 194 def ret(self):
139 self.sub, self.frame, self.instruction_pointer, self.variables = self.stack.pop() 195 self.sub, self.frame, self.instruction_pointer, self.variables = self.stack.pop()
140 196
141 197
142 @instruction(20) 198 @instruction(39)
143 def add(self, variable_id, a, b): 199 def call_if_equal(self, sub, param1, param2, a, b):
144 #TODO: proper variable handling 200 if self._getval(a) == self._getval(b):
145 #TODO: int vs float thing 201 self.call(sub, param1, param2)
146 self.variables[-10001-variable_id] = self._getval(a) + self._getval(b)
147
148
149 @instruction(21)
150 def substract(self, variable_id, a, b):
151 #TODO: proper variable handling
152 #TODO: int vs float thing
153 self.variables[-10001-variable_id] = self._getval(a) - self._getval(b)
154 202
155 203
156 @instruction(43) 204 @instruction(43)
157 def set_pos(self, x, y, z): 205 def set_pos(self, x, y, z):
158 self._enemy.set_pos(x, y, z) 206 self._enemy.set_pos(x, y, z)
178 self._enemy.acceleration = acceleration 226 self._enemy.acceleration = acceleration
179 227
180 228
181 @instruction(51) 229 @instruction(51)
182 def target_player(self, unknown, speed): 230 def target_player(self, unknown, speed):
183 self._enemy.speed = speed #TODO: unknown 231 #TODO: unknown
184 player_x, player_y = 192., 384.#TODO 232 self._enemy.speed = speed
185 self._enemy.angle = atan2(player_y - self._enemy.y, player_x - self._enemy.x) #TODO 233 self._enemy.angle = self._enemy.get_player_angle(self._enemy.select_player(self._game_state.players))
186 234
187 235
188 @instruction(57) 236 @instruction(57)
189 def move_to(self, duration, x, y, z): 237 def move_to(self, duration, x, y, z):
190 self._enemy.move_to(duration, x, y, z) 238 self._enemy.move_to(duration, x, y, z)
239
240
241 @instruction(65)
242 def set_screen_box(self, xmin, ymin, xmax, ymax):
243 self._enemy.screen_box = xmin, ymin, xmax, ymax
244
245
246 @instruction(66)
247 def clear_screen_box(self):
248 self._enemy.screen_box = None
191 249
192 250
193 @instruction(77) 251 @instruction(77)
194 def set_bullet_interval(self, value): 252 def set_bullet_interval(self, value):
195 self._enemy.bullet_launch_interval = value 253 self._enemy.bullet_launch_interval = value