Sayomie550

Untitled

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