Advertisement
ArsKvsh

[CC] spiral ceiling builder (replace)

Mar 2nd, 2020
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if not turtle then
  2. printError( "Requires a Turtle" )
  3. return
  4. end
  5.  
  6. local tArgs = { ... }
  7. if #tArgs ~= 3 then
  8. print( "Usage: sfb <length> <width> <material>" )
  9. return
  10. end
  11.  
  12. -- Mine in a quarry pattern until we hit something we can't dig
  13. local length = tonumber( tArgs[1] )
  14. local width = tonumber( tArgs[2] )
  15. local material = tArgs[3]
  16.  
  17. if length < 1 then
  18. print( "Floor length must be natural number" )
  19. return
  20. end
  21.  
  22. if width < 1 then
  23. print( "Floor width must be natural number" )
  24. return
  25. end
  26.  
  27. local depth = 0
  28. local item = 1
  29.  
  30. local function count()
  31. length = length + 1
  32. if math.fmod(length, 10) == 0 then
  33. print( "Faced "..length.." section." )
  34. end
  35. end
  36.  
  37. local function tryDig()
  38. while turtle.detect() do
  39. if turtle.dig() then
  40. sleep(0.1)
  41. else
  42. return false
  43. end
  44. end
  45. return true
  46. end
  47.  
  48. local function tryDigUp()
  49. while turtle.detectUp() do
  50. if turtle.digUp() then
  51. sleep(0.1)
  52. else
  53. return false
  54. end
  55. end
  56. return true
  57. end
  58.  
  59. local function refuel()
  60. local fuelLevel = turtle.getFuelLevel()
  61. if fuelLevel == "unlimited" or fuelLevel > 0 then
  62. return
  63. end
  64.  
  65. local function tryRefuel()
  66. for n=1,16 do
  67. if turtle.getItemCount(n) > 0 then
  68. turtle.select(n)
  69. if turtle.refuel(1) then
  70. turtle.select(1)
  71. return true
  72. end
  73. end
  74. end
  75. turtle.select(1)
  76. return false
  77. end
  78.  
  79. if not tryRefuel() then
  80. print( "Add more fuel to continue." )
  81. while not tryRefuel() do
  82. os.pullEvent( "turtle_inventory" )
  83. end
  84. print( "Resuming floor building..." )
  85. end
  86. end
  87.  
  88. local function charge()
  89. local function tryCharge()
  90. for n=1,16 do
  91. if turtle.getItemCount(n) > 0 then
  92. if turtle.getItemDetail(n).name == material then
  93. turtle.select(n)
  94. return true
  95. end
  96. end
  97. end
  98. turtle.select(2)
  99. return false
  100. end
  101. if not tryCharge() then
  102. print( "Add more material to continue." )
  103. while not tryCharge() do
  104. os.pullEvent( "turtle_inventory" )
  105. end
  106. print( "Resuming floor building..." )
  107. end
  108. end
  109.  
  110. local function tryForward()
  111. refuel()
  112. while not turtle.forward() do
  113. if turtle.detect() then
  114. if not tryDig() then
  115. return false
  116. end
  117. elseif turtle.attack() then
  118. else
  119. sleep( 0.1 )
  120. end
  121. end
  122. return true
  123. end
  124.  
  125. local function tryPlaceUp()
  126. refuel()
  127. charge()
  128. while not turtle.detectUp() do
  129. if turtle.placeUp() then
  130. sleep(0.1)
  131. else
  132. return false
  133. end
  134. end
  135. return true
  136. end
  137.  
  138. print( "Building the floor..." )
  139.  
  140. local function buildForward(dir)
  141. for n=1,dir do
  142. tryForward()
  143. tryDigUp()
  144. tryPlaceUp()
  145. end
  146. end
  147.  
  148. local frwDir = true;
  149.  
  150. while (length > 0 or width > 0) do
  151. if frwDir == true then
  152. buildForward(length)
  153. width = width - 1
  154. frwDir = false
  155. else
  156. buildForward(width)
  157. length = length- 1
  158. frwDir = true
  159. end
  160. turtle.turnRight()
  161. end
  162.  
  163. print( "Floor building complete" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement