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

Project1

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

那个..有没有高手帮忙改下脚本

 关闭 [复制链接]

Lv1.梦旅人

梦石
0
星屑
50
在线时间
466 小时
注册时间
2006-2-25
帖子
1863
跳转到指定楼层
1
发表于 2008-1-20 05:41:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
RGSS2 Character的通行判定貌似有较大的变动
没有方向的判定了
所以..
想请高手帮忙改下4方向寻路脚本
  1. #==============================================================================
  2. # ■ Find_Path
  3. #------------------------------------------------------------------------------
  4. #  寻路算法
  5. #   By whbm
  6. #==============================================================================
  7. class Find_Path
  8. #--------------------------------------------------------------------------
  9. def initialize  #初始化
  10. @open_list = []
  11. @close_lise = []
  12. end  #结束初始化
  13. #--------------------------------------------------------------------------
  14. def fp_passable?(x, y, d)  #开始判定通行
  15. if $game_player.passable?(x, y, d)
  16.    return true
  17. else
  18.    return false
  19. end
  20. end  #结束判定通行
  21. #--------------------------------------------------------------------------
  22. def get_g(now_point)  #开始计算G值
  23. d = now_point[2]
  24. return 0 if d == 5
  25. father_point = get_father_point(now_point)
  26. g = father_point[3] + 10
  27. return g
  28. end  #结束计算G值
  29. #--------------------------------------------------------------------------
  30. def get_h(now_point)  #开始计算H值(曼哈顿方法)
  31. now_x = now_point[0]
  32. now_y = now_point[1]
  33. h = (@trg_x - now_x).abs + (@trg_y - now_y).abs
  34. return h
  35. end  #结束计算H值
  36. #--------------------------------------------------------------------------
  37. def get_f(now_point)  #开始计算F值
  38. f = now_point[3] + now_point[4]
  39. return f
  40. end  #结束计算F值
  41. #--------------------------------------------------------------------------
  42. def get_point(x, y) #取已知坐标点
  43. if @open_list.size != 0
  44.    @open_list.each do |point|
  45.      if point[0] == x and point[1] == y
  46.        return point
  47.        break
  48.      end
  49.    end
  50. end
  51. if @close_list.size != 0
  52.    @close_list.each do |point|
  53.      if point[0] == x and point[1] == y
  54.        return point
  55.        break
  56.      end
  57.    end
  58. end
  59. end  #结束取已知坐标点
  60. #--------------------------------------------------------------------------
  61. def get_father_point(now_point)  #取已知点的父节点
  62. d = now_point[2]
  63. return now_point if d == 5
  64. x = now_point[0] + (d == 6 ? 1 : d == 4 ? -1 : 0)
  65. y = now_point[1] + (d == 2 ? 1 : d == 8 ? -1 : 0)
  66. return get_point(x, y)
  67. end  #结束取已知点的父节点
  68. #--------------------------------------------------------------------------
  69. def new_point(x, y, d)  #开始建立新节点
  70. #print x,y,d
  71. point = [x, y, d]
  72. point.push get_g(point)
  73. point.push get_h(point)
  74. point.push get_f(point)
  75. return point
  76. end  #结束建立新节点
  77. #--------------------------------------------------------------------------
  78. def find_short_path(self_x, self_y, trg_x, trg_y)  #开始搜索路径
  79. return [] if not (fp_passable?(trg_x, trg_y, 8) or
  80.                    fp_passable?(trg_x, trg_y, 4) or
  81.                    fp_passable?(trg_x, trg_y, 6) or
  82.                    fp_passable?(trg_x, trg_y, 2))
  83. @self_x = self_x
  84. @self_y = self_y
  85. @now_x = self_x
  86. @now_y = self_y
  87. @trg_x = trg_x
  88. @trg_y = trg_y
  89. @open_list = []
  90. @close_list = []
  91. #准备搜索
  92. #print @self_x,@self_y
  93. @now_point = new_point(@self_x, @self_y, 5) #令起始点为当前点
  94. @open_list.push @now_point #将当前点加入关闭列表
  95. #开始搜索
  96. loop do
  97.    check_trg = check_around_point(@now_point)
  98.    if check_trg == true
  99.      @path = get_path
  100.      break
  101.    end
  102.    @now_point = get_lowest_f_point
  103.    if @now_point == [] or @now_point == nil
  104.      @path = []
  105.      break
  106.    end
  107. end
  108. return @path
  109. end  #结束搜索路径
  110. #--------------------------------------------------------------------------
  111. def find_player_short_path(trg_x, trg_y)  #寻找角色的最短路径
  112. self_x = $game_player.x
  113. self_y = $game_player.y
  114. return find_short_path(self_x, self_y, trg_x, trg_y)
  115. end  #结束角色的寻找路径
  116. #--------------------------------------------------------------------------
  117. def get_path  #取得最终的路径
  118. path = []
  119. now_point = @open_list[@open_list.size - 1]
  120. path.push(10 - now_point[2])
  121. last_point = now_point
  122. loop do
  123.    now_point = get_father_point(now_point)
  124.    break if now_point[2] == 5
  125.    path.push(10 - now_point[2])
  126. end
  127. return path.reverse
  128. end  #结束取得最终的路径
  129. #--------------------------------------------------------------------------
  130. def get_lowest_f_point  #开始取得最低F值的点
  131. if @open_list == []
  132.    return []
  133. end
  134. last_lowest_f_point = @open_list[0]
  135. @open_list.each do |point|
  136.    last_lowest_f_point = point if point[5] < last_lowest_f_point[5]
  137. end
  138. return last_lowest_f_point
  139. end  #结束取得最低F值点
  140. #--------------------------------------------------------------------------
  141. def check_around_point(point)  #开始检查已知点的四方节点
  142. for d in [2, 4, 6, 8]
  143.    x = point[0] + (d == 6 ? 1 : d == 4 ? -1 : 0)
  144.    y = point[1] + (d == 2 ? 1 : d == 8 ? -1 : 0)
  145.    if in_close_list?(x, y) #在关闭列表中
  146.      next
  147.    elsif in_open_list?(x, y) #在开启列表中
  148.      get_new_g_point = new_point(x, y, 10 - d)
  149.      get_last_g_point = get_point(x, y)
  150.      if get_new_g_point[3] >= get_last_g_point[3]
  151.        next
  152.      else
  153.        #如果改变父节点是新G值更小则确定改变
  154.        @open_list[@open_list.index(get_last_g_point)] = get_new_g_point
  155.      end
  156.    else
  157.      if fp_passable?(point[0], point[1], d)
  158.        # 如果不在开启列表中、且不在关闭列表中、且通行则添加它到新八周节点
  159.        @open_list.push new_point(x, y, 10 - d)
  160.        #如果将目标点添加到了开启列表中就返回true
  161.        return true if x == @trg_x and y == @trg_y
  162.      end
  163.    end
  164. end
  165. #此刻没有找到目标点并将当前点加入关闭列表并在开启列表中删除
  166. @close_list.push point
  167. @open_list.delete(point)
  168. #此刻没找到目标点并返回false
  169. return false
  170. end  #结束计算已知点的四方节点
  171. #--------------------------------------------------------------------------
  172. def in_open_list?(x, y)  #开始检查谋点是否在开启列表中
  173. @open_list.each do |point|
  174.    return true if point[0] == x and point[1] == y
  175. end
  176. return false
  177. end  #结束检查谋点是否在开启列表中
  178. #--------------------------------------------------------------------------
  179. def in_close_list?(x, y)  #开始检查谋点是否在关闭列表中
  180. @close_list.each do |point|
  181.    return true if point[0] == x and point[1] == y
  182. end
  183. return false
  184. end  #结束检查谋点是否在关闭列表中
  185. #--------------------------------------------------------------------------
  186. end
复制代码

原地址:http://rpg.blue/web/htm/news457.htm
本人脑袋太笨了- -
看不太懂别人写的东西
而且里面还有什么G,H,F点和曼哈顿方法什么的
看了头就更晕了- -
希望高手帮忙下
感激
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

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

GMT+8, 2024-5-1 13:09

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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