Guest User

COMPUTERCRAFT 3D MAZE SOLVER

a guest
Jun 26th, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.80 KB | None | 0 0
  1. --[[
  2.     COMPUTERCRAFT 3D MAZE SOLVER
  3.     BY 0099
  4.     FOR KONLAB'S COMPETITION
  5. ]]
  6.  
  7. -- directions:
  8. -- 1. north
  9. -- 2. east
  10. -- 3. south
  11. -- 4. west
  12. -- 5. up
  13. -- 6. down
  14.  
  15. local opp = {
  16.     [1] = 3,
  17.     [2] = 4,
  18.     [3] = 1,
  19.     [4] = 2,
  20.     [5] = 6,
  21.     [6] = 5,
  22. }
  23.  
  24. local right = {
  25.     [1] = 2,
  26.     [2] = 3,
  27.     [3] = 4,
  28.     [4] = 1,
  29. }
  30.  
  31. local left = {
  32.     [1] = 4,
  33.     [2] = 1,
  34.     [3] = 2,
  35.     [4] = 3,
  36. }
  37.  
  38. function seqGen()
  39.     local f
  40.     f = function(n, l)
  41.         for i=1,n,1 do
  42.             if l == 1 then
  43.                 coroutine.yield(tostring(i))
  44.             else
  45.                 local c = coroutine.create(f)
  46.                 while true do
  47.                     local status, a = coroutine.resume(c, n, l-1)
  48.                     if status == false then
  49.                         error("Coroutine error (1) : "..a)
  50.                     end
  51.                     if a == "end" then
  52.                         break
  53.                     end
  54.                     coroutine.yield(tostring(i)..","..a)
  55.                 end
  56.             end
  57.         end
  58.         return "end"
  59.     end
  60.     local f2 = function()
  61.         for i=1,1000000,1 do
  62.             local c = coroutine.create(f)
  63.             while true do
  64.                 local status, a = coroutine.resume(c, i, i)
  65.                 if status == false then
  66.                     error("Coroutine error (2) : "..a)
  67.                 end
  68.                 if a == "end" then
  69.                     break
  70.                 end
  71.                 coroutine.yield(a)
  72.             end
  73.         end
  74.         return "end"
  75.     end
  76.     local c = coroutine.create(f2)
  77.     local f3 = function()
  78.         while true do
  79.             local status, a = coroutine.resume(c)
  80.             if status == false then
  81.                 error("Coroutine error (3) : "..a)
  82.             end
  83.             if a == "end" then
  84.                 error("This maze is impossible. I give up.")
  85.             end
  86.             local t = loadstring("return {"..a.."}")()
  87.             for i=1,#t,1 do
  88.                 coroutine.yield(t[i])
  89.             end
  90.         end
  91.     end
  92.     local c2 = coroutine.create(f3)
  93.     local f4 = function()
  94.         local status, a = coroutine.resume(c2)
  95.         if status == false then
  96.             error("Coroutine error (4) : "..a)
  97.         end
  98.         return a
  99.     end
  100.     return f4
  101. end
  102.  
  103. function scan()
  104.     local dirs = {}
  105.     local dirs2 = {}
  106.     local n = 0
  107.     if map[gx] and map[gx][gy] and map[gx][gy][gz] then
  108.         return unpack(map[gx][gy][gz])
  109.     end
  110.     for i=1,4,1 do
  111.         if turtle.detect() == false then
  112.             dirs[dir] = true
  113.             table.insert(dirs2, dir)
  114.             n = n + 1
  115.         end
  116.         turtle.turnRight()
  117.         dir = right[dir]
  118.     end
  119.     if turtle.detectUp() == false then
  120.         dirs[5] = true
  121.         table.insert(dirs2, 5)
  122.         n = n+1
  123.     end
  124.     if turtle.detectDown() == false then
  125.         dirs[6] = true
  126.         table.insert(dirs2, 6)
  127.         n = n+1
  128.     end
  129.     if not map[gx] then
  130.         map[gx] = {}
  131.     end
  132.     if not map[gx][gy] then
  133.         map[gx][gy] = {}
  134.     end
  135.     map[gx][gy][gz] = {dirs, dirs2, n}
  136.     return dirs, dirs2, n
  137. end
  138.  
  139. function goTo(d)
  140.     print("Going to "..tostring(d))
  141.     if d == 5 then
  142.         turtle.up()
  143.         gy = gy+1
  144.     elseif d == 6 then
  145.         turtle.down()
  146.         gy = gy-1
  147.     else
  148.         if d == right[dir] then
  149.             turtle.turnRight()
  150.         elseif d == left[dir] then
  151.             turtle.turnLeft()
  152.         elseif d == opp[dir] then
  153.             turtle.turnRight()
  154.             turtle.turnRight()
  155.         end
  156.         turtle.forward()
  157.         dir = d
  158.         if dir == 1 then
  159.             gz = gz-1
  160.         elseif dir == 2 then
  161.             gx = gx+1
  162.         elseif dir == 3 then
  163.             gz = gz+1
  164.         elseif dir == 4 then
  165.             gx = gx-1
  166.         end
  167.     end
  168.     cameFrom = opp[d]
  169. end
  170.  
  171. function main()
  172.     local gen = seqGen()
  173.     dir = 1
  174.     gx, gy, gz = 0, 0, 0
  175.     map = {}
  176.     while true do
  177.         print(string.format("x=%d, y=%d, z=%d", gx, gy, gz))
  178.         if turtle.getFuelLevel() == 0 then
  179.             turtle.refuel()
  180.         end
  181.         local dirs, dirs2, n = scan()
  182.         print(tostring(n).." directions detected")
  183.         print(unpack(dirs2))
  184.         if n == 1 then
  185.             for i in pairs(dirs) do
  186.                 goTo(i)
  187.             end
  188.         elseif n == 2 then
  189.             for i in pairs(dirs) do
  190.                 if i ~= cameFrom then
  191.                     goTo(i)
  192.                     break
  193.                 end
  194.             end
  195.         else   
  196.             local d = gen()
  197.             print("Sequence: "..tostring(d))
  198.             local j
  199.             for i, d2 in pairs(dirs2) do
  200.                 if d2 == cameFrom or cameFrom == nil then
  201.                     j = i
  202.                     break
  203.                 end
  204.             end
  205.             print("Old dir: "..tostring(dirs2[j]))
  206.             j = (j-1+d)%n+1
  207.             print("New dir: "..tostring(dirs2[j]))
  208.             goTo(dirs2[j])
  209.         end
  210.     end
  211. end
  212.  
  213. main()
Advertisement
Add Comment
Please, Sign In to add comment