Advertisement
0xSRK6

Computercraft Stripmine

Apr 12th, 2023 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. --Written by shankofmeat
  2.  
  3.  
  4.  
  5. local MAX_LENGTH = 128
  6.  
  7. local function convertSide(num) --just converts number to side (for the for loops)
  8. local conversionKey = {"front", "up", "right", "down", "left"}
  9. return conversionKey[num]
  10. end
  11.  
  12.  
  13. local function analyze(name, entries) --returns true if something to mine, false if not
  14. if name and name ~= "nil" then
  15. for i, v in ipairs(entries) do
  16. if v == name then
  17. return true
  18. end
  19. end
  20. return false
  21. end
  22. end
  23.  
  24.  
  25.  
  26. local function check() --checks the sides of the turtle, and returns a data buffer with the name of the block on each side of the turtle
  27. local statusBuf = {}
  28.  
  29. --check front
  30. local t, data = turtle.inspect()
  31. if t then statusBuf[#statusBuf+1] = data.name else statusBuf[#statusBuf+1] = "nil" end
  32.  
  33. --check up
  34. t, data = turtle.inspectUp()
  35. if t then statusBuf[#statusBuf+1] = data.name else statusBuf[#statusBuf+1] = "nil" end
  36.  
  37. --check right
  38. turtle.turnRight()
  39. t, data = turtle.inspect()
  40. if t then statusBuf[#statusBuf+1] = data.name else statusBuf[#statusBuf+1] = "nil" end
  41.  
  42. --check down
  43. t, data = turtle.inspectDown()
  44. if t then statusBuf[#statusBuf+1] = data.name else statusBuf[#statusBuf+1] = "nil" end
  45.  
  46. --check left
  47. turtle.turnRight()
  48. turtle.turnRight()
  49. t, data = turtle.inspect()
  50. if t then statusBuf[#statusBuf+1] = data.name else statusBuf[#statusBuf+1] = "nil" end
  51.  
  52. turtle.turnRight()
  53.  
  54. return statusBuf
  55. end
  56.  
  57. local function moveByCmd(cmd) --moves the turtle based on a simplistic command name I.E. "left"
  58. if cmd == "front" then
  59. turtle.forward()
  60. elseif cmd == "right" then
  61. turtle.turnRight()
  62. elseif cmd == "left" then
  63. turtle.turnLeft()
  64. elseif cmd == "up" then
  65. turtle.up()
  66. elseif cmd == "down" then
  67. turtle.down()
  68. elseif cmd == "back" then
  69. turtle.turnRight()
  70. turtle.turnRight()
  71. turtle.forward()
  72. turtle.turnRight()
  73. turtle.turnRight()
  74. end
  75. end
  76.  
  77. local function removeStone()
  78. for i = 1, 16 do
  79. local slotItem = turtle.getItemDetail(i, false)
  80. if slotItem ~= nil and slotItem.name == "minecraft:cobblestone" then
  81. turtle.select(i)
  82. turtle.drop(slotItem.count)
  83. end
  84. end
  85. turtle.select(1)
  86. end
  87.  
  88. local function initalize()
  89. local mineData = fs.open("minedata.txt", "r")
  90. local parsedData = {}
  91. while true do
  92. local line = mineData.readLine()
  93.  
  94. if not line then break end
  95.  
  96. parsedData[#parsedData+1] = line
  97. end
  98. return parsedData
  99. end
  100.  
  101.  
  102. local function inversion(movement) --used to invert the latest movement inside of the movement buffer
  103. local invertedMovements = {["front"] = "back", ["left"] = "right", ["right"] = "left", ["up"] = "down", ["down"] = "up", ["back"] = "front"}
  104. return invertedMovements[movement]
  105. end
  106.  
  107.  
  108.  
  109. local function veinMine(initSide, entries)
  110. local moveBuf = {}
  111.  
  112. if initSide == "right" or initSide == "left" then
  113. moveByCmd(initSide)
  114. moveBuf[#moveBuf+1] = initSide
  115. else
  116. if initSide == "up" then
  117. turtle.digUp()
  118. elseif initSide == "down" then
  119. turtle.digDown()
  120. elseif initSide == "front" then
  121. turtle.dig()
  122. end
  123. moveByCmd(initSide)
  124. moveBuf[#moveBuf+1] = initSide
  125. end
  126.  
  127. local foundBlock = true
  128. while foundBlock do
  129. foundBlock = false
  130. local statusBuf = check()
  131. for i, v in ipairs(statusBuf) do
  132. local sideName = convertSide(i)
  133. if analyze(v, entries) then
  134. foundBlock = true
  135. print(sideName)
  136. if sideName == "up" then
  137. turtle.digUp()
  138. elseif sideName == "down" then
  139. turtle.digDown()
  140. elseif sideName == "front" then
  141. turtle.dig()
  142. end
  143. moveByCmd(sideName)
  144. moveBuf[#moveBuf+1] = sideName
  145. end
  146. end
  147. print(textutils.serialise(moveBuf))
  148. end
  149.  
  150. -- Backtracking
  151. for i = #moveBuf, 1, -1 do
  152. local statusBuf = check()
  153. for j = 2, 5 do
  154. if (analyze(statusBuf[j], entries)) then
  155. veinMine(convertSide(j), entries)
  156. end
  157. end
  158. moveByCmd(inversion(moveBuf[i]))
  159. print(inversion(moveBuf[i]) .. " -backtrack")
  160. end
  161. end
  162.  
  163.  
  164.  
  165. local function main() --the entry point of the program
  166. local currentPos = 1
  167.  
  168. local mineData = initalize()
  169.  
  170. turtle.dig()
  171. turtle.forward()
  172. while (currentPos <= MAX_LENGTH) do
  173. local statusBuf = check()
  174. print(textutils.serialise(statusBuf))
  175. for i = 2, 5 do
  176. if (analyze(statusBuf[i], mineData)) then
  177. veinMine(convertSide(i), mineData)
  178. end
  179. end
  180. turtle.dig()
  181. turtle.forward()
  182. currentPos = currentPos + 1
  183. if currentPos % 16 == 0 then removeStone() end
  184. print("---------------------")
  185. end
  186. turtle.turnRight()
  187. turtle.turnRight()
  188. for i = 1, MAX_LENGTH do
  189. turtle.forward()
  190. end
  191. turtle.turnRight()
  192. turtle.turnRight()
  193.  
  194. end
  195.  
  196.  
  197. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement