Skip to content

Commit 07c3eca

Browse files
committed
Properly handle hallucinated units. Fixes #153.
1 parent 50e3adc commit 07c3eca

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

sc2reader/data/__init__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Unit(object):
3737
Represents an in-game unit.
3838
"""
3939

40-
def __init__(self, unit_id, flags):
40+
def __init__(self, unit_id):
4141
#: A reference to the player that currently owns this unit. Only available for 2.0.8+ replays.
4242
self.owner = None
4343

@@ -66,8 +66,6 @@ def __init__(self, unit_id, flags):
6666
#: behind the fog of war is targetted.
6767
self.id = unit_id
6868

69-
self.flags = flags
70-
7169
#: A reference to the unit type this unit is current in.
7270
#: e.g. SeigeTank is a different type than SeigeTankSeiged
7371
self._type_class = None
@@ -76,8 +74,14 @@ def __init__(self, unit_id, flags):
7674
#: in order by frame the type was acquired.
7775
self.type_history = OrderedDict()
7876

79-
#: Is this unit type a hallucinated one? Unsure of this flag..
80-
self.hallucinated = (flags & 2 == 2)
77+
#: Is this unit type a hallucinated one?
78+
self.hallucinated = False
79+
80+
self.flags = 0
81+
82+
def apply_flags(self, flags):
83+
self.flags = flags
84+
self.hallucinated = flags & 2 == 2
8185

8286
def set_type(self, unit_type, frame):
8387
self._type_class = unit_type
@@ -231,14 +235,14 @@ def __init__(self, build_id):
231235
#: A dictionary mapping integer ids to available abilities.
232236
self.abilities = dict()
233237

234-
def create_unit(self, unit_id, unit_type, unit_flags, frame):
238+
def create_unit(self, unit_id, unit_type, frame):
235239
"""
236240
:param unit_id: The unique id of this unit.
237241
:param unit_type: The unit type to assign to the new unit
238242
239243
Creates a new unit and assigns it to the specified type.
240244
"""
241-
unit = Unit(unit_id, unit_flags)
245+
unit = Unit(unit_id)
242246
self.change_type(unit, unit_type, frame)
243247
return unit
244248

sc2reader/engine/plugins/context.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def handleTargetAbilityEvent(self, event, replay):
5555
# Often when the target_unit_id is not in replay.objects it is 0 because it
5656
# is a target building/destructable hidden by fog of war. Perhaps we can match
5757
# it through the fog using location?
58-
unit = replay.datapack.create_unit(event.target_unit_id, event.target_unit_type, 0x00, event.frame)
58+
unit = replay.datapack.create_unit(event.target_unit_id, event.target_unit_type, event.frame)
5959
event.target = unit
6060
replay.objects[event.target_unit_id] = unit
6161

@@ -64,7 +64,8 @@ def handleSelectionEvent(self, event, replay):
6464
return
6565

6666
units = list()
67-
for (unit_id, unit_type, subgroup, intra_subgroup) in event.new_unit_info:
67+
# TODO: Blizzard calls these subgroup flags but that doesn't make sense right now
68+
for (unit_id, unit_type, subgroup_flags, intra_subgroup_flags) in event.new_unit_info:
6869
# If we don't have access to tracker events, use selection events to create
6970
# new units and track unit type changes. It won't be perfect, but it is better
7071
# than nothing.
@@ -79,14 +80,17 @@ def handleSelectionEvent(self, event, replay):
7980
if not unit.is_type(unit_type):
8081
replay.datapack.change_type(unit, unit_type, event.frame)
8182
else:
82-
unit = replay.datapack.create_unit(unit_id, unit_type, 0x00, event.frame)
83+
unit = replay.datapack.create_unit(unit_id, unit_type, event.frame)
8384
replay.objects[unit_id] = unit
8485

8586
# If we have tracker events, the unit must already exist and must already
8687
# have the correct unit type.
8788
else:
8889
unit = replay.objects[unit_id]
8990

91+
# Selection events hold flags on units (like hallucination)
92+
unit.apply_flags(intra_subgroup_flags)
93+
9094
units.append(unit)
9195

9296
event.new_units = event.objects = units
@@ -110,7 +114,7 @@ def handleUnitBornEvent(self, event, replay):
110114
event.unit = replay.objects[event.unit_id]
111115
else:
112116
# TODO: How to tell if something is hallucination?
113-
event.unit = replay.datapack.create_unit(event.unit_id, event.unit_type_name, 0, event.frame)
117+
event.unit = replay.datapack.create_unit(event.unit_id, event.unit_type_name, event.frame)
114118
replay.objects[event.unit_id] = event.unit
115119

116120
replay.active_units[event.unit_id_index] = event.unit
@@ -189,7 +193,7 @@ def handleUnitInitEvent(self, event, replay):
189193
event.unit = replay.objects[event.unit_id]
190194
else:
191195
# TODO: How to tell if something is hallucination?
192-
event.unit = replay.datapack.create_unit(event.unit_id, event.unit_type_name, 0, event.frame)
196+
event.unit = replay.datapack.create_unit(event.unit_id, event.unit_type_name, event.frame)
193197
replay.objects[event.unit_id] = event.unit
194198

195199
replay.active_units[event.unit_id_index] = event.unit

0 commit comments

Comments
 (0)