Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --By xXxMoNkEyMaNxXx (Whitemambaa)
- --doz
- local next=next
- local max=math.max
- local min=math.min
- --Stolen from my vec.lua--
- local vec={}
- function vec.add(a,b)
- local new={}
- for d=1,max(#a,#b) do
- new[d]=(a[d] or 0)+(b[d] or 0)
- end
- return new
- end
- function vec.sub(a,b)
- local new={}
- for d=1,max(#a,#b) do
- new[d]=(a[d] or 0)-(b[d] or 0)
- end
- return new
- end
- function vec.tostring(a)
- local new=tostring(a[1])
- for d=2,#a do
- new=new..", "..tostring(a[d])
- end
- return new
- end
- --------------------------
- local start=1
- local finish
- local nodes={{0,0,0}}
- local links={{}}--Adjacent nodes indexed by direction
- local jumps={0}--How far to the start
- local state={}--Exploring state of the node
- local known=0--Number of nodes that have no unexplored adjacent nodes
- local pos=1
- local dir=1
- local units={{0,0,1},{-1,0,0},{0,0,-1},{1,0,0}}
- local unitNames={"N","W","S","E"}
- local function getDir(a,b)
- local d=vec.sub(b,a)
- if d[1]==1 then
- return 4
- elseif d[3]==1 then
- return 1
- elseif d[1]==-1 then
- return 2
- elseif d[3]==-1 then
- return 3
- end
- end
- local function update(nodeId,dis_j)
- jumps[nodeId]=min(jumps[nodeId],dis_j)
- local dis_i=jumps[nodeId]+1
- for _,i in next,links[nodeId] do
- if jumps[i]>dis_i then
- update(i,dis_i)
- end
- end
- end
- local function register(node,parent)
- local p=nodes[parent]
- local dis_i=jumps[parent]+1
- for i=1,#nodes do
- local n=nodes[i]
- if n[1]==node[1] and n[2]==node[2] and n[3]==node[3] then
- links[i][getDir(n,p)]=parent
- links[parent][getDir(p,n)]=i
- update(i,dis_i)
- return not state[i]
- end
- end
- local i=#nodes+1
- nodes[i]=node
- jumps[i]=dis_i
- links[i]={}
- links[i][getDir(node,p)]=parent
- links[parent][getDir(p,node)]=i
- return true
- end
- local function turn(turns)-- + is Left
- while turns>0 do
- if turtle.turnLeft() then
- turns=turns-1
- dir=dir%4+1
- end
- end
- while turns<0 do
- if turtle.turnRight() then
- turns=turns+1
- dir=(dir-2)%4+1
- end
- end
- end
- local function f(d)
- turn((d-dir+2)%4-2)
- repeat until turtle.forward()
- pos=links[pos][(d-1)%4+1]
- end
- local function b(d)
- turn((d-dir+2)%4-2)
- repeat until turtle.back()
- pos=links[pos][(d+1)%4+1]
- end
- print'There is a finish... right?'
- local iteration=0
- while known<#nodes do--Even though known will not be filled in in ascending order, this will only be true when the whole maze has been explored.
- if not state[pos] then
- turn(1)
- state[pos]=-1
- if not turtle.detect() and register(vec.add(nodes[pos],units[dir]),pos) then--Could be logically simpler, but the program would register walls.
- f(dir)
- end
- elseif state[pos]==-1 then
- turn(-1)
- state[pos]=0
- if not turtle.detect() and register(vec.add(nodes[pos],units[dir]),pos) then
- f(dir)
- end
- elseif state[pos]==0 then
- turn(-1)
- state[pos]=1
- if not turtle.detect() and register(vec.add(nodes[pos],units[dir]),pos) then
- f(dir)
- end
- elseif state[pos]==1 then
- if not turtle.detectUp() then
- if finish then
- if jumps[pos]<jumps[finish] then
- print'Hey, this finish is closer!'
- finish=pos
- else
- print'I found the... other finish'
- end
- else
- finish=pos
- print'I found the finish!'
- end
- end
- turn(1)
- state[pos]=true
- known=known+1
- if pos~=start then
- b(dir)
- end
- else
- print'Uh, what am I doing here?'
- print"Whatever, I'll throw some crazy error."
- error"[SEVERE] YO MAMA's SO FAT THAT SHE ACQUIRED PRIORITY OVER ALL OTHER PROCESSES."
- end
- iteration=iteration+1
- --print(iteration,vec.tostring(nodes[pos]),unitNames[dir])
- end
- if finish then
- print'I now know the best path from here to every point. (Including the finish)'
- else
- print"I know the best path to every REACHABLE point, but there's no finish you bastard!"
- error'No finish'
- end
- sleep(2)
- print'Time for a speed-run!'
- sleep(1.5)
- for i=3,1,-1 do
- print(i)
- sleep(1)
- end
- print'Go!'
- print'Calculating optimal path...'
- local path={}
- do
- local insert=table.insert
- local c=finish
- repeat
- local b,v,n=math.huge
- for d,i in next,links[c] do
- if jumps[i]<b then
- b,v,n=jumps[i],(d+1)%4+1,i
- end
- end
- insert(path,1,v)
- c=n
- until c==start
- end
- print'Done. Lol, umad?'
- for i=1,#path do
- f(path[i])-- :D
- end
Advertisement
Add Comment
Please, Sign In to add comment