Advertisement
QQII

Build Square V0.1

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