Advertisement
masa-

CC Turtle: Feller v0.3

Feb 20th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.39 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: programname <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 digOneStripUp()
  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 digOneStripDown()
  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.             res = turtle.drop()
  73.  
  74.             -- Chest is full
  75.             if res == false and items > 0 then
  76.                 return false
  77.             end
  78.         end
  79.     end
  80.  
  81.     return true
  82. end
  83.  
  84. function feller()
  85.     turtle.select(1)
  86.  
  87.     if turtle.detect() == false then
  88.         print("Error: No block in front!")
  89.         return
  90.     end
  91.  
  92.     -- Dig the first block ...
  93.     if turtle.dig() == false then
  94.         print("Error: Could not dig the first block!")
  95.         return
  96.     end
  97.  
  98.     -- ... and move to position "inside" the tree area
  99.     if turtle.forward() == true then
  100.         posX = posX + 1
  101.     else
  102.         print("Error: Could not move to position!")
  103.         return
  104.     end
  105.  
  106.     if diam == 2 then
  107.         turtle.dig()
  108.     end
  109.  
  110.     -- Dig the first colum (1 or 2 blocks wide)
  111.     while turtle.detectUp() == true do
  112.         if digOneStripUp() == false then
  113.             print("Error while digging and moving up")
  114.             return false
  115.         end
  116.     end
  117.  
  118.     -- If we are digging a 2x2 tree, move the the other column:
  119.     if diam == 2 then
  120.         turtle.turnRight()
  121.         turtle.dig() -- Dig just to be sure there are no leaves blocking our way
  122.         turtle.forward()
  123.         posZ = posZ + 1
  124.         turtle.turnLeft()
  125.         turtle.dig()
  126.  
  127.         -- Dig down until we are at the same level that we started
  128.         while posY > 0 do
  129.             if digOneStripDown() == false then
  130.                 print("Error while digging and moving down")
  131.                 return false
  132.             end
  133.         end
  134.  
  135.         -- Move back to the same position that we started in
  136.         -- First, move in the "Z"-direction
  137.         turtle.turnLeft()
  138.         while posZ > 0 do
  139.             turtle.forward()
  140.             posZ = posZ - 1
  141.         end
  142.  
  143.         turtle.turnLeft()
  144.     elseif diam == 1 then
  145.         while posY > 0 do
  146.             if turtle.down() == false then
  147.                 print("Error while moving down")
  148.                 return false
  149.             end
  150.             posY = posY - 1
  151.         end
  152.         turtle.turnRight()
  153.         turtle.turnRight()
  154.     end
  155.  
  156.     -- Move back in the "X"-direction
  157.     if turtle.forward() == false then
  158.         print("Error while trying to move to the starting spot")
  159.         return false
  160.     end
  161.     posX = posX - 1
  162.  
  163.     dumpInventory()
  164.  
  165.     -- And finally, reorient
  166.     turtle.turnRight()
  167.     turtle.turnRight()
  168.     suckSaplings()
  169. end
  170.  
  171. function doReplant()
  172.     if diam == 1 then
  173.         turtle.select(15)
  174.         turtle.place()
  175.         turtle.select(16)
  176.         turtle.place()
  177.     elseif diam == 2 then
  178.         turtle.select(15)
  179.         turtle.forward()
  180.         turtle.forward()
  181.         turtle.turnRight()
  182.         turtle.place()
  183.         turtle.turnLeft()
  184.         turtle.back()
  185.         turtle.place()
  186.         turtle.turnRight()
  187.         turtle.place()
  188.         turtle.turnLeft()
  189.         turtle.back()
  190.         turtle.place()
  191.         turtle.select(16)
  192.         turtle.place()
  193.     end
  194. end
  195.  
  196.  
  197. function suckSaplings()
  198.     local down = false
  199.     turtle.select(15)
  200.     turtle.suckDown()
  201.  
  202.     -- If there is no block below, go down and suck the saplings
  203.     -- that the water currents have brought us.
  204.     if turtle.detectDown() == false then
  205.         down = true
  206.         turtle.down()
  207.     end
  208.  
  209.     turtle.turnRight()
  210.     turtle.suck()
  211.     turtle.turnLeft()
  212.     turtle.turnLeft()
  213.     turtle.suck()
  214.  
  215.     if down == true then
  216.         turtle.turnLeft()
  217.         turtle.suck()
  218.         turtle.turnRight()
  219.         turtle.up()
  220.     end
  221.  
  222.     turtle.turnRight()
  223. end
  224.  
  225.  
  226. for i = 1,maxloops do
  227.     feller()
  228.     if replant == true then
  229.         os.sleep(replant_delay)
  230.         suckSaplings()
  231.         turtle.turnRight()
  232.         turtle.turnRight()
  233.         dumpInventory()
  234.         turtle.turnRight()
  235.         turtle.turnRight()
  236.  
  237.         if i < maxloops then
  238.             doReplant()
  239.         end
  240.     end
  241. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement