Skip to content

Commit 5ac224c

Browse files
committed
Modified read_coordinate to return the x,y coordinate pair as floats
1 parent 1a2e34b commit 5ac224c

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

sc2reader/parsers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def parse_ability_event(self, buffer, frames, type, code, pid):
3535

3636
if ability_flags & 0x10:
3737
# ability(3), coordinates (4), ?? (4)
38-
location = (buffer.read_coordinate(), buffer.read_coordinate())
38+
location = buffer.read_coordinate()
3939
buffer.skip(4)
4040
return LocationAbilityEvent(frames, pid, type, code, ability, location)
4141

@@ -53,7 +53,7 @@ def parse_ability_event(self, buffer, frames, type, code, pid):
5353

5454
elif atype & 0x40: # location/move
5555
# coordinates (4), ?? (6)
56-
location = (buffer.read_coordinate(), buffer.read_coordinate())
56+
location = buffer.read_coordinate()
5757
buffer.skip(5)
5858
return LocationAbilityEvent(frames, pid, type, code, ability, location)
5959

sc2reader/utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __init__(self, file):
6868
self.lo_masks_inv = [0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF]
6969
self.hi_masks = [0xFF ^ mask for mask in self.lo_masks]
7070
self.hi_masks_inv = [0xFF ^ mask for mask in self.lo_masks_inv]
71+
self.coord_convert = [(2**(12 - i),1.0/2**i) for i in range(1,13)]
7172

7273
self.read_basic = self.io.read
7374
'''
@@ -215,9 +216,22 @@ def read_object_id(self):
215216
""" Object ID is big-endian int32 """
216217
return self.read_int(endian=BIG_ENDIAN)
217218

218-
def read_coordinate(self):
219-
coord = self.read(bits=20)
220-
return [coord[0], coord[1] << 4 | coord[2],]
219+
def read_coordinate(self):
220+
# Combine coordinate whole and fraction
221+
def _coord_to_float(coord):
222+
fraction = 0
223+
for mask,quotient in self.coord_convert:
224+
if (coord[1] & mask):
225+
fraction = fraction + quotient
226+
return coord[0] + fraction
227+
228+
# Read an x or y coordinate dimension
229+
def _coord_dimension():
230+
coord = self.read(bits=20)
231+
return _coord_to_float([coord[0], coord[1] << 4 | coord[2],])
232+
233+
# TODO?: Handle optional z dimension
234+
return (_coord_dimension(), _coord_dimension())
221235

222236
def read_bitmask(self):
223237
""" Reads a bitmask given the current bitoffset """

0 commit comments

Comments
 (0)