Advertisement
hoblin

Logger

Nov 18th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.25 KB | None | 0 0
  1. --Edited from BikerEleven's original code, which can be found here:
  2. --http://bit.ly/NYbb3u
  3. --meant to be used with two of my other scripts, found below:
  4. --http://pastebin.com/fe99rTPB
  5. --http://pastebin.com/54wJt5fV
  6. --fixed typo 'startLoging' to 'startLogging'
  7. --allows for more rednet control by broadcasting messages.
  8. --turtles now compare instead of detect during harvestTree()
  9. --turtles will not attempt to plant saplings if they run out
  10. --turtles are slightly more efficient
  11. --turtles are smarter
  12. --turtles work correctly with spruce and jungle trees
  13. --turtles now have an option to wait until saplings are full between cycles
  14.  
  15. local broadcast = true
  16. local shouldOutput = false
  17. local width = 0
  18. local length = 0
  19. local itemsB = {}
  20. local itemsA = {}
  21. local function loadSettings()
  22.   file = io.open( "loggerSettings", "r" )
  23.   while true do
  24.     line = file:read()
  25.     if line == nil then break end
  26.     if line == "true" then
  27.       shouldOutput = true
  28.     else
  29.       shouldOutput = false
  30.     end
  31.     line = file:read()
  32.     if line == "true" then
  33.       shouldWait = true
  34.     else
  35.       shouldWait = false
  36.     end
  37.     line = file:read()
  38.     if line == nil then break end
  39.     width = tonumber(line)
  40.     line = file:read()
  41.     if line == nil then break end
  42.     length = tonumber(line)
  43.     file:close()
  44.     return true
  45.   end
  46.   file:close()
  47.   return false
  48. end
  49. local function createSettings()
  50.   term.clear()
  51.   term.setCursorPos( 1,1 )
  52.   print( "Set the loggerSettings." )
  53.   write( "Output Redstone on cycle: " )
  54.   local str = string.lower( read() )
  55.   if str == "1" or str == "yes" or str == "true" then
  56.     shouldOutput = true
  57.   else
  58.     shouldOutput = false
  59.   end
  60.   write( "Wait for full saplings: " )
  61.   local str = string.lower( read() )
  62.   if str == "1" or str == "yes" or str == "true" then
  63.     shouldWait = true
  64.   else
  65.     shouldWait = false
  66.   end
  67.   write( "Rows: " )
  68.   width = tonumber(read())
  69.   write( "Trees: " )
  70.   length = tonumber(read())
  71.   file = io.open("loggerSettings", "w")
  72.   if file == nil then
  73.     -- error? could be locked
  74.     return
  75.   end
  76.   if shouldOutput then
  77.     file:write( "true" )
  78.     file:write( "\n" )
  79.   else
  80.     file:write( "false" )
  81.     file:write( "\n" )
  82.   end
  83.   if shouldWait then
  84.     file:write( "true" )
  85.     file:write( "\n" )
  86.   else
  87.     file:write( "false" )
  88.     file:write( "\n" )
  89.   end
  90.   file:write( width )
  91.   file:write( "\n" )
  92.   file:write( length )
  93.   file:write( "\n" )
  94.   file:close()
  95.   term.clear()
  96.   term.setCursorPos( 1,1 )
  97.   print( "Ready to cycle" )
  98. end
  99. term.clear()
  100. term.setCursorPos( 1,1 )
  101. print( "Logger program starting..." )
  102. if fs.exists( "loggerSettings" ) then
  103.   if not loadSettings() then
  104.     createSettings()
  105.   end
  106.   term.clear()
  107.   term.setCursorPos( 1,1 )
  108.   print( "Ready to cycle" )
  109. else
  110.   createSettings()
  111. end
  112. rednet.open( "right" )
  113.  
  114. local function tryMove( direction )
  115.   for i = 3, 9 do
  116.     itemsB[i] = turtle.getItemCount(i)
  117.   end
  118.   if direction == "down" then
  119.     while not turtle.down() do
  120.       turtle.digDown()
  121.     end
  122.   end
  123.   if direction == "forward" then
  124.     while not turtle.forward() do
  125.       turtle.dig()
  126.     end
  127.   end
  128.   if direction == "up" then
  129.     while not turtle.up() do
  130.       turtle.digUp()
  131.     end
  132.   end
  133. end
  134.  
  135. local function plantTree()
  136.   turtle.select(1)
  137.   saplingsCount = turtle.getItemCount(1)
  138.   if saplingsCount == 0 and not outOfSaplings then
  139.     outOfSaplings = true
  140.     if broadcast then
  141.       rednet.broadcast( "noLogging" )
  142.     end
  143.   end
  144.   if not outOfSaplings then
  145.     turtle.placeDown()
  146.   end
  147.   if saplingsCount == turtle.getItemCount(1) and not outOfSaplings then --must have not been able to place a sapling
  148.     turtle.select(2)
  149.     tryMove("down")
  150.     if not turtle.compareDown() then
  151.       turtle.digDown()
  152.       turtle.placeDown()
  153.     end
  154.     tryMove("up")
  155.     turtle.select(1)
  156.     turtle.placeDown()
  157.   end
  158.   if turtle.getItemCount(1) == 0 then
  159.     outOfSaplings = true
  160.     if broadcast then
  161.       rednet.broadcast( "noLogging" )
  162.     end
  163.   end
  164. end
  165.  
  166. local function digSaplings()
  167.   -- *****
  168.   -- *****
  169.   -- **^**
  170.   -- *****
  171.   -- *****
  172.   turtle.select(1)
  173.   tryMove( "forward" )
  174.   tryMove( "forward" )
  175.   turtle.turnLeft()
  176.   -- **<**
  177.   -- ** **
  178.   -- **o**
  179.   -- *****
  180.   -- *****
  181.   tryMove( "forward" )
  182.   tryMove( "forward" )
  183.   turtle.turnLeft()
  184.   -- _  **
  185.   -- ** **
  186.   -- **o**
  187.   -- *****
  188.   -- *****
  189.   tryMove( "forward" )
  190.   tryMove( "forward" )
  191.   tryMove( "forward" )
  192.   tryMove( "forward" )
  193.   turtle.turnLeft()
  194.   --    **
  195.   --  * **
  196.   --  *o**
  197.   --  ****
  198.   -- >****
  199.   tryMove( "forward" )
  200.   tryMove( "forward" )
  201.   tryMove( "forward" )
  202.   tryMove( "forward" )
  203.   turtle.turnLeft()
  204.   --    **
  205.   --  * **
  206.   --  *o**
  207.   --  ****
  208.   --     ^
  209.   tryMove( "forward" )
  210.   tryMove( "forward" )
  211.   tryMove( "forward" )
  212.   tryMove( "forward" )
  213.   turtle.turnLeft()
  214.   --    *<
  215.   --  * *
  216.   --  *o*
  217.   --  ***
  218.   --      
  219.   tryMove( "forward" )
  220.   turtle.turnLeft()
  221.   --    _
  222.   --  * *
  223.   --  *o*
  224.   --  ***
  225.   --      
  226.   tryMove( "forward" )
  227.   tryMove( "forward" )
  228.   tryMove( "forward" )
  229.   turtle.turnRight()
  230.   --      
  231.   --  *  
  232.   --  *o  
  233.   --  **<
  234.   --      
  235.   tryMove( "forward" )
  236.   tryMove( "forward" )
  237.   turtle.turnRight()
  238.   --      
  239.   --  *  
  240.   --  *o  
  241.   --  ^  
  242.   --      
  243.   tryMove( "forward" )
  244.   tryMove( "forward" )
  245.   turtle.turnRight()
  246.   --      
  247.   --  >  
  248.   --   o  
  249.   --      
  250.   --      
  251.   tryMove( "forward" )
  252.   turtle.turnLeft()
  253.   turtle.back()
  254. end
  255.  
  256. local function harvestTree()
  257.   steps = 2
  258.   turtle.select(3)
  259.   if turtle.detect() then
  260.     turtle.dig()
  261.   end
  262.   while not turtle.forward() do
  263.     turtle.dig()
  264.     sleep(1)
  265.   end
  266.   tryMove( "up" )
  267.   if turtle.detect() then digSaplings() end
  268.   turtle.select(3)
  269.   while turtle.compareUp() do
  270.     tryMove( "up" )
  271.     if turtle.detect() then digSaplings() end
  272.     turtle.select(3)
  273.     steps = steps + 1
  274.     if steps == 10 then break end
  275.   end
  276.   while steps > 2 do
  277.     tryMove( "down" )
  278.     steps = steps - 1
  279.   end
  280.   if not outOfSaplings then
  281.     plantTree()
  282.   end
  283. end
  284.  
  285. local function checkTree()
  286.   if turtle.detect() then
  287.     tryMove( "down" )
  288.     harvestTree()
  289.   else
  290.     tryMove( "forward" )
  291.     if not turtle.detectDown() then
  292.       if not outOfSaplings then plantTree() end
  293.     end
  294.   end
  295. end
  296.  
  297. local function harvest()
  298.   if shouldWait then
  299.     for i = 3, 9 do
  300.       if turtle.getItemCount( i ) ~= 0 then
  301.         turtle.select( i )
  302.         turtle.drop()
  303.         sleep(0.5)
  304.       end
  305.     end
  306.   end
  307.   if turtle.getItemCount(1) <= ( length * width ) then
  308.     if broadcast then
  309.       rednet.broadcast( "helpLogging" )
  310.     end
  311.   end
  312.   turtle.select(1)
  313.   outOfSaplings = false
  314.   if turtle.getItemCount(1) == 0 then
  315.     outOfSaplings = true
  316.     if broadcast then
  317.       rednet.broadcast( "noLogging" )
  318.     end
  319.   end
  320.   turtle.select(1)
  321.   tryMove( "up" )
  322.   tryMove( "forward" )
  323.   checkTree()
  324.   bump = false
  325.   for w = 1, width do
  326.     for l = 1, length do
  327.       if l ~= length then
  328.         tryMove( "forward" )
  329.         tryMove( "forward" )
  330.         checkTree()
  331.       end
  332.     end
  333.     if w ~= width then
  334.       if not bump then
  335.         turtle.turnLeft()
  336.         tryMove( "forward" )
  337.         tryMove( "forward" )
  338.       else
  339.         turtle.turnRight()
  340.         tryMove( "forward" )
  341.         tryMove( "forward" )
  342.       end
  343.       checkTree()
  344.       if bump then
  345.         turtle.turnRight()
  346.       else
  347.         turtle.turnLeft()
  348.       end
  349.       bump = not bump
  350.     end
  351.   end
  352.   -- Return
  353.   if not bump then
  354.     turtle.turnLeft()
  355.     tryMove( "forward" )
  356.     turtle.turnLeft()
  357.     for i = 1, length do
  358.       tryMove( "forward" )
  359.       tryMove( "forward" )
  360.       if i ~= length then tryMove( "forward" ) end
  361.     end
  362.   else
  363.     tryMove( "forward" )
  364.     tryMove( "forward" )
  365.   end
  366.   turtle.turnLeft()
  367.   if width == 1 then
  368.     tryMove( "forward" )
  369.   else
  370.     for i = 1, width - 1 do
  371.       tryMove( "forward" )
  372.       tryMove( "forward" )
  373.       tryMove( "forward" )
  374.     end
  375.     if not bump then
  376.       tryMove( "forward" )
  377.     end
  378.   end
  379.   turtle.turnLeft()
  380.   tryMove( "down" )
  381.   --Drop off and resupply
  382.   if shouldOutput then
  383.     redstone.setOutput("right", true)
  384.     redstone.setOutput("left", true)
  385.     redstone.setOutput("bottom", true)
  386.     output = true
  387.     if shouldWait then
  388.       wait = true
  389.     else
  390.       wait = false
  391.     end
  392.     s3 = false
  393.     s4 = false
  394.     s5 = false
  395.     s6 = false
  396.     s7 = false
  397.     s8 = false
  398.     s9 = false
  399.     while output do
  400.       for i = 3, 9 do
  401.         if wait and turtle.getItemSpace( 1 ) == 0 then
  402.           redstone.setOutput("right", false)
  403.           redstone.setOutput("left", false)
  404.           redstone.setOutput("bottom", false)
  405.           wait = false
  406.         end
  407.         if turtle.getItemSpace( 3 ) == 64 then
  408.           s3 = true
  409.         end
  410.         if turtle.getItemSpace( 4 ) == 64 then
  411.           s4 = true
  412.         end
  413.         if turtle.getItemSpace( 5 ) == 64 then
  414.           s5 = true
  415.         end
  416.         if turtle.getItemSpace( 6 ) == 64 then
  417.           s6 = true
  418.         end
  419.         if turtle.getItemSpace( 7 ) == 64 then
  420.           s7 = true
  421.         end
  422.         if turtle.getItemSpace( 8 ) == 64 then
  423.           s8 = true
  424.         end
  425.         if turtle.getItemSpace( 9 ) == 64 then
  426.           s9 = true
  427.         end
  428.         if turtle.getItemSpace( i ) ~= 64 then
  429.           break
  430.         elseif i==9 and turtle.getItemSpace( 9 ) == 64 and not wait then
  431.           output = false
  432.         end
  433.       end
  434.       sleep(0.5)
  435.       if shouldWait then
  436.         if s3 and turtle.getItemSpace( 3 ) ~= 64 then
  437.           turtle.select( 3 )
  438.           turtle.drop()
  439.         end
  440.         if s4 and turtle.getItemSpace( 4 ) ~= 64 then
  441.           turtle.select( 4 )
  442.           turtle.drop()
  443.         end
  444.         if s5 and turtle.getItemSpace( 5 ) ~= 64 then
  445.           turtle.select( 5 )
  446.           turtle.drop()
  447.         end
  448.         if s6 and turtle.getItemSpace( 6 ) ~= 64 then
  449.           turtle.select( 6 )
  450.           turtle.drop()
  451.         end
  452.         if s7 and turtle.getItemSpace( 7 ) ~= 64 then
  453.           turtle.select( 7 )
  454.           turtle.drop()
  455.         end
  456.         if s8 and turtle.getItemSpace( 8 ) ~= 64 then
  457.           turtle.select( 8 )
  458.           turtle.drop()
  459.         end
  460.         if s9 and turtle.getItemSpace( 9 ) ~= 64 then
  461.           turtle.select( 9 )
  462.           turtle.drop()
  463.         end
  464.       end
  465.     end
  466.   end
  467.   if broadcast then
  468.     rednet.broadcast("doneLogging")
  469.   end
  470.   term.clear()
  471.   term.setCursorPos( 1,1 )
  472.   print( "Ready to cycle" )
  473. end
  474. while true do
  475.   event, p1, p2 = os.pullEvent()
  476.   if event == "rednet_message" and p2 == "startLogging" and broadcast then
  477.     print( "Rednet message received, harvest tarting." )
  478.     sleep( 1 )
  479.     shell.run( "clear" )
  480.     print( "Logger" )
  481.     harvest()
  482.   end
  483.   if event == "char" and p1 == "s" then
  484.     createSettings()
  485.   end
  486.   if event == "char" and p1 == "r" then
  487.     print("User override, harvest starting.")
  488.     if broadcast then
  489.       rednet.broadcast( "startLogging" )
  490.     end
  491.     harvest()
  492.   end
  493.   if event == "redstone" then
  494.     print("Redstone current received, harvest starting.")
  495.     sleep( 1 )
  496.     shell.run( "clear" )
  497.     print( "Logger" )
  498.     if broadcast then
  499.       rednet.broadcast( "startLogging" )
  500.     end
  501.     harvest()
  502.   end
  503. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement