asweigart

trenchminer

Mar 30th, 2016
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. -- A ComputerCraft script for mining in a straight line forward.
  2.  
  3. local tArgs = {...}
  4. local depth = 0
  5. local distance = 0
  6. local MAX_DISTANCE
  7.  
  8. if tonumber(tArgs[1]) == nil then
  9. MAX_DISTANCE = 8
  10. else
  11. MAX_DISTANCE = tonumber(tArgs[1])
  12. end
  13.  
  14.  
  15. local success, lastDistance, lastDepth
  16.  
  17.  
  18.  
  19. local function unloadInventory()
  20. local i
  21. print('Emptying inventory...')
  22. -- TODO: check for chest?
  23. turtle.turnLeft()
  24. turtle.turnLeft()
  25. for i = 1,16 do
  26. turtle.select(i)
  27. turtle.drop(64)
  28. end
  29. turtle.turnLeft()
  30. turtle.turnLeft()
  31. end
  32.  
  33. local function refuelIfNeeded()
  34. local i, itemData, coalNeeded
  35. coalNeeded = math.floor((turtle.getFuelLimit() - turtle.getFuelLevel()) / 80)
  36.  
  37. for i = 1,16 do
  38. itemData = turtle.getItemDetail(i)
  39. if itemData ~= nil and itemData[name] == 'minecraft:coal' and coalNeeded > 0 then
  40. print('Refueling, coal needed = ' .. coalNeeded)
  41. turtle.refuel(coalNeeded)
  42. coalNeeded = math.floor((turtle.getFuelLimit() - turtle.getFuelLevel()) / 80)
  43. end
  44. end
  45. end
  46.  
  47. local function returnHome()
  48. local i
  49. print('Returning home...')
  50. for i = 1,depth do
  51. turtle.up()
  52. depth = depth - 1
  53. end
  54. for i = 1,distance do
  55. turtle.back()
  56. distance = distance - 1
  57. end
  58.  
  59. unloadInventory()
  60. end
  61.  
  62. local function mustUnloadInventory()
  63. -- check for free inventory space
  64. refuelIfNeeded() -- clear out any coal inventory
  65.  
  66. local filledUp = true
  67. for i = 1,16 do
  68. if turtle.getItemCount(i) == 0 then
  69. filledUp = false
  70. end
  71. end
  72. if filledUp then
  73. print('Must unload inventory.')
  74. lastDistance = distance
  75. lastDepth = depth
  76. returnHome()
  77. if lastDepth + lastDistance < turtle.getFuelLevel() then
  78. for i = 1,lastDistance do
  79. turtle.forward()
  80. distance = distance + 1
  81. end
  82. for i = 1,lastDepth do
  83. turtle.down()
  84. depth = depth + 1
  85. end
  86. end
  87. end
  88. return false
  89. end
  90.  
  91. local function mustRefuel()
  92. refuelIfNeeded()
  93. if depth + distance >= turtle.getFuelLevel() then
  94. print('Not enough fuel to continue.')
  95. returnHome()
  96. return true
  97. end
  98. return false
  99. end
  100.  
  101. local function main()
  102. local i, blockData
  103. while true do
  104. -- fuel check
  105. if mustRefuel() then break end
  106.  
  107. -- dig left, right, down
  108. turtle.turnLeft()
  109. if turtle.inspect() ~= false then turtle.dig() end
  110. turtle.turnRight()
  111. if mustUnloadInventory() then break end
  112. turtle.turnRight()
  113. if turtle.inspect() ~= false then turtle.dig() end
  114. turtle.turnLeft()
  115. if mustUnloadInventory() then break end
  116. if turtle.inspectDown() ~= false then turtle.digDown() end
  117. if mustUnloadInventory() then break end
  118.  
  119. blockExists, blockData = turtle.inspectDown()
  120. if blockExists == true and blockData ~= nil and blockData['name'] == 'minecraft:bedrock' then
  121. -- hit bedrock, return home
  122. print('Hit bedrock')
  123. for i = 1,depth do
  124. turtle.up()
  125. depth = depth - 1
  126. end
  127. if distance == MAX_DISTANCE - 1 then
  128. print('Reached max distance.')
  129. returnHome()
  130. break
  131. end
  132. -- move forward
  133. if turtle.inspect() ~= false then turtle.dig() end
  134. if mustUnloadInventory() then break end
  135. turtle.forward()
  136. distance = distance + 1
  137. if mustRefuel() then break end
  138. else
  139. -- go down
  140. turtle.down()
  141. depth = depth + 1
  142. if mustRefuel() then break end
  143. end
  144. end
  145. end
  146.  
  147. main()
Advertisement
Add Comment
Please, Sign In to add comment