annotate pytouhou/vm/anmrunner.py @ 239:901489c21d19

Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
author Thibaut Girka <thib@sitedethib.com>
date Sun, 01 Jan 2012 21:12:50 +0100
parents 0e1762b1ab9f
children 02de1563fa18
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
236
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
61 if self.waiting:
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
62 return True
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
63
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
64 sprite = self._sprite
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
65
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 while self._running:
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
67 frame, opcode, args = self.script[self.instruction_pointer]
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 if frame > self.frame:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 break
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 else:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 self.instruction_pointer += 1
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 if frame == self.frame:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75 try:
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
76 callback = self._handlers[opcode]
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77 except KeyError:
129
284ac8f97a96 Fix potential crash
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
78 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
79 else:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 callback(self, *args)
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
81 sprite._changed = True
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 self.frame += 1
120
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
83
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
84 # Update sprite
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
85 sprite.frame += 1
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
86
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
87 if sprite.rotations_speed_3d != (0., 0., 0.):
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
88 ax, ay, az = sprite.rotations_3d
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
89 sax, say, saz = sprite.rotations_speed_3d
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
90 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
91 sprite._changed = True
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
92
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
93 if sprite.scale_speed != (0., 0.):
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
94 rx, ry = sprite.rescale
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
95 rsx, rsy = sprite.scale_speed
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
96 sprite.rescale = rx + rsx, ry + rsy
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
97 sprite._changed = True
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
98
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
99 if sprite.fade_interpolator:
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
100 sprite.fade_interpolator.update(sprite.frame)
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
101 sprite.alpha = int(sprite.fade_interpolator.values[0])
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
102 sprite._changed = True
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
103
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
104 if sprite.scale_interpolator:
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
105 sprite.scale_interpolator.update(sprite.frame)
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
106 sprite.rescale = sprite.scale_interpolator.values
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
107 sprite._changed = True
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
108
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
109 if sprite.offset_interpolator:
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
110 sprite.offset_interpolator.update(sprite.frame)
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
111 sprite.dest_offset = sprite.offset_interpolator.values
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
112 sprite._changed = True
4300a832f033 Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents: 81
diff changeset
113
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114 return self._running
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
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
117 @instruction(0)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
118 def remove(self):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
119 self._sprite._removed = True
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
120 self._running = False
69
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
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
123 @instruction(1)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
124 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
125 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
126
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
127
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
128 @instruction(2)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
129 def set_scale(self, sx, sy):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
130 self._sprite.rescale = sx, sy
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
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
133 @instruction(3)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
134 def set_alpha(self, alpha):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
135 self._sprite.alpha = alpha % 256 #TODO
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
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
138 @instruction(4)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
139 def set_color(self, b, g, r):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
140 self._sprite.color = (r, g, b)
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
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
143 @instruction(5)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
144 def jump(self, instruction_pointer):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
145 #TODO: is that really how it works?
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
146 self.instruction_pointer = instruction_pointer
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
147 self.frame = self.script[self.instruction_pointer][0]
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
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
150 @instruction(7)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
151 def toggle_mirrored(self):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
152 self._sprite.mirrored = not self._sprite.mirrored
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
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
155 @instruction(9)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
156 def set_rotations_3d(self, rx, ry, rz):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
157 self._sprite.rotations_3d = rx, ry, rz
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
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
160 @instruction(10)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
161 def set_rotations_speed_3d(self, srx, sry, srz):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
162 self._sprite.rotations_speed_3d = srx, sry, srz
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
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
165 @instruction(11)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
166 def set_scale_speed(self, ssx, ssy):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
167 self._sprite.scale_speed = ssx, ssy
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
168
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
169
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
170 @instruction(12)
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
171 def fade(self, new_alpha, duration):
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
172 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
173
72
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
174
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
175 @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
176 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
177 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
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
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
180 @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
181 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
182 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
183
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 71
diff changeset
184
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
185 @instruction(15)
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
186 def keep_still(self):
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
187 self._running = False
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
188
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
189 @instruction(16)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
190 def load_random_sprite(self, min_idx, amp):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
191 #TODO: use the game's PRNG?
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
192 self.load_sprite(min_idx + randrange(amp))
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
193
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
194
170
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
195 @instruction(17)
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
196 def move(self, x, y, z):
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
197 self._sprite.dest_offset = (x, y, z)
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
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
200 @instruction(18)
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
201 def move_in_linear(self, x, y, z, duration):
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
202 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
203
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
204
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
205 @instruction(19)
170
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
206 def move_in_decel(self, x, y, z, duration):
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
207 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
208
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
209
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
210 @instruction(20)
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
211 def move_in_accel(self, x, y, z, duration):
e7902309305c Implement move anm instructions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 129
diff changeset
212 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
213
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
214
236
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
215 @instruction(21)
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
216 def wait(self):
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
217 """Wait for an interrupt.
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
218 """
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
219 self.waiting = True
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
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
222 @instruction(22)
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
223 def interrupt_label(self, interrupt):
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
224 """Noop"""
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
225 pass
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
226
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 170
diff changeset
227
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
228 @instruction(23)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
229 def set_corner_relative_placement(self):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
230 self._sprite.corner_relative_placement = True #TODO
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
231
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
232
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
233 @instruction(25)
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
234 def set_allow_dest_offset(self, value):
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
235 self._sprite.allow_dest_offset = bool(value)
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
236
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
237
81
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
238 @instruction(26)
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
239 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
240 """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
241 """
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
242 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
243
f5f9b5eb69a3 Handle one more ANM instruction, and handle sprite indexes offsets
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
244
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
245 @instruction(27)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
246 def shift_texture_x(self, dx):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
247 tox, toy = self._sprite.texoffsets
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
248 self._sprite.texoffsets = tox + dx, toy
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
249
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
250
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
251 @instruction(28)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
252 def shift_texture_y(self, dy):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
253 tox, toy = self._sprite.texoffsets
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
254 self._sprite.texoffsets = tox, toy + dy
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
255
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
256
71
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
257 @instruction(30)
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
258 def scale_in(self, sx, sy, duration):
a03d7a94b997 Add support for a few ANM instructions
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
259 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
260