Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --todo:
- -- maybe i can use slot 16 to detect fullness instead of available slot etc
- -- refueling during doChest etc
- -- maybe refuel limit = 1
- -- ge4QnKbY
- local programState = "done"
- local refuelingState = ""
- local curX = 0
- local curY = 0
- local curZ = 0
- local subStep = 0
- local sizeX = 0
- local sizeY = 0
- local sizeZ = 0
- local useEnderChest = false
- local useEnderFuel = false
- local useChest = false
- local useCobblestone = false
- local saveFile = "savedata"
- local availableSlots = 0
- local outputSlot = 0
- local cobbleSlot = 0
- local function getYesNo()
- local i
- local event, param
- while true do
- event, param = os.pullEvent("key")
- if param == 21 then
- return true
- end
- if param == 49 then
- return false
- end
- end
- end
- local function requestFuel()
- local i
- local c = false
- local event, param
- print("Press F to refuel from slot 16")
- print("Press Q to exit the program")
- if turtle.getFuelLevel() >= 64 then
- c = true
- print("Press C to continue quarry")
- end
- while true do
- event, param = os.pullEvent("key")
- if param == 33 then --f
- turtle.select(16)
- turtle.refuel()
- if not c and turtle.getFuelLevel() >= 64 then
- c = true
- print("Press C to continue quarry")
- end
- end
- if param == 16 then --q
- return false
- end
- if c and param == 46 then --c
- return true
- end
- end
- end
- local function restoreProgress()
- if fs.exists(saveFile) then
- local file = fs.open(saveFile,"r")
- programstate = tostring(file.readLine())
- refuelingstate = tostring(file.readLine())
- curX = tonumber(file.readLine())
- curY = tonumber(file.readLine())
- curZ = tonumber(file.readLine())
- subStep = tonumber(file.readLine())
- sizeX = tonumber(file.readLine())
- sizeY = tonumber(file.readLine())
- sizeZ = tonumber(file.readLine())
- if file.readLine() == "true" then useEnderChest = true else useEnderChest = false end
- if file.readLine() == "true" then useEnderFuel = true else useEnderFuel = false end
- if file.readLine() == "true" then useChest = true else useChest = false end
- if file.readLine() == "true" then useCobblestone = true else useCobblestone = false end
- file.close()
- end
- end
- local function saveProgress()
- local file = fs.open(saveFile,"w")
- file.write(tostring(programstate).."\n")
- file.write(tostring(refuelingstate).."\n")
- file.write(tostring(curX).."\n")
- file.write(tostring(curY).."\n")
- file.write(tostring(curZ).."\n")
- file.write(tostring(subStep).."\n")
- file.write(tostring(sizeX).."\n")
- file.write(tostring(sizeY).."\n")
- file.write(tostring(useEnderChest).."\n")
- file.write(tostring(useEnderFuel).."\n")
- file.write(tostring(useChest).."\n")
- file.write(tostring(useCobblestone).."\n")
- file.close()
- end
- --should eliminate these
- local function moveForward()
- turtle.dig()
- while not turtle.forward() do
- sleep(0)
- turtle.dig()
- end
- end
- local function moveUp()
- turtle.digUp()
- while not turtle.up() do
- sleep(0)
- turtle.digUp()
- end
- end
- local function moveDown()
- turtle.digDown()
- while not turtle.down() do
- sleep(0)
- turtle.digDown()
- end
- end
- local function doRefuel()
- refuelingState = "refuel"
- saveProgress()
- while turtle.getFuelLevel() < 64 do
- if turtle.getItemCount(2) > 0 then
- turtle.select(2)
- while not turtle.placeUp() do
- sleep(0)
- turtle.digUp()
- end
- end
- turtle.select(16)
- turtle.drop()
- for maxstacks = 1, 1 do
- turtle.suckUp()
- if not turtle.refuel() then turtle.drop() end
- end
- end
- turtle.select(2)
- turtle.digUp()
- refuelingState = ""
- saveProgress()
- end
- local function doEmpty()
- refuelingState = "empty"
- saveProgress()
- if turtle.getItemCount(1) > 0 then
- turtle.select(1)
- while not turtle.placeUp() do
- sleep(0)
- turtle.digUp()
- end
- end
- for slot = outputSlot, 16 do
- while turtle.getItemCount(slot) > 0 do
- turtle.select(slot)
- turtle.dropUp()
- sleep(0)
- end
- end
- turtle.select(1)
- turtle.digUp()
- refuelingState = ""
- saveProgress()
- end
- local function doChest()
- programState = "chest"
- saveProgress()
- --orient toward x
- local limit
- limit = 1
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- if (curX % 2 == 0) == isReversed then
- --facing = 1
- turtle.turnRight()
- else
- --facing = 3
- turtle.turnLeft()
- end
- end
- limit = limit + curY
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- moveUp()
- end
- limit = limit + curX
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- moveForward()
- end
- limit = limit + 1
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- turtle.turnLeft()
- end
- limit = limit + curZ
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- moveForward()
- end
- limit = limit + 1
- while subStep < limit do
- for slot = outputSlot, 16 do
- turtle.select(slot)
- while not turtle.drop() do
- sleep(0)
- end
- end
- subStep = subStep + 1
- saveProgress()
- turtle.turnRight()
- turtle.turnRight()
- end
- limit = limit + curZ
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- moveForward()
- end
- limit = limit + 1
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- turtle.turnRight()
- end
- limit = limit + curX
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- moveForward()
- end
- limit = limit + curY
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- moveDown()
- end
- limit = limit + 1
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- if (curX % 2 == 0) == isReversed then
- --facing = 1
- turtle.turnRight()
- else
- --facing = 3
- turtle.turnLeft()
- end
- end
- turtle.select(cobbleSlot)
- programState = "quarry"
- subStep = 0
- saveProgress()
- end
- local function doReturn()
- programState = "return"
- saveProgress()
- --orient toward x
- local limit
- limit = 1
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- if (curX % 2 == 0) == isReversed then
- --facing = 1
- turtle.turnRight()
- else
- --facing = 3
- turtle.turnLeft()
- end
- end
- limit = limit + curY
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- moveUp()
- end
- limit = limit + curX
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- moveForward()
- end
- limit = limit + 1
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- turtle.turnLeft()
- end
- limit = limit + curZ
- while subStep < limit do
- subStep = subStep + 1
- saveProgress()
- moveForward()
- end
- if useChest then
- limit = limit + 1
- while subStep < limit do
- for slot = outputSlot, 16 do
- turtle.select(slot)
- while not turtle.drop() do
- sleep(0)
- end
- end
- subStep = subStep + 1
- saveProgress()
- end
- end
- turtle.turnRight()
- turtle.turnRight()
- turtle.select(1)
- programState = "done"
- subStep = 0
- saveProgress()
- end
- local function checkInventory()
- if turtle.getItemCount(16) == 0 then
- return
- end
- if useCobblestone then
- turtle.select(cobbleSlot)
- turtle.drop(turtle.getItemCount(cobbleSlot) - 1)
- for slot = outputSlot, 16 do
- while turtle.compareTo(slot) do
- turtle.select(slot)
- turtle.drop()
- for swap = 16, slot, -1 do
- if turtle.getItemCount(swap) > 0 then
- turtle.select(swap)
- turtle.transferTo(slot)
- break
- end
- end
- turtle.select(cobbleSlot)
- end
- end
- if turtle.getItemCount(16) == 0 then
- return
- end
- end
- if useEnderChest then
- doEmpty()
- elseif useChest then
- doChest()
- else
- print("Inventory Full")
- local full = true
- while full do
- full = false
- sleep(5)
- for slot = outputSlot, 16 do
- if turtle.getItemCount(slot) > 0 then
- full = true
- break
- end
- end
- end
- end
- end
- local function doQuarry()
- if turtle.getFuelLevel() < 64 then
- if useEnderFuel then
- doRefuel()
- else
- if not requestFuel() then
- programState = "done"
- return
- end
- end
- end
- turtle.select(cobbleSlot)
- if curY > 0 then
- checkInventory()
- turtle.digUp()
- end
- if curY < sizeY - 1 then
- checkInventory()
- turtle.digDown()
- end
- checkInventory()
- if curX % 2 == 0 then
- if isReversed then
- if curZ == 0 then
- if curX == 0 then
- if curY >= sizeY - 2 then
- programState = "return"
- return
- else
- checkInventory()
- for y = 1, 3 do
- if curY >= sizeY - 2 then
- break
- end
- curY = curY + 1
- moveDown()
- end
- turtle.turnRight()
- turtle.turnRight()
- isReversed = false
- end
- else
- curX = curX - 1
- turtle.turnRight()
- moveForward()
- turtle.turnRight()
- end
- else
- curZ = curZ - 1
- moveForward()
- end
- else
- if curZ == sizeZ - 1 then
- if curX == sizeX - 1 then
- if curY >= sizeY - 2 then
- programState = "return"
- return
- else
- checkInventory()
- for y = 1, 3 do
- if curY >= sizeY - 2 then
- break
- end
- curY = curY + 1
- moveDown()
- end
- turtle.turnRight()
- turtle.turnRight()
- isReversed = true
- end
- else
- curX = curX + 1
- turtle.turnRight()
- moveForward()
- turtle.turnRight()
- end
- else
- curZ = curZ + 1
- moveForward()
- end
- end
- else -- curX % 2 ~= 0
- if isReversed then
- if curZ == sizeZ - 1 then
- if curX == 0 then
- if curY >= sizeY - 2 then
- programState = "return"
- return
- else
- checkInventory()
- for y = 1, 3 do
- if curY >= sizeY - 2 then
- break
- end
- curY = curY + 1
- moveDown()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- isReversed = true
- end
- else
- curX = curX - 1
- turtle.turnLeft()
- moveForward()
- turtle.turnLeft()
- end
- else
- curZ = curZ + 1
- moveForward()
- end
- else
- if curZ == 0 then
- if curX == sizeX - 1 then
- if curY >= sizeY - 2 then
- programState = "return"
- return
- else
- checkInventory()
- for y = 1, 3 do
- if curY >= sizeY - 2 then
- break
- end
- curY = curY + 1
- moveDown()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- isReversed = true
- end
- else
- curX = curX + 1
- turtle.turnLeft()
- moveForward()
- turtle.turnLeft()
- end
- else
- curZ = curZ - 1
- moveForward()
- end
- end
- end
- saveProgress()
- end
- local function getArgs()
- if sizeX == nil or sizeY == nil or sizeZ == nil then
- return false
- end
- sizeX = tonumber(sizeX)
- sizeY = tonumber(sizeY)
- sizeZ = tonumber(sizeZ)
- programState = "done"
- refuelingState = ""
- curX = 0
- curY = 0
- curZ = 0
- subStep = 0
- useEnderChest = false
- useEnderFuel = false
- useChest = false
- useCobblestone = false
- print("Excavate right " .. sizeX .. ", down " .. sizeY .. ", forward " .. sizeZ)
- print("Is this correct?")
- if not getYesNo() then
- return false
- end
- print("Use Ender Chest in slot 1 for output?")
- useEnderChest = getYesNo()
- if useEnderChest then
- print("Use Ender Chest in slot 2 for fuel?")
- useEnderFuel = getYesNo()
- else
- print("Use Chest behind turtle for output?")
- useChest = getYesNo()
- end
- if useEnderChest then
- if useEnderFuel then
- outputSlot = 3
- else
- outputSlot = 2
- end
- else
- outputSlot = 1
- end
- print("Discard Cobblestone? (requires cobblestone in slot" .. outputSlot .. ")")
- useCobblestone = getYesNo()
- local fuelRequired = (sizeX * sizeZ) * math.ceil(sizeY / 3) + sizeY
- print("Fuel Required: " .. fuelRequired)
- print("Fuel Current: " .. turtle.getFuelLevel())
- if not useEnderFuel and turtle.getFuelLevel() < fuelRequired then
- if not requestFuel() then
- return false
- end
- end
- print("Start Quarry with these parameters?")
- if not getYesNo() then
- return false
- end
- programState = "init"
- saveProgress()
- return true
- end
- local function main()
- if refuelingstate == "refuel" then
- doRefuel()
- end
- if refuelingstate == "empty" then
- doEmpty()
- end
- if useEnderChest then
- if turtle.getItemCount(1) == 0 then
- print("Place Ender Chest in slot 1")
- while turtle.getItemCount(1) == 0 do
- sleep(1)
- end
- end
- if useEnderFuel then
- if turtle.getItemCount(2) == 0 then
- print("Place Ender Chest in slot 2")
- while turtle.getItemCount(2) == 0 do
- sleep(1)
- end
- end
- outputSlot = 3
- else
- outputSlot = 2
- end
- else
- outputSlot = 1
- end
- cobbleSlot = outputSlot
- if useCobblestone then
- if turtle.getItemCount(cobbleSlot) == 0 then
- print("Place Cobblestone in slot " .. cobbleSlot)
- while turtle.getItemCount(cobbleSlot) == 0 do
- sleep(1)
- end
- end
- outputSlot = outputSlot + 1
- end
- while true do
- if programState == "quarry" then
- doQuarry()
- end
- if programState == "chest" then
- doChest()
- end
- if programState == "init" then
- if curY < sizeY - 1 then
- curY = curY + 1
- moveDown()
- end
- programState = "quarry"
- end
- if programState == "return" then
- doReturn()
- break
- end
- end
- end
- restoreProgress()
- if programState == "done" then
- sizeX, sizeY, sizeZ = ...
- if not getArgs() then
- print("Usage")
- print(" startup x y z")
- return
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement