Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local runtimeArgs = { ... }
- local isReturningHome = false
- local isTerminating = false
- local xPos, yPos, zPos, curDir = 0, 0, 0, 0
- local directions = { [0] = "forward", [1] = "right", [2] = "back", [3] = "left" }
- local turnDirs = { right = turtle.turnRight, left = turtle.turnLeft }
- local digDirs = { up = turtle.digUp, down = turtle.digDown, forward = turtle.dig, back = turtle.dig, left = turtle.dig, right = turtle.dig }
- local moveDirs = { forward = turtle.forward, back = turtle.back, up = turtle.up, down = turtle.down }
- local placeDirs = { forward = turtle.place, up = turtle.placeDown }
- local fuelPerCoal = 0
- local currentlyDroppingOffMaterials = false
- local buildingBlocks = { ["minecraft:cobblestone"] = true, ["minecraft:cobbled_deepslate"] = true, ["minecraft:netherrack"] = true }
- local importantItems = { ["minecraft:coal"] = true }
- local shouldAlwaysSkip = { ["minecraft:coal"] = true }
- for k, v in pairs(buildingBlocks) do table.insert(importantItems, v) end
- function directionValFix(dir)
- return math.abs(dir % 4)
- end
- function printCurDir()
- --print("[+] Current Direction: " .. directions[curDir] .. " [-]")
- end
- function printCurPos()
- --print("[+] Current Pos: XPos- " .. xPos .. " YPos- " .. yPos .. "[-]")
- end
- function printCurrentFuel()
- --print("[+] Current Fuel Level: " .. turtle.getFuelLevel() .. " [-]")
- end
- function selectItem(itemID)
- --print("[+] Selecting Item...")
- for i = 1, 16 do
- local curItem = turtle.getItemDetail(i) or { name = "Empty" }
- --print("Checking Slot: " .. i .. " - " .. curItem.name)
- if curItem.name == itemID then
- turtle.select(i)
- --print("[-] Selected Item.")
- return true
- end
- end
- --print("[-!] Failed Select Item.")
- return false
- end
- function terminateProgram()
- if isTerminating then return end
- isTerminating = true
- print("!!! Terminating Program !!!")
- print("!!! Returning Home !!!")
- returnHome()
- print("!!! Terminated Program !!!")
- sleep(1)
- os.queueEvent("terminate")
- sleep(1)
- os.queueEvent("terminate")
- end
- function refuel()
- --printCurrentFuel()
- --print("[+] Refueling...")
- local returnHomeFuelAmount = math.abs(xPos) + math.abs(yPos) + math.abs(zPos) + 5
- local numCoal = 0
- if selectItem("minecraft:coal") then
- numCoal = turtle.getItemCount()
- end
- local curFuel = turtle.getFuelLevel()
- local shouldRefuel = curFuel <= returnHomeFuelAmount
- if fuelPerCoal == 0 and turtle.refuel(1) then
- fuelPerCoal = turtle.getFuelLevel() - curFuel
- curFuel = turtle.getFuelLevel()
- end
- if numCoal * fuelPerCoal <= returnHomeFuelAmount then
- terminateProgram()
- end
- if shouldRefuel then
- if not turtle.refuel(1) then
- while not turtle.refuel(1) and (not isReturningHome or curFuel <= 0) do
- sleep(0.5)
- print("!!! OUT OF FUEL !!!")
- selectItem("minecraft:coal")
- end
- end
- end
- --printCurrentFuel()
- end
- function turn(dir)
- --printCurDir()
- --print("[+] Turning: " .. dir .. "...")
- local dirVal = dir == "left" and -1 or 1
- curDir = directionValFix(curDir + dirVal)
- turnDirs[dir]()
- --print("[-] Turned: " .. dir .. ".")
- --printCurDir()
- end
- function move(dir)
- refuel()
- dropOffMaterials()
- --printCurPos()
- --print("[+] Moving: " .. dir .. "...")
- function actualMove(direct)
- digDirs[dir]()
- if dir ~= "up" and dir ~= "down" then
- if moveDirs[direct]() then
- if curDir == 0 then zPos = zPos + 1 end
- if curDir == 1 then xPos = xPos + 1 end
- if curDir == 2 then zPos = zPos - 1 end
- if curDir == 3 then xPos = xPos - 1 end
- --print("[-] Moved: " .. dir .. ".")
- else
- --print("[-!] Failed Moved: " .. dir .. ".")
- end
- else
- if moveDirs[direct]() then
- if dir == "up" then yPos = yPos + 1 end
- if dir == "down" then yPos = yPos - 1 end
- --print("[-] Moved: " .. dir .. ".")
- else
- --print("[-!] Failed Moved: " .. dir .. ".")
- end
- end
- end
- if dir ~= "up" and dir ~= "down" then
- if dir == "back" then
- turn("right")
- turn("right")
- end
- if dir == "right" or dir == "left" then
- turn(dir)
- end
- actualMove("forward")
- else
- actualMove(dir)
- end
- --printCurPos()
- end
- function build(dir, blockID)
- selectItem(blockID)
- return placeDirs[dir]()
- end
- function faceDirection(dir)
- if directions[directionValFix(curDir - 1)] == dir then
- turn("left")
- return
- end
- for i = 0, 3 do
- if directions[curDir] == dir then return end
- turn("right")
- end
- end
- function moveDirection(dir)
- if dir ~= "up" and dir ~= "down" then
- faceDirection(dir)
- move("forward")
- else
- move(dir)
- end
- end
- function moveTo(x, y, z)
- function axisMove(axis, curVal, toVal)
- if curVal ~= toVal then
- local changeVal = curVal > toVal and -1 or 1
- for curAxisVal = curVal, toVal - changeVal, changeVal do
- if axis == "x" then
- moveDirection(changeVal > 0 and "right" or "left")
- elseif axis == "y" then
- moveDirection(changeVal > 0 and "up" or "down")
- elseif axis == "z" then
- moveDirection(changeVal > 0 and "forward" or "back")
- end
- end
- end
- end
- axisMove("y", yPos, y)
- axisMove("x", xPos, x)
- axisMove("z", zPos, z)
- end
- function toBool(val)
- return val == "true" or val == true
- end
- function placeBuildingBlock(dir)
- for key, value in pairs(buildingBlocks) do
- if build(dir, key) then return true end
- end
- return false
- end
- function returnHome()
- if isReturningHome then return end
- isReturningHome = true
- moveTo(0, 0, 0)
- faceDirection("forward")
- isReturningHome = false
- end
- function dropOffMaterials()
- if currentlyDroppingOffMaterials then return false end
- local isInventoryFilled = true
- for i = 1, 16 do
- local curItem = turtle.getItemDetail(i) or { name = "Empty" }
- if curItem.name == "Empty" then
- isInventoryFilled = false
- break
- end
- end
- if isInventoryFilled then
- currentlyDroppingOffMaterials = true
- local startData = { dir = curDir, x = xPos, y = yPos, z = zPos }
- returnHome()
- local skippedItem = {}
- for i = 1, 16 do
- local curItem = turtle.getItemDetail(i) or { name = "Empty" }
- local curID = curItem.name
- local isImportant = importantItems[curID]
- local shouldDrop = isImportant and skippedItem[curID]
- if shouldDrop then
- turtle.select(i)
- turtle.dropDown()
- end
- if isImportant and not shouldAlwaysSkip[curID] then
- skippedItem[curID] = true
- end
- end
- currentlyDroppingOffMaterials = false
- moveTo(xPos, yPos, startData.z)
- moveTo(startData.x, yPos, zPos)
- moveTo(xPos, startData.y, zPos)
- faceDirection(directions[startData.dir])
- end
- end
- function promptDimensions()
- print("Enter the width of the area to clear:")
- local width = tonumber(read())
- print("Enter the length of the area to clear:")
- local length = tonumber(read())
- print("Enter the height of the area to clear:")
- local height = tonumber(read())
- return width, length, height
- end
- function clearArea(width, length, height)
- for h = 0, height - 1 do
- for l = 0, length - 1 do
- if l % 2 == 0 then
- for w = 0, width - 1 do
- moveTo(w, h, l)
- end
- else
- for w = width - 1, 0, -1 do
- moveTo(w, h, l)
- end
- end
- end
- end
- end
- local width, length, height = promptDimensions()
- clearArea(width, length, height)
- returnHome()
- -- End of Program
- dropOffMaterials()
- returnHome()
- --[[
- - Turns once to either direction
- turn("left")
- turn("right")
- - Fully turns til it reaches that direction based on the original orientation of the turtle when the program started
- faceDirection("forward")
- faceDirection("back")
- faceDirection("right")
- faceDirection("left")
- - Moves turtle based on its current orientation
- move("forward")
- move("back")
- move("right")
- move("left")
- move("up")
- move("down")
- - Moves turtle to inputted coords based on/to the original pos and direction the turtle was when program started
- (Will move on y axis first, then x, and last z.)
- moveTo(x, y, z)
- - Moves turtle in direction based on the original orientation of the turtle when the program started
- moveDirection("forward")
- moveDirection("back")
- moveDirection("right")
- moveDirection("left")
- moveDirection("up")
- moveDirection("down")
- - Searches in inventory and selects inputted item, if the item doesn't exist it will terminate the program so things don't break.
- selectItem(itemID)
- - Places the block in whatever direction based on the turtle's current orientation
- build("forward", blockID)
- build("up", blockID)
- build("down", blockID)]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement