itsmartin

Floor

Jan 4th, 2013
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.58 KB | None | 0 0
  1. -- Floor by itsmartin
  2. -- Lays a floor in the layer below the turtle.
  3. -- Fill the inventory with blocks, and put fuel in slot 16. The turtle will place blocks below itself
  4. -- and change directions whenever it hits a wall.
  5.  
  6. function place(slot)
  7.     -- If there is already a block below, do nothing
  8.     if turtle.detectDown() then
  9.         return slot
  10.     end
  11.    
  12.     -- Find a slot containing blocks. Slot 16 is fuel.
  13.     local slotsTried = 0
  14.     while turtle.getItemCount(slot) == 0 do
  15.         slot = slot + 1
  16.         if slot > 15 then
  17.             slot = 1
  18.         end
  19.         slotsTried = slotsTried + 1
  20.         if slotsTried > 15 then
  21.             print ("Out of blocks!")
  22.             error()
  23.         end
  24.     end
  25.    
  26.     -- Place a block
  27.     turtle.select(slot)
  28.     turtle.placeDown()
  29.    
  30.     -- Return the new slot
  31.     return slot
  32. end
  33.  
  34. function turn(direction)
  35.     if direction then
  36.         turtle.turnRight()
  37.     else
  38.         turtle.turnLeft()
  39.     end
  40. end
  41.  
  42. function moveForward()
  43.     if not checkFuel() then
  44.         print "Out of fuel"
  45.         error()
  46.     end
  47.     return turtle.forward()
  48. end
  49.  
  50. function checkFuel()
  51.     if turtle.getFuelLevel() == 0 then
  52.         turtle.select(16)
  53.         turtle.refuel(1)
  54.         if turtle.getFuelLevel() == 0 then
  55.             return false
  56.         end
  57.     end
  58.     return true
  59. end
  60.  
  61.  
  62. local direction = true -- true = moving forward, turning right; false = moving backward, turning left
  63.  
  64. local slot = 1 -- Start with slot 1
  65.  
  66. local finished = false
  67.  
  68. while not finished do
  69.     slot = place(slot) -- Place first block
  70.  
  71.     while moveForward() do
  72.         slot = place(slot)
  73.     end
  74.  
  75.     turn(direction)
  76.    
  77.     if not moveForward() then
  78.         finished = true
  79.     end
  80.  
  81.     turn(direction)
  82.  
  83.     direction = not direction -- Change direction
  84. end
Advertisement
Add Comment
Please, Sign In to add comment