Advertisement
Cool_Dalek

chunker

Jan 15th, 2023
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.94 KB | None | 0 0
  1. local function copyVec(vec)
  2.     return vector.new(vec.x, vec.y, vec.z)
  3. end
  4. local function updateX(pos, update)
  5.     return vector.new(update(pos.x), pos.y, pos.z)
  6. end
  7. local function updateZ(pos, update)
  8.     return vector.new(pos.x, pos.y, update(pos.z))
  9. end
  10. local function updateY(pos, update)
  11.     return vector.new(pos.x, update(pos.y), pos.z)
  12. end
  13. local function incr(x)
  14.     return x + 1
  15. end
  16. local function decr(x)
  17.     return x - 1
  18. end
  19. local function eqVec(f, s)
  20.     return f.x == s.x and f.y == s.y and f.z == s.z
  21. end
  22.  
  23. local N = 0
  24. local S = 1
  25. local W = 2
  26. local E = 3
  27.  
  28. local function oppositeFace(f)
  29.     if f == N then return S end
  30.     if f == S then return N end
  31.     if f == W then return E end
  32.     if f == E then return W end
  33. end
  34.  
  35. local function faceTo(c, t)
  36.     if (c == N and t == E) or (c == E and t == S) or (c == S and t == W) or (c == W and t == N) then
  37.         turtle.turnRight()
  38.     elseif (c == N and t == W) or (c == W and t == S) or (c == S and t == E) or (c == E and t == N) then
  39.         turtle.turnLeft()
  40.     elseif (c == N and t == S) or (c == S and t == N) or (c == W and t == E) or (c == E and t == W) then
  41.         turtle.turnRight()
  42.         turtle.turnRight()
  43.     end
  44. end
  45.  
  46. local function newMove(m, l, f)
  47.     return {move = m, loc = l, face = f}
  48. end
  49.  
  50. local RTurn = 0
  51. local LTurn = 1
  52. local FwdMv = 2
  53. local DwnMv = 3
  54.  
  55. local function tern(x, y, z)
  56.     if x then return y else return z end
  57. end
  58.    
  59. local function next(pos, face)
  60.     local x = pos.x
  61.     local y = pos.y
  62.     local z = pos.z
  63.     local r2l = y % 2 == 0
  64.     local evenRow = z % 2 == 0
  65.     local d2t = tern(r2l, evenRow, not evenRow)
  66.     local rowEnd = tern(d2t, x == 15, x == 0)
  67.     local lastRow = tern(r2l, z == 15, z == 0)
  68.     local chunkEnd = rowEnd and lastRow
  69.     if chunkEnd then
  70.         local loc = updateY(pos, decr)
  71.         return newMove(DwnMv, loc, oppositeFace(face))
  72.     elseif rowEnd then
  73.         local turn = tern(d2t ~= r2l, RTurn, LTurn)
  74.         local update = tern(r2l, incr, decr)
  75.         local loc = updateZ(pos, update)
  76.         return newMove(turn, loc, oppositeFace(face))
  77.     else
  78.         local update = tern(d2t, incr, decr)
  79.         local loc = updateX(pos, update)
  80.         return newMove(FwdMv, loc, face)
  81.     end
  82. end
  83.  
  84. local function newStep(d, f, l)
  85.     return {dir = d, face = f, loc = l}
  86. end
  87.  
  88. local StepUp = 0
  89. local StepDwn = 1
  90. local StepFwd = 2
  91. local Arrived = 3
  92.  
  93. local function pathTo(curr, face, trgt)
  94.    
  95.     local function flat()
  96.         if curr.x ~= trgt.x then
  97.             if curr.x < trgt.x then
  98.                 return newStep(StepFwd, N, updateX(curr, incr))
  99.             else
  100.                 return newStep(StepFwd, S, updateX(curr, decr))
  101.             end
  102.         elseif curr.z ~= trgt.z then
  103.             if curr.z < trgt.z then
  104.                 return newStep(StepFwd, W, updateZ(curr, incr))
  105.             else
  106.                 return newStep(StepFwd, E, updateZ(curr, decr))
  107.             end
  108.         else return Arrived end
  109.     end
  110.    
  111.     if curr.y < trgt.y then
  112.         return newStep(StepUp, face, updateY(curr, incr))
  113.     elseif curr.y > trgt.y then
  114.         local res = flat()
  115.         if res == Arrived then
  116.             return newStep(StepDwn, face, updateY(curr, decr))
  117.         else return res end
  118.     else return flat() end
  119. end
  120.  
  121. local function tryRefuel(furthest)
  122.     local x = math.abs(furthest.x)
  123.     local y = math.abs(furthest.y)
  124.     local z = math.abs(furthest.z)
  125.     local path = x + y + z
  126.     local cost = path * 2
  127.     local enough = false
  128.     local level = turtle.getFuelLevel()
  129.     if cost > level then
  130.         local consume = turtle.getItemCount(1) - 1
  131.         if consume > 0 then
  132.             turtle.select(1)
  133.             turtle.refuel(consume)
  134.             enough = cost < turtle.getFuelLevel()
  135.         end
  136.     else enough = true end
  137.     return enough
  138. end
  139.  
  140. local function destination()
  141.     local absolute = vector.new(gps.locate())
  142.     if absolute.y < -57 then
  143.         error("position is not vaild")
  144.     else
  145.         local y = -(absolute.y + 57)
  146.         return vector.new(16, y, 16)
  147.     end
  148. end
  149.  
  150. local finish = destination()
  151. local start = vector.new(0, 0, 0)
  152. local location = vector.new(0, 0, 0)
  153. local face = N
  154.  
  155. local function moveTo(target)
  156.     local loop = true
  157.     while loop do
  158.         local step = pathTo(location, face, target)
  159.         if step == Arrived then
  160.             loop = false
  161.         else
  162.             location = step.loc
  163.             faceTo(face, step.face)
  164.             face = step.face
  165.             local direction = step.dir
  166.             if direction == StepUp then
  167.                 turtle.up()
  168.             elseif direction == StepDwn then
  169.                 turtle.down()
  170.             elseif direction == StepFwd then
  171.                 turtle.forward()
  172.             end
  173.         end
  174.     end
  175. end
  176.  
  177. local function unload()
  178.     moveTo(start)
  179.     faceTo(face, S)
  180.     face = S
  181.     for i=2,16 do
  182.         turtle.select(i)
  183.         turtle.drop()
  184.     end
  185. end
  186.  
  187. if not tryRefuel(finish) then
  188.     error("not enough fuel")
  189. end
  190. while not eqVec(location, finish) do
  191.     local nextMove = next(location, face)
  192.     location = nextMove.loc
  193.     face = nextMove.face
  194.     local move = nextMove.move
  195.     if move == RTurn then
  196.         turtle.turnRight()
  197.         turtle.dig()
  198.         turtle.forward()
  199.         turtle.turnRight()
  200.     elseif move == LTurn then
  201.         turtle.turnLeft()
  202.         turtle.dig()
  203.         turtle.forward()
  204.         turtle.turnLeft()
  205.     elseif move == FwdMv then
  206.         turtle.dig()
  207.         turtle.forward()
  208.     elseif move == DwnMv then
  209.         turtle.digDown()
  210.         turtle.down()
  211.         turtle.turnLeft()
  212.         turtle.turnLeft()
  213.     end
  214.     tryRefuel(location)
  215.     if turtle.getItemCount(16) > 0 then
  216.         local contPos = copyVec(location)
  217.         local contFace = face
  218.         unload()
  219.         turtle.select(1)
  220.         moveTo(contPos)
  221.         faceTo(face, contFace)
  222.         face = contFace
  223.     end
  224. end
  225. unload()
  226.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement