Guest User

Untitled

a guest
Aug 24th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.51 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 standby() -- Function for when the turtle is to enter standby
  100.     local standby = 0
  101.     local seconds = 0
  102.     local minutes = 0
  103.     local hours = 0
  104.    
  105.     while not turtle.detectDown() do -- Checks to see if the turtle should continue on or go into standby
  106.         if seconds > 59 then -- Counter to change seconds into minutes
  107.             seconds = 0
  108.             minutes = minutes + 1
  109.         end
  110.        
  111.         if minutes > 59 then -- Counter to change minutes into hours
  112.             minutes = 0
  113.             hours = hours + 1
  114.         end
  115.        
  116.         if standby <= 0 then -- Prints a message saying the turtle is entering standby mode
  117.             data()
  118.             print("Entering Stadyby Mode")
  119.             sleep(2)
  120.             seconds = 2
  121.             standby = 1
  122.         else -- Prints a screen showing the user how long the turtle has been in standby mode
  123.             term.clear() -- Clears the screen
  124.             term.setCursorPos(1,1) --Sets the cursor position to the start
  125.             print("-----------Standby Mode-----------")
  126.             print("In standby Mode for")
  127.             print("Hours   : "..hours)
  128.             print("Minutes : "..minutes)
  129.             print("Seconds : "..seconds)
  130.             print("")
  131.             print("Trees Chopped Last Run  : "..tree)
  132.             print("Wood Collected Last Run : "..wood)
  133.             seconds = seconds + 1
  134.             sleep(1)
  135.         end
  136.     end
  137.    
  138.     if standby == 1 then -- Once the turtle is out of standby mode it resets the counters
  139.         tree = 0
  140.         wood = 0
  141.     end
  142. end
  143.  
  144. function stock() -- Function to drop off the inventory of the turtle and stock up saplings
  145.     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)
  146.         local slot = 2 -- Resets the Lumber drop off slot number
  147.        
  148.         while slot ~= 10 do -- Cycles through and checks slots 2-9 (The lumber slots)
  149.             data()
  150.             print("Dropping off Lumber")
  151.             while turtle.getItemSpace(slot) ~= 64 do -- Checks the current slot
  152.                 redstone.setOutput("back",true) -- Sends a redstone pulse to the retriever to collect the lumber in the turtle
  153.                 sleep(0.2)
  154.                 redstone.setOutput("back",false) -- Ends the redstone pulse
  155.                 sleep(0.2)
  156.             end
  157.            
  158.             slot = slot + 1 -- Counter for the inventory slots
  159.         end
  160.        
  161.         while turtle.getItemSpace(1) ~= 0 do -- Checks to see if the turtle needs more saplings
  162.             data()
  163.             print("Stocking Up On Saplings")
  164.             redstone.setOutput("left",true) -- Sends a pulse to the retriever to send the turtle a sapling
  165.             sleep(0.2)
  166.             redstone.setOutput("left",false) -- Ends the redstone pulse
  167.             sleep(0.2)
  168.         end
  169.        
  170.         turtle.down()
  171.         data()
  172.         print("Checking for off switch")
  173.    
  174.         standby() -- Checks to see if the turtle should go into standby mode
  175.        
  176.         turtle.up() -- Brings the turtle back up
  177.         data()
  178.         print("Patroling For Trees")
  179.     end
  180. end
  181.  
  182. while true do -- Creates a loop to keep the turtle running
  183.  
  184.     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
  185.         turtle.forward()
  186.         data()
  187.         print("Patroling For Trees")
  188.     end
  189.    
  190.     treeleft() -- Runs the treeleft function
  191.     treeright() -- Runs the treeright function
  192.     treefront() -- Runs the treefront function
  193.    
  194.     if turtle.detect() then -- Checks to see if there is a block infront of the turtle and if there is turns the turtle Right
  195.         turtle.turnRight()
  196.         data()
  197.         print("Turning")
  198.     end
  199.    
  200.     treeleft()
  201.     treeright()
  202.     treefront()
  203.     stock()
  204. end
Advertisement
Add Comment
Please, Sign In to add comment