# State Remove by Weakness by Coelocanth
#
# This script alters the "remove by damage" flag on states by giving some
# extra options about what sort of damage removes the state.
#
# Instructions
#
# Install this script in the usual way by inserting below Materials in the
# script editor.
#
# In the database, add the following note tags to states with the
# "remove by damage" flag set:
#
# <remove by weakness>
# Damage will only remove the state if the damage element is one which the
# target is weak against. (An element rate of > 100% or 1.0)
#
# This could be used to implement a "guard break" type effect where hitting
# an enemy with its weakpoint removes a defensive state and leaves it open to
# other damage sources.
#
# <remove by strength>
# Damage will only remove the state if the damage element is one which the
# target is strong against. (An element rate of < 100% or 1.0)
#
# This is the opposite of remove by weakness, I'm sure you'll think of a use
# for it.
#
# <remove by element:id>
# Damage will only remove the state if it is of the specified type.
# This tag can be specified multiple times, in which case any of the matching
# damage types will work.
#
# This could be used for something like an Icy Aura which needs to be hit with
# fire damage to remove. A common thing for puzzle bosses in RPGs.
#
# Script calls for lunatics
#
# @result.element_id contains the element type used
# @result.element_rate contains the overall element rate
#
# You may be able to use these in combination with another script like Yanfly's
# Lunatic States for more hidden fun stuff.
#
# Conflicts
# This script overrides (replaces) the Game_Battler::remove_states_by_damage
# method. Other scripts that do the same thing are likely to conflict without
# editing.
# Other scripts that hook this method may work by placing after this script.

# Hook the database load to parse note tags
module DataManager
  class <<self; alias load_database_ccsrw load_database; end
  def self.load_database
    load_database_ccsrw
    for state in $data_states
      state.load_notetags_ccsrw unless state.nil?
    end
  end
end


class RPG::State < RPG::BaseItem
  attr_accessor :remove_by_weakness
  attr_accessor :remove_by_strength
  attr_accessor :remove_by_elements
  def load_notetags_ccsrw
    @remove_by_weakness = false
    @remove_by_strength = false
    @remove_by_elements = []
    self.note.split(/^/).each do |line|
      case line
      when /<remove by weakness>/i
        @remove_by_weakness = true
      when /<remove by strength>/i
        @remove_by_strength = true
      when /<remove by element:(\d+)/
        @remove_by_elements.push($1.to_i)
      end
    end
  end
end

class Game_ActionResult
  # elem_rate stores what the damage multiplier was
  attr_accessor :element_rate
  attr_accessor :element_id
  alias clear_damage_values_ccsrw clear_damage_values
  def clear_damage_values
    clear_damage_values_ccsrw
    @element_rate = 1.0
    @element_id = 0
  end
end

class Game_Battler
  alias make_damage_value_ccsrw make_damage_value
  def make_damage_value(user, item)
    make_damage_value_ccsrw(user, item)
    @result.element_rate = item_element_rate(user, item)
    @result.element_id = item.damage.element_id
  end

  # OVERRIDE method
  def remove_states_by_damage
    states.each do |state|
      if state.remove_by_damage && rand(100) < state.chance_by_damage
        next if state.remove_by_weakness && @result.element_rate <= 1.0
        next if state.remove_by_strength && @result.element_rate >= 1.0
        next unless state.remove_by_elements.empty? ||
                    state.remove_by_elements.include?(@result.element_id)
        remove_state(state.id)
      end
    end
  end
end