幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1435|回复: 1

[已解决] 晕,这个好象是四方向的鼠标脚本

[复制链接]

38

主题

129

帖子

1151

积分

⑥精研

无既空,空既色

积分
1151
发表于 2010-1-20 12:54:05 | 显示全部楼层 |阅读模式
  1. #------------------------------------------------------------------------------
  2. #  寻路算法--完整鼠标系统(四方向)专用版
  3. #   By whbm
  4. #==============================================================================
  5. class Find_Path
  6. #--------------------------------------------------------------------------
  7. def initialize  #初始化
  8. @open_list = []
  9. @close_lise = []
  10. @path = []
  11. end  #结束初始化
  12. #--------------------------------------------------------------------------
  13. def fp_passable?(x, y, d, tr_x = -2, tr_y = -2)  #开始判定通行
  14. return false if (tr_x == @unable_xa or
  15.                  tr_x == @unable_xb or
  16.                  tr_y == @unable_ya or
  17.                  tr_y == @unable_yb)
  18. if $game_player.passable?(x, y, d)
  19.    return true
  20. else
  21.    return false
  22. end
  23. end  #结束判定通行
  24. #--------------------------------------------------------------------------
  25. def get_g(now_point)  #开始计算G值
  26. d = now_point[2]
  27. return 0 if d == 5
  28. father_point = get_father_point(now_point)
  29. g = father_point[3] + 10
  30. return g
  31. end  #结束计算G值
  32. #--------------------------------------------------------------------------
  33. def get_h(now_point)  #开始计算H值
  34. now_x = now_point[0]
  35. now_y = now_point[1]
  36. #print @trg_x,now_x,@trg_y,now_y
  37. h = (@trg_x - now_x).abs + (@trg_y - now_y).abs
  38. return h * 10
  39. end  #结束计算H值
  40. #--------------------------------------------------------------------------
  41. def get_f(now_point)  #开始计算F值
  42. f = now_point[3] + now_point[4]
  43. return f
  44. end  #结束计算F值
  45. #--------------------------------------------------------------------------
  46. def get_point(x, y) #取已知坐标点
  47. if @open_list.size != 0
  48.    @open_list.each do |point|
  49.      if point[0] == x and point[1] == y
  50.        return point
  51.        break
  52.      end
  53.    end
  54. end
  55. if @close_list.size != 0
  56.    @close_list.each do |point|
  57.      if point[0] == x and point[1] == y
  58.        return point
  59.        break
  60.      end
  61.    end
  62. end
  63. end  #结束取已知坐标点
  64. #--------------------------------------------------------------------------
  65. def get_father_point(now_point)  #取已知点的父节点
  66. d = now_point[2]
  67. return now_point if d == 5
  68. x = now_point[0] + (d == 6 ? 1 : (d == 4 ? -1 : 0))
  69. y = now_point[1] + (d == 2 ? 1 : (d == 8 ? -1 : 0))
  70. return get_point(x, y)
  71. end  #结束取已知点的父节点
  72. #--------------------------------------------------------------------------
  73. def new_point(x, y, d)  #开始建立新节点
  74. #print x,y,d
  75. point = [x, y, d]
  76. point.push get_g(point)
  77. point.push get_h(point)
  78. point.push get_f(point)
  79. return point
  80. end  #结束建立新节点
  81. #--------------------------------------------------------------------------
  82. def get_direction(self_x, self_y, trg_x, trg_y)
  83.   if trg_x > self_x
  84.     if trg_y - self_y > -  ( trg_x - self_x ) and
  85.       trg_y - self_y < ( trg_x - self_x )
  86.       return 6
  87.     end
  88.     if trg_y - self_y > ( trg_x - self_x )
  89.       return 2
  90.     end
  91.     if trg_y - self_y < - ( trg_x - self_x )
  92.       return 8
  93.     end
  94.   end
  95.   if trg_x < self_x
  96.     if trg_y - self_y > - ( self_x - trg_x ) and
  97.       trg_y - self_y < ( self_x - trg_x )
  98.       return 4
  99.     end
  100.     if trg_y - self_y > ( self_x - trg_x )
  101.       return 2
  102.     end
  103.     if trg_y - self_y < - ( self_x - trg_x )
  104.       return 8
  105.     end
  106.   end
  107. end
  108. #--------------------------------------------------------------------------
  109. def get_d_x_y(x, y, d)
  110.   d_x = x + (d == 6 ? 1 : (d == 4 ? -1 : 0))
  111.   d_y = y + (d == 2 ? 1 : (d == 8 ? -1 : 0))
  112.   return d_x, d_y
  113. end
  114. #--------------------------------------------------------------------------
  115. def find_short_path_other(self_x, self_y, trg_x, trg_y,
  116.                           real_self_x, real_self_y, real_trg_x, real_trg_y)
  117.   @self_x = self_x
  118.   @self_y = self_y
  119.   @now_x = self_x
  120.   @now_y = self_y
  121.   @trg_x = trg_x
  122.   @trg_y = trg_y
  123.   @path = []
  124.   direction = get_direction(real_self_x, real_self_y, real_trg_x, real_trg_y)
  125.   @now_trg_x, @now_trg_y = get_d_x_y(@self_x, @self_y, direction)
  126.   while fp_passable?(@now_x, @now_y, direction)
  127.     @path.push direction
  128.     @now_x = @now_trg_x
  129.     @now_y = @now_trg_y
  130.     @now_trg_x, @now_trg_y = get_d_x_y(@now_x, @now_y, direction)
  131.   end
  132.   return @path
  133. end
  134. #--------------------------------------------------------------------------
  135. def find_short_path(self_x, self_y, trg_x, trg_y,
  136.                     real_self_x, real_self_y, real_trg_x, real_trg_y)  #开始搜索路径
  137.   
  138. return find_short_path_other(self_x, self_y, trg_x, trg_y,
  139.                               real_self_x, real_self_y, real_trg_x, real_trg_y) if not
  140.                   (fp_passable?(trg_x, trg_y + 1, 8) or
  141.                    fp_passable?(trg_x + 1, trg_y, 4) or
  142.                    fp_passable?(trg_x - 1, trg_y, 6) or
  143.                    fp_passable?(trg_x, trg_y - 1, 2)) and @goal_type != 1
  144.                   
  145.                   
  146.   #根据屏幕限定搜索面积..加速
  147. @unable_xa = $game_map.display_x / 128 - 1
  148. @unable_ya = $game_map.display_y / 128 - 1
  149. @unable_xb = $game_map.display_x / 128 + 20
  150. @unable_yb = $game_map.display_y / 128 + 20
  151. @self_x = self_x
  152. @self_y = self_y
  153. @now_x = self_x
  154. @now_y = self_y
  155. @trg_x = trg_x
  156. @trg_y = trg_y
  157. @open_list = []
  158. @close_list = []
  159. #准备搜索
  160. #print @self_x,@self_y
  161. @now_point = new_point(@self_x, @self_y, 5) #令起始点为当前点
  162. @open_list.push @now_point #将当前点加入关闭列表
  163. #开始搜索
  164. begin
  165. loop do
  166.    check_trg = check_around_point(@now_point)
  167.    if check_trg == true
  168.      @path = get_path
  169.      break
  170.    end
  171.    @now_point = get_lowest_f_point
  172.    if @now_point == [] or @now_point == nil
  173.      @path = []
  174.      break
  175.    end
  176. end
  177. rescue Hangup
  178.   retry
  179. end
  180. return @path
  181. end  #结束搜索路径
  182. #--------------------------------------------------------------------------
  183. def find_player_short_path(trg_x, trg_y,
  184.                            real_trg_x, real_trg_y)  #寻找角色的最短路径
  185. self_x = $game_player.x
  186. self_y = $game_player.y
  187. real_self_x = $game_player.screen_x
  188. real_self_y = $game_player.screen_y
  189. @goal_type, event = $game_map.check_event_custom_exist(real_trg_x, real_trg_y)
  190. if @goal_type == 1
  191.    trg_x = event.x
  192.    trg_y = event.y
  193. end
  194. return find_short_path(self_x, self_y, trg_x, trg_y,
  195.                         real_self_x, real_self_y, real_trg_x, real_trg_y)
  196. end  #结束角色的寻找路径
  197. #--------------------------------------------------------------------------
  198. def get_path  #取得最终的路径
  199. path = []
  200. now_point = @open_list[@open_list.size - 1]
  201. path.push(10 - now_point[2])
  202. last_point = now_point
  203. loop do
  204.    now_point = get_father_point(now_point)
  205.    break if now_point[2] == 5
  206.    path.push(10 - now_point[2])
  207. end
  208. return path.reverse
  209. end  #结束取得最终的路径
  210. #--------------------------------------------------------------------------
  211. def get_lowest_f_point  #开始取得最低F值的点
  212. if @open_list == []
  213.    return []
  214. end
  215. last_lowest_f_point = @open_list[0]
  216. @open_list.each do |point|
  217.    last_lowest_f_point = point if point[5] < last_lowest_f_point[5]
  218. end
  219. return last_lowest_f_point
  220. end  #结束取得最低F值点
  221. #--------------------------------------------------------------------------
  222. def check_around_point(point)  #开始检查已知点的八方节点
  223. for d in [2, 4, 6, 8]
  224.    x = point[0] + (d == 6 ? 1 : (d == 4 ? -1 : 0))
  225.    y = point[1] + (d == 2 ? 1 : (d == 8 ? -1 : 0))
  226.    if in_close_list?(x, y) #在关闭列表中
  227.      next
  228.    elsif in_open_list?(x, y) #在开启列表中
  229.      get_new_g_point = new_point(x, y, 10 - d)
  230.      get_last_g_point = get_point(x, y)
  231.      if get_new_g_point[3] >= get_last_g_point[3]
  232.        next
  233.      else
  234.        #如果改变父节点是新G值更小则确定改变
  235.        @open_list[@open_list.index(get_last_g_point)] = get_new_g_point
  236.      end
  237.    else
  238.      if fp_passable?(point[0], point[1], d, x, y)
  239.        # 如果不在开启列表中、且不在关闭列表中、且通行则添加它到新八周节点
  240.        @open_list.push new_point(x, y, 10 - d)
  241.        #如果将目标点添加到了开启列表中就返回true
  242.        return true if x == @trg_x and y == @trg_y
  243.        return true if @goal_type == 1 and ([1, -1].include?(x - @trg_x) and y - @trg_y == 0) or ([1, -1].include?(y - @trg_y) and x - @trg_x == 0)
  244.      end
  245.    end
  246. end
  247. #此刻没有找到目标点并将当前点加入关闭列表并在开启列表中删除
  248. @close_list.push point
  249. @open_list.delete(point)
  250. #此刻没找到目标点并返回false
  251. return false
  252. end  #结束计算已知点的八方节点
  253. #--------------------------------------------------------------------------
  254. def in_open_list?(x, y)  #开始检查谋点是否在开启列表中
  255. @open_list.each do |point|
  256.    return true if point[0] == x and point[1] == y
  257. end
  258. return false
  259. end  #结束检查谋点是否在开启列表中
  260. #--------------------------------------------------------------------------
  261. def in_close_list?(x, y)  #开始检查谋点是否在关闭列表中
  262. @close_list.each do |point|
  263.    return true if point[0] == x and point[1] == y
  264. end
  265. return false
  266. end  #结束检查谋点是否在关闭列表中
  267. #--------------------------------------------------------------------------
复制代码
回复

使用道具 举报

550

主题

9116

帖子

214748万

积分

超级版主

如同神一般的存在,腿神!拖后腿的神~~

Rank: 8Rank: 8

积分
2147483647
发表于 2010-1-20 13:08:54 | 显示全部楼层
这不写的4方向么?有什么疑问么?
我就是你们的神,庶民们,追随我吧!跟着我一起拖后腿!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-5-14 23:06 , Processed in 0.022284 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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