mullniaxgr

Untitled

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