<commit_before>
module MapRandomizer
  DOS_WARP_ROOM_STRS = [
    "00-00-18",
    "00-01-3E",
    "00-02-24",
    "00-03-23",
    "00-04-17",
    "00-05-1E",
    "00-05-1F",
    "00-06-20",
    "00-07-10",
    "00-08-21",
    "00-09-1F",
    "00-0B-23",
  ]
  CONDEMNED_TOWER_ROOM_INDEXES = (0..0xC).to_a + [0x19, 0x1A, 0x1C, 0x1E]
  
  def randomize_doors_no_overlap(&block)
    add_extra_helper_rooms()
    remove_all_wooden_doors()
    
    @rooms_unused_by_map_rando = []
    
    maps_rendered = 0
    
    case GAME
    when "dos"
      castle_rooms = []
      abyss_rooms = []
      game.each_room do |room|
        case room.sector_index
        when 0..9
          castle_rooms << room
        when 0xA..0xB
          abyss_rooms << room
        end
      end
      
      starting_room = game.areas[0].sectors[0].rooms[1] # TODO dummy starting room for DoS, need to select a proper one somehow for starting room rando to work
      randomize_doors_no_overlap_for_area(castle_rooms, 64, 45, starting_room)
      randomize_doors_no_overlap_for_area(abyss_rooms, 18, 25, game.room_by_str("00-0B-00"))
    when "por"
      game.areas.each do |area|
        next if area.area_index >= 0xA
        
        rooms = []
        area.sectors.each do |sector|
          rooms += sector.rooms
        end
        
        starting_room = case area.name
        when "Dracula's Castle"
          area.sectors[0].rooms[0]
        when "City of Haze"
          area.sectors[0].rooms[0x1A]
        when "13th Street"
          area.sectors[0].rooms[7]
        when "Sandy Grave"
          area.sectors[0].rooms[0]
        when "Forgotten City"
          area.sectors[0].rooms[0]
        when "Nation of Fools"
          area.sectors[0].rooms[0x21]
        when "Burnt Paradise"
          area.sectors[0].rooms[0x20]
        when "Forest of Doom"
          area.sectors[0].rooms[0]
        when "Dark Academy"
          area.sectors[1].rooms[6]
        when "Nest of Evil"
          area.sectors[0].rooms[0]
        else
          raise "Invalid area"
        end
        randomize_doors_no_overlap_for_area(rooms, 64, 45, starting_room)
      end
    when "ooe"
      game.areas.each do |area|
        next if area.area_index == 1 # Wygol
        next if area.area_index == 2 # Ecclesia
        next if area.area_index == 0x13 # Epilogue/Boss rush/Practice mode
        
        rooms = []
        area.sectors.each do |sector|
          rooms += sector.rooms
        end
        
        starting_room = case area.name
        when "Dracula's Castle"
          area.sectors[0xC].rooms[0]
        when "Training Hall"
          area.sectors[0].rooms[0]
        when "Ruvas Forest"
          area.sectors[0].rooms[0]
        when "Argila Swamp"
          area.sectors[0].rooms[3]
        when "Kalidus Channel"
          area.sectors[0].rooms[0] # TODO need to do something about kalidus's alternate entrance
        when "Somnus Reef"
          area.sectors[0].rooms[0]
        when "Minera Prison Island"
          area.sectors[0].rooms[0]
        when "Lighthouse"
          area.sectors[0].rooms[0]
        when "Tymeo Mountains"
          area.sectors[0].rooms[0]
        when "Tristis Pass"
          area.sectors[0].rooms[0]
        when "Large Cavern"
          area.sectors[0].rooms[0]
        when "Giant's Dwelling"
          area.sectors[0].rooms[0]
        when "Mystery Manor"
          area.sectors[0].rooms[0xC]
        when "Misty Forest Road"
          area.sectors[0].rooms[8]
        when "Oblivion Ridge"
          area.sectors[1].rooms[6]
        when "Skeleton Cave"
          area.sectors[0].rooms[0]
        when "Monastery"
          area.sectors[1].rooms[0]
        else
          raise "Invalid area"
        end
        randomize_doors_no_overlap_for_area(rooms, 64, 45, starting_room)
      end
    end
    
    replace_outer_boss_doors()
    
    if GAME == "por"
      # Add the white barrier to the transition room before the Throne Room.
      transition_for_throne_room = game.room_by_str("00-0A-17")
      barrier = transition_for_throne_room.add_new_entity()
      barrier.type = 2
      barrier.subtype = 0x81
      barrier.write_to_rom()
      
      # Remove the white barrier from the room that originally had it (and the event and font loader too).
      orig_white_barrier_room = game.room_by_str("00-0A-01")
      [1, 3, 4].each do |entity_index|
        entity = orig_white_barrier_room.entities[entity_index]
        entity.type = 0
        entity.write_to_rom()
      end
    end
  end
  
  def add_extra_helper_rooms
    case GAME
    when "por"
      sector = game.areas[0].sectors[9]
      sector.add_new_room()
      room = sector.rooms[-1]
      filename = "./dsvrandom/roomedits/por_map_rando_00-09-04.tmx"
      tiled.read(filename, room)
    end
  end
  
  def randomize_doors_no_overlap_for_area(area_rooms, map_width, map_height, area_starting_room)
    area_index = area_rooms.first.area_index
    map_spots = Array.new(map_width) { Array.new(map_height) }
    unplaced_transition_rooms = @transition_rooms.select{|room| room.area_index == area_index}
    placed_transition_rooms = []
    unreachable_subroom_doors = []
    
    puts "ON AREA: %02X" % area_index
    
    #sectors_done = 0
    #total_sectors = 10
    <commit_msg>Fix DoS castle transition rooms being deleted from map by abyss map rando<commit_after>
module MapRandomizer
  DOS_WARP_ROOM_STRS = [
    "00-00-18",
    "00-01-3E",
    "00-02-24",
    "00-03-23",
    "00-04-17",
    "00-05-1E",
    "00-05-1F",
    "00-06-20",
    "00-07-10",
    "00-08-21",
    "00-09-1F",
    "00-0B-23",
  ]
  CONDEMNED_TOWER_ROOM_INDEXES = (0..0xC).to_a + [0x19, 0x1A, 0x1C, 0x1E]
  
  def randomize_doors_no_overlap(&block)
    add_extra_helper_rooms()
    remove_all_wooden_doors()
    
    @rooms_unused_by_map_rando = []
    
    maps_rendered = 0
    
    case GAME
    when "dos"
      castle_rooms = []
      abyss_rooms = []
      game.each_room do |room|
        case room.sector_index
        when 0..9
          castle_rooms << room
        when 0xA..0xB
          abyss_rooms << room
        end
      end
      
      starting_room = game.areas[0].sectors[0].rooms[1] # TODO dummy starting room for DoS, need to select a proper one somehow for starting room rando to work
      randomize_doors_no_overlap_for_area(castle_rooms, 64, 45, starting_room)
      randomize_doors_no_overlap_for_area(abyss_rooms, 18, 25, game.room_by_str("00-0B-00"))
    when "por"
      game.areas.each do |area|
        next if area.area_index >= 0xA
        
        rooms = []
        area.sectors.each do |sector|
          rooms += sector.rooms
        end
        
        starting_room = case area.name
        when "Dracula's Castle"
          area.sectors[0].rooms[0]
        when "City of Haze"
          area.sectors[0].rooms[0x1A]
        when "13th Street"
          area.sectors[0].rooms[7]
        when "Sandy Grave"
          area.sectors[0].rooms[0]
        when "Forgotten City"
          area.sectors[0].rooms[0]
        when "Nation of Fools"
          area.sectors[0].rooms[0x21]
        when "Burnt Paradise"
          area.sectors[0].rooms[0x20]
        when "Forest of Doom"
          area.sectors[0].rooms[0]
        when "Dark Academy"
          area.sectors[1].rooms[6]
        when "Nest of Evil"
          area.sectors[0].rooms[0]
        else
          raise "Invalid area"
        end
        randomize_doors_no_overlap_for_area(rooms, 64, 45, starting_room)
      end
    when "ooe"
      game.areas.each do |area|
        next if area.area_index == 1 # Wygol
        next if area.area_index == 2 # Ecclesia
        next if area.area_index == 0x13 # Epilogue/Boss rush/Practice mode
        
        rooms = []
        area.sectors.each do |sector|
          rooms += sector.rooms
        end
        
        starting_room = case area.name
        when "Dracula's Castle"
          area.sectors[0xC].rooms[0]
        when "Training Hall"
          area.sectors[0].rooms[0]
        when "Ruvas Forest"
          area.sectors[0].rooms[0]
        when "Argila Swamp"
          area.sectors[0].rooms[3]
        when "Kalidus Channel"
          area.sectors[0].rooms[0] # TODO need to do something about kalidus's alternate entrance
        when "Somnus Reef"
          area.sectors[0].rooms[0]
        when "Minera Prison Island"
          area.sectors[0].rooms[0]
        when "Lighthouse"
          area.sectors[0].rooms[0]
        when "Tymeo Mountains"
          area.sectors[0].rooms[0]
        when "Tristis Pass"
          area.sectors[0].rooms[0]
        when "Large Cavern"
          area.sectors[0].rooms[0]
        when "Giant's Dwelling"
          area.sectors[0].rooms[0]
        when "Mystery Manor"
          area.sectors[0].rooms[0xC]
        when "Misty Forest Road"
          area.sectors[0].rooms[8]
        when "Oblivion Ridge"
          area.sectors[1].rooms[6]
        when "Skeleton Cave"
          area.sectors[0].rooms[0]
        when "Monastery"
          area.sectors[1].rooms[0]
        else
          raise "Invalid area"
        end
        randomize_doors_no_overlap_for_area(rooms, 64, 45, starting_room)
      end
    end
    
    replace_outer_boss_doors()
    
    if GAME == "por"
      # Add the white barrier to the transition room before the Throne Room.
      transition_for_throne_room = game.room_by_str("00-0A-17")
      barrier = transition_for_throne_room.add_new_entity()
      barrier.type = 2
      barrier.subtype = 0x81
      barrier.write_to_rom()
      
      # Remove the white barrier from the room that originally had it (and the event and font loader too).
      orig_white_barrier_room = game.room_by_str("00-0A-01")
      [1, 3, 4].each do |entity_index|
        entity = orig_white_barrier_room.entities[entity_index]
        entity.type = 0
        entity.write_to_rom()
      end
    end
  end
  
  def add_extra_helper_rooms
    case GAME
    when "por"
      sector = game.areas[0].sectors[9]
      sector.add_new_room()
      room = sector.rooms[-1]
      filename = "./dsvrandom/roomedits/por_map_rando_00-09-04.tmx"
      tiled.read(filename, room)
    end
  end
  
  def randomize_doors_no_overlap_for_area(area_rooms, map_width, map_height, area_starting_room)
    area_index = area_rooms.first.area_index
    map_spots = Array.new(map_width) { Array.new(map_height) }
    unplaced_transition_rooms = @transition_rooms & area_rooms
    placed_transition_rooms = []
    unreachable_subroom_doors = []
    
    puts "ON AREA: %02X" % area_index
    
    #sectors_done = 0
    #total_sectors = 10
    
{'fraction_non_alphanumeric': 0.10413519637462236, 'fraction_numerical': 0.038897280966767374, 'mean_word_length': 2.0492227979274613, 'pattern_counts': {'":': 0, '<': 11, '<?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': '97995', 'n_tokens_mistral': 3899, 'n_tokens_neox': 3678, 'n_words': 755}