#==============================================================================
# ★ 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

