Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pit
- -- Mines pit to specified depth or bedrock
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, June 2014
- args = {...}
- nArgs = #args
- version = "pit: Rev 2.4"
- mission = "Mines pit to specified length, width and depth or bedrock."
- usage = "walls <length> <width> <depth>"
- x = 0
- y = 0
- z = 0
- orgX = 0
- orgY = 0
- orgZ = 0
- orgLength = 0
- orgWidth = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- startLevel = 0
- local currentFuelLevel = turtle.getFuelLevel()
- targetArea = 0
- areaMined = 0
- bedrockReached = false
- lowestLevelReached = false
- lowestLevelMined = 0
- finalApproach = false
- liquidCaution = true
- areaCovered = 0
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(mission)
- print(usage)
- return
- end
- if nArgs ~= 3 then
- print(usage)
- return
- end
- length = tonumber(args[1])
- if length == nil then
- print("\"", args[1], "\" is not a valid length")
- return
- end
- if length < 1 then
- print("length must be a positive number greater than zero")
- end
- orgLength = length
- width = tonumber(args[2])
- if width == nil then
- print("\"", args[2], "\" is not a valid width")
- return
- end
- if width < 1 then
- print("width must be a positive number greater than zero")
- end
- orgWidth = width
- targetArea = length * width
- depth = tonumber(args[3])
- if startLevel == nil then
- print("\"", args[3], "\" is not a valid depth")
- return
- end
- currentSlot = 2
- materialSlot = 1
- maxSlot = 16
- fillerSlot = 2
- nextTurn = "left"
- local clock = os.clock
- function sleep(n) -- seconds
- local t0 = clock()
- while clock() - t0 <= n do end
- end
- function left()
- if face == 0 then face = 1 turtle.turnLeft() return end
- if face == 1 then face = 2 turtle.turnLeft() return end
- if face == 2 then face = 3 turtle.turnLeft() return end
- if face == 3 then face = 0 turtle.turnLeft() return end
- print("function left\(\): Bad face value: ", face)
- end
- function right()
- if face == 0 then face = 3 turtle.turnRight() return end
- if face == 1 then face = 0 turtle.turnRight() return end
- if face == 2 then face = 1 turtle.turnRight() return end
- if face == 3 then face = 2 turtle.turnRight() return end
- print("function right\(\): Bad face value: ", face)
- end
- function up()
- while turtle.detectUp() do -- This loop added in case of falling sand or whatever
- turtle.digUp()
- end
- turtle.up()
- y = y+1
- end
- function rise(nBlocks)
- local i
- for i=1,nBlocks do
- up()
- end
- end
- function down()
- if turtle.detectDown() then
- if not turtle.digDown() then
- return -- must be at bedrock
- end
- end
- turtle.down()
- y = y-1
- -- print("down: y is now ", y)
- end
- function turn()
- if nextTurn == "left" then
- left()
- else
- right()
- end
- chomp()
- forward()
- if nextTurn == "left" then
- left()
- nextTurn = "right"
- else
- right()
- nextTurn = "left"
- end
- areaCovered = areaCovered + 1
- end
- function forward()
- if liquidCaution and (not turtle.detect()) then
- turtle.select(fillerSlot)
- turtle.place()
- end
- while turtle.detect() do -- This loop added in case of falling sand or whatever
- turtle.dig()
- end
- for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
- if turtle.forward() then
- break
- end
- turtle.attack()
- sleep(2)
- end
- areaCovered = areaCovered + 1
- if face == 0 then z = z+1 return end
- if face == 1 then x = x-1 return end
- if face == 2 then z = z-1 return end
- if face == 3 then x = x+1 return end
- end
- function setFace(f)
- if f == 0 then
- if face == 0 then return end
- if face == 1 then right() return end
- if face == 2 then right() right() return end
- if face == 3 then left() return end
- end
- if f == 1 then
- if face == 0 then left() return end
- if face == 1 then return end
- if face == 2 then right() return end
- if face == 3 then right() right() return end
- end
- if f == 2 then
- if face == 0 then left() left() return end
- if face == 1 then left() return end
- if face == 2 then return end
- if face == 3 then right() return end
- end
- if f == 3 then
- if face == 0 then right() return end
- if face == 1 then left() left() return end
- if face == 2 then left() return end
- if face == 3 then return end
- end
- end
- function home(targetY)
- -- print("home:face ", face, ", x = ", x, ", z = ", z)
- if finalApproach then print("Final approach to home...") end
- liquidCaution = false -- No need to watch for liquid blocks
- if x < 0 then
- setFace(3)
- while x < 0 do
- forward()
- end
- else
- if x > 0 then
- setFace(1)
- while x > 0 do
- forward()
- end
- end
- end
- if z < 0 then
- setFace(0)
- while z < 0 do
- forward()
- end
- else
- if z > 0 then
- setFace(2)
- while z > 0 do
- forward()
- end
- end
- end
- if finalApproach then print(" y = ", y, ", lowestLevel = ", lowestLevel) end
- if targetY == lowestLevel then
- setFace(0)
- liquidCaution = true
- return
- end
- if finalApproach then print(" y = ", y, ", targetY = ", targetY) end
- if y < targetY then
- if finalApproach then print(" Heading up..." ) end
- while y < targetY do
- if finalApproach then print(" up..." ) end
- up()
- end
- end
- setFace(0)
- liquidCaution = true
- end
- function isKeeper()
- for i=1,4 do
- if turtle.compareTo(i) then
- return true
- end
- end
- return false
- end
- function isDisposable()
- for i=1,4 do
- if turtle.compareTo(i) then
- return true
- end
- end
- return false
- end
- function purge()
- --[[
- for i=1,4 do
- turtle.select(i)
- if isDisposable() then
- count = turtle.getItemCount(i)
- turtle.dropUp(count - 1)
- end
- end
- --]]
- for i=5,16 do
- turtle.select(i)
- if isDisposable() then
- turtle.dropUp()
- end
- end
- turtle.select(1)
- end
- function isFull()
- for i=5,16 do
- if turtle.getItemCount(i) == 0 then
- return false
- end
- end
- turtle.select(1)
- return true
- end
- function emptySlots()
- empties = 0
- for i=5,16 do
- if turtle.getItemCount(i) == 0 then
- empties = empties + 1
- end
- end
- return empties
- end
- function ensureMaterial()
- if turtle.getItemCount(materialSlot) < 3 then
- organizeMaterial()
- end
- if turtle.getItemCount(materialSlot) < 3 then
- print("No more material")
- return false
- end
- return true
- end
- function organizeMaterial()
- local i
- materialCount = turtle.getItemCount(materialSlot)
- if materialCount < 3 then
- -- print("Attempting to refill slot ", materialSlot)
- for i=2,16 do
- turtle.select(i)
- if turtle.compareTo(materialSlot) then
- turtle.transferTo(materialSlot)
- end
- end
- end
- turtle.select(materialSlot)
- end
- -- Group all materials into minimum slots
- function consolidate()
- for dst = 5,16 do
- for src=(dst+1),16 do
- turtle.select(src)
- if turtle.getItemCount(src) > 0 then
- if turtle.compareTo(dst) then
- turtle.transferTo(dst)
- end
- end
- end
- end
- turtle.select(1)
- end
- function layBlock()
- turtle.select(materialSlot)
- if startLevel > 2 then
- if turtle.detectUp() then
- if turtle.compareUp() then
- return
- else
- turtle.digUp()
- end
- end
- if ensureMaterial() then
- turtle.placeUp()
- end
- else
- if turtle.detectDown() then
- if turtle.compareDown() then
- return
- else
- turtle.digDown()
- end
- end
- if ensureMaterial() then
- turtle.placeDown()
- end
- end
- end
- function checkFuel()
- if currentFuelLevel == "unlimited" then
- return true
- end
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel() then
- areaCovered = targetArea
- missionMessage = "Mission aborted due to low fuel."
- abort = true
- return false
- end
- end
- return true
- end
- currentSlot = 1
- function tryToGoDown()
- success = false
- if bedrockReached then
- -- print("tryToGoDown: No can do. Already at bedrock.")
- return success
- end
- if lowestLevelReached then
- -- print("tryToGoDown: Already at lowest level. Duh.")
- return success
- end
- -- print("Trying to go down a level...")
- if y == lowestLevel then
- lowestLevelReached = true
- print(" but y is ", y, " and lowestLevel is ", lowestLevel)
- print("tryToGoDown: lowest level reached at ", y, "/", lowestLevel)
- return success
- end
- if turtle.detectDown() then
- if not turtle.digDown() then
- bedrockReached = true
- print("tryToGoDown: bedrock reached at y = ", y)
- else
- turtle.down()
- success = true
- y = y - 1
- end
- else
- turtle.down()
- success = true
- y = y - 1
- end
- return success
- end
- -- Try for 3 levels down, but 2 will be considered successful
- function goToNextLevel()
- success = false
- if tryToGoDown() == success then -- We need to go down at least 2 blocks
- if tryToGoDown() == success then -- 2nd level down counts as success
- success = true
- tryToGoDown() -- 3rd level down is ideal, but not mandatory
- end
- end
- return success
- end
- function dig()
- if not turtle.detect() then
- turtle.select(fillerSlot)
- turtle.place()
- end
- turtle.dig()
- turtle.select(1)
- end
- function digUp()
- if not turtle.detectUp() then
- turtle.select(fillerSlot)
- turtle.placeUp()
- end
- turtle.digUp()
- turtle.select(1)
- end
- function digDown()
- if not turtle.detectDown() then
- turtle.select(fillerSlot)
- turtle.placeDown()
- end
- turtle.select(1)
- return turtle.digDown()
- end
- function digRow(nBlocks)
- -- print("digRow nBlocks: ", nBlocks)
- for i=1,nBlocks do
- digUp()
- if not digDown() then
- if not bedrockReached then
- bedrockReached = true
- -- print("digRow: bedrock reached at y = ", y)
- end
- end
- areaMined = areaMined + 1
- -- print(" areaMined = ", areaMined)
- if i < nBlocks then
- forward()
- end
- end
- if isFull() then
- purge()
- consolidate()
- end
- end
- function layRow(nBlocks)
- turtle.select(materialSlot)
- for i=1,nBlocks do
- if ensureMaterial() then
- turtle.placeDown()
- turtle.forward()
- else
- if ensureMaterial() then
- turtle.placeDown()
- turtle.forward()
- else
- print("Can't find more material")
- return
- end
- end
- end
- end
- function mineArea()
- areaMined = 0
- length = orgLength
- width = orgWidth
- turtle.select(1)
- -- while areaMined < targetArea do
- while length >= 1 and width >= 1 do
- setFace(0)
- digRow(length)
- setFace(1)
- forward()
- digRow(width - 1)
- setFace(2)
- forward()
- digRow(length - 1)
- setFace(3)
- digRow(width - 1)
- left()
- forward()
- -- print("Finished area?")
- turtle.digUp()
- turtle.digDown()
- length = length - 2
- width = width - 2
- end
- lowestLevelMined = y
- -- print("AREA IS MINED!")
- end
- function dump()
- --[[
- for i = 1,4 do
- turtle.select(i)
- count = turtle.getItemCount(i)
- if count > 1 then
- turtle.drop(count - 1)
- end
- end
- --]]
- for i=5,16 do
- turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- turtle.drop()
- end
- end
- end
- function offload()
- saveY = y
- while y < 0 do
- turtle.up()
- y = y+1
- end
- turtle.turnLeft()
- turtle.turnLeft()
- if turtle.detect() then
- dump()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- while y > saveY do
- turtle.down()
- y = y-1
- end
- end
- function displayTime(time)
- --print("time = ", time)
- rawSeconds = math.floor(time)
- rawMinutes = math.floor(rawSeconds/60)
- hours = math.floor(rawMinutes/60)
- seconds = rawSeconds - (rawMinutes * 60)
- minutes = rawMinutes - (hours * 60)
- -- print("Raw: ", hours, "h ", rawMinutes, "m ", rawSeconds, "s")
- -- print(hours, "h ", minutes, "m ", seconds, "s")
- timeString = "Elapsed time: "
- hStr = " hrs "
- mStr = " mins "
- sStr = " secs"
- if hours == 1 then hStr = " hr " end
- if minutes == 1 then mStr = " min " end
- if seconds == 1 then sStr = " sec " end
- if hours > 0 then
- timeString = timeString .. hours .. hStr
- end
- if minutes > 0 then
- timeString = timeString .. minutes .. mStr
- end
- if seconds > 0 then
- timeString = timeString .. seconds .. sStr
- end
- print(timeString)
- end
- function thisLevelNeedsMining()
- need = false
- if lowestLevelMined == 0 then
- -- print("thisLevelNeedsMining: lowestLevelMined is zero.")
- return true
- end
- if lowestLevelReached or bedrockReached then
- -- print("thisLevelNeedsMining: lowestLevelReach ", lowestLevelReached, " bedrockRockReached ", bedrockReached)
- if y ~= lowestLevelMined and (y ~= lowestLevelMined - 1) then
- -- print("thisLevelNeedsMining: y ", y, " lowestLevelMined ", lowestLevelMined)
- need = true
- end
- else
- need = true
- end
- -- print("thislevelneedsmining: need")
- return need
- end
- -- MAIN PROGRAM
- turtle.select(1)
- startFuel = turtle.getFuelLevel()
- --print("fuelLevel = ", startFuel)
- --print("length = ", length)
- --print("width = ", width)
- --print("face = ", face)
- --print("Current Fuel Level: ", currentFuelLevel)
- if currentFuelLevel ~= "unlimited" then
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel() then
- print("No fuel")
- return
- end
- end
- end
- if depth == 1 then end
- if depth == 2 then down() end
- if depth == 3 then down() down() end
- notFinished = true
- depth = -depth
- lowestLevel = depth + 1 -- lowest level the turtle needs to go
- startTime = os.clock()
- down()
- down()
- while thisLevelNeedsMining() do
- setFace(0)
- mineArea()
- -- print("Loop: lowestLevelReached: ", lowestLevelReached, " bedrockReached: ", bedrockReached, " lowestLevelMined: ", lowestLevelMined)
- -- print("Heading home to level ", y)
- home(y)
- -- print(" Made it. Now at level ", y)
- purge()
- consolidate()
- if emptySlots() <= 3 then
- offload()
- end
- if (not bedrockReached) and (not lowestLevelReached) then
- -- if y > lowestLevelMined then
- goToNextLevel()
- else
- break
- end
- end
- --print("Break out: y ", y, " lowestLevelMined ", lowestLevelMined, " bedrockReached ", bedrockReached)
- --finalApproach = true
- home(0)
- setFace(0)
- offload()
- print(missionMessage)
- --print("lowestLevelReached = ", lowestLevelReached, ", bedrockReached = ", bedrockReached)
- endTime = os.clock()
- elapsedTime = endTime - startTime
- endFuel = turtle.getFuelLevel()
- usedFuel = startFuel - endFuel
- displayTime(elapsedTime)
- print("Fuel used: ", usedFuel, " units")
- print(" Current fuel level is ", turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement