hhhzzzsss

sparseceil.lua

Aug 26th, 2023 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.98 KB | None | 0 0
  1. if not turtle then
  2.     printError("Requires a Turtle")
  3.     return
  4. end
  5.  
  6. local tArgs = { ... }
  7. if #tArgs ~= 4 then
  8.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  9.     print("Usage: " .. programName .. " <# width> <# length> <interval> <item to place>")
  10.     return
  11. end
  12.  
  13. local width = tonumber(tArgs[1])
  14. local length = tonumber(tArgs[2])
  15. local interval = tonumber(tArgs[3])
  16. local targetItem = tArgs[4]
  17.  
  18. local function refuel(amount)
  19.     local fuelLevel = turtle.getFuelLevel()
  20.     if fuelLevel == "unlimited" then
  21.         return true
  22.     end
  23.  
  24.     turtle.select(16)
  25.     while turtle.getItemCount(16) > 0 and turtle.getFuelLevel() < amount do
  26.         if not turtle.refuel(1) then
  27.             return false
  28.         end
  29.     end
  30.  
  31.     if turtle.getFuelLevel() < amount then
  32.         return false
  33.     else
  34.         return true
  35.     end
  36. end
  37.  
  38. local function waitForFuel(amount)
  39.     if not refuel(amount) then
  40.         print("Please put fuel in slot 16")
  41.         while not refuel(amount) do
  42.             sleep(0.5)
  43.         end
  44.         print("Received sufficient fuel. Continuing...")
  45.     end
  46. end
  47.  
  48. local function selectItem(name)
  49.     for i=15, 1, -1 do
  50.         itemDetail = turtle.getItemDetail(i)
  51.         if itemDetail and itemDetail.name == name then
  52.             turtle.select(i)
  53.             return true
  54.         end
  55.     end
  56.     return false
  57. end
  58.  
  59. local function countItem(name)
  60.     num = 0
  61.     for i=1, 15 do
  62.         itemDetail = turtle.getItemDetail(i)
  63.         if itemDetail and itemDetail.name == name then
  64.             num = num + itemDetail.count
  65.         end
  66.     end
  67.     return num
  68. end
  69.  
  70. local function waitForItem(name, amount)
  71.     if countItem(name) < amount then
  72.         print("Please provide more " .. name)
  73.         while countItem(name) < amount do
  74.             sleep(0.5)
  75.         end
  76.         print("Received sufficient " .. name .. ". Continuing...")
  77.     end
  78.     selectItem(name)
  79. end
  80.  
  81. local function waitForDrop()
  82.     if turtle.getItemCount() > 0 and not turtle.drop() then
  83.         print("Please make room for turtle to drop")
  84.         while turtle.getItemCount() > 0 and not turtle.drop() do
  85.             sleep(0.5)
  86.         end
  87.         print("Turtle successfully dropped. Continuing...")
  88.     end
  89. end
  90.  
  91. local function waitForDropDown()
  92.     if turtle.getItemCount() > 0 and not turtle.dropDown() then
  93.         print("Please make room for turtle to drop")
  94.         while turtle.getItemCount() > 0 and not turtle.dropDown() do
  95.             sleep(0.5)
  96.         end
  97.         print("Turtle successfully dropped. Continuing...")
  98.     end
  99. end
  100.  
  101. local function getBlock()
  102.     local hasBlock, data = turtle.inspect()
  103.     if hasBlock then
  104.         return data.name
  105.     else
  106.         return "minecraft:air"
  107.     end
  108. end
  109.  
  110. local function getBlockDown()
  111.     local hasBlock, data = turtle.inspectDown()
  112.     if hasBlock then
  113.         return data.name
  114.     else
  115.         return "minecraft:air"
  116.     end
  117. end
  118.  
  119. local function getBlockUp()
  120.     local hasBlock, data = turtle.inspectUp()
  121.     if hasBlock then
  122.         return data.name
  123.     else
  124.         return "minecraft:air"
  125.     end
  126. end
  127.  
  128. local function tryForward()
  129.     waitForFuel(1)
  130.     if not turtle.forward() then
  131.         exit()
  132.     end
  133. end
  134.  
  135. local function repeatForward(amount)
  136.     for i=1, amount do
  137.         tryForward()
  138.     end
  139. end
  140.  
  141.  
  142. local function tryMakeCeiling()
  143.     if turtle.detectUp() then
  144.         turtle.digUp()
  145.     end
  146.     waitForItem(targetItem, 1)
  147.     turtle.placeUp()
  148. end
  149.  
  150. waitForFuel(2*width*length*interval)
  151. waitForItem(targetItem, width*length)
  152.  
  153. for i=1, width do
  154.     for j=1, length-1 do        
  155.         tryMakeCeiling()
  156.         repeatForward(interval)
  157.     end
  158.     tryMakeCeiling()
  159.     if i < width then
  160.         if i % 2 == 1 then
  161.             turtle.turnRight()
  162.             repeatForward(interval)
  163.             turtle.turnRight()
  164.         else
  165.             turtle.turnLeft()
  166.             repeatForward(interval)
  167.             turtle.turnLeft()
  168.         end
  169.     end
  170. end
Advertisement
Add Comment
Please, Sign In to add comment