 #06/11/05
#==============================================================================
# ■ Game_Battler
#==============================================================================
class Game_Battler
  include HIDE_ALL_PAGE
  attr_accessor :counter                  # [カウンター]発動フラグ
  attr_accessor :user_attacker            # 攻撃してくる相手を取得
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  alias hidesp_counter_initialize initialize
  def initialize
    # 元の処理を開始
    hidesp_counter_initialize
    @counter = [false,false,false,0,0,0,0,0]
    @user_attacker = nil
  end
  #--------------------------------------------------------------------------
  # ■ 通常攻撃の効果適用
  #--------------------------------------------------------------------------
  alias hidesp_counter_attack_effect attack_effect
  def attack_effect(attacker)
    # □行動できない場合は無効
    if self.restriction > 2
      self.user_attacker = nil
    else
      self.user_attacker = attacker
    end
    # 元の処理を開始
    hidesp_counter_attack_effect(attacker)
  end
  #--------------------------------------------------------------------------
  # ■ スキルの効果適用
  #--------------------------------------------------------------------------
  alias hidesp_counter_skill_effect skill_effect
  def skill_effect(user, skill)
    # □全体攻撃は無効/行動できない場合は無効
    if skill.scope == 2 or self.restriction > 2
      self.user_attacker = nil
    else
      self.user_attacker = user
    end
    # 元の処理を開始
    hidesp_counter_skill_effect(user, skill)
  end
  #--------------------------------------------------------------------------
  # ■ ステート衝撃解除 (物理ダメージごとに呼び出し)
  #  [カウンター]発動条件の確認
  #--------------------------------------------------------------------------
  alias hidesp_counter_remove_states_shock remove_states_shock
  def remove_states_shock
    if self.user_attacker == nil
      # 元の処理を開始
      hidesp_counter_remove_states_shock
      return
    end
    # ■パーティーアタック、自分自身に攻撃の場合、カウンター無効
    if self.user_attacker == self or 
      (self.is_a?(Game_Enemy) && self.user_attacker.is_a?(Game_Enemy)) or 
      (self.is_a?(Game_Actor) && self.user_attacker.is_a?(Game_Actor))
      # 元の処理を開始
      hidesp_counter_remove_states_shock
      return
    end
    #…………………………………………………………………………………………………
    # ■ [カウンター]発生条件
    #…………………………………………………………………………………………………
    #@『反撃』ステートの場合
    #A『カウンター』攻撃以外(カウンターにカウンターは無効)
    #B『防御』以外(防御中は無効)
    #C『全体攻撃』は無効
    #…………………………………………………………………………………………………
    if State?(self,"反撃") && self.user_attacker.counter[1] == false && ! self.guarding?
      # ■[カウンター]発生率
      counter_rata = 1
      #…………………………………………………………………………………………………
      # ■属性攻撃補正
      #…………………………………………………………………………………………………
      if Element?(self,"パンチ")
        #『パンチ』属性を含む[武器攻撃]の場合、カウンター発生率UP!
        counter_rata += 1
      end
      #…………………………………………………………………………………………………
      # ■装備品補正
      #…………………………………………………………………………………………………
      if Equipping?(self,4,"ブラックベルト")
        #『ブラックベルト』を装備しているならば、カウンター発生率UP!
        counter_rata += 2
      end
      #…………………………………………………………………………………………………
      # ■[カウンター]発生確率の計算
      #…………………………………………………………………………………………………
      if rand(self.dex * rand(10)) < (self.user_attacker.dex * rand(10)) * counter_rata
        self.critical = false
        self.damage = 0
        # □[反撃]フラグをON
        self.counter[0] = true
      else
        # ■[カウンター]失敗の場合、受けるダメージ120%
        self.damage = self.damage * 6 / 5
      end
    elsif self.user_attacker.counter[1] == true
      # ■[カウンター]攻撃の場合、与えるダメージ75%
      self.damage = self.damage * 3 / 4
    end
    # 元の処理を開始
    hidesp_counter_remove_states_shock
  end
  #--------------------------------------------------------------------------
  # ■ ステート変化 (+) の適用
  #  [カウンター]発生の確認
  #--------------------------------------------------------------------------
  alias hidesp_counter_states_plus states_plus
  def states_plus(plus_state_set)
    # ■[カウンター]発生時に表示する文字
    if self.counter[0] == true
      self.damage = "counter!"
    end
    # 元の処理を開始
    hidesp_counter_states_plus(plus_state_set)
  end
end
#==============================================================================
# ■ Scene_Battle
#==============================================================================
class Scene_Battle
  #--------------------------------------------------------------------------
  # ■ フレーム更新 (メインフェーズ ステップ 5 : ダメージ表示)
  #--------------------------------------------------------------------------
  alias hidesp_counter_update_p4s5 update_phase4_step5
  def update_phase4_step5
    
    if @target_battlers[0] == nil
      # 元の処理を開始
      hidesp_counter_update_p4s5
      return
    end
    # □全体攻撃は無効
    if @target_battlers.size != 1
      # 元の処理を開始
      hidesp_counter_update_p4s5
      return
    end
    # ■[カウンター]条件を満たす場合
    if (@target_battlers[0].counter[0] == true && @target_battlers[0].counter[1] == false) or 
      @active_battler.counter[1] == true
      # ヘルプウィンドウを隠す
      @help_window.visible = false
      # ステータスウィンドウをリフレッシュ
      @status_window.refresh
      # □[カウンター]の確認
      counter_action(@target_battlers[0])
      @target_battlers[0].damage_pop = true
      # □[カウンター]の開始
      if @active_battler.counter[1] == true
        @phase4_step = 2
        return
      end
      # ステップ 6 に移行
      @phase4_step = 6
    else
      # 元の処理を開始
      hidesp_counter_update_p4s5
      return
    end
  end
  #--------------------------------------------------------------------------
  # ■ [カウンター]
  #--------------------------------------------------------------------------
  def counter_action(target)
    #…………………………………………………………………………………………………
    # ■[カウンター]が発動した場合
    #…………………………………………………………………………………………………
    if @active_battler.counter[1] == true
##      # □ターゲット変更フラグを戻す
##      @active_battler.target_change = @active_battler.counter[2]
      # アクションを戻す
      @active_battler.current_action.kind = @active_battler.counter[3]
      @active_battler.current_action.basic = @active_battler.counter[4]
      @active_battler.current_action.target_index = @active_battler.counter[5]
      @active_battler.current_action.skill_id = @active_battler.counter[6]
      @active_battler.current_action.item_id = @active_battler.counter[7]
      # [カウンター]フラグを初期化
      @active_battler.counter = [false,false,false,0,0,0,0,0]
    end
    #…………………………………………………………………………………………………
    # ■[カウンター]が発生した場合
    #…………………………………………………………………………………………………
    if target.counter[0] == true && target.counter[1] == false
      # □[カウンター]アニメの表示
      @active_battler.damage_pop = true
      target.animation_id = 1
      @wait_count = 16
      target.animation_hit = (target.damage != "Miss")
      target.damage_pop = true
      # □[カウンター]発生フラグをOFF
      target.counter[0] = false
      # □行動者もしくは対象が死亡している場合は無効
      unless @active_battler.exist? or target.exist?
        return
      end
##      # □ターゲット変更フラグを格納
##      target.counter[2] = target.target_change
      # □アクションを格納
      target.counter[3] = target.current_action.kind
      target.counter[4] = target.current_action.basic
      target.counter[5] = target.current_action.target_index
      target.counter[6] = target.current_action.skill_id
      target.counter[7] = target.current_action.item_id
      #…………………………………………………………………………………………………
      # ■行動者がアクターの場合
      #…………………………………………………………………………………………………
      if @active_battler.is_a?(Game_Actor)
        # □[カウンター]するアクターIDを取得（行動者）
        target.current_action.target_index = $game_party.actors.index(@active_battler)
        # ■アクターがアクターを攻撃（PTアタック）
        if target.is_a?(Game_Actor)
##          # □ターゲット変更フラグをON
##          target.target_change = true
        # ■アクターがエネミーを攻撃
        elsif target.is_a?(Game_Enemy)
          
        end
      #…………………………………………………………………………………………………
      # ■行動者がエネミーの場合
      #…………………………………………………………………………………………………
      elsif @active_battler.is_a?(Game_Enemy)
        # □[カウンター]するエネミーIDを取得（行動者）
        target.current_action.target_index = $game_troop.enemies.index(@active_battler)
        # ■エネミーがアクターを攻撃
        if target.is_a?(Game_Actor)
        # ■エネミーがエネミーを攻撃（PTアタック）
        elsif target.is_a?(Game_Enemy)
##          # □ターゲット変更フラグをON
##          target.target_change = true
        end
      end
      # □行動者をtargetに変更
      @active_battler = target
      # □通常攻撃を代入！
      @active_battler.current_action.kind = 0
      @active_battler.current_action.basic = 0
      
      # [カウンター]発動フラグをON
      @active_battler.counter[1] = true
    end
  end

end

