annotate pytouhou/formats/sht.py @ 219:091301805cce

Move “eclviewer.py” to “eosd” and get rid of run-time depency on Cython.
author Thibaut Girka <thib@sitedethib.com>
date Thu, 15 Dec 2011 19:24:11 +0100
parents 136d29ffe3c2
children 0595315d3880
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
214
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 from struct import unpack
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 from pytouhou.utils.helpers import get_logger
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 logger = get_logger(__name__)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 class Shot(object):
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 def __init__(self):
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 self.interval = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 self.unknown1 = None
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 self.pos = (0., 0.)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 self.hitbox = (0., 0.)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 self.angle = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 self.speed = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 self.damage = 0
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 self.orb = 0
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 self.unknown2 = None
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 self.sprite = 0
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 self.unknown3 = None
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 self.unknown4 = None
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 self.homing = False
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 self.unknown5 = None
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 self.unknown6 = None
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 class SHT(object):
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 def __init__(self):
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 self.unknown1 = None
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 self.level_count = 9
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 self.bombs = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 self.unknown2 = None
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 self.hitbox = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 self.graze_hitbox = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 self.autocollected_item_speed = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 self.item_hitbox = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 self.percentage_of_cherry_loss_on_die = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 self.point_of_collection = 0
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 self.horizontal_vertical_speed = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 self.horizontal_vertical_focused_speed = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 self.diagonal_speed = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 self.diagonal_focused_speed = 0.
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 self.shots = {}
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 @classmethod
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 def read(cls, file):
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 (_, level_count, bombs, _, hitbox, graze_hitbox,
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 autocollected_item_speed, item_hitbox, percentage_of_cherry_loss_on_die,
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 point_of_collection, horizontal_vertical_speed,
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 horizontal_vertical_focused_speed, diagonal_speed,
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 diagonal_focused_speed) = unpack('<hhfI10f', file.read(52))
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 levels = []
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 for i in xrange(level_count):
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 offset, power = unpack('<II', file.read(8))
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 levels.append((power, offset))
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 sht = cls()
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 sht.shots = {}
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 for power, offset in levels:
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 sht.shots[power] = []
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 file.seek(offset)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 while True:
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 interval, unknown1 = unpack('<HH', file.read(4))
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 if interval == 0xffff and unknown1 == 0xffff:
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 break
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 shot = Shot()
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 data = file.read(48)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 (x, y, hitbox_x, hitbox_y, shot.angle, shot.speed,
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 shot.damage, shot.orb, shot.unknown2, shot.sprite,
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 shot.unknown3, unknown4, homing, unknown5,
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 unknown6) = unpack('<6fHBBhh4I', data)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 shot.pos = (x, y)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 shot.hitbox = (hitbox_x, hitbox_y)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 shot.unknown4 = bool(unknown4)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 shot.homing = bool(homing)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 shot.unknown5 = bool(unknown5)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 shot.unknown6 = bool(unknown6)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 sht.shts[power].append(shot)
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 return sht
136d29ffe3c2 Add support for PCB’s SHT format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104