Ubidibity

downramp

Apr 15th, 2025 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.20 KB | Gaming | 0 0
  1. -- Program: digToDepth
  2. -- Digs a 2-block tall shaft (1x1 base, 2 high) to a specified depth, handling liquids and compressing cobblestone
  3.  
  4. -- Get depth from command line argument
  5. local args = {...}
  6. if #args < 1 then
  7.   print("Usage: digToDepth <depth>")
  8.   return
  9. end
  10.  
  11. local depth = tonumber(args[1])
  12. if not depth or depth < 1 then
  13.   print("Error: Depth must be a positive number")
  14.   return
  15. end
  16.  
  17. -- Maximum attempts to move forward or down (safety for unbreakable blocks)
  18. local MAX_ATTEMPTS = 100
  19.  
  20. -- Function to ensure turtle has enough fuel
  21. local function checkFuel()
  22.   if turtle.getFuelLevel() < 1 then
  23.     print("Out of fuel!")
  24.     return false
  25.   end
  26.   return true
  27. end
  28.  
  29. -- Function to find a placeable block in inventory (for sealing liquids)
  30. local function findPlaceableBlock()
  31.   for slot = 1, 16 do
  32.     local item = turtle.getItemDetail(slot)
  33.     if item and turtle.select(slot) then
  34.       -- Assume most mined blocks are placeable (cobblestone, dirt, etc.)
  35.       return true
  36.     end
  37.   end
  38.   print("No placeable blocks in inventory!")
  39.   return false
  40. end
  41.  
  42. -- Function to place a block in a specific direction
  43. local function placeBlock(direction)
  44.   if not findPlaceableBlock() then
  45.     return false
  46.   end
  47.   if direction == "forward" then
  48.     return turtle.place()
  49.   elseif direction == "up" then
  50.     return turtle.placeUp()
  51.   elseif direction == "down" then
  52.     return turtle.placeDown()
  53.   elseif direction == "back" then
  54.     return turtle.place() -- After moving forward, place behind
  55.   end
  56.   return false
  57. end
  58.  
  59. -- Function to check if a block is a liquid (source or flowing)
  60. local function isLiquid(data)
  61.   return data and data.name and (
  62.     data.name:match("water") or
  63.     data.name:match("lava") or
  64.     data.name:match("oil") or
  65.     data.name:match("fluid")
  66.   )
  67. end
  68.  
  69. -- Function to compress cobblestone when 9 or more are in inventory
  70. local function compressCobblestone()
  71.   -- Count total cobblestone in inventory
  72.   local cobbleCount = 0
  73.   local cobbleSlots = {} -- Track slots containing cobblestone
  74.   for slot = 1, 16 do
  75.     local item = turtle.getItemDetail(slot)
  76.     if item and item.name == "minecraft:cobblestone" then
  77.       cobbleCount = cobbleCount + turtle.getItemCount(slot)
  78.       table.insert(cobbleSlots, {slot = slot, count = turtle.getItemCount(slot)})
  79.     end
  80.   end
  81.  
  82.   -- If 9 or more cobblestone, compress them
  83.   while cobbleCount >= 9 do
  84.     -- Clear crafting slots (1-9)
  85.     for slot = 1, 9 do
  86.       turtle.select(slot)
  87.       if turtle.getItemCount(slot) > 0 then
  88.         -- Move items to slots 10-16
  89.         for targetSlot = 10, 16 do
  90.           if turtle.getItemCount(targetSlot) == 0 then
  91.             turtle.transferTo(targetSlot)
  92.             break
  93.           end
  94.         end
  95.       end
  96.     end
  97.  
  98.     -- Arrange 9 cobblestone in slots 1-9
  99.     local remaining = 9
  100.     for _, cobble in ipairs(cobbleSlots) do
  101.       if remaining > 0 then
  102.         turtle.select(cobble.slot)
  103.         local toTransfer = math.min(remaining, cobble.count)
  104.         for slot = 1, 9 do
  105.           if turtle.getItemCount(slot) == 0 then
  106.             turtle.select(cobble.slot)
  107.             turtle.transferTo(slot, toTransfer)
  108.             remaining = remaining - toTransfer
  109.             cobble.count = cobble.count - toTransfer
  110.             break
  111.           end
  112.         end
  113.       end
  114.     end
  115.  
  116.     -- Craft Compressed Cobblestone
  117.     turtle.select(1)
  118.     if not turtle.craft() then
  119.       print("Failed to craft Compressed Cobblestone!")
  120.       return false
  121.     end
  122.  
  123.     -- Update cobbleCount after crafting (9 cobblestone → 1 Compressed Cobblestone)
  124.     cobbleCount = cobbleCount - 9
  125.  
  126.     -- Recount cobblestone for the next iteration
  127.     cobbleSlots = {}
  128.     for slot = 1, 16 do
  129.       local item = turtle.getItemDetail(slot)
  130.       if item and item.name == "minecraft:cobblestone" then
  131.         table.insert(cobbleSlots, {slot = slot, count = turtle.getItemCount(slot)})
  132.         cobbleCount = cobbleCount + turtle.getItemCount(slot)
  133.       end
  134.     end
  135.   end
  136.  
  137.   return true
  138. end
  139.  
  140. -- Function to check for liquids and seal them (used for up direction only now)
  141. local function checkForLiquid()
  142.   -- Check above
  143.   local success, data = turtle.inspectUp()
  144.   if success and isLiquid(data) then
  145.     if not placeBlock("up") then
  146.       print("Cannot seal liquid above!")
  147.       return false
  148.     end
  149.   end
  150.  
  151.   return true
  152. end
  153.  
  154. -- Function to dig forward until no blocks remain, handling liquids
  155. local function digForward()
  156.   local attempts = 0
  157.   local success = false
  158.  
  159.   -- Keep trying to move forward until success or max attempts reached
  160.   repeat
  161.     attempts = attempts + 1
  162.  
  163.     -- Check for a block or liquid in front
  164.     local hasBlock = turtle.detect()
  165.     local successInspect, data = turtle.inspect()
  166.  
  167.     if hasBlock and not (successInspect and isLiquid(data)) then
  168.       -- If it's a solid block (not a liquid), dig it
  169.       turtle.dig() -- Dig block in front
  170.       sleep(0.5) -- Wait for falling blocks like sand/gravel
  171.     elseif successInspect and isLiquid(data) then
  172.       -- If it's a liquid (source or flowing), move into it and seal it
  173.       if not turtle.forward() then
  174.         print("Cannot move into liquid block forward!")
  175.         return false
  176.       end
  177.       -- Place a block behind to seal the liquid
  178.       turtle.turnLeft()
  179.       turtle.turnLeft() -- Face backward
  180.       if not placeBlock("back") then
  181.         print("Cannot seal liquid behind!")
  182.         turtle.turnLeft()
  183.         turtle.turnLeft() -- Face forward again
  184.         turtle.back() -- Move back to original position
  185.         return false
  186.       end
  187.       turtle.turnLeft()
  188.       turtle.turnLeft() -- Face forward again
  189.       turtle.back() -- Move back to original position
  190.       -- Continue the loop to check the next block
  191.     else
  192.       -- No block or liquid, try to move forward
  193.       success = turtle.forward()
  194.       if not success then
  195.         print("Forward blocked, retrying (" .. attempts .. "/" .. MAX_ATTEMPTS .. ")")
  196.         sleep(0.5) -- Wait before retrying
  197.       end
  198.     end
  199.  
  200.     -- Attempt to compress cobblestone after each forward attempt
  201.     compressCobblestone()
  202.   until success or attempts >= MAX_ATTEMPTS
  203.  
  204.   if not success then
  205.     print("Stopped: Cannot move forward after " .. MAX_ATTEMPTS .. " attempts")
  206.     return false
  207.   end
  208.   return true
  209. end
  210.  
  211. -- Function to dig up until no blocks remain
  212. local function digUp()
  213.   while turtle.detectUp() do
  214.     turtle.digUp() -- Dig block above
  215.     sleep(0.5) -- Wait for falling blocks
  216.   end
  217.   -- Check for liquids above
  218.   if not checkForLiquid() then
  219.     print("Stopped: Cannot handle liquid above")
  220.     return false
  221.   end
  222.   -- Attempt to compress cobblestone after digging up
  223.   compressCobblestone()
  224.   return true
  225. end
  226.  
  227. -- Function to dig down until no blocks remain, handling liquids
  228. local function digDown()
  229.   local attempts = 0
  230.   local success = false
  231.  
  232.   -- Keep trying to move down until success or max attempts reached
  233.   repeat
  234.     attempts = attempts + 1
  235.  
  236.     -- Check for a block or liquid below
  237.     local hasBlock = turtle.detectDown()
  238.     local successInspect, data = turtle.inspectDown()
  239.  
  240.     if hasBlock and not (successInspect and isLiquid(data)) then
  241.       -- If it's a solid block (not a liquid), dig it
  242.       turtle.digDown() -- Dig block below
  243.       sleep(0.5) -- Wait for falling blocks like sand/gravel
  244.     elseif successInspect and isLiquid(data) then
  245.       -- If it's a liquid (source or flowing), move into it and seal it
  246.       if not turtle.down() then
  247.         print("Cannot move into liquid block below!")
  248.         return false
  249.       end
  250.       -- Place a block above to seal the liquid
  251.       if not placeBlock("up") then
  252.         print("Cannot seal liquid above!")
  253.         turtle.up() -- Move back to original position
  254.         return false
  255.       end
  256.       turtle.up() -- Move back to original position
  257.       -- Continue the loop to check the next block
  258.     else
  259.       -- No block or liquid, try to move down
  260.       success = turtle.down()
  261.       if not success then
  262.         print("Down blocked, retrying (" .. attempts .. "/" .. MAX_ATTEMPTS .. ")")
  263.         sleep(0.5) -- Wait before retrying
  264.       end
  265.     end
  266.  
  267.     -- Attempt to compress cobblestone after each down attempt
  268.     compressCobblestone()
  269.   until success or attempts >= MAX_ATTEMPTS
  270.  
  271.   if not success then
  272.     print("Stopped: Cannot move down after " .. MAX_ATTEMPTS .. " attempts")
  273.     return false
  274.   end
  275.   return true
  276. end
  277.  
  278. -- Main digging loop
  279. local function digToDepth(targetDepth)
  280.   local currentDepth = 0
  281.  
  282.   while currentDepth < targetDepth do
  283.     -- Check fuel
  284.     if not checkFuel() then
  285.       return
  286.     end
  287.  
  288.     -- Step 1: Dig and move forward
  289.     if not digForward() then
  290.       print("Stopped: Cannot proceed forward")
  291.       return
  292.     end
  293.  
  294.     -- Step 2: Dig up to clear 2-block height
  295.     if not digUp() then
  296.       print("Stopped: Cannot dig up")
  297.       return
  298.     end
  299.  
  300.     -- Step 3: Dig and move down
  301.     if not digDown() then
  302.       print("Stopped: Cannot proceed down")
  303.       return
  304.     end
  305.  
  306.     currentDepth = currentDepth + 1
  307.     print("Depth reached: " .. currentDepth .. "/" .. targetDepth)
  308.   end
  309.  
  310.   print("Reached target depth: " .. targetDepth)
  311. end
  312.  
  313. -- Run the program
  314. digToDepth(depth)
Add Comment
Please, Sign In to add comment