#===============================================================================
# 
# Craze's Script Asylum - Element HEC Boost
# Last Date Updated: 2010.09.28
# Level: Normal
# 
# Allow certain states and pieces of equipment to raise the chance that a
# battler inflicts a critical hit with an element, or evades a specific element.
# This is inspired entirely by Persona 4.
#===============================================================================
# Instructions
# -----------------------------------------------------------------------------
# To install this script, open up your script editor and copy/paste this script
# to an open slot below ▼ Materials and Battle Engine Melody but above ▼ Main. 
# Remember to save.
# 
# <ele hit bonus x: +y> <ele hit bonus x: -y>
# The element with id x has a static +/-y% chance to connect successfully.
# 
# <ele cri bonus x: +y> <ele cri bonus x: -y>
# The element with id x has a static +/-y% chance to achieve a critical hit.
# 
# <ele eva bonus x: +y> <ele eva bonus x: -y>
# The element with id x has a static +/-y% chance to be evaded.
#
# Critical hit tags will only work if you have BEM installed, and also only
# for actions that are allowed to get critical hits (check Section IV.A of
# BEM I for the CRITICAL_SKILLS hash).
# 
#===============================================================================
# Examples
# -----------------------------------------------------------------------------
# <ele hit bonus 1: +10> 
# The actor or enemy would have a flat +10% chance to connect with any Melee
# action, on top of its natural hit rate. 
#
# <ele cri bonus 4: +25> 
# The actor or enemy would have a flat +25% chance to get a critical hit with
# Ice techniques, on top of its natural crit rate.
# 
# <ele eva bonus 6: -100> 
# The actor or enemy would have a flat -100% chance to evade Earth attacks,
# most likely making it impossible for it.
# 
# This is for equipment and state noteboxes only.
#===============================================================================
# Compatibility
# -----------------------------------------------------------------------------
# Note: This script requires Yanfly's Battle Engine Melody for <ele cri bonus>
# tags. Hit and Evasion work just fine, but Critical tags will not work without
# BEM present. Make sure to place this script below BEM and any other HEC-stat
# altering scripts.
#===============================================================================
 
$imported = {} if $imported == nil
$imported["EleHECBoost"] = true
 
#==============================================================================
# RPG::BaseItem
#==============================================================================
 
class RPG::BaseItem
 
  #--------------------------------------------------------------------------
  # ele_hits 
  #--------------------------------------------------------------------------

  def ele_hits
    return @ele_hits if @ele_hits != nil
    @ele_hits = {}
    self.note.split(/[\r\n]+/).each { |line|
      case line
      when /<(?:ELE_HIT_BONUS|ele hit bonus)[ ](\d+):[ ]*([\+\-]\d+)>/i
          @ele_hits[$1.to_i] += $2.to_i if $1.to_i != 0 and !@ele_hits[$1.to_i] == nil
          @ele_hits[$1.to_i] = $2.to_i if $1.to_i != 0
      end
    }
    return @ele_hits
  end
 
  #--------------------------------------------------------------------------
  # ele_cris 
  #--------------------------------------------------------------------------

  def ele_cris
    return @ele_cris if @ele_cris != nil
    @ele_cris = {}
    self.note.split(/[\r\n]+/).each { |line|
      case line
      when /<(?:ELE_cri_BONUS|ele cri bonus)[ ](\d+):[ ]*([\+\-]\d+)>/i
          @ele_cris[$1.to_i] += $2.to_i if $1.to_i != 0 and !@ele_cris[$1.to_i] == nil
          @ele_cris[$1.to_i] = $2.to_i if $1.to_i != 0
      end
    }
    return @ele_cris
  end
 
  #--------------------------------------------------------------------------
  # ele_evas 
  #--------------------------------------------------------------------------

  def ele_evas
    return @ele_evas if @ele_evas != nil
    @ele_evas = {}
    self.note.split(/[\r\n]+/).each { |line|
      case line
      when /<(?:ELE_EVA_BONUS|ele eva bonus)[ ](\d+):[ ]*([\+\-]\d+)>/i
          @ele_evas[$1.to_i] += $2.to_i if $1.to_i != 0 and !@ele_evas[$1.to_i] == nil
          @ele_evas[$1.to_i] = $2.to_i if $1.to_i != 0
      end
    }
    return @ele_evas
  end
 
end
 
#==============================================================================
# RPG::State
#==============================================================================
 
class RPG::State
 
  #--------------------------------------------------------------------------
  # ele_hits 
  #--------------------------------------------------------------------------

  def ele_hits
    return @ele_hits if @ele_hits != nil
    @ele_hits = {}
    self.note.split(/[\r\n]+/).each { |line|
      case line
      when /<(?:ELE_HIT_BONUS|ele hit bonus)[ ](\d+):[ ]*([\+\-]\d+)>/i
          @ele_hits[$1.to_i] += $2.to_i if $1.to_i != 0 and !@ele_hits[$1.to_i] == nil
          @ele_hits[$1.to_i] = $2.to_i if $1.to_i != 0
      end
    }
    return @ele_hits
  end
 
  #--------------------------------------------------------------------------
  # ele_cris 
  #--------------------------------------------------------------------------

  def ele_cris
    return @ele_cris if @ele_cris != nil
    @ele_cris = {}
    self.note.split(/[\r\n]+/).each { |line|
      case line
      when /<(?:ELE_cri_BONUS|ele cri bonus)[ ](\d+):[ ]*([\+\-]\d+)>/i
          @ele_cris[$1.to_i] += $2.to_i if $1.to_i != 0 and !@ele_cris[$1.to_i] == nil
          @ele_cris[$1.to_i] = $2.to_i if $1.to_i != 0
      end
    }
    return @ele_cris
  end
 
  #--------------------------------------------------------------------------
  # ele_evas 
  #--------------------------------------------------------------------------

  def ele_evas
    return @ele_evas if @ele_evas != nil
    @ele_evas = {}
    self.note.split(/[\r\n]+/).each { |line|
      case line
      when /<(?:ELE_EVA_BONUS|ele eva bonus)[ ](\d+):[ ]*([\+\-]\d+)>/i
          @ele_evas[$1.to_i] += $2.to_i if $1.to_i != 0 and !@ele_evas[$1.to_i] == nil
          @ele_evas[$1.to_i] = $2.to_i if $1.to_i != 0
      end
    }
    return @ele_evas
  end
 
end
 
#==============================================================================
# Game_Battler
#==============================================================================
 
class Game_Battler
  
  alias csa_elehec_calc_hit calc_hit unless $@
  def calc_hit(user, obj = nil)
    hit = csa_elehec_calc_hit(user, obj)
    if obj != nil
      set = obj.element_set
      if obj.physical_attack and YEM::BATTLE_ENGINE::APPLY_PHYSICAL_ELE and $imported["BattleEngineMelody"]
        set += user.element_set
      elsif YEM::BATTLE_ENGINE::APPLY_MAGICAL_ELE and $imported["BattleEngineMelody"]
        set += user.element_set
      end
    else
      set = user.element_set
    end
    set = $game_temp.action_elements if $game_temp.action_elements != nil
    for i in set
      unless user.enemy?
        for equip in user.equips.compact
          next if equip == nil
          hit += equip.ele_hits[i] unless equip.ele_hits[i] == nil
        end
      end
      for state in user.states
        hit += state.ele_hits[i] unless state.ele_hits[i] == nil
      end
    end
   return hit
  end
  
  alias csa_elehec_calc_eva calc_eva unless $@
  def calc_eva(user, obj = nil)
    eva = csa_elehec_calc_eva(user, obj)
    if obj != nil
      set = obj.element_set
      if obj.physical_attack and YEM::BATTLE_ENGINE::APPLY_PHYSICAL_ELE and $imported["BattleEngineMelody"]
        set += user.element_set
      elsif YEM::BATTLE_ENGINE::APPLY_MAGICAL_ELE and $imported["BattleEngineMelody"]
        set += user.element_set
      end
    else
      set = user.element_set
    end
    set = $game_temp.action_elements if $game_temp.action_elements != nil
    for i in set
      unless self.enemy?
        for equip in self.equips.compact
          next if equip == nil
          eva += equip.ele_evas[i] unless equip.ele_evas[i] == nil
        end
      end
      for state in user.states
        eva += state.ele_evas[i] unless state.ele_evas[i] == nil
      end
    end
   return eva
  end
 
end # Game_Battler

if $imported["BattleEngineMelody"]
# Only affect crit rate if BEM is installed
 
#==============================================================================
# Game_Actor
#==============================================================================
  
class Game_Actor < Game_Battler
  
  alias csa_elehec_cri cri unless $@
  def cri
    n = csa_elehec_cri
    if self.action.skill?
      set = self.action.skill.element_set
      if self.action.skill.physical_attack and YEM::BATTLE_ENGINE::APPLY_PHYSICAL_ELE
        set += self.element_set
      elsif YEM::BATTLE_ENGINE::APPLY_MAGICAL_ELE
        set += self.element_set
      end
    elsif self.action.item?
      set = []
    else
      set = self.element_set
    end
    set = $game_temp.action_elements if $game_temp.action_elements != nil
    for i in set
      for equip in self.equips.compact
        next if equip == nil
        n += equip.ele_cris[i] unless equip.ele_cris[i] == nil
      end
      for state in self.states
        n += state.ele_cris[i] unless state.ele_cris[i] == nil
      end
    end
   return Integer(n)
  end

end # Game_Actor
 
#==============================================================================
# Game_Enemy
#==============================================================================
  
class Game_Enemy < Game_Battler
  
  alias csa_elehec_cri cri unless $@
  def cri
    n = csa_elehec_cri
    if self.action.skill?
      set = self.action.skill.element_set
      if self.action.skill.physical_attack and YEM::BATTLE_ENGINE::APPLY_PHYSICAL_ELE
        set += self.element_set
      elsif YEM::BATTLE_ENGINE::APPLY_MAGICAL_ELE
        set += self.element_set
      end
    elsif self.action.item?
      set = []
    else
      set = self.element_set
    end
    set = $game_temp.action_elements if $game_temp.action_elements != nil
    for i in set
      for state in self.states
        n += state.ele_cris[i] unless state.ele_cris[i] == nil
      end
    end
   return Integer(n)
  end

end # Game_Enemy
 
end # $imported["BattleEngineMelody"]
 
#===============================================================================
# 
# END OF FILE
# 
#===============================================================================