Advertisement
SemlerPDX

StairsDown

Aug 30th, 2016
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | None | 0 0
  1. --Stairs Down Simple Script v1
  2. --Turtle Program Computer Craft
  3. --by SemlerPDX(July2016)
  4.  
  5. --COMMENT SYNTAX EXPLAINATION
  6. --torches must be in slot 1 (one for every 3rd layer dug)
  7. --stairs can be in slot 2 if desired, see below
  8.  
  9. --StairsDown 20 (number of stairs down) 1 (or blank -- 1 will add stairs from slot 2)
  10. --example:  StairsDown 20 (makes stairs 20 deep with torches but no stair blocks)
  11. --example2: StairsDown 20 1 (makes stairs 20 deep w/ torches and stair blocks)
  12.  
  13.  
  14. -- Variables --
  15. local args = {...}
  16. local d = args[1] or 0
  17. local s = args[2] or 0
  18. local r = 0 + d
  19. local t = turtle
  20. local lit = 2
  21.  
  22. -- F-ACK --
  23. if #args ~= 2 then
  24.   print("<error420: syntax>")
  25.   print("<ex: Stairs 20 1 (starts digging stairs 20 blocks deep with stair blocks in slot 2)>")
  26.   return
  27. end
  28.  
  29. if d == 0 then
  30.   print("<error421: missing length>")
  31.   print("<ex: Stairs 20 (starts digging stairs 20 blocks deep)>")
  32.   return
  33. end
  34.  
  35. -- Functions --
  36. --force forward function by CCCode
  37. local function forceForward()
  38.   while not t.forward() do
  39.     t.dig()
  40.   end
  41. end
  42.  
  43. --dig down, decrement lit variable
  44. local function fDown()
  45.   t.digDown()
  46.   t.down()
  47.   lit = lit - 1
  48. end
  49.  
  50. --shave 4 blocks and place torch if needed
  51. local function fShave()
  52.   for v=1,4 do
  53.     if v == 3 then
  54.         if lit == 0 then
  55.             t.placeUp()
  56.             lit = lit + 2
  57.             forceForward()
  58.          else
  59.             forceForward()
  60.          end
  61.     else
  62.          forceForward()
  63.     end
  64.   end
  65. end
  66.  
  67. --move to next shaving position
  68. local function fReturn()
  69.   for v=1,3 do
  70.     t.back()
  71.   end
  72. end
  73.  
  74. --place stairs while returning home
  75. local function fHomeS()
  76.   t.turnLeft()
  77.   t.turnLeft()
  78.   t.select(2)
  79.   while r ~=0 do
  80.     t.place()
  81.     t.up()
  82.     t.forward()
  83.     r = r -1
  84.   end
  85. end
  86.  
  87. --return home
  88. local function fHome()
  89.   while r ~=0 do
  90.     t.up()
  91.     t.back()
  92.     r = r -1
  93.   end
  94. end
  95.  
  96. --clear display screen
  97. local function fset()
  98.   term.clear()
  99.   term.setCursorPos(1,1)
  100. end
  101.  
  102. -- Pre-fire scripts --
  103. fset()
  104. print("Building staircase "..tostring(d).." blocks down...")
  105. t.digDown()
  106. t.down()
  107. sleep(2)
  108.  
  109. ---- Main Loop ----
  110. while d ~= 0 do
  111.  
  112.   local function fcancel()
  113.     local event, key = os.pullEvent("key")
  114.   end
  115.  
  116.   local function fmain()
  117.     fShave()
  118.     fReturn()
  119.     fDown()
  120.     d = d - 1
  121.   end
  122.  
  123.   FunctionEnabled = parallel.waitForAny(fcancel,fmain)
  124.  
  125.   if FunctionEnabled == 1 then
  126.     textutils.slowPrint("Cancelling Build...", 15)
  127.     sleep(1.65)
  128.     break
  129.   end
  130.  
  131. end
  132.  
  133. -- Evaluate Ending --
  134. if FunctionEnabled == 1 then    --(1 = functions cancelled, 2 = standard function looped)
  135.   fset()
  136.   print("Build Cancelled by user")
  137.   print("...returning to start in 5 seconds - move out of path")
  138.   sleep(6)
  139.   fHome()
  140.   t.select(1)
  141. else
  142.   fset()
  143.   print("Build Completed")
  144.   if s == 1 then
  145.     print("Building stairs and returning home...")
  146.     fHomeS()
  147.     t.select(1)
  148.     fset()
  149.     print("...program complete")
  150.   else
  151.     fset()
  152.     fHome()
  153.     t.select(1)
  154.     print("...program complete")
  155.   end
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement