Advertisement
ThaBullfrog

Untitled

Jan 25th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. local computer = require('computer')
  2. local robot = require("robot");
  3. local component = require("component")
  4. local inv = component.inventory_controller
  5. local geo = component.geolyzer
  6. local chunk = component.chunkloader
  7.  
  8. chunk.setActive(true)
  9.  
  10. local branchLength = 32
  11. local turnRight = true
  12.  
  13. local toolSlotCount = 2
  14.  
  15. function newSet(arr)
  16. local set = {}
  17. for i, v in ipairs(arr) do
  18. set[v] = true
  19. end
  20. return set
  21. end
  22.  
  23. local slots = {
  24. {'charger', 1},
  25. {'lever', 1},
  26. {'battery', 1},
  27. {'fortune', 1},
  28. {'tool', 2}
  29. }
  30.  
  31. local toolIds = newSet({
  32. 'minecraft:stone_pickaxe',
  33. 'minecraft:iron_pickaxe',
  34. 'minecraft:diamond_pickaxe'
  35. })
  36.  
  37. local validItemIds = {
  38. charger = newSet({'opencomputers:charger'}),
  39. lever = newSet({'minecraft:lever'}),
  40. battery = newSet({'thermalexpansion:cell'}),
  41. fortune = toolIds,
  42. tool = toolIds
  43. }
  44.  
  45. slotIdRange = {}
  46.  
  47. local itemSlotCount = 0
  48. local currentSlot = robot.inventorySize()
  49.  
  50. for i = #slots, 1, -1 do
  51. slotIdRange[slots[i][1]] = {currentSlot - (slots[i][2] - 1), currentSlot}
  52. itemSlotCount = itemSlotCount + slots[i][2]
  53. currentSlot = currentSlot - slots[i][2]
  54. end
  55.  
  56. local validTools = {
  57. 'minecraft:stone_pickaxe',
  58. 'minecraft:iron_pickaxe',
  59. 'minecraft:diamond_pickaxe'
  60. }
  61.  
  62. local ores = newSet({
  63. 'minecraft:gold_ore',
  64. 'minecraft:iron_ore',
  65. 'minecraft:coal_ore',
  66. 'minecraft:lapis_ore',
  67. 'minecraft:diamond_ore',
  68. 'minecraft:redstone_ore',
  69. 'minecraft:emerald_ore',
  70. 'thaumcraft:ore_cinnabar',
  71. })
  72.  
  73. local fortuneOres = newSet({
  74. 'minecraft:diamond_ore',
  75. 'minecraft:emerald_ore',
  76. 'thermalfoundation:ore',
  77. })
  78.  
  79. local bottom = 0
  80. local top = 1
  81. local back = 2
  82. local front = 3
  83. local right = 4
  84. local left = 5
  85.  
  86. function forward()
  87. for i = 1, 5, 1 do
  88. if robot.forward() then
  89. break
  90. else
  91. swingIfTool()
  92. end
  93. end
  94. end
  95.  
  96. function back()
  97. for i = 1, 5, 1 do
  98. if robot.back() then
  99. break
  100. else
  101. robot.turnAround()
  102. swingIfTool()
  103. robot.turnAround()
  104. end
  105. end
  106. end
  107.  
  108. function up()
  109. for i = 1, 5, 1 do
  110. if robot.up() then
  111. break
  112. else
  113. swingUpIfTool()
  114. end
  115. end
  116. end
  117.  
  118. function down()
  119. for i = 1, 5, 1 do
  120. if robot.down() then
  121. break
  122. else
  123. swingDownIfTool()
  124. end
  125. end
  126. end
  127.  
  128. function place()
  129. for i = 1, 5, 1 do
  130. if robot.place() then
  131. break
  132. else
  133. swingIfTool()
  134. end
  135. end
  136. end
  137.  
  138. function placeUp()
  139. for i = 1, 5, 1 do
  140. if robot.placeUp() then
  141. break
  142. else
  143. swingUpIfTool()
  144. end
  145. end
  146. end
  147.  
  148. function placeDown()
  149. for i = 1, 5, 1 do
  150. if robot.placeDown() then
  151. break
  152. else
  153. swingDownIfTool()
  154. end
  155. end
  156. end
  157.  
  158. function fortuneSwap()
  159. if slotIdRange['fortune'] then
  160. local item = inv.getStackInInternalSlot(slotIdRange['fortune'][1])
  161. if isValid('fortune', item) then
  162. equipSlot(slotIdRange['fortune'][1])
  163. end
  164. end
  165. end
  166.  
  167. function isValid(kind, item)
  168. if item == nil then
  169. return false
  170. end
  171. return validItemIds[kind][item.name]
  172. end
  173.  
  174. function isOre(block)
  175. return ores[block.name] or false
  176. end
  177.  
  178. function isFortuneOre(block)
  179. return fortuneOres[block.name] or false
  180. end
  181.  
  182. function mineOre()
  183. for i = 0, 5, 1 do
  184. local block = geo.analyze(i)
  185. if isOre(block) then
  186. local isFortune = isFortuneOre(block)
  187. if isFortune then
  188. fortuneSwap()
  189. end
  190. mineInDir(i)
  191. if isFortune then
  192. fortuneSwap()
  193. end
  194. mineOre()
  195. undoMineInDir(i)
  196. end
  197. end
  198. end
  199.  
  200. function mineInDir(dir)
  201. if dir == 0 then
  202. swingDownIfTool()
  203. down()
  204. elseif dir == 1 then
  205. swingUpIfTool()
  206. up()
  207. elseif dir == 2 then
  208. robot.turnAround()
  209. swingIfTool()
  210. forward()
  211. elseif dir == 3 then
  212. swingIfTool()
  213. forward()
  214. elseif dir == 4 then
  215. robot.turnRight()
  216. swingIfTool()
  217. forward()
  218. elseif dir == 5 then
  219. robot.turnLeft()
  220. swingIfTool()
  221. forward()
  222. else
  223. error('ERROR: invalid dir')
  224. end
  225. end
  226.  
  227. function undoMineInDir(dir)
  228. if dir == 0 then
  229. up()
  230. elseif dir == 1 then
  231. down()
  232. elseif dir == 2 then
  233. robot.turnAround()
  234. forward()
  235. elseif dir == 3 then
  236. back()
  237. elseif dir == 4 then
  238. back()
  239. robot.turnLeft()
  240. elseif dir == 5 then
  241. back()
  242. robot.turnRight()
  243. else
  244. error('ERROR: invalid dir')
  245. end
  246. end
  247.  
  248. function turnInBranchDir()
  249. if turnRight then
  250. robot.turnRight()
  251. else
  252. robot.turnLeft()
  253. end
  254. end
  255.  
  256. function swingIfTool(dontRecharge)
  257. for i = 1, 5, 1 do
  258. if robot.durability() == nil then
  259. equip('tool')
  260. end
  261. robot.swing()
  262. raiseIfInventoryFull()
  263. if not dontRecharge then
  264. rechargeIfNeeded()
  265. end
  266. if not robot.detect() then
  267. break
  268. end
  269. end
  270. end
  271.  
  272. function swingUpIfTool(dontRecharge)
  273. for i = 1, 5, 1 do
  274. if robot.durability() == nil then
  275. equip('tool')
  276. end
  277. robot.swingUp()
  278. raiseIfInventoryFull()
  279. if not dontRecharge then
  280. rechargeIfNeeded()
  281. end
  282. if not robot.detectUp() then
  283. break
  284. end
  285. end
  286. end
  287.  
  288. function swingDownIfTool(dontRecharge)
  289. for i = 1, 5, 1 do
  290. if robot.durability() == nil then
  291. equip('tool')
  292. end
  293. robot.swingDown()
  294. raiseIfInventoryFull()
  295. if not dontRecharge then
  296. rechargeIfNeeded()
  297. end
  298. if not robot.detectDown() then
  299. break
  300. end
  301. end
  302. end
  303.  
  304. function hasAny(kind)
  305. for i = slotIdRange[kind][1], slotIdRange[kind][2], 1 do
  306. if isValid(kind, inv.getStackInInternalSlot(i)) then
  307. return true
  308. end
  309. end
  310. return false
  311. end
  312.  
  313. function hasChargedBattery()
  314. for i = slotIdRange['battery'][1], slotIdRange['battery'][2], 1 do
  315. local stack = inv.getStackInInternalSlot(i)
  316. if isValid('battery', inv.getStackInInternalSlot(i)) then
  317. return stack.Energy and stack.Energy > 1000
  318. end
  319. end
  320. return false
  321. end
  322.  
  323. function raiseIfInventoryFull()
  324. local lastSlot = robot.inventorySize() - itemSlotCount
  325. if inv.getStackInInternalSlot(lastSlot) ~= nil then
  326. error('ERROR: inventory full')
  327. end
  328. end
  329.  
  330. function includes(arr, item)
  331. for i, v in ipairs(arr) do
  332. if v == item then
  333. return true
  334. end
  335. end
  336. return false
  337. end
  338.  
  339. function equip(kind)
  340. local itemFound = false
  341. for i = slotIdRange[kind][1], slotIdRange[kind][2], 1 do
  342. if isValid(kind, inv.getStackInInternalSlot(i)) then
  343. equipSlot(i)
  344. itemFound = true
  345. end
  346. end
  347. if not itemFound then
  348. error('ERROR: no item \''..kind..'\' found')
  349. end
  350. end
  351.  
  352. function selectItem(kind)
  353. local itemFound = false
  354. for i = slotIdRange[kind][1], slotIdRange[kind][2], 1 do
  355. if isValid(kind, inv.getStackInInternalSlot(i)) then
  356. robot.select(i)
  357. itemFound = true
  358. end
  359. end
  360. if not itemFound then
  361. error('ERROR: no item \''..kind..'\' found')
  362. end
  363. end
  364.  
  365. function selectBlankItemSlot(kind)
  366. local blankSlotFound = false
  367. for i = slotIdRange[kind][1], slotIdRange[kind][2], 1 do
  368. if inv.getStackInInternalSlot(i) == nil then
  369. robot.select(i)
  370. blankSlotFound = true
  371. end
  372. end
  373. if not blankSlotFound then
  374. error('ERROR: no blank slot found for item \''..kind..'\'')
  375. end
  376. end
  377.  
  378. function equipSlot(slotIndex)
  379. robot.select(slotIndex)
  380. inv.equip()
  381. robot.select(1)
  382. end
  383.  
  384. function fixPositionOf(kind)
  385. for i = 1, robot.inventorySize() - itemSlotCount, 1 do
  386. if isValid(kind, inv.getStackInInternalSlot(i)) then
  387. for j = slotIdRange[kind][1], slotIdRange[kind][2], 1 do
  388. if not isValid(kind, inv.getStackInInternalSlot(j)) then
  389. robot.select(i)
  390. robot.transferTo(j)
  391. robot.select(1)
  392. break
  393. end
  394. end
  395. end
  396. end
  397. end
  398.  
  399. function putAway()
  400. foundSlot = false
  401. for i = slotIdRange['tool'][1], slotIdRange['tool'][2], 1 do
  402. if not isValid('tool', inv.getStackInInternalSlot(i)) then
  403. equipSlot(i)
  404. foundSlot = true
  405. break
  406. end
  407. end
  408. if not foundSlot then
  409. error('ERROR: not open tool slot found')
  410. end
  411. end
  412.  
  413. local high = false
  414.  
  415. function mineForward()
  416. swingIfTool()
  417. forward()
  418. if not high then
  419. placeDownIfAir()
  420. end
  421. mineOre()
  422. if high then
  423. swingDownIfTool()
  424. down()
  425. placeDownIfAir()
  426. else
  427. swingUpIfTool()
  428. up()
  429. end
  430. high = not high
  431. mineOre()
  432. end
  433.  
  434. function placeDownIfAir()
  435. if not robot.detectDown() then
  436. robot.placeDown()
  437. end
  438. if not robot.detectDown() then
  439. error("ERROR: place failed!")
  440. end
  441. end
  442.  
  443. function branch()
  444. turnInBranchDir()
  445. for i = 1, branchLength, 1 do
  446. mineForward()
  447. end
  448. robot.turnAround()
  449. for i = 1, branchLength, 1 do
  450. forward()
  451. end
  452. turnInBranchDir()
  453. end
  454.  
  455. function rechargeIfNeeded()
  456. local energyLow = computer.energy() / computer.maxEnergy() < 0.15
  457. if energyLow and hasChargedBattery() then
  458. recharge()
  459. end
  460. end
  461.  
  462. function recharge()
  463. robot.turnAround()
  464. swingIfTool(true)
  465. swingUpIfTool(true)
  466. forward()
  467. swingUpIfTool(true)
  468. swingIfTool(true)
  469. forward()
  470. swingUpIfTool(true)
  471. robot.turnAround()
  472. forward()
  473. selectItem('battery')
  474. place()
  475. back()
  476. selectItem('charger')
  477. place()
  478. up()
  479. selectItem('lever')
  480. place()
  481. robot.use()
  482. down()
  483. while computer.energy() / computer.maxEnergy() < .99 do
  484. os.sleep(3)
  485. end
  486. up()
  487. swingIfTool(true)
  488. down()
  489. selectBlankItemSlot('charger')
  490. swingIfTool(true)
  491. forward()
  492. selectBlankItemSlot('battery')
  493. swingIfTool(true)
  494. robot.select(1)
  495. forward()
  496. end
  497.  
  498. while true do
  499. for i = 1, 4, 1 do
  500. mineForward()
  501. end
  502. branch()
  503. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement