#≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
# ** Glitchfinder's Multiple Timers               [RPG Maker XP] [RPG Maker VX]
#    Version 2.31
#------------------------------------------------------------------------------
#  This script helps scripters and eventers use multiple hidden in-game timers
#  that can be called and modified at any time.
#==============================================================================
# * 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
#==============================================================================
# * 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_temp.game_timer.start(seconds, timer_number)
#  Where seconds is the number of seconds you want the timer to run, and
#  timer_number is the number of that specific timer. You must use a seperate
#  number for every timer, because when one finishes, only the first script
#  checking for that number will return with the timer completed.
#
#  To start an extended timer, use the following call:
#    $game_temp.game_timer.extended_start(hours, minutes, seconds, timer_number)
#  Where hours, minutes, and seconds are the number of hours, minutes, and
#  seconds you wish the timer to run. timer_number follows the same rules as it
#  does in the method above.
#
#  To check whether or not a timer has finished, use the following call:
#    $game_temp.game_timer.check(timer_number)
#  Where timer_number is the number of the timer that you need to check. See
#  the rules above regarding timer_number.
#
#  To stop a timer, you use the following call:
#    $game_temp.game_timer.kill_timer(timer_number)
#  Where timer_number is the number of the timer you wish to stop. Please read
#  the above rules regarding timer_number.
#
#  To adjust the remaining time, use the following call:
#    $game_temp.game_timer.adjust_time(setting, value, timer_number)
#  Where setting is what kind of operation you wish to perform. (1 for
#  addition, 2 for subtraction, 3 for multiplication, and 4 for division) Value
#  must be the number of seconds you wish to add or subtract, or the number you
#  wish to add or divide by. Finally, timer_number is the number of the timer
#  you wish to edit. (Once again, see above for rules regarding timer_number)
#
#  To update a single timer seperately, you must use the following call:
#    $game_temp.game_timer.update_single(timer_number)
#  Where timer_number is the number of the timer you wish to update. This is
#  meant to be used to update specific timers in scenes that this script
#  doesn't normally update in. (In other words, anything other than Scene_Map)
#  It can also be used to update a specific timer at a rate that is faster than
#  normal, when variable speeds are necessary.
#
#  For debugging purposes, check the timer with the following call:
#    $game_temp.game_timer.check_timing(timer_number)
#  Where timer_number is the number of the timer you are checking. (once again,
#  see above for details on timer_number) It will either display the remaining
#  frames before the timer is finished, or it will tell you that the timer has
#  finished.
#==============================================================================
# * Glitchfinder's Advice
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  This script is meant for people with a medium to high 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 script and events, and will function in
#  both cases.
#==============================================================================
# * 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, within reason.
#≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡

#==============================================================================
# ** 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
  module Timers
    # This is the actual timer, each value is a number of frames
    Timers = [
      0
    ]
    # This tells the script that a timer is currently active
    Active_Timers = [
      2
    ]
    # This stores the specific number for each active timer
    Timer_Numbers = [
      0
    ]
    # This tells the script which timers have finished
    Finished_Timer_Numbers = [
      0
    ]
  end
  
  #--------------------------------------------------------------------------
  # * Update Timer
  #--------------------------------------------------------------------------
  def update
    # While there is an active timer,
    while Timers::Active_Timers.include?(1)
      # Return the number of the active timer
      position = Timers::Active_Timers.index(1)
      # Temporarily set the timer info to 0
      Timers::Active_Timers[position] = 0
      # Set value to current frame count and subtract one
      array_value = Timers::Timers[position]
      value = array_value.to_s.to_i
      value -= 1
      # Update frame count in the array
      Timers::Timers[position] = value.to_a
      # If the frame count is <= 0,
      if value <= 0
        # Add the timer number to the correct array, to indicate it is finished
        dead_timer = Timers::Timer_Numbers[position]
        timer_number = dead_timer.to_s.to_i
        Timers::Finished_Timer_Numbers.insert 1, timer_number
        # Delete the current timer's frame count and information
        Timers::Timers.delete_at(position)
        Timers::Active_Timers.delete_at(position)
        Timers::Timer_Numbers.delete_at(position)
      end
    end
    # While there are active timers with info set temporarily to 0,
    while Timers::Active_Timers.include?(0)
      # Set their info back to 1
      position = Timers::Active_Timers.index(0)
      Timers::Active_Timers[position] = 1
    end
  end
  #--------------------------------------------------------------------------
  # * Update Specific Timer
  #--------------------------------------------------------------------------
  def update_single(timer_number)
    # Find the position of the specified timer
    position = Timers::Timer_Numbers.index(timer_number)
    # If the specified timer is running,
    if position != nil
      # Set value to current frame count and subtract one
      array_value = Timers::Timers[position]
      value = array_value.to_s.to_i
      value -= 1
      # Update frame count in the array
      Timers::Timers[position] = value.to_a
      # If the frame count is <= 0,
      if value <= 0
        # Add the timer number to the correct array, to indicate it is finished
        Timers::Finished_Timer_Numbers.insert 1, timer_number
        # Delete the current timer's frame count and information
        Timers::Timers.delete_at(position)
        Timers::Active_Timers.delete_at(position)
        Timers::Timer_Numbers.delete_at(position)
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Start Timer
  #--------------------------------------------------------------------------
  def start(seconds, timer_number)
    # Add frame count to the correct array
    Timers::Timers.insert(1,[seconds*40])
    # Set the timer to active
    Timers::Active_Timers.insert(1, 1)
    # Set the timer number
    Timers::Timer_Numbers.insert(1, timer_number)
  end
  #--------------------------------------------------------------------------
  # * Extended Start Timer
  #--------------------------------------------------------------------------
  def extended_start(hours, minutes, seconds, timer_number)
    # Add frame count to the correct array
    Timers::Timers.insert(1,[(hours*144000)+(minutes*2400)+(seconds*40)])
    # Set the timer to active
    Timers::Active_Timers.insert(1, 1)
    # Set the timer number
    Timers::Timer_Numbers.insert(1, timer_number )
  end
  #--------------------------------------------------------------------------
  # * Check Timer
  #--------------------------------------------------------------------------
  def check(timer_number)
    # If the timer number matches a finished timer,
    if Timers::Finished_Timer_Numbers.include?(timer_number)
      # Delete the data from the array
      Timers::Finished_Timer_Numbers.delete(timer_number)
      # Return as finished
      return true
    else
      # Return as still timing
      return false
    end
  end
  #--------------------------------------------------------------------------
  # * Check Timer
  #--------------------------------------------------------------------------
  def check_timing(timer_number)
    if Timers::Timer_Numbers.include?(timer_number)
      # Set position to specified timer
      position = Timers::Timer_Numbers.index(timer_number)
      # If the timer is still running,
      # If the specified timer is finished,
      if Timers::Timers[position] == nil
        # Print "Time is up."
        print "Time is up."
      # Otherwise,
      else
        # Print number of remaining frames
        time = Timers::Timers[position].to_s
        print time
      end
    else
      # Print "Time is up."
      print "Time is up."
    end
  end
  #--------------------------------------------------------------------------
  # * Kill Timer
  #--------------------------------------------------------------------------
  def kill_timer(timer_number)
    # Set position to the current timer
    position = Timers::Timer_Numbers.index(timer_number)
    # If the specified timer is still running,
    if position != nil
      # Add the timer number to the correct array, to indicate it is finished
      Timers::Finished_Timer_Numbers.insert 1, timer_number
      # Delete the current timer's frame count and information
      Timers::Timers.delete_at(position)
      Timers::Active_Timers.delete_at(position)
      Timers::Timer_Numbers.delete_at(position)
    end
  end
  #--------------------------------------------------------------------------
  # * Adjust Remaining Time
  #--------------------------------------------------------------------------
  def adjust_time(setting, value, timer_number)
    position = Timers::Timer_Numbers.index(timer_number)
    case setting
    when 1
      time = Timers::Timers[position].to_i
      new_time = time + (value * Graphics.frame_rate)
      Timers::Timers[position] = new_time.to_a
    when 2
      time = Timers::Timers[position].to_i
      new_time = time - (value * Graphics.frame_rate)
      Timers::Timers[position] = new_time.to_a
    when 3
      time = Timers::Timers[position].to_i
      new_time = time * value
      Timers::Timers[position] = new_time.to_a
    when 4
      time = Timers::Timers[position].to_i
      new_time = time / value
      Timers::Timers[position] = new_time.to_a
    end
  end   
end