#===============================================================================
# 
# King Arthur's Scripts - Area-Based Battle Backgrounds
# Internal Script Name: KA-Scripts::BattleBG
# Last Date Updated: 2010.11.26
# 
# This script reads in an image file and draws it as a background behind a
# battle instead of the default swirly screenshot of the current game map.
#
# Criteria for which background to draw is based on areas defined on the map
# and works from the names of the areas defined.
# Two image files are also directly specified for locations outside of named
# areas and battle tests through the editor respectively.
#
# Background image files are read in from the following directories:
# * /BattleBG
# * /Parallaxes
# * /System
# * /Pictures
# All directories are a sub-directory of the /Graphics directory.
# Read priority is followed in the order given above, from top to bottom.
#
# /BattleBG is a script-specific directory not native to RMVX and is available
# for people who wish to keep battle backgrounds separate from other image
# files.
#
# If a specified image file is not found in one directory this script will move
# on to the next until finally finding the file or passing a "File not found"
# exception error.
# 
#===============================================================================
# Updates
# -----------------------------------------------------------------------------
# o 2010.11.26 - Script finished.
# o 2010.11.25 - Script started.
#===============================================================================
# Instructions
# -----------------------------------------------------------------------------
# To install this script, open up your script editor and copy/paste this 
# script to an open slot below ▼ Material and above ▼ Main.
#
# Scroll down and edit the module as you see fitting for your game.
#
# If applicable, create a /BattleBG directory inside /Graphics.
#===============================================================================
# Credits
# -----------------------------------------------------------------------------
# o King Arthur - Script author.
#===============================================================================
# Compatibility
# -----------------------------------------------------------------------------
# - Works with: RMVX DBS, RPG Tankentai, Battle Engine Melody.
#               No guarantees, but is assumed to be compatible with 
#               other battle systems as well. Try it out and see if it works!
#
# - new classes: none
#
# - aliased methods: Spriteset_Battle: create_battlefloor
#
# - overwriten methods: Spriteset_Battle: create_battleback
#
# - new methods: Cache: self.battlebg
#                Game_Player: get_area_name
#                Spriteset_Battle: draw_battle_background
#                Spriteset_Battle: file_exist?
# -----------------------------------------------------------------------------
# Notes: N/A
#===============================================================================
# Terms of Use
# -----------------------------------------------------------------------------
# This script may be used freely in non-commercial applications assuming
# the following conditions are all met:
#
# o This information header must be kept in full without any modifications.
#
# o Proper credit must be attributed to everyone listed under "Credits".
#
# o The project this script is used in must be open source (unencrypted) and
#   this script's source code must be readily available for viewing within the
#   RPG Maker VX editor in the final release of the game project.
#   The script author DOES NOT support project encryption and does not condone
#   use of this script in projects that utilize encryption.
# -----------------------------------------------------------------------------
# COMMERCIAL USE OF THIS SCRIPT OR ANY DERIATIVE THEREOF IS NOT PERMITTED
# UNDER ANY CIRCUMSTANCES! NO EXCEPTIONS WILL EVER BE GIVEN!
#===============================================================================

$imported = {} if $imported == nil
$imported["KA-Scripts::BattleBG"] = true

module KingArthur
  module Scripts
    module BattleBG

      #=========================================================================
      # Internal System Battle Background Hash
      # -----------------------------------------------------------------------
      # This hash stores information regarding the image files used for
      # when a hero is located outside of any area or when a battle test is
      # initiated via the RMVX editor.
      # Syntax: "System Value" => "Image Filename",
      #
      # NOTE: All values are expected to be strings. This means you must
      #       encapsulate all values with quotes.
      #       RTP files will NOT be detected! Import them into your project!
      #
      # IMPORTANT: Do not change or remove any of the system values. Only
      #            modify their associated image filenames.
      #=========================================================================
      SYSTEM_BG = { # Follow instructions above.
        "DEFAULT" => "FallBackBG",
        "BTEST"   => "BattleTestBG",
      } # Do not delete this!

      #=========================================================================
      # Area => Battle Background Hash
      # -----------------------------------------------------------------------
      # This hash stores information concerning the relationship between
      # area names and their associated battle background image files.
      # Syntax: "Area Name" => "Image Filename",
      #
      # NOTE: All values are expected to be strings. This means you must
      #       encapsulate all values with quotes.
      #       RTP files will NOT be detected! Import them into your project!
      #=========================================================================
      BG_HASH = { # Follow instructions above.
        "BattleArea" => "AssociatedBattleBG",
      } # Do not delete this!

      # This constant is used to determine whether you would
      # like to draw the battlefloor on top of the background.
      # Default value is false.
      # NOTE: Will be overridden to "true" for compatibility purposes
      #       if RPG Tankentai is installed. Refer to RPG Tankentai's own
      #       battlefloor configurations if this applies to you.
      BATTLEFLOOR = false

    end
  end
end

#===============================================================================
# Editting anything past this point may potentially result in causing computer
# damage, incontinence, explosion of user's head, coma, death, and/or halitosis.
# Therefore, edit at your own risk.
#===============================================================================

#===============================================================================
# Cache
#===============================================================================
module Cache

  #--------------------------------------------------------------------------
  # new method: self.battlebg
  #--------------------------------------------------------------------------
  def self.battlebg(filename)
    load_bitmap("Graphics/BattleBG/", filename)
  end

end


#===============================================================================
# Game_Player < Game_Character
#===============================================================================
class Game_Player < Game_Character

  #--------------------------------------------------------------------------
  # new method: get_area_name
  #--------------------------------------------------------------------------
  def get_area_name
    if !$BTEST
      for area in $data_areas.values
        return area.name if in_area?(area)
      end
    end
    return nil
  end

end

#===============================================================================
# Spriteset_Battle
#===============================================================================
class Spriteset_Battle

  #--------------------------------------------------------------------------
  # new method: draw_battle_background
  #--------------------------------------------------------------------------
  def draw_battle_background
    if $game_player.get_area_name != nil
      image = KingArthur::Scripts::BattleBG::BG_HASH[$game_player.get_area_name]
    elsif $BTEST
      image = KingArthur::Scripts::BattleBG::SYSTEM_BG["BTEST"]
    else
      image = KingArthur::Scripts::BattleBG::SYSTEM_BG["DEFAULT"]
    end
    if file_exist?(image)
      @battleback_sprite = Sprite.new(@viewport1)
      case @battlebg_dir
      when "BattleBG/"
        @battleback_sprite.bitmap = Cache.battlebg(image)
      when "Parallaxes/"
        @battleback_sprite.bitmap = Cache.parallax(image)
      when "System/"
        @battleback_sprite.bitmap = Cache.system(image)
      when "Pictures/"
        @battleback_sprite.bitmap = Cache.pictures(image)
      end
    end
  end

  #--------------------------------------------------------------------------
  # new method: file_exist?
  #--------------------------------------------------------------------------
  def file_exist?(file)
    dir = ["BattleBG/", "Parallaxes/", "System/", "Pictures/"]
    ext = [".png", ".jpg", ".bmp"]
    for d in dir
      for e in ext
        if FileTest.exist?("Graphics/" + d + file + e)
          @battlebg_dir = d
          return true
        end
      end
    end
    text = "- The specified file \"" + file + "\" was not found"
    raise Errno::ENOENT, text
  end

  #--------------------------------------------------------------------------
  # overwritten method: create_battleback
  #--------------------------------------------------------------------------
  def create_battleback
    draw_battle_background
  end

  #--------------------------------------------------------------------------
  # aliased method: create_battlefloor
  #--------------------------------------------------------------------------
  alias create_battlefloor_rtp create_battlefloor unless $@
  def create_battlefloor
    @battlefloor_sprite = Sprite.new(@viewport1)
    if $imported["TankentaiSideview"] || KingArthur::Scripts::BattleBG::BATTLEFLOOR
      dispose_battlefloor
      create_battlefloor_rtp
    end
  end

end

#===============================================================================
# 
# END OF FILE
# 
#===============================================================================