Advertisement
tike

Une Mine

Sep 28th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. --Local
  2.  
  3. local bedrockLayer = 5
  4.  
  5. local startingX = 0
  6. local startingY
  7. local startingZ = 0
  8. local startingD = 0
  9.  
  10. local currentX = 0
  11. local currentY = 0
  12. local currentZ = 0
  13. local currentD = 0
  14.  
  15. local squareSize
  16. local done = true
  17. local currentSlot
  18. local data
  19. local succes
  20. local horizontalState = 0
  21.  
  22. local i
  23.  
  24. direction = {NORTH=0, EAST=1, SOUTH=2, WEST=3}
  25.  
  26. -- Movement functions
  27.  
  28. function printAllData()
  29.  
  30. print("startingX" .. startingX)
  31. print("startingY" .. startingY)
  32. print("startingZ" .. startingZ)
  33. print("startingD" .. startingD)
  34. print("currentX" .. currentX)
  35. print("currentY" .. currentY)
  36. print("currentZ" .. currentZ)
  37. print("currentD" .. currentD)
  38. print("squareSize" .. squareSize)
  39.  
  40. end
  41.  
  42. function goUp()
  43.  
  44. currentY = currentY + 1
  45. turtle.digUp()
  46. turtle.up()
  47. checkFuelLevel()
  48. end
  49.  
  50. function goDown()
  51.  
  52. currentY = currentY - 1
  53. turtle.digDown()
  54. turtle.down()
  55. checkFuelLevel()
  56. end
  57.  
  58. function goNorth()
  59.  
  60. if(currentD == 3) then
  61. turtle.turnRight()
  62. end
  63.  
  64. if(currentD == 2) then
  65. turtle.turnRight()
  66. turtle.turnRight()
  67. end
  68.  
  69. if(currentD == 1) then
  70. turtle.turnLeft()
  71.  
  72. end
  73.  
  74. currentX = currentX + 1
  75. currentD = 0
  76. turtle.dig()
  77. turtle.forward()
  78. checkFuelLevel()
  79.  
  80. end
  81.  
  82. function goSouth()
  83.  
  84. if(currentD == 1) then
  85. turtle.turnRight()
  86. end
  87.  
  88. if(currentD == 0) then
  89. turtle.turnRight()
  90. turtle.turnRight()
  91. end
  92.  
  93. if(currentD == 3) then
  94. turtle.turnLeft()
  95. end
  96.  
  97. currentX = currentX - 1
  98.  
  99. currentD = 2
  100. turtle.dig()
  101. turtle.forward()
  102. checkFuelLevel()
  103.  
  104. end
  105.  
  106. function goEast()
  107.  
  108. if(currentD == 0) then
  109. turtle.turnRight()
  110. end
  111.  
  112. if(currentD == 3) then
  113. turtle.turnRight()
  114. turtle.turnRight()
  115. end
  116.  
  117. if(currentD == 2) then
  118. turtle.turnLeft()
  119. end
  120.  
  121. currentZ = currentZ + 1
  122.  
  123. currentD = 1
  124. turtle.dig()
  125. turtle.forward()
  126. checkFuelLevel()
  127.  
  128. end
  129.  
  130. function goWest()
  131.  
  132. if(currentD == 2) then
  133. turtle.turnRight()
  134. end
  135.  
  136. if(currentD == 1) then
  137. turtle.turnRight()
  138. turtle.turnRight()
  139. end
  140.  
  141. if(currentD == 0) then
  142. turtle.turnLeft()
  143. end
  144.  
  145. currentZ = currentZ - 1
  146.  
  147. currentD = 3
  148. turtle.dig()
  149. turtle.forward()
  150. checkFuelLevel()
  151. end
  152.  
  153. function goToMiningStart()
  154. print("goToMiningStart")
  155. while(currentY > (bedrockLayer)) do
  156. goDown()
  157. print("should go down")
  158. end
  159. end
  160.  
  161. function goToZero()
  162. print("goToZero")
  163. if(currentX < 0) then
  164. while (currentX < 0) do
  165. goNorth()
  166. end
  167. elseif(currentX > 0) then
  168. while (currentX > 0) do
  169. goSouth()
  170. end
  171. end
  172.  
  173. if(currentZ < 0) then
  174. while (currentZ < 0) do
  175. goEast()
  176. end
  177. elseif(currentZ > 0) then
  178. while (currentZ > 0) do
  179. goWest()
  180. end
  181. end
  182. end
  183.  
  184. function goToNextSlice()
  185. print("goToNextSlice")
  186. goToZero()
  187. faceNorth()
  188.  
  189. goUp()
  190. goUp()
  191.  
  192. end
  193.  
  194. function returnToStartPoint()
  195. print("returnToStartPoint")
  196. goToZero()
  197.  
  198. if(currentY <= startingY) then
  199. while(currentY < startingY) do
  200. goUp()
  201. end
  202. else
  203. while(currentY > startingY) do
  204. goDown()
  205. end
  206. end
  207. end
  208.  
  209. function faceNorth()
  210.  
  211. if(currentD == 3) then
  212. turtle.turnRight()
  213. end
  214.  
  215. if(currentD == 2) then
  216. turtle.turnRight()
  217. turtle.turnRight()
  218. end
  219.  
  220. if(currentD == 1) then
  221. turtle.turnLeft()
  222. end
  223.  
  224. currentD = 0
  225.  
  226. end
  227.  
  228. -- Check functions
  229.  
  230. function checkFuelLevel()
  231.  
  232. if(turtle.getFuelLevel() < 80) then
  233. refuelFromEnderChest()
  234. end
  235. end
  236.  
  237. function refuelFromEnderChest()
  238. print("refuelFromEnderChest")
  239. dumpIntoEnderChest()
  240.  
  241. turtle.select(16)
  242. turtle.place()
  243.  
  244. turtle.select(1)
  245. turtle.suck(32)
  246. turtle.refuel(32)
  247.  
  248. if(turtle.getItemCount(1) ~= 0) then
  249. turtle.drop()
  250. end
  251.  
  252. turtle.dig()
  253. turtle.transferTo(16)
  254.  
  255. end
  256.  
  257. function isBlockUseful(data)
  258.  
  259. if(data.name == "minecraft:stone" or data.name == "minecraft:dirt") then
  260. return false
  261. else
  262. return true
  263. end
  264. end
  265.  
  266. function checkAvailableInventory()
  267.  
  268. for i = 1, 14 do
  269. if(turtle.getItemCount(i) == 0) then
  270. return i
  271. end
  272. end
  273.  
  274. return -1
  275.  
  276. end
  277.  
  278. function dumpIntoEnderChest()
  279. print("dumpIntoEnderChest")
  280. turtle.dig()
  281. turtle.select(15)
  282. turtle.place()
  283.  
  284. for i = 1,14 do
  285. turtle.select(i)
  286.  
  287. if(turtle.getItemCount(i) ~= 0) then
  288. done = turtle.drop()
  289. end
  290.  
  291. while(done == false) do
  292. print("enter while")
  293. sleep(10)
  294. done = turtle.drop()
  295. end
  296. done = true
  297. end
  298.  
  299. turtle.select(1)
  300. turtle.dig()
  301. turtle.transferTo(15)
  302.  
  303. end
  304.  
  305. function changeHorizontalState()
  306. if(horizontalState == 0) then
  307. horizontalState = 1
  308. else
  309. horizontalState = 0
  310. end
  311. end
  312. -- Mining function
  313. -- Direction code : Front = 0, Under = 1, Above = 2
  314.  
  315. function mineDirection(direc)
  316.  
  317. if(direc == 0 ) then
  318. succes, data = turtle.inspect()
  319.  
  320. if(succes == true and isBlockUseful(data) == true) then
  321. if(checkAvailableInventory() == -1) then
  322. dumpIntoEnderChest()
  323. end
  324. turtle.dig()
  325. end
  326. end
  327.  
  328. if(direc == 1 ) then
  329. succes, data = turtle.inspectDown()
  330.  
  331. if(succes == true and isBlockUseful(data) == true) then
  332. if(checkAvailableInventory() == -1) then
  333. dumpIntoEnderChest()
  334. end
  335. turtle.digDown()
  336. end
  337. end
  338.  
  339. if(direc == 2 ) then
  340. succes, data = turtle.inspectUp()
  341.  
  342. if(succes == true and isBlockUseful(data) == true) then
  343. if(checkAvailableInventory() == -1) then
  344. dumpIntoEnderChest()
  345. end
  346. turtle.digUp()
  347. end
  348. end
  349. end
  350.  
  351. function mine()
  352. turtle.turnLeft()
  353. mineDirection(0)
  354.  
  355. turtle.turnRight()
  356. mineDirection(0)
  357.  
  358. turtle.turnRight()
  359. mineDirection(0)
  360.  
  361. turtle.turnLeft()
  362. mineDirection(2)
  363.  
  364. end
  365.  
  366. function doVerticalLine()
  367. if(currentD == 0) then
  368. print("toto")
  369. while(currentX < squareSize) do
  370. mine()
  371. goNorth()
  372. end
  373. mine()
  374. goEast()
  375. end
  376.  
  377. if (currentD == 2) then
  378. while(currentX > 0) do
  379. mine()
  380. goSouth()
  381. end
  382. mine()
  383. goEast()
  384. end
  385.  
  386. end
  387.  
  388. function doHorizontalLine()
  389.  
  390. if (currentD == 1) then
  391. for i = 0, 1 do
  392. mine()
  393. goEast()
  394. end
  395. mine()
  396. if(horizontalState == 0 ) then
  397. goSouth()
  398. changeHorizontalState()
  399. else
  400. goNorth()
  401. changeHorizontalState()
  402. end
  403. end
  404.  
  405. end
  406.  
  407. function doSlice()
  408. print("doSlice")
  409.  
  410. printAllData()
  411. while(currentX <= squareSize and currentZ <= squareSize) do
  412. doVerticalLine()
  413. doHorizontalLine()
  414. end
  415. end
  416.  
  417. function mineAll()
  418. goToMiningStart()
  419. doSlice()
  420.  
  421. print("before while")
  422. while(currentY < 30) do
  423. print("entered while")
  424. goToNextSlice()
  425. doSlice()
  426. end
  427.  
  428. dumpIntoEnderChest()
  429. returnToStartPoint()
  430.  
  431. end
  432. -- Main
  433.  
  434.  
  435. local args = { ... }
  436. local canRun = false
  437.  
  438. print("Please input two argument, square size and current turtle height ")
  439.  
  440. if (#args <= 1 or #args > 2) then
  441. print("Please input two argument, square size, current turtle height and turtle facing direction")
  442. elseif(#args == 2) then
  443. print("Detected 2 arguments")
  444. squareSize = tonumber(args[1])
  445. startingY = tonumber(args[2])
  446. currentY = startingY
  447. canRun = true
  448. end
  449.  
  450. if (canRun == true) then
  451. print("Mining started")
  452. mineAll()
  453. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement