|
发表于 2013-8-10 16:38:03
|
显示全部楼层
- maze={}
- maze.inpos={}
- maze.outpos={}
- maze.currpos={}
- maze.struct={}
- maze.exits={}
- on_mazemap=function (n,l,w,s)
- table.insert(maze.struct,w[1])
- local length=0
- for m, t in pairs (s) do
- length=length+s[m].length
- for am, at in pairs (s[m]) do
- if s[m].text == "★" then
- maze.currpos.y=table.getn(maze.struct)/2
- maze.currpos.x=length/4
- end
- if s[m].backcolour == 32768 then
- maze.inpos.y=table.getn(maze.struct)/2
- maze.inpos.x=length/4
- end
- if s[m].backcolour == 128 then
- maze.outpos.y=table.getn(maze.struct)/2
- maze.outpos.x=length/4
- end
- --world.Note ("s["..m.."]["..am.."] = "..s[m][am])
- end
- end
- end
- maze_exits=function()
- for i=1,table.getn(maze.struct)-1 do
- local y=math.ceil(i/2)
- if i%2 == 1 then
- maze.exits[y]={}
- for j=1,string.len(maze.struct[i])-4,4 do
- local x=math.ceil(j/4)
- maze.exits[y][x]={}
- if string.sub (maze.struct[i], j+2 ,j+3) == " " then
- maze.exits[y][x]["way"]=maze.exits[y][x]["way"] and maze.exits[y][x]["way"].."n" or "n"
- maze.exits[y-1][x]["way"]=maze.exits[y-1][x]["way"] and maze.exits[y-1][x]["way"].."s" or "s"
- end
- end
- end
- if i%2 == 0 then
- for j=1,string.len(maze.struct[i])-4,4 do
- local x=math.ceil(j/4)
- if string.sub (maze.struct[i], j+4 ,j+5) == " " then
- maze.exits[y][x]["way"]=maze.exits[y][x]["way"] and maze.exits[y][x]["way"].."e" or "e"
- maze.exits[y][x+1]["way"]=maze.exits[y][x+1]["way"] and maze.exits[y][x+1]["way"].."w" or "w"
- end
- end
- end
- end
- end
- maze_getpath=function()
- x,y=maze.inpos.x,maze.inpos.y
- mazepath=""
- while maze.exits[y][x].way do
- waydir=maze.exits[y][x].way
- if string.len(waydir) > 1 then
- if string.sub(waydir,1,1) == backdir then
- godir=string.sub(waydir,2,2)
- else
- godir=string.sub(waydir,1,1)
- end
- else
- godir=string.sub(waydir,1,1)
- end
- print(godir)
- currx,curry=x,y
- if godir == "s" then backdir="n";y=y+1 end
- if godir == "n" then backdir="s";y=y-1 end
- if godir == "e" then backdir="w";x=x+1 end
- if godir == "w" then backdir="e";x=x-1 end
- print(godir,curry,currx,maze.exits[y][x].way,y,x)
- if maze.exits[y][x].way then
- if string.len(waydir) == 1 then
- maze.exits[curry][currx]["way"]=nil
- maze.exits[y][x]["way"]=string.gsub(maze.exits[y][x]["way"],backdir, "")
- else
- maze.exits[y][x]["way"]=string.gsub(maze.exits[y][x]["way"],backdir, "")..backdir
- end
- if string.sub(mazepath,-1) == backdir then
- mazepath=string.sub(mazepath,1,-2)
- else
- mazepath=mazepath..godir
- maze.exits[y][x]["path"]=mazepath
- end
- end
- end
- end
- showstruct=function()
- for i,v in pairs(maze.struct) do
- print(maze.struct[i])
- end
- Note("入口:"..maze.inpos.y..","..maze.inpos.x.."出口:"..maze.outpos.y..","..maze.outpos.x.."当前:"..maze.currpos.y..","..maze.currpos.x)
- for i,v in pairs(maze.exits) do
- for ii,vv in pairs(maze.exits[i]) do
- print(i,ii,vv.path)
- end
- end
- end
- maze_update=function()
- addtri("maze_struct","^([┌│├└].+[┐│┤┘])$","maze","on_mazemap")
- end
- maze_update()
复制代码 这是我去年玩NT时候做的算迷宫路径的,貌似可以随便从某点到某点,具体记不清了,当时没接触深度广度算法,所以上面的算法乱七八糟的,你可以参考下 |
|