Project1
标题:
RMXP关于myownroc的寻路系统如何中途停止寻路
[打印本页]
作者:
夕仔
时间:
2014-11-12 20:27
标题:
RMXP关于myownroc的寻路系统如何中途停止寻路
@myownroc
M君大神
终止命令是什么? 自动寻路的过程中 中途停止 。另外我用的是鼠标系统+八方向
写了个小地图系统 排得上用场,虽然搜索到了小地图寻路,不过那个太卡了,CPU暴涨,M君这个不错。
谢谢了
#寻路 调用方法 BFS.find_path($game_player,x,y)
module BFS
def self.find_path(character, tx, ty)
ox, oy = character.x, character.y
return if (tx > $game_map.width - 1) || (ty > $game_map.height - 1)
return if !($game_map.passable?(tx, ty, 0, character))
return if (tx == ox) && (ty == oy)
checked = []
father = []
for i in 0...$game_map.width
checked << []
father << []
for j in 0...$game_map.height
checked[-1] << 0
father[-1] << nil
end
end
path = []
found = false
checked[ox][oy] = 1
empty = false
loop do
empty = true
for i in 0...$game_map.width
for j in 0...$game_map.height
if checked[i][j] == 1
checked[i][j] = 2
if $game_map.passable?(i, j + 1, 2, character) &&
checked[i][j + 1] == 0
checked[i][j + 1] = 1
father[i][j + 1] = [i, j]
end
if $game_map.passable?(i - 1, j, 4, character) &&
checked[i - 1][j] == 0
checked[i - 1][j] = 1
father[i - 1][j] = [i, j]
end
if $game_map.passable?(i, j - 1, 8, character) &&
checked[i][j - 1] == 0
checked[i][j - 1] = 1
father[i][j - 1] = [i, j]
end
if $game_map.passable?(i + 1, j, 6, character) &&
checked[i + 1][j] == 0
checked[i + 1][j] = 1
father[i + 1][j] = [i, j]
end
end
if checked[tx][ty] == 1
found = true
break
end
end
end
for i in 0...$game_map.width
empty = false if checked[i].include?(1)
end
if found || empty
break
end
end
return if !found
if found
x, y = tx, ty
while [x, y] != [ox, oy]
dx, dy = father[x][y][0] - x, father[x][y][1] - y
step = 2 if dy == -1
step = 4 if dx == 1
step = 8 if dy == 1
step = 6 if dx == -1
path << step
x, y = father[x][y][0], father[x][y][1]
end
path.reverse!
end
list = []
path.each{|e|
code = 1 if e == 2
code = 2 if e == 4
code = 4 if e == 8
code = 3 if e == 6
list << RPG::MoveCommand.new(code)
}
list << RPG::MoveCommand.new(0)
skippable = false
repeat = false
commands = RPG::MoveRoute.new
commands.skippable = skippable
commands.repeat = repeat
commands.list = list
if character == nil
return true
end
character.force_move_route(commands)
return true
end
end
复制代码
作者:
myownroc
时间:
2014-11-12 21:34
本帖最后由 myownroc 于 2014-11-12 21:37 编辑
目前暂时不支持中途停下(因为不知道停下的条件是什么=_=)
于是你能不能说一说为什么要停下(先猜一下:是不是用了鼠标脚本之类的……)?
作者:
夕仔
时间:
2014-11-14 19:24
标题:
关于M君的寻路
可以每行都注释一下吗?我想学习学习
拜托了
module BFS
def self.find_path(character, tx, ty)
ox, oy = character.x, character.y
return if (tx > $game_map.width - 1) || (ty > $game_map.height - 1)
return if !($game_map.passable?(tx, ty, 0, character))
return if (tx == ox) && (ty == oy)
checked = []
father = []
for i in 0...$game_map.width
checked << []
father << []
for j in 0...$game_map.height
checked[-1] << 0
father[-1] << nil
end
end
path = []
found = false
checked[ox][oy] = 1
empty = false
loop do
empty = true
for i in 0...$game_map.width
for j in 0...$game_map.height
if checked[i][j] == 1
checked[i][j] = 2
if $game_map.passable?(i, j + 1, 2, character) &&
checked[i][j + 1] == 0
checked[i][j + 1] = 1
father[i][j + 1] = [i, j]
end
if $game_map.passable?(i - 1, j, 4, character) &&
checked[i - 1][j] == 0
checked[i - 1][j] = 1
father[i - 1][j] = [i, j]
end
if $game_map.passable?(i, j - 1, 8, character) &&
checked[i][j - 1] == 0
checked[i][j - 1] = 1
father[i][j - 1] = [i, j]
end
if $game_map.passable?(i + 1, j, 6, character) &&
checked[i + 1][j] == 0
checked[i + 1][j] = 1
father[i + 1][j] = [i, j]
end
end
if checked[tx][ty] == 1
found = true
break
end
end
end
for i in 0...$game_map.width
empty = false if checked[i].include?(1)
end
if found || empty
break
end
end
return if !found
if found
x, y = tx, ty
while [x, y] != [ox, oy]
dx, dy = father[x][y][0] - x, father[x][y][1] - y
step = 2 if dy == -1
step = 4 if dx == 1
step = 8 if dy == 1
step = 6 if dx == -1
path << step
x, y = father[x][y][0], father[x][y][1]
end
path.reverse!
end
list = []
path.each{|e|
code = 1 if e == 2
code = 2 if e == 4
code = 4 if e == 8
code = 3 if e == 6
list << RPG::MoveCommand.new(code)
}
list << RPG::MoveCommand.new(0)
skippable = false
repeat = false
commands = RPG::MoveRoute.new
commands.skippable = skippable
commands.repeat = repeat
commands.list = list
if character == nil
return true
end
character.force_move_route(commands)
return true
end
end
复制代码
作者:
myownroc
时间:
2014-11-14 19:44
提问区.rar
(199.3 KB, 下载次数: 44)
2014-11-14 19:43 上传
点击文件名下载附件
按Esc中断,不过会呼出菜单=_=(不过我是不是可以改成其他按键……)
作者:
myownroc
时间:
2014-11-14 20:10
module BFS
def self.find_path(character, tx, ty)
#设置起点坐标
ox, oy = character.x, character.y
#如果目的地超出了地图就退出
return if (tx > $game_map.width - 1) || (ty > $game_map.height - 1)
#如果目的地不可通行(全方向)就退出
return if !($game_map.passable?(tx, ty, 0, character))
#如果目的地就是起点就退出
return if (tx == ox) && (ty == oy)
#初始化数组checked,用于标记已经检测过的点
checked = []
#初始化数组father,用于存放点的父节点(父节点就是来到一个点之前要到达的点)
father = []
#给数组赋值
for i in 0...$game_map.width
checked << []
father << []
for j in 0...$game_map.height
checked[-1] << 0
father[-1] << nil
end
end
#初始化数组path,用于存放路径
path = []
#found表示是否达到目的地
found = false
#将起点标记为“待检测”(0为未检测但不需要检测,1为待检测,2为已经检测)
checked[ox][oy] = 1
#empty表示是否还存在待检测的点
empty = false
#算法的核心
loop do
#假定已经没有待检测的点了
empty = true
#对地图的每一个点进行判断
for i in 0...$game_map.width
for j in 0...$game_map.height
#如果点是待检测点
if checked[i][j] == 1
#把它标记为已经检测
checked[i][j] = 2
#判断这个点旁边四个点是否能通过该点到达且旁边的这些点是否未检测(依次为↓←↑→)
if $game_map.passable?(i, j + 1, 2, character) &&
checked[i][j + 1] == 0
#标记为待检测
checked[i][j + 1] = 1
#设置旁边的点的父节点为当前的点(经过当前的点才能到达旁边的点)
father[i][j + 1] = [i, j]
end
if $game_map.passable?(i - 1, j, 4, character) &&
checked[i - 1][j] == 0
checked[i - 1][j] = 1
father[i - 1][j] = [i, j]
end
if $game_map.passable?(i, j - 1, 8, character) &&
checked[i][j - 1] == 0
checked[i][j - 1] = 1
father[i][j - 1] = [i, j]
end
if $game_map.passable?(i + 1, j, 6, character) &&
checked[i + 1][j] == 0
checked[i + 1][j] = 1
father[i + 1][j] = [i, j]
end
end
#如果目的地是待检测的点,说明已经找到目的地了
if checked[tx][ty] == 1
#找到=_=
found = true
#跳出循环
break
end
end
end
#判断是否还存在待检测的点
for i in 0...$game_map.width
empty = false if checked[i].include?(1)
end
#如果不存在待检测的点就跳出循环
if found || empty
break
end
end
#如果没找到到达目的地的路径就退出
return if !found
#找到了的话就生成路径(2、4、6、8代表↓←→↑)
if found
x, y = tx, ty
while [x, y] != [ox, oy]
dx, dy = father[x][y][0] - x, father[x][y][1] - y
step = 2 if dy == -1
step = 4 if dx == 1
step = 8 if dy == 1
step = 6 if dx == -1
path << step
x, y = father[x][y][0], father[x][y][1]
end
path.reverse!
end
#将2、4、6、8翻译为移动角色的命令
list = []
path.each{|e|
code = 1 if e == 2
code = 2 if e == 4
code = 4 if e == 8
code = 3 if e == 6
list << RPG::MoveCommand.new(code)
}
list << RPG::MoveCommand.new(0)
skippable = false
repeat = false
commands = RPG::MoveRoute.new
commands.skippable = skippable
commands.repeat = repeat
commands.list = list
#如果角色不存在就退出否则就强制移动角色
if character == nil
return true
end
character.find_path = true
character.force_move_route(commands)
return true
end
end
复制代码
作者:
夕仔
时间:
2014-11-14 23:39
A寻路初探.rar
(73.63 KB, 下载次数: 50)
2014-11-14 23:38 上传
点击文件名下载附件
@myownroc 这是在6R找到的自动寻路 感觉效率不错,但我不会转换成地图,路障不知道怎么弄。。你的寻路用于100以内的地图效果非常不错,但是超过100很多就会卡掉了。。{:2_264:}
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1