Second_Cup

Untitled

Jul 25th, 2025 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. -- Turtle Block Replacer
  2. -- Breaks block below and replaces with dirt in a defined area
  3. -- Uses dirt from all available slots
  4.  
  5. -- Get area dimensions from user
  6. print("Enter area width (x):")
  7. local width = tonumber(read())
  8. print("Enter area length (z):")
  9. local length = tonumber(read())
  10.  
  11. -- Calculate total dirt across all slots
  12. local totalDirt = 0
  13. for slot = 1, 16 do
  14. local item = turtle.getItemDetail(slot)
  15. if item and item.name:find("dirt") then
  16. totalDirt = totalDirt + item.count
  17. end
  18. end
  19.  
  20. -- Calculate required dirt and check inventory
  21. local requiredDirt = width * length
  22. print("Total dirt available: "..totalDirt)
  23. print("Dirt needed: "..requiredDirt)
  24.  
  25. if totalDirt < requiredDirt then
  26. print("Not enough dirt! Need "..requiredDirt..", have "..totalDirt)
  27. return
  28. end
  29.  
  30. -- Check fuel requirements (1 fuel per move + 1 per dig/place)
  31. local requiredFuel = (width * length * 3) + (width + length)
  32. local currentFuel = turtle.getFuelLevel()
  33.  
  34. if currentFuel < requiredFuel then
  35. print("Warning: Low fuel! Need "..requiredFuel..", have "..currentFuel)
  36. print("Place fuel in any slot to continue or type 'cancel'")
  37. local input = read()
  38. if input:lower() == "cancel" then return end
  39.  
  40. -- Refuel from all slots that contain fuel
  41. for slot = 1, 16 do
  42. turtle.select(slot)
  43. if turtle.refuel(0) then -- Check if item is fuel
  44. turtle.refuel()
  45. end
  46. end
  47. currentFuel = turtle.getFuelLevel()
  48. if currentFuel < requiredFuel then
  49. print("Still not enough fuel! Canceling operation.")
  50. return
  51. end
  52. end
  53.  
  54. -- Main operation function
  55. local function findDirtSlot()
  56. for slot = 1, 16 do
  57. local item = turtle.getItemDetail(slot)
  58. if item and item.name:find("dirt") and item.count > 0 then
  59. return slot
  60. end
  61. end
  62. return nil
  63. end
  64.  
  65. -- Begin replacement pattern
  66. for z = 1, length do
  67. for x = 1, width do
  68. -- Break block below
  69. while not turtle.digDown() do
  70. sleep(0.5)
  71. end
  72.  
  73. -- Find and select dirt
  74. local dirtSlot = findDirtSlot()
  75. if not dirtSlot then
  76. print("Ran out of dirt unexpectedly!")
  77. return
  78. end
  79. turtle.select(dirtSlot)
  80.  
  81. -- Place dirt block
  82. turtle.placeDown()
  83.  
  84. -- Move forward if not at end of row
  85. if x < width then
  86. while not turtle.forward() do
  87. turtle.dig()
  88. sleep(0.5)
  89. end
  90. end
  91. end
  92.  
  93. -- Return to start of row if not on last row
  94. if z < length then
  95. if z % 2 == 0 then
  96. turtle.turnLeft()
  97. turtle.forward()
  98. turtle.turnLeft()
  99. else
  100. turtle.turnRight()
  101. turtle.forward()
  102. turtle.turnRight()
  103. end
  104. end
  105. end
  106.  
  107. print("Area processed successfully!")
  108. print("Blocks replaced: "..(width * length))
  109. print("Fuel remaining: "..turtle.getFuelLevel())
Add Comment
Please, Sign In to add comment