查看: 3333|回复: 2

[RMVA发布] RMVA A+自动寻路 系统(应该不算原创

[复制链接]
梦石
0
星屑
110
在线时间
67 小时
注册时间
2007-12-16
回帖
64
发表于 2012-8-15 21:44:56 | 显示全部楼层 |阅读模式

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

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

×
这脚本其实只是把之前神隐不器VX上发的A*自动寻路二叉堆版
实装到VA的地图上而已
然后修正了一下地图循环的部分~~~

这算是伪八方向的版本
如果要改四方向就把
11行
  1. Directions = 8 # 寻路方向(4或8)
复制代码

改成
  1. Directions = 4 # 寻路方向(4或8)
复制代码

334~337行
  1.     if Input.dir8 > 0
  2.       init_mouse_control_info
  3.       move_direction(Input.dir8)
  4.     end
复制代码

改成
  1.     if Input.dir4 > 0
  2.       init_mouse_control_info
  3.       move_direction(Input.dir4)
  4.     end
复制代码

401~428行
  1.     if @mouse_drag
  2.       @mpath.clear unless @mpath.empty?
  3.       dir = -1
  4.       # 滑鼠移動控制
  5.       dir = 6 if mouse_x > self.screen_x + 30
  6.       dir = 4 if mouse_x < self.screen_x - 30
  7.       if mouse_y > self.screen_y + 10
  8.         case dir
  9.         when 4
  10.           dir = 1
  11.         when 6
  12.           dir = 3
  13.         else
  14.           dir = 2
  15.         end
  16.       end
  17.       if mouse_y < self.screen_y - 48
  18.         case dir
  19.         when 4
  20.           dir = 7
  21.         when 6
  22.           dir = 9
  23.         else
  24.           dir = 8
  25.         end
  26.       end
  27.       move_direction(dir) if dir > 0
  28.     end
复制代码

改成
  1.     if @mouse_drag
  2.       @mpath.clear unless @mpath.empty?
  3.       dir = -1
  4.       # 滑鼠移動控制
  5.       dir = 6 if mouse_x > self.screen_x + 30
  6.       dir = 4 if mouse_x < self.screen_x - 30
  7.       dir = 2 if mouse_y > self.screen_y + 10
  8.       dir = 8 if mouse_y < self.screen_y - 48
  9.       move_direction(dir) if dir > 0
  10.     end
复制代码



要用真八方向的话就自己改
Game_Player的move_direction(d)方法吧

  1. #==============================================================================
  2. # ■ RMVA A+自动寻路 系统
  3. #------------------------------------------------------------------------------
  4. #  A*自動尋路 部分 By 神隱不器
  5. #     其他腳本 部分 By 釣到一隻猴子@_@
  6. #==============================================================================

  7. #==============================================================================
  8. # ■ PathFinding
  9. #------------------------------------------------------------------------------
  10. #  A*自動尋路 二叉堆版。
  11. #     By 神隱不器
  12. #==============================================================================

  13. module PathFinding
  14.   #--------------------------------------------------------------------------
  15.   # ● 参数
  16.   #--------------------------------------------------------------------------
  17.   Directions = 8  # 寻路方向(4或8)
  18.   PathType = 1    # 返回值的类型(0:节点坐标, 1:移动方向)
  19.   #--------------------------------------------------------------------------
  20.   Node = Struct.new(:x, :y, :d, :parent, :h, :g, :f)
  21.   #--------------------------------------------------------------------------
  22.   # ● 搜索路径
  23.   #--------------------------------------------------------------------------
  24.   def self.find_short_path(self_x, self_y, trg_x, trg_y)
  25.     return find_path(self_x, self_y, trg_x, trg_y)
  26.   end
  27.   #--------------------------------------------------------------------------
  28.   # ● 寻找角色的最短路径
  29.   #--------------------------------------------------------------------------
  30.   def self.find_player_short_path(trg_x, trg_y)
  31.     if Input.mouse_icon != 0
  32.       @possible_x = trg_x
  33.       @possible_y = trg_y
  34.     end
  35.     self_x = $game_player.x
  36.     self_y = $game_player.y
  37.     return find_path(self_x, self_y, trg_x, trg_y)
  38.   end
  39.   #--------------------------------------------------------------------------
  40.   # ● 开始
  41.   #--------------------------------------------------------------------------
  42.   def self.find_path(start_x, start_y, dest_x, dest_y)
  43.     @dest_pos = [dest_x, dest_y]
  44.     ## 加入起始节点
  45.     size = [$game_map.width, $game_map.height]
  46.     @open = BinaryHeap.new(size, make_node(start_x, start_y, nil)) {|a,b| a.f <=> b.f }
  47.     @link = @open.link
  48.     path = []
  49.     while !@open.data.empty?
  50.       node = @open.dequeue
  51.       if [node.x,node.y] == @dest_pos
  52.         path = make_path(node,PathType)
  53.         break
  54.       end
  55.       self.check(node)
  56.     end
  57.     @possible_x = nil
  58.     @possible_y = nil
  59.     return path
  60.   end
  61.   #--------------------------------------------------------------------------
  62.   # ● 生成路径
  63.   # type: 返回的形式 (0:节点坐标, 1:移动方向)
  64.   #--------------------------------------------------------------------------
  65.   def self.make_path(node,type)
  66.     path = []
  67.     loop do
  68.       case type
  69.       when 0
  70.         path.unshift [node.x, node.y]
  71.       when 1
  72.         path.unshift direction(node.x,node.y,node.parent)
  73.       end
  74.       ## 循环条件
  75.       node = node.parent
  76.       break if node.nil? || node.parent.nil?
  77.     end
  78.     return path
  79.   end
  80.   #--------------------------------------------------------------------------
  81.   # ● 生成节点
  82.   #--------------------------------------------------------------------------
  83.   def self.make_node(x, y, parent)
  84.     buf = Node.new(x,y,direction(x,y,parent),parent)
  85.     buf.h = self.h(buf)
  86.     buf.g = self.g(buf)
  87.     buf.f = buf.h + buf.g
  88.     return buf
  89.   end
  90.   #--------------------------------------------------------------------------
  91.   # ● 寻路
  92.   #--------------------------------------------------------------------------
  93.   def self.check(node)
  94.     px = $game_map.round_x(node.x)
  95.     py = $game_map.round_y(node.y)
  96.     @link[px, py] = -2 ## closed
  97.     self.child_nodes(node).each do |n|
  98.       x = $game_map.round_x(n.x)
  99.       y = $game_map.round_y(n.y)
  100.       next if @link[x, y] == -2 ## closed
  101.       next if !passable?(px, py, n.d)
  102.       ## 检查更新
  103.       if @link[x, y] - 1 >= 0 && @open.data[@link[x, y] - 1]
  104.         @open.replace(@link[x, y] - 1, n) if @open.data[@link[x, y] - 1].g > n.g
  105.       else
  106.         @open.enqueue(n)
  107.       end
  108.     end
  109.   end
  110.   #--------------------------------------------------------------------------
  111.   # ● h值
  112.   #--------------------------------------------------------------------------
  113.   def self.h(node)
  114.     return ((node.x-@dest_pos[0]).abs + (node.y-@dest_pos[1]).abs)*20
  115.   end
  116.   #--------------------------------------------------------------------------
  117.   # ● g值
  118.   #--------------------------------------------------------------------------
  119.   def self.g(node)
  120.     parent = node.parent
  121.     return 0 if parent.nil?
  122.     dg = parent.d == node.d ? 0 : 14
  123.                                   ## 改变方向的附加开销
  124.     dx = node.x - parent.x
  125.     dy = node.y - parent.y
  126.     ## 判断相对坐标
  127.     if dx != 0 && dy != 0
  128.       return parent.g + 14 + dg
  129.     else
  130.       return parent.g + 10 + dg
  131.     end
  132.   end
  133.   #--------------------------------------------------------------------------
  134.   # ● 方向
  135.   #--------------------------------------------------------------------------
  136.   def self.direction(x,y,parent)
  137.     return parent.nil? ? 5 : (5 + (x-parent.x)*1 + (y-parent.y)*-3)
  138.   end
  139.   #--------------------------------------------------------------------------
  140.   # ● 子节点
  141.   #--------------------------------------------------------------------------
  142.   def self.child_nodes(node)
  143.     nodes = []
  144.     case Directions
  145.     when 4 # 4方向寻路
  146.       nodes<<make_node(node.x,node.y-1,node)<<make_node(node.x,node.y+1,node)
  147.       nodes<<make_node(node.x-1,node.y,node)<<make_node(node.x+1,node.y,node)
  148.     when 8 # 8方向寻路
  149.       for x in -1..1
  150.         for y in -1..1
  151.           next if x == 0 && y == 0
  152.           nodes << make_node(node.x+x, node.y+y, node)
  153.         end
  154.       end
  155.     end
  156.     return nodes
  157.   end
  158.   #--------------------------------------------------------------------------
  159.   # ● 通行?
  160.   #--------------------------------------------------------------------------
  161.   def self.passable?(x, y, d)
  162.     if @possible_x and @possible_y
  163.       tx, ty = x, y
  164.       case d
  165.       when 1 # 左下
  166.         tx -= 1
  167.         ty += 1
  168.       when 2 # 下
  169.         ty += 1
  170.       when 3 # 右下
  171.         tx += 1
  172.         ty += 1
  173.       when 4 # 左
  174.         tx -= 1
  175.       when 6 # 右
  176.         tx += 1
  177.       when 7 # 左上
  178.         tx -= 1
  179.         ty -= 1
  180.       when 8 # 上
  181.         ty -= 1
  182.       when 9 # 右上
  183.         tx += 1
  184.         ty -= 1
  185.       end
  186.       return true if @possible_x == tx and @possible_y == ty
  187.     end
  188.     x = $game_map.round_x(x)
  189.     y = $game_map.round_y(y)
  190.     case d
  191.     when 0, 2, 4, 6, 8
  192.       return $game_player.passable?(x, y, d)
  193.     when 1
  194.       return $game_player.diagonal_passable?(x, y, 4, 2)
  195.     when 3
  196.       return $game_player.diagonal_passable?(x, y, 6, 2)
  197.     when 7
  198.       return $game_player.diagonal_passable?(x, y, 4, 8)
  199.     when 9
  200.       return $game_player.diagonal_passable?(x, y, 6, 8)
  201.     end
  202.   end
  203.   #--------------------------------------------------------------------------
  204.   # ●
  205.   #--------------------------------------------------------------------------
  206.   def self.open
  207.     @open.data
  208.   end
  209.   #--------------------------------------------------------------------------
  210.   # ●
  211.   #--------------------------------------------------------------------------
  212.   def self.close
  213.     result = []
  214.     for i in [email]0...@link.xsize[/email]
  215.       for j in [email]0...@link.ysize[/email]
  216.         result<<[i,j] if @link[i,j]==-2
  217.       end
  218.     end
  219.     return result
  220.   end
  221. end

  222. #==============================================================================
  223. # ■ BinaryHeap
  224. #------------------------------------------------------------------------------
  225. #  二叉堆 寻路用。
  226. #==============================================================================

  227. class BinaryHeap
  228.   #--------------------------------------------------------------------------
  229.   # ● 定义实例变量
  230.   #--------------------------------------------------------------------------
  231.   attr_reader :data
  232.   attr_reader :link
  233.   #--------------------------------------------------------------------------
  234.   # ● 初始化
  235.   #--------------------------------------------------------------------------
  236.   def initialize(link,*val,&cmp)
  237.     @link = Table.new(*link)
  238.     @data = val.sort
  239.     @cmp = cmp
  240.     @cmp = Proc.new{|a,b| a<=>b} if @cmp.nil?
  241.     @data.each_index{|i| @link[@data[i].x,@data[i].y] = i+1}
  242.   end
  243.   #--------------------------------------------------------------------------
  244.   # ● 入列
  245.   #--------------------------------------------------------------------------
  246.   def enqueue(val)
  247.     return replace(@data.size,val)
  248.   end
  249.   #--------------------------------------------------------------------------
  250.   # ● 出列
  251.   #--------------------------------------------------------------------------
  252.   def dequeue
  253.     shift = @data[0]
  254.     if @data.size <= 1
  255.       @data.clear
  256.       return shift
  257.     end
  258.     @data[0] = @data.pop
  259.     linkto(0)
  260.     fid,cid = 0,[1,2]
  261.     while @data[cid[0]]     ## 左枝存在时
  262.       if @data[cid[1]].nil? ## 右枝不存在时
  263.         min = cid[0]
  264.       else
  265.         min = @cmp.call(@data[cid[0]],@data[cid[1]]) < 0 ? cid[0] : cid[1]
  266.       end
  267.       if @cmp.call(@data[min],@data[fid]) < 0
  268.         @data[min],@data[fid] = @data[fid],@data[min]
  269.         linkto(min,fid)
  270.         fid = min
  271.         cid = [fid*2+1,fid*2+2]
  272.       else
  273.         break
  274.       end
  275.     end
  276.     return shift
  277.   end
  278.   #--------------------------------------------------------------------------
  279.   # ● 修改
  280.   #--------------------------------------------------------------------------
  281.   def replace(pos, val)
  282.     @data[pos] = val
  283.     linkto(pos)
  284.     cid,fid = pos,(pos-1)/2
  285.     while cid > 0 ## 大于顶层时
  286.       if @cmp.call(val,@data[fid]) < 0
  287.         @data[cid] = @data[fid]
  288.         linkto(cid)
  289.         cid = fid
  290.         fid = (cid-1)/2
  291.       else
  292.         break
  293.       end
  294.     end
  295.     @data[cid] = val
  296.     linkto(cid)
  297.     return cid
  298.   end
  299.   #--------------------------------------------------------------------------
  300.   # ● 关联
  301.   #--------------------------------------------------------------------------
  302.   def linkto(*id)
  303.     id.each{|i| @link[@data[i].x,@data[i].y]=i+1}
  304.   end
  305. end

  306. #==============================================================================
  307. # ■ Game_Player
  308. #------------------------------------------------------------------------------
  309. #  处理玩家人物的类。拥有事件启动的判定、地图的卷动等功能。
  310. #   本类的实例请参考 $game_player 。
  311. #==============================================================================

  312. class Game_Player < Game_Character
  313.   #--------------------------------------------------------------------------
  314.   # ● 初始化对象
  315.   #--------------------------------------------------------------------------
  316.   alias :d8_ori_init :initialize unless defined? d8_ori_init
  317.   def initialize
  318.     d8_ori_init
  319.     init_mouse_control_info
  320.   end
  321.   #--------------------------------------------------------------------------
  322.   # ● 初始化滑鼠訊息
  323.   #--------------------------------------------------------------------------
  324.   def init_mouse_control_info
  325.     @mouse_drag = false
  326.     @mouse_count = 0
  327.     @mouse_speeding = false
  328.     @find_path = 0
  329.     @mpath ? @mpath.clear : @mpath = []
  330.   end
  331.   #--------------------------------------------------------------------------
  332.   # ● 由玩家控制移动
  333.   #--------------------------------------------------------------------------
  334.   def move_by_input
  335.     return if !movable? || $game_map.interpreter.running?
  336.     if Input.dir8 > 0
  337.       init_mouse_control_info
  338.       move_direction(Input.dir4)
  339.     end
  340.     update_mouse_control
  341.   end
  342.   #--------------------------------------------------------------------------
  343.   # ● 更新滑鼠控制
  344.   #--------------------------------------------------------------------------
  345.   def update_mouse_control
  346.     # Count refresh
  347.     @find_path += 1 if @find_path != 0
  348.     # Mouse
  349.     mouse_x, mouse_y = Input.get_mouse_pos
  350.     # 滑鼠連點狀態
  351.     if Input.dbclick?(Input::M_L)
  352.       @mouse_speeding = true
  353.       @find_path = 1
  354.       @mouse_count = 0
  355.     elsif Input.trigger?(Input::M_L)
  356.       @find_path = 1
  357.       @mouse_count = 0
  358.     elsif Input.press?(Input::M_L)
  359.       if @mouse_count > 10
  360.         @mouse_drag = true
  361.         @find_path = 0
  362.       else
  363.         @mouse_count += 1
  364.       end
  365.     else
  366.       @mouse_count = 0
  367.       @mouse_speeding = false if @mpath.empty? and @find_path == 0
  368.       @mouse_drag = false
  369.     end
  370.     # 自動尋路
  371.     if @find_path > 12
  372.       @find_path = 0
  373.       if Input.mouse_icon != 0
  374.         if @target_target_event != 0
  375.           @mouse_trigger_event = false
  376.           @target_target_event = 0
  377.         else
  378.           @mouse_trigger_event = true
  379.           @target_target_event = @target_event
  380.         end
  381.       end
  382.       # 計算滑鼠所在地圖座標
  383.       if $game_map.loop_horizontal? and $game_map.display_x + Graphics.width / 32 > $game_map.width and x < $game_map.display_x
  384.         dx = $game_map.display_x - $game_map.width
  385.       else
  386.         dx = $game_map.display_x
  387.       end
  388.       if $game_map.loop_vertical? and $game_map.display_y + Graphics.height / 32 > $game_map.height and y < $game_map.display_y
  389.         dy = $game_map.display_y - $game_map.height
  390.       else
  391.         dy = $game_map.display_y
  392.       end
  393.       mx = dx + mouse_x / 32
  394.       my = dy + mouse_y / 32
  395.       mx = mx <= mx.floor + 0.5 ? mx.ceil : mx.floor
  396.       my = my >= my.floor + 0.5 ? my.ceil : my.floor
  397.       if passable?(mx, my, 0) or Input.mouse_icon != 0
  398.         @mpath = PathFinding.find_player_short_path(mx, my)
  399.         @mouse_drag = false
  400.       end
  401.     end
  402.     if @mouse_drag
  403.       @mpath.clear unless @mpath.empty?
  404.       dir = -1
  405.       # 滑鼠移動控制
  406.       dir = 6 if mouse_x > self.screen_x + 30
  407.       dir = 4 if mouse_x < self.screen_x - 30
  408.       if mouse_y > self.screen_y + 10
  409.         case dir
  410.         when 4
  411.           dir = 1
  412.         when 6
  413.           dir = 3
  414.         else
  415.           dir = 2
  416.         end
  417.       end
  418.       if mouse_y < self.screen_y - 48
  419.         case dir
  420.         when 4
  421.           dir = 7
  422.         when 6
  423.           dir = 9
  424.         else
  425.           dir = 8
  426.         end
  427.       end
  428.       move_direction(dir) if dir > 0
  429.     end
  430.     # 依自動尋路路徑行走
  431.     move_direction(@mpath.shift) if !@mpath.empty?
  432.   end
  433.   #--------------------------------------------------------------------------
  434.   # ● 依方向移動
  435.   #--------------------------------------------------------------------------
  436.   def move_direction(d)
  437.     case d
  438.     when 2, 4, 6, 8
  439.       move_straight(d)
  440.     # 斜向需作修正
  441.     when 1
  442.       return move_diagonal(4, 2) if diagonal_passable?(@x, @y, 4, 2)
  443.       move_straight(4) if passable?(@x, @y, 4)
  444.       move_straight(2) if passable?(@x, @y, 2)
  445.     when 3
  446.       return move_diagonal(6, 2) if diagonal_passable?(@x, @y, 6, 2)
  447.       move_straight(6) if passable?(@x, @y, 6)
  448.       move_straight(2) if passable?(@x, @y, 2)
  449.     when 7
  450.       return move_diagonal(4, 8) if diagonal_passable?(@x, @y, 4, 8)
  451.       move_straight(4) if passable?(@x, @y, 4)
  452.       move_straight(8) if passable?(@x, @y, 8)
  453.     when 9
  454.       return move_diagonal(6, 8) if diagonal_passable?(@x, @y, 6, 8)
  455.       move_straight(6) if passable?(@x, @y, 6)
  456.       move_straight(8) if passable?(@x, @y, 8)
  457.     end
  458.   end
  459. end
复制代码


然后这脚本用的鼠标系统是咱自己习惯用的自订版本
用其他的鼠标脚本就自己改判断的部分吧(不负责的闪人~~~
(其实只是把一般鼠标用的Mouse模组整个丢进Input里)
(基本上跟優化版的鼠標系統+全鍵盤差不多)
鼠标系统就丟在範例裡了

附件大小限制的关系
System文件夹自己拷一份
MouseMove.rar (347.62 KB, 下载次数: 715)

哆啦菌

梦石
0
星屑
46
在线时间
795 小时
注册时间
2010-7-24
回帖
3507
发表于 2012-8-15 22:04:54 | 显示全部楼层
能再解释下用法么。。。另外是不是沉影不器?
随便看看
回复

使用道具 举报

梦石
0
星屑
1630
在线时间
423 小时
注册时间
2010-12-26
回帖
284
发表于 2012-8-16 18:13:52 | 显示全部楼层
报错,内存不能为什么什么的
摘自《暗悲传》
某人
天啊!我无骂无打怎么这么多的狗,每天都要来咬我呢?......天理何在
某人
在路上看到了许多行人说道我眼前怎么全是无肺黑心肝在行走呢?
某人
有时被整的实在是受不了,很想杀狗,看看他们的心脏是黑的吗?
某人
我很喜欢黑夜与下雨,或许我只能在虚拟世界能过上一般人的生活......
某人
无论别人无良心骂我,诅咒死,刻意整蛊......希望能坚持活着!要永远记住那些人的奸诈嘴脸!
某人
又有谁能出来主持公道呢?谁能理解你,谁能站出来说句话.....他是无辜又悲苦......这真是无聊的故事吗?
回复

使用道具 举报

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

本版积分规则

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

在本版发帖返回顶部