Whitemambaa

Testing

Jul 6th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.24 KB | None | 0 0
  1. --By xXxMoNkEyMaNxXx (Whitemambaa)
  2. --doz
  3.  
  4. local next=next
  5. local max=math.max
  6. local min=math.min
  7.  
  8. --Stolen from my vec.lua--
  9. local vec={}
  10. function vec.add(a,b)
  11.     local new={}
  12.     for d=1,max(#a,#b) do
  13.         new[d]=(a[d] or 0)+(b[d] or 0)
  14.     end
  15.     return new
  16. end
  17. function vec.sub(a,b)
  18.     local new={}
  19.     for d=1,max(#a,#b) do
  20.         new[d]=(a[d] or 0)-(b[d] or 0)
  21.     end
  22.     return new
  23. end
  24. function vec.tostring(a)
  25.     local new=tostring(a[1])
  26.     for d=2,#a do
  27.         new=new..", "..tostring(a[d])
  28.     end
  29.     return new
  30. end
  31. --------------------------
  32.  
  33. local start=1
  34. local finish
  35.  
  36. local nodes={{0,0,0}}
  37. local links={{}}--Adjacent nodes indexed by direction
  38. local jumps={0}--How far to the start
  39. local state={}--Exploring state of the node
  40. local known=0--Number of nodes that have no unexplored adjacent nodes
  41.  
  42. local pos=1
  43. local dir=1
  44.  
  45. local units={{0,0,1},{-1,0,0},{0,0,-1},{1,0,0}}
  46. local unitNames={"N","W","S","E"}
  47.  
  48. local function getDir(a,b)
  49.     local d=vec.sub(b,a)
  50.     if d[1]==1 then
  51.         return 4
  52.     elseif d[3]==1 then
  53.         return 1
  54.     elseif d[1]==-1 then
  55.         return 2
  56.     elseif d[3]==-1 then
  57.         return 3
  58.     end
  59. end
  60.  
  61. local function update(nodeId,dis_j)
  62.     jumps[nodeId]=min(jumps[nodeId],dis_j)
  63.     local dis_i=jumps[nodeId]+1
  64.     for _,i in next,links[nodeId] do
  65.         if jumps[i]>dis_i then
  66.             update(i,dis_i)
  67.         end
  68.     end
  69. end
  70.  
  71. local function register(node,parent)
  72.     local p=nodes[parent]
  73.     local dis_i=jumps[parent]+1
  74.     for i=1,#nodes do
  75.         local n=nodes[i]
  76.         if n[1]==node[1] and n[2]==node[2] and n[3]==node[3] then
  77.             links[i][getDir(n,p)]=parent
  78.             links[parent][getDir(p,n)]=i
  79.             update(i,dis_i)
  80.             return not state[i]
  81.         end
  82.     end
  83.     local i=#nodes+1
  84.     nodes[i]=node
  85.     jumps[i]=dis_i
  86.     links[i]={}
  87.     links[i][getDir(node,p)]=parent
  88.     links[parent][getDir(p,node)]=i
  89.     return true
  90. end
  91.  
  92. local function turn(turns)-- + is Left
  93.     while turns>0 do
  94.         if turtle.turnLeft() then
  95.             turns=turns-1
  96.             dir=dir%4+1
  97.         end
  98.     end
  99.     while turns<0 do
  100.         if turtle.turnRight() then
  101.             turns=turns+1
  102.             dir=(dir-2)%4+1
  103.         end
  104.     end
  105. end
  106.  
  107. local function f(d)
  108.     turn((d-dir+2)%4-2)
  109.     repeat until turtle.forward()
  110.     pos=links[pos][(d-1)%4+1]
  111. end
  112.  
  113. local function b(d)
  114.     turn((d-dir+2)%4-2)
  115.     repeat until turtle.back()
  116.     pos=links[pos][(d+1)%4+1]
  117. end
  118.  
  119. print'There is a finish... right?'
  120. local iteration=0
  121. 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.
  122.     if not state[pos] then
  123.         turn(1)
  124.         state[pos]=-1
  125.         if not turtle.detect() and register(vec.add(nodes[pos],units[dir]),pos) then--Could be logically simpler, but the program would register walls.
  126.             f(dir)
  127.         end
  128.     elseif state[pos]==-1 then
  129.         turn(-1)
  130.         state[pos]=0
  131.         if not turtle.detect() and register(vec.add(nodes[pos],units[dir]),pos) then
  132.             f(dir)
  133.         end
  134.     elseif state[pos]==0 then
  135.         turn(-1)
  136.         state[pos]=1
  137.         if not turtle.detect() and register(vec.add(nodes[pos],units[dir]),pos) then
  138.             f(dir)
  139.         end
  140.     elseif state[pos]==1 then
  141.         if not turtle.detectUp() then
  142.             if finish then
  143.                 if jumps[pos]<jumps[finish] then
  144.                     print'Hey, this finish is closer!'
  145.                     finish=pos
  146.                 else
  147.                     print'I found the... other finish'
  148.                 end
  149.             else
  150.                 finish=pos
  151.                 print'I found the finish!'
  152.             end
  153.         end
  154.         turn(1)
  155.         state[pos]=true
  156.         known=known+1
  157.         if pos~=start then
  158.             b(dir)
  159.         end
  160.     else
  161.         print'Uh, what am I doing here?'
  162.         print"Whatever, I'll throw some crazy error."
  163.         error"[SEVERE] YO MAMA's SO FAT THAT SHE ACQUIRED PRIORITY OVER ALL OTHER PROCESSES."
  164.     end
  165.     iteration=iteration+1
  166.     --print(iteration,vec.tostring(nodes[pos]),unitNames[dir])
  167. end
  168. if finish then
  169.     print'I now know the best path from here to every point. (Including the finish)'
  170. else
  171.     print"I know the best path to every REACHABLE point, but there's no finish you bastard!"
  172.     error'No finish'
  173. end
  174.  
  175. sleep(2)
  176. print'Time for a speed-run!'
  177. sleep(1.5)
  178. for i=3,1,-1 do
  179.     print(i)
  180.     sleep(1)
  181. end
  182. print'Go!'
  183.  
  184. print'Calculating optimal path...'
  185. local path={}
  186. do
  187.     local insert=table.insert
  188.     local c=finish
  189.     repeat
  190.         local b,v,n=math.huge
  191.         for d,i in next,links[c] do
  192.             if jumps[i]<b then
  193.                 b,v,n=jumps[i],(d+1)%4+1,i
  194.             end
  195.         end
  196.         insert(path,1,v)
  197.         c=n
  198.     until c==start
  199. end
  200. print'Done.  Lol, umad?'
  201.  
  202. for i=1,#path do
  203.     f(path[i])-- :D
  204. end
Advertisement
Add Comment
Please, Sign In to add comment