hhhzzzsss

ceiling.lua

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