Lion4ever

turtle cube

Sep 6th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. if turtle.getFuelLevel()==0 then
  2.     print("No fuel!")
  3.     return
  4. end
  5. local x,y,z,fx,fz=0,0,0,1,0
  6. local size=2
  7. local bottomLayer
  8. local isFull
  9. turtle.select(1)
  10. function face(tfx,tfz)
  11.     if tfx==-fz and tfz==fx then
  12.         rt()
  13.     end
  14.     while tfx~=fx or tfz~=fz do
  15.         lt()
  16.     end
  17.     return true
  18. end
  19. function goto(tx,ty,tz,txDir,tzDir)
  20.     while y<ty do
  21.         u()
  22.     end
  23.     while y>ty do
  24.         d()
  25.     end
  26.     if x<tx then
  27.         face(1,0)
  28.         repeat
  29.             f()
  30.         until x==tx
  31.     end
  32.     if x>tx then
  33.         face(-1,0)
  34.         repeat
  35.             f()
  36.         until x==tx
  37.     end
  38.     if z<tz then
  39.         face(0,1)
  40.         repeat
  41.             f()
  42.         until z==tz
  43.     end
  44.     if z>tz then
  45.         face(0,-1)
  46.         repeat
  47.             f()
  48.         until z==tz
  49.     end
  50.     if txDir and tzDir then
  51.         face(txDir,tzDir)
  52.     end
  53. end
  54. function c()
  55.     if turtle.getItemCount(16)>0 and not isFull then
  56.         local ox,oy,oz,ofx,ofz=x,y,z,fx,fz
  57.         isFull=true
  58.         if ox==0 and oz==0 then
  59.             f()
  60.         end
  61.         goto(0,0,0)
  62.         for e=1,16 do
  63.             turtle.select(e)
  64.             turtle.refuel()
  65.             turtle.dropDown()
  66.         end
  67.         turtle.select(1)
  68.         goto(ox,0,oz)
  69.         goto(ox,oy,oz,ofx,ofz)
  70.         isFull=false
  71.     end
  72. end
  73. function f()
  74.     if turtle.detect() and not turtle.dig() then
  75.         bottomLayer=y
  76.         return
  77.     end
  78.     c()
  79.     x=x+fx
  80.     z=z+fz
  81.     while not turtle.forward() do
  82.         turtle.dig()
  83.         sleep(0.5)
  84.     end
  85. end
  86. function u()
  87.     y=y+1
  88.     repeat
  89.         turtle.digUp()
  90.     until turtle.up()
  91. end
  92. function d()
  93.     if turtle.detectDown() and not turtle.digDown() then   
  94.         bottomLayer=y-1
  95.     end
  96.     c()
  97.     repeat
  98.     until turtle.down()
  99.     y=y-1
  100. end
  101. function lt()
  102.     fx,fz=fz,-fx
  103.     return turtle.turnLeft()
  104. end
  105. function rt()
  106.     fx,fz=-fz,fx
  107.     return turtle.turnRight()
  108. end
  109. function layer()
  110.     for i=0,size do
  111.         for j=1,size do
  112.             f()
  113.             if bottomLayer and y==bottomLayer then
  114.                 return
  115.             end
  116.         end
  117.         if i<size then
  118.             if i%2==0 then
  119.                 rt()
  120.                 f()
  121.                 rt()
  122.             else
  123.                 lt()
  124.                 f()
  125.                 lt()
  126.             end
  127.         end
  128.     end
  129. end
  130. while true do
  131.     goto(-size/2,size/2-1,-size/2,1,0)
  132.     layer()
  133.     for q=2,(bottomLayer and y-bottomLayer or size) do
  134.         d()
  135.         for w=1,4 do
  136.             lt()
  137.             for k=1,size do
  138.                 f()
  139.             end
  140.         end
  141.     end
  142.     if not bottomLayer then
  143.         lt()
  144.         lt()
  145.         d()
  146.     end
  147.     if not bottomLayer then --bc d() could create it
  148.         layer()
  149.     end
  150.     size=size+2
  151. end
Advertisement
Add Comment
Please, Sign In to add comment