Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Edited from BikerEleven's original code, which can be found here:
- --http://bit.ly/NYbb3u
- --meant to be used with two of my other scripts, found below:
- --http://pastebin.com/fe99rTPB
- --http://pastebin.com/54wJt5fV
- --fixed typo 'startLoging' to 'startLogging'
- --allows for more rednet control by broadcasting messages.
- --turtles now compare instead of detect during harvestTree()
- --turtles will not attempt to plant saplings if they run out
- --turtles are slightly more efficient
- --turtles are smarter
- --turtles work correctly with spruce and jungle trees
- --turtles now have an option to wait until saplings are full between cycles
- local broadcast = true
- local shouldOutput = false
- local width = 0
- local length = 0
- local itemsB = {}
- local itemsA = {}
- local function loadSettings()
- file = io.open( "loggerSettings", "r" )
- while true do
- line = file:read()
- if line == nil then break end
- if line == "true" then
- shouldOutput = true
- else
- shouldOutput = false
- end
- line = file:read()
- if line == "true" then
- shouldWait = true
- else
- shouldWait = false
- end
- line = file:read()
- if line == nil then break end
- width = tonumber(line)
- line = file:read()
- if line == nil then break end
- length = tonumber(line)
- file:close()
- return true
- end
- file:close()
- return false
- end
- local function createSettings()
- term.clear()
- term.setCursorPos( 1,1 )
- print( "Set the loggerSettings." )
- write( "Output Redstone on cycle: " )
- local str = string.lower( read() )
- if str == "1" or str == "yes" or str == "true" then
- shouldOutput = true
- else
- shouldOutput = false
- end
- write( "Wait for full saplings: " )
- local str = string.lower( read() )
- if str == "1" or str == "yes" or str == "true" then
- shouldWait = true
- else
- shouldWait = false
- end
- write( "Rows: " )
- width = tonumber(read())
- write( "Trees: " )
- length = tonumber(read())
- file = io.open("loggerSettings", "w")
- if file == nil then
- -- error? could be locked
- return
- end
- if shouldOutput then
- file:write( "true" )
- file:write( "\n" )
- else
- file:write( "false" )
- file:write( "\n" )
- end
- if shouldWait then
- file:write( "true" )
- file:write( "\n" )
- else
- file:write( "false" )
- file:write( "\n" )
- end
- file:write( width )
- file:write( "\n" )
- file:write( length )
- file:write( "\n" )
- file:close()
- term.clear()
- term.setCursorPos( 1,1 )
- print( "Ready to cycle" )
- end
- term.clear()
- term.setCursorPos( 1,1 )
- print( "Logger program starting..." )
- if fs.exists( "loggerSettings" ) then
- if not loadSettings() then
- createSettings()
- end
- term.clear()
- term.setCursorPos( 1,1 )
- print( "Ready to cycle" )
- else
- createSettings()
- end
- rednet.open( "right" )
- local function tryMove( direction )
- for i = 3, 9 do
- itemsB[i] = turtle.getItemCount(i)
- end
- if direction == "down" then
- while not turtle.down() do
- turtle.digDown()
- end
- end
- if direction == "forward" then
- while not turtle.forward() do
- turtle.dig()
- end
- end
- if direction == "up" then
- while not turtle.up() do
- turtle.digUp()
- end
- end
- end
- local function plantTree()
- turtle.select(1)
- saplingsCount = turtle.getItemCount(1)
- if saplingsCount == 0 and not outOfSaplings then
- outOfSaplings = true
- if broadcast then
- rednet.broadcast( "noLogging" )
- end
- end
- if not outOfSaplings then
- turtle.placeDown()
- end
- if saplingsCount == turtle.getItemCount(1) and not outOfSaplings then --must have not been able to place a sapling
- turtle.select(2)
- tryMove("down")
- if not turtle.compareDown() then
- turtle.digDown()
- turtle.placeDown()
- end
- tryMove("up")
- turtle.select(1)
- turtle.placeDown()
- end
- if turtle.getItemCount(1) == 0 then
- outOfSaplings = true
- if broadcast then
- rednet.broadcast( "noLogging" )
- end
- end
- end
- local function digSaplings()
- -- *****
- -- *****
- -- **^**
- -- *****
- -- *****
- turtle.select(1)
- tryMove( "forward" )
- tryMove( "forward" )
- turtle.turnLeft()
- -- **<**
- -- ** **
- -- **o**
- -- *****
- -- *****
- tryMove( "forward" )
- tryMove( "forward" )
- turtle.turnLeft()
- -- _ **
- -- ** **
- -- **o**
- -- *****
- -- *****
- tryMove( "forward" )
- tryMove( "forward" )
- tryMove( "forward" )
- tryMove( "forward" )
- turtle.turnLeft()
- -- **
- -- * **
- -- *o**
- -- ****
- -- >****
- tryMove( "forward" )
- tryMove( "forward" )
- tryMove( "forward" )
- tryMove( "forward" )
- turtle.turnLeft()
- -- **
- -- * **
- -- *o**
- -- ****
- -- ^
- tryMove( "forward" )
- tryMove( "forward" )
- tryMove( "forward" )
- tryMove( "forward" )
- turtle.turnLeft()
- -- *<
- -- * *
- -- *o*
- -- ***
- --
- tryMove( "forward" )
- turtle.turnLeft()
- -- _
- -- * *
- -- *o*
- -- ***
- --
- tryMove( "forward" )
- tryMove( "forward" )
- tryMove( "forward" )
- turtle.turnRight()
- --
- -- *
- -- *o
- -- **<
- --
- tryMove( "forward" )
- tryMove( "forward" )
- turtle.turnRight()
- --
- -- *
- -- *o
- -- ^
- --
- tryMove( "forward" )
- tryMove( "forward" )
- turtle.turnRight()
- --
- -- >
- -- o
- --
- --
- tryMove( "forward" )
- turtle.turnLeft()
- turtle.back()
- end
- local function harvestTree()
- steps = 2
- turtle.select(3)
- if turtle.detect() then
- turtle.dig()
- end
- while not turtle.forward() do
- turtle.dig()
- sleep(1)
- end
- tryMove( "up" )
- if turtle.detect() then digSaplings() end
- turtle.select(3)
- while turtle.compareUp() do
- tryMove( "up" )
- if turtle.detect() then digSaplings() end
- turtle.select(3)
- steps = steps + 1
- if steps == 10 then break end
- end
- while steps > 2 do
- tryMove( "down" )
- steps = steps - 1
- end
- if not outOfSaplings then
- plantTree()
- end
- end
- local function checkTree()
- if turtle.detect() then
- tryMove( "down" )
- harvestTree()
- else
- tryMove( "forward" )
- if not turtle.detectDown() then
- if not outOfSaplings then plantTree() end
- end
- end
- end
- local function harvest()
- if shouldWait then
- for i = 3, 9 do
- if turtle.getItemCount( i ) ~= 0 then
- turtle.select( i )
- turtle.drop()
- sleep(0.5)
- end
- end
- end
- if turtle.getItemCount(1) <= ( length * width ) then
- if broadcast then
- rednet.broadcast( "helpLogging" )
- end
- end
- turtle.select(1)
- outOfSaplings = false
- if turtle.getItemCount(1) == 0 then
- outOfSaplings = true
- if broadcast then
- rednet.broadcast( "noLogging" )
- end
- end
- turtle.select(1)
- tryMove( "up" )
- tryMove( "forward" )
- checkTree()
- bump = false
- for w = 1, width do
- for l = 1, length do
- if l ~= length then
- tryMove( "forward" )
- tryMove( "forward" )
- checkTree()
- end
- end
- if w ~= width then
- if not bump then
- turtle.turnLeft()
- tryMove( "forward" )
- tryMove( "forward" )
- else
- turtle.turnRight()
- tryMove( "forward" )
- tryMove( "forward" )
- end
- checkTree()
- if bump then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- bump = not bump
- end
- end
- -- Return
- if not bump then
- turtle.turnLeft()
- tryMove( "forward" )
- turtle.turnLeft()
- for i = 1, length do
- tryMove( "forward" )
- tryMove( "forward" )
- if i ~= length then tryMove( "forward" ) end
- end
- else
- tryMove( "forward" )
- tryMove( "forward" )
- end
- turtle.turnLeft()
- if width == 1 then
- tryMove( "forward" )
- else
- for i = 1, width - 1 do
- tryMove( "forward" )
- tryMove( "forward" )
- tryMove( "forward" )
- end
- if not bump then
- tryMove( "forward" )
- end
- end
- turtle.turnLeft()
- tryMove( "down" )
- --Drop off and resupply
- if shouldOutput then
- redstone.setOutput("right", true)
- redstone.setOutput("left", true)
- redstone.setOutput("bottom", true)
- output = true
- if shouldWait then
- wait = true
- else
- wait = false
- end
- s3 = false
- s4 = false
- s5 = false
- s6 = false
- s7 = false
- s8 = false
- s9 = false
- while output do
- for i = 3, 9 do
- if wait and turtle.getItemSpace( 1 ) == 0 then
- redstone.setOutput("right", false)
- redstone.setOutput("left", false)
- redstone.setOutput("bottom", false)
- wait = false
- end
- if turtle.getItemSpace( 3 ) == 64 then
- s3 = true
- end
- if turtle.getItemSpace( 4 ) == 64 then
- s4 = true
- end
- if turtle.getItemSpace( 5 ) == 64 then
- s5 = true
- end
- if turtle.getItemSpace( 6 ) == 64 then
- s6 = true
- end
- if turtle.getItemSpace( 7 ) == 64 then
- s7 = true
- end
- if turtle.getItemSpace( 8 ) == 64 then
- s8 = true
- end
- if turtle.getItemSpace( 9 ) == 64 then
- s9 = true
- end
- if turtle.getItemSpace( i ) ~= 64 then
- break
- elseif i==9 and turtle.getItemSpace( 9 ) == 64 and not wait then
- output = false
- end
- end
- sleep(0.5)
- if shouldWait then
- if s3 and turtle.getItemSpace( 3 ) ~= 64 then
- turtle.select( 3 )
- turtle.drop()
- end
- if s4 and turtle.getItemSpace( 4 ) ~= 64 then
- turtle.select( 4 )
- turtle.drop()
- end
- if s5 and turtle.getItemSpace( 5 ) ~= 64 then
- turtle.select( 5 )
- turtle.drop()
- end
- if s6 and turtle.getItemSpace( 6 ) ~= 64 then
- turtle.select( 6 )
- turtle.drop()
- end
- if s7 and turtle.getItemSpace( 7 ) ~= 64 then
- turtle.select( 7 )
- turtle.drop()
- end
- if s8 and turtle.getItemSpace( 8 ) ~= 64 then
- turtle.select( 8 )
- turtle.drop()
- end
- if s9 and turtle.getItemSpace( 9 ) ~= 64 then
- turtle.select( 9 )
- turtle.drop()
- end
- end
- end
- end
- if broadcast then
- rednet.broadcast("doneLogging")
- end
- term.clear()
- term.setCursorPos( 1,1 )
- print( "Ready to cycle" )
- end
- while true do
- event, p1, p2 = os.pullEvent()
- if event == "rednet_message" and p2 == "startLogging" and broadcast then
- print( "Rednet message received, harvest tarting." )
- sleep( 1 )
- shell.run( "clear" )
- print( "Logger" )
- harvest()
- end
- if event == "char" and p1 == "s" then
- createSettings()
- end
- if event == "char" and p1 == "r" then
- print("User override, harvest starting.")
- if broadcast then
- rednet.broadcast( "startLogging" )
- end
- harvest()
- end
- if event == "redstone" then
- print("Redstone current received, harvest starting.")
- sleep( 1 )
- shell.run( "clear" )
- print( "Logger" )
- if broadcast then
- rednet.broadcast( "startLogging" )
- end
- harvest()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement