Advertisement
VADemon

Treehugger turtle 2.16 | 2*2 tree

Apr 25th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. --version: 2.16
  2. saplingSlot = 1
  3. minimumSaplings = 4
  4. minimumFuelLevel = 128
  5. compareSlot = 16 --put there a block to compare with
  6. compareBlockDir = "compareDown" --where the same block is placed // "compareDown" or "compareUp"
  7. chestDir = "dropUp"
  8. plantSaplings = true
  9.  
  10. --DO NOT TOUCH THE CODE BELOW
  11. local args = { ... }
  12. local height = 0
  13. local work = true
  14.  
  15. function drop_all_items()
  16.     for i = 2, 14 do
  17.         if turtle.getItemCount(i)~=0 then
  18.             turtle.select(i)
  19.             turtle[chestDir]()
  20.         end
  21.     end
  22.    
  23.     turtle.select(saplingSlot)
  24. end
  25.  
  26. function checkSaplings()
  27.     if turtle.getItemCount(saplingSlot)>=minimumSaplings then
  28.         return true
  29.     end
  30.     print("Not enough saplings!")
  31.     return false
  32. end
  33.  
  34. function plant2x2()
  35.     if plantSaplings then
  36.         turtle.place()
  37.  
  38.         turtle.turnRight()
  39.         turtle.back()
  40.         turtle.place()
  41.  
  42.         turtle.turnLeft()
  43.         turtle.place()
  44.         turtle.back()
  45.  
  46.         turtle.place()
  47.     end
  48. end
  49.  
  50. function cutUp()
  51.     turtle.digUp()
  52.     turtle.up()
  53. end
  54.  
  55. function cutDown()
  56.     turtle.digDown()
  57.     turtle.down()
  58. end
  59.  
  60. function run()
  61.     height = 0
  62.     turtle.select( saplingSlot )
  63.    
  64.     -- chop down the first block
  65.     turtle.dig()
  66.     turtle.forward() -- ]]
  67.  
  68.     while turtle.digUp()==true do
  69.         turtle.dig()
  70.         turtle.up()
  71.         height = height + 1 --count height of the tree
  72.     end
  73.  
  74.     turtle.turnRight(); turtle.dig(); turtle.forward(); turtle.turnLeft() --turn on the top
  75.  
  76.     for i=1, height do
  77.         cutDown()
  78.         turtle.dig()
  79.     end
  80.  
  81.     --print("End of tree or no blocks found. End of while")
  82.     --Only plants from
  83.     -- ##
  84.     -- #@
  85.     plant2x2()
  86.    
  87.     drop_all_items()
  88.     print("Jobs done! Current height: " ..height)
  89. end
  90.  
  91. function continueDown()
  92.     turtle.select( compareSlot )
  93.     while turtle.compareDown()==false do cutDown(); turtle.dig() end
  94.     print("detectDown==false //01")
  95.     turtle.select( saplingSlot )
  96.     plant2x2(); drop_all_items()
  97. end
  98.  
  99. function continueUp()
  100.     height = 0
  101.     if turtle.detect() then turtle.dig() end
  102.         while turtle.digUp()==true do
  103.             turtle.dig()
  104.             turtle.up()
  105.             height = height + 1 --count height of the tree
  106.         end
  107.         turtle.turnRight(); turtle.dig(); turtle.forward(); turtle.turnLeft() --turn on the top
  108.         for i=1, height do
  109.             cutDown()
  110.             turtle.dig()
  111.         end
  112.         print("Sucessfully worked with given height, continueDown")
  113.         continueDown()
  114. end
  115.  
  116. function detectDirection()
  117.     turtle.select(compareSlot)
  118.     if turtle[ compareBlockDir ]() then --everythings fine, we're on the floor
  119.         print("Dir: I am on the floor")
  120.         turtle.select( saplingSlot ) --bug fixed in 2.15
  121.         return true
  122.     end
  123.     turtle.select(saplingSlot)
  124.     if turtle.detectUp() then
  125.         if turtle.detect() then turtle.dig() end
  126.         continueUp()
  127.            
  128.     elseif turtle.detectDown() then
  129.         if turtle.detect() then turtle.dig() end
  130.         continueDown()
  131.     else
  132.         if turtle.up() and turtle.detectUp() then --if there's a 1 block space between tree and turtle
  133.             continueUp()
  134.         elseif turtle.down() and turtle.down() and turtle.detectDown() then
  135.             continueDown()
  136.         else
  137.             print("Can't find the tree or I ran out of fuel")
  138.             print("Fuel level: "..turtle.getFuelLevel())
  139.             shell.exit()
  140.         end
  141.     end
  142.     turtle.select(saplingSlot)
  143. end
  144.  
  145.  
  146.  
  147. local function work()
  148.     while os.pullEvent("work") do
  149.         if turtle.compare()==true then --sleep if it's the sapling
  150.             --mySleep() --15sec
  151.         else
  152.             if checkSaplings() then
  153.                 run()
  154.             else
  155.                 work = false
  156.             end
  157.             --mySleep() --60sec
  158.         end
  159.     end
  160. end
  161.  
  162. local function keyInput()
  163.     sleep(.1) os.queueEvent("start")
  164.     while true do
  165.         local event, char = os.pullEvent("char")
  166.         if char == "s" then
  167.             print("Stopped working!")
  168.             work = false
  169.         elseif char == "g" then -- go
  170.             print("Beginning to work!")
  171.             work = true
  172.             os.queueEvent("start")
  173.             os.queueEvent("work")
  174.         elseif char == "p" then
  175.             print("Getting current direction...")
  176.             detectDirection()
  177.         elseif char == "l" then
  178.             print("Exitting...")
  179.             break
  180.         end
  181.     end
  182. end
  183.  
  184. local function mySleepTimer()
  185.     while os.pullEvent("start") do
  186.         while work do
  187.             os.queueEvent("work")
  188.             sleep(15)
  189.         end
  190.     end
  191. end
  192.  
  193. if turtle.getFuelLevel()<minimumFuelLevel then
  194.     print("I don't have fuel!")
  195.     shell.exit()
  196. end
  197.  
  198. print("Hello!")
  199. print("Keys avaible:\n")
  200. print("S - stop working\nG - Go & Work\nP - get current position and work\nL - exit")
  201.  
  202. if #args==1 then
  203.     if turtle.getItemCount(compareSlot)~=0 and checkSaplings() then
  204.         print("Continuing my job!")
  205.         detectDirection()
  206.         print("dDir end")
  207.     else print("Setup me properly first!"); error()
  208.     end
  209. end
  210.  
  211. turtle.select(saplingSlot)
  212.  
  213. parallel.waitForAny(work, keyInput, mySleepTimer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement