Advertisement
mullniaxgr

Untitled

Jan 18th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 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. os.sleep(5)
  31. end
  32. end
  33.  
  34. local b, reason = turtle.forward()
  35. if not b then
  36. print("failed,", reason)
  37. return false
  38. end
  39.  
  40. local facing = getFacing()
  41. pos.x = pos.x + facing.x
  42. pos.z = pos.z + facing.z
  43.  
  44. print("coords", pos.x, pos.y, pos.z)
  45. print("fuel", getFuelLevel())
  46.  
  47. return true
  48. end
  49.  
  50. up = function()
  51. if not turtle.up() then
  52. return false
  53. end
  54. pos.y = pos.y + 1
  55.  
  56. return true
  57. end
  58.  
  59. down = function()
  60. if not turtle.down() then
  61. return false
  62. end
  63. pos.y = pos.y - 1
  64.  
  65. return true
  66. end
  67.  
  68. turnLeft = function()
  69. facingIdx = (facingIdx - 1) % 4
  70. turtle.turnLeft()
  71. end
  72.  
  73. turnRight = function()
  74. facingIdx = (facingIdx + 1) % 4
  75. turtle.turnRight()
  76. end
  77.  
  78. local findDirIdx = function(idx)
  79. if facingIdx == idx then
  80. return
  81. end
  82.  
  83. -- created from truth table
  84. if facingIdx-1 == idx or (facingIdx == 0 and idx == 3) then
  85. while facingIdx ~= idx do
  86. turnLeft()
  87. end
  88. else
  89. while facingIdx ~= idx do
  90. turnRight()
  91. end
  92. end
  93. end
  94.  
  95. shouldDeposit = function()
  96. local total = 0
  97. for i = 1, 16 do
  98. if turtle.getItemCount(i) == 0 then
  99. total = total + 1
  100. end
  101. end
  102. return total <= 1
  103. end
  104.  
  105. depositInventoryForward = function()
  106. for i = 3, 16 do
  107. turtle.select(i)
  108. turtle.drop()
  109. end
  110. end
  111.  
  112. goHomeAndDeposit = function()
  113. print("dropping")
  114. turtle.dig()
  115. turtle.select(2)
  116. turtle.place()
  117. depositInventoryForward()
  118. turtle.select(2)
  119. turtle.dig()
  120. turtle.select(1)
  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. -- do it after to not dig towards the wrong location
  158. if preFunc ~= nil then
  159. preFunc()
  160. end
  161.  
  162. if (pos.x ~= x) then
  163. repeat
  164. findDirIdx(desiredXDir)
  165. until pos.x == x or forward() == false
  166. end
  167.  
  168. if (pos.z ~= z) then
  169. repeat
  170. findDirIdx(desiredZDir)
  171. until pos.z == z or forward() == false
  172. end
  173.  
  174. while pos.y < y and up() do end
  175.  
  176. while pos.y > y and down() do end
  177. end
  178. end
  179.  
  180. digRectangle = function(x1, y1, z1, x2, y2, z2)
  181. goToRelativeCoords(x1, y1, z1, quarry)
  182.  
  183. for gy = 0, math.floor(math.abs(y2-y1)/3) do
  184. for gx = 0, x2-x1 do
  185. for pz = 0, z2-z1 do
  186. local gz = pz
  187.  
  188. if gx % 2 == 1 then
  189. gz = z2-z1 - pz
  190. end
  191. goToRelativeCoords(x1+gx, gy * -3, z1+gz, quarry)
  192. end
  193. end
  194. end
  195. end
  196.  
  197. buildFunction = function(w, h, f)
  198. for x = 0, w do
  199. for py = 0, h do
  200. local y = py
  201. if x % 2 == 1 then
  202. y = h - py
  203. end
  204.  
  205. print(math.floor(f(x, y)))
  206. goToRelativeCoords(x, math.floor(f(x, y)), y)
  207. for i = 1, 16 do
  208. turtle.select(i)
  209. if turtle.placeUp() then
  210. break
  211. end
  212. end
  213. end
  214. end
  215. end
  216.  
  217. findDirIdx(0)
  218.  
  219.  
  220. local i = 0
  221. while true do
  222. digRectangle(i,0,i, i+4, -60, i+4)
  223. i = i + 4
  224. end
  225.  
  226. --[[while true do
  227. local func = loadstring(io.read())
  228. setfenv(func, getfenv())
  229. turtle.refuel(1)
  230. func()
  231. goHomeAndDeposit()
  232. end]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement