Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --pastebin get mFD6ygbm Lighter
- local tArgs = { ... }
- local filename = shell.getRunningProgram()
- if #tArgs < 3 then
- print( "Usage: " .. filename .. " <spacing> <length> <width>" )
- print( "Slot 1 should be a stack of fuel." )
- print( "All other slots are light blocks to place below at interval." )
- return
- end
- -- Mine in a quarry pattern until we hit something we can't dig
- local digType = "Count"
- local nDelay = tonumber( tArgs[1] )
- local inLength = tonumber( tArgs[2] )
- local inWidth = tonumber( tArgs[3] )
- local relCurFacing = 0
- local movedCnt = 0
- if tall == nil then
- tall = 3
- end
- if inLength < 1 then
- inLength = 1
- end
- local length = inLength
- if inWidth < 1 then
- inWidth = 1
- end
- local width = inWidth
- if tall < 1 then
- tall = 1
- 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 function saveState()
- return true
- 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 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 unload()
- if alreadyUnloading then
- return false
- end
- print( "Unloading items..." )
- --1: Left
- --2: Right
- --3: Chests
- --4: Fuel
- 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
- turtle.select(1)
- end
- local function collect()
- collected = collected + 1
- if math.fmod(collected, 25) == 0 then
- print( "Mined "..collected.." items." )
- 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=1,1 do
- if n ~= 17 and n ~= 27 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)
- 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()
- 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 tryForward()
- 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()
- return true
- 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 readNumber(sf)
- print("Read Number")
- local x = tonumber(sf.readLine()) + 0
- print("Read ", x)
- return x
- 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
- function compareFrontToSlot(inSlot)
- turtle.select(inSlot)
- return turtle.compare()
- end
- local function placeAny()
- for n=2,16 do
- if turtle.getItemCount(n) > 1 then
- turtle.select(n)
- tryDigDown()
- if turtle.placeDown() then
- turtle.select(1)
- return true
- end
- end
- end
- turtle.select(1)
- return false
- end
- function main()
- digType = string.lower(digType)
- haveRealGPS = false
- planFace = realFace
- originalFace = realFace
- while width >= 0 do
- local dropCnt = 0
- for lPos = 1,length do
- if dropCnt == 0 then
- placeAny()
- end
- dropCnt = dropCnt + 1
- if dropCnt == nDelay then
- dropCnt = 0
- end
- tryForward()
- end
- if dropCnt == 0 then
- placeAny()
- end
- if width < nDelay then
- width = width - nDelay
- relTurnLeft()
- relTurnLeft()
- elseif realFace == originalFace then
- relTurnLeft()
- for lPos = 1,nDelay do
- tryForward()
- width = width - 1
- end
- relTurnLeft()
- else
- relTurnRight()
- for lPos = 1,nDelay do
- tryForward()
- width = width - 1
- end
- relTurnRight()
- end
- end
- print( "Lighting complete." )
- print( "Blocking "..collected.." items total." )
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment