Sayomie550

Untitled

Aug 21st, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 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() -- Function to print the logging stats
  10.     local sapling = turtle.getItemCount(1) -- Gets the count of saplings in the Turtles inventory (Item slot 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) -- Counts the ammount of lumber in the turtles inventory (Slots 2-9)
  12.     term.clear() -- Clears the screen
  13.     term.setCursorPos(1,1) --Sets the cursor position to the start
  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() -- Prints the logging stats
  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 -- Counter for the ammount of wood harvested
  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 -- Adds one to the height counter so the turtle knows how far to go back down
  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
  51.         while height ~= 0 do
  52.             turtle.digDown() -- Mines under the turtle incase a tree grew around the turtle (Has happened to me -_-)
  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() -- Function to drop off the inventory of the turtle and stock up saplings
  100.     if not turtle.detectDown() then -- Checks to see if there is a block under the turtle (If not then the turtle will start the stocking procedure)
  101.         local slot = 1 -- Resets the Lumber drop off slot number
  102.        
  103.         while slot ~= 10 do -- Cycles through and checks slots 2-9 (The lumber slots)
  104.             data()
  105.             print("Dropping off Lumber")
  106.             while turtle.getItemSpace(slot) ~= 64 do -- Checks the current slot
  107.                 redstone.setOutput("back",true) -- Sends a redstone pulse to the retriever to collect the lumber in the turtle
  108.                 sleep(0.2)
  109.                 redstone.setOutput("back",false) -- Ends the redstone pulse
  110.                 sleep(0.2)
  111.             end
  112.            
  113.             slot = slot + 1 -- Counter for the inventory slots
  114.         end
  115.        
  116.         while turtle.getItemSpace(1) ~= 0 do -- Checks to see if the turtle needs more saplings
  117.             data()
  118.             print("Stocking Up On Saplings")
  119.             redstone.setOutput("left",true) -- Sends a pulse to the retriever to send the turtle a sapling
  120.             sleep(0.2)
  121.             redstone.setOutput("left",false) -- Ends the redstone pulse
  122.             sleep(0.2)
  123.         end
  124.        
  125.     end
  126. end
  127.  
  128. textutils.slowPrint("Forestry Turtle V1.0")
  129. textutils.slowPrint("By Sayomie555")
  130. sleep(2)
  131.  
  132. while true do -- Creates a loop to keep the turtle running (Will be replaced later to allow for a on off switch)
  133.  
  134.     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
  135.         turtle.forward()
  136.     end
  137.    
  138.     treeleft() -- Runs the treeleft function
  139.     treeright() -- Runs the treeright function
  140.     treefront() -- Runs the treefront function
  141.    
  142.     if turtle.detect() then -- Checks to see if there is a block infront of the turtle and if there is turns the turtle left
  143.         turtle.turnRight()
  144.     end
  145.    
  146.     treeleft()
  147.     treeright()
  148.     treefront()
  149.     stock()
  150. end
Advertisement
Add Comment
Please, Sign In to add comment