#==============================================================================
# ■ 附加状态后 普通攻击消耗SP
#------------------------------------------------------------------------------
#  使用方法:在状态名字里写[SP消费 xxx]来使用
#   例: 毒[SP消费 50]    => 每次普通攻击消耗50SP
#        沉默[SP消费 50%] => 每次普通攻击消耗一半SP
#   备注:有多个状态时,优先计算百分比扣除,再计算定值扣除。
#         多个百分比扣除会取最大百分比。
#==============================================================================

class RPG::State
  
  SP_COST_FORBID_ATTACK = true
  # SP不足时,是否导致无法普通攻击
  
  REG_SP_COST_STATE = /\[SP消费[ ]*(\d+%?)\]/i
  # 不要编辑这个
  
  alias name_for_sp_cost_state name
  def name
    name_for_sp_cost_state.gsub(REG_SP_COST_STATE, '')
  end

  def sp_cost_state?
    name_for_sp_cost_state =~ REG_SP_COST_STATE
  end
  
  def sp_cost_percentage?
    (name_for_sp_cost_state =~ REG_SP_COST_STATE) ? $1.include?("%") : false
  end
  
  def sp_cost
    (name_for_sp_cost_state =~ REG_SP_COST_STATE) ? $1.gsub("%", '').to_i : 0
  end
  
end

module Sound
  def self.play_buzzer
    $game_system.se_play($data_system.buzzer_se)
  end
end

class Game_Actor
  def attack_sp_enough?
    return true if !RPG::State::SP_COST_FORBID_ATTACK
    return true if [email protected]{|i|$data_states[i]}.any?{|s|s.sp_cost_state?}
    @sp >= attack_sp_cost
  end
  
  def attack_sp_cost
    cost = 0
    sp_cost_states = @states.map{|i|$data_states[i]}.select{|s|s.sp_cost_state?}
    return cost if sp_cost_states.empty?
    # 百分比消耗
    sp_percentage_states = sp_cost_states.select{|s|s.sp_cost_percentage?}
    if !sp_percentage_states.empty?
      max_cost_percentage = sp_percentage_states.sort_by{|s|s.sp_cost}.first.sp_cost
      max_cost_percentage = [max_cost_percentage.to_i, 100].min
      cost += @sp * (100 - max_cost_percentage) / 100
    end
    # 固定值消耗
    sp_states = sp_cost_states - sp_percentage_states
    if !sp_states.empty?
      cost += sp_states.collect{|s|s.sp_cost}.inject{|s,n|s+n}
    end
    return cost
  end
end

class Scene_Battle
  #--------------------------------------------------------------------------
  # ● 刷新画面
  #--------------------------------------------------------------------------
  alias cld99_update update
  def update
    if @actor_command_window && @active_battler && @active_battler.is_a?(Game_Actor)
      if !@active_battler.attack_sp_enough? 
        @actor_command_window.disable_item(0)
      else 
        @actor_command_window.refresh
      end
    end
    cld99_update
  end
  #--------------------------------------------------------------------------
  # ● 刷新画面 (角色命令回合 : 基本命令)
  #--------------------------------------------------------------------------
  alias cld99_update_phase3_basic_command update_phase3_basic_command
  def update_phase3_basic_command
    if Input.trigger?(Input::C)
      if @actor_command_window.index == 0 #攻击
        return Sound.play_buzzer if !@active_battler.attack_sp_enough?
      end
    end
    cld99_update_phase3_basic_command
  end
  #--------------------------------------------------------------------------
  # ● 生成基本行动结果
  #--------------------------------------------------------------------------
  alias make_basic_action_result_for_sp_cost make_basic_action_result
  def make_basic_action_result
    # 攻击的情况下
    if @active_battler.current_action.basic == 0
      @active_battler.sp -= @active_battler.attack_sp_cost if @active_battler.is_a?(Game_Actor)
    end
    make_basic_action_result_for_sp_cost
  end
end