###############################################################################
## CaitSith2's Demo Mode script Version 1.3                                  ##
###############################################################################
## Version History                                                           ##
##   1.0 - Initial release                                                   ##
##   1.1 - Added expiration message display.                                 ##
##   1.2 - Variables to access demo state from in game itself.               ##
##       - Added options for controlling automatic behaviour.                ##
##       - Added option on whether manual saving is also disabled on         ##
##         demo expiration.                                                  ##
##   1.3 - Expired saves cannot be reloaded unless AUTO_QUIT is false        ##
##       - Save disabled seperated into a message of its own. "" for no      ##
##         displayed message as usual.                                       ##
##   1.4 - Save should never have been disabled at timeout if DEMO_MODE was  ##
##         false.                                                            ##
###############################################################################
($imported ||= {})["CaitSith2DemoTimer"] = true
module DT
  $save_screen_shown = 0
###############################################################################
## SETUP                                                                     ##
###############################################################################

#Set up whether game is in DEMO mode or not.
  DEMO_MODE = true
  
#if in DEMO_MODE, then define whether to automatically save the game.
#once time runs out, in the case that you also decide to set AUOT_QUIT to false.
  AUTO_SAVE = true
  
#if in DEMO_MODE, then define whether to automatically quit to Title screen
#at expiration.
  AUTO_QUIT = true
  
#if in DEMO_MODE, define if manual saving is disabled at demo expiration,
#only applies if AUTO_QUIT is false.
  DISABLE_SAVE = true
  
#Set the time limit, in seconds after which, the player gets a chance to save
#Saving happens as soon as any battle in progress is finished.
  DEMO_TIME_LIMIT = 3600 #1 hour is 3600 seconds.
  
#Set your message to dispaly if the game is being quit automatically.
# Set it as "" if you wish to not display a message.
  D_Q_MSG = \
    "Be sure to get the Full Version :)"
    
#Set your message to display prior to the auto save screen.
#This gives the player one last opportunity to save, as further saving will be
#disabled.
  D_S_MSG = \
		"Demo Expired :(\n"
		
	D_SD_MSG = \
	  "This is your last chance to save\n" + \
	  "\n" + \
    "All further saving in this demo is\n" + \
    "disabled."
  
  
#Set these to variable numbers in your game, if you wish to have access to
#this info from within the game itself, like if you wish to not allow certain
#stories to advance in the demo.

  DEMO_MODE_SWITCH = 0
  DEMO_EXPIRED_SWITCH = 0
  DEMO_TIME_LIMIT_VAR = 0
  DEMO_TIME_REMAINING_VAR = 0

###############################################################################
## FINISHED SETUP. Don't touch after this point.                             ##
###############################################################################
end

class Game_System
	def on_after_load_demotimer
    Graphics.frame_count = @frames_on_save
  end 
end #Game_System

class Scene_Load < Scene_File
	def playtimeleft
		DT::DEMO_TIME_LIMIT - (Graphics.frame_count / Graphics.frame_rate)
	end
	
	alias DemoTimer_on_load_success on_load_success
	def on_load_success
		$game_system.on_after_load_demotimer
		if DT::DEMO_MODE && DT::AUTO_QUIT
			DemoTimer_on_load_success() unless playtimeleft <= 0
			Sound.play_buzzer if playtimeleft <= 0
		else
			DemoTimer_on_load_success()
		end
	end #on_load_success
end #Scene_Load < Scene_File


class Window_MenuCommand < Window_Command
  def playtime
    Graphics.frame_count / Graphics.frame_rate
  end #playtime
  
  def add_save_command
    if (playtime < DT::DEMO_TIME_LIMIT) || (DT::DEMO_MODE == false)
      add_command(Vocab::save, :save, save_enabled)
    else
      if DT::DISABLE_SAVE
        add_command(Vocab::save, :save, false)
      else
        add_command(Vocab::save, :save, save_enabled)
      end #if
    end #if
  end #add_save_command
end #Window_MenuCommand < Window_Command

class Game_Event < Game_Character
  alias DemoUpdateCall update
  def playtime
    Graphics.frame_count / Graphics.frame_rate
  end #playtime
  
  def timeremaining
    DT::DEMO_TIME_LIMIT - playtime
  end #timeremaining
  
  def update
    DemoUpdateCall()
    if DT::DEMO_MODE_SWITCH > 0
      if $game_switches[DT::DEMO_MODE_SWITCH] != DT::DEMO_MODE
        $game_switches[DT::DEMO_MODE_SWITCH] = DT::DEMO_MODE
      end
    end
    if DT::DEMO_EXPIRED_SWITCH > 0
      if DT::DEMO_MODE
        if (DT::DEMO_TIME_LIMIT - playtime) <= 0
          if $game_switches[DT::DEMO_EXPIRED_SWITCH] == false
            $game_switches[DT::DEMO_EXPIRED_SWITCH] = true
          end
        else
          if $game_switches[DT::DEMO_EXPIRED_SWITCH]
            $game_switches[DT::DEMO_EXPIRED_SWITCH] = false
          end
        end
      else
        if $game_switches[DT::DEMO_EXPIRED_SWITCH]
          $game_switches[DT::DEMO_EXPIRED_SWITCH] = false
        end
      end
    end
    if DT::DEMO_TIME_LIMIT_VAR > 0
      if $game_variables[DT::DEMO_TIME_LIMIT_VAR] != DT::DEMO_TIME_LIMIT
        $game_variables[DT::DEMO_TIME_LIMIT_VAR] = DT::DEMO_TIME_LIMIT
      end
    end
    
    return if $game_message.busy?
    return unless DT::DEMO_MODE
    if DT::DEMO_TIME_REMAINING_VAR > 0
      if $game_variables[DT::DEMO_TIME_REMAINING_VAR] != timeremaining
        $game_variables[DT::DEMO_TIME_REMAINING_VAR] = timeremaining
      end
    end
    $save_screen_shown = 0 if playtime < DT::DEMO_TIME_LIMIT
    return if playtime < DT::DEMO_TIME_LIMIT
    return if $game_party.in_battle
    if DT::AUTO_SAVE || DT::AUTO_QUIT
      $save_screen_shown = $save_screen_shown+1
    end
    
    if DT::AUTO_SAVE
      $game_message.add(DT::D_S_MSG) if (DT::D_S_MSG != "") && 
      ($save_screen_shown == 1)
			$game_message.add(DT::D_SD_MSG) if (DT::D_SD_MSG != "") &&
			($save_screen_shown == 1) && DT::DISABLE_SAVE
      $save_screen_shown = -1 if $save_screen_shown == 2
      SceneManager.call(Scene_Save) if $save_screen_shown == 0
      $save_screen_shown = 2 if $save_screen_shown == 0
    end
    if DT::AUTO_QUIT
      $game_message.add(DT::D_Q_MSG) if (DT::D_Q_MSG != "") && 
        ($save_screen_shown == 3)
      $save_screen_shown = 0 if $save_screen_shown > 4
      SceneManager.goto(Scene_Title) if $save_screen_shown == 0
      $game_message.add("Quitting") if $save_screen_shown == 0
    end
  end #update
end #Game_Event < Game_Character