#===============================================================================
# RGSS3: Quicksave v.1.1
#===============================================================================
# by Cryranos/Metatron
#===============================================================================
# Instructions:  Paste In the Materials section.  Edit Module if desired.
#===============================================================================
# This script allows the player to quickly save and quit the game.  Upon
# reopening the game, the player will be prompted to load the game.  If it is
# loaded or declined, the quicksave file will be deleted.
#===============================================================================

module CryQuicksave
  QSFile  = "Save.qsav"  # Filename for the quicksave.
  MenuOp  = true         # Add a quicksave command to the menu?
  QSVNam  = "Quicksave"  # This is what the menu command will show up as
  # These are the Quicksave help and the Quickload help.  Insert the second line
  # with \n.
  QSVMess  = "Would you like to save and quit?\nYour save will be deleted upon resuming."
  QSVLoad  = "Would you like to load your quicksave?\nPress cancel to close the game."
  # These are for those who wish to bind the quicksave function to a key rather
  # than to the menu.
  UseKey   = false
  QSVKey   = Input::F5
end

module DataManager
  def self.quicksave_file_exists?
    !Dir.glob(CryQuicksave::QSFile).empty?
  end
  def self.make_quicksave_filename
    sprintf(CryQuicksave::QSFile)
  end
  def self.quicksave_game
    self.quicksave_game_without_rescue
  end
  def self.quicksave_game_without_rescue
    File.open(make_quicksave_filename, "wb") do |file|
      $game_system.on_before_save
      Marshal.dump(make_save_header, file)
      Marshal.dump(make_save_contents, file)
    end
  end
  def self.quickload_game
    self.load_quicksave_without_rescue
    self.dispose_quicksave
  end
  def self.load_quicksave_without_rescue
    File.open(make_quicksave_filename, "rb") do |file|
      Marshal.load(file)
      extract_save_contents(Marshal.load(file))
      reload_map_if_updated
    end
  end
  def self.dispose_quicksave
    File.delete(make_quicksave_filename) rescue nil
  end
end

module SceneManager
  def self.first_scene_class
    if $BTEST 
      Scene_Battle 
    elsif DataManager.quicksave_file_exists?
      Scene_QuickLoadTitle
    else
      Scene_Title
    end
  end 
end

if CryQuicksave::UseKey
class Game_Player < Game_Character  
  alias quicksave_update update
  def update
    quicksave_update
    if Input.trigger?(CryQuicksave::QSVKey)
      SceneManager.call(Scene_Quicksave)
    end
  end
end
end

class Window_QuicksaveCommand < Window_Command
  def initialize
    super(0, 0)
    update_placement
  end
  def window_width
    return 160
  end
  def update_placement
    self.x = (Graphics.width - width) / 2
    self.y = (Graphics.height * 1.6 - height) / 2
  end
  def make_command_list
    add_command("Yes", :confirm)
    add_command("No", :decline)
  end
end

class Scene_Quicksave < Scene_MenuBase
  def start
    super
    create_command_window
    create_help_window
  end
  def help_window_text
    CryQuicksave::QSVMess
  end
  def create_command_window
    @command_window = Window_QuicksaveCommand.new
    @command_window.set_handler(:confirm,      method(:command_quicksave))
    @command_window.set_handler(:decline,     method(:command_decline))
    @command_window.set_handler(:cancel,    method(:return_scene))
  end

  def create_help_window
    @help_window = Window_Help.new(2)
    @help_window.set_text(help_window_text)
  end
  def command_quicksave
    DataManager.quicksave_game
    fadeout_all
    SceneManager.exit
  end
  def command_decline
    SceneManager.return
  end
  def return_scene
    SceneManager.return
  end
end

class Scene_QuickLoadTitle < Scene_Title
  def start
    super
    SceneManager.clear
    Graphics.freeze
    create_background
    create_foreground
    create_command_window
    play_title_music
    create_help_window
  end
  def help_window_text
    CryQuicksave::QSVLoad
  end
  def create_command_window
    @command_window = Window_QuicksaveCommand.new
    @command_window.set_handler(:confirm,      method(:command_quickload))
    @command_window.set_handler(:decline,     method(:command_decline))
    @command_window.set_handler(:cancel,    method(:return_scene))
  end
  def create_help_window
    @help_window = Window_Help.new(2)
    @help_window.set_text(help_window_text)
  end
  def command_quickload
    close_command_window
    DataManager.quickload_game
    Sound.play_load
    fadeout_all
    $game_system.on_after_load
    SceneManager.goto(Scene_Map)
  end
  def command_decline
    DataManager.dispose_quicksave
    close_windows
    SceneManager.goto(Scene_Title)
  end
  def return_scene
    fadeout_all
    SceneManager.exit
  end
end

if CryQuicksave::MenuOp
class Window_MenuCommand
  alias add_qsv_command add_save_command
  def add_save_command
    add_qsv_command
    add_command(CryQuicksave::QSVNam, :qsav)
  end
end

class Scene_Menu
  alias add_qsv_to_commands create_command_window
  def create_command_window
    add_qsv_to_commands
    @command_window.set_handler(:qsav,      method(:command_quicksave))    
  end  
  def command_quicksave 
    SceneManager.call(Scene_Quicksave)
  end
end
end