Skip to content

Commit e703b98

Browse files
committed
Fix unit types and add regression test.
Closes #136.
1 parent 1356f25 commit e703b98

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ CHANGELOG
44
0.5.2 -
55
--------------------
66

7+
* Fixed #136, unit types from tracker events are used when available.
78
* Deprecated player.gateway for player.region
89
* Reorganized the person/player/observer hierarchy. Top level classes are now Computer, Participant, and Observer. Participant and Computer are both children of player so any isinstance code should still work fine.
910
* Player.uid now means something completely different! Use player.toon_id instead
10-
* Player.uid is now the user id of the player
11+
* Player.uid is now the user id of the player (was player.cid)
1112
* PersonDict can no longer be constructed from a player list and new players cannot be added by string (name). Only integer keys accepted for setting.
1213
* Added a sc2json script contributed by @ChrisLundquist
1314
* Hooked up travis-ci for continuous testing. https://travis-ci.org/GraylinKim/sc2reader

sc2reader/events/game.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,27 @@ def load_context(self, replay):
296296

297297
units = list()
298298
for (unit_id, unit_type, subgroup, intra_subgroup) in self.new_unit_info:
299-
# Hack that defaults viking selection to fighter mode instead of assault
300-
if replay.versions[1] == 2 and replay.build >= 23925 and unit_type == 71:
301-
unit_type = 72
302-
303-
if unit_id in replay.objects:
304-
unit = replay.objects[unit_id]
305-
if not unit.is_type(unit_type):
306-
replay.datapack.change_type(unit, unit_type, self.frame)
299+
# If we don't have access to tracker events, use selection events to create
300+
# new units and track unit type changes. It won't be perfect, but it is better
301+
# than nothing.
302+
if not replay.tracker_events:
303+
# Starting at 23925 the default viking mode is assault. Most people expect
304+
# the default viking mode to be figher so fudge it a bit here.
305+
if replay.versions[1] == 2 and replay.build >= 23925 and unit_type == 71:
306+
unit_type = 72
307+
308+
if unit_id in replay.objects:
309+
unit = replay.objects[unit_id]
310+
if not unit.is_type(unit_type):
311+
replay.datapack.change_type(unit, unit_type, self.frame)
312+
else:
313+
unit = replay.datapack.create_unit(unit_id, unit_type, 0x00, self.frame)
314+
replay.objects[unit_id] = unit
315+
316+
# If we have tracker events, the unit must already exist and must already
317+
# have the correct unit type.
307318
else:
308-
unit = replay.datapack.create_unit(unit_id, unit_type, 0x00, self.frame)
309-
replay.objects[unit_id] = unit
319+
unit = replay.objects[unit_id]
310320

311321
units.append(unit)
312322

sc2reader/resources.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ def __init__(self, replay_file, filename=None, load_level=4, **options):
229229
self.active_units = {}
230230
self.game_fps = 16.0
231231

232+
self.tracker_events = list()
233+
self.game_events = list()
234+
232235
# Bootstrap the readers.
233236
self.registered_readers = defaultdict(list)
234237
self.register_default_readers()
46.9 KB
Binary file not shown.

test_replays/test_all.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ def test_cn_replays(self):
244244
self.assertEquals(replay.gateway, "cn")
245245
self.assertEquals(replay.expansion, "WoL")
246246

247+
def test_unit_types(self):
248+
""" sc2reader#136 regression test """
249+
replay = sc2reader.load_replay('test_replays/2.0.8.25604/issue136.SC2Replay')
250+
hellion_times = [u.started_at for u in replay.players[0].units if u.name == 'Hellion']
251+
hellbat_times = [u.started_at for u in replay.players[0].units if u.name == 'BattleHellion']
252+
self.assertEquals(hellion_times, [5180, 5183])
253+
self.assertEquals(hellbat_times, [6736, 6741, 7215, 7220, 12004, 12038])
254+
247255
def test_plugins(self):
248256
from sc2reader.plugins.replay import APMTracker, SelectionTracker, toJSON
249257
factory = sc2reader.factories.SC2Factory()

0 commit comments

Comments
 (0)