Advertisement
Guest User

Untitled

a guest
May 26th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. local ENDER_CHEST_SLOT = 2
  2. local DIG_SLOT = 3
  3.  
  4. --save movments
  5. function move(move, detect, dig, attack)
  6. if needsPitstop() then
  7. doPitstop()
  8. end
  9.  
  10. local first = true
  11. while not move() do
  12. if detect() then
  13. turtle.select(DIG_SLOT)
  14. dig()
  15. else
  16. attack()
  17. end
  18.  
  19. if not first then
  20. sleep(0.5)
  21. end
  22. first = false
  23. end
  24. end
  25.  
  26. function moveForward()
  27. move(turtle.forward, turtle.detect, turtle.dig, turtle.attack)
  28. end
  29.  
  30. function moveBack()
  31. turn180()
  32. moveForward()
  33. turn180()
  34. end
  35.  
  36. function moveUp()
  37. move(turtle.up, turtle.detectUp, turtle.digUp, turtle.attackUp)
  38. end
  39.  
  40. function moveDown()
  41. move(turtle.down, turtle.detectDown, turtle.digDown, turtle.attackDown)
  42. end
  43.  
  44. function turn180()
  45. turtle.turnLeft()
  46. turtle.turnLeft()
  47. end
  48.  
  49. --pitstop
  50. function doPitstop()
  51. if turtle.detectUp() then
  52. turtle.select(DIG_SLOT)
  53. turtle.digUp()
  54. end
  55.  
  56. turtle.select(ENDER_CHEST_SLOT)
  57. turtle.placeUp()
  58.  
  59. local enderChest = peripherals.wrap()
  60.  
  61. if not hasSpace() then
  62. for i = 3, 16, 1 do
  63. while turtle.getItemCount(i) > 0 do
  64. local moved = false
  65. for j = 2, enderChest.getInventorySize(), 1 do
  66. if enderChest.pullItem("down", i, 64, j) ~= 0 then
  67. moved = true
  68. end
  69. end
  70.  
  71. if not moved then
  72. sleep(2)
  73. end
  74. end
  75. end
  76. end
  77.  
  78. --TODO: add support for normal fuel
  79. while turtle.getFuelLevel() < 5 do
  80. local lavaBucket = enderChest.getStackInSlot(1)
  81. if lavaBucket ~= nil and lavaBucket["id"] == "minecraft:lava_bucket" then
  82. enderChest.pushItem("down", 1, 64, 1)
  83. turtle.select(1)
  84. turtle.refuel()
  85. enderChest.pullItem("down", 1, 64, 1)
  86. end
  87.  
  88. if turtle.getFuelLevel() < 5 then
  89. sleep(1)
  90. end
  91. end
  92.  
  93. turtle.select(ENDER_CHEST_SLOT)
  94. turtle.digUp()
  95. end
  96.  
  97. function needsPitstop()
  98. return (not hasSpace()) or turtle.getFuelLevel() < 5
  99. end
  100.  
  101. function hasSpace()
  102. local count = 0
  103. for i = 3, 16, 1 do
  104. if turtle.getItemCount(i) == 0 then
  105. count = count + 1
  106. end
  107. end
  108. return count > 1
  109. end
  110.  
  111. function readNumber()
  112. local num = -1
  113. repeat
  114. if num ~= -1 then
  115. print("Text is not a number.")
  116. end
  117. num = tonumber(read())
  118. until num ~= nil
  119. return num
  120. end
  121.  
  122. --ore stuff
  123. function isOre()
  124. local success, data = turtle.inspect()
  125. return checkOre(success, data)
  126. end
  127.  
  128. function isOreUp()
  129. local success, data = turtle.inspectUp()
  130. return checkOre(success, data)
  131. end
  132.  
  133. function isOreDown()
  134. local success, data = turtle.inspectDown()
  135. return checkOre(success, data)
  136. end
  137.  
  138. function checkOre(success, data)
  139. if success then
  140. local name = data["name"]
  141. if name ~= nil then
  142. --Alternative name bypass
  143. if name == "Forestry:resources" or name == "TConstruct:SearedBrick"
  144. or name == "denseores:block0" or name == "rftools:dimensionalShardBlock"
  145. or name == "harvestcraft:salt" then
  146. return true
  147. end
  148.  
  149. local index = string.find(name, ":")
  150. if index ~= nil then
  151. local part = string.lower(string.sub(name, index))
  152. local isOre = string.find(part, "ore")
  153. return isOre ~= nil
  154. end
  155. end
  156. end
  157. return false
  158. end
  159.  
  160. --vein stuff
  161. function mineVein()
  162. while true do
  163. if isOre() then
  164. veinMoveForward()
  165. elseif isOreUp() then
  166. veinMoveUp()
  167. elseif isOreDown() then
  168. veinMoveDown()
  169. else
  170. local success = false
  171. for i = 1, 3, 1 do
  172. veinMoveLeft()
  173. if isOre() then
  174. veinMoveForward()
  175. success = true
  176. break
  177. end
  178. end
  179.  
  180. if not success then
  181. if not popVeinMovment() then
  182. return
  183. end
  184. end
  185. end
  186. sleep(0.5)
  187. end
  188. end
  189.  
  190. function veinMoveForward()
  191. moveForward()
  192. pushToVeinStack(1)
  193. end
  194.  
  195. function veinMoveUp()
  196. moveUp()
  197. pushToVeinStack(2)
  198. end
  199.  
  200. function veinMoveDown()
  201. moveDown()
  202. pushToVeinStack(3)
  203. end
  204.  
  205. function veinMoveLeft()
  206. turtle.turnLeft()
  207. pushToVeinStack(4)
  208. end
  209.  
  210. function veinMoveRight()
  211. turtle.turnRight()
  212. pushToVeinStack(5)
  213. end
  214.  
  215. function veinMoveBack()
  216. moveBack()
  217. pushToVeinStack(6)
  218. end
  219.  
  220. function popVeinMovment()
  221. local direction = getFromVeinStack()
  222.  
  223. if direction == nil then
  224. return false
  225. end
  226.  
  227. if direction == 1 then
  228. moveBack()
  229. elseif direction == 2 then
  230. moveDown()
  231. elseif direction == 3 then
  232. moveUp()
  233. elseif direction == 4 then
  234. turtle.turnRight()
  235. removeLastVeinStack()
  236. return popVeinMovment()
  237. elseif direction == 5 then
  238. turtle.turnLeft()
  239. removeLastVeinStack()
  240. return popVeinMovment()
  241. elseif direction == 6 then
  242. moveForward()
  243. elseif direction == 0 then
  244. removeLastVeinStack()
  245. return popVeinMovment()
  246. end
  247.  
  248. removeLastVeinStack()
  249.  
  250. return true;
  251. end
  252.  
  253. function pushToVeinStack(direction)
  254. local handle = fs.open("VeinStack.dat", "a")
  255. handle.writeLine(tostring(direction))
  256. handle.close()
  257. end
  258.  
  259. function getFromVeinStack()
  260. local data = getVeinStack()
  261.  
  262. if data ~= nil then
  263. return tonumber(data[#data])
  264. end
  265. return nil
  266. end
  267.  
  268. function getVeinStack()
  269. --optimize vein stack
  270. local handle = fs.open("VeinStack.dat", "r")
  271.  
  272. if handle ~= nil then
  273. local data = {}
  274. local index = 0
  275. repeat
  276. index = index + 1
  277. data[index] = handle.readLine()
  278. until data[index] == nil
  279. handle.close()
  280.  
  281. if #data > 0 then
  282. data = optimizeVeinStack(data)
  283. if #data > 0 then
  284. return data
  285. end
  286. end
  287. end
  288. return nil
  289. end
  290.  
  291. function optimizeVeinStack(data)
  292. local lastAction = 0
  293. local actionCount = 0
  294.  
  295. for i = 1, #data, 1 do
  296. --turn right and then left is somewhat useless
  297. if data[i] == 4 and lastAction == 5 or
  298. data[i] == 5 and lastAction == 4 then
  299. data = moveArrayContent(data, i + 2, 2)
  300.  
  301. if #data < 1 then
  302. break
  303. end
  304. end
  305.  
  306. if data[i] ~= lastAction then
  307. lastAction = 0
  308. actionCount = 0
  309. end
  310.  
  311. if data[i] == 4 then
  312. lastAction = 4
  313. actionCount = actionCount + 1
  314. elseif data[i] == 5 then
  315. lastAction = 5
  316. actionCount = actionCount + 1
  317. end
  318.  
  319. if actionCount == 3 then
  320. local newAction = 4
  321. if lastAction == 4 then
  322. newAction = 5
  323. end
  324.  
  325. data = moveArrayContent(data, i + 1, 2)
  326. data[i - 2] = newAction
  327.  
  328. i = i - 1
  329. end
  330. end
  331. return data
  332. end
  333.  
  334. function moveArrayContent(array, startIndex, amount)
  335. local size = #array
  336. for i = startIndex, size, 1 do
  337. array[i - amount] = array[i]
  338. array[i] = nil
  339. end
  340. return array
  341. end
  342.  
  343. function removeLastVeinStack()
  344. local data = getVeinStack()
  345.  
  346. if data ~= nil then
  347. handle = fs.open("VeinStack.dat", "w")
  348. for i = 1, #data - 1, 1 do
  349. handle.writeLine(data[i])
  350. end
  351. handle.close()
  352. end
  353. end
  354.  
  355.  
  356. mineVein()
  357. --print("Enter the max depth of the mine.")
  358. --local depth = readNumber()
  359.  
  360. --print("Enter the max width of the mine.")
  361. --local width = readNumber()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement