Advertisement
mullniaxgr

Untitled

Jan 17th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. local facingDirs = {{x=0, z=1}, {x=-1, z=0}, {x=0, z=-1}, {x=1, z=0}}
  2. local facingIdx = 0
  3. pos = {x=0, y=0, z=0}
  4.  
  5. local directions = { forward=0 }
  6.  
  7. local getFacing = function()
  8. return facingDirs[facingIdx + 1]
  9. end
  10.  
  11. getFuelLevel = function()
  12. return (turtle.getItemCount(1)-1) * 80 + turtle.getFuelLevel()
  13. end
  14.  
  15. needFuel = function() return getFuelLevel() <= (math.abs(pos.x) + math.abs(pos.y) + math.abs(pos.z) + 40) or turtle.getFuelLevel() <= 0 end
  16.  
  17. --[[
  18. ########
  19. movement
  20. ########
  21. ]]
  22.  
  23. forward = function()
  24. while needFuel() do
  25. if turtle.getItemCount(1) >= 1 then
  26. turtle.select(1)
  27. turtle.refuel(1)
  28. else
  29. print("i need fuel! ", getFuelLevel())
  30. goToRelativeCoords(0, 0, 0)
  31. os.sleep(3)
  32. end
  33. end
  34.  
  35. local b, reason = turtle.forward()
  36. if not b then
  37. print("failed,", reason)
  38. return false
  39. end
  40.  
  41. local facing = getFacing()
  42. pos.x = pos.x + facing.x
  43. pos.z = pos.z + facing.z
  44.  
  45. print("coords", pos.x, pos.y, pos.z)
  46. print("fuel", getFuelLevel())
  47.  
  48. return true
  49. end
  50.  
  51. up = function()
  52. if not turtle.up() then
  53. return false
  54. end
  55. pos.y = pos.y + 1
  56.  
  57. return true
  58. end
  59.  
  60. down = function()
  61. if not turtle.down() then
  62. return false
  63. end
  64. pos.y = pos.y - 1
  65.  
  66. return true
  67. end
  68.  
  69. turnLeft = function()
  70. facingIdx = (facingIdx - 1) % 4
  71. turtle.turnLeft()
  72. end
  73.  
  74. turnRight = function()
  75. facingIdx = (facingIdx + 1) % 4
  76. turtle.turnRight()
  77. end
  78.  
  79. local findDirIdx = function(idx)
  80. if facingIdx == idx then
  81. return
  82. end
  83.  
  84. -- created from truth table
  85. if facingIdx-1 == idx or (facingIdx == 0 and idx == 3) then
  86. while facingIdx ~= idx do
  87. turnLeft()
  88. end
  89. else
  90. while facingIdx ~= idx do
  91. turnRight()
  92. end
  93. end
  94. end
  95.  
  96. shouldDeposit = function()
  97. local total = 0
  98. for i = 1, 16 do
  99. if turtle.getItemCount(i) == 0 then
  100. total = total + 1
  101. end
  102. end
  103. return total <= 1
  104. end
  105.  
  106. depositInventoryForward = function()
  107. for i = 3, 16 do
  108. turtle.select(i)
  109. turtle.drop()
  110. end
  111. end
  112.  
  113. goHomeAndDeposit = function()
  114. print("dropping")
  115. turtle.dig()
  116. turtle.select(2)
  117. turtle.place()
  118. depositInventoryForward()
  119. turtle.select(2)
  120. turtle.dig()
  121. end
  122.  
  123. --[[
  124. #########
  125. dig types
  126. #########
  127. ]]
  128.  
  129. manageInventorySpace = function()
  130. if shouldDeposit() then
  131. goHomeAndDeposit()
  132. end
  133.  
  134. return true
  135. end
  136.  
  137. fullDig = function()
  138.  
  139. turtle.dig()
  140. turtle.digUp()
  141. turtle.digDown()
  142.  
  143. return true
  144. end
  145.  
  146. quarry = function()
  147. return manageInventorySpace() and fullDig()
  148. end
  149.  
  150. goToRelativeCoords = function(x, y, z, preFunc)
  151.  
  152. local desiredXDir = pos.x > x and 1 or 3
  153. local desiredZDir = pos.z > z and 2 or 0
  154.  
  155. -- could minimize turning by looping actions until failure
  156. while pos.x ~= x or pos.y ~= y or pos.z ~= z do
  157. if pos.x ~= x then
  158. findDirIdx(desiredXDir)
  159. elseif pos.z ~= z then
  160. findDirIdx(desiredZDir)
  161. end
  162.  
  163. -- do it after to not dig towards the wrong location
  164. if preFunc ~= nil then
  165. preFunc()
  166. end
  167.  
  168. while pos.x ~= x and forward() do end
  169. while pos.z ~= z and forward() do end
  170.  
  171. while pos.y < y and up() do end
  172.  
  173. while pos.y > y and down() do end
  174. end
  175. end
  176.  
  177. digRectangle = function(x1, y1, z1, x2, y2, z2)
  178. goToRelativeCoords(x1, y1, z1)
  179.  
  180. for gy = 0, math.floor(math.abs(y2-y1)/3) do
  181. for gx = 0, x2-x1 do
  182. for pz = 0, z2-z1 do
  183. local gz = pz
  184.  
  185. if gx % 2 == 1 then
  186. gz = z2-z1 - pz
  187. end
  188. goToRelativeCoords(x1+gx, gy * -3, z1+gz, quarry)
  189. end
  190. end
  191. end
  192. end
  193.  
  194. buildFunction = function(w, h, f)
  195. for x = 0, w do
  196. for py = 0, h do
  197. local y = py
  198. if x % 2 == 1 then
  199. y = h - py
  200. end
  201.  
  202. print(math.floor(f(x, y)))
  203. goToRelativeCoords(x, math.floor(f(x, y)), y)
  204. for i = 1, 16 do
  205. turtle.select(i)
  206. if turtle.placeUp() then
  207. break
  208. end
  209. end
  210. end
  211. end
  212. end
  213.  
  214. findDirIdx(0)
  215. digRectangle(0,0,0,24, 0, 24)
  216.  
  217. --[[
  218. local i = 0
  219. while true do
  220. digRectangle(i,0,i, i+8, -60, i+8)
  221. i = i + 1
  222. end
  223. while true do
  224. local func = loadstring(io.read())
  225. setfenv(func, getfenv())
  226. turtle.refuel(1)
  227. func()
  228. goHomeAndDeposit()
  229. end]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement