设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1683|回复: 3
打印 上一主题 下一主题

[已经过期] 找一个寻路脚本……

[复制链接]

Lv1.梦旅人

虱子

梦石
0
星屑
121
在线时间
1782 小时
注册时间
2010-6-19
帖子
3597
跳转到指定楼层
1
发表于 2010-12-4 19:20:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
本帖最后由 Wind2010 于 2010-12-4 19:55 编辑

whbm的四方向寻路算法
↑有人有这个脚本吗?


于是乎找到了,但是这个脚本又提示62行出错了……
  1. #============================================================================
  2. # 本脚本来自www.66RPG.com,使用和转载请保留此信息
  3. #============================================================================
  4. #----------------------------------------------------------------------------
  5. #            自动移动 By OCTSJimmy 感谢 whbm 提供帮助
  6. =begin
  7.   该脚本必须有 whbm 的 寻路算法:四方向 的支持
  8.   使用该脚本到是很简单
  9.   在需要开始自动移动的事件中填入
  10.    
  11.   设置移动路线:(目标人物)
  12.               脚本 tomove(目标坐标x, 目标坐标y)
  13.                
  14.   如此即可
  15.    
  16.   例如要使 角色 开始 自动移动 到地图的 (0,0) 位置那么就可以这样写
  17.   ◆设置移动路线: 角色
  18.    :            : ◇脚本 : tomove(0, 0)
  19.   ◆
  20.   其余的同样如此设置
  21.    
  22.   需要注意的是:
  23.       如果目标坐标所处的图块为不可通行且穿透没有打开的话那么将不会有任何动作
  24.   甚至连路线数组也不会生成。
  25. =end
  26. #----------------------------------------------------------------------------
  27. class Interpreter
  28. #--------------------------------------------------------------------------
  29. # ● 获取角色
  30. #     parameter : 能力值
  31. #--------------------------------------------------------------------------
  32. def get_character_n(parameter)
  33.    # 能力值分支
  34.    #★★★★★★★★增加★★★★★★★★★★
  35.    $mubiao = parameter
  36.    $event_id_all = @event_id
  37.    #★★★★★★★★增加★★★★★★★★★★
  38.    case parameter
  39.    when -1  # 角色
  40.      return $game_player
  41.    when 0  # 本事件
  42.      events = $game_map.events
  43.      return events == nil ? nil : events[@event_id]
  44.    else  # 特定的事件
  45.      events = $game_map.events
  46.      return events == nil ? nil : events[parameter]
  47.    end
  48. end
  49. alias get_character get_character_n
  50. end
  51. class Game_Character
  52. #---------------------------新增-------------------------------------------
  53. # ● 寻路移动
  54. #---------------------------新增-------------------------------------------
  55. def tomove(x, y)
  56.    #当操作目标为 角色 时
  57.    if $mubiao == -1
  58.      @tmp_x = x
  59.      @tmp_y = y
  60.      #调用寻路算法,开始计算路线
  61.      $find_path = Find_Path.new
  62.      @path = $find_path.find_player_short_path(@tmp_x,@tmp_y)
  63.       
  64.    #当操作目标为 本事件 时
  65.    elsif $mubiao == 0
  66.      @tmp_x = x
  67.      @tmp_y = y
  68.      @tmp_id_x = $game_map.events[$event_id_all].x
  69.      @tmp_id_y = $game_map.events[$event_id_all].y
  70.      #调用寻路算法,开始计算路线
  71.      $find_path = Find_Path.new
  72.      @path = $find_path.find_short_path(@tmp_id_x, @tmp_id_y, @tmp_x, @tmp_y)
  73.    #当操作目标为 其他事件 时
  74.    else
  75.      @tmp_x = x
  76.      @tmp_y = y
  77.      @tmp_id_x = $game_map.events[$mubiao].x
  78.      @tmp_id_y = $game_map.events[$mubiao].y
  79.      #调用寻路算法,开始计算路线
  80.      $find_path = Find_Path.new
  81.      @path = $find_path.find_short_path(@tmp_id_x, @tmp_id_y, @tmp_x, @tmp_y)
  82.    end
  83.    #开始自动移动,参照本类的 update方法
  84.    @go_path = true
  85.    #自动移动 处理位置初始化
  86.    @path_id = 0
  87. end
  88. #--------------------------------------------------------------------------
  89. # ● 刷新画面
  90. #--------------------------------------------------------------------------
  91. def update_n
  92. # 跳跃中、移动中、停止中的分支
  93.    if jumping?
  94.      update_jump
  95.    elsif moving?
  96.      update_move
  97.    else
  98.      update_stop
  99.    end
  100.    # 动画计数超过最大值的情况下
  101.    # ※最大值等于基本值减去移动速度 * 1 的值
  102.    if @anime_count > 18 - @move_speed * 2
  103.      # 停止动画为 OFF 并且在停止中的情况下
  104.      if not @step_anime and @stop_count > 0
  105.        # 还原为原来的图形
  106.        @pattern = @original_pattern
  107.      # 停止动画为 ON 并且在移动中的情况下
  108.      else
  109.        # 更新图形
  110.        @pattern = (@pattern + 1) % 4
  111.      end
  112.      # 清除动画计数
  113.      @anime_count = 0
  114.    end
  115.    # 等待中的情况下
  116.    if @wait_count > 0
  117.      # 减少等待计数
  118.      @wait_count -= 1
  119.      return
  120.    end
  121.    # 强制移动路线的场合
  122.    if @move_route_forcing
  123.      # 自定义移动
  124.      move_type_custom
  125.      return
  126.    end
  127.    # 事件执行待机中并且为锁定状态的情况下
  128.    if @starting or lock?
  129.      # 不做规则移动
  130.      return
  131.    end
  132.    # 如果停止计数超过了一定的值(由移动频度算出)
  133.    if @stop_count > (40 - @move_frequency * 2) * (6 - @move_frequency)
  134.      # 移动类型分支
  135.      case @move_type
  136.      when 1  # 随机
  137.        move_type_random
  138.      when 2  # 接近
  139.        move_type_toward_player
  140.      when 3  # 自定义
  141.        move_type_custom
  142.      end
  143.    end
  144.    #---------------------------新增-------------------------------------------
  145.    #当 自动移动 标记为真 以及 不在移动时
  146.    if @go_path and !moving?
  147.      #依据 自动移动 处理位置 判断下一步该往哪儿走
  148.      case @path[@path_id]
  149.      when 2
  150.        move_down
  151.      when 4
  152.        move_left
  153.      when 6
  154.        move_right
  155.      when 8
  156.        move_up
  157.      end
  158.      #自动移动 处理位置 递增
  159.      @path_id += 1
  160.      #假如 自动移动 处理位置 到达 路线数组的末尾
  161.      if @path_id == @path.size
  162.        #使得 自动移动 标记重置
  163.        @go_path = false
  164.      end
  165.    end
  166. end
  167. alias update update_n
  168. end
  169. #============================================================================
  170. # 本脚本来自www.66RPG.com,使用和转载请保留此信息
  171. #============================================================================
复制代码
有谁可以解决设个问题么0 0

点评

喵呜话说我最近在做跨地图的寻路导航  发表于 2010-12-4 19:25

http://rpg.blue/thread-175056-1-2.html
PVZ型塔防物一个
http://rpg.blue/thread-155199-1-2.html
RMXP技术讨论区手动认可帖,得到答案请认可

Lv1.梦旅人

梦石
0
星屑
50
在线时间
4 小时
注册时间
2010-6-24
帖子
27
2
发表于 2010-12-5 12:44:52 | 只看该作者
于是说错误代码是虾米……NoMethodsError?

点评

wrong number of arguments(2 for 4)  发表于 2010-12-5 12:52
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
55
在线时间
323 小时
注册时间
2010-8-21
帖子
666
3
发表于 2010-12-5 16:10:59 | 只看该作者
额..仔细看看第61行.少个类....

#==============================================================================
# ■ Find_Path
#------------------------------------------------------------------------------
#  寻路算法
#   By whbm
#==============================================================================
class Find_Path
#--------------------------------------------------------------------------
def initialize  #初始化
@open_list = []
@close_lise = []
end  #结束初始化
#--------------------------------------------------------------------------
def fp_passable?(x, y, d)  #开始判定通行
if [2, 4, 6, 8].include?(d)
   if $game_player.passable?(x, y, d)
     return true
   else
     return false
   end
else
   case d
   when 1
     if ($game_player.passable?(x, y, 4) and
       $game_player.passable?(x - 1, y, 2)) or
        ($game_player.passable?(x, y, 2) and
       $game_player.passable?(x, y + 1, 4))
       return true
     else
       return false
     end
   when 3
     if ($game_player.passable?(x, y, 6) and
       $game_player.passable?(x + 1, y, 2)) or
        ($game_player.passable?(x, y, 2) and
       $game_player.passable?(x, y + 1, 6))
       return true
     else
       return false
     end
   when 7
     if ($game_player.passable?(x, y, 4) and
       $game_player.passable?(x - 1, y, 8)) or
        ($game_player.passable?(x, y, 8) and
       $game_player.passable?(x, y - 1, 4))
       return true
     else
       return false
     end
   when 9
     if ($game_player.passable?(x, y, 6) and
       $game_player.passable?(x + 1, y, 8)) or
        ($game_player.passable?(x, y, 8) and
       $game_player.passable?(x, y - 1, 6))
       return true
     else
       return false
     end
   end
end
end  #结束判定通行
#--------------------------------------------------------------------------
def get_g(now_point)  #开始计算G值
d = now_point[2]
return 0 if d == 5
father_point = get_father_point(now_point)
g = father_point[3] + ((d == 1 or d == 3 or d == 7 or d == 9) ? 14 : 10)
return g
end  #结束计算G值
#--------------------------------------------------------------------------
def get_h(now_point)  #开始计算H值
now_x = now_point[0]
now_y = now_point[1]
#print @trg_x,now_x,@trg_y,now_y
h = (@trg_x - now_x).abs + (@trg_y - now_y).abs
return h
end  #结束计算H值
#--------------------------------------------------------------------------
def get_f(now_point)  #开始计算F值
f = now_point[3] + now_point[4]
return f
end  #结束计算F值
#--------------------------------------------------------------------------
def get_point(x, y) #取已知坐标点
if @open_list.size != 0
   @open_list.each do |point|
     if point[0] == x and point[1] == y
       return point
       break
     end
   end
end
if @close_list.size != 0
   @close_list.each do |point|
     if point[0] == x and point[1] == y
       return point
       break
     end
   end
end
end  #结束取已知坐标点
#--------------------------------------------------------------------------
def get_father_point(now_point)  #取已知点的父节点
d = now_point[2]
return now_point if d == 5
x = now_point[0] + ((d == 9 or d == 6 or d == 3) ? 1 : ((d == 7 or d == 4 or d == 1) ? -1 : 0))
y = now_point[1] + ((d == 1 or d == 2 or d == 3) ? 1 : ((d == 7 or d == 8 or d == 9) ? -1 : 0))
return get_point(x, y)
end  #结束取已知点的父节点
#--------------------------------------------------------------------------
def new_point(x, y, d)  #开始建立新节点
#print x,y,d
point = [x, y, d]
point.push get_g(point)
point.push get_h(point)
point.push get_f(point)
return point
end  #结束建立新节点
#--------------------------------------------------------------------------
def find_short_path(self_x, self_y, trg_x, trg_y)  #开始搜索路径
return [] if not (fp_passable?(trg_x, trg_y, 8) or
                   fp_passable?(trg_x, trg_y, 4) or
                   fp_passable?(trg_x, trg_y, 6) or
                   fp_passable?(trg_x, trg_y, 2))
@self_x = self_x
@self_y = self_y
@now_x = self_x
@now_y = self_y
@trg_x = trg_x
@trg_y = trg_y
@open_list = []
@close_list = []
#准备搜索
#print @self_x,@self_y
@now_point = new_point(@self_x, @self_y, 5) #令起始点为当前点
@open_list.push @now_point #将当前点加入关闭列表
#开始搜索
loop do
   check_trg = check_around_point(@now_point)
   if check_trg == true
     @path = get_path
     break
   end
   @now_point = get_lowest_f_point
   if @now_point == [] or @now_point == nil
     @path = []
     break
   end
end
return @path
end  #结束搜索路径
#--------------------------------------------------------------------------
def find_player_short_path(trg_x, trg_y)  #寻找角色的最短路径
self_x = $game_player.x
self_y = $game_player.y
return find_short_path(self_x, self_y, trg_x, trg_y)
end  #结束角色的寻找路径
#--------------------------------------------------------------------------
def get_path  #取得最终的路径
path = []
now_point = @open_list[@open_list.size - 1]
path.push(10 - now_point[2])
last_point = now_point
loop do
   now_point = get_father_point(now_point)
   break if now_point[2] == 5
   path.push(10 - now_point[2])
end
return path.reverse
end  #结束取得最终的路径
#--------------------------------------------------------------------------
def get_lowest_f_point  #开始取得最低F值的点
if @open_list == []
   return []
end
last_lowest_f_point = @open_list[0]
@open_list.each do |point|
   last_lowest_f_point = point if point[5] < last_lowest_f_point[5]
end
return last_lowest_f_point
end  #结束取得最低F值点
#--------------------------------------------------------------------------
def check_around_point(point)  #开始检查已知点的八方节点
for d in [1, 2, 3, 4, 6, 7, 8, 9]
   x = point[0] + ((d == 9 or d == 6 or d == 3) ? 1 : ((d == 7 or d == 4 or d == 1) ? -1 : 0))
   y = point[1] + ((d == 1 or d == 2 or d == 3) ? 1 : ((d == 7 or d == 8 or d == 9) ? -1 : 0))
   if in_close_list?(x, y) #在关闭列表中
     next
   elsif in_open_list?(x, y) #在开启列表中
     get_new_g_point = new_point(x, y, 10 - d)
     get_last_g_point = get_point(x, y)
     if get_new_g_point[3] >= get_last_g_point[3]
       next
     else
       #如果改变父节点是新G值更小则确定改变
       @open_list[@open_list.index(get_last_g_point)] = get_new_g_point
     end
   else
     if fp_passable?(point[0], point[1], d)
       # 如果不在开启列表中、且不在关闭列表中、且通行则添加它到新八周节点
       @open_list.push new_point(x, y, 10 - d)
       #如果将目标点添加到了开启列表中就返回true
       return true if x == @trg_x and y == @trg_y
     end
   end
end
#此刻没有找到目标点并将当前点加入关闭列表并在开启列表中删除
@close_list.push point
@open_list.delete(point)
#此刻没找到目标点并返回false
return false
end  #结束计算已知点的八方节点
#--------------------------------------------------------------------------
def in_open_list?(x, y)  #开始检查谋点是否在开启列表中
@open_list.each do |point|
   return true if point[0] == x and point[1] == y
end
return false
end  #结束检查谋点是否在开启列表中
#--------------------------------------------------------------------------
def in_close_list?(x, y)  #开始检查谋点是否在关闭列表中
@close_list.each do |point|
   return true if point[0] == x and point[1] == y
end
return false
end  #结束检查谋点是否在关闭列表中
#--------------------------------------------------------------------------
end
#==============================================================================
# ■ Find_Path
#------------------------------------------------------------------------------
#  寻路算法
#   By whbm
#==============================================================================
class Find_Path
#--------------------------------------------------------------------------
def initialize  #初始化
@open_list = []
@close_lise = []
end  #结束初始化
#--------------------------------------------------------------------------
def fp_passable?(x, y, d)  #开始判定通行
if $game_player.passable?(x, y, d)
   return true
else
   return false
end
end  #结束判定通行
#--------------------------------------------------------------------------
def get_g(now_point)  #开始计算G值
d = now_point[2]
return 0 if d == 5
father_point = get_father_point(now_point)
g = father_point[3] + 10
return g
end  #结束计算G值
#--------------------------------------------------------------------------
def get_h(now_point)  #开始计算H值(曼哈顿方法)
now_x = now_point[0]
now_y = now_point[1]
h = (@trg_x - now_x).abs + (@trg_y - now_y).abs
return h
end  #结束计算H值
#--------------------------------------------------------------------------
def get_f(now_point)  #开始计算F值
f = now_point[3] + now_point[4]
return f
end  #结束计算F值
#--------------------------------------------------------------------------
def get_point(x, y) #取已知坐标点
if @open_list.size != 0
   @open_list.each do |point|
     if point[0] == x and point[1] == y
       return point
       break
     end
   end
end
if @close_list.size != 0
   @close_list.each do |point|
     if point[0] == x and point[1] == y
       return point
       break
     end
   end
end
end  #结束取已知坐标点
#--------------------------------------------------------------------------
def get_father_point(now_point)  #取已知点的父节点
d = now_point[2]
return now_point if d == 5
x = now_point[0] + (d == 6 ? 1 : d == 4 ? -1 : 0)
y = now_point[1] + (d == 2 ? 1 : d == 8 ? -1 : 0)
return get_point(x, y)
end  #结束取已知点的父节点
#--------------------------------------------------------------------------
def new_point(x, y, d)  #开始建立新节点
#print x,y,d
point = [x, y, d]
point.push get_g(point)
point.push get_h(point)
point.push get_f(point)
return point
end  #结束建立新节点
#--------------------------------------------------------------------------
def find_short_path(self_x, self_y, trg_x, trg_y)  #开始搜索路径
return [] if not (fp_passable?(trg_x, trg_y, 8) or
                   fp_passable?(trg_x, trg_y, 4) or
                   fp_passable?(trg_x, trg_y, 6) or
                   fp_passable?(trg_x, trg_y, 2))
@self_x = self_x
@self_y = self_y
@now_x = self_x
@now_y = self_y
@trg_x = trg_x
@trg_y = trg_y
@open_list = []
@close_list = []
#准备搜索
#print @self_x,@self_y
@now_point = new_point(@self_x, @self_y, 5) #令起始点为当前点
@open_list.push @now_point #将当前点加入关闭列表
#开始搜索
loop do
   check_trg = check_around_point(@now_point)
   if check_trg == true
     @path = get_path
     break
   end
   @now_point = get_lowest_f_point
   if @now_point == [] or @now_point == nil
     @path = []
     break
   end
end
return @path
end  #结束搜索路径
#--------------------------------------------------------------------------
def find_player_short_path(trg_x, trg_y)  #寻找角色的最短路径
self_x = $game_player.x
self_y = $game_player.y
return find_short_path(self_x, self_y, trg_x, trg_y)
end  #结束角色的寻找路径
#--------------------------------------------------------------------------
def get_path  #取得最终的路径
path = []
now_point = @open_list[@open_list.size - 1]
path.push(10 - now_point[2])
last_point = now_point
loop do
   now_point = get_father_point(now_point)
   break if now_point[2] == 5
   path.push(10 - now_point[2])
end
return path.reverse
end  #结束取得最终的路径
#--------------------------------------------------------------------------
def get_lowest_f_point  #开始取得最低F值的点
if @open_list == []
   return []
end
last_lowest_f_point = @open_list[0]
@open_list.each do |point|
   last_lowest_f_point = point if point[5] < last_lowest_f_point[5]
end
return last_lowest_f_point
end  #结束取得最低F值点
#--------------------------------------------------------------------------
def check_around_point(point)  #开始检查已知点的四方节点
for d in [2, 4, 6, 8]
   x = point[0] + (d == 6 ? 1 : d == 4 ? -1 : 0)
   y = point[1] + (d == 2 ? 1 : d == 8 ? -1 : 0)
   if in_close_list?(x, y) #在关闭列表中
     next
   elsif in_open_list?(x, y) #在开启列表中
     get_new_g_point = new_point(x, y, 10 - d)
     get_last_g_point = get_point(x, y)
     if get_new_g_point[3] >= get_last_g_point[3]
       next
     else
       #如果改变父节点是新G值更小则确定改变
       @open_list[@open_list.index(get_last_g_point)] = get_new_g_point
     end
   else
     if fp_passable?(point[0], point[1], d)
       # 如果不在开启列表中、且不在关闭列表中、且通行则添加它到新八周节点
       @open_list.push new_point(x, y, 10 - d)
       #如果将目标点添加到了开启列表中就返回true
       return true if x == @trg_x and y == @trg_y
     end
   end
end
#此刻没有找到目标点并将当前点加入关闭列表并在开启列表中删除
@close_list.push point
@open_list.delete(point)
#此刻没找到目标点并返回false
return false
end  #结束计算已知点的四方节点
#--------------------------------------------------------------------------
def in_open_list?(x, y)  #开始检查谋点是否在开启列表中
@open_list.each do |point|
   return true if point[0] == x and point[1] == y
end
return false
end  #结束检查谋点是否在开启列表中
#--------------------------------------------------------------------------
def in_close_list?(x, y)  #开始检查谋点是否在关闭列表中
@close_list.each do |point|
   return true if point[0] == x and point[1] == y
end
return false
end  #结束检查谋点是否在关闭列表中
#--------------------------------------------------------------------------
end
#66编按:寻路算法有什么用?想来制作SLG的各位都是很清楚的。对于不做SLG的各位吗……用途不多。最多也就是在某些
#设置的时候可能能帮忙省些事。总之,自己看好了……




点评

这个脚本我已经找到了,但是现在是下一行出现错误了……  发表于 2010-12-5 17:49
>>猛戳>>MetalSagaR游戏主页<<这里<<
欢迎提供您的意见
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
4 小时
注册时间
2010-6-24
帖子
27
4
发表于 2010-12-11 18:27:01 | 只看该作者
找的寻路脚本版本不对,向方法传递的个数有问题
回复 支持 反对

使用道具 举报

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

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2025-2-21 18:21

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表