posicat

/patchtunnel

Sep 12th, 2024 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. --File: /patchtunnel
  2.  
  3. dofile("/cattech/turtle.lua")
  4.  
  5. local patchItem
  6.  
  7. function getItemTypeInSlot2()
  8. local itemCount = turtle.getItemCount(2) -- Get the count of items in slot 2
  9. if itemCount > 0 then
  10. local itemDetail = turtle.getItemDetail(2) -- Get details of the item in slot 2
  11. return itemDetail.name -- Return the item name
  12. else
  13. return nil -- Return nil if slot 2 is empty
  14. end
  15. end
  16.  
  17. function selectItemFromSlots(itemName)
  18. for slot = 1, 16 do -- Loop through all 16 slots
  19. local itemCount = turtle.getItemCount(slot) -- Get the count of items in the current slot
  20. if itemCount > 0 then
  21. local itemDetail = turtle.getItemDetail(slot) -- Get details of the item in the current slot
  22. if itemDetail.name == itemName then
  23. turtle.select(slot) -- Select the slot if it matches the item name
  24. return true
  25. end
  26. end
  27. end
  28. print(patchItem .. "not found.") -- If item is not found
  29. return false
  30. end
  31.  
  32. function patchUp()
  33. success, blockData = turtle.inspectUp()
  34. if success and isLiquid(blockData.name) then
  35. selectItemFromSlots(patchItem)
  36. turtle.placeUp()
  37. end
  38. end
  39.  
  40. function patchDown()
  41. success, blockData = turtle.inspectDown()
  42. if success and isLiquid(blockData.name) then
  43. selectItemFromSlots(patchItem)
  44. turtle.placeDown()
  45. end
  46. end
  47.  
  48. function patchForward()
  49. success, blockData = turtle.inspect()
  50. if success and isLiquid(blockData.name) then
  51. selectItemFromSlots(patchItem)
  52. turtle.place()
  53. end
  54. end
  55.  
  56. function patchLeft()
  57. turtle.turnLeft()
  58. success, blockData = turtle.inspect()
  59. if success and isLiquid(blockData.name) then
  60. selectItemFromSlots(patchItem)
  61. turtle.place()
  62. end
  63. turtle.turnRight()
  64. end
  65.  
  66. function patchRight()
  67. turtle.turnRight()
  68. success, blockData = turtle.inspect()
  69. if success and isLiquid(blockData.name) then
  70. selectItemFromSlots(patchItem)
  71. turtle.place()
  72. end
  73. turtle.turnLeft()
  74. end
  75.  
  76. -- Function to move the turtle back the number of spaces it has traveled
  77. function moveBack(steps)
  78. print("Returning back " .. steps .. " spaces.")
  79. for i = 1, steps do
  80. refuelIfNeeded(10)
  81. while not turtle.back() do
  82. print("Blocked! Pausing for 1 second before retrying.")
  83. sleep(1) -- Wait for 1 second if it can't move back
  84. end
  85. end
  86. end
  87.  
  88. -- Main routine
  89. function checkAndPatch(directionString)
  90. for i = 1, string.len(directionString) do
  91. local char = directionString:sub(i, i)
  92.  
  93. if char == "u" then
  94. patchUp()
  95. elseif char == "d" then
  96. patchDown()
  97. elseif char == "l" then
  98. patchLeft()
  99. elseif char == "r" then
  100. patchRight()
  101. elseif char == "f" then
  102. patchForward()
  103. end
  104. end
  105. end
  106.  
  107. function isNotAirLavaOrWater(success,blockData)
  108. if not success then
  109. return false -- If no block is detected, return false
  110. end
  111.  
  112. -- Check the block type
  113. local blockName = blockData.name
  114. if blockName ~= "minecraft:air" and blockName ~= "minecraft:water" and blockName ~= "minecraft:lava" then
  115. return true -- Return true if the block is not air, lava, or water
  116. else
  117. return false -- Return false if the block is air, lava, or water
  118. end
  119. end
  120.  
  121. function patchFull3by2()
  122.  
  123. -- Middle D Facing Forward
  124. checkAndPatch("d")
  125. turtle.turnRight()
  126. turtle.forward()
  127. -- Right D Facing Right
  128. checkAndPatch("df")
  129. turtle.up()
  130. -- Right U Facing Right
  131. checkAndPatch("uf")
  132. turtle.turnLeft()
  133. turtle.turnLeft()
  134. turtle.forward()
  135. -- Middle U Facing Left
  136. checkAndPatch("u")
  137. turtle.forward()
  138. -- Left U Facing Left
  139. checkAndPatch("uf")
  140. turtle.down()
  141. -- Left D Facing Left
  142. checkAndPatch("df")
  143. turtle.turnRight()
  144. turtle.turnRight()
  145. turtle.forward()
  146. turtle.turnLeft()
  147. -- Middle D Facing Forward
  148. end
  149.  
  150. function getTunnelSides()
  151. local dir = ""
  152.  
  153. -- Check right
  154. turtle.turnRight()
  155. if isNotAirLavaOrWater(turtle.inspect()) then
  156. dir = dir .. "r"
  157. end
  158. turtle.turnLeft()
  159.  
  160. -- Check left
  161. turtle.turnLeft()
  162. if isNotAirLavaOrWater(turtle.inspect()) then
  163. dir = dir .. "l"
  164. end
  165. turtle.turnRight()
  166.  
  167. -- Check Up
  168. if isNotAirLavaOrWater(turtle.inspectUp()) then
  169. dir = dir .. "u"
  170. end
  171.  
  172. -- Check Down
  173. if isNotAirLavaOrWater(turtle.inspectDown()) then
  174. dir = dir .. "d"
  175. end
  176.  
  177. print ("Auto-direction : " .. dir)
  178. return dir
  179. end
  180.  
  181.  
  182. -- Main program
  183. local maxSpaces = 50
  184. if #arg < 1 then
  185. print("Going " .. maxSpaces .. " default steps.")
  186. else
  187. local maxSpaces = tonumber(arg[1])
  188. end
  189.  
  190. local spaces = 0
  191.  
  192. patchItem = getItemTypeInSlot2()
  193. if patchItem == nil then
  194. print("Item slot 2 empty.")
  195. else
  196. while spaces < maxSpaces do
  197. refuelIfNeeded(30)
  198.  
  199. if selectItemFromSlots(patchItem) then
  200.  
  201. if turtle.forward() then
  202. patchFull3by2()
  203. spaces = spaces + 1
  204. else
  205. print("Could not move foward")
  206. break
  207. end
  208. else
  209. print("Ran out of " .. patchItem)
  210. break
  211. end
  212. end
  213. end
  214.  
  215. print("Returning " .. spaces .. " steps.")
  216. moveBack(spaces);
  217.  
Add Comment
Please, Sign In to add comment