#==============================================================================
#                    ==[Self Switch Toggler]==
#                         Version: 1.00
#
#------------------------------------------------------------------------------
#                             NOTES:
# - Detects and disables self switches on the CURRENT MAP.
# - Will only disable self switches that are currently set to TRUE
# - To toggle self switches on the CURRENT MAP, simply call the 
# appropriate script. 
# - prints results of script in console, informs you what switches were disabled
#------------------------------------------------------------------------------
# Gump_S_S_T_A - Script for A Switches
# Gump_S_S_T_B - Script for B Switches
# Gump_S_S_T_C - Script for C Switches
# Gump_S_S_T_D - Script for D Switches
# Gump_S_S_T_ALL - Script for ALL Switches
#------------------------------------------------------------------------------
#                    ALIASED or OVERWRITTEN METHODS:
#
# * No aliased or overwritten methods
#
#------------------------------------------------------------------------------
#                  OTHER DEFAULT SCRIPT ADDITIONS/TWEAKS:
#
# * Added attribute accessor to Game_SelfSwitches: data
#
#------------------------------------------------------------------------------
#                        ==[LEGAL CRAP]==
# - You may modify this collection of scripts as you see fit
# - You may use this collection of scripts freely in any commercial or free game
# - If using this collection of scripts (modified or unmodified) in your game,
# you must provide credit to the scripts origonal author (using full name + nickname)
#
# Scripts Authored by: Matt Sully aka "Gump"
#------------------------------------------------------------------------------
# Version History:
#
# 1.00 - Release Candidate. Added commenting to all scripts + console feedback
# 0.99 - Fixed bug in 'ALL' script (was missing turn off casing for swicth D's)
# 0.98 - Finished 'ALL' script
# 0.01 to 0.97 - Created A to D scripts, started 'ALL' script. Decided to make
# script collection free for public.
#
#==============================================================================

class Game_SelfSwitches
  attr_accessor :data   #accessor for data of self switches
end

#==============================================================================
# * Script for Self Switch A 
#==============================================================================
class Gump_S_S_T_A < Scene_Base
  def main
    p 'Starting self switch data:', $game_self_switches.data
    get_results
    p 'Resulting self switch data:', $game_self_switches.data
    finish_processing
  end
  #--------------------------------------------------------------------------
  # * Logic for only clearing self switches that are on the current map
  #--------------------------------------------------------------------------
  def get_results
    eventpoll = $game_map.events.keys #array of event ids on current map [3, 7]
    mapid = $game_map.map_id
    while eventpoll != nil
      evid = eventpoll.shift
      if evid == nil #if the shifted value is nil
        eventpoll == nil #set the array to nil
        return
      end
      crc = [mapid, evid, "A"] #current calculated key value to compare with
      if $game_self_switches.data.include?(crc)
        $game_self_switches.data.delete(crc)
        @ssreset = true
        p 'self switch key detected and deleted:', crc
      end
    end
  end
  #manually refresh the map if any switches were toggled.
  #needs_refresh did not work for properly refreshing the events.
  #$game_map.update_events also did not properly refresh. 
  def finish_processing
    if @ssreset == true
      @ssreset == false
      $game_map.setup($game_map.map_id)
      $game_player.center($game_player.x, $game_player.y)
      $game_player.make_encounter_count
    end
    SceneManager.return
  end
end


#==============================================================================
# * Script for Self Switch B
#==============================================================================
class Gump_S_S_T_B < Scene_Base
  def main
    p 'Starting self switch data:', $game_self_switches.data
    get_results
    p 'Resulting self switch data:', $game_self_switches.data
    finish_processing
  end
  #--------------------------------------------------------------------------
  # * Logic for only clearing self switches that are on the current map
  #--------------------------------------------------------------------------
  def get_results
    eventpoll = $game_map.events.keys #array of event ids on current map [3, 7]
    mapid = $game_map.map_id
    while eventpoll != nil
      evid = eventpoll.shift
      if evid == nil #if the shifted value is nil
        eventpoll == nil #set the array to nil
        return
      end
      crc = [mapid, evid, "B"] #current calculated key value to compare with
      if $game_self_switches.data.include?(crc)
        $game_self_switches.data.delete(crc)
        @ssreset = true
        p 'self switch key detected and deleted:', crc
      end
    end
  end
  #manually refresh the map if any switches were toggled.
  #needs_refresh did not work for properly refreshing the events.
  #$game_map.update_events also did not properly refresh. 
  def finish_processing
    if @ssreset == true
      @ssreset == false
      $game_map.setup($game_map.map_id)
      $game_player.center($game_player.x, $game_player.y)
      $game_player.make_encounter_count
    end
    SceneManager.return
  end
end


#==============================================================================
# * Script for Self Switch C
#==============================================================================
class Gump_S_S_T_C < Scene_Base
  def main
    p 'Starting self switch data:', $game_self_switches.data
    get_results
    p 'Resulting self switch data:', $game_self_switches.data
    finish_processing
  end
  #--------------------------------------------------------------------------
  # * Logic for only clearing self switches that are on the current map
  #--------------------------------------------------------------------------
  def get_results
    eventpoll = $game_map.events.keys #array of event ids on current map [3, 7]
    mapid = $game_map.map_id
    while eventpoll != nil
      evid = eventpoll.shift
      if evid == nil #if the shifted value is nil
        eventpoll == nil #set the array to nil
        return
      end
      crc = [mapid, evid, "C"] #current calculated key value to compare with
      if $game_self_switches.data.include?(crc)
        $game_self_switches.data.delete(crc)
        @ssreset = true
        p 'self switch key detected and deleted:', crc
      end
    end
  end
  #manually refresh the map if any switches were toggled.
  #needs_refresh did not work for properly refreshing the events.
  #$game_map.update_events also did not properly refresh. 
  def finish_processing
    if @ssreset == true
      @ssreset == false
      $game_map.setup($game_map.map_id)
      $game_player.center($game_player.x, $game_player.y)
      $game_player.make_encounter_count
    end
    SceneManager.return
  end
end


#==============================================================================
# * Script for Self Switch D
#==============================================================================
class Gump_S_S_T_D < Scene_Base
  def main
    p 'Starting self switch data:', $game_self_switches.data
    get_results
    p 'Resulting self switch data:', $game_self_switches.data
    finish_processing
  end
  #--------------------------------------------------------------------------
  # * Logic for only clearing self switches that are on the current map
  #--------------------------------------------------------------------------
  def get_results
    eventpoll = $game_map.events.keys #array of event ids on current map [3, 7]
    mapid = $game_map.map_id
    while eventpoll != nil
      evid = eventpoll.shift
      if evid == nil #if the shifted value is nil
        eventpoll == nil #set the array to nil
        return
      end
      crc = [mapid, evid, "D"] #current calculated key value to compare with
      if $game_self_switches.data.include?(crc)
        $game_self_switches.data.delete(crc)
        @ssreset = true
        p 'self switch key detected and deleted:', crc
      end
    end
  end
  #manually refresh the map if any switches were toggled.
  #needs_refresh did not work for properly refreshing the events.
  #$game_map.update_events also did not properly refresh. 
  def finish_processing
    if @ssreset == true
      @ssreset == false
      $game_map.setup($game_map.map_id)
      $game_player.center($game_player.x, $game_player.y)
      $game_player.make_encounter_count
    end
    SceneManager.return
  end
end


#==============================================================================
# * Script for ALL Self Switches
#==============================================================================
class Gump_S_S_T_ALL < Scene_Base
  def main
    p 'Starting self switch data:', $game_self_switches.data
    get_results
    p 'Resulting self switch data:', $game_self_switches.data
    finish_processing
  end
  #--------------------------------------------------------------------------
  # * Logic for only clearing self switches that are on the current map
  #--------------------------------------------------------------------------
  def get_results
    eventpoll = $game_map.events.keys #array of event ids on current map [3, 7]
    mapid = $game_map.map_id
    while eventpoll != nil
      evid = eventpoll.shift
      if evid == nil #if the shifted value is nil
        eventpoll == nil #set the array to nil
        return
      end
      crc = [mapid, evid, "A"] #current calculated key value to compare with
      if $game_self_switches.data.include?(crc)
        $game_self_switches.data.delete(crc)
        @ssreset = true
        p 'self switch key detected and deleted:', crc
      end
      crc = [mapid, evid, "B"] #current calculated key value to compare with
      if $game_self_switches.data.include?(crc)
        $game_self_switches.data.delete(crc)
        @ssreset = true
        p 'self switch key detected and deleted:', crc
      end
      crc = [mapid, evid, "C"] #current calculated key value to compare with
      if $game_self_switches.data.include?(crc)
        $game_self_switches.data.delete(crc)
        @ssreset = true
        p 'self switch key detected and deleted:', crc
      end
      crc = [mapid, evid, "D"] #current calculated key value to compare with
      if $game_self_switches.data.include?(crc)
        $game_self_switches.data.delete(crc)
        @ssreset = true
        p 'self switch key detected and deleted:', crc
      end
    end
  end
  #manually refresh the map if any switches were toggled.
  #needs_refresh did not work for properly refreshing the events.
  #$game_map.update_events also did not properly refresh. 
  def finish_processing
    if @ssreset == true
      @ssreset == false
      $game_map.setup($game_map.map_id)
      $game_player.center($game_player.x, $game_player.y)
      $game_player.make_encounter_count
    end
    SceneManager.return
  end
end