Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --pastebin get Bct0mdbv Dig
- --pastebin get SpEd82Jp StripLayer
- local tArgs = { ... }
- local filename = shell.getRunningProgram()
- if #tArgs < 2 then
- print( "Usage: " .. filename .. " <type> <length> <width> <height> <ender chest (0:1)>" )
- print( "Type: Room, Quarry, Branch, or Tunnel" )
- print( "Slot 1 should be a non-wood floor block" )
- print( "Slot 2 should be a fuel" )
- print( "Slot 3 should be a stack of chests" )
- print( "Slot 4 should be a stack of torches" )
- return
- end
- -- Mine in a quarry pattern until we hit something we can't dig
- local digType = tArgs[1]
- local inLength = tonumber( tArgs[2] )
- local inWidth = tonumber( tArgs[3] )
- local tall = tonumber( tArgs[4] )
- local useEnderChest = tonumber( tArgs[5] )
- local relCurFacing = 0
- local movedCnt = 0
- local collectMode = 0
- if tall == nil then
- tall = 3
- end
- if useEnderChest == nil then
- useEnderChest = 0
- end
- if inLength < 1 then
- print( "Room length must be positive" )
- return
- end
- local length = inLength
- if tall < 1 then
- print( "Tunnel height must be positive" )
- return
- end
- local depth = 0
- local collected = 0
- local unloaded = 0
- local faceForward = 0
- local faceBack = -2
- local faceLeft = -1
- local faceRight = 1
- local relX, relY, relZ = 0, 0, 0
- local absX, absY, absZ = 0, 0, 0
- local North = 2
- local South = 0
- local East = 3
- local West = 1
- local realFace = South
- local activity = ""
- local planX, planY, planZ = 0, 0, 0
- local planFace = South
- local route = {}
- local routeLen = 0
- local routePos = 1
- local alreadyUnloading = false
- local haveRealGPS = false
- local dontSave = false
- local sOpenedSide = nil
- local function openModem()
- local bOpen, sFreeSide = false, nil
- for n,sSide in pairs(rs.getSides()) do
- if peripheral.getType( sSide ) == "modem" then
- sFreeSide = sSide
- if rednet.isOpen( sSide ) then
- bOpen = true
- break
- end
- end
- end
- if not bOpen then
- if sFreeSide then
- print( "No modem active. Opening "..sFreeSide.." modem" )
- rednet.open( sFreeSide )
- sOpenedSide = sFreeSide
- return true
- else
- print( "No modem attached" )
- return false
- end
- end
- return true
- end
- function closeModem()
- if sOpenedSide then
- rednet.close( sOpenedSide )
- end
- end
- local function saveState()
- --print("Saving State")
- if dontSave then
- return true
- end
- local sf = fs.open(filename .. ".state", "w")
- sf.writeLine('' .. digType)
- sf.writeLine('' .. collected)
- sf.writeLine('' .. unloaded)
- sf.writeLine('' .. relX)
- sf.writeLine('' .. relY)
- sf.writeLine('' .. relZ)
- sf.writeLine('' .. relCurFacing)
- sf.writeLine('' .. absX)
- sf.writeLine('' .. absY)
- sf.writeLine('' .. absZ)
- sf.writeLine('' .. realFace)
- sf.writeLine('' .. routePos)
- if alreadyUnloading then
- sf.writeLine("unloading")
- else
- sf.writeLine("")
- end
- sf.writeLine('' .. movedCnt)
- print("Saving State Values: ")
- print("rel: ", relX, ", ", relY, ", ", relZ)
- sf.close()
- sleep(0)
- end
- local function isFull()
- local bFull = true
- local nTotalItems = 0
- for n=1,15 do
- local nCount = turtle.getItemCount(n)
- if nCount == 0 then
- bFull = false
- end
- nTotalItems = nTotalItems + nCount
- end
- if bFull then
- print( "No empty slots left." )
- return true
- end
- return false
- end
- --local function relTurnFacing(dir)
- -- print("used wrong relTurnFacing")
- -- return nil
- --end
- local function relTurnLeft()
- turtle.turnLeft()
- relCurFacing = relCurFacing - 1
- while relCurFacing <= -3 do
- relCurFacing = relCurFacing + 4
- end
- realFace = realFace - 1
- while realFace < South do
- realFace = realFace + 4
- end
- saveState()
- end
- local function relTurnRight()
- turtle.turnRight()
- relCurFacing = relCurFacing + 1
- while relCurFacing >= 2 do
- relCurFacing = relCurFacing - 4
- end
- realFace = realFace + 1
- while realFace > East do
- realFace = realFace - 4
- end
- saveState()
- end
- local function relTurnFacing(wantDir)
- while wantDir <= -3 do
- wantDir = wantDir + 4
- end
- while wantDir >= 2 do
- wantDir = wantDir - 4
- end
- --Turn right once more to face behind the general dirction of travel.
- if relCurFacing == faceRight and wantDir == faceBack then
- relTurnRight()
- end
- if relCurFacing == faceBack and wantDir == faceRight then
- relTurnLeft()
- end
- --Turn left until we are facing our desired direction
- while relCurFacing > wantDir do
- --print("Left: " .. relCurFacing .. " Want: " .. wantDir)
- relTurnLeft()
- end
- --Turn right until we are facing our desired direction
- while relCurFacing < wantDir do
- --print("Right: " .. relCurFacing .. " Want: " .. wantDir)
- relTurnRight()
- end
- end
- local function absTurnFacing(wantDir)
- print("Absolute Turning: Want: ", wantDir, " At: ", realFace )
- while wantDir < South do
- wantDir = wantDir + 4
- end
- while wantDir > East do
- wantDir = wantDir - 4
- end
- --Turn right once more to face behind the general dirction of travel.
- if realFace == East and wantDir == South then
- relTurnRight()
- end
- if realFace == South and wantDir == East then
- relTurnLeft()
- end
- --Turn left until we are facing our desired direction
- while realFace > wantDir do
- print("Left: " .. realFace .. " Want: " .. wantDir)
- relTurnLeft()
- end
- --Turn right until we are facing our desired direction
- while realFace < wantDir do
- print("Right: " .. realFace .. " Want: " .. wantDir)
- relTurnRight()
- end
- end
- local function simpleDig()
- while turtle.detect() do
- if turtle.dig() then
- sleep(1)
- else
- return false
- end
- end
- return true
- end
- local function simpleDigUp()
- while turtle.detectUp() do
- if turtle.digUp() then
- sleep(1)
- else
- return false
- end
- end
- return true
- end
- local function unload()
- if alreadyUnloading then
- return false
- end
- print( "Unloading items..." )
- alreadyUnloading = true
- local wasFacing = relCurFacing
- relTurnFacing(faceBack)
- turtle.select(3)
- existingChest = false
- if turtle.detect() then
- --There is something in front of us.
- if turtle.suck() then
- --It is a chest!
- existingChest = true
- end
- --Or it was an empty chest and we'll just swap it out with ours.
- end
- if not existingChest then
- while not turtle.place() do
- if turtle.detect() then
- if not simpleDig() then
- return false
- end
- elseif turtle.attack() then
- sleep( 0.5 )
- else
- sleep( 0.5 )
- end
- end
- end
- --1: Flooring
- --2: Fuel
- --3: Chests
- --4: Torches
- for n=5,16 do
- unloaded = unloaded + turtle.getItemCount(n)
- turtle.select(n)
- if turtle.getItemCount(n) > 0 then
- while not turtle.drop() do
- print("The target chest is full")
- sleep(30)
- end
- end
- end
- if useEnderChest ~= 0 then
- turtle.select(3)
- turtle.dig()
- end
- if turtle.getItemCount(3) == 0 then
- print("Please load more chests")
- while turtle.getItemCount(3) == 0 do
- sleep(10)
- end
- end
- --collected = 0
- turtle.select(1)
- relTurnFacing(wasFacing)
- alreadyUnloading = false
- end
- local function findCompress()
- for n=1,16 do
- --print("Slot " .. n .. " has " .. turtle.getItemCount(n))
- if turtle.getItemCount(n) > 8 then
- --print("Slot " .. n .. " will be compressed.")
- --sleep(0.01)
- return n
- end
- end
- --print("No slot has enough to compress")
- --sleep(0.01)
- return 0
- end
- local function tryCompress()
- local cmpr
- print("Restacking")
- turtle.select(1)
- while turtle.suck() do
- --sleep(0)
- cmpr = 0
- end
- cmpr = findCompress()
- while cmpr > 0 do
- --get rid of what we don't need
- for n=1,16 do
- if n ~= cmpr and turtle.getItemCount(n) > 0 then
- turtle.select(n)
- while not turtle.drop() do
- print("The target chest is full")
- sleep(30)
- end
- end
- end
- print("Should have only the item to compress.")
- turtle.select(cmpr)
- turtle.transferTo(1)
- print("Moved it to the top left")
- while turtle.getItemCount(1) > 8 do
- turtle.select(1)
- turtle.transferTo(2, 1)
- turtle.transferTo(3, 1)
- turtle.transferTo(5, 1)
- turtle.transferTo(6, 1)
- turtle.transferTo(7, 1)
- turtle.transferTo(9, 1)
- turtle.transferTo(10, 1)
- turtle.transferTo(11, 1)
- turtle.select(4)
- if not turtle.craft(1) then
- print("Failed to craft.")
- --It does not compress, get it out of the system.
- turtle.select(1)
- turtle.dropUp()
- else
- print("Made one!")
- --put the result into the chest
- turtle.drop()
- end
- end
- --put the leftovers into the chest (dupe bug work-around, I hope)
- turtle.select(1)
- turtle.drop()
- --Re stack everything to cascade the compression and start a new search.
- print("Restacking")
- turtle.select(1)
- while turtle.suck() do
- cmpr = findCompress()
- if cmpr > 0 then
- break
- end
- sleep(0)
- end
- cmpr = findCompress()
- end
- print("Dumping.")
- --Put everything into the chest
- for n=1,16,1 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- while not turtle.drop() do
- print("The target chest is full")
- sleep(30)
- end
- end
- end
- print("Done Compressing")
- return true
- end
- local function compress()
- if useEnderChest ~= 0 then
- return unload()
- end
- print( "Compressing items..." )
- local wasFacing = relCurFacing
- relTurnFacing(faceBack)
- turtle.select(3)
- existingChest = false
- if turtle.detect() then
- --There is something in front of us.
- if turtle.suck() then
- --It is a chest!
- existingChest = true
- end
- --Or it was an empty chest and we'll just swap it out with ours.
- end
- if not existingChest then
- while not turtle.place() do
- if turtle.detect() then
- if not simpleDig() then
- return false
- end
- elseif turtle.attack() then
- sleep( 0.5 )
- else
- sleep( 0.5 )
- end
- end
- end
- --again with a chest above.
- existingChest = false
- if turtle.detectUp() then
- --There is something in front of us.
- if turtle.suckUp() then
- --It is a chest!
- existingChest = true
- end
- --Or it was an empty chest and we'll just swap it out with ours.
- end
- if not existingChest then
- while not turtle.placeUp() do
- if turtle.detectUp() then
- if not simpleDigUp() then
- return false
- end
- elseif turtle.attackUp() then
- sleep( 0.5 )
- else
- sleep( 0.5 )
- end
- end
- end
- --1: Flooring
- --2: Fuel
- --3: Chests
- --4: Torches
- --Unload the stuff we need to keep in proper place.
- for n=1,4 do
- --unloaded = unloaded + turtle.getItemCount(n)
- turtle.select(n)
- if turtle.getItemCount(n) > 0 then
- while not turtle.dropUp() do
- print("The target chest is full")
- sleep(30)
- end
- end
- end
- --
- for n=5,16 do
- unloaded = unloaded + turtle.getItemCount(n)
- turtle.select(n)
- if turtle.getItemCount(n) > 0 then
- while not turtle.drop() do
- print("The target chest is full")
- sleep(30)
- end
- end
- end
- tryCompress()
- --Reload the stuff we need to keep in proper place.
- for n=1,16 do
- turtle.select(n)
- turtle.suckUp()
- end
- for n=16,5,-1 do
- turtle.select(n)
- turtle.suck()
- end
- --collect the chests
- turtle.select(3)
- simpleDigUp()
- simpleDig()
- if turtle.getItemCount(3) == 0 then
- print("Please load more chests")
- while turtle.getItemCount(3) == 0 do
- sleep(10)
- end
- end
- --collected = 0
- turtle.select(1)
- relTurnFacing(wasFacing)
- end
- local function collect()
- collected = collected + 1
- if math.fmod(collected, 25) == 0 then
- print( "Mined "..collected.." items." )
- end
- if isFull() then
- if collectMode == 0 then
- unload()
- else
- compress()
- end
- end
- end
- local function tryDig()
- --Collect stuff from chests.
- local cnt = false
- while turtle.suck() do
- collect()
- end
- while turtle.detect() do
- if turtle.dig() then
- collect()
- cnt = true
- --sleep(0.5)
- else
- return false, cnt
- end
- end
- return true, cnt
- end
- local function tryDigUp()
- while turtle.suckUp() do
- collect()
- end
- while turtle.detectUp() do
- if turtle.digUp() then
- collect()
- --sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryDigDown()
- while turtle.suckDown() do
- collect()
- end
- while turtle.detectDown() do
- if turtle.digDown() then
- collect()
- --sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function refuel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" or fuelLevel > 0 then
- return
- end
- local function tryRefuel()
- for n=2,16 do
- if n ~= 3 and n ~= 4 then
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- turtle.select(1)
- return true
- end
- end
- end
- end
- turtle.select(1)
- return false
- end
- if not tryRefuel() then
- print( "Add more fuel to continue." )
- while not tryRefuel() do
- sleep(1)
- end
- print( "Resuming Tunnel." )
- end
- end
- local function tryUp()
- refuel()
- while not turtle.up() do
- if turtle.detectUp() then
- if not tryDigUp() then
- return false
- end
- elseif turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- relY = relY + 1
- absY = absY + 1
- saveState()
- print("rel: ", relX, ", ", relY, ", ", relZ)
- print("abs: ", absX, ", ", absY, ", ", absZ)
- print("route pos: ", routePos)
- if routePos ~= nil and route[routePos] ~= nil then
- print("route: ", route[routePos].x, ", ", route[routePos].y, ", ", route[routePos].z)
- end
- return true
- end
- local function tryDown()
- refuel()
- while not turtle.down() do
- if turtle.detectDown() then
- if not tryDigDown() then
- return false
- end
- elseif turtle.attackDown() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- relY = relY - 1
- absY = absY - 1
- saveState()
- print("rel: ", relX, ", ", relY, ", ", relZ)
- print("abs: ", absX, ", ", absY, ", ", absZ)
- print("route pos: ", routePos)
- if routePos ~= nil and route[routePos] ~= nil then
- print("route: ", route[routePos].x, ", ", route[routePos].y, ", ", route[routePos].z)
- end
- return true
- end
- local function absVector(inDir)
- if inDir == North then
- return 0, -1
- elseif inDir == South then
- return 0, 1
- elseif inDir == East then
- return 1, 0
- elseif inDir == West then
- return -1, 0
- end
- return nil, nil
- end
- local function trySimpleForwardNoTorch()
- refuel()
- while not turtle.forward() do
- if turtle.detect() then
- return false
- elseif turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- movedCnt = movedCnt + 1
- local dx, dz = absVector(realFace)
- relX = relX + dx
- absX = absX + dx
- relZ = relZ + dz
- absZ = absZ + dz
- saveState()
- print("rel: ", relX, ", ", relY, ", ", relZ)
- print("abs: ", absX, ", ", absY, ", ", absZ)
- print("route pos: ", routePos)
- if routePos ~= nil and route[routePos] ~= nil then
- print("route: ", route[routePos].x, ", ", route[routePos].y, ", ", route[routePos].z)
- end
- return true
- end
- local function tryForwardNoTorch()
- refuel()
- while not turtle.forward() do
- if turtle.detect() then
- if not tryDig() then
- return false
- end
- elseif turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- movedCnt = movedCnt + 1
- local dx, dz = absVector(realFace)
- relX = relX + dx
- absX = absX + dx
- relZ = relZ + dz
- absZ = absZ + dz
- saveState()
- print("rel: ", relX, ", ", relY, ", ", relZ)
- print("abs: ", absX, ", ", absY, ", ", absZ)
- print("route pos: ", routePos)
- if routePos ~= nil and route[routePos] ~= nil then
- print("route: ", route[routePos].x, ", ", route[routePos].y, ", ", route[routePos].z)
- end
- return true
- end
- local function tryForward()
- local result
- result = tryForwardNoTorch()
- if result == false then
- return false
- end
- if math.fmod(movedCnt, 6) == 0 and turtle.getItemCount(4) then
- if turtle.getItemCount(4) == 1 then
- print("Please load more torches")
- while turtle.getItemCount(4) <= 1 do
- sleep(10)
- end
- end
- relTurnLeft()
- relTurnLeft()
- turtle.select(4)
- turtle.place()
- turtle.select(1)
- relTurnLeft()
- relTurnLeft()
- end
- return true
- end
- local function planAdd(a, px, py, pz)
- routeLen = routeLen + 1
- point = {}
- point.action = a
- point.x = px
- point.y = py
- point.z = pz
- route[routeLen] = point
- end
- local function planForward(planDist)
- if planDist == nil then
- planDist = 1
- end
- if planFace == North then
- planZ = planZ - planDist
- elseif planFace == South then
- planZ = planZ + planDist
- elseif planFace == West then
- planX = planX - planDist
- elseif planFace == East then
- planX = planX + planDist
- end
- --planAdd(planX, planY, planZ)
- end
- local function planBack(planDist)
- if planDist == nil then
- planDist = 1
- end
- planForward(-planDist)
- end
- local function planUp(planDist)
- if planDist == nil then
- planDist = 1
- end
- planY = planY + planDist
- --planAdd(planX, planY, planZ)
- end
- local function planDown(planDist)
- if planDist == nil then
- planDist = 1
- end
- planUp(-planDist)
- end
- local function planTurnLeft()
- planFace = planFace - 1
- while planFace < South do
- planFace = planFace + 4
- end
- end
- local function planTurnRight()
- planFace = planFace + 1
- while planFace > East do
- planFace = planFace - 4
- end
- end
- local function tryBack()
- refuel()
- while not turtle.back() do
- relTurnRight()
- relTurnRight()
- if turtle.detect() then
- if not tryDig() then
- return false
- end
- elseif turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- relTurnRight()
- relTurnRight()
- end
- movedCnt = movedCnt - 1
- local dx, dz = absVector(realFace)
- relX = relX - dx
- absX = absX - dx
- relZ = relZ - dz
- absZ = absZ - dz
- saveState()
- return true
- end
- local function getRealFacing()
- print("Real Facing.")
- openModem()
- dontSave = true
- local startX, startY, startZ = gps.locate(5, true)
- print(startX)
- if startX == nil then
- dontSave = false
- return false
- end
- if not tryForward() then
- print("could not go forward during facing test")
- dontSave = false
- return false
- end
- absX, absY, absZ = gps.locate(5)
- if absX == nil then
- absX, absY, absZ = 0, 0, 0
- dontSave = false
- return false
- end
- dx = startX - absX
- dz = startZ - absZ
- if dx == -1 then
- realFace = East
- elseif dx == 1 then
- realFace = West
- elseif dz == -1 then
- realFace = South
- elseif dz == 1 then
- realFace = North
- end
- print("Real facing: ", realFace)
- tryBack()
- relX, relY, relZ = 0, 0, 0
- absX, absY, absZ = startX, startY, startZ
- --sleep(10)
- dontSave = false
- return true
- end
- local function planHallway(hallLength, ltall)
- planForward(hallLength - 1)
- planAdd("t", planX, planY, planZ)
- end
- local function planTube(hallLength, ltall)
- planForward(hallLength - 1)
- planAdd("t1", planX, planY, planZ)
- end
- local function hallway(hallLength, ltall)
- relCurFacing = 0
- local leftDig, rightDig, resCollect, resDig
- for n=1,hallLength do
- leftDig, rightDig, preCol = 0, 0, collected
- turtle.placeDown()
- relTurnFacing(faceLeft)
- ResDig, resCollect = tryDig()
- if resCollect then
- leftDig = leftDig + 1
- end
- for m=2, ltall do
- tryDigUp()
- tryUp()
- ResDig, resCollect = tryDig()
- if resCollect then
- leftDig = leftDig + 1
- end
- end
- relTurnFacing(faceRight)
- ResDig, resCollect = tryDig()
- if resCollect then
- rightDig = rightDig + 1
- end
- for m=2, ltall do
- tryDown()
- ResDig, resCollect = tryDig()
- if resCollect then
- rightDig = rightDig + 1
- end
- end
- --Clean up the damn gravel/sand
- if rightDig > 0 then
- sleep(0.5)
- while turtle.detect() do
- tryDig()
- sleep(2)
- end
- end
- if leftDig > 0 then
- relTurnFacing(faceLeft)
- while turtle.detect() do
- tryDig()
- sleep(2)
- end
- end
- relTurnFacing(faceForward)
- while ltall > 1 and turtle.detectUp() do
- tryDigUp()
- sleep(2)
- end
- if n<hallLength then
- tryDig()
- if not tryForward() then
- return false
- end
- else
- return true
- end
- end
- end
- local function tube(hallLength, ltall)
- relCurFacing = 0
- local leftDig, rightDig, resCollect, resDig
- for n=1,hallLength do
- leftDig, rightDig, preCol = 0, 0, collected
- turtle.placeDown()
- if ltall == 2 then
- tryDigUp()
- elseif ltall > 2 then
- for m=2, ltall do
- tryDigUp()
- tryUp()
- ResDig, resCollect = tryDig()
- if resCollect then
- leftDig = leftDig + 1
- end
- end
- end
- if ltall > 2 then
- for m=2, ltall do
- tryDown()
- ResDig, resCollect = tryDig()
- if resCollect then
- rightDig = rightDig + 1
- end
- end
- end
- --Clean up the damn gravel/sand
- relTurnFacing(faceForward)
- while ltall > 1 and turtle.detectUp() do
- tryDigUp()
- sleep(2)
- end
- if n<hallLength then
- tryDig()
- if not tryForward() then
- return false
- end
- else
- return true
- end
- end
- end
- local function PlanRoom()
- print( "Planning Room..." )
- planTurnRight()
- planTurnRight()
- planForward(1)
- planAdd("d1", planX, planY, planZ)
- planHallway(1, tall)
- planTurnRight()
- planTurnRight()
- planForward(1)
- planAdd("d1", planX, planY, planZ)
- local lmod = 0
- for s=1,4 do
- print( "Side: "..s )
- planHallway(length - lmod, tall)
- planBack()
- if s == 4 then
- planBack()
- end
- planAdd("d1", planX, planY, planZ)
- planTurnLeft()
- --if s < 4 then
- planForward(2)
- planAdd("d1", planX, planY, planZ)
- lmod = 2
- --end
- if s == 3 then
- lmod = 4
- end
- end
- --planTurnRight()
- --planBack(1)
- --planAdd("d1", planX, planY, planZ)
- --planTurnLeft()
- --planForward()
- --planAdd("d1", planX, planY, planZ)
- -- cut + hall error + length mod.
- length = length - 6 + 1 + 1
- reduceRun = true
- while length > 1 do
- print( "Length: "..length )
- planHallway(length - 1, tall)
- if reduceRun then
- length = length - 3
- reduceRun = false
- else
- length = length - 0
- reduceRun = true
- end
- planBack()
- planAdd("d1", planX, planY, planZ)
- planTurnLeft()
- planForward(2)
- planAdd("d1", planX, planY, planZ)
- end
- print( "Room planning complete." )
- end
- local function planBranchMining()
- print( "Planning Mine..." )
- planTube(inLength, 2)
- planTurnLeft()
- local turnLeft = true
- local cnt = inLength / 3
- for tn = 1, cnt do
- planTube(inWidth, 2)
- if turnLeft then
- planTurnLeft()
- else
- planTurnRight()
- end
- planTube(4, 2)
- if turnLeft then
- planTurnLeft()
- turnLeft = false
- else
- planTurnRight()
- turnLeft = true
- end
- end
- print( "Mine planning complete." )
- end
- local function savePlan()
- print("Save Plan")
- local sf = fs.open(filename .. ".plan", "w")
- sf.writeLine(routeLen)
- for rp = 1, routeLen do
- sf.writeLine(route[rp].action)
- sf.writeLine(route[rp].x)
- sf.writeLine(route[rp].y)
- sf.writeLine(route[rp].z)
- end
- sf.close()
- end
- local function readNumber(sf)
- print("Read Number")
- local x = tonumber(sf.readLine()) + 0
- print("Read ", x)
- return x
- end
- local function loadState()
- print("Load State")
- local sf = fs.open(filename .. ".state", "r")
- digType = sf.readLine()
- collected = readNumber(sf)
- unloaded = readNumber(sf)
- relX = readNumber(sf)
- relY = readNumber(sf)
- relZ = readNumber(sf)
- relCurFacing = readNumber(sf)
- absX = readNumber(sf)
- absY = readNumber(sf)
- absZ = readNumber(sf)
- realFace = readNumber(sf)
- routePos = readNumber(sf)
- alreadyUnloading = (sf.readLine() == "unloading")
- movedCnt = readNumber(sf)
- sf.close()
- --sleep(5)
- print("State Values: ")
- print("digType: ", digType)
- --print("collected: ", collected)
- --print("unloaded: ", unloaded)
- --print("absX: ", absX)
- --print("absY: ", absY)
- --print("absZ: ", absZ)
- print("routePos: ", routePos)
- --print("alreadyUnloading: ", alreadyUnloading)
- print("movedCnt: ", movedCnt)
- print("rel: ", relX, ", ", relY, ", ", relZ)
- --sleep(12)
- end
- local function loadPlan()
- print("Load Plan")
- local sf = fs.open(filename .. ".plan", "r")
- local rl = readNumber(sf)
- local rp, x, y, z
- for rp = 1, rl do
- action = sf.readLine()
- x = readNumber(sf)
- y = readNumber(sf)
- z = readNumber(sf)
- planAdd(action, x, y, z)
- end
- sf.close()
- end
- local function tryFace(targX, targZ)
- print("Try Face")
- local dx, dz = targX - relX, targZ - relZ
- print("X: ", relX, " to ", targX, " Z: ", relZ, " to ", targZ)
- if math.abs(dx) > math.abs(dz) then
- if dx > 0 then
- absTurnFacing(East)
- print("Turnig to the East")
- elseif dx < 0 then
- absTurnFacing(West)
- print("Turnig to the West")
- end
- else
- if dz > 0 then
- absTurnFacing(South)
- print("Turnig to the South")
- elseif dz < 0 then
- absTurnFacing(North)
- print("Turnig to the North")
- end
- end
- --sleep(5)
- return true
- end
- local function DoGo(x, y, z)
- print("Do Go")
- local dx, dy, dz = x - relX, y - relY, z - relZ
- local adx, ady, adz, tryRes
- while dx ~= 0 or dy ~= 0 or dz ~= 0 do
- print(dx, dy, dz)
- adx = math.abs(dx)
- ady = math.abs(dy)
- adz = math.abs(dz)
- tryRes = true
- if dy ~= 0 and (ady <= adx or dx == 0) and (ady <= adz or dz ==0) then
- if dy > 0 then
- tryRes = tryUp()
- else
- tryRes = tryDown()
- end
- elseif dx ~= 0 and (adx <= ady or dy == 0) and (adx <= adz or dz ==0) then
- if dx > 0 then
- absTurnFacing(East)
- else
- absTurnFacing(West)
- end
- relCurFacing = faceForward
- tryRes = tryForward()
- elseif dz ~= 0 and (adz <= ady or dy == 0) and (adz <= adx or dx ==0) then
- if dz > 0 then
- absTurnFacing(South)
- else
- absTurnFacing(North)
- end
- relCurFacing = faceForward
- tryRes = tryForward()
- end
- if tryRes == false then
- print("Unable to go to desired location!")
- return false
- end
- dx, dy, dz = x - relX, y - relY, z - relZ
- end
- return true
- end
- local function DoGoAbs(x, y, z)
- print("Do Go")
- local dx, dy, dz = x - absX, y - absY, z - absZ
- local adx, ady, adz, tryRes
- while dx ~= 0 or dy ~= 0 or dz ~= 0 do
- print(dx, dy, dz)
- adx = math.abs(dx)
- ady = math.abs(dy)
- adz = math.abs(dz)
- tryRes = true
- if dy ~= 0 and ady < adx and ady < adz then
- if dy > 0 then
- tryRes = tryUp()
- else
- tryRes = tryDown()
- end
- elseif dx ~= 0 and adx < ady and adx < adz then
- if dx > 0 then
- absTurnFacing(East)
- else
- absTurnFacing(West)
- end
- tryRes = tryForward()
- elseif dz ~= 0 and adz < ady and adz < adx then
- if dz > 0 then
- absTurnFacing(South)
- else
- absTurnFacing(North)
- end
- tryRes = tryForward()
- end
- if tryRes == false then
- print("Unable to go to desired location!")
- return false
- end
- dx, dy, dz = x - absX, y - absY, z - absZ
- end
- return true
- end
- local function DoPlan(startPos)
- print("Do Plan")
- local p
- for p=startPos,routeLen do
- routePos = p
- if route[p].action == "t" then
- tryFace(route[p].x, route[p].z)
- local dist = math.abs(route[p].x - relX) + math.abs(route[p].z - relZ)
- hallway(dist + 1, tall)
- elseif route[p].action == "t1" then
- tryFace(route[p].x, route[p].z)
- local dist = math.abs(route[p].x - relX) + math.abs(route[p].z - relZ)
- tube(dist + 1, tall)
- elseif route[p].action == "d1" then
- DoGo(route[p].x, route[p].y, route[p].z)
- end
- end
- end
- local function resume()
- print("Resume")
- local curX, curY, curZ, curFace = absX, absY, absZ, realFace
- loadState()
- if haveRealGPS then
- print("Real GPS said ", curX, ", ", curY, ", ", curZ, ", ", curFace)
- print("State GPS said ", absX, ", ", absY, " , ", absZ, ", ", realFace)
- print("State Rel said ", relX, ", ", relY, " , ", relZ, ", ", realFace)
- realFace = curFace
- --sleep(5)
- end
- loadPlan()
- if alreadyUnloading then
- alreadyUnloading = false
- if useEnderChest ~= 0 then
- turtle.select(3)
- turtle.dig()
- unload()
- elseif turtle.getItemCount(3) == 0 then
- print("Please load more chests")
- while turtle.getItemCount(3) == 0 do
- sleep(10)
- end
- else
- unload()
- end
- end
- if routePos > routeLen then
- return false
- end
- if route[routePos].action == "t" and route[routePos].y < relY then
- while route[routePos].y < relY do
- if tryDown() == false then
- print("could not get back into position")
- return false
- end
- end
- end
- DoPlan(routePos)
- return true
- end
- local function createStartup()
- print("Create Startup")
- if fs.exists("startup") then
- if fs.exists("startup.original") then
- fs.delete("startup.original")
- end
- fs.copy("startup", "startup.original")
- end
- local sf = fs.open("startup", "w")
- sf.writeLine("shell.run('" .. filename .. "', 'resume', " .. inLength .. ", " .. inWidth .. ", " .. tall .. ", " .. useEnderChest .. ")")
- sf.close()
- end
- local function revertStartup()
- print("Revert Startup")
- fs.delete("startup")
- if fs.exists("startup.original") then
- fs.move("startup.original", "startup")
- end
- end
- local function startPicky()
- local d, compressTime
- compressTime = 0
- collectMode = 1
- print("Planed Distance: " .. inLength)
- for d = 1,inLength do
- print("Distance: " .. d .. " of " .. inLength)
- turtle.select(1)
- if turtle.detect() then
- --Make sure we have a block to deal with.
- if turtle.compare() then
- turtle.dig()
- trySimpleForwardNoTorch()
- else
- relTurnRight()
- if turtle.detect() then
- if turtle.compare() then
- turtle.dig()
- trySimpleForwardNoTorch()
- else
- relTurnLeft()
- relTurnLeft()
- if turtle.detect() then
- if turtle.compare() then
- turtle.dig()
- trySimpleForwardNoTorch()
- else
- relTurnLeft()
- tryForwardNoTorch()
- relTurnLeft()
- end
- end
- end
- end
- end
- else
- --Nothing in front of us, just go!
- trySimpleForwardNoTorch()
- end
- turtle.select(1)
- if turtle.compareUp() then
- turtle.digUp()
- end
- compressTime = compressTime + 1
- if isFull() and compressTime > 8 then
- compressTime = 0
- compress()
- end
- end
- end
- local function startPlan()
- print("Start Plan")
- saveState()
- savePlan()
- createStartup()
- return DoPlan(1)
- end
- function main()
- digType = string.lower(digType)
- haveRealGPS = false --getRealFacing()
- --if haveRealGPS then
- planFace = realFace
- --end
- if digType == "resume" then
- resume()
- elseif digType == "room" or digType == "r" then
- PlanRoom()
- startPlan()
- elseif digType =="quarry" or digType == "q" then
- planDown(tall)
- planAdd("d1", planX, planY, planZ)
- PlanRoom()
- startPlan()
- elseif digType =="branch" or digType == "bm" then
- planBranchMining()
- startPlan()
- elseif digType == "tunnel" or digType == "t" then
- planHallway(length, tall)
- startPlan()
- elseif digType == "down" or digType == "d" then
- planDown(length)
- planAdd("d1", planX, planY, planZ)
- startPlan()
- elseif digType == "up" or digType == "u" then
- planUp(length)
- planAdd("d1", planX, planY, planZ)
- startPlan()
- elseif digType == "forward" or digType == "f" then
- planForward(length)
- planAdd("d1", planX, planY, planZ)
- startPlan()
- elseif digType == "back" or digType == "b" then
- planBack(length)
- planAdd("d1", planX, planY, planZ)
- startPlan()
- elseif digType == "south" then
- absTurnFacing(South)
- elseif digType == "north" then
- absTurnFacing(North)
- elseif digType == "east" then
- absTurnFacing(East)
- elseif digType == "west" then
- absTurnFacing(West)
- elseif digType == "picky" or digType == "p" then
- startPicky()
- elseif digType == "crafttest" then
- compress()
- end
- if digType == "back" or digType == "b" then
- turtle.turnRight()
- turtle.turnRight()
- end
- if useEnderChest ~= 0 then
- unload()
- end
- print( "Digging complete." )
- print( "Mined "..collected.." items total." )
- revertStartup()
- closeModem()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement