Skip to content

Commit

Permalink
Merged pull request #14 from grom358/obslibmerge.
Browse files Browse the repository at this point in the history
Modified read_coordinate to return the x,y coordinate pair as floats
Minor improvements to data.py and the ability event interface.
  • Loading branch information
GraylinKim committed Apr 29, 2011
2 parents 00b9fb2 + 38b7c78 commit abb235e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
6 changes: 6 additions & 0 deletions sc2reader/data.py
Expand Up @@ -126,6 +126,10 @@ class GameObject(object):
@classmethod
def get_type(cls, code):
return OBJECTTYPE_CODES[code]

@classmethod
def get_ability(cls, code):
return ABILITIES[code]

@classmethod
def has_type(cls, code):
Expand Down Expand Up @@ -1245,8 +1249,10 @@ class RichVespeneGeyser(GameObject):
# The following mineral field and vespene geyser codes were found on the 'Agria Valley' map
class MineralField2(GameObject):
code = 0xf801
name = 'Mineral Field'
class VespeneGeyser2(GameObject):
code = 0xf901
name = 'Vespene Geyser'

class XelnagaTower(GameObject):
code = 0xad01
Expand Down
13 changes: 7 additions & 6 deletions sc2reader/objects.py
Expand Up @@ -244,12 +244,13 @@ def apply(self):
if self.ability not in ABILITIES:
print "Unknown ability (%s) in frame %s" % (hex(self.ability),self.frame)
#raise ValueError("Unknown ability (%s)" % (hex(self.ability)),)
ability = ABILITIES[self.ability]
able = self.get_able_selection(ability)
if able:
object = able[0]
ability = getattr(object, ability)
ability(self.frame)
else:
ability = ABILITIES[self.ability]
able = self.get_able_selection(ability)
if able:
object = able[0]
ability = getattr(object, ability)
ability(self.frame)

# claim units
for obj in self.player.get_selection().current:
Expand Down
4 changes: 2 additions & 2 deletions sc2reader/parsers.py
Expand Up @@ -35,7 +35,7 @@ def parse_ability_event(self, buffer, frames, type, code, pid):

if ability_flags & 0x10:
# ability(3), coordinates (4), ?? (4)
location = (buffer.read_coordinate(), buffer.read_coordinate())
location = buffer.read_coordinate()
buffer.skip(4)
return LocationAbilityEvent(frames, pid, type, code, ability, location)

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

elif atype & 0x40: # location/move
# coordinates (4), ?? (6)
location = (buffer.read_coordinate(), buffer.read_coordinate())
location = buffer.read_coordinate()
buffer.skip(5)
return LocationAbilityEvent(frames, pid, type, code, ability, location)

Expand Down
20 changes: 17 additions & 3 deletions sc2reader/utils.py
Expand Up @@ -68,6 +68,7 @@ def __init__(self, file):
self.lo_masks_inv = [0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF]
self.hi_masks = [0xFF ^ mask for mask in self.lo_masks]
self.hi_masks_inv = [0xFF ^ mask for mask in self.lo_masks_inv]
self.coord_convert = [(2**(12 - i),1.0/2**i) for i in range(1,13)]

self.read_basic = self.io.read
'''
Expand Down Expand Up @@ -215,9 +216,22 @@ def read_object_id(self):
""" Object ID is big-endian int32 """
return self.read_int(endian=BIG_ENDIAN)

def read_coordinate(self):
coord = self.read(bits=20)
return [coord[0], coord[1] << 4 | coord[2],]
def read_coordinate(self):
# Combine coordinate whole and fraction
def _coord_to_float(coord):
fraction = 0
for mask,quotient in self.coord_convert:
if (coord[1] & mask):
fraction = fraction + quotient
return coord[0] + fraction

# Read an x or y coordinate dimension
def _coord_dimension():
coord = self.read(bits=20)
return _coord_to_float([coord[0], coord[1] << 4 | coord[2],])

# TODO?: Handle optional z dimension
return (_coord_dimension(), _coord_dimension())

def read_bitmask(self):
""" Reads a bitmask given the current bitoffset """
Expand Down

0 comments on commit abb235e

Please sign in to comment.