Advertisement
ThaBullfrog

Untitled

Jan 20th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. local robot = require("robot");
  2. local component = require("component")
  3. local inv = component.inventory_controller
  4. local geo = component.geolyzer
  5.  
  6. local branchLength = 32
  7. local turnRight = true
  8.  
  9. local toolSlotCount = 2
  10. local fortune = true
  11.  
  12. local lastSlot = robot.inventorySize() - toolSlotCount
  13. if fortune then
  14. lastSlot = lastSlot - 1
  15. end
  16. local fortuneSlot = robot.inventorySize() - toolSlotCount
  17. local firstToolSlot = robot.inventorySize() - (toolSlotCount - 1)
  18.  
  19. local validTools = {
  20. 'minecraft:stone_pickaxe',
  21. 'minecraft:iron_pickaxe',
  22. 'minecraft:diamond_pickaxe'
  23. }
  24.  
  25. local oreList = {
  26. 'minecraft:dirt',
  27. 'minecraft:stone',
  28. }
  29.  
  30. local ores = {}
  31. for i, ore in ipairs(oreList) do
  32. ores[ore] = true
  33. end
  34.  
  35. local bottom = 0
  36. local top = 1
  37. local back = 2
  38. local front = 3
  39. local right = 4
  40. local left = 5
  41.  
  42. function fortuneSwap()
  43. if fortune then
  44. robot.select(fortuneSlot)
  45. inv.equip()
  46. robot.select(1)
  47. end
  48. end
  49.  
  50. function isOre(block)
  51. return ores[block.name] or false
  52. end
  53.  
  54. function mineOre(inOrePos)
  55. for i = 0, 5, 1 do
  56. local block = geo.analyze(i)
  57. local oreInDir = isOre(block)
  58. if oreInDir or inOrePos then
  59. local isDiamond = block.name == 'minecraft:stone'
  60. if isDiamond then
  61. fortuneSwap()
  62. end
  63. mineInDir(i)
  64. if isDiamond then
  65. fortuneSwap()
  66. end
  67. mineOre(oreInDir)
  68. undoMineInDir(i)
  69. end
  70. end
  71. end
  72.  
  73. function mineInDir(dir)
  74. if dir == 0 then
  75. swingDownIfTool()
  76. robot.down()
  77. elseif dir == 1 then
  78. swingUpIfTool()
  79. robot.up()
  80. elseif dir == 2 then
  81. robot.turnAround()
  82. swingIfTool()
  83. robot.forward()
  84. elseif dir == 3 then
  85. swingIfTool()
  86. robot.forward()
  87. elseif dir == 4 then
  88. robot.turnRight()
  89. swingIfTool()
  90. robot.forward()
  91. elseif dir == 5 then
  92. robot.turnLeft()
  93. swingIfTool()
  94. robot.forward()
  95. else
  96. error('ERROR: invalid dir')
  97. end
  98. end
  99.  
  100. function undoMineInDir(dir)
  101. if dir == 0 then
  102. robot.up()
  103. elseif dir == 1 then
  104. robot.down()
  105. elseif dir == 2 then
  106. robot.turnAround()
  107. robot.forward()
  108. elseif dir == 3 then
  109. robot.back()
  110. elseif dir == 4 then
  111. robot.back()
  112. robot.turnLeft()
  113. elseif dir == 5 then
  114. robot.back()
  115. robot.turnRight()
  116. else
  117. error('ERROR: invalid dir')
  118. end
  119. end
  120.  
  121. function isValidTool(stack)
  122. return includes(validTools, stack.name)
  123. end
  124.  
  125. function turnInBranchDir()
  126. if turnRight then
  127. robot.turnRight()
  128. else
  129. robot.turnLeft()
  130. end
  131. end
  132.  
  133. function swingIfTool()
  134. if robot.durability() == nil then
  135. grabTool()
  136. end
  137. robot.swing()
  138. raiseIfInventoryFull()
  139. end
  140.  
  141. function swingUpIfTool()
  142. if robot.durability() == nil then
  143. grabTool()
  144. end
  145. robot.swingUp()
  146. raiseIfInventoryFull()
  147. end
  148.  
  149. function swingDownIfTool()
  150. if robot.durability() == nil then
  151. grabTool()
  152. end
  153. robot.swingDown()
  154. raiseIfInventoryFull()
  155. end
  156.  
  157. function raiseIfInventoryFull()
  158. if inv.getStackInInternalSlot(lastSlot) ~= nil then
  159. error('ERROR: inventory full')
  160. end
  161. end
  162.  
  163. function includes(arr, item)
  164. for i, v in ipairs(arr) do
  165. if v == item then
  166. return true
  167. end
  168. end
  169. return false
  170. end
  171.  
  172. function grabTool()
  173. local toolFound = false
  174. for i = robot.inventorySize(), firstToolSlot, -1 do
  175. local stack = inv.getStackInInternalSlot(i)
  176. if stack ~= nil and isValidTool(stack) then
  177. robot.select(i)
  178. inv.equip()
  179. robot.select(1)
  180. toolFound = true
  181. end
  182. end
  183. if not toolFound then
  184. error('ERROR: no tool')
  185. end
  186. end
  187.  
  188. function mineForward()
  189. swingIfTool()
  190. robot.forward()
  191. placeDownIfAir()
  192. mineOre()
  193. swingUpIfTool()
  194. robot.up()
  195. mineOre()
  196. robot.down()
  197. end
  198.  
  199. function placeDownIfAir()
  200. if not robot.detectDown() then
  201. robot.placeDown()
  202. end
  203. if not robot.detectDown() then
  204. error("ERROR: place failed!")
  205. end
  206. end
  207.  
  208. function branch()
  209. turnInBranchDir()
  210. for i = 1, branchLength, 1 do
  211. mineForward()
  212. end
  213. robot.turnAround()
  214. for i = 1, branchLength, 1 do
  215. robot.forward()
  216. end
  217. turnInBranchDir()
  218. end
  219.  
  220. while true do
  221. for i = 1, 4, 1 do
  222. mineForward()
  223. end
  224. branch()
  225. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement