c3808250

Untitled

Jan 31st, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1. args = {...}
  2.  
  3.  
  4. turtle.refuel()
  5.  
  6.  
  7.  
  8. blocksToIgnore = {"minecraft:air", "minecraft:dirt", "minecraft:grass", "minecraft:cobblestone", "minecraft:gravel", "minecraft:stone"} -- do not mine these blocks
  9. blocksToDrop = {"minecraft:dirt", "minecraft:grass", "minecraft:cobblestone", "minecraft:gravel", "minecraft:stone", "minecraft:quarry"} -- add shitty marble
  10.  
  11.  
  12.  
  13. logOutputFileName = string.format("quarry_log_%s.txt", os.time())
  14.  
  15. currentX = 0
  16. currentY = 0
  17. currentZ = 0
  18. currentOrientation = 0
  19.  
  20. stringCurrentCordinates = string.format("X: %i, Y: %i, Z: %i, D: %i", currentX, currentY, currentZ, currentOrientation)
  21.  
  22. savedX = 0
  23. savedY = 0
  24. savedZ = 0
  25. savedOrientation = 0
  26.  
  27. diameter = args[1] - 1
  28. initialDepth = args[2]
  29.  
  30. print([[
  31. (( /|_/|
  32. \\.._.' , ,\
  33. /\ | '.__ v /
  34. (_ . / "
  35. ) _)._ _ /
  36. '.\ \|( / ( mrf
  37. '' ''\\ \\
  38. ===== NICECAT QUARRY =====
  39. ]])
  40.  
  41. function putMessage(message, level)
  42. -- prints message with level and time to stdout and write to logOutputFile
  43. -- ### add rednet/modem api messaging
  44. -- edit when fancy logging
  45.  
  46. if level == "trace" then
  47. level = "TRACE"
  48. elseif level == "info" then
  49. level = "INFO"
  50. elseif level == "warning" then
  51. level = "WARNING"
  52. elseif level == "error" then
  53. level = "ERROR"
  54. elseif level == "fatal" then
  55. level = "FATAL"
  56. end
  57.  
  58. local formattedMessage = string.format("[%s] [%s] %s", os.time(), level, message)
  59.  
  60. print(formattedMessage)
  61.  
  62. end
  63.  
  64. function checkIfBedrockInFront()
  65. local forwardBlockSuccess, forwardBlockData = turtle.inspect()
  66.  
  67. if forwardBlockData.name == "minecraft:bedrock" then
  68. foundBedrock = true
  69. checkIfTurtleShouldReturn()
  70. end
  71.  
  72.  
  73. end
  74.  
  75. function checkIfBedrockBelow()
  76. local downBlockSuccess, downBlockData = turtle.inspectDown()
  77.  
  78. if downBlockData.name == "minecraft:bedrock" then
  79. foundBedrock = true
  80. checkIfTurtleShouldReturn()
  81. end
  82.  
  83. end
  84.  
  85. function updateOrientation(direction)
  86. if direction == "left" then
  87. currentOrientation = currentOrientation - 1
  88. elseif direction == "right" then
  89. currentOrientation = currentOrientation + 1
  90. end
  91.  
  92. if currentOrientation == 4 then
  93. currentOrientation = 0
  94. elseif currentOrientation == -1 then
  95. currentOrientation = 3
  96. else
  97. putMessage(string.format("Updated orientation [%s] was not in 0-3 range", currentOrientation), "error")
  98. end
  99. end
  100.  
  101. function turnRight(times)
  102. -- turn right [times] times
  103. if type(times) == "nil" then
  104. times = 1
  105. end
  106.  
  107. for i=1,times do
  108. turtle.turnRight()
  109. updateOrientation("right")
  110. end
  111. end
  112.  
  113. function turnLeft(times)
  114. -- turn left [times] times
  115. if type(times) == "nil" then
  116. times = 1
  117. end
  118.  
  119. for i=1,times do
  120. turtle.turnLeft()
  121. updateOrientation("left")
  122. end
  123. end
  124.  
  125. function forward()
  126. checkIfBedrockInFront()
  127. while not turtle.forward() do
  128. turtle.dig()
  129. end
  130. putMessage(string.format("FW L-> X: %i, Y: %i, Z: %i, D: %i", currentX, currentY, currentZ, currentOrientation), "trace")
  131. end
  132.  
  133. function moveForward(blocks)
  134. -- move one block forward and break blocks that are in the way
  135.  
  136. if type(blocks) == "nil" then
  137. blocks = 1
  138. end
  139.  
  140. for i=1,blocks do
  141. forward()
  142. currentZ = currentZ + 1
  143. end
  144. end
  145.  
  146.  
  147.  
  148. function moveRight(blocks, rotateFlag)
  149. -- move [blocks] right and break blocks that are in the way
  150.  
  151. if type(blocks) == "nil" then
  152. blocks = 1
  153. end
  154.  
  155. if rotateFlag ~= "noRotate" then
  156. turnRight()
  157. end
  158.  
  159. for i=1,blocks do
  160. forward()
  161. currentX = currentX + 1
  162. end
  163.  
  164. if rotateFlag ~= "noRotate" then
  165. turnLeft()
  166. end
  167.  
  168. end
  169.  
  170. function moveRightNoRotate(blocks)
  171. -- move [blocks] right and do not rotate (only change coords and call forward())
  172. moveRight(blocks, "noRotate")
  173. end
  174.  
  175.  
  176. function moveLeft(blocks, rotateFlag)
  177. -- move [blocks] left and break blocks that are in the way
  178. if type(blocks) == "nil" then
  179. blocks = 1
  180. end
  181.  
  182. if rotateFlag ~= "noRotate" then
  183. turnLeft()
  184. end
  185.  
  186. for i=1,blocks do
  187. forward()
  188. currentX = currentX - 1
  189. end
  190.  
  191. if rotateFlag ~= "noRotate" then
  192. turnRight()
  193. end
  194.  
  195. end
  196.  
  197. function moveLeftNoRotate(blocks)
  198. moveLeft(blocks, "noRotate")
  199. end
  200.  
  201. function moveBack(blocks)
  202. -- move [blocks] backwards and break blocks that are in the way
  203. if type(blocks) == "nil" then
  204. blocks = 1
  205. end
  206.  
  207. if blocks > 0 then -- stops crazy spinning when x = 0
  208. turnLeft(2)
  209. end
  210.  
  211. for i=1,blocks do
  212. forward()
  213. currentZ = currentZ -1
  214. end
  215.  
  216. if blocks > 0 then
  217. turnRight(2)
  218. end
  219. end
  220.  
  221. function moveUp(blocks)
  222. -- move [blocks] up and break blocks that are in the way
  223. if type(blocks) == "nil" then
  224. blocks = 1
  225. end
  226.  
  227. for i=1,blocks do
  228. while not turtle.up() do
  229. turtle.digUp()
  230. end
  231. currentY = currentY - 1
  232. end
  233. end
  234.  
  235. function moveDown(blocks)
  236. -- move [blocks] down and break blocks that are in the way
  237.  
  238. if type(blocks) == "nil" then
  239. blocks = 1
  240. end
  241.  
  242.  
  243. for i=1,blocks do
  244. checkIfBedrockBelow()
  245. while not turtle.down() do
  246. turtle.digDown()
  247. end
  248.  
  249. currentY = currentY + 1
  250. end
  251. end
  252.  
  253. function digDown()
  254. while turtle.detectDown() do
  255. turtle.digDown()
  256. end
  257. end
  258.  
  259. function digUp()
  260. while turtle.detectUp() do
  261. turtle.digUp()
  262. end
  263. end
  264.  
  265. function dig()
  266. while turtle.detect() do
  267. turtle.digUp()
  268. end
  269. end
  270.  
  271.  
  272. function inventoryFull()
  273. if turtle.getItemCount(16) > 0 then
  274. return true
  275. end
  276. return false
  277. end
  278.  
  279.  
  280. function depositItems()
  281. putMessage("Depositing items in left chest", "info")
  282.  
  283. dropShitItems()
  284. turnLeft()
  285. for slot=1,16 do
  286. turtle.select(slot)
  287. turtle.drop()
  288. end
  289.  
  290. turnRight()
  291.  
  292. turtle.select(1)
  293.  
  294. end
  295.  
  296.  
  297.  
  298. function saveMiningPosition()
  299. savedZ = currentZ
  300. savedY = currentY
  301. savedX = currentX
  302. savedOrientation = currentOrientation
  303. end
  304.  
  305. function returnToStart()
  306. -- moves turtle to the start position
  307.  
  308.  
  309. turnLeft(currentOrientation)
  310. moveBack(currentZ)
  311. moveLeft(currentX)
  312. moveUp(currentY)
  313.  
  314.  
  315. if currentZ == 0 and savedY == 0 and savedX == 0 and savedOrientation == 0 then
  316. putMessage("nice turtle has reached start location", "info")
  317. return true
  318. else
  319. putMessage("FUCK! turtle was not reach start !!! !111", "fatal")
  320. return false
  321. end
  322.  
  323. end
  324.  
  325. function returnToSavedPosition()
  326. moveDown(savedY)
  327. moveRight(savedX)
  328. moveForward(savedZ)
  329. turnRight(savedOrientation)
  330. end
  331.  
  332.  
  333. function hasEnoughFuel()
  334. -- checks to see if turtle is below fuel limit (fuel to return + 10)
  335. local fuelNeededToReturn = currentX + currentY + currentZ
  336. fuelNeededToReturn = fuelNeededToReturn + 10
  337.  
  338. if fuelNeededToReturn > turtle.getFuelLevel() then
  339. putMessage(string.format("Fuel needed to return [%s] is greater than current fuel level [%s]", fuelNeededToReturn, turtle.getFuelLevel()), "warning")
  340. return false
  341. else
  342. return true
  343. end
  344. end
  345.  
  346. function refuelFromChest()
  347. putMessage(string.format("Nice turtle is refuel from chesticle, we are %s fuel", turtle.getFuelLevel()), "info")
  348. turnRight()
  349.  
  350. while turtle.suck(16) and turtle.getFuelLevel() < 10000 do
  351. refuelFromInventory()
  352. end
  353.  
  354. turnLeft()
  355.  
  356. end
  357.  
  358. function refuelFromInventory()
  359. for slot=1,16 do
  360. turtle.select(slot)
  361. turtle.refuel()
  362. end
  363. end
  364.  
  365.  
  366. function checkIfTurtleShouldReturn()
  367. -- checks inventory space, if full then return turtle to start,
  368. if not hasEnoughFuel() then
  369. refuelFromInventory()
  370. end
  371.  
  372. if inventoryFull() then
  373. refuelFromInventory() -- see if can free up space by using mined coal
  374. dropShitItems()
  375. end
  376.  
  377. if not hasEnoughFuel() or inventoryFull() or foundBedrock then
  378. saveMiningPosition()
  379. returnToStart() -- recover from return to start failure
  380. depositItems()
  381. refuelFromChest()
  382.  
  383. if not foundBedrock then
  384. returnToSavedPosition()
  385. else
  386. putMessage("Found bedrock, quitting", "info")
  387. exit()
  388. end
  389. end
  390.  
  391. end
  392.  
  393. function dropShitItems()
  394. --
  395. putMessage("Looking for shitty items", "trace")
  396. for slot=1,16 do
  397. local slotBlockData = turtle.getItemDetail(slot)
  398.  
  399. if slotBlockData ~= nil then
  400. for key, block in pairs(blocksToDrop) do
  401. if block == slotBlockData.name then
  402. putMessage(string.format("Found shitty item (%s), nofear I drop him", block), "info")
  403. turtle.select(slot)
  404. turtle.drop()
  405. end
  406. end
  407. end
  408. end
  409. turtle.select(1)
  410. end
  411.  
  412. function detectAndDig()
  413. local upBlockSuccess, upBlockData = turtle.inspectUp()
  414. local downBlockSuccess, downBlockData = turtle.inspectDown()
  415. local mineUp = true
  416. local mineDown = true
  417.  
  418. for key, value in pairs(blocksToIgnore) do
  419.  
  420.  
  421. if value == upBlockData.name then
  422. mineUp = false
  423. end
  424.  
  425. if value == downBlockData.name then
  426. mineDown = false
  427. end
  428. end
  429.  
  430. if mineDown then
  431. turtle.digDown()
  432. end
  433.  
  434. if mineUp then
  435. turtle.digUp()
  436. end
  437.  
  438. end
  439.  
  440.  
  441. for y=1,100 do
  442.  
  443. if currentY > 0 then
  444. moveDown(3)
  445. else
  446. if initialDepth == nil then
  447. moveDown(2)
  448. else
  449. moveDown(initialDepth)
  450. end
  451. end
  452.  
  453.  
  454.  
  455. for z=1,diameter+1 do
  456.  
  457. if currentX > 0 then
  458. xDirection = "left"
  459. turnLeft()
  460. else
  461. xDirection = "right"
  462. turnRight()
  463.  
  464. end
  465.  
  466. for x=1,diameter do
  467. detectAndDig()
  468.  
  469. if xDirection == "left" then -- changed this from z to currentX
  470. moveLeftNoRotate()
  471. else
  472. moveRightNoRotate()
  473. end
  474.  
  475. checkIfTurtleShouldReturn()
  476.  
  477. putMessage(string.format("iter z=%s, y=%s, x=%s", z, y, x), "trace")
  478. putMessage(string.format("OUT L -> X: %i, Y: %i, Z: %i, D: %i", currentX, currentY, currentZ, currentOrientation), "trace")
  479. putMessage(string.format("Fuel: %s", turtle.getFuelLevel()), "trace")
  480. end
  481.  
  482.  
  483.  
  484. if xDirection == "left" then
  485. turnRight()
  486. else
  487. turnLeft()
  488. end
  489. detectAndDig()
  490.  
  491. if currentZ ~= diameter then
  492. moveForward()
  493. end
  494.  
  495. dropShitItems()
  496. --moveBack(currentZ)
  497. end
  498.  
  499. moveLeft(currentX)
  500. moveBack(currentZ)
  501.  
  502. --[[
  503.  
  504. if currentX == 0 then
  505. turnRight(2)
  506. else
  507. turnLeft(2)
  508. end
  509. --]]
  510. end
  511.  
  512.  
  513.  
  514.  
  515.  
  516. -- messed up with 10: check even numbers
  517.  
  518. print("HE IS COMPLETE")
Add Comment
Please, Sign In to add comment