visiongaming43

Untitled

May 22nd, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. GPS_ERROR_MESSAGE = "GPS SYSTEM DOES NOT WORK"
  2. PARAM_ERROR = "INVALID PARAMETER HAS BEEN PASSED"
  3. ACCEPTABLE_COLORS = { colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.gray, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black }
  4. ACCEPTABLE_DIRECTIONS = { "North", "East", "South", "West" }
  5.  
  6. -- One line color print
  7. function printColor(color, text)
  8. for index, value in ipairs(ACCEPTABLE_COLORS) do
  9. if (color == value) then break end
  10. if (index == tableLength(ACCEPTABLE_COLORS) and color ~= value) then
  11. print(text)
  12. return
  13. end
  14. end
  15. term.setTextColor(color)
  16. print(text)
  17. term.setTextColor(colors.white)
  18. end
  19.  
  20. -- Safer and simpler way of getting coordinates in library
  21. function getCoordinates()
  22. local startX, startY, startZ = gps.locate(1)
  23. if (not startX) then
  24. printError(GPS_ERROR_MESSAGE)
  25. else
  26. return startX, startY, startZ
  27. end
  28. end
  29.  
  30. -- While detecting a block in front, keep digging it away
  31. function keepDigging()
  32. while (turtle.detect()) do
  33. turtle.dig()
  34. end
  35. end
  36.  
  37. -- returns the length of the entire table
  38. function tableLength(table)
  39. local count = 0
  40. for _ in pairs(table) do count = count + 1 end
  41. return count
  42. end
  43.  
  44. --
  45. -- Figure out if the turtle is looking at North, South, West, or East
  46. function getDirection()
  47. local direction
  48. local startX, _, startZ = getCoordinates()
  49. keepDigging()
  50. if (turtle.forward()) then
  51. local endX, _, endZ = getCoordinates()
  52. if (startX > endX) then
  53. direction = "West"
  54. elseif (startX < endX) then
  55. direction = "East"
  56. elseif (startZ > endZ) then
  57. direction = "North"
  58. elseif (startZ < endZ) then
  59. direction = "South"
  60. end
  61. turtle.back()
  62. end
  63. return direction
  64. end
  65.  
  66. -- Recalibrates turtle to face North, South, West, or East (but with knowledge of given direction)
  67. function faceDirectionP(directionToFace, facedDirection)
  68. if (facedDirection == directionToFace) then return true end
  69. for key, value in ipairs(ACCEPTABLE_DIRECTIONS) do
  70. if (facedDirection == value) then
  71. facedDirection = key
  72. elseif (directionToFace == value) then
  73. directionToFace = key
  74. end
  75. end
  76. local difference = math.abs(facedDirection - directionToFace)
  77. local command
  78. if (facedDirection < directionToFace) then
  79. command = turtle.turnRight
  80. else
  81. command = turtle.turnLeft
  82. end
  83. while (difference > 0) do
  84. command()
  85. difference = difference - 1
  86. end
  87. end
  88.  
  89. -- Recalibrates turtle to face North, South, West, or East
  90. function faceDirection(directionToFace)
  91. local facing = getDirection()
  92. faceDirectionP(directionToFace, facing)
  93. end
  94.  
  95. -- Moves the turtle to the given coordinates and gives verification at the end
  96. function moveTo(endX, endY, endZ)
  97. local startX, startY, startZ = getCoordinates()
  98. local distX = math.abs(startX - endX)
  99. local distZ = math.abs(startZ - endZ)
  100. local distY = math.abs(startY - endY)
  101. local changeY
  102. local detectorY
  103. local diggerY
  104. if (endY > startY) then
  105. changeY = turtle.up
  106. detectorY = turtle.detectUp
  107. diggerY = turtle.digUp
  108. elseif (endY < startY) then
  109. changeY = turtle.down
  110. detectorY = turtle.detectDown
  111. diggerY = turtle.digDown
  112. end
  113. while (distY > 0) do
  114. changeY()
  115. while (detectorY()) do
  116. diggerY()
  117. end
  118. distY = distY - 1
  119. end
  120. if (endX > startX) then
  121. faceDirection("East")
  122. elseif (endX < startX) then
  123. faceDirection("West")
  124. end
  125. while (distX > 0) do
  126. keepDigging()
  127. turtle.forward()
  128. distX = distX - 1
  129. end
  130. if (endZ > startZ) then
  131. faceDirection("South")
  132. elseif (endZ < startZ) then
  133. faceDirection("North")
  134. end
  135. while (distZ > 0) do
  136. keepDigging()
  137. turtle.forward()
  138. distZ = distZ - 1
  139. end
  140. end
  141.  
  142. -- Goes through the turtles inventory, slots 1-16, refueling as much as it can
  143. function refuelAll()
  144. for slot = 1, 16, 1 do
  145. turtle.select(slot)
  146. turtle.refuel(turtle.getItemCount(slot))
  147. end
  148. end
  149.  
  150. -- Goes through the turtles inventory from slots 1-16 and find the first empty slot...returns -1 if no empty slots
  151. function findEmptySpace()
  152. for slot = 1, 16, 1 do
  153. turtle.select(slot)
  154. if (turtle.getItemDetail() == nil) then
  155. return slot
  156. end
  157. end
  158. return -1
  159. end
  160.  
  161. -- Same as last one, just 1-16 slots...returns -1 if no slot
  162. function findSlot(...)
  163. for slot = 1, 16, 1 do
  164. turtle.select(slot)
  165. local item = turtle.getItemDetail()
  166. if (item ~= nil) then
  167. for v in ipairs(arg) do
  168. if (string.match(item.name, v)) then
  169. return slot
  170. end
  171. end
  172. end
  173. end
  174. return -1
  175. end
  176.  
  177. -- Does the same thing, just 1-16
  178. function findSlots(...)
  179. local listOfSlots = {}
  180. local curIdx = 1
  181. for slot = 1, 16, 1 do
  182. turtle.select(slot)
  183. local item = turtle.getItemDetail()
  184. if (item ~= nil) then
  185. for v in ipairs(arg) do
  186. if (string.match(item.name, v)) then
  187. listOfSlots[curIdx] = slot
  188. curIdx = curIdx + 1
  189. end
  190. end
  191. end
  192. end
  193. return listOfSlots
  194. end
  195.  
  196. -- Reverses the turn in case it is needed for quarrying or navigating a set pattern
  197. function oppositeTurn(turnType)
  198. if (turnType == turtle.turnLeft) then
  199. return turtle.turnRight
  200. elseif (turnType == turtle.turnRight) then
  201. return turtle.turnLeft
  202. else
  203. return error("WAS NOT GIVEN A TURTLE TURN METHOD!")
  204. end
  205. end
  206.  
  207. -- Assume turtle is in backleft corner of Quarry, one block behind.
  208. function quarryP(length, width, height, facedDirection)
  209. local startX, startY, startZ = getCoordinates()
  210. local firstTurn = turtle.turnRight
  211. local changeY
  212. if (height >= 0) then
  213. changeY = turtle.up
  214. else
  215. changeY = turtle.down
  216. endX
  217. keepDigging()
  218. turtle.forward()
  219. for d = height,
  220. for l = 1, length do
  221. firstTurn()
  222. keepDigging()
  223. turtle.forward()
  224. end
  225. end
  226.  
  227. -- Assume turtle is in backleft corner again, one block behind
  228. function quarry(length, width, height)
  229. local facedDirection = getDirection()
  230. quarryP(length, width, height, facedDirection)
  231. end
Add Comment
Please, Sign In to add comment