Sayomie550

Untitled

Aug 21st, 2012
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. --[[
  2. Forestry Turtle 1.0
  3. By Sayomie555
  4. --]]
  5. local tree = 0
  6. local wood = 0
  7.  
  8. function data()
  9.     local sapling = turtle.getItemCount(1)
  10.     local lumber = turtle.getItemCount(2) + turtle.getItemCount(3) + turtle.getItemCount(4) + turtle.getItemCount(5) + turtle.getItemCount(6) + turtle.getItemCount(7) + turtle.getItemCount(8) + turtle.getItemCount(9)
  11.     term.clear()
  12.     term.setCursorPos(1,1)
  13.     print("Number Of Trees Choped: "..tree)
  14.     print("Ammount Of Wood Harvested: "..wood)
  15.     print("Number Of saplings In Stock: "..sapling)
  16.     print("Ammount of Lumber In Stock: "..lumber)
  17.     print("------------------------------------")
  18. end
  19.  
  20. function treelocated() -- Function to print that a tree has been found
  21.     tree = tree + 1 -- Adds one to the tree counter
  22.     data()
  23.     print("Tree Detected Starting Logging")
  24.     sleep(0.2)
  25. end
  26.  
  27. function choplant() -- function to chop down a tree and then replant it
  28.     data()
  29.     print("Logging")
  30.     turtle.dig() -- Chops the base of the tree
  31.     turtle.forward() -- Moves the turtle under the tree
  32.     wood = wood + 1
  33.  
  34.     while turtle.detectUp() do -- Mines upward so long as there is a block above the turtle
  35.         turtle.digUp()
  36.         turtle.up()
  37.         data()
  38.         print("Logging")
  39.         wood = wood + 1
  40.     end
  41.     wood = wood - 1
  42.    
  43.     data()
  44.     print("Returning to Ground")
  45.     while not turtle.detectUp() and not turtle.detectDown() do -- Moves the turtle back down until there is a block below the turtle
  46.         turtle.down()
  47.     end
  48.    
  49.     data()
  50.     print("Backing Up")
  51.     sleep(0.2)
  52.     turtle.back() -- Moves the turtle back
  53.    
  54.    
  55.     data()
  56.     print("Replanting Sapling")
  57.     sleep(0.2)
  58.     turtle.select(1) -- The turtle selects the first slot in its inventory (The sapling)
  59.     turtle.place(1) -- Places the item from the first slot in its inventory
  60.    
  61.     data()
  62.     print("Patroling For Trees")
  63. end
  64.  
  65. function treeleft() -- Function to detect if there is a tree to the left of the turtle
  66.     if redstone.getInput("left",true) then -- Checks to see if there is a positive redstone input to the left of the turtle (This is to check if there is a tree vs a sapling)
  67.         treelocated() -- Runs the treelocated function
  68.         turtle.turnLeft() -- Turns the turtle left to face the tree
  69.         choplant() -- Runs the Choplant Function
  70.         turtle.turnRight() -- Turns the turtle to the right
  71.     end
  72. end
  73.  
  74. function treeright() -- Same as treeleft but check for trees on the right
  75.     if redstone.getInput("right",true) then
  76.         treelocated()
  77.         turtle.turnRight()
  78.         choplant()
  79.         turtle.turnLeft()
  80.     end
  81. end
  82.  
  83. function treefront() -- Same as tree left but checks for trees infront
  84.     if redstone.getInput("front",true) then
  85.         treelocated()
  86.         choplant()
  87.     end
  88. end
  89.  
  90. function stock()
  91.     if not turtle.detectDown() then
  92.         local slot = 2
  93.        
  94.         while turtle.getItemSpace(1) ~= 0 do
  95.             data()
  96.             print("Stocking Up On Saplings")
  97.             redstone.setOutput("left",true)
  98.             sleep(0.2)
  99.             redstone.setOutput("left",false)
  100.             sleep(0.2)
  101.         end
  102.        
  103.         while slot ~= 10 do
  104.             data()
  105.             print("Dropping off Lumber")
  106.             while turtle.getItemSpace(slot) ~= 64 do
  107.                 redstone.setOutput("back",true)
  108.                 sleep(0.2)
  109.                 redstone.setOutput("back",false)
  110.                 sleep(0.2)
  111.             end
  112.            
  113.             slot = slot + 1
  114.         end
  115.     end
  116. end
  117.  
  118. while true do -- Creates a loop to keep the turtle running (Will be replaced later to allow for a on off switch)
  119.  
  120.     if not turtle.detect() then -- Checks to see if there is a block infront of the turtle and moves the turtle forward if there is not
  121.         turtle.forward()
  122.     end
  123.    
  124.     treeleft() -- Runs the treeleft function
  125.     treeright() -- Runs the treeright function
  126.     treefront() -- Runs the treefront function
  127.    
  128.     if turtle.detect() then -- Checks to see if there is a block infront of the turtle and if there is turns the turtle left
  129.         turtle.turnRight()
  130.     end
  131.    
  132.     treeleft()
  133.     treeright()
  134.     treefront()
  135.     stock()
  136. end
Advertisement
Add Comment
Please, Sign In to add comment