查看: 1750|回复: 4

[已经解决] 寻路脚本问题

[复制链接]
梦石
0
星屑
544
在线时间
59 小时
注册时间
2017-11-19
回帖
62
发表于 2018-4-29 18:26:24 | 显示全部楼层 |阅读模式
悬赏20星屑已解决
本帖最后由 寒冷的企鹅 于 2018-4-30 11:33 编辑

问题只解决了第二个
这个脚本我已经按照上面说的在移动路线里设置脚本,可每次都报错,只有初始时向玩家移动没事 捕获.PNG
  1. #==============================================================================
  2. # ■ 経路探索VXAce (VX Ace用)
  3. #------------------------------------------------------------------------------
  4. # 製作者     : CanariAlternate
  5. # サイト名   : カルトの鳥篭
  6. # サイトURL  : [url]http://canarialt.blog.fc2.com[/url]
  7. #------------------------------------------------------------------------------
  8. # ■ 概要 : 指定した座標(またはキャラクター)まで経路探索をしながら移動する。
  9. #           この経路探索では一定時間動いていないイベントは障害物として認識する。
  10. #
  11. # ■ 必須 : 「注釈取得スクリプト」
  12. #
  13. # ■ 位置 : 「注釈取得スクリプト」より下
  14. #           「マルチレイヤーシステム」より下
  15. #------------------------------------------------------------------------------
  16. # ■ 機能 : 1. 指定した座標(またはキャラクター)まで移動する。
  17. #
  18. #           2. 経路探索の移動で進入禁止の領域をリージョンIDで指定する。
  19. #
  20. #           3. 注釈にキーワードを記述すると経路探索において障害物と見なさない。
  21. #
  22. # ■ スクリプト(移動ルートの設定) :
  23. #           1. route_search(引数)
  24. #              目標に到達するまで移動を繰り返す。
  25. #              移動ルートの設定の「移動できない場合は飛ばす」は route_search には無効である。
  26. #              引数はハッシュで設定する。省略するとあらかじめ設定されている初期値が採用される。
  27. #              a. 目標の設定(※初期値はプレイヤー)
  28. #                 :x => x座標
  29. #                 :y => y座標
  30. #                 :z => z座標(通常は使用しないので省略可能)
  31. #                 :char_id => イベントID
  32. #                 :character => オブジェクト($game_playerなど)
  33. #
  34. #                 指定する方法は以下の3通りである。
  35. #                 :x, :y, :z は座標で指定する。:x と :y の両方設定しないと無効になる。
  36. #                 :char_id はイベントのIDで指定する。プレイヤーを指定したい場合は -1 である。
  37. #                 :character はオブジェクトを直接指定する。
  38. #                 例1 route_search(:x => 5, :y => 7)             座標(5, 7)が目標
  39. #                 例2 route_search(:char_id => 5)                イベントIDが5番のイベントが目標
  40. #                 例3 route_search(:character => $game_player)   プレイヤーが目標
  41. #
  42. #              b. 斜め移動の使用(※初期値はfalse)
  43. #                 :diagonal => true / false
  44. #                 true  は斜め移動を使用する。
  45. #                 false は斜め移動を使用しない。
  46. #
  47. #              c. 探索失敗時の行動(※初期値はtrue)
  48. #                 :fail => true / false
  49. #                 true  は失敗時はランダムに移動する。
  50. #                 false は失敗時はウェイトする。
  51. #
  52. #              d. 経路探索を実行する目標との距離(※初期値は25)
  53. #                 :length => 数値
  54. #                 設定した数値より目標が遠い場合はウェイトする。
  55. #
  56. #              e. ウェイトの時間(※初期値は6)
  57. #                 :wait => フレーム数
  58. #                 false を設定するとウェイトせずに経路探索を中止する。
  59. #
  60. #              f. 目標の何歩前まで移動するか(※初期値は0)
  61. #                 :near => 数値
  62. #                 例えば、1 の場合は目標の1歩手前で探索終了する。
  63. #                 0 であれば目標と重なるまで探索を終了しない。
  64. #
  65. #              g. 探索回数の上限(※初期値はfalse)
  66. #                 :give_up => 数値
  67. #                 false を設定すると自動モードになる。
  68. #                 大きな数値(およそ100以上)を設定すると遠くまで探索するがPCへの負荷も大きくなる。
  69. #
  70. #              h. 1回移動したら次の命令を実行(※初期値はfalse)
  71. #                 :manual => true / false
  72. #                 true  は1回移動すると次の命令を実行します。
  73. #                 false は目標への移動が完了するまで移動を続けます。
  74. #
  75. #              i. 進入禁止のリージョンIDを設定(※初期値はこの下で設定)
  76. #                 :region => [5, 6, 7]
  77. #                 リージョンIDは配列で設定する必要がある。
  78. #
  79. #              これらの引数は全て省略可能で route_search と記述するだけでもプレイヤーを追尾する。
  80. #------------------------------------------------------------------------------
  81. # 更新履歴 : 2013/01/05 Ver2.05 スクリプトを大幅に変更した。スクリプト名を変更した。
  82. #            2013/01/09 Ver3.05 内部処理を大幅に変更した。
  83. #            2013/02/19 Ver3.06 共通処理スクリプトの廃止による変更を施した。
  84. #            2013/06/28 Ver3.07 失敗時のランダム移動のバグを修正した。
  85. #            2013/06/28 Ver3.08 斜め移動では[経路探索で無視]を無効にするように変更した。
  86. #            2013/06/29 Ver3.09 ある条件を満たすと経路情報を捨てる機能を廃止した。
  87. #            2013/06/29 Ver3.10 1回移動したら次の命令を実行するオプションを追加した。
  88. #            2013/06/29 Ver3.11 ループしたマップで正常に移動しないバグを修正した。
  89. #            2013/08/18 Ver3.12 進入禁止のリージョンIDを個別に設定できる機能を追加した。
  90. #==============================================================================

  91. $imported ||= {}
  92. $imported[:CanariAlternate_RouteSearch] = true

  93. #==============================================================================
  94. # ■ Calt
  95. #------------------------------------------------------------------------------
  96. #  CanariAlternateが製作したスクリプト用のモジュールです。
  97. #==============================================================================
  98. module Calt
  99.   #-----------------------------------------------------------------------------
  100.   # ◆経路探索で進入禁止にするリージョンIDの配列(引数を省略した際に適用される設定)
  101.   NOT_ENTER_REGION_ID = [4]
  102.   #
  103.   # ◆イベントを障害物として扱わないことを設定するキーワード
  104.   SCAN_THROUGH_EVENT_NOTE = /\[経路探索で無視\]/ # 記述した頁に適用
  105.   SCAN_THROUGH_WHOLE_NOTE = /\<経路探索で無視\>/ # 全ての頁に適用(1頁目に記述)
  106.   #-----------------------------------------------------------------------------
  107. end

  108. #==============================================================================
  109. # ■ 探索処理 ■
  110. #==============================================================================
  111. #==============================================================================
  112. # ■ Game_Character
  113. #------------------------------------------------------------------------------
  114. #  主に移動ルートなどの処理を追加したキャラクターのクラスです。Game_Player、
  115. # Game_Follower、GameVehicle、Game_Event のスーパークラスとして使用されます。
  116. #==============================================================================
  117. class Game_Character < Game_CharacterBase
  118.   #--------------------------------------------------------------------------
  119.   # ● 定数
  120.   #--------------------------------------------------------------------------
  121.   LongSearchRate = 0.1                    # 探索回数の自動設定で長距離探索する確率
  122.   #--------------------------------------------------------------------------
  123.   # ● クラス変数
  124.   #--------------------------------------------------------------------------
  125.   @@route_search_frame = 0                # 経路探索を実行したフレームを記憶
  126.   @@give_up_rate       = LongSearchRate   # 長距離探索を実行する確率
  127.   #--------------------------------------------------------------------------
  128.   # ● 経路探索で移動 [新規]
  129.   #--------------------------------------------------------------------------
  130.   def route_search(argument={})
  131.     # パラメータの取得
  132.     gx, gy, gz = get_goal_coordinate(argument)                          # 目的地(対象)の座標を取得
  133.     diagonal = argument[:diagonal] != nil ? argument[:diagonal] : false # 斜め移動の使用の有無
  134.     length   = argument[:length]   != nil ? argument[:length]   : 25    # 探索を実行する対象との距離
  135.     wait     = argument[:wait]     != nil ? argument[:wait]     : 6     # ウェイト時間(false は待機せず探索中止)
  136.     near     = argument[:near]     != nil ? argument[:near]     : 0     # 対象の何歩手前で停止するか
  137.     fail     = argument[:fail]     != nil ? argument[:fail]     : true  # 探索失敗時に適当な移動を行うか
  138.     give_up  = argument[:give_up]  != nil ? argument[:give_up]  : false # 探索回数の上限(false は自動)
  139.     manual   = argument[:manual]   != nil ? argument[:manual]   : false # 1回移動したら次の命令を実行
  140.     region   = argument[:region]   != nil ? argument[:region]   : Calt::NOT_ENTER_REGION_ID # 進入禁止のリージョンIDの配列

  141.     # 経路探索の設定
  142.     @routing_object ||= Route_Search.new(self)                          # 経路探索のクラスのオブジェクトを生成
  143.     @routing_object.not_enter_region_id     = region                    # 進入禁止のリージョンIDの配列を設定
  144.     @routing_object.limit_search_count      = get_give_up(give_up)      # 探索回数の上限を設定
  145.     @routing_object.always_collide_through  = false                     # 常に衝突判定無視を無効に設定
  146.     @routing_object.ending_collide_distance = near != 0 ? near - 1 : 0  # 終点付近で衝突判定無視の距離を設定
  147.     @routing_object.ending_through_distance = near - 1                  # 終点付近で通行判定無視の距離を設定
  148.     @routing_object.diagonal                = diagonal                  # 斜め移動の使用の有無を設定
  149.     @routing_object.active_collide_through  = 60                        # 指定フレーム以内に移動してるキャラクターは衝突判定無視を設定

  150.     # 探索実行範囲内か判定
  151.     distance = @routing_object.distance_xy(@x, @y, gx, gy)              # 終点までの最短距離を取得
  152.     return wait_search(wait, manual) if distance > length               # 探索範囲外の場合はウェイト

  153.     # 探索成功範囲内か判定
  154.     if distance <= near && !(gx == @x && gy == @y && gz != z)           # 探索成功の場合
  155.       return end_route_search                                           # 経路探索終了の手続きを実行
  156.     end

  157.     last_x, last_y, last_z = @x, @y, z                                  # 現在の座標を記憶
  158.     last_route_list = @route_list                                       # 現在の経路情報を記憶
  159.    
  160.     # 通常の経路探索
  161.     @route_list = @routing_object.a_star_search(gx, gy, gz)             # 経路情報の取得
  162.     if !@route_list || @route_list.empty?                               # 経路情報を取得できなかった場合
  163.       @route_list = nil                                                 # 経路情報を消去
  164.       return wait_search(wait, manual) unless fail                      # 失敗時にランダム移動しない場合はウェイト
  165.       fail_move_random(diagonal, region)                                # 失敗した場合の移動
  166.       set_move_succeed(wait, last_x, last_y, last_z)                    # 移動成功を判定
  167.       continue_route_search unless manual                               # 経路探索継続の手続きを実行
  168.       return
  169.     end
  170.     if @route_list[0] == [gx, gy, gz]                                   # 終点までの経路情報の取得に成功した場合
  171.       route_list_to_move(@route_list)                                   # 経路に従って移動
  172.       set_move_succeed(wait, last_x, last_y, last_z)                    # 移動成功を判定
  173.       continue_route_search unless manual                               # 経路探索継続の手続きを実行
  174.       return
  175.     end

  176.     # 以前の経路情報を使用して経路探索
  177.     new_route_list = @route_list                                        # 最新の推定経路情報を記憶
  178.     @routing_object.limit_search_count = get_give_up(give_up)           # 探索回数の上限を設定
  179.     @route_list = @routing_object.reuse_route_list(last_route_list, 5, gx, gy, gz)  # 以前の経路情報を使って探索
  180.     if !@route_list || @route_list.empty?                               # 経路情報を取得できなかった場合
  181.       @route_list = nil                                                 # 経路情報を消去
  182.       return wait_search(wait, manual) unless fail                      # 失敗時に移動しない場合はウェイト
  183.       @route_list = new_route_list                                      # 最新の推定経路情報を復帰
  184.       route_list_to_move(@route_list)                                   # 経路に従って移動
  185.       set_move_succeed(wait, last_x, last_y, last_z)                    # 移動成功を判定
  186.       continue_route_search unless manual                               # 経路探索継続の手続きを実行
  187.       return
  188.     end
  189.     if @route_list[0] == [gx, gy, gz]                                   # 終点までの経路情報の取得に成功した場合
  190.       route_list_to_move(@route_list)                                   # 経路に従って移動
  191.       set_move_succeed(wait, last_x, last_y, last_z)                    # 移動成功を判定
  192.       continue_route_search unless manual                               # 経路探索継続の手続きを実行
  193.       return
  194.     end
  195.     unless fail                                                         # 失敗時に移動しない場合
  196.       @route_list = nil                                                 # 経路情報を消去
  197.       wait_search(wait, manual)                                         # ウェイト
  198.       return
  199.     end
  200.     old_x = @route_list[0][0]
  201.     old_y = @route_list[0][1]
  202.     old_distance = @routing_object.distance_xy(old_x, old_y, gx, gy)    # 更新推定経路の終点までの最短距離を取得
  203.     new_x = new_route_list[0][0]
  204.     new_y = new_route_list[0][1]
  205.     new_distance = @routing_object.distance_xy(new_x, new_y, gx, gy)    # 最新推定経路の終点までの最短距離を取得
  206.     @route_list = new_route_list if old_distance >= new_distance        # 最新推定経路の方が優れている場合は経路情報を復帰
  207.     route_list_to_move(@route_list)                                     # 経路に従って移動
  208.     set_move_succeed(wait, last_x, last_y, last_z)                      # 移動成功を判定
  209.     continue_route_search unless manual                                 # 経路探索継続の手続きを実行
  210.     return
  211.   end
  212.   #--------------------------------------------------------------------------
  213.   # ● 対象の座標を取得 [新規]
  214.   #--------------------------------------------------------------------------
  215.   def get_goal_coordinate(argument)
  216.     if argument[:x] && argument[:y]           # 座標で直接指定の場合
  217.       return [argument[:x], argument[:y], argument[:z] || 0]
  218.     end
  219.     character = argument[:char]               # キャラクターのオブジェクトを直接指定
  220.     if !character && id = argument[:char_id]  # キャラクターのIDで指定の場合
  221.       character = case id
  222.       when 0 ; self                           # id : 0  はこのキャラクター自体
  223.       when -1; $game_player                   # id : -1 はプレイヤー
  224.       else   ; $game_map.events[id] || self   # それ以外はイベント
  225.       end
  226.     else
  227.       character = $game_player                # 指定が無い場合はプレイヤー
  228.     end
  229.     return [character.x, character.y, character.z]
  230.   end
  231.   #--------------------------------------------------------------------------
  232.   # ● 経路探索を待機 [新規]
  233.   #--------------------------------------------------------------------------
  234.   def wait_search(wait, manual)
  235.     if wait
  236.       @wait_count = wait      # ウェイトを設定
  237.       manual ? end_route_search : continue_route_search
  238.     else
  239.       @wait_count = 0         # ウェイトはなし
  240.       end_route_search
  241.     end
  242.   end
  243.   #--------------------------------------------------------------------------
  244.   # ● 経路探索による移動を終了の手続き [新規]
  245.   #--------------------------------------------------------------------------
  246.   def end_route_search
  247.     @move_succeed = true
  248.   end
  249.   #--------------------------------------------------------------------------
  250.   # ● 経路探索による移動を継続の手続き [新規]
  251.   #--------------------------------------------------------------------------
  252.   def continue_route_search
  253.     @move_succeed = true
  254.     @move_route_index -= 1  # もう1度実行されるように実行位置を1つ戻す
  255.   end
  256.   #--------------------------------------------------------------------------
  257.   # ● 経路リストに従って移動 [新規]
  258.   #--------------------------------------------------------------------------
  259.   def route_list_to_move(route_list)
  260.     dx = distance_x_from(route_list[-1][0])
  261.     dy = distance_y_from(route_list[-1][1])
  262.     if dx == 0
  263.       dy == 0 ? nil              : (dy < 0 ? move_straight(2)    : move_straight(8)   )
  264.     elsif dx < 0
  265.       dy == 0 ? move_straight(6) : (dy < 0 ? move_diagonal(6, 2) : move_diagonal(6, 8))
  266.     else
  267.       dy == 0 ? move_straight(4) : (dy < 0 ? move_diagonal(4, 2) : move_diagonal(4, 8))
  268.     end
  269.   end
  270.   #--------------------------------------------------------------------------
  271.   # ● 移動成功を判定 [新規]
  272.   #--------------------------------------------------------------------------
  273.   def set_move_succeed(wait, last_x, last_y, last_z)
  274.     if last_x != @x || last_y != @y || last_z != z
  275.       @move_succeed = true                                      # 移動成功
  276.     else
  277.       @move_succeed = false                                     # 移動失敗
  278.       @wait_count = (wait || 6)                                 # 過負荷を回避する為にウェイト
  279.       @route_list.push [@x, @y, z] if @route_list               # 経路情報を修正
  280.     end
  281.   end
  282.   #--------------------------------------------------------------------------
  283.   # ● 負荷を考慮した探索回数の上限値を取得 [新規]
  284.   #--------------------------------------------------------------------------
  285.   def get_give_up(give_up)
  286.     return give_up if give_up
  287.     if @@route_search_frame != Graphics.frame_count
  288.       @@give_up_rate = LongSearchRate                           # 確率を初期化
  289.       @@route_search_frame = Graphics.frame_count               # フレームの記憶を更新
  290.     end
  291.     if rand(10000) < 10000 * @@give_up_rate
  292.       @@give_up_rate = 0                                        # 確率を更新
  293.       result = 10 + rand(140)                                   # 高い上限を設定
  294.     else
  295.       @@give_up_rate *= @@give_up_rate / (1.0 - @@give_up_rate) # 確率を更新
  296.       result = 10                                               # 低い上限を設定
  297.     end
  298.     return result
  299.   end
  300.   #--------------------------------------------------------------------------
  301.   # ● 失敗した場合の移動 [新規]
  302.   #--------------------------------------------------------------------------
  303.   def fail_move_random(diagonal, region)
  304.     # 斜め移動の場合
  305.     if diagonal && rand(2) == 0
  306.       horz = rand(2) == 0 ? 4 : 6
  307.       vert = rand(2) == 0 ? 2 : 8
  308.       nx = $game_map.round_x_with_direction(@x, horz)
  309.       ny = $game_map.round_y_with_direction(@y, vert)
  310.       return if region.include?($game_map.region_id(nx, ny))
  311.       move_diagonal(horz, vert)
  312.       return
  313.     end
  314.     # 4方向移動の場合
  315.     if rand(2) == 0
  316.       horz = rand(2) == 0 ? 4 : 6
  317.       nx = $game_map.round_x_with_direction(@x, horz)
  318.       return if region.include?($game_map.region_id(nx, @y))
  319.       move_straight(horz)
  320.     else
  321.       vert = rand(2) == 0 ? 2 : 8
  322.       ny = $game_map.round_y_with_direction(@y, vert)
  323.       return if region.include?($game_map.region_id(@x, ny))
  324.       move_straight(vert)
  325.     end
  326.   end
  327. end

  328. #==============================================================================
  329. # ■ 基礎定義 ■
  330. #==============================================================================
  331. #==============================================================================
  332. # ■ Game_CharacterBase
  333. #------------------------------------------------------------------------------
  334. #  キャラクターを扱う基本のクラスです。全てのキャラクターに共通する、座標やグ
  335. # ラフィックなどの基本的な情報を保持します。
  336. #==============================================================================
  337. class Game_CharacterBase
  338.   #--------------------------------------------------------------------------
  339.   # ● クラス変数
  340.   #--------------------------------------------------------------------------
  341.   @@active_collide_through = false        # 座標一致判定の無効化
  342.   @@collide_through        = false        # 衝突判定無視
  343.   @@search_diagonal        = false        # 斜めの移動判定か否か
  344.   def active_collide_through=(arg) ; @@active_collide_through = arg ; end
  345.   def collide_through=(arg)        ; @@collide_through        = arg ; end
  346.   def search_diagonal=(arg)        ; @@search_diagonal        = arg ; end
  347.   #--------------------------------------------------------------------------
  348.   # ● 公開インスタンス変数
  349.   #--------------------------------------------------------------------------
  350.   attr_reader   :stopwatch                # 連続停止時間
  351.   #--------------------------------------------------------------------------
  352.   # ● z 座標の取得 [新規]
  353.   #--------------------------------------------------------------------------
  354.   def z
  355.     return 0                              # 座標が3次元の場合は再定義する
  356.   end
  357.   #--------------------------------------------------------------------------
  358.   # ● 公開メンバ変数の初期化 [追加]
  359.   #--------------------------------------------------------------------------
  360.   alias init_public_members_RouteSearch init_public_members
  361.   def init_public_members
  362.     init_public_members_RouteSearch
  363.     @stopwatch = 1.0/0                    # 1.0/0 #=> Infinity
  364.     @movement_record = [[nil, nil, nil]]  # 移動履歴
  365.   end
  366.   #--------------------------------------------------------------------------
  367.   # ● 非公開メンバ変数の初期化 [追加]
  368.   #--------------------------------------------------------------------------
  369.   alias init_private_members_RouteSearch init_private_members
  370.   def init_private_members
  371.     init_private_members_RouteSearch
  372.     @scan_through_event = false           # 経路探索において障害物として扱わないフラグ
  373.   end
  374.   #--------------------------------------------------------------------------
  375.   # ● フレーム更新 [追加]
  376.   #--------------------------------------------------------------------------
  377.   alias update_RouteSearch update
  378.   def update
  379.     update_RouteSearch
  380.     update_stopwatch                      # 連続停止時間を更新
  381.   end
  382.   #--------------------------------------------------------------------------
  383.   # ● 連続停止時間を更新 [新規]
  384.   #--------------------------------------------------------------------------
  385.   def update_stopwatch
  386.     if @movement_record[-1] != [x, y, z]
  387.       @stopwatch = 0 if @movement_record.size > 1
  388.       @movement_record.push [x, y, z]
  389.       @movement_record.shift if @movement_record.size > 10
  390.     elsif !moving?
  391.       @stopwatch += 1
  392.     end
  393.   end
  394.   #--------------------------------------------------------------------------
  395.   # ● 座標一致判定 [追加]
  396.   #--------------------------------------------------------------------------
  397.   alias pos_RouteSearch? pos?
  398.   def pos?(*args)
  399.     return false if @@collide_through and !tile? || @scan_through_event && !@@search_diagonal
  400.     # 連続停止時間が指定値以下の場合は座標を不一致にすることで衝突判定を回避する。
  401.     return false if @@active_collide_through and @stopwatch <= @@active_collide_through && !tile? || @scan_through_event && !@@search_diagonal
  402.     pos_RouteSearch?(*args)
  403.   end
  404. end
  405. #==============================================================================
  406. # ■ Game_Event
  407. #------------------------------------------------------------------------------
  408. #  イベントを扱うクラスです。条件判定によるイベントページ切り替えや、並列処理
  409. # イベント実行などの機能を持っており、Game_Map クラスの内部で使用されます。
  410. #==============================================================================
  411. class Game_Event < Game_Character
  412.   #--------------------------------------------------------------------------
  413.   # ● 衝突判定無視の設定を取得 [新規]
  414.   #--------------------------------------------------------------------------
  415.   def get_scan_through_event_note
  416.     event_result = event_note_include?(Calt::SCAN_THROUGH_EVENT_NOTE)
  417.     whole_result = whole_note_include?(Calt::SCAN_THROUGH_WHOLE_NOTE)
  418.     return event_result ^ whole_result
  419.   end
  420.   #--------------------------------------------------------------------------
  421.   # ● イベントページ更新時に注釈から設定を取得 [追加]
  422.   #--------------------------------------------------------------------------
  423.   alias setup_note_settings_RouteSearch setup_note_settings
  424.   def setup_note_settings
  425.     setup_note_settings_RouteSearch
  426.     @scan_through_event = get_scan_through_event_note
  427.   end
  428. end
  429. #==============================================================================
  430. # ■ Route_Search
  431. #------------------------------------------------------------------------------
  432. #  経路探索の処理を定義したクラスです。
  433. #==============================================================================
  434. class Route_Search
  435.   #--------------------------------------------------------------------------
  436.   # ● 基礎変数の初期化
  437.   #--------------------------------------------------------------------------
  438.   def init_base_instance(character)
  439.     @character = character                # キャラクターへの参照
  440.     @x , @y , @z  = 0, 0, 0               # 今のノードの座標
  441.     @sx, @sy, @sz = 0, 0, 0               # 始点の座標
  442.     @gx, @gy, @gz = 0, 0, 0               # 終点の座標
  443.     @diagonal = false                     # 斜め移動の使用の有無
  444.     @collide_through = false              # 衝突判定を無視するフラグ
  445.     @active_collide_through = 60          # 指定フレーム以内に移動してるキャラクターは衝突判定無視
  446.   end
  447.   #--------------------------------------------------------------------------
  448.   # ● 2点間の x 方向の距離
  449.   #--------------------------------------------------------------------------
  450.   def distance_x(start_x, end_x)
  451.     result = end_x - start_x
  452.     return result unless $game_map.loop_horizontal? && result.abs > $game_map.width / 2
  453.     return result + (result < 0 ? $game_map.width : -$game_map.width)
  454.   end
  455.   #--------------------------------------------------------------------------
  456.   # ● 2点間の y 方向の距離
  457.   #--------------------------------------------------------------------------
  458.   def distance_y(start_y, end_y)
  459.     result = end_y - start_y
  460.     return result unless $game_map.loop_vertical? && result.abs > $game_map.height / 2
  461.     return result + (result < 0 ? $game_map.height : -$game_map.height)
  462.   end
  463.   #--------------------------------------------------------------------------
  464.   # ● 2点間の z 方向の距離
  465.   #--------------------------------------------------------------------------
  466.   def distance_z(start_z, end_z)
  467.     return end_z - start_z
  468.   end
  469.   #--------------------------------------------------------------------------
  470.   # ● 2点の距離を取得
  471.   #--------------------------------------------------------------------------
  472.   def distance_xy(start_x, start_y, end_x, end_y)
  473.     if @diagonal
  474.       return [distance_x(start_x, end_x).abs, distance_y(start_y, end_y).abs].max # 8方向の場合
  475.     else
  476.       return distance_x(start_x, end_x).abs + distance_y(start_y, end_y).abs      # 4方向の場合
  477.     end
  478.   end
  479.   #--------------------------------------------------------------------------
  480.   # ● x 座標の取得
  481.   #--------------------------------------------------------------------------
  482.   def character_x
  483.     return @character.x
  484.   end
  485.   #--------------------------------------------------------------------------
  486.   # ● y 座標の取得
  487.   #--------------------------------------------------------------------------
  488.   def character_y
  489.     return @character.y
  490.   end
  491.   #--------------------------------------------------------------------------
  492.   # ● z 座標の取得
  493.   #--------------------------------------------------------------------------
  494.   def character_z
  495.     return @character.z
  496.   end
  497.   #--------------------------------------------------------------------------
  498.   # ● z 座標の変換
  499.   #--------------------------------------------------------------------------
  500.   def xyz_to_z(x, y, z)
  501.     return z
  502.   end
  503.   #--------------------------------------------------------------------------
  504.   # ● 進入禁止のリージョンIDの座標か判定
  505.   #--------------------------------------------------------------------------
  506.   def not_enter_coordinate?(x, y, z)
  507.     return not_enter_region_id.include?($game_map.region_id(x, y))
  508.   end
  509.   #--------------------------------------------------------------------------
  510.   # ● 終点か判定
  511.   #--------------------------------------------------------------------------
  512.   def end_coordinate?(x, y, z)
  513.     return x == @gx && y == @gy && z == @gz
  514.   end
  515.   #--------------------------------------------------------------------------
  516.   # ● 終点から範囲内か判定
  517.   #--------------------------------------------------------------------------
  518.   def ending_distance?(x, y, z, distance)
  519.     return false if x == @gx && y == @gy && z != @gz
  520.     return false if distance_xy(x, y, @gx, @gy) > distance
  521.     return true
  522.   end
  523.   # #--------------------------------------------------------------------------
  524.   # # ● 始点から範囲内か判定
  525.   # #--------------------------------------------------------------------------
  526.   # def starting_distance?(x, y, z, distance)
  527.   #   return false if x == @sx && y == @sy && z != @sz
  528.   #   return false if distance_xy(x, y, @sx, @sy) > distance
  529.   #   return true
  530.   # end
  531.   # #--------------------------------------------------------------------------
  532.   # # ● 始点の隣接座標か判定(始点を含む)
  533.   # #--------------------------------------------------------------------------
  534.   # def adjoin_coordinate?(x, y, z)
  535.   #   return false unless starting_distance?(x, y, z, 1)
  536.   #   return xyz_to_z(x, y, @sz) == z
  537.   # end
  538.   #--------------------------------------------------------------------------
  539.   # ● 通行可能判定
  540.   #--------------------------------------------------------------------------
  541.   def passable?(d)
  542.     @character.active_collide_through = @active_collide_through
  543.     @character.collide_through = @collide_through
  544.     @character.search_diagonal = false
  545.     result = base_passable?(:passable?, d)
  546.     @character.collide_through = false
  547.     @character.active_collide_through = false
  548.     return result
  549.   end
  550.   #--------------------------------------------------------------------------
  551.   # ● 斜めの通行可能判定
  552.   #--------------------------------------------------------------------------
  553.   def diagonal_passable?(horz, vert)
  554.     @character.active_collide_through = @active_collide_through
  555.     @character.collide_through = @collide_through
  556.     @character.search_diagonal = true
  557.     result = base_passable?(:diagonal_passable?, horz, vert)
  558.     @character.collide_through = false
  559.     @character.active_collide_through = false
  560.     return result
  561.   end
  562.   #--------------------------------------------------------------------------
  563.   # ● 通行可能判定の呼び出し
  564.   #--------------------------------------------------------------------------
  565.   def base_passable?(name, *d)
  566.     return @character.send(name, @x, @y, *d)
  567.   end
  568. end

  569. #==============================================================================
  570. # ■ 内部処理 ■
  571. #==============================================================================
  572. #==============================================================================
  573. # ■ Route_Search
  574. #------------------------------------------------------------------------------
  575. #  経路探索の処理を定義したクラスです。
  576. #==============================================================================
  577. class Route_Search
  578.   #--------------------------------------------------------------------------
  579.   # ● 座標の配列のインデックス
  580.   #--------------------------------------------------------------------------
  581.   X = 0                                   # x座標
  582.   Y = 1                                   # y座標
  583.   Z = 2                                   # z座標
  584.   #--------------------------------------------------------------------------
  585.   # ● ノードの配列のインデックス
  586.   #--------------------------------------------------------------------------
  587.   RANK_A = 0                              # 評価値A
  588.   RANK_B = 1                              # 評価値B
  589.   RANK_C = 2                              # 評価値C
  590.   PARENT = 3                              # 親の座標
  591.   LENGTH = 4                              # 移動距離
  592.   #--------------------------------------------------------------------------
  593.   # ● 公開インスタンス変数
  594.   #--------------------------------------------------------------------------
  595.   attr_accessor :not_enter_region_id      # 進入禁止のリージョンIDの配列
  596.   attr_accessor :limit_search_count       # 探索回数の上限値
  597.   attr_accessor :always_collide_through   # 常に衝突判定を無視のフラグ
  598.   attr_accessor :ending_collide_distance  # 終点付近で衝突判定を無視の距離
  599.   attr_accessor :ending_through_distance  # 終点付近で通行判定を無視の距離
  600.   attr_accessor :diagonal                 # 斜め移動の使用の有無
  601.   attr_accessor :active_collide_through   # 指定フレーム以内に移動してるキャラクターは衝突判定無視
  602.   #--------------------------------------------------------------------------
  603.   # ● オブジェクト初期化
  604.   #--------------------------------------------------------------------------
  605.   def initialize(character)
  606.     init_base_instance(character)
  607.     @parent = [nil, nil, nil]             # 親の座標
  608.     @past_distance = 0                    # 探索点までの移動距離
  609.     @open_list = {}                       # 探索候補のノードのリスト
  610.     @close_list = {}                      # 探索済みのノードのリスト
  611.     @limit_search_count = 100
  612.     @always_collide_through = false
  613.     @ending_collide_distance = 0
  614.     @ending_through_distance = 0
  615.   end
  616.   #--------------------------------------------------------------------------
  617.   # ● A*経路探索の通常実行
  618.   #--------------------------------------------------------------------------
  619.   def a_star_search(gx, gy, gz=0)
  620.     init_process(gx, gy, gz)              # A*経路探索の初期化
  621.     return main_process                   # A*経路探索の主要処理
  622.   end
  623.   #--------------------------------------------------------------------------
  624.   # ● A*経路探索の初期化
  625.   #--------------------------------------------------------------------------
  626.   def init_process(gx, gy, gz)
  627.     @x, @y, @z = character_x, character_y, character_z # 現在の座標
  628.     @sx, @sy, @sz = @x, @y, @z            # 始点の座標
  629.     @gx, @gy, @gz = gx, gy, gz            # 終点の座標
  630.     @open_list  = {[@sx, @sy, @sz]=>create_node(@sx, @sy, @sz, [nil, nil, nil], 0)}
  631.     @close_list = {}
  632.   end
  633.   #--------------------------------------------------------------------------
  634.   # ● A*経路探索の主要処理
  635.   #--------------------------------------------------------------------------
  636.   def main_process
  637.     count = 0
  638.     until @open_list.empty?
  639.       node, coordinate = get_best_node(@open_list)  # 最も高評価のノードを探索候補のリストから取得
  640.       
  641.       # 探索成功の場合
  642.       return create_route_list(node, coordinate) if end_coordinate?(*coordinate)

  643.       # 探索回数の上限オーバーの場合
  644.       return create_estimate_list if (count += 1) > @limit_search_count

  645.       # 隣接点を探索候補に追加する処理
  646.       @close_list[coordinate] = @open_list.delete(coordinate) # 探索候補から探索済みに移動
  647.       @x, @y, @z     = *coordinate
  648.       @parent        = node[PARENT]
  649.       @past_distance = node[LENGTH] + 1
  650.       down  = $game_map.round_y(@y + 1)
  651.       up    = $game_map.round_y(@y - 1)
  652.       right = $game_map.round_x(@x + 1)
  653.       left  = $game_map.round_x(@x - 1)
  654.       open_list_add(@x, down , 2) # 下
  655.       open_list_add(@x, up   , 8) # 上
  656.       open_list_add(right, @y, 6) # 右
  657.       open_list_add(left , @y, 4) # 左
  658.       if @diagonal
  659.         open_list_add_diagonal(right, down, 6, 2) # 右下
  660.         open_list_add_diagonal(left , down, 4, 2) # 左下
  661.         open_list_add_diagonal(right, up  , 6, 8) # 右上
  662.         open_list_add_diagonal(left , up  , 4, 8) # 左上
  663.       end
  664.     end
  665.     return false  # 到達する経路は存在しない。
  666.   end
  667.   #--------------------------------------------------------------------------
  668.   # ● ノードのデータを生成
  669.   #--------------------------------------------------------------------------
  670.   def create_node(x, y, z, parent, length)
  671.     dx = distance_x(x, @gx).abs
  672.     dy = distance_y(y, @gy).abs
  673.     rank_a = length + dx + dy
  674.     rank_b = Math.sqrt(dx ** 2 + dy ** 2)
  675.     rank_c = distance_z(z, @gz).abs
  676.     return [rank_a, rank_b, rank_c, parent, length]
  677.     # [評価値A, 評価値B, 評価値C, 親の座標, 移動距離]
  678.     # 評価値A : 始点から終点までの推定最短経路の距離
  679.     # 評価値B : 終点との直線距離
  680.     # 評価値C : z 座標の距離
  681.     # 親の座標 : 移動元の座標
  682.     # 移動距離 : 始点からこの点までの移動距離
  683.   end
  684.   #--------------------------------------------------------------------------
  685.   # ● 最も高評価のノードを取得
  686.   #--------------------------------------------------------------------------
  687.   def get_best_node(hash_list, compare=:node_a_better?)
  688.     best_node = [1.0/0, 1.0/0, 1.0/0] # 1.0/0 #=> Infinity
  689.     best_coordinate = nil
  690.     hash_list.each do |coordinate, node|
  691.       next if send(compare, best_node, node)  # node よりも best_node が高評価なら next
  692.       best_node = node
  693.       best_coordinate = coordinate
  694.     end
  695.     return best_node, best_coordinate
  696.   end
  697.   #--------------------------------------------------------------------------
  698.   # ● ノードの優劣判定
  699.   #--------------------------------------------------------------------------
  700.   def node_a_better?(node_a, node_b)
  701.     return true  if node_a[RANK_A] < node_b[RANK_A]
  702.     return false if node_a[RANK_A] > node_b[RANK_A]
  703.     return true  if node_a[RANK_B] < node_b[RANK_B]
  704.     return false if node_a[RANK_B] > node_b[RANK_B]
  705.     return true  if node_a[RANK_C] < node_b[RANK_C]
  706.     return false if node_a[RANK_C] > node_b[RANK_C]
  707.     return true
  708.   end
  709.   #--------------------------------------------------------------------------
  710.   # ● 探索候補リストに隣接点を追加
  711.   #--------------------------------------------------------------------------
  712.   def open_list_add(nx, ny, d)
  713.     return if @parent[X] == nx && @parent[Y] == ny
  714.     nz = xyz_to_z(nx, ny, @z)                         # z 座標の変換
  715.     result = exception_decision?(nx, ny, nz)
  716.     return if result == false                         # 通行禁止の場合
  717.     if result == nil                                  # 通行可能判定に委ねる場合
  718.       @collide_through = collide_through?(nx, ny, nz)
  719.       return if !passable?(d)                         # 通行可能判定
  720.     end
  721.     open_list_add_node(nx, ny, nz)                    # 探索候補リストに追加
  722.   end
  723.   #--------------------------------------------------------------------------
  724.   # ● 探索候補リストに隣接点を追加(斜めの判定版)
  725.   #--------------------------------------------------------------------------
  726.   def open_list_add_diagonal(nx, ny, horz, vert)
  727.     return if @parent[X] == nx && @parent[Y] == ny
  728.     nz = xyz_to_z(nx, ny, @z)                         # z 座標の変換
  729.     result = exception_decision?(nx, ny, nz)
  730.     return if result == false                         # 通行禁止の場合
  731.     if result == nil                                  # 通行可能判定に委ねる場合
  732.       @collide_through = collide_through?(nx, ny, nz)
  733.       return if !diagonal_passable?(horz, vert)       # 斜めの通行可能判定
  734.     end
  735.     open_list_add_node(nx, ny, nz)                    # 探索候補リストに追加
  736.   end
  737.   #--------------------------------------------------------------------------
  738.   # ● 例外通行判定
  739.   #--------------------------------------------------------------------------
  740.   def exception_decision?(x, y, z)
  741.     return false if not_enter_coordinate?(x, y, z)
  742.     return true if ending_distance?(x, y, z, @ending_through_distance)
  743.     return nil
  744.   end
  745.   #--------------------------------------------------------------------------
  746.   # ● 衝突判定を無視するか判定
  747.   #--------------------------------------------------------------------------
  748.   def collide_through?(x, y, z)
  749.     return true if @always_collide_through
  750.     return true if ending_distance?(x, y, z, @ending_collide_distance)
  751.     return false
  752.   end
  753.   #--------------------------------------------------------------------------
  754.   # ● 探索候補リストに隣接点を追加する処理
  755.   #--------------------------------------------------------------------------
  756.   def open_list_add_node(nx, ny, nz)
  757.     next_coordinate = [nx, ny, nz]
  758.     if old_node = @close_list[next_coordinate]                            # 探索済みリストに既にある場合
  759.       if old_node[LENGTH] > @past_distance                                # 移動距離が優れている場合
  760.         @open_list[next_coordinate] = @close_list.delete(next_coordinate) # 探索候補リストに移動
  761.         update_node(old_node)
  762.       end
  763.       return
  764.     end
  765.     if old_node = @open_list[next_coordinate]                             # 探索候補リストに既にある場合
  766.       update_node(old_node) if old_node[LENGTH] > @past_distance          # 移動距離が優れている場合
  767.       return
  768.     end
  769.     @open_list[next_coordinate] = create_node(*next_coordinate, [@x, @y, @z], @past_distance)
  770.   end
  771.   #--------------------------------------------------------------------------
  772.   # ● ノードのデータを更新
  773.   #--------------------------------------------------------------------------
  774.   def update_node(old_node)
  775.     old_node[RANK_A] += @past_distance - old_node[LENGTH] # 移動距離の差を減算
  776.     old_node[PARENT]  = [@x, @y, @z]                      # 親の座標を更新
  777.     old_node[LENGTH]  = @past_distance                    # 移動距離を更新
  778.   end
  779.   #--------------------------------------------------------------------------
  780.   # ● 経路の配列を生成
  781.   #--------------------------------------------------------------------------
  782.   def create_route_list(node, coordinate)
  783.     parent = node[PARENT]
  784.     route_list = []
  785.     while parent != [nil, nil, nil]
  786.       route_list.push coordinate
  787.       parent = @close_list[coordinate = parent][PARENT]
  788.     end
  789.     return route_list # 先頭が終点で末尾が始点の次の座標(推定経路の場合は先頭は終点ではない)
  790.   end
  791.   #--------------------------------------------------------------------------
  792.   # ● 推定経路の配列を生成
  793.   #--------------------------------------------------------------------------
  794.   def create_estimate_list
  795.     open_node, open_coordinate = get_best_node(@open_list, :estimate_node_a_better?)
  796.     close_node, close_coordinate = get_best_node(@close_list, :estimate_node_a_better?)
  797.     if estimate_node_a_better?(open_node, close_node)
  798.       return create_route_list(open_node, open_coordinate)
  799.     else
  800.       return create_route_list(close_node, close_coordinate)
  801.     end
  802.   end
  803.   #--------------------------------------------------------------------------
  804.   # ● ノードの優劣判定(推定経路用)
  805.   #--------------------------------------------------------------------------
  806.   def estimate_node_a_better?(node_a, node_b)
  807.     return true  if node_a[RANK_B] < node_b[RANK_B]
  808.     return false if node_a[RANK_B] > node_b[RANK_B]
  809.     return true  if node_a[RANK_C] < node_b[RANK_C]
  810.     return false if node_a[RANK_C] > node_b[RANK_C]
  811.     return true
  812.   end
  813.   #--------------------------------------------------------------------------
  814.   # ● 探索結果を再利用
  815.   #--------------------------------------------------------------------------
  816.   def reuse_route_list(route_list, reuse_distance, gx, gy, gz=0)
  817.     return false unless route_list
  818.     @sx, @sy, @sz = character_x, character_y, character_z
  819.     return false unless index = route_list.index([@sx, @sy, @sz]) # 始点を含んでいるか判定
  820.     (route_list.size - index).times { route_list.pop }
  821.     return false if route_list.empty?
  822.     @gx, @gy, @gz = gx, gy, gz
  823.     if index = route_list.index([@gx, @gy, @gz])                  # 終点を含んでいるか判定
  824.       index.times { route_list.shift }
  825.       return false unless route_list_valid?(route_list)           # 経路を使用可能か判定
  826.       return route_list
  827.     else
  828.       return false unless ending_distance?(*route_list[0], reuse_distance)
  829.       return false unless route_list_valid?(route_list)           # 経路を使用可能か判定
  830.       reuse_search(route_list)                                    # 以前の探索結果を使用して経路探索
  831.     end
  832.   end
  833.   #--------------------------------------------------------------------------
  834.   # ● 経路を使用可能か判定
  835.   #--------------------------------------------------------------------------
  836.   def route_list_valid?(route_list)
  837.     @x, @y, @z = @sx, @sy, @sz
  838.     route_list.reverse.each do |coordinate|
  839.       nx, ny, nz = *coordinate
  840.       result = exception_decision?(nx, ny, nz)
  841.       return false if result == false                             # 通行禁止の場合
  842.       if result == nil                                            # 通行可能判定に委ねる場合
  843.         @collide_through = collide_through?(nx, ny, nz)
  844.         if nx == @x
  845.           return false unless ny == @y ? nil          : (ny > @y ? passable?(2)             : passable?(8)            )
  846.         elsif nx > @x
  847.           return false unless ny == @y ? passable?(6) : (ny > @y ? diagonal_passable?(6, 2) : diagonal_passable?(6, 8))
  848.         else
  849.           return false unless ny == @y ? passable?(4) : (ny > @y ? diagonal_passable?(4, 2) : diagonal_passable?(4, 8))
  850.         end
  851.       end
  852.       @x , @y , @z  = nx, ny, nz
  853.     end
  854.     return true
  855.   end
  856.   #--------------------------------------------------------------------------
  857.   # ● 以前の探索結果を使用して経路探索
  858.   #--------------------------------------------------------------------------
  859.   def reuse_search(route_list)
  860.     init_process(@gx, @gy, @gz)
  861.     parent = [@sx, @sy, @sz]
  862.     route_list.reverse.each do |coordinate|
  863.       @open_list[coordinate] = create_node(*coordinate, parent, 0)  # 移動距離に細工をして追加
  864.       parent = coordinate
  865.     end
  866.     @close_list = @open_list.dup
  867.     return main_process
  868.   end
  869. end

  870. #==============================================================================
  871. # ■ マルチレイヤーシステムとの競合回避 ■
  872. #==============================================================================
  873. if $imported[:CanariAlternate_Multilayer]
  874.   #==============================================================================
  875.   # ■ Game_CharacterBase
  876.   #------------------------------------------------------------------------------
  877.   #  キャラクターを扱う基本のクラスです。全てのキャラクターに共通する、座標やグ
  878.   # ラフィックなどの基本的な情報を保持します。
  879.   #==============================================================================
  880.   class Game_CharacterBase
  881.     #--------------------------------------------------------------------------
  882.     # ● z 座標の取得 [◆再定義]
  883.     #--------------------------------------------------------------------------
  884.     def z
  885.       return current_floor
  886.     end
  887.   end
  888.   #==============================================================================
  889.   # ■ Route_Search
  890.   #------------------------------------------------------------------------------
  891.   #  経路探索の処理を定義したクラスです。
  892.   #==============================================================================
  893.   class Route_Search
  894.     #--------------------------------------------------------------------------
  895.     # ● 定数
  896.     #--------------------------------------------------------------------------
  897.     DummyEvent = Game_CharacterBase.new     # ダミーのイベント
  898.     #--------------------------------------------------------------------------
  899.     # ● z 座標の変換 [◆再定義]
  900.     #--------------------------------------------------------------------------
  901.     def xyz_to_z(x, y, z)
  902.       return $game_map.floor_xy(x, y, z)
  903.     end
  904.     #--------------------------------------------------------------------------
  905.     # ● 進入禁止のリージョンIDの座標か判定 [◆再定義]
  906.     #--------------------------------------------------------------------------
  907.     def not_enter_coordinate?(x, y, z)
  908.       return not_enter_region_id.include?($game_map.region_id(x, y, z))
  909.     end
  910.     #--------------------------------------------------------------------------
  911.     # ● 通行可能判定の呼び出し [◆再定義]
  912.     #--------------------------------------------------------------------------
  913.     def base_passable?(name, *d)
  914.       DummyEvent.current_floor = @z
  915.       return $game_map.temp_execute(DummyEvent) { @character.send(name, @x, @y, *d) }
  916.     end
  917.   end
  918. end
复制代码

他还需要一个前置脚本
  1. #==============================================================================
  2. # ■ 注釈取得スクリプト (VX Ace用)
  3. #------------------------------------------------------------------------------
  4. # 製作者     : CanariAlternate
  5. # サイト名   : カルトの鳥篭
  6. # サイトURL  : [url]http://canarialt.blog.fc2.com[/url]
  7. #------------------------------------------------------------------------------
  8. # ■ 概要 : 注釈を取得する処理を定義する。
  9. #
  10. # ■ 必須 : なし
  11. #
  12. # ■ 位置 : 「Game_Event」より下
  13. #------------------------------------------------------------------------------
  14. # ■ 注釈での設定方法
  15. #   以下の例のように実行内容の1行目から注釈以外のコマンドまでの注釈のどこかに
  16. #   キーワードを記述すると有効(結果を反転してる場合には無効)になります。
  17. #
  18. #             例 : 実行内容
  19. #                   ◆注釈:キーワードA キーワードC
  20. #                   :    :キーワードG キーワードF
  21. #                   :    :キーワードE
  22. #                   ◆注釈:キーワードD
  23. #                   :    :キーワードH キーワードB
  24. #                   ◆変数の操作などの注釈以外のイベントコマンド
  25. #                   ◆注釈:ここ以下に書いても無視される。
  26. #                   ◆
  27. #
  28. # ※キーワードとなる文字列の設定は正規表現です。詳しいことはヘルプを「正規表現」で検索すると出てきます。
  29. #   簡単に言うと / で囲まれた文字列があるかないかで判定します。
  30. #   \ とか ! みたいな記号を使いたい時はヘルプを参照して下さい。
  31. #   設定例、/\[キーワード\]/  #=>  [キーワード]
  32. #
  33. # ※キーワードを記述すると有効と無効が切り替わります。初期状態は無効です。
  34. #   例 : 初期状態を反転, 記述したイベントの全ての頁を反転, 記述した頁のみを反転
  35. #   まず  「初期状態を反転」                  で有効に
  36. #   次に  「記述したイベントの全ての頁を反転」で無効に
  37. #   最後に「記述した頁のみを反転」            で有効に
  38. #------------------------------------------------------------------------------
  39. # 更新履歴 : 2012/10/05 Ver1.00 コードを共通処理スクリプトに統一
  40. #            2012/10/06 Ver1.01 コードを共通処理スクリプトから分離
  41. #            2012/10/08 Ver1.02 コードの整理
  42. #            2013/02/19 Ver1.03 共通処理スクリプトの廃止による変更を施した。
  43. #==============================================================================

  44. $imported ||= {}
  45. $imported[:CanariAlternate_EventNote] = true

  46. #==============================================================================
  47. # ■ Calt
  48. #------------------------------------------------------------------------------
  49. #  CanariAlternateが製作したスクリプトの管理用モジュール
  50. #==============================================================================
  51. module Calt
  52.   #--------------------------------------------------------------------------
  53.   # ● イベントコマンドから注釈を取得 [新規]
  54.   #--------------------------------------------------------------------------
  55.   def self.Read_EventNote(list)
  56.     return String.new unless list && list[i = 0].code == 108
  57.     note = list[i].parameters[0]
  58.     note += "\n#{list[i].parameters[0]}" while [108, 408].include?(list[i += 1].code)
  59.     return note
  60.   end
  61. end

  62. #==============================================================================
  63. # ■ Game_Event
  64. #------------------------------------------------------------------------------
  65. #  イベントを扱うクラスです。条件判定によるイベントページ切り替えや、並列処理
  66. # イベント実行などの機能を持っており、Game_Map クラスの内部で使用されます。
  67. #==============================================================================
  68. class Game_Event < Game_Character
  69.   #--------------------------------------------------------------------------
  70.   # ● イベントページの設定をセットアップ [追加]
  71.   #--------------------------------------------------------------------------
  72.   alias setup_page_settings_EventNote setup_page_settings
  73.   def setup_page_settings
  74.     setup_page_settings_EventNote
  75.     setup_note_settings
  76.   end
  77.   #--------------------------------------------------------------------------
  78.   # ● イベントページ更新時に注釈から設定を取得 [新規]
  79.   #--------------------------------------------------------------------------
  80.   def setup_note_settings
  81.     # 注釈の取得処理はここに追加
  82.   end
  83.   #--------------------------------------------------------------------------
  84.   # ● 1頁目の注釈にキーワードが含まれているか判定 [新規]
  85.   #--------------------------------------------------------------------------
  86.   def whole_note_include?(keyword)
  87.     return @event.pages[0].event_note =~ keyword ? true : false
  88.   end
  89.   #--------------------------------------------------------------------------
  90.   # ● この頁の注釈にキーワードが含まれているか判定 [新規]
  91.   #--------------------------------------------------------------------------
  92.   def event_note_include?(keyword)
  93.     return @page.event_note =~ keyword ? true : false
  94.   end
  95. end

  96. #==============================================================================
  97. # ■ RPG::Event::Page
  98. #------------------------------------------------------------------------------
  99. #  イベントページのデータクラス。
  100. #==============================================================================
  101. class RPG::Event::Page
  102.   #--------------------------------------------------------------------------
  103.   # ● 取得する注釈内容を変更 [新規]
  104.   #--------------------------------------------------------------------------
  105.   attr_writer   :event_note
  106.   #--------------------------------------------------------------------------
  107.   # ● この頁の注釈を取得 [新規]
  108.   #--------------------------------------------------------------------------
  109.   def event_note
  110.     return @event_note ||= Calt::Read_EventNote(@list)
  111.   end
  112. end
复制代码

PS:还有一个寻路脚本,同样有BUG,就是那个等待到移动结束没法用,不知道有没有那个脚本大佬帮帮忙
  1. #-------------------------------------------------------------------------------
  2. # * [ACE] Khas 寻路
  3. #-------------------------------------------------------------------------------
  4. # * By Khas Arcthunder - arcthunder.site40.net
  5. # * Version: 1.0 EN
  6. # * Released on: 28/02/2012
  7. #
  8. #-------------------------------------------------------------------------------
  9. # * Terms of Use
  10. #-------------------------------------------------------------------------------
  11. # When using any Khas script, you agree with the following terms:
  12. # 1. You must give credit to Khas;
  13. # 2. All Khas scripts are licensed under a Creative Commons license;
  14. # 3. All Khas scripts are for non-commercial projects. If you need some script
  15. #    for your commercial project (I accept requests for this type of project),
  16. #    send an email to nilokruch@live.com with your request;
  17. # 4. All Khas scripts are for personal use, you can use or edit for your own
  18. #    project, but you are not allowed to post any modified version;
  19. # 5. You can’t give credit to yourself for posting any Khas script;
  20. # 6. If you want to share a Khas script, don’t post the script or the direct
  21. #    download link, please redirect the user to arcthunder.site40.net
  22. # 7. You are not allowed to convert any of Khas scripts to another engine,
  23. #    such converting a RGSS3 script to RGSS2 or something of that nature.
  24. #
  25. # Check all terms at [url]http://arcthunder.site40.net/terms/[/url]
  26. #
  27. #-------------------------------------------------------------------------------
  28. # * Features
  29. #-------------------------------------------------------------------------------
  30. # Smart pathfinding
  31. # Fast Algorithm
  32. # Easy to use
  33. # Plug'n'Play
  34. # Game_Character objects compatible
  35. # Log tool
  36. #
  37. #-------------------------------------------------------------------------------
  38. # * 使用方法
  39. #-------------------------------------------------------------------------------
  40. # 使用脚本
  41. # find_path(id,x,y)
  42. #
  43. # id => -1玩家, 0本事件,x事件ID
  44. #
  45. # find_path(id,x,y,true)
  46. # 等待直到结束
  47. #
  48. # 如果你要启用寻路日志的功能, 请将"Log"设定为true.
  49. #
  50. #-------------------------------------------------------------------------------
  51. # * Register
  52. #-------------------------------------------------------------------------------
  53. $khas_awesome = [] if $khas_awesome.nil?
  54. $khas_awesome << ["Pathfinder",1.0]
  55. #-------------------------------------------------------------------------------
  56. # * Script
  57. #-------------------------------------------------------------------------------
  58. class Game_Interpreter
  59.   def find_path(char,fx,fy,wait=false)
  60.     $game_map.refresh if $game_map.need_refresh
  61.     character = get_character(char)
  62.     return if character.nil?
  63.     return unless Path_Core.runnable?(character,fx,fy)
  64.     path = Path_Core.run(character,fx,fy)
  65.     return if path.nil?
  66.     route = RPG::MoveRoute.new
  67.     route.repeat = false
  68.     route.wait = wait
  69.     route.skippable = true
  70.     route.list = []
  71.     path << 0x00
  72.     path.each { |code| route.list << RPG::MoveCommand.new(code)}
  73.     character.force_move_route(route)
  74.     @moving_character = character if wait
  75.   end
  76. end
  77. class Path
  78.   attr_accessor :axis
  79.   attr_accessor :from
  80.   attr_accessor :cost
  81.   attr_accessor :dir
  82.   def initialize(x,y,f,c,d)
  83.     @axis = [x,y]
  84.     @from = f
  85.     @cost = c
  86.     @dir = d
  87.   end
  88. end
  89. module Path_Core
  90.   Log = false #是否启用寻路日志
  91.   Directions = {[1,0] => 3,[-1,0] => 2,[0,-1] => 4,[0,1] => 1}
  92.   def self.runnable?(char,x,y)
  93.     return false unless $game_map.valid?(x,y)
  94.     return false if char.collide_with_characters?(x,y)
  95.     $game_map.all_tiles(x,y).each { |id|
  96.     flag = $game_map.tileset.flags[id]
  97.     next if flag & 0x10 != 0
  98.     return flag & 0x0f != 0x0f}
  99.     return false
  100.   end
  101.   def self.run(char,fx,fy)
  102.     return nil if char.x == fx && char.y == fy
  103.     st = Time.now
  104.     @char = char
  105.     @start = Path.new(@char.x,@char.y,nil,0,nil)
  106.     @finish = Path.new(fx,fy,nil,0,nil)
  107.     @list = []
  108.     @queue = []
  109.     @preference = ((@char.x-fx).abs > (@char.y-fy).abs ? 0x0186aa : 0x01d)
  110.     class << @list
  111.       def new_path?(path_class)
  112.         for path in self
  113.           return false if path.axis == path_class.axis
  114.         end
  115.         return true
  116.       end
  117.     end
  118.     class << @queue
  119.       def new_path?(path_class)
  120.         for path in self
  121.           return false if path.axis == path_class.axis
  122.         end
  123.         return true
  124.       end
  125.     end
  126.     if @preference & 0x02 == 0x02
  127.       @queue << Path.new(@char.x,@char.y+1,@start,1,[0,1]) if @char.passable?(@char.x,@char.y,2)
  128.       @queue << Path.new(@char.x,@char.y-1,@start,1,[0,-1]) if @char.passable?(@char.x,@char.y,8)
  129.       @queue << Path.new(@char.x+1,@char.y,@start,1,[1,0]) if @char.passable?(@char.x,@char.y,6)
  130.       @queue << Path.new(@char.x-1,@char.y,@start,1,[-1,0]) if @char.passable?(@char.x,@char.y,4)
  131.       @list << @start
  132.       loop do
  133.         break if @queue.empty?
  134.         @cpath = @queue[0]
  135.         if @cpath.axis == @finish.axis
  136.           @finish.cost = @cpath.cost
  137.           @finish.from = @cpath
  138.           break
  139.         end
  140.         @list << @cpath
  141.         @path_array = []
  142.         p1 = Path.new(@cpath.axis[0]+1,@cpath.axis[1],@cpath,@cpath.cost+1,[1,0])
  143.         p2 = Path.new(@cpath.axis[0]-1,@cpath.axis[1],@cpath,@cpath.cost+1,[-1,0])
  144.         p3 = Path.new(@cpath.axis[0],@cpath.axis[1]+1,@cpath,@cpath.cost+1,[0,1])
  145.         p4 = Path.new(@cpath.axis[0],@cpath.axis[1]-1,@cpath,@cpath.cost+1,[0,-1])
  146.         @path_array << p3 if @char.passable?(@cpath.axis[0],@cpath.axis[1],2) && @list.new_path?(p3) && @queue.new_path?(p3)
  147.         @path_array << p4 if @char.passable?(@cpath.axis[0],@cpath.axis[1],8) && @list.new_path?(p4) && @queue.new_path?(p4)
  148.         @path_array << p1 if @char.passable?(@cpath.axis[0],@cpath.axis[1],6) && @list.new_path?(p1) && @queue.new_path?(p1)
  149.         @path_array << p2 if @char.passable?(@cpath.axis[0],@cpath.axis[1],4) && @list.new_path?(p2) && @queue.new_path?(p2)
  150.         @path_array.each { |path| @queue << path }
  151.         @queue.delete(@cpath)
  152.       end
  153.     else
  154.       @queue << Path.new(@char.x+1,@char.y,@start,1,[1,0]) if @char.passable?(@char.x,@char.y,6)
  155.       @queue << Path.new(@char.x-1,@char.y,@start,1,[-1,0]) if @char.passable?(@char.x,@char.y,4)
  156.       @queue << Path.new(@char.x,@char.y+1,@start,1,[0,1]) if @char.passable?(@char.x,@char.y,2)
  157.       @queue << Path.new(@char.x,@char.y-1,@start,1,[0,-1]) if @char.passable?(@char.x,@char.y,8)
  158.       @list << @start
  159.       loop do
  160.         break if @queue.empty?
  161.         @cpath = @queue[0]
  162.         if @cpath.axis == @finish.axis
  163.           @finish.cost = @cpath.cost
  164.           @finish.from = @cpath
  165.           break
  166.         end
  167.         @list << @cpath
  168.         @path_array = []
  169.         p1 = Path.new(@cpath.axis[0]+1,@cpath.axis[1],@cpath,@cpath.cost+1,[1,0])
  170.         p2 = Path.new(@cpath.axis[0]-1,@cpath.axis[1],@cpath,@cpath.cost+1,[-1,0])
  171.         p3 = Path.new(@cpath.axis[0],@cpath.axis[1]+1,@cpath,@cpath.cost+1,[0,1])
  172.         p4 = Path.new(@cpath.axis[0],@cpath.axis[1]-1,@cpath,@cpath.cost+1,[0,-1])
  173.         @path_array << p1 if @char.passable?(@cpath.axis[0],@cpath.axis[1],6) && @list.new_path?(p1) && @queue.new_path?(p1)
  174.         @path_array << p2 if @char.passable?(@cpath.axis[0],@cpath.axis[1],4) && @list.new_path?(p2) && @queue.new_path?(p2)
  175.         @path_array << p3 if @char.passable?(@cpath.axis[0],@cpath.axis[1],2) && @list.new_path?(p3) && @queue.new_path?(p3)
  176.         @path_array << p4 if @char.passable?(@cpath.axis[0],@cpath.axis[1],8) && @list.new_path?(p4) && @queue.new_path?(p4)
  177.         @path_array.each { |path| @queue << path }
  178.         @queue.delete(@cpath)
  179.       end
  180.     end
  181.     if @finish.from.nil?
  182.       return nil
  183.     else
  184.       steps = [@finish.from]
  185.       loop do
  186.         cr = steps[-1]
  187.         if cr.cost == 1
  188.           @result = []
  189.           steps.each { |s| @result << Directions[s.dir]}
  190.           break
  191.         else
  192.           steps << cr.from
  193.         end
  194.       end
  195.       self.print_log(Time.now-st) if Log
  196.       return @result.reverse
  197.     end
  198.   end
  199.   def self.print_log(time)
  200.     print "\n--------------------\n"
  201.     print "Khas Pathfinder\n"
  202.     print "Time: #{time}\n"
  203.     print "Size: #{@result.size}\n"
  204.     print "--------------------\n"
  205.   end
  206. end
复制代码

最佳答案

查看完整内容

大概是这样用? khas的寻路 74行 @moving_character = character if wait 后面 end 前面加上 [pre lang="ruby"] if wait Fiber.yield while character.x != fx || character.y != fy end[/pre]

评分

参与人数 1赞 +1 收起 理由
21世纪游戏吧 + 1 塞糖

查看全部评分

老鹰

梦石
40
星屑
38044
在线时间
7127 小时
注册时间
2012-5-26
回帖
3306

极短24评委极短23参与极短22参与极短21评委老司机慢点开短篇十吟唱者组别冠军开拓者剧作品鉴家

发表于 2018-4-29 18:26:25 | 显示全部楼层
本帖最后由 百里_飞柳 于 2018-4-29 22:52 编辑
  1. route_search({:char_id => 5})
复制代码

大概是这样用?

khas的寻路
74行 @moving_character = character if wait 后面 end 前面加上
  1.    
  2. if wait
  3.   Fiber.yield while character.x != fx || character.y != fy
  4. end
复制代码
回复

使用道具 举报

梦石
0
星屑
544
在线时间
59 小时
注册时间
2017-11-19
回帖
62
 楼主| 发表于 2018-4-30 11:02:14 | 显示全部楼层
百里_飞柳 发表于 2018-4-29 22:50
大概是这样用?

khas的寻路

嗯,是这样

点评

有效,我的意思就是有效  发表于 2018-4-30 11:27
那有没有效……  发表于 2018-4-30 11:08
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部