#===============================================================================
#                       No Gameover Script
#                       Ver: 1.0
#                       Author: Day Ja Voo Games (Hunter Heidenreich)
#                       Date: July 20, 2013
#-------------------------------------------------------------------------------
#   Description: This script bypasses RPG Maker's usual gameover call and 
#                instead warps you to the last location that you "recovered
#                all" or the starting point of the character as set by you for 
#                the start of the game.
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#   Version History:
#   
#     Ver 1.0
#       - Created Script
#       - Warps to Location
#       - Added Battle Deaths
#       - Added Event Deaths
#------------------------------------------------------------------------------
#   Planned for Future Updates
#       -Nothing yet!
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#   Instructions - Plug and play!
#===============================================================================



#==============================================================================
# ** Game_Interpreter
#------------------------------------------------------------------------------
#  An interpreter for executing event commands. This class is used within the
# Game_Map, Game_Troop, and Game_Event classes.
#==============================================================================

class Game_Interpreter
  #--------------------------------------------------------------------------
  # * Recover All
  #--------------------------------------------------------------------------
  def command_314
    iterate_actor_var(@params[0], @params[1]) do |actor|
      actor.recover_all
    end
    $game_player.heal_x = $game_player.x
    $game_player.heal_y = $game_player.y
    $game_player.heal_map = $game_map.map_id
  end
  #--------------------------------------------------------------------------
  # * Change HP
  #--------------------------------------------------------------------------
  def command_311
    value = operate_value(@params[2], @params[3], @params[4])
    iterate_actor_var(@params[0], @params[1]) do |actor|
      next if actor.dead?
      actor.change_hp(value, @params[5])
      actor.perform_collapse_effect if actor.dead?
    end
    if $game_party.all_dead?
      Graphics.fadeout(30)
      $game_player.reserve_transfer($game_player.heal_map, $game_player.heal_x, $game_player.heal_y)
      
      while $game_player.x != $game_player.heal_x && $game_player.y != $game_player.heal_y
        $game_player.perform_transfer()
      end
      command_314()
      Graphics.update()
      fix_fadein
    end
  end
  def fix_fadein()

  end
end


#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  This class handles the player. It includes event starting determinants and
# map scrolling functions. The instance of this class is referenced by
# $game_player.
#==============================================================================

class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor   :heal_x                
  attr_accessor   :heal_y
  attr_accessor   :heal_map
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias dayjavoo_gameplayer_initialize_gameover initialize
  def initialize
    dayjavoo_gameplayer_initialize_gameover()
    @heal_x = $data_system.start_x
    @heal_y = $data_system.start_y
    @heal_map = $data_system.start_map_id
  end
end


#==============================================================================
# ** Scene_Base
#------------------------------------------------------------------------------
#  This is a super class of all scenes within the game.
#==============================================================================

class Scene_Base
  #--------------------------------------------------------------------------
  # * Determine if Game Is Over
  #   Transition to the game over screen if the entire party is dead.
  #--------------------------------------------------------------------------
  def check_gameover
    if $game_party.all_dead?
      Graphics.fadeout(30)
      $game_player.reserve_transfer($game_player.heal_map, $game_player.heal_x, $game_player.heal_y)
      $game_player.perform_transfer()
      Graphics.fadein(30)
    end
  end
end


#==============================================================================
# ** BattleManager
#------------------------------------------------------------------------------
#  This module manages battle progress.
#==============================================================================

module BattleManager
  @current_map_bgm = RPG::BGM.last
   RPG::BGM.stop()
  #--------------------------------------------------------------------------
  # * Defeat Processing
  #--------------------------------------------------------------------------
  def self.process_defeat
    $game_message.add(sprintf(Vocab::Defeat, $game_party.name))
    wait_for_message
    if @can_lose
      revive_battle_members
      replay_bgm_and_bgs
      SceneManager.return
    else
      if $game_party.all_dead?
        $game_player.reserve_transfer($game_player.heal_map, $game_player.heal_x, $game_player.heal_y)
        $game_player.perform_transfer()
        revive_battle_members
        replay_bgm_and_bgs
        SceneManager.goto(Scene_Map)
      end
    end
    battle_end(2)
    return true
  end
end