yosomith

TurtleRoadBuilder

Feb 9th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 1 then
  3. print( "Usage: road <length>" )
  4. return
  5. end
  6.  
  7. local length = tonumber( tArgs[1] )
  8. if length < 1 then
  9. print( "Road length must be positive" )
  10. return
  11. end
  12. -- initialize starting position
  13. local x = 0
  14. local y = 0
  15.  
  16. function clear()
  17. term.clear()
  18. term.setCursorPos(1,1)
  19. end
  20.  
  21. local function Dig()
  22. while turtle.detect() do
  23. turtle.dig()
  24. sleep(0.5)
  25. end
  26. end
  27.  
  28. local function DigDown()
  29. while turtle.detectDown() do
  30. turtle.digDown()
  31. sleep(0.5)
  32. end
  33. end
  34.  
  35. local function DigUp()
  36. while turtle.detectUp() do
  37. turtle.digUp()
  38. end
  39. end
  40.  
  41. local function refuel()
  42. local fuelLevel = turtle.getFuelLevel()
  43. if fuelLevel == "unlimited" or fuelLevel > 5 then
  44. return
  45. end
  46. local function tryRefuel()
  47. for i = 1 , 16, 1 do
  48. if turtle.getItemCount(i) > 0 then
  49. turtle.select(i)
  50. if turtle.refuel(1) then
  51. return true
  52. end
  53. end
  54. end
  55. return false
  56. end
  57.  
  58. if not tryRefuel() then
  59. print( "Add more fuel to continue." )
  60. while not tryRefuel() do
  61. sleep(1)
  62. end
  63. end
  64. end
  65.  
  66. local function Up()
  67. refuel()
  68. while not turtle.up() do
  69. if turtle.detectUp() then
  70. DigUp()
  71. else
  72. turtle.attackUp()
  73. sleep(0.5)
  74. end
  75. end
  76. end
  77.  
  78. local function Down()
  79. refuel()
  80. while not turtle.down() do
  81. if turtle.detectDown() then
  82. DigDown()
  83. else
  84. turtle.attackDown()
  85. sleep(0.5)
  86. end
  87. end
  88. end
  89.  
  90. local function Forward()
  91. refuel()
  92. while not turtle.forward() do
  93. if turtle.detect() then
  94. Dig()
  95. else
  96. turtle.attack()
  97. sleep(0.5)
  98. end
  99. end
  100. end
  101.  
  102. local function Back()
  103. refuel()
  104. if not turtle.back() then
  105. turtle.turnLeft()
  106. turtle.turnLeft()
  107. Forward()
  108. turtle.turnRight()
  109. turtle.turnRight()
  110. end
  111. end
  112.  
  113. cslot = 1
  114.  
  115. function placeBlock()
  116.  
  117. if turtle.getItemCount(cslot) == 0 then
  118. foundSlot = false
  119. while not foundSlot do
  120. for i = 9,15 do
  121. if turtle.getItemCount(i) > 0 then
  122. foundSlot = i
  123. break
  124. end
  125. end
  126. if not foundSlot then
  127. -- No resources
  128. print("Out of building materials. Please refill and press enter to continue.")
  129. io.read()
  130. end
  131. end
  132. cslot = foundSlot
  133. turtle.select(foundSlot)
  134. end
  135.  
  136. turtle.placeDown()
  137. end
  138.  
  139. local function Road()
  140. turtle.select(1)
  141. Dig()
  142. Forward()
  143. DigDown()
  144. placeBlock()
  145. end
  146.  
  147. local function roadway()
  148. for i = 1, length, 1 do
  149. Road()
  150. end
  151. x = x + 1
  152. unload()
  153.  
  154. end
  155. x = 0
  156. y = 0
  157.  
  158. local function start()
  159. print("Add fuel in last slot")
  160. sleep(4)
  161. refuel()
  162. print("Starting Road")
  163. end
  164.  
  165. clear()
  166. start()
  167. roadway()
Advertisement
Add Comment
Please, Sign In to add comment