visiongaming43

Untitled

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