Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tArgs = {...}
- if #tArgs ~= 1 then
- print("Usage: lumberjack <length>")
- return
- end
- length = tonumber(tArgs[1])
- if length < 3 then
- print("Length must be greater than 2")
- return
- end
- length = length - 1
- depth = 0
- xPos, zPos = 0, 0
- xDir, zDir = 0, 1
- function readProgress()
- if not fs.exists("/tmp/prog.sav") then
- return 1, 1
- end
- local file = fs.open("/tmp/prog.sav", "r")
- local i = tonumber(file.readLine())
- local j = tonumber(file.readLine())
- length = tonumber(file.readLine())
- depth = tonumber(file.readLine())
- xPos = tonumber(file.readLine())
- zPos = tonumber(file.readLine())
- xDir = tonumber(file.readLine())
- zDir = tonumber(file.readLine())
- file.close()
- return i, j
- end
- function writeProgress(i, j)
- if not fs.exists("/tmp/") then
- fs.makeDir("/tmp/")
- end
- local file = fs.open("/tmp/prog.sav", "w")
- file.writeLine(i)
- file.writeLine(j)
- file.writeLine(length)
- file.writeLine(depth)
- file.writeLine(xPos)
- file.writeLine(zPos)
- file.writeLine(xDir)
- file.writeLine(zDir)
- file.close()
- end
- function isItemLeaves(item)
- local i = string.find(item.name, ":") + 1
- local name = string.sub(item.name, i)
- return name == "leaves"
- end
- function isLeaves()
- local success, item = turtle.inspect()
- if success then
- return isItemLeaves(item)
- else
- return false
- end
- end
- function isLeavesUp()
- local success, item = turtle.inspectUp()
- if success then
- return isItemLeaves(item)
- else
- return false
- end
- end
- function isLeavesDown()
- local success, item = turtle.inspectDown()
- if success then
- return isItemLeaves(item)
- else
- return false
- end
- end
- function itemIsDirt(item)
- local i = string.find(item.name, ":") + 1
- local name = string.sub(item.name, i)
- return name == "dirt"
- end
- function isItemALog(item)
- local i = string.find(item.name, ":") + 1
- local name = string.sub(item.name, i)
- return name == "log"
- end
- function isLog()
- local success, item = turtle.inspect()
- if success then
- return isItemALog(item)
- else
- return false
- end
- end
- function isLogUp()
- local success, item = turtle.inspectUp()
- if success then
- return isItemALog(item)
- else
- return false
- end
- end
- function isLogDown()
- local success, item = turtle.inspectDown()
- if success then
- return isItemALog(item)
- else
- return false
- end
- end
- function forward()
- return turtle.forward()
- end
- function back()
- return turtle.back()
- end
- function up()
- local success = turtle.up()
- if success then
- depth = depth + 1
- end
- return success
- end
- function down()
- local success = turtle.down()
- if success then
- depth = depth - 1
- end
- return success
- end
- function turnLeft()
- xDir, zDir = -zDir, xDir
- return turtle.turnLeft()
- end
- function turnRight()
- xDir, zDir = zDir, -xDir
- return turtle.turnRight()
- end
- function toGround()
- while down() do end
- end
- function startFelling()
- while isLog() do
- turtle.digDown()
- down()
- end
- up()
- for i = 1, 16 do
- if itemIsDirt(turtle.getItemDetail(i)) then
- turtle.select(i)
- turtle.placeDown()
- break
- end
- end
- if isLog() then
- turtle.dig()
- forward()
- end
- while isLogUp() do
- turtle.digUp()
- up()
- end
- toGround()
- while isLogDown() do
- turtle.digDown()
- down()
- end
- back()
- end
- function tryForward()
- while not forward() do
- if isLog() then
- startFelling()
- elseif isLeaves() then
- turtle.dig()
- elseif turtle.detectUp() and not isLogUp() then
- turtle.digUp()
- up()
- elseif not turtle.detectUp() then
- up()
- elseif isLogUp() then
- back()
- turtle.digUp()
- up()
- turtle.startFelling()
- end
- end
- xPos = xPos + xDir
- zPos = zPos + zDir
- toGround()
- end
- function edgeTurnRight()
- turnRight()
- tryForward()
- toGround()
- turnRight()
- end
- function edgeTurnLeft()
- turnLeft()
- tryForward()
- toGround()
- turnLeft()
- end
- function ai()
- local startI, startJ = readProgress()
- for i = startI, length + 1 do
- for j = startJ, length do
- tryForward()
- writeProgress(i, j)
- end
- startJ = 1
- if i ~= length + 1 then
- if i % 2 == 0 then
- edgeTurnLeft()
- else
- edgeTurnRight()
- end
- end
- writeProgress(i, 1)
- end
- end
- function goto(x, y, z, xD, zD)
- while depth ~= y do
- if depth > y then
- if not down() then
- turtle.digDown()
- turtle.attackDown()
- end
- else
- if not up() then
- turtle.digUp()
- turtle.attackUp()
- end
- end
- end
- if xPos > x then
- while xDir ~= -1 do
- turnLeft()
- end
- while xPos > x do
- if forward() then
- xPos = xPos - 1
- else
- turtle.dig()
- turtle.attack()
- end
- end
- elseif xPos < x then
- while xDir ~= 1 do
- turnLeft()
- end
- while xPos > x do
- if forward() then
- xPos = xPos - 1
- else
- turtle.dig()
- turtle.attack()
- end
- end
- end
- if zPos > z then
- while zDir ~= -1 do
- turnLeft()
- end
- while zPos > z do
- if forward() then
- zPos = zPos - 1
- else
- turtle.dig()
- turtle.attack()
- end
- end
- elseif zPos < z then
- while zDir ~= 1 do
- turnLeft()
- end
- while zPos > z do
- if forward() then
- zPos = zPos - 1
- else
- turtle.dig()
- turtle.attack()
- end
- end
- end
- while zDir ~= zD or xDir ~= xD do
- turnLeft()
- end
- end
- function dumpInv()
- goto(0, 0, 0, 0, -1)
- for i = 16, 1, -1 do
- turtle.select(i)
- if turtle.getItemCount() ~= 0 then
- if itemIsDirt(turtle.getItemDetail()) then
- if i == 1 then
- if turtle.getItemCount() > 10 then
- turtle.drop(turtle.getItemCount() - 10)
- end
- else
- turtle.transferTo(1)
- end
- else
- turtle.drop()
- end
- end
- end
- end
- ai()
- fs.delete("/tmp/prog.sav")
- dumpInv()
- turtle.select(1)
- goto(0, 0, 0, 0, -1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement