Advertisement
Silasko

Turtle simple dig

Nov 24th, 2020 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. local SLOT_COUNT = 16
  2.  
  3. local width, depth = 10, 10
  4.  
  5. if (#arg == 2) then
  6. width = tonumber(arg[1])
  7. depth = tonumber(arg[2])
  8. else
  9. print('None or Malformed Size Given, Defaulting to 10x10')
  10. end
  11.  
  12. function moveDigForward()
  13. turtle.dig()
  14. turtle.digUp()
  15. turtle.digDown()
  16. turtle.forward()
  17. end
  18.  
  19.  
  20. function refuelTurtle()
  21. for slot = 1, SLOT_COUNT, 1 do
  22. turtle.select(slot)
  23. local item = turtle.getItemDetail(slot)
  24. if (turtle.refuel(2)) then
  25. print("Turtle refueled")
  26. return true
  27. end
  28. end
  29. end
  30.  
  31. function checkFuel()
  32. local fuel = turtle.getFuelLevel()
  33. if(fuel < 20) then
  34. refuelTurtle()
  35. else
  36. print("No fuel needed")
  37. end
  38. end
  39.  
  40. function turnLeft()
  41. turtle.turnLeft()
  42. turtle.dig()
  43. turtle.digUp()
  44. turtle.digDown()
  45. turtle.forward()
  46. turtle.turnLeft()
  47. end
  48.  
  49. function turnRight()
  50. turtle.turnRight()
  51. turtle.dig()
  52. turtle.digUp()
  53. turtle.digDown()
  54. turtle.forward()
  55. turtle.turnRight()
  56. end
  57.  
  58. function returnHome()
  59. if(math.fmod(depth,2) == 0) then
  60. turtle.turnRight()
  61. for i = 1, depth, 1 do
  62. if(turtle.detect() == true) then
  63. turtle.dig()
  64. turtle.forward()
  65. else
  66. turtle.forward()
  67. end
  68. end
  69. else --depth not divisible by 2
  70. turtle.turnLeft()
  71. for i = 1, depth, 1 do
  72. if(turtle.detect() == true) then
  73. turtle.dig()
  74. turtle.forward()
  75. else
  76. turtle.forward()
  77. end
  78. end
  79. turtle.turnRight()
  80. for i = 1, width, 1 do
  81. if(turtle.detect() == true) then
  82. turtle.dig()
  83. turtle.forward()
  84. else
  85. turtle.forward()
  86. end
  87. end
  88. end
  89. print("Return home completed")
  90. listen()
  91. end
  92.  
  93. function listen()
  94. rednet.open("left")
  95. while true do
  96. local sender, message, protocol = rednet.receive()
  97. if message == "ping" then
  98. broadcastGPS()
  99. end
  100. end
  101. end
  102.  
  103. function broadcastGPS()
  104. rednet.open("left")
  105. local x,y,z = gps.locate()
  106. rednet.broadcast("gps: "..x.." "..y.." "..z)
  107. end
  108.  
  109.  
  110.  
  111. DROPPED_ITEMS = {
  112. "minecraft:stone",
  113. "minecraft:dirt",
  114. "minecraft:cobblestone",
  115. "minecraft:sand",
  116. "minecraft:gravel"
  117. }
  118.  
  119. function purgeCrap()
  120. for slot = 1, SLOT_COUNT, 1 do
  121. turtle.select(slot)
  122. local item = turtle.getItemDetail(slot)
  123. if (item ~= nil ) then
  124. for itemIndex = 1, #DROPPED_ITEMS, 1 do
  125. if(item["name"] == DROPPED_ITEMS[itemIndex]) then
  126. turtle.dropDown()
  127. print("Purging ",item["name"])
  128. end
  129. end
  130.  
  131.  
  132. end
  133. end
  134. end
  135.  
  136. function getEnderIndex()
  137. for slot = 1, SLOT_COUNT, 1 do
  138. local item = turtle.getItemDetail(slot)
  139. if(item ~= nil) then
  140. if(item["name"] == "enderstorage:ender_storage") then
  141. return slot
  142. end
  143. end
  144. end
  145. return nil
  146. end
  147.  
  148. function restackInventory()
  149. local index = getEnderIndex()
  150. turtle.select(index)
  151. while (turtle.detectUp()) do
  152. turtle.digUp()
  153. sleep(1)
  154. end
  155. turtle.placeUp()
  156. for slot = 1, SLOT_COUNT, 1 do
  157. turtle.select(slot)
  158. local item = turtle.getItemDetail(slot)
  159. if(item ~= nil) then
  160. if(item["name"] ~= "minecraft:coal") then
  161. turtle.dropUp()
  162. end
  163. end
  164. end
  165. turtle.digUp()
  166. end
  167.  
  168. function start()
  169. checkFuel()
  170. for col = 1, depth, 1 do
  171. for row = 1, width, 1 do
  172. moveDigForward()
  173. print(string.format("position: column %d row %d ",col,row))
  174. end
  175. --start of each col
  176. checkFuel()
  177. purgeCrap()
  178. restackInventory()
  179. if(math.fmod(col,2) == 0) then
  180. turnRight()
  181. else
  182. turnLeft()
  183. end
  184.  
  185. end
  186. returnHome()
  187. end
  188.  
  189. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement