查看: 1927|回复: 0

[已经过期] 怎么看自动寻路的路程

[复制链接]
梦石
0
星屑
50
在线时间
85 小时
注册时间
2011-3-6
回帖
36
发表于 2016-12-13 18:04:12 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 VIPArcher 于 2016-12-13 18:54 编辑

这是我网上下的自动寻路脚本
想要可以输出一个变量显示他寻路的路程
比如找到寻路最短路线是6格就输出6
或者有更好的脚本有人提供下吗OAO
谢谢!
  1. #encoding:utf-8
  2. #==============================================================================
  3. #   ■ 事件自动寻路
  4. #
  5. #   本插件的前置插件为:《多帧4_8方图,4_8向行走》+《多帧事件连续移动》
  6. #   1.定点,障碍绕行,可后台超低速寻路,保障帧率
  7. #   2.多个事件一起寻路,帧率影响不太明显,但会寻路比较慢
  8. #------------------------------------------------------------------------------
  9. #使用说明:
  10. #   让事件可以寻路,调用make_passing(true),false则该事件不能寻路
  11. #   自动寻向指定点,move_toward_pos(x,y)函数
  12. #   取消当前寻路指令,cancel_pathing函数
  13. #   如果有几个事件一起寻路的话,最好不要同时放出,建议间隔10帧,若目标很远要隔久一点。
  14. #   若要追逐玩家的话,不要每帧都追,隔几十帧追一下。
  15. #
  16. #   注意:是全地图搜索。越远越慢,地形越迷宫越慢,建议距离不要超过100图块
  17. #------------------------------------------------------------------------------
  18. #  by rmav (有任何问题请毫无顾忌滴提出)
  19. #
  20. #  
  21. #   v1.04 若在路径中发生意外,重新寻路
  22. #==============================================================================
  23. $imported ||= {}
  24. $imported[:rmav_pathing] = 20140213.1
  25. module Rmav
  26.   Opt_pathing={
  27.     detour: true,       #在路径上碰到临时障碍,是否绕行
  28.                         ##不绕行的话,障碍消失,会自动继续下去
  29.    
  30.     total_cycles: 50,   #越大寻路越快,但在超大地图迷宫地形上相隔很远的话,容易卡顿
  31.                         ##如果在游戏中掉帧明显,可降低10
  32.                         
  33.     failed_and_stop: false,  #全地图寻路失败时,停止
  34.     failed_and_rand: false,  #全地图寻路失败时,随机移动
  35.     failed_and_back: true    #全地图寻路失败时,在后台极低速一直寻路
  36.                              ##全地图寻路失败时,以上只能三选一
  37.    
  38.   }
  39.   
  40.   

  41. class Path_node
  42. attr_accessor :i,:j,:f,:g,:h,:pre,:next,:pos
  43.         def initialize(point=[0,0])
  44.                 @i=point[0]
  45.                 @j=point[1]
  46.                 @f=@g=@h=0
  47.                 @pre=@next=nil
  48.         end
  49.         def p
  50.                 [@i,@j]
  51.         end
  52.        
  53.         def cal_fgh(s_p, e_p)
  54.                 if @pre
  55.                         @g=@pre.g+(@pre.p[0]-@i).abs+(@pre.p[1]-@j).abs
  56.                 else
  57.                         @g=(s_p[0]-@i).abs+(s_p[1]-@j).abs
  58.                 end
  59.                 @h=(e_p[0]-@i).abs+(e_p[1]-@j).abs
  60.                 @f=@g+@h
  61.         end
  62. end

  63. class Pathfinder
  64.   @@total_cycles_per_frame=Rmav::Opt_pathing[:total_cycles]
  65.   @@left_cycles_curFrame=Rmav::Opt_pathing[:total_cycles]
  66.   @@finder_cnt=0
  67.   @@total_low_speeds=0
  68.   @@mini_cycles_per_frame=10
  69.   @@mini_finder_cnt=0
  70.   @@low_speed_max=10
  71.   def initialize(passable,p_max=[99,99],p_min=[0,0])
  72.     @open_nodes=[]
  73.     @open_points={}
  74.     @close_points={}
  75.     @passable=passable
  76.                 @p_min=p_min
  77.                 @p_max=p_max
  78.     @cancel=false
  79.     @mini_cancel=false
  80.     @low_speed=0
  81.   end
  82.   def set_low_speed(low_speed)
  83.     tmp=[low_speed,@@low_speed_max].min
  84.     if @@total_low_speeds+@low_speed<@@total_cycles_per_frame      
  85.       @low_speed=tmp
  86.       @@total_low_speeds+=@low_speed
  87.     end   
  88.   end
  89.   def self.reset_left_cycles
  90.     @@left_cycles_curFrame=Rmav::Opt_pathing[:total_cycles]
  91.   end
  92.   def self.clear_left_cycles
  93.     @@left_cycles_curFrame=0
  94.   end



  95. def min_heapify(sz,i)
  96.         return nil if sz.size<=i || i<0
  97.         l_child=r_child=min_child=0
  98.         cur_size=sz.size
  99.         i_node=sz[i]
  100.         loop do
  101.                 l_child=2*i+1
  102.                 r_child=2*i+2
  103.                 if l_child>=cur_size
  104.                         break
  105.                 elsif r_child>=cur_size
  106.                         min_child=l_child
  107.                 else
  108.                         min_child=
  109.                         (sz[l_child].f<=sz[r_child].f)?(l_child):(r_child)
  110.                 end
  111.                
  112.                 if sz[min_child].f>=i_node.f
  113.                         break
  114.                 else
  115.                         sz[i]=sz[min_child]
  116.                         sz[i].pos=i
  117.                         i=min_child
  118.                 end
  119.         end
  120.         sz[i]=i_node
  121.         sz[i].pos=i
  122. end

  123. def heap_extract_min(sz)
  124.         return nil if sz.size<=0
  125.         min=sz[0]
  126.         sz[0] = sz[-1]
  127.         sz[0].pos=0
  128.         sz.pop
  129.         min_heapify(sz,0)
  130.         min
  131. end

  132. def heap_decrease_key(sz,i,f)
  133.         return nil if sz.size<=0|| sz[i].f<=f
  134.        
  135.         sz[i].f=f
  136.         tmp=i
  137.         i_node=sz[i]
  138.         parent=0
  139.         loop do
  140.                 parent=(tmp-1)/2
  141.                 if sz[parent].f>f
  142.                         sz[tmp]=sz[parent]
  143.                         sz[tmp].pos=tmp
  144.                         tmp=parent
  145.                 else
  146.                         break
  147.                 end
  148.           break if parent<=0
  149.         end
  150.         sz[tmp]=i_node
  151.         sz[tmp].pos=tmp
  152. end


  153. def min_heap_insert(sz,node)
  154.         f=node.f
  155.         node.f=999999999
  156.         node.pos=sz.size
  157.         sz<<node
  158.         heap_decrease_key(sz,sz.size-1,f)
  159. end

  160. def adj_p(p,d,p_max=@p_max,p_min=@p_min)
  161.   case d
  162.   when 2; [p[0],p[1]+1]  if p[1]+1<=p_max[1]
  163.   when 4; [p[0]-1,p[1]]  if p[0]-1>=p_min[0]
  164.   when 6; [p[0]+1,p[1]]  if p[0]+1<=p_max[0]
  165.   when 8; [p[0],p[1]-1]  if p[1]-1>=p_min[1]
  166.   end
  167. end

  168. def a_star(start_p,end_p)
  169.   return nil if start_p==end_p
  170.   @cancel=false
  171.   @@finder_cnt+=1
  172.   while (@@total_cycles_per_frame-@@total_low_speeds)/@@finder_cnt<8
  173.     Fiber.yield
  174.     if @cancel
  175.       @@finder_cnt-=1
  176.       @@total_low_speeds-=@low_speed
  177.       @low_speed=0
  178.       return nil
  179.     end
  180.   end
  181.         @open_nodes.clear
  182.         @open_points.clear
  183.         @close_points.clear
  184.   cycles_cnt=0
  185.         reach_end_node=nil
  186.         start_node=Path_node.new(start_p)
  187.         @open_nodes<<start_node
  188.                 node = @open_nodes.shift       
  189.                 [2,4,6,8].each{|d|
  190.       point=adj_p(node.p,d)
  191.                         if point&&@passable.call(node.i,node.j,d)
  192.                                 tmp=Path_node.new(point)
  193.                                 tmp.pre=node
  194.                                 tmp.cal_fgh(start_p, end_p)
  195.                                 min_heap_insert(@open_nodes,tmp)
  196.                                 @open_points[tmp.p]=tmp
  197.                         end
  198.                 }
  199.                 @close_points[node.p]=node
  200.                        
  201.         while(@open_nodes.size>0)
  202.       
  203.     if @low_speed>0
  204.       if cycles_cnt>@low_speed
  205.         @@left_cycles_curFrame-=cycles_cnt
  206.         cycles_cnt=0
  207.         Fiber.yield
  208.       end
  209.     elsif cycles_cnt>(@@total_cycles_per_frame-@@total_low_speeds)/@@finder_cnt
  210.       @@left_cycles_curFrame-=cycles_cnt
  211.       cycles_cnt=0
  212.       Fiber.yield
  213.     end
  214.     if @cancel      
  215.       break
  216.     end
  217.     if @@left_cycles_curFrame<1
  218.       Fiber.yield
  219.       break if @cancel
  220.     end

  221.                 min=heap_extract_min(@open_nodes)
  222.                 @open_points.delete(min.p)
  223.                 [2,4,6,8].each{|d|
  224.       point=adj_p(min.p,d)
  225.       next unless point
  226.       if point==end_p
  227.           tmp=Path_node.new(point)
  228.                                         tmp.pre=min         
  229.                                         reach_end_node=tmp
  230.         break
  231.       end
  232.                         if point!=min.pre.p&&@passable.call(min.i,min.j,d)
  233.                                 if !@close_points.has_key?(point)                                 
  234.                                         tmp=Path_node.new(point)
  235.                                         tmp.pre=min
  236.                                         tmp.cal_fgh(start_p, end_p)
  237.                                         tmp2=@open_points[tmp.p]
  238.                                         if tmp2
  239.                                                 if tmp2.g>tmp.g
  240.                                                         tmp2_f_pre=tmp2.f
  241.                                                         tmp2.pre=min
  242.                                                         tmp2.g=tmp.g
  243.                                                         new_f=tmp.g+tmp.h                                                       
  244.                                                         heap_decrease_key(@open_nodes,tmp2.pos,new_f)
  245.                                                 end
  246.                                         else       
  247.                                                 min_heap_insert(@open_nodes,tmp)
  248.                                                 @open_points[tmp.p]=tmp
  249.                                                
  250.           end
  251.                                
  252.                                 end
  253.                                
  254.                         end
  255.                 }
  256.                 @close_points[min.p]=min
  257.                 break if reach_end_node
  258.                 cycles_cnt+=1
  259.         end
  260.   @@finder_cnt-=1
  261.   @@total_low_speeds-=@low_speed
  262.   @low_speed=0
  263.   @open_nodes.clear
  264.         @open_points.clear
  265.         @close_points.clear
  266.         reach_end_node
  267. end

  268. def terminate
  269.         @open_nodes.clear
  270.         @open_points.clear
  271.         @close_points.clear
  272. end

  273. def mini_star(start_p,end_p,view=9)
  274.   return nil if start_p==end_p
  275.   @mini_cancel=false
  276.   @@mini_finder_cnt+=1
  277.   while @@mini_cycles_per_frame/@@mini_finder_cnt<5
  278.     Fiber.yield
  279.     if @mini_cancel
  280.       @@mini_finder_cnt-=1
  281.       return nil
  282.     end
  283.   end
  284.   cycles_cnt=0

  285.   p_min=[];p_max=[]
  286.   p_min[0]=(start_p[0]<end_p[0])?(start_p[0]-view/2):(end_p[0]-view/2)
  287.   p_min[1]=(start_p[1]<end_p[1])?(start_p[1]-view/2):(end_p[1]-view/2)
  288.   p_max[0]=(start_p[0]>end_p[0])?(start_p[0]+view/2):(end_p[0]+view/2)
  289.   p_max[1]=(start_p[1]>end_p[1])?(start_p[1]+view/2):(end_p[1]+view/2)
  290.   p_min[0]=@p_min[0] if p_min[0]<@p_min[0]
  291.   p_min[1]=@p_min[1] if p_min[1]<@p_min[1]
  292.   p_max[0]=@p_max[0] if p_max[0]>@p_max[0]
  293.   p_max[1]=@p_max[1] if p_max[1]>@p_max[1]
  294.         open_nodes=[]
  295.         open_points={}
  296.         close_points={}
  297.         reach_end_node=nil
  298.         start_node=Path_node.new(start_p)

  299.         open_nodes<<start_node
  300.                 node = open_nodes.shift       
  301.                 [2,4,6,8].each{|d|
  302.       point=adj_p(node.p,d,p_max,p_min)
  303.                         if point&&@mini_passable.call(node.i,node.j,d)
  304.         
  305.                                 tmp=Path_node.new(point)
  306.                                 tmp.pre=node
  307.                                 tmp.cal_fgh(start_p, end_p)
  308.                                 min_heap_insert(open_nodes,tmp)
  309.                                 open_points[tmp.p]=tmp
  310.                         end
  311.                 }
  312.                 close_points[node.p]=node
  313.         while(open_nodes.size>0)

  314.     if cycles_cnt>@@mini_cycles_per_frame/@@mini_finder_cnt
  315.       cycles_cnt=0
  316.       Fiber.yield
  317.     end
  318.     if @mini_cancel
  319.         break
  320.     end
  321.                 min=heap_extract_min(open_nodes)
  322.                 open_points.delete(min.p)
  323.                 [2,4,6,8].each{|d|
  324.       point=adj_p(min.p,d,p_max,p_min)
  325.       next unless point
  326.       if point==end_p
  327.           tmp=Path_node.new(point)
  328.                                         tmp.pre=min
  329.                                         reach_end_node=tmp       
  330.         break
  331.       end
  332.                         if point!=min.pre.p&&@mini_passable.call(min.i,min.j,d)        
  333.                                 if !close_points.has_key?(point)
  334.                                         tmp=Path_node.new(point)
  335.                                         tmp.pre=min
  336.                                         tmp.cal_fgh(start_p, end_p)
  337.                                         tmp2=open_points[tmp.p]
  338.                                         if tmp2
  339.                                                 if tmp2.g>tmp.g
  340.                                                         tmp2_f_pre=tmp2.f
  341.                                                         tmp2.pre=min
  342.                                                         tmp2.g=tmp.g
  343.                                                         new_f=tmp.g+tmp.h                                                       
  344.                                                         heap_decrease_key(open_nodes,tmp2.pos,new_f)
  345.                                                 end
  346.                                         else       
  347.                                                 min_heap_insert(open_nodes,tmp)
  348.                                                 open_points[tmp.p]=tmp
  349.                                         end
  350.                                
  351.                                 end
  352.                                
  353.                         end
  354.                 }
  355.                 close_points[min.p]=min
  356.                 break if reach_end_node
  357.                 cycles_cnt+=1
  358.         end
  359.   @@mini_finder_cnt-=1
  360.   open_nodes.clear
  361.         open_points.clear
  362.         close_points.clear
  363.         reach_end_node
  364. end

  365. def cancel
  366.   @cancel=@mini_cancel=true
  367. end
  368. def mini_cancel
  369.   @mini_cancel=true
  370. end
  371. def set_localpassable_method(method)
  372.   @mini_passable=method
  373. end
  374. def set_pass_method(method)
  375.   @passable=method
  376. end

  377. end#class Pathfinder
  378. end#module Rmav


  379. class Game_Event
  380.   alias_method :initialize_org_event_rmav_7,:initialize
  381.   def initialize(map_id, event)
  382.     initialize_org_event_rmav_7(map_id, event)
  383.    
  384.     @path_finder=false
  385.   end
  386.   def make_passing(enable=true)
  387.     @path_finder=enable
  388.     init_pathing if enable
  389.   end
  390.   def init_pathing
  391.     @path_to_point=nil
  392.     @blocked_cnt=0
  393.     @repassable=Proc.new{|x,y,d| passable_ex?(x, y, d)}
  394.   end

  395.   #--------------------------------------------------------------------------
  396.   # ● 判定是否可以通行(检查 地图的通行度 和 前方是否有路障)
  397.   #     d : 方向(2,4,6,8)
  398.   #--------------------------------------------------------------------------
  399.   def passable_ex?(x, y, d)
  400.     x2 = $game_map.round_x_with_direction(x, d)
  401.     y2 = $game_map.round_y_with_direction(y, d)
  402.     return false unless $game_map.valid?(x2, y2)
  403.     return true if @through || debug_through?
  404.     return false unless map_passable?(x, y, d)
  405.     return false unless map_passable?(x2, y2, reverse_dir(d))
  406.     return false if collide_with_characters?(x2, y2)
  407.     return false if $game_player.pos_nt?(x2, y2)
  408.     return true
  409.   end

  410.   def find_path_to(x,y)
  411.       cancel_pathing
  412.       @path_to_point=[x,y]
  413.       @pathnode=nil
  414.       passable_method=Proc.new{|x,y,d| $game_map.passable?(x,y,d)}
  415.       unless @pathfinder
  416.         @pathfinder=Rmav::Pathfinder.new(
  417.              passable_method,[$game_map.width,$game_map.height])

  418.       end
  419.       @pathfinder.set_pass_method(passable_method)
  420.       @pathing=Fiber.new{
  421.         @pathnode=@pathfinder.a_star([x,y],[@x,@y])
  422.         @pathing=nil
  423.       }
  424.   end
  425.   
  426.   def move_toward_pos(x,y)
  427.     if @path_to_point!=[x,y]
  428.       find_path_to(x,y)
  429.     end
  430.   end
  431.   
  432.   def repath_to(x,y,speed_ctl=0)
  433.       reset_pathing
  434.       node=nil
  435.       unless @pathfinder
  436.         @pathfinder=Rmav::Pathfinder.new(
  437.              nil,[$game_map.width,$game_map.height])

  438.       end
  439.       @pathfinder.set_pass_method(@repassable)
  440.       @pathfinder.set_low_speed(speed_ctl)
  441.       @pathing=Fiber.new{
  442.         @pathnode=@pathfinder.a_star([x,y],[@x,@y])
  443.         @pathing=nil
  444.       }
  445.       
  446.   end

  447.   def local_pathing(s_p,e_p)
  448.      return nil unless @pathfinder
  449.      reset_local_pathing
  450.      @mini_node=nil
  451.      @pathfinder.set_localpassable_method(@repassable)
  452.      @local_pathing=Fiber.new{
  453.        @mini_node=@pathfinder.mini_star(s_p,e_p)
  454.        @local_pathing=nil
  455.      }
  456.   end

  457. def reset_pathing
  458.     @pathnode=nil
  459.     @pathfinder.cancel if @pathfinder
  460.     @pathing.resume    if @pathing
  461.       
  462.     @repathing.resume if @repathing
  463.     @local_pathing.resume if @local_pathing
  464. end
  465. def cancel_pathing
  466.   @path_to_point=nil
  467.   reset_pathing
  468. end
  469. def reset_local_pathing
  470.   @pathfinder.mini_cancel if @pathfinder
  471.   @local_pathing.resume if @local_pathing
  472. end
  473.   
  474.   alias_method :update_self_movement_rmav_org_rmav_7,:update_self_movement_rmav
  475.   def update_self_movement_rmav
  476.     return if moving? || @move_route_forcing
  477.     if @path_finder&&!$game_map.interpreter.running?

  478.       return unless @path_to_point#没有目标点
  479.       if near_the_screen?
  480.          
  481.         if @pathing
  482.           @pathing.resume;return
  483.         end
  484.         if @repathing
  485.           @repathing.resume;return
  486.         end
  487.         if @local_pathing
  488.           @local_pathing.resume;return
  489.         end
  490.         unless @pathnode  #全地图寻路失败
  491.           @rand_steps||=1
  492.             if Rmav::Opt_pathing[:failed_and_rand]
  493.               if (@rand_steps%=3)!=0
  494.                 move_type_random_rmav
  495.                 @rand_steps+=1
  496.               elsif @stop_count>180
  497.                 move_type_random_rmav
  498.                 @rand_steps+=1
  499.               end
  500.             elsif Rmav::Opt_pathing[:failed_and_back]  
  501.               repath_to(*@path_to_point,2)
  502.             end
  503.           return
  504.         end
  505.         @pathnode=@pathnode.pre if @pathnode.p==[@x,@y]
  506.         if @pathnode
  507.           #@path_to_point=nil if @path_to_point==@pathnode.p #到达目的         
  508.           dx=@pathnode.p[0]-@x
  509.           dy=@pathnode.p[1]-@y
  510.          
  511.           if Rmav::Opt_walk[:dir8_on]
  512.             if @pathnode.pre&&@pathnode.pre.p[0]!=@x&&@pathnode.pre.p[1]!=@y
  513.               dx=@pathnode.pre.p[0]-@x
  514.               dy=@pathnode.pre.p[1]-@y

  515.             end
  516.           end
  517.           if dx.abs>1||dy.abs>1
  518.             @pathnode=nil
  519.             repath_to(*@path_to_point,10)
  520.             return
  521.           end
  522.           move_diagonal([0,6,4][dx],[0,2,8][dy])
  523.             if @move_succeed
  524.               @path_to_point=nil if @path_to_point==@pathnode.p #到达目的
  525.               if Rmav::Opt_walk[:dir8_on]&&dx.abs==dy.abs
  526.                 @pathnode=@pathnode.pre.pre
  527.               else
  528.                 @pathnode=@pathnode.pre
  529.               end
  530.               @failed_node=nil
  531.               @blocked_cnt=0
  532.             else
  533.             #挡住了
  534.             @blocked_cnt+=1
  535. #p "dang~~~"
  536.               if Rmav::Opt_walk[:dir8_on]&&dx.abs==dy.abs
  537.                 @failed_node=@pathnode.pre
  538.               else
  539.                 @failed_node=@pathnode
  540.               end
  541.   
  542.               if Rmav::Opt_pathing[:detour]
  543.                 if @blocked_cnt>60
  544.                   repath_to(*@path_to_point,10)
  545.                   @blocked_cnt=1                  
  546.                 else               
  547.                     if @failed_node.pre
  548.                        if collide_with_characters?(*@failed_node.pre.p)
  549.                          failed_node=@failed_node.pre
  550.                        else
  551.                          failed_node=@failed_node
  552.                        end
  553.                        if failed_node.pre &&!collide_with_characters?(*failed_node.pre.p)

  554.                           if @mini_node
  555.                             tmp2_node=@mini_node
  556.                             while tmp2_node&&tmp2_node.pre
  557.                               tmp2_node=tmp2_node.pre
  558.                             end
  559.                             tmp2_node.pre=@failed_node_reserved.pre.pre
  560.                             @pathnode=@mini_node                             
  561.                             @mini_node=nil
  562.                             @failed_node_reserved=nil
  563.                             while @pathnode&&@pathnode.p!=[@x,@y]
  564.                               @pathnode=@pathnode.pre
  565.                             end
  566.                           else
  567.                             if @local_pathing
  568.                               @local_pathing.resume
  569.                             else
  570.                               @failed_node_reserved=failed_node
  571.                               local_pathing(failed_node.pre.p,[@x,@y])
  572.                             end                          
  573.                           end
  574.                        else#后续多点被堵
  575.                         #repathing
  576.                         repath_to(*@path_to_point) if @blocked_cnt>5
  577.                        end

  578.                     else
  579.                         #终点被占用,到达倒数第2点
  580.                     end
  581.                   end#@blocked_cnt
  582.                 else#指定不绕,会停在那直到障碍移开
  583.                  #不用处理,不绕
  584.                 end
  585.             end
  586.          
  587.         end
  588.       end
  589.       return
  590.     end
  591.     update_self_movement_rmav_org_rmav_7
  592.   end
  593. end


  594. class Scene_Base
  595.   alias_method :update_org_base_rmav_7,:update
  596.   def update
  597.     Rmav::Pathfinder.reset_left_cycles
  598.     update_org_base_rmav_7
  599.   end
  600. end
复制代码
发代码可以使用代码框<>工具 —— VIPArcher 留
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

在本版发帖返回顶部