查看: 2444|回复: 0

[已经解决] 这个寻路脚本,怎么让他到达目标后不要跑来跑去

[复制链接]
梦石
0
星屑
12161
在线时间
4435 小时
注册时间
2014-4-11
回帖
5616

开拓者

发表于 2016-2-24 21:40:36 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
我想要到达目标后就不要动,当坐标差<=1时不移动


  1. #==================================================================#
  2. # AStar Core v1.01 by 禾西 on 2012.01.02
  3. #------------------------------------------------------------------#
  4. # (此算法基于 4 方向)
  5. # public methods
  6. # * new(Game_Map map)
  7. #     生成關聯地圖的 A* 實例
  8. # * set_origin(int ox, int oy)
  9. #     設置起始點(超過地圖范圍會引起 Runtime Error )
  10. # * set_target(int tx, int ty)
  11. #     設置終止點(超過地圖范圍會引起 Runtime Error )
  12. # * Array do_search(bool print_rst = false)
  13. #     進行尋路,返回包含移動信息的數組
  14. #
  15. # private methods
  16. # * int _f(x, y)
  17. #     f 值算法
  18. # * print_data
  19. #     數據打印算法(此方法僅在 do_search(true) 時被使用)
  20. #==================================================================#
  21. #
  22. #調用方法
  23. #    astr = AStar.new($game_map)
  24. #    astr.set_origin($game_player.x, $game_player.y)
  25. #    astr.set_target(0, 0)
  26. #    path = astr.do_search
  27. #
  28. #==================================================================#
  29. class AStar
  30.   Point = Struct.new(:x, :y)
  31.   public
  32.   # ============================================================== #
  33.   # 初始化數據
  34.   # ============================================================== #
  35.   def initialize(map)
  36.     @map_width  = map.width
  37.     @map_height = map.height
  38.     @g_data = Table.new(@map_width, @map_height)
  39.     @f_data = Table.new(@map_width, @map_height)
  40.     @p_data = Table.new(@map_width, @map_height)
  41.     @ox = 0;@oy = 0
  42.     @tx = 0;@ty = 0
  43.     @openList = []
  44.     @g = 0
  45.     @search_done = false
  46.   end
  47.   # ============================================================== #
  48.   # 設置 起始點
  49.   # ============================================================== #
  50.   def set_origin(ox, oy)
  51.     @ox = ox;@oy = oy
  52.     if is_Overmap(ox, oy)
  53.       raise RuntimeError, "Origin location is overmap!"
  54.     end
  55.   end
  56.   # ============================================================== #
  57.   # 設置 目標點
  58.   # ============================================================== #
  59.   def set_target(tx, ty)
  60.     @tx = tx;@ty = ty
  61.     if is_Overmap(tx, ty)
  62.       raise RuntimeError, "Target location is overmap!"
  63.     end
  64.   end
  65.   # ============================================================== #
  66.   # 開始尋路
  67.   # ============================================================== #
  68.   # 主邏輯
  69.   def do_search(print_rst = false)
  70.     x = @ox;y = @oy
  71.     @g_data[x, y] = 2;@f_data[x, y] = 1
  72.     @openList << [x, y]
  73.     t0 = Time.now
  74.     t = 0
  75.     begin
  76.       t += 1
  77.       point = @openList.shift
  78.       return [] if point == nil
  79.     check_4dir(point[0], point[1]) # ->檢查 4 方向
  80.     end until @search_done
  81.     if @g_data[@tx,@ty] == 1
  82.       @tx = point[0];@ty = point[1];
  83.     end
  84.     t1 = Time.now
  85.     make_path # ->生成路徑
  86.     if print_rst
  87.       print "#{t1 - t0}, #{t}\n"
  88.       print_data # ->打印數據
  89.     end
  90.     return @path
  91.   end

  92.   private
  93.   # ============================================================== #
  94.   # 檢查 4 方向
  95.   def check_4dir(x, y)
  96.     @g = @g_data[x, y] + 1
  97.     mark_point(x, y - 1, 8) # ->檢查單點
  98.     mark_point(x, y + 1, 2) # ->檢查單點
  99.     mark_point(x - 1, y, 4) # ->檢查單點
  100.     mark_point(x + 1, y, 6) # ->檢查單點
  101.   end
  102.   # ============================================================== #
  103.   # 檢查單點
  104.   def mark_point(x, y, dir)
  105.         if x == @tx && y == @ty
  106.       @search_done = true
  107.     end

  108.     if is_Overmap(x, y) # ->檢查地圖是否超界
  109.       return
  110.     end
  111.     if @g_data[x, y] > 1
  112.       return
  113.     end
  114.     if check_passibility(x, y, dir) # ->檢查通行度
  115.       f = _f(x, y)
  116.       @g_data[x, y] = @g
  117.       @f_data[x, y] = f
  118.       point = @openList[0]
  119.       if point.nil?
  120.         @openList.push [x, y]
  121.       elsif (f <= @f_data[point[0], point[1]])
  122.         @openList.unshift [x, y]
  123.       else
  124.         @openList.push [x, y]
  125.       end
  126.     else
  127.       @g_data[x, y] = 1
  128.       @f_data[x, y] = _f(x, y)
  129.     end
  130.   end
  131.   # ============================================================== #
  132.   # 生成路徑
  133.   def make_path
  134.     x = @tx;y = @ty
  135.     @path = []
  136.     while !(x == @ox && y == @oy) #TODO:有過多障礙時死轉……
  137.       @g = @g_data[x, y]
  138.       @best_f = 0
  139.       dir = 0
  140.       dir = make_step(x, y - 1, 2)||dir # ->生成單步
  141.       dir = make_step(x, y + 1, 8)||dir # ->生成單步
  142.       dir = make_step(x - 1, y, 6)||dir # ->生成單步
  143.       dir = make_step(x + 1, y, 4)||dir # ->生成單步
  144.       @path.unshift(dir)
  145.       case dir
  146.       when 2 then y -= 1;
  147.       when 8 then y += 1;
  148.       when 6 then x -= 1;
  149.       when 4 then x += 1;
  150.       end
  151.       @p_data[x, y] = 1
  152.     end
  153.   end
  154.   # ============================================================== #
  155.   # 生成單步
  156.   def make_step(x, y, dir)
  157.     if @g_data[x, y].nil? || @p_data[x, y] == 1
  158.       return nil
  159.     end
  160.     if (@g - @g_data[x, y]) == 1 || @g == 1
  161.       f = @f_data[x, y]
  162.       if f > 0 && (@best_f == 0 || f < @best_f)
  163.         @best_f = f
  164.         return dir
  165.       end
  166.     end
  167.     return nil
  168.   end
  169.   # ============================================================== #
  170.   # 檢查地圖通行度
  171.   def check_passibility(x, y, dir)
  172.     case dir
  173.     when 2 then y -= 1;
  174.     when 8 then y += 1;
  175.     when 4 then x += 1;
  176.     when 6 then x -= 1;
  177.     end
  178.     return $game_player.passable?(x, y, dir)
  179.   end
  180.   # ============================================================== #
  181.   # 檢查地圖是否超界
  182.   def is_Overmap(x, y)
  183.     return (x|y|(@map_width - x - 1)|(@map_height - y - 1)) < 0
  184.   end
  185.   # ============================================================== #
  186.   # f 值算法
  187.   def _f(x, y)
  188.     return ((x - @tx).abs + (y - @ty).abs) + @g
  189.   end
  190.   # ============================================================== #
  191.   # 打印數據
  192.   def print_data
  193.     strf = ""
  194.     strg = ""
  195.     for y in [email]0...@f_data.ysize[/email]
  196.       for x in [email]0...@f_data.xsize[/email]
  197.         if @f_data[x, y] == 0
  198.           strf += "  "
  199.         else
  200.           strf += sprintf("%02d", @f_data[x, y])
  201.         end
  202.         strf += ","
  203.         if @g_data[x, y] < 2
  204.           strg += sprintf("%02d", @g_data[x, y])#"  "
  205.         else
  206.           strg += sprintf("%02d", @g_data[x, y])
  207.         end
  208.         strg += ","
  209.       end
  210.       strf += "\n"
  211.       strg += "\n"
  212.     end
  213.     print "f:\n"
  214.     print strf + "\n"
  215.     print "g:\n"
  216.     print strg + "\n"
  217.     strp = ""
  218.     for y in [email]0...@p_data.ysize[/email]
  219.       for x in [email]0...@p_data.xsize[/email]
  220.         if @p_data[x, y] == 0
  221.           strp += "  "
  222.         else
  223.           strp += sprintf("%02d", @p_data[x, y])
  224.         end
  225.         strp += ","
  226.       end
  227.       strp += "\n"
  228.     end

  229.     print "p:\n"
  230.     print strp
  231.     print @path
  232.   end
  233. end

  234. class Game_Character
  235.   def move_dr(dr)
  236.     case dr
  237.     when 1
  238.       move_lower_left
  239.     when 2
  240.       move_down
  241.     when 3
  242.       move_lower_right
  243.     when 4
  244.       move_left
  245.     when 6
  246.       move_right
  247.     when 7
  248.       move_upper_left
  249.     when 8
  250.       move_up
  251.     when 9
  252.       move_upper_right
  253.     else
  254.       move_toward_player
  255.     end
  256.   end
  257. #简化脚本,在事件中需要调用的命令减少,并可在移动规则中自定义。
  258. #使用命令:astarmove(终止ID,出发ID(可不填,不填则默认为该事件))
  259. #id:0为player,其他为事件
  260.   def astarmove(to = 0,from = "self")
  261.     from = @id if from=="self"
  262.     astr = AStar.new($game_map)
  263.     from = from==0 ? $game_player : $game_map.events[from]
  264.     to = to==0 ? $game_player : $game_map.events[to]
  265.     #p from.id,to.id if from.is_a?(Game_Event)
  266.     unless from.moving?
  267.       if from != nil and to != nil
  268.       astr.set_origin(from.x,from.y)
  269.       astr.set_target(to.x,to.y)
  270.       dr = astr.do_search
  271.       move_dr(dr[0])
  272.       end
  273.     end
  274.   end
  275. end
复制代码
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

在本版发帖返回顶部