Advertisement
LazyNub

Lazy Nub Stairs v0.04

May 19th, 2013
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.44 KB | None | 0 0
  1. --[[
  2.     Lazy Nub Stairs v0.04
  3.    
  4.     lava resistant blocks in slot 1 (optional)
  5.     fuel in slot 15 (coal, charcoal, lava bucket so on..)
  6.     torches in slot 16 (optional)
  7.  
  8.     Will build downward straight or spiral single width stair.
  9.    
  10.     It will place torches if you wish and at any
  11.     distance apart you specify.
  12.    
  13.     Suggestion: Torches 7 apart.
  14.    
  15.     Also will ensure there is always a step placed
  16.     but optionally will build 100% enclosed stair well
  17.     in order to traverse lava and water areas safely.
  18.    
  19.     if you select secure building, place lava resistant
  20.     blocks in slot 1.
  21.    
  22.     fuel required is 2 times stair depth for normal stairs
  23.     or 4 times stair depth for secure stairs.
  24.    
  25.     Turtle will run a fuel level check before digging.
  26.     fuel slot is 15
  27.    
  28. ]]--
  29. --[[ Robust Turtle
  30.  Modified version of
  31.  Robust Turtle API by SpeedR
  32.  integrated
  33. ]]--
  34. --Digging with gravel/sand detection
  35. function dig()
  36.   local tries = 0
  37.   while turtle.detect() do
  38.     turtle.dig()
  39.     sleep(0.5)
  40.     tries = tries + 1
  41.     if tries>500 then
  42.       print("Error: dug for too long.")
  43.       return false
  44.     end
  45.   end
  46.   return true
  47. end
  48.  
  49. function digUp()
  50.   local tries = 0
  51.   while turtle.detectUp() do
  52.     turtle.digUp()
  53.     sleep(0.5)
  54.     tries = tries + 1
  55.     if tries>500 then
  56.       print("Error: dug up for too long.")
  57.       return false
  58.     end
  59.   end
  60.   return true
  61. end
  62.  
  63. function digDown()
  64.   local tries = 0
  65.   while turtle.detectDown() do
  66.     turtle.digDown()
  67.     sleep(0.5)
  68.     tries = tries + 1
  69.     if tries>500 then
  70.       print("Error: dug down for too long.")
  71.       return false
  72.     end
  73.   end
  74.   return true
  75. end
  76.  
  77.  
  78. --Traveling: Goes in the direction no matter what (almost)
  79. --Will not be stopped by blocks or mobs
  80. function forward(l)
  81.   l=l or 1
  82.   for i=1,l do
  83.     local tries = 0
  84.     while turtle.forward() ~= true do
  85.       turtle.dig()
  86.       turtle.attack()
  87.       sleep(0.2)
  88.       tries = tries + 1
  89.       if tries>500 then
  90.         print("Error: can't move forward.")
  91.         return false
  92.       end
  93.     end
  94.   end
  95.   return true
  96. end
  97.  
  98. function up(l)
  99.   l=l or 1
  100.   for i=1,l do
  101.     local tries = 0
  102.     while turtle.up() ~= true do
  103.       turtle.digUp()
  104.       turtle.attackUp()
  105.       sleep(0.2)
  106.       tries = tries + 1
  107.       if tries>500 then
  108.         print("Error: can't move up.")
  109.         return false
  110.       end
  111.     end
  112.   end
  113.   return true
  114. end
  115.  
  116. function down(l)
  117.   l=l or 1
  118.   for i=1,l do
  119.     local tries = 0
  120.     while turtle.down() ~= true do
  121.       turtle.digDown()
  122.       turtle.attackDown()
  123.       sleep(0.2)
  124.       tries = tries + 1
  125.       if tries>500 then
  126.         print("Error: can't move down.")
  127.         return false
  128.       end
  129.     end
  130.   end
  131.   return true
  132. end
  133.  
  134. function back(l)
  135.   l=l or 1
  136.   for i=1,l do
  137.     if turtle.back() ~= true then
  138.       turnAround()
  139.       forward()
  140.       turnAround()
  141.     end
  142.   end
  143. end
  144.  
  145.  
  146. --Place blocks
  147. --Does not place when there's already the right block.
  148. function place(block)
  149.   turtle.select(block)
  150.   if turtle.compare()==false then
  151.     if turtle.getItemCount(block)==0 then
  152.       outOfResource(block)
  153.     end
  154.     dig()
  155.     turtle.place()
  156.   end
  157. end
  158.  
  159. function placeUp(block)
  160.   turtle.select(block)
  161.   if turtle.compareUp()==false then
  162.     if turtle.getItemCount(block)==0 then
  163.       outOfResource(block)
  164.     end
  165.     digUp()
  166.     turtle.placeUp()
  167.   end
  168. end
  169.  
  170. function placeDown(block)
  171.   turtle.select(block)
  172.   if turtle.compareDown()==false then
  173.     if turtle.getItemCount(block)==0 then
  174.       outOfResource(block)
  175.     end
  176.     digDown()
  177.     turtle.placeDown()
  178.   end
  179. end
  180.  
  181. local function outOfResource()
  182.   print("Ran out of a resource. Block: ",block , ".")
  183.   print("Refill, then say something to proceed.")
  184.   read()
  185. end
  186.  
  187. function placeRight(block)
  188.   turtle.turnRight()
  189.   place(block)
  190.   turtle.turnLeft()
  191. end
  192.  
  193. function placeLeft(block)
  194.   turtle.turnLeft()
  195.   place(block)
  196.   turtle.turnRight()
  197. end
  198.  
  199. function placeBack(block)
  200.   turnAround()
  201.   place(block)
  202.   turnAround()
  203. end
  204.  
  205. --place row     e.g. placeRow(up, marble, forward, 15)
  206. function placeRow(placeDir, block, travelDir, l)
  207.   l=l or 1
  208.   for i=1,l do
  209.     if placeDir == "forward" then
  210.       place(block)
  211.     elseif placeDir == "up" then
  212.       placeUp(block)
  213.     elseif placeDir == "down" then
  214.       placeDown(block)
  215.     elseif placeDir == "right" then
  216.       placeRight(block)
  217.     elseif placeDir == "left" then
  218.       placeLeft(block)
  219.     elseif placeDir == "back" then
  220.       placeBack(block)
  221.     else
  222.       print('"', placeDir, '" is not a valid direction!')
  223.       return false
  224.     end
  225.     if travelDir == "forward" then
  226.       forward()
  227.     elseif travelDir == "up" then
  228.       up()
  229.     elseif travelDir == "down" then
  230.       down()
  231.     elseif travelDir == "right" then
  232.       strafeRight()
  233.     elseif travelDir == "left" then
  234.       strafeLeft()
  235.     elseif travelDir == "back" then
  236.       back()
  237.     else
  238.       print('"', travelDir, '" is not a valid direction!')
  239.       return false
  240.     end
  241.   end
  242.   return true
  243. end
  244.  
  245.  
  246. --Turning
  247. function turnAround()
  248.   turtle.turnRight()
  249.   turtle.turnRight()
  250. end
  251.  
  252. function right()
  253.   turtle.turnRight()
  254. end
  255.  
  256. function left()
  257.   turtle.turnLeft()
  258. end
  259.  
  260. function goRight(l)
  261.   l=l or 1
  262.   turtle.turnRight()
  263.   forward(l)
  264. end
  265.  
  266. function goLeft(l)
  267.   l=l or 1
  268.   turtle.turnLeft()
  269.   forward(l)
  270. end
  271.  
  272. function strafeRight(l)
  273.   l=l or 1
  274.   goRight(l)
  275.   turtle.turnLeft()
  276. end
  277.  
  278. function strafeLeft(l)
  279.   l=l or 1
  280.   goLeft(l)
  281.   turtle.turnRight()
  282. end
  283. --[[ End of Robust Turtle
  284. ]]--
  285. function getBlock()
  286.     i = 1
  287.     while turtle.getItemCount(i) < 1 and i < 16 do
  288.         i= i+1
  289.     end
  290.    
  291.     return i
  292. end
  293. --[[
  294.     Main
  295. ]]--
  296. local stairLength = 1
  297. local torchDistance = 5
  298. local placeTorch = false
  299. local torchSlot = 16
  300. local running = true
  301. local secureStairs = false
  302. local spiralStairs = false
  303.  
  304. term.clear()
  305. term.setCursorPos (1, 1)
  306. print("Lazy Nub Stairs v0.04")
  307. print("=====================")
  308. print("What length of stair: ")
  309. stairLength = read()
  310. print("Spiral Stairs (y/n): ")
  311. ut = read()
  312. if ut == "y" or ut == "Y" then
  313.     spiralStairs = true
  314. end
  315. print("Use Torches (y/n): ")
  316. ut = read()
  317. if ut == "y" or ut == "Y" then
  318.     placeTorch = true
  319.     print("How Far Apart: ")
  320.     torchDistance = read()
  321.     if (stairLength/torchDistance) > (turtle.getItemCount(torchSlot)) then
  322.         print("Place atleast ".. math.ceil(stairLength/torchDistance).." in slot "..torchSlot.." <ENTER>")
  323.         ut = read()
  324.     end
  325. end
  326. print("Build Securely (y/n): ")
  327. ut = read()
  328. if ut == "y" or ut == "Y" then
  329.     secureStairs = true
  330.     if not turtle.getItemCount(1) then
  331.         print("Place lava resistant blocks in slot 1")
  332.         while not turtle.getItemCount(1) do
  333.         end
  334.     end
  335. end
  336.  
  337. if (not secureStairs and (turtle.getFuelLevel() < (stairLength*2)))
  338.     or (secureStairs and (turtle.getFuelLevel() < (stairLength*4))) then
  339.     if secureStairs then
  340.         print("Need "..(stairLength*4).." fuel")
  341.     else
  342.         print("Need "..(stairLength*2).." fuel")
  343.     end
  344.     print("Fuel Level at: "..turtle.getFuelLevel())
  345.     while (not secureStairs and (turtle.getFuelLevel() < (stairLength*2)))
  346.     or (secureStairs and (turtle.getFuelLevel() < (stairLength*4))) do
  347.         turtle.select(15)
  348.         print ("Place fuel item into slot 15 and press <Enter>")
  349.         ut = read()
  350.         turtle.refuel(1)
  351.         print("Fuel Level at: "..turtle.getFuelLevel())
  352.     end
  353.     turtle.select(1)
  354. end
  355. while running do
  356.     for i = 1,stairLength do
  357.         if spiralStairs and ( i%3 == 0) then  -- may need to add wall check for secure builds
  358.             right()
  359.         end
  360.         forward()
  361.         if placeTorch == true then
  362.             if i%torchDistance ==0 then
  363.                 turtle.select(torchSlot)
  364.                 turnAround()
  365.                 place(torchSlot)
  366.                 turnAround()
  367.             end
  368.         end
  369.         if secureStairs then
  370.             right()
  371.             if not turtle.detect() then
  372.                 place(getBlock())
  373.             end
  374.             turnAround()
  375.             if not turtle.detect() then
  376.                 place(getBlock())
  377.             end
  378.             right()
  379.         end
  380.         digUp()
  381.         if secureStairs then
  382.             up()
  383.             if not turtle.detectUp() then
  384.                 placeUp(getBlock())
  385.             end
  386.             if not turtle.detect() then
  387.                 place(getBlock())
  388.             end
  389.             right()
  390.             if not turtle.detect() then
  391.                 place(getBlock())
  392.             end
  393.             turnAround()
  394.             if not turtle.detect() then
  395.                 place(getBlock())
  396.             end
  397.             right()
  398.             down()
  399.         end
  400.         down()
  401.         if secureStairs then
  402.             right()
  403.             if not turtle.detect() then
  404.                 place(getBlock())
  405.             end
  406.             turnAround()
  407.             if not turtle.detect() then
  408.                 place(getBlock())
  409.             end
  410.             right()
  411.         end
  412.         if not turtle.detectDown() then
  413.             placeDown(getBlock())
  414.         end
  415.        
  416.     end
  417.     running = false
  418. end
  419. print("finished")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement