robathome

Untitled

Nov 4th, 2025
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. -- right_edge_rules.lua
  2.  
  3. -- IDs to match
  4. local IS_DIRT = { ["minecraft:dirt"]=true, ["minecraft:coarse_dirt"]=true }
  5. local IS_BRICKS = { ["minecraft:bricks"]=true }
  6.  
  7. -- inventory
  8. local function selectAny()
  9. for i=1,16 do if turtle.getItemCount(i)>0 then turtle.select(i); return true end end
  10. return false
  11. end
  12.  
  13. -- placement helpers
  14. local function placeDownIfAir()
  15. if turtle.detectDown() then return end
  16. if selectAny() then turtle.placeDown() end
  17. end
  18.  
  19. local function placeBehindIfAir()
  20. turtle.turnRight(); turtle.turnRight()
  21. local ok,_ = turtle.inspect()
  22. if not ok then if selectAny() then turtle.place() end end
  23. turtle.turnRight(); turtle.turnRight()
  24. end
  25.  
  26. -- motion
  27. local function safeForward()
  28. local tries=0
  29. while not turtle.forward() do
  30. if turtle.detect() then turtle.dig() else turtle.attack() end
  31. tries=tries+1; if tries>10 then return false end
  32. sleep(0.1)
  33. end
  34. return true
  35. end
  36.  
  37. -- sensing
  38. local function aheadBlocked()
  39. return turtle.detect()
  40. end
  41.  
  42. local function aboveIsBricks()
  43. local ok,up = turtle.inspectUp()
  44. return ok and IS_BRICKS[up.name] or false
  45. end
  46.  
  47. -- right lane checks:
  48. -- 1) is the ground to the right a dirt tile?
  49. -- 2) is the space to the right at turtle level free to turn into?
  50. local function rightGroundIsDirt()
  51. turtle.turnRight()
  52. local ok,down = turtle.inspectDown()
  53. local isDirt = ok and IS_DIRT[down.name] or false
  54. turtle.turnLeft()
  55. return isDirt
  56. end
  57.  
  58. local function rightSpaceIsAir()
  59. turtle.turnRight()
  60. local blocked = turtle.detect()
  61. turtle.turnLeft()
  62. return not blocked
  63. end
  64.  
  65. -- main step applying your rules
  66. local function step()
  67. -- Rule 1: if the block ahead is not air, stop
  68. if aheadBlocked() then return false end
  69.  
  70. local upBricks = aboveIsBricks()
  71. local rightDirt = rightGroundIsDirt()
  72. local rightAir = rightSpaceIsAir()
  73.  
  74. -- Rule 2: if above is bricks OR right-ground is dirt
  75. if upBricks or rightDirt then
  76. placeDownIfAir()
  77. placeBehindIfAir()
  78. return safeForward()
  79. end
  80.  
  81. -- Rule 3: if above and right are air -> behind, turn right, forward
  82. if (not upBricks) and rightAir then
  83. placeBehindIfAir()
  84. turtle.turnRight()
  85. return safeForward()
  86. end
  87.  
  88. -- Fallback (interprets your unfinished line as "otherwise, go straight"):
  89. -- place below+behind, then forward.
  90. placeDownIfAir()
  91. placeBehindIfAir()
  92. return safeForward()
  93. end
  94.  
  95. -- loop
  96. local function run()
  97. while true do
  98. if not step() then
  99. print("Halt.")
  100. break
  101. end
  102. end
  103. end
  104.  
  105. run()
  106.  
Advertisement
Add Comment
Please, Sign In to add comment