#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# Chaos Rage Limit System (Chaos Drive, Soul Rage, Soul Limit) by Blizzard
# Version: 6.2b
# Type: Actor Transformation System, Enhanced Limit Break System
# Date 5.0: 7.7.2007
# Date 5.0b: 12.7.2007
# Date 5.3b: 24.9.2007
# Date 5.31b: 25.10.2007
# Date 5.32b: 10.12.2007
# Date 5.4b: 6.1.2008
# Date 5.5b: 22.1.2008
# Date 5.6b: 26.1.2008
# Date 5.7b: 17.2.2008
# Date 6.0b: 13.7.2008
# Date 6.01b: 28.7.2008
# Date 6.02b: 1.8.2008
# Date 6.03b: 14.8.2008
# Date 6.04b: 30.9.2008
# Date 6.1b: 13.12.2008
# Date 6.11b: 7.3.2009
# Date 6.12b: 8.3.2009
# Date 6.2b: 26.7.2009
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#   
#  This work is protected by the following license:
# #----------------------------------------------------------------------------
# #  
# #  Creative Commons - Attribution-NonCommercial-ShareAlike 3.0 Unported
# #  ( http://creativecommons.org/licenses/by-nc-sa/3.0/ )
# #  
# #  You are free:
# #  
# #  to Share - to copy, distribute and transmit the work
# #  to Remix - to adapt the work
# #  
# #  Under the following conditions:
# #  
# #  Attribution. You must attribute the work in the manner specified by the
# #  author or licensor (but not in any way that suggests that they endorse you
# #  or your use of the work).
# #  
# #  Noncommercial. You may not use this work for commercial purposes.
# #  
# #  Share alike. If you alter, transform, or build upon this work, you may
# #  distribute the resulting work only under the same or similar license to
# #  this one.
# #  
# #  - For any reuse or distribution, you must make clear to others the license
# #    terms of this work. The best way to do this is with a link to this web
# #    page.
# #  
# #  - Any of the above conditions can be waived if you get permission from the
# #    copyright holder.
# #  
# #  - Nothing in this license impairs or restricts the author's moral rights.
# #  
# #----------------------------------------------------------------------------
# 
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# 
#   THIS SCRIPT MAY NOT BE USED BY PEOPLE WHO ARE MAINLY ACTIVE ON RMXP.ORG!!!
# 
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# 
# IMPORTANT NOTE:
# 
#   This script allows the use of the "Soul Rage System" (SRS), the "Soul Limit
#   System" (SLS) and the "Chaos Drive System" (CDS) at the same time. Special
#   Thanks to GuardianAngelX72 for the idea for Chaos Drive.
# 
# 
# Compatibility:
# 
#   98% compatible with SDK 1.x. 70% compatible with SDK 2.x. WILL corrupt your
#   old savegames. Can cause incompatibilty issues with following scripts
#   and/or systems:
#   - exotic CBS-es
#   - Limit Break systems
#   - Custom Equipment systems (i.e. 2 accessories) (can be EASILY merged)
#   - needs the 6.x version of the Scene_SoulRage add-on if you use one
#   
# RTAB specific Compatibility Issues and Notes:
# 
#   - Compatible with RTAB 1.16 and a majority of the available plugins.
#   - COMPLETELY INCOMPATIBLE WITH CONNECTED ATTACKING!!! Remove Connected
#     Attacking and turn it off, otherwise you WILL experience bugs! Also
#     remove the Consume Items Patch for Connected Attacking if you have it!
#   - Disables LEFT/RIGHT selection of actors in battle, use Q/W instead.
#   - Seph's slant bars will be used when using the DRAW_SR_BAR option
#   - not compatible with Chaos Drive System yet, refrain from using it in RTAB
# 
# 
# Features:
# 
#   - Chaos Drive, Soul Rage and Soul Limit Systems in one script
#   - configure your databases easily
#   - using Blizzard gradient styler 4.x with 7 different styles
#   - contains universal font fix, never ever "I can't see the letters"
#   - completely overworked all three systems
#   - compatible with Tons of Add-ons
#   - maximum compatibility with other scripts
#   - moving Chaos Drive/Soul Rage/Soul Limit command in defineable color and a
#     faster animated cursor
# 
# SRS specific features:
#   - use multiple skills per equipment part
#   - SR % increase when you get attacked, use it to unleash SR skills
#   - cycle through available skills with LEFT/RIGHT
# SLS specific features:
#   - configure Soul Limit for each character separately
#   - using a limit can kill a character if you set it up that way
#   - use a different command than the actual SL skill name
#     (example: command name is "Bahamut", skill name is "Giga Flare")
# CDS specific features:
#   - replaces entire party with one Chaos Drive OR transforms only the actor
#     who is using the CD skill
#   - revert Chaos Drive anytime
#   - configurable conditions like in Soul Limit
# 
# new in v5.3b:
#   - compatible with Multi-Hit from Tons of Add-ons v5.x and higher
#   - rewritten conditions using classic syntax to avoid RGSS conditioning bug
#   - added possiblities that status effect, armors and weapons can increase
#     the SRS_RATE for a character
#   - fixed a couple of bugs and glitches
#   - removed the barstyle plugin, please get the newest version of Tons of
#     Add-ons if you want to keep using the bars
#   - Chaos Drive Status Effect can now be named however you like, but you need
#     to configure it anyway
#   - improved coding
# 
# new in v5.31b:
#   - fixed a slight bug that would never occur anyway
#   - now the global variable for compatibility contains the version of CRLS
# 
# new in v5.32b:
#   - improved performance
# 
# new in v5.4b:
#   - now it is possible to define actors who can use SRS/SLS/CDS and who can't
#   - other bugs were not fixed, I was too drunk to fix them at the time being
# 
# new in v5.5b:
#   - fixed the bugs that were not fixed in the last version
# 
# new in v5.6b:
#   - made easy renaming of the systems possible
#   - added SR_REVERSAL option
#   - decreased lag during the slide-from-the-right animation
#   - organized configuration a little bit better
#   - commented code
# 
# new in v5.7b:
#   - now compatible with Item Requirement System from Tons of Add-ons
# 
# new in v6.0b:
#   - now compatible with Guilamme777's Multi-Slot Equipment System
#   - now compatible with RTAB v1.16 + majority of plugins
#   - now compatible with Blizz-ABS v1.99 or higher (requires the additional
#     compatibility plugin)
#   - better coding
# 
# new in v6.01b:
#   - fixed the bugs that occured at the end of battle
# 
# new in v6.02b:
#   - added Chaos Drive state removal upon reversal for convenience and full
#     Blizz-ABS compatibility
# 
# new in v6.03b:
#   - fixed bug when using a Soul Limit would take SR additionally to SP
#     instead of the real SR rate
# 
# new in v6.04b:
#   - fixed updating issue with SDK 2.x
# 
# new in v6.1b:
#   - improved customizability and compatibility with Blizz-ABS
# 
# new in v6.11b:
#   - improved coding and customizability
# 
# new in v6.12b:
#   - fixed an issue with RTAB
# 
# new in v6.2b:
#   - now compatible with HP Consuming Skills from Tons of Add-ons
# 
# 
# General configuration:
# 
#   DRAW_SR     - set this value to false if you don't want SR% display at all
#   DRAW_SR_BAR - set this value to false if you don't want an SR bar at all
#                 (does not work if you don't have a script for HP/SP/EXP bars)
#   SR_NAME     - this is the name of the SR % if you have SRS turned off
#   SR_ENABLED  - add any actor ID of actors who actually have the SR attribute
#                 drawn in battle
#   SR_REVERSAL - if you set this value to true, gaining HP will decrease SR
#   RTAB_ACTIVE - set this value to true if you are using RTAB v1.16 by Minkoff
# 
# 
# Instructions:
# 
#                           ...::: Soul Rage :::...
# 
# Explanation:
# 
#   This script allows the player equipment to have built-in skills. If enemies
#   attack a character a value (the Soul Rage or SR) will increase. With a
#   certain ammount of SR it is possible to perform special skills that are
#   implemented into equipment. Soul Rage is similar to "Ikari skills" from
#   "Lufia 2 - Rise of the Sinistrals" for the SNES. Although, this script
#   provides the possiblity of using more skills with one equipment part with
#   several enhanced functionalities and allows using the SR value for more
#   than just Soul Rage (like for Soul Limit and/or Chaos Drive).
# 
# 
# Configuration:
# 
#   Press CRTL+SHIFT+F and type into the window:
#   Soul Rage Database
#   You can jump now to the database directly. There are more instructions.
#   Also please configure the following global variable found below:
# 
#   SRS_ACTIVE - set this value to false to disable Rage (if you do so you
#                don't need to configure ANYTHING CONNECTED to Soul Rage)
#                (Side-Note: It's pointless to disable both)
#   SRS_NAME   - the name displayed for the Soul Rage System
#   SRS_RATE   - set the filling rate of the SR, 1000 is standard, 500 is 2
#                times slower and 2000 would be 2 times faster
#   RAGE_COLOR - set the values in the () to numbers between 0-255, also note
#                that they determine the color ammount in the color like this
#                example: (RED, GREEN, BLUE, ALPHA) - note: alpha = opacity
#   SR_USERS   - add any actor ID of actors who can use SRS
# 
# 
# Additional info:
# 
#   If you want to change the value of the SR % ingame use the "Call script"
#   event command and use this syntax:
# 
#     $game_actors[X].sr = Y
# 
#   X - ID of the hero in the database
#   Y - new value of the SR
# 
#   You can also use another syntax:
# 
#     $game_party.actors[X].sr = Y
# 
#   X - position of the hero in the party
#   Y - new value of the SR
# 
#   Note that X starts from 0 and NOT 1. The ammount is shown as 100,0%, this
#   is 1000 SR. i.e. 59,1% would be 591 SR.
# 
# 
# Note:
# 
#   Using Soul Rage skills as normal skills is not recommended, because of the
#   SP/SR cost. It is better you create a different skill if you want to use it
#   as Soul Rage. But then again, you don't have to... In the other hand Soul
#   Limit skills can be used as normal skills without any problems.
# 
# 
#                           ...::: Soul Limit :::...
# 
# Explanation:
# 
#   Soul Limit is a special attack an actor can use if he has either low HP,
#   high SR, etc. It is an enhanced version of Limit Break / Overdrive, but
#   CONNECTED to the Soul Rage. 
# 
# 
# Configuration:
# 
#   SLS_ACTIVE -  set this value to false to disable Limit (if you do so you
#                 don't need to configure ANYTHING CONNECTED to Soul Limit)
#   LIMIT_COLOR - set the values in the () to numbers between 0-255, also
#                 note that they determine the color ammount in the color
#                 like this example: (RED, GREEN, BLUE, ALPHA)
#                 note: alpha = opacity
#   SL_USERS    - add any actor ID of actors who can use SLS
# 
#   Press CRTL+SHIFT+F and type into the window:
#   Soul Limit / Chaos Drive Database
# 
# 
# Additional info:
# 
#   Soul Limit is more superior than Limit Break as you can involve HP, SR and
#   even SP conditions as well as consuming. You can create a game where a
#   character can use his Limit skill as long as his HP are under 20% without
#   consuming any stats. (i.e. Final Fantasy VIII) or create a game where the
#   using of a Limit skill requires suicide just by setting the character's
#   @hp_consume value to 100.
#   If you want to change the Soul Limit skill ID and/or command ingame use the
#   "Call script" event command and use this syntax:
# 
#     $game_actors[X].set_new_limit('NEW_NAME', ID)
# 
#   X        - ID of the hero in the database and Y the ammount
#   NEW_NAME - the new name for the SL command
#   ID       - ID of the new SL skill in the database
# 
#   You can also use another syntax:
# 
#     $game_party.actors[Y].set_new_limit('NEW_NAME', ID)
# 
#   Y        - postion of the hero in the party, starts from 0 (NOT 1!)
#   NEW_NAME - the new name for the SL command
#   ID       - ID of the new SL skill in the database
# 
# 
#                          ...::: Chaos Drive :::...
# 
# Explanation:
# 
#   This script will allow the player to use Chaos Drive when in critical
#   HP/SP/SR zone. Chaos Drive will transform the character into another and
#   remove the entire party from battle. If a Chaos Drive dies, his original
#   form will also die and the party will be brought back. Chaos Drives are
#   always full with HP and SP when they enter the battle.
# 
# 
# Configuration:
# 
#   Press CRTL+SHIFT+F and type into the window:
#   START Chaos Drive Database
#   You can jump now to the database directly. There are more instructions.
#   Also please configure the following macros found below:
# 
#   CDS_ACTIVE    - set this value to false to disable CD (if you do so you
#                   don't need to configure ANYTHING CONNECTED to Chaos Drive)
#   CDS_NAME      - the name displayed for the Chaos Drive System
#   REVERT_NAME   - the name displayed for the Revert command
#   CD_ID         - set this value to the state ID of Chaos Drive
#   CD_EXP_NORM   - set to true if the EXP/LVL of your Chaos Drives should be
#                   set to the same as their caller (a stronger caller would
#                   call a stronger version of the same Chaos Drive then)
#   REMOVE_PARTY  - set to true if you want the party to disappear when a CD is
#                   called, set to false if you only want replace the caller
#   MAX_PARTY     - set to the value of the maximum size of your party
#   CHAOS_COLOR   - set the values in the () to numbers between 0-255, also
#                   note that they determine the color ammount in the color
#                   like this
#                   example: (RED, GREEN, BLUE, ALPHA) - note: alpha = opacity
#   REVERT_COLOR  - set the values in the () to numbers between 0-255, also
#                   note that they determine the color ammount in the color
#                   like this
#                   example: (RED, GREEN, BLUE, ALPHA) - note: alpha = opacity
#   CONNECTED     - if you set this value to false, when a Chaos Drive dies,
#                   the caster will still be alive
#   STAY_DAMAGE   - if you set this value to true, Chaos Drives will not be
#                   healed when they are reverted, they will stay dead when
#                   they get killed and calling them will be impossible until
#                   they are revived (can be used for summoning systems), note
#                   that you might need special add-on to allow that
#   CD_USERS      - add any actor ID of actors who can use CDS
# 
#   To add a new Chaos Drive to a characer use the "Call Script" event command
#   and use following syntax:
# 
#     $game_actors[ID].learn_cd(CD)
# 
#   To remove a Chaos Drive from a character use this syntax:
# 
#     $game_actors[ID].forget_cd(CD)
# 
#   ID - ID of the actor in the database
#   CD - ID of the Chaos Drive skill in the database
#   
#   Also create a status effect and set CD_ID to its ID in the database. Make
#   the skill target the user or you will get a bug. Add whatever animation you
#   want.
# 
# 
# Additional info:
# 
#   You can use animations and common event calls (i.e. for enhanced
#   animations) for any CD skill. It is recommended that CD skills target the
#   user.
# 
# 
# Important note:
# 
#   Chaos Drives are not normal skills and should be NOT used as such. Chaos
#   Drive skills as normal skills will not make a transformation happen. Do not
#   assign two different characters the same Chaos Drive character. If you want
#   them to access the same character, make a duplicate CD character in your
#   database.
# 
# 
# If you find any bugs, please report them here:
# http://forum.chaos-project.com
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=

$crls = 6.2

#==============================================================================
# module BlizzCFG
#------------------------------------------------------------------------------
#  This module serves for configuration in my scripts and for consistency.
#==============================================================================

module BlizzCFG
  
  SR_KIND = 3
  SL_KIND = 9
  CD_KIND = 8
  
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Basic CRLS Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  DRAW_SR = true
  DRAW_SR_BAR = true
  SR_NAME = 'SR'
  SR_ENABLED = [1, 2, 3, 4, 5, 6, 7, 8]
  SR_REVERSAL = false
  RTAB_ACTIVE = false
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Basic CRLS Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Soul Rage Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  SRS_ACTIVE = true
  SRS_NAME = 'Soul Rage'
  SRS_RATE = 1000
  RAGE_COLOR = Color.new(255, 0, 0)
  SR_USERS = [1, 2, 3, 4, 5, 6, 7, 8]
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Soul Rage Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Soul Limit Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  SLS_ACTIVE = true
  LIMIT_COLOR = Color.new(0, 255, 0)
  SL_USERS = [1, 2, 3, 4, 5, 6, 7, 8]
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Soul Limit Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Chaos Drive Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  CDS_ACTIVE = true
  CDS_NAME = 'Chaos Drive'
  REVERT_NAME = 'Revert'
  CD_ID = 17
  CD_EXP_NORM = false
  REMOVE_PARTY = true
  MAX_PARTY = 4
  CHAOS_COLOR = Color.new(128, 64, 255)
  REVERT_COLOR = Color.new(255, 255, 255)
  CONNECTED = true
  STAY_DAMAGE = true
  CD_USERS = [1, 2, 3, 4, 5, 6, 7, 8]
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Chaos Drive Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

  def self.sr_database(equip_id, weapon = false)
    skill_ids = []
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Soul Rage Database
#
#   This is your equipment Soul Rage database. To add a new Soul Rage skill to
#   a weapon is very simple. Add another "when"-branch in the script snipplet
#   below (they have comments next to it). Configure it like this template:
# 
#     when WEAPON_ID
#       skill_ids.push(SKILL_ID)
#       skill_ids.push(SKILL_ID)
# 
#   The same works for armors:
# 
#     when ARMOR_ID
#       skill_ids.push(SKILL_ID)
#       skill_ids.push(SKILL_ID)
# 
#   WEAPON_ID - the ID of the weapon in your database
#   ARMOR_ID  - the ID of the armor in your database
#   SKILL_ID  - the ID of the skill in your database
#  
#   The lines are commented below so you should have no problems with the
#   script. To determine the percentage of Soul Rage consumed to use a skill,
#   set the SP to the percentage. e.g. 33SP would mean 33% of the Soul Rage
#   bar.
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    if weapon
      case equip_id # weapon IDs
      when 1
        skill_ids.push(1)
      when 6
        skill_ids.push(10)
        skill_ids.push(11)
        skill_ids.push(12)
      when 10
        skill_ids.push(8)
      end
    else
      case equip_id # armor IDs
      when 5
        skill_ids.push(1)
      when 17
        skill_ids.push(25)
      end
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Soul Rage Database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    end
    return (skill_ids.size > 0 ? skill_ids : [0])
  end
  
  def self.sre_database(equip_id, weapon = false)
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Soul Rage Equipment Database
#
#   This is your Soul Rage Equipment database. Adding equipment IDs here will
#   cause them to increase or decrease the SRS_RATE if they are equipped. All
#   values are in %, that means a value of i.e. 20 will cause 20 more SR, two
#   values of 20 will cause 40, etc. Negative values will decrease the rate.
#   Configure it like this template:
# 
#     when WEAPON_ID then return RATE
# 
#   The same works for armors:
# 
#     when ARMOR_ID then return RATE
# 
#   WEAPON_ID - the ID of the weapon in your database
#   ARMOR_ID  - the ID of the armor in your database
#   RATE      - the increase/decrease rate
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    if weapon
      case equip_id # weapon IDs
      when 2 then return 5
      when 15 then return 5
      end
    else
      case equip_id # armor IDs
      when 3 then return 20
      when 13 then return 10
      end
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Soul Rage Database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    end
    return 0
  end
  
  def self.srs_database(state_id)
    case state_id
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Soul Rage State Database
#
#   This is your Soul Rage State database. Adding status effect IDs here will
#   cause them to increase or decrease the SRS_RATE when they are inflicted.
#   All values are in %, that means a value of i.e. 20 will cause 20 more SR,
#   two values of 20 will cause 40, etc. Negative values will decrease the
#   rate. Configure it like this template:
# 
#     when STATE_ID then return RATE
# 
#   STATE_ID - the ID of the status effect in your database
#   RATE     - the increase/decrease rate
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    when 17 then return 10
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Soul Rage Database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    end
    return 0
  end
  
  def self.cd_database(id)
    case id
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Chaos Drive Database
# 
#   Use following template to connect Chaos Drive skills and their Chaos
#   Drives:
# 
#     when SKILL_ID then return ACTOR_ID
# 
#   SKILL_ID - the ID of the skill in your database
#   ACTOR_ID - the ID of the actor in your database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    when 4 then return 3
    when 5 then return 4
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Chaos Drive Database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    end
    return 0
  end
  
  def self.crls_database(id)
    case id
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Soul Limit / Chaos Drive Database
# 
#   This is your Soul Limit/Chaos Drive Database. Below you can configure the
#   Special attack and the command name of the Soul Limit. You can also
#   configure the conditions that need to be met to allow the usage of Soul
#   Limit and Chaos Drive. Use following syntax to setup the Soul Limit for
#   each character:
# 
#   when ACTOR_ID
#     limit_name = 'NAME'
#     limit_id = SKILL_ID
#     slsr_rate = VAL1
#     slhp_rate = VAL2
#     slsp_rate = VAL3
#     slsr_consume = VAL4
#     slhp_consume = VAL5
#     slsp_consume = VAL6
#     cdsr_rate = VAL7
#     cdhp_rate = VAL8
#     cdsp_rate = VAL9
#     cdsr_consume = VALa
#     cdhp_consume = VALb
#     cdsp_consume = VALc
#     allow_suicide = BOOL
#
#   ACTOR_ID - the ID of the character in the normal database
#   NAME     - the name the limit command should have
#   SKILL_ID - the ID of the Soul Limit skill in the normal database
#   VAL1     - minimum SR (in %) needed to make the Soul Limit skill available
#   VAL2     - maximum HP (in %) needed to make the Soul Limit skill available
#   VAL3     - maximum SP (in %) needed to make the Soul Limit skill available
#   VAL4     - SR (in %) cunsumed if Soul Limit is used
#   VAL5     - HP (in %) cunsumed if Soul Limit is used
#   VAL6     - SP (in %) cunsumed if Soul Limit is used
#   VAL7     - minimum SR (in %) needed to make the Chaos Drive skills available
#   VAL8     - maximum HP (in %) needed to make the Chaos Drive skills available
#   VAL9     - maximum SP (in %) needed to make the Chaos Drive skills available
#   VALa     - SR (in %) cunsumed if Chaos Drive is used
#   VALb     - HP (in %) cunsumed if Chaos Drive is used
#   VALc     - SP (in %) cunsumed if Chaos Drive is used
#   BOOL     - true or false
# 
#   Example:
#   
#   when 2
#     limit_name = "Saturn Cannon"
#     limit_id = 101
#     slsr_rate = 80
#     slhp_rate = 50
#     slsp_rate = 100
#     slsr_consume = -10
#     slhp_consume = 10
#     slsp_consume = 50
#     cdsr_rate = 0
#     cdhp_rate = 10
#     cdsp_rate = 10
#     cdsr_consume = 0
#     cdhp_consume = 0
#     cdsp_consume = 10
#     allow_suicide = false
# 
#  (let's say skill number 101 is called "Last Judgement")
# 
#   Character number 2 needs minimum 80% SR, his HP must be less than or equal
#   to 50% and his SP must be less than or equal to 100% (in other words, SP
#   don't affect the availability of the SL skill of character number 2). If he
#   uses the Soul Limit command displayed as "Saturn Cannon", he will use the
#   skill "Last Judgement" (ID number 101). He will gain 10% of his maximum SR,
#   lose 10% of his maximum HP and lose 50% of his maximum SP. If his HP are
#   less than or equal to 10% he will not die, but his HP will only decrease to
#   1. Chaos drive can be used if the HP and SP are 10% or lower. If used, 10%
#   SP of the using actor will be consumed. If you are not using either SL or
#   CD, you can just skip configuring those numbers.
# 
# Side-notes:
#   
#   The VAL values can be from 0 to 100 (since it is in %). Every value over
#   100 is automatically limited to 100. Negative values will cause stat
#   gaining instead of losing. (You can create skills, that heal the user AND
#   attack the enemies without a common event call). If you set the values
#   VAL1, VAL2 and/or VAL3 to a negative number, this character will have no
#   Soul Limit.
# 
# Notes:
#   
#   - you have the possibility to i.e. set a command to "Bahamut", but the
#     actual skill name that will be displayed during its animation will be
#     "Giga Flare" (does not work in Blizz-ABS)
#   - to change a Soul Limit skill during the game read the instructions at the
#     beginning of the script
#   - do not set it up that way that the user dies from using a Chaos Drive. It
#     might bug your game. Best is to let using a CD skill not consume any HP.
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    when 1
      limit_name = "Devastator"
      limit_id = 79
      slsr_rate = 20
      slhp_rate = 50
      slsp_rate = 100
      slsr_consume = 20
      slhp_consume = 0
      slsp_consume = 0
      cdsr_rate = 0
      cdhp_rate = 100
      cdsp_rate = 100
      cdsr_consume = 0
      cdhp_consume = 0
      cdsp_consume = 0
      allow_suicide = false
    when 2
      limit_name = "Summon Proximus"
      limit_id = 80
      slsr_rate = 100
      slhp_rate = 100
      slsp_rate = 100
      slsr_consume = 20
      slhp_consume = 0
      slsp_consume = 0
      cdsr_rate = 20
      cdhp_rate = 100
      cdsp_rate = 100
      cdsr_consume = 100
      cdhp_consume = 0
      cdsp_consume = 0
      allow_suicide = true
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Soul Limit / Chaos Drive Database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    else
      limit_name, allow_suicide = '', true
      slsr_rate = slhp_rate = slsp_rate = cdsr_rate = cdhp_rate = cdsp_rate = -1
      limit_id = slsr_consume = slhp_consume = slsp_consume = cdsr_consume =
      cdhp_consume = cdsp_consume = 0
    end
    return [limit_name, limit_id, slsr_rate, slhp_rate, slsp_rate,
            slsr_consume, slhp_consume, slsp_consume, cdsr_rate, cdhp_rate,
            cdsp_rate, cdsr_consume, cdhp_consume, cdsp_consume, allow_suicide]
  end

  def self.find_chaos(id)
    (1...$data_actors.size).each {|i|
        if $game_actors[i] != nil
          return $game_actors[i] if $game_actors[i].chaos_id == id
        end}
  end
  
end

#==============================================================================
# Game_System
#------------------------------------------------------------------------------
#  Enhanced with new variables.
#==============================================================================

class Game_System
  
  attr_accessor :chaos_party
  attr_accessor :bar_style
  attr_reader   :bar_opacity
  #----------------------------------------------------------------------------
  # override initialize
  #----------------------------------------------------------------------------
  alias init_crls_later initialize
  def initialize
    init_crls_later
    # adding helping variables
    @chaos_party = []
    BlizzCFG::MAX_PARTY.times {@chaos_party.push(0)} unless BlizzCFG::REMOVE_PARTY
  end
  #----------------------------------------------------------------------------
  # bar_opacity=
  #  alpha - new value
  #  Sets the new value of the bar opacity.
  #----------------------------------------------------------------------------
  def bar_opacity=(alpha)
    if alpha > 255
      @bar_opacity = 255
    elsif alpha < 0
      @bar_opacity = 0
    else
      @bar_opacity = alpha
    end
  end
  
end

#==============================================================================
# Game_Actor
#------------------------------------------------------------------------------
#  Enhanced with variables and methods to handle CRLS.
#==============================================================================

class Game_Actor

  attr_accessor :chaos_id
  attr_accessor :chaos_action
  attr_reader   :cd_skills
  attr_reader   :sr
  attr_reader   :limit_name
  attr_reader   :limit_id
  #----------------------------------------------------------------------------
  # override setup
  #  commands - new commands
  #  Sets a new list of skills and updates the display.
  #----------------------------------------------------------------------------
  alias setup_crls_later setup
  def setup(actor_id)
    setup_crls_later(actor_id)
    @cd_skills, @sr, @chaos_id, @chaos_action = [], 0, 0, 0
    @limit_name, @limit_id, @slsr_rate, @slhp_rate, @slsp_rate, @slsr_consume,
    @slhp_consume, @slsp_consume, @cdsr_rate, @cdhp_rate, @cdsp_rate,
    @cdsr_consume, @cdhp_consume, @cdsp_consume, @allow_suicide = 
        BlizzCFG.crls_database(actor_id)
  end
  #----------------------------------------------------------------------------
  # sr=
  #  sr - new SR value
  #  Sets a new value of SR.
  #----------------------------------------------------------------------------
  def sr=(sr)
    if sr > 1000
      @sr = 1000
    elsif sr < 0
      @sr = 0
    else
      @sr = sr
    end
  end
  #----------------------------------------------------------------------------
  # set_new_limit
  #  name - new Soul Limit name
  #  id   - skill ID of the new Soul Limit skill
  #  Changes the Soul Limit of the actor.
  #----------------------------------------------------------------------------
  def set_new_limit(name, id)
    @limit_name, @limit_id = name, id
  end
  #----------------------------------------------------------------------------
  # learn_cd
  #  id - skill ID of the new Chaos Drive skill
  #  This method allows learning of CD skills. It is called by the user.
  #----------------------------------------------------------------------------
  def learn_cd(id)
    # if valid CD skill
    if id > 0 && !@cd_skills.include?(id)
      # learn and sort by skill ID
      @cd_skills.push(id)
      @cd_skills.sort!
      # teach the Chaos Drive character the skill as well
      actor_id = BlizzCFG.cd_database(id)
      $game_actors[actor_id].learn_cd(id) if self.id != actor_id
      return true
    end
    return false
  end
  #----------------------------------------------------------------------------
  # forget_cd
  #  id - skill ID of the Chaos Drive skill
  #  This method allows forgetting of CD skills. It is called by the user.
  #----------------------------------------------------------------------------
  def forget_cd(id)
    # forget the CD skill
    @cd_skills.delete(id)
    # the Chaos Drive character forgets the skill as well
    actor_id = BlizzCFG.cd_database(id)
    $game_actors[actor_id].forget_cd(id) if self.id != actor_id
    return true
  end
  #----------------------------------------------------------------------------
  # use_chaos
  #  id - skill ID of the Chaos Drive skill
  #  This method processes the use of a CD skill.
  #----------------------------------------------------------------------------
  def use_chaos(id)
    # get CD ID
    @chaos_id = BlizzCFG.cd_database(id)
    # stop if not a CD skill
    return if @chaos_id == 0
    # process extra configurations
    $game_actors[@chaos_id].exp = self.exp if BlizzCFG::CD_EXP_NORM
    $game_actors[@chaos_id].recover_all unless BlizzCFG::STAY_DAMAGE
    # if to remove the entire party
    if BlizzCFG::REMOVE_PARTY
      # change settings of actors
      $game_party.actors.each {|actor| actor.chaos_action = -1}
      $game_system.chaos_party = $game_party.actors
      # remove all actors
      $game_party.actors = []
      # add CD actor
      $game_party.add_actor(@chaos_id)
      # inflict CD state
      $game_party.actors[0].add_state(BlizzCFG::CD_ID)
    else
      # store original actor
      $game_system.chaos_party[self.index] = @id
      # replace with CD actor
      $game_party.actors[self.index] = $game_actors[@chaos_id]
      # inflict CD state
      $game_actors[@chaos_id].add_state(BlizzCFG::CD_ID)
      # change settings
      $game_actors[@chaos_id].chaos_action = @chaos_action = -1
    end
    # apply stat consumption
    last_sr = self.sr
    self.hp -= @cdhp_consume * self.maxhp / 100
    self.sp -= @cdsp_consume * self.maxsp / 100
    self.sr = last_sr - @cdsr_consume * 10
  end
  #----------------------------------------------------------------------------
  # revert_chaos
  #  This method processes the reverting from the Chaos Drive.
  #----------------------------------------------------------------------------
  def revert_chaos
    # remove Chaos Drive state
    remove_state(BlizzCFG::CD_ID)
    # if party was removed
    if BlizzCFG::REMOVE_PARTY
      # restore old party
      $game_party.actors = $game_system.chaos_party
      # remove all CD settings
      $game_party.actors.each {|actor| actor.chaos_action = -1}
      # delete temporary party storage
      $game_system.chaos_party = []
      # apply death if CONNECTED option used and died
      BlizzCFG.find_chaos(id).hp = 0 if BlizzCFG::CONNECTED && self.dead?
    else
      # delete actor from temporary storage
      $game_system.chaos_party[self.index] = 0
      # get original actor
      origin_actor = BlizzCFG.find_chaos(id)
      # add original actor back into party
      $game_party.actors[self.index] = origin_actor
      # apply death if CONNECTED option used and died
      origin_actor.hp = 0 if BlizzCFG::CONNECTED && self.dead?
      # remove CD setting
      origin_actor.chaos_action = @chaos_action = -1
    end
  end
  #----------------------------------------------------------------------------
  # sr_can_use?
  #  id - skill ID of SR skill
  #  This method tests whether an SR skill can be used.
  #----------------------------------------------------------------------------
  def sr_can_use?(id)
    left = sr_left(id)
    return (BlizzCFG::SRS_ACTIVE && item_req_test(id) && sr_left(id) != 0)
  end
  #----------------------------------------------------------------------------
  # sl_can_use?
  #  This method tests whether the SL skill can be used.
  #----------------------------------------------------------------------------
  def sl_can_use?
    return false if self.sr / 10 < @slsr_rate
    return false if 100 * self.hp / self.maxhp > @slhp_rate
    return false if 100 * self.sp / self.maxsp > @slsp_rate
    return (BlizzCFG::SLS_ACTIVE && item_req_test(@limit_id) && sl_left != 0)
  end
  #----------------------------------------------------------------------------
  # cd_can_use?
  #  This method tests whether any CD skill can be used.
  #----------------------------------------------------------------------------
  def cd_can_use?
    return false if self.sr / 10 < @cdsr_rate
    return false if 100 * self.hp / self.maxhp > @cdhp_rate
    return false if 100 * self.sp / self.maxsp > @cdsp_rate
    return (BlizzCFG::CDS_ACTIVE && !self.dead? && cd_left != 0)
  end
  #----------------------------------------------------------------------------
  # sr_left
  # id - ID of the SR skill
  #  Returns how many times this SR can be used.
  #----------------------------------------------------------------------------
  def sr_left(id)
    return -1 if $data_skills[id].sp_cost == 0
    return (self.sr / 10 / $data_skills[id].sp_cost)
  end
  #----------------------------------------------------------------------------
  # sl_left
  #  Returns how many times SL can be used.
  #----------------------------------------------------------------------------
  def sl_left
    return -1 if @slsr_consume == 0 && @slhp_consume == 0 && @slsp_consume == 0
    sl = [@slsr_consume == 0 ? -1 : self.sr / 10 / @slsr_consume,
        @slhp_consume == 0 ? -1 : 100 * self.hp / self.maxhp / @slhp_consume,
        @slsp_consume == 0 ? -1 : 100 * self.sp / self.maxsp / @slsp_consume]
    return (sl - [-1]).min
  end
  #----------------------------------------------------------------------------
  # cd_left
  #  Returns how many times CD can be used.
  #----------------------------------------------------------------------------
  def cd_left
    return -1 if @cdsr_consume == 0 && @cdhp_consume == 0 && @cdsp_consume == 0
    cd = [@cdsr_consume == 0 ? -1 : self.sr / 10 / @cdsr_consume,
        @cdhp_consume == 0 ? -1 : 100 * self.hp / self.maxhp / @cdhp_consume,
        @cdsp_consume == 0 ? -1 : 100 * self.sp / self.maxsp / @cdsp_consume]
    return (cd - [-1]).min
  end
  #----------------------------------------------------------------------------
  # item_req_test
  #  This method tests if the Item Requirement System exists and whether a
  #  special skill can be used depending on item consumption.
  #----------------------------------------------------------------------------
  def item_req_test(id)
    if $tons_version != nil && $tons_version >= 6.0 &&
        $game_system.ITEM_REQUIREMENT
      data = BlizzCFG.item_reqs(id)
      return ($game_party.item_number(data[0]) >= data[1])
    end
    return true
  end
  #----------------------------------------------------------------------------
  # override skill_can_use?
  #  id - skill ID
  #  This method was enhanced to disallow the usage of CD skills if the CD
  #  actor was killed previously.
  #----------------------------------------------------------------------------
  alias skill_can_use_crls_later? skill_can_use?
  def skill_can_use?(id)
    # if a CD skill
    if @cd_skills.include?(id)
      # can't use CD if required items not there
      return false if !item_req_test(id)
      # can use CD if STAY_DAMAGE is turned off
      return true unless BlizzCFG::STAY_DAMAGE
      # can use CD if Chaos Drive
      return true if @states.include?(BlizzCFG::CD_ID)
      # find the skill's corresponding CD actor
      id = BlizzCFG.cd_database(id)
      # can be used if skill onnfiguration exists and CD actor is not dead
      return (id != 0 && !$game_actors[id].dead?)
    end
    # normal result processing
    return skill_can_use_crls_later?(id)
  end
  #----------------------------------------------------------------------------
  # use_limit
  #  This method processes stat consumption of SL use.
  #----------------------------------------------------------------------------
  def use_limit
    last_sr = self.sr
    self.hp -= @slhp_consume * self.maxhp / 100
    self.hp = 1 if self.hp == 0 && !@allow_suicide
    self.sp -= @slsp_consume * self.maxsp / 100
    self.sr = last_sr - @slsr_consume * 10
  end
  #----------------------------------------------------------------------------
  # sr_text
  #  This method returns SR in a text format.
  #----------------------------------------------------------------------------
  def sr_text
    return "#{@sr/10}.#{@sr%10}%"
  end
  #----------------------------------------------------------------------------
  # sr_equipment
  #  Returns weapon and armor IDs for SR recognition.
  #----------------------------------------------------------------------------
  def sr_equipment
    # Guilamme777's Multi-Slot Equipment System Compatibility
    return self.weapon_ids, self.armor_ids if defined?(G7_MS_MOD)
    # normal weapons and armors
    return [@weapon_id], [@armor1_id, @armor2_id, @armor3_id, @armor4_id]
  end
  
end
 
#==============================================================================
# Game_Battler
#------------------------------------------------------------------------------
#  This class was enhanced with the capability to increase SR upon change of
#  HP.
#==============================================================================

class Game_Battler
    
  #----------------------------------------------------------------------------
  # override hp=
  #  val - new value
  #  This method processes SR change upon HP change with considering the added
  #  extra values from armors and states.
  #----------------------------------------------------------------------------
  alias hp_is_equal_to_crls_later hp=
  def hp=(val)
    # store old value
    last_hp = self.hp
    # call normal process
    hp_is_equal_to_crls_later(val)
    # if actor
    if self.is_a?(Game_Actor)
      # if dead
      if self.dead?
        # remove all SR
        self.sr = 0
      # if got damaged or SR_REVERSAL is turned on and got healed
      elsif self.hp < last_hp || BlizzCFG::SR_REVERSAL && self.hp > last_hp
        # initialize normal increase rate
        exrate = 100
        # add equipment rate change
        exrate += BlizzCFG.sre_database(@weapon_id, true)
        [@armor1_id, @armor2_id, @armor3_id, @armor4_id].each {|i|
            exrate += BlizzCFG.sre_database(i, false)}
        # add state rate change
        @states.each {|i| exrate += BlizzCFG.srs_database(i)}
        # change SR
        self.sr += (last_hp - self.hp) * BlizzCFG::SRS_RATE *
            exrate / [last_hp, self.hp].min / 100
      end
    end
  end

end

#==============================================================================
# Game_Party
#------------------------------------------------------------------------------
#  This class was changed to support direct access to the instance variable
#  actors.
#==============================================================================

class Game_Party

  attr_accessor :actors
  
end

#==============================================================================
# Spriteset_Battle
#------------------------------------------------------------------------------
#  This class was changed to be able to apply actor changes in the party during
#  CD use/reversal so it looks nice.
#==============================================================================

class Spriteset_Battle

  #----------------------------------------------------------------------------
  # update_actors
  #  flag - flag to change the display instantly or with fading
  #  This method processes sprite update.
  #----------------------------------------------------------------------------
  def update_actors(flag = false)
    # freeze display
    Graphics.freeze
    # for every actor
    @actor_sprites.each_index {|i|
        # set new actor
        actor = @actor_sprites[i].battler = $game_party.actors[i]
        # who exists and is about to change
        if actor != nil && actor.chaos_action == -1
          # remove old sprite
          @actor_sprites[i].dispose
          # load new sprite
          @actor_sprites[i] = Sprite_Battler.new(@viewport2)
          @actor_sprites[i].update
          # sprite setup
          @actor_sprites[i].battler, actor.chaos_action = actor, 0
        end}
    Graphics.transition((flag ? 0 : 20))
    Graphics.freeze
    update
    Graphics.transition((flag ? 0 : 20))
  end
  
end

#==============================================================================
# Window_Base
#------------------------------------------------------------------------------
#  This class was enhanced with a couple of methods for additional drawing.
#==============================================================================

class Window_Base
  
  #----------------------------------------------------------------------------
  # draw_actor_sr
  #  actor - the actor
  #  x     - x-coordinate
  #  y     - y-coordinate
  #  width - maximal allowed width
  #  This method draws the SR attribute.
  #----------------------------------------------------------------------------
  def draw_actor_sr(actor, x, y, w = 144)
    w = 120 if !$scene.is_a?(Scene_Menu) && !BlizzCFG::RTAB_ACTIVE
    self.contents.font.color = system_color
    self.contents.draw_text(x, y-16, width, 64, BlizzCFG::SR_NAME)
    self.contents.font.color = normal_color
    self.contents.draw_text(x, y, w, 32, actor.sr_text, 2)
  end
  #----------------------------------------------------------------------------
  # draw_actor_sr_with_bar
  #  actor - the actor
  #  x     - x-coordinate
  #  y     - y-coordinate
  #  width - maximal allowed width
  #  This method draws the bar of the SR attribute.
  #----------------------------------------------------------------------------
  def draw_actor_sr_with_bar(actor, x, y, w = 148)
    w -= 12
    rate = actor.sr.to_f / 1000
    # SR bar colors
    color1 = Color.new(80, 0, 0, 192) 
    color2 = Color.new(240, 0, 0, 192) 
    color3 = Color.new(80, 0, 0, 192)
    # if using RTAB
    if BlizzCFG::RTAB_ACTIVE
      # set colors full opaque
      color1.alpha = color2.alpha = 255
      # draw Seph's megaslow and laggy bar
      draw_slant_bar(x, y + 12, actor.sr, 1000, w, 6, color1, color2)
    else
      # draw the bar
      self.contents.gradient_bar(x, y, w, color1, color2, color3, rate)
    end
    # draw the text
    if $scene.is_a?(Scene_Battle)
      draw_actor_sr(actor, x, y, w)
    else
      draw_actor_sr(actor, x, y)
    end
  end
  #----------------------------------------------------------------------------
  # draw_item_name_srs
  #  item  - the item
  #  x     - x-coordinate
  #  y     - y-coordinate
  #  color - text color
  #  This method draw the equipment part's name in the given color. It is used
  #  by the SRS window.
  #----------------------------------------------------------------------------
  def draw_item_name_srs(item, x, y, color)
    # stop if item doesn't exist
    return if item == nil
    opacity = self.contents.font.color == normal_color ? 255 : 128
    # cache icon
    bitmap = RPG::Cache.icon(item.icon_name)
    # draw icon
    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
    self.contents.font.color = color
    # draw item name
    self.contents.draw_text(x + 28, y, 288, 32, item.name)
  end
  
end

#==============================================================================
# Window_BattleStatus
#------------------------------------------------------------------------------
#  This class was changed to display the SR attribute and support "Centered
#  Battlers" from Tons of Add-ons.
#==============================================================================

class Window_BattleStatus
  
  #----------------------------------------------------------------------------
  # override refresh
  #----------------------------------------------------------------------------
  alias refresh_crls_later refresh
  def refresh(number = 0)
    # reroute if using RTAB
    if BlizzCFG::RTAB_ACTIVE
      refresh_crls_later(number)
      return
    # abort if not to draw SR anyway
    elsif !BlizzCFG::DRAW_SR
      refresh_crls_later
      return
    end
    self.contents.clear
    # font fix
    if $fontface != nil
      self.contents.font.name = $fontface
      self.contents.font.size = $fontsize
    elsif $defaultfonttype != nil
      self.contents.font.name = $defaultfonttype
      self.contents.font.size = $defaultfontsize
    end
    # iteration through all party members
    $game_party.actors.each_index {|i|
        # decide x position of the status display
        if $tons_version != nil && $tons_version >= 4.82 &&
            $game_system.CENTER_BATTLER
          actor_x = case $game_party.actors.size
          when 1 then 4 + 240
          when 2 then 4 + 80 + i * 320
          when 3 then 4 + 80 + i * 160
          when 4 then 4 + i * 160
          end
        else
          actor_x = i * 160 + 4
        end
        draw_actor_name($game_party.actors[i], actor_x, -8)
        draw_actor_hp($game_party.actors[i], actor_x, 14, 120)
        draw_actor_sp($game_party.actors[i], actor_x, 44, 120)
        # if using SR
        if BlizzCFG::SR_ENABLED.include?($game_party.actors[i].id)
          # draw with or without bar
          if BlizzCFG::DRAW_SR_BAR && $tons_version != nil &&
              $tons_version >= 4.82
            draw_actor_sr_with_bar($game_party.actors[i], actor_x, 74, 120)
          else
            draw_actor_sr($game_party.actors[i], actor_x, 74, 120)
          end
        end
        # if leveled up
        if @level_up_flags[i]
          # draw "LEVEL UP!" instead of state
          self.contents.font.color = normal_color
          self.contents.draw_text(actor_x, 100, 120, 32, 'LEVEL UP!')
        else
          # draw normal state
          draw_actor_state($game_party.actors[i], actor_x, 100)
        end}
  end
  
end

# if using RTAB
if BlizzCFG::RTAB_ACTIVE
#==============================================================================
# Window_DetailsStatus
#==============================================================================

class Window_ActorStatus < Window_Base
  #--------------------------------------------------------------------------
  # override refresh
  #--------------------------------------------------------------------------
  alias refresh_crls_later refresh
  def refresh(level_up_flags)
    refresh_crls_later(level_up_flags)
    @status_window[1].refresh($game_party.actors[@actor_num])
  end
  
end

#==============================================================================
# Window_DetailsStatus
#==============================================================================

class Window_DetailsStatus
  
  #----------------------------------------------------------------------------
  # refresh
  #  actor          - the current actor
  #  level_up_flags - whether the actor has leveled up or not
  #  This method was redefined to allow the display of SR.
  #----------------------------------------------------------------------------
  def refresh(actor, level_up_flags = false)
    self.contents.clear
    # if aligned
    if $bstat_align == 0
      # which attribute will be drawn
      case @status_id
      when 0 then draw_actor_name(actor, 4, -8)
      when 1
        draw_actor_hp(actor, 84, -8, 80)
        draw_actor_sr_with_bar(actor, 266, -8, 112)
      when 2 then draw_actor_sp(actor, 180, -8, 80)
      when 3
        if level_up_flags
          self.contents.font.color = normal_color
          self.contents.draw_text(324, -8, 120, 32, "LEVEL UP!")
        else
          draw_actor_state(actor, 324, -8)
        end
      when 4 then draw_actor_atg(actor, 488, -8, 120)
      end
    else  
      # which attribute will be drawn
      case @status_id
      when 0 then draw_actor_name(actor, 488, -8)
      when 1
        draw_actor_hp(actor, 228, -8, 80)
        draw_actor_sr_with_bar(actor, 420, -8, 112)
      when 2 then draw_actor_sp(actor, 324, -8, 80)
      when 3
        if level_up_flags
          self.contents.font.color = normal_color
          self.contents.draw_text(140, -8, 120, 32, "LEVEL UP!")
        else
          draw_actor_state(actor, 140, -8)
        end
      when 4 then draw_actor_atg(actor, 0, -8, 120)
      end
    end
  end
  
end

end

#==============================================================================
# Window_SoulRage
#------------------------------------------------------------------------------
#  This window displays SR skills and the equipment they are bound to.
#==============================================================================

class Window_SoulRage < Window_Selectable

  attr_accessor :index
  #----------------------------------------------------------------------------
  # Initialization
  #----------------------------------------------------------------------------
  def initialize(actor)
    super(0, 128, 640, 384)
    # setting basic data
    @column_max, @actor, @index = 1, actor, 0
    refresh
    # change appearance if in battle
    self.y, self.height, self.back_opacity = 64, 256, 160 if $game_temp.in_battle
  end
  #----------------------------------------------------------------------------
  # update_actor
  #  actor - the actor
  #  This method replaces the current actor of the window and refreshes the
  #  display.
  #----------------------------------------------------------------------------
  def update_actor(actor)
    @actor = actor
    refresh  
  end
  #----------------------------------------------------------------------------
  # refresh
  #  Refreshes the display.
  #----------------------------------------------------------------------------
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    # data
    @skill_ids, @alt_index, @equipment = [], [], []
    @weapons, @armors = @actor.sr_equipment
    # get all weapon skills
    @weapons.each {|id|
        # skill IDs
        ids = BlizzCFG.sr_database(id, true)
        # add skill IDs
        @skill_ids.push(ids.size == 0 ? [0] : ids)
        # corresponding equipment part
        @equipment.push($data_weapons[id])}
    # get all armor skills
    @armors.each {|id|
        # skill IDs
        ids = BlizzCFG.sr_database(id, false)
        # add skill IDs
        @skill_ids.push(ids.size == 0 ? [0] : ids)
        # corresponding equipment part
        @equipment.push($data_armors[id])}
    # add indices
    @equipment.size.times {@alt_index.push(0)}
    @item_max = @equipment.size
    if @item_max > 0
      self.contents = Bitmap.new(width - 32, @item_max * 32)
      # font fix
      if $fontface != nil
        self.contents.font.name = $fontface
        self.contents.font.size = $fontsize
      elsif $defaultfonttype != nil
        self.contents.font.name = $defaultfonttype
        self.contents.font.size = $defaultfontsize
      end
      (0...@item_max).each {|i| draw_item(i)} if @actor != nil
    end
  end
  #----------------------------------------------------------------------------
  # draw_item
  #  i - the item's index
  #  This method draws the equipment part's name and all bound skills.
  #----------------------------------------------------------------------------
  def draw_item(i)
    # get currently displayed skill
    id = @skill_ids[i][@alt_index[i]]
    # if skill exists
    if id != 0
      skill = $data_skills[id]
      # decide color
      if @actor.sr_can_use?(skill.id)
        self.contents.font.color = normal_color
      else
        self.contents.font.color = disabled_color
      end
      rect = Rect.new(0, i*32, self.width / @column_max - 32, 32)
      self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
      # cache icon
      bitmap = RPG::Cache.icon(skill.icon_name)
      opacity = self.contents.font.color == normal_color ? 255 : 128
      # draw_icon
      self.contents.blt(300, 4+i*32, bitmap, Rect.new(0, 0, 24, 24), opacity)
      self.contents.draw_text(328, i*32, 204, 32, skill.name, 0)
      # if cost is not 0
      unless skill.sp_cost == 0
        # draw SR cost
        self.contents.draw_text(516, i*32, 64, 32, "#{skill.sp_cost}%", 2)
      end
      # if more than one skill available
      if @skill_ids[i].size > 1
        # draw arrows
        self.contents.draw_text(284, i*32, 32, 32, '«')
        self.contents.draw_text(564, i*32, 32, 32, '»', 2)
      end
    else
      # not skills
      self.contents.font.color = disabled_color
      self.contents.draw_text(328, i*32, 204, 32, 'not available', 0)
    end
    # if equipment part is ok
    if @equipment[i] != nil
      # decide color
      if id == 0 || !@actor.sr_can_use?(id)
        color = disabled_color
      else
        color = normal_color
      end
      # draw name
      draw_item_name_srs(@equipment[i], 4, i*32, color)
    else
      # draw that there is nothing
      self.contents.font.color = disabled_color
      self.contents.draw_text(4, i*32, 288, 32, 'Nothing equipped')
    end
  end
  #----------------------------------------------------------------------------
  # override update
  #  This method was modified to support additional refreshing if right/left
  #  are pressing on the SR skill multiselection.
  #----------------------------------------------------------------------------
  alias upd_crls_later update
  def update
    upd_crls_later
    # temporary variable
    size = @skill_ids[@index].size
    # if RIGHT is being pressed
    if Input.trigger?(Input::RIGHT)
      # store old index and change index
      old_index = @alt_index[@index]
      @alt_index[@index] = (@alt_index[@index] + 1) % size
      # refresh if old index is different than current
      if old_index != @alt_index[@index]
        $game_system.se_play($data_system.cursor_se)
        draw_item(@index)
      end
    elsif Input.trigger?(Input::LEFT)
      # store old index and change index
      old_index = @alt_index[@index]
      @alt_index[@index] = (@alt_index[@index] + size - 1) % size
      # refresh if old index is different than current
      if old_index != @alt_index[@index]
        $game_system.se_play($data_system.cursor_se)
        draw_item(@index)
      end
    end
  end
  #----------------------------------------------------------------------------
  # update_help
  #  Updates the description window of the SR skill.
  #----------------------------------------------------------------------------
  def update_help
    @help_window.set_text(self.skill == nil ? '' : self.skill.description)
  end
  #----------------------------------------------------------------------------
  # skill
  #  This method accesses the SR skill ID from the complex inner data structure
  #  of the window.
  #----------------------------------------------------------------------------
  def skill
    return $data_skills[@skill_ids[@index][@alt_index[@index]]]
  end
  
end

#==============================================================================
# Window_ChaosDrive
#------------------------------------------------------------------------------
#  This window is a modification of the Window_Skill classto display CD skill
#  without skill cost.
#==============================================================================

class Window_ChaosDrive < Window_Skill
  
  #----------------------------------------------------------------------------
  # refresh
  #  This method overrides the original to display CD skills instead of normal
  #  skills.
  #----------------------------------------------------------------------------
  def refresh
    # remove bitmap
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    # get all skills
    @data = []
    @actor.cd_skills.each {|i| @data.push($data_skills[i]) if $data_skills[i] != nil}
    @item_max = @data.size
    # if skills exist
    if @item_max > 0
      # make display possible
      self.contents = Bitmap.new(width - 32, row_max * 32)
      if $fontface != nil
        self.contents.font.name = $fontface
        self.contents.font.size = $fontsize
      elsif $defaultfonttype != nil
        self.contents.font.name = $defaultfonttype
        self.contents.font.size = $defaultfontsize
      end
      # draw skill list
      (0...@item_max).each {|i| draw_item(i)}
    end
  end
  #----------------------------------------------------------------------------
  # draw_item
  #  i - index of the skill
  #  This method removes the SP cost.
  #----------------------------------------------------------------------------
  def draw_item(i)
    super
    self.contents.fill_rect(236+i%2*320, i/2*32, 48, 32, Color.new(0, 0, 0, 0))
  end
  
end

#==============================================================================
# Window_Command
#------------------------------------------------------------------------------
#  This class was modified to support the slide-from-the-right feature for
#  CDS, SRS and SLS.
#==============================================================================

class Window_Command
  
  attr_accessor :commands
  attr_reader   :actor
  #----------------------------------------------------------------------------
  # override initialize
  #----------------------------------------------------------------------------
  alias initialize_crls_later initialize
  def initialize(width, commands)
    initialize_crls_later(width, commands)
    @commands2 = [@commands[0], BlizzCFG::SRS_NAME, BlizzCFG::CDS_NAME]
  end
  #----------------------------------------------------------------------------
  # set_player_command
  #  new_actor - the new actor
  #  This method updates the actor in the window and all connected displays and
  #  data.
  #----------------------------------------------------------------------------
  def set_player_command(new_actor)
    # if old actor exists and command is limit
    if @actor != nil && @commands[0] == @actor.limit_name
      # put back old command
      @commands[0] = @commands2[0]
    end
    # set command to new actor's command
    @commands2[0] = new_actor.limit_name
    # store actor
    @actor = new_actor
    refresh
  end
  #----------------------------------------------------------------------------
  # swap_commands
  #  index - at which position
  #  This method changes the command in the window.
  #----------------------------------------------------------------------------
  def swap_commands(index)
    @commands[index], @commands2[index] = @commands2[index], @commands[index]
    refresh
  end
  #----------------------------------------------------------------------------
  # override refresh
  #  This method was completely modified to support the faster animated cursor
  #  and the moving animation.
  #----------------------------------------------------------------------------
  alias refresh_crls_later refresh
  def refresh
    # fotn fix
    if $fontface != nil
      self.contents.font.name = $fontface
      self.contents.font.size = $fontsize
    elsif $defaultfonttype != nil
      self.contents.font.name = $defaultfonttype
      self.contents.font.size = $defaultfontsize
    end
    # if actor exists
    if @actor != nil
      # if SLS usable and already command active
      if BlizzCFG::SLS_ACTIVE && BlizzCFG::SL_USERS.include?(@actor.id) &&
          @actor.sl_can_use?
        if self.index != 0 || @commands[0] != @actor.limit_name
          # draw the normal command
          draw_item(0, normal_color)
          # draw arrows
          self.contents.draw_text(4, -1, width-40, 32, '››', 2)
        elsif @commands[0] == @actor.limit_name
          # get color
          self.contents.font.color = BlizzCFG::LIMIT_COLOR
          # move the command in 6 steps
          (0...6).each {|j|
              # remove display
              self.contents.fill_rect(0, 0, width-32, 32, Color.new(0, 0, 0, 0))
              rect = Rect.new(164-j * 32, 0, width-40, 32)
              # draw command
              self.contents.draw_text(rect, @commands[0])
              Graphics.update}
        end
      else
        draw_item(0, normal_color)
      end
      # if SRS usable and already command active
      if BlizzCFG::SRS_ACTIVE && BlizzCFG::SR_USERS.include?(@actor.id)
        if self.index != 1 || @commands[1] != BlizzCFG::SRS_NAME
          # draw the normal command
          draw_item(1, normal_color)
          # draw arrows
          self.contents.draw_text(4, 31, width-40, 32, '››', 2)
        elsif @commands[1] == BlizzCFG::SRS_NAME
          # get color
          self.contents.font.color = BlizzCFG::RAGE_COLOR
          # move the command in 6 steps
          (0...6).each {|j|
              # remove display
              self.contents.fill_rect(0, 32, width-32, 32, Color.new(0, 0, 0, 0))
              rect = Rect.new(164-j * 32, 32, width-40, 32)
              # draw command
              self.contents.draw_text(rect, @commands[1])
              Graphics.update}
        end
      else
        draw_item(1, normal_color)
      end
      # if CDS usable and already command active
      if BlizzCFG::CDS_ACTIVE && BlizzCFG::CD_USERS.include?(@actor.id)
        if self.index != 2 || @commands[2] != BlizzCFG::CDS_NAME
          # draw the normal command
          draw_item(2, normal_color)
          # draw arrows
          self.contents.draw_text(4, 63, width-40, 32, '››', 2)
        elsif @commands[2] == BlizzCFG::CDS_NAME
          # move the command in 6 steps
          (0...6).each {|j|
              # remove display
              self.contents.fill_rect(0, 64, width-32, 32, Color.new(0, 0, 0, 0))
              rect = Rect.new(164-j * 32, 64, width-40, 32)
              # if already CD actor
              if @actor.states.include?(BlizzCFG::CD_ID)
                # show Revert command
                self.contents.font.color = BlizzCFG::REVERT_COLOR
                self.contents.draw_text(rect, BlizzCFG::REVERT_NAME)
              else
                # get color
                if @actor.cd_can_use?
                  self.contents.font.color = BlizzCFG::CHAOS_COLOR
                else
                  self.contents.font.color = disabled_color
                end
                # draw command
                self.contents.draw_text(rect, @commands[2])
              end
              Graphics.update}
        end
      else
        draw_item(2, normal_color)
      end
    else
      refresh_crls_later
    end
  end
  #----------------------------------------------------------------------------
  # test_limit
  #  This method serves for quick testing if SL is available.
  #----------------------------------------------------------------------------
  def test_limit
    return (@actor != nil && @actor.sl_can_use? && @index == 0)
  end
  #----------------------------------------------------------------------------
  # limit
  #  This method serves for SL name retrieval.
  #----------------------------------------------------------------------------
  def limit
    return (@actor != nil ? @actor.limit_name : '')
  end
  
end

#==============================================================================
# Scene_Battle
#------------------------------------------------------------------------------
#  This class was modified to allow the processing of CDS, SRS and SLS during
#  battle.
#==============================================================================

class Scene_Battle
  
  #----------------------------------------------------------------------------
  # override main
  #  This method was enhanced with window disposal.
  #----------------------------------------------------------------------------
  alias main_crls_later main
  def main
    main_crls_later
    [@rage_window, @chaos_window].each {|win| win.dispose if win != nil}
  end
  #----------------------------------------------------------------------------
  # phase3_setup_command_window
  #  This method was modified to update the command display.
  #----------------------------------------------------------------------------
  alias phase3_setup_command_window_crls_later phase3_setup_command_window
  def phase3_setup_command_window
    phase3_setup_command_window_crls_later
    # if using RTAB
    if BlizzCFG::RTAB_ACTIVE
      # RTAB compatible command setup
      @actor_command_window.set_player_command(@active_actor)
    else
      # command setup
      @actor_command_window.set_player_command(@active_battler)
    end
  end
  #----------------------------------------------------------------------------
  # update_phase3
  #  This method was modified to update SR and CD selection windows.
  #----------------------------------------------------------------------------
  alias update_phase3_crls_later update_phase3
  def update_phase3
    # if SR window exists and visible
    if @rage_window != nil && @rage_window.visible
      # update handling
      @rage_window.update unless defined?(SDK)
      update_phase3_rage_select
    # if CD window exists and visible
    elsif @chaos_window != nil && @chaos_window.visible
      # update handling
      @chaos_window.update unless defined?(SDK)
      update_phase3_chaos_select
    else
      update_phase3_crls_later
    end
  end
  #----------------------------------------------------------------------------
  # update_phase3_enemy_select
  #  This method was modified to additionally dispose the SR window if
  #  necessary.
  #----------------------------------------------------------------------------
  alias update_phase3_enemy_select_crls_later update_phase3_enemy_select
  def update_phase3_enemy_select
    end_rage_select_plus if Input.trigger?(Input::B)
    if BlizzCFG::RTAB_ACTIVE && Input.trigger?(Input::C)
      kind = @active_actor.current_action.kind
      update_phase3_enemy_select_crls_later
      if [BlizzCFG::SR_KIND, BlizzCFG::SL_KIND,
          BlizzCFG::CD_KIND].include?(kind)
        @active_actor.current_action.kind = kind
      end
    else
      update_phase3_enemy_select_crls_later
    end
  end
  #----------------------------------------------------------------------------
  # update_phase3_actor_select
  #  This method was modified to additionally dispose the SR window if
  #  necessary.
  #----------------------------------------------------------------------------
  alias update_phase3_actor_select_crls_later update_phase3_actor_select
  def update_phase3_actor_select
    end_rage_select_plus if Input.trigger?(Input::B)
    if BlizzCFG::RTAB_ACTIVE && Input.trigger?(Input::C)
      kind = @active_actor.current_action.kind
      update_phase3_actor_select_crls_later
      if [BlizzCFG::SR_KIND, BlizzCFG::SL_KIND,
          BlizzCFG::CD_KIND].include?(kind)
        @active_actor.current_action.kind = kind
      end
    else
      update_phase3_actor_select_crls_later
    end
  end
  #----------------------------------------------------------------------------
  # phase3_next_actor
  #  This method was modified to completely dispose the SR and CD windows if
  #  necessary.
  #----------------------------------------------------------------------------
  alias phase3_next_actor_crls_later phase3_next_actor
  def phase3_next_actor
    # remove the SR and CD windows if they exist
    end_rage_select if @rage_window != nil
    end_chaos_select if @chaos_window != nil
    phase3_next_actor_crls_later
  end
  #----------------------------------------------------------------------------
  # update_phase3_rage_select
  #  This method handles the input behaviour when being in the SR menu.
  #----------------------------------------------------------------------------
  def update_phase3_rage_select
    # RTAB compatibility
    battler = (BlizzCFG::RTAB_ACTIVE ? @active_actor : @active_battler)
    # if B is being pressed
    if Input.trigger?(Input::B)
      # exit SR selection
      $game_system.se_play($data_system.cancel_se)
      end_rage_select if @rage_window.visible
    # if C is being pressed
    elsif Input.trigger?(Input::C) 
      @skill = @rage_window.skill
      # if SR skill doesn't exist or actor can't use
      if @skill == nil || !battler.sr_can_use?(@skill.id)
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # prepare skill use process
      $game_system.se_play($data_system.decision_se)
      battler.current_action.skill_id = @skill.id
      @rage_window.visible = false
      if @skill.scope == 1
        start_enemy_select
      elsif [3, 5].include?(@skill.scope)
        start_actor_select
      else
        phase3_next_actor
      end
    end
  end
  #----------------------------------------------------------------------------
  # update_phase3_chaos_select
  #  This method handles the input behaviour when being in the CD menu.
  #----------------------------------------------------------------------------
  def update_phase3_chaos_select
    # RTAB compatibility
    battler = (BlizzCFG::RTAB_ACTIVE ? @active_actor : @active_battler)
    # if B is being pressed
    if Input.trigger?(Input::B)
      # exit CD selection
      $game_system.se_play($data_system.cancel_se)
      end_chaos_select if @chaos_window.visible
    # if C is being pressed
    elsif Input.trigger?(Input::C) 
      @skill = @chaos_window.skill
      # if CD skill doesn't exist or actor can't use
      if @skill == nil || !battler.cd_can_use?
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # prepare skill use process
      $game_system.se_play($data_system.decision_se)
      battler.current_action.skill_id = @skill.id
      @chaos_window.visible = false
      phase3_next_actor
    end
  end
  #----------------------------------------------------------------------------
  # start_rage_select
  #  This method creates an SR window and sets it active.
  #----------------------------------------------------------------------------
  def start_rage_select
    # RTAB compatibility
    battler = (BlizzCFG::RTAB_ACTIVE ? @active_actor : @active_battler)
    # create and set up window
    @rage_window = Window_SoulRage.new(battler)
    @rage_window.help_window = @help_window
    @actor_command_window.active = @actor_command_window.visible = false
  end
  #----------------------------------------------------------------------------
  # end_rage_select
  #  This method removes the SR window and returns to the command menu.
  #----------------------------------------------------------------------------
  def end_rage_select
    end_rage_select_plus
    @actor_command_window.swap_commands(1)
    @rage_window.dispose
    @rage_window, @help_window.visible = nil, false
  end
  #----------------------------------------------------------------------------
  # end_rage_select_plus
  #  This method triggers the SR window and the other windows visible.
  #----------------------------------------------------------------------------
  def end_rage_select_plus
    # if SR window exists
    if @rage_window != nil
      if @rage_window.visible
        # set command window to active
        @actor_command_window.active = @actor_command_window.visible = true
        @help_window.visible = false
      else
        # set SR window to active and make visible
        @rage_window.active = @rage_window.visible = true
      end
    end
  end
  #----------------------------------------------------------------------------
  # start_rage_select
  #  This method creates a CD window and sets it active.
  #----------------------------------------------------------------------------
  def start_chaos_select
    # RTAB compatibility
    battler = (BlizzCFG::RTAB_ACTIVE ? @active_actor : @active_battler)
    # create and set up window
    @chaos_window = Window_ChaosDrive.new(battler)
    @chaos_window.help_window = @help_window
    @actor_command_window.active = @actor_command_window.visible = false
  end
  #----------------------------------------------------------------------------
  # end_chaos_select
  #  This method removes the CD window and returns to the command menu.
  #----------------------------------------------------------------------------
  def end_chaos_select
    end_chaos_select_plus
    @actor_command_window.swap_commands(2)
    @chaos_window.dispose
    @chaos_window, @help_window.visible = nil, false
  end
  #----------------------------------------------------------------------------
  # end_rage_select_plus
  #  This method triggers the CD window and the other windows visible.
  #----------------------------------------------------------------------------
  def end_chaos_select_plus
    # if SR window exists
    if @chaos_window != nil
      if @chaos_window.visible
        # set command window to active
        @actor_command_window.active = @actor_command_window.visible = true
        @help_window.visible = false
      else
        # set CD window to active and make visible
        @chaos_window.active = @chaos_window.visible = true
      end
    end
  end
  # if using RTAB
  if false && BlizzCFG::RTAB_ACTIVE
  #----------------------------------------------------------------------------
  # override make_skill_action_result
  #  This method overrides Connected Attacking.
  #----------------------------------------------------------------------------
  alias make_skill_action_result_crls_later make_skill_action_result
  def make_skill_action_result(battler)
    make_skill_action_result_crls_later(battler)
  end
  end
  #----------------------------------------------------------------------------
  # make_sl_action_result
  #  battler - the currently active battler
  #  This method processes the scene based part of SL use.
  #----------------------------------------------------------------------------
  def make_sl_action_result(battler)
    @skill = $data_skills[battler.current_action.skill_id]
    # if using Tons of Add-ons
    if !BlizzCFG::RTAB_ACTIVE && $tons_version != nil && $tons_version >= 5.0
      # stat consumption when using the first attack
      battler.use_limit if @repeat[0] <= 1
    else
      # stat consumption
      battler.use_limit
    end
    make_crls_postaction_result(battler)
  end
  #----------------------------------------------------------------------------
  # make_rage_action_result
  #  battler - the currently active battler
  #  This method processes the scene based part of SR use.
  #----------------------------------------------------------------------------
  def make_rage_action_result(battler)
    @skill = $data_skills[battler.current_action.skill_id]
    # if not forcing and not able to use
    unless battler.current_action.forcing || battler.sr_can_use?(@skill.id)
      # abort
      $game_temp.forcing_battler = nil
      @phase4_step = 1
      return
    end
    # if using Tons of Add-ons
    if !BlizzCFG::RTAB_ACTIVE && $tons_version != nil && $tons_version >= 5.0
      # SR consumption when using the first attack
      battler.sr -= @skill.sp_cost * 10 if @repeat[0] <= 1
    else
      # SR consumption
      battler.sr -= @skill.sp_cost * 10
    end
    make_crls_postaction_result(battler)
  end
  #----------------------------------------------------------------------------
  # make_chaos_action_result
  #  battler - the currently active battler
  #  This method processes the scene based part of CD use.
  #----------------------------------------------------------------------------
  def make_chaos_action_result(battler)
    @skill = $data_skills[battler.current_action.skill_id]
    # if skill exists
    unless @skill == nil
      # set CD action
      battler.chaos_action = 1
      make_crls_postaction_result(battler)
    end
  end
  #----------------------------------------------------------------------------
  # make_crls_postaction_result
  #  battler - the currently active battler
  #  This method processes the mutual part of CD, SR and SL use.
  #----------------------------------------------------------------------------
  def make_crls_postaction_result(battler)
    # if using Tons of Add-ons and HP Consuming Skills
    if $tons_version != nil && $tons_version >= 7.2 && $game_system.HP_SKILL
      # subtract HP cost
      battler.hp -= @skill.hp_cost if @phase != 1 && @skill.hp_cost > 0
    end
    @status_window.refresh
    @help_window.set_text(@skill.name, 1)
    # set animations
    @animation1_id, @animation2_id = @skill.animation1_id, @skill.animation2_id
    @common_event_id = @skill.common_event_id
    # if using RTAB
    if BlizzCFG::RTAB_ACTIVE
      # set targets and execute
      set_target_battlers(@skill.scope, battler)
      battler.target.each {|target| target.skill_effect(battler, @skill)}
    else
      # set targets and execute
      set_target_battlers(@skill.scope)
      @target_battlers.each {|target| target.skill_effect(battler, @skill)}
    end
  end
  #----------------------------------------------------------------------------
  # override update_phase4_step2
  #  battler - the currently active battler
  #  This method was enhanced with handling the new types of action.
  #----------------------------------------------------------------------------
  alias update_phase4_step2_crls_later update_phase4_step2
  def update_phase4_step2(battler = nil)
    # if using RTAB
    if BlizzCFG::RTAB_ACTIVE
      # call original method
      update_phase4_step2_crls_later(battler)
    else
      # call original method
      update_phase4_step2_crls_later
      # other battler
      battler = @active_battler
    end
    # decide action result
    case battler.current_action.kind
    when BlizzCFG::SR_KIND then make_rage_action_result(battler)
    when BlizzCFG::CD_KIND then make_chaos_action_result(battler)
    when BlizzCFG::SL_KIND then make_sl_action_result(battler)
    end
  end
  #----------------------------------------------------------------------------
  # override update_phase3_basic_command
  #  This method was enhanced with advanced selection control of the
  #  slide-from-the-right commands.
  #----------------------------------------------------------------------------
  alias update_phase3_basic_command_crls_later update_phase3_basic_command
  def update_phase3_basic_command
    # RTAB compatibility
    battler = (BlizzCFG::RTAB_ACTIVE ? @active_actor : @active_battler)
    # restore commands
    restore_commands(battler)
    # stop if SLS activated
    return if update_sls_input(battler)
    # stop if SRS activated
    return if update_srs_input(battler)
    # stop if CDS activated
    return if update_cds_input(battler)
    # RTAB left pressing limitation
    if !BlizzCFG::RTAB_ACTIVE || !Input.press?(Input::LEFT) && !Input.press?(Input::RIGHT)
      # call original method
      update_phase3_basic_command_crls_later
    end
  end
  #----------------------------------------------------------------------------
  # restore_commands
  #  battler - the currently active battler
  #  Sets up the commands displayed.
  #----------------------------------------------------------------------------
  def restore_commands(battler)
    # if able to use SLS, but not selected
    if BlizzCFG::SLS_ACTIVE && BlizzCFG::SL_USERS.include?(battler.id) &&
        @actor_command_window.index != 0 &&
        @actor_command_window.commands[0] == @actor_command_window.limit
      # reset command
      @actor_command_window.swap_commands(0)
      @help_window.set_text('', 1)
      @help_window.visible = false
    end
    # if able to use SRS, but not selected
    if BlizzCFG::SRS_ACTIVE && BlizzCFG::SR_USERS.include?(battler.id) &&
        @actor_command_window.index != 1 &&
        @actor_command_window.commands[1] == BlizzCFG::SRS_NAME
      # reset command
      @actor_command_window.swap_commands(1)
    end
    # if able to use CDS, but not selected
    if BlizzCFG::CDS_ACTIVE && BlizzCFG::CD_USERS.include?(battler.id) &&
        @actor_command_window.index != 2 &&
        @actor_command_window.commands[2] == BlizzCFG::CDS_NAME
      # reset command
      @actor_command_window.swap_commands(2)
    end
  end
  #----------------------------------------------------------------------------
  # update_sls_input
  #  battler - the currently active battler
  #  Sets up the command window data.
  #----------------------------------------------------------------------------
  def update_sls_input(battler)
    # if able to use SLS
    if BlizzCFG::SLS_ACTIVE && BlizzCFG::SL_USERS.include?(battler.id)
      # if able to use SL and RIGHT is being hold
      if @actor_command_window.test_limit && Input.press?(Input::RIGHT)
        # if command not visible
        if @actor_command_window.commands[0] != @actor_command_window.limit
          # make it visible
          $game_system.se_play($data_system.decision_se)
          skill = $data_skills[@actor_command_window.actor.limit_id]
          @help_window.set_text(skill.description, 1)
          @actor_command_window.swap_commands(0)
        end
        # prevent going crazy when pressing UP or DOWN
        if !Input.press?(Input::UP) && !Input.press?(Input::DOWN)
          @actor_command_window.update
        end
        # RTAB fix
        abort = true
      # if command visible
      elsif @actor_command_window.commands[0] == @actor_command_window.limit
        # change back
        $game_system.se_play($data_system.cancel_se)
        @actor_command_window.swap_commands(0)
        @help_window.set_text('', 1)
        @help_window.visible = false
      end
      # if command visible and C is being pressed
      if @actor_command_window.commands[0] == @actor_command_window.limit &&
          Input.trigger?(Input::C)
        # execute SL use setup
        $game_system.se_play($data_system.decision_se)
        battler.current_action.kind = BlizzCFG::SL_KIND
        battler.current_action.skill_id = @actor_command_window.actor.limit_id
        @skill = $data_skills[battler.current_action.skill_id]
        @help_window.visible = false
        # depending on the skill specsifications, allow target selection
        if @skill.scope == 1
          start_enemy_select
          @actor_command_window.swap_commands(0)
        elsif [3, 5].include?(@skill.scope)
          start_actor_select
          @actor_command_window.swap_commands(0)
        else
          @actor_command_window.swap_commands(0)
          phase3_next_actor
        end
        return true
      end
      # abort if RTAB to prevent character change
      return true if abort
    end
  end
  #----------------------------------------------------------------------------
  # update_srs_input
  #  battler - the currently active battler
  #  Sets up the command window data.
  #----------------------------------------------------------------------------
  def update_srs_input(battler)
    # if able to use SRS
    if BlizzCFG::SRS_ACTIVE && BlizzCFG::SR_USERS.include?(battler.id)
      # if able to use SR and RIGHT is being hold
      if @actor_command_window.index == 1 && Input.press?(Input::RIGHT)
        # if command not visible
        if @actor_command_window.commands[1] != BlizzCFG::SRS_NAME
          # make it visible
          $game_system.se_play($data_system.decision_se)
          @actor_command_window.swap_commands(1)
        end
        # prevent going crazy when pressing UP or DOWN
        if !Input.press?(Input::UP) && !Input.press?(Input::DOWN)
          @actor_command_window.update
        end
        # RTAB fix
        abort = true
      # if command visible
      elsif @actor_command_window.commands[1] == BlizzCFG::SRS_NAME
        # change back
        $game_system.se_play($data_system.cancel_se)
        @actor_command_window.swap_commands(1)
      end
      # if command visible and C is being pressed
      if @actor_command_window.commands[1] == BlizzCFG::SRS_NAME &&
          Input.trigger?(Input::C)
        # execute SR use setup
        $game_system.se_play($data_system.decision_se)
        battler.current_action.kind = BlizzCFG::SR_KIND
        start_rage_select
        return true
      end
      # abort if RTAB to prevent character change
      return true if abort
    end
  end
  #----------------------------------------------------------------------------
  # update_cds_input
  #  battler - the currently active battler
  #  Sets up the command window data.
  #----------------------------------------------------------------------------
  def update_cds_input(battler)
    # if able to use CDS
    if BlizzCFG::CDS_ACTIVE && BlizzCFG::CD_USERS.include?(battler.id)
      # if able to use CD and RIGHT is being hold
      if @actor_command_window.index == 2 && Input.press?(Input::RIGHT)
        # if command not visible
        if @actor_command_window.commands[2] != BlizzCFG::CDS_NAME
          # make it visible
          $game_system.se_play($data_system.decision_se)
          @actor_command_window.swap_commands(2)
        end
        # prevent going crazy when pressing UP or DOWN
        if !Input.press?(Input::UP) && !Input.press?(Input::DOWN)
          @actor_command_window.update
        end
        # RTAB fix
        abort = true
      # if command visible
      elsif @actor_command_window.commands[2] == BlizzCFG::CDS_NAME
        # change back
        $game_system.se_play($data_system.cancel_se)
        @actor_command_window.swap_commands(2)
      end
      # if command visible and C is being pressed
      if @actor_command_window.commands[2] == BlizzCFG::CDS_NAME &&
          Input.trigger?(Input::C)
        unless @actor_command_window.actor.states.include?(BlizzCFG::CD_ID)
          # if can use CD
          if @actor_command_window.actor.cd_can_use?
            # execute CD use setup
            $game_system.se_play($data_system.decision_se)
            battler.current_action.kind = BlizzCFG::CD_KIND
            start_chaos_select
          else
            $game_system.se_play($data_system.buzzer_se)
          end
        else
          # execute CD revert setup
          $game_system.se_play($data_system.decision_se)
          battler.current_action.kind = BlizzCFG::CD_KIND
          battler.chaos_action = 2
          @actor_command_window.swap_commands(2)
          phase3_next_actor
        end
        return true
      end
      # abort if RTAB to prevent character change
      return true if abort
    end
  end
  #----------------------------------------------------------------------------
  # override update_phase4_step4
  #  battler - the currently active battler
  #  This method was enhanced to add the "Chaos Drive" popup text when using a
  #  CD skill.
  #----------------------------------------------------------------------------
  alias update_phase4_step4_crls_later update_phase4_step4
  def update_phase4_step4(battler = nil)
    # if using RTAB
    if BlizzCFG::RTAB_ACTIVE
      # for each target
      battler.target.each {|target|
          # if target is actor and action is set to transform
          if target.is_a?(Game_Actor) && target.chaos_action == 1
            # set damage to "Chaos Drive!"
            target.damage = BlizzCFG::CDS_NAME + '!'
          end}
      # call original method
      update_phase4_step4_crls_later(battler)
    else
      # for each target
      @target_battlers.each {|target|
          # if target is actor and action is set to transform
          if target.is_a?(Game_Actor) && target.chaos_action == 1
            # set damage to "Chaos Drive!"
            target.damage = BlizzCFG::CDS_NAME + '!'
          end}
      # call original method
      update_phase4_step4_crls_later
    end
  end
  #----------------------------------------------------------------------------
  # override update_phase4_step6
  #  battler - the currently active battler
  #  This method was enhanced to additionally process CD actions.
  #----------------------------------------------------------------------------
  alias update_phase4_step6_crls_later update_phase4_step6
  def update_phase4_step6(battler = nil)
    # if using RTAB
    if BlizzCFG::RTAB_ACTIVE
      # call original method
      update_phase4_step6_crls_later(battler)
    else
      # call original method
      update_phase4_step6_crls_later
    end
    flag = false
    # for each actor
    $game_party.actors.each {|actor|
        # if valid chaos action
        if [1, 2].include?(actor.chaos_action)
          # store index
          index = actor.index
          # execute action
          case actor.chaos_action
          when 1 then actor.use_chaos(actor.current_action.skill_id)
          when 2 then actor.revert_chaos
          end
          # set up a new battler if using RTAB
          crls_rtab_setup($game_party.actors[index]) if BlizzCFG::RTAB_ACTIVE
          # transformation has occured
          flag = true
        end}
    # action executed
    if flag
      # update status window
      @status_window.refresh
      # update spriteset
      @spriteset.update_actors
    end
  end
  #----------------------------------------------------------------------------
  # crls_rtab_setup
  #  battler - the new battler
  #  This method sets up all necessary parameters for RTAB.
  #----------------------------------------------------------------------------
  def crls_rtab_setup(battler)
    # set up a new battler
    spell_reset(battler)
    battler.at = 0
    battler.damage_pop = {}
    battler.damage = {}
    battler.damage_sp = {}
    battler.critical = {}
    battler.recover_hp = {}
    battler.recover_sp = {}
    battler.state_p = {}
    battler.state_m = {}
    battler.animation = []
    battler.atp = 100 * battler.at / @max
    # if removing the party
    if BlizzCFG::REMOVE_PARTY
      # recreate status window
      @status_window.dispose
      @status_window = Window_BattleStatus.new
    end
  end
  #----------------------------------------------------------------------------
  # exit_chaos
  #  dead - flag to determine whether dead or simply transforming back
  #  This method processes the data that allows actors to return to their
  #  normal form.
  #----------------------------------------------------------------------------
  def exit_chaos(dead = true)
    flag = true
    # for all actors in the party
    $game_party.actors.clone.each {|actor|
        # if CD actor and either reverting or really dead
        if actor.states.include?(BlizzCFG::CD_ID) && (dead || actor.dead?)
          # revert action
          actor.revert_chaos
          actor.recover_all unless BlizzCFG::STAY_DAMAGE
          flag = false
        end}
    # refresh status window if changes occured
    @status_window.refresh unless flag
    return flag
  end
  #----------------------------------------------------------------------------
  # override judge
  #  This method was modified to transform actors back upon death and disallow
  #  losing a battle without the real actors' deaths.
  #----------------------------------------------------------------------------
  alias judge_crls_later judge
  def judge
    # revert Chaos Drive if necessary
    result = exit_chaos(false)
    # refresh the status window
    @status_window.refresh
    # update the sprites
    @spriteset.update_actors(true)
    return (result && judge_crls_later)
  end
  #----------------------------------------------------------------------------
  # override start_phase5
  #  This method was modified to transform actors back before result display.
  #----------------------------------------------------------------------------
  alias start_phase5_crls_later start_phase5
  def start_phase5
    exit_chaos
    @spriteset.update_actors
    # update the sprites
    start_phase5_crls_later
  end
  #----------------------------------------------------------------------------
  # override battle_end
  #  result - battle result
  #  This method was modified to transform actors back before leaving the
  #  battle scene.
  #----------------------------------------------------------------------------
  alias battle_end_crls_later battle_end
  def battle_end(result)
    exit_chaos
    # update the sprites
    @spriteset.update_actors(true)
    # for convenience
    $game_player.refresh
    battle_end_crls_later(result)
  end
  
end
