Advertisement
towwey

roomdig

Jan 14th, 2025 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | None | 0 0
  1. -- Turtle Program to Dig Out Rooms with Configurable Size and Visual Guide
  2. -- Starts at the bottom-left corner of the room and ensures correct height handling, including sand/gravel.
  3.  
  4. -- Function to display a visual representation of where to place the turtle
  5. local function showPlacementGuide()
  6.     print("Place the turtle at the bottom-left corner of the room facing forward.")
  7.     print("It should be one block below the desired floor level.")
  8. end
  9.  
  10. -- Function to prompt user for room dimensions
  11. local function getRoomDimensions()
  12.     print("Enter the dimensions for the room:")
  13.     print("Length (blocks): ")
  14.     local length = tonumber(io.read())
  15.     print("Width (blocks): ")
  16.     local width = tonumber(io.read())
  17.     print("Height (blocks): ")
  18.     local height = tonumber(io.read())
  19.  
  20.     return length, width, height
  21. end
  22.  
  23. -- Function to calculate and check total fuel needed
  24. local function checkFuelNeeded(length, width, height)
  25.     local totalBlocks = length * width * height
  26.     local fuelNeeded = totalBlocks * 2  -- Estimate: 2 fuel per block (including turns and adjustments)
  27.  
  28.     print("Total blocks to mine: " .. totalBlocks)
  29.     print("Estimated fuel needed: " .. fuelNeeded)
  30.  
  31.     if turtle.getFuelLevel() < fuelNeeded then
  32.         print("Not enough fuel! Please refuel the turtle.")
  33.         while turtle.getFuelLevel() < fuelNeeded do
  34.             print("Place fuel in the turtle's inventory and press Enter.")
  35.             os.pullEvent("turtle_inventory")
  36.             turtle.refuel()
  37.         end
  38.         print("Turtle refueled! Fuel level: " .. turtle.getFuelLevel())
  39.     else
  40.         print("Sufficient fuel available. Fuel level: " .. turtle.getFuelLevel())
  41.     end
  42. end
  43.  
  44. -- Function to dig forward, handling falling blocks
  45. local function digForward()
  46.     while turtle.detect() do
  47.         turtle.dig()
  48.         sleep(0.5)  -- Wait for falling blocks to settle
  49.     end
  50.     turtle.forward()
  51. end
  52.  
  53. -- Function to dig up, handling falling blocks
  54. local function digUp()
  55.     while turtle.detectUp() do
  56.         turtle.digUp()
  57.         sleep(0.5)  -- Wait for falling blocks to settle
  58.     end
  59. end
  60.  
  61. -- Function to dig down, handling falling blocks
  62. local function digDown()
  63.     while turtle.detectDown() do
  64.         turtle.digDown()
  65.         sleep(0.5)  -- Wait for falling blocks to settle
  66.     end
  67. end
  68.  
  69. -- Function to dig forward, up, and down in one move
  70. local function digAll()
  71.     digForward()
  72.     digUp()
  73.     digDown()
  74. end
  75.  
  76. -- Main function to dig out the room
  77. local function digRoom(length, width, height)
  78.     -- Start by moving up one block to account for the initial position
  79.     turtle.up()
  80.  
  81.     for layer = 1, height do
  82.         for w = 1, width do
  83.             for l = 1, length do
  84.                 if l == 1 and w == 1 and layer > 1 then
  85.                     digUp()
  86.                     digDown()
  87.                 end
  88.                 digAll()
  89.             end
  90.  
  91.             -- Turn at the end of the row
  92.             if w < width then
  93.                 if w % 2 == 1 then
  94.                     turtle.turnRight()
  95.                     digForward()
  96.                     digUp()
  97.                     digDown()
  98.                     turtle.turnRight()
  99.                 else
  100.                     turtle.turnLeft()
  101.                     digForward()
  102.                     digUp()
  103.                     digDown()
  104.                     turtle.turnLeft()
  105.                 end
  106.             end
  107.         end
  108.  
  109.         -- Move up to the next layer if needed
  110.         if layer < height then
  111.             digUp()
  112.             turtle.up()
  113.             if width % 2 == 1 then
  114.                 turtle.turnRight()
  115.                 turtle.turnRight()
  116.             end
  117.         end
  118.     end
  119.  
  120.     -- Ensure the turtle stops after completing the specified height
  121.     if height > 1 then
  122.         while turtle.down() do end
  123.     end
  124.  
  125.     -- Return to the starting position
  126.     turtle.turnRight()
  127.     for i = 1, length - 1 do turtle.back() end
  128.     turtle.turnRight()
  129.     for i = 1, width - 1 do turtle.back() end
  130.     turtle.turnRight()
  131. end
  132.  
  133. -- Show placement guide
  134. showPlacementGuide()
  135.  
  136. -- Get room dimensions from user
  137. local roomLength, roomWidth, roomHeight = getRoomDimensions()
  138.  
  139. -- Check if sufficient fuel is available
  140. checkFuelNeeded(roomLength, roomWidth, roomHeight)
  141.  
  142. -- Run the program
  143. digRoom(roomLength, roomWidth, roomHeight)
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement