#===============================================================================
# 
# Yanfly Engine Melody - Item Overhaul
# Last Date Updated: 2010.06.20
# Level: Normal, Hard
# 
# The item scene can become seriously lacking as it often keeps players in the
# dark as to what items actually do. This script rewrites the item scene and
# provides the player adequate information about how an item functions, be it
# a healing item, battle item, weapon, or a piece of armour. And if needed,
# custom data can be inserted, too.
# 
#===============================================================================
# Updates
# -----------------------------------------------------------------------------
# o 2010.06.20 - Finished Script.
# o 2010.06.18 - Started Script.
#===============================================================================
# Instructions
# -----------------------------------------------------------------------------
# To install this script, open up your script editor and copy/paste this script
# to an open slot below ▼ Materials but above ▼ Main. Remember to save.
# 
# -----------------------------------------------------------------------------
# Item and Equip Tags - For Items, Weapons, and Armours.
# -----------------------------------------------------------------------------
# <key item>
# This labels the item as a key item, making it appear in the key item category
# inside of the item scene. Does nothing else.
# 
# <custom data>
# icon, string1, string2
# </custom data>
# This allows you to input in custom data to be displayed in the item list
# data window. Replace "icon" with a number to depict which icon you want
# shown in the data. Replace "string1" with the category you want shown in the
# data. Replace "string2" with the text you want shown in the data.
#===============================================================================

$imported = {} if $imported == nil
$imported["ItemOverhaul"] = true

module YEM
  module ITEM
    
    #===========================================================================
    # Section I. Basic Settings
    # --------------------------------------------------------------------------
    # The following below will adjust the basic settings and vocabulary that
    # alter the basic item scene the players see before them.
    #===========================================================================
    
    # This adjusts the commands used within the item scene. The command window
    # is the window that lets you switch between various item scene options.
    # 
    # :usable           Opens up usable menu items.
    # :battle           Views battle-only items.
    # :weapons          Views weapons list.
    # :armours          Views armours list.
    # :misc             Views miscellaneous items.
    # :key_items        Views key items.
    # :all_items        Views all items.
    # 
    COMMANDS =[
      :usable,        # Opens up usable menu items.
      :battle,        # Views battle-only items.
      :weapons,       # Views weapons list.
      :armours,       # Views armours list.
      :misc,          # Views miscellaneous items.
      :key_items,     # Views key items.
    # :all_items,     # Views all items.
    ] # Do not remove this.
    
    # The following determines the vocabulary used for the remade item scene.
    # Adjust it accordingly to set the way text is displayed.
    VOCAB ={
      :usable         => "Usable",
      :battle         => "Battle",
      :weapons        => "Weapons",
      :armours        => "Armours",
      :misc           => "Various",
      :key_items      => "Key Items",
      :all_items      => "All Items",
    } # Do not remove this.
    
    #===========================================================================
    # Section II. Data Window Settings
    # --------------------------------------------------------------------------
    # These settings adjust what you see in the data window shown to the lower
    # right of the item scene. The window shows the selected item's properties.
    # The data represented differs depending on the item, its tags, and whether
    # or not it is an item, weapon, or armour.
    #===========================================================================
    
    # These constants determine how the gold value appears in the data windows.
    DRAW_GOLD_ICON  = 205
    DRAW_GOLD_TEXT  = "Value"
    
    # This adjusts the settings shown for items. Adjust as you see fit. The
    # majority of these keys are vocab settings.
    ITEM_DATA ={
      :properties   => "Properties",
      :gold_value   => true,
      :fontsize     => 16,
      :hp_heal_icon => 128,
      :hp_heal_text => "HP Heal",
      :mp_heal_icon => 214,
      :mp_heal_text => "MP Heal",
      :dmg_icon     => 119,
      :base_dmg     => "Base Damage",
      :heal_icon    => 128,
      :base_heal    => "Base Healing",
      :multiplier   => "%s Multiplier",
      :element      => "Element",
      :add_state    => "Applies",
      :rem_state    => "Cancels",
      :growth       => "%s Growth",
      :resist       => "%s Resist",
      :bonus        => "%s Bonus",
    } # Do not remove this.
    
    # This adjusts the settings shown for equipment. Adjust as you see fit. For
    # :shown_stats, valid stats to be shown are as follows:
    #   :hp, :mp, :atk, :def, :spi, :res, :dex, :agi
    EQUIP_DATA ={
      :properties   => "Attributes",
      :gold_value   => false,
      :fontsize     => 16,
      :shown_stats  => [:hp, :mp, :atk, :def, :spi, :res, :dex, :agi],
      :ele_weapon   => "Adds",
      :ele_armour   => "Guards",
      :state_weapon => "Applies",
      :state_armour => "Resists",
    } # Do not remove this.
    
    # This array contains all of the elements you want displayed and in which
    # order to display them in. You can display them as individual integers or
    # as number ranges.
    SHOWN_ELEMENTS = [3, 4, 5, 6..10]
    
    # Since elements do not have icons innately, use the following hash below
    # adjust and apply icons to them.
    ELEMENT_ICONS ={
      3 => 104,  # Fire element.
      4 => 105,  # Ice element.
      5 => 106,  # Volt element.
      6 => 108,  # Earth element.
      7 => 107,  # Water element.
      8 => 109,  # Air element.
      9 => 110,  # Light element.
     10 => 111,  # Dark element.
    } # Do not remove this.
    
  end # ITEM
end # YEM

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

module YEM
  module REGEXP
  module BASEITEM
      
    KEY_ITEM = /<(?:KEY_ITEM|key item|key)>/i
    
    CUSTOM_DATA1 = /<(?:CUSTOM_DATA|custom data)>/i
    CUSTOM_DATA2 = /<\/(?:CUSTOM_DATA|custom data)>/i
    
  end # BASEITEM
  end # REGEXP
  module ITEM
    module_function
    #--------------------------------------------------------------------------
    # convert_integer_array
    #--------------------------------------------------------------------------
    def convert_integer_array(array)
      result = []
      array.each { |i|
        case i
        when Range; result |= i.to_a
        when Integer; result |= [i]
        end }
      return result
    end
  
    #--------------------------------------------------------------------------
    # converted_contants
    #--------------------------------------------------------------------------
    SHOWN_ELEMENTS = convert_integer_array(SHOWN_ELEMENTS)
  end # ITEM
end # YEM

#===============================================================================
# module Icon
#===============================================================================

module Icon
  
  #--------------------------------------------------------------------------
  # self.gold_cost
  #--------------------------------------------------------------------------
  def self.gold_cost; return YEM::ITEM::DRAW_GOLD_ICON; end
  
  #--------------------------------------------------------------------------
  # self.element
  #--------------------------------------------------------------------------
  def self.element(element_id)
    icon = YEM::ITEM::ELEMENT_ICONS[element_id]
    return (icon == nil) ? 0 : icon
  end
  
  #--------------------------------------------------------------------------
  # self.hp_heal
  #--------------------------------------------------------------------------
  def self.hp_heal; return YEM::ITEM::ITEM_DATA[:hp_heal_icon]; end
  
  #--------------------------------------------------------------------------
  # self.mp_heal
  #--------------------------------------------------------------------------
  def self.mp_heal; return YEM::ITEM::ITEM_DATA[:mp_heal_icon]; end
  
  #--------------------------------------------------------------------------
  # self.base_damage
  #--------------------------------------------------------------------------
  def self.base_damage; return YEM::ITEM::ITEM_DATA[:dmg_icon]; end
  
  #--------------------------------------------------------------------------
  # self.base_healing
  #--------------------------------------------------------------------------
  def self.base_healing; return YEM::ITEM::ITEM_DATA[:heal_icon]; end
  
  #--------------------------------------------------------------------------
  # self.stat
  #--------------------------------------------------------------------------
  def self.stat(actor, item); return 0; end
  
end # Icon

#===============================================================================
# RPG::BaseItem
#===============================================================================

class RPG::BaseItem
  
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :custom_data
  attr_accessor :key_item
  
  #--------------------------------------------------------------------------
  # common cache: yem_cache_baseitem_io
  #--------------------------------------------------------------------------
  def yem_cache_baseitem_io
    return if @cached_baseitem_io; @cached_baseitem_io = true
    @key_item = false
    enable_custom_data = false
    @custom_data = [] unless self.is_a?(RPG::Skill)
    self.note.split(/[\r\n]+/).each { |line|
      case line
      #---
      when YEM::REGEXP::BASEITEM::KEY_ITEM
        @key_item = true
      #---
      when YEM::REGEXP::BASEITEM::CUSTOM_DATA1
        next if self.is_a?(RPG::Skill)
        enable_custom_data = true
      when YEM::REGEXP::BASEITEM::CUSTOM_DATA2
        next if self.is_a?(RPG::Skill)
        enable_custom_data = false
      when /(\d+),[ ](.*),[ ](.*)/i
        next unless enable_custom_data
        next if self.is_a?(RPG::Skill)
        array = [$1.to_i, $2.to_s, $3.to_s]
        @custom_data.push(array)
      end
    } # end self.note.split
  end # yem_cache_baseitem_io
  
end # RPG::BaseItem

#===============================================================================
# Scene_Title
#===============================================================================

class Scene_Title < Scene_Base
  
  #--------------------------------------------------------------------------
  # alias method: load_bt_database
  #--------------------------------------------------------------------------
  alias load_bt_database_io load_bt_database unless $@
  def load_bt_database
    load_bt_database_io
    load_io_cache
  end
  
  #--------------------------------------------------------------------------
  # alias method: load_database
  #--------------------------------------------------------------------------
  alias load_database_io load_database unless $@
  def load_database
    load_database_io
    load_io_cache
  end
  
  #--------------------------------------------------------------------------
  # new method: load_io_cache
  #--------------------------------------------------------------------------
  def load_io_cache
    groups = [$data_items, $data_weapons, $data_armors]
    for group in groups
      for obj in group
        next if obj == nil
        obj.yem_cache_baseitem_io if obj.is_a?(RPG::BaseItem)
      end
    end
  end
  
end # Scene_Title

#===============================================================================
# Window_ItemStatus
#===============================================================================

class Window_ItemStatus < Window_Base
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize
    super(160, 0, Graphics.width - 160, 128)
    refresh
  end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    @item = @item_window == nil ? nil : @item_window.item
    draw_item
    draw_party_members
  end
  
  #--------------------------------------------------------------------------
  # set_item_window
  #--------------------------------------------------------------------------
  def set_item_window(new_item_window)
    @item_window = new_item_window
    update
  end
  
  #--------------------------------------------------------------------------
  # update
  #--------------------------------------------------------------------------
  def update
    super
    return if @item_window == nil
    refresh if @item != @item_window.item
  end
  
  #--------------------------------------------------------------------------
  # draw_item
  #--------------------------------------------------------------------------
  def draw_item
    return if @item == nil
    draw_icon(@item.icon_index, 0, 0)
    self.contents.draw_text(24, 0, contents.width-28, WLH, @item.name, 0)
  end
  
  #--------------------------------------------------------------------------
  # draw_party_members
  #--------------------------------------------------------------------------
  def draw_party_members
    $game_temp.in_battle = true
    size = $game_party.members.size
    dx = (contents.width - (size * 64))/2 + 32
    dy = contents.height * 3/4
    for member in $game_party.members
      next if member == nil
      draw_member(member, dx, dy)
      dx += 64
    end
    $game_temp.in_battle = false
  end
  
  #--------------------------------------------------------------------------
  # draw_member
  #--------------------------------------------------------------------------
  def draw_member(actor, dx, dy)
    bitmap = Cache.character(actor.character_name)
    sign = actor.character_name[/^[\!\$]./]
    if sign != nil and sign.include?('$')
      cw = bitmap.width / 3
      ch = bitmap.height / 4
    else
      cw = bitmap.width / 12
      ch = bitmap.height / 8
    end
    n = actor.character_index
    src_rect = Rect.new((n%4*3+1)*cw, (n/4*4)*ch, cw, ch)
    if @item.is_a?(RPG::Weapon) or @item.is_a?(RPG::Armor)
      opacity = actor.equippable?(@item) ? 255 : 128
    elsif @item.is_a?(RPG::Item)
      opacity = @item.for_friend? ? 255 : 128
    else
      opacity = 255
    end
    self.contents.blt(dx - cw / 2, dy - ch, bitmap, src_rect, opacity)
  end
  
end # Window_ItemStatus

#===============================================================================
# Window_ItemList
#===============================================================================

class Window_ItemList < Window_Selectable
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(help_window)
    @help_window = help_window
    dy = @help_window.y + @help_window.height
    super(0, dy, Graphics.width - 240, Graphics.height - dy)
    refresh
    self.active = false
    self.index = 0
  end
  
  #--------------------------------------------------------------------------
  # item
  #--------------------------------------------------------------------------
  def item; return @data[[self.index, 0].max]; end
  
  #--------------------------------------------------------------------------
  # used_item_refresh
  #--------------------------------------------------------------------------
  def used_item_refresh(array)
    for item in array
      next if item == nil
      next unless @data.include?(item)
      refresh
      break
    end
  end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    @data = []
    for item in $game_party.items
      next unless include?(item)
      @data.push(item)
    end
    @data.push(nil) if @data.size <= 0
    @item_max = @data.size
    self.index = [self.index, @item_max - 1].min
    create_contents
    for i in 0...@item_max; draw_item(i); end
  end
  
  #--------------------------------------------------------------------------
  # include?
  #--------------------------------------------------------------------------
  def include?(item)
    return false if item == nil
    return false if item.name == ""
    return false unless item.is_a?(RPG::Item)
    return item.menu_ok?
  end
  
  #--------------------------------------------------------------------------
  # draw_item
  #--------------------------------------------------------------------------
  def draw_item(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    item = @data[index]
    return if item == nil
    enabled = enable?(item)
    draw_obj_name(item, rect.clone, enabled)
    draw_obj_charges(item, rect.clone, enabled)
    draw_obj_total(item, rect.clone, enabled)
  end
  
  #--------------------------------------------------------------------------
  # enable?
  #--------------------------------------------------------------------------
  def enable?(item)
    return false if item == nil
    return $game_party.has_item?(item)
  end
  
  #--------------------------------------------------------------------------
  # new method: draw_obj_name
  #--------------------------------------------------------------------------
  def draw_obj_name(obj, rect, enabled)
    draw_icon(obj.icon_index, rect.x, rect.y, enabled)
    self.contents.font.size = Font.default_size
    self.contents.font.color = normal_color
    self.contents.font.color.alpha = enabled ? 255 : 128
    rect.width -= 48
    self.contents.draw_text(rect.x+24, rect.y, rect.width-24, WLH, obj.name)
  end
  
  #--------------------------------------------------------------------------
  # new method: draw_obj_charges
  #--------------------------------------------------------------------------
  def draw_obj_charges(obj, rect, enabled)
    return unless $imported["BattleEngineMelody"]
    return unless obj.is_a?(RPG::Item)
    return unless obj.consumable
    return if obj.charges <= 1
    $game_party.item_charges = {} if $game_party.item_charges == nil
    $game_party.item_charges[obj.id] = obj.charges if
      $game_party.item_charges[obj.id] == nil
    charges = $game_party.item_charges[obj.id]
    dx = rect.x; dy = rect.y + WLH/3
    self.contents.font.size = YEM::BATTLE_ENGINE::ITEM_SETTINGS[:charge]
    self.contents.font.color = normal_color
    self.contents.font.color.alpha = enabled ? 255 : 128
    self.contents.draw_text(dx, dy, 24, WLH * 2/3, charges, 2)
    self.contents.font.size = Font.default_size
  end
  
  #--------------------------------------------------------------------------
  # new method: draw_obj_total
  #--------------------------------------------------------------------------
  def draw_obj_total(obj, rect, enabled)
    if $imported["BattleEngineMelody"]
      hash = YEM::BATTLE_ENGINE::ITEM_SETTINGS
    else
      hash ={ :size => Font.default_size, :colour => 0, :text => "×%2d" }
    end
    number = $game_party.item_number(obj)
    dx = rect.x + rect.width - 36; dy = rect.y; dw = 32
    text = sprintf(hash[:text], number)
    self.contents.font.size = hash[:size]
    self.contents.font.color = text_color(hash[:colour])
    self.contents.font.color.alpha = enabled ? 255 : 128
    self.contents.draw_text(dx, dy, dw, WLH, text, 2)
  end
  
  #--------------------------------------------------------------------------
  # update_help
  #--------------------------------------------------------------------------
  def update_help
    @help_window.set_text(item == nil ? "" : item.description)
  end
  
end # Window_ItemList

#===============================================================================
# Window_BattleItemList
#===============================================================================

class Window_BattleItemList < Window_ItemList
  
  #--------------------------------------------------------------------------
  # include?
  #--------------------------------------------------------------------------
  def include?(item)
    return false if item == nil
    return false if item.name == ""
    return false unless item.is_a?(RPG::Item)
    return false if item.menu_ok?
    return item.battle_ok?
  end
  
end # Window_BattleItemList

#===============================================================================
# Window_WeaponItemList
#===============================================================================

class Window_WeaponItemList < Window_ItemList
  
  #--------------------------------------------------------------------------
  # include?
  #--------------------------------------------------------------------------
  def include?(item)
    return false if item == nil
    return false if item.name == ""
    return item.is_a?(RPG::Weapon)
  end
  
end # Window_WeaponItemList

#===============================================================================
# Window_ArmourItemList
#===============================================================================

class Window_ArmourItemList < Window_ItemList
  
  #--------------------------------------------------------------------------
  # include?
  #--------------------------------------------------------------------------
  def include?(item)
    return false if item == nil
    return false if item.name == ""
    return item.is_a?(RPG::Armor)
  end
  
end # Window_ArmourItemList

#===============================================================================
# Window_MiscItemList
#===============================================================================

class Window_MiscItemList < Window_ItemList
  
  #--------------------------------------------------------------------------
  # include?
  #--------------------------------------------------------------------------
  def include?(item)
    return false if item == nil
    return false if item.name == ""
    return false if item.is_a?(RPG::Weapon)
    return false if item.is_a?(RPG::Armor)
    return false if item.menu_ok?
    return false if item.battle_ok?
    return false if item.key_item
    return true
  end
  
end # Window_MiscItemList

#===============================================================================
# Window_KeyItemList
#===============================================================================

class Window_KeyItemList < Window_ItemList
  
  #--------------------------------------------------------------------------
  # include?
  #--------------------------------------------------------------------------
  def include?(item)
    return false if item == nil
    return false if item.name == ""
    return item.key_item
  end
  
end # Window_KeyItemList

#===============================================================================
# Window_AllItemList
#===============================================================================

class Window_AllItemList < Window_ItemList
  
  #--------------------------------------------------------------------------
  # include?
  #--------------------------------------------------------------------------
  def include?(item)
    return false if item == nil
    return false if item.name == ""
    return true
  end
  
end # Window_AllItemList

#===============================================================================
# Window_ItemData
#===============================================================================

class Window_ItemData < Window_Base
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(help_window)
    dy = help_window.y + help_window.height
    super(Graphics.width - 240, dy, 240, Graphics.height - dy)
    create_clone
  end
  
  #--------------------------------------------------------------------------
  # create_clone
  #--------------------------------------------------------------------------
  def create_clone
    @clone = Game_Actor.new(1)
    @clone.maxhp = @clone.base_maxhp
    @clone.maxmp = @clone.base_maxmp
    @clone.atk = @clone.base_atk
    @clone.def = @clone.base_def
    @clone.spi = @clone.base_spi
    @clone.res = @clone.base_res if $imported["RES Stat"]
    @clone.dex = @clone.base_dex if $imported["DEX Stat"]
    @clone.agi = @clone.base_agi
  end
  
  #--------------------------------------------------------------------------
  # set_item_window
  #--------------------------------------------------------------------------
  def set_item_window(new_item_window)
    @item_window = new_item_window
    update
  end
  
  #--------------------------------------------------------------------------
  # update
  #--------------------------------------------------------------------------
  def update
    super
    return if @item_window == nil
    refresh if @item != @item_window.item
  end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    @item = @item_window.item
    return if @item == nil
    dx = 0; dy = 0
    dy = draw_item_name(dy)
    if @item.is_a?(RPG::Item)
      @hash = YEM::ITEM::ITEM_DATA
      dy = draw_item_properties(dy)
    elsif @item.is_a?(RPG::Weapon) or @item.is_a?(RPG::Armor)
      @hash = YEM::ITEM::EQUIP_DATA
      dy = draw_equip_properties(dy)
    end
  end
  
  #--------------------------------------------------------------------------
  # draw_item_name
  #--------------------------------------------------------------------------
  def draw_item_name(dy)
    draw_icon(@item.icon_index, 0, dy)
    self.contents.font.size = Font.default_size
    self.contents.font.color = normal_color
    self.contents.draw_text(24, dy, contents.width-28, WLH, @item.name)
    dy += WLH
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_item_properties
  #--------------------------------------------------------------------------
  def draw_item_properties(dy)
    dy = draw_properties(dy)
    dy = draw_item_value(dy)
    dy = draw_healing_properties(dy)
    dy = draw_base_damage(dy)
    dy = draw_multipliers(dy)
    dy = draw_elements(dy)
    dy = draw_plus_states(dy)
    dy = draw_minus_states(dy)
    dy = draw_item_growth(dy)
    dy = draw_custom_data(dy)
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_equip_properties
  #--------------------------------------------------------------------------
  def draw_equip_properties(dy)
    dy = draw_properties(dy)
    dy = draw_item_value(dy)
    dy = draw_equip_stats(dy)
    dy = draw_elements(dy)
    dy = draw_states(dy)
    dy = draw_custom_data(dy)
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_properties
  #--------------------------------------------------------------------------
  def draw_properties(dy)
    text = @hash[:properties]
    self.contents.font.size = @hash[:fontsize]
    self.contents.font.color = system_color
    self.contents.draw_text(4, dy, contents.width-8, WLH, text, 1)
    dy += WLH
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_item_value
  #--------------------------------------------------------------------------
  def draw_item_value(dy)
    return dy unless @hash[:gold_value]
    return dy if @item.price == 0
    draw_icon(Icon.gold_cost, 0, dy)
    text = YEM::ITEM::DRAW_GOLD_TEXT
    self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
    text = sprintf("%d%s", @item.price, Vocab.gold)
    self.contents.font.color = normal_color
    self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
    dy += WLH
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_healing_properties
  #--------------------------------------------------------------------------
  def draw_healing_properties(dy)
    return dy if dy + WLH > contents.height
    #---
    if @item.hp_recovery_rate != 0 or @item.hp_recovery != 0
      draw_icon(Icon.hp_heal, 0, dy)
      self.contents.font.color = system_color
      text = @hash[:hp_heal_text]
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      self.contents.font.color = normal_color
      if @item.hp_recovery_rate != 0 and @item.hp_recovery != 0
        text = sprintf("%+d%%", @item.hp_recovery_rate)
        self.contents.draw_text(24, dy, (contents.width-28)*2/3, WLH, text, 2)
        text = sprintf("%+d", @item.hp_recovery)
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      elsif @item.hp_recovery_rate != 0
        text = sprintf("%+d%%", @item.hp_recovery_rate)
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      elsif @item.hp_recovery != 0
        text = sprintf("%+d", @item.hp_recovery)
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      end
      dy += WLH
    end
    #---
    return dy if dy + WLH > contents.height
    #---
    if @item.mp_recovery_rate != 0 or @item.mp_recovery != 0
      draw_icon(Icon.mp_heal, 0, dy)
      self.contents.font.color = system_color
      text = @hash[:mp_heal_text]
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      self.contents.font.color = normal_color
      if @item.mp_recovery_rate != 0 and @item.mp_recovery != 0
        text = sprintf("%+d%%", @item.mp_recovery_rate)
        self.contents.draw_text(24, dy, (contents.width-28)*2/3, WLH, text, 2)
        text = sprintf("%+d", @item.mp_recovery)
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      elsif @item.mp_recovery_rate != 0
        text = sprintf("%+d%%", @item.mp_recovery_rate)
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      elsif @item.mp_recovery != 0
        text = sprintf("%+d", @item.mp_recovery)
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      end
      dy += WLH
    end
    #---
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_base_damage
  #--------------------------------------------------------------------------
  def draw_base_damage(dy)
    return dy if dy + WLH > contents.height
    return dy if (-5..5) === @item.base_damage
    if @item.base_damage > 0
      draw_icon(Icon.base_damage, 0, dy)
      text = @hash[:base_dmg]
    else
      draw_icon(Icon.base_healing, 0, dy)
      text = @hash[:base_heal]
    end
    self.contents.font.color = system_color
    self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
    self.contents.font.color = normal_color
    text = (@item.base_damage).abs
    self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2); dy += WLH
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_multipliers
  #--------------------------------------------------------------------------
  def draw_multipliers(dy)
    return dy if dy + WLH > contents.height
    return dy if @item.base_damage == 0
    icon = @item.base_damage > 0 ? Icon.base_damage : Icon.base_healing
    #---
    if @item.atk_f > 0
      draw_icon(icon, 0, dy)
      text = sprintf(@hash[:multiplier], Vocab.atk)
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = sprintf("%s%%", @item.atk_f)
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      dy += WLH
    end
    #---
    return dy if dy + WLH > contents.height
    #---
    if $imported["BattleEngineMelody"] and @item.def_f > 0
      draw_icon(icon, 0, dy)
      text = sprintf(@hash[:multiplier], Vocab.def)
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = sprintf("%s%%", @item.def_f)
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      dy += WLH
    end
    #---
    return dy if dy + WLH > contents.height
    #---
    if @item.spi_f > 0
      draw_icon(icon, 0, dy)
      text = sprintf(@hash[:multiplier], Vocab.spi)
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = sprintf("%s%%", @item.spi_f)
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      dy += WLH
    end
    #---
    return dy if dy + WLH > contents.height
    #---
    if $imported["BattleEngineMelody"] and $imported["RES Stat"] and
    @item.res_f > 0
      draw_icon(icon, 0, dy)
      text = sprintf(@hash[:multiplier], Vocab.res)
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = sprintf("%s%%", @item.res_f)
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      dy += WLH
    end
    #---
    return dy if dy + WLH > contents.height
    #---
    if $imported["BattleEngineMelody"] and $imported["DEX Stat"] and
    @item.dex_f > 0
      draw_icon(icon, 0, dy)
      text = sprintf(@hash[:multiplier], Vocab.dex)
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = sprintf("%s%%", @item.dex_f)
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      dy += WLH
    end
    #---
    return dy if dy + WLH > contents.height
    #---
    if $imported["BattleEngineMelody"] and @item.agi_f > 0
      draw_icon(icon, 0, dy)
      text = sprintf(@hash[:multiplier], Vocab.agi)
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = sprintf("%s%%", @item.agi_f)
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      dy += WLH
    end
    #---
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_elements
  #--------------------------------------------------------------------------
  def draw_elements(dy)
    return dy if @item.element_set == []
    for element_id in YEM::ITEM::SHOWN_ELEMENTS
      break if dy + WLH > contents.height
      next unless @item.element_set.include?(element_id)
      draw_icon(Icon.element(element_id), 0, dy)
      text = @hash[:element] if @item.is_a?(RPG::Item)
      text = @hash[:ele_weapon] if @item.is_a?(RPG::Weapon)
      text = @hash[:ele_armour] if @item.is_a?(RPG::Armor)
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = $data_system.elements[element_id]
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      dy += WLH
    end
    return dy
  end
  
  #--------------------------------------------------------------------------
  # total_drawn_states
  #--------------------------------------------------------------------------
  def total_drawn_states(array)
    result = 0
    for state_id in array
      next if $data_states[state_id] == nil
      next if $data_states[state_id].icon_index == 0
      result += 1
    end
    return [result, 8].min
  end
  
  #--------------------------------------------------------------------------
  # draw_states
  #--------------------------------------------------------------------------
  def draw_states(dy)
    return dy if @item.state_set == []
    total = total_drawn_states(@item.state_set)
    if total == 1
      return dy if dy + WLH > contents.height
      state = $data_states[@item.state_set[0]]
      draw_icon(state.icon_index, 0, dy)
      text = @hash[:state_weapon] if @item.is_a?(RPG::Weapon)
      text = @hash[:state_armour] if @item.is_a?(RPG::Armor)
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = state.name
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
    else
      return dy if dy + WLH*2 > contents.height
      text = @hash[:state_weapon] if @item.is_a?(RPG::Weapon)
      text = @hash[:state_armour] if @item.is_a?(RPG::Armor)
      self.contents.font.color = system_color
      self.contents.draw_text(4, dy, contents.width-8, WLH, text, 1)
      dy += WLH
      dx = (contents.width - total*24)/2
      for state_id in @item.state_set
        break if dx + 24 > contents.width
        state = $data_states[state_id]
        next if state.icon_index == 0
        draw_icon(state.icon_index, dx, dy)
        dx += 24
      end
    end
    dy += WLH
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_plus_states
  #--------------------------------------------------------------------------
  def draw_plus_states(dy)
    return dy if @item.plus_state_set == []
    total = total_drawn_states(@item.plus_state_set)
    if total == 1
      return dy if dy + WLH > contents.height
      state = $data_states[@item.plus_state_set[0]]
      draw_icon(state.icon_index, 0, dy)
      text = @hash[:add_state]
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = state.name
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
    else
      return dy if dy + WLH*2 > contents.height
      text = @hash[:add_state]
      self.contents.font.color = system_color
      self.contents.draw_text(4, dy, contents.width-8, WLH, text, 1)
      dy += WLH
      dx = (contents.width - total*24)/2
      for state_id in @item.plus_state_set
        break if dx + 24 > contents.width
        state = $data_states[state_id]
        next if state.icon_index == 0
        draw_icon(state.icon_index, dx, dy)
        dx += 24
      end
    end
    dy += WLH
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_minus_states
  #--------------------------------------------------------------------------
  def draw_minus_states(dy)
    return dy if @item.minus_state_set == []
    total = total_drawn_states(@item.minus_state_set)
    if total == 1
      return dy if dy + WLH > contents.height
      state = $data_states[@item.minus_state_set[0]]
      draw_icon(state.icon_index, 0, dy)
      text = @hash[:rem_state]
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = state.name
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
    else
      return dy if dy + WLH*2 > contents.height
      text = @hash[:rem_state]
      self.contents.font.color = system_color
      self.contents.draw_text(4, dy, contents.width-8, WLH, text, 1)
      dy += WLH
      dx = (contents.width - total*24)/2
      for state_id in @item.minus_state_set
        break if dx + 24 > contents.width
        state = $data_states[state_id]
        next if state.icon_index == 0
        draw_icon(state.icon_index, dx, dy)
        dx += 24
      end
    end
    dy += WLH
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_item_growth
  #--------------------------------------------------------------------------
  def draw_item_growth(dy)
    #---
    if @item.parameter_type != 0
      case @item.parameter_type
      when 1; text = sprintf(@hash[:growth], Vocab.hp)
      when 2; text = sprintf(@hash[:growth], Vocab.mp)
      when 3; text = sprintf(@hash[:growth], Vocab.atk)
      when 4; text = sprintf(@hash[:growth], Vocab.def)
      when 5; text = sprintf(@hash[:growth], Vocab.spi)
      when 6; text = sprintf(@hash[:growth], Vocab.agi)
      end
      draw_icon(@item.icon_index, 0, dy)
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = sprintf("%+d", @item.parameter_points)
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      dy += WLH
    end
    #---
    if $imported["StatusMenuMelody"]
      for stat in [:hp, :mp, :atk, :def, :spi, :res, :dex, :agi]
        return dy if dy + WLH > contents.height
        next if stat == :res and !$imported["RES Stat"]
        next if stat == :dex and !$imported["DEX Stat"]
        next if @item.stat_growth[stat] == 0 or @item.stat_growth[stat] == nil
        text = sprintf(@hash[:growth], eval("Vocab." + stat.to_s))
        draw_icon(@item.icon_index, 0, dy)
        self.contents.font.color = system_color
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
        text = sprintf("%+d", @item.stat_growth[stat])
        self.contents.font.color = normal_color
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
        dy += WLH
      end
    end
    #---
    if $imported["BattleEngineMelody"]
      growth = []
      for key in @item.element_growth
        growth.push(key[0]) if YEM::ITEM::SHOWN_ELEMENTS.include?(key[0])
      end
      growth.sort!
      for element_id in growth
        return dy if dy + WLH > contents.height
        icon = Icon.element(element_id)
        next if icon == 0
        draw_icon(icon, 0, dy)
        text = sprintf(@hash[:resist], $data_system.elements[element_id])
        self.contents.font.color = system_color
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
        text = sprintf("%+d%%", -@item.element_growth[element_id])
        self.contents.font.color = normal_color
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
        dy += WLH
      end
    end
    #---
    if $imported["BattleEngineMelody"]
      growth = []
      for key in @item.state_growth
        growth.push(key[0])
      end
      growth.sort!
      for state_id in growth
        return dy if dy + WLH > contents.height
        state = $data_states[state_id]
        next if state == nil
        icon = state.icon_index
        next if icon == 0
        draw_icon(icon, 0, dy)
        text = sprintf(@hash[:resist], state.name)
        self.contents.font.color = system_color
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
        text = sprintf("%+d%%", -@item.state_growth[state_id])
        self.contents.font.color = normal_color
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
        dy += WLH
      end
    end
    #---
    if $imported["EquipmentOverhaul"]
      for stat in [:hp, :mp, :atk, :def, :spi, :res, :dex, :agi]
        return dy if dy + WLH > contents.height
        next if stat == :res and !$imported["RES Stat"]
        next if stat == :dex and !$imported["DEX Stat"]
        next if @item.apt_growth[stat] == 0 or @item.apt_growth[stat] == nil
        text = sprintf(@hash[:bonus], eval("Vocab." + stat.to_s))
        draw_icon(@item.icon_index, 0, dy)
        self.contents.font.color = system_color
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
        text = sprintf("%+d%%", @item.apt_growth[stat])
        self.contents.font.color = normal_color
        self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
        dy += WLH
      end
    end
    #---
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_equip_stats
  #--------------------------------------------------------------------------
  def draw_equip_stats(dy)
    dx = 0
    array = [:hp, :mp, :atk, :def, :spi, :res, :dex, :agi]
    for stat in @hash[:shown_stats]
      return if dy + WLH > contents.height
      next if stat == :res and !$imported["RES Stat"]
      next if stat == :dex and !$imported["DEX Stat"]
      next if [:hp, :mp].include?(stat) and !$imported["EquipmentOverhaul"]
      next unless array.include?(stat)
      draw_icon(Icon.stat(@clone, stat), dx, dy)
      text = eval("Vocab." + stat.to_s)
      self.contents.font.color = system_color
      self.contents.draw_text(dx+24, dy, contents.width/2-28, WLH, text, 0)
      stat_text = stat.to_s
      stat_text = "max" + stat_text if ["hp", "mp"].include?(stat_text)
      item_value = eval("@item." + stat_text)
      text = sprintf("%+d", item_value)
      self.contents.font.color = normal_color
      self.contents.font.color.alpha = item_value == 0 ? 128 : 255
      self.contents.draw_text(dx+24, dy, contents.width/2-28, WLH, text, 2)
      if dx == 0
        dx = contents.width/2
      else
        dx = 0
        dy += WLH
      end
    end
    dy += WLH if dx != 0
    return dy
  end
  
  #--------------------------------------------------------------------------
  # draw_custom_data
  #--------------------------------------------------------------------------
  def draw_custom_data(dy)
    return dy if @item.custom_data == []
    for array in @item.custom_data
      break if dy + WLH > contents.height
      draw_icon(array[0], 0, dy)
      text = array[1]
      self.contents.font.color = system_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 0)
      text = array[2]
      self.contents.font.color = normal_color
      self.contents.draw_text(24, dy, contents.width-28, WLH, text, 2)
      dy += WLH
    end
    return dy
  end
  
end # Window_ItemData

#===============================================================================
# Scene_Item
#===============================================================================

class Scene_Item < Scene_Base

  #--------------------------------------------------------------------------
  # overwrite method: start
  #--------------------------------------------------------------------------
  def start
    super
    create_menu_background
    @viewport = Viewport.new(0, 0, Graphics.width, Graphics.height)
    @help_window = Window_Help.new
    @help_window.viewport = @viewport
    @help_window.y = 128
    @data_window = Window_ItemData.new(@help_window)
    @data_window.viewport = @viewport
    @windows = []
    @target_window = Window_MenuStatus.new(0, 0)
    @target_window.visible = false
    @target_window.active = false
    @status_window = Window_ItemStatus.new
    @status_window.viewport = @viewport
    create_command_window
    update_windows
  end
  
  #--------------------------------------------------------------------------
  # new method: create_command_window
  #--------------------------------------------------------------------------
  def create_command_window
    commands = []; @data = []
    for command in YEM::ITEM::COMMANDS
      case command
      when :usable
        @usable_window = Window_ItemList.new(@help_window)
        @windows.push(@usable_window)
      when :battle
        @battle_window = Window_BattleItemList.new(@help_window)
        @windows.push(@battle_window)
      when :weapons
        @weapon_window = Window_WeaponItemList.new(@help_window)
        @windows.push(@weapon_window)
      when :armours
        @armour_window = Window_ArmourItemList.new(@help_window)
        @windows.push(@armour_window)
      when :misc
        @misc_window = Window_MiscItemList.new(@help_window)
        @windows.push(@misc_window)
      when :key_items
        @key_item_window = Window_KeyItemList.new(@help_window)
        @windows.push(@key_item_window)
      when :all_items
        @all_item_window = Window_AllItemList.new(@help_window)
        @windows.push(@all_item_window)
      else; next
      end
      @data.push(command)
      if YEM::ITEM::VOCAB[command] != nil
        commands.push(YEM::ITEM::VOCAB[command])
      end
    end
    for window in @windows
      next if window == nil?
      window.active = false
      window.viewport = @viewport
      window.help_window = @help_window if window.is_a?(Window_Selectable)
    end
    @command_window = Window_Command_Centered.new(160, commands)
    @command_window.viewport = @viewport
    @command_window.height = 128
    @command_window.active = true
  end
  
  #--------------------------------------------------------------------------
  # new method: update_windows
  #--------------------------------------------------------------------------
  def update_windows
    @last_command_index = @command_window.index
    @help_window.y = Graphics.height*8
    @data_window.y = Graphics.height*8
    for window in @windows
      next if window == nil
      window.y = Graphics.height*8
    end
    #---
    case @data[@command_window.index]
    when :usable
      show_item_windows(@usable_window)
    when :battle
      show_item_windows(@battle_window)
    when :weapons
      show_item_windows(@weapon_window)
    when :armours
      show_item_windows(@armour_window)
    when :misc
      show_item_windows(@misc_window)
    when :key_items
      show_item_windows(@key_item_window)
    when :all_items
      show_item_windows(@all_item_window)
    end
  end
  
  #--------------------------------------------------------------------------
  # overwrite method: terminate
  #--------------------------------------------------------------------------
  def terminate
    super
    dispose_menu_background
    @help_window.dispose
    @command_window.dispose
    @status_window.dispose
    for window in @windows
      next if window == nil
      next if window.disposed?
      window.dispose
    end
    @viewport.dispose
  end
  
  #--------------------------------------------------------------------------
  # overwrite method: update
  #--------------------------------------------------------------------------
  def update
    super
    update_menu_background
    if @command_window.active
      update_command_selection
    elsif @target_window.active
      update_target_selection
    elsif @item_window != nil and @item_window.active
      update_item_selection
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: update_command_selection
  #--------------------------------------------------------------------------
  def update_command_selection
    @command_window.update
    update_windows if @last_command_index != @command_window.index
    if Input.trigger?(Input::B)
      Sound.play_cancel
      return_scene
    elsif Input.trigger?(Input::C)
      Sound.play_decision
      @command_window.active = false
      case @data[@command_window.index]
      when :usable, :battle, :weapons, :armours, :misc, :key_items, :all_items
        @item_window.active = true
      end
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: show_usable_windows
  #--------------------------------------------------------------------------
  def show_item_windows(window)
    @help_window.y = 128
    @data_window.set_item_window(window)
    @status_window.set_item_window(window)
    @data_window.y = @help_window.y + @help_window.height
    window.y = @data_window.y
    window.update_help
    @item_window = window
  end
  
  #--------------------------------------------------------------------------
  # overwrite method: update_item_selection
  #--------------------------------------------------------------------------
  def update_item_selection
    @status_window.update
    @item_window.update
    @data_window.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      @item_window.active = false
      @command_window.active = true
      return unless @item_used
      for window in @windows
        next unless window.is_a?(Window_ItemList)
        window.used_item_refresh(@item_used)
      end
      @item_used = []
    elsif Input.trigger?(Input::C)
      @item = @item_window.item
      if enable_item?
        Sound.play_decision
        determine_item
      end
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: enable_item?
  #--------------------------------------------------------------------------
  def enable_item?
    return false unless @item.is_a?(RPG::Item)
    return false unless @item.menu_ok?
    return false unless @item_window.enable?(@item)
    return true
  end
  
  #--------------------------------------------------------------------------
  # alias method: update_target_selection
  #--------------------------------------------------------------------------
  alias update_target_selection_io update_target_selection unless $@
  def update_target_selection
    @target_window.update
    update_target_selection_io
  end
  
  #--------------------------------------------------------------------------
  # alias method: show_target_window
  #--------------------------------------------------------------------------
  alias show_target_window_io show_target_window unless $@
  def show_target_window(right)
    show_target_window_io(true)
  end
  
  #--------------------------------------------------------------------------
  # alias method: use_item_nontarget
  #--------------------------------------------------------------------------
  alias use_item_nontarget_io use_item_nontarget unless $@
  def use_item_nontarget
    @item_used = [] if @item_used == nil
    @item_used.push(@item) unless @item_used.include?(@item)
    use_item_nontarget_io
    @status_window.refresh
  end
  
end # Scene_Base

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