Advertisement
masa-

CC Turtle: Feller v0.3.1

Mar 11th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.71 KB | None | 0 0
  1. local args = { ... }
  2. local posX = 0
  3. local posZ = 0
  4. local posY = 0
  5. local replant = false
  6. local replant_delay = 60
  7. local maxloops = 1
  8.  
  9. if #args < 1 then
  10.     print("Usage: progname <diam. (1|2)> [loop count] [delay (s)]")
  11.     return false
  12. end
  13.  
  14. local diam = tonumber( args[1] )
  15.  
  16. if #args >= 2 then
  17.     maxloops = tonumber( args[2] )
  18.     replant = true
  19. end
  20.  
  21. if #args >= 3 then
  22.     replant_delay = tonumber( args[3] )
  23. end
  24.  
  25. if maxloops > 1 then
  26.     if turtle.getItemCount(15) == 0 or turtle.getItemCount(16) == 0 then
  27.         print("Error: need saplings and bonemeal in slots 15 (s) and 16 (bm)")
  28.         return false
  29.     end
  30. end
  31.  
  32.  
  33. function digOneUp()
  34.     turtle.digUp()
  35.     if turtle.up() == false then
  36.         print("Error: Could not move up")
  37.         return false
  38.     end
  39.     posY = posY + 1
  40.  
  41.     if diam > 1 then
  42.         turtle.dig()
  43.     end
  44.  
  45.     return true
  46. end
  47.  
  48.  
  49. function digOneDown()
  50.     turtle.digDown()
  51.     if turtle.down() == false then
  52.         print("Error: Could not move down")
  53.         return false
  54.     end
  55.     posY = posY - 1
  56.  
  57.     if diam > 1 then
  58.         turtle.dig()
  59.     end
  60.  
  61.     return true
  62. end
  63.  
  64.  
  65. function dumpInventory()
  66.     if turtle.detect() then
  67.         -- Now we should be where we started
  68.         for i=1,14 do
  69.             local items = 0
  70.             items = turtle.getItemCount(i)
  71.             turtle.select(i)
  72.  
  73.             -- Chest is full
  74.             if turtle.drop() == false and items > 0 then
  75.                 return false
  76.             end
  77.         end
  78.     end
  79.  
  80.     return true
  81. end
  82.  
  83. function feller()
  84.     turtle.select(1)
  85.  
  86.     if turtle.detect() == false then
  87.         print("Error: No block in front!")
  88.         return false
  89.     end
  90.  
  91.     -- Dig the first block ...
  92.     if turtle.dig() == false then
  93.         print("Error: Could not dig the first block!")
  94.         return false
  95.     end
  96.  
  97.     -- ... and move to position "inside" the tree area
  98.     if turtle.forward() == true then
  99.         posX = posX + 1
  100.     else
  101.         print("Error: Could not move to position!")
  102.         return false
  103.     end
  104.  
  105.     if diam >= 2 then
  106.         turtle.dig()
  107.     end
  108.  
  109.     -- Dig the first colum (1 or 2 blocks long)
  110.     while turtle.detectUp() == true do
  111.         if digOneUp() == false then
  112.             print("Error while digging and moving up")
  113.             return false
  114.         end
  115.     end
  116.  
  117.     -- If we are digging a 2x2 tree, move the the other column:
  118.     if diam == 2 then
  119.         turtle.turnRight()
  120.         turtle.dig() -- Dig just to be sure there are no leaves blocking our way
  121.         turtle.forward()
  122.         posZ = posZ + 1
  123.         turtle.turnLeft()
  124.         turtle.dig()
  125.  
  126.         -- Dig down until we are at the same level that we started
  127.         while posY > 0 do
  128.             if digOneDown() == false then
  129.                 print("Error while digging and moving down")
  130.                 return false
  131.             end
  132.         end
  133.  
  134.         -- Move back to the same position that we started in
  135.         -- First, move in the "Z"-direction
  136.         turtle.turnRight()
  137.         while posZ > 0 do
  138.             turtle.back()
  139.             posZ = posZ - 1
  140.         end
  141.  
  142.         turtle.turnRight()
  143.     elseif diam == 1 then
  144.         while posY > 0 do
  145.             if turtle.down() == false then
  146.                 print("Error while moving down")
  147.                 return false
  148.             end
  149.             posY = posY - 1
  150.         end
  151.         turtle.turnRight()
  152.         turtle.turnRight()
  153.     end
  154.  
  155.     -- Move back in the "X"-direction
  156.     if turtle.forward() == false then
  157.         print("Error while trying to move to the starting spot")
  158.         return false
  159.     end
  160.     posX = posX - 1
  161.  
  162.     dumpInventory()
  163.  
  164.     -- And finally, reorient
  165.     turtle.turnRight()
  166.     turtle.turnRight()
  167. end
  168.  
  169. function replantTree(bonemeal)
  170.     if bonemeal == nil then
  171.         bonemeal = false
  172.     end
  173.  
  174.     if diam == 1 then
  175.         turtle.select(15)
  176.         turtle.place()
  177.  
  178.         -- Use bonemeal to grow the tree?
  179.         if bonemeal == true then
  180.             turtle.select(16)
  181.             turtle.place()
  182.         end
  183.     elseif diam == 2 then
  184.         turtle.select(15)
  185.         turtle.forward()
  186.         turtle.forward()
  187.         turtle.turnRight()
  188.         turtle.place()
  189.         turtle.turnLeft()
  190.         turtle.back()
  191.         turtle.place()
  192.         turtle.turnRight()
  193.         turtle.place()
  194.         turtle.turnLeft()
  195.         turtle.back()
  196.         turtle.place()
  197.  
  198.         -- Use bonemeal to grow the tree?
  199.         if bonemeal == true then
  200.             turtle.select(16)
  201.             turtle.place()
  202.         end
  203.     end
  204. end
  205.  
  206.  
  207. function suckSaplings()
  208.     local down = false
  209.     turtle.select(15)
  210.     turtle.suckDown()
  211.  
  212.     -- If there is no block below, go down and suck the saplings
  213.     -- that the water currents have brought us.
  214.     if turtle.detectDown() == false then
  215.         down = true
  216.         turtle.down()
  217.     end
  218.  
  219.     turtle.turnRight()
  220.     turtle.suck()
  221.     turtle.turnLeft()
  222.     turtle.turnLeft()
  223.     turtle.suck()
  224.  
  225.     if down == true then
  226.         turtle.turnLeft()
  227.         turtle.suck()
  228.         turtle.turnRight()
  229.         turtle.up()
  230.     end
  231.  
  232.     turtle.turnRight()
  233. end
  234.  
  235.  
  236. for i = 1,maxloops do
  237.     feller()
  238.     if replant == true then
  239.         os.sleep(replant_delay)
  240.  
  241.         -- Suck the sapling that are on the ground
  242.         suckSaplings()
  243.  
  244.         -- Dump the inventory again
  245. --      turtle.turnRight()
  246. --      turtle.turnRight()
  247. --      dumpInventory()
  248. --      turtle.turnRight()
  249. --      turtle.turnRight()
  250.  
  251.         -- Replant the tree
  252.         if i < maxloops then
  253.             replantTree(true)
  254.         else
  255.             replantTree(false)
  256.         end
  257.     end
  258. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement