Advertisement
nainmagic

computercraft/floor

Jun 30th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | None | 0 0
  1. os.loadAPI("tmnt")
  2.  
  3. local currentSlot = 0
  4. local blockPlaced = 0
  5. local requiredQuantity = 0
  6.  
  7. function turnForNext(x)
  8.     if x % 2 == 0 then
  9.         turtle.turnLeft()
  10.     else
  11.         turtle.turnRight()
  12.     end
  13. end
  14.  
  15. function checkQuantity()
  16.     sum = 0
  17.    
  18.     for i = 1, 16 do
  19.         sum = sum + turtle.getItemCount(i)
  20.     end
  21.    
  22.     local qty = requiredQuantity - blockPlaced
  23.  
  24.     if sum < qty then
  25.         print("Not enough blocks")
  26.         print("Got: "..sum)
  27.         print("Need: "..requiredQuantity)
  28.         tmnt.pause("Please insert some blocks in the turtle")
  29.        
  30.         checkQuantity()
  31.     end
  32. end
  33.  
  34. function findNextBlock()
  35.     currentSlot = currentSlot + 1
  36.  
  37.     while currentSlot <= 16 do
  38.         if turtle.getItemCount(currentSlot) > 0 then
  39.             turtle.select(currentSlot)
  40.             return
  41.         end
  42.  
  43.         currentSlot = currentSlot + 1
  44.     end
  45.  
  46.     checkQuantity()
  47. end
  48.  
  49. function main(width, depth)
  50.     tmnt.checkFuel()
  51.  
  52.     requiredQuantity = width * depth
  53.     checkQuantity()
  54.  
  55.     turtle.select(1)
  56.     currentSlot = 1
  57.  
  58.     tmnt.forceUp()
  59.  
  60.     for x = 1, width do
  61.         for y = 1, depth do
  62.             if turtle.getItemCount(currentSlot) == 0 then
  63.                 findNextBlock()
  64.             end
  65.  
  66.             tmnt.tryPlaceDown()
  67.             blockPlaced = blockPlaced + 1
  68.  
  69.             if y < depth then
  70.                 tmnt.forceForward()
  71.             end
  72.         end
  73.  
  74.         if x < width then
  75.             turnForNext(x)
  76.             tmnt.forceForward()
  77.             turnForNext(x)
  78.         else
  79.             turnForNext(x + 1)
  80.         end
  81.     end
  82. end
  83.  
  84. --------------------
  85. -- Argument parser
  86. --------------------
  87.  
  88. local argv = { ... }
  89.  
  90. if #argv ~= 2 then
  91.     error("Usage: floor <width> <depth>")
  92. end
  93.  
  94. local width = tonumber(argv[1])
  95. local depth = tonumber(argv[2])
  96.  
  97. main(width, depth)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement