Skip to content

Commit 8e7e0da

Browse files
committed
Add support for unit.type_history.
1 parent 79607b2 commit 8e7e0da

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

sc2reader/data/__init__.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import pkgutil
5+
from collections import OrderedDict
56
from sc2reader.log_utils import loggable
67

78
ABIL_LOOKUP = dict()
@@ -28,14 +29,30 @@ def __init__(self, unit_id, flags):
2829
self.id = unit_id
2930
self.flags = flags
3031
self._type_class = None
32+
self.type_history = OrderedDict()
3133
self.hallucinated = (flags & 2 == 2)
3234

33-
def is_type(self, unit_type):
34-
if isinstance(unit_type, int):
35-
# Unknown units have id==0 and should be equal
36-
return unit_type == self._type_class.id if self._type_class else unit_type == 0
35+
def set_type(self, unit_type, frame):
36+
self._type_class = unit_type
37+
self.type_history[frame] = unit_type
38+
39+
def is_type(self, unit_type, strict=True):
40+
if strict:
41+
if isinstance(unit_type, int):
42+
if self._type_class:
43+
return unit_type == self._type_class.id
44+
else:
45+
return unit_type == 0
46+
else:
47+
return self._type_class == unit_type
3748
else:
38-
return self._type_class == unit_type
49+
if isinstance(unit_type, int):
50+
if self._type_class:
51+
return unit_type in [utype.id for utype in self.type_history.values()]
52+
else:
53+
return unit_type == 0
54+
else:
55+
return unit_type in self.type_history.values()
3956

4057
@property
4158
def name(self):
@@ -101,17 +118,17 @@ def __init__(self, build_id):
101118
self.units = dict()
102119
self.abilities = dict()
103120

104-
def create_unit(self, unit_id, unit_type, unit_flags):
121+
def create_unit(self, unit_id, unit_type, unit_flags, frame):
105122
unit = Unit(unit_id, unit_flags)
106-
self.change_type(unit, unit_type)
123+
self.change_type(unit, unit_type, frame)
107124
return unit
108125

109-
def change_type(self, unit, new_type):
126+
def change_type(self, unit, new_type, frame):
110127
if new_type in self.units:
111-
reference_unit = self.units[new_type]
112-
unit._type_class = reference_unit
128+
unit_type = self.units[new_type]
129+
unit.set_type(unit_type, frame)
113130
else:
114-
self.logger.error("Unable to change type of {0} to {1}; unit type not found in build {2}".format(unit,hex(new_type),self.id))
131+
self.logger.error("Unable to change type of {0} to {1} [frame {2}]; unit type not found in build {3}".format(unit,hex(new_type),frame,self.id))
115132

116133
def add_ability(self, ability_id, name, title=None, is_build=False, build_time=None, build_unit=None):
117134
ability = type(name,(Ability,), dict(

sc2reader/events.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,15 @@ def load_context(self, replay):
255255
if self.target_id in replay.objects:
256256
self.target = replay.objects[self.target_id]
257257
if not self.target.is_type(self.target_type):
258-
replay.datapack.change_type(self.target, self.target_type)
258+
replay.datapack.change_type(self.target, self.target_type, self.frame)
259259

260260
else:
261261
if self.target_type not in replay.datapack.units:
262262
self.logger.error("{0}\t{1}\tMissing unit {2} from {3}".format(self.frame, self.player.name, hex(self.target_type), replay.datapack.id))
263263
unit = Unit(self.target_id, 0x00)
264264

265265
else:
266-
unit = replay.datapack.create_unit(self.target_id, self.target_type, 0x00)
266+
unit = replay.datapack.create_unit(self.target_id, self.target_type, 0x00, self.frame)
267267

268268
self.target = unit
269269
replay.objects[self.target_id] = unit
@@ -337,10 +337,10 @@ def load_context(self, replay):
337337
if unit_id in replay.objects:
338338
unit = replay.objects[unit_id]
339339
if not unit.is_type(unit_type):
340-
replay.datapack.change_type(unit, unit_type)
340+
replay.datapack.change_type(unit, unit_type, self.frame)
341341
else:
342342
if unit_type in replay.datapack.units:
343-
unit = replay.datapack.create_unit(unit_id, unit_type, unit_flags)
343+
unit = replay.datapack.create_unit(unit_id, unit_type, unit_flags, self.frame)
344344
else:
345345
msg = "Unit Type {0} not found in {1}"
346346
self.logger.error(msg.format(hex(unit_type), replay.datapack.__class__.__name__))

0 commit comments

Comments
 (0)