Advertisement
tike

UneMineV2

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