Advertisement
charonme

stairup

Apr 8th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. -- stairup
  2. -- builds covered stairs up until it runs out of material or hits an obstacle
  3. -- stair (bottom) material in slots 1-2 (eg. cobblestone steps/stairs)
  4. -- WARNING! Don't use the stairs block in computercraft prior 1.5.1, placing stairs does not work there properly!
  5. -- walls and ceiling in slots 3-16 (eg. cobblestone)
  6.  
  7.  
  8. local posX = 0
  9. local height = 0
  10. local stepCount = 0
  11. local orient = 1 -- 1 forward, -1 back
  12. function selectFromTo(fromSlot,toSlot)
  13.     local slotIndex = fromSlot
  14.     local result = false
  15.     turtle.select(slotIndex)
  16.     while (turtle.getItemCount(slotIndex)==0) and (slotIndex < toSlot) do
  17.         slotIndex = slotIndex + 1
  18.         turtle.select(slotIndex)
  19.     end
  20.     if turtle.getItemCount(slotIndex)>0 then
  21.         result = true
  22.     end
  23.     if not result then print("select from "..fromSlot.." failed") end
  24.     return result
  25. end
  26. function selectStair()
  27.     return selectFromTo(1,2)
  28. end
  29.  
  30. function selectWall()
  31.     return selectFromTo(3,16)
  32. end
  33.  
  34. function forward()
  35.     local result = false
  36.     if turtle.forward() then
  37.         result = true
  38.         posX = posX + orient
  39.     end
  40.     if not result then print("forward failed") end
  41.     return result
  42. end
  43. function back()
  44.     local result = false
  45.     if turtle.back() then
  46.         result = true
  47.         posX = posX - orient
  48.     end
  49.     if not result then print("back failed") end
  50.     return result
  51. end
  52. function up()
  53.     local result = false
  54.     if turtle.up() then
  55.         result = true
  56.         height = height + 1
  57.     end
  58.     if not result then print("up failed") end
  59.     return result
  60. end
  61. function down()
  62.     local result = false
  63.     if turtle.down() then
  64.         result = true
  65.         height = height - 1
  66.     end
  67.     if not result then print("down failed") end
  68.     return result
  69. end
  70.  
  71. function stepUp()
  72.     local result = true
  73.     result = selectStair()
  74.     if result then
  75.         result = turtle.place()
  76.         selectWall()
  77.         turtle.turnLeft()
  78.         turtle.place()
  79.         turtle.turnRight()
  80.         turtle.turnRight()
  81.         turtle.place()
  82.         if result then
  83.             result = up()
  84.             if turtle.detectUp() then result = false end
  85.             if result then
  86.                 turtle.place()
  87.                 turtle.turnLeft()
  88.                 turtle.turnLeft()
  89.                 turtle.place()
  90.                 turtle.turnRight()
  91.                 if result then result = forward() end
  92.             else
  93.                 turtle.turnLeft()
  94.             end
  95.         else
  96.             turtle.turnLeft()
  97.         end
  98.     end
  99.     return result
  100. end
  101.  
  102. function isAlignedDown()
  103.     local result = false
  104.     if height == (posX + 2) then
  105.         result = true
  106.     end
  107.     return result
  108. end
  109. function compensateOffset()
  110.     while (not isAlignedDown()) and (posX>0) and (height>1) do
  111.         if not isAlignedDown() then forward() end
  112.         if not isAlignedDown() then forward() end
  113.         if not isAlignedDown() then down() end
  114.         if not isAlignedDown() then forward() end
  115.         if not isAlignedDown() then forward() end
  116.         while (not isAlignedDown()) and (posX+2 < height) and (posX>0) and (height>1) and back() do end
  117.     end
  118.     if not isAlignedDown() then return false end
  119.     return true
  120. end
  121.  
  122. function stepDown()
  123.     selectWall()
  124.     turtle.placeUp()
  125.     turtle.turnLeft()
  126.     turtle.place()
  127.     turtle.turnRight()
  128.     turtle.turnRight()
  129.     turtle.place()
  130.     turtle.turnLeft()
  131.     down()
  132.     forward()
  133. end
  134.  
  135.  
  136. local buildUp = true
  137. while buildUp do
  138.     buildUp = stepUp()
  139.     os.sleep(0.3)
  140.     stepCount = stepCount + 1
  141.     if turtle.detectUp() or turtle.detect() then
  142.         buildUp = false
  143.     end
  144. end
  145. turtle.turnLeft()
  146. turtle.turnLeft()
  147. orient = -1
  148. if not compensateOffset() then
  149.     print("can't compensate offset, something's wrong, exiting")
  150.     return false
  151. end
  152. -- build ceiling on the way back
  153. for i = 2,height do
  154.     stepDown()
  155. end
  156. print("built "..stepCount.." steps")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement