Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- chute
- -- Creates a ladder chute to the height or depth specified
- -- If supplied, ladder is placed
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- print("chute: Rev 1.1")
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- minLevel = 6
- missionMessage = "Mission complete."
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- patchSlot = 1
- ladderSlot = 16
- goingUp = false
- useLadders = true
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print("Creates a ladder chute straight up or down.")
- print("This program assumes turtle is pre-fueled.")
- print("Usage: chute <height> <useLadders>")
- print("Place ladder in slot ", ladderSlot, ".")
- return
- end
- if nArgs ~= 2 then
- print("Usage: chute <height> <useLadders>")
- return
- end
- targetDepth = tonumber(args[1])
- if targetDepth == nil then
- print("\"", args[1], "\" is not a valid depth")
- return
- end
- if args[2] == "true" then
- useLadders = true
- elseif args[2] == "false" then
- useLadders = false
- else
- print("Second argument must be true or false")
- return
- end
- if (targetDepth < 0) then
- targetDepth = targetDepth + 1
- goingUp = true
- end
- depth = 0
- 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 patchWalls()
- if turtle.getItemCount(patchSlot) < 4 then
- gatherPatchMaterial()
- end
- turtle.select(patchSlot)
- left()
- turtle.place()
- right()
- turtle.place()
- right()
- turtle.place()
- left()
- end
- function up()
- attackCount = 0
- while turtle.detectUp() do -- This loop added in case of falling sand or whatever
- turtle.digUp()
- end
- for i=1,10 do -- This loop tries to handle pests (mob) that might be in the way
- if turtle.up() then
- break
- end
- sleep(2)
- end
- if attackCount > 1 then
- turtle.attackUp()
- end
- gatherPatchMaterial()
- patchWalls()
- setFace(north)
- depth = depth-1
- y = y+1
- end
- function down()
- attackCount = 0
- if depth >= targetDepth then
- print("Hit target depth")
- return
- end
- if turtle.detectDown() then
- turtle.digDown()
- end
- for i=1,10 do -- This loop trys to handle pests (mobs) that might be in the way
- if turtle.down() then
- break
- end
- turtle.attackDown()
- attackCount = attackCount + 1
- sleep(2)
- end
- if attackCount > 1 then
- turtle.suckDown()
- turtle.suckUp()
- end
- gatherPatchMaterial()
- turtle.select(patchSlot)
- if not turtle.detect() then
- turtle.place()
- end
- turtle.turnLeft()
- if not turtle.detect() then
- turtle.place()
- end
- turtle.turnLeft()
- if not turtle.detect() then
- turtle.place()
- end
- turtle.turnLeft()
- if not turtle.detect() then
- turtle.place()
- end
- turtle.turnLeft()
- depth = depth+1
- y = y-1
- end
- function rise(nBlocks)
- local i
- for i=1,nBlocks do
- turtle.up()
- y = y+1
- end
- end
- function gatherPatchMaterial()
- local i
- patchCount = turtle.getItemCount(patchSlot)
- if patchCount < 5 then
- -- print("Attempting to refill slot ", patchSlot)
- for i=2,13 do
- turtle.select(i)
- if turtle.compareTo(patchSlot) then
- turtle.transferTo(patchSlot, 64-patchCount)
- -- print("Transferred ", 64 - patchCount, " cobble to slot ", patchSlot)
- end
- end
- end
- turtle.select(patchSlot)
- 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
- moreLadder = true
- function placeLadder()
- if not useLadders then
- return
- end
- if turtle.getItemCount(ladderSlot) > 0 then
- turtle.select(ladderSlot)
- elseif turtle.getItemCount(ladderSlot-1) > 0 then
- turtle.select(ladderSlot-1)
- elseif turtle.getItemCount(ladderSlot-2) > 0 then
- turtle.select(ladderSlot-2)
- else
- moreLadder = false
- end
- -- print("placeLadder: Ladder count = ", turtle.getItemCount(ladderSlot), " moreLadder = ", moreLadder)
- if moreLadder then
- turtle.place()
- end
- end
- function placeLadderUp()
- if turtle.getItemCount(ladderSlot) < 1 then
- ladderSlot = ladderSlot - 1
- if turtle.getItemCount(ladderSlot) < 1 then
- ladderSlot = ladderSlot - 1
- if turtle.getItemCount(ladderSlot) < 1 then
- ladderSlot = ladderSlot - 1
- if turtle.getItemCount(ladderSlot) < 1 then
- end
- end
- end
- end
- turtle.select(ladderSlot)
- turtle.placeUp()
- end
- function homeUp()
- setFace(north)
- turtle.select(ladderSlot)
- turtle.suckUp() -- in case we have monster guts on top of us
- while y < 0 do
- turtle.up()
- if not turtle.detectDown() then
- turtle.placeDown()
- end
- y = y+1
- end
- end
- function ladderDown()
- setFace(north)
- turtle.back()
- z = z - 1
- firstLadder = true
- while y >= 0 do
- placeLadder()
- if turtle.getItemCount(patchSlot) < 4 then
- gatherPatchMaterial()
- end
- if (not firstLadder and y ~= 0) then
- turtle.select(patchSlot)
- turtle.placeUp()
- end
- firstLadder = false
- if y > 0 then
- turtle.down()
- y = y - 1
- else
- break
- end
- end
- end
- function homeDown()
- setFace(north)
- turtle.select(ladderSlot)
- turtle.suckDown() -- in case we have monster guts on top of us
- while y > 0 do
- turtle.down()
- if not turtle.detectDown() then
- placeLadderUp()
- end
- y = y-1
- end
- end
- -- Main program loop
- if goingUp then
- patchWalls()
- while depth > targetDepth do
- up()
- end
- ladderDown()
- else
- while depth < targetDepth do
- down()
- end
- homeUp()
- end
- setFace(north)
- print(missionMessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement