Mercurial > touhou
annotate pytouhou/game/enemymanager.py @ 18:ca26a84916cb
Add preliminary ECL viewer/interpreter.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Tue, 09 Aug 2011 11:40:48 +0200 |
parents | |
children | 6ebf9539c077 |
rev | line source |
---|---|
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
1 from itertools import chain |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
2 from io import BytesIO |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
3 import os |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
4 from struct import unpack, pack |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
5 from pytouhou.game.sprite import Sprite |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
6 from math import cos, sin |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
7 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
8 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
9 class Enemy(object): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
10 def __init__(self, pos, life, _type, script, anms): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
11 self.anms = tuple(anms) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
12 self.anm = None |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
13 self.script = list(script) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
14 self.x, self.y = pos |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
15 self.life = life |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
16 self.type = _type |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
17 self.frame = 0 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
18 self.sprite = None |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
19 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
20 self.angle = 0. |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
21 self.speed = 0. |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
22 self.rotation_speed = 0. |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
23 self.acceleration = 0. |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
24 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
25 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
26 def update(self, frame): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
27 if not self.script: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
28 return True |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
29 if self.script[0][0] == self.frame: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
30 for instr_type, rank_mask, param_mask, args in self.script.pop(0)[1]: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
31 if instr_type == 1: # delete |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
32 return False |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
33 elif instr_type == 97: # set_enemy_sprite |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
34 script_index, = unpack('<I', args) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
35 if script_index in self.anms[0].scripts: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
36 self.sprite = Sprite(self.anms[0], script_index) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
37 self.anm = self.anms[0] |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
38 else: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
39 self.sprite = Sprite(self.anms[1], script_index) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
40 self.anm = self.anms[1] |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
41 elif instr_type == 45: # set_angle_speed |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
42 self.angle, self.speed = unpack('<ff', args) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
43 elif instr_type == 46: # set_angle |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
44 self.rotation_speed, = unpack('<f', args) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
45 elif instr_type == 47: # set_speed |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
46 self.speed, = unpack('<f', args) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
47 elif instr_type == 48: # set_acceleration |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
48 self.acceleration, = unpack('<f', args) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
49 if self.sprite: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
50 self.sprite.update() |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
51 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
52 self.speed += self.acceleration #TODO: units? Execution order? |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
53 self.angle += self.rotation_speed #TODO: units? Execution order? |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
54 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
55 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
56 if self.type == 2: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
57 self.x -= dx |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
58 else: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
59 self.x += dx |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
60 self.y += dy |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
61 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
62 self.frame += 1 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
63 return True |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
64 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
65 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
66 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
67 class EnemyManager(object): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
68 def __init__(self, stage, anims, ecl): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
69 self.stage = stage |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
70 self.anims = tuple(anims) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
71 self.main = [] |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
72 self.subs = {} |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
73 self.objects_by_texture = {} |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
74 self.enemies = [] |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
75 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
76 # Populate main |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
77 for frame, sub, instr_type, args in ecl.main: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
78 if not self.main or self.main[-1][0] < frame: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
79 self.main.append((frame, [(sub, instr_type, args)])) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
80 elif self.main[-1][0] == frame: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
81 self.main[-1][1].append((sub, instr_type, args)) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
82 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
83 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
84 # Populate subs |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
85 for i, sub in enumerate(ecl.subs): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
86 for frame, instr_type, rank_mask, param_mask, args in sub: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
87 if i not in self.subs: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
88 self.subs[i] = [] |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
89 if not self.subs[i] or self.subs[i][-1][0] < frame: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
90 self.subs[i].append((frame, [(instr_type, rank_mask, param_mask, args)])) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
91 elif self.subs[i][-1][0] == frame: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
92 self.subs[i][-1][1].append((instr_type, rank_mask, param_mask, args)) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
93 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
94 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
95 def update(self, frame): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
96 if self.main and self.main[0][0] == frame: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
97 for sub, instr_type, args in self.main.pop(0)[1]: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
98 if instr_type in (0, 2): # Normal/mirrored enemy |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
99 x, y, z, life, unknown1, unknown2, unknown3 = args |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
100 self.enemies.append(Enemy((x, y), life, instr_type, self.subs[sub], self.anims)) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
101 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
102 # Update enemies |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
103 for enemy in tuple(self.enemies): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
104 if not enemy.update(frame): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
105 self.enemies.remove(enemy) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
106 continue |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
107 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
108 # Add enemies to vertices/uvs |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
109 self.objects_by_texture = {} |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
110 for enemy in self.enemies: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
111 ox, oy = enemy.x, enemy.y |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
112 if enemy.sprite: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
113 key = enemy.anm.first_name, enemy.anm.secondary_name |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
114 if not key in self.objects_by_texture: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
115 self.objects_by_texture[key] = (0, [], []) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
116 vertices = tuple((x + ox, y + oy, z) for x, y, z in enemy.sprite._vertices) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
117 self.objects_by_texture[key][2].extend(enemy.sprite._uvs) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
118 self.objects_by_texture[key][1].extend(vertices) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
119 for key, (nb_vertices, vertices, uvs) in self.objects_by_texture.items(): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
120 nb_vertices = len(vertices) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
121 vertices = pack('f' * (3 * nb_vertices), *chain(*vertices)) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
122 uvs = pack('f' * (2 * nb_vertices), *chain(*uvs)) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
123 self.objects_by_texture[key] = (nb_vertices, vertices, uvs) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
124 |