#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# Soul Force Combo System by Blizzard
# Version: 1.2b
# Type: Combo Enhancement for SRS
# Date: 25.10.2007
# Date v1.2b: 28.1.2008
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#   
#  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.
# #  
# #----------------------------------------------------------------------------
# 
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# 
# Compatibility:
# 
#   96% compatible with SDK 1.x. 40% 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 5.x version of the Scene_SoulRage add-on if you use one
#   This script requires Chaos Rage Limit System v5.3b or higher to work. SRS
#   can be deactivated, but doesn't have to. This system does NOT support the
#   Overload feature.
# 
# 
# new in 1.2b:
#   - now able to display SR cost as well
#   - now you are able to create combo skills that can be used by only one
#     actor, by any actor without much effort
# 
# 
# Explanation:
#   
#   This Script allows you to use a combo system derivate from Unity Force
#   which was originally created for Chaos Project.
# 
# 
# Instructions:
#   
#   To use this system you need to set up the database below using the given
#   template. When the first actor uses a skill, the second one won't be able
#   to act at all. When the second actor uses a skill, the first one's action
#   will be cancelled. Both actors will lose SR upon skill use, but only the
#   actor who used the skill will lose SP. Keep in mind that the actor who used
#   the skill will be the one who performs it.
#   
#   It is recommended that both actors know the skill and that an actor who
#   can't perform the skill doesn't ever learn it.
# 
# 
# Configuration:
# 
#   DESCRIPTION_ADD_NAME - if you set this value to true, the names of the
#                          performing actors will be displayed in the
#                          description of the skill
#   DESCRIPTION_ADD_SR   - if you set this value to true, the SR cost will be
#                          displayed in the description of the skill
# 
# 
# If you find any bugs, please report them here:
# http://forum.chaos-project.com
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

DESCRIPTION_ADD_NAME = true
DESCRIPTION_ADD_COST = true

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

if $crls == nil || $crls == true || $crls < 5.31
  raise 'Soul Force Combo System requires Chaos Rage Limit System v5.31b or higher'
end

#==============================================================================
# RPG::Skill
#==============================================================================

class RPG::Skill
  
  alias description_sfc_later description
  def description
    data = BlizzCFG.sfc_database(@id)
    if data != nil
      text = @description
      text += " (#{data[0]}% #{BlizzCFG::SR_NAME})" if DESCRIPTION_ADD_COST
      if DESCRIPTION_ADD_NAME && data.size > 1
        text += " (#{$game_actors[data[1]].name}"
        data[2, data.size-2].each {|i| text += ", #{$game_actors[i].name}"}
        text += ')'
      end
      return text
    end
    return @description
  end
  
end

#==============================================================================
# module BlizzCFG
#==============================================================================

module BlizzCFG
  
  def self.sfc_database(id)
    case id
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Soul Force Database
# 
#   To set up the configuration correctly use following template:
#   
#   when ID then return [SR_COST, AC_ID1, AC_ID2, ...]
# 
#   ID      - skill ID
#   SR_COST - SR consumed by each actor if used
#   AC_ID1  - ID of actor 1
#   AC_ID2  - ID of actor 2
#   ...     - more actor IDs
#   
#   If you add more than 2 IDs, the skill will simply require more actors to
#   work out. Keep in mind that the SR cost is in %, that means that i.e.
#   64 means 64%, 25 means 25%, etc. If you use only 1 actor ID, only one actor
#   is needed to perform the skill.
#   
# Note:
#   
#   If you leave out the actor IDs, the skill will be usable by any actor. In
#   other words, you can create normal skills that can consume SR as well.
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    when 1 then return [10]
    when 57 then return [20, 1, 2]
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Soul Force Database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    end
    return nil
  end
  
end


#==============================================================================
# Game_Actor
#==============================================================================

class Game_Actor < Game_Battler
  
  alias skill_can_use_sfc_later? skill_can_use?
  def skill_can_use?(id)
    data = BlizzCFG.sfc_database(id)
    return skill_can_use_sfc_later?(id) && (data == nil || data.size == 1 &&
        data[0] * 10 <= @sr || data.size > 1 && $game_party.sfc_can_use?(id))
  end
  
  alias inputable_sfc_later? inputable?
  def inputable?
    return (inputable_sfc_later? && !$game_party.sfc_block.include?(self))
  end
  
end

#==============================================================================
# Game_Party
#==============================================================================

class Game_Party
  
  attr_accessor :sfc_block
  
  alias init_sfc_later initialize
  def initialize
    init_sfc_later
    @sfc_block = []
  end
  
  def sfc_can_use?(id)
    data = BlizzCFG.sfc_database(id)
    return data[1, data.size-1].all? {|id|
        $game_party.actors.include?($game_actors[id]) &&
        !$game_actors[id].dead? && data[0] * 10 <= $game_actors[id].sr}
  end
  
end

#==============================================================================
# Scene_Battle
#==============================================================================

class Scene_Battle
  
  alias start_phase2_sfc_later start_phase2
  def start_phase2
    $game_party.sfc_block = []
    start_phase2_sfc_later
  end
  
  def make_sfc_action_result
    make_skill_action_result
    if @active_battler.current_action.forcing ||
        @active_battler.skill_can_use?(@skill.id)
      data = BlizzCFG.sfc_database(@skill.id)
      if data.size == 1
        @active_battler.sr -= data[0] * 10
      else
        data[1, data.size-1].each {|id| $game_actors[id].sr -= data[0] * 10}
      end
      @status_window.refresh
    end
  end
  
  alias end_skill_select_sfc_later end_skill_select
  def end_skill_select
    end_skill_select_sfc_later
    data = BlizzCFG.sfc_database(@skill.id)
    if Input.trigger?(Input::C) && @skill != nil &&
          @active_battler.skill_can_use?(@skill.id) && data != nil
      @active_battler.current_action.kind = 6
      if data.size > 1
        data[1, data.size-1].each {|id| $game_party.sfc_block.push($game_actors[id])}
        $game_party.sfc_block.delete(@active_battler)
        $game_party.sfc_block.each {|a| a.current_action.clear}
      end
    end
  end
  
  alias phase3_prior_actor_sfc_later phase3_prior_actor
  def phase3_prior_actor
    phase3_prior_actor_sfc_later
    if @active_battler != nil && @active_battler.current_action.kind == 6
      @active_battler.current_action.clear
      $game_party.sfc_block = []
    end
  end
  
  alias upd_phase4_step2_sfc_later update_phase4_step2
  def update_phase4_step2
    upd_phase4_step2_sfc_later
    make_sfc_action_result if @active_battler.current_action.kind == 6
  end
  
end
