Advertisement
posicat

Track.lua

Jul 30th, 2022 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. local distance
  2. local defaultBlockUnderneith
  3.  
  4. -- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  5. function refuel()
  6. -- This beast is designed to run on coal
  7. if turtle.getFuelLevel() <= 20 then
  8. turtle.select(1)
  9. return turtle.refuel(1)
  10. end
  11. return true
  12. end
  13.  
  14. -- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  15. function findBlockOrWait(name)
  16. local b=0
  17. local warn=true
  18. while ( b == 0 ) do
  19. b = findBlockOrWaitOrFail(name)
  20.  
  21. if (b>0) then
  22. return b
  23. end
  24.  
  25. if warn then
  26. print("Could not find " .. name .. "! Please insert to continue.")
  27. warn = false
  28. end
  29. sleep(2)
  30. end
  31. end
  32.  
  33. -- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  34. function findBlockOrWaitOrFail(name)
  35. local s = 1
  36. repeat
  37. local d = turtle.getItemDetail(s)
  38. if ( d ) then
  39. if (d.name == name) then
  40. return s
  41. end
  42. end
  43. s = s + 1
  44. until s > 16
  45. return 0
  46. end
  47. -- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  48. function placeBlockDownSafely(item)
  49.  
  50. local invSlot = findBlockOrWait(item)
  51. turtle.select(invSlot)
  52.  
  53. local placedOk = turtle.placeDown()
  54. --print("Placed ok : " .. tostring(placedOk) .. " : " .. item)
  55.  
  56. if not placedOk then
  57. -- If we didn't place a block, try to stick a "default" block underneith where we are
  58. turtle.down()
  59.  
  60. local repSlot = findBlockOrWait(defaultBlockUnderneith.name)
  61. turtle.select(repSlot)
  62. turtle.placeDown()
  63.  
  64. turtle.up()
  65.  
  66. -- Try to place again.
  67. turtle.select(invSlot)
  68. placedOk = turtle.placeDown(invSlot)
  69. --print("Second try Placed ok : " .. tostring(placedOk) .. " : " .. item)
  70. end
  71. return placedOk
  72. end
  73. -- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  74. function placeBlockMoveForward(item,number)
  75.  
  76. local a=number
  77. while a>0 and distance > 1 do
  78. a = a - 1
  79. placeBlockDownSafely(item)
  80. turtle.forward()
  81. distance = distance - 1
  82. print ("Distance remaining : " .. distance)
  83. end
  84. end
  85.  
  86. -- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  87. function placeRedstoneTorch()
  88. turtle.down()
  89. turtle.digDown()
  90. turtle.down()
  91. turtle.digDown()
  92. placeBlockDownSafely("minecraft:redstone_torch")
  93. turtle.up()
  94. placeBlockDownSafely(defaultBlockUnderneith.name)
  95. turtle.up()
  96. end
  97.  
  98. -- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  99. function beginPlacingTrack()
  100.  
  101. repeat
  102. placeRedstoneTorch()
  103. placeBlockMoveForward("minecraft:golden_rail",1)
  104. placeBlockMoveForward("minecraft:rail",6)
  105. turtle.up()
  106. placeBlockDownSafely(defaultBlockUnderneith.name)
  107. turtle.up()
  108. end
  109.  
  110. -- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  111. function beginPlacingTrack()
  112.  
  113. repeat
  114. placeRedstoneTorch()
  115. placeBlockMoveForward("minecraft:golden_rail",1)
  116. placeBlockMoveForward("minecraft:rail",6)
  117. refuel()
  118. until distance <= 1
  119.  
  120. turtle.down()
  121. end
  122. -- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  123.  
  124. local tArgs = { ... }
  125. if #tArgs ~= 1 then
  126. print( "Usage: track <distance>" )
  127. return
  128. end
  129.  
  130. distance = tonumber( tArgs[1] )
  131.  
  132. print("Posi's Trackbuilder ... Use CTRL+T to terminate.")
  133.  
  134. refuel()
  135.  
  136. -- Find what block we're sitting on, that's our default for any supports/etc/
  137. local success
  138. local d
  139.  
  140. success,defaultBlockUnderneith = turtle.inspectDown()
  141.  
  142. if (not success) then
  143. print "Must be placed on a block to start."
  144. return
  145. end
  146.  
  147. -- We need to be one block up in the air to place track
  148. while (success) do
  149. turtle.up()
  150. success,d = turtle.inspectDown()
  151. end
  152.  
  153. beginPlacingTrack()
  154.  
  155. -- Done
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement