Mercurial > touhou
annotate pytouhou/formats/std.py @ 86:a87a3c080585
Handle a few attack flags
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 04 Sep 2011 00:49:22 +0200 |
parents | 3da4de9decd0 |
children | ca571697ec83 |
rev | line source |
---|---|
52
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
2 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
4 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
8 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
12 ## GNU General Public License for more details. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
13 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
14 |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
15 |
0 | 16 from struct import pack, unpack |
58 | 17 from pytouhou.utils.helpers import read_string, get_logger |
0 | 18 |
58 | 19 logger = get_logger(__name__) |
0 | 20 |
21 | |
22 class Object(object): | |
23 def __init__(self): | |
24 self.header = (b'\x00') * 28 #TODO | |
25 self.quads = [] | |
26 | |
27 | |
28 | |
29 class Stage(object): | |
15 | 30 def __init__(self, num): |
31 self.num = num | |
0 | 32 self.name = '' |
33 self.bgms = (('', ''), ('', ''), ('', '')) | |
34 self.objects = [] | |
35 self.object_instances = [] | |
36 self.script = [] | |
37 | |
38 | |
39 @classmethod | |
15 | 40 def read(cls, file, num): |
41 stage = Stage(num) | |
0 | 42 |
43 nb_objects, nb_faces = unpack('<HH', file.read(4)) | |
44 object_instances_offset, script_offset = unpack('<II', file.read(8)) | |
45 if file.read(4) != b'\x00\x00\x00\x00': | |
46 raise Exception #TODO | |
47 | |
48 stage.name = read_string(file, 128, 'shift-jis') | |
49 | |
50 bgm_a = read_string(file, 128, 'shift-jis') | |
51 bgm_b = read_string(file, 128, 'shift-jis') | |
52 bgm_c = read_string(file, 128, 'shift-jis') | |
53 bgm_d = read_string(file, 128, 'shift-jis') | |
54 | |
55 bgm_a_path = read_string(file, 128, 'ascii') | |
56 bgm_b_path = read_string(file, 128, 'ascii') | |
57 bgm_c_path = read_string(file, 128, 'ascii') | |
58 bgm_d_path = read_string(file, 128, 'ascii') | |
59 | |
60 stage.bgms = [(bgm_a, bgm_a_path), (bgm_b, bgm_b_path), (bgm_c, bgm_c_path), (bgm_d, bgm_d_path)] #TODO: handle ' ' | |
61 | |
62 # Read object definitions | |
63 offsets = unpack('<%s' % ('I' * nb_objects), file.read(4 * nb_objects)) | |
64 for offset in offsets: | |
65 obj = Object() | |
66 obj.header = file.read(28) #TODO: this has to be reversed! | |
67 while True: | |
68 unknown, size = unpack('<HH', file.read(4)) | |
69 if unknown == 0xffff: | |
70 break | |
71 if size != 0x1c: | |
72 raise Exception #TODO | |
73 script_index, _padding, x, y, z, width, height = unpack('<HHfffff', file.read(24)) | |
74 #TODO: store script_index, x, y, z, width and height | |
75 obj.quads.append((script_index, x, y, z, width, height)) | |
76 stage.objects.append(obj) | |
77 | |
78 | |
79 # Read object usages | |
80 file.seek(object_instances_offset) | |
81 while True: | |
82 obj_id, unknown, x, y, z = unpack('<HHfff', file.read(16)) | |
83 if (obj_id, unknown) == (0xffff, 0xffff): | |
84 break | |
85 if unknown != 256: | |
86 raise Exception #TODO | |
87 stage.object_instances.append((stage.objects[obj_id], x, y, z)) | |
88 | |
89 | |
90 # Read other funny things (script) | |
91 file.seek(script_offset) | |
92 while True: | |
93 frame, message_type, size = unpack('<IHH', file.read(8)) | |
94 if (frame, message_type, size) == (0xffffffff, 0xffff, 0xffff): | |
95 break | |
96 if size != 0x0c: | |
97 raise Exception #TODO | |
98 data = file.read(12) | |
13 | 99 #TODO: maybe add a name somewhere |
100 if message_type == 0: # ViewPos | |
101 args = unpack('<fff', data) | |
102 elif message_type == 1: # Color | |
103 args = unpack('<BBBBff', data) | |
104 elif message_type == 2: # ViewPos2 | |
20
6ebf9539c077
Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
105 args = unpack('<fff', data) |
13 | 106 elif message_type == 3: # StartInterpolatingViewPos2 |
107 args = tuple(unpack('<III', data)[:1]) | |
108 elif message_type == 4: # StartInterpolatingFog | |
109 args = tuple(unpack('<III', data)[:1]) | |
110 else: | |
111 args = (data,) | |
58 | 112 logger.warn('unknown opcode %d (data: %r)', message_type, data) |
13 | 113 stage.script.append((frame, message_type, args)) |
0 | 114 |
115 return stage | |
116 |