#==============================================================================
# ☆ RGSS2(VX)専用 VXバグフィックス Ver 1.0
#------------------------------------------------------------------------------
# 河原 つつみ
#  連絡先 :『アクマの脳髄』http://www.akunou.com/
#------------------------------------------------------------------------------
#  VXデフォルトスクリプトのスプライト関係のバグや記述抜けを修正します。
#  修正のため、関数をほとんど再定義していますので少し注意。
#
#  ◆修正内容
#   敵を倒した後、イベントで敵全体にステートを付加すると
#   コラプスエフェクトが再表示されてしまうバグの修正。
#   Scene_Skillのビューポートの解放のし忘れを修正。
#   逃走したはずの敵が、ステート解消と同時に可視になってしまうバグの修正。
#
#==============================================================================

#==============================================================================
# ■ Game_Interpreter
#==============================================================================

class Game_Interpreter
  #--------------------------------------------------------------------------
  # ◎ ステートの変更
  #--------------------------------------------------------------------------
  def command_313
    iterate_actor_id(@params[0]) do |actor|
      if @params[1] == 0
        live = (actor.hp > 0)
        actor.add_state(@params[2])
        actor.perform_collapse if live
      else
        actor.remove_state(@params[2])
      end
    end
    return true
  end
  #--------------------------------------------------------------------------
  # ◎ 敵キャラのステート変更
  #--------------------------------------------------------------------------
  def command_333
    iterate_enemy_index(@params[0]) do |enemy|
      if @params[2] == 1                    # 戦闘不能の変更なら
        enemy.immortal = false              # 不死身フラグをクリア
      end
      if @params[1] == 0
        live = (enemy.hp > 0)
        enemy.add_state(@params[2])
        enemy.perform_collapse if live
      else
        enemy.remove_state(@params[2])
      end
    end
    return true
  end
end

#==============================================================================
# ■ Scene_Skill
#==============================================================================

class Scene_Skill < Scene_Base
  #--------------------------------------------------------------------------
  # ● 終了処理
  #--------------------------------------------------------------------------
  alias akunou3_terminate terminate
  def terminate
    akunou3_terminate
    @viewport.dispose
  end
end

#==============================================================================
# ■ Scene_Battle
#==============================================================================

class Scene_Battle < Scene_Base
  #--------------------------------------------------------------------------
  # ◎ 戦闘行動の処理
  #--------------------------------------------------------------------------
  def process_action
    return if judge_win_loss
    return if $game_temp.next_scene != nil
    set_next_active_battler
    if @active_battler == nil
      turn_end
      return
    end
    return if @active_battler.dead?
    if !@active_battler.hidden
      @message_window.clear
      wait(5)
      @active_battler.white_flash = true
      unless @active_battler.action.forcing
        @active_battler.action.prepare
      end
      if @active_battler.action.valid?
        execute_action
      end
    end
    unless @active_battler.action.forcing
      @message_window.clear if !@active_battler.hidden
      remove_states_auto
      display_current_state if !@active_battler.hidden
    end
    if !@active_battler.hidden
      @active_battler.white_flash = false
      @message_window.clear
    end
  end
  #--------------------------------------------------------------------------
  # ◎ ステート自然解除
  #--------------------------------------------------------------------------
  def remove_states_auto
    last_st = @active_battler.states
    @active_battler.remove_states_auto
    if !@active_battler.hidden and @active_battler.states != last_st
      wait(5)
      display_state_changes(@active_battler)
      wait(30)
      @message_window.clear
    end
  end
end