Advertisement
McBaron

Untitled

Jun 29th, 2025 (edited)
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | Gaming | 0 0
  1. -- Turtle Fill: Single-Layer Skip, Chest Behind, Robust Home Tracking
  2.  
  3. -- == Helpers ==
  4. heading = 0
  5. posX, posY, posZ = 0, 0, 0
  6.  
  7. function turnRight()
  8.   turtle.turnRight()
  9.   heading = (heading + 1) % 4
  10. end
  11. function turnLeft()
  12.   turtle.turnLeft()
  13.   heading = (heading + 3) % 4
  14. end
  15. function face(dir)
  16.   local turns = (dir - heading) % 4
  17.   if turns == 1 then turnRight()
  18.   elseif turns == 2 then turnRight() turnRight()
  19.   elseif turns == 3 then turnLeft()
  20.   end
  21. end
  22. function forward()
  23.   while not turtle.forward() do turtle.dig() sleep(0.2) end
  24.   if heading == 0 then posX = posX + 1
  25.   elseif heading == 1 then posZ = posZ + 1
  26.   elseif heading == 2 then posX = posX - 1
  27.   else posZ = posZ - 1 end
  28. end
  29. function moveUp() while not turtle.up() do turtle.digUp() sleep(0.2) end posY = posY + 1 end
  30. function moveDown() while not turtle.down() do turtle.digDown() sleep(0.2) end posY = posY - 1 end
  31.  
  32. function refuelIfNeeded(needed)
  33.   turtle.select(1)
  34.   if turtle.getFuelLevel() == "unlimited" then return true end
  35.   if turtle.getFuelLevel() < needed then
  36.     print("Refueling from slot 1...")
  37.     turtle.refuel(64)
  38.   end
  39.   return turtle.getFuelLevel() >= needed
  40. end
  41.  
  42. function dumpToChest()
  43.   print("Dumping items to chest behind...")
  44.   turnLeft(); turnLeft()
  45.   for i = 2, 16 do
  46.     turtle.select(i)
  47.     local itm = turtle.getItemDetail()
  48.     if itm and itm.name ~= targetBlockName then turtle.drop() end
  49.   end
  50.   turnLeft(); turnLeft()
  51. end
  52.  
  53. function isInventoryFull()
  54.   for i = 3, 16 do if turtle.getItemCount(i) == 0 then return false end end
  55.   return true
  56. end
  57.  
  58. function placeSlot2Block()
  59.   turtle.digDown()
  60.   if turtle.detectDown() then return true end
  61.   for i = 2, 16 do
  62.     turtle.select(i)
  63.     local itm = turtle.getItemDetail()
  64.     if itm and itm.name == targetBlockName then return turtle.placeDown() end
  65.   end
  66.   return "out_of_blocks"
  67. end
  68.  
  69. function goTo(tx, ty, tz, th)
  70.   while posY < ty do moveUp() end
  71.   while posY > ty do moveDown() end
  72.   if posX < tx then face(0) while posX < tx do forward() end
  73.   elseif posX > tx then face(2) while posX > tx do forward() end end
  74.   if posZ < tz then face(1) while posZ < tz do forward() end
  75.   elseif posZ > tz then face(3) while posZ > tz do forward() end end
  76.   face(th)
  77. end
  78.  
  79. -- == Input ==
  80. function askNumber(p)
  81.   print(p)
  82.   local n = tonumber(read())
  83.   while not n do print("Enter a number:") n = tonumber(read()) end
  84.   return math.floor(n)
  85. end
  86.  
  87. function askDirection()
  88.   print("Build 'up' or 'down'?")
  89.   local d = read()
  90.   while d ~= "up" and d ~= "down" do print("Type 'up' or 'down'") d = read() end
  91.   return d
  92. end
  93.  
  94. local length = askNumber("Length?")
  95. local width = askNumber("Width?")
  96. local height = askNumber("Height?")
  97. local direction
  98. if height == 1 then
  99.   direction = "up"
  100.   print("Single layer: defaulting to up.")
  101. else
  102.   direction = askDirection()
  103. end
  104.  
  105. local est = length * width * height * 3 + (height-1)*(length+width)
  106. print("Need fuel:", est)
  107. if not refuelIfNeeded(est) then
  108.   print("Not enough fuel!"); return
  109. end
  110.  
  111. turtle.select(2)
  112. local blk = turtle.getItemDetail()
  113. if not blk then print("Put fill block in slot 2."); return end
  114. targetBlockName = blk.name
  115. print("Using:", targetBlockName)
  116.  
  117. print("Put chest BEHIND turtle before starting.")
  118. forward()
  119.  
  120. function buildLayer()
  121.   for w = 1, width do
  122.     for l = 1, length do
  123.       turtle.dig()
  124.       local pr = placeSlot2Block()
  125.       if pr == "out_of_blocks" then
  126.         goTo(0,0,0,0); dumpToChest()
  127.         print("Out of blocks. Returned home. Refill and rerun.")
  128.         return "stop"
  129.       elseif not pr then return false end
  130.       if isInventoryFull() then
  131.         goTo(0,0,0,0); dumpToChest()
  132.         print("Inventory full. Returned home.")
  133.         return "stop"
  134.       end
  135.       if l < length then forward() end
  136.     end
  137.     if w < width then
  138.       if w%2==1 then turnRight() forward() turnRight()
  139.       else turnLeft() forward() turnLeft()
  140.       end
  141.     end
  142.   end
  143.   goTo(0,posY,0,0)
  144.   return true
  145. end
  146.  
  147. -- == Build Main ==
  148. function build()
  149.   if direction == "up" then
  150.     for h = 1, height do
  151.       print("Layer",h,"/",height)
  152.       local r = buildLayer()
  153.       if r=="stop" then return end
  154.       if h<height then moveUp() end
  155.     end
  156.   else
  157.     for i=1,height-1 do moveUp() end
  158.     for h=height,1,-1 do
  159.       print("Layer",h,"/",height)
  160.       local r = buildLayer()
  161.       if r=="stop" then return end
  162.       if h>1 then moveDown() end
  163.     end
  164.   end
  165.   goTo(0,0,0,0); dumpToChest()
  166.   print("✅ Build complete!")
  167. end
  168.  
  169. build()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement