def create_tilelist(self, raw_sc2_data): """ Stores information about a tile. Args: raw_sc2_data (bytes): Uncompressed .sc2 file. """ for row in range(self.city_size): for col in range(self.city_size): tile = Tile(self.traffic, self.pollution, self.value, self.crime, self.police, self.fire, self.density, self.growth, self.labels) tile_idx = row * self.city_size + col tile_coords = (row, col) tile.coordinates = tile_coords if self.debug: print(f"index: {tile_idx} at {tile_coords}") # First start with parsing the terrain related features. altm = raw_sc2_data["ALTM"][tile_idx * 2:tile_idx * 2 + 2] xter = raw_sc2_data["XTER"][tile_idx:tile_idx + 1] altm_bits = int_to_bitstring(parse_uint16(altm), 16) tile.is_water = bool(int(altm_bits[7:8], 2)) tile.altitude_unknown = int(altm_bits[8:10], 2) tile.altitude = int(altm_bits[11:], 2) tile.terrain = parse_uint8(xter) if self.debug: print(f"altm: {altm_bits}, xter: {tile.terrain}") tile.altidue_tunnel = int(altm_bits[0:7], 2) # Next parse city stuff. # skip self.building for now, it's handled specially. xzon = raw_sc2_data["XZON"][tile_idx:tile_idx + 1] xzon_bits = int_to_bitstring(parse_uint8(xzon), 8) tile.zone_corners = xzon_bits[0:4] tile.zone = int(xzon_bits[4:], 2) xund = raw_sc2_data["XUND"][tile_idx:tile_idx + 1] tile.underground = parse_uint8(xund) if self.debug: print( f"zone: {tile.zone}, corners: {tile.zone_corners}, underground: {tile.underground}" ) # text/signs xtxt = raw_sc2_data["XTXT"][tile_idx:tile_idx + 1] tile.text_pointer = parse_uint8(xtxt) # bit flags xbit = raw_sc2_data["XBIT"][tile_idx:tile_idx + 1] tile.bit_flags = BitFlags(parse_uint8(xbit)) if self.debug: print( f"text pointer: {tile.text_pointer}, bit flags: {tile.bit_flags}" ) # Add the new tile to the tilelist self.tilelist[(row, col)] = tile
def parse_labels(self, xlab_segment): """ Parses the label data. Todo: Make handling of "special" labels easier. Args: xlab_segment (bytes): XLAB sgement of the raw .sc2 file. """ for x in range(0, len(xlab_segment), 25): label_id = x // 25 raw_label = xlab_segment[x : x + 25] label_len = parse_uint8(raw_label[0 : 1]) label = raw_label[1 : label_len + 1].decode('ascii', 'replace') self.labels[label_id] = label if self.debug: print(f"Label: {label_id}: '{label}'")
def create_minimaps(self, raw_sc2_data): """ Creates the 8 minimaps. Args: raw_sc2_data (bytes): Uncompressed .sc2 file. """ # Minimaps that map 4 tiles to 1. map_size = self.city_size // 2 for x in range(map_size): for y in range(map_size): tile_idx = x * map_size + y tile_key = (x, y) xtrf = raw_sc2_data["XTRF"][tile_idx:tile_idx + 1] xplt = raw_sc2_data["XPLT"][tile_idx:tile_idx + 1] xval = raw_sc2_data["XVAL"][tile_idx:tile_idx + 1] xcrm = raw_sc2_data["XCRM"][tile_idx:tile_idx + 1] self.traffic[tile_key] = parse_uint8(xtrf) self.pollution[tile_key] = parse_uint8(xplt) self.value[tile_key] = parse_uint8(xval) self.crime[tile_key] = parse_uint8(xcrm) if self.debug: print( f"{tile_key}: traffic: {parse_uint8(xtrf)}, pollution: {parse_uint8(xplt)}, land value: {parse_uint8(xval)}, crime: {parse_uint8(xcrm)}\n" ) # Minimaps that map 16 tiles to 1. map_size_small = self.city_size // 4 for x in range(map_size_small): for y in range(map_size_small): tile_idx = x * map_size_small + y tile_key = (x, y) xplc = raw_sc2_data["XPLC"][tile_idx:tile_idx + 1] xfir = raw_sc2_data["XFIR"][tile_idx:tile_idx + 1] xpop = raw_sc2_data["XPOP"][tile_idx:tile_idx + 1] xrog = raw_sc2_data["XROG"][tile_idx:tile_idx + 1] self.police[tile_key] = parse_uint8(xplc) self.fire[tile_key] = parse_uint8(xfir) self.density[tile_key] = parse_uint8(xpop) self.growth[tile_key] = parse_uint8(xrog) if self.debug: print( f"{tile_key}: police: {parse_uint8(xplc)}, fire: {parse_uint8(xfir)}, densitye: {parse_uint8(xpop)}, growth: {parse_uint8(xrog)}\n" )