jille_Jr

CC: Shaft

Mar 9th, 2025
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.64 KB | None | 0 0
  1. --[[
  2.     Dig a mine shaft, but avoid digging ores
  3.     to let the player mine them with a fortune pickaxe
  4. ]]
  5.  
  6. local junk = {
  7.     "chisel:basalt1",
  8.     "chisel:basalt2",
  9.     "chisel:limestone1",
  10.     "chisel:limestone2",
  11.     "chisel:marble1",
  12.     "chisel:marble2",
  13.     "create:asurine",
  14.     "minecraft:andesite",
  15.     "minecraft:cobbled_deepslate",
  16.     "minecraft:cobblestone",
  17.     "minecraft:deepslate",
  18.     "minecraft:diorite",
  19.     "minecraft:dirt",
  20.     "minecraft:granite",
  21.     "minecraft:grass",
  22.     "minecraft:gravel",
  23.     "minecraft:mossy_cobblestone",
  24.     "minecraft:sand",
  25.     "minecraft:stone",
  26.     "minecraft:tuff",
  27. }
  28.  
  29. --- Current up/down position, where 1 is the bottom
  30. local currentY = 1
  31. --- Current forward position, where 0 is the starting position,
  32. --- but 1 is the first vertical column to dig.
  33. local currentZ = 0
  34.  
  35. ---@param tbl table
  36. local function contains(tbl, elem)
  37.     for _, v in ipairs(tbl) do
  38.         if v == elem then
  39.             return true
  40.         end
  41.     end
  42.     return false
  43. end
  44.  
  45. ---@param direction "" | "Up" | "Down" | nil
  46. local function dig(direction)
  47.     direction = direction or ""
  48.     local success, data = turtle["inspect" .. direction]()
  49.     if not success then
  50.         return true
  51.     end
  52.     if not contains(junk, data.name) then
  53.         print(string.format("Found non-junk '%s'", data.name))
  54.         return false
  55.     end
  56.     repeat until not turtle["dig" .. direction]()
  57.     return true
  58. end
  59.  
  60. local function moveUp()
  61.     for attempt = 1, 20 do
  62.         if not dig("Up") then
  63.             return false
  64.         end
  65.         local success, err = turtle.up()
  66.         if success then
  67.             currentY = currentY + 1
  68.             return true
  69.         end
  70.         print("Can't move up:", err)
  71.     end
  72.     print("Can't move up: max attempts reached")
  73.     return false
  74. end
  75.  
  76. local function moveDown()
  77.     for attempt = 1, 20 do
  78.         if not dig("Down") then
  79.             return false
  80.         end
  81.         local success, err = turtle.down()
  82.         if success then
  83.             currentY = currentY - 1
  84.             return true
  85.         end
  86.         print("Can't move down:", err)
  87.     end
  88.     print("Can't move down: max attempts reached")
  89.     return false
  90. end
  91.  
  92. local function moveForward()
  93.     for attempt = 1, 20 do
  94.         if not dig("") then
  95.             return false
  96.         end
  97.         local success, err = turtle.forward()
  98.         if success then
  99.             currentZ = currentZ + 1
  100.             return true
  101.         end
  102.         print("Can't move forward:", err)
  103.     end
  104.     print("Can't move forward: max attempts reached")
  105.     return false
  106. end
  107.  
  108. ---@param maxHeight number
  109. local function moveForwardSomewhere(maxHeight)
  110.     if moveForward() then
  111.         return true
  112.     end
  113.  
  114.     print("Can't move forward here, trying another position")
  115.  
  116.     while currentY > 1 do
  117.         if not moveDown() then
  118.             break
  119.         end
  120.         if moveForward() then
  121.             return true
  122.         end
  123.     end
  124.     print("Can't move further down, so let's try moving up")
  125.  
  126.     while currentY < maxHeight do
  127.         if not moveUp() then
  128.             break
  129.         end
  130.         if moveForward() then
  131.             return true
  132.         end
  133.     end
  134.     print("Can't move further up. I'm stuck!")
  135.  
  136.     local success, data = turtle.inspect()
  137.     print(string.format("Please break the block in front of me: '%s'", success and data.name or "air"))
  138.  
  139.     repeat
  140.         sleep(1)
  141.     until not turtle.detect()
  142.  
  143.     moveForward()
  144. end
  145.  
  146. ---@param maxHeight number
  147. local function digColumnUp(maxHeight)
  148.     if currentY > 1 then
  149.         dig("Down")
  150.     end
  151.     for attempts = 1, maxHeight do
  152.         if currentY >= maxHeight then
  153.             print("Already too high up")
  154.             return true
  155.         elseif currentY == maxHeight - 1 then
  156.             return dig("Up")
  157.         else
  158.             if not moveUp() then
  159.                 return false
  160.             end
  161.         end
  162.     end
  163.     error("Tried moving up too many times. Aborting")
  164. end
  165.  
  166. ---@param maxHeight number
  167. local function digColumnDown(maxHeight)
  168.     if currentY <= maxHeight - 1 then
  169.         dig("Up")
  170.     end
  171.     for attempts = 1, maxHeight do
  172.         if currentY <= 1 then
  173.             print("Already too far down")
  174.             return true
  175.         elseif currentY == 2 then
  176.             return dig("Down")
  177.         else
  178.             if not moveDown() then
  179.                 return false
  180.             end
  181.         end
  182.     end
  183.     error("Tried moving down too many times. Aborting")
  184. end
  185.  
  186. local function usage(sMsg)
  187.     sMsg = sMsg and (sMsg .. "\n") or ""
  188.     error(sMsg .. "Usage: shaft <length> [height=3]", 0)
  189. end
  190.  
  191. local args = { ... }
  192.  
  193. if #args < 1 then usage("Missing length.") end
  194.  
  195. local maxLength = tonumber(args[1])
  196. if not maxLength then usage("Failed to parse length as number") end
  197. if maxLength < 0 then usage("Length must be positive") end
  198.  
  199. local maxHeight = 3
  200. if #args >= 2 then
  201.     local h = tonumber(args[1])
  202.     if not h then usage("Failed to parse height as number") end
  203.     if h < 1 then usage("Height must be at least 1") end
  204.     maxHeight = h
  205. end
  206.  
  207. --(( Main program ))--
  208.  
  209. print(string.format("Shaft %d long, %d high", maxLength, maxHeight))
  210.  
  211. moveForward()
  212.  
  213. local goUp = true
  214. while currentZ < maxLength do
  215.     if goUp then
  216.         print("Digging upwards")
  217.         digColumnUp(maxHeight)
  218.     else
  219.         print("Digging downwards")
  220.         digColumnDown(maxHeight)
  221.     end
  222.     goUp = not goUp
  223.     moveForwardSomewhere(maxHeight)
  224.     print(string.format("Progress: %d / %d", currentZ, maxLength))
  225. end
  226.  
  227. digColumnDown(maxHeight)
  228. if currentY == 2 then
  229.     moveDown()
  230. end
  231.  
  232. print("Done!")
  233.  
Advertisement
Add Comment
Please, Sign In to add comment