Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- quarry
- -- Mines a quarry to the dimensions supplied by the user
- -- Turtle drops the spoils on coveyer belts by 3 types
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, April 2014
- args = {...}
- nArgs = #args
- print("quarry: Rev 3.0")
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- notDone = true
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- rockBottom = 0
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- -- Status data for marking a spot to return to after going 'home'
- markX = 0
- markY = 0
- markZ = 0
- markFace = 0
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print("Quarry an area specified by the given length, width, and height")
- print("Usage: Quarry <length> <width> [height]")
- return
- end
- if nArgs ~= 3 then
- print("Usage: Quarry <length> <width> [height]")
- 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
- 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
- height = tonumber(args[3])
- if height == nil then
- print("\"", args[3], "\" is not a valid height")
- return
- end
- if height < 1 then
- print("height must be a positive number greater than zero")
- end
- targetArea = length * width
- areaCovered = 1;
- function chomp()
- while turtle.detect() do
- turtle.dig()
- end
- areaCovered = areaCovered+1
- end
- direction = "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
- while not turtle.up() do
- turtle.attackUp()
- sleep(2)
- end
- 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
- print("Unable to dig down. Assuming bedrock")
- rockBottom = y
- return
- end
- end
- while not turtle.down() do
- turtle.attackDown()
- sleep(2)
- end
- y = y-1
- end
- function turn()
- if direction == "left" then
- left()
- else
- right()
- end
- -- chomp()
- forward(true)
- if direction == "left" then
- left()
- direction = "right"
- else
- right()
- direction = "left"
- end
- end
- function startNextRow()
- if direction == "left" then
- left()
- else
- right()
- end
- forward(true)
- turtle.digUp()
- turtle.digDown()
- if direction == "left" then
- left()
- direction = "right"
- else
- right()
- direction = "left"
- end
- end
- function forward(keepTrackOfArea)
- while turtle.detect() do -- This loop added in case of falling sand or whatever
- turtle.dig()
- end
- for i=1,10 do
- if turtle.forward() then
- if keepTrackOfArea then
- areaCovered = areaCovered + 1
- end
- break
- end
- turtle.attack()
- sleep(2)
- end
- 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 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
- setFace(0)
- if (targetY == 0) then
- while y < 0 do
- up()
- end
- end
- end
- function strip(nBlocks)
- local i
- for i=1,nBlocks do
- forward(true)
- turtle.digUp()
- turtle.digDown()
- 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
- cobbleSlot = 2
- dirtSlot = 1
- dirtConveyor = -4
- cobbleConveyor = -3
- oresConveyor = -2
- dumpLevel = 1
- function goEmptyInventoryToChestAndReturn()
- saveAreaCovered = areaCovered
- markX = x
- markY = y
- markZ = z
- markFace = face
- home()
- setFace(east)
- while y > 0 do
- down()
- end
- dump()
- while y < markY do
- up()
- end
- returnToMark()
- areaCovered = saveAreaCovered
- end
- function returnToMark()
- if markZ < z then
- setFace(south)
- while z > markZ do
- forward(false)
- end
- else
- if markZ > z then
- setFace(north)
- while z < markZ do
- forward(false)
- end
- end
- end
- if markX < 0 then
- setFace(west)
- while x > markX do
- forward(false)
- end
- else
- if markX > 0 then
- setFace(east)
- while x < markX do
- forward(false)
- end
- end
- end
- while y > markY do
- down()
- end
- setFace(markFace)
- end
- function dumpItemType(typeSlot)
- if typeSlot > 0 then
- turtle.select(typeSlot)
- dropCount = turtle.getItemCount(typeSlot)
- if dropCount > 1 then
- turtle.dropDown(dropCount - 1)
- end
- for i = 3,16 do
- turtle.select(i)
- if turtle.compareTo(typeSlot) then
- turtle.dropDown()
- end
- end
- else
- for i = 3,16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.dropDown()
- end
- end
- end
- end
- function dumpAll()
- saveAreaCovered = areaCovered
- markX = x
- markY = y
- markZ = z
- markFace = face
- setFace (south)
- while y < dumpLevel do
- up()
- end
- while z > dirtConveyor do
- forward(false)
- end
- dumpItemType(dirtSlot)
- setFace(north)
- forward(false)
- dumpItemType(cobbleSlot)
- forward(false)
- dumpItemType(0)
- turtle.select(dirtSlot)
- returnToMark()
- end
- currentLevel = 255
- -- MAIN PROGRAM
- print("fuelLevel = ", currentFuelLevel)
- print("length = ", length)
- print("width = ", width)
- print("height = ", height)
- print("face = ", face)
- print("Current Fuel Level: ", currentFuelLevel)
- while notDone do
- dumpLevel = 1
- turtle.select(1)
- if currentFuelLevel ~= "unlimited" then
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel() then
- print("No fuel")
- return
- end
- end
- end
- if currentLevel == rockBottom then
- missionMessage = "Hit rock bottom. "
- break
- end
- down()
- down()
- turtle.digDown()
- for w=1,width do
- strip(length-1)
- if z == 0 and x ~= 0 then
- currentLevel = y
- dumpAll()
- while y > currentLevel do
- down()
- end
- end
- if areaCovered < targetArea then
- startNextRow()
- end
- if abort then
- home(0)
- end
- end
- home(99)
- down()
- areaCovered = 1
- direction = "left"
- end
- print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
- home(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement