Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 0 -> North
- -- 1 -> East
- -- 2 -> South
- -- 3 -> West
- -- start of digging tranche
- local tx = -6276;
- local ty = -10;
- local tz = 1380;
- local odir = 3;
- -- parking info (unload area)
- local unloadX, unloadY, unloadZ = -6276, -7, 1379;
- -- current info
- local tdir = 3;
- local lbtIndex = 1;
- local slbtIndex = 0; -- to not start the row over and over again.
- function saveData()
- local file = fs.open("save.txt", "w")
- file.writeLine(lbtIndex)
- file.writeLine(slbtIndex)
- file.close()
- end
- function checkData()
- if fs.exists("save.txt") then
- local file = fs.open("save.txt", "r")
- lbtIndex = tonumber(file.readLine())
- slbtIndex = tonumber(file.readLine())
- file.close()
- end
- validateCurrentDirection()
- end
- function checkInventoryFull()
- local filled = false
- for slot = 1, 16 do
- if turtle.getItemSpace(slot) == 0 then
- filled = true
- elseif turtle.getItemCount(slot) == 0 then
- return false
- end
- end
- return filled
- end
- function getCurrentLocation()
- local x, y, z = gps.locate()
- return x, y, z
- end
- function turnToDirection(newDir)
- while tdir ~= newDir do
- local rightDir = getRight(tdir)
- local leftDir = getLeft(tdir)
- if rightDir == newDir then
- turtle.turnRight()
- tdir = rightDir
- elseif leftDir == newDir then
- turtle.turnLeft()
- tdir = leftDir
- else
- turtle.turnRight()
- tdir = rightDir
- end
- end
- end
- local function moveUp()
- while not turtle.up() do
- safeMiningUp()
- end
- end
- local function moveDown()
- while not turtle.down() do
- safeMiningDown()
- end
- end
- local function moveForward()
- while not turtle.forward() do
- safeMiningForward()
- end
- end
- function validateCurrentDirection()
- local initialX, initialY, initialZ = gps.locate()
- moveForward()
- local newX, newY, newZ = gps.locate()
- if newX > initialX then
- tdir = 1 -- East
- elseif newX < initialX then
- tdir = 3 -- West
- elseif newZ > initialZ then
- tdir = 2 -- South
- elseif newZ < initialZ then
- tdir = 0 -- North
- end
- turtle.back()
- end
- function gotoLocation(targetX, targetY, targetZ)
- local currentX, currentY, currentZ = getCurrentLocation()
- while currentY < targetY do
- moveUp()
- currentY = currentY + 1
- end
- while currentY > targetY do
- moveDown()
- currentY = currentY - 1
- end
- if targetX > currentX then
- turnToDirection(1) -- East
- while currentX < targetX do
- moveForward()
- currentX = currentX + 1
- end
- elseif targetX < currentX then
- turnToDirection(3) -- West
- while currentX > targetX do
- moveForward()
- currentX = currentX - 1
- end
- end
- if targetZ > currentZ then
- turnToDirection(2) -- South
- while currentZ < targetZ do
- moveForward()
- currentZ = currentZ + 1
- end
- elseif targetZ < currentZ then
- turnToDirection(0) -- North
- while currentZ > targetZ do
- moveForward()
- currentZ = currentZ - 1
- end
- end
- end
- function safeMiningDown()
- while turtle.detectDown() do
- local success, data = turtle.inspectDown()
- if success and data.name == "computercraft:turtle_advanced" then
- os.sleep(1)
- else
- turtle.digDown()
- return
- end
- end
- end
- function safeMiningUp()
- while turtle.detectUp() do
- local success, data = turtle.inspectUp()
- if success and data.name == "computercraft:turtle_advanced" then
- os.sleep(1)
- else
- turtle.digUp()
- return
- end
- end
- end
- function safeMiningForward()
- while turtle.detect() do
- local success, data = turtle.inspect()
- if success and data.name == "computercraft:turtle_advanced" then
- os.sleep(1)
- else
- turtle.dig()
- return
- end
- end
- end
- function digAndBack()
- local initialX, initialY, initialZ = gps.locate()
- while true do
- if turtle.detectDown() and not turtle.digDown() then
- break
- end
- moveDown()
- end
- local currentX, currentY, currentZ = gps.locate()
- while currentY < initialY do
- moveUp()
- currentY = currentY + 1
- end
- end
- function moveForwardRetry()
- while not turtle.forward() do
- if not turtle.detectUp() then
- turtle.up()
- os.sleep(2)
- while not turtle.down() do
- os.sleep(1)
- end
- else
- turtle.down()
- os.sleep(2)
- while not turtle.up() do
- os.sleep(1)
- end
- end
- os.sleep(1)
- end
- end
- function turnToSouth()
- while tdir ~= 2 do
- turtle.turnRight()
- tdir = (tdir + 1) % 4
- end
- end
- function parkAndEmpty()
- turnToSouth()
- moveForwardRetry()
- for slot = 1, 16 do
- turtle.select(slot)
- turtle.dropDown()
- end
- moveForwardRetry()
- end
- function trancheDigging(startX, startZ, forward, toDir)
- validateCurrentDirection()
- gotoLocation(startX, ty, startZ)
- turnToDirection(toDir)
- saveData()
- while slbtIndex > 0 do
- moveForward()
- slbtIndex = slbtIndex - 1
- forward = forward - 1
- end
- while forward > 1 do
- digAndBack()
- moveForward()
- forward = forward - 1
- slbtIndex = slbtIndex + 1
- saveData()
- if checkInventoryFull() then
- gotoLocation(unloadX, unloadY, unloadZ)
- parkAndEmpty()
- os.reboot()
- return
- end
- end
- end
- function getRight(currentDir)
- return (currentDir + 1) % 4
- end
- function getLeft(currentDir)
- return (currentDir + 3) % 4
- end
- function calculateLine(startX, startZ, initialDir, rightTurns)
- if initialDir == 0 then
- startX = startX - 1
- elseif initialDir == 2 then
- startX = startX + 1
- elseif initialDir == 1 then
- startZ = startZ - 1
- elseif initialDir == 3 then
- startZ = startZ + 1
- end
- local lx, lz = startX, startZ;
- local x, z = startX, startZ;
- local dir = initialDir;
- local distance = 2;
- for turn = 1, rightTurns do
- lx = x
- lz = z
- if dir == 0 then -- North
- z = z - distance
- elseif dir == 1 then -- East
- x = x + distance
- elseif dir == 2 then -- South
- z = z + distance
- elseif dir == 3 then -- West
- x = x - distance
- end
- dir = getRight(dir)
- distance = distance + 2
- end
- return lx, lz, x, z
- end
- function calculateDirection(x1, z1, x2, z2)
- if x2 > x1 then
- return 1 -- East
- elseif x2 < x1 then
- return 3 -- West
- elseif z2 > z1 then
- return 2 -- South
- elseif z2 < z1 then
- return 0 -- North
- else
- return nil
- end
- end
- function calculateDistance(x1, z1, x2, z2)
- local dx = x2 - x1
- local dz = z2 - z1
- return math.sqrt(dx * dx + dz * dz)
- end
- function main()
- checkData()
- term.clear()
- term.setCursorPos(1,1)
- term.write("turtle #" .. os.getComputerID())
- term.setCursorPos(1,2)
- term.write("fuel: " .. turtle.getFuelLevel())
- while true do
- if turtle.getFuelLevel() < 2000 then
- gotoLocation(unloadX, unloadY, unloadZ)
- turtle.up()
- turtle.up()
- turtle.up()
- os.shutdown()
- end
- if checkInventoryFull() then
- gotoLocation(unloadX, unloadY, unloadZ)
- parkAndEmpty()
- end
- local startX, startZ, endX, endZ = calculateLine(tx, tz, odir, lbtIndex)
- trancheDigging(startX, startZ, calculateDistance(startX, startZ, endX, endZ), calculateDirection(startX, startZ, endX, endZ))
- slbtIndex = 0
- lbtIndex = lbtIndex + 1;
- saveData()
- end
- end
- -- 0 -> North
- -- 1 -> East
- -- 2 -> South
- -- 3 -> West
- main()
Advertisement
Comments
-
- Update : fixed turtles destroying other turtles
- Update : fixed invalid path resume
- Update : adding tranche resume at last "digDown"
-
- Update 4 : Fixed invalid path calculation + Fixed turtle blocking eachothers + simplified first init + Removed hard codded goto.
Add Comment
Please, Sign In to add comment
Advertisement