GraylinKim / sc2reader

A python library that extracts data from various Starcraft II resources to power tools and services for the SC2 community. Who doesn't want to hack on the games they play?
http://sc2reader.readthedocs.org
MIT License
413 stars 85 forks source link

Implement new unit model #93

Closed GraylinKim closed 11 years ago

GraylinKim commented 11 years ago

The old model of having instantiating units of a specific unit type is broken because a unit can be upgraded or change modes into a different type but still be the same unit.

The easiest way to do this will be to just instantiate raw Unit objects and set unit.name and unit.type appropriately. When type changing events occur, just update the unit.name and unit.type values.

GraylinKim commented 11 years ago

I've implemented a fix to this issue in the new_unit branch. The idea is to switch from a unit being a type to a unit having a type. Example:

unit # <class 'Unit'>
unit.name # SeigeTank
unit.type # <class 'SeigeTank'>
unit.type.race # Terran

Additionally, units are no longer unique based on type. Only uid is considered when comparing units for identity.

class Unit(object):
  def __cmp__(self, other):
    return cmp(self.uid, other.uid)

This will reduce the total number of unit objects created when processing replays because new units won't be created when their type changes (SeigeTanks, Hatcheries, Roaches[Burrowed]). This makes it more important to understand the circumstances that the engine changes the type instead of creating a new unit. Type is changed for events like Burrow and Morphing (type changed to cocoon) but the finished Baneling is a new unit.

GraylinKim commented 11 years ago

I added property methods to the unit class to make the same interface available that was there before.

unit.type => 188
unit.race => 'Zerg'
unit.minerals => 75
unit.vespene => 25
unit.supply => 2

All tests passed. Any further issues on this line of inquiry can go in a new issue.