kreezxil

Turtlescript Stairs Command

Jul 23rd, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. --[[
  2. 7/1/2014 Created Stairs
  3. Logic borrowed from http://turtlescripts.com/project/gjdh4w-Staircases
  4. - by johncena1469
  5. Inspiration: everyone who has built turtle staircases
  6. --]]
  7.  
  8. function pauseAnyKey()
  9. -- if event == "key" and p1 == keys.q then
  10. while true do
  11. local event, p1,p2,p3 = os.pullEvent()
  12. if event == "key" then
  13. return p1
  14. end
  15. sleep(0.5)
  16. end
  17. end
  18.  
  19. function refuel()
  20. --Check fuel and refuel if nessesary
  21. --Code source: sethbling http://www.youtube.com/watch?v=DSsx4VSe-Uk&feature=share&list=SP2Qvl4gaBge02Eh4AqtDSWg3sojt3jeRO
  22.  
  23. while turtle.getFuelLevel() < 200 do
  24. turtle.select(16)
  25. if turtle.getItemCount() < 1 then
  26. print("Turtle is out of fuel, please add fuel to slot 16")
  27. print("then press any key to continue")
  28. pauseAnyKey()
  29. end
  30. turtle.refuel(1)
  31. end
  32. end
  33.  
  34. function nextInventory()
  35. for i=1,15 do
  36. turtle.select(i)
  37. if turtle.getItemCount() > 0 then
  38. return i
  39. end
  40. end
  41.  
  42. return false
  43. end
  44.  
  45. function getInventory()
  46. slot = nextInventory()
  47. if slot == false then
  48. print("Out of inventory")
  49. print("Please reload inventory")
  50. print("Then press any key")
  51. pauseAnyKey()
  52. slot = nextInventory()
  53. if slot == false then
  54. print("Looks like you've given up!")
  55. return
  56. end
  57. end
  58. turtle.select(slot)
  59. end
  60.  
  61. function digFront()
  62. while turtle.detect() do
  63. turtle.dig()
  64. turtle.attack()
  65. end
  66. end
  67.  
  68. function digUp()
  69. while turtle.detectUp() do
  70. turtle.digUp()
  71. turtle.attackUp()
  72. end
  73. end
  74.  
  75. function digDown()
  76. while turtle.detectDown() do
  77. turtle.digDown()
  78. turtle.attackDown()
  79. end
  80. end
  81.  
  82. function putDown()
  83. if not turtle.detectDown() then
  84. turtle.placeDown()
  85. end
  86. end
  87.  
  88. function buildDown(height)
  89. refuel()
  90.  
  91. for i=1, height do
  92. digFront()
  93. turtle.forward()
  94. digUp()
  95. digDown()
  96. turtle.up()
  97. digUp()
  98. turtle.down()
  99. turtle.down()
  100. getInventory()
  101. putDown()
  102. end
  103. for i=1, height do
  104. digUp()
  105. turtle.up()
  106. turtle.back()
  107. getInventory()
  108. putDown()
  109. end
  110. end
  111.  
  112. function buildUp(height)
  113. refuel()
  114.  
  115. for i=1, height do
  116. digFront()
  117. digUp()
  118. getInventory()
  119. turtle.up()
  120. turtle.forward()
  121. putDown()
  122. end
  123.  
  124. for i=1, height do
  125. turtle.back()
  126. digDown()
  127. turtle.down()
  128. getInventory()
  129. putDown()
  130. end
  131.  
  132. end
  133.  
  134. args = { ... }
  135.  
  136. if #args ~= 2 then
  137. print("Usage:")
  138. print("")
  139. print(" stairs <up|down> <height>")
  140. return
  141. end
  142.  
  143. test = string.sub( args[1], 1, 1)
  144. test = string.lower( test )
  145.  
  146. if string.find("du",test) == nil then
  147. print("Direction must be one of 'up' or 'down'")
  148. return
  149. end
  150.  
  151. direction = test
  152.  
  153. h = tonumber(args[2])
  154.  
  155. if h == nil or h < 0 then
  156. print("Height must be a real number higher than 0")
  157. return
  158. end
  159.  
  160. if refuel == false then
  161. print("The turtle needs fuel, please put fuel in slot 16")
  162. return
  163. end
  164.  
  165. if direction == "u" then
  166. buildUp(h)
  167. else
  168. buildDown(h)
  169. end
Add Comment
Please, Sign In to add comment