Skip to content

Commit abb235e

Browse files
committed
Merged pull request #14 from grom358/obslibmerge.
Modified read_coordinate to return the x,y coordinate pair as floats Minor improvements to data.py and the ability event interface.
2 parents 00b9fb2 + 38b7c78 commit abb235e

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

sc2reader/data.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ class GameObject(object):
126126
@classmethod
127127
def get_type(cls, code):
128128
return OBJECTTYPE_CODES[code]
129+
130+
@classmethod
131+
def get_ability(cls, code):
132+
return ABILITIES[code]
129133

130134
@classmethod
131135
def has_type(cls, code):
@@ -1245,8 +1249,10 @@ class RichVespeneGeyser(GameObject):
12451249
# The following mineral field and vespene geyser codes were found on the 'Agria Valley' map
12461250
class MineralField2(GameObject):
12471251
code = 0xf801
1252+
name = 'Mineral Field'
12481253
class VespeneGeyser2(GameObject):
12491254
code = 0xf901
1255+
name = 'Vespene Geyser'
12501256

12511257
class XelnagaTower(GameObject):
12521258
code = 0xad01

sc2reader/objects.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,13 @@ def apply(self):
244244
if self.ability not in ABILITIES:
245245
print "Unknown ability (%s) in frame %s" % (hex(self.ability),self.frame)
246246
#raise ValueError("Unknown ability (%s)" % (hex(self.ability)),)
247-
ability = ABILITIES[self.ability]
248-
able = self.get_able_selection(ability)
249-
if able:
250-
object = able[0]
251-
ability = getattr(object, ability)
252-
ability(self.frame)
247+
else:
248+
ability = ABILITIES[self.ability]
249+
able = self.get_able_selection(ability)
250+
if able:
251+
object = able[0]
252+
ability = getattr(object, ability)
253+
ability(self.frame)
253254

254255
# claim units
255256
for obj in self.player.get_selection().current:

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)