#≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
# ** Glitchfinder's Cornered Battle Statistics                   [RPG Maker XP]
#    Version 1.00
#------------------------------------------------------------------------------
#  This script helps game designers to manipulate the statistics of an enemy or
#  character based on their remaining HP.
#==============================================================================
# * Version History
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#   Version 1 ---------------------------------------------------- (2008-08-10)
#==============================================================================
# * Instructions
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  Place this script above Main, and below the default scripts. (I realize this
#  is obvious to most, but some people don't get it.)
#
#  To change the default percentage of HP at which an enemy will start
#  experiencing stat changes, edit the values in the Enemy_Percent array within
#  the Conrnered_Stats module. Each value corresponds to a specific enemy, with
#  the first being enemy 001 (The default is Ghost), the second being enemy 002
#  (Basilisk), and so on.
#
#  To change the default percentage of HP at which an actor will start
#  experiencing stat changes, edit the values in the Actor_Percent array within
#  the Cornered_Stats module. This array follows the same rule as the
#  Enemy_Percent array mentioned above.
#
#  To modify the maximum percentage that an enemy's stats can change, edit the
#  values in the Enemy_Percent_Change array in the Cornered_Stats module. The
#  order of the values is Strength, Dexterity, Agility, Intelligence, Attack
#  Power, Physical Defense Power, Magic Defense Power, and Evasion Correction.
#  The first row is the first enemy in the database, (Ghost), the second row is
#  the second enemy in the database (Basilisk), and so on.
#
#  To modify the maximum percentage that an actor's stats can change, edit the
#  values in the Actor_Percent_Change array in the Cornered_Stats module. The
#  rules are the same as within the Enemy_Percent_Change array mentioned above.
#
#  The percentages placed within the Enemy_Percent and Actor_Percent arrays must
#  be in the decimal forma, while those placed within the Enemy_Percent_Change
#  and Actor_Percent_Change arrays must be placed as a percentage, minus the %
#  symbol.
#
#  The percentages in the Enemy_Percent_Change and the Actor_Percent_Change
#  arrays may be either negative or positive, while those placed in the
#  Enemy_Percent and Actor_Percent arrays must be positive.
#
#  If you do not want an enemy or actor to recive stat changes, then put nil in
#  the proper location in the Enemy_Percent or Actor_Percent arrays.
#
#  If you have designated an enemy or actor as unable to recieve stat changes,
#  you must still define their row in their place in the Enemy_Percent_Change or
#  Actor_Percent_Change arrays by placing a 0 in each stat.
#
#  The amount that an enemy's or an actor's stats will change when this script
#  comes into effect depends upon their remaining HP. The remaining HP is
#  divided by the threshhold, and then is multiplied by the percent that the
#  stat can change.(The threshhold is the amount of HP that corresponds to the
#  percent defined in the Enemy_Percent or Actor_Percent arrays of the
#  Cornered_Stats module.)
#==============================================================================
# * Glitchfinder's Advice
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  This script is meant for people with at least basic scripting knowledge. It
#  may be difficult to use, and thus, if you are unsure of your abilities, it
#  may be simpler to avoid using this script.
#
#  I would recommend leaving the percentages within the Enemy_Percent_Change and
#  Actor_Percent_Change arrays low, due to large stat distortions that may
#  occur. (For example, even minor changes in one or two stats will render an
#  enemy either unable to hit an actor or able to hit an actor every time)
#==============================================================================
# * Contact
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  Glitchfinder, the author of this script, may be contacted through his
#  website, found at http://www.glitchkey.com
#
#  You may also find Glitchfinder at http://www.hbgames.org
#==============================================================================
# * Usage
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  This script may be used with the following terms and conditions:
#
#    1. This script is free to use in any noncommercial project. If you wish to
#       use this script in a commercial (paid) project, please contact
#       Glitchfinder at his website.
#    2. This script may only be hosted at the following domains:
#         http://www.glitchkey.com
#         http://www.hbgames.org
#    3. If you wish to host this script elsewhere, please contact Glitchfinder.
#    4. If you wish to translate this script, please contact Glitchfinder. He
#       will need the web address that you plan to host the script at, as well
#       as the language this script is being translated to.
#    5. This header must remain intact at all times.
#    6. Glitchfinder remains the sole owner of this code. He may modify or
#       revoke this license at any time, for any reason.
#    7. Any code derived from code within this script is owned by Glitchfinder,
#       and you must have his permission to publish, host, or distribute his
#       code.
#    8. This license applies to all code derived from the code within this
#       script.
#    9. If you use this script within your project, you must include visible
#       credit to Glitchfinder, within reason.
#≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡

#==============================================================================
# ** Cornered_Stats
#------------------------------------------------------------------------------
#  This module deals with values used in an addition to the battle system. Its
#  used within the Game_Battler and Scene_Battle classes.
#==============================================================================
module Cornered_Stats
  Enemy_Percent = [0.50, 0.25, 0.30, 0.40, 0.35, nil, nil, nil, nil, nil, nil,
                    nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
                    nil, nil, nil, nil, nil, nil, nil, nil, nil]
  Actor_Percent = [0.15, nil, nil, nil, nil, nil, nil, nil]
  Enemy_Percent_Change = [
    0, 0, 0, 0, 11, -5, -9, -3,
    0, 0, 0, 0, 16, -2, -5, 0,
    0, 0, 0, 0, 4, -7, -6, 0,
    0, 0, 0, 0, 7, -4, -3, 0,
    0, 0, 0, 0, 13, -2, -5, 2,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0
  ]
  Actor_Percent_Change = [
    0, 0, 0, 0, 16, -7, -9, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0
  ]
end

#==============================================================================
# ** Game_Battler
#------------------------------------------------------------------------------
#  This class deals with battlers. Its used as a superclass for the Game_Actor
#  and Game_Enemy classes.
#==============================================================================

class Game_Battler
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias glitchfinder_cornered_stats_initialize initialize
  def initialize
    glitchfinder_cornered_stats_initialize
    @str_change = 0
    @dex_change = 0
    @agi_change = 0
    @int_change = 0
    @atk_change = 0
    @pdef_change = 0
    @mdef_change = 0
    @eva_change = 0
  end
  #--------------------------------------------------------------------------
  # * Get Strength (STR)
  #--------------------------------------------------------------------------
  def str
    n = [[base_str + @str_plus + @str_change, 1].max, 999].min
    for i in @states
      n *= $data_states[i].str_rate / 100.0
    end
    n = [[Integer(n), 1].max, 999].min
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Dexterity (DEX)
  #--------------------------------------------------------------------------
  def dex
    n = [[base_dex + @dex_plus + @dex_change, 1].max, 999].min
    for i in @states
      n *= $data_states[i].dex_rate / 100.0
    end
    n = [[Integer(n), 1].max, 999].min
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Agility (AGI)
  #--------------------------------------------------------------------------
  def agi
    n = [[base_agi + @agi_plus + @agi_change, 1].max, 999].min
    for i in @states
      n *= $data_states[i].agi_rate / 100.0
    end
    n = [[Integer(n), 1].max, 999].min
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Intelligence (INT)
  #--------------------------------------------------------------------------
  def int
    n = [[base_int + @int_plus + @int_change, 1].max, 999].min
    for i in @states
      n *= $data_states[i].int_rate / 100.0
    end
    n = [[Integer(n), 1].max, 999].min
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Attack Power
  #--------------------------------------------------------------------------
  def atk
    n = base_atk + @atk_change
    for i in @states
      n *= $data_states[i].atk_rate / 100.0
    end
    return Integer(n)
  end
  #--------------------------------------------------------------------------
  # * Get Physical Defense Power
  #--------------------------------------------------------------------------
  def pdef
    n = base_pdef + @pdef_change
    for i in @states
      n *= $data_states[i].pdef_rate / 100.0
    end
    return Integer(n)
  end
  #--------------------------------------------------------------------------
  # * Get Magic Defense Power
  #--------------------------------------------------------------------------
  def mdef
    n = base_mdef + @mdef_change
    for i in @states
      n *= $data_states[i].mdef_rate / 100.0
    end
    return Integer(n)
  end
  #--------------------------------------------------------------------------
  # * Get Evasion Correction
  #--------------------------------------------------------------------------
  def eva
    n = base_eva + @eva_change
    for i in @states
      n += $data_states[i].eva
    end
    return n
  end
  #--------------------------------------------------------------------------
  # * Set Attack Power Modifier
  #     atk : new Attack Power
  #--------------------------------------------------------------------------
  def str_modifier=(str)
    @str_change = str
  end
  #--------------------------------------------------------------------------
  # * Set Attack Power Modifier
  #     atk : new Attack Power
  #--------------------------------------------------------------------------
  def dex_modifier=(dex)
    @dex_change = dex
  end
  #--------------------------------------------------------------------------
  # * Set Attack Power Modifier
  #     atk : new Attack Power
  #--------------------------------------------------------------------------
  def agi_modifier=(agi)
    @agi_change = agi
  end
  #--------------------------------------------------------------------------
  # * Set Attack Power Modifier
  #     atk : new Attack Power
  #--------------------------------------------------------------------------
  def int_modifier=(int)
    @int_change = int
  end
  #--------------------------------------------------------------------------
  # * Set Attack Power Modifier
  #     atk : new Attack Power
  #--------------------------------------------------------------------------
  def atk_modifier=(atk)
    @atk_change = atk
  end
  #--------------------------------------------------------------------------
  # * Set Physical Defense Power Modifier
  #     pdef : new Physical Defense Power
  #--------------------------------------------------------------------------
  def pdef_modifier=(pdef)
    @pdef_change = pdef
  end
  #--------------------------------------------------------------------------
  # * Set Magic Defense Power Modifier
  #     mdef : new Magic Defense Power
  #--------------------------------------------------------------------------
  def mdef_modifier=(mdef)
    @mdef_change = mdef
  end
  #--------------------------------------------------------------------------
  # * Set Evasion Correction Modifier
  #     eva : new Evasion Correction
  #--------------------------------------------------------------------------
  def eva_modifier=(eva)
    @eva_change = eva
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Stats
  #--------------------------------------------------------------------------
  def cornered_stats(percent_change)
    cornered_str(percent_change)
    cornered_dex(percent_change)
    cornered_agi(percent_change)
    cornered_int(percent_change)
    cornered_atk(percent_change)
    cornered_pdef(percent_change)
    cornered_mdef(percent_change)
    cornered_eva(percent_change)
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Strength
  #--------------------------------------------------------------------------
  def cornered_str(percent_change)
    str_formula = (8 * (self.id)) - 8
    if self.is_a?(Game_Enemy)
      str_change_default = Cornered_Stats::Enemy_Percent_Change[str_formula]
    else
      str_change_default = Cornered_Stats::Actor_Percent_Change[str_formula]
    end
    str_change_percent = str_change_default * (percent_change * 100)
    str_change = (base_str * str_change_percent)
    self.str_modifier = str_change
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Dexterity
  #--------------------------------------------------------------------------
  def cornered_dex(percent_change)
    dex_formula = (8 * (self.id)) - 7
    if self.is_a?(Game_Enemy)
      dex_change_default = Cornered_Stats::Enemy_Percent_Change[dex_formula]
    else
      dex_change_default = Cornered_Stats::Actor_Percent_Change[dex_formula]
    end
    dex_change_percent = dex_change_default * (percent_change * 100)
    dex_change = (base_dex * dex_change_percent)
    self.dex_modifier = dex_change
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Agility
  #--------------------------------------------------------------------------
  def cornered_agi(percent_change)
    agi_formula = (8 * (self.id)) - 6
    if self.is_a?(Game_Enemy)
      agi_change_default = Cornered_Stats::Enemy_Percent_Change[agi_formula]
    else
      agi_change_default = Cornered_Stats::Actor_Percent_Change[agi_formula]
    end
    agi_change_percent = agi_change_default * (percent_change * 100)
    agi_change = (base_agi * agi_change_percent)
    self.agi_modifier = agi_change
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Intelligence
  #--------------------------------------------------------------------------
  def cornered_int(percent_change)
    int_formula = (8 * (self.id)) - 5
    if self.is_a?(Game_Enemy)
      int_change_default = Cornered_Stats::Enemy_Percent_Change[int_formula]
    else
      int_change_default = Cornered_Stats::Actor_Percent_Change[int_formula]
    end
    int_change_percent = int_change_default * (percent_change * 100)
    int_change = (base_int * int_change_percent)
    self.int_modifier = int_change
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Attack Power
  #--------------------------------------------------------------------------
  def cornered_atk(percent_change)
    atk_formula = (8 * (self.id)) - 4
    if self.is_a?(Game_Enemy)
      atk_change_default = Cornered_Stats::Enemy_Percent_Change[atk_formula]
    else
      atk_change_default = Cornered_Stats::Actor_Percent_Change[atk_formula]
    end
    atk_change_percent = atk_change_default * (percent_change * 100)
    atk_change = (base_atk * atk_change_percent)
    self.atk_modifier = atk_change
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Physical Defense Power
  #--------------------------------------------------------------------------
  def cornered_pdef(percent_change)
    pdef_formula = (8 * (self.id)) - 3
    if self.is_a?(Game_Enemy)
      pdef_change_default = Cornered_Stats::Enemy_Percent_Change[pdef_formula]
    else
      pdef_change_default = Cornered_Stats::Actor_Percent_Change[pdef_formula]
    end
    pdef_change_percent = pdef_change_default * (percent_change * 100)
    pdef_change = (base_pdef * pdef_change_percent)
    self.pdef_modifier = pdef_change
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Magic Defense Power
  #--------------------------------------------------------------------------
  def cornered_mdef(percent_change)
    mdef_formula = (8 * (self.id)) - 2
    if self.is_a?(Game_Enemy)
      mdef_change_default = Cornered_Stats::Enemy_Percent_Change[mdef_formula]
    else
      mdef_change_default = Cornered_Stats::Actor_Percent_Change[mdef_formula]
    end
    mdef_change_percent = mdef_change_default * (percent_change * 100)
    mdef_change = (base_mdef * mdef_change_percent)
    self.mdef_modifier = mdef_change
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Evasion Correction
  #--------------------------------------------------------------------------
  def cornered_eva(percent_change)
    eva_formula = (8 * (self.id)) - 1
    if self.is_a?(Game_Enemy)
      eva_change_default = Cornered_Stats::Enemy_Percent_Change[eva_formula]
    else
      eva_change_default = Cornered_Stats::Actor_Percent_Change[eva_formula]
    end
    eva_change_percent = eva_change_default * (percent_change * 100)
    eva_change = (base_eva * eva_change_percent)
    self.eva_modifier = eva_change
  end
end

#==============================================================================
# ** Scene_Battle
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle
  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 2 : start action)
  #--------------------------------------------------------------------------
  alias glitchfinder_cornered_stats_update_phase4_step2 update_phase4_step2
  def update_phase4_step2
    hp_percent = @active_battler.hp / @active_battler.base_maxhp
    battler_id = @active_battler.id - 1
    if @active_battler.is_a?(Game_Enemy)
      enemy_cornered_percent = Cornered_Stats::Enemy_Percent[battler_id]
      if enemy_cornered_percent != nil && enemy_cornered_percent.to_s.to_f != 0
        if enemy_cornered_percent.to_s.to_f >= hp_percent
          percent_change = (((enemy_cornered_percent.to_s.to_f *
          @active_battler.base_maxhp) - @active_battler.hp) /
          (enemy_cornered_percent.to_s.to_f * @active_battler.base_maxhp))
          @active_battler.cornered_stats(percent_change.to_f)
        end
      end
    end
    if @active_battler.is_a?(Game_Actor)
      actor_cornered_percent = Cornered_Stats::Actor_Percent[battler_id]
      if actor_cornered_percent != nil && actor_cornered_percent.to_s.to_i != 0
        if actor_cornered_percent.to_s.to_i >= hp_percent
          percent_change = (((actor_cornered_percent.to_s.to_f *
          @active_battler.base_maxhp) - @active_battler.hp) /
          (actor_cornered_percent.to_s.to_f * @active_battler.base_maxhp))
          @active_battler::cornered_stats(percent_change)
        end
      end
    end
    glitchfinder_cornered_stats_update_phase4_step2
  end
end