Advertisement
Chronno

Computercraft Tree Felling

May 2nd, 2013
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. -- Updated to work with Minecraft 1.6.4
  2. -- Slot 1: Saplings
  3. -- Slot 2: Bonemeal
  4. -- Usage: treeFelling <trees> <chest side>
  5. -- "Trees" is how many saplings to grow and cut down.  This is a numerical value.
  6. -- "Chest side" is the side a chest is to empty inventory into.  "Back", "Left", "Right", and "Bottom" are supported
  7. -- If no arguments are given, one tree will be grown and cut-down with the inventory staying in the turtle.
  8. -- The second argument is optional, but if used the first is required.
  9.  
  10.  
  11. local trees = {...}
  12. local progEnd = 0
  13.  
  14. function goUp()
  15.     while turtle.up() == false do
  16.         turtle.digUp()
  17.         turtle.attackUp()
  18.     end
  19. end
  20.  
  21. function goDown()
  22.     while turtle.down() == false do
  23.         turtle.digDown()
  24.         turtle.attackDown()
  25.     end
  26. end
  27.  
  28. function goForward()
  29.     while turtle.forward() == false do
  30.         turtle.dig()
  31.         turtle.attack()
  32.     end
  33. end
  34.  
  35. function goBack()
  36.     turtle.turnLeft()
  37.     turtle.turnLeft()
  38.     goForward()
  39.     turtle.turnLeft()
  40.     turtle.turnLeft()
  41. end
  42.  
  43. function plant()
  44.     turtle.select(1)
  45.     if turtle.getItemCount(1) == 0 then
  46.         print("Out of saplings, replace and press any key")
  47.         print("Press 'Q' to quit")
  48.         local event, param1 = os.pullEvent ("char")
  49.         if param1 == "q" then
  50.             progEnd = 1
  51.             return
  52.         end
  53.     end
  54.     turtle.place()
  55.     turtle.select(2)
  56.     if turtle.getItemCount(2) == 0 then
  57.         print("Out of bonemeal, replace and press any key")
  58.         print("Press 'Q' to quit")
  59.         local event, param1 = os.pullEvent ("char")
  60.         if param1 == "q" then
  61.             progEnd = 1
  62.             return
  63.         end
  64.     end
  65.     while turtle.place() == true do
  66.     end
  67.     turtle.select(3)
  68. end
  69.  
  70. function emptyInventory(dir)
  71.     for i=3,16 do
  72.         turtle.select(i)
  73.         if turtle.compareTo(1) == true then
  74.             turtle.transferTo(1)
  75.         elseif turtle.compareTo(2) == true then
  76.             turtle.transferTo(2)
  77.         end
  78.     end
  79.     if string.lower(dir) == "back" then
  80.         turtle.turnLeft()
  81.         turtle.turnLeft()
  82.         for i=3,16 do
  83.             turtle.select(i)
  84.             turtle.drop()
  85.         end
  86.         turtle.turnLeft()
  87.         turtle.turnLeft()
  88.     elseif string.lower(dir) == "bottom" then
  89.         for i=3,16 do
  90.             turtle.select(i)
  91.             turtle.dropDown()
  92.         end
  93.     elseif string.lower(dir) == "left" then
  94.         turtle.left()
  95.         for i=3,16 do
  96.             turtle.select(i)
  97.             turtle.drop()
  98.         end
  99.         turtle.right()
  100.     elseif string.lower(dir) == "right" then
  101.         turtle.right()
  102.         for i=3,16 do
  103.             turtle.select(i)
  104.             turtle.drop()
  105.         end
  106.         turtle.left()
  107.     end
  108. end
  109.  
  110.  
  111. if #trees < 1 then
  112.     trees[1] = 1
  113. elseif #trees >= 1 then
  114.     if not tonumber(trees[1]) then
  115.         local file = shell.getRunningProgram()
  116.         if string.find(file, "/") ~= null then
  117.             file = string.reverse(file)
  118.             file = string.reverse(string.sub(file, 1, string.find(file, "/") - 1))
  119.         end
  120.         print("Usage: " .. file .. " <number> <side>")
  121.         print("Number: amount of trees to be chopped")
  122.         print("Side: Side output chest is on (optional)")
  123.         return
  124.     end
  125. end
  126. for i=1,trees[1] do
  127.     local dist = 0
  128.     plant()
  129.     if progEnd == 1 then
  130.         print("User exit")
  131.         return
  132.     end
  133.     goForward()
  134.     while turtle.detectUp() == true do
  135.         goUp()
  136.         dist = dist + 1
  137.     end
  138.     for o=1,dist do
  139.         goDown()
  140.     end
  141.     goBack()
  142.     if not trees[2] then
  143.         trees[2] = "none"
  144.     end
  145.     emptyInventory(trees[2])
  146. end
  147. print("Program Complete")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement