M4thG33k

Turtle Construction

Jun 16th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.97 KB | None | 0 0
  1. h = fs.open("text","r")
  2.  
  3. local args = {...}
  4.  
  5. local myChar = args[1]
  6. local height = 0
  7. local ascending = true
  8.  
  9. --reset slot selection to 1
  10. turtle.select(1)
  11.  
  12. --checks to see if it has 2 or more blocks. if it only has one, it will back away from construction until it has been refilled
  13. function checkInventory()
  14.     --checks the current slot
  15.     local amount = turtle.getItemCount()
  16.     if amount>=2 then
  17.         return
  18.     end
  19.    
  20.     if (turtle.getSelectedSlot()==16) or (turtle.getItemCount(turtle.getSelectedSlot()+1)==0) then
  21.         turtle.back()
  22.         turtle.select(1)
  23.         --once it backs up, it sleeps five seconds at a time and then rechecks if it has been filled
  24.         while turtle.getItemCount()<=1 do
  25.             sleep(5)
  26.         end
  27.         --tries to move forward again
  28.         while (turtle.detect() == true) do
  29.             sleep(1)
  30.         end
  31.         turtle.forward()
  32.         --ready to continue
  33.         return
  34.     end
  35.    
  36.     if (amount==0) then
  37.         turtle.select(turtle.getSelectedSlot()+1)
  38.     end
  39. end
  40.  
  41. --tries to move up. if it fails, it waits 5 seconds then tries again.
  42. function up()
  43.     while (turtle.up()==false) do
  44.         sleep(5)
  45.     end
  46.     height = height + 1
  47. end
  48.  
  49. --tries to move down. if it fails, it waits 5 seconds then tries again.
  50. function down()
  51.     if height==0 then
  52.         return
  53.     end
  54.     while (turtle.down()==false) do
  55.         sleep(5)
  56.     end
  57.     height = height-1
  58. end
  59.  
  60. --performs the turn at the top/bottom of a column
  61. function turn()
  62.     turtle.turnRight()
  63.     while (turtle.forward() == false) do
  64.         sleep(5)
  65.     end
  66.     turtle.turnLeft()
  67.     down()
  68. end
  69.  
  70.  
  71. checkInventory()
  72. line = h.readLine()
  73.  
  74. while ((line ~= "") and (line ~= nil)) do
  75.     for i=1,string.len(line) do
  76.         checkInventory()
  77.         if (not turtle.detect()) and (string.sub(line,i,i) == myChar) then
  78.             turtle.place()
  79.         end
  80.         --move then check to see if it has enough resources to continue
  81.         if (ascending==true) then
  82.             up()
  83.         else
  84.             down()
  85.         end
  86.     end
  87.    
  88.     --the column is complete, so we perform a turn
  89.     turn()
  90.     line = h.readLine()
  91.     ascending = not ascending
  92. end
  93.  
  94. turtle.back()
  95. turtle.back()
Advertisement
Add Comment
Please, Sign In to add comment