Second_Cup

Untitled

Jul 15th, 2025 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. -- Function to get user input safely
  2. local function getNumber(prompt)
  3. print(prompt)
  4. local n = tonumber(read())
  5. while not n or n < 1 do
  6. print("Please enter a positive number.")
  7. n = tonumber(read())
  8. end
  9. return math.floor(n)
  10. end
  11.  
  12. -- Prompt for dimensions and options
  13. local length = getNumber("Enter length:")
  14. local width = getNumber("Enter width:")
  15. local height = getNumber("Enter height:")
  16. print("Start at TOP or BOTTOM? (type 'top' or 'bottom')")
  17. local startPos = read():lower()
  18. while startPos ~= "top" and startPos ~= "bottom" do
  19. print("Please type 'top' or 'bottom'")
  20. startPos = read():lower()
  21. end
  22.  
  23. -- Save orientation and position tracking
  24. local pos = {x = 0, y = 0, z = 0}
  25. local dir = 0 -- 0=+X, 1=+Z, 2=-X, 3=-Z
  26.  
  27. local function turnLeft()
  28. turtle.turnLeft()
  29. dir = (dir - 1) % 4
  30. end
  31.  
  32. local function turnRight()
  33. turtle.turnRight()
  34. dir = (dir + 1) % 4
  35. end
  36.  
  37. local function face(d)
  38. while dir ~= d do turnRight() end
  39. end
  40.  
  41. -- Movement wrappers to track position
  42. local function tryDig() while turtle.detect() do turtle.dig(); sleep(0.2) end end
  43. local function tryDigDown() while turtle.detectDown() do turtle.digDown(); sleep(0.2) end end
  44. local function tryDigUp() while turtle.detectUp() do turtle.digUp(); sleep(0.2) end end
  45.  
  46. local function forward()
  47. tryDig()
  48. while not turtle.forward() do sleep(0.2) end
  49. if dir == 0 then pos.x = pos.x + 1
  50. elseif dir == 1 then pos.z = pos.z + 1
  51. elseif dir == 2 then pos.x = pos.x - 1
  52. elseif dir == 3 then pos.z = pos.z - 1 end
  53. end
  54.  
  55. local function up()
  56. tryDigUp()
  57. while not turtle.up() do sleep(0.2) end
  58. pos.y = pos.y + 1
  59. end
  60.  
  61. local function down()
  62. tryDigDown()
  63. while not turtle.down() do sleep(0.2) end
  64. pos.y = pos.y - 1
  65. end
  66.  
  67. local function back()
  68. while not turtle.back() do turtle.turnLeft(); turtle.turnLeft(); tryDig(); turtle.turnLeft(); turtle.turnLeft(); sleep(0.2) end
  69. if dir == 0 then pos.x = pos.x - 1
  70. elseif dir == 1 then pos.z = pos.z - 1
  71. elseif dir == 2 then pos.x = pos.x + 1
  72. elseif dir == 3 then pos.z = pos.z + 1 end
  73. end
  74.  
  75. -- Estimate fuel usage
  76. local volume = length * width * height
  77. local estimatedFuel = volume * 3 + (length + width + height) * 2
  78. if turtle.getFuelLevel() < estimatedFuel then
  79. print("Not enough fuel! Required: " .. estimatedFuel)
  80. return
  81. end
  82.  
  83. -- Dig one horizontal layer
  84. local function digLayer()
  85. for w = 1, width do
  86. for l = 1, length - 1 do forward() end
  87. if w < width then
  88. if w % 2 == 1 then
  89. turnRight(); forward(); turnRight()
  90. else
  91. turnLeft(); forward(); turnLeft()
  92. end
  93. end
  94. end
  95. -- Return to starting row
  96. if width % 2 == 1 then
  97. face(2) -- Turn around
  98. for i = 1, length - 1 do forward() end
  99. face(3) -- Back to default facing
  100. else
  101. face(3)
  102. for i = 1, width - 1 do forward() end
  103. face(0)
  104. end
  105. end
  106.  
  107. -- Move to correct start level
  108. if startPos == "top" then
  109. for i = 1, height - 1 do down() end
  110. end
  111.  
  112. -- Main dig loop
  113. for h = 1, height do
  114. digLayer()
  115. if h < height then
  116. if startPos == "bottom" then up() else down() end
  117. end
  118. end
  119.  
  120. -- Return to Y level
  121. if startPos == "bottom" then
  122. for i = 1, height - 1 do down() end
  123. else
  124. for i = 1, height - 1 do up() end
  125. end
  126.  
  127. -- Return to (0,0,0)
  128. if pos.z > 0 then face(3) for i = 1, pos.z do forward() end
  129. elseif pos.z < 0 then face(1) for i = 1, -pos.z do forward() end end
  130.  
  131. if pos.x > 0 then face(2) for i = 1, pos.x do forward() end
  132. elseif pos.x < 0 then face(0) for i = 1, -pos.x do forward() end end
  133.  
  134. -- Return to original height
  135. while pos.y > 0 do down() end
  136. while pos.y < 0 do up() end
  137.  
  138. -- Face original direction
  139. face(0)
  140. print("Excavation complete. Returned to start.")
  141.  
Advertisement
Add Comment
Please, Sign In to add comment