#==============================================================================
# ★ ExSkill_Escape
#------------------------------------------------------------------------------
# 　設定「逃跑」的技能或是物品
#==============================================================================

# 逃跑技能／物品的識別用文字。
# 當這行文字寫再 [備註] 裡時，代表可以逃跑。
# [效果範圍] 設為 [無]、[使用場合] 設為 [功能表中] 。
EXSKL_ESCAPE_SIGNATURE = "*ESCAPE"

# 表示可以逃跑的狀態開關 ID。
# 當這個編號的開關為 ON 時、則允許使用逃跑技能/物品。
# 事件進行中可以設置開關為OFF
EXSKL_ESCAPE_PERMISSION_SID = 2

# 指定逃跑場所的變數 ID。
# 指定逃跑開關狀態的變數 ID ：逃跑後，允許逃跑的開關狀態 (0: OFF 1: ON)、
# 再依序指定：地圖 ID、X 座標、Y 座標。
EXSKL_ESCAPE_VIDS = [11, 12, 13, 14]

# 使用時的 SE。
# 依次指定 SE 的名稱、音量、音調，。
# 若是不想要播放 SE ，就留[]
EXSKL_ESCAPE_SE = ["Teleport", 80, 100]

# 逃跑的位置設定、使用「腳本」指令來指定
# ※下面的座標使用變數來指定
# 
# ．隨意指定座標
# $game_party.set_escape_place(逃跑的狀態, 地圖 ID, X 座標, Y 座標)
# 
# ．將當前主角所在的地圖和座標記錄為逃跑位置
# switch = 0 # 或 1
# map_id = $game_map.map_id
# x = $game_player.x
# y = $game_player.y
# $game_party.set_escape_place(switch, map_id, x, y)

#------------------------------------------------------------------------------

class Game_Actor
  alias _exsesc_skill_can_use? skill_can_use?
  #--------------------------------------------------------------------------
  # ● 可以使用技能的判定 (追加定義)
  #     skill : 技能
  #--------------------------------------------------------------------------
  def skill_can_use?(skill)
    return false unless skill.is_a?(RPG::Skill)
    if skill.note.include?(EXSKL_ESCAPE_SIGNATURE)
      return false if $game_party.escape_place[0] == 0
      return false unless $game_switches[EXSKL_ESCAPE_PERMISSION_SID]
      return false if $game_player.in_vehicle?
    end
    return _exsesc_skill_can_use?(skill)
  end
end

class Game_Party
  alias _exsesc_item_can_use? item_can_use?
  #--------------------------------------------------------------------------
  # ○ 可以使用物品的能判定 (追加定義)
  #     item : 物品
  #--------------------------------------------------------------------------
  def item_can_use?(item)
    return false unless item.is_a?(RPG::Item)
    return false if item_number(item) == 0
    if item.note.include?(EXSKL_ESCAPE_SIGNATURE)
      return false if escape_place[0] == 0
      return false unless $game_switches[EXSKL_ESCAPE_PERMISSION_SID]
      return false if $game_player.in_vehicle?
    end
    return _exsesc_item_can_use?(item)
  end
  #--------------------------------------------------------------------------
  # ☆ 獲取逃跑的場所
  #--------------------------------------------------------------------------
  def escape_place
    map_id = $game_variables[EXSKL_ESCAPE_VIDS[1]]
    x = $game_variables[EXSKL_ESCAPE_VIDS[2]]
    y = $game_variables[EXSKL_ESCAPE_VIDS[3]]
    return [map_id, x, y]
  end
  #--------------------------------------------------------------------------
  # ☆ 設定逃跑的場所
  #     map_id : 地圖 ID
  #     x      : X 座標
  #     y      : Y 座標
  #--------------------------------------------------------------------------
  def set_escape_place(switch, map_id, x, y)
    $game_variables[EXSKL_ESCAPE_VIDS[0]] = switch
    $game_variables[EXSKL_ESCAPE_VIDS[1]] = map_id
    $game_variables[EXSKL_ESCAPE_VIDS[2]] = x
    $game_variables[EXSKL_ESCAPE_VIDS[3]] = y
  end
end

class Scene_Item
  alias _exsesc_determine_item determine_item
  #--------------------------------------------------------------------------
  # ○ 物品確認 (追加定義)
  #--------------------------------------------------------------------------
  def determine_item
    if @item.note.include?(EXSKL_ESCAPE_SIGNATURE)
      determine_escape
    else
      _exsesc_determine_item
    end
  end
  #--------------------------------------------------------------------------
  # ☆ 逃跑確認
  #--------------------------------------------------------------------------
  def determine_escape
    place = $game_party.escape_place
    if place == nil
      Sound.play_buzzer
    else
      se = EXSKL_ESCAPE_SE
      RPG::SE.new(se[0], se[1], se[2]).play unless se.empty?
      $game_party.last_item_id = @item.id
      $game_party.consume_item(@item)
      $game_player.reserve_transfer(place[0], place[1], place[2], 2)
      if $game_variables[EXSKL_ESCAPE_VIDS[0]] == 0
        $game_switches[EXSKL_ESCAPE_PERMISSION_SID] = false
      else
        $game_switches[EXSKL_ESCAPE_PERMISSION_SID] = true
      end
      $scene = Scene_Map.new
    end
  end
end

class Scene_Skill
  alias _exsesc_determine_skill determine_skill
  #--------------------------------------------------------------------------
  # ○ 技能確認 (追加定義)
  #--------------------------------------------------------------------------
  def determine_skill
    if @skill.note.include?(EXSKL_ESCAPE_SIGNATURE)
      determine_escape
    else
      _exsesc_determine_skill
    end
  end
  #--------------------------------------------------------------------------
  # ☆ 逃跑確認
  #--------------------------------------------------------------------------
  def determine_escape
    place = $game_party.escape_place
    if place == nil
      Sound.play_buzzer
    else
      se = EXSKL_ESCAPE_SE
      RPG::SE.new(se[0], se[1], se[2]).play unless se.empty?
      @actor.last_skill_id = @skill.id
      @actor.mp -= @actor.calc_mp_cost(@skill)
      $game_player.reserve_transfer(place[0], place[1], place[2], 2)
      if $game_variables[EXSKL_ESCAPE_VIDS[0]] == 0
        $game_switches[EXSKL_ESCAPE_PERMISSION_SID] = false
      else
        $game_switches[EXSKL_ESCAPE_PERMISSION_SID] = true
      end
      $scene = Scene_Map.new
    end
  end
end

