Advertisement
Vendily

AI In-Fighting

Aug 25th, 2023 (edited)
1,992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.83 KB | None | 0 0
  1. #===============================================================================
  2. # AI In-Fighting - By Vendily [v21]
  3. #===============================================================================
  4. # This script adds in in-fighting between AI controlled battlers, such as
  5. #  Seviper/Zangoose fighting each other in horde encounters, or Mareanie
  6. #  targeting Corsola when summoned by SOS.
  7. # It also allows the feature to be given to Trainers, though unlike wild battles,
  8. #  Trainer controlled battlers will not actively seek out to attack allies, they
  9. #  are just allowed to target them, and are not negatively scored if they do so.
  10. #===============================================================================
  11. # An additional modification is necessary to made to the base scripts.
  12. #  in `def pbGetMoveScores` in script section `AI_ChooseMove`
  13. # `next if target_data.targets_foe && !@user.battler.opposes?(b)` becomes
  14. # `next if target_data.targets_foe && !@user.battler.opposes?(b) && !@trainer.should_in_fight?(@user, b)`
  15. #===============================================================================
  16. # Trainer Types or Wild Pokémon Species with the "Infighting" flag will be
  17. #  allowed to target allies.
  18. # Wild Pokémon Species with the "InFighting_SPECIES" flag will specifically
  19. #  target SPECIES allies at an increased rate. The "InFighting_SPECIES" flag
  20. #  automatically applies the "Infighting" Flag.
  21. #  Only the Species with the "InFighting_SPECIES" flag will target allies.
  22. #===============================================================================
  23. PluginManager.register({
  24.   :name    => "AI In-Fighting",
  25.   :version => "1.0",
  26.   :essentials => "21.1",
  27.   :credits => "Vendily"
  28. })
  29.  
  30.  
  31. class Battle::AI::AITrainer
  32.   attr_reader :skill_flags
  33.  
  34.   alias _ai_infight_set_up_skill_flags set_up_skill_flags
  35.   def set_up_skill_flags
  36.     _ai_infight_set_up_skill_flags
  37.     return if @trainer
  38.     added_infighting = false
  39.     @ai.battle.eachSameSideBattler(@side) do |b|
  40.       sp_data = b.pokemon.species_data
  41.       if sp_data.has_flag?("InFighting")
  42.         @skill_flags.push("InFighting")
  43.         added_infighting = true
  44.       end
  45.       sp_data.flags.each do |flag|
  46.         next unless flag[/^InFighting_(\w+)$/i]
  47.         target_species = $~[1].upcase.to_sym
  48.         next unless GameData::Species.exists?(target_species)
  49.         @skill_flags.push(sprintf("InFighting_%s_%s",target_species,sp_data.species))
  50.         unless added_infighting
  51.           @skill_flags.push("InFighting")
  52.           added_infighting = true
  53.         end
  54.       end
  55.     end
  56.   end
  57.  
  58.   def should_in_fight?(user,target)
  59.     return false if !has_skill_flag?("InFighting")
  60.     return true if @trainer
  61.     user_species = user.battler.species
  62.     target_species = target.species
  63.     return has_skill_flag?(sprintf("InFighting_%s_%s",target_species,user_species))
  64.   end
  65. end
  66.  
  67. class Battle::AI
  68.   alias _ai_infight_add_move_to_choices add_move_to_choices
  69.   def add_move_to_choices(choices, idxMove, score, idxTarget = -1)
  70.     if idxTarget>=0 && @trainer.should_in_fight?(@user,@battle.battlers[idxTarget]) &&
  71.         !@user.battler.opposes?(idxTarget)
  72.       # reverse the reversal of Ally targeting
  73.       old_score = score
  74.       score = ((1.85 * MOVE_BASE_SCORE) - score).to_i
  75.       PBDebug.log_score_change(score - old_score, "#{@user.name} is in-fighting, intentionally targeting ally.")
  76.     end
  77.     _ai_infight_add_move_to_choices(choices, idxMove, score, idxTarget)
  78.     return unless @user.wild?
  79.     # Increase chance of infighting for wild mons that specifically target a species
  80.     if idxTarget>=0 && @trainer.should_in_fight?(@user,@battle.battlers[idxTarget]) &&
  81.         !@user.battler.opposes?(idxTarget)
  82.       PBDebug.log_ai("#{@user.name} is in-fighting, extra attempt against ally.")
  83.       choices.push([idxMove, score, idxTarget])
  84.     end
  85.   end
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement