def read_from_rom(self, rom): self.enemy_config_table.from_block(block=rom, offset=from_snes_address(ENEMY_CONFIGURATION_TABLE_DEFAULT_OFFSET)) self.enemy_group_bg_table.from_block(block=rom, offset=from_snes_address(ENEMY_GROUP_BACKGROUND_TABLE_DEFAULT_OFFSET)) # Read the sprites log.debug("Reading battle sprites") self.graphics_pointer_table.from_block( rom, from_snes_address(read_asm_pointer(block=rom, offset=GRAPHICS_POINTER_TABLE_ASM_POINTER_OFFSET))) self.battle_sprites = [] for i in range(self.graphics_pointer_table.num_rows): with EbCompressibleBlock() as compressed_block: compressed_block.from_compressed_block( block=rom, offset=from_snes_address(self.graphics_pointer_table[i][0])) sprite = EbBattleSprite() sprite.from_block(block=compressed_block, offset=0, size=self.graphics_pointer_table[i][1]) self.battle_sprites.append(sprite) # Determine how many palettes there are num_palettes = 0 for i in range(self.enemy_config_table.num_rows): num_palettes = max(num_palettes, self.enemy_config_table[i][14]) num_palettes += 1 # Read the palettes log.debug("Reading palettes") palettes_offset = from_snes_address(read_asm_pointer(block=rom, offset=PALETTES_ASM_POINTER_OFFSET)) self.palettes = [] for i in range(num_palettes): palette = EbPalette(num_subpalettes=1, subpalette_length=16) palette.from_block(block=rom, offset=palettes_offset) self.palettes.append(palette) palettes_offset += palette.block_size() # Read the groups log.debug("Reading enemy groups") self.enemy_group_table.from_block(rom, from_snes_address(ENEMY_GROUP_TABLE_DEFAULT_OFFSET)) self.enemy_groups = [] for i in range(self.enemy_group_table.num_rows): group = [] group_offset = from_snes_address(self.enemy_group_table[i][0]) while rom[group_offset] != 0xff: group.append(EnemyGroupTableEntry.from_block(block=rom, offset=group_offset)) group_offset += EnemyGroupTableEntry.size self.enemy_groups.append(group)
def read_from_project(self, resource_open): with resource_open("enemy_configuration_table", "yml") as f: self.enemy_config_table.from_yml_file(f) # Read the sprites and palettes self.battle_sprites = [] self.palettes = [] sprite_hashes = dict() num_sprites = 0 palette_hashes = dict() num_palettes = 0 for i in range(self.enemy_config_table.num_rows): battle_sprite = EbBattleSprite() palette = EbPalette(num_subpalettes=1, subpalette_length=16) try: with resource_open("BattleSprites/" + str(i).zfill(3), "png") as f: image = open_indexed_image(f) battle_sprite.from_image(image) palette.from_image(image) del image except IOError: # No battle sprite self.enemy_config_table[i][4] = 0 self.enemy_config_table[i][14] = 0 continue sprite_hash = battle_sprite.hash() try: self.enemy_config_table[i][4] = sprite_hashes[sprite_hash] + 1 except KeyError: self.enemy_config_table[i][4] = num_sprites + 1 sprite_hashes[sprite_hash] = num_sprites self.battle_sprites.append(battle_sprite) num_sprites += 1 palette_hash = palette.hash() try: self.enemy_config_table[i][14] = palette_hashes[palette_hash] except KeyError: self.enemy_config_table[i][14] = num_palettes palette_hashes[palette_hash] = num_palettes self.palettes.append(palette) num_palettes += 1 # Read the groups with resource_open("enemy_groups", "yml") as f: self.enemy_group_table.from_yml_file(f) with resource_open("enemy_groups", "yml") as f: self.enemy_group_bg_table.from_yml_file(f) with resource_open("enemy_groups", "yml") as f: self.enemy_groups = [] enemy_groups_yml_rep = yml_load(f) for entry in enemy_groups_yml_rep.itervalues(): enemy_group = entry["Enemies"] if type(enemy_group) == dict: enemy_group = [enemy_group[x] for x in sorted(enemy_group.keys())] group = [EnemyGroupTableEntry.from_yml_rep(x) for x in enemy_group] self.enemy_groups.append(group)