1
- from __future__ import absolute_import
1
+ # -*- coding: utf-8 -*-
2
+ from __future__ import absolute_import , print_function , unicode_literals , division
2
3
3
4
import json
4
5
import pkgutil
11
12
from sc2reader .log_utils import loggable
12
13
13
14
ABIL_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 )
17
19
ABIL_LOOKUP [str_id ] = abilities .split (',' )
18
20
19
21
UNIT_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
22
25
str_id , title = entry .strip ().split (',' )
23
26
UNIT_LOOKUP [str_id ] = title
24
27
25
- unit_data = pkgutil .get_data ('sc2reader.data' , 'unit_info.json' )
28
+ unit_data = pkgutil .get_data ('sc2reader.data' , 'unit_info.json' ). decode ( 'utf8' )
26
29
unit_lookup = json .loads (unit_data )
27
30
28
- command_data = pkgutil .get_data ('sc2reader.data' , 'train_commands.json' )
31
+ command_data = pkgutil .get_data ('sc2reader.data' , 'train_commands.json' ). decode ( 'utf8' )
29
32
train_commands = json .loads (command_data )
30
33
34
+
31
35
class Unit (object ):
32
36
"""
33
37
Represents an in-game unit.
@@ -64,7 +68,7 @@ def __init__(self, unit_id, flags):
64
68
#: in order by frame the type was acquired.
65
69
self .type_history = OrderedDict ()
66
70
67
-
71
+ #: Is this unit type a hallucinated one? Unsure of this flag..
68
72
self .hallucinated = (flags & 2 == 2 )
69
73
70
74
def set_type (self , unit_type , frame ):
@@ -84,7 +88,7 @@ def is_type(self, unit_type, strict=True):
84
88
if self ._type_class :
85
89
return unit_type == self ._type_class .str_id
86
90
else :
87
- return unit_type == None
91
+ return unit_type is None
88
92
else :
89
93
if isinstance (unit_type , int ):
90
94
if self ._type_class :
@@ -97,7 +101,7 @@ def is_type(self, unit_type, strict=True):
97
101
if self ._type_class :
98
102
return unit_type in [utype .str_id for utype in self .type_history .values ()]
99
103
else :
100
- return unit_type == None
104
+ return unit_type is None
101
105
102
106
@property
103
107
def name (self ):
@@ -154,6 +158,24 @@ def __str__(self):
154
158
def __cmp__ (self , other ):
155
159
return cmp (self .id , other .id )
156
160
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
+
157
179
def __hash__ (self ):
158
180
return hash (self .id )
159
181
@@ -193,7 +215,7 @@ class Build(object):
193
215
"""
194
216
def __init__ (self , build_id ):
195
217
#: The integer id of the build
196
- self .id = build_id
218
+ self .id = build_id
197
219
198
220
#: A dictionary mapping integer ids to available unit types.
199
221
self .units = dict ()
@@ -223,10 +245,10 @@ def change_type(self, unit, new_type, frame):
223
245
unit_type = self .units [new_type ]
224
246
unit .set_type (unit_type , frame )
225
247
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 ))
227
249
228
250
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 (
230
252
id = ability_id ,
231
253
name = name ,
232
254
title = title or name ,
@@ -238,7 +260,7 @@ def add_ability(self, ability_id, name, title=None, is_build=False, build_time=N
238
260
self .abilities [ability_id ] = ability
239
261
240
262
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 (
242
264
id = type_id ,
243
265
str_id = str_id ,
244
266
name = name ,
@@ -255,41 +277,44 @@ def add_unit_type(self, type_id, str_id, name, title=None, race='Neutral', miner
255
277
self .units [type_id ] = unit
256
278
self .units [str_id ] = unit
257
279
280
+
258
281
def load_build (expansion , version ):
259
282
build = Build (version )
260
283
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
264
288
int_id , str_id = entry .strip ().split (',' )
265
- unit_type = int (int_id ,10 )
289
+ unit_type = int (int_id , 10 )
266
290
title = UNIT_LOOKUP [str_id ]
267
291
268
292
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' ):
270
294
if title .lower () in unit_lookup [race ]:
271
295
values .update (unit_lookup [race ][title .lower ()])
272
- values ['race' ]= race
296
+ values ['race' ] = race
273
297
break
274
298
275
299
build .add_unit_type (** values )
276
300
277
- abil_file = '{0}/{1}_abilities.csv' .format (expansion ,version )
301
+ abil_file = '{0}/{1}_abilities.csv' .format (expansion , version )
278
302
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
281
306
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
283
308
284
309
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 () != '' ]
286
311
287
- if len (real_abils )== 0 :
312
+ if len (real_abils ) == 0 :
288
313
real_abils = [(0 , str_id )]
289
314
290
315
for index , ability_name in real_abils :
291
316
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
293
318
unit_name = unit_name [12 :]
294
319
295
320
build .add_ability (
@@ -304,13 +329,12 @@ def load_build(expansion, version):
304
329
305
330
# Load the WoL Data
306
331
wol_builds = dict ()
307
- for version in ('16117' ,'17326' ,'18092' ,'19458' ,'22612' ,'24944' ):
332
+ for version in ('16117' , '17326' , '18092' , '19458' , '22612' , '24944' ):
308
333
wol_builds [version ] = load_build ('WoL' , version )
309
334
310
335
# Load HotS Data
311
336
hots_builds = dict ()
312
- for version in ('base' ,'23925' ,'24247' ,'24764' ):
337
+ for version in ('base' , '23925' , '24247' , '24764' ):
313
338
hots_builds [version ] = load_build ('HotS' , version )
314
339
315
- builds = {'WoL' :wol_builds ,'HotS' :hots_builds }
316
-
340
+ builds = {'WoL' : wol_builds , 'HotS' : hots_builds }
0 commit comments