Skip to content

Commit e3ff027

Browse files
committed
Use generic UnitType and Ability classes for data.
The dynamically created classes don't play well with pickle and were unncessarily complex. The only change here is that you can't use this anymore. unit._type_class.__class__.__name__ Instead you can use the shorter: unit._type_class.name No problem. Conflicts: CHANGELOG.rst
1 parent f9cbeaa commit e3ff027

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
0.6.5 - December ?? 2013
66
---------------------------
77

8+
* Use generic UnitType and Ability classes for data. This means no more unit._type_class.__class__.__name__. But hopefully people were not doing that anyway.
89
* Removed the defunct replay.player_names attribute.
910
* Removed the defunct replay.events_by_type attribute.
1011
* Removed the defunct replay.other_people attribute.

sc2reader/data/__init__.py

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333

3434

3535
class Unit(object):
36-
"""
37-
Represents an in-game unit.
38-
"""
39-
36+
"""Represents an in-game unit."""
4037
def __init__(self, unit_id):
4138
#: A reference to the player that currently owns this unit. Only available for 2.0.8+ replays.
4239
self.owner = None
@@ -195,22 +192,64 @@ def __repr__(self):
195192
return str(self)
196193

197194

195+
class UnitType(object):
196+
""" Represents an in game unit type """
197+
def __init__(self, type_id, str_id=None, name=None, title=None, race=None, minerals=0,
198+
vespene=0, supply=0, is_building=False, is_worker=False, is_army=False):
199+
#: The internal integer id representing this unit type
200+
self.id = type_id
201+
202+
#: The internal string id representing this unit type
203+
self.str_id = str_id
204+
205+
#: The name of this unit type
206+
self.name = name
207+
208+
#: The printable title of this unit type; has spaces and possibly punctuation
209+
self.title = title
210+
211+
#: The race this unit type belongs to
212+
self.race = race
213+
214+
#: The mineral cost of this unit type
215+
self.minerals = minerals
216+
217+
#: The vespene cost of this unit type
218+
self.vespene = vespene
219+
220+
#: The supply cost of this unit type
221+
self.supply = supply
222+
223+
#: Boolean flagging this unit type as a building
224+
self.is_building = is_building
225+
226+
#: Boolean flagging this unit type as a worker
227+
self.is_worker = is_worker
228+
229+
#: Boolean flagging this unit type as an army unit
230+
self.is_army = is_army
231+
232+
198233
class Ability(object):
234+
""" Represents an in-game ability """
235+
def __init__(self, id, name=None, title=None, is_build=False, build_time=0, build_unit=None):
236+
#: The internal integer id representing this ability.
237+
self.id = id
199238

200-
#: The internal integer id representing this ability.
201-
id = None
239+
#: The name of this ability
240+
self.name = name
202241

203-
#: The name of this ability
204-
name = ""
242+
#: The printable title of this ability; has spaces and possibly punctuation.
243+
self.title = title
205244

206-
#: Boolean flagging this ability as creating a new unit.
207-
is_build = False
245+
#: Boolean flagging this ability as creating a new unit.
246+
self.is_build = is_build
208247

209-
#: The number of seconds required to build this unit. 0 if not ``is_build``.
210-
build_time = 0
248+
#: The number of seconds required to build this unit. 0 if not ``is_build``.
249+
self.build_time = build_time
211250

212-
#: A reference to the :class:`Unit` type built by this ability. None if not ``is_build``.
213-
build_unit = None
251+
#: A reference to the :class:`UnitType` type built by this ability. Default to None.
252+
self.build_unit = build_unit
214253

215254

216255
@loggable
@@ -260,20 +299,20 @@ def change_type(self, unit, new_type, frame):
260299
self.logger.error("Unable to change type of {0} to {1} [frame {2}]; unit type not found in build {3}".format(unit, new_type, frame, self.id))
261300

262301
def add_ability(self, ability_id, name, title=None, is_build=False, build_time=None, build_unit=None):
263-
ability = type(str(name), (Ability,), dict(
264-
id=ability_id,
302+
ability = Ability(
303+
ability_id,
265304
name=name,
266305
title=title or name,
267306
is_build=is_build,
268307
build_time=build_time,
269308
build_unit=build_unit
270-
))
309+
)
271310
setattr(self, name, ability)
272311
self.abilities[ability_id] = ability
273312

274313
def add_unit_type(self, type_id, str_id, name, title=None, race='Neutral', minerals=0, vespene=0, supply=0, is_building=False, is_worker=False, is_army=False):
275-
unit = type(str(name), (Unit,), dict(
276-
id=type_id,
314+
unit = UnitType(
315+
type_id,
277316
str_id=str_id,
278317
name=name,
279318
title=title or name,

0 commit comments

Comments
 (0)