Siirtan05

lua

Apr 27th, 2025
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. if not turtle then
  2. error("Only runs on turtles")
  3. end
  4.  
  5. local x, y, z = 0, 0, nil
  6. local direction = 0
  7. local maxHeight, size, startHeight = nil, nil, nil
  8. local badBlocks = {
  9. ["minecraft:cobblestone"]=true,
  10. ["minecraft:stone"]=true,
  11. ["minecraft:dirt"]=true,
  12. ["minecraft:gravel"]=true,
  13. ["minecraft:sand"]=true,
  14. ["minecraft:bedrock"]=true,
  15. ["minecraft:flint"]=true,
  16. ["minecraft:rotten_flesh"]=true,
  17. ["minecraft:sandstone"]=true,
  18. ["minecraft:diorite"]=true,
  19. ["minecraft:andesite"]=true,
  20. ["chisel:limestone"]=true,
  21. ["chisel:diorite"]=true,
  22. ["chisel:marble"]=true
  23. }
  24.  
  25. local currentLayer
  26.  
  27. local chatBox = peripheral.find("chatbox")
  28.  
  29. function say(message)
  30. print(message)
  31.  
  32. if chatBox then
  33. pcall(chatBox.say, message) --prevent too many messages error
  34. end
  35. end
  36.  
  37. -- 0 y+
  38. -- 3 ^ 1 x- x+
  39. -- 2 y-
  40.  
  41. function turnTo(newDirection)
  42. while newDirection < 0 do
  43. newDirection = newDirection + 4
  44. end
  45. newDirection = newDirection % 4
  46.  
  47. if (newDirection - direction) % 4 > 1 then
  48. while direction ~= newDirection do
  49. turtle.turnLeft()
  50. direction = (direction + 3) % 4
  51. end
  52. else
  53. while direction ~= newDirection do
  54. turtle.turnRight()
  55. direction = (direction + 1) % 4
  56. end
  57. end
  58. end
  59.  
  60. function fastSelect(i)
  61. if turtle.getSelectedSlot() ~= i then
  62. turtle.select(i)
  63. end
  64. end
  65.  
  66. function move(moveFunc, digFunc, attackFunc)
  67. local failCount = 0
  68.  
  69. digFunc()
  70.  
  71. while not moveFunc() do
  72. digFunc()
  73. attackFunc()
  74. sleep(0.25)
  75.  
  76. failCount = failCount + 1
  77.  
  78. if failCount == 100 then
  79. say("Unable to move")
  80. end
  81. end
  82. end
  83.  
  84. function up()
  85. move(turtle.up, turtle.digUp, turtle.attackUp)
  86. z = z + 1
  87. end
  88.  
  89. function down()
  90. move(turtle.down, turtle.digDown, turtle.attackDown)
  91. z = z - 1
  92. end
  93.  
  94. function forward()
  95. move(turtle.forward, turtle.dig, turtle.attack)
  96.  
  97. if direction == 0 then
  98. y = y + 1
  99. elseif direction == 1 then
  100. x = x + 1
  101. elseif direction == 2 then
  102. y = y - 1
  103. else
  104. x = x - 1
  105. end
  106. end
  107.  
  108. function check()
  109. local success, data = turtle.inspectUp()
  110. if success and not badBlocks[data.name] then
  111. fastSelect(1)
  112. turtle.digUp()
  113. end
  114. success, data = turtle.inspectDown()
  115. if success and not badBlocks[data.name] then
  116. fastSelect(1)
  117. turtle.digDown()
  118. end
  119. end
  120.  
  121. function gotoZ(newZ)
  122. while z < newZ do
  123. up()
  124. end
  125. while z > newZ do
  126. down()
  127. end
  128. end
  129.  
  130. function dropAndSortItems()
  131. say("claning up inventory")
  132.  
  133. for i=1, 16 do
  134. local data = turtle.getItemDetail(i)
  135.  
  136. if data and badBlocks[data.name] then
  137. fastSelect(i)
  138. turtle.dropDown()
  139. else
  140. for ii=1, i do
  141. local other = turtle.getItemDetail(ii)
  142. if not other or (other.name == data.name and turtle.getItemSpace(ii) > 0) then
  143. fastSelect(i)
  144. turtle.transferTo(ii)
  145. if turtle.getItemCount(i) == 0 then
  146. break
  147. end
  148. end
  149. end
  150. end
  151. end
  152. end
  153.  
  154. function emptyInventory(force)
  155. if not force then
  156. if turtle.getItemCount(16) == 0 then
  157. return
  158. end
  159. end
  160.  
  161. dropAndSortItems()
  162.  
  163. if not force then
  164. if turtle.getItemCount(15) == 0 then
  165. fastSelect(1)
  166. return
  167. end
  168. end
  169.  
  170. say("returning to the surface to drop off items")
  171.  
  172. local oldX, oldY, oldZ, oldDir = x, y, z, direction
  173.  
  174. turnTo(3)
  175.  
  176. while x > 0 do
  177. forward()
  178. end
  179.  
  180. turnTo(2)
  181.  
  182. while y > 0 do
  183. forward()
  184. end
  185.  
  186. gotoZ(startHeight)
  187.  
  188. for i=1, 16 do
  189. if turtle.getItemCount(i) > 0 then
  190. fastSelect(i)
  191. while not turtle.drop() do
  192. up()
  193. end
  194. end
  195. end
  196.  
  197. fastSelect(1)
  198.  
  199. gotoZ(oldZ)
  200.  
  201. turnTo(0)
  202.  
  203. while y < oldY do
  204. forward()
  205. end
  206.  
  207. turnTo(1)
  208.  
  209. while x < oldX do
  210. forward()
  211. end
  212.  
  213. turnTo(oldDir)
  214. end
  215.  
  216.  
  217. startHeight, size, maxHeight, currentLayer = ...
  218.  
  219. startHeight = tonumber(startHeight)
  220. size = tonumber(size)
  221. maxHeight = tonumber(maxHeight)
  222. currentLayer = tonumber(currentLayer)
  223.  
  224. if not (startHeight and size) then
  225. print("usage: OreQuarry [Y coordinate] [Size] <Max mining height: Y-2 > <Start Height: 5>")
  226. print("turtle will mine a square of [size] blocks")
  227. print("place a vertical column of chests behind the turtle")
  228. return
  229. end
  230.  
  231. startHeight = math.floor(startHeight)
  232. z = startHeight
  233. size = math.floor(size)
  234.  
  235. if not currentLayer then
  236. currentLayer = 5
  237. end
  238.  
  239. if not maxHeight then
  240. maxHeight = startHeight - 2
  241. end
  242.  
  243. maxHeight = math.floor(maxHeight)
  244. currentLayer = math.floor(currentLayer)
  245.  
  246. if size % 2 ~= 0 then
  247. print("Size must be multiple of 2")
  248. return
  249. end
  250.  
  251. if size < 0 or startHeight < 6 or startHeight > 255 or maxHeight < 8 or maxHeight > startHeight - 2 then
  252. print("Bad Arguments")
  253. return
  254. end
  255.  
  256.  
  257.  
  258. while currentLayer < maxHeight do
  259. say("Mining Layer " .. currentLayer)
  260.  
  261. gotoZ(currentLayer)
  262.  
  263. while true do
  264. turnTo((x % 2) * 2)
  265.  
  266. if x % 2 == 0 then
  267. while y < size - 1 do
  268. check()
  269. forward()
  270. emptyInventory()
  271. end
  272. else
  273. while y > 0 do
  274. check()
  275. forward()
  276. emptyInventory()
  277. end
  278. end
  279.  
  280. check()
  281.  
  282. if x < size - 1 then
  283. turnTo(1)
  284. forward()
  285. else
  286. turnTo(3)
  287.  
  288. while x > 0 do
  289. forward()
  290. end
  291.  
  292. turnTo(2)
  293.  
  294. while y > 0 do
  295. forward()
  296. end
  297. break
  298. end
  299. end
  300. currentLayer = currentLayer + 3
  301. end
  302. dropAndSortItems()
  303. gotoZ(startHeight)
  304. emptyInventory(true)
  305.  
  306. say("Done")
Add Comment
Please, Sign In to add comment