<commit_before>class Sector
  class SectorReadError < StandardError ; end
  
  attr_reader :fs,
              :game,
              :area,
              :sector_ram_pointer,
              :area_index,
              :sector_index,
              :room_pointers,
              :rooms,
              :is_hardcoded

  def initialize(area, sector_index, sector_ram_pointer, game, next_sector_pointer: nil, hardcoded_room_pointers: nil)
    @area = area
    @sector_ram_pointer = sector_ram_pointer
    @next_sector_pointer = next_sector_pointer
    @area_index = area.area_index
    @sector_index = sector_index
    @fs = game.fs
    @game = game
    
    if hardcoded_room_pointers
      @room_pointers = hardcoded_room_pointers
      @is_hardcoded = true
    else
      read_room_pointers_from_rom()
    end
  end
  
  def rooms
    @rooms ||= read_rooms_from_rom()
  end
  
  def overlay_id
    AREA_INDEX_TO_OVERLAY_INDEX[area.area_index][sector_index]
  end
  
  def load_necessary_overlay
    fs.load_overlay(overlay_id)
  end
  
  def name
    if SECTOR_INDEX_TO_SECTOR_NAME[area_index]
      SECTOR_INDEX_TO_SECTOR_NAME[area_index][sector_index]
    else
      return AREA_INDEX_TO_AREA_NAME[area_index]
    end
  end
  
  def add_new_room
    if is_hardcoded
      raise "Can't add new room; sector is hardcoded."
    end
    if rooms.length >= 0xFF
      raise "Can't add new room; sector already has FF rooms."
    end
    
    load_necessary_overlay()
    
    new_room_index = rooms.length
    
    new_room_pointer = fs.get_free_space(Room.data_size, nil)
    new_room = Room.new(self, new_room_pointer, area.area_index, sector_index, new_room_index, game)
    
    new_room.layer_list_ram_pointer = fs.get_free_space(Room.max_number_of_layers*RoomLayer.layer_list_entry_size, overlay_id)
    
    other_room_in_sector = rooms.first
    if other_room_in_sector
      new_room.gfx_list_pointer = other_room_in_sector.gfx_list_pointer
      new_room.palette_wrapper_pointer = other_room_in_sector.palette_wrapper_pointer
    else
      new_room.gfx_list_pointer = 0
      new_room.palette_wrapper_pointer = 0
    end
    
    new_room.entity_list_ram_pointer = fs.get_free_space(0xC, overlay_id)
    fs.write(new_room.entity_list_ram_pointer, [0x7FFF7FFF, 0, 0].pack("V*"))
    new_room.door_list_ram_pointer = 0
    
    new_room.entities = []
    new_room.original_number_of_entities = 0
    new_room.doors = []
    if GAME != "hod"
      new_room.number_of_doors = 0
    end
    new_room.original_number_of_doors = 0
    
    new_room.room_xpos_on_map = 0
    new_room.room_ypos_on_map = 0
    new_room.palette_page_index = 0
    
    if SYSTEM == :gba
      new_room.lcd_control = 0x1F00
      new_room.state_swap_event_flag = 0xFFFF
      new_room.alternate_room_state_pointer = 0
    end
    if GAME == "hod"
      new_room.entity_gfx_list_pointer = fs.get_free_space(4, nil)
      new_room.palette_shift_func = 0
      new_room.palette_shift_index = 0
      new_room.is_castle_b = (sector_index % 2)
      new_room.has_breakable_wall = 0
      
      initialize_entity_gfx_list(new_room.entity_gfx_list_pointer)
    end
    if GAME == "aos"
      new_room.color_effects = 0
    end
    
    # This both saves the room and then reads it to initialize various other things like layers.
    new_room.write_to_rom()
    
    new_room.layers.each do |layer|
      layer.opacity = 0x1F
      layer.write_layer_list_entry_to_rom()
    end
    
    @rooms << new_room
    @room_pointers << new_room_pointer
    write_room_list_to_rom()
    
    game.generate_list_of_sectors_by_room_pointer()
  end
  
  def write_room_list_to_rom
    if is_hardcoded
      raise "Can't write room list to ROM; sector is hardcoded."
    end
    <commit_msg>When adding new room, initialize scroll mode and z index<commit_after>class Sector
  class SectorReadError < StandardError ; end
  
  attr_reader :fs,
              :game,
              :area,
              :sector_ram_pointer,
              :area_index,
              :sector_index,
              :room_pointers,
              :rooms,
              :is_hardcoded

  def initialize(area, sector_index, sector_ram_pointer, game, next_sector_pointer: nil, hardcoded_room_pointers: nil)
    @area = area
    @sector_ram_pointer = sector_ram_pointer
    @next_sector_pointer = next_sector_pointer
    @area_index = area.area_index
    @sector_index = sector_index
    @fs = game.fs
    @game = game
    
    if hardcoded_room_pointers
      @room_pointers = hardcoded_room_pointers
      @is_hardcoded = true
    else
      read_room_pointers_from_rom()
    end
  end
  
  def rooms
    @rooms ||= read_rooms_from_rom()
  end
  
  def overlay_id
    AREA_INDEX_TO_OVERLAY_INDEX[area.area_index][sector_index]
  end
  
  def load_necessary_overlay
    fs.load_overlay(overlay_id)
  end
  
  def name
    if SECTOR_INDEX_TO_SECTOR_NAME[area_index]
      SECTOR_INDEX_TO_SECTOR_NAME[area_index][sector_index]
    else
      return AREA_INDEX_TO_AREA_NAME[area_index]
    end
  end
  
  def add_new_room
    if is_hardcoded
      raise "Can't add new room; sector is hardcoded."
    end
    if rooms.length >= 0xFF
      raise "Can't add new room; sector already has FF rooms."
    end
    
    load_necessary_overlay()
    
    new_room_index = rooms.length
    
    new_room_pointer = fs.get_free_space(Room.data_size, nil)
    new_room = Room.new(self, new_room_pointer, area.area_index, sector_index, new_room_index, game)
    
    new_room.layer_list_ram_pointer = fs.get_free_space(Room.max_number_of_layers*RoomLayer.layer_list_entry_size, overlay_id)
    
    other_room_in_sector = rooms.first
    if other_room_in_sector
      new_room.gfx_list_pointer = other_room_in_sector.gfx_list_pointer
      new_room.palette_wrapper_pointer = other_room_in_sector.palette_wrapper_pointer
    else
      new_room.gfx_list_pointer = 0
      new_room.palette_wrapper_pointer = 0
    end
    
    new_room.entity_list_ram_pointer = fs.get_free_space(0xC, overlay_id)
    fs.write(new_room.entity_list_ram_pointer, [0x7FFF7FFF, 0, 0].pack("V*"))
    new_room.door_list_ram_pointer = 0
    
    new_room.entities = []
    new_room.original_number_of_entities = 0
    new_room.doors = []
    if GAME != "hod"
      new_room.number_of_doors = 0
    end
    new_room.original_number_of_doors = 0
    
    new_room.room_xpos_on_map = 0
    new_room.room_ypos_on_map = 0
    new_room.palette_page_index = 0
    
    if SYSTEM == :gba
      new_room.lcd_control = 0x1F00
      new_room.state_swap_event_flag = 0xFFFF
      new_room.alternate_room_state_pointer = 0
    end
    if GAME == "hod"
      new_room.entity_gfx_list_pointer = fs.get_free_space(4, nil)
      new_room.palette_shift_func = 0
      new_room.palette_shift_index = 0
      new_room.is_castle_b = (sector_index % 2)
      new_room.has_breakable_wall = 0
      
      initialize_entity_gfx_list(new_room.entity_gfx_list_pointer)
    end
    if GAME == "aos"
      new_room.color_effects = 0
    end
    
    # This both saves the room and then reads it to initialize various other things like layers.
    new_room.write_to_rom()
    
    new_room.layers.each do |layer|
      layer.opacity = 0x1F
      layer.scroll_mode = 1
      layer.z_index = 0x16
      layer.write_layer_list_entry_to_rom()
    end
    
    @rooms << new_room
    @room_pointers << new_room_pointer
    write_room_list_to_rom()
    
    game.generate_list_of_sectors_by_room_pointer()
  end
  
  def write_room_list_to_rom
    if is_hardcoded
      raise "Can't write room list to ROM; sector is hardcoded."
    end
    
{'fraction_non_alphanumeric': 0.06525177959398892, 'fraction_numerical': 0.008436593725283418, 'mean_word_length': 2.86697247706422, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1627686', 'n_tokens_mistral': 2774, 'n_tokens_neox': 2664, 'n_words': 483}