#==============================================================================
# ** Glitchfinder's Multiple Timers (Version 2.32)
#------------------------------------------------------------------------------
# Â 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 ---------------------------------------------------- (??????????)
# Â Version 2 ---------------------------------------------------- (2008-06-11)
# Â Â - Script completely rewritten to work with multiple timers.
# Â Â Version 2.1 ------------------------------------------------- (2002-06-18)
# Â Â - Added the check_timing() command. Bug with check() command fixed.
# Â Â Version 2.11 ------------------------------------------------ (2007-06-19)
# Â Â - Reformatted and cleaned script.
# Â Â Version 2.21 ------------------------------------------------ (2007-06-22)
# Â Â - Added the extended_start() command.
# Â Â Version 2.31 ------------------------------------------------ (2007-07-20)
# Â Â - Added the update_single(), kill_timer(), and adjust_time() commands.
# Â Â Version 2.32 ------------------------------------------------ (2007-07-24)
# Â Â - Fixed bug with VX and the start() aand extended_start() commands.
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# * 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
# normall 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.
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# * Advice
# Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â -
# Â This script is meant for people with at least basic scripting knowledge. It
# may be difficult to use, and thus, if you are unsure of you're abilities, it
# may be simpler to avoid scripted timing.
#
# Â This script has been tested in both script and events, and will function in
# both cases.
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# * Usage
# Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â - Â -
# Â This script may be used in any commercial or non-commercial project.
#
# Â Please credit Glitchfinder if you use this script.
#==============================================================================
Â
#==============================================================================
# ** Game_Temp
#------------------------------------------------------------------------------
# Â This class handles temporary data that is not included with save data.
# Â Refer to "$game_temp" for the instance of this class.
#==============================================================================
Â
class Game_Temp
 attr_accessor :game_timer
Â
 alias old_init initialize
 def initialize
  old_init
  @game_timer = Game_Timer.new
 end
end
Â
#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
# Â This class performs map screen processing.
#==============================================================================
Â
class Scene_Map
 alias old_update update
 def update
  #old_update
  $game_temp.game_timer.update
  old_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
 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*Graphics.frame_rate])
  # 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*3600*(Graphics.frame_rate)) +
  (minutes*(60*Graphics.frame_rate)) + (seconds*Graphics.frame_rate)])
  # 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 (Debugging)
 #--------------------------------------------------------------------------
 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