<commit_before>
module ItemSkillStatRandomizer
  def get_n_damage_types(all_damage_types, possible_n_values)
    known_damage_type_names = all_damage_types.select{|name| name !~ /\d$/}
    normal_damage_types = all_damage_types[0,8] & known_damage_type_names
    known_damage_type_names += normal_damage_types # Double the chance of normal damage types compared to status effects and other bits.
    num_damage_types = possible_n_values.sample(random: rng)
    damage_types_to_set = known_damage_type_names.sample(num_damage_types)
    damage_types_to_set
  end
  
  def randomize_item_stats
    (ITEM_GLOBAL_ID_RANGE.to_a - NONRANDOMIZABLE_PICKUP_GLOBAL_IDS).each do |item_global_id|
      item = game.items[item_global_id]
      
      progress_item = checker.all_progression_pickups.include?(item_global_id)
      
      # Don't randomize unequip/starting items.
      if item.name == "---" || item.name == "Bare knuckles" || item.name == "Casual Clothes" || item.name == "Encyclopedia"
        next
      end
      case GAME
      when "dos"
        next if item.name == "Knife"
      when "por"
        next if item["Item ID"] == 0x61 # starting Vampire Killer
      end
      
      if progress_item
        # Always make progression items be worth 0 gold so they can't be sold on accident.
        item["Price"] = 0
      elsif item.name == "CASTLE MAP 1" && GAME == "por"
        # Don't randomize castle map 1 in PoR so it doesn't cost a lot to buy for the first quest.
      else
        item["Price"] = rand_range_weighted_low(*@item_price_range)/100*100
      end
      
      description = game.text_database.text_list[TEXT_REGIONS["Item Descriptions"].begin + item["Item ID"]]
      
      case item.item_type_name
      when "Consumables"
        case GAME
        when "dos", "por"
          possible_types = (0..0x8).to_a
          possible_types -= [4, 5, 6] # Don't allow unusable items
          possible_types += [0, 0, 0, 0, 0, 0, 1, 1, 2] # Increase chances of some item types
          possible_types -= [7, 8] # Don't allow max ups, only certain items we already chose earlier may be max ups.
          # Don't allow potions/mind ups to subtract HP
          if (0..5).include?(item["Item ID"])
            possible_types.delete(3)
          end
          
          if progress_item
            # Always make progression items unusable so the player can't accidentally eat one and softlock themself.
            possible_types = [4]
          end
          
          if @max_up_items[0] == item["Item ID"]
            # HP Max Up
            possible_types = [7]
          elsif @max_up_items[1] == item["Item ID"]
            # MP Max Up
            possible_types = [8]
          end
          
          item["Type"] = possible_types.sample(random: rng)
          
          case item["Type"]
          when 0, 1, 3 # Restores HP/restores MP/subtracts HP
            item["Var A"] = rand_range_weighted_low(*@restorative_amount_range)
          when 2 # Cures status effect
            item["Var A"] = [1, 1, 1, 2, 2, 2, 4].sample(random: rng)
          end
          
          case item["Type"]
          when 0
            description.decoded_string = "Restores #{item["Var A"]} HP."
          when 1
            description.decoded_string = "Restores #{item["Var A"]} MP."
          when 2
            case item["Var A"]
            when 1
              description.decoded_string = "Cures poison."
            when 2
              description.decoded_string = "Nullifies curse."
            when 4
              description.decoded_string = "Cures petrify."
            end
          when 3
            description.decoded_string = "Subtracts #{item["Var A"]} HP."
          when 7
            description.decoded_string = "HP Max up."
          when 8
            description.decoded_string = "MP Max up."
          end
        when "ooe"
          possible_types = (0..0xA).to_a
          possible_types -= [4, 5, 6] # Don't allow max ups, only certain items we already chose earlier may be max ups.
          possible_types -= [8] # Don't allow unusable items
          unless (0x75..0xAC).include?(item["Item ID"])
            possible_types -= [9] # Don't allow records unless it will actually play a song.
          end
          possible_types += [0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 3, 3] # Increase chances of some item types
          # Don't allow potions/mind ups/heart repairs to subtract HP
          if (0x75..0x7B).include?(item["Item ID"])
            possible_types.delete(7)
          end
          
          if (0x9C..0xA0).include?(item["Item ID"]) && rng.rand >= 0.60
            # Drops. These are the only ones that can be AP increasers, so give them a 60% to be an AP increaser.
            possible_types = [0xB]
          end
          
          if progress_item
            # Always make progression items unusable so the player can't accidentally eat one and softlock themself.
            possible_types = [4]
          end
          
          if @max_up_items[0] == item["Item ID"]
            # HP Max Up
            possible_types = [4]
          elsif @max_up_items[1] == item["Item ID"]
            # MP Max Up
            possible_types = [5]
          elsif @max_up_items[2] == item["Item ID"]
            # HEART Max Up
            possible_types = [6]
          end
          
          item["Type"] = possible_types.sample(random: rng)
          
          case item["Type"]
          when 0, 1, 7 # Restores HP/restores MP/subtracts HP
            item["Var A"] = rand_range_weighted_low(*@restorative_amount_range)
          when 2 # Restores hearts
            item["Var A"] = rand_range_weighted_low(*@heart_restorative_amount_range)
          when 3 # Cures status effect
            item["Var A"] = [1, 1, 1, 2, 2, 2, 4].sample(random: rng)
          when 0xB # Increases AP
            item["Var A"] = rand_range_weighted_low(*@ap_increase_amount_range)
          end
          
          case item["Type"]
          when 0
            description.decoded_string = "Restores #{item["Var A"]} HP."
          when 1
            description.decoded_string = "Restores #{item["Var A"]} MP."
          when 2
            description.decoded_string = "Restores #{item["Var A"]} hearts."
          when 3
            case item["Var A"]
            when 1
              description.decoded_string = "Cures poison."
            when 2
              description.decoded_string = "Nullifies curse."
            when 4
              description.decoded_string = "Cures petrify."
            end
          when 4
            description.decoded_string = "Increases your maximum HP."
          when 5
            description.decoded_string = "Increases your maximum MP."
          when 6
            description.decoded_string = "Increases your heart maximum."
          when 7
            description.decoded_string = "Subtracts #{item["Var A"]} HP."
          when 9
            description.decoded_string = "Adjust the background music\\nto your liking."
          when 0xA
            description.decoded_string = "A one-way pass to return\\nto the village immediately."
          when 0xB
            # Don't change description of drops.
          end
        end
      when "Weapons"
        item["Attack"] = rand_range_weighted_low(*@weapon_attack_range)
        
        extra_stats = ["Defense", "Strength", "Constitution", "Intelligence", "Luck"]
        extra_stats << "Mind" if GAME == "por" || GAME == "ooe"
        total_num_extra_stats = extra_stats.length
        
        num_extra_stats_for_this_item = rand_range_weighted_very_low(0..total_num_extra_stats)
        extra_stats.sample(num_extra_stats_for_this_item, random: rng).each do |stat_name|
          item[stat_name] = rand_range_weighted_low(*@item_extra_stats_range)<commit_msg>Drops: Add amount of AP increased to description<commit_after>
module ItemSkillStatRandomizer
  def get_n_damage_types(all_damage_types, possible_n_values)
    known_damage_type_names = all_damage_types.select{|name| name !~ /\d$/}
    normal_damage_types = all_damage_types[0,8] & known_damage_type_names
    known_damage_type_names += normal_damage_types # Double the chance of normal damage types compared to status effects and other bits.
    num_damage_types = possible_n_values.sample(random: rng)
    damage_types_to_set = known_damage_type_names.sample(num_damage_types)
    damage_types_to_set
  end
  
  def randomize_item_stats
    (ITEM_GLOBAL_ID_RANGE.to_a - NONRANDOMIZABLE_PICKUP_GLOBAL_IDS).each do |item_global_id|
      item = game.items[item_global_id]
      
      progress_item = checker.all_progression_pickups.include?(item_global_id)
      
      # Don't randomize unequip/starting items.
      if item.name == "---" || item.name == "Bare knuckles" || item.name == "Casual Clothes" || item.name == "Encyclopedia"
        next
      end
      case GAME
      when "dos"
        next if item.name == "Knife"
      when "por"
        next if item["Item ID"] == 0x61 # starting Vampire Killer
      end
      
      if progress_item
        # Always make progression items be worth 0 gold so they can't be sold on accident.
        item["Price"] = 0
      elsif item.name == "CASTLE MAP 1" && GAME == "por"
        # Don't randomize castle map 1 in PoR so it doesn't cost a lot to buy for the first quest.
      else
        item["Price"] = rand_range_weighted_low(*@item_price_range)/100*100
      end
      
      description = game.text_database.text_list[TEXT_REGIONS["Item Descriptions"].begin + item["Item ID"]]
      
      case item.item_type_name
      when "Consumables"
        case GAME
        when "dos", "por"
          possible_types = (0..0x8).to_a
          possible_types -= [4, 5, 6] # Don't allow unusable items
          possible_types += [0, 0, 0, 0, 0, 0, 1, 1, 2] # Increase chances of some item types
          possible_types -= [7, 8] # Don't allow max ups, only certain items we already chose earlier may be max ups.
          # Don't allow potions/mind ups to subtract HP
          if (0..5).include?(item["Item ID"])
            possible_types.delete(3)
          end
          
          if progress_item
            # Always make progression items unusable so the player can't accidentally eat one and softlock themself.
            possible_types = [4]
          end
          
          if @max_up_items[0] == item["Item ID"]
            # HP Max Up
            possible_types = [7]
          elsif @max_up_items[1] == item["Item ID"]
            # MP Max Up
            possible_types = [8]
          end
          
          item["Type"] = possible_types.sample(random: rng)
          
          case item["Type"]
          when 0, 1, 3 # Restores HP/restores MP/subtracts HP
            item["Var A"] = rand_range_weighted_low(*@restorative_amount_range)
          when 2 # Cures status effect
            item["Var A"] = [1, 1, 1, 2, 2, 2, 4].sample(random: rng)
          end
          
          case item["Type"]
          when 0
            description.decoded_string = "Restores #{item["Var A"]} HP."
          when 1
            description.decoded_string = "Restores #{item["Var A"]} MP."
          when 2
            case item["Var A"]
            when 1
              description.decoded_string = "Cures poison."
            when 2
              description.decoded_string = "Nullifies curse."
            when 4
              description.decoded_string = "Cures petrify."
            end
          when 3
            description.decoded_string = "Subtracts #{item["Var A"]} HP."
          when 7
            description.decoded_string = "HP Max up."
          when 8
            description.decoded_string = "MP Max up."
          end
        when "ooe"
          possible_types = (0..0xA).to_a
          possible_types -= [4, 5, 6] # Don't allow max ups, only certain items we already chose earlier may be max ups.
          possible_types -= [8] # Don't allow unusable items
          unless (0x75..0xAC).include?(item["Item ID"])
            possible_types -= [9] # Don't allow records unless it will actually play a song.
          end
          possible_types += [0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 3, 3] # Increase chances of some item types
          # Don't allow potions/mind ups/heart repairs to subtract HP
          if (0x75..0x7B).include?(item["Item ID"])
            possible_types.delete(7)
          end
          
          if (0x9C..0xA0).include?(item["Item ID"]) && rng.rand >= 0.60
            # Drops. These are the only ones that can be AP increasers, so give them a 60% to be an AP increaser.
            possible_types = [0xB]
          end
          
          if progress_item
            # Always make progression items unusable so the player can't accidentally eat one and softlock themself.
            possible_types = [4]
          end
          
          if @max_up_items[0] == item["Item ID"]
            # HP Max Up
            possible_types = [4]
          elsif @max_up_items[1] == item["Item ID"]
            # MP Max Up
            possible_types = [5]
          elsif @max_up_items[2] == item["Item ID"]
            # HEART Max Up
            possible_types = [6]
          end
          
          item["Type"] = possible_types.sample(random: rng)
          
          case item["Type"]
          when 0, 1, 7 # Restores HP/restores MP/subtracts HP
            item["Var A"] = rand_range_weighted_low(*@restorative_amount_range)
          when 2 # Restores hearts
            item["Var A"] = rand_range_weighted_low(*@heart_restorative_amount_range)
          when 3 # Cures status effect
            item["Var A"] = [1, 1, 1, 2, 2, 2, 4].sample(random: rng)
          when 0xB # Increases AP
            item["Var A"] = rand_range_weighted_low(*@ap_increase_amount_range)
          end
          
          case item["Type"]
          when 0
            description.decoded_string = "Restores #{item["Var A"]} HP."
          when 1
            description.decoded_string = "Restores #{item["Var A"]} MP."
          when 2
            description.decoded_string = "Restores #{item["Var A"]} hearts."
          when 3
            case item["Var A"]
            when 1
              description.decoded_string = "Cures poison."
            when 2
              description.decoded_string = "Nullifies curse."
            when 4
              description.decoded_string = "Cures petrify."
            end
          when 4
            description.decoded_string = "Increases your maximum HP."
          when 5
            description.decoded_string = "Increases your maximum MP."
          when 6
            description.decoded_string = "Increases your heart maximum."
          when 7
            description.decoded_string = "Subtracts #{item["Var A"]} HP."
          when 9
            description.decoded_string = "Adjust the background music\\nto your liking."
          when 0xA
            description.decoded_string = "A one-way pass to return\\nto the village immediately."
          when 0xB
            ap_types = %w(flame ice lightning light dark)
            ap_type_index = item["Item ID"]-0x9C
            ap_type = ap_types[ap_type_index]
            description.decoded_string = "Increases your #{ap_type}\\nattribute points by #{item["Var A"]}."
          end
        end
      when "Weapons"
        item["Attack"] = rand_range_weighted_low(*@weapon_attack_range)
        
        extra_stats = ["Defense", "Strength", "Constitution", "Intelligence", "Luck"]
        extra_stats << "Mind" if GAME == "por" || GAME == "ooe"
        total_num_extra_stats = extra_stats.length
        
        num_extra_stats_for_this_item = rand_range_weighted_very_low(0..total_num_extra_stats)
        extra_stats.sample(num_extra_stats_for_this_item, random: rng).each do |stat_name|
          item[stat_name] = rand_range_weighted_low(*@item_extra_stats_range)
{'fraction_non_alphanumeric': 0.09511826874685456, 'fraction_numerical': 0.017111222949169603, 'mean_word_length': 1.9477099944372334, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1999171', 'n_tokens_mistral': 4911, 'n_tokens_neox': 4640, 'n_words': 1336}