#==============================================================================
#  Scene_Map
#------------------------------------------------------------------------------
# @}bvʂ̏sNXłB
#==============================================================================

class Scene_Map < Scene_Base
  #--------------------------------------------------------------------------
  #  Jn
  #--------------------------------------------------------------------------
  def start
    super
    $game_map.refresh
    @spriteset = Spriteset_Map.new
    @message_window = Window_Message.new
    #T14-019 ADD--->
    #퓬wiIɕύXꍇ̔wiA
    if $game_temp.bk_auto_changed == true
      $game_temp.bk_auto_changed = false
      $game_system.set_env( "BattleBack", Ex::BATTLEBK_AUTO )
    end
    #<--------------
  end
  #--------------------------------------------------------------------------
  #  gWVs
  #--------------------------------------------------------------------------
  def perform_transition
    if Graphics.brightness == 0       # 퓬A[hȂ
      fadein(30)
    else                              # j[̕AȂ
      Graphics.transition(15)
    end
  end
  #--------------------------------------------------------------------------
  #  I
  #--------------------------------------------------------------------------
  def terminate
    super
    if $scene.is_a?(Scene_Battle)     # ogʂɐ؂ւ̏ꍇ
      @spriteset.dispose_characters   # wi쐬̂߂ɃLB
    end
    snapshot_for_background
    @spriteset.dispose
    @message_window.dispose
    if $scene.is_a?(Scene_Battle)     # ogʂɐ؂ւ̏ꍇ
      perform_battle_transition       # 퓬OgWVs
    end
  end
  #--------------------------------------------------------------------------
  #  {XV
  #--------------------------------------------------------------------------
  def update_basic
    Graphics.update                   # Q[ʂXV
    Input.update                      # ͏XV
    $game_map.update                  # }bvXV
    @spriteset.update                 # XvCgZbgXV
  end
  #--------------------------------------------------------------------------
  #  t[XV
  #--------------------------------------------------------------------------
  def update
    super
    $game_map.interpreter.update      # C^v^XV
    $game_map.update                  # }bvXV
    $game_player.update               # vC[XV
    $game_system.update               # ^C}[XV
    @spriteset.update                 # XvCgZbgXV
    @message_window.update            # bZ[WEBhEXV
    unless $game_message.visible      # bZ[W\ȊO
      update_transfer_player
      update_encounter
      update_call_menu
      update_call_debug
      update_scene_change
    end
  end
  #--------------------------------------------------------------------------
  #  ʂ̃tF[hC
  #     duration : 
  #    }bvʂł́AGraphics.fadeout 𒼐ڎgƓVGtFNg≓ĩX
  #  N[Ȃǂ~܂Ȃǂ̕ss邽߁AIɃtF[hCsB
  #--------------------------------------------------------------------------
  def fadein(duration)
    Graphics.transition(0)
    for i in 0..duration-1
      Graphics.brightness = 255 * i / duration
      update_basic
    end
    Graphics.brightness = 255
  end
  #--------------------------------------------------------------------------
  #  ʂ̃tF[hAEg
  #     duration : 
  #    L̃tF[hCƓAGraphics.fadein ͒ڎgȂB
  #--------------------------------------------------------------------------
  def fadeout(duration)
    Graphics.transition(0)
    for i in 0..duration-1
      Graphics.brightness = 255 - 255 * i / duration
      update_basic
    end
    Graphics.brightness = 0
  end
  #--------------------------------------------------------------------------
  #  ꏊړ̏
  #--------------------------------------------------------------------------
  def update_transfer_player
    return unless $game_player.transfer?
    fade = (Graphics.brightness > 0)
    fadeout(30) if fade
    @spriteset.dispose              # XvCgZbg
    $game_player.perform_transfer   # ꏊړ̎s
    $game_map.autoplay              # BGM  BGS ̎؂ւ
    $game_map.update
    Graphics.wait(15)
    @spriteset = Spriteset_Map.new  # XvCgZbgč쐬
    fadein(30) if fade
    Input.update
  end
  #--------------------------------------------------------------------------
  #  GJEg̏
  #--------------------------------------------------------------------------
  def update_encounter
    return if $game_player.encounter_count > 0        # H
    return if $game_map.interpreter.running?          # CxgsH
    return if $game_system.encounter_disabled         # GJEg֎~H
    troop_id = $game_player.make_encounter_troop_id   # GO[v
    return if $data_troops[troop_id] == nil           # GO[vH
    $game_troop.setup(troop_id)
    $game_troop.can_escape = true
    $game_temp.battle_proc = nil
    $game_temp.next_scene = "battle"
    preemptive_or_surprise
  end
  #--------------------------------------------------------------------------
  #  搧Uƕsӑł̊m
  #--------------------------------------------------------------------------
  def preemptive_or_surprise
    actors_agi = $game_party.average_agi
    enemies_agi = $game_troop.average_agi
    if actors_agi >= enemies_agi
      percent_preemptive = 5
      percent_surprise = 3
    else
      percent_preemptive = 3
      percent_surprise = 5
    end
    if rand(100) < percent_preemptive
      $game_troop.preemptive = true
    elsif rand(100) < percent_surprise
      $game_troop.surprise = true
    end
  end
  #--------------------------------------------------------------------------
  #  LZ{^ɂ郁j[Ăяo
  #--------------------------------------------------------------------------
  def update_call_menu
    if Input.trigger?(Input::B)
      return if $game_map.interpreter.running?        # CxgsH
      return if $game_system.menu_disabled            # j[֎~H
      $game_temp.menu_beep = true                     # SE ttOݒ
      $game_temp.next_scene = "menu"
    end
  end
  #--------------------------------------------------------------------------
  #  F9 L[ɂfobOĂяo
  #--------------------------------------------------------------------------
  def update_call_debug
    if $TEST and Input.press?(Input::F9)    # eXgvC F9 L[
      $game_temp.next_scene = "debug"
    end
  end
  #--------------------------------------------------------------------------
  #  ʐ؂ւ̎s
  #--------------------------------------------------------------------------
  def update_scene_change
    return if $game_player.moving?    # vC[̈ړH
    case $game_temp.next_scene
    when "battle"
      call_battle
    when "shop"
      call_shop
    when "name"
      call_name
    when "menu"
      call_menu
    when "save"
      call_save
    when "debug"
      call_debug
    when "gameover"
      call_gameover
    when "title"
      call_title
    else
      $game_temp.next_scene = nil
    end
  end
  #--------------------------------------------------------------------------
  #  ogʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_battle
    @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
    RPG::BGM.stop
    RPG::BGS.stop
    Sound.play_battle_start
    $game_system.battle_bgm.play
    $game_temp.next_scene = nil
    $scene = Scene_Battle.new
    #T14-019 ADD--->
    #퓬wiIɕύX鏈
    if $game_system.get_env( "BattleBack" ) == Ex::BATTLEBK_AUTO
      $game_system.set_env( "BattleBack", battlebk_auto_change )
      $game_temp.bk_auto_changed = true
    end
    #<--------------
  end
  #--------------------------------------------------------------------------
  #  Vbvʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_shop
    $game_temp.next_scene = nil
    $scene = Scene_Shop.new
  end
  #--------------------------------------------------------------------------
  #  O͉ʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_name
    $game_temp.next_scene = nil
    $scene = Scene_Name.new
  end
  #--------------------------------------------------------------------------
  #  j[ʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_menu
    if $game_temp.menu_beep
      Sound.play_decision
      $game_temp.menu_beep = false
    end
    $game_temp.next_scene = nil
    $scene = Scene_Menu.new
  end
  #--------------------------------------------------------------------------
  #  Z[uʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_save
    $game_temp.next_scene = nil
    $scene = Scene_File.new(true, false, true)
  end
  #--------------------------------------------------------------------------
  #  fobOʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_debug
    Sound.play_decision
    $game_temp.next_scene = nil
    $scene = Scene_Debug.new
  end
  #--------------------------------------------------------------------------
  #  Q[I[o[ʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_gameover
    $game_temp.next_scene = nil
    $scene = Scene_Gameover.new
  end
  #--------------------------------------------------------------------------
  #  ^Cgʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_title
    $game_temp.next_scene = nil
    $scene = Scene_Title.new
    fadeout(60)
  end
  #--------------------------------------------------------------------------
  #  퓬OgWVs
  #--------------------------------------------------------------------------
  def perform_battle_transition
    Graphics.transition(80, "Graphics/System/BattleStart", 80)
    Graphics.freeze
  end
  #T14-019 ADD--->
  #--------------------------------------------------------------------------
  #  vC[݈̌ʒu퓬wiIɌ肷
  #--------------------------------------------------------------------------
  def battlebk_auto_change
    #}bvf[^ƃvC[̍Wϊ^CZbg̏
    tile_set = ""  #^CZbg̕A`E
    tile_x = 0     #^CZbg̉W
    tile_y = 0     #^CZbg̏cW
    
    #[N̈̏
    result = nil          #̌
    map = $game_map.data  #}bvf[^̃e[u
    x = $game_player.x    #vC[XW
    y = $game_player.y    #vC[YW
    v0 = map[x, y, 0]     #}bvf[^(^CA1`A5)
    v1 = map[x, y, 1]     #}bvf[^(^CA1`A5̕⑫)
    v2 = map[x, y, 2]     #}bvf[^(^CB`E)
    
    #^CB`Eł邩ǂ𔻒肷
    if (v2 >= 0x0001) and (v2 <= 0x03ff)
      tile_sets = ["B", "C", "D", "E"]
      tile_set = tile_sets[(v2 & 0x0f00) >> 8]
      tile_x = (v2 & 0x0007) + 1
      tile_y = ((v2 & 0x00ff) / 8) + 1
      result = Ex.get_auto_battlebk( tile_set, tile_x, tile_y )
    end
    
    #^CA5ł邩ǂ𔻒肷
    if (result == nil) and (v0 >= 0x0600) and (v0 <= 0x067f)
      tile_set = "A"
      tile_x = (v0 & 0x0007) + 1
      tile_y = ((v0 & 0x00ff) / 8) + 17
      result = Ex.get_auto_battlebk( tile_set, tile_x, tile_y )
    end
    
    #^CA1`A4ł邩ǂ𔻒肷
    if (result == nil) and (v0 >= 0x0800) and (v0 <= 0x1fff)
      idx = (v0 - 0x0800) / 0x0030
      
      #[C,,X,R,΂,̔
      case idx
      when 0   #C
        idx = 1 if (v1 >= 0x0830) and (v1 <= 0x085f)  #[C
        idx = 2 if (v1 >= 0x0860) and (v1 <= 0x088f)  #1
        idx = 3 if (v1 >= 0x0890) and (v1 <= 0x08bf)  #2
      when 16  #1
        idx = 17 if (v1 >= 0x0b30) and (v1 <= 0x0b5f)  #X1
        idx = 18 if (v1 >= 0x0b60) and (v1 <= 0x0b8f)  #R1
      when 19  #n1
        idx = 20 if (v1 >= 0x0bc0) and (v1 <= 0x0bdf)  #΂1
        idx = 21 if (v1 >= 0x0bf0) and (v1 <= 0x0c1f)  #1
      when 24  #2
        idx = 25 if (v1 >= 0x0cb0) and (v1 <= 0x0cdf)  #X2
        idx = 26 if (v1 >= 0x0ce0) and (v1 <= 0x0d0f)  #R2
      when 27  #n2
        idx = 28 if (v1 >= 0x0d40) and (v1 <= 0x0d6f)  #΂2
        idx = 29 if (v1 >= 0x0d70) and (v1 <= 0x0d9f)  #2
      when 32  #3
        idx = 33 if (v1 >= 0x0e30) and (v1 <= 0x0e5f)  #X3
        idx = 34 if (v1 >= 0x0e60) and (v1 <= 0x0e8f)  #R3
      when 35  #n3
        idx = 36 if (v1 >= 0x0ec0) and (v1 <= 0x0eef)  #΂3
        idx = 37 if (v1 >= 0x0ef0) and (v1 <= 0x0f1f)  #3
      when 40  #4
        idx = 41 if (v1 >= 0x0fb0) and (v1 <= 0x0fdf)  #X4
        idx = 42 if (v1 >= 0x0fe0) and (v1 <= 0x100f)  #R4
      when 43  #n4
        idx = 44 if (v1 >= 0x1040) and (v1 <= 0x106f)  #΂4
        idx = 45 if (v1 >= 0x1070) and (v1 <= 0x109f)  #4
      end

      tile_set = "A"
      tile_x = (idx & 0x0007) + 1
      tile_y = ((idx & 0x00ff) / 8) + 1
      result = Ex.get_auto_battlebk( tile_set, tile_x, tile_y )
    end
    
    return result
  end
  #<--------------
end
