Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- cactus
- -- Harvests the top 2 blocks of cacti from the top
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, April 2014
- args = {...}
- nArgs = #args
- print("cactus: Rev 1.2")
- x = 0
- y = 0
- z = 0
- face = 0
- cactusSlot = 1
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print("length, width, and height")
- print("Usage: cactus <length> <width>")
- return
- end
- if nArgs ~= 2 then
- print("Usage: clear <length> <width>")
- 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
- targetArea = length * width
- areaCovered = 1;
- function chomp()
- turtle.select(cactusSlot)
- if turtle.compareDown() then
- turtle.digDown()
- 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
- chomp()
- forward()
- 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()
- end
- for i=1,10 do
- if turtle.forward() then
- 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 harvest(nBlocks)
- local i
- for i=1,nBlocks do
- chomp()
- checkFuel()
- if abort then
- areaCovered = targetArea
- end
- 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
- -- MAIN PROGRAM
- turtle.select(1)
- print("fuelLevel = ", currentFuelLevel)
- print("length = ", length)
- print("width = ", width)
- up()
- up()
- up()
- height = 2
- for d=1,height do
- for w=1,width do
- harvest(length-1)
- if areaCovered < targetArea then
- turn()
- end
- if abort then
- home(0)
- end
- end
- home(99)
- if d < height then
- down()
- direction = "left"
- areaCovered = 1;
- end
- end
- print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
- home(0)
- down()
- down()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement