#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# Creation System by Blizzard
# Version: 1.5b
# Type: In-game Weapon/Armor/Item Creation System
# Date: 5.5.2008
# Date v1.0b: 26.5.2008
# Date v1.3b: 21.7.2008
# Date v1.31b: 29.3.2009
# Date v1.4b: 6.5.2009
# Date v1.5b: 1.6.2009
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#   
#  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:
# 
#   90% compatible with SDK 1.x. 80% compatible with SDK 2.x. WILL corrupt your
#   old savegames. Can cause incompatibilty issues with following scripts:
#   - weapon/armor level systems
#   - ordinary crafting systems
#   - custom targeting systems
#   - exotic equipment systems
# 
# 
# Features:
# 
#   - create weapons, armors and items from scratch within the game
#   - fully operational scenes for creation with wide range of customizability
#   - special skills, normal attributes and material used determine the quality
#     and attributes of created weapons, armors and items
# 
# new in 1.0b:
# 
#   - added option for armors that allows changing the armor type
#   - fixed crash bug that appeared when trying to set an Auto-State, but there
#     is none available and the material menu wasn't entered yet
#   - fixed crash bug with "set_stats"
#   - now beta
# 
# new in 1.3b:
# 
#   - fixed bug where created armors couldn't be equipped
#   - fixed bug where attribute limit wouldn't apply
#   - now it is possible to use armors and weapons as materials as well
#   - improved coding
# 
# new in 1.31b:
# 
#   - fixed bug where it wasn't possible to switch to the target animation
#     selection window
# 
# new in 1.4b:
# 
#   - fixed bug when trying to change the item hit rate
#   - fixed bug where several item attributes wouldn't be limited
# 
# new in 1.5b:
# 
#   - fixed bugs regarding level limitations and attribute appliance
# 
# 
# Introduction:
# 
#   Ordinary crafting systems were yesterday! This script allows you to create
#   completely original Weapons, Armors and Items during the game. It is easy
#   to set up, easy to use and has a very large number of possibilities.
# 
# 
# Instructions:
# 
#   Create an event and use follwowing syntax to call the creation scene:
# 
#     $scene = Scene_Creation.new(XXX)
# 
#   XXX - type of creation (0 for weapons, 1 for armors, 2 for items)
# 
# 
# Configuration:
# 
#   EXP_FACTOR       - higher numbers will make it that more EXP are needed to
#                      increase the creation skill level (default is 5)
#   COST_FACTOR      - how much should the created items cost in relation to
#                      the sum of all the materials used (in %), note that a
#                      higher creation skill will create more valuable items
#   LVL_MAX          - max level of creation skills
#   ELM_LVL_LIMIT    - min level required for object creation with elemental
#                      attributes
#   STA_LVL_LIMIT    - min level required for object creation with status
#                      attributes
#   ATTR_LVL_LIMIT   - min level required for object creation with primary
#                      attribute affection
#   PWR_LVL_LIMIT    - read the "Note" further below for information
#   FORGER_NAME      - name for the weapon creation skill
#   ARMORER_NAME     - name for the armor creation skill
#   ENCHANTER_NAME   - name for the item creation skill
#   ELM_DUMMIES      - add any dummy elements if you are using any
#   STA_DUMMIES      - add any dummy states if you are using any
#   ST_ANIMATIONS    - add any animation IDs which the player can use for
#                      created objects from the start of the game
#   ST_ICONS         - add any icon file names which the player can use for
#                      created objects from the start of the game
#   ST_SOUNDS        - add any sound effect file names which the player can use
#                      for created objects from the start of the game
#   ARMOR            - word for "Armor"
#   EVASION          - word for "Evasion"
#   RIGHT_WINS       - puts the little windows to the right side
#   CREATE_SOUND     - sound effect played when an object was created, the
#                      template you need to use is ('NAME', VOLUME, PITCH)
#   BATTLER          - the battler filename used for animation preview, set to
#                      '' if you don't want to display any battler
#   ATTR_LIMIT       - max possible value of object attributes
#   MAX_HP           - maximum possible HP heal value
#   MAX_SP           - maximum possible SP heal value
#   MAX_ITEMS        - the number of maximum weapons/armors/items in inventory
#   RECURSIVE_USE    - if you set this value to true, you will be able to use
#                      created items as materials
#   REQUIRE_MATERIAL - if you set this value to false, you can create objects
#                      without using any materials, otherwise at least one
#                      piece of one material is required
# 
# Additional commands:
# 
#   Use following syntax to unlock another animation:
# 
#     $game_system.unlock_animation(ID)
#   ID - the ID number of the animation be be unlocked
# 
#   Same goes for icons:
# 
#     $game_system.unlock_icon('ICON_FILE_NAME')
#   ICON_FILE_NAME - the icon's filename without the extension
# 
#   And for sound effects:
# 
#     $game_system.unlock_sound('SOUND_FILE_NAME')
#   SOUND_FILE_NAME - the sound effect's filename without the extension
# 
#   You can add to the skill exp by using:
# 
#     $game_actors[ID].add_creation_exp(X, Y)
#     $game_party.actors[POS].add_creation_exp(X, Y)
#   ID  - ID of the actor in the database
#   POS - position of the actor in the party (starts from 0, not 1!)
#   X   - value of exp to add
#   Y   - which skill (0 = forger, 1 = armorer, 2 = enchanter)
# 
#   You can also modify the level by using:
# 
#     $game_actors[ID].set_creation_lvl(X, Y)
#     $game_party.actors[POS].set_creation_lvl(X, Y)
#   
#   ID  - ID of the actor in the database
#   POS - position of the actor in the party (starts from 0, not 1!)
#   X   - new level
#   Y   - which skill (0 = forger, 1 = armorer, 2 = enchanter)
# 
#   Note that you can read the skill and EXP values by using:
# 
#     actor.lvlforger
#     actor.lvlarmorer
#     actor.lvlenchanter
#     actor.expforger
#     actor.exparmorer
#     actor.expenchanter
# 
# 
# Note:
#   
#   When an actor does not have the required PWR_LVL_LIMIT level to apply
#   attributes onto created weapons and armors, only the highest values of the
#   ATK, PDEF and MDEF values will be used from the available materials.
#   
#   Example:
#     Materials:
#       2 x weapon with ATK:  120
#       1 x weapon with ATK:  110
#       3 x weapon with ATK:   80
#       3 x armor  with PDEF:  50
#       1 x armor  with MDEF:  30
#       2 x armor  with MDEF:  60
#     Result with level PWR_LVL_LIMIT or higher:
#       weapon with ATK:  2 x 120 + 1 x 110 + 3 x  80 = 590
#              with PDEF: 3 x  50                     = 150
#              with MDEF: 1 x  30 + 2 x  60           = 150
#     Result with level lower than PWR_LVL_LIMIT:
#       weapon with ATK:  max(120, 110, 80)           = 120
#   
#   This is to prevent the mass creation of super weapons/armors from many
#   weak weapons/armors until the required level has been reached (or not at
#   all). Also note that using the cross-attributes PDEF and MDEF between
#   weapons and armors is not possible as well until PWR_LVL_LIMIT has been
#   reached.
#   
#   
# IMPORTANT NOTE:
#   
#   Be very careful in your choice of materials and their availability. i.e. A
#   player could create an item that increases STR permanently from a weapon
#   that increases STR when equipped. If you enable this weapon to be bought in
#   shops, the player will be able to create an unlimited ammount of that item
#   and boost his STR up to the maximum.
#   
# 
# If you find any bugs, please report them here:
# http://forum.chaos-project.com
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=

#==============================================================================
# module BlizzCFG
#------------------------------------------------------------------------------
#  This module serves for configuration in Blizzard's scripts.
#==============================================================================

module BlizzCFG

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  
  EXP_FACTOR = 5
  COST_FACTOR = 100
  LVL_MAX = 30
  ELM_LVL_LIMIT = 10
  STA_LVL_LIMIT = 20
  ATTR_LVL_LIMIT = 30
  PWR_LVL_LIMIT = 100
  FORGER_NAME = 'Forger'
  ARMORER_NAME = 'Armorer'
  ENCHANTER_NAME = 'Enchanter'
  ELM_DUMMIES = []
  STA_DUMMIES = []
  ST_ANIMATIONS = []
  ST_ICONS = []
  ST_SOUNDS = []
  ARMOR = 'Armor'
  EVASION = 'Evasion'
  RIGHT_WINS = false
  #                                ('NAME', VOLUME, PITCH)
  CREATE_SOUND = RPG::AudioFile.new('056-Right02', 80, 100)
  BATTLER = '001-Fighter01'
  ATTR_LIMIT = 999
  MAX_HP = 9999
  MAX_SP = 9999
  MAX_ITEMS = 99
  RECURSIVE_USE = false
  REQUIRE_MATERIAL = true
  
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  
  def self.cs_materials(object)
    return 0 if object == nil
    return object.quality if RECURSIVE_USE && object.id >= 1000
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Material Database
# 
#   Use following template to give your materials a quality factor.
# 
#     when ID then QUALITY_FACTOR
# 
#   ID             - ID of the weapon/armor/item in the database
#   QUALITY_FACTOR - a positive/negative integer
# 
#   Note that you need to create items, call them however you want and then you
#   can use those items in the Creation System. This factor is the minimum
#   required level that one needs to be able to use the material. Use a
#   negative factor to make the item have element/status/parameter affection.
#   If you are using the RECURSIVE_USE option, new items will have the double
#   of the absolute sum of the quality factors of all materials used to create
#   that item.
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    if object.is_a?(RPG::Weapon)
      factor = case object.id
    ### START Weapons
      when 1 then 1
      when 2 then 2
      when 3 then 4
      when 4 then 10
      when 5 then 2
      when 6 then 3
      when 7 then 5
      when 8 then 9
    ### END Weapons
      end
    elsif object.is_a?(RPG::Armor)
      factor = case object.id
    ### START Armors
      when 1 then 1
      when 2 then 3
      when 3 then 5
      when 4 then 7
      when 5 then 2
      when 6 then 4
      when 7 then 6
      when 8 then 8
      when 9 then 3
      when 10 then 4
      when 11 then 7
      when 12 then 12
      when 13 then -5
      when 14 then -5
      when 15 then -5
      when 16 then -5
    ### END Armors
      end
    elsif object.is_a?(RPG::Item)
      factor = case object.id
    ### START Items
      when 1 then 1
      when 4 then 1
      when 8 then 1
      when 9 then 1
      when 10 then 1
      when 11 then -1
      when 17 then -3
      when 18 then -3
      when 19 then -3
      when 20 then -3
      when 21 then -3
      when 22 then -3
      when 34 then 4
      when 35 then 4
      when 36 then 7
      when 37 then -10
      when 38 then -10
    ### END Items
      end
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Material Database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    end
    return 0 if factor == nil
    return LVL_MAX if factor > LVL_MAX
    return factor
  end
  
  if $DUMMY_ELEMENTS == nil
    $DUMMY_ELEMENTS = ELM_DUMMIES.clone
  else
    $DUMMY_ELEMENTS |= ELM_DUMMIES
  end
  if $DUMMY_STATES == nil
    $DUMMY_STATES = STA_DUMMIES.clone
  else
    $DUMMY_STATES |= STA_DUMMIES
  end
  ELM_DUMMIES = STA_DUMMIES = nil

  # ensures compatibility
  $creation_system = 1.5
  
end

#==============================================================================
# RPG::Class
#------------------------------------------------------------------------------
#  This class was modified to allow equipping of newly created weapons and
#  armors.
#==============================================================================

class RPG::Class
  
  def weapon_set
    return @weapon_set + (1000...$data_weapons.size).to_a
  end
  
  def armor_set
    return @armor_set + (1000...$data_armors.size).to_a
  end
  
end

#==============================================================================
# RPG::Weapon
#------------------------------------------------------------------------------
#  This class was enhanced with a variable to allow use of created items as
#  materials for new items.
#==============================================================================

class RPG::Weapon
  
  attr_accessor :quality
  
end

#==============================================================================
# RPG::Armor
#------------------------------------------------------------------------------
#  This class was enhanced with a variable to allow use of created items as
#  materials for new items.
#==============================================================================

class RPG::Armor
  
  attr_accessor :quality
  
end

#==============================================================================
# RPG::Item
#------------------------------------------------------------------------------
#  This class was enhanced with a variable to allow use of created items as
#  materials for new items.
#==============================================================================

class RPG::Item
  
  attr_accessor :quality
  
end

#==============================================================================
# Game_System
#------------------------------------------------------------------------------
#  This class was enhanced with instance variables to keep track of unlocked
#  animations, icons and sounds. Also now it stores the newly screated objects
#  and stores the requirements to recreate them.
#==============================================================================

class Game_System
 
  attr_accessor :weapons
  attr_accessor :armors
  attr_accessor :items
  attr_reader   :animations
  attr_reader   :icon_filenames
  attr_reader   :sound_filenames
  attr_reader   :require
  
  alias init_creation_later initialize
  def initialize
    init_creation_later
    @weapons, @armors, @items, @require = [], [], [], {}
    @animations = BlizzCFG::ST_ANIMATIONS
    @icon_filenames = BlizzCFG::ST_ICONS
    @sound_filenames = BlizzCFG::ST_SOUNDS
  end
 
  def requirements(object)
    number = case object
    when RPG::Weapon then $game_party.weapon_number(object.id)
    when RPG::Armor then $game_party.armor_number(object.id)
    when RPG::Item then $game_party.item_number(object.id)
    end
    return false if @require[object] == nil
    return false if number >= BlizzCFG::MAX_ITEMS
    @require[object].each {|i|
        number = case i[0]
        when 0 then $game_party.weapon_number(i[1])
        when 1 then $game_party.armor_number(i[1])
        when 2 then $game_party.item_number(i[1])
        end
        return false if number / i[2] < 1}
    return true
  end
  
  def unlock_animation(id)
    @animations.push(id) unless @animations.include?(id)
  end
  
  def unlock_icon(name)
    @icon_filenames.push(name) unless @icon_filenames.include?(name)
  end
  
  def unlock_sound(name)
    @sound_filenames.push(name) unless @sound_filenames.include?(name)
  end
  
end

#==============================================================================
# Game_Actor
#------------------------------------------------------------------------------
#  This class was enhanced with instance variables to keep track of creation
#  skill level and EXP.
#==============================================================================

class Game_Actor < Game_Battler
  
  attr_reader :lvlforger
  attr_reader :lvlarmorer
  attr_reader :lvlenchanter
  attr_reader :expforger
  attr_reader :exparmorer
  attr_reader :expenchanter
  
  alias init_creation_later initialize
  def initialize(id)
    init_creation_later(id)
    @lvlforger = @lvlarmorer = @lvlenchanter = 1
    @expforger = @exparmorer = @expenchanter = 0
  end
 
  alias equippable_creation_system_later? equippable?
  def equippable?(item)
    if (item.is_a?(RPG::Weapon) || item.is_a?(RPG::Armor)) && item.id >= 1000
      return true
    end
    return equippable_creation_system_later?(item)
  end
  
  def add_creation_exp(val, type)
    case type
    when 0
      return if @lvlforger >= BlizzCFG::LVL_MAX
      @expforger += val
      while @lvlforger < BlizzCFG::LVL_MAX &&
          @expforger >= BlizzCFG::EXP_FACTOR * @lvlforger*(@lvlforger+1)
        @lvlforger += 1
      end
    when 1
      return if @lvlarmorer >= BlizzCFG::LVL_MAX
      @exparmorer += val
      while @lvlarmorer < BlizzCFG::LVL_MAX &&
          @exparmorer >= BlizzCFG::EXP_FACTOR * @lvlarmorer*(@lvlarmorer+1)
        @lvlarmorer += 1
      end
    when 2
      return if @lvlenchanter >= BlizzCFG::LVL_MAX
      @expenchanter += val
      while @lvlenchanter < BlizzCFG::LVL_MAX &&
          @expenchanter >= BlizzCFG::EXP_FACTOR * @lvlenchanter*(@lvlenchanter+1)
        @lvlenchanter += 1
      end
    end
  end
  
  def set_creation_lvl(val, type)
    case type
    when 0
      @lvlforger = [val, BlizzCFG::LVL_MAX].min
      @expforger = BlizzCFG::EXP_FACTOR * @lvlforger*(@lvlforger-1)
    when 1
      @lvlarmorer = [val, BlizzCFG::LVL_MAX].min
      @exparmorer = BlizzCFG::EXP_FACTOR * @lvlarmorer*(@lvlarmorer-1)
    when 2
      @lvlenchanter = [val, BlizzCFG::LVL_MAX].min
      @expenchanter = BlizzCFG::EXP_FACTOR * @lvlenchanter*(@lvlenchanter-1)
    end
  end
  
end

#==============================================================================
# Sprite_Animation
#------------------------------------------------------------------------------
#  This class is a special sprite that allows animation to be played upon and
#  is being displayed in the center of the screen.
#==============================================================================

class Sprite_Animation < RPG::Sprite
  
  attr_writer :animation_id
  
  def initialize
    super
    self.bitmap = RPG::Cache.battler(BlizzCFG::BATTLER, 0)
    self.x, self.y = 320 - self.bitmap.width/2, 240 - self.bitmap.height/2
    self.z, @animation_id = 200, 0
  end
  
  def update
    super
    if @animation_id != 0
      animation($data_animations[@animation_id], true)
      @animation_id = 0
    end
  end
  
end

#==============================================================================
# Array_Wrapper
#------------------------------------------------------------------------------
#  This class wraps an array and accesses the created objects transparently.
#  It is the class that serves for heavy compatibility increase so other
#  scripts can treat the $data_weapons, $data_armors and $data_items arrays as
#  if this script was not being used at all.
#==============================================================================

class Array_Wrapper
  
  def initialize(original_ary)
    @data = original_ary
  end
  
  def [](id)
    return ((0..999).include?(id) ? @data[id] : self.xdata(id-1000))
  end
  
  def xdata(id = nil)
    case @data[1]
    when RPG::Weapon
      (id == nil ? $game_system.weapons : $game_system.weapons[id])
    when RPG::Armor
      (id == nil ? $game_system.armors : $game_system.armors[id])
    when RPG::Item
      (id == nil ? $game_system.items : $game_system.items[id])
    end
  end
  
  def size
    case @data[1]
    when RPG::Weapon then return 1000 + $game_system.weapons.size
    when RPG::Armor then return 1000 + $game_system.armors.size
    when RPG::Item then return 1000 + $game_system.items.size
    end
  end
  
  def self
    case @data[1]
    when RPG::Weapon then return @data + $game_system.weapons
    when RPG::Armor then return @data + $game_system.armors
    when RPG::Item then return @data + $game_system.items
    end
  end
  
  def push(new)
    case @data[1]
    when RPG::Weapon then $game_system.weapons.push(new)
    when RPG::Armor then $game_system.armors.push(new)
    when RPG::Item then $game_system.items.push(new)
    end
  end
  
  def real_size
    return @data.size
  end
  
end

#==============================================================================
# Scene_Title
#------------------------------------------------------------------------------
#  This class was modified to replace the normal objects array with the
#  special Array_Wrapper class.
#==============================================================================

class Scene_Title
  
  alias main_creation_system_later main
  def main
    main_creation_system_later
    if $scene != nil
      $data_weapons = Array_Wrapper.new($data_weapons)
      $data_armors = Array_Wrapper.new($data_armors)
      $data_items = Array_Wrapper.new($data_items)
    end
  end
  
end

#==============================================================================
# Window_Base
#------------------------------------------------------------------------------
#  This class was enhanced with supporting methods for drawing.
#==============================================================================

class Window_Base
  
  def draw_actor_clvl(actor, x, y, type)
    case type
    when 0 then name, lvl = BlizzCFG::FORGER_NAME, @actor.lvlforger
    when 1 then name, lvl = BlizzCFG::ARMORER_NAME, @actor.lvlarmorer
    when 2 then name, lvl = BlizzCFG::ENCHANTER_NAME, @actor.lvlenchanter
    end
    self.contents.font.color = system_color
    self.contents.draw_text(x, y, 120, 32, name)
    self.contents.font.color = normal_color
    self.contents.draw_text(x, y, 156, 32, lvl.to_s, 2)
  end
  
  def draw_actor_cexp(actor, x, y, type)
    case type
    when 0 then lvl, exp = @actor.lvlforger, @actor.expforger
    when 1 then lvl, exp = @actor.lvlarmorer, @actor.exparmorer
    when 2 then lvl, exp = @actor.lvlenchanter, @actor.expenchanter
    end
    if lvl >= BlizzCFG::LVL_MAX
      exp = '-------'
    else
      exp = BlizzCFG::EXP_FACTOR * lvl*(lvl+1) - exp
    end
    self.contents.font.color = system_color
    self.contents.draw_text(x, y, 80, 32, 'next')
    self.contents.font.color = normal_color
    self.contents.draw_text(x, y, 156, 32, exp.to_s, 2)
  end
  
end

#==============================================================================
# Window_Dialog
#------------------------------------------------------------------------------
#  This class is a window with a dialog.
#==============================================================================

class Window_Dialog < Window_Command
  
  def initialize(width, commands = ['Yes', 'No'], caption = 'Are you sure?')
    commands.push('')
    @caption = caption
    @cursor_width = 32
    super(width, commands)
    if $fontface != nil
      self.contents.font.name = $fontface
      self.contents.font.size = $fontsize
    elsif $defaultfonttype != nil
      self.contents.font.name = $defaultfonttype
      self.contents.font.size = $defaultfontsize
    end
    self.x, self.y, self.z = 320 - self.width/2, 200 - self.height/2, 1500
    @item_max, self.opacity = commands.size-1, 192
    commands.each {|c|
        w = self.contents.text_size(c).width
        @cursor_width = w if @cursor_width < w}
    @cursor_width += 32
    refresh
    update_cursor_rect
  end
  
  def refresh
    super
    self.contents.font.color = system_color
    self.contents.draw_text(4, 0, self.width-40, 32, @caption, 1)
  end
  
  def draw_item(index, color)
    self.contents.font.color = color
    rect = Rect.new(4, 32 * (index+1), self.contents.width - 8, 32)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    self.contents.draw_text(rect, @commands[index], 1)
  end
  
  def update_cursor_rect
    if @index < 0 || @item_max == 0
      self.cursor_rect.empty
    else
      x = (self.contents.width - @cursor_width) / 2
      self.cursor_rect.set(x, (@index+1)*32, @cursor_width, 32)
    end
  end
  
end

#==============================================================================
# Window_CreationInput
#------------------------------------------------------------------------------
#  This class is a window with name or description input for created objects.
#==============================================================================

class Window_CreationInput < Window_NameInput
  
  CHARACTER_TABLE =
  [ 'A','B','C','D','E',
    'F','G','H','I','J',
    'K','L','M','N','O',
    'P','Q','R','S','T',
    'U','V','W','X','Y',
    'Z','.',',','_','?',
    '+','-','*','/','!',
    '1','2','3','4','5',
    '[',']','{','}',' ',
    'a','b','c','d','e',
    'f','g','h','i','j',
    'k','l','m','n','o',
    'p','q','r','s','t',
    'u','v','w','x','y',
    'z','"','\'','(',')',
    '#','$','%','&','@',
    '6','7','8','9','0',
    '^','=','~','<','>',]
  
  def character
    return CHARACTER_TABLE[@index]
  end
  
  def refresh
    self.contents.clear
    (0...90).each {|i|
        x = 140 + i / 5 / 9 * 180 + i % 5 * 32
        y = i / 5 % 9 * 32
        self.contents.draw_text(x, y, 32, 32, CHARACTER_TABLE[i], 1)}
    self.contents.draw_text(428, 9 * 32, 48, 32, 'OK', 1)
  end
  
end

#==============================================================================
# Window_CreationNumber
#------------------------------------------------------------------------------
#  This class serves as superclass for windows that ask for a number input.
#==============================================================================

class Window_CreationNumber < Window_Base
  
  attr_reader :number
  
  def initialize(min_val, max_val, current, text, offset)
    super(160, 240, 320, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    @text, @offset, @min_val, @max_val = text, offset, min_val, max_val
    @number, self.z, self.opacity = current, 1000, 192
    self.cursor_rect.set(230, 0, 32, 32)
    refresh
  end
  
  def refresh
    self.contents.clear
    self.contents.font.color = system_color
    self.contents.draw_text(@offset, 0, 212 - @offset, 32, @text)
    self.contents.draw_text(212, 0, 32, 32, '×')
    draw_number
  end
  
  def draw_number
    self.contents.font.color = normal_color
    self.contents.fill_rect(232, 0, 24, 32, Color.new(0, 0, 0, 0))
    self.contents.draw_text(232, 0, 24, 32, @number.to_s, 2)
  end
  
  def update
    super
    if self.active
      if Input.repeat?(Input::RIGHT)
        if @number < @max_val
          $game_system.se_play($data_system.cursor_se)
          @number += 1
          draw_number
        else
          $game_system.se_play($data_system.buzzer_se)
        end
      elsif Input.repeat?(Input::LEFT)
        if @number > @min_val
          $game_system.se_play($data_system.cursor_se)
          @number -= 1
          draw_number
        else
          $game_system.se_play($data_system.buzzer_se)
        end
      elsif Input.repeat?(Input::UP)
        if @number < @max_val
          $game_system.se_play($data_system.cursor_se)
          @number += 10
          @number = @max_val if @number > @max_val
          draw_number
        else
          $game_system.se_play($data_system.buzzer_se)
        end
      elsif Input.repeat?(Input::DOWN)
        if @number > @min_val
          $game_system.se_play($data_system.cursor_se)
          @number -= 10
          @number = @min_val if @number < @min_val
          draw_number
        else
          $game_system.se_play($data_system.buzzer_se)
        end
      end
    end
  end
  
end

#==============================================================================
# Window_CreationNumberPercent
#------------------------------------------------------------------------------
#  This class is a window that asks for the number of precents.
#==============================================================================

class Window_CreationNumberPercent < Window_CreationNumber
  
  attr_reader :old_value
  
  def initialize(min_val, max_val, current, text)
    @refresh_state, @old_value = 0, current
    super(min_val, max_val, current, text, 4)
  end
  
  def refresh
    super
    self.contents.font.color = system_color
    self.contents.draw_text(260, 0, 24, 32, '%')
  end
  
  def should_refresh_main?
    return false if @refresh_state != 2
    @refresh_state = 0
    return true
  end
  
  def update
    super
    if self.active && (Input.press?(Input::DOWN) || Input.press?(Input::LEFT) ||
        Input.press?(Input::RIGHT) || Input.press?(Input::UP))
      @refresh_state = 1
    elsif @refresh_state == 1
      @refresh_state = 2
    end
  end
  
end

#==============================================================================
# Window_CreationNumberItem
#------------------------------------------------------------------------------
#  This class is a window that asks for the number of objects.
#==============================================================================

class Window_CreationNumberItem < Window_CreationNumber
  
  def initialize(min_val, max_val, current, item)
    @icon = item.icon_name
    super(min_val, max_val, current, item.name, 32)
  end
  
  def refresh
    super
    self.contents.blt(4, 0, RPG::Cache.icon(@icon), Rect.new(0, 0, 24, 24))
  end
  
end

#==============================================================================
# Window_Icons
#------------------------------------------------------------------------------
#  This class is a window with a selection of icons.
#==============================================================================

class Window_Icons < Window_Selectable
  
  def initialize
    super(0, 64, 640, 416)
    @column_max, @item_max = 19, $game_system.icon_filenames.size + 1
    self.contents = Bitmap.new(width - 32, row_max * 32)
    self.height = [416, row_max * 32 + 32].min
    self.contents.fill_rect(2, 2, 28, 28, Color.new(255, 255, 255))
    self.contents.fill_rect(3, 3, 26, 26, Color.new(0, 0, 0, 0))
    (1...@item_max).each {|i|
        bitmap = RPG::Cache.icon($game_system.icon_filenames[i-1])
        self.contents.blt(4+i%19*32, 4+i/19*32, bitmap, Rect.new(0, 0, 24, 24))}
    self.z, self.index = 300, 0
  end
  
  def update_cursor_rect
    if @index < 0
      self.cursor_rect.empty
      return
    end
    row = @index / @column_max
    self.top_row = row if row < self.top_row
    self.top_row = row - (page_row_max - 1) if row > top_row + (page_row_max - 1)
    self.cursor_rect.set(@index%19*32+3, @index/19*32+3-self.oy, 26, 26)
  end
  
end


#==============================================================================
# Window_Animations
#------------------------------------------------------------------------------
#  This class is a window with a selection of animations.
#==============================================================================

class Window_Animations < Window_Command
  
  def initialize(left = true)
    commands = [left ? '[No User Animation]' : '[No Target Animation]']
    $game_system.animations.each {|id| commands.push($data_animations[id].name)}
    super(320, commands)
    self.x = 320 unless left
    self.active, self.index, self.y, self.z, self.height = left, 0, 64, 300, 416
  end
  
end

#==============================================================================
# Window_MaterialItem
#------------------------------------------------------------------------------
#  This class is a window that displays materials and serves as superclass for
#  other windows.
#==============================================================================

class Window_MaterialItem < Window_Selectable
  
  def initialize(x, help_window, h = 416, data = [])
    super(x, 64, 320, h)
    @data, @column_max, self.z, self.index, self.active = data, 1, 300, 0, false
    @help_window = help_window
    refresh
  end
  
  def item
    return @data[self.index]
  end
  
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    @item_max = @data.size
    if @item_max > 0
      self.contents = Bitmap.new(width - 32, row_max * 32)
      if $fontface != nil
        self.contents.font.name = $fontface
        self.contents.font.size = $fontsize
      elsif $defaultfonttype != nil
        self.contents.font.name = $defaultfonttype
        self.contents.font.size = $defaultfontsize
      end
      (0...@item_max).each {|i| draw_item(i)}
    end
  end
  
  def draw_item(i, w = 252)
    self.contents.fill_rect(0, i*32, 288, 32, Color.new(0, 0, 0, 0))
    if @data[i].is_a?(String)
      self.contents.draw_text(4, i*32, w, 32, @data[i])
    else
      bitmap = RPG::Cache.icon(@data[i].icon_name)
      opacity = (self.contents.font.color == normal_color ? 255 : 128)
      self.contents.blt(4, 4+i*32, bitmap, Rect.new(0, 0, 24, 24), opacity)
      self.contents.draw_text(32, i*32, w, 32, @data[i].name)
    end
  end
  
  def update
    self.active ? super : self.cursor_rect.empty
  end
  
  def update_help
    @help_window.set_text(self.item == nil ? "" : self.item.description)
  end
  
end

#==============================================================================
# Window_CreationMaterial
#------------------------------------------------------------------------------
#  This class is a window that displays available materials.
#==============================================================================

class Window_CreationMaterial < Window_MaterialItem
  
  attr_reader :data
  
  def initialize(help_window, level)
    @level = level
    super((BlizzCFG::RIGHT_WINS ? 0 : 320), help_window)
  end
  
  def refresh
    (1...$data_weapons.size).each {|i|
        quality = BlizzCFG.cs_materials($data_weapons[i])
        if quality != 0 && @level >= quality.abs &&
            $game_party.weapon_number(i) > 0
          @data.push($data_weapons[i])
        end}
    (1...$data_armors.size).each {|i|
        quality = BlizzCFG.cs_materials($data_armors[i])
        if quality != 0 && @level >= quality.abs &&
            $game_party.armor_number(i) > 0
          @data.push($data_armors[i])
        end}
    (1...$data_items.size).each {|i|
        quality = BlizzCFG.cs_materials($data_items[i])
        if quality != 0 && @level >= quality.abs &&
            $game_party.item_number(i) > 0
          @data.push($data_items[i])
        end}
    super
  end
  
  def draw(item, minus = 0)
    draw_item(@data.index(item), minus)
  end
  
  def draw_item(i, minus = 0)
    super(i, 212)
    number = case @data[i]
    when RPG::Weapon then $game_party.weapon_number(@data[i].id) - minus
    when RPG::Armor then $game_party.armor_number(@data[i].id) - minus
    when RPG::Item then $game_party.item_number(@data[i].id) - minus
    end
    self.contents.draw_text(244, i*32, 16, 32, ':', 1)
    self.contents.draw_text(260, i*32, 24, 32, number.to_s, 2)
  end
  
end

#==============================================================================
# Window_CreationAdded
#------------------------------------------------------------------------------
#  This class is a window that displays selected materials.
#==============================================================================

class Window_CreationAdded < Window_MaterialItem
  
  def initialize(data, quant)
    @quant = quant
    super((BlizzCFG::RIGHT_WINS ? 320 : 0), nil, 416, data)
  end
  
  def add_material(item)
    return false if @data.include?(item)
    string = @data.shift
    @data.push(item)
    @data.sort! {|a, b| a.id - b.id}
    @data.unshift(string)
    refresh
    return true
  end
  
  def remove_material(item)
    return false unless @data.include?(item)
    @data.delete(item)
    refresh
    return true
  end
  
  def set_material(item, number)
    if number == 0
      if @data.include?(item)
        index = @data.index(item)
        @data.delete_at(index)
        @quant.delete_at(index)
        refresh
      end
    elsif @data.include?(item)
      index = @data.index(item)
      if @quant[index] != number
        @quant[index] = number
        refresh
      end
    else
      i = 1
      while i <= @data.size
        if @data[i] == nil ||@data[i].id > item.id
          @data.insert(i, item)
          @quant.insert(i, number)
          break
        end
        i += 1
      end
      refresh
    end
  end
  
  def draw_item(i)
    super(i, 212)
    if @quant[i] > 0
      self.contents.draw_text(244, i*32, 16, 32, ':', 1)
      self.contents.draw_text(260, i*32, 24, 32, @quant[i].to_s, 2)
    end
  end
  
  def materials
    return @data[1, @data.size-1]
  end
  
  def quantities
    return @quant[1, @quant.size-1]
  end
  
end

#==============================================================================
# Window_CreationItem
#------------------------------------------------------------------------------
#  This class is a window that displays already created objects for selection
#  to recreate one.
#==============================================================================

class Window_CreationItem < Window_Item
  
  attr_reader :item_max
  
  def initialize(type)
    @type = type
    super()
  end
  
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    @data = []
    case @type
    when 0 then (1000...$data_weapons.size).each {|i| @data.push($data_weapons[i])}
    when 1 then (1000...$data_armors.size).each {|i| @data.push($data_armors[i])}
    when 2 then (1000...$data_items.size).each {|i| @data.push($data_items[i])}
    end
    @data.push('[Cancel]')
    @item_max = @data.size
    self.contents = Bitmap.new(width - 32, row_max * 32)
    if $fontface != nil
      self.contents.font.name = $fontface
      self.contents.font.size = $fontsize
    elsif $defaultfonttype != nil
      self.contents.font.name = $defaultfonttype
      self.contents.font.size = $defaultfontsize
    end
    (0...@item_max).each {|i| draw_item(i)}
  end
  
  def draw_item(i)
    x = 4 + i % 2 * 320
    y = i / 2 * 32
    if @data[i].is_a?(String)
      self.contents.font.color = normal_color
      self.contents.draw_text(x, y, 212, 32, '[Cancel]')
    else
      number = case @data[i]
      when RPG::Weapon then $game_party.weapon_number(@data[i].id)
      when RPG::Armor then $game_party.armor_number(@data[i].id)
      when RPG::Item then $game_party.item_number(@data[i].id)
      end
      if $game_system.requirements(@data[i])
        self.contents.font.color = normal_color
      else
        self.contents.font.color = disabled_color
      end
      rect = Rect.new(x, y, self.width / @column_max - 32, 32)
      self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
      bitmap = RPG::Cache.icon(@data[i].icon_name)
      opacity = self.contents.font.color == normal_color ? 255 : 128
      self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
      self.contents.draw_text(x + 28, y, 212, 32, @data[i].name, 0)
      self.contents.draw_text(x + 240, y, 16, 32, ":", 1)
      self.contents.draw_text(x + 256, y, 24, 32, number.to_s, 2)
    end
  end
  
  def update_help
    @data[index].is_a?(String) ? @help_window.set_text('') : super
  end
  
end

#==============================================================================
# Window_Sounds
#------------------------------------------------------------------------------
#  This window displays available menu sound effects.
#==============================================================================

class Window_Sounds < Window_Item
  
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    @data = (['[None]'] + $game_system.sound_filenames)
    @item_max = @data.size
    if @item_max > 0
      self.contents = Bitmap.new(width - 32, row_max * 32)
      (0...@item_max).each {|i| draw_item(i)}
    end
  end
  
  def set_index(name)
    i = @data.index(name)
    self.index = (i == nil ? 0 : i)
  end
  
  def draw_item(i)
    item = @data[i]
    x = 4 + i % 2 * (288 + 32)
    y = i / 2 * 32
    rect = Rect.new(x, y, self.width / @column_max - 32, 32)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    self.contents.draw_text(x + 28, y, 212, 32, @data[i], 0)
  end
  
  def update_help
    @help_window.set_text('')
  end
  
end

#==============================================================================
# Window_CreationStatus
#------------------------------------------------------------------------------
#  This class is a window with vital information about the creating actor.
#==============================================================================

class Window_CreationStatus < Window_Base
  
  attr_accessor :actor
  
  def initialize(actor, type)
    super((BlizzCFG::RIGHT_WINS ? 448 : 0), 0, 192, 160)
    self.contents = Bitmap.new(width - 32, height - 32)
    if $fontface != nil
      self.contents.font.name = $fontface
      self.contents.font.size = $fontsize
    elsif $defaultfonttype != nil
      self.contents.font.name = $defaultfonttype
      self.contents.font.size = $defaultfontsize
    end
    @actor, @type = actor, type
    refresh
  end
  
  def refresh
    self.contents.clear
    draw_actor_name(@actor, 4, 0)
    draw_actor_level(@actor, 4, 32)
    draw_actor_clvl(@actor, 4, 64, @type)
    draw_actor_cexp(@actor, 4, 96, @type)
  end
  
end

#==============================================================================
# Window_CreationMain
#------------------------------------------------------------------------------
#  This class is a window that completely displays the information about the
#  object. It displays any of the three types. It also handles recreation and
#  final object creation confirmation.
#==============================================================================

class Window_CreationMain < Window_Base
  
  attr_accessor :input
  attr_reader   :object
  attr_reader   :attributes
  attr_reader   :origin
  attr_reader   :scopes
  attr_reader   :scope_text
  attr_reader   :auto_states
  
  def initialize(type, actor, object)
    @attributes = [0, 0, 0, 0, 0, 0]
    @scopes = [0]
    super((BlizzCFG::RIGHT_WINS ? 0 : 192), 0, 448, 480)
    self.contents = Bitmap.new(width - 32, height - 32)
    if $fontface != nil
      self.contents.font.name = $fontface
      self.contents.font.size = $fontsize
    elsif $defaultfonttype != nil
      self.contents.font.name = $defaultfonttype
      self.contents.font.size = $defaultfontsize
    end
    @type, @object, @actor, @mode = type, object, actor, 0
    @auto_states, @materials, @quantities = [0], [], []
    @scope_text = ['None', '1 Enemy', 'All Enemies', '1 Ally', 'All Allies',
        '1 "dead" Ally', 'All "dead" Allies', 'User']
    refresh
  end
  
  def mode=(newmode)
    @mode = newmode
    @input = (newmode == 2 ? @object.description : @object.name)
    refresh
  end
  
  def apply(yes = true)
    if yes
      case @mode
      when 1 then @object.name = @input
      when 2 then @object.description = @input
      end
    end
    @mode = 0
    refresh
  end
  
  def set_stats
    @object.recover_hp = @stats[0] * 100 / @object.hit
    @object.recover_hp_rate = @stats[1] * 100 / @object.hit
    @object.recover_sp = @stats[2] * 100 / @object.hit
    @object.recover_sp_rate = @stats[3] * 100 / @object.hit
    @object.parameter_points = @stats[4] * 100 / @object.hit
    apply_attribute_limits
  end
  
  def setup_materials
    reset_object
    @scopes = [0]
    level = case @type
    when 0 then @actor.lvlforger
    when 1 then @actor.lvlarmorer
    when 2 then @actor.lvlenchanter
    end
    @auto_states, @attributes = [0], [0, 0, 0, 0, 0, 0]
    @materials.each_index {|i|
        item, quantity = @materials[i], @quantities[i]
        @object.price += item.price * quantity
        set_object_parameters(item, quantity, level)
        if BlizzCFG.cs_materials(item) < 0
          set_object_elements_states(item, level)
        end
        if @type == 2 && item.is_a?(RPG::Item)
          set_object_recover_parameters(item, quantity)
        end}
    if level >= BlizzCFG::ATTR_LVL_LIMIT && @type == 2
      @attributes.each_index {|i|
          if @attributes[i] > 0 && @object.parameter_points < @attributes[i]
            @object.parameter_type = i + 1
            @object.parameter_points = @attributes[i]
          end}
      @attributes.each_index {|i| @attributes[i] *= 100 / @object.hit}
    end
    @object.price -= 1 if @object.price > 1
    @scopes = (@scopes | @scopes).sort
    @type == 1 ? set_armor_elements_states : fix_object_elements_states
    apply_attribute_limits
    set_item_extras if @type == 2
  end
  
  def set_object_parameters(item, quantity, level)
    case item
    when RPG::Weapon, RPG::Armor
      if @type == 2
        if level >= BlizzCFG::ATTR_LVL_LIMIT && BlizzCFG.cs_materials(item) < 0
          @attributes[2] += item.str_plus * quantity
          @attributes[3] += item.dex_plus * quantity
          @attributes[4] += item.agi_plus * quantity
          @attributes[5] += item.int_plus * quantity
        end
      else
        if @type == 0 && item.is_a?(RPG::Weapon)
          if level >= BlizzCFG::PWR_LVL_LIMIT
            @object.atk += item.atk * quantity
            @object.pdef += item.pdef * quantity
            @object.mdef += item.mdef * quantity
          else
            @object.atk = item.atk if @object.atk < item.atk 
            @object.pdef = item.pdef if @object.pdef < item.pdef
            @object.mdef = item.mdef if @object.mdef < item.mdef
          end
        elsif @type == 1 && item.is_a?(RPG::Armor)
          @object.eva += item.eva * quantity
          if level >= BlizzCFG::PWR_LVL_LIMIT
            @object.pdef += item.pdef * quantity
            @object.mdef += item.mdef * quantity
          else
            @object.pdef = item.pdef if @object.pdef < item.pdef
            @object.mdef = item.mdef if @object.mdef < item.mdef
          end
        elsif level >= BlizzCFG::PWR_LVL_LIMIT
          @object.pdef += item.pdef * quantity
          @object.mdef += item.mdef * quantity
        end
        if level >= BlizzCFG::ATTR_LVL_LIMIT && BlizzCFG.cs_materials(item) < 0
          @object.str_plus += item.str_plus * quantity
          @object.dex_plus += item.dex_plus * quantity
          @object.agi_plus += item.agi_plus * quantity
          @object.int_plus += item.int_plus * quantity
        end
      end
    when RPG::Item
      if @type == 2
        if level >= BlizzCFG::ATTR_LVL_LIMIT
          @object.pdef_f += item.pdef_f * quantity
          @object.mdef_f += item.mdef_f * quantity
          @object.pdef_f = 100 if @object.pdef_f > 100
          @object.mdef_f = 100 if @object.mdef_f > 100
          if BlizzCFG.cs_materials(item) < 0 && item.parameter_type > 0
            @attributes[item.parameter_type-1] +=
                item.parameter_points * quantity
          end
        end
      elsif level >= BlizzCFG::ATTR_LVL_LIMIT && BlizzCFG.cs_materials(item) < 0 
        case item.parameter_type
        when 3 then @object.str_plus += item.parameter_points * quantity
        when 4 then @object.dex_plus += item.parameter_points * quantity
        when 5 then @object.agi_plus += item.parameter_points * quantity
        when 6 then @object.int_plus += item.parameter_points * quantity
        end
      end
    end
  end
  
  def set_object_recover_parameters(item, quantity)
    @object.recover_hp += item.recover_hp * quantity
    @object.recover_hp_rate += item.recover_hp_rate * quantity
    @object.recover_sp += item.recover_sp * quantity
    @object.recover_sp_rate += item.recover_sp_rate * quantity
    @scopes.push(item.scope)
  end
  
  def set_object_elements_states(item, level)
    case item
    when RPG::Weapon, RPG::Item
      if @type == 1
        if level >= BlizzCFG::ELM_LVL_LIMIT
          @object.guard_element_set |= item.element_set
        end
        if level >= BlizzCFG::STA_LVL_LIMIT
          @auto_states |= item.plus_state_set
          @object.guard_state_set |= item.minus_state_set
        end
      else
        if level >= BlizzCFG::ELM_LVL_LIMIT
          @object.element_set |= item.element_set
        end
        if level >= BlizzCFG::STA_LVL_LIMIT
          @object.plus_state_set |= item.plus_state_set
          @object.minus_state_set |= item.minus_state_set
        end
      end
    when RPG::Armor
      if @type == 1
        if level >= BlizzCFG::ELM_LVL_LIMIT
          @object.guard_element_set |= item.guard_element_set
        end
        if level >= BlizzCFG::STA_LVL_LIMIT
          @auto_states |= [item.auto_state_id] unless item.auto_state_id == 0
          @object.guard_state_set |= item.guard_state_set
        end
      else
        if level >= BlizzCFG::ELM_LVL_LIMIT
          @object.element_set |= item.guard_element_set
        end
        if level >= BlizzCFG::STA_LVL_LIMIT
          unless item.auto_state_id == 0
            @object.plus_state_set |= [item.auto_state_id]
          end
          @object.minus_state_set |= item.guard_state_set
        end
      end
    end
  end
  
  def apply_attribute_limits
    case @type
    when 0, 1
      if @type == 0
        @object.atk = BlizzCFG::ATTR_LIMIT if @object.atk > BlizzCFG::ATTR_LIMIT
      elsif @type == 1
        @object.eva = 100 if @object.eva > 100
      end
      @object.pdef = BlizzCFG::ATTR_LIMIT if @object.pdef > BlizzCFG::ATTR_LIMIT
      @object.mdef = BlizzCFG::ATTR_LIMIT if @object.mdef > BlizzCFG::ATTR_LIMIT
      @object.str_plus = BlizzCFG::ATTR_LIMIT if @object.str_plus > BlizzCFG::ATTR_LIMIT
      @object.dex_plus = BlizzCFG::ATTR_LIMIT if @object.dex_plus > BlizzCFG::ATTR_LIMIT
      @object.agi_plus = BlizzCFG::ATTR_LIMIT if @object.agi_plus > BlizzCFG::ATTR_LIMIT
      @object.int_plus = BlizzCFG::ATTR_LIMIT if @object.int_plus > BlizzCFG::ATTR_LIMIT
    when 2
      if @object.recover_hp > BlizzCFG::MAX_HP
        @object.recover_hp = BlizzCFG::MAX_HP
      elsif @object.recover_hp < -BlizzCFG::MAX_HP
        @object.recover_hp = -BlizzCFG::MAX_HP
      end
      if @object.recover_hp_rate > 100
        @object.recover_hp_rate = 100
      elsif @object.recover_hp_rate < -100
        @object.recover_hp_rate = -100
      end
      if @object.recover_sp > BlizzCFG::MAX_SP
        @object.recover_sp = BlizzCFG::MAX_SP
      elsif @object.recover_sp < -BlizzCFG::MAX_SP
        @object.recover_sp = -BlizzCFG::MAX_SP
      end
      if @object.recover_sp_rate > 100
        @object.recover_sp_rate = 100
      elsif @object.recover_sp_rate < -100
        @object.recover_sp_rate = -100
      end
    end
  end
  
  def set_armor_elements_states
    @object.guard_element_set -= $DUMMY_ELEMENTS
    @object.guard_state_set -= $DUMMY_STATES
    @auto_states -= $DUMMY_STATES
    @object.guard_element_set.sort!
    @object.guard_state_set.sort!
    @auto_states.sort!
  end
  
  def fix_object_elements_states
    @object.element_set -= $DUMMY_ELEMENTS
    @object.minus_state_set -= @object.plus_state_set
    @object.plus_state_set -= $DUMMY_STATES
  end
  
  def set_item_extras
    @object.scope = 0 unless @scopes.include?(@object.scope)
    @stats = [@object.recover_hp, @object.recover_hp_rate, @object.recover_sp,
        @object.recover_sp_rate, @object.parameter_points]
    set_stats
  end
  
  def set_parameter(type, val)
    @object.parameter_type, @stats[4] = type, val
    set_stats
  end
  
  def reset_object
    case @object
    when RPG::Weapon
      @object.atk = @object.pdef = @object.mdef = @object.str_plus =
      @object.dex_plus = @object.agi_plus = @object.int_plus = 0
      @object.element_set, @object.plus_state_set,
      @object.minus_state_set = [], [], []
    when RPG::Armor
      @object.auto_state_id = @object.pdef = @object.mdef = @object.eva =
      @object.str_plus = @object.dex_plus = @object.agi_plus =
      @object.int_plus = 0
      @object.guard_element_set, @object.guard_state_set = [], []
    when RPG::Item
      @object.parameter_type = @object.parameter_points =
      @object.pdef_f = @object.mdef_f =
      @object.recover_hp = @object.recover_hp_rate =
      @object.recover_sp = @object.recover_sp_rate = 0
      @object.element_set, @object.plus_state_set,
      @object.minus_state_set = [], [], []
    end
    @object.price = 1
  end
  
  def refresh(materials = @materials, quantities = @quantities)
    if @materials != materials || @quantities != quantities
      @materials = materials.clone
      @quantities = quantities.clone
      setup_materials
    end
    self.contents.clear
    icon = RPG::Cache.icon(@object.icon_name)
    self.contents.blt(384, 4, icon, Rect.new(0, 0, 24, 24))
    self.contents.font.color = system_color
    self.contents.draw_text(4, 0, 80, 32, 'Type:')
    self.contents.draw_text(4, 28, 80, 32, 'Name:')
    self.contents.draw_text(296, 0, 80, 32, 'Icon:', 2)
    self.contents.draw_text(4, 56, 440, 32, 'Description:')
    self.contents.font.color = normal_color
    refresh_mode
    case @type
    when 0 then refresh_weapon
    when 1 then refresh_armor
    when 2 then refresh_item
    end
  end
  
  def refresh_mode
    self.contents.fill_rect(84, 28, 328, 32, Color.new(0, 0, 0, 0))
    self.contents.fill_rect(4, 84, 408, 32, Color.new(0, 0, 0, 0))
    case @mode
    when 0
      self.contents.draw_text(84, 28, 328, 32, @object.name)
      self.contents.draw_text(4, 84, 408, 32, @object.description)
    when 1
      self.contents.draw_text(84, 28, 328, 32, @input)
      self.contents.draw_text(4, 84, 408, 32, @object.description)
    when 2
      self.contents.draw_text(84, 28, 328, 32, @object.name)
      self.contents.draw_text(4, 84, 408, 32, @input)
    end
  end
  
  def refresh_weapon
    self.contents.font.color = system_color
    self.contents.draw_text(4, 112, 80, 32, 'Price:')
    self.contents.draw_text(4, 140, 144, 32, $data_system.words.atk)
    self.contents.draw_text(4, 168, 144, 32, $data_system.words.pdef)
    self.contents.draw_text(4, 196, 144, 32, $data_system.words.mdef)
    self.contents.draw_text(216, 112, 144, 32, $data_system.words.str)
    self.contents.draw_text(216, 140, 144, 32, $data_system.words.dex)
    self.contents.draw_text(216, 168, 144, 32, $data_system.words.agi)
    self.contents.draw_text(216, 196, 144, 32, $data_system.words.int)
    self.contents.draw_text(4, 224, 440, 32, 'Elemental Attack:')
    self.contents.draw_text(4, 280, 440, 32, 'Adding Status Attack:')
    self.contents.draw_text(4, 336, 440, 32, 'Nullifying Status Attack:')
    self.contents.font.color = normal_color
    self.contents.draw_text(84, 0, 440, 32, $data_system.words.weapon)
    self.contents.draw_text(100, 112, 100, 32, @object.price.to_s, 2)
    self.contents.draw_text(152, 140, 48, 32, @object.atk.to_s, 2)
    self.contents.draw_text(152, 168, 48, 32, @object.pdef.to_s, 2)
    self.contents.draw_text(152, 196, 48, 32, @object.mdef.to_s, 2)
    self.contents.draw_text(364, 112, 48, 32, @object.str_plus.to_s, 2)
    self.contents.draw_text(364, 140, 48, 32, @object.dex_plus.to_s, 2)
    self.contents.draw_text(364, 168, 48, 32, @object.agi_plus.to_s, 2)
    self.contents.draw_text(364, 196, 48, 32, @object.int_plus.to_s, 2)
    if @object.element_set.size > 0
      text = $data_system.elements[@object.element_set[0]]
      (1...@object.element_set.size).each {|i|
          unless $DUMMY_ELEMENTS.include?(@object.element_set[i])
            text += ", #{$data_system.elements[@object.element_set[i]]}"
          end}
      self.contents.draw_text(4, 252, 440, 32, text)
    else
      self.contents.draw_text(4, 252, 440, 32, 'None')
    end
    if @object.plus_state_set.size > 0
      text = $data_states[@object.plus_state_set[0]].name
      (1...@object.plus_state_set.size).each {|i|
          unless $DUMMY_STATES.include?(@object.plus_state_set[i])
            text += ", #{$data_states[@object.plus_state_set[i]].name}"
          end}
      self.contents.draw_text(4, 308, 440, 32, text)
    else
      self.contents.draw_text(4, 308, 440, 32, 'None')
    end
    if @object.minus_state_set.size > 0
      text = $data_states[@object.minus_state_set[0]].name
      (1...@object.minus_state_set.size).each {|i|
          unless $DUMMY_STATES.include?(@object.minus_state_set[i])
            text += ", #{$data_states[@object.minus_state_set[i]].name}"
          end}
      self.contents.draw_text(4, 364, 440, 32, text)
    else
      self.contents.draw_text(4, 364, 440, 32, 'None')
    end
  end
  
  def refresh_armor
    self.contents.font.color = system_color
    self.contents.draw_text(4, 112, 80, 32, 'Price:')
    self.contents.draw_text(4, 140, 144, 32, $data_system.words.pdef)
    self.contents.draw_text(4, 168, 144, 32, $data_system.words.mdef)
    self.contents.draw_text(4, 196, 144, 32, BlizzCFG::EVASION)
    self.contents.draw_text(216, 112, 144, 32, $data_system.words.str)
    self.contents.draw_text(216, 140, 144, 32, $data_system.words.dex)
    self.contents.draw_text(216, 168, 144, 32, $data_system.words.agi)
    self.contents.draw_text(216, 196, 144, 32, $data_system.words.int)
    self.contents.draw_text(4, 224, 132, 32, 'Auto-Status:')
    self.contents.draw_text(4, 252, 440, 32, 'Elemental protection:')
    self.contents.draw_text(4, 308, 440, 32, 'Status protection:')
    kind = case @object.kind
    when 0 then $data_system.words.armor1
    when 1 then $data_system.words.armor2
    when 2 then $data_system.words.armor3
    when 3 then $data_system.words.armor4
    end
    self.contents.font.color = normal_color
    self.contents.draw_text(84, 0, 360, 32, "#{BlizzCFG::ARMOR} (#{kind})")
    self.contents.draw_text(100, 112, 100, 32, @object.price.to_s, 2)
    self.contents.draw_text(152, 140, 48, 32, @object.pdef.to_s, 2)
    self.contents.draw_text(152, 168, 48, 32, @object.mdef.to_s, 2)
    self.contents.draw_text(152, 196, 48, 32, @object.eva.to_s, 2)
    self.contents.draw_text(364, 112, 48, 32, @object.str_plus.to_s, 2)
    self.contents.draw_text(364, 140, 48, 32, @object.dex_plus.to_s, 2)
    self.contents.draw_text(364, 168, 48, 32, @object.agi_plus.to_s, 2)
    self.contents.draw_text(364, 196, 48, 32, @object.int_plus.to_s, 2)
    if @object.auto_state_id > 0
      text = $data_states[@object.auto_state_id].name
      self.contents.draw_text(140, 224, 120, 32, text)
    else
      self.contents.draw_text(140, 224, 120, 32, 'None')
    end
    if @object.guard_element_set.size > 0
      text = $data_system.elements[@object.guard_element_set[0]]
      (1...@object.guard_element_set.size).each {|i|
          unless $DUMMY_ELEMENTS.include?(@object.guard_element_set[i])
            text += ", #{$data_system.elements[@object.guard_element_set[i]]}"
          end}
      self.contents.draw_text(4, 280, 440, 32, text)
    else
      self.contents.draw_text(4, 280, 440, 32, 'None')
    end
    if @object.guard_state_set.size > 0
      text = $data_states[@object.guard_state_set[0]].name
      (1...@object.guard_state_set.size).each {|i|
          unless $DUMMY_STATES.include?(@object.guard_state_set[i])
            text += ", #{$data_states[@object.guard_state_set[i]].name}"
          end}
      self.contents.draw_text(4, 336, 440, 32, text)
    else
      self.contents.draw_text(4, 336, 440, 32, 'None')
    end
  end
  
  def refresh_item
    self.contents.font.color = system_color
    self.contents.draw_text(4, 112, 80, 32, 'Price:')
    self.contents.draw_text(176, 112, 80, 32, 'Scope:')
    if @object.parameter_type > 0 && @object.parameter_points > 0
      parameter = case @object.parameter_type
      when 1 then "Max #{$data_system.words.hp}"
      when 2 then "Max #{$data_system.words.sp}"
      when 3 then $data_system.words.str
      when 4 then $data_system.words.dex
      when 5 then $data_system.words.agi
      when 6 then $data_system.words.int
      end
      self.contents.draw_text(4, 140, 108, 32, "#{parameter}:")
    else
      self.contents.draw_text(4, 140, 152, 32, 'No parameter')
    end
    self.contents.draw_text(176, 140, 80, 32, 'Sound:')
    self.contents.draw_text(4, 168, 144, 32, "Change #{$data_system.words.hp}:")
    self.contents.draw_text(4, 196, 144, 32, "Change #{$data_system.words.sp}:")
    self.contents.draw_text(216, 168, 144, 32, "Change #{$data_system.words.hp}:")
    self.contents.draw_text(216, 196, 144, 32, "Change #{$data_system.words.sp}:")
    self.contents.draw_text(4, 224, 144, 32, "#{$data_system.words.pdef} affection:")
    self.contents.draw_text(4, 252, 144, 32, "#{$data_system.words.mdef} affection:")
    self.contents.draw_text(216, 224, 144, 32, 'Hit rate:')
    self.contents.draw_text(216, 252, 144, 32, 'Variance:')
    self.contents.draw_text(4, 280, 440, 32, 'Elemental attributes:')
    self.contents.draw_text(4, 336, 440, 32, 'Adding Status:')
    self.contents.draw_text(4, 392, 440, 32, 'Nullifying Status:')
    self.contents.font.color = normal_color
    self.contents.draw_text(84, 0, 440, 32, $data_system.words.item)
    self.contents.draw_text(60, 112, 100, 32, @object.price.to_s, 2)
    scope = @scope_text[@object.scope]
    self.contents.draw_text(264, 112, 200, 32, scope)
    if @object.parameter_type > 0 && @object.parameter_points > 0
      self.contents.draw_text(112, 140, 48, 32, sprintf('%+d', @object.parameter_points), 2)
    end
    if @object.menu_se.name != ''
      self.contents.draw_text(264, 140, 200, 32, @object.menu_se.name)
    else
      self.contents.draw_text(264, 140, 200, 32, 'None')
    end
    self.contents.draw_text(136, 168, 64, 32, sprintf('%+d', @object.recover_hp), 2)
    self.contents.draw_text(136, 196, 64, 32, sprintf('%+d', @object.recover_sp), 2)
    self.contents.draw_text(348, 168, 64, 32, sprintf('%+d%', @object.recover_hp_rate), 2)
    self.contents.draw_text(348, 196, 64, 32, sprintf('%+d%', @object.recover_sp_rate), 2)
    self.contents.draw_text(136, 224, 64, 32, "#{@object.pdef_f}%", 2)
    self.contents.draw_text(136, 252, 64, 32, "#{@object.mdef_f}%", 2)
    self.contents.draw_text(348, 224, 64, 32, "#{@object.hit}%", 2)
    self.contents.draw_text(348, 252, 64, 32, "#{@object.variance}%", 2)
    if @object.element_set.size > 0
      text = $data_system.elements[@object.element_set[0]]
      (1...@object.element_set.size).each {|i|
          unless $DUMMY_ELEMENTS.include?(@object.element_set[i])
            text += ", #{$data_system.elements[@object.element_set[i]]}"
          end}
      self.contents.draw_text(4, 308, 440, 32, text)
    else
      self.contents.draw_text(4, 308, 440, 32, 'None')
    end
    if @object.plus_state_set.size > 0
      text = $data_states[@object.plus_state_set[0]].name
      (1...@object.plus_state_set.size).each {|i|
          unless $DUMMY_STATES.include?(@object.plus_state_set[i])
            text += ", #{$data_states[@object.plus_state_set[i]].name}"
          end}
      self.contents.draw_text(4, 364, 440, 32, text)
    else
      self.contents.draw_text(4, 364, 440, 32, 'None')
    end
    if @object.minus_state_set.size > 0
      text = $data_states[@object.minus_state_set[0]].name
      (1...@object.minus_state_set.size).each {|i|
          unless $DUMMY_STATES.include?(@object.minus_state_set[i])
            text += ", #{$data_states[@object.minus_state_set[i]].name}"
          end}
      self.contents.draw_text(4, 420, 440, 32, text)
    else
      self.contents.draw_text(4, 420, 440, 32, 'None')
    end
  end
  
  def setup(object, materials, quantities)
    @object = object
    refresh(materials, quantities)
  end
  
  def create(number = 1)
    new_object = false
    case @type
    when 0
      if $data_weapons.size == @object.id
        $data_weapons.push(@object)
        new_object = true
      end
      $game_party.gain_weapon(@object.id, number)
    when 1
      if $data_armors.size == @object.id
        $data_armors.push(@object)
        new_object = true
      end
      $game_party.gain_armor(@object.id, number)
    when 2
      if $data_items.size == @object.id
        $data_items.push(@object)
        new_object = true
      end
      $game_party.gain_item(@object.id, number)
    end
    quality, attr = 0,  false
    @materials.each_index {|i|
        result = BlizzCFG.cs_materials(@materials[i])
        quality += result.abs * @quantities[i]
        attr = true if result < 0}
    quality = 1 if quality == 0
    if new_object
      @object.quality = (attr ? -quality * 2 : quality * 2)
      if @object.quality.abs > BlizzCFG::LVL_MAX
        @object.quality = (attr ? BlizzCFG::LVL_MAX : -BlizzCFG::LVL_MAX)
      end
      objects = []
      @materials.each_index {|i|
          type = case @materials[i]
          when RPG::Weapon then 0
          when RPG::Armor then 1
          when RPG::Item then 2
          end
          objects.push([type, @materials[i].id, @quantities[i]])}
      $game_system.require[@object] = objects
    end
    @materials.each_index {|i|
        id, quantity = @materials[i].id, number * @quantities[i]
        case @materials[i]
        when RPG::Weapon then $game_party.lose_weapon(id, quantity)
        when RPG::Armor then $game_party.lose_armor(id, quantity)
        when RPG::Item then $game_party.lose_item(id, quantity)
        end}
    @actor.add_creation_exp(quality * number, @type)
    return quality * number
  end
  
end

#==============================================================================
# Scene_Creation
#------------------------------------------------------------------------------
#  This class handles all interactions of the player during the creation
#  process.
#==============================================================================

class Scene_Creation
  
  def initialize(type, index = 0)
    @actor, @type, @last_scene = $game_party.actors[index], type, $scene.class
  end
  
  def main
    if [0, 1, 2].include?(@type)
      @main_window = case @type
      when 0 then Window_CreationMain.new(@type, @actor, make_raw_weapon)
      when 1 then Window_CreationMain.new(@type, @actor, make_raw_armor)
      when 2 then Window_CreationMain.new(@type, @actor, make_raw_item)
      end
      @status_window = Window_CreationStatus.new(@actor, @type)
      commands = ['Create', 'Manage materials', 'Edit name', 'Edit description',
                  'Edit icon', 'Edit animation', 'Edit extras', 'Recreate',
                  'Exit']
      if @type == 1
        commands[5] = 'Edit type'
        commands[6] = 'Auto-Status'
      end
      @menu_window = Window_Command.new(192, commands)
      @menu_window.disable_item(0) if BlizzCFG::REQUIRE_MATERIAL 
      @disabled = []
      case @type
      when 0
        size = $game_system.weapons.size
        @disabled.push(6)
      when 1 then size = $game_system.armors.size
      when 2 then size = $game_system.items.size
      end
      @disabled.push(7) if size == 0
      @disabled.each {|i| @menu_window.disable_item(i)}
      @menu_window.x = 448 if BlizzCFG::RIGHT_WINS
      @menu_window.y = 160
      @menu_window.active = true
      @data, @materials, @quantities, @break_point = [0, 0], [], [], false
      Graphics.transition
      loop do
        Graphics.update
        Input.update
        update
        break if @break_point
      end
      if @exp_gained != nil
        @info_window = Window_Dialog.new(320, [],
            "You have earned #{@exp_gained} EXP")
        50.times { Graphics.update }
        @info_window.dispose
      end
      Graphics.freeze
      @main_window.dispose if @main_window != nil
      @status_window.dispose if @status_window != nil
      @menu_window.dispose if @menu_window != nil
    end
    $scene = @last_scene.new
  end
  
  def update
    if @menu_window.active
      update_main
    elsif @input_window != nil && @input_window.active
      update_input
    elsif @template_window != nil
      update_template_dialog
    elsif @icons_window != nil && @icons_window.active
      update_icons
    elsif @anima_windows != nil && @anima_windows.any? {|obj| obj.active}
      update_animations
    elsif @recreate_window != nil && @recreate_window.active
      update_recreation
    elsif @number_window != nil
      update_material_number
    elsif @materials_window != nil
      update_materials
    elsif @states_window != nil
      update_states
    elsif @sounds_window != nil
      update_sounds
    elsif @scopes_window != nil
      update_scopes
    elsif @variation_window != nil
      update_variation
    elsif @hitrate_window != nil
      update_hitrate
    elsif @parameters_window != nil
      update_parameters
    elsif @extras_window != nil
      update_extras
    elsif @armortype_window != nil
      update_armortype
    end
  end
  
  def update_main
    if @confirm_window != nil
      @confirm_window.update
      if Input.trigger?(Input::B)
        $game_system.se_play($data_system.cancel_se)
        @confirm_window.dispose
        @confirm_window = nil
      elsif Input.trigger?(Input::C)
        case @confirm_window.index
        when 0
          $game_system.se_play(BlizzCFG::CREATE_SOUND)
          @exp_gained = @main_window.create(@input_window.number)
          @confirm_window.dispose
          @input_window.dispose
          @confirm_window, @input_window, @break_point = nil, nil, true
        when 1
          $game_system.se_play($data_system.decision_se)
          @confirm_window.dispose
          @confirm_window = nil
        end
      end
    elsif @input_window != nil
      @input_window.update
      if Input.trigger?(Input::B)
        $game_system.se_play($data_system.cancel_se)
        if @old_data != nil
          object, @materials, @quantities = @old_data
          @main_window.setup(object, @materials, @quantities)
        end
        @input_window.dispose
        @input_window = nil
      elsif Input.trigger?(Input::C)
        $game_system.se_play($data_system.decision_se)
        @confirm_window = Window_Dialog.new(192)
      end
    else
      update_command
    end
  end
  
  def update_command
    @main_window.update
    @status_window.update
    @menu_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @break_point = true
    elsif Input.trigger?(Input::C)
      case @menu_window.index
      when 0
        if BlizzCFG::REQUIRE_MATERIAL && @materials.size == 0
          $game_system.se_play($data_system.buzzer_se)
        else
          $game_system.se_play($data_system.decision_se)
          @old_data = [@main_window.object, @materials, @quantities]
          setup_final_steps
        end
      when 1
        $game_system.se_play($data_system.decision_se)
        @menu_window.active = false
        @help_window = Window_Help.new
        @help_window.z = 300
        @added_window = Window_CreationAdded.new(
            ['Added materials:'] + @materials, [-1] + @quantities)
        @added_window.index = -1
        level = case @type
        when 0 then @actor.lvlforger
        when 1 then @actor.lvlarmorer
        when 2 then @actor.lvlenchanter
        end
        @materials_window = Window_CreationMaterial.new(@help_window, level)
        @materials.each_index {|i|
            @materials_window.draw(@materials[i], @quantities[i])}
        @materials_window.active = true
      when 2, 3
        $game_system.se_play($data_system.decision_se)
        @main_window.mode = @menu_window.index - 1
        @menu_window.active = false
        @input_window = Window_CreationInput.new
        @input_window.z = 300
        @tbc_sprite = Sprite.new
        @tbc_sprite.bitmap = Bitmap.new(640, 480)
        @tbc_sprite.bitmap.fill_rect(0, 0, 640, 480, Color.new(0, 0, 0, 96))
        if @menu_window.index == 2
          x = BlizzCFG::RIGHT_WINS ? 92 : 288
          @tbc_sprite.bitmap.fill_rect(x, 46, 336, 28, Color.new(0, 0, 0, 0))
        else
          x = BlizzCFG::RIGHT_WINS ? 12 : 208
          @tbc_sprite.bitmap.fill_rect(x, 102, 416, 28, Color.new(0, 0, 0, 0))
        end
        @tbc_sprite.z = 299
      when 4
        $game_system.se_play($data_system.decision_se)
        @menu_window.active = false
        @icons_window = Window_Icons.new
        if @main_window.object.icon_name != ''
          @icons_window.index =
              $game_system.icon_filenames.index(@main_window.object.icon_name)+1
        end
        @help_window = Window_Help.new
        @help_window.set_text('Choose the Icon.', 1)
        @help_window.z = 300
      when 5
        @menu_window.active = false
        $game_system.se_play($data_system.decision_se)
        if @type == 1
          commands = [$data_system.words.armor1, $data_system.words.armor2,
                      $data_system.words.armor3, $data_system.words.armor4]
          @armortype_window = Window_Command.new(192, commands)
          @armortype_window.x = 448 if BlizzCFG::RIGHT_WINS
          @armortype_window.y, @armortype_window.z = 224, 500
          @armortype_window.index = @main_window.object.kind
        else
          @anima_windows = [Window_Animations.new, Window_Animations.new(false)]
          if @main_window.object.animation1_id != 0
            @anima_windows[0].index = $game_system.animations.index(
                @main_window.object.animation1_id) + 1
          end
          if @main_window.object.animation2_id != 0
            @anima_windows[1].index = $game_system.animations.index(
                @main_window.object.animation2_id) + 1
          end
          @help_window = Window_Help.new
          @help_window.set_text('Choose the animations. Press SHIFT for preview.', 1)
          @help_window.z = 300
          @tbc_sprite = Sprite.new
          @tbc_sprite.bitmap = Bitmap.new(640, 480)
          @tbc_sprite.bitmap.fill_rect(0, 0, 640, 480, Color.new(0, 0, 0, 160))
          @tbc_sprite.z = 190
          @anima_sprite = Sprite_Animation.new
        end
      when 6
        case @type
        when 1
          $game_system.se_play($data_system.decision_se)
          commands = ['[None]']
          @main_window.auto_states.each {|id|
              commands.push($data_states[id].name) if id != 0}
          @states_window = Window_Command.new(192, commands)
          @states_window.x = 448 if BlizzCFG::RIGHT_WINS
          @states_window.height = 320 if @states_window.height > 320
          @states_window.y, @states_window.z = 160, 1000
          @states_window.index =
              @main_window.auto_states.index(@main_window.object.auto_state_id)
          @menu_window.active = false
        when 2
          $game_system.se_play($data_system.decision_se)
          commands = ['Menu sound', 'Target scope', 'Variance', 'Hit rate', 'Parameter', 'Exit']
          @extras_window = Window_Command.new(192, commands)
          @extras_window.x = 448 if BlizzCFG::RIGHT_WINS
          @extras_window.y, @extras_window.z = 160, 500
          @extras_window.disable_item(1) if @main_window.scopes.size == 1
          if @main_window.attributes.all? {|i| i == 0}
            @extras_window.disable_item(4)
          end
          @menu_window.active = false
        else
          $game_system.se_play($data_system.buzzer_se)
        end
      when 7
        size = case @type
        when 0 then $game_system.weapons.size
        when 1 then $game_system.armors.size
        when 2 then $game_system.items.size
        end
        if size == 0
          $game_system.se_play($data_system.buzzer_se)
        else
          $game_system.se_play($data_system.decision_se)
          @menu_window.active = false
          @recreate_window = Window_CreationItem.new(@type)
          @recreate_window.help_window = @help_window = Window_Help.new
          @recreate_window.z = @help_window.z = 500
        end
      when 8
        $game_system.se_play($data_system.decision_se)
        @break_point = true
      end
    end
  end
  
  def setup_final_steps
    max = BlizzCFG::MAX_ITEMS - case @main_window.object
    when RPG::Weapon then $game_party.weapon_number(@main_window.object.id)
    when RPG::Armor then $game_party.armor_number(@main_window.object.id)
    when RPG::Item then $game_party.item_number(@main_window.object.id)
    end
    @materials.each_index {|i|
        number = case @materials[i]
        when RPG::Weapon then $game_party.weapon_number(@materials[i].id)
        when RPG::Armor then $game_party.armor_number(@materials[i].id)
        when RPG::Item then $game_party.item_number(@materials[i].id)
        end
        val = number / @quantities[i]
        max = val if max > val}
    @input_window = Window_CreationNumberItem.new(1, max, 1, @main_window.object)
  end
  
  def update_input
    @input_window.update
    if Input.repeat?(Input::B)
      if @main_window.input.size == 0
        if Input.trigger?(Input::B)
          $game_system.se_play($data_system.cancel_se)
          @main_window.apply(false)
          @input_window.dispose
          @tbc_sprite.dispose
          @input_window, @tbc_sprite, @menu_window.active = nil, nil, true
        end
      else
        $game_system.se_play($data_system.cancel_se)
        @main_window.input = @main_window.input[0, @main_window.input.size-1]
        @main_window.refresh_mode
      end
    elsif Input.repeat?(Input::C)
      if @input_window.character == nil
        $game_system.se_play($data_system.decision_se)
        @main_window.apply(@main_window.input != '')
        @input_window.dispose
        @tbc_sprite.dispose
        @input_window, @tbc_sprite, @menu_window.active = nil, nil, true
      elsif @main_window.input.size == (@menu_window.index == 2 ? 40 : 100)
        $game_system.se_play($data_system.buzzer_se)
      else
        $game_system.se_play($data_system.decision_se)
        @main_window.input += @input_window.character
        @main_window.refresh_mode
      end
    end
  end

  def update_icons
    @icons_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @icons_window.dispose
      @help_window.dispose
      @icons_window, @help_window, @menu_window.active = nil, nil, true
    elsif Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      if @icons_window.index == 0
        @main_window.object.icon_name = ''
      else
        @main_window.object.icon_name =
            $game_system.icon_filenames[@icons_window.index-1]
      end
      @main_window.refresh
      @icons_window.dispose
      @help_window.dispose
      @icons_window, @help_window, @menu_window.active = nil, nil, true
    end
  end

  def update_animations
    @tbc_sprite.update
    @anima_sprite.update
    if @data[0] > 0
      @data[0] -= 1
      if @data[0] == $data_animations[@data[1]].frame_max + 20
        @anima_sprite.animation_id = @data[1]
      elsif @data[0] == 0
        @anima_sprite.z -= 500
        @tbc_sprite.z -= 500
        @help_window.set_text('Choose the animations. Press SHIFT for preview.', 1)
      end
      return
    end
    @anima_windows.each {|obj| obj.update}
    if Input.trigger?(Input::A)
      if @anima_windows[0].active && @anima_windows[0].index > 0
        id = $game_system.animations[@anima_windows[0].index-1]
        @data = [$data_animations[id].frame_max + 30, id]
        @anima_sprite.z += 500
        @tbc_sprite.z += 500
        @help_window.set_text('This is animation 1.', 1)
      elsif @anima_windows[1].active && @anima_windows[1].index > 0
        id = $game_system.animations[@anima_windows[1].index-1]
        @data = [$data_animations[id].frame_max + 30, id]
        @anima_sprite.z += 500
        @tbc_sprite.z += 500
        @help_window.set_text('This is animation 2.', 1)
      else
        $game_system.se_play($data_system.buzzer_se)
      end
    elsif Input.trigger?(Input::LEFT) || Input.trigger?(Input::RIGHT)
      $game_system.se_play($data_system.cursor_se)
      @anima_windows.each {|obj| obj.active = !obj.active}
    elsif Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      (@anima_windows + [@tbc_sprite, @anima_sprite, @help_window]).each {|obj|
          obj.dispose}
      @anima_windows = @tbc_sprite = @anima_sprite = @help_window = nil
      @menu_window.active = true
    elsif Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      if @anima_windows[0].index == 0
        @main_window.object.animation1_id = 0
      else
        @main_window.object.animation1_id =
            $game_system.animations[@anima_windows[0].index-1]
      end
      if @anima_windows[1].index == 0
        @main_window.object.animation2_id = 0
      else
        @main_window.object.animation2_id =
            $game_system.animations[@anima_windows[1].index-1]
      end
      @main_window.refresh
      (@anima_windows + [@tbc_sprite, @anima_sprite, @help_window]).each {|obj|
          obj.dispose}
      @anima_windows = @tbc_sprite = @anima_sprite = @help_window = nil
      @menu_window.active = true
    end
  end
  
  def update_material_number
    @number_window.update
    if Input.repeat?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @number_window.dispose
      @number_window, @materials_window.active = nil, true
    elsif Input.repeat?(Input::C)
      $game_system.se_play($data_system.decision_se)
      @added_window.set_material(@materials_window.item, @number_window.number)
      @materials_window.draw(@materials_window.item, @number_window.number)
      @number_window.dispose
      @number_window, @materials_window.active = nil, true
    end
  end
  
  def update_materials
    @materials_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @materials = @added_window.materials
      @quantities = @added_window.quantities
      @help_window.dispose
      @materials_window.dispose
      @added_window.dispose
      @help_window = @materials_window = @added_window = nil
      @menu_window.active = true
      @main_window.refresh(@materials, @quantities)
      if BlizzCFG::REQUIRE_MATERIAL
        @menu_window.refresh
        @disabled.each {|i| @menu_window.disable_item(i)}
        @menu_window.disable_item(0) if @materials.size == 0
      end
    elsif Input.trigger?(Input::C)
      item = @materials_window.item
      if item == nil
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      $game_system.se_play($data_system.decision_se)
      max = case item
      when RPG::Weapon then $game_party.weapon_number(item.id)
      when RPG::Armor then $game_party.armor_number(item.id)
      when RPG::Item then $game_party.item_number(item.id)
      end
      if @added_window.materials.include?(item)
        index = @added_window.materials.index(item)
        number = @added_window.quantities[index]
      else
        number = 0
      end
      @number_window = Window_CreationNumberItem.new(0, max, number, item)
    end
  end
  
  def update_states
    @states_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @states_window.dispose
      @states_window, @menu_window.active = nil, true
    elsif Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      @main_window.object.auto_state_id =
          @main_window.auto_states[@states_window.index]
      @main_window.refresh
      @states_window.dispose
      @states_window, @menu_window.active = nil, true
    end
  end
  
  def update_sounds
    @sounds_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @sounds_window.dispose
      @help_window.dispose
      @sounds_window, @help_window, @extras_window.active = nil, nil, true
    elsif Input.trigger?(Input::A)
      if @sounds_window.index > 0
        sound = RPG::AudioFile.new(@sounds_window.item, 80, 100)
        $game_system.se_play(sound)
      end
    elsif Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      if @sounds_window.index > 0
        @main_window.object.menu_se.name = @sounds_window.item
      else
        @main_window.object.menu_se.name = ''
      end
      @main_window.refresh
      @sounds_window.dispose
      @help_window.dispose
      @sounds_window, @help_window, @extras_window.active = nil, nil, true
    end
  end
  
  def update_scopes
    @scopes_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @scopes_window.dispose
      @scopes_window, @params, @extras_window.active = nil, nil, true
    elsif Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      @main_window.object.scope = @params[@scopes_window.index]
      @main_window.refresh
      @scopes_window.dispose
      @scopes_window, @params, @extras_window.active = nil, nil, true
    end
  end
  
  def update_variation
    @variation_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @variation_window.dispose
      @variation_window, @extras_window.active = nil, true
    elsif Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      @main_window.object.variance = @variation_window.number
      @main_window.refresh
      @variation_window.dispose
      @variation_window, @extras_window.active = nil, true
    end
  end
  
  def update_hitrate
    @hitrate_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @main_window.object.hit = @hitrate_window.old_value
      @main_window.refresh
      @hitrate_window.dispose
      @hitrate_window, @extras_window.active = nil, true
    elsif Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      @main_window.object.hit = @hitrate_window.number
      @main_window.set_item_extras
      @hitrate_window.dispose
      @hitrate_window, @extras_window.active = nil, true
    elsif @hitrate_window.should_refresh_main?
      @main_window.object.hit = @hitrate_window.number
      @main_window.set_item_extras
      @main_window.refresh
    end
  end
  
  def update_parameters
    @parameters_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @parameters_window.dispose
      @parameters_window, @params, @extras_window.active = nil, nil, true
    elsif Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      data = @params[@parameters_window.index]
      @main_window.set_parameter(data[0], data[1])
      @main_window.refresh
      @parameters_window.dispose
      @parameters_window, @params, @extras_window.active = nil, nil, true
    end
  end
  
  def update_extras
    @extras_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @extras_window.dispose
      @extras_window, @menu_window.active = nil, true
    elsif Input.trigger?(Input::C)
      case @extras_window.index
      when 0
        $game_system.se_play($data_system.decision_se)
        @sounds_window = Window_Sounds.new
        @help_window = Window_Help.new
        @help_window.set_text('Choose a menu sound. Press SHIFT for preview.')
        @sounds_window.z, @help_window.z = 1000, 300
        @sounds_window.set_index(@main_window.object.menu_se.name)
        @extras_window.active = false
      when 1
        if @main_window.scopes.size == 1
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        $game_system.se_play($data_system.decision_se)
        commands, @params = [], []
        @main_window.scopes.each {|i|
            commands.push(@main_window.scope_text[i])
            @params.push(i)}
        @scopes_window = Window_Command.new(192, commands)
        @scopes_window.x = 448 if BlizzCFG::RIGHT_WINS
        @scopes_window.y, @scopes_window.z = 224, 600
        @scopes_window.index = @params.index(@main_window.object.scope)
      when 2
        $game_system.se_play($data_system.decision_se)
        @variation_window = Window_CreationNumberPercent.new(0, 100,
            @main_window.object.variance, 'Variance')
        @extras_window.active = false
      when 3
        $game_system.se_play($data_system.decision_se)
        @hitrate_window = Window_CreationNumberPercent.new(1, 100,
            @main_window.object.hit, 'Hit rate')
        @extras_window.active = false
      when 4
        if @main_window.attributes.all? {|i| i == 0}
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        $game_system.se_play($data_system.decision_se)
        commands, @params = ['[None]'], [[0, 0]]
        words = ["max #{$data_system.words.hp}", "max #{$data_system.words.sp}",
                 $data_system.words.str, $data_system.words.dex,
                 $data_system.words.agi, $data_system.words.int]
        index = @main_window.object.parameter_type
        words.each_index {|i|
            if @main_window.attributes[i] > 0
              commands.push("#{words[i]} +#{@main_window.attributes[i]}")
              @params.push([i+1, @main_window.attributes[i]])
            elsif i <= index && index > 1
              index -= 1
            end}
        @parameters_window = Window_Command.new(192, commands)
        @parameters_window.x = 448 if BlizzCFG::RIGHT_WINS
        @parameters_window.y, @parameters_window.z = 224, 600
        @parameters_window.index = index
      when 5
        $game_system.se_play($data_system.decision_se)
        @extras_window.dispose
        @extras_window, @menu_window.active = nil, true
      end
    end
  end
  
  def update_armortype
    @armortype_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @armortype_window.dispose
      @armortype_window, @menu_window.active = nil, true
    elsif Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      @main_window.object.kind = @armortype_window.index
      @main_window.refresh
      @armortype_window.dispose
      @armortype_window, @menu_window.active = nil, true
    end
  end
  
  def update_template_dialog
    @template_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      object, @materials, @quantities = @old_data
      @main_window.setup(object, @materials, @quantities)
      @recreate_window.active = @recreate_window.visible = true
      @template_window.dispose
      @template_window = nil
    elsif Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      case @template_window.index
      when 0
        setup_final_steps
        @recreate_window.dispose
        @help_window.dispose
        @recreate_window, @help_window, @menu_window.active = nil, nil, true
      when 1
        case @type
        when 0 then @main_window.object.id = 1000 + $game_system.weapons.size
        when 1 then @main_window.object.id = 1000 + $game_system.armors.size
        when 2 then @main_window.object.id = 1000 + $game_system.items.size
        end
        @main_window.object.name += ' (COPY)'
        @main_window.refresh
        @recreate_window.dispose
        @help_window.dispose
        @recreate_window, @help_window, @menu_window.active = nil, nil, true
      when 2
        object, @materials, @quantities = @old_data
        @main_window.setup(object, @materials, @quantities)
        @recreate_window.active = @recreate_window.visible = true
      end
      @template_window.dispose
      @template_window = nil
    end
  end
  
  def update_recreation
    @recreate_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @recreate_window.dispose
      @help_window.dispose
      @recreate_window, @help_window, @menu_window.active = nil, nil, true
    elsif Input.trigger?(Input::C)
      object = @recreate_window.item
      if object == '[Cancel]'
        $game_system.se_play($data_system.decision_se)
        @recreate_window.dispose
        @help_window.dispose
        @recreate_window, @help_window, @menu_window.active = nil, nil, true
        return
      elsif object == nil || !$game_system.requirements(object)
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      $game_system.se_play($data_system.decision_se)
      @old_data = [@main_window.object, @materials, @quantities]
      reqs = $game_system.require[object]
      @materials, @quantities = [], []
      reqs.each {|req|
          case req[0]
          when 0 then @materials.push($data_weapons[req[1]])
          when 1 then @materials.push($data_armors[req[1]])
          when 2 then @materials.push($data_items[req[1]])
          end
          @quantities.push(req[2])}
      @main_window.setup(object.clone, @materials, @quantities)
      @recreate_window.active = @recreate_window.visible =
          @help_window.visible = false
      @template_window = Window_Dialog.new(320, ['Create again', 'Use as template',
          'Cancel'], 'What do you want to do?')
    end
  end
  
  def make_raw_weapon
    w = RPG::Weapon.new
    w.id = 1000 + $game_system.weapons.size
    w.name = "#{$data_system.words.weapon} ID <#{w.id}>"
    w.description = "#{$data_system.words.weapon} ID <#{w.id}>"
    return w
  end
  
  def make_raw_armor
    a = RPG::Armor.new
    a.id = 1000 + $game_system.armors.size
    a.name = "#{BlizzCFG::ARMOR} ID <#{a.id}>"
    a.description = "#{BlizzCFG::ARMOR} ID <#{a.id}>"
    return a
  end
  
  def make_raw_item
    i = RPG::Item.new
    i.id = 1000 + $game_system.items.size
    i.name = "#{$data_system.words.item} ID <#{i.id}>"
    i.description = "#{$data_system.words.item} ID <#{i.id}>"
    return i
  end
  
end
