gravitowl

checkOres

Mar 6th, 2021 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. local mine_blocks = {
  2. "minecraft:coal_ore",
  3. "minecraft:iron_ore",
  4. "minecraft:gold_ore",
  5. "minecraft:redstone_ore",
  6. "minecraft:lapis_ore",
  7. "minecraft:emerald_ore",
  8. "minecraft:diamond_ore"
  9. }
  10.  
  11. local Dir = {
  12. Front = 0,
  13. Back = 1,
  14. Up = 2,
  15. Down = 3,
  16. Left = 4,
  17. Right = 5
  18. }
  19.  
  20. local turnTrack = {}
  21. local moveTrack = {}
  22. local depth = 0
  23.  
  24. local function hasValue( tbl, str )
  25. local f = false
  26. for i = 1, #tbl do
  27. if type( tbl[i] ) == "table" then
  28. f = hasValue( tbl[i], str ) -- return value from recursion
  29. if f then break end -- if it returned true, break out of loop
  30. elseif tbl[i] == str then
  31. return true
  32. end
  33. end
  34. return f
  35. end
  36.  
  37. function trackValue(depth, dir)
  38. return {depth=depth, dir=dir}
  39. end
  40.  
  41. function moveAndDig(dir)
  42.  
  43. if dir == Dir.Front then
  44. while turtle.forward() == false do
  45. turtle.dig()
  46. end
  47. table.insert(moveTrack, trackValue(depth, Dir.Front))
  48. elseif dir == Dir.Back then
  49. turtle.back()
  50. table.insert(moveTrack, trackValue(depth, Dir.Back))
  51. elseif dir == Dir.Up then
  52. while turtle.up() == false do
  53. turtle.digUp()
  54. end
  55. table.insert(moveTrack, trackValue(depth, Dir.Up))
  56. elseif dir == Dir.Down then
  57. while turtle.down() == false do
  58. turtle.digDown()
  59. end
  60. table.insert(moveTrack, trackValue(depth, Dir.Down))
  61. end
  62. end
  63.  
  64. function alignDir(dir)
  65.  
  66. if dir == Dir.Left then
  67. turtle.turnLeft()
  68. table.insert(turnTrack, trackValue(depth, Dir.Left))
  69.  
  70. elseif dir == Dir.Right then
  71. turtle.turnRight()
  72. table.insert(turnTrack, trackValue(depth, Dir.Right))
  73.  
  74. elseif dir == Dir.Back then
  75. turtle.turnRight()
  76. turtle.turnRight()
  77. table.insert(turnTrack, trackValue(depth, Dir.Right))
  78. table.insert(turnTrack, trackValue(depth, Dir.Right))
  79.  
  80. end
  81. end
  82.  
  83. function undoAlignDir(dir)
  84.  
  85. if dir == Dir.Left then
  86. turtle.turnRight()
  87. print("Right")
  88.  
  89. elseif dir == Dir.Right then
  90. turtle.turnLeft()
  91. print("Left")
  92.  
  93. elseif dir == Dir.Back then
  94. turtle.turnLeft()
  95. turtle.turnLeft()
  96. print("Left Left")
  97.  
  98. end
  99. end
  100.  
  101. function backtrack(targetDepth)
  102. local depth = depth - 1
  103. while depth >= targetDepth do
  104. while #moveTrack > 0 and moveTrack[#moveTrack].depth == depth do
  105. local vec = table.remove(moveTrack)
  106. if vec.dir == Dir.Front then
  107. turtle.back()
  108. print("Back")
  109. elseif vec.dir == Dir.Up then
  110. turtle.down()
  111. print("Down")
  112. elseif vec.dir == Dir.Down then
  113. turtle.up()
  114. print("Up")
  115. end
  116. end
  117.  
  118. while #turnTrack > 0 and turnTrack[#turnTrack].depth == depth do
  119. local vec = table.remove(turnTrack)
  120. undoAlignDir(vec.dir)
  121. end
  122. depth = depth - 1
  123. end
  124. end
  125.  
  126. function testProgram()
  127. for i = 1, 10, 1 do
  128. local rand = math.random(1, 4)
  129. if rand == 1 then
  130. alignDir(Dir.Left)
  131. elseif rand == 2 then
  132. alignDir(Dir.Right)
  133. elseif rand == 3 then
  134. moveAndDig(Dir.Front)
  135. elseif rand == 4 then
  136. moveAndDig(Dir.Up)
  137. elseif rand == 5 then
  138. moveAndDig(Dir.Down)
  139. end
  140. depth = depth + 1
  141. os.sleep(1)
  142. end
  143. end
  144.  
  145. function detectOres()
  146. local succes, blockInfo
  147. local returnValue = {}
  148.  
  149. succes, blockInfo = turtle.inspect()
  150. if succes and hasValue(mine_blocks, blockInfo.name) then
  151. table.insert(returnValue, Dir.Front)
  152. return returnValue
  153. end
  154.  
  155. turtle.turnLeft()
  156. succes, blockInfo = turtle.inspect()
  157. if succes and hasValue(mine_blocks, blockInfo.name) then
  158. table.insert(returnValue, Dir.Left)
  159. turtle.turnRight()
  160. return returnValue
  161. end
  162.  
  163. turtle.turnRight()
  164. turtle.turnRight()
  165. succes, blockInfo = turtle.inspect()
  166. if succes and hasValue(mine_blocks, blockInfo.name) then
  167. table.insert(returnValue, Dir.Right)
  168. turtle.turnLeft()
  169. return returnValue
  170. end
  171.  
  172. turtle.turnLeft()
  173.  
  174. succes, blockInfo = turtle.inspectUp()
  175. if succes and hasValue(mine_blocks, blockInfo.name) then
  176. table.insert(returnValue, Dir.Up)
  177. return returnValue
  178. end
  179.  
  180. succes, blockInfo = turtle.inspectDown()
  181. if succes and hasValue(mine_blocks, blockInfo.name) then
  182. table.insert(returnValue, Dir.Down)
  183. return returnValue
  184. end
  185.  
  186. return returnValue
  187. end
  188.  
  189. function dfs()
  190. local ores = detectOres()
  191.  
  192. while #ores ~= 0 do
  193. ores = detectOres()
  194. alignDir(ores[1])
  195. print(ores[1])
  196.  
  197. if ores[1] == Dir.Up then
  198. moveAndDig(Dir.Up)
  199. elseif ores[1] == Dir.Down then
  200. moveAndDig(Dir.Down)
  201. else
  202. moveAndDig(Dir.Front)
  203. end
  204. depth = depth + 1
  205. end
  206.  
  207. while depth > 0 do
  208. backtrack(depth - 1)
  209. ores = detectOres()
  210. if #ores ~= 0 then
  211. dfs()
  212. break
  213. end
  214. depth = depth - 1
  215. end
  216. end
  217.  
  218. dfs()
  219. -- testProgram()
  220. -- os.sleep(2)
  221.  
Add Comment
Please, Sign In to add comment