Sayomie550

Untitled

Aug 22nd, 2012
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.98 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()
  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 < 3 then
  117.             data()
  118.             print("Entering Stadyby Mode")
  119.             sleep(2)
  120.             standby = 3
  121.             seconds = 3
  122.         end
  123.        
  124.         if minutes < 2 then
  125.             local mins = "minute"
  126.         else
  127.             local mins = "minutes"
  128.         end
  129.        
  130.         if hours < 2 then
  131.             local hrs = "hour"
  132.         else
  133.             local hrs = "hours"
  134.         end
  135.        
  136.         if standby < 60 then
  137.             term.clear() -- Clears the screen
  138.             term.setCursorPos(1,1) --Sets the cursor position to the start
  139.             print("--------Standby Mode--------")
  140.             print("In standby Mode for")
  141.             print("Seconds: "..seconds)
  142.             standby = standby + 1
  143.             seconds = seconds + 1
  144.             sleep(1)
  145.         end
  146.        
  147.         if standby > 59 and standby < 3600 then
  148.             term.clear() -- Clears the screen
  149.             term.setCursorPos(1,1) --Sets the cursor position to the start
  150.             print("--------Standby Mode--------")
  151.             print("In standby Mode for")
  152.             print(""..mins": "..minutes)
  153.             print("Seconds: "..seconds)
  154.             standby = standby + 1
  155.             seconds = seconds + 1
  156.             sleep(1)
  157.         end
  158.        
  159.         if standby > 3599 then
  160.             term.clear() -- Clears the screen
  161.             term.setCursorPos(1,1) --Sets the cursor position to the start
  162.             print("--------Standby Mode--------")
  163.             print("In standby Mode for")
  164.             print(""..hrs": "..hours)
  165.             print(""..mins": "..minutes)
  166.             print("Seconds: "..seconds)
  167.             standby = standby + 1
  168.             seconds = seconds + 1
  169.             sleep(1)
  170.         end
  171.        
  172.         local tree = 0
  173.         local wood = 0
  174.     end
  175. end
  176.  
  177. function stock() -- Function to drop off the inventory of the turtle and stock up saplings
  178.     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)
  179.         local slot = 2 -- Resets the Lumber drop off slot number
  180.        
  181.         while slot ~= 10 do -- Cycles through and checks slots 2-9 (The lumber slots)
  182.             data()
  183.             print("Dropping off Lumber")
  184.             while turtle.getItemSpace(slot) ~= 64 do -- Checks the current slot
  185.                 redstone.setOutput("back",true) -- Sends a redstone pulse to the retriever to collect the lumber in the turtle
  186.                 sleep(0.2)
  187.                 redstone.setOutput("back",false) -- Ends the redstone pulse
  188.                 sleep(0.2)
  189.             end
  190.            
  191.             slot = slot + 1 -- Counter for the inventory slots
  192.         end
  193.        
  194.         while turtle.getItemSpace(1) ~= 0 do -- Checks to see if the turtle needs more saplings
  195.             data()
  196.             print("Stocking Up On Saplings")
  197.             redstone.setOutput("left",true) -- Sends a pulse to the retriever to send the turtle a sapling
  198.             sleep(0.2)
  199.             redstone.setOutput("left",false) -- Ends the redstone pulse
  200.             sleep(0.2)
  201.         end
  202.        
  203.         turtle.down() -- Sends the turtle down 1 block
  204.         data()
  205.         print("Checking for off switch")
  206.    
  207.         standby()
  208.    
  209.         turtle.up() -- Brings the turtle back up
  210.         data()
  211.         print("Patroling For Trees")
  212.     end
  213. end
  214.  
  215. while true do -- Creates a loop to keep the turtle running
  216.  
  217.     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
  218.         turtle.forward()
  219.         data()
  220.         print("Patroling For Trees")
  221.     end
  222.    
  223.     treeleft() -- Runs the treeleft function
  224.     treeright() -- Runs the treeright function
  225.     treefront() -- Runs the treefront function
  226.    
  227.     if turtle.detect() then -- Checks to see if there is a block infront of the turtle and if there is turns the turtle Right
  228.         turtle.turnRight()
  229.         data()
  230.         print("Turning")
  231.     end
  232.    
  233.     treeleft()
  234.     treeright()
  235.     treefront()
  236.     stock()
  237. end
Advertisement
Add Comment
Please, Sign In to add comment