Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. ------------------------------------------------------------------------------
  2. -- Utility library for computercraft's turtles by Hea3veN
  3. -- Version 0.1
  4. ------------------------------------------------------------------------------
  5. -- Example:
  6. --
  7. -- loadfile("turtle_utils.lua")()
  8. --
  9. -- forward(5)
  10. -- turtle.turnRight()
  11. -- turtle.turnRight()
  12. -- forward(5)
  13. -- down(2)
  14. --
  15. -- Interface:
  16. --
  17. -- fuelFor(movement)
  18. -- Checks if turtle has enough fuel to move *movement* number of times.
  19. -- If it does not, it tries to refuel from the first slot until it has
  20. -- enough.
  21. --
  22. -- dig()
  23. -- digUp()
  24. -- digDown()
  25. -- Digs the block in front/up/down of the turtle until there is only
  26. -- air left. It returns false if it could not dig any more(which is a
  27. -- sign of bedrock), otherwise returns true.
  28. --
  29. -- forward(num)
  30. -- up(num)
  31. -- down(num)
  32. -- Moves the turtle forward/up/down *num* number of times, digging up
  33. -- all the blocks that are in it's way. It returns false if it could
  34. -- not move any more(which is a sign of bedrock), otherwise returns
  35. -- true.
  36. --
  37.  
  38.  
  39. function fuelFor(movement)
  40. if turtle.getFuelLevel() <= (movement + 1) then
  41. turtle.select(1)
  42. print("Refuelling")
  43. while turtle.getFuelLevel() <= (movement + 1) do
  44. turtle.refuel(1)
  45. os.sleep(0.5)
  46. end
  47. end
  48. end
  49.  
  50. function dig()
  51. while turtle.detect() do
  52. if not turtle.dig() then
  53. return false
  54. end
  55. os.sleep(0.5)
  56. end
  57. return true
  58. end
  59.  
  60. function digDown()
  61. while turtle.detectDown() do
  62. if not turtle.digDown() then
  63. return false
  64. end
  65. os.sleep(0.5)
  66. end
  67. return true
  68. end
  69.  
  70. function digUp()
  71. while turtle.detectUp() do
  72. if not turtle.digUp() then
  73. return false
  74. end
  75. os.sleep(0.5)
  76. end
  77. return true
  78. end
  79.  
  80. function forward(num)
  81. fuelFor(num)
  82. for i=1,num do
  83. local moved = false
  84. while not moved do
  85. if not dig() then
  86. return false
  87. end
  88. if turtle.forward() then
  89. moved = true
  90. else
  91. while turtle.attack() do
  92. os.sleep(0.5)
  93. end
  94. end
  95. end
  96. end
  97. return true
  98. end
  99.  
  100. function up(num)
  101. fuelFor(num)
  102. for i=1,num do
  103. local moved = false
  104. while not moved do
  105. if not digUp() then
  106. return false
  107. end
  108. if turtle.up() then
  109. moved = true
  110. else
  111. while turtle.attack() do
  112. os.sleep(0.5)
  113. end
  114. end
  115. end
  116. end
  117. return true
  118. end
  119.  
  120. function down(num)
  121. fuelFor(num)
  122. for i=1,num do
  123. local moved = false
  124. while not moved do
  125. if not digDown() then
  126. return false
  127. end
  128. if turtle.down() then
  129. moved = true
  130. else
  131. while turtle.attack() do
  132. os.sleep(0.5)
  133. end
  134. end
  135. end
  136. end
  137. return true
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement