MtnMCG

bridge turtle

Sep 4th, 2024 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. -- Bridge Building Program for CC:Tweaked Turtle
  2.  
  3. -- Function to count building blocks in inventory
  4. local function countBuildingBlocks()
  5. local total = 0
  6. for i = 1, 16 do
  7. local item = turtle.getItemDetail(i)
  8. if item then
  9. total = total + item.count
  10. end
  11. end
  12. return total
  13. end
  14.  
  15. -- Function to check and refuel if necessary
  16. local function refuel(neededFuel)
  17. local fuelLevel = turtle.getFuelLevel()
  18. if fuelLevel == "unlimited" then return true end
  19.  
  20. for i = 1, 16 do
  21. turtle.select(i)
  22. if turtle.refuel(0) then
  23. local needed = neededFuel - fuelLevel
  24. if needed > 0 then
  25. turtle.refuel(needed)
  26. end
  27. return true
  28. end
  29. end
  30. return false
  31. end
  32.  
  33. -- Function to find and select a block for placement
  34. local function selectBlock()
  35. for i = 1, 16 do
  36. if turtle.getItemCount(i) > 0 then
  37. turtle.select(i)
  38. return true
  39. end
  40. end
  41. return false
  42. end
  43.  
  44. -- Main program
  45. print("Bridge Building Program")
  46. print("Enter the length of the bridge:")
  47. local length = tonumber(read())
  48.  
  49. if not length or length < 1 then
  50. print("Invalid length. Please enter a positive number.")
  51. return
  52. end
  53.  
  54. local blocksNeeded = length
  55. local blocksAvailable = countBuildingBlocks()
  56.  
  57. print("Blocks needed: " .. blocksNeeded)
  58. print("Blocks available: " .. blocksAvailable)
  59.  
  60. if blocksAvailable < blocksNeeded then
  61. print("Not enough blocks to complete the bridge.")
  62. return
  63. end
  64.  
  65. local fuelNeeded = length * 2 -- Move forward and back
  66. local currentFuel = turtle.getFuelLevel()
  67.  
  68. if currentFuel ~= "unlimited" and currentFuel < fuelNeeded then
  69. print("Attempting to refuel...")
  70. if not refuel(fuelNeeded) then
  71. print("Not enough fuel to complete the bridge.")
  72. return
  73. end
  74. end
  75.  
  76. print("Starting bridge construction...")
  77.  
  78. -- Build the bridge
  79. for i = 1, length do
  80. if not selectBlock() then
  81. print("Ran out of blocks unexpectedly.")
  82. break
  83. end
  84. turtle.placeDown()
  85. if i < length then
  86. turtle.forward()
  87. end
  88. end
  89.  
  90. -- Return to start
  91. turtle.turnRight()
  92. turtle.turnRight()
  93. for i = 1, length - 1 do
  94. turtle.forward()
  95. end
  96.  
  97. print("Bridge construction complete!")
Advertisement
Add Comment
Please, Sign In to add comment