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

Project1

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

[已经解决] RMXP关于myownroc的寻路系统如何中途停止寻路

[复制链接]

Lv1.梦旅人

梦石
0
星屑
85
在线时间
210 小时
注册时间
2013-7-26
帖子
346
跳转到指定楼层
1
发表于 2014-11-12 20:27:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
@myownroc  M君大神
终止命令是什么? 自动寻路的过程中 中途停止  。另外我用的是鼠标系统+八方向

写了个小地图系统 排得上用场,虽然搜索到了小地图寻路,不过那个太卡了,CPU暴涨,M君这个不错。
谢谢了
  1. #寻路 调用方法 BFS.find_path($game_player,x,y)

  2. module BFS
  3.   def self.find_path(character, tx, ty)
  4.     ox, oy = character.x, character.y
  5.     return if (tx > $game_map.width - 1) || (ty > $game_map.height - 1)
  6.     return if !($game_map.passable?(tx, ty, 0, character))
  7.     return if (tx == ox) && (ty == oy)
  8.     checked = []
  9.     father = []
  10.     for i in 0...$game_map.width
  11.       checked << []
  12.       father << []
  13.       for j in 0...$game_map.height
  14.         checked[-1] << 0
  15.         father[-1] << nil
  16.       end
  17.     end
  18.     path = []
  19.     found = false
  20.     checked[ox][oy] = 1
  21.     empty = false
  22.     loop do
  23.       empty = true
  24.       for i in 0...$game_map.width
  25.         for j in 0...$game_map.height
  26.           if checked[i][j] == 1
  27.             checked[i][j] = 2
  28.             if $game_map.passable?(i, j + 1, 2, character) &&
  29.               checked[i][j + 1] == 0
  30.               checked[i][j + 1] = 1
  31.               father[i][j + 1] = [i, j]
  32.             end
  33.             if $game_map.passable?(i - 1, j, 4, character) &&
  34.               checked[i - 1][j] == 0
  35.               checked[i - 1][j] = 1
  36.               father[i - 1][j] = [i, j]
  37.             end
  38.             if $game_map.passable?(i, j - 1, 8, character) &&
  39.               checked[i][j - 1] == 0
  40.               checked[i][j - 1] = 1
  41.               father[i][j - 1] = [i, j]
  42.             end
  43.             if $game_map.passable?(i + 1, j, 6, character) &&
  44.               checked[i + 1][j] == 0
  45.               checked[i + 1][j] = 1
  46.               father[i + 1][j] = [i, j]
  47.             end
  48.           end
  49.           if checked[tx][ty] == 1
  50.             found = true
  51.             break
  52.           end
  53.         end
  54.       end
  55.       for i in 0...$game_map.width
  56.         empty = false if checked[i].include?(1)
  57.       end
  58.       if found || empty
  59.         break
  60.       end
  61.     end
  62.     return if !found
  63.     if found
  64.       x, y = tx, ty
  65.       while [x, y] != [ox, oy]
  66.         dx, dy = father[x][y][0] - x, father[x][y][1] - y
  67.         step = 2 if dy == -1
  68.         step = 4 if dx == 1
  69.         step = 8 if dy == 1
  70.         step = 6 if dx == -1
  71.         path << step
  72.         x, y = father[x][y][0], father[x][y][1]
  73.       end
  74.       path.reverse!
  75.     end
  76.     list = []
  77.     path.each{|e|
  78.       code = 1 if e == 2
  79.       code = 2 if e == 4
  80.       code = 4 if e == 8
  81.       code = 3 if e == 6
  82.       list << RPG::MoveCommand.new(code)
  83.     }
  84.     list << RPG::MoveCommand.new(0)
  85.     skippable = false
  86.     repeat = false
  87.     commands = RPG::MoveRoute.new
  88.     commands.skippable = skippable
  89.     commands.repeat = repeat
  90.     commands.list = list
  91.     if character == nil
  92.       return true
  93.     end
  94.     character.force_move_route(commands)
  95.     return true
  96.   end
  97. end
复制代码

Lv3.寻梦者

梦石
0
星屑
2744
在线时间
2630 小时
注册时间
2013-1-16
帖子
5657

贵宾

2
发表于 2014-11-12 21:34:49 | 只看该作者
本帖最后由 myownroc 于 2014-11-12 21:37 编辑

目前暂时不支持中途停下(因为不知道停下的条件是什么=_=)
于是你能不能说一说为什么要停下(先猜一下:是不是用了鼠标脚本之类的……)?

点评

↓可以吧=_=  发表于 2014-11-12 22:42
我可以问个问题不 $xdtkg = FileTest.exist?("Graphics\Pictures\#{$game_map.name}0000.png") 怎么在这里变量就用不了?  发表于 2014-11-12 22:06
↓我试试看按Esc取消  发表于 2014-11-12 21:45
BFS.find_path($game_player,$game_player.x + 1,$game_player.y + 1) 虽然不能立刻停止 但能起到点效果  发表于 2014-11-12 21:41
我弄到小地图里面,如果玩家突然不想寻路了。。怎么办 貌似有办法了  发表于 2014-11-12 21:38
(Created by @喵kano)


施工现场:hotege.github.io
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
85
在线时间
210 小时
注册时间
2013-7-26
帖子
346
3
 楼主| 发表于 2014-11-14 19:24:06 | 只看该作者

关于M君的寻路

可以每行都注释一下吗?我想学习学习
拜托了
  1. module BFS
  2.   def self.find_path(character, tx, ty)
  3.     ox, oy = character.x, character.y
  4.     return if (tx > $game_map.width - 1) || (ty > $game_map.height - 1)
  5.     return if !($game_map.passable?(tx, ty, 0, character))
  6.     return if (tx == ox) && (ty == oy)
  7.     checked = []
  8.     father = []
  9.     for i in 0...$game_map.width
  10.       checked << []
  11.       father << []
  12.       for j in 0...$game_map.height
  13.         checked[-1] << 0
  14.         father[-1] << nil
  15.       end
  16.     end
  17.     path = []
  18.     found = false
  19.     checked[ox][oy] = 1
  20.     empty = false
  21.     loop do
  22.       empty = true
  23.       for i in 0...$game_map.width
  24.         for j in 0...$game_map.height
  25.           if checked[i][j] == 1
  26.             checked[i][j] = 2
  27.             if $game_map.passable?(i, j + 1, 2, character) &&
  28.               checked[i][j + 1] == 0
  29.               checked[i][j + 1] = 1
  30.               father[i][j + 1] = [i, j]
  31.             end
  32.             if $game_map.passable?(i - 1, j, 4, character) &&
  33.               checked[i - 1][j] == 0
  34.               checked[i - 1][j] = 1
  35.               father[i - 1][j] = [i, j]
  36.             end
  37.             if $game_map.passable?(i, j - 1, 8, character) &&
  38.               checked[i][j - 1] == 0
  39.               checked[i][j - 1] = 1
  40.               father[i][j - 1] = [i, j]
  41.             end
  42.             if $game_map.passable?(i + 1, j, 6, character) &&
  43.               checked[i + 1][j] == 0
  44.               checked[i + 1][j] = 1
  45.               father[i + 1][j] = [i, j]
  46.             end
  47.           end
  48.           if checked[tx][ty] == 1
  49.             found = true
  50.             break
  51.           end
  52.         end
  53.       end
  54.       for i in 0...$game_map.width
  55.         empty = false if checked[i].include?(1)
  56.       end
  57.       if found || empty
  58.         break
  59.       end
  60.     end
  61.     return if !found
  62.     if found
  63.       x, y = tx, ty
  64.       while [x, y] != [ox, oy]
  65.         dx, dy = father[x][y][0] - x, father[x][y][1] - y
  66.         step = 2 if dy == -1
  67.         step = 4 if dx == 1
  68.         step = 8 if dy == 1
  69.         step = 6 if dx == -1
  70.         path << step
  71.         x, y = father[x][y][0], father[x][y][1]
  72.       end
  73.       path.reverse!
  74.     end
  75.     list = []
  76.     path.each{|e|
  77.       code = 1 if e == 2
  78.       code = 2 if e == 4
  79.       code = 4 if e == 8
  80.       code = 3 if e == 6
  81.       list << RPG::MoveCommand.new(code)
  82.     }
  83.     list << RPG::MoveCommand.new(0)
  84.     skippable = false
  85.     repeat = false
  86.     commands = RPG::MoveRoute.new
  87.     commands.skippable = skippable
  88.     commands.repeat = repeat
  89.     commands.list = list
  90.     if character == nil
  91.       return true
  92.     end
  93.     character.force_move_route(commands)
  94.     return true
  95.   end
  96. end
复制代码

点评

不过脚本中的算法思想可能有点难理解,我先注释,不懂再问即可。  发表于 2014-11-14 19:48
ok不过以后这样的问题发短信息吧,另外寻路中断有了进展。  发表于 2014-11-14 19:42
回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
2744
在线时间
2630 小时
注册时间
2013-1-16
帖子
5657

贵宾

4
发表于 2014-11-14 19:44:48 | 只看该作者
提问区.rar (199.3 KB, 下载次数: 44)
按Esc中断,不过会呼出菜单=_=(不过我是不是可以改成其他按键……)
(Created by @喵kano)


施工现场:hotege.github.io
回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
2744
在线时间
2630 小时
注册时间
2013-1-16
帖子
5657

贵宾

5
发表于 2014-11-14 20:10:57 | 只看该作者
  1. module BFS
  2.   def self.find_path(character, tx, ty)
  3.     #设置起点坐标
  4.     ox, oy = character.x, character.y
  5.          #如果目的地超出了地图就退出
  6.     return if (tx > $game_map.width - 1) || (ty > $game_map.height - 1)
  7.          #如果目的地不可通行(全方向)就退出
  8.     return if !($game_map.passable?(tx, ty, 0, character))
  9.          #如果目的地就是起点就退出
  10.     return if (tx == ox) && (ty == oy)
  11.          #初始化数组checked,用于标记已经检测过的点
  12.     checked = []
  13.          #初始化数组father,用于存放点的父节点(父节点就是来到一个点之前要到达的点)
  14.     father = []
  15.          #给数组赋值
  16.     for i in 0...$game_map.width
  17.       checked << []
  18.       father << []
  19.       for j in 0...$game_map.height
  20.         checked[-1] << 0
  21.         father[-1] << nil
  22.       end
  23.     end
  24.          #初始化数组path,用于存放路径
  25.     path = []
  26.          #found表示是否达到目的地
  27.     found = false
  28.          #将起点标记为“待检测”(0为未检测但不需要检测,1为待检测,2为已经检测)
  29.     checked[ox][oy] = 1
  30.          #empty表示是否还存在待检测的点
  31.     empty = false
  32.          #算法的核心
  33.     loop do
  34.                 #假定已经没有待检测的点了
  35.       empty = true
  36.            #对地图的每一个点进行判断
  37.       for i in 0...$game_map.width
  38.         for j in 0...$game_map.height
  39.                           #如果点是待检测点
  40.           if checked[i][j] == 1
  41.                        #把它标记为已经检测
  42.             checked[i][j] = 2
  43.                             #判断这个点旁边四个点是否能通过该点到达且旁边的这些点是否未检测(依次为↓←↑→)
  44.             if $game_map.passable?(i, j + 1, 2, character) &&
  45.               checked[i][j + 1] == 0
  46.                               #标记为待检测
  47.               checked[i][j + 1] = 1
  48.                               #设置旁边的点的父节点为当前的点(经过当前的点才能到达旁边的点)
  49.               father[i][j + 1] = [i, j]
  50.             end
  51.             if $game_map.passable?(i - 1, j, 4, character) &&
  52.               checked[i - 1][j] == 0
  53.               checked[i - 1][j] = 1
  54.               father[i - 1][j] = [i, j]
  55.             end
  56.             if $game_map.passable?(i, j - 1, 8, character) &&
  57.               checked[i][j - 1] == 0
  58.               checked[i][j - 1] = 1
  59.               father[i][j - 1] = [i, j]
  60.             end
  61.             if $game_map.passable?(i + 1, j, 6, character) &&
  62.               checked[i + 1][j] == 0
  63.               checked[i + 1][j] = 1
  64.               father[i + 1][j] = [i, j]
  65.             end
  66.           end
  67.                     #如果目的地是待检测的点,说明已经找到目的地了
  68.           if checked[tx][ty] == 1
  69.                       #找到=_=
  70.             found = true
  71.                            #跳出循环
  72.             break
  73.           end
  74.         end
  75.       end
  76.            #判断是否还存在待检测的点
  77.       for i in 0...$game_map.width
  78.         empty = false if checked[i].include?(1)
  79.       end
  80.            #如果不存在待检测的点就跳出循环
  81.       if found || empty
  82.         break
  83.       end
  84.     end
  85.          #如果没找到到达目的地的路径就退出
  86.     return if !found
  87.          #找到了的话就生成路径(2、4、6、8代表↓←→↑)
  88.     if found
  89.       x, y = tx, ty
  90.       while [x, y] != [ox, oy]
  91.         dx, dy = father[x][y][0] - x, father[x][y][1] - y
  92.         step = 2 if dy == -1
  93.         step = 4 if dx == 1
  94.         step = 8 if dy == 1
  95.         step = 6 if dx == -1
  96.         path << step
  97.         x, y = father[x][y][0], father[x][y][1]
  98.       end
  99.       path.reverse!
  100.     end
  101.          #将2、4、6、8翻译为移动角色的命令
  102.     list = []
  103.     path.each{|e|
  104.       code = 1 if e == 2
  105.       code = 2 if e == 4
  106.       code = 4 if e == 8
  107.       code = 3 if e == 6
  108.       list << RPG::MoveCommand.new(code)
  109.     }
  110.     list << RPG::MoveCommand.new(0)
  111.     skippable = false
  112.     repeat = false
  113.     commands = RPG::MoveRoute.new
  114.     commands.skippable = skippable
  115.     commands.repeat = repeat
  116.     commands.list = list
  117.          #如果角色不存在就退出否则就强制移动角色
  118.     if character == nil
  119.       return true
  120.     end
  121.     character.find_path = true
  122.     character.force_move_route(commands)
  123.     return true
  124.   end
  125. end
复制代码

点评

↓说的是借助=_=  发表于 2014-11-15 17:37
还是图结构最容易实现广度优先了,不解释= =  发表于 2014-11-15 17:34
↓广度优先搜索不像A*那样有启发性,所以队列、栈、链表等线性数据结构都可以而且差别不会很大。  发表于 2014-11-15 14:15
求问广度优先是不是借助队列来实现?  发表于 2014-11-15 13:57
谢谢  发表于 2014-11-14 23:30

评分

参与人数 3星屑 +33 梦石 +1 收起 理由
RyanBern + 1 塞糖
夕仔 + 10
kuerlulu + 23 赞版主认真回复

查看全部评分

(Created by @喵kano)


施工现场:hotege.github.io
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
85
在线时间
210 小时
注册时间
2013-7-26
帖子
346
6
 楼主| 发表于 2014-11-14 23:39:47 | 只看该作者
A寻路初探.rar (73.63 KB, 下载次数: 50)
@myownroc 这是在6R找到的自动寻路 感觉效率不错,但我不会转换成地图,路障不知道怎么弄。。你的寻路用于100以内的地图效果非常不错,但是超过100很多就会卡掉了。。{:2_264:}

点评

你最好人了 萌萌的  发表于 2014-11-15 13:47
好吧,给我点时间  发表于 2014-11-15 12:42
要发 要发,我是把自动寻路的时候把鼠标系统禁用这个问题解决了,寻路当然没有。。  发表于 2014-11-15 11:01
问题解决了?不用我再发东西了》  发表于 2014-11-15 01:38
解决了。。  发表于 2014-11-15 01:10
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-9-22 12:48

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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