Miki_Tellurium

stairs

Sep 24th, 2025
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | Gaming | 0 0
  1. --[[
  2.   Simple 1-block-wide stairs maker by Miki_Tellurium
  3.   Version: 1.0
  4.   Make a 1-block-wide staircase of specified height (min = 3).
  5.   Usage:
  6.   <fileName> -- default to 5
  7.   or
  8.   <fileName> height
  9.   ]]
  10. if not turtle then
  11.     error("Computer is not a turtle.")
  12. end
  13.  
  14. local arg = ...
  15. local maxHeight = 5
  16.  
  17. if arg then
  18.     local n = tonumber(arg)
  19.     if n then
  20.         maxHeight = n
  21.     end
  22. end
  23.  
  24. -- Max height less than 3 breaks digNext()
  25. -- also a player wouldn't fit in the stairs
  26. if maxHeight < 3 then
  27.     error("Height can't be less than 3.")
  28. end
  29.  
  30. local fuel = turtle.getFuelLevel()
  31. if fuel == 0 then
  32.     error("No fuel.")
  33. elseif fuel < maxHeight * 50 then
  34.     local ogColor = term.getTextColor()
  35.     term.setTextColor(colors.orange)
  36.     print("Low fuel level, consider refueling.")
  37.     term.setTextColor(ogColor)
  38. end
  39.  
  40. -- Handle falling blocks
  41. -- try to advance for 10 seconds and return false if no success
  42. local function advance()
  43.     local function timeout()
  44.         sleep(10)
  45.         print("Timeout reached, giving up.")
  46.     end
  47.     local function tryAdvance()
  48.         while not turtle.forward() do
  49.             turtle.dig()
  50.         end
  51.     end
  52.     local result = parallel.waitForAny(tryAdvance, timeout)
  53.     if result == 1 then
  54.         return true
  55.     else
  56.         return false
  57.     end
  58. end
  59.  
  60. -- Return false if an unbreakable block is found
  61. local function digNext(height)
  62.     local toMove = height - 2 -- Exclude top and bottom block
  63.     if not advance() then return false end
  64.     turtle.digDown()
  65.     for _ = 1,toMove do
  66.         turtle.down()
  67.         if turtle.detectDown() and not turtle.digDown() then
  68.             return false
  69.         end
  70.     end
  71.     if toMove == maxHeight-2 then toMove = toMove - 1 end -- Move up one block less if we are doing full stairs height
  72.     for _ = 1,toMove do
  73.         turtle.up()
  74.     end
  75.     return true
  76. end
  77.  
  78. local keepGoing = true
  79. local currentHeight = 3
  80. local steps = 0
  81.  
  82. print("Selected block height: "..maxHeight)
  83. turtle.digUp()
  84. turtle.up()
  85. while keepGoing do
  86.     if turtle.getFuelLevel() == 0 then
  87.         error("Run out of fuel.")
  88.     end
  89.  
  90.     keepGoing = digNext(currentHeight)
  91.     if keepGoing then
  92.         if currentHeight < maxHeight then
  93.             currentHeight = currentHeight + 1
  94.         end
  95.         steps = steps + 1
  96.     else
  97.         print("Unbreakable block found.")
  98.     end
  99. end
  100.  
  101. print("Finished stairs with "..steps.." steps.")
Advertisement
Add Comment
Please, Sign In to add comment