1- from __future__ import absolute_import
1+ # -*- coding: utf-8 -*-
2+ from __future__ import absolute_import , print_function , unicode_literals , division
23
34import json
45import pkgutil
1112from sc2reader .log_utils import loggable
1213
1314ABIL_LOOKUP = dict ()
14- for entry in pkgutil .get_data ('sc2reader.data' , 'ability_lookup.csv' ).split ('\n ' ):
15- if not entry : continue
16- str_id , abilities = entry .split (',' ,1 )
15+ for entry in pkgutil .get_data ('sc2reader.data' , 'ability_lookup.csv' ).decode ('utf8' ).split ('\n ' ):
16+ if not entry :
17+ continue
18+ str_id , abilities = entry .split (',' , 1 )
1719 ABIL_LOOKUP [str_id ] = abilities .split (',' )
1820
1921UNIT_LOOKUP = dict ()
20- for entry in pkgutil .get_data ('sc2reader.data' , 'unit_lookup.csv' ).split ('\n ' ):
21- if not entry : continue
22+ for entry in pkgutil .get_data ('sc2reader.data' , 'unit_lookup.csv' ).decode ('utf8' ).split ('\n ' ):
23+ if not entry :
24+ continue
2225 str_id , title = entry .strip ().split (',' )
2326 UNIT_LOOKUP [str_id ] = title
2427
25- unit_data = pkgutil .get_data ('sc2reader.data' , 'unit_info.json' )
28+ unit_data = pkgutil .get_data ('sc2reader.data' , 'unit_info.json' ). decode ( 'utf8' )
2629unit_lookup = json .loads (unit_data )
2730
28- command_data = pkgutil .get_data ('sc2reader.data' , 'train_commands.json' )
31+ command_data = pkgutil .get_data ('sc2reader.data' , 'train_commands.json' ). decode ( 'utf8' )
2932train_commands = json .loads (command_data )
3033
34+
3135class Unit (object ):
3236 """
3337 Represents an in-game unit.
@@ -64,7 +68,7 @@ def __init__(self, unit_id, flags):
6468 #: in order by frame the type was acquired.
6569 self .type_history = OrderedDict ()
6670
67-
71+ #: Is this unit type a hallucinated one? Unsure of this flag..
6872 self .hallucinated = (flags & 2 == 2 )
6973
7074 def set_type (self , unit_type , frame ):
@@ -84,7 +88,7 @@ def is_type(self, unit_type, strict=True):
8488 if self ._type_class :
8589 return unit_type == self ._type_class .str_id
8690 else :
87- return unit_type == None
91+ return unit_type is None
8892 else :
8993 if isinstance (unit_type , int ):
9094 if self ._type_class :
@@ -97,7 +101,7 @@ def is_type(self, unit_type, strict=True):
97101 if self ._type_class :
98102 return unit_type in [utype .str_id for utype in self .type_history .values ()]
99103 else :
100- return unit_type == None
104+ return unit_type is None
101105
102106 @property
103107 def name (self ):
@@ -154,6 +158,24 @@ def __str__(self):
154158 def __cmp__ (self , other ):
155159 return cmp (self .id , other .id )
156160
161+ def __lt__ (self , other ):
162+ return self .id < other .id
163+
164+ def __le__ (self , other ):
165+ return self .id <= other .id
166+
167+ def __eq__ (self , other ):
168+ return self .id == other .id
169+
170+ def __ne__ (self , other ):
171+ return self .id != other .id
172+
173+ def __gt__ (self , other ):
174+ return self .id > other .id
175+
176+ def __ge__ (self , other ):
177+ return self .id >= other .id
178+
157179 def __hash__ (self ):
158180 return hash (self .id )
159181
@@ -193,7 +215,7 @@ class Build(object):
193215 """
194216 def __init__ (self , build_id ):
195217 #: The integer id of the build
196- self .id = build_id
218+ self .id = build_id
197219
198220 #: A dictionary mapping integer ids to available unit types.
199221 self .units = dict ()
@@ -223,10 +245,10 @@ def change_type(self, unit, new_type, frame):
223245 unit_type = self .units [new_type ]
224246 unit .set_type (unit_type , frame )
225247 else :
226- 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 ))
248+ 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 ))
227249
228250 def add_ability (self , ability_id , name , title = None , is_build = False , build_time = None , build_unit = None ):
229- ability = type (name , (Ability ,), dict (
251+ ability = type (str ( name ), (Ability ,), dict (
230252 id = ability_id ,
231253 name = name ,
232254 title = title or name ,
@@ -238,7 +260,7 @@ def add_ability(self, ability_id, name, title=None, is_build=False, build_time=N
238260 self .abilities [ability_id ] = ability
239261
240262 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 ):
241- unit = type (name , (Unit ,), dict (
263+ unit = type (str ( name ), (Unit ,), dict (
242264 id = type_id ,
243265 str_id = str_id ,
244266 name = name ,
@@ -255,41 +277,44 @@ def add_unit_type(self, type_id, str_id, name, title=None, race='Neutral', miner
255277 self .units [type_id ] = unit
256278 self .units [str_id ] = unit
257279
280+
258281def load_build (expansion , version ):
259282 build = Build (version )
260283
261- unit_file = '{0}/{1}_units.csv' .format (expansion ,version )
262- for entry in pkgutil .get_data ('sc2reader.data' , unit_file ).split ('\n ' ):
263- if not entry : continue
284+ unit_file = '{0}/{1}_units.csv' .format (expansion , version )
285+ for entry in pkgutil .get_data ('sc2reader.data' , unit_file ).decode ('utf8' ).split ('\n ' ):
286+ if not entry :
287+ continue
264288 int_id , str_id = entry .strip ().split (',' )
265- unit_type = int (int_id ,10 )
289+ unit_type = int (int_id , 10 )
266290 title = UNIT_LOOKUP [str_id ]
267291
268292 values = dict (type_id = unit_type , str_id = str_id , name = title )
269- for race in ('Protoss' ,'Terran' ,'Zerg' ):
293+ for race in ('Protoss' , 'Terran' , 'Zerg' ):
270294 if title .lower () in unit_lookup [race ]:
271295 values .update (unit_lookup [race ][title .lower ()])
272- values ['race' ]= race
296+ values ['race' ] = race
273297 break
274298
275299 build .add_unit_type (** values )
276300
277- abil_file = '{0}/{1}_abilities.csv' .format (expansion ,version )
301+ abil_file = '{0}/{1}_abilities.csv' .format (expansion , version )
278302 build .add_ability (ability_id = 0 , name = 'RightClick' , title = 'Right Click' )
279- for entry in pkgutil .get_data ('sc2reader.data' , abil_file ).split ('\n ' ):
280- if not entry : continue
303+ for entry in pkgutil .get_data ('sc2reader.data' , abil_file ).decode ('utf8' ).split ('\n ' ):
304+ if not entry :
305+ continue
281306 int_id_base , str_id = entry .strip ().split (',' )
282- int_id_base = int (int_id_base ,10 ) << 5
307+ int_id_base = int (int_id_base , 10 ) << 5
283308
284309 abils = ABIL_LOOKUP [str_id ]
285- real_abils = [(i ,abil ) for i ,abil in enumerate (abils ) if abil .strip ()!= '' ]
310+ real_abils = [(i , abil ) for i , abil in enumerate (abils ) if abil .strip () != '' ]
286311
287- if len (real_abils )== 0 :
312+ if len (real_abils ) == 0 :
288313 real_abils = [(0 , str_id )]
289314
290315 for index , ability_name in real_abils :
291316 unit_name , build_time = train_commands .get (ability_name , ('' , 0 ))
292- if 'Hallucinated' in unit_name : # Not really sure how to handle hallucinations
317+ if 'Hallucinated' in unit_name : # Not really sure how to handle hallucinations
293318 unit_name = unit_name [12 :]
294319
295320 build .add_ability (
@@ -304,13 +329,12 @@ def load_build(expansion, version):
304329
305330# Load the WoL Data
306331wol_builds = dict ()
307- for version in ('16117' ,'17326' ,'18092' ,'19458' ,'22612' ,'24944' ):
332+ for version in ('16117' , '17326' , '18092' , '19458' , '22612' , '24944' ):
308333 wol_builds [version ] = load_build ('WoL' , version )
309334
310335# Load HotS Data
311336hots_builds = dict ()
312- for version in ('base' ,'23925' ,'24247' ,'24764' ):
337+ for version in ('base' , '23925' , '24247' , '24764' ):
313338 hots_builds [version ] = load_build ('HotS' , version )
314339
315- builds = {'WoL' :wol_builds ,'HotS' :hots_builds }
316-
340+ builds = {'WoL' : wol_builds , 'HotS' : hots_builds }
0 commit comments