Advertisement
ArsKvsh

[CC] spiral floor builder

Jan 26th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  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 refuel()
  49. local fuelLevel = turtle.getFuelLevel()
  50. if fuelLevel == "unlimited" or fuelLevel > 0 then
  51. return
  52. end
  53.  
  54. local function tryRefuel()
  55. for n=1,16 do
  56. if turtle.getItemCount(n) > 0 then
  57. turtle.select(n)
  58. if turtle.refuel(1) then
  59. turtle.select(1)
  60. return true
  61. end
  62. end
  63. end
  64. turtle.select(1)
  65. return false
  66. end
  67.  
  68. if not tryRefuel() then
  69. print( "Add more fuel to continue." )
  70. while not tryRefuel() do
  71. os.pullEvent( "turtle_inventory" )
  72. end
  73. print( "Resuming floor building..." )
  74. end
  75. end
  76.  
  77. local function charge()
  78. local function tryCharge()
  79. for n=1,16 do
  80. if turtle.getItemCount(n) > 0 then
  81. if turtle.getItemDetail(n).name == material then
  82. turtle.select(n)
  83. return true
  84. end
  85. end
  86. end
  87. turtle.select(2)
  88. return false
  89. end
  90. if not tryCharge() then
  91. print( "Add more material to continue." )
  92. while not tryCharge() do
  93. os.pullEvent( "turtle_inventory" )
  94. end
  95. print( "Resuming floor building..." )
  96. end
  97. end
  98.  
  99. local function tryForward()
  100. refuel()
  101. while not turtle.forward() do
  102. if turtle.detect() then
  103. if not tryDig() then
  104. return false
  105. end
  106. elseif turtle.attack() then
  107. else
  108. sleep( 0.1 )
  109. end
  110. end
  111. return true
  112. end
  113.  
  114. local function tryPlaceDown()
  115. refuel()
  116. charge()
  117. while not turtle.detectDown() do
  118. if turtle.placeDown() then
  119. sleep(0.1)
  120. else
  121. return false
  122. end
  123. end
  124. return true
  125. end
  126.  
  127. print( "Building the floor..." )
  128.  
  129. local function buildForward(dir)
  130. for n=1,dir do
  131. tryForward()
  132. tryPlaceDown()
  133. end
  134. end
  135.  
  136. local frwDir = true;
  137.  
  138. while (length > 0 or width > 0) do
  139. if frwDir == true then
  140. buildForward(length)
  141. width = width - 1
  142. frwDir = false
  143. else
  144. buildForward(width)
  145. length = length- 1
  146. frwDir = true
  147. end
  148. turtle.turnRight()
  149. end
  150.  
  151. print( "Floor building complete" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement