Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- clear
- -- Mines a clear space to the dimensions supplied by the user
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, March 2014 Last update: May 22, 2014
- args = {...}
- nArgs = #args
- print("clear: Rev 6.0")
- x = 0
- y = 0
- z = 0
- gX = x
- gZ = z
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- lowestLevel = 0
- highestLevel = 0
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print("Clear an area specified by the given length, width, and height")
- print("Usage: clear <length><width><height>")
- return
- end
- if nArgs ~= 3 then
- print("Usage: clear <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
- --]]
- if height > 0 then
- highestLevel = height - 1
- lowestLevel = 0
- elseif height < 0 then
- highestLevel = 0
- lowestLevel = 0 + height
- else
- highestLevel = 0
- lowestLevel = 0
- end
- print("highestLevel = ", highestLevel, ", lowestLevel = ", lowestLevel)
- targetArea = length * width
- areaCovered = 1;
- function chomp()
- -- if y < highestLevel and turtle.detectUp() then
- if y < top then
- turtle.digUp()
- end
- -- if turtle.detect() then
- -- turtle.dig()
- -- end
- -- if y > 0 then
- if y > bottom then
- if turtle.detectDown() then
- turtle.digDown()
- end
- elseif y < 0 then
- if turtle.detectUp() then
- turtle.digUp()
- end
- 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
- 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
- turtle.digDown()
- end
- turtle.down()
- y = y-1
- end
- function turn()
- if direction == "left" then
- left()
- else
- right()
- end
- if areaCovered < targetArea then
- forward()
- end
- -- chomp()
- if direction == "left" then
- left()
- direction = "right"
- else
- right()
- direction = "left"
- end
- end
- function forward()
- while turtle.detect() do -- This loop added in case of falling sand or whatever
- turtle.dig()
- -- chomp()
- end
- for i=1,10 do
- if turtle.forward() then
- chomp()
- 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)
- -- print("home: y = ", y, ", targetY = ", targetY)
- if (targetY == 0) then
- if y > 0 then
- while y > 0 do
- down()
- end
- elseif y < 0 then
- while y < 0 do
- up()
- end
- end
- end
- end
- function xzHome()
- local X = gX
- local Z = gZ
- if x ~= X then
- if x < X then
- setFace(east)
- while x < X do
- forward()
- end
- else
- setFace(west)
- while x > X do
- forward()
- end
- end
- end
- if z ~= Z then
- if z < Z then
- setFace(north)
- while z < Z do
- forward()
- end
- else
- setFace(south)
- while z > Z do
- forward()
- end
- end
- end
- end
- function strip(nBlocks)
- local i
- print("strip ", nBlocks)
- for i=1,nBlocks do
- checkFuel()
- if abort then
- areaCovered = targetArea
- end
- -- print("strip: Cleared ", areaCovered, " out of ", targetArea, " blocks")
- if areaCovered <= targetArea then
- forward()
- 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
- function goToLevel(level)
- if y < level then
- while y < level do
- up()
- end
- elseif y > level then
- while y > level do
- down()
- end
- end
- end
- function nextLevel()
- if y > 3 then
- return y - 3
- else
- return 0
- end
- end
- function goToNextLevel()
- if height > 0 then
- if y > 3 then
- goToLevel(y - 3)
- else
- goToLevel(bottom)
- end
- elseif height < 0 then
- if y >= bottom + 3 then
- goToLevel(y - 3)
- else
- goToLevel(bottom)
- end
- end
- end
- -- MAIN PROGRAM
- currentLevel = y
- turtle.select(1)
- print("fuelLevel = ", currentFuelLevel)
- print("length = ", length)
- print("width = ", width)
- print("height = ", height)
- 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 height > 0 then
- top = height - 1
- bottom = 0
- if height > 2 then
- topLevel = height - 2
- else
- topLevel = 0
- end
- if height % 3 == 0 then
- lowestLevel = 1
- else
- lowestLevel = 0
- end
- elseif height < 0 then
- top = 0
- bottom = height + 1
- if height < -2 then
- topLevel = -2
- else
- topLevel = 0
- end
- if height % 3 == 0 then
- lowestLevel = -1
- else
- lowestLevel = height + 1
- end
- end
- print("top = ", top, ", bottom = ", bottom)
- print("topLevel = ", topLevel, ", lowestLevel = ", lowestLevel)
- goToLevel(topLevel)
- done = false
- chomp()
- while not done do
- for w=1,width do
- if w == width then
- strip(length)
- else
- strip(length-1)
- end
- -- print("strip: Cleared ", areaCovered, " out of ", targetArea, " blocks")
- if areaCovered < targetArea then
- turn()
- end
- if abort then
- print("Aborting...")
- home(0)
- end
- end
- home(99)
- direction = "left"
- areaCovered = 1;
- print("y = ", y, ", lowestLevel = ", lowestLevel)
- if height > 0 then
- if y <= lowestLevel then
- done = true
- else
- goToNextLevel()
- end
- elseif height < 0 then
- if y == lowestLevel + 1 or y == lowestLevel then
- done = true
- print("That's it. It's over.")
- else
- goToNextLevel()
- end
- end
- end
- print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
- home(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement