Skip to content

Commit 6821e46

Browse files
committed
Full tracker event support. Unstable.
1 parent fbec6a7 commit 6821e46

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

sc2reader/events/tracker.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,11 @@ def load_context(self, replay):
170170
if self.unit_id in replay.objects:
171171
self.unit = replay.objects[self.unit_id]
172172
else:
173-
# Create a new unit, but we don't have a type name map yet!
174-
self.unit = None
173+
# TODO: How to tell if something is hallucination?
174+
self.unit = replay.datapack.create_unit(self.unit_id, self.unit_type_name, 0, self.frame)
175+
self.unit.location = self.location
176+
replay.objects[self.unit_id] = self.unit
177+
replay.active_units[self.unit_id_index] = self.unit
175178

176179

177180
class UnitDiedEvent(TrackerEvent):
@@ -204,8 +207,11 @@ def __init__(self, frames, data):
204207
def load_context(self, replay):
205208
if self.unit_id in replay.objects:
206209
self.unit = replay.objects[self.unit_id]
210+
self.unit.location = self.location
211+
del replay.active_units[self.unit_id_index]
207212
else:
208213
# Create a new unit, but we don't have a type name map yet!
214+
print "Unit died before it was born!"
209215
self.unit = None
210216

211217
if self.killer_pid: # This field isn't always available
@@ -245,6 +251,7 @@ def load_context(self, replay):
245251
self.unit = replay.objects[self.unit_id]
246252
else:
247253
# Create a new unit, but we don't have a type name map yet!
254+
print "Unit owner changed before it was born!"
248255
self.unit = None
249256

250257

@@ -269,8 +276,9 @@ def __init__(self, frames, data):
269276
def load_context(self, replay):
270277
if self.unit_id in replay.objects:
271278
self.unit = replay.objects[self.unit_id]
279+
replay.datapack.change_type(self.unit, self.unit_type_name, self.frame)
272280
else:
273-
# Create a new unit, but we don't have a type name map yet!
281+
print "Unit type changed before it was born!"
274282
self.unit = None
275283

276284

@@ -293,6 +301,7 @@ def __init__(self, frames, data):
293301
def load_context(self, replay):
294302
self.player = replay.player[self.pid]
295303
# TODO: We don't have upgrade -> ability maps
304+
# TODO: we can probably do the same thing we did for units
296305

297306

298307
class UnitInitEvent(TrackerEvent):
@@ -338,8 +347,12 @@ def load_context(self, replay):
338347
if self.unit_id in replay.objects:
339348
self.unit = replay.objects[self.unit_id]
340349
else:
341-
# Create a new unit, but we don't have a type name map yet!
342-
self.unit = None
350+
# TODO: How to tell if something is hallucination?
351+
self.unit = replay.datapack.create_unit(self.unit_id, self.unit_type_name, 0, self.frame)
352+
replay.objects[self.unit_id] = self.unit
353+
replay.active_units[self.unit_id_index] = self.unit
354+
355+
self.unit.location = self.location
343356

344357

345358
class UnitDoneEvent(TrackerEvent):
@@ -361,7 +374,7 @@ def load_context(self, replay):
361374
if self.unit_id in replay.objects:
362375
self.unit = replay.objects[self.unit_id]
363376
else:
364-
# Create a new unit, but we don't have a type name map yet!
377+
print "Unit done before it was started!"
365378
self.unit = None
366379

367380

@@ -377,6 +390,16 @@ def __init__(self, frames, data):
377390
#: ???
378391
self.items = data[1]
379392

393+
394+
self.units = list()
395+
396+
self.positions = list()
397+
unit_index = self.first_unit_index
398+
for i in range(0, len(self.items), 3):
399+
unit_index += self.items[i]
400+
x = self.items[i+1]*4
401+
y = self.items[i+2]*4
402+
self.positions.append((unit_index, (x,y)))
380403
""" We need to be able to find units without the recycle id, can't do this yet!
381404
unitTagIndex = event['m_firstUnitIndex']
382405
for i in xrange(len(event['m_items']) / 3):
@@ -385,3 +408,12 @@ def __init__(self, frames, data):
385408
y = event['m_items'][i * 3 + 2] * 4
386409
# the unit with unitTagIndex is now at coordinate (x, y)
387410
"""
411+
412+
def load_context(self, replay):
413+
for unit_index, (x,y) in self.positions:
414+
if unit_index in replay.active_units:
415+
unit = replay.active_units[unit_index]
416+
unit.location = (x,y)
417+
self.units.append(unit)
418+
else:
419+
print "Unit moved that doesn't exist!"

sc2reader/resources.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def __init__(self, replay_file, filename=None, load_level=4, **options):
221221
self.recorder = None # Player object
222222
self.packets = list()
223223
self.objects = {}
224+
self.active_units = {}
224225
self.game_fps = 16.0
225226

226227
# Bootstrap the readers.

0 commit comments

Comments
 (0)