Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *** addroof
- local function forward(nBlocks)
- for i=1,nBlocks do
- turtle.forward()
- end
- end
- turtle.up()
- forward(6)
- turtle.turnRight()
- forward(6)
- turtle.turnRight()
- shell.run("pattern roof10x10")
- forward(6)
- turtle.turnRight()
- forward(6)
- turtle.turnRight()
- *** b
- -- b
- -- Moves the turtle the specified distance backwards
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- distance = 1;
- if (nArgs == 1 and args[1]== "help") then
- print("Moves the turtle the specified distance backwards")
- print("Usage: f [distance]")
- return
- end
- if nArgs == 1 then
- distance = tonumber(args[1])
- if distance == nil then
- print(args[1], " is not a valid distance")
- return
- end
- end
- -- MAIN PROGRAM
- turtle.select(1)
- if currentFuelLevel ~= "unlimited" then
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel() then
- print("No fuel")
- return
- end
- end
- end
- for i=1,distance do
- turtle.back()
- end
- *** bivalve
- -- valve
- -- Pours and retrieves water at specified time intervals
- -- Written by HarvDad, June 2014
- args = {...}
- nArgs = #args
- version = "valve: Rev 1.0"
- mission = "Pour and retrieve water at intervals."
- instructions = "Place full bucket in slot 1, empty bucket in slot 2."
- usage1 = "usage: valve <direction> <t1> <t2>"
- usage2 = " direction = up, down, forward"
- usage3 = " t1 = seconds after pour"
- usage4 = " t2 = seconds after retrieval"
- secondsAfterPouring = 10
- secondsAfterRetrieving = 30
- up = 0
- down = 1
- forward = 2
- direction = 0
- abort = false
- function synchWithServerTime()
- print("Waiting for synch...")
- while true do
- time = os.time()
- timeStr = tostring(time)
- tailStr = string.sub(timeStr, -3)
- tail = tonumber(tailStr)
- if tail > 995 then
- break
- end
- sleep(0)
- end
- end
- function needWater(direction)
- if direction == up then
- turtle.select(1)
- if turtle.compareTo(3) then
- return true
- end
- elseif direction == down then
- turtle.select(2)
- if turtle.compareTo(3) then
- return true
- end
- end
- return false
- end
- -- prep: Retrieve any water possibly left over from previous run
- function prep()
- turtle.select(1)
- if turtle.compareTo(3) then
- if needWater(up) then
- turtle.placeUp()
- end
- if needWater(down) then
- turtle.placeDown()
- end
- end
- end
- function updateWater()
- turtle.select(1)
- turtle.placeUp()
- turtle.select(2)
- turtle.placeDown()
- end
- function doTheRightThing()
- local naptime = 0
- turtle.select(1)
- if turtle.compareTo(3) then
- napTime = secondsAfterRetrieving
- -- print("Retrieving water. napTime = ", napTime)
- else
- napTime = secondsAfterPouring
- -- print("Pouring water. napTime = ", napTime)
- end
- updateWater()
- return napTime
- end
- -- Main program
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(instructions)
- print(usage1)
- print(usage2)
- print(usage3)
- print(usage4)
- return
- end
- if nArgs < 2 or nArgs > 2 then
- print(usage1)
- print(usage2)
- print(usage3)
- print(usage4)
- return
- end
- secondsAfterPouring = tonumber(args[1])
- if secondsAfterPouring == nil then
- print("\"", args[1], "\" is not a valid time interval")
- return
- end
- if secondsAfterPouring < 1 then
- print("t1 must be a positive integer")
- return
- end
- secondsAfterRetrieving= tonumber(args[2])
- if secondsAfterRetrieving == nil then
- print("\"", args[2], "\" is not a valid time interval")
- return
- end
- if secondsAfterRetrieving < 1 then
- print("t2 must be a positive integer")
- return
- end
- if turtle.getItemCount(1) == 0 or turtle.getItemCount(2) == 0 then
- print("If no water running yet,")
- print(" place full bucket in slot 1.")
- print("If water is already running,")
- print(" place empty bucket in slot 1.")
- print("Always have empty bucket in slot 2 as comparitor.")
- return
- end
- prep()
- --synchWithServerTime()
- -- Main loop
- print("Beginning loop...")
- print("secondsAfterPouring: ", secondsAfterPouring, " secondsAfterRetrieving: ", secondsAfterRetrieving)
- while true do
- if abort then
- break
- end
- napTime = doTheRightThing()
- sleep(napTime)
- end
- *** branch
- -- branch
- -- Creates a 1x1x3 tunnel of the length specified by the user
- -- then mines 3 blocks to the right and tunnels back
- -- Torches, if supplied, will be placed on the right-hand wall at 8 block intervals
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- print("branch: Rev 1.3")
- loop = 0;
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print("Creates a 2x2 tunnel with sealed sides.")
- print("Place fuel in slot 1")
- print("Place patch material (like cobblestone) in slot 2.")
- print("If torches are desired, place torches in slot 16.")
- print("Usage: branch <length>")
- return
- end
- if nArgs ~= 1 then
- print("Usage: branch <length>")
- 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
- targetArea = length
- areaCovered = 1;
- 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()
- success = false
- repeat
- while turtle.detectUp() do -- This loop added in case of falling sand or whatever
- turtle.digUp()
- end
- success = turtle.up()
- until success
- y = y+1
- end
- function rise(nBlocks)
- local i
- for i=1,nBlocks do
- up()
- end
- end
- function descend(nBlocks)
- local i
- for i=1,nBlocks do
- down()
- end
- end
- function down()
- if turtle.detectDown() then
- turtle.digDown()
- end
- turtle.down()
- y = y-1
- 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 patch()
- turtle.select(2)
- turtle.place()
- end
- function patchUp()
- turtle.select(2)
- turtle.placeUp()
- end
- function patchDown()
- turtle.select(2)
- turtle.placeDown()
- end
- addTorch = true
- torchSpacing = 8
- torchSpot = 6
- function mineForward()
- forward()
- while turtle.detectUp() do -- This loop added in case of falling sand or whatever
- turtle.digUp()
- -- turtle.suckUp()
- end
- if turtle.detectDown() then
- turtle.digDown()
- -- turtle.suckDown()
- end
- if addTorch then
- if (torchSpot % torchSpacing) == 0 then
- left()
- left()
- turtle.select(16)
- turtle.place()
- right()
- right()
- end
- end
- torchSpot = torchSpot + 1
- 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 checkFuel()
- if currentFuelLevel == "unlimited" then
- return true
- end
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel(200) then
- areaCovered = targetArea
- missionMessage = "Mission aborted due to low fuel."
- abort = true
- return false
- end
- end
- return true
- 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
- end
- function dump()
- for i=2,14 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- function branchLoop()
- torchSpot = 6
- local i
- for i=1,length do
- mineForward()
- end
- right()
- for i=1,3 do
- mineForward()
- end
- right()
- for i=1,length do
- mineForward()
- end
- setFace(0)
- down()
- if turtle.detectDown() then
- turtle.select(15)
- if not turtle.compareDown() then
- turtle.digDown()
- -- turtle.suckDown()
- turtle.placeDown()
- end
- end
- if turtle.compareDown() then
- dump()
- end
- end
- -- MAIN PROGRAM
- turtle.select(1)
- 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
- for loop=1,5 do
- addTorch = false
- rise(1)
- for i=1,5 do
- mineForward()
- end
- turtle.back()
- turtle.back()
- left()
- addTorch = true
- branchLoop()
- end
- print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
- *** build
- -- build: Build predifined structures
- -- Rev 2.1
- args = {...}
- nArgs = #args
- version = "build: Rev 2.1"
- usage = "Usage: build <structure name>"
- -- Locations
- origin = {0, 0, 0}
- center = {0, 0, 15}
- -- length, width, xOffset, zOffset, yOffset
- base = {15, 15, 0, 0, 150}
- tower = {5, 5, 0, 0, 150}
- ladderChute = {1, 1, 0, -8, 0}
- drainFloor = {15, 15, 0, 0, 190}
- conveyors = {3, 3, 0, -1, 190}
- quicksand = {13, 13, 0, 0, 193}
- chamber = {15, 15, 0, 0, 190}
- roof = {15, 15, 0, 0, 198}
- -- Global Origin
- gX = 0
- gY = 0
- gZ = 0
- -- Base Origin
- bX = 5
- bY = 0
- bZ = 5
- -- Drain Origin
- dX = 0
- dY = 0
- dZ = 0
- -- Tower Origin
- tX = 0
- tY = 0
- tZ = 0
- -- Cobble Chest
- ccX = 0
- ccY = 0
- ccZ = 0
- -- Quicksand Chest
- qcX = -2
- qcY = 0
- qcZ = 0
- -- Slab Chest
- scX = -2
- scY = 0
- scZ = 0
- -- Conveyor Chest
- cvcX = -3
- cvcY = 0
- cvcZ = 0
- -- Ladder Chest
- lcX = -4
- lcY = 0
- lcZ = 0
- -- Torch Chest
- tcX = -5
- tcY = 0
- tcZ = 0
- x = gX
- y = gY
- z = gZ
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- function goToStartPt(data)
- length = data[1]
- width = data[2]
- xOffset = data[3]
- zOffset = data[4]
- yOffset = data[5]
- print("l: ", length, ", w: ", width)
- if width > 1 then
- targetX = center[1] + ((width - 1) / 2) + xOffset
- else
- targetX = center[1] + xOffset
- end
- if length > 1 then
- targetZ = center[3] - ((length - 1) / 2) + zOffset
- else
- targetZ = center[3] + zOffset
- end
- targetY = center[2] + yOffset
- print("xOff: ", xOffset, ", zOff: ", zOffset, ", yOff: ", yOffset)
- print("center[2]: ", center[2], " tX: ", targetX, ", tY: ", targetY, ", tZ: ", targetZ)
- moveTo(targetX, targetY, targetZ)
- end
- function goToStructure(structure)
- local x = 0
- local y = 0
- local z = 0
- if structure == "origin" then
- x = origin[1]
- y = origin[2]
- z = origin[3]
- elseif structure == "tower" then
- x = tower[1]
- y = tower[2]
- z = tower[3]
- elseif structure == "base" then
- x = base[1]
- y = base[2]
- z = base[3]
- elseif structure == "ladderChute" then
- x = ladderChute[1]
- y = ladderChute[2]
- z = ladderChute[3]
- elseif structure == "drainFloor" then
- x = drainFloor[1]
- y = drainFloor[2]
- z = drainFloor[3]
- elseif structure == "conveyors" then
- x = conveyors[1]
- y = conveyors[2]
- z = conveyors[3]
- elseif structure == "quicksand" then
- x = quicksand[1]
- y = quicksand[2]
- z = quicksand[3]
- elseif structure == "chamber" then
- x = chamber[1]
- y = chamber[2]
- z = chamber[3]
- elseif structure == "roof" then
- x = roof[1]
- y = roof[2]
- z = roof[3]
- end
- moveTo(x, y, z)
- end
- function moveUp()
- for i=1,10 do -- Keep trying in case something is temporarily in the way
- if turtle.up() then
- y = y + 1
- break
- else
- sleep(2)
- end
- end
- end
- function moveDown()
- for i=1,10 do -- Keep trying in case something is temporarily in the way
- if turtle.down() then
- y = y - 1
- break
- else
- sleep(2)
- end
- end
- end
- function moveTo(X, Y, Z)
- print("moveTo(", X, ", ", Y, ", ", Z, ") y: ", y)
- if y == nil then print("y is nil") end
- if Y == nil then print("Y is nil") end
- if y ~= Y then
- if y < Y then
- while y < Y do
- moveUp()
- end
- else
- while y > Y do
- moveDown()
- end
- end
- end
- if x ~= X then
- if x < X then
- setFace(east)
- while x < X do
- go(1)
- end
- else
- setFace(west)
- while x > X do
- go(1)
- end
- end
- end
- if z ~= Z then
- if z < Z then
- setFace(north)
- while z < Z do
- go(1)
- end
- else
- setFace(south)
- while z > Z do
- go(1)
- end
- end
- end
- end
- function goToChest(chest)
- if chest == cobbleChest then
- moveTo(ccX, ccY, ccZ)
- elseif chest == quicksandChest then
- moveTo(qcX, qcY, qcZ)
- elseif chest == slabChest then
- moveTo(scX, scY, scZ)
- elseif chest == conveyorChest then
- moveTo(cvcX, cvcY, cvcZ)
- elseif chest == ladderChest then
- moveTo(lcX, lcY, lcZ)
- elseif chest == torchChest then
- moveTo(tcX, tcY, tcZ)
- 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 back(nBlocks)
- local failed = true
- for i=1,nBlocks do
- for j=1,10 do -- multiple tries in case something is temporarily in the way
- if turtle.back() then
- failed = false
- break
- else
- sleep(2)
- end
- end
- end
- if face == north then z = z - nBlocks end
- if face == south then z = z + nBlocks end
- if face == east then x = x - nBlocks end
- if face == west then x = x + nBlocks end
- end
- function go(nBlocks)
- local failed = true
- for i=1,nBlocks do
- for j=1,10 do -- multiple tries in case something is temporarily in the way
- if turtle.forward() then
- failed = false
- break
- else
- sleep(2)
- end
- end
- end
- if failed then
- return
- end
- if face == north then z = z + nBlocks end
- if face == south then z = z - nBlocks end
- if face == east then x = x + nBlocks end
- if face == west then x = x - nBlocks 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 globalHome()
- print("globalHome...")
- moveTo(gX, gY, gZ)
- setFace(north)
- end
- function xzHome()
- local X = gX
- local Z = gZ
- if x ~= X then
- if x < X then
- setFace(east)
- while x < X do
- go(1)
- end
- else
- setFace(west)
- while x > X do
- go(1)
- end
- end
- end
- if z ~= Z then
- if z < Z then
- setFace(north)
- while z < Z do
- go(1)
- end
- else
- setFace(south)
- while z > Z do
- go(1)
- end
- end
- end
- end
- function zxHome()
- zHome()
- xHome()
- end
- function xHome()
- local X = gX
- if x ~= X then
- if x < X then
- setFace(east)
- while x < X do
- go(1)
- end
- else
- setFace(west)
- while x > X do
- go(1)
- end
- end
- end
- end
- function zHome()
- local Z = gZ
- if z ~= Z then
- if z < Z then
- setFace(north)
- while z < Z do
- go(1)
- end
- else
- setFace(south)
- while z > Z do
- go(1)
- end
- end
- end
- end
- function yHome()
- local Y = gY
- if y ~= Y then
- if y < Y then
- while y < Y do
- turtle.up()
- y = y + 1
- end
- else
- while y > Y do
- turtle.down()
- y = y - 1
- end
- end
- end
- end
- function grabStuff(stuffSlot)
- turtle.select(stuffSlot)
- turtle.suckDown()
- for i=5,16 do
- turtle.select(i)
- if not turtle.compareTo(stuffSlot) then
- turtle.dump()
- end
- turtle.suckDown()
- end
- turtle.select(stuffSlot)
- turtle.suckDown()
- end
- function dropStuff(stuffSlot)
- qty = turtle.getItemCount(stuffSlot) - 1
- turtle.dropDown(qty)
- for i=5,16 do
- turtle.select(i)
- if (turtle.compareTo(stuffSlot)) then
- turtle.dropDown()
- end
- end
- end
- function grabCobble()
- turtle.select(cobbleSlot)
- for i=5,16 do
- turtle.suckDown()
- qty = turtle.getItemCount(cobbleSlot) - 1
- turtle.transferTo(i, qty)
- end
- end
- function dropCobble()
- qty = turtle.getItemCount(cobbleSlot) - 1
- turtle.dropDown(qty)
- for i=5,16 do
- turtle.select(i)
- if (turtle.compareTo(cobbleSlot)) then
- turtle.dropDown()
- end
- end
- end
- function floorHole(length, width)
- for i = 1,width do
- for j = 1,length do
- turtle.digDown()
- if j < length then
- turtle.forward()
- if face == north then
- z = z + 1
- elseif face == south then
- z = z - 1
- end
- end
- end
- if i < width then
- if face == north then
- left()
- turtle.forward()
- x = x - 1
- left()
- elseif face == south then
- right()
- turtle.forward()
- x = x - 1
- right()
- end
- end
- end
- end
- function nextSpot()
- end
- function sprinkleTorches(length, width, interval)
- turtle.up()
- y = y + 1
- turtle.select(16)
- lengthSpan = length - (2 * interval) - 1
- widthSpan = width - (2 * interval) - 1
- left()
- for i = 1,interval do
- go(1)
- end
- right()
- for i = 1,interval do
- go(1)
- end
- turtle.placeDown()
- for i = 1,lengthSpan do
- go(1)
- end
- turtle.placeDown()
- left()
- for i = 1,widthSpan do
- go(1)
- end
- turtle.placeDown()
- left()
- for i = 1,lengthSpan do
- go(1)
- end
- turtle.placeDown()
- left()
- for i = 1,widthSpan + interval do
- go(1)
- end
- right()
- for i = 1,lengthSpan + interval do
- go(1)
- end
- turtle.down()
- y = y - 1
- end
- function sprinkleSurface(length, width, interval)
- totalPlaced = 0
- turtle.up()
- y = y + 1
- turtle.select(16)
- for i = 1,width do
- for j = 1,length do
- if j < length then
- if (i % interval == 0 and j % interval == 0) then
- turtle.placeDown()
- end
- turtle.forward()
- totalPlaced = totalPlaced + 1
- if face == north then
- z = z + 1
- elseif face == south then
- z = z - 1
- end
- end
- end
- if i < width then
- if face == north then
- left()
- while i % interval ~= 0 do
- turtle.forward()
- x = x - 1
- i = i + 1
- end
- left()
- elseif face == south then
- right()
- while i % interval ~= 0 do
- turtle.forward()
- x = x - 1
- i = i + 1
- end
- right()
- end
- end
- end
- end
- function load(chest, startSlot, endSlot)
- goToChest(chest)
- for slot=startSlot, endSlot do
- turtle.select(slot)
- turtle.suckDown()
- end
- end
- function dump(chest, startSlot, endSlot)
- goToChest(chest)
- for slot=startSlot, endSlot do
- turtle.select(slot)
- turtle.dropDown()
- end
- end
- saveX = 0
- saveY = 0
- saveZ = 0
- saveFace = 0
- function saveXYZ()
- saveX = x
- saveY = y
- saveZ = z
- saveFace = face
- print("Saved xyz as ", saveX, ", ", saveY, ", ", saveZ, ", face: ", saveFace)
- end
- function restoreXYZ()
- x = saveX
- y = saveY
- z = saveZ
- face = saveFace
- print("Restored xyz as ", x, ", ", y, ", ", z, ", face: ", face)
- end
- function run(script)
- saveXYZ()
- shell.run(script)
- restoreXYZ()
- end
- cobbleChest = 1
- quicksandChest = 2
- conveyorChest = 3
- ladderChest = 4
- torchChest = 5
- slabChest = 2
- cobbleSlot = 1
- quicksandSlot = 2
- conveyorSlot = 3
- torchSlot = 3
- --grabStuff(cobbleSlot)
- --sleep(3)
- --dropStuff(cobbleSlot)
- -- BUILD SKYSCRAPER QUICKSAND MOB TRAP
- function buildSandTrap()
- load(cobbleChest, 1, 8)
- load(torchChest, 16, 16)
- goToStartPt(base)
- run("floor 15 15")
- print("Base completed")
- setFace(north)
- sprinkleTorches(15, 15, 3)
- xzHome()
- yHome()
- dump(cobbleChest, 1, 8)
- dump(torchChest, 16, 16)
- load(cobbleChest, 1, 13)
- load(ladderChest, 14, 16)
- globalHome()
- goToStartPt(ladderChute)
- setFace(north)
- run("chute -150 true")
- z = z - 1 -- The 'chute' program finishes one block back from start
- print("Ladder chute completed")
- dump(cobbleChest, 1, 13)
- dump(ladderChest, 14, 16)
- load(cobbleChest, 1, 16)
- globalHome()
- goToStartPt(tower)
- run("walls 5 5 40")
- z = z - 1 -- The 'walls' program finishes one block back from start
- print("Tower completed")
- xzHome()
- print("xzHome completed")
- yHome()
- dump(cobbleChest, 1, 16)
- load(cobbleChest, 1, 8)
- goToStartPt(drainFloor)
- run("floor 15 15")
- left()
- for i = 1,6 do
- go(1)
- end
- right()
- for i = 1,6 do
- go(1)
- end
- floorHole(3, 3)
- xzHome()
- yHome()
- xzHome()
- dump(cobbleChest, 1, 8)
- load(conveyorChest, 1, 3)
- goToStartPt(conveyors)
- run("fabdrain")
- y = y + 1 -- The 'fabdrain' program finishes one block up from start
- xzHome()
- yHome()
- dump(conveyorChest, 1, 3)
- load(quicksandChest, 1, 3)
- goToStartPt(quicksand)
- run("floor 13 13")
- xzHome()
- yHome()
- dump(quicksandChest, 1, 3)
- load(cobbleChest, 1, 12)
- goToStartPt(chamber)
- run("walls 15 15 7")
- z = z - 1 -- The 'walls' program finishes one block back from start
- xzHome()
- yHome()
- dump(cobbleChest, 1, 12)
- load(cobbleChest, 1, 8)
- load(torchChest, 16, 16)
- goToStartPt(roof)
- run("floor 15 15")
- setFace(north)
- sprinkleTorches(15, 15, 3)
- xzHome()
- yHome()
- dump(cobbleChest, 1, 8)
- dump(torchChest, 16, 16)
- globalHome()
- end
- torchSlot = 16
- function goXZ(startFace, newX, newZ)
- setFace(startFace)
- if face == east then
- while x > newX do
- go(1)
- end
- elseif face == west then
- while x < newX do
- go(1)
- end
- else
- end
- if face == north then
- while z < newZ do
- go(1)
- end
- elseif face == south then
- while z > newZ do
- go(1)
- end
- else
- end
- end
- function goZX(startFace, newX, newZ)
- setFace(startFace)
- if face == north then
- while z < newZ do
- go(1)
- end
- elseif face == south then
- while z > newZ do
- go(1)
- end
- else
- end
- if face == east then
- while x > newX do
- go(1)
- end
- elseif face == west then
- while x < newX do
- go(1)
- end
- else
- end
- end
- function layTorches(distance, spacing)
- turtle.select(torchSlot)
- for i=1,distance do
- if i % spacing == 0 then
- turtle.placeDown()
- end
- if i < distance then
- go(1)
- end
- end
- end
- function lightPlatform()
- goToStartPt(platform)
- turtle.up()
- y = y + 1
- turtle.select(torchSlot)
- setFace(west)
- go(3)
- setFace(north)
- back(1)
- layTorches(platform[2], 4)
- setFace(west)
- go(5)
- setFace(south)
- back(2)
- layTorches(11, 4)
- turtle.placeDown()
- setFace(west)
- go(2)
- setFace(south)
- go(8)
- setFace(east)
- go(1)
- turtle.placeDown()
- go(2)
- turtle.placeDown()
- -- setFace(south)
- -- go(1)
- setFace(west)
- go(5)
- setFace(north)
- layTorches(17, 4)
- setFace(west)
- go(4)
- setFace(south)
- back(2)
- layTorches(17, 4)
- end
- function buildMobGrinder()
- center = {-6, 0, 20}
- -- length, width, xOffset, zOffset, yOffset
- base = {21, 23, 0, 0, 160}
- platform = {17, 17, 0, -2, base[5]-40}
- torch1 = {9, 9, 0, 0, base[5]+1}
- torch2 = {17, 17, 0, 0, base[5]+1}
- dropTower = {3, 3, 0, 0, platform[5]}
- ladderChute1 = {1, 1, 0, -11, 0}
- ladderChute2 = {1, 1, -6, 0, platform[5]}
- dropChute = {1, 1, 0, -6, platform[5]}
- torch3 = {9, 15, -3, -5, platform[5]+1}
- floorA = {13, 7, 4, 0, base[5]+4}
- wallA = {1, 5, 0, 0, floorA[5]}
- trapWallsA = {13, 7, 4, 0, floorA[5]}
- trapRoofA = {13, 7, 4, 0, floorA[5] + 4}
- trapSlabsA = {13, 7, 4, 0, trapRoofA[5] + 1}
- floorB = {13, 7, -4, 0, base[5] + 4}
- wallB = {1, 5, -4, 0, floorB[5]}
- trapWallsB = {13, 7, -4, 0, floorB[5]}
- trapRoofB = {13, 7, -4, 0, floorB[5] + 4}
- trapSlabsB = {13, 7, -4, 0, trapRoofB[5] + 1}
- drainFloor = {15, 15, 0, 0, 190}
- conveyors = {3, 3, 0, -1, 190}
- quicksand = {13, 13, 0, 0, 193}
- chamber = {15, 15, 0, 0, 190}
- roof = {15, 15, 0, 0, 198}
- -- Begin the build
- -- LADDER CHUTE FROM GROUND TO PLATFORM
- load(cobbleChest, 1,12)
- load(ladderChest, 14, 16)
- goToStartPt(ladderChute1)
- run("chute " .. -platform[5] .. " true")
- z = z - 1 -- The 'chute' program finishes one block back from start
- globalHome()
- dump(ladderChest, 14, 16)
- dump(cobbleChest, 1, 12)
- -- PLATFORM
- setFace(north)
- load(cobbleChest, 1, 12)
- load(torchChest, 16, 16)
- goToStartPt(platform)
- run("floor " .. platform[1] .. " " .. platform[2])
- -- TORCHES ON PLATFORM
- lightPlatform()
- zxHome()
- yHome()
- dump(torchChest, 16, 16)
- dump(cobbleChest, 1, 12)
- -- DROP CHUTE
- load(cobbleChest, 1, 12)
- load(ladderChest, 14, 16)
- goToStartPt(dropChute)
- setFace(north)
- height = platform[5] - base[5]
- run("chute " .. height .. " false") -- No ladders needed in drop chute
- z = z - 1 -- The 'chute' program finishes one block back from start
- -- LADDER CHUTE FROM PLATFORM TO BASE
- goToStartPt(ladderChute2)
- setFace(west)
- height = platform[5] - base[5]
- run("chute " .. height .. " true")
- x = x + 1 -- The 'chute' program finishes one block back from start
- zxHome()
- dump(ladderChest, 15, 16)
- dump(cobbleChest, 1, 12)
- -- BASE
- load(cobbleChest, 1, 12)
- load(torchChest, 16, 16)
- goToStartPt(base)
- setFace(north)
- run("floor " .. base[1] .. " " .. base[2])
- -- TORCHES ON BASE
- moveTo(center[1], torch1[5], center[3])
- turtle.select(16)
- turtle.placeDown()
- goToStartPt(torch1)
- setFace(north)
- run("sprinkle " .. torch1[1] .. " " .. torch1[2])
- goToStartPt(torch2)
- setFace(north)
- run("sprinkle " .. torch2[1] .. " " .. torch2[2])
- goToStartPt(floorB)
- run("floor " .. floorB[1] .. " " .. floorB[2])
- goToStartPt(floorA)
- setFace(north)
- run("floor " .. floorA[1] .. " " .. floorA[2])
- zxHome()
- yHome()
- dump(cobbleChest, 1, 12)
- dump(torchChest, 16, 16)
- load(cobbleChest, 1, 16)
- goToStartPt(wallB)
- setFace(west)
- run("wall 5 3")
- x = x + 1 -- The 'wall' program finishes one block back from start
- goToStartPt(wallA)
- setFace(east)
- run("wall 5 3")
- x = x - 1 -- The 'wall' program finishes one block back from start
- setFace(south)
- go(6)
- goToStartPt(trapWallsB)
- setFace(north)
- run("walls " .. trapWallsB[1] .. " " .. trapWallsB[2] .. " " .. 3)
- z = z - 1 -- The 'walls' program finishes one block back from start
- setFace(south)
- go(1)
- xHome()
- goToStartPt(trapWallsA)
- setFace(north)
- run("walls " .. trapWallsA[1] .. " " .. trapWallsA[2] .. " " .. 3)
- z = z - 1 -- The 'walls' program finishes one block back from start
- goToStartPt(trapRoofB)
- setFace(north)
- run("floor " .. trapRoofB[1] .. " " .. trapRoofB[2])
- goToStartPt(trapRoofA)
- setFace(north)
- run("floor " .. trapRoofA[1] .. " " .. trapRoofA[2])
- zxHome()
- yHome()
- dump(cobbleChest, 1, 16)
- load(slabChest, 1, 3)
- goToStartPt(trapSlabsB)
- setFace(north)
- run("floor " .. trapSlabsB[1] .. " " .. trapSlabsB[2])
- goToStartPt(trapSlabsA)
- setFace(north)
- run("floor " .. trapSlabsA[1] .. " " .. trapSlabsA[2])
- zxHome()
- setFace(north)
- yHome()
- dump(slabChest, 1, 3)
- globalHome()
- end
- function buildWaterHut()
- --[[
- for i=1,6 do
- turtle.down()
- end
- run("floor 5 5")
- run("walls 5 5 3")
- for i=1,4 do
- turtle.up()
- end
- go(1)
- run("floor 5 5")
- --]]
- while turtle.down() do
- y = y - 1
- end
- height = y + 1
- run("chute " .. height)
- back(1)
- yHome()
- end
- -- MAIN PROGRAM
- if nArgs ~= 1 then
- print(usage)
- return
- end
- face = north
- if args[1] == "sandtrap" then
- buildSandTrap()
- elseif args[1] == "mobgrinder" then
- buildMobGrinder()
- elseif args[1] == "waterhut" then
- buildWaterHut()
- else
- print("Unknown structure: \"", args[1], "\"")
- end
- *** bwb
- -- bwb (branch with bark)
- -- Creates a 1x1x3 tunnel with branches at intervals
- -- Each branch is the of the length specified by the user
- -- Seals all sides of tunnel against monsters
- -- Torches, if supplied, will be placed on the wall at 8 block interval
- -- Chests, if supplied, are placed at the end of each branch
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- usage = "Usage: bwb <length>"
- print("bwb: Rev 1.2")
- loop = 0;
- x = 0
- y = 0
- z = 0
- face = 0
- patchSlot = 1
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print("Creates a 1x3 tunnel with sealed sides.")
- print("Place patch material, like cobblestone, in slot", patchSlot)
- print("If torches are desired, place torches in slot 16.")
- printf("Chests, if supplied, are placed at the end of each branch")
- print("Usage: branch <length>")
- return
- end
- if nArgs ~= 1 then
- print("Usage: branch <length>")
- 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
- targetArea = length
- areaCovered = 1;
- 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()
- success = false
- repeat
- while turtle.detectUp() do -- This loop added in case of falling sand or whatever
- turtle.digUp()
- end
- success = turtle.up()
- until success
- y = y+1
- end
- function rise(nBlocks)
- local i
- for i=1,nBlocks do
- up()
- end
- end
- function descend(nBlocks)
- local i
- for i=1,nBlocks do
- down()
- end
- end
- function down()
- if turtle.detectDown() then
- turtle.digDown()
- end
- turtle.down()
- y = y-1
- 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 patch()
- turtle.select(2)
- turtle.place()
- gatherPatchMaterial(false);
- end
- function patchUp()
- turtle.select(2)
- turtle.placeUp()
- gatherPatchMaterial(false);
- end
- function patchDown()
- turtle.select(2)
- turtle.placeDown()
- gatherPatchMaterial(false);
- end
- function slurp()
- local i
- print("slurp...")
- for i=2,13 do
- turtle.select(i)
- if turtle.suck() then
- print("Sucky success for slot ", i)
- break
- else
- end
- end
- print("Total sucky failure")
- end
- function slurpUp()
- local i
- for i=2,13 do
- turtle.select(i)
- if turtle.suckUp() then
- break
- end
- end
- end
- function slurpDown()
- local i
- for i=2,13 do
- turtle.select(i)
- if turtle.suckDown() then
- break
- end
- end
- end
- function gatherPatchMaterial(force)
- local i
- patchCount = turtle.getItemCount(patchSlot)
- if (patchCount < 30) or force then
- print("Attempting to refill slot ", patchSlot)
- for i=1,14 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
- addTorch = true
- torchSpacing = 8
- torchSpot = 6
- function _mineForward()
- forward()
- while turtle.detectUp() do -- This loop added in case of falling sand or whatever
- turtle.digUp()
- -- slurpUp()
- end
- if turtle.detectDown() then
- turtle.digDown()
- -- slurpDown()
- end
- if addTorch then
- if (torchSpot % torchSpacing) == 0 then
- left()
- left()
- turtle.select(16)
- turtle.place()
- right()
- right()
- end
- end
- torchSpot = torchSpot + 1
- end
- function mineForwardUp()
- forward()
- -- slurp()
- if not turtle.detectDown() then
- patchDown()
- end
- patchSides()
- up()
- while turtle.detectUp() do -- This loop added in case of falling sand or whatever
- turtle.digUp()
- -- slurpUp()
- end
- patchSides()
- torchCheck()
- up()
- if not turtle.detectUp() then
- patchUp()
- end
- patchSides()
- torchSpot = torchSpot + 1
- end
- function mineForwardDown()
- forward()
- -- slurp()
- if not turtle.detectUp() then
- patchUp()
- end
- patchSides()
- down()
- patchSides()
- torchCheck()
- down()
- if not turtle.detectDown() then
- patchDown()
- end
- patchSides()
- torchSpot = torchSpot + 1
- end
- function patchCeiling()
- end
- function patchFloor()
- end
- function patchSides()
- if x == 0 then
- return
- end
- local currentFace = face
- left()
- if not turtle.detect() then
- patch()
- end
- right()
- right()
- if not turtle.detect() then
- patch()
- end
- setFace(currentFace)
- end
- function _patchSides()
- local currentFace = face
- left()
- if not turtle.detect() then
- if face == 0 and x ~= -1 then
- patch()
- end
- end
- right()
- right()
- if not turtle.detect() then
- if face == 0 and x ~= -1 then
- patch()
- else
- print("patchSides: opted to NOT patch, face = ", face, ", x = ", x)
- end
- end
- setFace(currentFace)
- -- left()
- end
- function torchCheck()
- if addTorch then
- if (torchSpot % torchSpacing) == 0 then
- right()
- right()
- turtle.select(16)
- turtle.place()
- left()
- left()
- end
- 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 checkFuel()
- if currentFuelLevel == "unlimited" then
- return true
- end
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel(200) then
- areaCovered = targetArea
- missionMessage = "Mission aborted due to low fuel."
- abort = true
- return false
- end
- end
- return true
- 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
- end
- function dump()
- gatherPatchMaterial(true)
- for i=2,14 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- function mineAhead()
- if y == 0 then
- mineForwardUp()
- else
- mineForwardDown()
- end
- end
- function branchLoop()
- torchSpot = 6
- local i
- for i=1,length do
- mineAhead()
- end
- right()
- for i=1,3 do
- mineAhead()
- end
- right()
- for i=1,length do
- mineAhead()
- end
- setFace(0)
- while y > 0 do
- down()
- end
- if turtle.detectDown() then
- turtle.select(15)
- if not turtle.compareDown() then
- turtle.digDown()
- -- slurpDown()
- turtle.placeDown()
- end
- end
- if turtle.compareDown() then
- dump()
- turtle.select(2)
- end
- end
- -- MAIN PROGRAM
- turtle.select(1)
- 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
- for loop=1,5 do
- addTorch = false
- -- rise(1)
- for i=1,5 do
- mineAhead()
- end
- turtle.back()
- turtle.back()
- left()
- addTorch = true
- branchLoop()
- end
- print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
- *** cactus
- -- 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()
- *** chute
- -- 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 2.0")
- 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)
- *** clear
- -- 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 5.0")
- x = 0
- y = 0
- z = 0
- 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
- 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 turtle.detect() then
- turtle.dig()
- if y - 1 <= highestLevel then
- turtle.digUp()
- end
- if y - 1 >= lowestLevel then
- turtle.digDown()
- 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
- 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 strip(nBlocks)
- local i
- -- print("strip ", nBlocks)
- 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)
- 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
- rise(height-1)
- elseif height < 0 then
- height = -height + 1 -- same vertical distance, but we don't rise first
- else
- return
- end
- for d=1,height do
- for w=1,width do
- strip(length-1)
- -- print("strip: Cleared ", areaCovered, " out of ", targetArea, " blocks")
- 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)
- *** collect
- -- collect
- -- Collects specified blocks from chests left by 'branch' and 'bwb' programs
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- usage = "Usage: collect"
- print("collect: Rev 0.1")
- loop = 0;
- x = 0
- y = 0
- z = 0
- face = 0
- patchSlot = 2
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- if (nArgs == 1 and args[1]== "help") then
- print("Collects specified blocks from in-ground chests")
- print(" as left by the 'branch' and 'bwb' programs.")
- print("Place fuel in slot 1")
- print("Place at least 1 block of target material in slot 2.")
- print("Turtle proceeds in straight line until it hits something")
- print(usage)
- return
- end
- if nArgs ~= 0 then
- print(usage)
- return
- end
- function suckChest()
- local i
- turtle.select(3)
- for i=3,16 do
- turtle.suckDown()
- end
- end
- targetSlot = 2
- function gatherTargetMaterial()
- local i
- for i=3,16 do
- turtle.select(i)
- if turtle.compareTo(targetSlot) then
- turtle.transferTo(targetSlot)
- end
- end
- end
- function dumpRejects()
- local i
- turtle.select(3)
- for i=3,16 do
- turtle.select(i)
- if not turtle.compareTo(targetSlot) then
- turtle.dropDown()
- end
- end
- end
- function home()
- while z > 0 do
- turtle.back()
- end
- end
- -- Main Program
- for i=1,100 do
- turtle.select(3)
- if turtle.suckDown() then
- turtle.dropDown()
- suckChest()
- dumpRejects()
- gatherTargetMaterial()
- end
- if not turtle.forward() then
- break
- end
- z = z+1
- end
- home()
- print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
- *** convey10x10-4
- conveyorSlot = 16
- elevation = 1
- local function forward(nBlocks)
- for i=1,nBlocks do
- turtle.forward()
- end
- end
- local function back(nBlocks)
- for i=1,nBlocks do
- turtle.back()
- end
- end
- local function up(nBlocks)
- for i=1,nBlocks do
- turtle.up()
- end
- end
- local function down(nBlocks)
- for i=1,nBlocks do
- turtle.down()
- end
- end
- local function goToStartPoint()
- up(2)
- elevation = 2
- turtle.turnRight()
- forward(4)
- turtle.turnLeft()
- back(5)
- end
- local function returnHome()
- forward(4)
- turtle.turnRight()
- forward(1)
- down(elevation)
- end
- function nextRow()
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- end
- function layRow(rowNumber, length)
- for i=1,length do
- if turtle.getItemCount(16) < 1 then
- turtle.select(conveyorSlot - 1)
- end
- turtle.placeDown()
- if i < length then
- if rowNumber % 2 == 0 then
- turtle.back()
- else
- turtle.forward()
- end
- end
- end
- end
- function layArea(length, width)
- for i=1,width do
- layRow(i,length)
- if i < width then
- nextRow()
- end
- end
- end
- turtle.select(conveyorSlot)
- goToStartPoint()
- layArea(4,10)
- forward(4)
- turtle.turnRight()
- layArea(4,2)
- turtle.turnRight()
- back(4)
- layArea(4,10)
- forward(4)
- turtle.turnRight()
- layArea(4,2)
- returnHome()
- *** convey12x12-4
- conveyorSlot = 16
- elevation = 1
- local function forward(nBlocks)
- for i=1,nBlocks do
- turtle.forward()
- end
- end
- local function back(nBlocks)
- for i=1,nBlocks do
- turtle.back()
- end
- end
- local function up(nBlocks)
- for i=1,nBlocks do
- turtle.up()
- end
- end
- local function down(nBlocks)
- for i=1,nBlocks do
- turtle.down()
- end
- end
- local function goToStartPoint()
- up(2)
- elevation = 2
- turtle.turnRight()
- forward(5)
- turtle.turnLeft()
- back(6)
- end
- local function returnHome()
- forward(5)
- turtle.turnRight()
- forward(1)
- down(elevation)
- end
- function nextRow()
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- end
- function layRow(rowNumber, length)
- for i=1,length do
- if turtle.getItemCount(conveyorSlot) < 1 then
- conveyorSlot = conveyorSlot-1
- if turtle.getItemCount(conveyorSlot) < 1 then
- conveyorSlot = conveyorSlot-1
- end
- end
- turtle.select(conveyorSlot)
- turtle.placeDown()
- if i < length then
- if rowNumber % 2 == 0 then
- turtle.back()
- else
- turtle.forward()
- end
- end
- end
- end
- function layArea(length, width)
- for i=1,width do
- layRow(i,length)
- if i < width then
- nextRow()
- end
- end
- end
- turtle.select(conveyorSlot)
- --goToStartPoint()
- turtle.turnRight()
- up(1)
- layArea(5,12)
- forward(5)
- turtle.turnRight()
- layArea(5,2)
- turtle.turnRight()
- back(5)
- layArea(5,12)
- forward(5)
- turtle.turnRight()
- layArea(5,2)
- returnHome()
- *** copyall
- -- copyall: Version 0.1
- -- Copys all files from source to destination
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- usage = "Usage: copyall <src directory><dst directory>"
- if (nArgs == 0 or args[1]== "help") then
- print("Copys all files from source to destination")
- print(usage)
- return
- end
- if nArgs ~= 2 then
- print(usage)
- return
- end
- src = args[1]
- dst = args[2]
- list = fs.list(src)
- for i=1,#list do
- if not fs.isDir(list[i]) then
- filename = list[i]
- -- print(filename)
- firstchar = string.sub(filename, 1, 1)
- -- print("firstchar = ", firstchar)
- if firstchar ~= "." then
- srcpath = src .. "/" .. filename
- dstpath = dst .. "/" .. filename
- print("Copying ", srcpath, " to ", dstpath)
- if fs.exists(dstpath) then
- fs.delete(dstpath)
- end
- fs.copy(srcpath, dstpath)
- else
- print("Ignoring: ", filename)
- end
- end
- end
- *** craft
- -- craft: Craft the named item
- -- Version 2.0
- usage = "usage: craft <itemName>"
- recipes = {
- {"2x2", 1, 1, 0, 1, 1, 0, 0, 0, 0},
- {"3x3", 1, 1, 1, 1, 1, 1, 1, 1, 1},
- {"chest", 1, 1, 1, 1, 0, 1, 1, 1, 1},
- {"furnace", 1, 1, 1, 1, 0, 1, 1, 1, 1},
- {"stairs", 1, 0, 0, 1, 1, 0, 1, 1, 1},
- {"slab", 1, 1, 1, 0, 0, 0, 0, 0, 0},
- {"door", 1, 1, 0, 1, 1, 0, 1, 1, 0},
- {"wall", 1, 1, 1, 1, 1, 1, 0, 0, 0},
- {"fence", 1, 1, 1, 1, 1, 1, 0, 0, 0},
- }
- -- getIndex: Return the array index of the entry containing <name>
- function printList()
- for i=1,#recipes do
- print(" ", recipes[i][1])
- end
- end
- function getIndex(name)
- rv = 0 -- return value
- nItems = #recipes
- for i= 1,nItems do
- if name == recipes[i][1] then
- rv = i
- break
- end
- end
- return(rv)
- end
- function requiredQty(recipe)
- qty = 0
- for i=2,10 do
- if recipe[i] > 0 then
- qty = qty + 1
- end
- end
- return qty
- end
- function occupiedSlotCount()
- count = 0
- for i=1,16 do
- if turtle.getItemCount(i) > 0 then
- count = count + 1
- end
- end
- return count
- end
- function highestMultipleOf(number, multiplier)
- return number - (number % multiplier)
- end
- function forceGoodMultiple(recipe)
- req = requiredQty(recipe)
- maxSlotCount = 64
- slotCount = 0
- maxSlotMultiple = highestMultipleOf(maxSlotCount, req)
- slotMultiple = 0
- turtle.select(1)
- turtle.dropUp() -- clear this slot for a fresh start
- turtle.suckUp()
- slotCount = turtle.getItemCount(1) -- see how much we got
- if slotCount > 64 then
- print("Holy crap! It gave us ", slotCount)
- return
- else
- -- print("Actual items from chest: ", slotCount)
- end
- slotMultiple = highestMultipleOf(slotCount, req)
- -- overage = maxSlotMultiple - slotMultiple
- overage = slotCount % req
- -- print("slotCount: ", slotCount, ", req: ", req, ", slotMultiple: ", slotMultiple)
- -- print(" overage = ", overage)
- if overage > 0 then
- turtle.dropUp(overage)
- count = turtle.getItemCount(1)
- end
- -- print("Forced count = ", slotCount)
- return count
- end
- function getMaterial(recipe)
- count = forceGoodMultiple(recipe)
- -- print(" Slot now contains: ", count)
- sleep(3)
- return count
- end
- function loadTable(recipe, qty)
- turtle.select(1)
- if recipe[2] > 0 then turtle.transferTo(5, qty) end
- if recipe[3] > 0 then turtle.transferTo(6, qty) end
- if recipe[4] > 0 then turtle.transferTo(7, qty) end
- if recipe[5] > 0 then turtle.transferTo(9, qty) end
- if recipe[6] > 0 then turtle.transferTo(10, qty) end
- if recipe[7] > 0 then turtle.transferTo(11, qty) end
- if recipe[8] > 0 then turtle.transferTo(13, qty) end
- if recipe[9] > 0 then turtle.transferTo(14, qty) end
- if recipe[10] > 0 then turtle.transferTo(15, qty) end
- end
- function clearInventory()
- for i=1,16 do
- turtle.select(i)
- turtle.dropUp()
- end
- turtle.select(1)
- end
- function craftRecipe(recipe)
- req = requiredQty(recipe)
- count = getMaterial(recipe)
- occupiedSlots = occupiedSlotCount()
- slotCount = turtle.getItemCount(1)
- if occupiedSlots ~= req then
- qty = slotCount / req
- loadTable(recipe, qty)
- slotCount = turtle.getItemCount(1)
- if slotCount ~= 0 then
- print("Wait. Slot 1 still has ", slotCount)
- end
- end
- occupiedSlots = occupiedSlotCount()
- -- print("occupied slots: ", occupiedSlots, ", req: ", req)
- if occupiedSlotCount() == req then
- turtle.select(16)
- if not turtle.craft() then
- print("Crafting failed. Wrong material?")
- turtle.select(1)
- return false
- else
- turtle.select(16)
- turtle.drop()
- turtle.select(1)
- end
- end
- return true
- end
- -- Main program
- args = {...}
- nArgs = #args
- if nArgs == 0 then
- print(usage)
- printList()
- return
- end
- if nArgs ~= 1 then
- print(usage)
- return
- end
- itemName = args[1]
- index = getIndex(itemName)
- if index == 0 then
- print("Can't find \"", itemName, "\"")
- return
- else
- recipe = recipes[index]
- req = requiredQty(recipe)
- print("Crafting recipe: ", recipe[1])
- print(req, " blocks material required for each.")
- print("Run must be terminated manually.")
- clearInventory()
- turtle.select(1)
- while true do
- if not craftRecipe(recipe) then
- break
- end
- sleep(0)
- end
- end
- *** d
- -- d
- -- Moves the turtle the specified distance down
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- distance = 1;
- if (nArgs == 1 and args[1]== "help") then
- print("Moves the turtle the specified distance down")
- print("Usage: f [distance]")
- return
- end
- if nArgs == 1 then
- distance = tonumber(args[1])
- if distance == nil then
- print(args[1], " is not a valid distance")
- return
- end
- end
- -- MAIN PROGRAM
- turtle.select(1)
- if currentFuelLevel ~= "unlimited" then
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel() then
- print("No fuel")
- return
- end
- end
- end
- for i=1,distance do
- turtle.down()
- end
- *** drainbox
- torchSlot = 13
- local function forward(nBlocks)
- for i=1,nBlocks do
- turtle.forward()
- end
- end
- local function back(nBlocks)
- for i=1,nBlocks do
- turtle.back()
- end
- end
- local function up(nBlocks)
- for i=1,nBlocks do
- turtle.up()
- end
- end
- local function down(nBlocks)
- for i=1,nBlocks do
- turtle.down()
- end
- end
- local function torchIt()
- turtle.select(torchSlot)
- up(1)
- forward(3)
- turtle.turnRight()
- forward(3)
- turtle.placeDown()
- forward(5)
- turtle.placeDown()
- turtle.turnLeft()
- forward(5)
- turtle.placeDown()
- turtle.turnLeft()
- forward(5)
- turtle.placeDown()
- forward(3)
- turtle.turnLeft()
- forward(8)
- turtle.turnLeft()
- turtle.turnLeft()
- down(1)
- end
- local function goToStartPoint()
- turtle.turnLeft()
- forward(4)
- turtle.turnRight()
- back(3)
- end
- local function returnHome()
- forward(4)
- turtle.turnRight()
- forward(1)
- down(elevation)
- end
- -- Quad Chute
- turtle.turnLeft()
- forward(2)
- turtle.turnRight()
- forward(2)
- shell.run("quadchute up 2 false")
- -- 12 x 12 Funnel Floor #1
- goToStartPoint()
- up(1)
- shell.run("pattern funnel12")
- torchIt()
- -- First Floor walls
- turtle.turnRight()
- shell.run("walls3 12 12")
- -- 12 x 12 Funnel Floor #2
- up(3)
- forward(1)
- turtle.turnRight()
- back(1)
- shell.run("pattern funnel12")
- torchIt()
- -- Second Floor Walls
- turtle.turnRight()
- shell.run("walls3 12 12")
- -- 12 x 12 Funnel Roof
- up(3)
- forward(1)
- turtle.turnRight()
- back(1)
- shell.run("pattern funnel12")
- -- Conveyor Belt Shingles On Roof
- shell.run("convey12x12-4")
- -- Return To Earth
- turtle.turnRight()
- forward(1)
- down(255)
- *** dump
- -- dump
- -- Dump turtle contents directly down
- -- Written by HarvDad, April 2014
- -- Rev 0.1
- args = {...}
- nArgs = #args
- function dump()
- for i=1,16 do
- if (turtle.getItemCount(i) > 0) then
- turtle.select(i)
- turtle.dropDown()
- end
- end
- end
- -- MAIN PROGRAM
- dump()
- *** eatlava
- -- eatlava
- -- Refuels from lava directly below turtle
- -- Must start with one lava bucket in slot 1
- -- Turtle travels requested area refueling when lava is directly below, then returns
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- print("eatLava: Rev 2.1")
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- usage = "Usage: eatlava <length> <width>"
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print("Refuels from lava directly below turtle.")
- print("First time: Must start with one lava bucket in slot 1")
- print("Usage: eatLava <length> <width>")
- return
- end
- if nArgs ~= 2 then
- print("Usage: eatLava <distance>")
- 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")
- return
- end
- width = tonumber(args[2])
- if width == nil then
- print("\"", args[1], "\" is not a valid width")
- return
- end
- if width < 1 then
- print("width must be a positive number greater than zero")
- return
- end
- areaCovered = 0;
- function eatLava()
- turtle.select(1)
- if turtle.placeDown() then
- turtle.refuel()
- 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 forward()
- 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 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 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)
- end
- -- MAIN PROGRAM
- turtle.select(1)
- 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
- nextTurn = "left"
- forward()
- targetArea = length * width
- while areaCovered < targetArea do
- if abort then
- break
- end
- for w=1,width do
- if abort then
- break
- end
- for z=1,length do
- if abort then
- break
- end
- eatLava()
- if z < length then
- forward()
- end
- end
- if nextTurn == "left" then
- if areaCovered < targetArea then
- left()
- forward()
- left()
- nextTurn = "right"
- end
- else
- if areaCovered < targetArea then
- right()
- forward()
- right()
- nextTurn = "left"
- end
- end
- end
- end
- home()
- print (missionMessage)
- print ("Fuel level is now ", turtle.getFuelLevel())
- *** f
- -- f
- -- Moves the turtle the specified distance forward
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- distance = 1;
- if (nArgs == 1 and args[1]== "help") then
- print("Moves the turtle the specified distance forward")
- print("Usage: f [distance]")
- return
- end
- if nArgs == 1 then
- distance = tonumber(args[1])
- if distance == nil then
- print(args[1], " is not a valid distance")
- return
- end
- end
- -- MAIN PROGRAM
- turtle.select(1)
- if currentFuelLevel ~= "unlimited" then
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel() then
- print("No fuel")
- return
- end
- end
- end
- for i=1,distance do
- turtle.forward()
- end
- *** fabdrain
- print("fabdrain: Rev 1.0")
- beltSlot = 1
- currentSlot = beltSlot
- goForward = 0
- goBackward = 1
- direction = goForward
- holeWidth = 3
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- x = 0
- y = 0
- z = 0
- currentFace = north
- 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 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 aboutFace()
- if face == north then setFace(south) return end
- if face == south then setFace(north) return end
- if face == east then setFace(west) return end
- if face == west then setFace(east) return end
- end
- function reverseDirection()
- if direction == goForward then
- direction = goBackward
- else
- direction = goForward
- end
- end
- function moveForward(nBlocks)
- for i = 1, nBlocks do
- turtle.forward()
- if face == north then
- z = z + 1
- elseif face == south then
- z = z - 1
- elseif face == east then
- x = x + 1
- elseif face == west then
- x = x - 1
- end
- end
- end
- function moveBackward(nBlocks)
- for i = 1, nBlocks do
- turtle.back()
- if face == north then
- z = z - 1
- elseif face == south then
- z = z + 1
- elseif face == east then
- x = x - 1
- elseif face == west then
- x = x + 1
- end
- end
- end
- function home()
- end
- function xzHome()
- local X = gX
- local Z = gZ
- if x ~= X then
- if x < X then
- setFace(east)
- while x < X do
- turtle.forward()
- x = x + 1
- end
- else
- setFace(west)
- while x > X do
- turtle.forward()
- x = x - 1
- end
- end
- end
- if z ~= Z then
- if z < Z then
- setFace(north)
- while z < Z do
- turtle.forward()
- z = z + 1
- end
- else
- setFace(south)
- while z > Z do
- turtle.forward()
- z = z - 1
- end
- end
- end
- end
- function layBeltRow(direction, length)
- for j=1,5 do
- if turtle.getItemCount(currentSlot) < 1 then
- currentSlot = currentSlot + 1
- if turtle.getItemCount(currentSlot) < 1 then
- abort("No more belts in inventory.")
- end
- turtle.select(currentSlot)
- end
- turtle.placeDown()
- if j < 5 then
- if direction == goForward then
- moveForward(1)
- else
- moveBackward(1)
- end
- end
- end
- end
- rowHeading = west
- function goToNextRow()
- currentFace = face
- if face == north or face == south then
- setFace(rowHeading)
- moveForward(1)
- setFace(currentFace)
- else
- setFace(rowHeading)
- moveForward(1)
- setFace(currentFace)
- end
- end
- function layBeltSection(length, width)
- moveBackward(length - 1)
- for i = 1,width do
- direction = goForward
- layBeltRow(direction, length)
- moveForward(holeWidth+1)
- aboutFace()
- direction = goBackward
- layBeltRow(direction, length)
- goToNextRow()
- end
- setFace(east)
- rowHeading = south
- direction = goBackwards
- segLength = length + 3 + length
- for i = 1, segLength do
- layBeltRow(direction, length)
- if i < segLength then
- goToNextRow()
- reverseDirection()
- end
- end
- setFace(east)
- moveForward(length + holeWidth)
- aboutFace()
- rowHeading = north
- direction = goBackwards
- for i = 1, segLength do
- layBeltRow(direction, length)
- if i < segLength then
- goToNextRow()
- reverseDirection()
- end
- end
- end
- function abort(message)
- print(message)
- error()
- end
- -- Main Program
- turtle.up()
- y = y + 1
- turtle.select(1)
- layBeltSection(5, holeWidth)
- xzHome()
- yHome()
- setFace(north)
- *** floor
- -- floor
- -- Lays down a floor with whatever material is provided
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- version = "floor: Rev 2.0"
- mission = "Lay down a floor with the supplied material"
- instructions = "Place stack(s) of material starting in first slot."
- usage = "floor <length> <width> [level]"
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- startLevel = 0
- local currentFuelLevel = turtle.getFuelLevel()
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(mission)
- print(instructions)
- print(usage)
- return
- end
- if nArgs < 2 or 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
- 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
- if nArgs == 3 then
- startLevel = tonumber(args[3])
- if startLevel == nil then
- print("\"", args[3], "\" is not a valid start level")
- return
- end
- end
- targetArea = length * width
- areaCovered = 1
- currentSlot = 2
- materialSlot = 1
- maxSlot = 16
- 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
- turtle.digDown()
- end
- turtle.down()
- y = y-1
- 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()
- 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 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 y > 0 then
- while y > 0 do
- down()
- end
- end
- setFace(0)
- end
- function strip(nBlocks)
- local i
- -- print("strip ", nBlocks)
- for i=1,nBlocks do
- chomp()
- checkFuel()
- if abort then
- areaCovered = targetArea
- end
- if areaCovered < targetArea then
- forward()
- end
- end
- 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
- 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
- -- MAIN PROGRAM
- turtle.select(1)
- print("fuelLevel = ", currentFuelLevel)
- print("l: ", length, ", w: ", width, ", f: ", 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 startLevel < 2 then
- rise(startLevel)
- else
- rise(startLevel-2)
- end
- while areaCovered < targetArea do
- if abort then
- break
- end
- for w=1,width do
- if abort then
- break
- end
- for z=1,length do
- if abort then
- break
- end
- layBlock()
- if z < length then
- forward()
- end
- end
- if nextTurn == "left" then
- if areaCovered < targetArea then
- left()
- forward()
- left()
- nextTurn = "right"
- end
- else
- if areaCovered < targetArea then
- right()
- forward()
- right()
- nextTurn = "left"
- end
- end
- end
- end
- --print("Finish the main loop.")
- --print(missionMessage)
- --print(" Current fuel level is ", turtle.getFuelLevel())
- home(0)
- *** funneltrap
- local function rise(nBlocks)
- for i=1,nBlocks do
- turtle.up()
- end
- end
- local function back(nBlocks)
- for i=1,nBlocks do
- turtle.back()
- end
- end
- local function left(nBlocks)
- turtle.turnLeft()
- if nBlocks == nil or nBlocks == 0 then
- return
- end
- for i=1,nBlocks do
- turtle.forward()
- end
- end
- local function right(nBlocks)
- turtle.turnRight()
- if nBlocks == nil or nBlocks == 0 then
- return
- end
- for i=1,nBlocks do
- turtle.forward()
- end
- end
- height = 42
- shell.run("quadchute up ", height/3)
- rise(height)
- back(3)
- left(4)
- right(0)
- shell.run("pattern funnel12")
- right(0)
- shell.run("walls 12 12 4")
- *** l
- -- l
- -- Turns and/or move the turtle to the left
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- distance = nil;
- if (nArgs == 1 and args[1]== "help") then
- print("Turns and/or moves the turtle to the left")
- print("Usage: l [distance]")
- return
- end
- if nArgs == 1 then
- distance = tonumber(args[1])
- if distance == nil then
- print(args[1], " is not a valid distance")
- return
- end
- end
- -- MAIN PROGRAM
- turtle.turnLeft()
- if distance ~= nil then
- turtle.select(1)
- if currentFuelLevel ~= "unlimited" then
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel() then
- print("No fuel")
- return
- end
- end
- end
- for i=1,distance do
- turtle.forward()
- end
- end
- *** logger
- -- logger
- -- Grows a tree, cuts it down.
- -- Stores the logs in a chest
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- version = "logger: Rev 2.1"
- saplingsSlot = 1
- sampleSlot = 16 -- This slot must have at least 1 sample of the correct log type
- bonemealSlot = 2
- y = 0
- if nArgs == 1 and args[1] == "help" then
- print(version)
- print("No arguments required")
- print("Place sapling supply in slot ", saplingsSlot)
- print("Place log sample in slot ", sampleSlot)
- print("Needs to be part of special tree farm")
- end
- function whackLeaves()
- turtle.dig()
- turtle.turnRight()
- turtle.dig()
- turtle.turnRight()
- turtle.dig()
- turtle.turnRight()
- turtle.dig()
- turtle.turnRight()
- end
- function focusSaplings()
- local i
- saplingCount = turtle.getItemCount(saplingsSlot)
- if saplingCount < 64 then
- -- print("Organizing saplings into slot ", saplingsSlot)
- for i=2,15 do
- turtle.select(i)
- if turtle.compareTo(saplingsSlot) then
- turtle.transferTo(saplingsSlot, 64-saplingCount)
- print("Transferred ", 64 - saplingCount, " saplings to slot ", saplingsSlot)
- saplingCount = turtle.getItemCount(saplingsSlot)
- if saplingCount >= 64 then
- turtle.select(saplingsSlot)
- break
- end
- end
- end
- end
- sampleCount = turtle.getItemCount(sampleSlot)
- if sampleCount > 1 then
- turtle.select(sampleSlot)
- turtle.dropDown(sampleCount - 1)
- end
- turtle.select(saplingsSlot)
- 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 suckSaplings()
- for i=1,4 do
- turtle.suck()
- turtle.suckUp()
- turtle.turnRight()
- end
- setFace(0)
- end
- function dump()
- for i=3,15 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.dropDown()
- end
- end
- sampleCount = turtle.getItemCount(sampleSlot)
- if sampleCount > 1 then
- turtle.select(sampleSlot)
- turtle.dropDown(sampleCount - 1)
- end
- turtle.select(saplingsSlot)
- end
- function tryBonemeal()
- if turtle.getItemCount(bonemealSlot) > 1 then
- turtle.select(bonemealSlot)
- turtle.place()
- end
- end
- intervalLength = 30
- interval = 0
- print("Logging is enabled")
- while true do
- turtle.select(saplingsSlot)
- turtle.place()
- tryBonemeal()
- turtle.select(sampleSlot)
- repeat
- sleep(2)
- if interval > intervalLength then
- focusSaplings()
- interval = 0
- else
- interval = interval + 1
- end
- tryBonemeal()
- turtle.select(sampleSlot)
- until turtle.compare()
- turtle.dig()
- turtle.forward()
- turtle.select(2)
- while turtle.detectUp() do
- turtle.digUp()
- turtle.up()
- end
- while not turtle.detectDown() do
- turtle.suckDown()
- turtle.down()
- end
- suckSaplings()
- turtle.back()
- suckSaplings()
- dump()
- end
- *** obsidian
- -- obsidian
- -- Generate obsidian blocks from redstone dust
- -- Version: Rev 0.1
- message1 = "Place redstone dust in slot 1"
- message2 = "Place bucket of lava in slot 6"
- usage = "obsidian (No arguments)"
- args = {...}
- nArgs = #args
- function forward(nBlocks)
- for i=1,nBlocks do
- turtle.forward()
- end
- end
- function back(nBlocks)
- for i=1,nBlocks do
- turtle.back()
- end
- end
- function placeRedstone()
- back(3)
- turtle.select(1)
- for i=1,7 do
- turtle.placeDown()
- forward(1)
- end
- back(3)
- end
- function mineObsidian()
- back(3)
- turtle.select(5)
- for i=1,3 do
- turtle.digDown()
- forward(1)
- end
- forward(1)
- for i=1,3 do
- turtle.digDown()
- forward(1)
- end
- back(3)
- end
- function employLava()
- turtle.up()
- turtle.select(16)
- turtle.placeDown()
- sleep(8)
- turtle.placeDown()
- sleep(3)
- turtle.down()
- end
- function offloadObsidian()
- turtle.up()
- turtle.turnLeft()
- if turtle.detect() then
- turtle.select(5)
- turtle.drop()
- end
- turtle.down()
- turtle.turnRight()
- end
- if nArgs > 0 then
- print(message1)
- print(message2)
- print(usage)
- end
- while true do
- if turtle.getItemCount(1) < 6 then
- break
- end
- placeRedstone()
- employLava()
- mineObsidian()
- offloadObsidian()
- end
- print("Redstone count is below 6.")
- *** ores
- -- ores
- -- Mines specifically for ores, every third level within specified range
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- version = "ores: Rev 2.1"
- mission = "Mine specifically for ores, every third level"
- instructions = "The first 3 slots must contain blocks to be ignored.."
- usage = "ores <length> <width> [level]"
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- maxLevel = 0
- local currentFuelLevel = turtle.getFuelLevel()
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(mission)
- print(instructions)
- print(usage)
- return
- end
- if nArgs < 2 or 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
- 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
- if nArgs == 3 then
- maxLevel = ((math.floor(tonumber(args[3]) / 3)) * 3) + 1
- if maxLevel == nil then
- print("\"", args[3], "\" is not a valid start level")
- return
- end
- end
- targetArea = length * width
- areaCovered = 1
- currentSlot = 2
- chestSlot = 16
- garbageSlot = 3
- maxSlot = 16
- 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()
- for i=1,10 do
- if not turtle.up() then
- if turtle.detectUp() then
- digUp()
- else
- turtle.attackUp()
- sleep(2)
- end
- else
- break
- end
- end
- y = y+1
- end
- function rise(nBlocks)
- local i
- for i=1,nBlocks do
- up()
- end
- end
- function down()
- for i=1,10 do
- if not turtle.down() then
- if turtle.detectDown() then
- digDown()
- else
- turtle.attackDown()
- sleep(2)
- end
- else
- break
- end
- end
- y = y-1
- 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(keepTrackOfArea)
- if keepTrackOfArea == nil then
- keepTrackOfArea = true
- end
- while turtle.detect() do -- This loop added in case of falling sand or whatever
- dig()
- end
- for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
- if not turtle.forward() then
- if turtle.detect() then
- dig()
- else
- turtle.attack()
- sleep(2)
- end
- else
- break
- end
- end
- if keepTrackOfArea then
- areaCovered = areaCovered + 1
- 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 dig()
- if noMoreEmptySlots() then
- jettisonGarbage()
- goEmptyInventoryToChestAndReturn()
- end
- turtle.dig()
- end
- function digUp()
- if noMoreEmptySlots() then
- jettisonGarbage()
- goEmptyInventoryToChestAndReturn()
- end
- turtle.digUp()
- end
- function digDown()
- if noMoreEmptySlots() then
- jettisonGarbage()
- goEmptyInventoryToChestAndReturn()
- end
- turtle.digDown()
- 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(homeFace)
- if x < 0 then
- setFace(east)
- while x < 0 do
- forward()
- end
- else
- if x > 0 then
- setFace(west)
- while x > 0 do
- forward()
- end
- end
- end
- if z < 0 then
- setFace(north)
- while z < 0 do
- forward()
- end
- else
- if z > 0 then
- setFace(south)
- while z > 0 do
- forward()
- end
- end
- end
- if y > 0 then
- while y > 0 do
- down()
- end
- end
- if homeFace ~= nil then
- setFace(homeFace)
- end
- end
- function returnToMark()
- if markZ < 0 then
- setFace(south)
- while z > markZ do
- forward(false)
- end
- else
- if markZ > 0 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
- setFace(markFace)
- 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
- function ignoreUp()
- for slot=1,3 do
- turtle.select(slot)
- if turtle.compareUp() then
- return true
- end
- end
- return false
- end
- function ignoreDown()
- for slot=1,3 do
- turtle.select(slot)
- if turtle.compareDown() then
- return true
- end
- end
- return false
- end
- function mineUp()
- while turtle.detectUp() do
- digUp()
- end
- end
- function mineDown()
- if turtle.detectDown() then
- digDown()
- end
- end
- function mineAtLevel()
- end
- function layBlock()
- turtle.select(materialSlot)
- if maxLevel > 2 then
- if turtle.detectUp() then
- if turtle.compareUp() then
- return
- else
- digUp()
- end
- end
- if ensureMaterial() then
- turtle.placeUp()
- end
- else
- if turtle.detectDown() then
- if turtle.compareDown() then
- return
- else
- 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
- -- MAIN PROGRAM
- turtle.select(1)
- if currentFuelLevel ~= "unlimited" then
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel() then
- print("No fuel")
- return
- end
- end
- end
- function mineLoop()
- while areaCovered < targetArea do
- if abort then
- break
- end
- for w=1,width do
- if abort then
- break
- end
- for z=1,length do
- if abort then
- break
- end
- if not ignoreUp() then
- mineUp()
- end
- if not ignoreDown() then
- mineDown()
- end
- if z < length then
- forward()
- end
- end
- if nextTurn == "left" then
- if areaCovered < targetArea then
- left()
- forward()
- left()
- nextTurn = "right"
- end
- else
- if areaCovered < targetArea then
- right()
- forward()
- right()
- nextTurn = "left"
- end
- end
- end
- end
- end
- function inventoryGettingFull()
- return ((turtle.getItemCount(14) > 0) or (turtle.getItemCount(15) > 0))
- end
- function noMoreEmptySlots()
- return (turtle.getItemCount(15) > 0)
- end
- markX = 0
- markY = 0
- markZ = 0
- markFace = nil
- 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
- jettisonCount = 0
- function jettisonGarbage()
- jettisonCount = jettisonCount + 1
- print("Jettison count ", jettisonCount)
- turtle.select(garbageSlot)
- for i=4,15 do
- turtle.select(i)
- if turtle.compareTo(garbageSlot) then
- turtle.dropDown()
- end
- end
- if turtle.getItemCount(15) > 0 then
- turtle.select(15)
- for i=4,13 do
- if turtle.getItemCount(i) == 0 then
- turtle.transferTo(i)
- end
- end
- end
- turtle.select(1)
- end
- purgeCount = 0
- function purgeGarbage()
- purgeCount = purgeCount + 1
- print("Purge garbage ", purgeCount)
- if turtle.detectDown() then
- turtle.digDown()
- end
- turtle.select(garbageSlot)
- for i=4,15 do
- turtle.select(i)
- if turtle.compareTo(garbageSlot) then
- turtle.dropDown()
- end
- end
- if turtle.getItemCount(14) > 0 then
- turtle.select(14)
- for i=4,13 do
- if turtle.getItemCount(i) == 0 then
- turtle.transferTo(i)
- end
- end
- end
- if turtle.getItemCount(15) > 0 then
- turtle.select(15)
- for i=4,13 do
- if turtle.getItemCount(i) == 0 then
- turtle.transferTo(i)
- end
- end
- end
- turtle.select(1)
- end
- function collapseInventory()
- turtle.select(15)
- for i=4,15 do
- if turtle.getItemCount(i) == 0 then
- turtle.transferTo(i)
- end
- end
- end
- function dump()
- -- setFace(east)
- --[[ turtle.select(chestSlot)
- if not turtle.compare() then
- plantChest()
- end
- --]]
- for i=1,3 do
- turtle.select(i)
- itemCount = turtle.getItemCount(i)
- if itemCount > 0 then
- turtle.drop(itemCount-1)
- end
- end
- for i=4,15 do
- turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- if not turtle.drop() then
- print("Dump failed. Chest is full.")
- return
- end
- end
- end
- turtle.select(1)
- setFace(north)
- end
- function plantChest()
- turtle.select(chestSlot)
- setFace(east)
- forward()
- mineUp()
- left()
- forward()
- mineUp()
- left()
- forward()
- left()
- left()
- turtle.place()
- right()
- forward()
- left()
- turtle.place()
- setFace(east)
- turtle.select(1)
- end
- y = 0
- --up()
- mineLevel = 1
- saveY = y
- plantChest()
- setFace(north)
- while mineLevel <= maxLevel do
- areaCovered = 1
- nextTurn = "left"
- mineLoop()
- home()
- while y > 0 do
- down()
- end
- setFace(east)
- dump()
- if mineLevel < maxLevel then
- mineLevel = mineLevel + 3
- while y < mineLevel-1 do
- up()
- end
- else
- break
- end
- end
- function sealItOff()
- turtle.select(4)
- turtle.up()
- turtle.placeUp()
- turtle.down()
- turtle.placeUp()
- turtle.place()
- turtle.turnLeft()
- turtle.place()
- turtle.turnRight()
- end
- print(missionMessage)
- print(" Current fuel level is ", turtle.getFuelLevel())
- home(north)
- sealItOff()
- *** pattern
- args = {...}
- nArgs = #args
- version = "pattern: Rev 0.1"
- mission = "Lay down the specified pattern."
- instructions = "Place stack(s) of material starting in first slot."
- usage = "usage: pattern <pattern name>"
- -- Water roof to cover a 10 x 10 structure
- roof10x10 = {
- " = ",
- " === ",
- " ===== ",
- " ======= ",
- " ========== ",
- " ============ ",
- " ============== ",
- " ======== ======= ",
- " ================= ",
- " =================== ",
- " ===================== ",
- "======= =============== ",
- " =============== =======",
- " ===================== ",
- " =================== ",
- " ================= ",
- " ======= ======== ",
- " ============== ",
- " ============ ",
- " ========== ",
- " ======= ",
- " ===== ",
- " === ",
- " = ",
- }
- funnel12 = {
- "============",
- "============",
- "============",
- "============",
- "============",
- "===== =====",
- "===== =====",
- "============",
- "============",
- "============",
- "============",
- "============",
- }
- fullShape = {
- " = ",
- " === ",
- " ===== ",
- " ======= ",
- " ========= ",
- " =========== ",
- " ============= ",
- " =============== ",
- "=================",
- " =============== ",
- " ============= ",
- " =========== ",
- " ========= ",
- " ======= ",
- " ===== ",
- " === ",
- " = ",
- }
- reducedShape = {
- " ",
- " = ",
- " === ",
- " ===== ",
- " ======= ",
- " ========= ",
- " =========== ",
- " ============= ",
- " =============== ",
- " ============= ",
- " =========== ",
- " ========= ",
- " ======= ",
- " ===== ",
- " === ",
- " = ",
- " ",
- }
- walls = {
- " = ",
- " = = ",
- " = = ",
- " = = ",
- " = = ",
- " = = ",
- " = = ",
- " = = ",
- "= =",
- " = = ",
- " = = ",
- " = = ",
- " = = ",
- " = = ",
- " = = ",
- " = = ",
- " = ",
- }
- x = 0
- y = 0
- z = 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
- materialSlot = 1
- face = north
- nextTurn = "right"
- 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
- 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 turn()
- if nextTurn == "left" then
- left()
- else
- right()
- end
- forward()
- if nextTurn == "left" then
- left()
- nextTurn = "right"
- else
- right()
- nextTurn = "left"
- end
- end
- function forward()
- 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
- if face == north then z = z+1 return end
- if face == west then x = x-1 return end
- if face == south then z = z-1 return end
- if face == east 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(east)
- while x < 0 do
- forward()
- end
- else
- if x > 0 then
- setFace(west)
- while x > 0 do
- forward()
- end
- end
- end
- if z < 0 then
- setFace(north)
- while z < 0 do
- forward()
- end
- else
- if z > 0 then
- setFace(south)
- while z > 0 do
- forward()
- end
- end
- end
- if y > 0 then
- while y > 0 do
- down()
- end
- end
- setFace(0)
- end
- function layPattern(pattern)
- for i=1,#pattern do
- row = pattern[i]
- length = string.len(row)
- if face == north then
- for j=1,string.len(row) do
- char = string.sub(row, j, j)
- if char ~= " " then
- ensureMaterial()
- turtle.select(materialSlot)
- turtle.placeDown()
- end
- if j < length then
- forward()
- end
- end
- else
- for j=string.len(row),1,-1 do
- char = string.sub(row, j, j)
- if char ~= " " then
- ensureMaterial()
- turtle.select(materialSlot)
- turtle.placeDown()
- end
- if j > 1 then
- forward()
- end
- end
- end
- turn()
- end
- end
- if nArgs < 1 then
- print(usage)
- return
- end
- if args[1] == "roof10x10" then
- layPattern(roof10x10)
- elseif args[1] == "funnel12" then
- layPattern(funnel12)
- elseif args[1] == "full" then
- layPattern(fullShape)
- elseif args[1] == "reduced" then
- layPattern(reducedShape)
- elseif args[1] == "walls" then
- layPattern(walls)
- else
- print("Unknown pattern: \"", args[1], "\"")
- return
- end
- home()
- *** pit
- -- 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())
- *** pop
- version = "pop: Rev 0.1"
- mission= "Protect Our Portal"
- instructions = "Place stack(s) of material starting in first slot."
- args = {...}
- nArgs = #args
- usage = "pop"
- materialSlot = 1
- length = 8
- width = 6
- function place()
- ensureMaterial()
- turtle.place()
- end
- function placeUp()
- ensureMaterial()
- turtle.placeUp()
- end
- function placeDown()
- ensureMaterial()
- turtle.placeDown()
- 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 < 6 then
- -- print("Attempting to refill slot ", materialSlot)
- for i=2,12 do
- turtle.select(i)
- if turtle.compareTo(materialSlot) then
- turtle.transferTo(materialSlot)
- end
- end
- end
- turtle.select(materialSlot)
- end
- function buildWall(length)
- for i=1,length do
- ensureMaterial()
- turtle.select(materialSlot)
- placeUp()
- placeDown()
- if i == length then
- turtle.turnLeft()
- end
- turtle.back()
- place()
- end
- end
- turtle.forward()
- turtle.turnRight()
- turtle.up()
- buildWall(length)
- buildWall(width-1)
- buildWall(length-1)
- turtle.down()
- turtle.turnLeft()
- turtle.turnLeft()
- shell.run("floor 4 7 4")
- *** quadchute
- -- quadchute
- -- Builds a tower up or down with 2x2 interior
- -- Written by HarvDad, June 2014
- args = {...}
- nArgs = #args
- version = "quadchute: Rev 0.1"
- mission = "Builds tower with 2x2 interior to levels specified"
- instructions = "Place stack(s) of material starting in first slot."
- usage = "quadchute <up | down> <levels>"
- x = 0
- y = 0
- z = 0
- down = 0
- up = 1
- direction = 0
- materialSlot = 1
- ladderSlot = 16
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- face = north
- returnToEarth = true
- function up()
- if not turtle.detectUp() then
- turtle.up()
- y = y+1
- end
- end
- function down()
- if not turtle.detectDown() then
- turtle.down()
- y = y-1
- end
- end
- function left()
- if face == north then face = west turtle.turnLeft() return end
- if face == west then face = south turtle.turnLeft() return end
- if face == south then face = east turtle.turnLeft() return end
- if face == east then face = north turtle.turnLeft() return end
- print("function left\(\): Bad face value: ", face)
- end
- function right()
- if face == north then face = east turtle.turnRight() return end
- if face == west then face = north turtle.turnRight() return end
- if face == south then face = west turtle.turnRight() return end
- if face == east then face = south turtle.turnRight() return end
- print("function right\(\): Bad face value: ", face)
- end
- function forward()
- 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 == north then z = z+1 return end
- if face == west then x = x-1 return end
- if face == south then z = z-1 return end
- if face == east then x = x+1 return end
- end
- function back()
- turtle.back()
- if face == north then z = z-1 return end
- if face == west then x = x+1 return end
- if face == south then z = z+1 return end
- if face == east then x = x-1 return end
- end
- function wall()
- ensureMaterial()
- turtle.select(materialSlot)
- turtle.placeDown()
- ensureMaterial()
- turtle.select(materialSlot)
- turtle.placeUp()
- back()
- ensureMaterial()
- turtle.select(materialSlot)
- turtle.place()
- ensureMaterial()
- turtle.select(materialSlot)
- turtle.placeDown()
- ensureMaterial()
- turtle.select(materialSlot)
- turtle.placeUp()
- back()
- ensureMaterial()
- turtle.select(materialSlot)
- turtle.place()
- end
- function segment()
- wall()
- left()
- back()
- wall()
- left()
- back()
- wall()
- left()
- back()
- wall()
- end
- function nextLevel()
- if direction == up then
- up()
- up()
- up()
- left()
- back()
- else
- down()
- down()
- down()
- left()
- back()
- end
- 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
- if not (nArgs > 1) then
- print(usage)
- return
- end
- direction = args[1]
- if not (direction == "up" or direction == "down") then
- print("\"", args[1], "\" is not a valid direction")
- return
- end
- if direction == "up" then
- direction = up
- else
- direction = down
- end
- levels = tonumber(args[2])
- if levels == nil then
- print("\"", args[2], "\" is not valid for levels")
- return
- end
- if levels < 1 then
- print("levels must be a positive number greater than zero")
- return
- end
- levels = tonumber(args[2])
- if nArgs == 3 then
- if args[3] == "true" then
- returnToEarth = true
- elseif args[3] == "false" then
- returnToEarth = false
- else
- print("3rd argument must be 'true' or 'false'")
- return
- end
- end
- up()
- for i=1,levels do
- segment()
- if i < levels then
- nextLevel()
- end
- end
- up()
- up()
- left()
- back()
- back()
- back()
- back()
- down()
- if returnToEarth then
- turtle.select(ladderSlot)
- while y > 0 do
- turtle.place()
- if y > 0 then
- down()
- end
- end
- turtle.place()
- end
- *** quarry
- -- 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)
- *** r
- -- r
- -- Turns and/or move the turtle to the right
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- distance = nil;
- if (nArgs == 1 and args[1]== "help") then
- print("Turns and/or moves the turtle to the right")
- print("Usage: r [distance]")
- return
- end
- if nArgs == 1 then
- distance = tonumber(args[1])
- if distance == nil then
- print(args[1], " is not a valid distance")
- return
- end
- end
- -- MAIN PROGRAM
- turtle.turnRight()
- if distance ~= nil then
- turtle.select(1)
- if currentFuelLevel ~= "unlimited" then
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel() then
- print("No fuel")
- return
- end
- end
- end
- for i=1,distance do
- turtle.forward()
- end
- end
- *** rmall
- -- rmall: Delete all files listed here
- -- Version 0.1
- files = {
- "allfiles",
- "b",
- "branch",
- "build",
- "bwb",
- "cactus",
- "chute",
- "clear",
- "collect",
- "copyall",
- "d",
- "dump",
- "eatlava",
- "f",
- "fabdrain",
- "floor",
- "l",
- "logger",
- "ores",
- "quarry",
- "r",
- "room",
- "shaft",
- "sifter",
- "sprinkle",
- "suck",
- "tree",
- "trees",
- "tube",
- "u",
- "wall",
- "walls",
- "wt"
- }
- count = 0
- for i=1,#files do
- if (shell.run("rm " .. files[i])) ~= nil then
- count = count + 1
- end
- end
- print("Files deleted: ", count)
- *** room
- -- room
- -- Creates a room with solid walls
- -- Clears the interior
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- version = "room: Rev 3.2"
- usage = "Usage: room <length> <width> <height>"
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- patchSlot = 1
- chestSlot = 15
- torchSlot = 16
- garbageSlot = 5
- torchSpacing = 2
- torchSpot = 5
- nextTurn = "left"
- addTorches = false
- -- Status data for marking a spot to return to after going 'home'
- markX = 0
- markY = 0
- markZ = 0
- markFace = 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
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print("Creates a room with solid walls")
- print("Clears the interior")
- print("Turtle should be pre-fueled")
- print("Slot 1 is for patch material")
- print("Slot 5 is for garbage material")
- print("Slot 15 is for storage chests")
- print("Slot 16 is for torches")
- 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
- 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 patch()
- if turtle.getItemCount(patchSlot) < 2 then
- gatherPatchMaterial()
- if turtle.getItemCount(patchSlot) < 2 then
- abort = true
- areaCovered = targetArea
- missionMessage = "Aborted: No more wall patch"
- return
- end
- end
- turtle.select(patchSlot)
- turtle.place()
- end
- function patchUp()
- if turtle.getItemCount(patchSlot) < 2 then
- gatherPatchMaterial()
- if turtle.getItemCount(patchSlot) < 2 then
- abort = true
- areaCovered = targetArea
- missionMessage = "Aborted: No more patch material"
- return
- end
- end
- turtle.select(patchSlot)
- turtle.placeUp()
- gatherPatchMaterial();
- end
- function patchDown()
- if turtle.getItemCount(patchSlot) < 2 then
- gatherPatchMaterial()
- if turtle.getItemCount(patchSlot) < 2 then
- abort = true
- areaCovered = targetArea
- missionMessage = "Aborted: No more patch material"
- return
- end
- end
- turtle.select(patchSlot)
- turtle.placeUp()
- gatherPatchMaterial();
- end
- function patchDown()
- turtle.select(patchSlot)
- turtle.placeDown()
- gatherPatchMaterial();
- end
- function gatherPatchMaterial()
- local i
- patchCount = turtle.getItemCount(patchSlot)
- garbageCount = turtle.getItemCount(garbageSlot)
- if patchCount < 3 or garbageCount < 20 then
- -- print("Attempting to refill slot ", patchSlot)
- for i=2,14 do
- turtle.select(i)
- if turtle.compareTo(patchSlot) then
- turtle.transferTo(patchSlot, 64-patchCount)
- -- print("Transferred ", 64 - patchCount, " cobble to slot ", patchSlot)
- end
- if turtle.compareTo(garbageSlot) then
- turtle.transferTo(garbageSlot)
- end
- end
- end
- turtle.select(patchSlot)
- 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()
- for i=1,10 do
- if not turtle.up() then
- if turtle.detectUp() then
- digUp()
- else
- turtle.attackUp()
- sleep(2)
- end
- else
- break
- end
- end
- y = y+1
- end
- function down()
- for i=1,10 do
- if not turtle.down() then
- if turtle.detectDown() then
- digDown()
- else
- turtle.attackDown()
- sleep(2)
- end
- else
- break
- end
- end
- y = y-1
- end
- 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 dig()
- if noMoreEmptySlots() then
- -- jettisonGarbage()
- goEmptyInventoryToChestAndReturn()
- end
- turtle.dig()
- end
- function digUp()
- if noMoreEmptySlots() then
- -- jettisonGarbage()
- goEmptyInventoryToChestAndReturn()
- end
- turtle.digUp()
- end
- function digDown()
- if noMoreEmptySlots() then
- -- jettisonGarbage()
- goEmptyInventoryToChestAndReturn()
- end
- turtle.digDown()
- end
- function noMoreEmptySlots()
- return (turtle.getItemCount(14) > 0)
- 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
- -- digAboveForward: This function requires that the current level is already cleared
- function DigAboveForward()
- while turtle.detect() do -- This loop added in case of falling sand or whatever
- turtle.dig()
- end
- end
- function forward(keepTrackOfArea)
- if keepTrackOfArea == nil then
- keepTrackOfArea = true
- end
- turtle.select(chestSlot)
- if not turtle.compare() then
- turtle.select(garbageSlot)
- turtle.place() -- in case we're facing an undectable water/lava source block, replace
- end
- turtle.select(patchSlot)
- while turtle.detect() do -- This loop added in case of falling sand or whatever
- dig()
- end
- for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
- if not turtle.forward() then
- if turtle.detect() then
- dig()
- else
- turtle.attack()
- sleep(2)
- end
- else
- break
- end
- end
- if keepTrackOfArea then
- areaCovered = areaCovered + 1
- end
- torchCheck()
- 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 sealRow(distance)
- local count = 0
- turtle.select(patchSlot)
- for count=1,distance do
- if y == height-1 then
- patchUp()
- if height > 1 then
- turtle.digDown()
- end
- -- turtle.placeUp() -- experiment: if solid block is there it will just fail. Maybe help with lava/water?
- end
- if y == 0 then
- patchDown()
- if height > 1 then
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- end
- right()
- patch()
- left()
- if count < distance then
- forward()
- end
- end
- end
- function torchCheck()
- if addTorches then
- if y == 1 then
- if (x % torchSpacing) == 0 then
- if (z % torchSpacing) == 0 then
- turtle.select(torchSlot)
- turtle.placeDown()
- end
- end
- end
- end
- turtle.select(patchSlot)
- end
- function clearLevel()
- areaCovered = 0
- setFace(west)
- forward()
- setFace(north)
- forward()
- if y == height-1 then
- turtle.select(patchSlot)
- turtle.placeUp()
- end
- nextTurn = "left"
- areaCovered = 0
- while areaCovered < targetArea do
- if abort then
- break
- end
- for w=1,width-2 do
- if abort then
- -- print("clearLevel: ABORT ABORT ABORT")
- break
- end
- for l=1,length-2 do
- if abort then
- break
- end
- if l < length-2 then
- forward()
- if (y == 0) then
- turtle.placeDown()
- if height > 1 then
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- end
- if y == height-1 then
- turtle.select(patchSlot) -- experiment: if solid block is there, it will just fail. Who cares?
- if turtle.getItemCount(patchSlot) < 1 then
- print("clearLevel: Ran out of patch material!")
- end
- turtle.placeUp()
- if height > 1 then
- turtle.digDown()
- end
- end
- end
- end
- if w == width-2 then
- -- print("There's no more to do at this level. Bailing out.")
- levelsCompleted = levelsCompleted + 1
- return
- end
- if w <= width-3 then
- if nextTurn == "left" then
- left()
- forward()
- if y == 0 then
- if height > 1 then
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- turtle.placeDown()
- end
- if y == height-1 then
- turtle.select(patchSlot)
- turtle.placeUp()
- if height > 1 then
- turtle.digDown()
- end
- end
- left()
- nextTurn = "right"
- else
- right()
- forward()
- if y == 0 then
- if height > 1 then
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- turtle.placeDown()
- end
- if y == height-1 then
- turtle.select(patchSlot)
- turtle.placeUp()
- if height > 1 then
- turtle.digDown()
- end
- end
- right()
- nextTurn = "left"
- end
- else
- break
- end
- end
- end
- levelsCompleted = levelsCompleted + 1
- end
- function bounce()
- local saveY = y
- for i=1,255 do
- if y > 0 then
- turtle.down()
- y = y - 1
- else
- break
- end
- end
- dump()
- while y<saveY do
- turtle.up()
- y = y + 1
- end
- end
- function sealLevel()
- sealRow(length)
- left()
- sealRow(width)
- left()
- sealRow(length)
- left()
- sealRow(width)
- left()
- end
- function sealWalls()
- local i = 0
- for i=1,height do
- sealLevel()
- if y ~= 1 then
- clearLevel()
- end
- if i < height then
- up()
- home()
- end
- end
- end
- function sealCeiling()
- areaCovered = 0
- while areaCovered < targetArea do
- if abort then
- break
- end
- for w=1,width do
- if abort then
- break
- end
- for z=1,length do
- if abort then
- break
- end
- if not turtle.detectUp() then
- patchUp()
- end
- if z < length then
- forward()
- end
- end
- if w < width then
- if nextTurn == "left" then
- left()
- forward()
- left()
- nextTurn = "right"
- else
- right()
- forward()
- right()
- nextTurn = "left"
- end
- else
- return
- end
- end
- end
- end
- function sealFloor()
- areaCovered = 0
- while areaCovered < targetArea do
- if abort then
- break
- end
- for w=1,width do
- if abort then
- break
- end
- for z=1,length do
- if abort then
- break
- end
- if not turtle.detectDown() then
- patchDown()
- end
- while turtle.detectUp() do
- turtle.digUp()
- end
- if z < length then
- forward()
- end
- end
- if w < width then
- if nextTurn == "left" then
- left()
- forward()
- left()
- nextTurn = "right"
- else
- right()
- forward()
- right()
- nextTurn = "left"
- end
- else
- return
- end
- end
- end
- end
- function dump()
- turtle.select(chestSlot)
- if not turtle.compareDown() then
- turtle.digDown()
- turtle.placeDown()
- setFace(west)
- forward()
- turtle.digDown()
- turtle.select(chestSlot)
- turtle.placeDown()
- setFace(east)
- forward()
- setFace(north)
- end
- gatherPatchMaterial()
- for i=2,4 do
- turtle.select(i)
- if not turtle.compareTo(patchSlot) then
- if not turtle.dropDown() then
- print("Dump failed. Chest is full.")
- return
- end
- end
- end
- for i=6,14 do
- turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- if not turtle.dropDown() then
- print("Dump failed. Chest is full.")
- return
- end
- end
- end
- turtle.select(patchSlot)
- setFace(north)
- end
- function home()
- if x < 0 then
- setFace(east)
- while x < 0 do
- forward()
- end
- else
- if x > 0 then
- setFace(west)
- while x > 0 do
- forward()
- end
- end
- end
- if z < 0 then
- setFace(north)
- while z < 0 do
- forward()
- end
- else
- if z > 0 then
- setFace(south)
- while z > 0 do
- forward()
- end
- end
- end
- setFace(north)
- nextTurn = "left"
- end
- function ceiling()
- while y < height-1 do
- up()
- end
- end
- function floor()
- while y > 0 do
- y = y - 1
- turtle.down()
- end
- end
- workHeight = height
- levelsCompleted = 0
- -- Clear the top level and seal the ceiling
- setFace(east)
- turtle.select(patchSlot)
- for i=1,height-1 do
- up()
- patch()
- end
- setFace(north)
- sealLevel()
- home()
- bounce()
- clearLevel()
- home()
- while y > 0 do
- turtle.down()
- y = y-1
- end
- dump()
- -- Clear the first level and seal the floor
- sealLevel()
- home()
- dump()
- clearLevel()
- home()
- dump()
- setFace(north)
- -- Clear the rest of the levels
- addTorches = true -- torchCheck only adds torches when turtle is at level 1
- workHeight = 1
- while levelsCompleted < height do
- while y < workHeight do
- up()
- end
- sealLevel()
- home()
- bounce()
- clearLevel()
- home()
- while y > 0 do
- turtle.down()
- y = y-1
- end
- dump()
- workHeight = workHeight + 1
- end
- home()
- while y > 0 do
- turtle.down()
- y = y-1
- end
- print(missionMessage)
- print("Fuel level is now ", turtle.getFuelLevel())
- *** shaft
- -- shaft
- -- Digs a vertical shaft straight down to the depth specified by the user
- -- If supplied, ladder is placed
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- print("shaft: Rev 2.2")
- 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
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print("Digs a vertical shaft straight down.")
- print("This program assumes turtle is pre-fueled.")
- print("Usage: shaft <depth>")
- print("Specify the number of blocks to dig down.")
- print("If desired, place ladder in slot ", ladderSlot, ".")
- return
- end
- if nArgs ~= 1 then
- print("Usage: shaft <depth>")
- return
- end
- targetDepth = tonumber(args[1])
- if targetDepth == nil then
- print("\"", args[1], "\" is not a valid depth")
- return
- end
- if (targetDepth < 0) then
- 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)
- turtle.place()
- left()
- turtle.place()
- left()
- turtle.place()
- left()
- 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
- 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 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
- while depth > targetDepth do
- up()
- end
- homeDown()
- else
- while depth < targetDepth do
- down()
- end
- homeUp()
- end
- setFace(north)
- print(missionMessage)
- *** sifter
- -- Sifter: Load contents of chest into specific chests
- -- Rev 1.0
- x = 0
- y = 0
- z = 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
- face = north
- -- Chest coordinates are relative to the drop chest
- -- x, y, z, dropping direction, turtle slot number
- boneChest = {-4, 0, 0, east, 1}
- pearlChest = {-4, 0, 2, east, 2}
- arrowChest = {-4, 0, 3, east, 3}
- rottenFleshChest = {-2, 0, 6, south, 8}
- heartChest = {0, 0, 6, south, 7}
- junk2Chest = {1, 0, 6, south, -1}
- gunpowderChest = {4, 0, 3, west, 6}
- zombieBrainChest = {4, 0, 2, west, 5}
- stringChest = {4, 0, 0, west, 4}
- boneSlot = 1
- pearlSlot = 2
- arrowSlot = 3
- function uploadDropChest()
- for slot=1,16 do
- turtle.select(slot)
- turtle.suckDown()
- end
- end
- function load(chest, startSlot, endSlot)
- goToChest(chest)
- for slot=startSlot, endSlot do
- turtle.select(slot)
- turtle.suckDown()
- end
- end
- function dropItems(slot)
- turtle.select(slot)
- turtle.drop(turtle.getItemCount(slot) - 1)
- for i=9,16 do
- turtle.select(i)
- if turtle.compareTo(slot) then
- turtle.drop()
- end
- end
- end
- function dump(chest, startSlot, endSlot)
- goToChest(chest)
- for slot=startSlot, endSlot do
- turtle.select(slot)
- turtle.dropDown()
- end
- end
- function unloadInto(chest)
- saveFace = face
- setFace(chest[4])
- slot = chest[5]
- if (slot > 0) then
- dropItems(slot)
- end
- setFace(saveFace)
- 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 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 goTo(X, Y, Z)
- print("goTo(", X, ", ", Y, ", ", Z, ")")
- if y == nil then print("y is nil") end
- if Y == nil then print("Y is nil") end
- if y ~= Y then
- if y < Y then
- while y < Y do
- turtle.up()
- y = y + 1
- end
- else
- while y > Y do
- turtle.down()
- y = y - 1
- end
- end
- end
- if x ~= X then
- if x < X then
- setFace(east)
- while x < X do
- turtle.forward()
- x = x + 1
- end
- else
- setFace(west)
- while x > X do
- turtle.forward()
- x = x - 1
- end
- end
- end
- if z ~= Z then
- if z < Z then
- setFace(north)
- while z < Z do
- turtle.forward()
- z = z + 1
- end
- else
- setFace(south)
- while z > Z do
- turtle.forward()
- z = z - 1
- end
- end
- end
- end
- function go(direction, nBlocks)
- setFace(direction)
- forward(nBlocks)
- end
- function back(nBlocks)
- for i=1,nBlocks do
- turtle.back()
- end
- if face == north then z = z - nBlocks end
- if face == south then z = z + nBlocks end
- if face == east then x = x - nBlocks end
- if face == west then x = x + nBlocks end
- end
- function forward(nBlocks)
- for i=1,nBlocks do
- turtle.forward()
- end
- if face == north then z = z + nBlocks end
- if face == south then z = z - nBlocks end
- if face == east then x = x + nBlocks end
- if face == west then x = x - nBlocks end
- end
- function goToChest(chestData)
- back(2)
- goTo(chestData[1], chestData[2], chestData[3])
- setFace(chestData[4])
- end
- function atChest()
- if (x == boneChest[1]) and (z == boneChest[3]) then return boneChest end
- if (x == pearlChest[1]) and (z == pearlChest[3]) then return pearlChest end
- if (x == arrowChest[1]) and (z == arrowChest[3]) then return arrowChest end
- if (x == rottenFleshChest[1]) and (z == rottenFleshChest[3]) then return rottenFleshChest end
- if (x == heartChest[1]) and (z == heartChest[3]) then return heartChest end
- if (x == junk2Chest[1]) and (z == junk2Chest[3]) then return junk2Chest end
- if (x == gunpowderChest[1]) and (z == gunpowderChest[3]) then return gunpowderChest end
- if (x == zombieBrainChest[1]) and (z == zombieBrainChest[3]) then return zombieBrainChest end
- if (x == stringChest[1]) and (z == stringChest[3]) then return stringChest end
- return nil
- end
- function siftRun()
- uploadDropChest()
- back(2)
- setFace(west)
- forward(4)
- setFace(north)
- for i=1,8 do
- forward(1)
- chest = atChest()
- if chest ~= nil then
- unloadInto(chest)
- end
- end
- setFace(east)
- for i=1,8 do
- forward(1)
- chest = atChest()
- if chest ~= nil then
- unloadInto(chest)
- end
- end
- setFace(south)
- for i=1,8 do
- forward(1)
- chest = atChest()
- if chest ~= nil then
- unloadInto(chest)
- end
- end
- forward(3)
- for i=9,16 do
- turtle.select(i)
- turtle.dropDown()
- end
- back(3)
- setFace(west)
- forward(4)
- setFace(north)
- forward(2)
- end
- function home()
- if (face == east) then
- setFace(south)
- while z > -2 do
- turtle.forward()
- z = z - 1
- end
- setFace(east)
- while x < 0 do
- turtle.forward()
- x = x + 1
- end
- setFace(north)
- forward(2)
- else
- setFace(south)
- while z > -2 do
- turtle.forward()
- z = z - 1
- end
- setFace(west)
- while x < 0 do
- turtle.forward()
- x = x + 1
- end
- setFace(north)
- forward(2)
- end
- end
- -- MAIN PROGRAM
- loopCount = 0
- loopInterval = 50 -- Approximately 5 minutes
- siftRun()
- while true do
- if loopCount < loopInterval then
- sleep(2)
- loopCount = loopCount + 1
- else
- siftRun()
- loopCount = 0
- end
- end
- *** sprinkle
- -- sprinkle: Place torches in rectangular pattern on horizontal surface
- -- Version: 1.0
- itemSlot = 16
- x = 0
- y = 0
- z = 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
- face = north
- length = 9
- width = 9
- interval = 4
- args = {...}
- nArgs = #args
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print("Lays down a pattern of items of length and width.")
- print("Usage: sprinkle <length> <width>")
- return
- end
- if nArgs ~= 2 then
- print("Usage: sprinkle <distance>")
- 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")
- return
- end
- width = tonumber(args[2])
- if width == nil then
- print("\"", args[1], "\" is not a valid width")
- return
- end
- if width < 1 then
- print("width must be a positive number greater than zero")
- return
- 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 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 forward(nBlocks)
- for i=1,nBlocks do
- turtle.forward()
- end
- if face == north then z = z+nBlocks return end
- if face == west then x = x-nBlocks return end
- if face == south then z = z-nBlocks return end
- if face == east then x = x+nBlocks return end
- end
- function layRow(nBlocks)
- turtle.select(itemSlot)
- for i=0,nBlocks do
- if (i % interval) == 0 then
- turtle.placeDown()
- end
- if i < nBlocks then
- forward(1)
- end
- end
- end
- function layPattern(l, w)
- -- setFace(north)
- layRow(w - 1)
- setFace(west)
- layRow(l - 1)
- setFace(south)
- layRow(w - 1)
- setFace(east)
- layRow(l - 1)
- setFace(north)
- end
- radius = interval
- -- Let's let the parent program get to start position
- --[[
- turtle.up()
- turtle.select(itemSlot)
- turtle.placeDown()
- -- Move to starting position
- setFace(north)
- forward(radius)
- setFace(west)
- forward(radius)
- --]]
- layPattern(length, width)
- setFace(north)
- *** suck
- while true do
- turtle.suckUp()
- turtle.suck()
- turtle.turnRight()
- turtle.suck()
- turtle.turnRight()
- turtle.suck()
- turtle.turnRight()
- turtle.suck()
- turtle.turnRight()
- end
- *** timer
- --time = os.time() print("time= ", time)
- clockTime = os.clock()
- print("clockTime: ", clockTime, ", minutes: ", clockTime / 60)
- fakeTime = 33.4567
- 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
- --[[
- formattedTime = textutils.formatTime(timeNumber, true)
- print("formattedTime: ", formattedTime)
- elapsedTime = 12.706 - 11.637
- print("elapsedTime: ", textutils.formatTime(elapsedTime, true))
- --]]
- --print("Elapsed time: ")
- --displayTime(915432.4567)
- print("Time since last reboot:")
- displayTime(clockTime)
- *** topgun
- while true do
- turtle.attackUp()
- sleep(3)
- end
- *** tree
- -- tree
- -- Cut down a single tree directly in front of turtle
- -- Trees must be 1x1 trunks with no branches
- -- Program assumes turtle is pre-fueled
- -- Written by HarvDad, April 2014
- args = {...}
- nArgs = #args
- version = "tree: Rev 0.1"
- mission = "Cut down a single tree"
- usage = "tree"
- x = 0
- y = 0
- z = 0
- face = 0
- missionMessage = "Mission complete."
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- areaCovered = 1
- currentSlot = 2
- materialSlot = 1
- maxSlot = 16
- nextTurn = "left"
- 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 forward()
- 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 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()
- for i=1,10 do
- if not turtle.up() then
- if turtle.detectUp() then
- digUp()
- else
- turtle.attackUp()
- sleep(2)
- end
- else
- break
- end
- end
- y = y+1
- end
- function down()
- for i=1,10 do
- if not turtle.down() then
- if turtle.detectDown() then
- digDown()
- else
- turtle.attackDown()
- sleep(2)
- end
- else
- break
- end
- end
- y = y-1
- end
- function chopTree()
- turtle.dig()
- forward()
- while turtle.detectUp() do
- turtle.digUp()
- turtle.up()
- end
- while not turtle.detectDown() do
- turtle.suckDown()
- turtle.down()
- end
- end
- function replantTree()
- if turtle.getItemCount(saplingSlot) > 0 then
- left()
- left()
- turtle.select(saplingSlot)
- turtle.place()
- left()
- left()
- end
- end
- -- Main program
- chopTree()
- replantTree()
- if abort then
- print("Mission aborted")
- end
- print(missionMessage)
- *** trees
- -- trees
- -- Harvests trees laid out in rows
- -- Trees must be 1x1 trunks with no branches
- -- If supplied, saplings will be replanted
- -- Program assumes turtle is pre-fueled
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, April 2014
- args = {...}
- nArgs = #args
- version = "trees: Rev 1.2"
- mission = "Harvest trees laid out in rows"
- usage = "Usage: trees <rows>"
- x = 0
- y = 0
- z = 0
- face = 0
- missionMessage = "Mission complete."
- abort = false
- logSlot = 1
- saplingSlot = 16
- markerSlot = 15
- local currentFuelLevel = turtle.getFuelLevel()
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(mission)
- print("Place sample log in slot ", logSlot)
- print("Place sapling supply in slot ", saplingSlot)
- print("Place marker block in slot ", markerSlot)
- print(usage)
- return
- end
- if nArgs ~= 1 then
- print(usage)
- return
- end
- rows = tonumber(args[1])
- if rows == nil then
- print("\"", args[1], "\" is not a valid row count.")
- return
- end
- if rows < 1 then
- print("Number of rows must be positive number.")
- end
- areaCovered = 1
- currentSlot = 2
- materialSlot = 1
- maxSlot = 16
- nextTurn = "left"
- 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 forward()
- 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 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()
- for i=1,10 do
- if not turtle.up() then
- if turtle.detectUp() then
- turtle.digUp()
- else
- turtle.attackUp()
- sleep(2)
- end
- else
- break
- end
- end
- y = y+1
- end
- function down()
- for i=1,10 do
- if not turtle.down() then
- if turtle.detectDown() then
- turtle.digDown()
- else
- turtle.attackDown()
- sleep(2)
- end
- else
- break
- end
- end
- y = y-1
- end
- function focusSaplings()
- local i
- saplingCount = turtle.getItemCount(saplingSlot)
- if saplingCount < 2 then
- for i=2,15 do
- turtle.select(i)
- if turtle.compareTo(saplingSlot) then
- turtle.transferTo(saplingSlot)
- end
- end
- end
- turtle.select(saplingSlot)
- end
- function nextTree()
- success = false
- while not success do
- if not turtle.forward() then
- turtle.select(logSlot)
- if turtle.compare() then
- success = true
- else
- turtle.select(saplingSlot)
- if turtle.compare() then
- success = true
- else
- if turtle.detect() then
- missionMessage = "Encountered unexpected block."
- sleep(5)
- abort = true
- return success
- else
- for i=1,10 do
- success = turtle.forward()
- if not success then
- turtle.attack()
- sleep(2)
- else
- if z >= lastZ or x <= lastX then
- return
- end
- updateLocation()
- end
- end
- missionMessage = "Could not get past unknown entity"
- abort = true
- return success
- end
- end
- end
- else
- if z >= lastZ or x <= lastX then
- return success
- end
- updateLocation()
- end
- -- print("nextTree: x = ", x, ", z = ", z)
- end
- return success
- end
- function updateLocation()
- 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 harvestSapling()
- turtle.dig()
- forward()
- up()
- turtle.select(saplingSlot)
- turtle.placeDown()
- turtle.select(logSlot)
- end
- function harvestTree()
- turtle.dig()
- forward()
- while turtle.detectUp() do
- turtle.digUp()
- up()
- end
- while y > 1 do
- down()
- end
- turtle.select(saplingSlot)
- turtle.placeDown()
- turtle.select(logSlot)
- end
- function chopTree()
- turtle.select(logSlot)
- if turtle.compare() then
- turtle.dig()
- forward()
- while turtle.detectUp() do
- turtle.digUp()
- turtle.up()
- end
- while not turtle.detectDown() do
- turtle.suckDown()
- turtle.down()
- end
- else
- turtle.dig()
- forward()
- end
- forward()
- totalCut = totalCut + 1
- end
- function replantTree()
- focusSaplings()
- if turtle.getItemCount(saplingSlot) > 0 then
- left()
- left()
- turtle.select(saplingSlot)
- turtle.place()
- left()
- left()
- end
- end
- function _home()
- setFace(east)
- forward()
- if z < 0 then
- setFace(north)
- while z < 0 do
- forward()
- end
- else
- if z > 0 then
- setFace(south)
- while z > 0 do
- forward()
- end
- end
- end
- if x < 0 then
- setFace(west)
- while x < 0 do
- forward()
- end
- else
- if x > 0 then
- setFace(east)
- while x > 0 do
- forward()
- end
- end
- end
- setFace(north)
- nextTurn = "left"
- end
- function nextRow()
- if nextTurn == "left" then
- left()
- for i=1,interval do
- forward()
- end
- left()
- nextTurn = "right"
- else
- right()
- for i=1,interval do
- forward()
- end
- right()
- nextTurn = "left"
- end
- end
- function facingTree()
- turtle.select(logSlot)
- return turtle.compare()
- end
- function facingSapling() -- WARNING!!! turtles cannot dependably compare to saplings (BUG)
- turtle.select(saplingSlot)
- return turtle.compare()
- end
- function facingMarker()
- turtle.select(markerSlot)
- return turtle.compare()
- end
- spanSet = false
- function harvestForward()
- if y > 0 then
- turtle.dig()
- forward()
- down()
- else
- if facingTree() then
- harvestTree()
- else
- if facingMarker() and x == 0 then
- spanZ = z
- spanSet = true
- else
- if turtle.detect() then -- sadly, we must assume it's a sapling
- harvestSapling()
- else
- forward()
- end
- end
- end
- end
- end
- function home()
- if z == 0 then
- left()
- while x < 0 do
- forward()
- end
- else
- right()
- while x < 0 do
- forward()
- end
- forward()
- right()
- while z > 0 do
- forward()
- end
- right()
- forward()
- end
- setFace(north)
- end
- -- Main program
- totalCut = 0
- rowCount = 0
- columnCount = 0
- firstZ = 0
- lastZ = 0
- lastX = 0
- interval = 5
- setFace(north)
- nextTurn = "left"
- --[[
- for i=1,30 do
- if facingTree() then
- break
- end
- forward()
- end
- --]]
- firstZ = z
- lastZ = firstZ + ((rows-1) * interval)
- spanZ = lastZ - firstZ
- spanX = -12
- spanZ = 100
- lastJ = 0
- for i = 1,rows do
- for j = 1,spanZ do
- -- forward()
- harvestForward()
- if x == 0 and spanSet then
- break
- end
- end
- if i < rows then
- nextRow()
- end
- end
- if y > 0 then
- turtle.dig()
- forward()
- down()
- end
- print("Heading home...")
- home()
- if abort then
- print("Mission aborted")
- end
- print(missionMessage)
- *** tube
- -- tube
- -- Creates a 2x2 tunnel of the length specified by the user
- -- All sides are sealed for protection
- -- Torches, if supplied, will be placed on the right-hand wall at 10 block intervals
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- print("tube: Rev 3.1")
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- patchSlot = 1
- torchSlot = 16
- local currentFuelLevel = turtle.getFuelLevel()
- zipTube = false
- usage = "Usage: tube <length> [zip]"
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print("Creates a tunnel with sealed sides, top, and bottom.")
- print("If zip tube, tunnel is 1x2, else 2x2")
- print("This programs assumes the turtle is pre-fueled.")
- print("Place patch material (like cobblestone) in slot ", patchSlot)
- print("If torches are desired, place torches in slot ", torchSlot)
- print(usage)
- return
- end
- if nArgs ~= 1 and nArgs ~= 2 then
- print("Usage: tube <length>")
- return
- end
- if nArgs == 2 then
- if args[2] == "zip" then
- zipTube = true
- else
- print("Usage: tube <length>")
- end
- 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
- targetArea = length
- areaCovered = 1;
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- 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()
- local success = false
- repeat
- while turtle.detectUp() do -- This loop added in case of falling sand or whatever
- turtle.digUp()
- end
- success = turtle.up()
- until success
- y = y+1
- end
- function rise(nBlocks)
- local i
- for i=1,nBlocks do
- up()
- end
- end
- function descend(nBlocks)
- local i
- for i=1,nBlocks do
- down()
- end
- end
- function down()
- if turtle.detectDown() then
- turtle.digDown()
- end
- turtle.down()
- y = y-1
- 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 -- This loop trys to handle pests (mob) that might be in the way
- if not turtle.forward() then
- if turtle.detect() then
- dig()
- else
- turtle.attack()
- sleep(2)
- end
- else
- break
- end
- end
- if keepTrackOfArea then
- areaCovered = areaCovered + 1
- 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 gatherPatchMaterial()
- local i
- patchCount = turtle.getItemCount(patchSlot)
- if patchCount < 3 then
- -- print("Attempting to refill slot ", patchSlot)
- for i=2,14 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 patch()
- gatherPatchMaterial()
- turtle.select(patchSlot)
- turtle.place()
- end
- function patchUp()
- gatherPatchMaterial()
- turtle.select(patchSlot)
- turtle.placeUp()
- end
- function patchDown()
- gatherPatchMaterial()
- turtle.select(patchSlot)
- turtle.placeDown()
- end
- function patchUpper()
- patchUp()
- left()
- patch()
- right()
- right()
- patch()
- left()
- end
- function patchLower()
- patchDown()
- left()
- patch()
- right()
- right()
- patch()
- left()
- end
- function patchTube()
- patchUp()
- patchDown()
- left()
- patch()
- right()
- right()
- patch()
- left()
- end
- addTorch = false
- torchSpacing = 6
- function mineForward()
- forward()
- if not turtle.detectDown() then
- patchDown()
- end
- right()
- if not turtle.detect() then
- patch()
- end
- up()
- if not turtle.detect() then
- patch()
- end
- left()
- if not turtle.detectUp() then
- patchUp()
- end
- if addTorch then
- if (torchSpot % torchSpacing) == 0 then
- left()
- turtle.select(torchSlot)
- turtle.place()
- right()
- end
- torchSpot = torchSpot + 1
- end
- turtle.down()
- 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 checkFuel()
- if currentFuelLevel == "unlimited" then
- return true
- end
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel(200) then
- areaCovered = targetArea
- missionMessage = "Mission aborted due to low fuel."
- abort = true
- return false
- end
- end
- return true
- end
- function home(targetY)
- -- print("home:face ", face, ", x = ", x, ", z = ", z)
- if x < 0 then
- setFace(east)
- while x < 0 do
- forward()
- end
- else
- if x > 0 then
- setFace(west)
- while x > 0 do
- forward()
- end
- end
- end
- if z < 0 then
- setFace(north)
- while z < 0 do
- forward()
- end
- else
- if z > 0 then
- setFace(south)
- while z > 0 do
- forward()
- end
- end
- end
- end
- function poopTorch()
- left()
- left()
- turtle.select(torchSlot)
- turtle.place()
- right()
- right()
- turtle.select(patchSlot)
- end
- -- MAIN PROGRAM
- turtle.select(patchSlot)
- 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 zipTube then
- print("Starting 1x2 zip tube...")
- for i=1,length do
- forward()
- patchLower()
- end
- patch()
- up()
- patch()
- left()
- left()
- patchUpper()
- for i=1,length-1 do
- forward()
- patchUpper()
- if (i % torchSpacing) == 0 then
- poopTorch()
- end
- end
- down()
- else
- print("Starting 2x2 double-wide tube...")
- for i=1,length do
- mineForward()
- end
- setFace(north)
- if not turtle.detect() then
- patch()
- end
- rise(1)
- if not turtle.detect() then
- patch()
- end
- descend(1)
- if not turtle.detect() then
- patch()
- end
- setFace(west)
- mineForward()
- if not turtle.detect() then
- patch()
- end
- rise(1)
- if not turtle.detect() then
- patch()
- end
- descend(1)
- setFace(south)
- addTorch = true
- torchSpot = 7
- for i=1,length-1 do
- mineForward()
- checkFuel()
- if abort then
- break
- end
- end
- end
- home(0)
- setFace(north)
- print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
- *** u
- -- u
- -- Moves the turtle the specified distance up
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- abort = false
- local currentFuelLevel = turtle.getFuelLevel()
- distance = 1;
- if (nArgs == 1 and args[1]== "help") then
- print("Moves the turtle the specified distance up")
- print("Usage: f [distance]")
- return
- end
- if nArgs == 1 then
- distance = tonumber(args[1])
- if distance == nil then
- print(args[1], " is not a valid distance")
- return
- end
- if distance > 75 then
- print("Maybe not a good idea")
- return
- end
- end
- -- MAIN PROGRAM
- turtle.select(1)
- if currentFuelLevel ~= "unlimited" then
- if currentFuelLevel < minimumFuel then
- if not turtle.refuel() then
- print("No fuel")
- return
- end
- end
- end
- for i=1,distance do
- turtle.up()
- end
- *** valve
- -- valve
- -- Pours and retrieves water at specified time intervals
- -- Written by HarvDad, June 2014
- args = {...}
- nArgs = #args
- version = "valve: Rev 1.0"
- mission = "Pour and retrieve water at intervals."
- instructions = "Place full bucket in slot 1, empty bucket in slot 2."
- usage1 = "usage: valve <direction> <t1> <t2>"
- usage2 = " direction = up, down, forward"
- usage3 = " t1 = seconds after pour"
- usage4 = " t2 = seconds after retrieval"
- secondsAfterPouring = 10
- secondsAfterRetrieving = 30
- up = 0
- down = 1
- forward = 2
- direction = 0
- abort = false
- function synchWithServerTime()
- print("Waiting for synch...")
- while true do
- time = os.time()
- timeStr = tostring(time)
- tailStr = string.sub(timeStr, -3)
- tail = tonumber(tailStr)
- if tail > 995 then
- break
- end
- sleep(0)
- end
- end
- function needWater()
- turtle.select(1)
- if turtle.compareTo(2) then
- return true
- end
- return false
- end
- -- prep: Retrieve any water possibly left over from previous run
- function prep()
- turtle.select(1)
- if turtle.compareTo(2) then
- if needWater() then
- turtle.placeUp()
- end
- if needWater() then
- turtle.placeDown()
- end
- if needWater() then
- turtle.place()
- end
- end
- end
- function updateWater()
- turtle.select(1)
- if direction == up then
- turtle.placeUp()
- elseif direction == down then
- turtle.placeDown()
- elseif direction == forward then
- if not needWater() then
- turtle.place()
- if not needWater() then
- print("Placing water forward failed.")
- print("Apparently can only place on solid block.")
- print("ComputerCraft bug? Misfeature?")
- abort = true
- end
- end
- else
- print("Invalid direction: ", direction)
- print("This should never happen.")
- abort = true
- end
- end
- function doTheRightThing()
- local naptime = 0
- turtle.select(1)
- if turtle.compareTo(2) then
- napTime = secondsAfterRetrieving
- -- print("Bucket is empty. Retrieving water.")
- else
- -- print("Bucket is full. Pouring water.")
- napTime = secondsAfterPouring
- end
- updateWater()
- return napTime
- end
- -- Main program
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(instructions)
- print(usage1)
- print(usage2)
- print(usage3)
- print(usage4)
- return
- end
- if nArgs < 3 or nArgs > 3 then
- print(usage1)
- print(usage2)
- print(usage3)
- print(usage4)
- return
- end
- if args[1] == "up" then
- direction = up
- elseif args[1] == "down" then
- direction = down
- elseif args[1] == "forward" then
- direction = forward
- else
- print("\"", args[1], "\" is not a valid direction.")
- return
- end
- secondsAfterPouring = tonumber(args[2])
- if secondsAfterPouring == nil then
- print("\"", args[2], "\" is not a valid time interval")
- return
- end
- if secondsAfterPouring < 1 then
- print("t1 must be a positive integer")
- return
- end
- secondsAfterRetrieving= tonumber(args[3])
- if secondsAfterRetrieving == nil then
- print("\"", args[3], "\" is not a valid time interval")
- return
- end
- if secondsAfterRetrieving < 1 then
- print("t2 must be a positive integer")
- return
- end
- if turtle.getItemCount(1) == 0 or turtle.getItemCount(2) == 0 then
- print("If no water running yet,")
- print(" place full bucket in slot 1.")
- print("If water is already running,")
- print(" place empty bucket in slot 1.")
- print("Always have empty bucket in slot 2 as comparitor.")
- return
- end
- prep()
- --synchWithServerTime()
- -- Main loop
- print("Beginning loop...")
- while true do
- if abort then
- break
- end
- napTime = doTheRightThing()
- sleep(napTime)
- end
- *** wall
- -- wall
- -- Build a wall of specified length and height specified
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, May 2014
- args = {...}
- nArgs = #args
- version = "walls: Rev 1.0"
- mission = "Build a wall of length and height specified"
- instructions = "Place stack(s) of material starting in first slot."
- usage = "wall <start> <end> <height>"
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- startLevel = 0
- local currentFuelLevel = turtle.getFuelLevel()
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(mission)
- print(instructions)
- print(usage)
- return
- end
- if nArgs ~= 2 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
- height = tonumber(args[2])
- if startLevel == nil then
- print("\"", args[2], "\" is not a valid height")
- return
- end
- currentSlot = 2
- materialSlot = 1
- maxSlot = 16
- 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()
- 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 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()
- 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
- 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 back()
- turtle.back()
- 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
- if y > 0 then
- while y > 0 do
- down()
- end
- end
- setFace(0)
- 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
- 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
- directon = "forward"
- function layRow(nBlocks)
- turtle.select(currentSlot)
- for i=1,nBlocks do
- if turtle.getItemCount(currentSlot) > 0 then
- turtle.placeDown()
- if direction == "forward" then
- if i < length then
- forward()
- end
- else
- if i < length then
- back()
- end
- end
- else
- currentSlot = currentSlot + 1
- if turtle.getItemCount(currentSlot) > 0 then
- turtle.select(currentSlot)
- turtle.placeDown()
- if direction == "forward" then
- if i < length then
- forward()
- end
- else
- if i < length then
- back()
- end
- end
- else
- print("Can't find more material")
- return
- end
- end
- end
- end
- -- MAIN PROGRAM
- turtle.select(1)
- print("fuelLevel = ", currentFuelLevel)
- print("length = ", length)
- 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
- notFinished = true
- direction = "forward"
- print("wall: Setting face to local north")
- setFace(0)
- z = 0
- while notFinished do
- if y == height then
- notFinished = false
- break
- end
- up()
- layRow(length)
- if direction == "forward" then
- direction = "backward"
- else
- direction = "forward"
- end
- end
- while z > 0 do
- turtle.back()
- z = z - 1
- end
- back()
- while y > 0 do
- turtle.down()
- y = y - 1
- end
- print(missionMessage)
- print(" Current fuel level is ", turtle.getFuelLevel())
- *** walls
- -- walls
- -- Puts up walls to the length, width, height specified
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, March 2014
- args = {...}
- nArgs = #args
- version = "walls: Rev 2.0"
- mission = "Put walls to the length, width, height specified"
- instructions = "Place stack(s) of material starting in first slot."
- usage = "walls <length> <width> <height>"
- x = 0
- y = 0
- z = 0
- face = 0
- minimumFuel = 100
- missionMessage = "Mission complete."
- abort = false
- startLevel = 0
- local currentFuelLevel = turtle.getFuelLevel()
- areaCovered = 0
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(mission)
- print(instructions)
- 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
- 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 startLevel == nil then
- print("\"", args[3], "\" is not a valid height")
- return
- end
- currentSlot = 2
- materialSlot = 1
- maxSlot = 16
- 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()
- 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 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()
- 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 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 y > 0 then
- while y > 0 do
- down()
- end
- end
- setFace(0)
- 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
- 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 layRow(nBlocks)
- turtle.select(currentSlot)
- for i=1,nBlocks do
- if turtle.getItemCount(currentSlot) > 0 then
- turtle.placeDown()
- turtle.forward()
- else
- currentSlot = currentSlot + 1
- if turtle.getItemCount(currentSlot) > 0 then
- turtle.select(currentSlot)
- turtle.placeDown()
- turtle.forward()
- else
- print("Can't find more material")
- return
- end
- end
- 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
- -- MAIN PROGRAM
- turtle.select(1)
- print("fuelLevel = ", currentFuelLevel)
- 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
- notFinished = true
- while notFinished do
- if y == height then
- notFinished = false
- break
- end
- up()
- setFace(0)
- layRow(length - 1)
- setFace(1)
- layRow(width - 1)
- setFace(2)
- layRow(length - 1)
- setFace(3)
- layRow(width - 1)
- end
- setFace(0)
- turtle.back()
- z = z - 1
- while y > 0 do
- turtle.down()
- y = y - 1
- end
- print(missionMessage)
- print(" Current fuel level is ", turtle.getFuelLevel())
- *** walls3
- version = "walls3: Rev 0.1"
- mission= "Place 3-block high walls at the length and width specified."
- instructions = "Place stack(s) of material starting in first slot."
- args = {...}
- nArgs = #args
- usage = "walls3 <length> <width>"
- materialSlot = 1
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(mission)
- print(instructions)
- print(usage)
- return
- end
- if nArgs ~= 2 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
- 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
- function place()
- ensureMaterial()
- turtle.place()
- end
- function placeUp()
- ensureMaterial()
- turtle.placeUp()
- end
- function placeDown()
- ensureMaterial()
- turtle.placeDown()
- 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 < 6 then
- -- print("Attempting to refill slot ", materialSlot)
- for i=2,12 do
- turtle.select(i)
- if turtle.compareTo(materialSlot) then
- turtle.transferTo(materialSlot)
- end
- end
- end
- turtle.select(materialSlot)
- end
- function buildWall(length)
- for i=1,length do
- ensureMaterial()
- turtle.select(materialSlot)
- placeUp()
- placeDown()
- if i == length then
- turtle.turnLeft()
- end
- turtle.back()
- place()
- end
- end
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.up()
- buildWall(length)
- buildWall(length-1)
- buildWall(length-1)
- buildWall(length-2)
- *** write
- -- write
- -- Builds the letters from the input text
- -- Written by HarvDad, June 2014
- version = "write: Rev 0.1"
- instructions = "Place stack(s) of material starting in first slot."
- usage = "write <text>"
- args = {...}
- nArgs = #args
- text = ""
- letters = {
- {"A", "====== ", " = =", " = =", " = =", "====== "},
- {"B", "=======", "= = =", "= = =", "= =", " == == "},
- {"C", " ===== ", "= =", "= =", "= =", " = = "},
- {"D", "=======", "= =", "= =", "= =", " ===== "},
- {"E", "=======", "= = =", "= = =", "= = =", "= ="},
- {"F", "=======", " = =", " = =", " = =", " ="},
- {"G", " ===== ", "= =", "= = =", "= = =", " === ="},
- {"H", "=======", " = ", " = ", " = ", "======="},
- {"I", " ", "= =", "=======", "= =", " "},
- {"J", " == ", "= ", "= ", "= ", " ======"},
- {"K", "=======", " = ", " = = ", " = = ", "= ="},
- {"L", "=======", "= ", "= ", "= ", "= "},
- {"M", "=======", " = ", " = ", " = ", "======="},
- {"N", "=======", " = ", " = ", " = ", "======="},
- {"O", " ===== ", "= =", "= =", "= =", " ===== "},
- {"P", "=======", " = =", " = =", " = =", " == "},
- {"Q", " ===== ", "= =", "= = =", "== =", "====== "},
- {"R", "=======", " = =", " == =", " = = =", "= == "},
- {"S", "= == ", "= = =", "= = =", "= = =", " == ="},
- {"T", " =", " =", "=======", " =", " ="},
- {"U", " ======", "= ", "= ", "= ", " ======"},
- {"V", " =====", " = ", "= ", " = ", " ====="},
- {"W", "=======", " = ", " == ", " = ", "======="},
- {"X", "== ==", " = = ", " = ", " = = ", "== =="},
- {"Y", " ===", " = ", "=== ", " = ", " ==="},
- {"Z", "== =", "= = =", "= = =", "= = =", "= =="},
- {"?", " = ", " =", "= == =", " = =", " == "},
- {"!", " ", " ", "= =====", " ", " "},
- {" ", " ", " ", " ", " ", " "},
- }
- materialSlot = 1
- x = 0
- y = 0
- z = 0
- face = 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
- goForward = 0
- goBackward = 1
- direction = goForward
- currentSlot = 1
- 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 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 moveForward(nBlocks)
- for i = 1, nBlocks do
- turtle.forward()
- if face == north then
- z = z + 1
- elseif face == south then
- z = z - 1
- elseif face == east then
- x = x + 1
- elseif face == west then
- x = x - 1
- end
- end
- end
- function moveBackward(nBlocks)
- for i = 1, nBlocks do
- turtle.back()
- if face == north then
- z = z - 1
- elseif face == south then
- z = z + 1
- elseif face == east then
- x = x - 1
- elseif face == west then
- x = x + 1
- end
- end
- end
- function home(targetY)
- -- print("home:face ", face, ", x = ", x, ", z = ", z)
- if x < 0 then
- setFace(east)
- while x < 0 do
- moveForward(1)
- end
- else
- if x > 0 then
- setFace(west)
- while x > 0 do
- moveForward(1)
- end
- end
- end
- if z < 0 then
- setFace(north)
- while z < 0 do
- moveForward(1)
- end
- else
- if z > 0 then
- setFace(south)
- while z > 0 do
- moveForward(1)
- end
- end
- end
- if y > 0 then
- while y > 0 do
- down()
- end
- end
- setFace(0)
- 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
- function getLetter(letter)
- letter = string.upper(letter)
- for i=1,#letters do
- if letters[i][1] == letter then
- return letters[i]
- end
- end
- print("Cannot find entry for \'", letter, "\'")
- return nil
- end
- function hitTheDeck()
- --print("hitTheDeck: y = ", y)
- while y > 0 do
- down()
- -- print(" Down")
- end
- -- print(" y ends at ", y)
- goingUp = true
- end
- goingUp = true
- function buildLetter(letter)
- letterData = getLetter(letter)
- turtle.select(1)
- if letter == " " then
- right()
- moveForward(7)
- left()
- return
- end
- --[[
- if letterData ~= nil then
- for i=1,#letterData do
- print(" ", letterData[i])
- end
- end
- --]]
- if letterData == nil then
- print("Cannot build \'", letter, "\'")
- return
- end
- for i=2,#letterData do
- column = letterData[i]
- height = string.len(column)
- if goingUp then
- for j=1,height do
- char = string.sub(column, j, j)
- if char ~= " " then
- ensureMaterial()
- turtle.select(materialSlot)
- turtle.place()
- end
- if j < height then
- up()
- end
- end
- else
- for j=height,1,-1 do
- char = string.sub(column, j, j)
- if char ~= " " then
- ensureMaterial()
- turtle.select(materialSlot)
- turtle.place()
- end
- if j > 1 then
- down()
- end
- end
- end
- if goingUp then
- goingUp = false
- else
- goingUp = true
- end
- right()
- moveForward(1)
- left()
- end
- end
- if nArgs == 0 then
- print(version)
- print(instructions)
- print(usage)
- return
- end
- text = ""
- for i=1,nArgs do
- text = text .. " " .. args[i]
- end
- text = string.sub(text, 2)
- for i=1,string.len(text) do
- buildLetter(string.sub(text,i,i))
- right()
- moveForward(1)
- -- hitTheDeck()
- moveForward(1)
- left()
- end
- home(0)*** wt
- print("Ready")
- rednet.open("right")
- while true do
- id, message = rednet.receive()
- print("received message \"", message, "\" from id ", id)
- if message == "quit" then
- break
- end
- shell.run(message)
- end
- rednet.close("right")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement