#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# Game Data Reloader by Blizzard
# Version: 1.0
# Type: Debug Tool
# Date: 11.9.2010
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#   
#  This work is protected by the following license:
# #----------------------------------------------------------------------------
# #  
# #  Creative Commons - Attribution-NonCommercial-ShareAlike 3.0 Unported
# #  ( http://creativecommons.org/licenses/by-nc-sa/3.0/ )
# #  
# #  You are free:
# #  
# #  to Share - to copy, distribute and transmit the work
# #  to Remix - to adapt the work
# #  
# #  Under the following conditions:
# #  
# #  Attribution. You must attribute the work in the manner specified by the
# #  author or licensor (but not in any way that suggests that they endorse you
# #  or your use of the work).
# #  
# #  Noncommercial. You may not use this work for commercial purposes.
# #  
# #  Share alike. If you alter, transform, or build upon this work, you may
# #  distribute the resulting work only under the same or similar license to
# #  this one.
# #  
# #  - For any reuse or distribution, you must make clear to others the license
# #    terms of this work. The best way to do this is with a link to this web
# #    page.
# #  
# #  - Any of the above conditions can be waived if you get permission from the
# #    copyright holder.
# #  
# #  - Nothing in this license impairs or restricts the author's moral rights.
# #  
# #----------------------------------------------------------------------------
# 
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# 
# Compatibility:
# 
#   99% compatible with SDK v1.x. 98% compatible with SDK 2.x. Compatible with
#   Blizz-ABS's Intelligent Passability. When using Custom Controls, the
#   buttons can be mapped to any buttons on the keyboard.
# 
# 
# Features:
# 
#   - allows you to reload all game data during the game
#   - allows you to refresh the current map during the game
# 
# 
# Explanation:
# 
#   This Script will allow you to reload game data while playtesting the game.
#   It can reload database game data and map data including refreshing the
#   current map you are in so you can view the changes you have made in your
#   game right away without restarting it. When refreshing the map, it will be
#   reloaded as if you exited and reentered the map, resetting all events to
#   its initial positions and resetting the map into its initial state. To be
#   able to make actual use of this script, always run the game from the
#   Game.exe, never from the editor.
# 
# 
# Notes:
# 
#   - When refreshing the map during event execution, the event execution will
#     continue. This can cause bugs (especially during auto-run events that
#     change switches and variable) so be careful if you want to refresh a map
#     during event execution.
#   - Changes in scripts cannot be reloaded. If you made changes to a script,
#     you have to restart the game for the changes to apply.
# 
# 
# If you find any bugs, please report them here:
# http://forum.chaos-project.com
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

BUTTON_RELOAD = Input::F6 # the button to reload the game data
BUTTON_REFRESH = Input::F5 # the button to refresh the map

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

$game_data_reloader = 1.0

#==============================================================================
# module Input
#==============================================================================

module Input
  
  class << Input
    alias update_reloader update
  end
  
  def self.update
    update_reloader
    reloading = Input.trigger?(BUTTON_RELOAD)
    refreshing = Input.trigger?(BUTTON_REFRESH)
    if reloading || refreshing
      load_sprite = Sprite.new
      load_sprite.z = 100000
      load_sprite.bitmap = Bitmap.new(640, 480)
      load_sprite.bitmap.font.color = Color.new(0, 0, 0)
      text = reloading ? 'Reloading...' : 'Refreshing...'
      load_sprite.bitmap.draw_text(-1, 223, 640, 32, text, 1)
      load_sprite.bitmap.draw_text(1, 223, 640, 32, text, 1)
      load_sprite.bitmap.draw_text(-1, 225, 640, 32, text, 1)
      load_sprite.bitmap.draw_text(1, 225, 640, 32, text, 1)
      load_sprite.bitmap.font.color = Color.new(255, 255, 0)
      load_sprite.bitmap.draw_text(0, 224, 640, 32, text, 1)
      10.times {Graphics.update}
      if reloading
        RPG.init_game
        $game_map.need_refresh = true if $scene.is_a?(Scene_Map)
      end
      if refreshing
        $BlizzABS.util.check_map_data if $BlizzABS != nil
        $scene.spriteset.dispose
        if $scene.is_a?(Scene_Map)
          $game_map.setup($game_map.map_id)
          $scene.spriteset = Spriteset_Map.new
          $game_player.center($game_player.x, $game_player.y)
          $game_map.need_refresh = true
        else
          $scene.spriteset = Spriteset_Battle.new
        end
      end
      load_sprite.dispose
    end
  end
  
end

#==============================================================================
# module RPG
#==============================================================================

module RPG

  def self.init_game
    # normal game data
    $data_actors        = load_data('Data/Actors.rxdata')
    $data_classes       = load_data('Data/Classes.rxdata')
    $data_skills        = load_data('Data/Skills.rxdata')
    $data_items         = load_data('Data/Items.rxdata')
    $data_weapons       = load_data('Data/Weapons.rxdata')
    $data_armors        = load_data('Data/Armors.rxdata')
    $data_enemies       = load_data('Data/Enemies.rxdata')
    $data_troops        = load_data('Data/Troops.rxdata')
    $data_states        = load_data('Data/States.rxdata')
    $data_animations    = load_data('Data/Animations.rxdata')
    $data_tilesets      = load_data('Data/Tilesets.rxdata')
    $data_common_events = load_data('Data/CommonEvents.rxdata')
    $data_system        = load_data('Data/System.rxdata')
    # used by some CMSes, etc., add more stuff here if you need it
    $map_infos          = load_data('Data/MapInfos.rxdata')
    $map_infos.each_key {|key| $map_infos[key] = $map_infos[key].name}
  end
  
end

#==============================================================================
# Scene_Map
#==============================================================================

class Scene_Map
  
  attr_accessor :spriteset
  
end

#==============================================================================
# Scene_Battle
#==============================================================================

class Scene_Battle
  
  attr_accessor :spriteset
  
end
