#==============================================================================
# ** TDS Cutscene Skip
#    Ver: 1.0
#------------------------------------------------------------------------------
#  * Description:
#  This script allows you to skip events that are marked as cutscenes when the
#  CTRL key is pressed.
#------------------------------------------------------------------------------
#  * Features: 
#  Skip evented cutscenes.
#------------------------------------------------------------------------------
#  * Instructions:
#  To make an evented sequence be a skippable cutscene you must add this to it.
#
#  From a script call:
#    
#  start_cutscene
#
#  To specify where the cutscene will be skipped to you must add this label.
#  
#  CUTSCENE_END
#
#  To check whether or not an evented cutscene was skipped you can add this to
#  a conditional branch on the 4th tag in the script line.
#
#  cutscene_skipped?
#
#------------------------------------------------------------------------------
#  * Notes:
#  Cutscenes need to be faded in after being skipped and music needs to be
#  replayed after being skipped since they are faded out to allow the creator
#  of the cutscene to arrange things accordingly before the player takes control.
#------------------------------------------------------------------------------
# WARNING:
#
# Do not release, distribute or change my work without my expressed written 
# consent, doing so violates the terms of use of this work.
#
# If you really want to share my work please just post a link to the original
# site.
#
# * Not Knowing English or understanding these terms will not excuse you in any
#   way from the consequenses.
#==============================================================================


#==============================================================================
# ** 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
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------
  alias tds_cutscene_skip_game_interpreter_initialize         initialize
  alias tds_cutscene_skip_game_interpreter_execute_command    execute_command
  alias tds_cutscene_skip_game_interpreter_command_118        command_118
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     depth : 
  #--------------------------------------------------------------------------
  def initialize(depth = 0)
    # Run Original Method
    tds_cutscene_skip_game_interpreter_initialize(depth)
    # Set Cutscene Flags
    @cutscene_active = @cutscene_skipped = @cutscene_skipping = false
  end 
  #--------------------------------------------------------------------------
  # * Determine if Cutscene is active
  #--------------------------------------------------------------------------
  def cutscene_active?  ; @cutscene_active and running? end
  def cutscene_skipped? ; @cutscene_skipped and running? end
  #--------------------------------------------------------------------------
  # * Start Cutscene
  #--------------------------------------------------------------------------    
  def start_cutscene
    # Set Cutscene Active Flag to true
    @cutscene_active = true
    # Set Cutscene Skipped Flag to false
    @cutscene_skipped = false
  end
  #--------------------------------------------------------------------------
  # * Skip Cutscene
  #--------------------------------------------------------------------------  
  def skip_cutscene
    # Set Cutscene Skipping and Skipped Flag to true
     @cutscene_skipping = @cutscene_skipped = true     
     # Clear Game Messages
     $game_message.clear
     # Reset Map Scrolling
     $game_map.finish_scrolling
     # Stop All Forced Movement on map
     $game_map.stop_all_forced_move_route     
     # Set Fiber to nil
     @fiber = nil
  end
  #--------------------------------------------------------------------------
  # * Finish Cutscene
  #--------------------------------------------------------------------------  
  def finish_cutscene
    # Set Cutscene Skipping and Cutscene Active Flag to false
    @cutscene_skipping = @cutscene_active = false
  end  
  #--------------------------------------------------------------------------
  # * Label
  #--------------------------------------------------------------------------
  def command_118
    # Run Original Method
    tds_cutscene_skip_game_interpreter_command_118
    # Finish Cutscene if the Cutscene End Label has been reached
    finish_cutscene if @params.at(0).include?("CUTSCENE_END")
  end   
  #--------------------------------------------------------------------------
  # * Set Center Screen Display Position
  #--------------------------------------------------------------------------
  def set_center_display_pos(x, y)    
    center_x = (Graphics.height / 32 - 1) / 2.0 
    center_y = (Graphics.width / 32 - 1) / 2.0
    $game_map.set_display_pos(x - 2 - center_x, y  - center_y)    
  end
  #--------------------------------------------------------------------------
  # * Execute Command
  #--------------------------------------------------------------------------
  def execute_command
    # If Cutscene has been skipped
    if @cutscene_skipping
      # Get Command and it's properties
      command = @list[@index] ; @params = command.parameters ; @indent = command.indent
      # Command Code Case
      case command.code
      when 118, 121..123, 125..129, 132..138, 201..203, 206, 211, 216, 217, 231, 233, 235, 281..285, 311..324
      when 204 # Scroll Map
        # Process Scrolling Method and Finish Map Scrolling
        command_204 ; $game_map.finish_scrolling ; return
      when 223 # Map tone Change
        # Change Fade time to 0 and remove wait
        @params[1] = 0 ; @params[2] = false
      when 234 # Change Picture Tone
        # Change Picture Tone and Remove Wait
        @params[2] = 0 ; @params[3] = false
      when 232 # Move Picture
        # Change Move Picture Duration and Remove Wait
        @params[10] = 0 ; @params[11] = false
      when 335 # Script
      else ; return
      end
      method_name = "command_#{command.code}"
      send(method_name) if respond_to?(method_name)
      return
    end
    # Run Original Command
    tds_cutscene_skip_game_interpreter_execute_command 
  end  
end


#==============================================================================
# ** Game_Map
#------------------------------------------------------------------------------
#  This class handles maps. It includes scrolling and passage determination
# functions. The instance of this class is referenced by $game_map.
#==============================================================================

class Game_Map
  #--------------------------------------------------------------------------
  # * Finish Map Scrolling
  #--------------------------------------------------------------------------
  def finish_scrolling
    # Scroll Direction Case
    case @scroll_direction
    when 2 ; set_display_pos(@display_x, @display_y + @scroll_rest.abs) # Down    
    when 4 ; set_display_pos(@display_x - @scroll_rest.abs, @display_y) # Left
    when 6 ; set_display_pos(@display_x + @scroll_rest.abs, @display_y) # Left      
    when 8 ; set_display_pos(@display_x, @display_y - @scroll_rest.abs) # Up
    end
    # Setup Scroll
    setup_scroll
  end
  #--------------------------------------------------------------------------
  # * Stop All forced movement
  #--------------------------------------------------------------------------
  def stop_all_forced_move_route
    # Stop Events Forced Move Route
    @events.each_value {|e| e.stop_move_route }    
    # Stop Player Move Route
    $game_player.stop_move_route 
  end
end

#==============================================================================
# ** Game_Character
#------------------------------------------------------------------------------
#  This class deals with characters. It's used as a superclass of the
# Game_Player, Game_Follower, GameVehicle and Game_Event classes.
#==============================================================================

class Game_Character < Game_CharacterBase
  #--------------------------------------------------------------------------
  # * Stop Move Route Processing
  #--------------------------------------------------------------------------
  def stop_move_route
    # Return if Move route is not forced
    return if !@move_route_forcing    
    # Move to current position (If they were already in mid movement)
    moveto(@x, @y)
    # Restore Move Route
    restore_move_route
    # Set Move Route Force Flag to false
    @move_route_forcing = false    
  end
end




#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs the map screen processing.
#==============================================================================

class Scene_Map < Scene_Base
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------
  alias tds_silver_heart_scene_map_update                              update
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update Cutscene Skip Input if in a Cutscene
    update_cutscene_skip_input if $game_map.interpreter.cutscene_active?    
    # Run Original Method
    tds_silver_heart_scene_map_update
  end
  #--------------------------------------------------------------------------
  # * Update Cutscene Skip Input
  #--------------------------------------------------------------------------
  def update_cutscene_skip_input
    # If Input Trigger CTRL
    if Input.trigger?(:CTRL)
      # Play Ok Sound
      Sound.play_ok
      # Make Cutscene Cover Sprite
      @cutscene_cover = Sprite.new
      @cutscene_cover.bitmap = Graphics.snap_to_bitmap
      @cutscene_cover.bitmap.blur      
      @cutscene_cover.opacity = 0      
      @cutscene_cover.tone.set(-30, -30, -30)
      @cutscene_cover.z = 5000
      # Make Cutscene Skip Window
      @cutscene_skip_window = Window_Cutscene_Skip_Prompt.new
      @cutscene_skip_window.x = (Graphics.width - @cutscene_skip_window.width) / 2
      @cutscene_skip_window.y = (Graphics.height - @cutscene_skip_window.height) / 2      
      @cutscene_skip_window.z = 5100
      @cutscene_skip_window.index = 1
      @cutscene_skip_window.openness = 0
      @cutscene_skip_window.open
      
      15.times do
        # Update Graphics
        Graphics.update
        # Increase Cutscene Cover Opacity
        @cutscene_cover.opacity += 17
      end
      10.times do
        # Update Graphics
        Graphics.update        
        # Update Cutscene Skip Window
        @cutscene_skip_window.update        
      end
      
      loop do
        # Update Graphics and Input
        Graphics.update ; Input.update
        # Update Cutscene Skip Window
        @cutscene_skip_window.update
        # If Input Trigger C (Confirm)
        if Input.trigger?(:C)
          # Play Ok Sound
          Sound.play_ok
          case @cutscene_skip_window.index 
          when 0 # Yes
            # Deactivate Skip Cutscene window
            @cutscene_skip_window.deactivate
            # Close Cutscene Skip Window
            @cutscene_skip_window.close
            10.times do
              # Update Graphics
              Graphics.update
              # Update Cutscene Skip Window
              @cutscene_skip_window.update                    
            end
            # Save BGM
            $game_system.save_bgm            
            # Fadeout
            $game_map.screen.start_fadeout(30)
            # Fade Time
            time = 500
            # Fadeout all Sound
            RPG::BGM.fade(time) ; RPG::BGS.fade(time) ; RPG::ME.fade(time)                        
            # Fading Out Wait
            35.times do 
              # Update Graphics
              Graphics.update
              $game_map.screen.update_fadeout
              @spriteset.update_viewports              
              @cutscene_cover.color.set(0, 0, 0, 255 - $game_map.screen.brightness)
            end
            # Stop All Sound
            RPG::BGM.stop ; RPG::BGS.stop ; RPG::ME.stop            
            # Dispose of Cutscene Cover
            @cutscene_cover.bitmap.dispose ; @cutscene_cover.dispose ; @cutscene_cover = nil
            # Dispose of Cutscene Skip Window
            @cutscene_skip_window.dispose ; @cutscene_skip_window = nil
            # Update Input
            Input.update         
            # Process Cutscene Skipping
            $game_map.interpreter.skip_cutscene            
            # Dispose of Message Window
            @message_window.dispose ; @message_window = nil
            # Create Message Window
            create_message_window
            break
          when 1 # No
            # Deactivate Skip Cutscene window
            @cutscene_skip_window.deactivate
            # Close Cutscene Skip Window
            @cutscene_skip_window.close
            10.times do
              # Update Graphics
              Graphics.update
              # Decrease Cutscene Cover Opacity
              @cutscene_cover.opacity -= 17       
              # Update Cutscene Skip Window
              @cutscene_skip_window.update                    
            end          
            # Dispose of Cutscene Cover
            @cutscene_cover.bitmap.dispose ; @cutscene_cover.dispose ; @cutscene_cover = nil
            # Dispose of Cutscene Skip Window
            @cutscene_skip_window.dispose ; @cutscene_skip_window = nil
            # Update Input
            Input.update            
          end
          break
        end

        # If Input Trigger B (Cancel)
        if Input.trigger?(:B)
          # Play Cancel Sound
          Sound.play_cancel
          # Deactivate Skip Cutscene window
          @cutscene_skip_window.deactivate
          # Close Cutscene Skip Window
          @cutscene_skip_window.close
          10.times do
            # Update Graphics
            Graphics.update
            # Decrease Cutscene Cover Opacity
            @cutscene_cover.opacity -= 17       
            # Update Cutscene Skip Window
            @cutscene_skip_window.update                    
          end          
          # Dispose of Cutscene Cover
          @cutscene_cover.bitmap.dispose ; @cutscene_cover.dispose ; @cutscene_cover = nil
          # Dispose of Cutscene Skip Window
          @cutscene_skip_window.dispose ; @cutscene_skip_window = nil
          # Update Input
          Input.update
          break
        end
      end
    end
  end
end


#==============================================================================
# ** Window_Save_File_Prompt
#------------------------------------------------------------------------------
#  This window handles save file overwrite prompt
#==============================================================================

class Window_Cutscene_Skip_Prompt < Window_Command
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(466, 170)
  end
  #--------------------------------------------------------------------------
  # * Window Width and Height
  #--------------------------------------------------------------------------
  def window_width  ; 190 end
  def window_height ; 77 end
  #--------------------------------------------------------------------------
  # * Window Standard Padding
  #--------------------------------------------------------------------------
  def standard_padding ; 0 end
  #--------------------------------------------------------------------------
  # * Item Rect
  #--------------------------------------------------------------------------
  def item_rect(index)
    rect = Rect.new
    rect.width = item_width
    rect.height = item_height
    rect.x = 12 + index % col_max * item_width + 5
    rect.y = 37 + index / col_max
    rect
  end
  #--------------------------------------------------------------------------
  # * Ok Enabled Handling
  #--------------------------------------------------------------------------
  def ok_enabled? ; false end
  #--------------------------------------------------------------------------
  # * Max Columns
  #--------------------------------------------------------------------------
  def col_max ; 2 end
  #--------------------------------------------------------------------------
  # * Command Text Alignment
  #--------------------------------------------------------------------------
  def alignment ; 1 end
  #--------------------------------------------------------------------------
  # * Make Commands List
  #--------------------------------------------------------------------------
  def make_command_list    
    add_command("Yes", :yes, true) ; add_command("No",  :no, true)
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    super
    contents.font.color = knockout_color
    draw_text(12, 12, 166, 24, "Skip Cutscene?", 1)
  end
end