class CapsuleSkill 
  attr_accessor :id  
  attr_accessor :containedSkill 
  
  

  def initialize(index)
    @id = index
    @containedSkill = $data_skills[index]
  end  
  
  def volume_base
    return $magicCapV[@id] if $magicCapV[@id]
    return 0
  end   
  # セットするのに必要なカプセルコンテナレベル  
  def settable_Lv
    return $magicCapSettableLv[@id] if $magicCapSettableLv[@id]
    return 0
  end  
  
  # 回復しかた
  def recoverMode
    return $magicCapRecoverMode[@id] if $magicCapRecoverMode[@id]
    return []
  end    
  
  # 回復速度
  def recoverValue
    return $magicCapRecoverValue[@id] if $magicCapRecoverValue[@id]
    return 0
  end   
  
#~   def containedSkill
#~     return $data_skills[@id] 
#~     
#~   end  
end  

class CapsuleContainer 
  attr_accessor :id 
  attr_accessor :containerLv  
  attr_accessor :containSkill
  attr_accessor :containerMP
  attr_accessor :recoverValue  
  attr_accessor :recoverFlag
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------

  
  def initialize(index,skill_id,containerLv )
    @id = index
    @containerLv  = containerLv
    @containerMP = 10
    @containSkill = CapsuleSkill.new(skill_id)
    @recoverValue = 0
    @recoverFlag = true
  end
  
  def containable?(index)
    if stype_id == $CAPCONST 
      true
    else  
      false
    end
  end  
  
  def totalVolume
    return @containSkill.volume_base + getVolumeByCLv
    
  end  
  #コンテナレベルによるvolumeの増分
  def getVolumeByCLv(cLv = @containerLv)
    return cLv * 12
  end  
  
  #コンテナレベルによるvolumeの増加の差分
  def defferenceVolumeByCLv(cLv = @containerLv)
    return getVolumeByCLv(cLv + 1) - getVolumeByCLv(cLv)
  end
  


  def recoverCapOnMap
    
    if @containerMP >= $CapMaxMp
      @recoverValue = 0 
    elsif $recoverValueMax <= @recoverValue 
      @recoverValue = 0  
      @containerMP += 1 if @recoverFlag
    else
      @recoverValue += @containSkill.recoverValue
    end        
    
  end   
  
 
end 



class Game_Actor < Game_Battler

  #カプセルの回復　updateで自動回復
  def recoverCapOnMap
    return unless @mpCapContainers

    (@mpCapContainers.select {|t| t != nil && t.containSkill.recoverMode.include?(1)}).each do |i|
      i.recoverCapOnMap
    end  
  end
  #カプセルの回復　歩き
  def recoverCapOnMapByWalk
    return unless @mpCapContainers
    (@mpCapContainers.select {|t| t != nil && t.containSkill.recoverMode.include?(2)}).each do |i|
      i.recoverCapOnMap

    end  
  end  
end

class Game_Battler < Game_BattlerBase
  
  #--------------------------------------------------------------------------
  # ● スキル／アイテムの使用
  #    行動側に対して呼び出され、使用対象以外に対する効果を適用する。
  #--------------------------------------------------------------------------
  def use_skillCap(item )
      pay_skillCap_cost(item)
      skill= item.containSkill.containedSkill
      skill.effects.each {|effect| item_global_effect_apply(effect) }
  end  
end 

class Game_BattlerBase
  
  attr_accessor :mpCapContainers
  attr_accessor :mpContainerMaxVolume
  attr_accessor :mpContainerNumLimit
  attr_accessor :containerLvLimit # 負の数の場合は限界無し
  alias tako1initialize initialize
  def initialize
    tako1initialize
    @mpCapContainers = []
    @mpContainerMaxVolume = 0
  end   
  #--------------------------------------------------------------------------
  # ● カプセルコンテナのセット
  #--------------------------------------------------------------------------  
  def setCapsule(index ,skill_id ,containerLv)
    newCapsule = CapsuleContainer.new(index , skill_id,containerLv)
    @mpCapContainers[index] = newCapsule
    
  end

  #--------------------------------------------------------------------------
  # ● カプセルコンテナのセット
  #--------------------------------------------------------------------------  
  def setCapsuleMP0(index ,skill_id ,containerLv)
    newCapsule = CapsuleContainer.new(index , skill_id,containerLv)
    @mpCapContainers[index] = newCapsule
    @mpCapContainers[index].containerMP = 0
  end    

  #--------------------------------------------------------------------------
  # ● カプセルコンテナの消去
  #--------------------------------------------------------------------------  
  def deleteCapsule(index)
    
    @mpCapContainers[index] = nil
    
  end  
  
  #--------------------------------------------------------------------------
  # ● カプセルコンテナの総容量
  #--------------------------------------------------------------------------  
  def totalVolumeOfContainer(containers)
    sum = 0
    containers.each do |i|
      sum += i.totalVolume if i
      
    end  
    return sum
  end  
  
  #--------------------------------------------------------------------------
  # ● カプセルコンテナの総容量
  #--------------------------------------------------------------------------  
  def volumeOfContainerRate
    
    return 1.0 * totalVolumeOfContainer(containers) / @mpContainerMaxVolume
  end    
  
  #--------------------------------------------------------------------------
  # ● カプセルコンテナの残量
  #--------------------------------------------------------------------------  
  def restVolumeOfContainer(containers = mpCapContainers)
    return @mpContainerMaxVolume - totalVolumeOfContainer(containers)
  end      
  
  #--------------------------------------------------------------------------
  # ● カプセルコンテナの総容量
  #--------------------------------------------------------------------------  
  def restvolumeOfContainerRate
    
    return 1.0 * restVolumeOfContainer / @mpContainerMaxVolume
  end    
    
  #--------------------------------------------------------------------------
  # ● スキル使用コストの支払い
  #--------------------------------------------------------------------------
  
  
  def pay_skillCap_cost(skillC)
    @mpCapContainers[skillC.id].containerMP -= skillC.containSkill.containedSkill.mp_cost 
    @mpCapContainers[skillC.id].recoverValue = 0

  end  
  
  #--------------------------------------------------------------------------
  # ● スキルの使用可能条件チェック
  #--------------------------------------------------------------------------
  def skillCap_conditions_met?(skillC)
    capUsableMp?(skillC) && skill_conditions_met?(skillC.containSkill.containedSkill)
      
  end
  
  #--------------------------------------------------------------------------
  # ● 全回復
  #--------------------------------------------------------------------------
  alias tako1recover_all recover_all
  def recover_all
    tako1recover_all
    @mpCapContainers.each do |i|
      i.containerMP = $CapMaxMp
      i.recoverValue = 0
    end  
  end  
  
  #部分回復
  def recoverCap(index)
    @mpCapContainers[index].containerMP = $CapMaxMp
    
  end  
  
  

  def capUsableMp?(skillC)
    return skillC.containerMP >= skillC.containSkill.containedSkill.mp_cost
  end  
  
  def capUsable?(index)
    skillCap = self.mpCapContainers[index]
    return false if skillCap.containSkill.id == 0
    return skillCap_conditions_met?(skillCap)
  end 

  #カプセルをセットすることによる増分
  def defferenceByCapSet(index ,skill_id)
    containerLv = @mpCapContainers[index] ? @mpCapContainers[index].containerLv : 0
    newCapsule = CapsuleContainer.new(index , skill_id,containerLv)
    return  totalVolumeOfContainer(@mpCapContainers.dup.insert(index , newCapsule)) - totalVolumeOfContainer(@mpCapContainers)
  end  
  

  
  def capSettable?(index , skill_id)
    containerLv = @mpCapContainers[index] ? @mpCapContainers[index].containerLv : 0
    newCapsule = CapsuleContainer.new(index , skill_id,containerLv)
     
    t =  (newCapsule.containSkill.settable_Lv <= containerLv) 
    return (totalVolumeOfContainer(@mpCapContainers.dup.insert(index , newCapsule)) <= @mpContainerMaxVolume) && t
  end   
  
  def capLevelUppable?(index )
    t = (@mpCapContainers[index].containerLv <= @containerLvLimit) || (@containerLvLimit < 0)
    t &= totalVolumeOfContainer(@mpCapContainers) + @mpCapContainers[index].defferenceVolumeByCLv <= @mpContainerMaxVolume 
    return t
  end 
end
module DataManager

  #--------------------------------------------------------------------------
  # ● 追加データベースをロード
  #--------------------------------------------------------------------------
  def self.load_database2
    setMagicCap
    setMagicCapSettableLv
    setMagicCapRecoverMode
    setMagicCapRecoverValue
    $data_magicCap = []
    $data_skills.each do |i|
      $data_magicCap[i.id] = CapsuleSkill.new(i.id) if i
    end
    $CAPCONST = 2
    $CapMaxMp = 10
    $recoverValueMax = 24000
    
  end
  

  
  def self.setMagicCap
    $magicCapV = [0] 
    $magicCapV[26] = 10
    $magicCapV[51] = 10
    $magicCapV[53] = 25
    $magicCapV[55] = 10
    
  end  

  def self.setMagicCapSettableLv
    $magicCapSettableLv = [0] 
    $magicCapSettableLv[26] = 0
    $magicCapSettableLv[51] = 0
    $magicCapSettableLv[53] = 2
    $magicCapSettableLv[55] = 0
  end   

  def self.setMagicCapRecoverMode
    $magicCapRecoverMode = [[0]] 
    $magicCapRecoverMode[26] = [1]
    $magicCapRecoverMode[51] = [1]
    $magicCapRecoverMode[53] = [1]
    $magicCapRecoverMode[55] = [1]
  end    
  def self.setMagicCapRecoverValue
    $magicCapRecoverValue = [0] 
    $magicCapRecoverValue[26] = 400
    $magicCapRecoverValue[51] = 200
    $magicCapRecoverValue[53] = 120
    $magicCapRecoverValue[55] = 200
  end    
end  
  
class Window_SkillList< Window_Selectable
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  alias tako1initialize initialize
  def initialize(x, y, width, height)
    tako1initialize(x, y, width, height)
    @dataCap = []
  end  
  
  #--------------------------------------------------------------------------
  # ● 項目数の取得
  #--------------------------------------------------------------------------
  alias tako1item_max item_max
  def item_max
    return tako1item_max unless @stype_id == $CAPCONST 
    return @dataCap ? @dataCap.size : 1 
  end
  #--------------------------------------------------------------------------
  # ● スキルの取得
  #--------------------------------------------------------------------------
  alias tako1item item
  def item
    return tako1item unless @stype_id == $CAPCONST 
    return (@dataCap[index] && index >= 0) ? @dataCap[index] : nil
  end
  #--------------------------------------------------------------------------
  # ● 選択項目の有効状態を取得
  #--------------------------------------------------------------------------
  alias tako1current_item_enabled? current_item_enabled?
  def current_item_enabled?

    return tako1current_item_enabled? unless @stype_id == $CAPCONST

    return @dataCap[index] && @dataCap[index].containSkill && enableCap?(index)

  end


  #--------------------------------------------------------------------------
  # ● スキルを許可状態で表示するかどうか
  #--------------------------------------------------------------------------

  def enableCap?(index )

    return @actor.capUsable?(index)

  end 
  
  
  #--------------------------------------------------------------------------
  # ● スキルリストの作成
  #--------------------------------------------------------------------------
  def make_itemCap_list
    @dataCap = @actor ? @actor.mpCapContainers : []
  end
  
  #--------------------------------------------------------------------------
  # ● 項目の描画
  #--------------------------------------------------------------------------
  alias tako1draw_item draw_item
  def draw_item(index)
    tako1draw_item(index) unless @stype_id == $CAPCONST
    draw_item_Cap(index) if @stype_id == $CAPCONST
    
  end  
  
  #--------------------------------------------------------------------------
  # ● 項目の描画
  #--------------------------------------------------------------------------
  def draw_item_Cap(index)
    skillCap = @dataCap[index] ? @dataCap[index].containSkill : nil 
    if skillCap
      skill = skillCap.containedSkill
      rect = item_rect(index)
      rect.width -= 4
      draw_item_name(skill, rect.x, rect.y, enableCap?(index ))
 
      draw_text(rect, @dataCap[index].containerLv , 2)
      rect.width *= 0.9
      draw_text(rect, @dataCap[index].containerMP , 2)
    end
  end  
  #--------------------------------------------------------------------------
  # ● ヘルプテキスト更新
  #--------------------------------------------------------------------------
  alias tako1update_help update_help
  def update_help
    if @stype_id == $CAPCONST 
      @help_window.set_item(item ? item.containSkill.containedSkill : nil) 
    else
      tako1update_help 
    end  
    
  end
  #--------------------------------------------------------------------------
  # ● リフレッシュ
  #--------------------------------------------------------------------------
  alias tako1refresh refresh
  def refresh
    make_itemCap_list
    tako1refresh 
  end  
end   


class Window_SkillCommand < Window_Command
  def stype_id
    return current_ext
  end    
end  

class Window_SkillCapSet < Window_SkillList
  attr_accessor :skill_id 
  attr_accessor :settingMode 
  attr_accessor :capSt_window   
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height , skill_id)
    super(x, y, width, height)
    @skill_id
  end  
  
  def current_item_enabled?

    return true

  end  
  #--------------------------------------------------------------------------
  # ● スキルを許可状態で表示するかどうか
  #--------------------------------------------------------------------------

  def enableCap?(index )
    return false if @actor.mpCapContainers[index].containSkill.id == 0
    return true

  end  
  
  #--------------------------------------------------------------------------
  # ● 項目数の取得
  #--------------------------------------------------------------------------
  def item_max
    @actor ? @actor.mpContainerNumLimit : 0# @data ? (@data.size) : 1
  end  
  
end  


class Scene_SkillSet < Scene_Skill
  def initialize(skill_id , id , mode_id , idpat = 0)
    if idpat == 0

      @actor2 =$game_party.menu_actor      
    elsif idpat == 1
      @actor2 = $data_actors[id]      
    else
      @actor2 = $game_party.members[id] 
    end  
    @skill_id = skill_id
    @settingMode = mode_id  
  end    
  #--------------------------------------------------------------------------
  # ● 開始処理
  #--------------------------------------------------------------------------
  def start
    super
    @actor = @actor2
    create_help_window
    create_capSt_window
    create_command_window
    create_status_window
    create_item_window
  end  
  
  #--------------------------------------------------------------------------
  # ● コマンドウィンドウの作成
  #--------------------------------------------------------------------------
  def create_command_window

  end 
     
  #--------------------------------------------------------------------------
  # ● カプセルコンテナウィンドウの作成
  #--------------------------------------------------------------------------
  def create_capSt_window
    y = @help_window.height
    ww = Graphics.width - @status_window.width
    wh = @status_window.height
    @capSt_window = Window_CapStatus.new(@status_window.width, y , ww,wh)
    @capSt_window.viewport = @viewport
    @capSt_window.actor = @actor
    @capSt_window.skillCap = $data_magicCap[@skill_id]
    @capSt_window.settingMode = @settingMode   
    @capSt_window.refresh     

  end   
  
  #--------------------------------------------------------------------------
  # ● ステータスウィンドウの作成
  #--------------------------------------------------------------------------
  def create_status_window
    y = @help_window.height
    @status_window = Window_SkillStatus.new(@help_window.x, y)
    @status_window.viewport = @viewport
    @status_window.actor = @actor
  end

  #--------------------------------------------------------------------------
  # ● アイテムウィンドウの作成
  #--------------------------------------------------------------------------
  def create_item_window
    wx = 0
    wy = @status_window.y + @status_window.height
    ww = Graphics.width
    wh = Graphics.height - wy
    @item_window = Window_SkillCapSet.new(wx, wy, ww, wh,@skill_id)
    @item_window.actor = @actor
    @item_window.viewport = @viewport
    @item_window.help_window = @help_window
    @item_window.capSt_window = @capSt_window
    @item_window.set_handler(:ok,     method(:on_item_ok))
    @item_window.set_handler(:cancel, method(:on_item_cancel))
    @item_window.set_handler(:pagedown, method(:next_actor))
    @item_window.set_handler(:pageup,   method(:prev_actor))
    @item_window.stype_id = $CAPCONST
    @item_window.index = 0
    @item_window.activate

  end  
  #--------------------------------------------------------------------------
  # ● アイテム［決定］
  #--------------------------------------------------------------------------
  def on_item_ok
    if item_usable?
      on_item_ok_in_item_usable
    else
      Sound.play_buzzer
    end
    activate_item_window
  end  
  
  def on_item_ok_in_item_usable    
    setCap(@item_window.index) if @settingMode >= 0
    deleteCap(@item_window.index) if @settingMode < 0  
  end 
  
  def setCap(index)
    @actor.last_skill.object = item
    containerLv = @actor.mpCapContainers[index] ? @actor.mpCapContainers[index].containerLv : 0
    @actor.setCapsuleMP0(@item_window.index ,@skill_id , containerLv)
    activate_item_window    
    @capSt_window.refresh
  end  
  
  def deleteCap(index)
    @actor.last_skill.object = item
    @actor.deleteCapsule(@item_window.index)
    activate_item_window    
    @capSt_window.refresh
  end     
  
  #--------------------------------------------------------------------------
  # ● アイテムの使用可能判定
  #--------------------------------------------------------------------------
  def item_usable?
    user.capSettable?(@item_window.index , @skill_id)
  end  

  #--------------------------------------------------------------------------
  # ● アイテム［キャンセル］
  #--------------------------------------------------------------------------
  def on_item_cancel

    return_scene

  end  
  #--------------------------------------------------------------------------
  # ● アクターの切り替え
  #--------------------------------------------------------------------------
  def on_actor_change

    @status_window.actor = @actor
    @item_window.actor = @actor
    @item_window.activate

    @capSt_window.actor = @actor
  end 
end  


class Window_SkillCapSet2 < Window_SkillList
  attr_accessor :skill_id 
  attr_accessor :settingMode
  attr_accessor :capSt_window    
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height , skill_id)
    super(x, y, width, height)
    @skill_id
  end  
  
  def current_item_enabled?

    return true

  end 

  #--------------------------------------------------------------------------
  # ● スキルを許可状態で表示するかどうか
  #--------------------------------------------------------------------------

  def enableCap?(index )
    return false if @actor.mpCapContainers[index].containSkill.id == 0
    return true

  end  
  
  #--------------------------------------------------------------------------
  # ● カーソルの移動処理
  #--------------------------------------------------------------------------
  def process_cursor_move
    return unless cursor_movable?
    last_index = @index
    cursor_down (Input.trigger?(:DOWN))  if Input.repeat?(:DOWN)
    cursor_up   (Input.trigger?(:UP))    if Input.repeat?(:UP)
    capLvup     (Input.trigger?(:RIGHT)) if Input.repeat?(:RIGHT) &&  [1,2,3,4].include?(@settingMode)
    capLvdown   (Input.trigger?(:LEFT))  if Input.repeat?(:LEFT) && [1,2].include?(@settingMode)
    cursor_pagedown   if !handle?(:pagedown) && Input.trigger?(:R)
    cursor_pageup     if !handle?(:pageup)   && Input.trigger?(:L)
    Sound.play_cursor if @index != last_index
  end  
  
  #--------------------------------------------------------------------------
  # ● 桁数の取得
  #--------------------------------------------------------------------------
  def col_max
    return 1
  end  
  
  #--------------------------------------------------------------------------
  # ● カーソルを下に移動
  #--------------------------------------------------------------------------
  def cursor_down(wrap = false)
    if index < item_max - col_max 
      select((index + col_max) % item_max)
    end
  end
  #--------------------------------------------------------------------------
  # ● カーソルを上に移動
  #--------------------------------------------------------------------------
  def cursor_up(wrap = false)
    if index >= col_max 
      select((index - col_max + item_max) % item_max)
    end
  end  
  
  #--------------------------------------------------------------------------
  # ● 項目数の取得
  #--------------------------------------------------------------------------
  def item_max
    @actor ? @actor.mpContainerNumLimit : 0
  end  
  
  #カプセルのレベルアップ
  def capLvup(t)
    if t && @actor.mpCapContainers[@index]
      if @actor.capLevelUppable?(index )
        @actor.mpCapContainers[@index].containerLv += 1
        refresh
        @capSt_window.refresh
      end
    end  
    
  end
  #カプセルのレベルダウン  
  def capLvdown(t)

    if t && @actor.mpCapContainers[@index]
      if @actor.mpCapContainers[@index].containerLv > 0
        @actor.mpCapContainers[@index].containerLv -= 1
        refresh
        @capSt_window.refresh
      end
    end  
  end  
end

class Scene_SkillSet2 < Scene_Skill
  
    

  def initialize(skill_id , id ,mode_id , idpat = 0)
    if idpat == 0
      
      @actor2 =$game_party.menu_actor      
    elsif idpat == 1
      @actor2 = $data_actors[id]      
    else
      @actor2 = $game_party.members[id] 
    end  
    @skill_id = skill_id
    @settingMode = mode_id 
  end  
  
  
  #settingMode    0:魔法のセット,  , 
  #               1:コンテナレベルアップ
  #               2:魔法とコンテナレベルアップ 
  #               3:コンテナレベルアップ（上昇のみ）
  #               4:魔法とコンテナレベルアップ（上昇のみ）    

    
  #--------------------------------------------------------------------------
  # ● 開始処理
  #--------------------------------------------------------------------------
  def start
    super
    @actor = @actor2
    create_help_window
    create_capSt_window    
    create_command_window
    create_status_window
    create_item_window


  end
  
  #--------------------------------------------------------------------------
  # ● コマンドウィンドウの作成
  #--------------------------------------------------------------------------
  def create_command_window

  end  
  
     
  #--------------------------------------------------------------------------
  # ● カプセルコンテナウィンドウの作成
  #--------------------------------------------------------------------------
  def create_capSt_window
    y = @help_window.height
    ww = Graphics.width - @status_window.width
    wh = @status_window.height
    @capSt_window = Window_CapStatus.new(@status_window.width, y , ww,wh)
    @capSt_window.viewport = @viewport
    @capSt_window.actor = @actor
    @capSt_window.skillCap = $data_magicCap[@skill_id]
    @capSt_window.settingMode = @settingMode
    @capSt_window.refresh 
  end 
  

  #--------------------------------------------------------------------------
  # ● ステータスウィンドウの作成
  #--------------------------------------------------------------------------
  def create_status_window
    y = @help_window.height
    @status_window = Window_SkillStatus.new(@help_window.x, y)
    @status_window.viewport = @viewport
    @status_window.actor = @actor
    
  end

  #--------------------------------------------------------------------------
  # ● アイテムウィンドウの作成
  #--------------------------------------------------------------------------
  def create_item_window
    wx = 0
    wy = @status_window.y + @status_window.height
    ww = Graphics.width
    wh = Graphics.height - wy
    @item_window = Window_SkillCapSet2.new(wx, wy, ww, wh,@skill_id)
    @item_window.actor = @actor
    @item_window.viewport = @viewport
    @item_window.help_window = @help_window
    @item_window.capSt_window = @capSt_window
    @item_window.set_handler(:ok,     method(:on_item_ok))
    @item_window.set_handler(:cancel, method(:on_item_cancel))
    @item_window.set_handler(:pagedown, method(:next_actor))
    @item_window.set_handler(:pageup,   method(:prev_actor))
    
    @item_window.stype_id = $CAPCONST
    @item_window.index = 0
    @item_window.activate
    @item_window.settingMode = @settingMode



  end  
  #--------------------------------------------------------------------------
  # ● アイテム［決定］
  #--------------------------------------------------------------------------
  def on_item_ok
    if item_usable?
      on_item_ok_in_item_usable
    else
      Sound.play_buzzer
    end
    activate_item_window
    
  end  
  
  def on_item_ok_in_item_usable    
    setCap(@item_window.index) if [0,2,4].include?(@settingMode) 
    deleteCap(@item_window.index) if @settingMode < 0  
  end  


  
  def setCap(index)
    @actor.last_skill.object = item
    containerLv = @actor.mpCapContainers[index] ? @actor.mpCapContainers[index].containerLv : 0
    @actor.setCapsuleMP0(@item_window.index ,@skill_id , containerLv)
    activate_item_window    
    @capSt_window.refresh
  end  
  
  def deleteCap(index)
    @actor.last_skill.object = item
    @actor.deleteCapsule(@item_window.index)
    activate_item_window    
    @capSt_window.refresh
  end   
  #--------------------------------------------------------------------------
  # ● アイテムの使用可能判定
  #--------------------------------------------------------------------------
  def item_usable?
    user.capSettable?(@item_window.index , @skill_id)
  end  

  #--------------------------------------------------------------------------
  # ● アイテム［キャンセル］
  #--------------------------------------------------------------------------
  def on_item_cancel

    return_scene

  end  
  #--------------------------------------------------------------------------
  # ● アクターの切り替え
  #--------------------------------------------------------------------------
  def on_actor_change

    @status_window.actor = @actor
    @item_window.actor = @actor
    @item_window.activate

    @capSt_window.actor = @actor

  end  

end    

class Window_CapStatus < Window_Selectable
  attr_accessor :skillCap
  attr_accessor :settingMode
  #-----------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(x, y,window_width, window_height)
    super(x, y, window_width, window_height)
    @pending_index = -1
    @settingMode = 0
    refresh
  end  
  
  #--------------------------------------------------------------------------
  # ● アクターの設定
  #--------------------------------------------------------------------------
  def actor=(actor)
    return if @actor == actor
    @actor = actor
    refresh
    self.oy = 0
  end  
  #--------------------------------------------------------------------------
  # ● 項目の描画
  #--------------------------------------------------------------------------
  def draw_ctxt    
    rect = captxt_rect(0)
    rect.width -= 4
    txt_captotal(rect)
  end

  
  #魔法容量を表示するテキスト
  def txt_captotal(rect)
    x = rect.x
    y = rect.y 
    width = rect.width
    if @actor 
      change_color(system_color)
      draw_text(x, y, 100, line_height, Vocab::MagicCapContainerVolumeRest)
      y += line_height
      n = @actor.restVolumeOfContainer
      draw_gauge(x + 10, y, width, @actor.restvolumeOfContainerRate, mpCapGuageCol(n) , mpCapGuageCol(n))
      draw_current_and_max_values(x, y,width , @actor.restVolumeOfContainer, @actor.mpContainerMaxVolume,
        normal_color, normal_color)  
      y += line_height  
      change_color(system_color)
      if @settingMode < 0
        draw_text(x, y, 100, line_height , "削除") 
      elsif @skillCap
        draw_text(x, y, 100, line_height, @skillCap.containedSkill.name)
        y += line_height - 2
        
        txt_volumeCap(x,y)
      else
        draw_text(x, y, 100, line_height , "Empty") 
        
      end
    end
  end 
  
  def txt_volumeCap(x,y)
    change_color(system_color)
    draw_text(x, y, 20, line_height, Vocab::MagicCapStatusVolume)
    change_color(normal_color)
    draw_text(x + 20, y, 40, line_height, @skillCap.volume_base)
    
    change_color(system_color)
    draw_text(x + 65, y, 20, line_height, Vocab::MagicCapStatusLv )
    change_color(normal_color)
    draw_text(x + 85, y, 40, line_height, @skillCap.settable_Lv)       
  end  
  
  def mpCapGuageCol(n)
    text_color(2)
  end  
    
  #--------------------------------------------------------------------------
  # ● 項目を描画する矩形の取得
  #--------------------------------------------------------------------------
  def captxt_rect(index)
    rect = Rect.new
    rect.width = 110
    rect.height = line_height
    rect.x = 8
    rect.y = index * line_height
    rect
  end
  #--------------------------------------------------------------------------
  # ● 横に項目が並ぶときの空白の幅を取得
  #--------------------------------------------------------------------------
  def spacing
    return 32
  end  
  #--------------------------------------------------------------------------
  # ● リフレッシュ
  #--------------------------------------------------------------------------
  def refresh
    super   
    draw_ctxt
  end   
end  

class Scene_ItemBase < Scene_MenuBase

  #--------------------------------------------------------------------------
  # ● アイテムの使用可能判定
  #--------------------------------------------------------------------------
  def item_usable?
    if item.is_a?(RPG::Skill) || item.is_a?(RPG::Item)
      user.usable?(item) && item_effects_valid?
    else
      
      user.capUsable?(item.id) && item_effects_valid?
    end  
    
  end  
  
  #--------------------------------------------------------------------------
  # ● アイテムの使用
  #--------------------------------------------------------------------------
  alias tako1use_item use_item
  def use_item
    if item.is_a?(RPG::Skill) || item.is_a?(RPG::Item)
      tako1use_item
    else        
      play_se_for_item
      user.use_skillCap(item )
      use_item_to_actors
      check_common_event
      check_gameover
      @actor_window.refresh    
    end  
  end  
end  

class Scene_Skill < Scene_ItemBase

  #--------------------------------------------------------------------------
  # ● アイテム［決定］
  #--------------------------------------------------------------------------
  alias tako1on_item_ok on_item_ok
  def on_item_ok
    if item.is_a?(RPG::Skill) || item.is_a?(RPG::Item)
      tako1on_item_ok
    else
      @actor.last_skill.object = item
      determine_itemCap
    end  
  end

  #--------------------------------------------------------------------------
  # ● アイテムの効果が有効かを判定
  #--------------------------------------------------------------------------
  alias tako1item_effects_valid? item_effects_valid?
  def item_effects_valid?
    
    if item.is_a?(RPG::Skill) || item.is_a?(RPG::Item)
      tako1item_effects_valid?
    else
      skill=item.containSkill.containedSkill
      item_target_actors.any? do |target|
        target.item_test(user, skill)
      end  
      
    end
  end
  #--------------------------------------------------------------------------
  # ● アイテムをアクターに対して使用
  #--------------------------------------------------------------------------
  alias tako1use_item_to_actors use_item_to_actors
  def use_item_to_actors
    if item.is_a?(RPG::Skill) || item.is_a?(RPG::Item)
      tako1use_item_to_actors
    else
      skill=item.containSkill.containedSkill
      item_target_actors.each do |target|
        skill.repeats.times { target.item_apply(user, skill) }
      end      
    end  
  end 
  
  #--------------------------------------------------------------------------
  # ● アイテムの使用対象となるアクターを配列で取得
  #--------------------------------------------------------------------------
  alias tako1item_target_actors item_target_actors
  def item_target_actors
    if item.is_a?(RPG::Skill) || item.is_a?(RPG::Item)
      tako1item_target_actors
    else
      skill=item.containSkill.containedSkill
      if !skill.for_friend?
        []
      elsif skill.for_all?
        $game_party.members
      else
        [$game_party.members[@actor_window.index]]
      end
    end  
  end    
  
  #--------------------------------------------------------------------------
  # ● アイテムの決定
  #--------------------------------------------------------------------------

  def determine_itemCap
    skill = item.containSkill.containedSkill
    if skill.for_friend?
      show_sub_window(@actor_window)
      @actor_window.select_for_item(skill)
    else
      use_item
      activate_item_window
    end
  end    
end  

class Scene_Battle < Scene_Base
  #--------------------------------------------------------------------------
  # ● スキル［決定］
  #--------------------------------------------------------------------------
  alias tako1on_skill_ok  on_skill_ok 
  def on_skill_ok
    
    @skill = @skill_window.item

    if @skill.is_a?(RPG::Skill) || @skill.is_a?(RPG::Item)
      tako1on_skill_ok
    else  
      
      @cskill = @skill.containSkill.containedSkill
      BattleManager.actor.input.skillCap = @skill
      BattleManager.actor.input.set_skill(@cskill.id)
      BattleManager.actor.last_skill.object = @skill
      if !@cskill.need_selection?
        @skill_window.hide
        next_command
      elsif @cskill.for_opponent?
        select_enemy_selection
      else
        select_actor_selection
      end
    
    end
  end  
  #--------------------------------------------------------------------------
  # ● スキル／アイテムの使用
  #--------------------------------------------------------------------------
  alias tako1use_item use_item
  def use_item
    item = @subject.current_action.item
    unless  item.is_a?(RPG::Skill) && item.stype_id == $CAPCONST
      tako1use_item     
    else
      sCap = @subject.current_action.skillCap

      @log_window.display_use_item(@subject, item)
      @subject.use_skillCap(sCap)
      refresh_status
      targets = @subject.current_action.make_targets.compact
      show_animation(targets, item.animation_id)
      targets.each {|target| item.repeats.times { invoke_item(target, item) } }
    end     
  end  
end    

class Game_Action
  attr_accessor :skillCap  
  #--------------------------------------------------------------------------
  # ● クリア
  #--------------------------------------------------------------------------
  alias takoclear clear
  def clear
    takoclear
    skillCap = nil
  end
  
  
  #--------------------------------------------------------------------------
  # ● スキルを設定
  #--------------------------------------------------------------------------
  alias tako1valid? valid?
  def valid?
    if item.is_a?(RPG::Skill) 
      tako1valid?
    else
      (forcing && skillCap) || subject.capUsable?(skillCap.id)
    end
  end
  
  #--------------------------------------------------------------------------
  # ● 敵キャラの戦闘行動を設定
  #     action : RPG::Enemy::Action
  #--------------------------------------------------------------------------
  alias tako1set_enemy_action set_enemy_action
  def set_enemy_action(action)
    tako1set_enemy_action(action)
    if $data_skills[action.skill_id].stype_id == $CAPCONST
      capid = subject.getEnableCapInContainer(action.skill_id) 
      @skillCap = subject.mpCapContainers[capid]      
    end  
  end 
end  

class Game_Enemy < Game_Battler
  
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  alias tako2initialize initialize
  def initialize(index, enemy_id)
    tako2initialize(index, enemy_id)
    setmagics(enemy_id)
    
  end  
  
  #--------------------------------------------------------------------------
  # ● 現在の状況で戦闘行動が有効か否かを判定
  #     action : RPG::Enemy::Action
  #--------------------------------------------------------------------------
  alias tako1action_valid? action_valid?
  def action_valid?(action)

    if $data_skills[action.skill_id].stype_id == $CAPCONST
      conditions_met?(action) && enableCapInContainer(action.skill_id)#enableCapInContainer(action.skill_id) >= 0
    else  
      tako1action_valid?(action)
      
    end
  end
  
  def enableCapInContainer(skill_id)
    @mpCapContainers.any? {|a| (capUsableMp?(a) && (a.containSkill.containedSkill.id == skill_id))}

  end   
  
  #カプセルコンテナの中ある指定した種類のスキルのカプセルで
  #使えるもののidをランダムに返す
  def getEnableCapInContainer(skill_id)
    mpEnoughCap = @mpCapContainers.select {|a| ((a.containerMP >= 10) && (a.containSkill.containedSkill.id == skill_id))}
    return -1 if mpEnoughCap.empty?
    mcon = mpEnoughCap[rand(mpEnoughCap.size)]
    return mcon.id if mcon
    return -1
  end  
  
  def capUsable?(index)
    skillCap = self.mpCapContainers[index]
    return  skillCap_conditions_met?(skillCap)
  end    

end   

class Game_Player < Game_Character
  
  alias tako1update update
  def update
    tako1update
    $game_party.members.each do |actor|
      actor.recoverCapOnMap
    end  
  end  

  
  #--------------------------------------------------------------------------
  # ● 歩数増加
  #--------------------------------------------------------------------------
  alias tako1increase_steps increase_steps
  def increase_steps    
    tako1increase_steps
    increase_recoverCapOnMap if normal_walk?
  end  
  
  def increase_recoverCapOnMap
    $game_party.members.each do |actor|
      actor.recoverCapOnMapByWalk
    end           
  end  
  
#リカバーフラグをtrueにする
  def all_recoverFlag_on
    $game_party.members.each do |actor|
      actor.all_recoverFlag_on
    end        
  end 
  
#リカバーフラグをfalseにする
  def all_recoverFlag_off
    $game_party.members.each do |actor|
      actor.all_recoverFlag_off
    end       
  end  
end  