Advertisement
QQII

Build Rectangle V0.1

Mar 31st, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Build Rectangle V0.1 by QQII
  2. --Declerations
  3. local Arguments = { ... }       --Program arguments
  4. local length = tonumber(Arguments[1]) or 0
  5. local width = tonumber(Arguments[2]) or length
  6.  
  7. --Functions
  8. local function refuel()
  9.     while turtle.getFuelLevel() <= 0 do
  10.         shell.run("refuel", "all")
  11.         if turtle.getFuelLevel() <= 0 then
  12.             print("ERROR: out of fuel")
  13.             read()
  14.         end
  15.     end
  16.     turtle.select(1)
  17. end
  18.  
  19. local function forward()        --Moves forward
  20.     refuel()
  21.     while not turtle.forward() do
  22.         if not turtle.dig() then
  23.             turtle.attack()
  24.         end
  25.     end
  26. end
  27.  
  28. local function inventory_sort()     --Fills the first slot with items from the other slots
  29.     for i = 2, 16 do
  30.         if turtle.getItemSpace(1) == 0 then
  31.             break
  32.         end
  33.         turtle.select(i)
  34.         turtle.transferTo(1, turtle.getItemCount(i))
  35.     end
  36.     turtle.select(1)
  37. end
  38.  
  39. local function place_down()     --Places a block below the turtle
  40.     while not turtle.detectDown() do
  41.         while not turtle.placeDown() do
  42.             inventory_sort()
  43.             if turtle.getItemCount(1) == 0 then
  44.                 print("ERROR: out of items")
  45.                 read()
  46.             end
  47.         end
  48.     end
  49. end
  50.  
  51. local function line(l)          --Places a line of size l
  52.     for i = 2, l do
  53.         place_down()
  54.         forward()
  55.     end
  56.     place_down()
  57. end
  58.  
  59. --User Inputs
  60. print("The turtle will build a rectangle")
  61. if length <= 0 then
  62.     io.write("Length: ")
  63.     while length <= 0 do
  64.         length = tonumber(read())
  65.     end
  66. else
  67.     print("Length: " .. length)
  68. end
  69. if  width <= 0 then
  70.     io.write("Width: ")
  71.     while width <= 0 do
  72.         width = tonumber(read())
  73.     end
  74. else
  75.     print("Width: " .. width)
  76. end
  77.  
  78. --Main Script
  79. refuel()
  80. for i = 2, width do
  81.     line(length)
  82.     if i % 2 ~= 0 then
  83.         turtle.turnLeft()
  84.         forward()
  85.         turtle.turnLeft()
  86.     elseif i % 2 == 0 then
  87.         turtle.turnRight()
  88.         forward()
  89.         turtle.turnRight()
  90.     end
  91. end
  92. line(length)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement