annotate pytouhou/vm/anmrunner.py @ 243:3893a6fc66f1

Update sprite's property even while waiting
author Thibaut Girka <thib@sitedethib.com>
date Mon, 02 Jan 2012 15:54:19 +0100
parents 02de1563fa18
children d3ba32a9096e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 from random import randrange
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 from pytouhou.utils.helpers import get_logger
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 from pytouhou.vm.common import MetaRegistry, instruction
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 logger = get_logger(__name__)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 class ANMRunner(object):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 __metaclass__ = MetaRegistry
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 __slots__ = ('_anm_wrapper', '_sprite', '_running',
81
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
27 'sprite_index_offset',
236
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
28 'script', 'instruction_pointer', 'frame',
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
29 'waiting')
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31
81
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
32 def __init__(self, anm_wrapper, script_id, sprite, sprite_index_offset=0):
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 self._anm_wrapper = anm_wrapper
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 self._sprite = sprite
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 self._running = True
236
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
36 self.waiting = False
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 anm, self.script = anm_wrapper.get_script(script_id)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 self.frame = 0
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 self.instruction_pointer = 0
81
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
41
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
42 self.sprite_index_offset = sprite_index_offset
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44
236
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
45 def interrupt(self, interrupt):
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
46 new_ip = self.script.interrupts.get(interrupt, None)
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
47 if new_ip is None:
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
48 new_ip = self.script.interrupts.get(-1, None)
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
49 if new_ip is None:
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
50 return False
239
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
51 self.instruction_pointer = new_ip
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
52 self.frame, opcode, args = self.script[self.instruction_pointer]
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
53 self.waiting = False
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
54 return True
236
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
55
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
56
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 def run_frame(self):
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
58 if not self._running:
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 return False
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
61 sprite = self._sprite
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
62
240
02de1563fa18 Fix ANM wait, translation/rotation order, and partially implement ANM0 instruction 24
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
63 while self._running and not self.waiting:
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
64 frame, opcode, args = self.script[self.instruction_pointer]
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 if frame > self.frame:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 break
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68 else:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 self.instruction_pointer += 1
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 if frame == self.frame:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 try:
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
73 callback = self._handlers[opcode]
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 except KeyError:
129
284ac8f97a96 Fix potential crash
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
75 logger.warn('unhandled opcode %d (args: %r)', opcode, args)
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76 else:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77 callback(self, *args)
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
78 sprite._changed = True
240
02de1563fa18 Fix ANM wait, translation/rotation order, and partially implement ANM0 instruction 24
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
79
243
3893a6fc66f1 Update sprite's property even while waiting
Thibaut Girka <thib@sitedethib.com>
parents: 240
diff changeset
80 if not self.waiting:
3893a6fc66f1 Update sprite's property even while waiting
Thibaut Girka <thib@sitedethib.com>
parents: 240
diff changeset
81 self.frame += 1
120
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
82
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
83 # Update sprite
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
84 sprite.frame += 1
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
85
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
86 if sprite.rotations_speed_3d != (0., 0., 0.):
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
87 ax, ay, az = sprite.rotations_3d
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
88 sax, say, saz = sprite.rotations_speed_3d
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
89 sprite.rotations_3d = ax + sax, ay + say, az + saz
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
90 sprite._changed = True
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
91
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
92 if sprite.scale_speed != (0., 0.):
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
93 rx, ry = sprite.rescale
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
94 rsx, rsy = sprite.scale_speed
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
95 sprite.rescale = rx + rsx, ry + rsy
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
96 sprite._changed = True
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
97
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
98 if sprite.fade_interpolator:
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
99 sprite.fade_interpolator.update(sprite.frame)
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
100 sprite.alpha = int(sprite.fade_interpolator.values[0])
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
101 sprite._changed = True
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
102
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
103 if sprite.scale_interpolator:
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
104 sprite.scale_interpolator.update(sprite.frame)
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
105 sprite.rescale = sprite.scale_interpolator.values
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
106 sprite._changed = True
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
107
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
108 if sprite.offset_interpolator:
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
109 sprite.offset_interpolator.update(sprite.frame)
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
110 sprite.dest_offset = sprite.offset_interpolator.values
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
111 sprite._changed = True
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
112
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
113 return self._running
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
116 @instruction(0)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
117 def remove(self):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
118 self._sprite._removed = True
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
119 self._running = False
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
120
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
121
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
122 @instruction(1)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
123 def load_sprite(self, sprite_index):
81
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
124 self._sprite.anm, self._sprite.texcoords = self._anm_wrapper.get_sprite(sprite_index + self.sprite_index_offset)
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
125
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
126
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
127 @instruction(2)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
128 def set_scale(self, sx, sy):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
129 self._sprite.rescale = sx, sy
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
130
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
131
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
132 @instruction(3)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
133 def set_alpha(self, alpha):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
134 self._sprite.alpha = alpha % 256 #TODO
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
135
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
136
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
137 @instruction(4)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
138 def set_color(self, b, g, r):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
139 self._sprite.color = (r, g, b)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
140
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
141
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
142 @instruction(5)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
143 def jump(self, instruction_pointer):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
144 #TODO: is that really how it works?
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
145 self.instruction_pointer = instruction_pointer
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
146 self.frame = self.script[self.instruction_pointer][0]
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
147
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
148
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
149 @instruction(7)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
150 def toggle_mirrored(self):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
151 self._sprite.mirrored = not self._sprite.mirrored
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
152
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
153
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
154 @instruction(9)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
155 def set_rotations_3d(self, rx, ry, rz):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
156 self._sprite.rotations_3d = rx, ry, rz
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
157
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
158
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
159 @instruction(10)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
160 def set_rotations_speed_3d(self, srx, sry, srz):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
161 self._sprite.rotations_speed_3d = srx, sry, srz
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
162
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
163
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
164 @instruction(11)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
165 def set_scale_speed(self, ssx, ssy):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
166 self._sprite.scale_speed = ssx, ssy
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
167
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
168
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
169 @instruction(12)
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
170 def fade(self, new_alpha, duration):
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
171 self._sprite.fade(duration, new_alpha, lambda x: x) #TODO: formula
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
172
72
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
173
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
174 @instruction(13)
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
175 def set_blendfunc_alphablend(self):
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
176 self._sprite.blendfunc = 1
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
177
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
178
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
179 @instruction(14)
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
180 def set_blendfunc_add(self):
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
181 self._sprite.blendfunc = 0 #TODO
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
182
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
183
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
184 @instruction(15)
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
185 def keep_still(self):
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
186 self._running = False
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
187
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
188 @instruction(16)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
189 def load_random_sprite(self, min_idx, amp):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
190 #TODO: use the game's PRNG?
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
191 self.load_sprite(min_idx + randrange(amp))
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
192
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
193
170
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
194 @instruction(17)
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
195 def move(self, x, y, z):
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
196 self._sprite.dest_offset = (x, y, z)
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
197
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
198
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
199 @instruction(18)
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
200 def move_in_linear(self, x, y, z, duration):
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
201 self._sprite.move_in(duration, x, y, z, lambda x: x)
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
202
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
203
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
204 @instruction(19)
170
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
205 def move_in_decel(self, x, y, z, duration):
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
206 self._sprite.move_in(duration, x, y, z, lambda x: 2. * x - x ** 2)
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
207
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
208
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
209 @instruction(20)
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
210 def move_in_accel(self, x, y, z, duration):
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
211 self._sprite.move_in(duration, x, y, z, lambda x: x ** 2)
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
212
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
213
236
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
214 @instruction(21)
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
215 def wait(self):
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
216 """Wait for an interrupt.
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
217 """
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
218 self.waiting = True
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
219
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
220
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
221 @instruction(22)
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
222 def interrupt_label(self, interrupt):
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
223 """Noop"""
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
224 pass
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
225
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
226
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
227 @instruction(23)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
228 def set_corner_relative_placement(self):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
229 self._sprite.corner_relative_placement = True #TODO
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
230
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
231
240
02de1563fa18 Fix ANM wait, translation/rotation order, and partially implement ANM0 instruction 24
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
232 @instruction(24)
02de1563fa18 Fix ANM wait, translation/rotation order, and partially implement ANM0 instruction 24
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
233 def wait_ex(self):
02de1563fa18 Fix ANM wait, translation/rotation order, and partially implement ANM0 instruction 24
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
234 """Hide/delete the sprite and wait for an interrupt.
02de1563fa18 Fix ANM wait, translation/rotation order, and partially implement ANM0 instruction 24
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
235 """
02de1563fa18 Fix ANM wait, translation/rotation order, and partially implement ANM0 instruction 24
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
236 #TODO: Hide/delete the sprite and figure what happens exactly
02de1563fa18 Fix ANM wait, translation/rotation order, and partially implement ANM0 instruction 24
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
237 self.waiting = True
02de1563fa18 Fix ANM wait, translation/rotation order, and partially implement ANM0 instruction 24
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
238
02de1563fa18 Fix ANM wait, translation/rotation order, and partially implement ANM0 instruction 24
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
239
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
240 @instruction(25)
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
241 def set_allow_dest_offset(self, value):
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
242 self._sprite.allow_dest_offset = bool(value)
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
243
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
244
81
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
245 @instruction(26)
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
246 def set_automatic_orientation(self, value):
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
247 """If true, rotate by pi-angle around the z axis.
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
248 """
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
249 self._sprite.automatic_orientation = bool(value)
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
250
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
251
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
252 @instruction(27)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
253 def shift_texture_x(self, dx):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
254 tox, toy = self._sprite.texoffsets
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
255 self._sprite.texoffsets = tox + dx, toy
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
256
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
257
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
258 @instruction(28)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
259 def shift_texture_y(self, dy):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
260 tox, toy = self._sprite.texoffsets
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
261 self._sprite.texoffsets = tox, toy + dy
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
262
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
263
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
264 @instruction(30)
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
265 def scale_in(self, sx, sy, duration):
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
266 self._sprite.scale_in(duration, sx, sy, lambda x: x) #TODO: formula
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
267