Sayomie550

Untitled

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