Advertisement
Zaflis

ccraft treecut

Jan 15th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.82 KB | None | 0 0
  1. -- Cutting starts by facing middle of the tree
  2.  
  3. -- Turtle facing rotations
  4. --   0
  5. -- 1   3
  6. --   2
  7.  
  8. x = 1
  9. y = 1
  10. z = 0
  11. rot = 0
  12.  
  13. function cut()
  14.   local fuelDelay = 260
  15.   -- rot 0:+Z, 1:+X, 2:-Z
  16.   local ix = 1
  17.   local oldix = ix
  18.   local sx, sz
  19.  
  20.   fuel(10000, 10000)
  21.   empty()
  22.  
  23.   -- Map the tree area
  24.   if not turtle.detect() then
  25.     print("Error, no tree in front")
  26.     return
  27.   else
  28.     -- Find Z depth of tree
  29.     repeat
  30.       turtle.dig()
  31.       if turtle.forward() then
  32.         z = z+1
  33.       end
  34.     until not turtle.detect()
  35.     sz = z
  36.     print("Size "..sz.." x "..sz)
  37.     if sz > 1 then
  38.       -- Go back to middle Z
  39.       for i = 1, sz/2 do
  40.         if turtle.back() then
  41.           z = z-1
  42.         end
  43.       end
  44.     end
  45.     turtle.turnRight()
  46.     -- rot = 3
  47.     -- Find X minimum
  48.     while turtle.detect() do
  49.       turtle.dig()
  50.       turtle.forward()
  51.     end
  52.     x = 1
  53.     if sz > 2 then
  54.       -- Go back to middle X
  55.       for i = 1, (sz-1)/2 do
  56.         if turtle.back() then
  57.           x = x+1
  58.         end
  59.       end
  60.     end
  61.     -- Dig to treetop (facing right)
  62.     repeat
  63.       turtle.digUp()
  64.       turtle.up()
  65.       y = y+1
  66.     until (not turtle.detectUp()) and (y > 2)
  67.     print("Height "..y)
  68.     -- rot = 3
  69.     -- Go to min X
  70.     while x > 1 do
  71.       turtle.dig()
  72.       if turtle.forward() then
  73.         x = x-1
  74.       end
  75.     end
  76.     turtle.turnRight()
  77.     -- rot = 2
  78.     -- Go to min Z
  79.     while z > 1 do
  80.       turtle.dig()
  81.       if turtle.forward() then
  82.         z = z-1
  83.       end
  84.     end
  85.     turtle.turnRight()
  86.     turtle.turnRight()
  87.     rot = 0
  88.   end
  89.   sx = sz
  90.   turtle.digDown()
  91.   turtle.down()
  92.   y = y-1
  93.  
  94.   -- Start cutting loop
  95.   sdelay = 0
  96.   while true do
  97.     if sdelay == 0 then sleep(1) end
  98.     sdelay = (sdelay+1) % 10
  99.    
  100.     oldix = ix
  101.     if rot == 0 then -- Going forward
  102.       if z < sz then
  103.         if dig() then
  104.           z = z+1
  105.         end
  106.       else -- Reached edge
  107.         if ix > 0 then
  108.           turtle.turnLeft()
  109.           rot = 1 -- Left
  110.         else
  111.           turtle.turnRight()
  112.           rot = 3 -- Right
  113.         end
  114.         if ((x >= sx) and (ix == 1)) or
  115.            ((x <= 1) and (ix == -1)) then
  116.           -- Reached horizontal plane corner
  117.           if y <= 1 then
  118.             -- Digging finished
  119.             break
  120.           else
  121.             -- Move down
  122.             for i=1,3 do
  123.               if y > 1 then
  124.                 if turtle.detectDown() then
  125.                   if turtle.digDown() then
  126.                     empty()
  127.                   else
  128.                     break
  129.                   end
  130.                 end
  131.                 y = y-1
  132.                 turtle.down()
  133.               end
  134.             end
  135.             ix = -ix
  136.           end
  137.         elseif sx>1 then
  138.           -- dig 1 to side to prepare for next column
  139.           if dig() then
  140.             x = x+ix
  141.           end
  142.         end
  143.         if oldix > 0 then
  144.           turtle.turnLeft()
  145.         else
  146.           turtle.turnRight()
  147.         end
  148.         rot = 2 -- backwards
  149.       end
  150.     elseif rot == 2 then -- Going backwards
  151.       if z > 1 then
  152.         if dig() then
  153.           z = z-1
  154.         end
  155.       else -- Reached edge
  156.         if ix > 0 then
  157.           turtle.turnRight()
  158.           rot = 1
  159.         else      
  160.           turtle.turnLeft()
  161.           rot = 3
  162.         end
  163.         if ((x >= sx) and (ix == 1)) or
  164.            ((x <= 1) and (ix == -1)) then
  165.           -- Reached horizontal plane corner
  166.           if y <= 1 then
  167.             -- Digging finished
  168.             break
  169.           else
  170.             -- Move down
  171.             for i=1,3 do
  172.               if y > 1 then
  173.                 if turtle.detectDown() then
  174.                   if turtle.digDown() then
  175.                     empty()
  176.                   else
  177.                     break
  178.                   end
  179.                 end
  180.                 y = y-1
  181.                 turtle.down()
  182.               end
  183.             end
  184.             ix = -ix
  185.           end
  186.         elseif sx>1 then
  187.           -- dig 1 to side to prepare for next column
  188.           if dig() then
  189.             x = x+ix
  190.           end
  191.         end
  192.         if oldix > 0 then
  193.           turtle.turnRight()
  194.         else      
  195.           turtle.turnLeft()
  196.         end
  197.         rot = 0 -- forwards
  198.       end
  199.     end
  200.     fuelDelay = fuelDelay-1
  201.     if fuelDelay < 0 then
  202.       fuelDelay = 260
  203.       if turtle.getFuelLevel() < 600 then
  204.         fuel(10000, 10000)
  205.       end
  206.     end
  207.   end
  208.  
  209.   -- Move back to starting position
  210.   local moves = 40
  211.   repeat
  212.     moves = moves-1
  213.   until moveStep(1+sx/2, 1, 0) or (moves < 0)
  214.  
  215.   -- Finally empty items to chest
  216.   empty()
  217.   print("Cutting trip finished!")
  218. end
  219.  
  220. function dig()
  221.   local ret
  222.   if turtle.detectUp then
  223.     turtle.digUp()
  224.   end
  225.   if (y > 1) and turtle.detectDown then
  226.     turtle.digDown()
  227.   end
  228.   if turtle.detect() then
  229.     turtle.dig()
  230.   end
  231.   ret = turtle.forward()
  232.   if turtle.detectUp then
  233.     turtle.digUp()
  234.   end
  235.   empty()
  236.   return ret
  237. end
  238.  
  239. function doEmpty()
  240.   print("Emptying inventory...")
  241.   while turtle.detectUp() do
  242.     if not turtle.digUp() then
  243.       break
  244.     end
  245.   end
  246.   turtle.select(16) -- Select ender chest for placing
  247.   if not turtle.placeUp() then
  248.     print("Error placing ender chest")
  249.     return
  250.   end  
  251.   for i=1,14 do
  252.     turtle.select(i)
  253.     turtle.dropUp()
  254.   end
  255.   turtle.select(16)
  256.   turtle.digUp()
  257.   sleep(1)
  258.   print("Emptying finished")
  259. end
  260.  
  261. function empty()
  262.   if (turtle.getItemCount(13) > 0) or (turtle.getItemCount(14) > 0) then
  263.     doEmpty()
  264.   end
  265. end
  266.  
  267. function fuel(minfuel, maxfuel)
  268.   empty() -- Make sure slot 14 is empty
  269.   write("Checking fuel...")
  270.   if turtle.getFuelLevel() > minfuel then
  271.     print("Fuel tank already full")
  272.     return
  273.   end
  274.   while turtle.detectUp() do
  275.     if not turtle.digUp() then
  276.       break
  277.     end
  278.   end
  279.   local chest = false
  280.   turtle.select(14)
  281.   while turtle.getFuelLevel() < maxfuel do
  282.     turtle.refuel()
  283.     if not chest then
  284.       turtle.select(15)
  285.       if not turtle.placeUp() then
  286.         print("Error placing ender chest")
  287.         break
  288.       end
  289.       chest = true
  290.       turtle.select(14)
  291.     end
  292.     turtle.suckUp()
  293.     print("Fuel "..turtle.getFuelLevel())
  294.   end
  295.   if chest then
  296.     turtle.select(15)
  297.     turtle.digUp()
  298.   end
  299.   print("Fuel tank filled")
  300. end
  301.  
  302. function turnTo(newRot)
  303.   function capRot(r)
  304.     if r < -0.5 then
  305.       r = r+4
  306.     elseif r > 3.5 then
  307.       r = r-4
  308.     end
  309.     return r
  310.   end
  311.  
  312.   if rot ~= newRot then
  313.     if capRot(rot-1) == newRot then
  314.       turtle.turnRight()
  315.     elseif capRot(rot+1) == newRot then
  316.       turtle.turnLeft()
  317.     else
  318.       turtle.turnRight()
  319.       turtle.turnRight()
  320.     end
  321.     rot = newRot
  322.   end  
  323. end
  324.  
  325. function moveStep(toX, toY, toZ)
  326.   toX = math.floor(toX)
  327.   toY = math.floor(toY)
  328.   toZ = math.floor(toZ)
  329.   local dx = math.abs(toX-x)
  330.   local dz = math.abs(toZ-z)
  331.   if (dx > dz+0.5) and (dx > 0.5) then
  332.     if toX > x then      
  333.       turnTo(1)
  334.       dx = 1
  335.     else
  336.       turnTo(3)
  337.       dx = -1
  338.     end
  339.     if turtle.detect() then
  340.       turtle.dig()
  341.     end
  342.     if turtle.forward() then
  343.       x = x+dx
  344.     end
  345.   elseif dz > 0.5 then
  346.     if toZ < z then
  347.       turnTo(2)
  348.       dz = -1
  349.     else
  350.       turnTo(0)
  351.       dz = 1
  352.     end
  353.     if turtle.detect() then
  354.       turtle.dig()
  355.     end
  356.     if turtle.forward() then
  357.       z = z+dz
  358.     end
  359.   else
  360.     -- In this script positive Y-axis goes upwards
  361.     if toY > y+0.5 then
  362.       if turtle.detectUp() then
  363.         turtle.digUp()
  364.       end
  365.       if turtle.up() then
  366.         y = y+1
  367.       end
  368.     elseif toY < y-0.5 then
  369.       if turtle.detectDown() then
  370.         turtle.digDown()
  371.       end
  372.       if turtle.down() then
  373.         y = y-1
  374.       end
  375.     end
  376.   end
  377.   return (x == toX) and (y == toY) and (z == toZ)
  378. end
  379.  
  380. cut()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement