Advertisement
SufficingPit

Area Clear

Dec 24th, 2024 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.42 KB | None | 0 0
  1. local runtimeArgs = { ... }
  2.  
  3. local isReturningHome = false
  4. local isTerminating = false
  5.  
  6. local xPos, yPos, zPos, curDir = 0, 0, 0, 0
  7.  
  8. local directions = { [0] = "forward", [1] = "right", [2] = "back", [3] = "left" }
  9. local turnDirs = { right = turtle.turnRight, left = turtle.turnLeft }
  10. local digDirs = { up = turtle.digUp, down = turtle.digDown, forward = turtle.dig, back = turtle.dig, left = turtle.dig, right = turtle.dig }
  11. local moveDirs = { forward = turtle.forward, back = turtle.back, up = turtle.up, down = turtle.down }
  12. local placeDirs = { forward = turtle.place, up = turtle.placeDown }
  13.  
  14. local fuelPerCoal = 0
  15. local currentlyDroppingOffMaterials = false
  16.  
  17. local buildingBlocks = { ["minecraft:cobblestone"] = true, ["minecraft:cobbled_deepslate"] = true, ["minecraft:netherrack"] = true }
  18. local importantItems = { ["minecraft:coal"] = true }
  19. local shouldAlwaysSkip = { ["minecraft:coal"] = true }
  20.  
  21. for k, v in pairs(buildingBlocks) do table.insert(importantItems, v) end
  22.  
  23. function directionValFix(dir)
  24.     return math.abs(dir % 4)
  25. end
  26.  
  27. function printCurDir()
  28.     --print("[+] Current Direction: " .. directions[curDir] .. " [-]")
  29. end
  30.  
  31. function printCurPos()
  32.     --print("[+] Current Pos: XPos- " .. xPos .. " YPos- " .. yPos .. "[-]")
  33. end
  34.  
  35. function printCurrentFuel()
  36.     --print("[+] Current Fuel Level: " .. turtle.getFuelLevel() .. " [-]")
  37. end
  38.  
  39. function selectItem(itemID)
  40.     --print("[+] Selecting Item...")
  41.     for i = 1, 16 do
  42.         local curItem = turtle.getItemDetail(i) or { name = "Empty" }
  43.         --print("Checking Slot: " .. i .. " - " .. curItem.name)
  44.         if curItem.name == itemID then
  45.             turtle.select(i)
  46.             --print("[-] Selected Item.")
  47.             return true
  48.         end
  49.     end
  50.     --print("[-!] Failed Select Item.")
  51.     return false
  52. end
  53.  
  54. function terminateProgram()
  55.     if isTerminating then return end
  56.     isTerminating = true
  57.     print("!!! Terminating Program !!!")
  58.     print("!!! Returning Home !!!")
  59.     returnHome()
  60.     print("!!! Terminated Program !!!")
  61.     sleep(1)
  62.     os.queueEvent("terminate")
  63.     sleep(1)
  64.     os.queueEvent("terminate")
  65. end
  66.  
  67. function refuel()
  68.     --printCurrentFuel()
  69.     --print("[+] Refueling...")
  70.     local returnHomeFuelAmount = math.abs(xPos) + math.abs(yPos) + math.abs(zPos) + 5
  71.     local numCoal = 0
  72.     if selectItem("minecraft:coal") then
  73.         numCoal = turtle.getItemCount()
  74.     end
  75.     local curFuel = turtle.getFuelLevel()
  76.     local shouldRefuel = curFuel <= returnHomeFuelAmount
  77.     if fuelPerCoal == 0 and turtle.refuel(1) then
  78.         fuelPerCoal = turtle.getFuelLevel() - curFuel
  79.         curFuel = turtle.getFuelLevel()
  80.     end
  81.     if numCoal * fuelPerCoal <= returnHomeFuelAmount then
  82.         terminateProgram()
  83.     end
  84.     if shouldRefuel then
  85.         if not turtle.refuel(1) then
  86.             while not turtle.refuel(1) and (not isReturningHome or curFuel <= 0) do
  87.                 sleep(0.5)
  88.                 print("!!! OUT OF FUEL !!!")
  89.                 selectItem("minecraft:coal")
  90.             end
  91.         end
  92.     end
  93.     --printCurrentFuel()
  94. end
  95.  
  96. function turn(dir)
  97.     --printCurDir()
  98.     --print("[+] Turning: " .. dir .. "...")
  99.     local dirVal = dir == "left" and -1 or 1
  100.     curDir = directionValFix(curDir + dirVal)
  101.     turnDirs[dir]()
  102.     --print("[-] Turned: " .. dir .. ".")
  103.     --printCurDir()
  104. end
  105.  
  106. function move(dir)
  107.     refuel()
  108.     dropOffMaterials()
  109.     --printCurPos()
  110.     --print("[+] Moving: " .. dir .. "...")
  111.     function actualMove(direct)
  112.         digDirs[dir]()
  113.         if dir ~= "up" and dir ~= "down" then
  114.             if moveDirs[direct]() then
  115.                 if curDir == 0 then zPos = zPos + 1 end
  116.                 if curDir == 1 then xPos = xPos + 1 end
  117.                 if curDir == 2 then zPos = zPos - 1 end
  118.                 if curDir == 3 then xPos = xPos - 1 end
  119.                 --print("[-] Moved: " .. dir .. ".")
  120.             else
  121.                 --print("[-!] Failed Moved: " .. dir .. ".")
  122.             end
  123.         else
  124.             if moveDirs[direct]() then
  125.                 if dir == "up" then yPos = yPos + 1 end
  126.                 if dir == "down" then yPos = yPos - 1 end
  127.                 --print("[-] Moved: " .. dir .. ".")
  128.             else
  129.                 --print("[-!] Failed Moved: " .. dir .. ".")
  130.             end
  131.         end
  132.     end
  133.     if dir ~= "up" and dir ~= "down" then
  134.         if dir == "back" then
  135.             turn("right")
  136.             turn("right")
  137.         end
  138.         if dir == "right" or dir == "left" then
  139.             turn(dir)
  140.         end
  141.         actualMove("forward")
  142.     else
  143.         actualMove(dir)
  144.     end
  145.     --printCurPos()
  146. end
  147.  
  148. function build(dir, blockID)
  149.     selectItem(blockID)
  150.     return placeDirs[dir]()
  151. end
  152.  
  153. function faceDirection(dir)
  154.     if directions[directionValFix(curDir - 1)] == dir then
  155.         turn("left")
  156.         return
  157.     end
  158.     for i = 0, 3 do
  159.         if directions[curDir] == dir then return end
  160.         turn("right")
  161.     end
  162. end
  163.  
  164. function moveDirection(dir)
  165.     if dir ~= "up" and dir ~= "down" then
  166.         faceDirection(dir)
  167.         move("forward")
  168.     else
  169.         move(dir)
  170.     end
  171. end
  172.  
  173. function moveTo(x, y, z)
  174.     function axisMove(axis, curVal, toVal)
  175.         if curVal ~= toVal then
  176.             local changeVal = curVal > toVal and -1 or 1
  177.             for curAxisVal = curVal, toVal - changeVal, changeVal do
  178.                 if axis == "x" then
  179.                     moveDirection(changeVal > 0 and "right" or "left")
  180.                 elseif axis == "y" then
  181.                     moveDirection(changeVal > 0 and "up" or "down")
  182.                 elseif axis == "z" then
  183.                     moveDirection(changeVal > 0 and "forward" or "back")
  184.                 end
  185.             end
  186.         end
  187.     end
  188.     axisMove("y", yPos, y)
  189.     axisMove("x", xPos, x)
  190.     axisMove("z", zPos, z)
  191. end
  192.  
  193. function toBool(val)
  194.     return val == "true" or val == true
  195. end
  196.  
  197. function placeBuildingBlock(dir)
  198.     for key, value in pairs(buildingBlocks) do
  199.         if build(dir, key) then return true end
  200.     end
  201.     return false
  202. end
  203.  
  204. function returnHome()
  205.     if isReturningHome then return end
  206.     isReturningHome = true
  207.     moveTo(0, 0, 0)
  208.     faceDirection("forward")
  209.     isReturningHome = false
  210. end
  211.  
  212. function dropOffMaterials()
  213.     if currentlyDroppingOffMaterials then return false end
  214.     local isInventoryFilled = true
  215.     for i = 1, 16 do
  216.         local curItem = turtle.getItemDetail(i) or { name = "Empty" }
  217.         if curItem.name == "Empty" then
  218.             isInventoryFilled = false
  219.             break
  220.         end
  221.     end
  222.     if isInventoryFilled then
  223.         currentlyDroppingOffMaterials = true
  224.         local startData = { dir = curDir, x = xPos, y = yPos, z = zPos }
  225.         returnHome()
  226.         local skippedItem = {}
  227.         for i = 1, 16 do
  228.             local curItem = turtle.getItemDetail(i) or { name = "Empty" }
  229.             local curID = curItem.name
  230.             local isImportant = importantItems[curID]
  231.             local shouldDrop = isImportant and skippedItem[curID]
  232.             if shouldDrop then
  233.                 turtle.select(i)
  234.                 turtle.dropDown()
  235.             end
  236.             if isImportant and not shouldAlwaysSkip[curID] then
  237.                 skippedItem[curID] = true
  238.             end
  239.         end
  240.         currentlyDroppingOffMaterials = false
  241.         moveTo(xPos, yPos, startData.z)
  242.         moveTo(startData.x, yPos, zPos)
  243.         moveTo(xPos, startData.y, zPos)
  244.         faceDirection(directions[startData.dir])
  245.     end
  246. end
  247.  
  248. function promptDimensions()
  249.     print("Enter the width of the area to clear:")
  250.     local width = tonumber(read())
  251.     print("Enter the length of the area to clear:")
  252.     local length = tonumber(read())
  253.     print("Enter the height of the area to clear:")
  254.     local height = tonumber(read())
  255.     return width, length, height
  256. end
  257.  
  258. function clearArea(width, length, height)
  259.     for h = 0, height - 1 do
  260.         for l = 0, length - 1 do
  261.             if l % 2 == 0 then
  262.                 for w = 0, width - 1 do
  263.                     moveTo(w, h, l)
  264.                 end
  265.             else
  266.                 for w = width - 1, 0, -1 do
  267.                     moveTo(w, h, l)
  268.                 end
  269.             end
  270.         end
  271.     end
  272. end
  273.  
  274. local width, length, height = promptDimensions()
  275. clearArea(width, length, height)
  276. returnHome()
  277.  
  278. -- End of Program
  279. dropOffMaterials()
  280. returnHome()
  281.  
  282. --[[
  283.     - Turns once to either direction
  284. turn("left")
  285. turn("right")
  286.  
  287.     - Fully turns til it reaches that direction based on the original orientation of the turtle when the program started
  288. faceDirection("forward")
  289. faceDirection("back")
  290. faceDirection("right")
  291. faceDirection("left")
  292.  
  293.     - Moves turtle based on its current orientation
  294. move("forward")
  295. move("back")
  296. move("right")
  297. move("left")
  298. move("up")
  299. move("down")
  300.  
  301.     - Moves turtle to inputted coords based on/to the original pos and direction the turtle was when program started
  302.     (Will move on y axis first, then x, and last z.)
  303. moveTo(x, y, z)
  304.  
  305.     - Moves turtle in direction based on the original orientation of the turtle when the program started
  306. moveDirection("forward")
  307. moveDirection("back")
  308. moveDirection("right")
  309. moveDirection("left")
  310. moveDirection("up")
  311. moveDirection("down")
  312.  
  313.     - Searches in inventory and selects inputted item, if the item doesn't exist it will terminate the program so things don't break.
  314. selectItem(itemID)
  315.  
  316.     - Places the block in whatever direction based on the turtle's current orientation
  317. build("forward", blockID)
  318. build("up", blockID)
  319. build("down", blockID)]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement