#============================================================================
#
# ■壁タイル拡張 □Ver.4.21 □製作者:月紳士
# ・RPGツクールVX用 RGSS2スクリプト
#
#    ●…書き換えメソッド(競合注意) ◎…メソッドのエイリアス ○…新規メソッド
#
#  ※二次配布禁止!配布元には利用規約があります。必ずそちらを見てください。
#------------------------------------------------------------------------------
# ※ このスクリプトは「段差マップ 通行判定拡張」をリネームしたものです。
#------------------------------------------------------------------------------
# 更新履歴
#   Ver.4.21  FUNCTION_TILEのIDに説明との誤りがあった為、修正。
#   Ver.4.20  イベント側からの接触判定に段違い処理を導入。
#   Ver.4.13  前回の修正より、エリア系処理が機能しなくなっていた不具合の修正。
#   Ver.4.12  ゲームロード時に特殊通行判定が無効になっていた不具合の修正。
#   Ver.4.11  影消しタイルの不具合修正。
#   Ver.4.10  マップデータの先読み機能、処理無視エリア設定の追加。
#   Ver.4.00  マップ移動時の処理軽量化と様々な不具合修正。
#
#   ※これ以前の更新履歴はマニュアルを参考にしてください。
#============================================================================
=begin
 
 このスクリプトは壁タイルの、主に通行判定を拡張し、
 次のような表現を可能にします。
 
 ○ 天井や屋根を歩くことが出来るようになる。
 
 ○ 壁・天井・屋根タイル・橋タイルの裏(下)へキャラが回り込む
 
 ※ 詳しい説明はマニュアルをご覧ください。
 
=end
#------------------------------------------------------------------------------
 
 
#==============================================================================
# □ カスタマイズ項目
#==============================================================================
 
module Expansion_Passable
 
  CLIFF_MAP = [4, 5]
  # 段差マップを使用するマップのID
 
  BACK_TILE_DEEP = false
  # 壁の深さを増すなら = true に。戻すなら = false に。
 
  BACK_SEEING = true
  # 裏回りした際、キャラを透過するなら = true に。しないなら = false に。
 
  SHADOW_ERASER_TILE_ID = 768
  # 「影消しタイル」にするタイルのIDをここで指定します。
  # このタイルが不要な際は = nil としてください。
 
  PROHIBITED_GET_OFF_TILE_ID = 769
  # 「接岸禁止」地点を示す為に使うタイルのIDをここで指定します。
  # このタイルが不要な際は = nil としてください。
 
  ADDITION_BACK_TILE_ID = 770
  # 「追加裏回り部分」を示す為に使うタイルのIDをここで指定します。
  # このタイルが不要な際は = nil としてください。
 
  DELETE_BACK_TILE_ID = 771
  # 「裏回り部分消去」を示す為に使うタイルのIDをここで指定します。
  # このタイルが不要な際は = nil としてください。
 
  ESCAPE_MAP = [99]
  # 裏回り処理自体をまったく行わないマップのID
  # 広いフィールドマップなどを、その読み込みを早める為に登録してください。
 
end
 
#==============================================================================
# □ タイル情報 ※カスタマイズ可能(上級者向け)
#==============================================================================
 
module Data_Tile
 
  # 擬似段差機能を使用する屋根・天井・地形タイル
  FUNCTION_TILE = [
   2816, 2960,                                     # 草原(フィールド用, 屋外用)
   3200, 3344,                                     # 乾燥地帯(フィールド用, 屋外用)
   3584, 3728,                                     # 砂漠(フィールド用, 屋外用)
   3968, 4112,                                     # 雪原(フィールド用, 屋外用)
   4352, 4400, 4448, 4496, 4544, 4592, 4640, 4688, # 屋根    (1列目)
   4736, 4784, 4832, 4880, 4928, 4976, 5024, 5072, # 建物の壁(1列目)
   5120, 5168, 5216, 5264, 5312, 5360, 5408, 5456, # 屋根    (2列目)
   5504, 5552, 5600, 5648, 5696, 5744, 5792, 5840, # 建物の壁(2列目)
   5888, 5936, 5984, 6032, 6080, 6128, 6176, 6224, # 洞窟の天井(1列目)
   6272, 6320, 6368, 6416, 6464, 6512, 6560, 6608, # 洞窟の壁  (1列目)
   6656, 6704, 6752, 6800, 6848, 6896, 6944, 6992, # 洞窟の天井(2列目)
   7040, 7088, 7136, 7184, 7232, 7280, 7328, 7376, # 洞窟の壁  (2列目)
   7424, 7472, 7520, 7568, 7616, 7664, 7712, 7760, # 洞窟の天井(3列目)
   7808, 7856, 7904, 7952, 8000, 8048, 8096, 8144, # 洞窟の壁  (3列目)
   1904, 1952, 2000]                               # ダミータイルID(消さないこと)
  # ※ 機能を使いたくないタイルがあればここから削ってください。
  #   (削り専用、付け加えても効果はありません)
 
  # 縦の橋としてあつかうタイルID
  BRIDRE_LENGTH = [5]
 
  # 横の橋としてあつかうタイルID
  BRIDGE_BREADTH = [18]
 
 
  # 階段・梯子・洞窟の入り口としてあつかうタイルID(ステップタイル)
  STEP = [67, 68, 69, 70, 71, 172, 173, 174, 175,
          260, 261, 262, 263, 268, 269, 270,
          271, 278, 279, 284, 285, 286, 287, 
          612, 613, 616, 617, 618, 619, 
          1600, 1601, 1602, 1603, 1604, 1605, 1606, 1607,
          1608, 1609, 1610, 1611, 1612, 1613, 1614, 1615]
  # ※ これらのタイルが隣接している時に、境目の通行不可判定を無視させます。
 
 
  # 橋の下の通行判定処理用 タイルID
  UNDER_BRIDGE = 1664
  # ※ デフォルトのID_1664はゲームでは使用されない、画像のないタイルです。
  #    必要がなければ変更しないで結構です。
 
end
 
#==============================================================================
# □ オートタイル情報 ※変更はしないでください。
#==============================================================================
 
module Data_Auto_Tile
 
  # 境目があるチップを方向ごとに配列化したもの↓(オートタイル内でのID)
  # 床タイプ
  VERGE_DOWN_A = [28, 29, 30, 31, 33, 38, 39, 40, 41, 43, 44, 45, 46]
  VERGE_LEFT_A = [16, 17, 18, 19, 32, 34, 35, 40, 41, 42, 43, 44, 46]
  VERGE_RIGHT_A = [24, 25, 26, 27, 32, 36, 37, 38, 39, 42, 44, 45, 46]
  VERGE_UP_A = [20, 21, 22, 23, 33, 34, 35, 36, 37, 42, 43, 45, 46]
 
  # 壁タイプ
  VERGE_DOWN_B = [8, 9, 10, 11, 12, 13, 14, 15]
  VERGE_LEFT_B = [1, 3, 5, 7, 9, 11, 13, 15]
  VERGE_RIGHT_B = [4, 5, 6, 7, 12, 13, 14, 15]
  VERGE_UP_B = [2, 3, 6, 7, 10, 11, 14, 15]
 
end
 
#==============================================================================
# ■ RPG::Area
#==============================================================================
 
class RPG::Area
  #--------------------------------------------------------------------------
  # ○ 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_accessor :ship_pass
  attr_accessor :disregard_processing 
end 
 
#==============================================================================
# ■ Game_Map
#------------------------------------------------------------------------------
#  マップを扱うクラスです。スクロールや通行可能判定などの機能を持っています。
# このクラスのインスタンスは $game_map で参照されます。
#==============================================================================
 
class Game_Map
  #--------------------------------------------------------------------------
  # ○ 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_reader   :map
  attr_reader   :ex_data
  attr_reader   :under_bridge_passages    # 橋の下の通行判定
  attr_reader   :back_tiles_position      # 裏回りする座標
  attr_reader   :keep_back_position       # 裏回りを維持する座標
  attr_reader   :prohibited_get_off       # 接岸禁止座標
  attr_reader   :ex_data                  # 拡張レイヤー用タイルデータ
  #--------------------------------------------------------------------------
  # ◎ セットアップ
  #     map_id : マップ ID
  #--------------------------------------------------------------------------
  alias tig_ew_setup setup
  def setup(map_id)
    tig_ew_setup(map_id)
    create_ex_passages
    pick_up_back_tiles
    setup_back_tiles
    $game_player.set_on_tile
    for event in $game_map.events.values
      event.update_turn_back
    end
  end
  #--------------------------------------------------------------------------
  # ○ 指定座標のタイルIDの取得
  #--------------------------------------------------------------------------
  def point_tile_id(x, y)
    return [data[x, y, 0], data[x, y, 1], data[x, y, 2]]
  end
  #--------------------------------------------------------------------------
  # ○ オートタイルIDの取得(オートタイル郡の中で順番にIDをつけた独自のIDです)
  #--------------------------------------------------------------------------
  def auto_(tile_id)
    return 0 unless (1904...8192).include?(tile_id)
    return (tile_id - 1904) % 48
  end
  #--------------------------------------------------------------------------
  # ○ トップタイルIDの取得(オートタイル郡の最初のID)
  #--------------------------------------------------------------------------
  def top_(tile_id)
    return 0 if tile_id == nil
    return (tile_id - auto_(tile_id))
  end
  #--------------------------------------------------------------------------
  # ○ スクリプト機能を使用する屋根・天井・壁タイルか?
  #--------------------------------------------------------------------------
  def ceiling?(tile_id)
    return false unless Data_Tile::FUNCTION_TILE.include?(top_(tile_id))
    return true if wall?(tile_id)
    return true if (2000...2048).include?(tile_id)
    return true if (4352...4736).include?(tile_id)
    return true if (5120...5504).include?(tile_id)
    return true if (5888...6272).include?(tile_id)
    return true if (6656...7040).include?(tile_id)
    return (7424...7808).include?(tile_id)
  end
  #--------------------------------------------------------------------------
  # ○ スクリプト機能を使用する壁タイルか?
  #--------------------------------------------------------------------------
  def wall?(tile_id)
    return false unless Data_Tile::FUNCTION_TILE.include?(top_(tile_id))
    return true if (1904...2000).include?(tile_id)
    return true if (4736...5120).include?(tile_id)
    return true if (5504...5888).include?(tile_id)
    return true if (6272...6656).include?(tile_id)
    return true if (7040...7424).include?(tile_id)
    return (7808...8192).include?(tile_id)
  end
  #--------------------------------------------------------------------------
  # ○ スクリプト機能を使用する地形タイルか?
  #--------------------------------------------------------------------------
  def cliff?(tile_id)
    return false unless Data_Tile::FUNCTION_TILE.include?(top_(tile_id))
    return [2816, 3200, 3584, 2960, 3344, 3728, 4112].include?(top_(tile_id))
  end
  #--------------------------------------------------------------------------
  # ○ 指定の向きにオートタイルチップの境目があるか?
  #--------------------------------------------------------------------------
  def verge?(tile_id, direction)
    auto_tile_id = auto_(tile_id)
 
    return false if auto_tile_id == 0
    return false unless ceiling?(tile_id) or cliff?(tile_id)
 
    if (1904...2000).include?(tile_id) or
       (4352...5888).include?(tile_id) or
       (6272...6656).include?(tile_id) or
       (7040...7424).include?(tile_id) or
       (7808...8192).include?(tile_id)           # オートタイルの線画タイプ判別
      case direction # 壁タイプの場合 方向ごとに振り分け
      when 2 ; return true if Data_Auto_Tile::VERGE_DOWN_B.include?(auto_tile_id)
      when 4 ; return true if Data_Auto_Tile::VERGE_LEFT_B.include?(auto_tile_id) 
      when 6 ; return true if Data_Auto_Tile::VERGE_RIGHT_B.include?(auto_tile_id) 
      when 8 ; return true if Data_Auto_Tile::VERGE_UP_B.include?(auto_tile_id) 
      end
    else
      case direction # 床タイプの場合 方向ごとに振り分け
      when 2 ; return true if Data_Auto_Tile::VERGE_DOWN_A.include?(auto_tile_id) 
      when 4 ; return true if Data_Auto_Tile::VERGE_LEFT_A.include?(auto_tile_id) 
      when 6 ; return true if Data_Auto_Tile::VERGE_RIGHT_A.include?(auto_tile_id) 
      when 8 ; return true if Data_Auto_Tile::VERGE_UP_A.include?(auto_tile_id)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ○ 指定の向きに有効な境目があるか?
  #--------------------------------------------------------------------------
  def rip?(direction, x, y, floating = false)
    #- 位置情報の準備
    tile_id = point_tile_id(x, y)
    front_x = x_with_direction(x, direction)
    front_y = y_with_direction(y, direction)
    front_tile_id = point_tile_id(front_x, front_y)
 
    #- ステップタイルがあれば無効
    return false if Data_Tile::STEP.include?(front_tile_id[0])
    return false if Data_Tile::STEP.include?(front_tile_id[2])
    return false if Data_Tile::STEP.include?(tile_id[0])
    return false if Data_Tile::STEP.include?(tile_id[2])
 
    #- 橋タイルの処理
    if floating
      if Data_Tile::BRIDRE_LENGTH.include?(tile_id[2]) or
         Data_Tile::BRIDGE_BREADTH.include?(tile_id[2])
        return false if @under_bridge_passages[[x, y]] == -1
      elsif Data_Tile::BRIDRE_LENGTH.include?(front_tile_id[2]) or
            Data_Tile::BRIDGE_BREADTH.include?(front_tile_id[2])
        return false if @under_bridge_passages[[front_x, front_y]] == -1  
      end
    end
 
    if bridge?(front_tile_id[2], direction)
      return false
    elsif Data_Tile::BRIDRE_LENGTH.include?(tile_id[2]) and back_tile?(x, y)
      return true if direction == 4 or direction == 6
      return false if direction == 2 or direction == 8
    elsif Data_Tile::BRIDGE_BREADTH.include?(tile_id[2]) and back_tile?(x, y)
      return true if direction == 2 or direction == 8
      return false if direction == 4 or direction == 6
    end
 
    #- 地形タイルの処理
    if cliff?(tile_id[0])
      if Expansion_Passable::CLIFF_MAP.include?($game_map.map_id)
        return false unless ceiling?(front_tile_id[0]) or cliff?(front_tile_id[0])
        if cliff?(front_tile_id[0]) and verge?(front_tile_id[0], 10 - direction)
          return false
        end
      else
        return false
      end
    end
 
    #- タイルに境目があるか?
    return verge?(tile_id[0], direction)
  end
  #--------------------------------------------------------------------------
  # ○ 指定の向きに準じた橋タイルか?(渡ることの出来る向きか)
  #--------------------------------------------------------------------------
  def bridge?(tile_id, direction)
    if direction == 2 or direction == 8
      return true if Data_Tile::BRIDRE_LENGTH.include?(tile_id)
    elsif direction == 4 or direction == 6
      return true if Data_Tile::BRIDGE_BREADTH.include?(tile_id)
    end
    return false
  end
  #--------------------------------------------------------------------------
  # ○ 上層に移すタイルか?
  #--------------------------------------------------------------------------
  def upper_layer?(tile_id)
    return false if @back_tiles_a == nil
    if Expansion_Passable::CLIFF_MAP.include?(@map_id)
      return true if @back_tiles_b.include?(tile_id)
    else
      unless cliff?(tile_id)
        return true if @back_tiles_a.include?(tile_id)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ○ <船の橋下通過>処理をするエリアか?
  #--------------------------------------------------------------------------
  def ship_pass_area?(x, y)
    result = false
    for area in $data_areas.values
      next if area == nil
      next unless area.ship_pass
      next if $game_map.map_id != area.map_id
      next if x < area.rect.x
      next if y < area.rect.y
      next if x >= area.rect.x + area.rect.width
      next if y >= area.rect.y + area.rect.height
      result = true
    end
    return result
  end
  #--------------------------------------------------------------------------
  # ○ 処理の無視をするエリアか?
  #--------------------------------------------------------------------------
  def disregard_processing_area?(x, y, map_id)
    result = false
    for area in $data_areas.values
      next if area == nil
      next unless area.disregard_processing
      next if map_id != area.map_id
      next if x < area.rect.x
      next if y < area.rect.y
      next if x >= area.rect.x + area.rect.width
      next if y >= area.rect.y + area.rect.height
      result = true
    end
    return result
  end
  #--------------------------------------------------------------------------
  # ○ 段差マップか?
  #--------------------------------------------------------------------------
  def cliff_map?
    return Expansion_Passable::CLIFF_MAP.include?(@map_id)
  end
  #--------------------------------------------------------------------------
  # ○ 特殊通行判定の作成
  #-------------------------------------------------------------------------- 
  def create_ex_passages
    @ex_passages = $data_system.passages
 
    #- 壁・屋根・天井タイルの通行判定を○に変える
    for i in 1904...2048
      @ex_passages[i] = 0x06 if ceiling?(i)
    end
    for i in 4352...8192
      @ex_passages[i] = 0x06 if ceiling?(i)
    end
 
    #- 橋タイルの通行判定を○に
    for i in (Data_Tile::BRIDRE_LENGTH + Data_Tile::BRIDGE_BREADTH)
      @ex_passages[i] = 0x00
    end
 
    @passages = @ex_passages
  end
  #--------------------------------------------------------------------------
  # ○ 接岸禁止タイル登録
  #     map_id : マップ ID
  #--------------------------------------------------------------------------
  def prohibited_get_off_set(x, y)
    @map.data[x, y, 2] = 0
    @prohibited_get_off.push([x, y])
  end
  #--------------------------------------------------------------------------
  # ○ 影消しタイル処理
  #--------------------------------------------------------------------------
  def shadow_eraser(x, y)
    r_x = round_x(x - 1) ; r_y = round_y(y - 1)
    tile_id = @map.data[r_x, y, 0]
 
    # 壁(線画・壁タイプ)
    if (4736...5120).include?(tile_id) or
       (5504...5888).include?(tile_id) or
       (6272...6656).include?(tile_id) or
       (7040...7424).include?(tile_id) or
       (7808...8192).include?(tile_id)
      dummy_tile_id = 1904 + auto_(tile_id)
    # 屋根(線画・壁タイプ)
    elsif (4352...4736).include?(tile_id) or
          (5120...5504).include?(tile_id)
      dummy_tile_id = 1952 + auto_(tile_id)
    # 天井(線画・床タイプ)
    elsif (5888...6272).include?(tile_id) or
          (6656...7040).include?(tile_id) or
          (7424...7808).include?(tile_id)
      dummy_tile_id = 2000 + auto_(tile_id)
    else
      return
    end
 
    @map.data[r_x, r_y, 1] = @map.data[r_x, r_y, 0]
    @map.data[r_x, r_y, 0] = dummy_tile_id
  end
  #--------------------------------------------------------------------------
  # ○ 裏回り消去タイル処理
  #--------------------------------------------------------------------------
  def back_tiles_position_delete(x, y)
    return if Expansion_Passable::DELETE_BACK_TILE_ID == nil
    y = round_y(y+1) if Expansion_Passable::BACK_TILE_DEEP
    if @map.data[x, y, 2] == Expansion_Passable::DELETE_BACK_TILE_ID or
       @ex_data[x, y, 2] == Expansion_Passable::DELETE_BACK_TILE_ID 
      @back_tiles_position.delete([x, y])
      @map.data[x, y, 2] = 0
      @ex_data[x, y, 2] = 0
    end
  end
  #--------------------------------------------------------------------------
  # ○ 裏回り用タイル種類ピックアップ
  #--------------------------------------------------------------------------  
  def pick_up_back_tiles
    return if @back_tiles_a != nil
 
    @back_tiles_a = []
    @back_tiles_b = []
    for i in 1904...8192
      next unless ceiling?(i) or cliff?(i)
      if wall?(i)
        next if verge?(i, 2)
        if verge?(i, 4) or verge?(i, 6)
          @back_tiles_a.push(i) unless cliff?(i)
        end
      else
        @back_tiles_a.push(i) unless cliff?(i)
        @back_tiles_b.push(i) if verge?(i, 8)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ○ 通行判定初期化と裏回り用タイル座標ピックアップ
  #--------------------------------------------------------------------------
  def setup_back_tiles
    if $game_player.game_map_data.include?(@map_id)
      @map = $game_player.game_map_data[@map_id].map
      @ex_data = $game_player.game_map_data[@map_id].ex_data
      @under_bridge_passages = $game_player.game_map_data[@map_id].under_bridge_passages
      @back_tiles_position = $game_player.game_map_data[@map_id].back_tiles_position
      @keep_back_position = $game_player.game_map_data[@map_id].keep_back_position
      @prohibited_get_off = $game_player.game_map_data[@map_id].prohibited_get_off
      return
    end
 
    @ex_data = Table.new(width, height, 3)
    @under_bridge_passages = {}
    @back_tiles_position = []
    @keep_back_position = []
    @prohibited_get_off = []
 
    return if Expansion_Passable::ESCAPE_MAP.include?(@map_id)
 
    #- 座標毎処理
    for x in 0...width
      for reverse_y in 1..height
        y = height - reverse_y   # Y座標を逆順にする為の処理
        next if disregard_processing_area?(x, y, @map_id)
 
        tile_id = point_tile_id(x, y)
 
        #- 橋タイル処理
        if (Data_Tile::BRIDRE_LENGTH + Data_Tile::BRIDGE_BREADTH).include?(tile_id[2])
          if (2048...2815).include?(tile_id[0])
            if ship_pass_area?(x, y)
              @under_bridge_passages[[x, y]] = 1
              @back_tiles_position.push([x, y])
              @ex_data[x, y, 2] = @map.data[x, y, 2]
            else
              @back_tiles_position.push([x, y])
              @under_bridge_passages[[x, y]] = -1
            end
          elsif @passages[tile_id[0]] != 0x06 or $game_map.wall?(tile_id[0])
            @under_bridge_passages[[x, y]] = -1
            @back_tiles_position.push([x, y])
            @ex_data[x, y, 2] = @map.data[x, y, 2]
          else
            @under_bridge_passages[[x, y]] = 0
            @back_tiles_position.push([x, y])
            @ex_data[x, y, 2] = @map.data[x, y, 2]
          end
          if Data_Tile::BRIDGE_BREADTH.include?(tile_id[2])
            @keep_back_position.push([x, round_y(y+1)])
          end
 
        #- 通行判定☆タイルを上層に移動
        elsif tile_id[2] != 0 and @passages[tile_id[2]] & 0x10 == 0x10
          if @back_tiles_position.include?([x, round_y(y+1)])
            @keep_back_position.push([x, y])
          else
            @ex_data[x, y, 2] = @map.data[x, y, 2]
          end
        end
 
        #- 影消しタイルの処理
        if @map.data[x, y, 2] == Expansion_Passable::SHADOW_ERASER_TILE_ID
          shadow_eraser(x, y)
          @map.data[x, y, 2] = 0
        #- 接岸禁止タイルの処理
        elsif @map.data[x, y, 2] == Expansion_Passable::PROHIBITED_GET_OFF_TILE_ID
          prohibited_get_off_set(x, y)
        end
 
        #- 壁・天井タイル処理
        if ceiling?(tile_id[0]) or (cliff?(tile_id[0]) and cliff_map?)
 
          #- 基本となる裏回りタイルの場合、裏回り位置に登録
          if not wall?(tile_id[0]) and verge?(tile_id[0], 8)
            @back_tiles_position.push([x, y])
          end
 
          #- 裏回り位置にするタイルの場合、上層に移動
          back_tiles = cliff_map? ? @back_tiles_b : @back_tiles_a
          if back_tiles.include?(tile_id[0])
            @ex_data[x, y, 0] = @map.data[x, y, 0]
            @ex_data[x, y, 1] = @map.data[x, y, 1]
            #- 裏回り位置追加タイルの誤用時には、タイルを消去する
            if tile_id[2] != Expansion_Passable::ADDITION_BACK_TILE_ID
              @ex_data[x, y, 2] = @map.data[x, y, 2]
            end
            @map.data[x, y, 1] = 0
            @map.data[x, y, 2] = 0
          end
 
          #- 深く裏回りする設定の際の処理(裏回り位置の下をさらに上層に)
          if Expansion_Passable::BACK_TILE_DEEP
            unless @back_tiles_position.include?([x, round_y(y+1)])
              if not wall?(tile_id[0]) and verge?(tile_id[0], 8)
                unless wall?(data[x, round_y(y+1), 0]) and verge?(data[x, round_y(y+1), 0], 2)
                  @back_tiles_position.push([x, round_y(y+1)])
                  @ex_data[x, round_y(y+1), 0] = @map.data[x, round_y(y+1), 0]
                  @ex_data[x, round_y(y+1), 1] = @map.data[x, round_y(y+1), 1]
                  @ex_data[x, round_y(y+1), 2] = @map.data[x, round_y(y+1), 2]
                  @map.data[x, round_y(y+1), 1] = 0
                  @map.data[x, round_y(y+1), 2] = 0
                end
              end
            end
          end
 
          #- 裏回り追加タイルの処理
          if Expansion_Passable::ADDITION_BACK_TILE_ID != nil and
             tile_id[2] == Expansion_Passable::ADDITION_BACK_TILE_ID
            @back_tiles_position.push([x, y])
            @ex_data[x, y, 0] = @map.data[x, y, 0]
            @ex_data[x, y, 1] = @map.data[x, y, 1]
            @map.data[x, y, 2] = 0
            if (2816...8192).include?(@map.data[x, round_y(y-1), 0])
              @ex_data[x, round_y(y-1), 0] = @map.data[x, round_y(y-1), 0]
              @ex_data[x, round_y(y-1), 1] = @map.data[x, round_y(y-1), 1]
            end
          end
        end
 
        #- 裏回り部分除去タイルの処理
        back_tiles_position_delete(x, y)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ○ 裏回り用タイルの存在する座標か?
  #--------------------------------------------------------------------------
  def back_tile?(x, y)
    return false if @back_tiles_position == nil
    return @back_tiles_position.include?([x, y])
  end
end
 
#==============================================================================
# ■ Game_Character
#------------------------------------------------------------------------------
#  キャラクターを扱うクラスです。このクラスは Game_Player クラスと Game_Event
# クラスのスーパークラスとして使用されます。
#==============================================================================
 
class Game_Character
  #--------------------------------------------------------------------------
  # ○ 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_reader   :on_tile
  attr_reader   :turn_back 
  attr_accessor :no_turn_back
  attr_reader   :back_seeing
  attr_reader   :not_back_seeing
  #--------------------------------------------------------------------------
  # ◎ オブジェクト初期化
  #--------------------------------------------------------------------------
  alias tig_ew_initialize initialize
  def initialize
    tig_ew_initialize
    @vehicle_type = -1
    @turn_back = false
    @no_turn_back = false
    @on_tile = true
    @back_seeing = false
    @not_back_seeing = false
  end
  #--------------------------------------------------------------------------
  # ◎ フレーム更新
  #--------------------------------------------------------------------------
  alias tig_ew_update update
  def update
    if @first_update == nil
      update_turn_back
      @first_update = true
    end
    tig_ew_update
  end
  #--------------------------------------------------------------------------
  # ◎ 指定位置に移動
  #     x : X 座標
  #     y : Y 座標
  #--------------------------------------------------------------------------
  alias tig_ew_moveto moveto
  def moveto(x, y)
    tig_ew_moveto(x, y)
    update_turn_back
  end
  #--------------------------------------------------------------------------
  # ◎ ジャンプ時の更新
  #--------------------------------------------------------------------------
  alias tig_ew_update_jump update_jump
  def update_jump
    tig_ew_update_jump
    update_turn_back
  end
  #--------------------------------------------------------------------------
  # ◎ 移動時の更新
  #--------------------------------------------------------------------------
  alias tig_ew_update_move update_move
  def update_move
    tig_ew_update_move
    update_turn_back unless moving?
  end
  #--------------------------------------------------------------------------
  # ◎ 歩数増加
  #--------------------------------------------------------------------------
  alias tig_ew_increase_steps increase_steps
  def increase_steps
    tig_ew_increase_steps
    update_turn_back
  end
  #--------------------------------------------------------------------------
  # ○ 2座標の位置関係を向きで取得
  #--------------------------------------------------------------------------
  def search_direction(start_x, start_y, arriv_x, arriv_y)
    if start_x == arriv_x
      if start_y == arriv_y
        return 5
      elsif start_y < arriv_y
        return 2
      elsif start_y > arriv_y
        return 8
      end
    elsif start_x < arriv_x
      if start_y == arriv_y
        return 6
      elsif start_y < arriv_y
        return 3
      elsif start_y > arriv_y
        return 9
      end
    elsif start_x > arriv_x
      if start_y == arriv_y
        return 4
      elsif start_y < arriv_y
        return 1
      elsif start_y > arriv_y
        return 7
      end
    end
  end
  #--------------------------------------------------------------------------
  # ○ 裏回りセット
  #--------------------------------------------------------------------------
  def set_turn_back
    @turn_back = true
    @on_tile = false
  end
  #--------------------------------------------------------------------------
  # ○ 天井載せセット
  #--------------------------------------------------------------------------
  def set_on_tile
    @turn_back = false
    @on_tile = true
  end
  #--------------------------------------------------------------------------
  # ○ 裏回り状態更新
  #--------------------------------------------------------------------------
  def update_turn_back
    tile_id = $game_map.point_tile_id(@x, @y)
    direction = search_direction(@real_x, @real_y, @x * 256, @y * 256)
    if self.is_a?(Game_Player) and [2, 4].include?(@vehicle_type)
      @turn_back = false
      @no_turn_back = false
      @on_tile = true
      return
    end
    #- 段差マップの場合
    if Expansion_Passable::CLIFF_MAP.include?($game_map.map_id)
      #- 停止時の更新
      if direction == 5
        @turn_back = false if @no_turn_back
        unless $game_map.back_tile?(@x, @y)
          @turn_back = false
          @no_turn_back = false
        end
        @on_tile = true unless @turn_back
      #- 移動開始時の更新 
      else
        if direction % 2 == 0
          if $game_map.rip?(10 - direction, @x, @y, floating?)
            @turn_back = true unless @no_turn_back
          end
        else
          if diagonal_move_turn_back(direction, @x, @y)
            @turn_back = true unless @no_turn_back
          end
        end
        @on_tile = false if @turn_back
      end
      if not @turn_back or @no_turn_back
        if $game_map.keep_back_position != nil and $game_map.keep_back_position.include?([@x, @y])
          @on_tile = false
          return
        end
      end
    #- 通常マップの場合
    else
      #- 停止時の更新
      if direction == 5
        unless $game_map.back_tile?(@x, @y)
          @turn_back = false
          @no_turn_back = false
        end
        if $game_map.upper_layer?(tile_id[0])
          @on_tile = true
        else
          @on_tile = false
        end
      #- 移動開始時の更新
      else
        if direction % 2 == 0
          if $game_map.rip?(10 - direction, @x, @y, floating?)
            @turn_back = true unless @no_turn_back
          end
        else
          if diagonal_move_turn_back(direction, @x, @y)
            @turn_back = true unless @no_turn_back
          end
        end
      end
      if $game_map.back_tile?(@x, @y)
        if @turn_back
          @on_tile = false
        else
          @on_tile = true
        end
      elsif Data_Tile::STEP.include?(tile_id[0]) or Data_Tile::STEP.include?(tile_id[2])
        @on_tile = true
      end
    end
  end
  #--------------------------------------------------------------------------
  # ○ 斜め移動時の裏回り状態判別
  #--------------------------------------------------------------------------
  def diagonal_move_turn_back(direction, x, y)
    turn_back = false
    case direction
    when 1
      turn_back = true if $game_map.rip?(8, x+1, y, floating?)
      turn_back = true if $game_map.rip?(6, x, y, floating?)
      turn_back = true if $game_map.rip?(6, x, y-1, floating?)
      turn_back = true if $game_map.rip?(8, x, y, floating?)
    when 3
      turn_back = true if $game_map.rip?(8, x-1, y, floating?)
      turn_back = true if $game_map.rip?(4, x, y, floating?)
      turn_back = true if $game_map.rip?(4, x, y-1, floating?)
      turn_back = true if $game_map.rip?(8, x, y, floating?)
    when 7
      turn_back = true if $game_map.rip?(6, x, y+1, floating?)
      turn_back = true if $game_map.rip?(2, x, y, floating?)
      turn_back = true if $game_map.rip?(2, x+1, y, floating?)
      turn_back = true if $game_map.rip?(6, x, y, floating?)
    when 9
      turn_back = true if $game_map.rip?(4, x, y+1, floating?)
      turn_back = true if $game_map.rip?(2, x, y, floating?)
      turn_back = true if $game_map.rip?(2, x-1, y, floating?)
      turn_back = true if $game_map.rip?(4, x, y, floating?)
    end
    return turn_back
  end
  #--------------------------------------------------------------------------
  # ○ この場が裏か判定
  #--------------------------------------------------------------------------
  def back_tile?(pos)
    return $game_map.back_tile?(pos[0], pos[1])
  end
  #--------------------------------------------------------------------------
  # ○ 橋の下の通行判定
  #--------------------------------------------------------------------------  
  def under_bridge_passages(pos)
    return true unless $game_map.under_bridge_passages.key?(pos)
    case @vehicle_type
    when -1
      return $game_map.under_bridge_passages[pos] == 0
    when 0..1
      return $game_map.under_bridge_passages[pos] > 0
    when 5
      return $game_map.under_bridge_passages[pos] >= 0
    else
      return false
    end
  end
  #--------------------------------------------------------------------------
  # ○ 浮かんでいるか?(乗り物拡張用・魔法の絨毯搭乗時)
  #--------------------------------------------------------------------------  
  def floating?
    return false
  end
  #--------------------------------------------------------------------------
  # ○ 壁タイル拡張通行判定
  #--------------------------------------------------------------------------
  #  x , y を省略すると…
  #    プレイヤーの現在位置(始点)からdirection方向へ通過出来るかどうかを判定。
  #  x , y を代入すると…
  #    その座標(終点)へdirection方向から進入できるかどうかを判定。
  #--------------------------------------------------------------------------
  def ex_passable(direction, x = nil, y = nil, turn_back = nil)
    return true if debug_through?        # デバッグ動作中なら通行可
    return true if @through              # すり抜け中なら通行可
 
    #- 位置情報の準備
    if x == nil
      start_pos = [@x, @y]
      arriv_pos = [$game_map.x_with_direction(@x, direction), $game_map.y_with_direction(@y, direction)]  
    else
      start_pos = [$game_map.x_with_direction(x, 10 - direction), $game_map.y_with_direction(y, 10 - direction)]
      arriv_pos = [x, y]
    end
    start_tile = $game_map.point_tile_id(start_pos[0], start_pos[1])
    arriv_tile = $game_map.point_tile_id(arriv_pos[0], arriv_pos[1])
    start_side_rip = $game_map.rip?(direction, start_pos[0], start_pos[1], floating?)
    arriv_side_rip = $game_map.rip?(10 - direction, arriv_pos[0], arriv_pos[1], floating?)
 
    turn_back = @turn_back if turn_back == nil
 
    #- 階段タイルなら通行可
    unless back_tile?(start_pos) and turn_back 
      return true if Data_Tile::STEP.include?(arriv_tile[0])
      return true if Data_Tile::STEP.include?(arriv_tile[2])
      unless $game_map.wall?(arriv_tile[0])
        return true if Data_Tile::STEP.include?(start_tile[0])
        return true if Data_Tile::STEP.include?(start_tile[2])
      end
    end
 
    #- 境目&橋の下の通行判定
    if back_tile?(start_pos) and turn_back               # 始点が裏かつ裏回り中
      return false unless under_bridge_passages(arriv_pos)  # 橋の下の通行判定
      unless back_tile?(arriv_pos)                         # 終点が裏でない 
        if start_side_rip                                   # 始点側に境目がある
          if arriv_side_rip                                  # 終点側に境目がある
            return false
          else
            return false if $game_map.wall?(arriv_tile[0])
          end
        else                                              # 始点側に境目がない 
          return false
        end
      end
    else                                                  # 始点は裏でない
      unless $game_map.bridge?(arriv_tile[2], direction) or # 橋がなければ
             $game_map.bridge?(start_tile[2], direction)
        if start_side_rip                                  # 始点側に境目がある
          return false
        else                                               # 始点側に境目がない
          if arriv_side_rip                                 # 終点側に境目がある
            if back_tile?(arriv_pos)                         # 終点が裏
              unless $game_map.bridge?(start_tile[2], direction)   # この場が橋でなければ
                return false unless under_bridge_passages(arriv_pos)  # 橋の下の通行判定
                turn_back = true
              end
            else                                             # 終点が裏でない
              unless $game_map.bridge?(start_tile[2], direction) # この場が橋でなければ
                return false
              end
            end
          else                                              # 終点側に境目がない
            return false if $game_map.wall?(arriv_tile[0])
          end
        end
      end
    end
 
    #- 上層の通行判定
    if $game_map.upper_layer?(arriv_tile[0]) and not turn_back 
      for i in [2, 1, 0]                        # レイヤーの上から順に調べる
        tile_id = $game_map.ex_data[arriv_pos[0], arriv_pos[1], i] # タイル ID を取得
        unless tile_id == nil
          pass = $game_map.passages[tile_id]    # 通行属性を取得
          next if pass & 0x10 == 0x10           # [☆] : 通行に影響しない
          next if pass & 0x01 == 0x00           # [○] : 通行可
          if pass & 0x01 == 0x01                # [×] : 通行不可
            return false
          end
        end
      end
    end  
 
    return true
  end
  #--------------------------------------------------------------------------
  # ◎ 下に移動
  #     turn_ok : その場での向き変更を許可
  #--------------------------------------------------------------------------
  alias tig_ew_move_down move_down
  def move_down(turn_ok = true)
    unless ex_passable(2)
      turn_down if turn_ok
      @move_failed = true
      check_event_trigger_touch(@x, @y+1)   # 接触イベントの起動判定
      return
    end
    tig_ew_move_down(turn_ok)
  end
  #--------------------------------------------------------------------------
  # ◎ 左に移動
  #     turn_ok : その場での向き変更を許可
  #--------------------------------------------------------------------------
  alias tig_ew_move_left move_left 
  def move_left(turn_ok = true)
    unless ex_passable(4)
      turn_left if turn_ok
      @move_failed = true
      check_event_trigger_touch(@x-1, @y)   # 接触イベントの起動判定
      return
    end
    tig_ew_move_left(turn_ok)
  end
  #--------------------------------------------------------------------------
  # ◎ 右に移動
  #     turn_ok : その場での向き変更を許可
  #--------------------------------------------------------------------------
  alias tig_ew_move_right move_right
  def move_right(turn_ok = true)
    unless ex_passable(6)
      turn_right if turn_ok
      check_event_trigger_touch(@x+1, @y)   # 接触イベントの起動判定
      @move_failed = true
      return
    end
    tig_ew_move_right(turn_ok)
  end
  #--------------------------------------------------------------------------
  # ◎ 上に移動
  #     turn_ok : その場での向き変更を許可
  #--------------------------------------------------------------------------
  alias tig_ew_move_up move_up
  def move_up(turn_ok = true)
    unless ex_passable(8)
      turn_up if turn_ok
      check_event_trigger_touch(@x, @y-1)   # 接触イベントの起動判定
      @move_failed = true
      return
    end
    tig_ew_move_up(turn_ok)
  end
  #--------------------------------------------------------------------------
  # ◎ 左下に移動
  #--------------------------------------------------------------------------
  alias tig_ew_move_lower_left move_lower_left
  def move_lower_left
    turn_back1 = $game_map.rip?(8, @x, @y+1) ? true : @turn_back
    turn_back2 = $game_map.rip?(6, @x-1, @y) ? true : @turn_back
    unless passable?(@x, @y+1) and passable?(@x-1, @y) and passable?(@x-1, @y+1) and
           ex_passable(2) and ex_passable(4, @x-1, @y+1, turn_back1) and
           ex_passable(4) and ex_passable(2, @x-1, @y+1, turn_back2)
      if @direction == 2
        if passable?(@x-1, @y) and ex_passable(4)
          move_left
        else
          move_down
        end
      elsif @direction == 4
        if passable?(@x, @y+1) and ex_passable(2)
          move_down
        else
          move_left
        end
      end
      return
    end
    tig_ew_move_lower_left
  end
  #--------------------------------------------------------------------------
  # ◎ 右下に移動
  #--------------------------------------------------------------------------
  alias tig_ew_move_lower_right move_lower_right
  def move_lower_right
    turn_back1 = $game_map.rip?(8, @x, @y+1) ? true : @turn_back
    turn_back2 = $game_map.rip?(4, @x+1, @y) ? true : @turn_back
    unless passable?(@x, @y+1) and passable?(@x+1, @y) and passable?(@x+1, @y+1) and
           ex_passable(2) and ex_passable(6, @x+1, @y+1, turn_back1) and
           ex_passable(6) and ex_passable(2, @x+1, @y+1, turn_back2)
      if @direction == 2
        if passable?(@x-1, @y) and ex_passable(6)
          move_right
        else
          move_down
        end
      elsif @direction == 6
        if passable?(@x, @y+1) and ex_passable(2)
          move_down
        else
          move_right
        end
      end
      return
    end
    tig_ew_move_lower_right
  end
  #--------------------------------------------------------------------------
  # ◎ 左上に移動
  #--------------------------------------------------------------------------
  alias tig_ew_move_upper_left move_upper_left
  def move_upper_left
    turn_back1 = $game_map.rip?(2, @x, @y-1) ? true : @turn_back
    turn_back2 = $game_map.rip?(6, @x-1, @y) ? true : @turn_back
    unless passable?(@x, @y-1) and passable?(@x-1, @y) and passable?(@x-1, @y-1) and
           ex_passable(8) and ex_passable(4, @x-1, @y-1, turn_back1) and
           ex_passable(4) and ex_passable(8, @x-1, @y-1, turn_back2)
      if @direction == 8
        if passable?(@x-1, @y) and ex_passable(4)
          move_left
        else
          move_up
        end
      elsif @direction == 4
        if passable?(@x, @y+1) and ex_passable(8)
          move_up
        else
          move_left
        end
      end
      return
    end
    tig_ew_move_upper_left
  end
  #--------------------------------------------------------------------------
  # ◎ 右上に移動
  #--------------------------------------------------------------------------
  alias tig_ew_move_upper_right move_upper_right
  def move_upper_right
    turn_back1 = $game_map.rip?(2, @x, @y-1) ? true : @turn_back
    turn_back2 = $game_map.rip?(4, @x+1, @y) ? true : @turn_back
    unless passable?(@x, @y-1) and passable?(@x+1, @y) and passable?(@x+1, @y-1) and
           ex_passable(8) and ex_passable(6, @x+1, @y-1, turn_back1) and
           ex_passable(6) and ex_passable(8, @x+1, @y-1, turn_back2)
      if @direction == 8
        if passable?(@x+1, @y) and ex_passable(6)
          move_right
        else
          move_up
        end
      elsif @direction == 6
        if passable?(@x, @y-1) and ex_passable(8)
          move_up
        else
          move_right
        end
      end
      return
    end
    tig_ew_move_upper_right
  end
  #--------------------------------------------------------------------------
  # ● キャラクター衝突判定
  #     x : X 座標
  #     y : Y 座標
  #    プレイヤーと乗り物を含め、通常キャラの衝突を検出する。
  #--------------------------------------------------------------------------
  def collide_with_characters?(x, y)
    for event in $game_map.events_xy(x, y)          # イベントの座標と一致
      next unless parallel?(event)                  ##### 追加部分 (この行) #####
      unless event.through                          # すり抜け OFF?
        return true if self.is_a?(Game_Event)       # 自分がイベント
        return true if event.priority_type == 1     # 相手が通常キャラ
      end
    end
    if @priority_type == 1                          # 自分が通常キャラ
      if parallel?($game_player)                   ##### 追加部分 (この行) #####
        return true if $game_player.pos_nt?(x, y)  # プレイヤーの座標と一致
      end                                          ##### 追加部分 (この行) #####
      return true if $game_map.boat.pos_nt?(x, y)   # 小型船の座標と一致
      return true if $game_map.ship.pos_nt?(x, y)   # 大型船の座標と一致
    end
    return false
  end
  #--------------------------------------------------------------------------
  # ○ キャラクターの同軸存在判定
  #--------------------------------------------------------------------------
  def parallel?(character)
    direction = search_direction(@x, @y, character.x, character.y)
    turn_back = @turn_back
    character_turn_back = character.turn_back
    if direction != 5
      if direction % 2 == 0
        if $game_map.rip?(10 - direction, character.x, character.y)
          turn_back = true
        end
      else
        if diagonal_move_turn_back(direction, character.x, character.y)
          turn_back = true
        end
      end
      if direction % 2 == 0
        if $game_map.rip?(direction, @x, @y)
          character_turn_back = true
        end
      else
        if diagonal_move_turn_back(10 - direction, @x, @y)
          character_turn_back = true
        end
      end
      tile_id = $game_map.data[character.x, character.y, 0]
      if $game_map.wall?(tile_id)
        return true unless ex_passable(direction)
      end
    end
 
    return turn_back == character_turn_back
  end
end
 
#==============================================================================
# ■ Game_Event
#------------------------------------------------------------------------------
#  イベントを扱うクラスです。条件判定によるイベントページ切り替えや、並列処理
# イベント実行などの機能を持っており、Game_Map クラスの内部で使用されます。
#==============================================================================
 
class Game_Event < Game_Character
  #--------------------------------------------------------------------------
  # ◎ オブジェクト初期化
  #     map_id : マップ ID
  #     event  : イベント (RPG::Event)
  #--------------------------------------------------------------------------
  alias tig_ew_event_initialize initialize
  def initialize(map_id, event)
    tig_ew_event_initialize(map_id, event)
    set_turn_back_setting
  end
  #--------------------------------------------------------------------------
  # ○ イベントの初期配置位置取得(裏配置)
  #--------------------------------------------------------------------------  
  def set_turn_back_setting
    @event.name = @event.name.sub(/[\<<]裏配置[\>>]/, "") 
    @turn_back = $& != nil
    @on_tile = $& == nil
    @event.name = @event.name.sub(/[\<<]裏透過禁止[\>>]/, "") 
    @not_back_seeing = $& != nil
  end
  #--------------------------------------------------------------------------
  # ● 接触イベントの起動判定
  #--------------------------------------------------------------------------
  def check_event_trigger_touch(x, y)
    return if $game_map.interpreter.running?
    if @trigger == 2 and $game_player.pos?(x, y)
      if @character_name != ""                     #### 追加部分 (ここから) ####
        return unless $game_player.parallel?(self)              
      end                                          #### 追加部分 (ここまで) ####
      start if not jumping? and @priority_type == 1
    end
  end
end
 
#==============================================================================
# ■ Game_Vehicle
#------------------------------------------------------------------------------
#  乗り物を扱うクラスです。このクラスは Game_Map クラスの内部で使用されます。
# 現在のマップに乗り物がないときは、マップ座標 (-1,-1) に設定されます。
#==============================================================================
 
class Game_Vehicle < Game_Character 
  #--------------------------------------------------------------------------
  # ○ タイル表示(on_tile)状態取得
  #-------------------------------------------------------------------------- 
  def on_tile
    if @driving or @altitude > 0
      return $game_player.on_tile
    else
      return false
    end
  end
  #--------------------------------------------------------------------------
  # ◎ プレイヤーとの同期
  #--------------------------------------------------------------------------
  alias tig_ew_sync_with_player sync_with_player
  def sync_with_player
    tig_ew_sync_with_player
    update_turn_back
  end  
end
 
#==============================================================================
# ■ Game_Player
#------------------------------------------------------------------------------
#  プレイヤーを扱うクラスです。イベントの起動判定や、マップのスクロールなどの
# 機能を持っています。このクラスのインスタンスは $game_player で参照されます。
#==============================================================================
 
class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # ○ 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_accessor :game_map_data
  attr_accessor :reserve_load_map_id
  #--------------------------------------------------------------------------
  # ◎ オブジェクト初期化
  #     map_id : マップ ID
  #     event  : イベント (RPG::Event)
  #--------------------------------------------------------------------------
  alias tig_ew_pl_initialize initialize
  def initialize
    tig_ew_pl_initialize
    @reserve_load_map_id = []
    @game_map_data = {}
  end
  #--------------------------------------------------------------------------
  # ○ マップデータの先読み
  #--------------------------------------------------------------------------
  def beforehand_load(map_id = @reserve_load_map_id)
    for i in map_id
      next if @game_map_data.key?(i)
      map_data = Game_Map.new
      map_data.setup(i)
      @game_map_data[i] = map_data
    end
    for i in @game_map_data.keys.clone
      unless map_id.include?(i)
        @game_map_data.delete(i)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ◎ 場所移動の実行
  #--------------------------------------------------------------------------
  alias tig_ew_perform_transfer perform_transfer
  def perform_transfer
    return unless @transferring
    unless $game_player.reserve_load_map_id.empty?
      $game_player.beforehand_load
    end
    tig_ew_perform_transfer
  end
  #--------------------------------------------------------------------------
  # ○ 浮かんでいるか?(乗り物拡張用・魔法の絨毯搭乗時)
  #--------------------------------------------------------------------------  
  def floating?
    return @vehicle_type == 5
  end
  #--------------------------------------------------------------------------
  # ● 同位置のイベント起動判定
  #     triggers : トリガーの配列
  #--------------------------------------------------------------------------
  def check_event_trigger_here(triggers)
    return false if $game_map.interpreter.running?
    result = false
    for event in $game_map.events_xy(@x, @y)
      next unless parallel?(event)                 ##### 追加部分 (この行) #####
      if triggers.include?(event.trigger) and event.priority_type != 1
        event.start
        result = true if event.starting
      end
    end
    return result
  end
  #--------------------------------------------------------------------------
  # ● 正面のイベント起動判定
  #     triggers : トリガーの配列
  #--------------------------------------------------------------------------
  def check_event_trigger_there(triggers)
    return false if $game_map.interpreter.running?
    result = false
    front_x = $game_map.x_with_direction(@x, @direction)
    front_y = $game_map.y_with_direction(@y, @direction)
    for event in $game_map.events_xy(front_x, front_y)
      next unless parallel?(event)                 ##### 追加部分 (この行) #####
      if triggers.include?(event.trigger) and event.priority_type == 1
        event.start
        result = true
      end
    end
    if result == false and $game_map.counter?(front_x, front_y)
      front_x = $game_map.x_with_direction(front_x, @direction)
      front_y = $game_map.y_with_direction(front_y, @direction)
      for event in $game_map.events_xy(front_x, front_y)
        next unless parallel?(event)               ##### 追加部分 (この行) #####
        if triggers.include?(event.trigger) and event.priority_type == 1
          event.start
          result = true
        end
      end
    end
    return result
  end
  #--------------------------------------------------------------------------
  # ● 接触イベントの起動判定
  #     x : X 座標
  #     y : Y 座標
  #--------------------------------------------------------------------------
  def check_event_trigger_touch(x, y)
    return false if $game_map.interpreter.running?
    result = false
    for event in $game_map.events_xy(x, y)
      if event.character_name != ""                #### 追加部分 (ここから) ####
        next unless parallel?(event)              
      end                                          #### 追加部分 (ここまで) ####
      if [1,2].include?(event.trigger) and event.priority_type == 1
        event.start
        result = true
      end
    end
    return result
  end
  #--------------------------------------------------------------------------
  # ◎ 乗り物から降りる
  #    現在乗り物に乗っていることが前提。
  #--------------------------------------------------------------------------
  alias tig_ew_get_off_vehicle get_off_vehicle
  def get_off_vehicle
    return if $game_map.back_tile?(@x, @y)
    if @vehicle_type == 0 or @vehicle_type == 1
      front_x = $game_map.x_with_direction(@x, @direction)
      front_y = $game_map.y_with_direction(@y, @direction)
      return if $game_map.wall?($game_map.point_tile_id(front_x, front_y)[0])
      return if $game_map.under_bridge_passages[[front_x, front_y]] == 1
      return if $game_map.prohibited_get_off.include?([front_x, front_y])
      @no_turn_back = true 
    end
    tig_ew_get_off_vehicle
  end
end
 
#==============================================================================
# ■ Spriteset_Map
#------------------------------------------------------------------------------
#  マップ画面のスプライトやタイルマップなどをまとめたクラスです。このクラスは
# Scene_Map クラスの内部で使用されます。
#==============================================================================
 
class Spriteset_Map
  #--------------------------------------------------------------------------
  # ◎ ビューポートの作成
  #--------------------------------------------------------------------------
  alias tig_ew_create_viewports create_viewports
  def create_viewports
    tig_ew_create_viewports
    @viewport4 = Viewport.new(0, 0, 640, 480) # 追加レイヤー用ビューポート
    if Expansion_Passable::BACK_SEEING
      @viewport5 = Viewport.new(0, 0, 640, 480) # 裏回りキャラ透過用ビューポート
    end
    if @viewport6 == nil
      @viewport6 = Viewport.new(0, 0, 640, 480) # 色調変更用ビューポート
      @viewport6.z = 25
    end
  end
  #--------------------------------------------------------------------------
  # ◎ タイルマップの作成
  #--------------------------------------------------------------------------
  alias tig_ew_create_tilemap create_tilemap
  def create_tilemap
    tig_ew_create_tilemap
    setup_shadow_eraser_tilemap
    @ex_tilemap = Tilemap.new(@viewport4)
    @ex_tilemap.bitmaps[0] = Cache.system("TileA1")
    @ex_tilemap.bitmaps[1] = Cache.system("TileA2")
    @ex_tilemap.bitmaps[2] = Cache.system("TileA3")
    @ex_tilemap.bitmaps[3] = Cache.system("TileA4")
    @ex_tilemap.bitmaps[4] = Cache.system("TileA5")
    @ex_tilemap.bitmaps[5] = Cache.system("TileB")
    @ex_tilemap.bitmaps[6] = Cache.system("TileC")
    @ex_tilemap.bitmaps[7] = Cache.system("TileD")
    @ex_tilemap.bitmaps[8] = Cache.system("TileE")
    @ex_tilemap.map_data = $game_map.ex_data
    @ex_tilemap.passages = $game_map.passages
  end
  #--------------------------------------------------------------------------
  # ○ 影消し処理用タイルの準備
  #--------------------------------------------------------------------------
  def setup_shadow_eraser_tilemap
    for i in 1952...2048
      @tilemap.passages[i] = 0x00
    end
  end 
  #--------------------------------------------------------------------------
  # ◎ 飛行船の影スプライトの作成
  #--------------------------------------------------------------------------
  alias tig_ew_create_shadow create_shadow
  def create_shadow
    tig_ew_create_shadow
    @shadow_sprite.viewport = @viewport4
  end
  #--------------------------------------------------------------------------
  # ◎ タイルマップの解放
  #--------------------------------------------------------------------------
  alias tig_ew_dispose_tilemap dispose_tilemap
  def dispose_tilemap
    tig_ew_dispose_tilemap
    @ex_tilemap.dispose
  end
  #--------------------------------------------------------------------------
  # ◎ ビューポートの解放
  #--------------------------------------------------------------------------
  alias tig_ew_dispose_viewports dispose_viewports
  def dispose_viewports
    tig_ew_dispose_viewports
    @viewport4.dispose
    @viewport5.dispose if Expansion_Passable::BACK_SEEING
    @viewport6.dispose if @viewport6 != nil
  end
  #--------------------------------------------------------------------------
  # ◎ タイルマップの更新
  #--------------------------------------------------------------------------
  alias tig_ew_update_tilemap update_tilemap
  def update_tilemap
    tig_ew_update_tilemap
    @ex_tilemap.ox = $game_map.display_x / 8
    @ex_tilemap.oy = $game_map.display_y / 8
    @ex_tilemap.update
  end
  #--------------------------------------------------------------------------
  # ◎ キャラクタースプライトの更新
  #--------------------------------------------------------------------------
  alias tig_ew_update_characters update_characters
  def update_characters
    back_seeing_characters = []
    for sprite in @character_sprites
      if sprite.character.on_tile or sprite.character.priority_type == 2
        sprite.viewport = @viewport4
      else
        sprite.viewport = @viewport1
      end
      next if sprite.character.not_back_seeing
      if $game_player.turn_back and Expansion_Passable::BACK_SEEING
        x = $game_player.x ; y = $game_player.y
        if ($game_map.round_x(x-2)..$game_map.round_x(x+2)).include?(sprite.character.x) and
          ($game_map.round_y(y-2)..$game_map.round_y(y+2)).include?(sprite.character.y)
          back_seeing_characters.push(sprite.character)
        end
      end
    end
    back_seeing(back_seeing_characters)
 
    tig_ew_update_characters 
  end
  #--------------------------------------------------------------------------
  # ○ 裏回りキャラ透過
  #--------------------------------------------------------------------------  
  def back_seeing(characters)
    return unless Expansion_Passable::BACK_SEEING
    @back_seeing_sprites = [] if @back_seeing_sprites == nil
    @characters = [] if @characters == nil
    now_character = []
    for character in characters
      now_character.push(character)
      unless @characters.include?(character)
        new_sprite = Sprite_Character.new(@viewport5, character)
        new_sprite.back_seeing = true
        @back_seeing_sprites.push(new_sprite)
        @characters.push(new_sprite.character)
      end
    end
 
    for sprite in @back_seeing_sprites
      if now_character.include?(sprite.character)
        sprite.update
      else
        @characters.delete(sprite.character)
        sprite.dispose
        @back_seeing_sprites.delete(sprite)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ◎ フレーム更新
  #--------------------------------------------------------------------------
  alias tig_ew_update update
  def update
    tig_ew_update
    update_ew_ex_viewports
  end
  #--------------------------------------------------------------------------
  # ● ビューポートの更新
  #--------------------------------------------------------------------------
  def update_viewports
    @viewport6.tone = $game_map.screen.tone        ##### 修正部分 (この行) #####
 
    @viewport1.ox = $game_map.screen.shake
    @viewport2.color = $game_map.screen.flash_color
    @viewport3.color.set(0, 0, 0, 255 - $game_map.screen.brightness)
    @viewport1.update
    @viewport2.update
    @viewport3.update
 
    @viewport6.update                              ##### 追加部分 (この行) #####
  end
  #--------------------------------------------------------------------------
  # ○ 拡張ビューポートの更新
  #--------------------------------------------------------------------------
  def update_ew_ex_viewports
    @viewport4.ox = $game_map.screen.shake
    @viewport4.update
    if Expansion_Passable::BACK_SEEING
      @viewport5.rect.set($game_player.screen_x - 48, $game_player.screen_y - 64, 96, 96) 
      @viewport5.ox = $game_map.screen.shake
      @viewport5.update
    end       
  end
end
 
#==============================================================================
# ■ Sprite_Character
#------------------------------------------------------------------------------
#  キャラクター表示用のスプライトです。Game_Character クラスのインスタンスを
# 監視し、スプライトの状態を自動的に変化させます。
#==============================================================================
 
class Sprite_Character < Sprite_Base
  #--------------------------------------------------------------------------
  # ○ 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_accessor :back_seeing
  #--------------------------------------------------------------------------
  # ◎ オブジェクト初期化
  #     viewport  : ビューポート
  #     character : キャラクター (Game_Character)
  #--------------------------------------------------------------------------
  alias tig_ew_initialize initialize
  def initialize(viewport, character = nil)
    tig_ew_initialize(viewport, character)
    @back_seeing = false
  end
  #--------------------------------------------------------------------------
  # ◎ フレーム更新
  #--------------------------------------------------------------------------
  alias tig_ew_update update
  def update
    tig_ew_update
    if @back_seeing
      self.opacity = 100 if self.opacity > 100
      self.x -= $game_player.screen_x - 48
      self.y -= $game_player.screen_y - 64
    end
  end
  #--------------------------------------------------------------------------
  # ◎ フキダシアイコン表示の開始
  #--------------------------------------------------------------------------
  alias tig_ew_start_balloon start_balloon
  def start_balloon
    tig_ew_start_balloon
    @balloon_sprite.viewport = @viewport4
  end
end
 
#==============================================================================
# ■ Scene_Title
#------------------------------------------------------------------------------
#  タイトル画面の処理を行うクラスです。
#==============================================================================
 
class Scene_Title < Scene_Base
  #--------------------------------------------------------------------------
  # ◎ 開始処理
  #--------------------------------------------------------------------------
  alias tig_ew_start start
  def start
    tig_ew_start
    $game_map.create_ex_passages
  end
  #--------------------------------------------------------------------------
  # ◎ データベースのロード
  #    name から<船の橋下通過>の設定を得ます。
  #--------------------------------------------------------------------------
  alias tig_ew_load_database load_database
  def load_database
    tig_ew_load_database
    for area in $data_areas.values
      area.name = area.name.sub(/[\<<]架け橋[\>>]/, "") # 名前の修正と
      area.ship_pass = $& != nil                    # 船の橋下通過エリアの設定
      area.name = area.name.sub(/[\<<]処理無視[\>>]/, "") # 名前の修正と
      area.disregard_processing = $& != nil         # 処理無視エリアの設定
    end
  end
end