#≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
# ** Glitchfinder's Cornered Battle Statistics                   [RPG Maker VX]
#    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 Slime), the second being enemy 002
#  (Bat), 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 Attack, Defense, Spirit, and Agility. 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.)
#==============================================================================
# * 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.75, 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,
    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,
    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
    @atk_change = 0
    @def_change = 0
    @spi_change = 0
    @agi_change = 0
    @atk_change = 0
    @pdef_change = 0
    @mdef_change = 0
    @eva_change = 0
  end
  #--------------------------------------------------------------------------
  # * Get Attack
  #--------------------------------------------------------------------------
  def atk
    n = [[base_atk + @atk_plus + @atk_change, 1].max, 999].min
    for state in states do n *= state.atk_rate / 100.0 end
    n = [[Integer(n), 1].max, 999].min
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Defense
  #--------------------------------------------------------------------------
  def def
    n = [[base_def + @def_plus + @def_change, 1].max, 999].min
    for state in states do n *= state.def_rate / 100.0 end
    n = [[Integer(n), 1].max, 999].min
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Spirit
  #--------------------------------------------------------------------------
  def spi
    n = [[base_spi + @spi_plus + @spi_change, 1].max, 999].min
    for state in states do n *= state.spi_rate / 100.0 end
    n = [[Integer(n), 1].max, 999].min
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Agility
  #--------------------------------------------------------------------------
  def agi
    n = [[base_agi + @agi_plus + @agi_change, 1].max, 999].min
    for state in states do n *= state.agi_rate / 100.0 end
    n = [[Integer(n), 1].max, 999].min
    return n
  end
  #--------------------------------------------------------------------------
  # * Set Attack Power Modifier
  #     atk : new Attack Power
  #--------------------------------------------------------------------------
  def atk_modifier=(atk)
    @atk_change = atk
  end
  #--------------------------------------------------------------------------
  # * Set Attack Power Modifier
  #     atk : new Attack Power
  #--------------------------------------------------------------------------
  def def_modifier=(defense)
    @def_change = defense
  end
  #--------------------------------------------------------------------------
  # * Set Spirit Modifier
  #     atk : new Attack Power
  #--------------------------------------------------------------------------
  def spi_modifier=(spi)
    @spi_change = spi
  end
  #--------------------------------------------------------------------------
  # * Set Agility Modifier
  #     atk : new Attack Power
  #--------------------------------------------------------------------------
  def agi_modifier=(agi)
    @agi_change = agi
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Stats
  #--------------------------------------------------------------------------
  def cornered_stats(percent_change)
    cornered_atk(percent_change)
    cornered_def(percent_change)
    cornered_spi(percent_change)
    cornered_agi(percent_change)
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Attack
  #--------------------------------------------------------------------------
  def cornered_atk(percent_change)
    atk_formula = (8 * (self.id)) - 8
    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 Defense
  #--------------------------------------------------------------------------
  def cornered_def(percent_change)
    def_formula = (8 * (self.id)) - 7
    if self.is_a?(Game_Enemy)
      def_change_default = Cornered_Stats::Enemy_Percent_Change[def_formula]
    else
      def_change_default = Cornered_Stats::Actor_Percent_Change[def_formula]
    end
    def_change_percent = def_change_default * (percent_change * 100)
    def_change = (base_def * def_change_percent)
    self.def_modifier = def_change
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Spirit
  #--------------------------------------------------------------------------
  def cornered_spi(percent_change)
    spi_formula = (8 * (self.id)) - 6
    if self.is_a?(Game_Enemy)
      spi_change_default = Cornered_Stats::Enemy_Percent_Change[spi_formula]
    else
      spi_change_default = Cornered_Stats::Actor_Percent_Change[spi_formula]
    end
    spi_change_percent = spi_change_default * (percent_change * 100)
    spi_change = (base_spi * spi_change_percent)
    self.spi_modifier = spi_change
  end
  #--------------------------------------------------------------------------
  # * Get Cornered Agility
  #--------------------------------------------------------------------------
  def cornered_agi(percent_change)
    agi_formula = (8 * (self.id)) - 5
    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
end

#==============================================================================
# ** Game_Enemy
#------------------------------------------------------------------------------
#  This class handles enemy characters. It's used within the Game_Troop class
# ($game_troop).
#==============================================================================

class Game_Enemy < Game_Battler
  #--------------------------------------------------------------------------
  # * Get Enemy ID
  #--------------------------------------------------------------------------
  def id
    return @enemy_id
  end
end

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

class Scene_Battle
  
  #--------------------------------------------------------------------------
  # * Battle Action Processing
  #--------------------------------------------------------------------------
  def process_action
    return if judge_win_loss
    return if $game_temp.next_scene != nil
    set_next_active_battler
    if @active_battler == nil
      turn_end
      return
    end
    hp_percent = @active_battler.hp.to_f / @active_battler.base_maxhp.to_f
    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_f >= 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
    @message_window.clear
    wait(5)
    @active_battler.white_flash = true
    unless @active_battler.action.forcing
      @active_battler.action.prepare
    end
    if @active_battler.action.valid?
      execute_action
    end
    unless @active_battler.action.forcing
      @message_window.clear
      remove_states_auto
      display_current_state
    end
    @active_battler.white_flash = false
    @message_window.clear
  end
end