#===============================================================================
# Chrono Trigger SkilL Learning
# Author game_guy
# Version 1.0
#-------------------------------------------------------------------------------
# Intro:
# Remember the good old game Chrono Trigger? You already knew skills but you
# couldn't just use them yet. Each skill needed a certain amount of "AP" to
# use. You got AP from enemies. Well this script is pretty much the same.
#
# Features:
# Easily setup AP for Enemies
# Easily setup AP for Skills
# Display amount AP the skill needs
#
# Instructions:
# Go down and look at the config. Follow the instructions. They're pretty 
# simple. If you can't understand it please do ask questions.
#
# Some noteworthy things. Any skill that's not configured will automatically
# be usable. Any enemy that's not configured, its ap will be set to 0.
#
# Credits:
# game_guy ~ for making it
# Chrono Trigger ~ for inspiring me to make this
#===============================================================================
module GameGuy
  #====================================
  # ApWord2 ~ Word used for displaying
  # AP at the end of a battle. 
  #====================================
  ApWord2    = "AP"
  #====================================
  # ShowAp ~ If true, it displays the
  # skills current ap in the skill
  # menu.
  #====================================
  ShowAp     = true
  def self.skillptz(id)
    #==============================================================
    # Config Skill Points
    # This is how many points a skill needs to be usable.
    # when skill_id then return points
    # Example: when 1 then return 5
    # So you need 5 "AP" points until Heal is officially learned.
    #==============================================================
    case id
    when 57 then return 10
    when 58 then return 2
    when 59 then return 6
    when 60 then return 8
    end
    return 0
  end
  def self.enemyptz(id)
    #=============================================================
    # Config Enemy Points
    # This is how many points enemies are worth.
    # when enemy_id then return points
    # Example: when 1 then return 1
    # So when ghost is killed you would get 1 ap point.
    #=============================================================
    case id
    when 1 then return 1
    end 
    return 0
  end
end
class Game_System
  attr_accessor :pointsa
  alias gg_ct_skills_lat initialize
  def initialize
    @pointsa = []
    return gg_ct_skills_lat
  end
end
class Game_Actor
  alias gg_init_parray_lat setup
  def setup(actor_id)
    $game_system.pointsa[actor_id] = []
    return gg_init_parray_lat(actor_id)
  end
  def learn_skill(skill_id)
    if skill_id > 0 and not skill_learn?(skill_id)
      @skills.push(skill_id)
      @skills.sort!
      $game_system.pointsa[id][skill_id] = 0
    end
  end
  def forget_skill(skill_id)
    @skills.delete(skill_id)
    $game_system.pointsa[id][skill_id] = nil
  end
  def add_points(skill, n)
    unless @skills.include?(skill)
      return
    end
    if $game_system.pointsa[id][skill] == nil
      $game_system.pointsa[id][skill] = 0
    end
    $game_system.pointsa[id][skill] += n
    if $game_system.pointsa[id][skill] > GameGuy.skillptz(skill)
      $game_system.pointsa[id][skill] = GameGuy.skillptz(skill)
    end
  end
  alias gg_skill_can_use_ct_lat skill_can_use?
  def skill_can_use?(skill_id)
    skill = $game_system.pointsa[id][skill_id]
    if skill >= GameGuy.skillptz(skill_id)
      return super
    else
      return false
    end
    return gg_skill_can_use_ct_lat(skill_id)
  end
end
class Scene_Battle
  def start_phase5
    ap = 0
    for enemy in $game_troop.enemies
      eid = enemy.id
      ap += GameGuy.enemyptz(eid)
      for i in $game_party.actors
        arrayt = $game_system.pointsa[i.id]
        for j in i.skills
          $game_actors[i.id].add_points(j, GameGuy.enemyptz(eid))
        end
      end
    end
    @phase = 5
    $game_system.me_play($game_system.battle_end_me)
    $game_system.bgm_play($game_temp.map_bgm)
    exp = 0
    gold = 0
    treasures = []
    for enemy in $game_troop.enemies
      unless enemy.hidden
        exp += enemy.exp
        gold += enemy.gold
        if rand(100) < enemy.treasure_prob
          if enemy.item_id > 0
            treasures.push($data_items[enemy.item_id])
          end
          if enemy.weapon_id > 0
            treasures.push($data_weapons[enemy.weapon_id])
          end
          if enemy.armor_id > 0
            treasures.push($data_armors[enemy.armor_id])
          end
        end
      end
    end
    treasures = treasures[0..5]
    for i in 0...$game_party.actors.size
      actor = $game_party.actors[i]
      if actor.cant_get_exp? == false
        last_level = actor.level
        actor.exp += exp
        if actor.level > last_level
          @status_window.level_up(i)
        end
      end
    end
    $game_party.gain_gold(gold)
    for item in treasures
      case item
      when RPG::Item
        $game_party.gain_item(item.id, 1)
      when RPG::Weapon
        $game_party.gain_weapon(item.id, 1)
      when RPG::Armor
        $game_party.gain_armor(item.id, 1)
      end
    end
    @result_window = Window_BattleResult.new(exp, gold, treasures, ap)
    @phase5_wait_count = 100
  end
end
class Window_BattleResult < Window_Base
  def initialize(exp, gold, treasures, ap)
    @exp = exp
    @gold = gold
    @treasures = treasures
    @ap = ap
    super(160, 0, 320, @treasures.size * 32 + 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.y = 160 - height / 2
    self.back_opacity = 160
    self.visible = false
    refresh
  end
  def refresh
    self.contents.clear
    x = 4
    self.contents.font.color = normal_color
    cx = contents.text_size(@exp.to_s).width
    self.contents.draw_text(x, 0, cx, 32, @exp.to_s)
    x += cx + 4
    self.contents.font.color = system_color
    cx = contents.text_size("EXP").width
    self.contents.draw_text(x, 0, 64, 32, "EXP")
    x += cx + 16
    self.contents.font.color = normal_color
    cx = contents.text_size(@gold.to_s).width
    self.contents.draw_text(x, 0, cx, 32, @gold.to_s)
    x += cx + 4
    self.contents.font.color = system_color
    self.contents.draw_text(x, 0, 128, 32, $data_system.words.gold)
    x += cx + 16
    self.contents.font.color = normal_color
    cx = contents.text_size(@ap.to_s).width
    self.contents.draw_text(x, 0, cx, 32, @ap.to_s)
    x += cx + 4
    self.contents.font.color = system_color
    self.contents.draw_text(x, 0, 128, 32, GameGuy::ApWord2)
    y = 32
    for item in @treasures
      draw_item_name(item, 4, y)
      y += 32
    end
  end
end
class Window_Skill < Window_Selectable
  def initialize(actor)
    super(0, 128, 640, 352)
    @actor = actor
    @column_max = 1
    refresh
    self.index = 0
    if $game_temp.in_battle
      self.y = 64
      self.height = 256
      self.back_opacity = 160
    end
  end
  def draw_item(index)
    skill = @data[index]
    if @actor.skill_can_use?(skill.id)
      self.contents.font.color = normal_color
    else
      self.contents.font.color = disabled_color
    end
    x = 4 + index % 1 * (288 + 32)
    y = index / 1 * 32
    rect = Rect.new(x, y, self.width / @column_max - 32, 32)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    bitmap = RPG::Cache.icon(skill.icon_name)
    opacity = self.contents.font.color == normal_color ? 255 : 128
    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
    self.contents.draw_text(x + 28, y, 204, 32, skill.name, 0)
    self.contents.draw_text(x + 232, y, 48, 32, skill.sp_cost.to_s, 2)
    if GameGuy::ShowAp
      skillp = $game_system.pointsa[@actor.id][skill.id]
      skillp2 = GameGuy.skillptz(skill.id)
      skillt = skillp.to_s + " / " + skillp2.to_s
      self.contents.draw_text(x, y, 600, 32, skillt, 2)
    end
  end
end