#===============================================================================
# 
# Yanfly Engine Melody - Victory Aftermath
# Last Date Updated: 2010.05.28
# Level: Normal, Hard, Lunatic
# 
# The victory aftermath scene after battles are finished hardly display any
# data at all for the player. All that's generally mentioned is how much EXP
# they've acquired, who levels up, and what items are dropped in only a message
# box window. Now, the whole process is more visual and will contain lists to
# show the player what changes have been made as far as the victory portion is
# concerned. The item drop list will also no longer go on and on forever with
# the message windows, but instead, show them properly in a list window.
# 
#===============================================================================
# Updates
# -----------------------------------------------------------------------------
# o 2010.05.28 - Double EXP "gain" bugfix.
# o 2010.05.22 - Compatibility Update with Custom Message Melody.
# o 2010.05.20 - Converted to Yanfly Engine Melody.
#===============================================================================
# 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.
#===============================================================================

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

module YEM
  module VICTORY
    
    #===========================================================================
    # Section I. General Settings
    # --------------------------------------------------------------------------
    # The following below will adjust the basic settings and vocabulary that
    # will display throughout the script. These settings will also determine
    # which windows you would like to have appear. Change them as you see fit.
    #===========================================================================
    
    # This is how many frames to wait before displaying the victory aftermath
    # scene on the screen.
    WAIT = 60
    
    # The following adjusts the two switches that are associated with this
    # script so bind them accordingly. Bypassing the BGM will cause no BGM
    # change from battle start to end. Skip victory switch will cause the
    # victory display portion to be ignored (but gaining EXP and such will
    # still occur).
    BYPASS_BGM_SWITCH   = 61
    SKIP_VICTORY_SWITCH = 62
    
    # This hash adjusts the vocabulary used throughout the script.
    VOCAB ={
      :top_message => "Battle Results",
      :gold_found  => "Found %s Gold",
      :exp_receive => "Earned %s EXP",
      :percent_exp => "%1.2f%%",
      :next_level  => "To Level %s",
      :max_level   => "Max Level",
      :next_exp    => "%s EXP",
      :level_up    => "LEVEL UP!",
      :to_level    => "to Level %s",
      :level_msg   => "%s has reached level %s!",
      :next_stat   => "→",
      :new_skills  => "Skills Acquired",
      :spoils      => "Victory Spoils",
      :no_items    => "No items were found.",
      :found_items => "%s has found items!",
      :item_amount => "×%2d",
    } # Do not remove this.
    
    # This determines the gauge information for the victory screens.
    FACE_OPACITY  = 200 # This adjusts the opacity of the actor's faces
    EXP_TICKS     = 10  # This is how many ticks it takes to fill the gauge.
    EXP_GAUGE_1   = 28  # Experience gauge colour 1 for non-leveled characters.
    EXP_GAUGE_2   = 29  # Experience gauge colour 2 for non-leveled characters.
    EXP_FONT_SIZE = 18  # Font size used for experience gauge.
    LVL_COLOUR    = 6   # Text colour used for leveling up.
    LVL_GAUGE_1   = 20  # Experience gauge colour 1 for leveled characters.
    LVL_GAUGE_2   = 21  # Experience gauge colour 2 for leveled characters.
    
    # These constants determine which series of windows will appear. By default
    # all of the windows will show up. The EXP windows will appear if there
    # are no drop items present.
    SHOW_EXP_WINDOWS   = true
    SHOW_LEVEL_WINDOWS = true
    SHOW_ITEM_WINDOWS  = true
    
    #===========================================================================
    # Section II. Experience Settings
    # --------------------------------------------------------------------------
    # The following below allows you to adjust the amount of experience each
    # actor will earn and the rules that govern the experience gained.
    #===========================================================================
    
    # If any allies are dead, are they allowed to gain EXP?
    DEAD_ALLY_EXP = false
    
    # Do you want the game to scale the experience based on how many actors are
    # in the party? This means actors can earn more or less.
    PARTY_SPLIT_EXP = false
    PARTY_SIZE_SCALING ={
      1 => 1.000,  # If one actor is active.
      2 => 0.500,  # If two actors are active.
      3 => 0.334,  # If three actors are active.
      4 => 0.250,  # If four actors are active.
      5 => 0.200,  # If five actors are active.
      6 => 0.167,  # If six actors are active.
      7 => 0.143,  # If seven actors are active.
      8 => 0.125,  # If eight actors are active.
    } # Do not remove this.
    
    #===========================================================================
    # Section III. Level Settings
    # --------------------------------------------------------------------------
    # The following below allows you to adjust the amount of experience each
    # actor will earn and the rules that govern the experience gained.
    #===========================================================================
    
    # The following array determines which stats will be shown with level up
    # increases and also what order. Only the following can be used.
    # :maxhp, :maxmp, :atk, :def, :spi, :res, :dex, :agi
    SHOWN_STATS = [:maxhp, :maxmp, :atk, :def, :spi, :res, :dex, :agi]
    
    # The following hash adjusts the colours used to display each of the
    # various objects inside of the level window.
    LEVEL_COLOURS ={
      :stat_name => 4,  # The stat's name.
      :arrow     => 4,  # Arrow in between the old and new stats.
      :old_stat  => 5,  # The older unleveled stat.
      :new_stat  => 6,  # The newer leveled stat.
    } # Do not remove this.
    
    # Changing the following value to an icon ID will change the → arrow to use
    # an icon instead. i.e. ARROW_ICON = 142
    ARROW_ICON = nil
    
    # Changing the following below will show the change in stat difference from
    # the old stat to the new stat rather than just showing the new stat.
    SHOW_DIFFERENCE = false
    
    # The following will determine how HP and MP recovery is calculated upon
    # leveling up. 0 - no change. 1 - difference from max. 2 - full recovery.
    HP_RECOVERY = 1   # Adjusts HP Recovery Style
    MP_RECOVERY = 1   # Adjusts MP Recovery Style
    
    #===========================================================================
    # Section IV. Audio Settings
    # --------------------------------------------------------------------------
    # This part of the module adjusts everything that's audio related. Be sure
    # to modify them accordingly.
    #===========================================================================
    
    # This victory BGM will play once the fanfare finishes.
    BGM = RPG::BGM.new("Field1", 100, 100)
    
    # This sound adjusts the ticking noise that occurs as the experience bar
    # goes upward. Adjust it accordingly.
    TICK_SOUND = RPG::SE.new("Decision1", 80, 100)
    
    # The following adjusts how long to play the Victory ME after finishing
    # battle in milliseconds (if it is still continuing).
    FANFARE_FADE = 1000
    
    # This is the level up sound that plays when an actor levels up.
    LEVEL_SOUND = RPG::SE.new("Flash1", 100, 100)
    
    # This is the sound that will play if items were found.
    ITEM_SOUND = RPG::SE.new("Item1", 80, 100)
    
  end # VICTORY
end # YEM

#===============================================================================
# Lunatic Mode - Victory Aftermath
# -----------------------------------------------------------------------------
# This mode allows you to add in a bit more of a personal touch to your game by
# adding in victory battle quotes. If they're on, actors who were participating
# in battle can brief the victory spoils and whatnot.
# 
# To use this mode, bind each actor to a common event ID. Those common events
# will play out the messages written for them. However, to distinguish when to
# display each message, use conditional branches with a script value check.
# Insert the following into your script value checks:
# 
#   victory_quote("Main")      Occurs at the first page of victory.
#   victory_quote("Level")     Occurs when leveling up your character.
#   victory_quote("Skill")     Occurs when leveling up and learning a skill.
#   victory_quote("Nothing")   Occurs when no drops are found.
#   victory_quote("Drops")     Occurs if the enemy has left drops.
# 
# When displaying the message events, a few new text shortcuts should be taken
# note of. They are the two following:
# 
#   \VN      This displays the talking actor's name.
#   \VF      This displays the talking actor's face.
# 
# Anything else in the common event will run normally.
#===============================================================================

module YEM::VICTORY
  
  # If this is enabled, victory quotes will occur instead of the typical
  # battle end messages.
  ENABLE_QUOTES = true
  
  # The following hash determines which common event ID's are bound to which
  # actors when their victory quotes are to be launched.
  ACTOR_QUOTES ={ # Actor 0 is the actor used for unlisted actors.
  # ActorID => Common Event ID
          0 => 20,  # Common Victory Quotes
          1 => 21,  # Ralph's Victory Quotes
          2 => 22,  # Ulrika's Victory Quotes
          3 => 23,  # Bennett's Victory Quotes
          4 => 24,  # Ylva's Victory Quotes
          5 => 25,  # Lawrence's Victory Quotes
          6 => 26,  # Oscar's Victory Quotes
          7 => 27,  # Vera's Victory Quotes
          8 => 28,  # Elmer's Victory Quotes
  } # Do not remove this.
  
end # YEM::VICTORY

#===============================================================================
# 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 Icon
#===============================================================================
if !$imported["BattleEngineMelody"] and !$imported["IconModuleLibrary"]
module Icon
  
  #--------------------------------------------------------------------------
  # self.stat
  #--------------------------------------------------------------------------
  def self.stat(actor, item); return 0; end
  
end # Icon
end # !$imported["BattleEngineMelody"] and !$imported["IconModuleLibrary"]

#===============================================================================
# Game_Temp
#===============================================================================

class Game_Temp
  
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :victory_actor
  attr_accessor :victory_quote
  attr_accessor :victory_clone
  
end # Game_Temp

#===============================================================================
# Game_Actor
#===============================================================================

class Game_Actor < Game_Battler
  
  #--------------------------------------------------------------------------
  # new method: learned_skills
  #--------------------------------------------------------------------------
  def learned_skills
    result = []
    for i in @skills
      result.push($data_skills[i])
    end
    return result
  end
  
  #--------------------------------------------------------------------------
  # new method: now_exp
  #--------------------------------------------------------------------------
  def now_exp
    return @exp - @exp_list[@level]
  end
  
  #--------------------------------------------------------------------------
  # new method: next_exp
  #--------------------------------------------------------------------------
  def next_exp
    return @exp_list[@level+1] > 0 ? @exp_list[@level+1] - @exp_list[@level] : 0
  end
  
end # Game_Actor

#==============================================================================
# Game_Interpreter
#==============================================================================

class Game_Interpreter
  
  #--------------------------------------------------------------------------
  # victory_quote
  #--------------------------------------------------------------------------
  def victory_quote(text)
    return false unless $game_temp.in_battle and $scene.is_a?(Scene_Battle)
    return false if $game_temp.victory_quote == nil
    return true if text.upcase == $game_temp.victory_quote
    return false
  end
  
end # Game_Interpreter

#===============================================================================
# Window_Message
#===============================================================================

class Window_Message < Window_Selectable
  
  #--------------------------------------------------------------------------
  # alias method: convert_special_characters
  #--------------------------------------------------------------------------
  unless $imported["CustomMessageMelody"]
  alias convert_special_characters_va convert_special_characters unless $@
  def convert_special_characters
    @text.gsub!(/\\VF/i) { victory_face_art }
    @text.gsub!(/\\VN/i) { victory_actor_name }
    convert_special_characters_va
  end
  end # $imported["CustomMessageMelody"]
  
  #--------------------------------------------------------------------------
  # new method: victory_actor_name
  #--------------------------------------------------------------------------
  def victory_actor_name
    return "" unless $scene.is_a?(Scene_Battle)
    return $scene.victory_actor.name
  end
  
  #--------------------------------------------------------------------------
  # new method: victory_face_art
  #--------------------------------------------------------------------------
  def victory_face_art
    return "" unless $scene.is_a?(Scene_Battle)
    $game_message.face_name  = $scene.victory_actor.face_name
    $game_message.face_index = $scene.victory_actor.face_index
    return ""
  end
  
end # Window_Message

#===============================================================================
# Window_Party_Exp_Back
#===============================================================================

class Window_Party_Exp_Back < Window_Base
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize
    super(0, 112, 544, 176)
    refresh
  end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    sw = self.width - 32
    dy = 0
    dx = (sw/2) - $game_party.members.size*60
    for actor in $game_party.members
      opacity = YEM::VICTORY::FACE_OPACITY
      draw_party_face(actor, dx+12, dy, opacity)
      self.contents.draw_text(dx+12, dy, 96, WLH, actor.name, 1)
      dx += 120
    end
  end
  
  #--------------------------------------------------------------------------
  # Draw Party Face
  #--------------------------------------------------------------------------
  def draw_party_face(actor, x, y, opacity = 255, size = 96)
    face_name = actor.face_name
    face_index = actor.face_index
    bitmap = Cache.face(face_name)
    rect = Rect.new(0, 0, 0, 0)
    rect.x = face_index % 4 * 96 + (96 - size) / 2
    rect.y = face_index / 4 * 96 + (96 - size) / 2
    rect.width = size
    rect.height = size
    self.contents.blt(x, y, bitmap, rect, opacity)
    bitmap.dispose
  end
  
end # Window_Party_Exp_Back

#===============================================================================
# Window_Party_Exp_Front
#===============================================================================

class Window_Party_Exp_Front < Window_Base
  
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :full_actors
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize
    super(0, 112, 544, 176)
    self.opacity = 0
    @extra_per = 0
    @increase = 100 / YEM::VICTORY::EXP_TICKS
    refresh
  end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh(extra_per = 0)
    @full = true if extra_per >= 100
    self.contents.clear
    @full_actors = 0
    sw = self.width - 32
    dy = WLH*4
    dx = (sw/2) - $game_party.members.size*60
    for actor in $game_party.members
      self.contents.font.color = normal_color
      self.contents.font.size = Font.default_size
      draw_exp_gauge(actor, dx, dy, extra_per)
      draw_exp_text(actor, dx, dy+WLH, extra_per)
      dx += 120
    end
  end
  
  #--------------------------------------------------------------------------
  # draw_exp_gauge
  #--------------------------------------------------------------------------
  def draw_exp_gauge(actor, dx, dy, extra_per)
    self.contents.font.size = YEM::VICTORY::EXP_FONT_SIZE
    gc1 = text_color(YEM::VICTORY::EXP_GAUGE_1)
    gc2 = text_color(YEM::VICTORY::EXP_GAUGE_2)
    if actor.level > $game_temp.victory_clone[actor.id].level
      gc1 = text_color(YEM::VICTORY::LVL_GAUGE_1)
      gc2 = text_color(YEM::VICTORY::LVL_GAUGE_2)
      gw = 96
      self.contents.font.color = text_color(YEM::VICTORY::LVL_COLOUR)
      text = YEM::VICTORY::VOCAB[:level_up]
    elsif actor.next_exp != 0
      gw = 96 * (actor.now_exp + exp_gained(extra_per, actor))
      gw /= actor.next_exp
      gw = [[gw, 96].min, 0].max
      text = sprintf(YEM::VICTORY::VOCAB[:next_level], actor.level + 1)
      if gw == 96
        self.contents.font.color = text_color(YEM::VICTORY::LVL_COLOUR)
        gc1 = text_color(YEM::VICTORY::LVL_GAUGE_1)
        gc2 = text_color(YEM::VICTORY::LVL_GAUGE_2)
        text = YEM::VICTORY::VOCAB[:level_up]
      end
    else
      gw = 96
      text = YEM::VICTORY::VOCAB[:max_level]
    end
    if $imported["CoreFixesUpgradesMelody"] and YEM::UPGRADE::OUTLINE
      self.contents.fill_rect(dx + 12, dy - 8, 96, 8, gauge_back_color)
      self.contents.gradient_fill_rect(dx + 13, dy - 7, gw-2, 6, gc1, gc2)
    else
      self.contents.fill_rect(dx + 12, dy - 6, 96, 6, gauge_back_color)
      self.contents.gradient_fill_rect(dx + 12, dy - 6, gw, 6, gc1, gc2)
    end
    self.contents.draw_text(dx, dy, 120, WLH, text, 1)
    self.contents.font.color = normal_color
  end
  
  #--------------------------------------------------------------------------
  # draw_exp_text
  #--------------------------------------------------------------------------
  def draw_exp_text(actor, dx, dy, extra_per)
    if actor.level > $game_temp.victory_clone[actor.id].level
      exp_percent = 100.0
      text1 = sprintf(YEM::VICTORY::VOCAB[:percent_exp], exp_percent)
      text2 = sprintf(YEM::VICTORY::VOCAB[:to_level], actor.level)
    elsif actor.next_exp != 0
      exp_percent = (actor.now_exp + exp_gained(extra_per, actor)) * 100.0
      exp_percent /= actor.next_exp
      exp_percent = [[exp_percent, 100.0].min, 0].max
      @full_actors += 1 if exp_percent >= 100.0
      value = [actor.next_rest_exp_s - exp_gained(extra_per, actor), 0].max
      text1 = sprintf(YEM::VICTORY::VOCAB[:percent_exp], exp_percent)
      text2 = sprintf(YEM::VICTORY::VOCAB[:next_exp], value)
    else
      exp_percent = 100.0 
      @full_actors += 1
      text1 = sprintf(YEM::VICTORY::VOCAB[:percent_exp], exp_percent)
      text2 = ""
    end
    self.contents.draw_text(dx, dy - WLH * 2, 120, WLH, text1, 1)
    self.contents.draw_text(dx, dy, 120, WLH, text2, 1)
  end
  
  #--------------------------------------------------------------------------
  # exp_gained
  #--------------------------------------------------------------------------
  def exp_gained(extra_per, actor)
    return 0 if actor.dead?
    extra_per = 100 if extra_per > 100
    exp = $game_troop.exp_total
    members = $game_party.members.size
    if YEM::VICTORY::PARTY_SPLIT_EXP
      size = YEM::VICTORY::DEAD_ALLY_EXP ? 
      $game_party.members.size : $game_party.existing_members.size
      exp = Integer(exp * YEM::VICTORY::PARTY_SIZE_SCALING[size])
    end
    exp *= 2 if actor.double_exp_gain
    exp *= extra_per
    exp /= 100
    return exp
  end
  
end # Window_Party_Exp_Front

#===============================================================================
# Window_New_Skills
#===============================================================================

class Window_New_Skills < Window_Selectable
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(new_skills)
    @new_skills = new_skills
    super(316, 80, 228, 208)
    self.index = -1
    self.opacity = 0
    refresh
  end
  
  #--------------------------------------------------------------------------
  # skill
  #--------------------------------------------------------------------------
  def skill; return @data[self.index]; end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    @data = []
    for skill in @new_skills
      @data.push(skill)
    end
    @item_max = @data.size
    create_contents
    for i in 0..(@item_max-1)
      draw_item(i)
    end
  end
  
  #--------------------------------------------------------------------------
  # draw_item
  #--------------------------------------------------------------------------
  def draw_item(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    skill = @data[index]
    return if skill == nil
    draw_item_name(skill, rect.x, rect.y)
  end
  
  #--------------------------------------------------------------------------
  # update_help
  #--------------------------------------------------------------------------
  def update_help
    @help_window.set_text(skill == nil ? "" : skill.description)
  end
  
end # Window_New_Skills

#===============================================================================
# Window_BattleDrops
#===============================================================================

class Window_BattleDrops < Window_Selectable
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(drop_items)
    super(0, 56, 544, 232)
    @drop_items = drop_items
    @column_max = 2
    self.index = -1
    refresh
  end
  
  #--------------------------------------------------------------------------
  # item
  #--------------------------------------------------------------------------
  def item; return @data[self.index]; end
  
  #--------------------------------------------------------------------------
  # refrehs
  #--------------------------------------------------------------------------
  def refresh
    @items = {}; @weapons = {}; @armours = {}; @goods = {}; @data = []
    for item in @drop_items
      case item
      when RPG::Item
        @items[item] = 0 if @items[item] == nil
        @items[item] += 1
      when RPG::Weapon
        @weapons[item] = 0 if @weapons[item] == nil
        @weapons[item] += 1
      when RPG::Armor
        @armours[item] = 0 if @armours[item] == nil
        @armours[item] += 1
      end
    end
    @items = @items.sort { |a,b| a[0].id <=> b[0].id }
    @weapons = @weapons.sort { |a,b| a[0].id <=> b[0].id }
    @armours = @armours.sort { |a,b| a[0].id <=> b[0].id }
    for key in @items; @goods[key[0]] = key[1]; @data.push(key[0]); end
    for key in @weapons; @goods[key[0]] = key[1]; @data.push(key[0]); end
    for key in @armours; @goods[key[0]] = key[1]; @data.push(key[0]); end
    @item_max = @data.size
    create_contents
    for i in 0..(@item_max-1); draw_item(i); end
  end
  
  #--------------------------------------------------------------------------
  # draw_item
  #--------------------------------------------------------------------------
  def draw_item(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    item = @data[index]
    return if item == nil
    number = @goods[item]
    rect.width -= 4
    self.contents.font.size = Font.default_size
    self.contents.font.color = normal_color
    draw_item_name(item, rect.x, rect.y)
    if $imported["BattleEngineMelody"]
      text = YEM::BATTLE_ENGINE::ITEM_SETTINGS[:text]
      self.contents.font.size = YEM::BATTLE_ENGINE::ITEM_SETTINGS[:size]
      colour = YEM::BATTLE_ENGINE::ITEM_SETTINGS[:colour]
      self.contents.font.color = text_color(colour)
      self.contents.draw_text(rect, sprintf(text, number), 2)
    else
      text = YEM::VICTORY::VOCAB[:item_amount]
      self.contents.draw_text(rect, sprintf(text, number), 2)
    end
  end
  
  #--------------------------------------------------------------------------
  # update_help
  #--------------------------------------------------------------------------
  def update_help
    @help_window.set_text(item == nil ? "" : item.description)
  end
  
end # Window_BattleDrops

#===============================================================================
# Window_Level_Up
#===============================================================================

class Window_Level_Up < Window_Base
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(actor, dx, dy, dw, dh)
    super(dx, dy, dw, dh)
    @actor = actor
    @clone = $game_temp.victory_clone[actor.id]
    refresh
  end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    sw = self.width - 32
    dy = 0; dx = sw/2 - 210/2
    draw_stats(dx, dy)
  end
  
  #--------------------------------------------------------------------------
  # draw_stats
  #--------------------------------------------------------------------------
  def draw_stats(dx, dy)
    arrow = YEM::VICTORY::VOCAB[:next_stat]
    colours = YEM::VICTORY::LEVEL_COLOURS
    @actor.clear_battle_cache if $imported["BattleEngineMelody"]
    for stat in YEM::VICTORY::SHOWN_STATS
      case stat
      when :maxhp
        text = Vocab.hp
        value1 = @clone.maxhp
        value2 = @actor.maxhp
      when :maxmp
        text = Vocab.mp
        value1 = @clone.maxmp
        value2 = @actor.maxmp
      when :atk
        text = Vocab.atk
        value1 = @clone.atk
        value2 = @actor.atk
      when :def
        text = Vocab.def
        value1 = @clone.def
        value2 = @actor.def
      when :spi
        text = Vocab.spi
        value1 = @clone.spi
        value2 = @actor.spi
      when :agi
        text = Vocab.agi
        value1 = @clone.agi
        value2 = @actor.agi
      when :dex
        next unless $imported["DEX Stat"]
        text = Vocab.dex
        value1 = @clone.dex
        value2 = @actor.dex
      when :res
        next unless $imported["RES Stat"]
        text = Vocab.res
        value1 = @clone.res
        value2 = @actor.res
      else; next
      end
      icon = Icon.stat(@actor, stat)
      icon = 0 if icon == nil
      value2 = sprintf("%+d", value2 - value1) if YEM::VICTORY::SHOW_DIFFERENCE
      draw_icon(icon, dx, dy)
      self.contents.font.color = text_color(colours[:stat_name])
      self.contents.draw_text(dx+24, dy, 60, WLH, text, 0)
      self.contents.font.color = text_color(colours[:old_stat])
      self.contents.draw_text(dx+84, dy, 60, WLH, value1, 2)
      if YEM::VICTORY::ARROW_ICON.is_a?(Integer)
        draw_icon(YEM::VICTORY::ARROW_ICON, dx+147, dy)
      else
        self.contents.font.color = text_color(colours[:arrow])
        self.contents.draw_text(dx+144, dy, 30, WLH, arrow, 1)
      end
      self.contents.font.color = text_color(colours[:new_stat])
      self.contents.draw_text(dx+174, dy, 60, WLH, value2, 0)
      dy += WLH
    end
  end
  
end # Window_Level_Up

#===============================================================================
# Scene Map
#===============================================================================

class Scene_Map < Scene_Base

  #--------------------------------------------------------------------------
  # alias method: call battle
  #--------------------------------------------------------------------------
  alias call_battle_va call_battle unless $@
  def call_battle
    if $game_switches[YEM::VICTORY::BYPASS_BGM_SWITCH]
      @spriteset.update
      Graphics.update
      $game_player.make_encounter_count
      $game_player.straighten
      $game_temp.map_bgm = RPG::BGM.last
      $game_temp.map_bgs = RPG::BGS.last
      Sound.play_battle_start
      $game_temp.next_scene = nil
      $scene = Scene_Battle.new
    else
      call_battle_va
    end
  end
  
end # Scene Map

#===============================================================================
# Scene_Battle
#===============================================================================

class Scene_Battle < Scene_Base

  #--------------------------------------------------------------------------
  # alias method: terminate
  #--------------------------------------------------------------------------
  alias terminate_va terminate unless $@
  def terminate
    @top_window.dispose if @top_window != nil
    @exp_window.dispose if @exp_window != nil
    @gold_window.dispose if @gold_window != nil
    @exp_back_window.dispose if @exp_back_window != nil
    @exp_front_window.dispose if @exp_front_window != nil
    @drops_window.dispose if @drops_window != nil
    terminate_va
  end
  
  #--------------------------------------------------------------------------
  # overwrite method: process_victory
  #--------------------------------------------------------------------------
  def process_victory
    if $imported["BattleEngineMelody"]
      @judge_win_loss = true
      pause_atb(true, true)
      @enemy_gauge_window.dispose if @enemy_gauge_window != nil
      @hide_battlecursor = true
      @party_input_flag = true
    end    
    wait_for_deaths if $imported["FancyDeaths"]
    unless $game_switches[YEM::VICTORY::BYPASS_BGM_SWITCH]
      RPG::BGM.stop
      $game_system.battle_end_me.play
      YEM::VICTORY::BGM.play unless YEM::VICTORY::BGM == nil
    end
    if $imported["BattleEngineMelody"]
      start_victory_poses
      @spriteset.update_dtb_order
      @spriteset.update_ctb_order
    end
    wait(YEM::VICTORY::WAIT, true)
    @info_viewport.visible = false
    if $imported["BattleEngineMelody"]
      @message_window.dispose
      @message_window = Window_BattleMessage.new
    elsif $imported["VictoryCompatibility"]
      @message_window.dispose
      @message_window = Window_BattleMessageCompatible.new
    end
    @message_window.visible = true
    create_victory_clones
    create_victory_actor
    display_exp_and_gold
    display_level_up
    display_drop_items
    battle_end(0)
    $game_temp.victory_actor = nil
    $game_temp.victory_clone = nil
    victory_me = Thread.new {
      time = YEM::VICTORY::FANFARE_FADE
      RPG::ME.fade(time)
      sleep(time / 1000.0)
      RPG::ME.stop }
    battle_end(0)
  end
  
  #--------------------------------------------------------------------------
  # overwrite method: display_exp_and_gold
  #--------------------------------------------------------------------------
  def display_exp_and_gold
    unless $game_switches[YEM::VICTORY::SKIP_VICTORY_SWITCH]
      show_exp_and_gold
    else
      @exp = $game_troop.exp_total
      if YEM::VICTORY::PARTY_SPLIT_EXP
        size = YEM::VICTORY::DEAD_ALLY_EXP ? 
        $game_party.members.size : $game_party.existing_members.size
        @exp = Integer(@exp * YEM::VICTORY::PARTY_SIZE_SCALING[size])
      end
      $game_party.gain_gold($game_troop.gold_total)
    end
  end
  
  #--------------------------------------------------------------------------
  # overwrite method: display_level_up
  #--------------------------------------------------------------------------
  def display_level_up
    unless $game_switches[YEM::VICTORY::SKIP_VICTORY_SWITCH]
      show_level_up
    else
      group = YEM::VICTORY::DEAD_ALLY_EXP ?
      $game_party.members : $game_party.existing_members
      for actor in group
        last_level = actor.level
        last_skills = actor.learned_skills.clone
        actor.gain_exp(@exp, false)
      end
    end
  end
  
  #--------------------------------------------------------------------------
  # overwrite method: display_drop_items
  #--------------------------------------------------------------------------
  def display_drop_items
    @active_battler = nil
    unless $game_switches[YEM::VICTORY::SKIP_VICTORY_SWITCH]
      show_drop_items
    else
      drop_items = $game_troop.make_drop_items
      for item in drop_items; $game_party.gain_item(item, 1); end
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: create_victory_clones
  #--------------------------------------------------------------------------
  def create_victory_clones
    $game_temp.victory_clone = {}
    $game_temp.in_battle = false
    for actor in $game_party.members
      $game_temp.victory_clone[actor.id] = actor.clone
    end
    $game_temp.in_battle = true
  end
  
  #--------------------------------------------------------------------------
  # new method: create_victory_actor
  #--------------------------------------------------------------------------
  def create_victory_actor
    $game_temp.victory_actor = nil
    @original_victory_actor = victory_actor
  end
  
  #--------------------------------------------------------------------------
  # new method: victory_actor
  #--------------------------------------------------------------------------
  def victory_actor
    if $game_temp.victory_actor == nil
      roulette = []
      for actor in $game_party.existing_members; roulette.push(actor); end
      $game_temp.victory_actor = roulette.size > 0 ?
        roulette[rand(roulette.size)] : nil
    end
    return $game_temp.victory_actor
  end
  
  #--------------------------------------------------------------------------
  # new method: victory_actor_quote
  #--------------------------------------------------------------------------
  def victory_actor_quote(value)
    $game_temp.victory_quote = value
    if YEM::VICTORY::ACTOR_QUOTES.include?(victory_actor.id)
      $game_temp.common_event_id = YEM::VICTORY::ACTOR_QUOTES[victory_actor.id]
    else
      $game_temp.common_event_id = YEM::VICTORY::ACTOR_QUOTES[0]
    end
    @message_window.visible = true
    process_victory_event
  end
  
  #--------------------------------------------------------------------------
  # new method: process_victory_event
  #--------------------------------------------------------------------------
  def process_victory_event
    loop do
      $game_troop.interpreter.update
      $game_troop.setup_battle_event
      wait_for_message
      return unless $game_troop.interpreter.running?
      update_basic
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: show_exp_and_gold
  #--------------------------------------------------------------------------
  def show_exp_and_gold
    #--- Draw Victory Message
    @top_window = Window_Help.new
    text = YEM::VICTORY::VOCAB[:top_message]
    @top_window.set_text(text, 1)
    #--- Draw EXP Window
    @exp = $game_troop.exp_total
    if YEM::VICTORY::PARTY_SPLIT_EXP
      size = YEM::VICTORY::DEAD_ALLY_EXP ? 
      $game_party.members.size : $game_party.existing_members.size
      @exp = Integer(@exp * YEM::VICTORY::PARTY_SIZE_SCALING[size])
    end
    @exp_window = Window_Base.new(272, 56, 272, 56)
    text = sprintf(YEM::VICTORY::VOCAB[:exp_receive], @exp)
    @exp_window.contents.draw_text(0, 0, 240, 24, text, 1)
    #--- Draw Gold Window
    @gold = $game_troop.gold_total
    $game_party.gain_gold(@gold)
    @gold_window = Window_Base.new(0, 56, 272, 56)
    text = sprintf(YEM::VICTORY::VOCAB[:gold_found], @gold)
    @gold_window.contents.draw_text(0, 0, 240, 24, text, 1)
    #--- Draw Party EXP Windows
    @exp_back_window = Window_Party_Exp_Back.new
    @exp_front_window = Window_Party_Exp_Front.new
    if $imported["BattleEngineMelody"]
      @top_window.viewport = @spriteset.viewportC
      @exp_window.viewport = @spriteset.viewportC
      @gold_window.viewport = @spriteset.viewportC
      @exp_back_window.viewport = @spriteset.viewportC
      @exp_front_window.viewport = @spriteset.viewportC
    end
    if !YEM::VICTORY::SHOW_EXP_WINDOWS
      @top_window.visible = false
      @exp_window.visible = false
      @gold_window.visible = false
      @exp_back_window.visible = false
      @exp_front_window.refresh(100)
      @exp_front_window.visible = false
      return
    end
    fill_exp_gauge
    #--- Battle Quotes
    if YEM::VICTORY::ENABLE_QUOTES
      victory_actor_quote("MAIN")
    else
      text = sprintf(Vocab::Victory, $game_party.name)
      $game_message.texts.push(text)
      wait_for_message
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: fill_exp_gauge
  #--------------------------------------------------------------------------
  def fill_exp_gauge
    ticks = 0
    percent = 0
    increase = 100 / YEM::VICTORY::EXP_TICKS
    until ticks > YEM::VICTORY::EXP_TICKS
      ticks += 1
      percent += increase
      @exp_front_window.refresh(percent)
      if @exp_front_window.full_actors != $game_party.existing_members.size
        YEM::VICTORY::TICK_SOUND.play if YEM::VICTORY::TICK_SOUND != nil
      end
      update_basic
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: show_level_up
  #--------------------------------------------------------------------------
  def show_level_up
    return unless YEM::VICTORY::SHOW_LEVEL_WINDOWS
    @top_window.visible = true
    @exp_window.visible = false
    @gold_window.visible = false
    @exp_back_window.visible = false
    @exp_front_window.visible = false
    group = YEM::VICTORY::DEAD_ALLY_EXP ?
      $game_party.members : $game_party.existing_members
    for actor in group
      last_level = actor.level
      last_skills = actor.learned_skills.clone
      actor.gain_exp(@exp, false)
      create_level_up_windows(actor, last_skills) if actor.level > last_level
    end
    wait_for_message
  end
  
  #--------------------------------------------------------------------------
  # new method: create_level_up_windows
  #--------------------------------------------------------------------------
  def create_level_up_windows(actor, last_skills)
    @active_battler = actor
    #--- Basic Settings
    YEM::VICTORY::LEVEL_SOUND.play if YEM::VICTORY::LEVEL_SOUND != nil
    $game_temp.victory_actor = actor
    new_skills = actor.learned_skills - last_skills
    @top_window.contents.clear
    text = sprintf(YEM::VICTORY::VOCAB[:level_msg], actor.name, actor.level)
    @top_window.set_text(text, 1)
    #--- HP and MP Recovery
    case YEM::VICTORY::HP_RECOVERY
    when 1; actor.hp += actor.maxhp - $game_temp.victory_clone[actor.id].maxhp
    when 2; actor.hp = actor.maxhp
    end
    case YEM::VICTORY::MP_RECOVERY
    when 1; actor.mp += actor.maxmp - $game_temp.victory_clone[actor.id].maxmp
    when 2; actor.mp = actor.maxmp
    end
    #--- Create Windows
    if new_skills != []
      @levelup_window = Window_Level_Up.new(actor, 0, 56, 316, 232)
      @skill_back_window = Window_Base.new(316, 56, 228, 232)
      @skill_back_window.contents.font.color = @skill_back_window.system_color
      text = YEM::VICTORY::VOCAB[:new_skills]
      @skill_back_window.contents.draw_text(0, 0, 196, 24, text, 1)
      @skill_list_window = Window_New_Skills.new(new_skills)
    else
      @levelup_window = Window_Level_Up.new(actor, 0, 56, 544, 232)
    end
    if $imported["BattleEngineMelody"]
      @levelup_window.viewport = @spriteset.viewportC
      if @skill_back_window != nil
        @skill_back_window.viewport = @spriteset.viewportC
        @skill_list_window.viewport = @spriteset.viewportC
      end
    end
    if YEM::VICTORY::ENABLE_QUOTES
      value = (last_skills != actor.learned_skills) ? "SKILL" : "LEVEL"
      victory_actor_quote(value)
    else
      text = sprintf(Vocab::LevelUp, actor.name, Vocab::level, actor.level)
      $game_message.new_page
      $game_message.texts.push(text)
      if new_skills != []
        for skill in new_skills
          text = sprintf(Vocab::ObtainSkill, skill.name)
          $game_message.texts.push(text)
        end
      end
      wait_for_message
    end
    #--- Skill Window Scrolling
    if new_skills.size > 7
      @skill_list_window.index = 0
      @skill_list_window.active = true
      @skill_list_window.help_window = @top_window
      loop do
        update_basic
        @skill_list_window.update
        if Input.trigger?(Input::C)
          Sound.play_decision; break
        elsif Input.trigger?(Input::B)
          Sound.play_cancel; break
        elsif Input.press?(Input::A)
          break
        end
      end
    end
    #--- Disposal of Windows
    @levelup_window.dispose if @levelup_window != nil
    @levelup_window = nil
    @skill_back_window.dispose if @skill_back_window != nil
    @skill_back_window = nil
    @skill_list_window.dispose if @skill_list_window != nil
    @skill_list_window = nil
  end
  
  #--------------------------------------------------------------------------
  # new method: show_drop_items
  #--------------------------------------------------------------------------
  def show_drop_items
    @drop_items = $game_troop.make_drop_items
    for item in @drop_items; $game_party.gain_item(item, 1); end
    return unless YEM::VICTORY::SHOW_ITEM_WINDOWS
    @top_window.visible = true
    if @drop_items != []
      @top_window.set_text(YEM::VICTORY::VOCAB[:spoils],1)
      YEM::VICTORY::ITEM_SOUND.play if YEM::VICTORY::ITEM_SOUND != nil
      @drops_window = Window_BattleDrops.new(@drop_items)
      if $imported["BattleEngineMelody"]
        @drops_window.viewport = @spriteset.viewportC
      end
    else
      @top_window.set_text(YEM::VICTORY::VOCAB[:no_items],1)
      @exp_window.visible = true
      @gold_window.visible = true
      @exp_back_window.visible = true
      @exp_front_window.visible = true
    end
    if YEM::VICTORY::ENABLE_QUOTES
      $game_temp.victory_actor = @original_victory_actor
      value = (@drop_items == []) ? "NOTHING" : "DROPS"
      victory_actor_quote(value)
    else
      if @drop_items == []
        text = YEM::VICTORY::VOCAB[:no_items]
      else
        text = sprintf(YEM::VICTORY::VOCAB[:found_items], $game_party.name)
      end
      $game_message.texts.push(text)
      wait_for_message
    end
    if @drop_items != [] and @drops_window.item_max > 16
      @drops_window.index = 0
      @drops_window.active = true
      @drops_window.help_window = @top_window
      loop do
        update_basic
        @drops_window.update
        if Input.trigger?(Input::C)
          Sound.play_decision; break
        elsif Input.trigger?(Input::B)
          Sound.play_cancel; break
        elsif Input.press?(Input::A)
          break
        end
      end
    end
  end
  
end # Scene_Battle

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