goldfiction

martin2250orequarry

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