#≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
# ** Glitchfinder's Multiple Timers               [RPG Maker XP] [RPG Maker VX]
#    Version 3.34
#------------------------------------------------------------------------------
#  This script helps scripters and eventers use multiple hidden in-game timers
#  that can be called and modified at any time. Refer to "$game_timer" for the
#  instance of this class.
#==============================================================================
# * Version History
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#   Version 1.00 ------------------------------------------------- (??????????)
#     - Initial version
#     - Author: Glitchfinder
#   Version 2.00 ------------------------------------------------- (2008-06-11)
#     - Script completely rewritten to work with multiple timers
#     - Author: Glitchfinder
#    Version 2.10 ------------------------------------------------ (2002-06-18)
#      - Added the check_timing() method
#      - Fixed bug with check() method
#      - Author: Glitchfinder
#     Version 2.11 ----------------------------------------------- (2007-06-19)
#       - Reformatted and cleaned script
#       - Author: Glitchfinder
#    Version 2.21 ------------------------------------------------ (2007-06-22)
#      - Added the extended_start() method
#      - Author: Glitchfinder
#    Version 2.31 ------------------------------------------------ (2007-07-20)
#      - Added the update_single() method
#      - Added the kill_timer() method
#      - Added the adjust_time() method
#      - Author: Glitchfinder
#     Version 2.32 ----------------------------------------------- (2007-07-24)
#       - Fixed bug with VX and the start() method
#       - Fixed bug with VX and the extended_start() method
#       - Author: Glitchfinder
#   Version 3.32 ------------------------------------------------- (2009-06-28)
#     - Script completely rewritten to increase efficiency
#     - Modified most methods to include error-checking
#     - Renamed the check() method to finished?()
#     - Removed the modifications of Game_Temp
#     - Added the script to the global variable $game_timer
#     - Added new output to the check_timing() method
#     - Modified the update() method of Scene_Battle
#     - Added the ability for a timer to end battle
#     - Added the end_battle() method
#     - Added the end_battle?() method
#     - Modified the update() method of Sprite_Timer
#     - Added the ability to display a timer using Sprite_Timer
#     - Modified the "Conditional Branch..." event command
#     - Modified the "Control Variables..." event command
#     - Modified the "Control Timer..." event command
#     - Replaced the default timer with $game_timer
#     - Added the display() method
#     - Added the display?() method
#     - Modified the write_save_data() method of Scene_Save
#     - Modified the read_save_data() method of Scene_Load
#     - Added the ability to save a timer when the game is saved
#     - Added the save() method
#     - Added the save?() method
#     - Added the load_save() method
#     - Author: Glitchfinder
#     Version 3.33 ----------------------------------------------- (2009-06-28)
#       - Script optimized further
#       - Unnecessary lines removed
#       - Code efficiency improved
#       - Author: theory
#     Version 3.34 ----------------------------------------------- (2009-06-29)
#       - Script optimized
#       - Unnecessary lines removed
#       - Author: Glitchfinder
#==============================================================================
# * Instructions
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  Place this script above Main, and below the default scripts. (I realize this
#  is obvious to most, but some people don't get it.)
#
#  To start a timer, use the following call:
#    $game_timer.start(seconds, key)
#  Where seconds is the number of seconds you want the timer to run, and
#  key is the name of that specific timer. You must use a seperate name for
#  every timer, because when one finishes, only the first script checking for
#  that name will return with the timer completed.
#
#  To check whether or not a timer has finished, use the following call:
#    $game_timer.finished?(key)
#  Where timer_number is the number of the timer that you need to check. See
#  the rules above regarding key.
#
#  To remove a timer, use the following call:
#    $game_timer.kill_timer(key)
#  Where key is the timer that you wish to remove.
#
#  To adjuct the time remaining on a timer, use the following call:
#    $game_timer.adjust_time(setting, value, key)
#  Where value is the number you want to adjust by, key is the timer you want
#  to adjust, and setting in the operation you want to perform. (1 to add
#  frames, 2 to subtract frames, 3 to multiply, 4 to divide, and 5 to perform a
#  modulus operation)
#
#  To stop or start a timer, use the following call:
#    $game_timer.active(key, bool)
#  Where key is the timer you want to start or stop, and bool is true if you
#  want to start the timer, and false if you want to stop it.
#
#  To have a timer force battles to abort when they reach zero, use the
#  following call:
#    $game_timer.end_battle(key, bool)
#  Where key is the timer you want to modify, and bool is true if you want it
#  to abort battles while it is 0, and false if you don't.
#
#  To set a timer to display, use the following call:
#    $game_timer.display(key, bool)
#  Where key is the timer you want to display, and bool is either true or
#  false, depending on the desired result.
#
#  To set a timer to be saved in the save file, use the following call:
#    $game_timer.save(key, bool)
#  Where key is the timer you want to have saved, and bool is true if you want
#  the timer to be saved, and false if you want it to disappear when the game
#  is closed.
#==============================================================================
# * Method List
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  initialize
#    Sets up the script. Intended for internal use only.
#
#  update
#    Updates all timers. Although intended for internal use, may be added to
#    other scenes to force a timer update elsewhere.
#
#  update_single(key)
#    Updates a single timer, specified by key.
#
#  start(seconds, key)
#    Set up a timer, which will be accessed in the future by using key.
#
#  extended_start(hours, minutes, seconds, key)
#    Set up and extended timer. Functions in the same way as start().
#
#  finished?(key)
#    Returns true if the timer specified by key has reached 0, false otherwise.
#    This method will delete the checked timer if it has reached 0.
#
#  check_timing(key)
#    Debugging method. Returns a variety of messages based on the current state
#    of the timer specified by key.
#
#  kill_timer(key)
#    Erases the timer specified by key.
#
#  adjust_time(setting, value, key)
#    Adjusts the remaining time in the timer specified by key. Setting
#    specifies an operation. (1 => addition, 2 => subtraction,
#    3 => multiplication, 4 = division, 5 = modulus
#
#  active(key, bool)
#    Sets the timer specified by key to active or inactive, based on whether
#    bool is true or false.
#
#  active?(key)
#    Returns true if the timer specified by key is active, and false otherwise.
#
#  end_battle(key, bool)
#    When bool is set to true, this method sets the timer specified by key to
#    abort battles when it reaches 0. Otherwise, the timer will not abort
#    battles when it reaches 0.
#
#  end_battle?
#    Checks to see if a timer that aborts battle has reached 0. Intended for
#    internal use.
#
#  display(key, bool)
#    If bool is set to true, will display the timer specified by key.
#    Otherwise, the timer is not displayed. Please note that only one timer will
#    be displayed at any given time, no matter how many are set to display.
#
#  display?
#    Checks to see if a timer is being displayed, and reuturns remaining time.
#    This method is intended for internal use only.
#
#  save(key, bool)
#    If bool is set to true, the timer specified by key will be saved along
#    with the rest of the game. If bool is false, the timer will be deleted
#    when the game closes, even if a save was made.
#
#  save?
#    Returns an array of all timers set to be saved. This method is intended
#    for internal use only.
#
#  load_save(timers)
#    Loads all timers in the timers variable. This method is intended for
#    internal use, but could be used to create new timers as well.
#
#  conditional_branch(parameters)
#    Part of the interpreter's conditional branch processing. Intended for
#    internal use only.
#
#  control_variables(parameters)
#    Part of the interpreter's control variable processing. Intended for
#    internal use only.
#
#  control_timer(parameters)
#    The interpreter's control timer processing. Intended for internal use
#    only, although it could be used to create an event timer through scripts.
#==============================================================================
# *Glitchfinder's Advice
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  This script is meant for people with at least a basic level of scripting
#  knowledge and ability. If you are unsure of your abilities, or don't know
#  how to script, then it would be a good idea to avoid this script.
#
#  This script has been tested in both scripts and events, and will function in
#  both cases.
#
#  This script will function in both RPG Maker XP and RPG Maker VX.
#==============================================================================
# * Contact
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  Glitchfinder, the author of this script, may be contacted through his
#  website, found at http://www.glitchkey.com
#
#  You may also find Glitchfinder at http://www.hbgames.org
#==============================================================================
# * Usage
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  This script may be used with the following terms and conditions:
#
#    1. This script is free to use in any noncommercial project. If you wish to
#       use this script in a commercial (paid) project, please contact
#       Glitchfinder at his website.
#    2. This script may only be hosted at the following domains:
#         http://www.glitchkey.com
#         http://www.hbgames.org
#    3. If you wish to host this script elsewhere, please contact Glitchfinder.
#    4. If you wish to translate this script, please contact Glitchfinder. He
#       will need the web address that you plan to host the script at, as well
#       as the language this script is being translated to.
#    5. This header must remain intact at all times.
#    6. Glitchfinder remains the sole owner of this code. He may modify or
#       revoke this license at any time, for any reason.
#    7. Any code derived from code within this script is owned by Glitchfinder,
#       and you must have his permission to publish, host, or distribute his
#       code.
#    8. This license applies to all code derived from the code within this
#       script.
#    9. If you use this script within your project, you must include visible
#       credit to Glitchfinder and theory, within reason.
#≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡


#==============================================================================
# ** Sprite_Timer
#------------------------------------------------------------------------------
#  This sprite is used to display the timer.It observes the $game_system
#  class and automatically changes sprite conditions.
#==============================================================================

class Sprite_Timer < Sprite
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    # Hide timer sprite and return if there is no currently displayed timer
    self.visible = $game_timer.display? == false ? false : true
    return if self.visible == false
    # If timer needs to be redrawn
    if $game_timer.display? / Graphics.frame_rate != @total_sec
      # Calculate total number of seconds and make a string
      @total_sec = $game_timer.display? / Graphics.frame_rate
      text = sprintf("%02d:%02d", @total_sec / 60, @total_sec % 60)
      # Clear window contents and draw timer
      self.bitmap.clear
      self.bitmap.draw_text(self.bitmap.rect, text, 1)
    end
  end
end

#----------------------------------------------------------------------------
# * Check for RGSS1
#----------------------------------------------------------------------------
unless (defined?(RGSS1p5) == true || defined?(Graphics.brightness) == true)

#==============================================================================
# ** Scene_Save
#------------------------------------------------------------------------------
#  This class performs save screen processing.
#==============================================================================

class Scene_Save < Scene_File
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias scriptastic_timer_scene_save_write_save_data write_save_data
  #--------------------------------------------------------------------------
  # * Write Save Data
  #     file : write file object (opened)
  #--------------------------------------------------------------------------
  def write_save_data(file)
    # Call original method
    scriptastic_timer_scene_save_write_save_data(file)
    # Write save data
    Marshal.dump($game_timer.save?, file)
  end
end

#==============================================================================
# ** Scene_Load
#------------------------------------------------------------------------------
#  This class performs load screen processing.
#==============================================================================

class Scene_Load < Scene_File
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias scriptastic_timer_scene_load_read_save_data read_save_data
  #--------------------------------------------------------------------------
  # * Read Save Data
  #     file : file object for reading (opened)
  #--------------------------------------------------------------------------
  def read_save_data(file)
    # Call original method
    scriptastic_timer_scene_load_read_save_data(file)
    # Load saved data
    $game_timer.load_save(Marshal.load(file)) unless file.eof?
  end
end

#----------------------------------------------------------------------------
# * End Check for RGSS1
#----------------------------------------------------------------------------
end

#----------------------------------------------------------------------------
# * Check for RGSS1 or RGSS1.5
#----------------------------------------------------------------------------
if (defined?(RGSS1p5) == true || defined?(Graphics.brightness) != true)

#==============================================================================
# ** Interpreter
#------------------------------------------------------------------------------
#  This interpreter runs event commands. This class is used within the
#  Game_System class and the Game_Event class.
#==============================================================================

class Interpreter
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias scriptastic_timer_interpreter_command_111 command_111
  alias scriptastic_timer_interpreter_command_122 command_122
  #--------------------------------------------------------------------------
  # * Conditional Branch
  #--------------------------------------------------------------------------
  def command_111
    # Initialize local variable: result
    result = false
    # If checking timer
    if @parameters[0] == 3
      # Call modified method
      result = $game_timer.conditional_branch(@parameters)
      # Skip if branch is false
      return command_skip if result == false
    end
    # Store determinant results in hash
    @branch[@list[@index].indent] = result
    # If determinant results are true
    if @branch[@list[@index].indent] == true
      # Delete branch data
      @branch.delete(@list[@index].indent)
      # Continue
      return true
    end
    # Call aliased method
    scriptastic_timer_interpreter_command_111
  end
  #--------------------------------------------------------------------------
  # * Control Variables
  #--------------------------------------------------------------------------
  def command_122
    # Initialize value
    value = 0
    # If @parameters[3] is set to other
    if @parameters[3] == 7
      # If @parameters[4] is set to timer
      if @parameters[4] == 5  # timer
        # Call modified method
        $game_timer.control_variables(@parameters)
        # Continue
        return true
      end
    end
    # Call aliased method
    scriptastic_timer_interpreter_command_122
  end
  #--------------------------------------------------------------------------
  # * Control Timer
  #--------------------------------------------------------------------------
  def command_124
    # Call modified method
    $game_timer.control_timer(@parameters)
    # Continue
    return true
  end
end

#----------------------------------------------------------------------------
# * End Check for RGSS1 or RGSS1.5
#----------------------------------------------------------------------------
end

#----------------------------------------------------------------------------
# * Check for RGSS2 or RGSS1.5
#----------------------------------------------------------------------------
if (defined?(RGSS1p5) == true || defined?(Graphics.brightness) == true)

#==============================================================================
# ** Scene_File
#------------------------------------------------------------------------------
#  This class performs the save and load screen processing.
#==============================================================================

class Scene_File < Scene_Base
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias scriptastic_timer_scene_file_write_save_data write_save_data
  alias scriptastic_timer_scene_file_read_save_data read_save_data
  #--------------------------------------------------------------------------
  # * Write Save Data
  #     file : write file object (opened)
  #--------------------------------------------------------------------------
  def write_save_data(file)
    # Call original method
    scriptastic_timer_scene_file_write_save_data(file)
    # Write save data
    Marshal.dump($game_timer.save?, file)
  end
  #--------------------------------------------------------------------------
  # * Read Save Data
  #     file : file object for reading (opened)
  #--------------------------------------------------------------------------
  def read_save_data(file)
    # Call original method
    scriptastic_timer_scene_file_read_save_data(file)
    # Load saved data
    $game_timer.load_save(Marshal.load(file)) unless file.eof?
  end
end

#----------------------------------------------------------------------------
# * End Check for RGSS2 or RGSS1.5
#----------------------------------------------------------------------------
end

#----------------------------------------------------------------------------
# * Check for RGSS2
#----------------------------------------------------------------------------
if (defined?(RGSS1p5) != true && defined?(Graphics.brightness) == true)

#==============================================================================
# ** 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 Methods
  #--------------------------------------------------------------------------
  alias scriptastic_timer_game_interpreter_command_111 command_111
  alias scriptastic_timer_game_interpreter_command_122 command_122
  #--------------------------------------------------------------------------
  # * Conditional Branch
  #--------------------------------------------------------------------------
  def command_111
    # Initialize local variable: result
    result = false
    # If checking timer
    if @params[0] == 3
      # Call modified method
      result = $game_timer.conditional_branch(@params)
      # Skip if branch is false
      return command_skip if reult == false
    end
    # Store determinant results in hash
    @branch[@indent] = result
    # If determinant results are true
    if @branch[@indent] == true
      # Delete branch data
      @branch.delete(@indent)
      # Continue
      return true
    end
    # Call aliased method
    scriptastic_timer_game_interpreter_command_111
  end
  #--------------------------------------------------------------------------
  # * Control Variables
  #--------------------------------------------------------------------------
  def command_122
    # Initialize value
    value = 0
    # If @params[3] is set to other
    if @params[3] == 7
      # If @params[4] is set to timer
      if @params[4] == 5  # timer
        # Call modified method
        $game_timer.control_variables(@params)
        # Continue
        return true
      end
    end
    # Call aliased method
    scriptastic_timer_game_interpreter_command_122
  end
  #--------------------------------------------------------------------------
  # * Control Timer
  #--------------------------------------------------------------------------
  def command_124
    # Call modified method
    $game_timer.control_timer(@params)
    # Continue
    return true
  end
end

#----------------------------------------------------------------------------
# * End Check for RGSS2
#----------------------------------------------------------------------------
end

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

class Scene_Map
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias scriptastic_timer_scene_map_update update
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Call original method
    scriptastic_timer_scene_map_update
    # Update Game_Timer
    $game_timer.update
  end
end

#==============================================================================
# ** Scene_Battle
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias scriptastic_timer_scene_battle_update update
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update Game_Timer
    $game_timer.update
    # Abort Battle if an appropriate timer has ended
    $game_temp.battle_abort = true if $game_timer.end_battle?
    # Call original method
    scriptastic_timer_scene_battle_update
  end
end

#==============================================================================
# ** Game_Timer
#------------------------------------------------------------------------------
#  This class performs processing for in-script timers.
# Refer to "$game_temp.game_timer" for the instance of this class.
#==============================================================================

class Game_Timer
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    # Create timer hash
    @timers = {}
    # Set timer hash defaults
    @timers.default = {'time' => 0, 'active' => false, 'display' => false,
                      'endbattle' => false, 'finished' => false,
                      'save' => false}
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Iterate through all timers
    @timers.keys.each { |key|
    # Skip if the current timer is inactive, otherwise deduct timer
    @timers[key]['active'] == false ? next : @timers[key]['time'] -= 1
    # Set the timer to finished if the frame count is 0 or less
    @timers[key]['active'] = false and
    @timers[key]['finished'] = true if @timers[key]['time'] <= 0}
  end
  #--------------------------------------------------------------------------
  # * Frame Update (individual timer)
  #     key : key for specific timer
  #--------------------------------------------------------------------------
  def update_single(key)
    # Return if the current timer is inactive, otherwise deduct timer
    @timers[key]['active'] == false ? return : @timers[key]['time'] -= 1
    # Set the timer to finished if the frame count is 0 or less
    @timers[key]['active'] = false and
    @timers[key]['finished'] = true if @timers[key]['time'] <= 0
  end
  #--------------------------------------------------------------------------
  # * Start Timer
  #     seconds : time, in seconds
  #     key     : key for specific timer
  #--------------------------------------------------------------------------
  def start(seconds, key)
    # Check the validity of the second value
    unless seconds.is_a?(Integer)
      # Print an error and exit if the second value is invalid
      p "You must set a timer's remaining time to a numeric value"
      exit
    end
    # Setup new timer
    @timers[key] = {'time' => seconds * Graphics.frame_rate, 'active' => true,
                    'display' => false, 'endbattle' => false,
                    'finished' => false, 'save' => false}
  end
  #--------------------------------------------------------------------------
  # * Start Timer (extended)
  #     hours   : added hours (not including minutes or seconds)
  #     minutes : added minutes (not including hours or seconds)
  #     seconds : added seconds (not including hours or minutes)
  #     key     : key for specific timer
  #--------------------------------------------------------------------------
  def extended_start(hours, minutes, seconds, key)
    # Check the validity of the time value
    unless (hours.is_a?(Integer) == true && minutes.is_a?(Integer) == true &&
           seconds.is_a?(Integer) == true)
      # Print an error and exit if the time value is invalid
      p "You must set a timer's remaining time to a numeric value"
      exit
    end
    # Calculate time
    time = ((hours * 3600 * Graphics.frame_rate) +
           (minutes * 60 * Graphics.frame_rate) +
           (seconds * Graphics.frame_rate))
    # Setup new timer
    @timers[key] = {'time' => time, 'active' => true, 'display' => false,
                    'endbattle' => false, 'finished' => false, 'save' => false}
  end
  #--------------------------------------------------------------------------
  # * Check Timer
  #     key : key for specific timer
  #--------------------------------------------------------------------------
  def finished?(key)
    # Return false if the timer has not finished
    return false if @timers[key]['finished'] == false
    # Delete timer
    @timers.delete(key)
    return true
  end
  #--------------------------------------------------------------------------
  # * Check Timer (Debugging)
  #     key : key for specific timer
  #--------------------------------------------------------------------------
  def check_timing(key)
    return unless ($DEBUG == true || $TEST == true)
    # If the Timers hash includes the key
    if @timers.include?(key)
      # If the timer has time remaining
      if @timers[key]['finished'] == false
        # Set up message and print
        s = 'The timer named ' + key.to_s + ' has ' + @timers[key]['time'].to_s
        s += ' frames remaining.'
        p s
      else
        # Print message
        p 'The timer named ' + key.to_s + ' has reached zero.'
      end
      # Print message if timer is inactive
      if @timers[key]['active'] == false
        p 'The timer named ' + key.to_s + ' is currently inactive.'
      end
      # Print message if timer is set to end battle
      if @timers[key]['endbattle'] == true
        p 'The timer named ' + key.to_s + ' is currently set to end battle.'
      end
      # print message if timer is set to be saved in the file
      if @timers[key]['save'] == true
        p 'The timer named ' + key.to_s + ' is currently saved with the game.'
      end
    else
      # Print message if the timer does not exist
      p 'The timer named ' + key.to_s + ' does not exist.'
    end
  end
  #--------------------------------------------------------------------------
  # * Kill Timer
  #     key : key for specific timer
  #--------------------------------------------------------------------------
  def kill_timer(key)
    # Delete the timer if it exists
    @timers.delete(key) if @timers.include?(key)
  end
  #--------------------------------------------------------------------------
  # * Adjust Remaining Time
  #     setting : type of operation
  #     value   : value to use in the operation
  #     key     : key for specific timer
  #--------------------------------------------------------------------------
  def adjust_time(setting, value, key)
    # Print an error and exit if the value is invalid
    unless value.is_a?(Integer)
      p "You must modify a timer's remaining time with a numeric value"
      exit
    end
    # Print an error and exit if the setting is invalid
    unless (setting >= 1 && setting <= 5)
      p "You have used an invalid setting when modifying the timer."
      exit
    end
    # Perform the appropriate math function based on setting variable
    case setting     
    when 1 then @timers[key]['time'] += value
    when 2 then @timers[key]['time'] -= value
    when 3 then @timers[key]['time'] *= value
    when 4 then @timers[key]['time'] /= value
    when 5 then @timers[key]['time'] %= value
    end
  end
  #--------------------------------------------------------------------------
  # * Set Timer's Active Boolean
  #     key  : key for specific timer
  #     bool : true or false, where true ends battle when timer is at 0
  #--------------------------------------------------------------------------
  def active(key, bool)
    # Check bool's validity
    unless (bool == true || bool == false)
      # Print an error and exit if bool is invalid
      p "You must set a timer's active boolean to either true or false."
      exit
    end
    # Set timer boolean if it exists
    @timers[key]['active'] == bool if @timers.include?(key)
  end
  #--------------------------------------------------------------------------
  # * Timer Active?
  #     key : key for specific timer
  #--------------------------------------------------------------------------
  def active?(key)
    # Return active value for specified timer
    return @timers[key]['active']
  end
  #--------------------------------------------------------------------------
  # * Set Timer's End Battle Boolean
  #     key  : key for specific timer
  #     bool : true or false, where true ends battle when timer is at 0
  #--------------------------------------------------------------------------
  def end_battle(key, bool)
    # Check bool's validity
    unless (bool == true || bool == false)
      # Print an error and exit if bool is invalid
      p "You must set a timer's end battle boolean to either true or false."
      exit
    end
    # Set timer boolean if it exists
    @timers[key]['endbattle'] == bool if @timers.include?(key)
  end
  #--------------------------------------------------------------------------
  # * End Battle?
  #--------------------------------------------------------------------------
  def end_battle?
    # Iterate through all timers
    @timers.keys.each {|key| next if @timers[key]['active'] == false
    # Return true if the timer is finished and is set to end battle
    return true if @timers[key]['endbattle'] && @timers[key]['time'] <= 0}
    return false
  end
  #--------------------------------------------------------------------------
  # * Set Timer's Display Boolean
  #     key  : key for specific timer
  #     bool : true or false, where true means the timer is displayed
  #--------------------------------------------------------------------------
  def display(key, bool)
    # Check bool's validity
    unless (bool == true || bool == false)
      # Print an error and exit if bool is invalid
      p "You must set a timer's display boolean to either true or false."
      exit
    end
    # Set timer boolean if it exists
    @timers[key]['display'] = bool if @timers.include?(key)
  end
  #--------------------------------------------------------------------------
  # * Display Timer?
  #--------------------------------------------------------------------------
  def display?
    # Iterate through all timers
    @timers.keys.each {|key| next if @timers[key]['active'] == false
    # Return remaining time if the timer is set to display
    return @timers[key]['time'] if @timers[key]['display'] == true}
    return false
  end
  #--------------------------------------------------------------------------
  # * Set Timer's Save Boolean
  #     key  : key for specific timer
  #     bool : true or false, where true means the timer is displayed
  #--------------------------------------------------------------------------
  def save(key, bool)
    # Check bool's validity
    unless (bool == true || bool == false)
      # Print an error and exit if bool is invalid
      p "You must set a timer's save boolean to either true or false."
      exit
    end
    # Set timer boolean if it exists
    @timers[key]['save'] = bool if @timers.include?(key)
  end
  #--------------------------------------------------------------------------
  # * Save Timer?
  #--------------------------------------------------------------------------
  def save?
    # Setup save hash
    save = {}
    # Iterate through timers and add the timer to the hash if it is set to save
    @timers.keys.each{|x| save[x] = @timers[x] if @timers[x]['save'] == true}
    return save
  end
  #--------------------------------------------------------------------------
  # * Load Saved Timer
  #--------------------------------------------------------------------------
  def load_save(timers)
    # Add timers to hash if any have been loaded
    timers.keys.each {|key| @timers[key] = timers[key]} unless timers.empty?
  end
  #--------------------------------------------------------------------------
  # * Conditional Branch
  #--------------------------------------------------------------------------
  def conditional_branch(parameters)
    # If event timer is active
    if active?('event')
      # Set timer variable to currently displayed timer
      timer = display?
      # Skip command if no timer is currently displayed
      return false if timer == false
      # Calculate remaining timer
      sec = timer / Graphics.frame_rate
      # If checking for a value <= remaining time
      if parameters[2] == 0
        # set result to the truth value of remaining time >= checked time
        result = (sec >= parameters[1])
      else
        # set result to the truth value of remaining time <= checked time
        result = (sec <= parameters[1])
      end
    else
      # Skip command if timer is not active
      return false
    end
  end
  #--------------------------------------------------------------------------
  # * Control Variables
  #--------------------------------------------------------------------------
  def control_variables(parameters)
    timer = display?
    timer = 0 if timer == false
    value = timer / Graphics.frame_rate
    # Loop for group control
    for i in parameters[0] .. parameters[1]
      # Branch with control
      case parameters[2]
      when 0 then $game_variables[i] = value
      when 1 then $game_variables[i] += value
      when 2 then$game_variables[i] -= value
      when 3 then $game_variables[i] *= value
      when 4  # divide
        if value != 0
          $game_variables[i] /= value
        end
      when 5  # remainder
        if value != 0
          $game_variables[i] %= value
        end
      end
      # Max / Min limit check
      $game_variables[i] = 99999999 if $game_variables[i] > 99999999
      $game_variables[i] = -99999999 if $game_variables[i] < -99999999
    end
    # Refresh map
    $game_map.need_refresh = true
  end
  #--------------------------------------------------------------------------
  # * Control Timer
  #--------------------------------------------------------------------------
  def control_timer(parameters)
    # If started
    if parameters[0] == 0
      # Start timer
      $game_timer.start(parameters[1], 'event')
      # Set timer to end battle
      $game_timer.end_battle('event', true)
      # Display timer
      $game_timer.display('event', true)
      # Set timer to save with game
      $game_timer.save('event', true)
    # If stopped
    else
      # Hide timer
      $game_timer.active('event', false)
    end
  end
end

begin
  # Setup Game_Timer
  $game_timer = Game_Timer.new
end